1//===- CodeExtractor.cpp - Pull code region into a new function -----------===// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7//===----------------------------------------------------------------------===// 9// This file implements the interface to tear out a code region, such as an 10// individual loop or a parallel section, into a new function, replacing it with 11// a call to the new function. 13//===----------------------------------------------------------------------===// 73#define DEBUG_TYPE "code-extractor" 75// Provide a command-line option to aggregate function arguments into a struct 76// for functions produced by the code extractor. This is useful when converting 77// extracted functions to pthread-based code, as only one argument (void*) can 78// be passed in to pthread_create(). 81cl::desc(
"Aggregate arguments to code-extracted functions"));
83/// Test whether a block is valid for extraction. 86bool AllowVarArgs,
bool AllowAlloca) {
87// taking the address of a basic block moved to another function is illegal 91// don't hoist code that uses another basicblock address, as it's likely to 92// lead to unexpected behavior, like cross-function jumps 99while (!ToVisit.
empty()) {
101if (!Visited.
insert(Curr).second)
103if (isa<BlockAddress const>(Curr))
104returnfalse;
// even a reference to self is likely to be not compatible 106if (isa<Instruction>(Curr) && cast<Instruction>(Curr)->
getParent() != &BB)
109for (
autoconst &U : Curr->
operands()) {
110if (
auto *UU = dyn_cast<User>(U))
115// If explicitly requested, allow vastart and alloca. For invoke instructions 116// verify that extraction is valid. 118if (isa<AllocaInst>(
I)) {
124if (
constauto *
II = dyn_cast<InvokeInst>(
I)) {
125// Unwind destination (either a landingpad, catchswitch, or cleanuppad) 126// must be a part of the subgraph which is being extracted. 127if (
auto *UBB =
II->getUnwindDest())
128if (!Result.count(UBB))
133// All catch handlers of a catchswitch instruction as well as the unwind 134// destination must be in the subgraph. 135if (
constauto *CSI = dyn_cast<CatchSwitchInst>(
I)) {
136if (
auto *UBB = CSI->getUnwindDest())
137if (!Result.count(UBB))
139for (
constauto *HBB : CSI->handlers())
140if (!Result.count(
const_cast<BasicBlock*
>(HBB)))
145// Make sure that entire catch handler is within subgraph. It is sufficient 146// to check that catch return's block is in the list. 147if (
constauto *CPI = dyn_cast<CatchPadInst>(
I)) {
148for (
constauto *U : CPI->users())
149if (
constauto *CRI = dyn_cast<CatchReturnInst>(U))
150if (!Result.count(
const_cast<BasicBlock*
>(CRI->getParent())))
155// And do similar checks for cleanup handler - the entire handler must be 156// in subgraph which is going to be extracted. For cleanup return should 157// additionally check that the unwind destination is also in the subgraph. 158if (
constauto *CPI = dyn_cast<CleanupPadInst>(
I)) {
159for (
constauto *U : CPI->users())
160if (
constauto *CRI = dyn_cast<CleanupReturnInst>(U))
161if (!Result.count(
const_cast<BasicBlock*
>(CRI->getParent())))
165if (
constauto *CRI = dyn_cast<CleanupReturnInst>(
I)) {
166if (
auto *UBB = CRI->getUnwindDest())
167if (!Result.count(UBB))
172if (
constCallInst *CI = dyn_cast<CallInst>(
I)) {
173if (
constFunction *
F = CI->getCalledFunction()) {
174auto IID =
F->getIntrinsicID();
175if (IID == Intrinsic::vastart) {
182// Currently, we miscompile outlined copies of eh_typid_for. There are 183// proposals for fixing this in llvm.org/PR39545. 184if (IID == Intrinsic::eh_typeid_for)
193/// Build a set of blocks to extract if the input blocks are viable. 196bool AllowVarArgs,
bool AllowAlloca) {
197assert(!BBs.
empty() &&
"The set of blocks to extract must be non-empty");
200// Loop over the blocks, adding them to our set-vector, and aborting with an 201// empty set if we encounter invalid blocks. 203// If this block is dead, don't process it. 207if (!Result.insert(BB))
211LLVM_DEBUG(
dbgs() <<
"Region front block: " << Result.front()->getName()
214for (
auto *BB : Result) {
218// Make sure that the first block is not a landing pad. 219if (BB == Result.front()) {
221LLVM_DEBUG(
dbgs() <<
"The first block cannot be an unwind block\n");
227// All blocks other than the first must not have predecessors outside of 228// the subgraph which is being extracted. 230if (!Result.count(PBB)) {
231LLVM_DEBUG(
dbgs() <<
"No blocks in this region may have entries from " 232"outside the region except for the first block!\n" 233 <<
"Problematic source BB: " << BB->getName() <<
"\n" 234 <<
"Problematic destination BB: " << PBB->getName()
246bool AllowVarArgs,
bool AllowAlloca,
247BasicBlock *AllocationBlock, std::string Suffix,
248bool ArgsInZeroAddressSpace)
250 BPI(BPI), AC(AC), AllocationBlock(AllocationBlock),
251 AllowVarArgs(AllowVarArgs),
253 Suffix(Suffix), ArgsInZeroAddressSpace(ArgsInZeroAddressSpace) {}
255/// definedInRegion - Return true if the specified value is defined in the 259if (
Blocks.count(
I->getParent()))
264/// definedInCaller - Return true if the specified value is defined in the 265/// function being code extracted, but not in the region being extracted. 266/// These values must be passed in as live-ins to the function. 268if (isa<Argument>(V))
returntrue;
270if (!
Blocks.count(
I->getParent()))
279// Internal edges, ok. 282if (!CommonExitBlock) {
283 CommonExitBlock = Succ;
286if (CommonExitBlock != Succ)
295return CommonExitBlock;
301if (
auto *AI = dyn_cast<AllocaInst>(&
II))
302 Allocas.push_back(AI);
304 findSideEffectInfoForBlock(BB);
308void CodeExtractorAnalysisCache::findSideEffectInfoForBlock(
BasicBlock &BB) {
310unsigned Opcode =
II.getOpcode();
311Value *MemAddr =
nullptr;
313case Instruction::Store:
314case Instruction::Load: {
315if (Opcode == Instruction::Store) {
317 MemAddr = SI->getPointerOperand();
322// Global variable can not be aliased with locals. 323if (isa<Constant>(MemAddr))
326if (!isa<AllocaInst>(
Base)) {
327 SideEffectingBlocks.insert(&BB);
330 BaseMemAddrs[&BB].insert(
Base);
338 SideEffectingBlocks.insert(&BB);
341// Treat all the other cases conservatively if it has side effects. 342if (
II.mayHaveSideEffects()) {
343 SideEffectingBlocks.insert(&BB);
353if (SideEffectingBlocks.count(&BB))
355auto It = BaseMemAddrs.find(&BB);
356if (It != BaseMemAddrs.end())
357return It->second.count(
Addr);
363AllocaInst *AI = cast<AllocaInst>(
Addr->stripInBoundsConstantOffsets());
366if (Blocks.count(&BB))
376BasicBlock *SinglePredFromOutlineRegion =
nullptr;
377assert(!Blocks.count(CommonExitBlock) &&
378"Expect a block outside the region!");
380if (!Blocks.count(Pred))
382if (!SinglePredFromOutlineRegion) {
383 SinglePredFromOutlineRegion = Pred;
384 }
elseif (SinglePredFromOutlineRegion != Pred) {
385 SinglePredFromOutlineRegion =
nullptr;
390if (SinglePredFromOutlineRegion)
391return SinglePredFromOutlineRegion;
397while (
I != BB->
end()) {
408// If there are any phi nodes, the single pred either exists or has already 409// be created before code extraction. 410assert(!getFirstPHI(CommonExitBlock) &&
"Phi not expected");
418if (Blocks.count(Pred))
422// Now add the old exit block to the outline region. 423 Blocks.insert(CommonExitBlock);
424return CommonExitBlock;
427// Find the pair of life time markers for address 'Addr' that are either 428// defined inside the outline region or can legally be shrinkwrapped into the 429// outline region. If there are not other untracked uses of the address, return 430// the pair of markers if found; otherwise return a pair of nullptr. 431CodeExtractor::LifetimeMarkerInfo
435 LifetimeMarkerInfo
Info;
440// We don't model addresses with multiple start/end markers, but the 441// markers do not need to be in the region. 445Info.LifeStart = IntrInst;
451Info.LifeEnd = IntrInst;
454// At this point, permit debug uses outside of the region. 455// This is fixed in a later call to fixupDebugInfoPostExtraction(). 456if (isa<DbgInfoIntrinsic>(IntrInst))
459// Find untracked uses of the address, bail. 464if (!
Info.LifeStart || !
Info.LifeEnd)
470if ((
Info.SinkLifeStart ||
Info.HoistLifeEnd) &&
474// Check to see if we have a place to do hoisting, if not, bail. 475if (
Info.HoistLifeEnd && !ExitBlock)
487auto moveOrIgnoreLifetimeMarkers =
488 [&](
const LifetimeMarkerInfo &LMI) ->
bool {
491if (LMI.SinkLifeStart) {
494 SinkCands.
insert(LMI.LifeStart);
496if (LMI.HoistLifeEnd) {
497LLVM_DEBUG(
dbgs() <<
"Hoisting lifetime.end: " << *LMI.LifeEnd <<
"\n");
498 HoistCands.
insert(LMI.LifeEnd);
503// Look up allocas in the original function in CodeExtractorAnalysisCache, as 504// this is much faster than walking all the instructions. 510// As a prior call to extractCodeRegion() may have shrinkwrapped the alloca, 511// check whether it is actually still in the original function. 516 LifetimeMarkerInfo MarkerInfo = getLifetimeMarkers(CEAC, AI, ExitBlock);
517bool Moved = moveOrIgnoreLifetimeMarkers(MarkerInfo);
524// Find bitcasts in the outlined region that have lifetime marker users 525// outside that region. Replace the lifetime marker use with an 526// outside region bitcast to avoid unnecessary alloca/reload instructions 527// and extra lifetime markers. 529for (
User *U : AI->users()) {
533if (U->stripInBoundsConstantOffsets() != AI)
537for (
User *BU : Bitcast->users()) {
549 << *Bitcast <<
" in out-of-region lifetime marker " 550 << *IntrInst <<
"\n");
551 LifetimeBitcastUsers.
push_back(IntrInst);
561I->replaceUsesOfWith(
I->getOperand(1), CastI);
564// Follow any bitcasts. 567for (
User *U : AI->users()) {
568if (U->stripInBoundsConstantOffsets() == AI) {
570 LifetimeMarkerInfo LMI = getLifetimeMarkers(CEAC, Bitcast, ExitBlock);
578// Found unknown use of AI. 585// Either no bitcasts reference the alloca or there are unknown uses. 589LLVM_DEBUG(
dbgs() <<
"Sinking alloca (via bitcast): " << *AI <<
"\n");
591for (
unsignedI = 0, E = Bitcasts.
size();
I != E; ++
I) {
593const LifetimeMarkerInfo &LMI = BitcastLifetimeInfo[
I];
595"Unsafe to sink bitcast without lifetime markers");
596 moveOrIgnoreLifetimeMarkers(LMI);
600 SinkCands.
insert(BitcastAddr);
612// For functions with varargs, check that varargs handling is only done in the 613// outlined function, i.e vastart and vaend are only used in outlined blocks. 614if (AllowVarArgs &&
F->getFunctionType()->isVarArg()) {
616if (
constCallInst *CI = dyn_cast<CallInst>(&
I))
617if (
constFunction *Callee = CI->getCalledFunction())
618return Callee->getIntrinsicID() == Intrinsic::vastart ||
619 Callee->getIntrinsicID() == Intrinsic::vaend;
624if (Blocks.count(&BB))
630// stacksave as input implies stackrestore in the outlined function. 631// This can confuse prolog epilog insertion phase. 632// stacksave's uses must not cross outlined function. 638bool IsSave =
II->getIntrinsicID() == Intrinsic::stacksave;
639bool IsRestore =
II->getIntrinsicID() == Intrinsic::stackrestore;
640if (IsSave &&
any_of(
II->users(), [&Blks = this->Blocks](
User *U) {
641 return !definedInRegion(Blks, U);
653bool CollectGlobalInputs)
const{
655// If a used value is defined outside the region, it's an input. If an 656// instruction is used outside the region, it's an output. 658for (
auto &OI :
II.operands()) {
660if (!SinkCands.
count(V) &&
662 (CollectGlobalInputs && llvm::isa<llvm::GlobalVariable>(V))))
675/// severSplitPHINodesOfEntry - If a PHI node has multiple inputs from outside 676/// of the region, we need to split the entry block of the region so that the 677/// PHI node is easier to deal with. 678void CodeExtractor::severSplitPHINodesOfEntry(
BasicBlock *&Header) {
679unsigned NumPredsFromRegion = 0;
680unsigned NumPredsOutsideRegion = 0;
682if (Header != &Header->getParent()->getEntryBlock()) {
683PHINode *PN = dyn_cast<PHINode>(Header->begin());
684if (!PN)
return;
// No PHI nodes. 686// If the header node contains any PHI nodes, check to see if there is more 687// than one entry from outside the region. If so, we need to sever the 688// header block into two. 691 ++NumPredsFromRegion;
693 ++NumPredsOutsideRegion;
695// If there is one (or fewer) predecessor from outside the region, we don't 696// need to do anything special. 697if (NumPredsOutsideRegion <= 1)
return;
700// Otherwise, we need to split the header block into two pieces: one 701// containing PHI nodes merging values from outside of the region, and a 702// second that contains all of the code for the block and merges back any 703// incoming values from inside of the region. 706// We only want to code extract the second block now, and it becomes the new 707// header of the region. 709 Blocks.remove(OldPred);
710 Blocks.insert(NewBB);
713// Okay, now we need to adjust the PHI nodes and any branches from within the 714// region to go to the new header block instead of the old header block. 715if (NumPredsFromRegion) {
717// Loop over all of the predecessors of OldPred that are in the region, 718// changing them to branch to NewBB instead. 725// Okay, everything within the region is now branching to the right block, we 726// just have to update the PHI nodes now, inserting PHI nodes into NewBB. 728for (AfterPHIs = OldPred->
begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
729PHINode *PN = cast<PHINode>(AfterPHIs);
730// Create a new PHI node in the new region, which has an incoming value 731// from OldPred of PN. 738// Loop over all of the incoming value in PN, moving them to NewPN if they 739// are from the extracted region. 751/// severSplitPHINodesOfExits - if PHI nodes in exit blocks have inputs from 752/// outlined region, we split these PHIs on two: one with inputs from region 753/// and other with remaining incoming blocks; then first PHIs are placed in 755void CodeExtractor::severSplitPHINodesOfExits() {
756for (
BasicBlock *ExitBB : ExtractedFuncRetVals) {
759for (
PHINode &PN : ExitBB->phis()) {
760// Find all incoming values from the outlining region. 766// Do not process PHI if there is one (or fewer) predecessor from region. 767// If PHI has exactly one predecessor from region, only this one incoming 768// will be replaced on codeRepl block, so it should be safe to skip PHI. 769if (IncomingVals.
size() <= 1)
772// Create block for new PHIs and add it to the list of outlined if it 773// wasn't done before. 776 ExitBB->getName() +
".split",
777 ExitBB->getParent(), ExitBB);
781if (Blocks.count(PredBB))
782 PredBB->getTerminator()->replaceUsesOfWith(ExitBB, NewBB);
784 Blocks.insert(NewBB);
791for (
unsigned i : IncomingVals)
793for (
unsigned i :
reverse(IncomingVals))
800void CodeExtractor::splitReturnBlocks() {
804Block->splitBasicBlock(RI->getIterator(),
Block->getName() +
".ret");
806// Old dominates New. New node dominates all other nodes dominated 820Function *CodeExtractor::constructFunctionDeclaration(
821const ValueSet &inputs,
const ValueSet &outputs,
BlockFrequency EntryFreq,
829// Assemble the function's parameter lists. 830 std::vector<Type *> ParamTy;
831 std::vector<Type *> AggParamTy;
834// Add the types of the input values to the function's argument list 837if (AggregateArgs && !ExcludeArgsFromAggregate.
contains(
value)) {
838 AggParamTy.push_back(
value->getType());
839 StructValues.insert(
value);
841 ParamTy.push_back(
value->getType());
844// Add the types of the output values to the function's argument list. 845for (
Value *output : outputs) {
847if (AggregateArgs && !ExcludeArgsFromAggregate.
contains(output)) {
848 AggParamTy.push_back(output->getType());
849 StructValues.insert(output);
856 (ParamTy.size() + AggParamTy.size()) ==
857 (inputs.size() + outputs.size()) &&
858"Number of scalar and aggregate params does not match inputs, outputs");
859assert((StructValues.empty() || AggregateArgs) &&
860"Expeced StructValues only with AggregateArgs set");
862// Concatenate scalar and aggregate params in ParamTy. 863if (!AggParamTy.empty()) {
866M->getContext(), ArgsInZeroAddressSpace ? 0 :
DL.getAllocaAddrSpace()));
871dbgs() <<
"Function type: " << *
RetTy <<
" f(";
872for (
Type *i : ParamTy)
880// Create the new function 885// Propagate personality info to the new function if there is one. 889// Inherit all of the target dependent attributes and white-listed 890// target independent attributes. 891// (e.g. If the extracted region contains a call to an x86.sse 892// instruction we need to make sure that the extracted region has the 893// "target-features" attribute allowing it to be lowered. 894// FIXME: This should be changed to check to see if a specific 895// attribute can not be inherited. 897if (Attr.isStringAttribute()) {
898if (Attr.getKindAsString() ==
"thunk")
901switch (Attr.getKindAsEnum()) {
902// Those attributes cannot be propagated safely. Explicitly list them 903// here so we get a warning if new attributes are added. 904case Attribute::AllocSize:
905case Attribute::Builtin:
906case Attribute::Convergent:
907case Attribute::JumpTable:
908case Attribute::Naked:
909case Attribute::NoBuiltin:
910case Attribute::NoMerge:
911case Attribute::NoReturn:
912case Attribute::NoSync:
913case Attribute::ReturnsTwice:
914case Attribute::Speculatable:
915case Attribute::StackAlignment:
916case Attribute::WillReturn:
917case Attribute::AllocKind:
918case Attribute::PresplitCoroutine:
919case Attribute::Memory:
920case Attribute::NoFPClass:
921case Attribute::CoroDestroyOnlyWhenComplete:
922case Attribute::CoroElideSafe:
923case Attribute::NoDivergenceSource:
925// Those attributes should be safe to propagate to the extracted function. 926case Attribute::AlwaysInline:
928case Attribute::DisableSanitizerInstrumentation:
929case Attribute::FnRetThunkExtern:
931case Attribute::HybridPatchable:
932case Attribute::NoRecurse:
933case Attribute::InlineHint:
934case Attribute::MinSize:
935case Attribute::NoCallback:
936case Attribute::NoDuplicate:
937case Attribute::NoFree:
938case Attribute::NoImplicitFloat:
939case Attribute::NoInline:
940case Attribute::NonLazyBind:
941case Attribute::NoRedZone:
942case Attribute::NoUnwind:
943case Attribute::NoSanitizeBounds:
944case Attribute::NoSanitizeCoverage:
945case Attribute::NullPointerIsValid:
946case Attribute::OptimizeForDebugging:
947case Attribute::OptForFuzzing:
948case Attribute::OptimizeNone:
949case Attribute::OptimizeForSize:
950case Attribute::SafeStack:
951case Attribute::ShadowCallStack:
952case Attribute::SanitizeAddress:
953case Attribute::SanitizeMemory:
954case Attribute::SanitizeNumericalStability:
955case Attribute::SanitizeThread:
956case Attribute::SanitizeType:
957case Attribute::SanitizeHWAddress:
958case Attribute::SanitizeMemTag:
959case Attribute::SanitizeRealtime:
960case Attribute::SanitizeRealtimeBlocking:
961case Attribute::SpeculativeLoadHardening:
962case Attribute::StackProtect:
963case Attribute::StackProtectReq:
964case Attribute::StackProtectStrong:
965case Attribute::StrictFP:
966case Attribute::UWTable:
967case Attribute::VScaleRange:
968case Attribute::NoCfCheck:
969case Attribute::MustProgress:
970case Attribute::NoProfile:
971case Attribute::SkipProfile:
973// These attributes cannot be applied to functions. 974case Attribute::Alignment:
975case Attribute::AllocatedPointer:
976case Attribute::AllocAlign:
977case Attribute::ByVal:
978case Attribute::Captures:
979case Attribute::Dereferenceable:
980case Attribute::DereferenceableOrNull:
981case Attribute::ElementType:
982case Attribute::InAlloca:
983case Attribute::InReg:
985case Attribute::NoAlias:
986case Attribute::NoCapture:
987case Attribute::NoUndef:
988case Attribute::NonNull:
989case Attribute::Preallocated:
990case Attribute::ReadNone:
991case Attribute::ReadOnly:
992case Attribute::Returned:
994case Attribute::StructRet:
995case Attribute::SwiftError:
996case Attribute::SwiftSelf:
997case Attribute::SwiftAsync:
999case Attribute::ImmArg:
1000case Attribute::ByRef:
1001case Attribute::WriteOnly:
1002case Attribute::Writable:
1003case Attribute::DeadOnUnwind:
1004case Attribute::Range:
1005case Attribute::Initializes:
1006case Attribute::NoExt:
1007// These are not really attributes. 1018// Create scalar and aggregate iterators to name all of the arguments we 1022// Set names and attributes for input and output arguments. 1024for (
Value *input : inputs) {
1025if (StructValues.contains(input))
1028 ScalarAI->
setName(input->getName());
1029if (input->isSwiftError())
1031 Attribute::SwiftError);
1034for (
Value *output : outputs) {
1035if (StructValues.contains(output))
1038 ScalarAI->
setName(output->getName() +
".out");
1042// Update the entry count of the function. 1045if (Count.has_value())
1053/// If the original function has debug info, we have to add a debug location 1054/// to the new branch instruction from the artificial entry block. 1055/// We use the debug location of the first instruction in the extracted 1056/// blocks, as there is no other equivalent line in the source code. 1063if (!
I.getDebugLoc())
1065// Don't use source locations attached to debug-intrinsics: they could 1066// be from completely unrelated scopes. 1067if (isa<DbgInfoIntrinsic>(
I))
1076/// Erase lifetime.start markers which reference inputs to the extraction 1077/// region, and insert the referenced memory into \p LifetimesStart. 1079/// The extraction region is defined by a set of blocks (\p Blocks), and a set 1080/// of allocas which will be moved from the caller function into the extracted 1081/// function (\p SunkAllocas). 1087auto *
II = dyn_cast<IntrinsicInst>(&
I);
1088if (!
II || !
II->isLifetimeStartOrEnd())
1091// Get the memory operand of the lifetime marker. If the underlying 1092// object is a sunk alloca, or is otherwise defined in the extraction 1093// region, the lifetime marker must not be erased. 1094Value *Mem =
II->getOperand(1)->stripInBoundsOffsets();
1098if (
II->getIntrinsicID() == Intrinsic::lifetime_start)
1099 LifetimesStart.
insert(Mem);
1100II->eraseFromParent();
1105/// Insert lifetime start/end markers surrounding the call to the new function 1106/// for objects defined in the caller. 1114// Emit lifetime markers for the pointers given in \p Objects. Insert the 1115// markers before the call if \p InsertBefore, and after the call otherwise. 1118for (
Value *Mem : Objects) {
1121"Input memory not defined in original function");
1129 Marker->insertBefore(Term->getIterator());
1133if (!LifetimesStart.
empty()) {
1134 insertMarkers(Intrinsic::lifetime_start, LifetimesStart,
1135/*InsertBefore=*/true);
1138if (!LifetimesEnd.
empty()) {
1139 insertMarkers(Intrinsic::lifetime_end, LifetimesEnd,
1140/*InsertBefore=*/false);
1144void CodeExtractor::moveCodeToFunction(
Function *newFunction) {
1145auto newFuncIt = newFunction->
begin();
1147// Delete the basic block from the old function, and the list of blocks 1148Block->removeFromParent();
1150// Insert this basic block into the new function 1151// Insert the original blocks after the entry block created 1152// for the new function. The entry block may be followed 1153// by a set of exit blocks at this point, but these exit 1154// blocks better be placed at the end of the new function. 1155 newFuncIt = newFunction->
insert(std::next(newFuncIt),
Block);
1159void CodeExtractor::calculateNewCallTerminatorWeights(
1166// Update the branch weights for the exit block. 1170// Block Frequency distribution with dummy node. 1171 Distribution BranchDist;
1176// Add each of the frequencies of the successors. 1178 BlockNode ExitNode(i);
1181 BranchDist.addExit(ExitNode, ExitFreq);
1186// Check for no total weight. 1187if (BranchDist.Total == 0) {
1192// Normalize the distribution so that they can fit in unsigned. 1193 BranchDist.normalize();
1195// Create normalized branch weights and set the metadata. 1196for (
unsignedI = 0, E = BranchDist.Weights.size();
I < E; ++
I) {
1197constauto &Weight = BranchDist.Weights[
I];
1199// Get the weight and update the current BFI. 1200 BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
1202 EdgeProbabilities[Weight.TargetNode.Index] = BP;
1206 LLVMContext::MD_prof,
1210/// Erase debug info intrinsics which refer to values in \p F but aren't in 1218if (DVI->getFunction() != &
F)
1219 DVI->eraseFromParent();
1221if (DVR->getFunction() != &
F)
1222 DVR->eraseFromParent();
1226/// Fix up the debug info in the old and new functions by pointing line 1227/// locations and debug intrinsics to the new subprogram scope, and by deleting 1228/// intrinsics which point to values outside of the new function. 1235// Erase any debug info the new function contains. 1237// Make sure the old function doesn't contain any non-local metadata refs. 1242// Create a subprogram for the new function. Leave out a description of the 1243// function arguments, as the parameters don't correspond to anything at the 1245assert(OldSP->getUnit() &&
"Missing compile unit for subprogram");
1250 DISubprogram::SPFlagOptimized |
1251 DISubprogram::SPFlagLocalToUnit;
1254/*LineNo=*/0, SPType,
/*ScopeLine=*/0, DINode::FlagZero, SPFlags);
1257auto IsInvalidLocation = [&NewFunc](
Value *Location) {
1258// Location is invalid if it isn't a constant or an instruction, or is an 1259// instruction but isn't in the new function. 1261 (!isa<Constant>(Location) && !isa<Instruction>(Location)))
1263Instruction *LocationInst = dyn_cast<Instruction>(Location);
1264return LocationInst && LocationInst->
getFunction() != &NewFunc;
1267// Debug intrinsics in the new function need to be updated in one of two 1269// 1) They need to be deleted, because they describe a value in the old 1271// 2) They need to point to fresh metadata, e.g. because they currently 1272// point to a variable in the wrong scope. 1279DINode *&NewVar = RemappedMetadata[OldVar];
1282 *OldVar->getScope(), *NewSP, Ctx, Cache);
1284 NewScope, OldVar->
getName(), OldVar->getFile(), OldVar->getLine(),
1285 OldVar->getType(),
/*AlwaysPreserve=*/false, DINode::FlagZero,
1286 OldVar->getAlignInBits());
1288return cast<DILocalVariable>(NewVar);
1291auto UpdateDbgLabel = [&](
auto *LabelRecord) {
1292// Point the label record to a fresh label within the new function if 1293// the record was not inlined from some other function. 1294if (LabelRecord->getDebugLoc().getInlinedAt())
1296DILabel *OldLabel = LabelRecord->getLabel();
1297DINode *&NewLabel = RemappedMetadata[OldLabel];
1300 *OldLabel->
getScope(), *NewSP, Ctx, Cache);
1304 LabelRecord->setLabel(cast<DILabel>(NewLabel));
1307auto UpdateDbgRecordsOnInst = [&](
Instruction &
I) ->
void {
1310 UpdateDbgLabel(DLR);
1315// Apply the two updates that dbg.values get: invalid operands, and 1316// variable metadata fixup. 1331 UpdateDbgRecordsOnInst(
I);
1333auto *DII = dyn_cast<DbgInfoIntrinsic>(&
I);
1337// Point the intrinsic to a fresh label within the new function if the 1338// intrinsic was not inlined from some other function. 1339if (
auto *DLI = dyn_cast<DbgLabelInst>(&
I)) {
1340 UpdateDbgLabel(DLI);
1344auto *DVI = cast<DbgVariableIntrinsic>(DII);
1345// If any of the used locations are invalid, delete the intrinsic. 1346if (
any_of(DVI->location_ops(), IsInvalidLocation)) {
1350// DbgAssign intrinsics have an extra Value argument: 1351if (
auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);
1352 DAI && IsInvalidLocation(DAI->getAddress())) {
1356// If the variable was in the scope of the old function, i.e. it was not 1357// inlined, point the intrinsic to a fresh variable within the new function. 1358if (!DVI->getDebugLoc().getInlinedAt())
1359 DVI->setVariable(GetUpdatedDIVariable(DVI->getVariable()));
1362for (
auto *DII : DebugIntrinsicsToDelete)
1363 DII->eraseFromParent();
1364for (
auto *DVR : DVRsToDelete)
1365 DVR->getMarker()->MarkedInstr->dropOneDbgRecord(DVR);
1368// Fix up the scope information attached to the line locations and the 1369// debug assignment metadata in the new function. 1377 *NewSP, Ctx, Cache));
1379// Loop info metadata may contain line locations. Fix them up. 1381if (
auto *Loc = dyn_cast_or_null<DILocation>(MD))
1406// Assumption: this is a single-entry code region, and the header is the first 1407// block in the region. 1411 normalizeCFGForExtraction(header);
1413// Remove @llvm.assume calls that will be moved to the new function from the 1414// old function's assumption cache. 1417if (
auto *AI = dyn_cast<AssumeInst>(&
I)) {
1420 AI->eraseFromParent();
1425ValueSet SinkingCands, HoistingCands;
1427findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit);
1430// Find inputs to, outputs from the code region. 1433// Collect objects which are inputs to the extraction region and also 1434// referenced by lifetime start markers within it. The effects of these 1435// markers must be replicated in the calling function to prevent the stack 1436// coloring pass from merging slots which store input objects. 1440if (!HoistingCands.
empty()) {
1443for (
auto *
II : HoistingCands)
1445 computeExtractedFuncRetVals();
1448// CFG/ExitBlocks must not change hereafter 1450// Calculate the entry frequency of the new function before we change the root 1455assert(BPI &&
"Both BPI and BFI are required to preserve profile info");
1463for (
BasicBlock *Succ : ExtractedFuncRetVals) {
1468// Update the branch weight for this successor. 1475// Determine position for the replacement code. Do so before header is moved 1476// to the new function. 1478while (ReplIP &&
Blocks.count(ReplIP))
1481// Construct new function based on inputs/outputs & add allocas for all defs. 1482 std::string SuffixToUse =
1489Function *newFunction = constructFunctionDeclaration(
1490 inputs, outputs, EntryFreq, oldFunction->
getName() +
"." + SuffixToUse,
1491 StructValues, StructTy);
1494 emitFunctionBody(inputs, outputs, StructValues, newFunction, StructTy, header,
1497 std::vector<Value *> Reloads;
1498CallInst *TheCall = emitReplacerCall(
1499 inputs, outputs, StructValues, newFunction, StructTy, oldFunction, ReplIP,
1500 EntryFreq, LifetimesStart.
getArrayRef(), Reloads);
1502 insertReplacerCall(oldFunction, header, TheCall->
getParent(), outputs,
1503 Reloads, ExitWeights);
1508 newFunction->
dump();
1518void CodeExtractor::normalizeCFGForExtraction(
BasicBlock *&header) {
1519// If we have any return instructions in the region, split those blocks so 1520// that the return is not in the region. 1521 splitReturnBlocks();
1523// If we have to split PHI nodes of the entry or exit blocks, do so now. 1524 severSplitPHINodesOfEntry(header);
1526// If a PHI in an exit block has multiple incoming values from the outlined 1527// region, create a new PHI for those values within the region such that only 1528// PHI itself becomes an output value, not each of its incoming values 1530 computeExtractedFuncRetVals();
1531 severSplitPHINodesOfExits();
1534void CodeExtractor::computeExtractedFuncRetVals() {
1535 ExtractedFuncRetVals.clear();
1543bool IsNew = ExitBlocks.
insert(Succ).second;
1545 ExtractedFuncRetVals.push_back(Succ);
1550Type *CodeExtractor::getSwitchType() {
1553assert(ExtractedFuncRetVals.size() < 0xffff &&
1554"too many exit blocks for switch");
1555switch (ExtractedFuncRetVals.size()) {
1560// Conditional branch, return a bool 1567void CodeExtractor::emitFunctionBody(
1568const ValueSet &inputs,
const ValueSet &outputs,
1569const ValueSet &StructValues,
Function *newFunction,
1574// The new function needs a root node because other nodes can branch to the 1575// head of the region, but the entry node of a function cannot have preds. 1580// Now sink all instructions which only have non-phi uses inside the region. 1581// Group the allocas at the start of the block, so that any bitcast uses of 1582// the allocas are well-defined. 1583for (
auto *
II : SinkingCands) {
1584if (!isa<AllocaInst>(
II)) {
1585 cast<Instruction>(
II)->moveBefore(*newFuncRoot,
1589for (
auto *
II : SinkingCands) {
1590if (
auto *AI = dyn_cast<AllocaInst>(
II)) {
1596Argument *AggArg = StructValues.empty()
1600// Rewrite all users of the inputs in the extracted region to use the 1601// arguments (or appropriate addressing into struct) instead. 1603for (
unsigned i = 0, e = inputs.size(), aggIdx = 0; i != e; ++i) {
1605if (StructValues.contains(inputs[i])) {
1610 StructArgTy, AggArg,
Idx,
"gep_" + inputs[i]->
getName(), newFuncRoot);
1612"loadgep_" + inputs[i]->getName(), newFuncRoot);
1615 RewriteVal = &*ScalarAI++;
1620 moveCodeToFunction(newFunction);
1622for (
unsigned i = 0, e = inputs.size(); i != e; ++i) {
1623Value *RewriteVal = NewValues[i];
1625 std::vector<User *>
Users(inputs[i]->user_begin(), inputs[i]->user_end());
1628if (
Blocks.count(inst->getParent()))
1629 inst->replaceUsesOfWith(inputs[i], RewriteVal);
1632// Since there may be multiple exits from the original region, make the new 1633// function return an unsigned, switch on that number. This loop iterates 1634// over all of the blocks in the extracted region, updating any terminator 1635// instructions in the to-be-extracted region that branch to blocks that are 1636// not in the region to be extracted. 1637 std::map<BasicBlock *, BasicBlock *> ExitBlockMap;
1639// Iterate over the previously collected targets, and create new blocks inside 1640// the function to branch to. 1641for (
autoP :
enumerate(ExtractedFuncRetVals)) {
1643size_t SuccNum =
P.index();
1646 Context, OldTarget->
getName() +
".exitStub", newFunction);
1647 ExitBlockMap[OldTarget] = NewTarget;
1649Value *brVal =
nullptr;
1651assert(ExtractedFuncRetVals.size() < 0xffff &&
1652"too many exit blocks for switch");
1653switch (ExtractedFuncRetVals.size()) {
1658case 2:
// Conditional branch, return a bool 1659 brVal = ConstantInt::get(
RetTy, !SuccNum);
1662 brVal = ConstantInt::get(
RetTy, SuccNum);
1675// add a new basic block which returns the appropriate value 1676BasicBlock *NewTarget = ExitBlockMap[OldTarget];
1677assert(NewTarget &&
"Unknown target block!");
1679// rewrite the original branch instruction with this new target 1684// Loop over all of the PHI nodes in the header and exit blocks, and change 1685// any references to the old incoming edge to be the new incoming edge. 1693// Connect newFunction entry block to new header. 1697// Store the arguments right after the definition of output value. 1698// This should be proceeded after creating exit stubs to be ensure that invoke 1699// result restore will be placed in the outlined function. 1703for (
Value *Input : inputs) {
1704if (StructValues.contains(Input))
1710for (
Value *Output : outputs) {
1711// Find proper insertion point. 1712// In case Output is an invoke, we insert the store at the beginning in the 1713// 'normal destination' BB. Otherwise we insert the store right after 1716if (
auto *InvokeI = dyn_cast<InvokeInst>(Output))
1717 InsertPt = InvokeI->getNormalDest()->getFirstInsertionPt();
1718elseif (
auto *Phi = dyn_cast<PHINode>(Output))
1719 InsertPt =
Phi->getParent()->getFirstInsertionPt();
1720elseif (
auto *OutI = dyn_cast<Instruction>(Output))
1721 InsertPt = std::next(OutI->getIterator());
1723// Globals don't need to be updated, just advance to the next argument. 1724if (StructValues.contains(Output))
1731assert((InsertPt->getFunction() == newFunction ||
1732Blocks.count(InsertPt->getParent())) &&
1733"InsertPt should be in new function");
1735if (StructValues.contains(Output)) {
1736assert(AggArg &&
"Number of aggregate output arguments should match " 1737"the number of defined values");
1742 StructArgTy, AggArg,
Idx,
"gep_" + Output->getName(), InsertPt);
1747"Number of scalar output arguments should match " 1748"the number of defined values");
1749newStoreInst(Output, &*ScalarAI, InsertPt);
1754if (ExtractedFuncRetVals.empty()) {
1755// Mark the new function `noreturn` if applicable. Terminators which resume 1756// exception propagation are treated as returning instructions. This is to 1757// avoid inserting traps after calls to outlined functions which unwind. 1760return isa<ReturnInst>(Term) || isa<ResumeInst>(Term);
1766CallInst *CodeExtractor::emitReplacerCall(
1767const ValueSet &inputs,
const ValueSet &outputs,
1768const ValueSet &StructValues,
Function *newFunction,
1771 std::vector<Value *> &Reloads) {
1776// This takes place of the original loop 1781 AllocationBlock ? AllocationBlock : &oldFunction->
getEntryBlock();
1784// Update the entry count of the function. 1788 std::vector<Value *> params;
1790// Add inputs as params, or to be filled into the struct 1791for (
Value *input : inputs) {
1792if (StructValues.contains(input))
1795 params.push_back(input);
1798// Create allocas for the outputs 1799 std::vector<Value *> ReloadOutputs;
1800for (
Value *output : outputs) {
1801if (StructValues.contains(output))
1805 output->getType(),
DL.getAllocaAddrSpace(),
nullptr,
1807 params.push_back(alloca);
1808 ReloadOutputs.push_back(alloca);
1812if (!StructValues.empty()) {
1815if (ArgsInZeroAddressSpace &&
DL.getAllocaAddrSpace() != 0) {
1817Struct, PointerType ::get(Context, 0),
"structArg.ascast");
1818 StructSpaceCast->insertAfter(
Struct->getIterator());
1819 params.push_back(StructSpaceCast);
1821 params.push_back(
Struct);
1825for (
Value *input : inputs) {
1826if (!StructValues.contains(input))
1833 StructArgTy,
Struct,
Idx,
"gep_" + input->getName());
1834GEP->insertInto(codeReplacer, codeReplacer->
end());
1841// Emit the call to the function 1843 newFunction, params, ExtractedFuncRetVals.size() > 1 ?
"targetBlock" :
"",
1846// Set swifterror parameter attributes. 1847unsigned ParamIdx = 0;
1849for (
auto input : inputs) {
1850if (StructValues.contains(input)) {
1853if (input->isSwiftError())
1859// Add debug location to the new call, if the original function has debug 1860// info. In that case, the terminator of the entry block of the extracted 1861// function contains the first debug location of the extracted function, 1862// set in extractCodeRegion. 1868// Reload the outputs passed in by reference, use the struct if output is in 1869// the aggregate or reload from the scalar argument. 1870for (
unsigned i = 0, e = outputs.size(), scalarIdx = 0; i != e; ++i) {
1871Value *Output =
nullptr;
1872if (StructValues.contains(outputs[i])) {
1878GEP->insertInto(codeReplacer, codeReplacer->
end());
1882 Output = ReloadOutputs[scalarIdx];
1887 outputs[i]->
getName() +
".reload", codeReplacer);
1888 Reloads.push_back(
load);
1891// Now we can emit a switch statement using the call as a value. 1894 codeReplacer, 0, codeReplacer);
1895for (
autoP :
enumerate(ExtractedFuncRetVals)) {
1897size_t SuccNum =
P.index();
1903// Now that we've done the deed, simplify the switch instruction. 1904Type *OldFnRetTy = TheSwitch->
getParent()->getParent()->getReturnType();
1905switch (ExtractedFuncRetVals.size()) {
1907// There are no successors (the block containing the switch itself), which 1908// means that previously this was the last part of the function, and hence 1909// this should be rewritten as a `ret` or `unreachable`. 1911// If fn is no return, end with an unreachable terminator. 1914// We have no return value. 1918// return what we have 1922// Otherwise we must have code extracted an unwind or something, just 1923// return whatever we want. 1931// Only a single destination, change the switch into an unconditional 1937// Only two destinations, convert to a condition branch. 1938// Remark: This also swaps the target branches: 1939// 0 -> false -> getSuccessor(2); 1 -> true -> getSuccessor(1) 1945// Otherwise, make the default destination of the switch instruction be one 1946// of the other successors. 1950// Remove redundant case 1956// Insert lifetime markers around the reloads of any output values. The 1957// allocas output values are stored in are only in-use in the codeRepl block. 1960// Replicate the effects of any lifetime start/end markers which referenced 1961// input objects in the extraction region by placing markers around the call. 1968void CodeExtractor::insertReplacerCall(
1973// Rewrite branches to basic blocks outside of the loop to new dummy blocks 1974// within the new function. This must be done before we lose track of which 1975// blocks were originally in the code region. 1978// The BasicBlock which contains the branch is not in the region 1979// modify the branch target to a new block 1981if (
I->isTerminator() &&
I->getFunction() == oldFunction &&
1982 !
Blocks.count(
I->getParent()))
1983I->replaceUsesOfWith(header, codeReplacer);
1985// When moving the code region it is sufficient to replace all uses to the 1986// extracted function values. Since the original definition's block 1987// dominated its use, it will also be dominated by codeReplacer's switch 1988// which joined multiple exit blocks. 1989for (
BasicBlock *ExitBB : ExtractedFuncRetVals)
1990for (
PHINode &PN : ExitBB->phis()) {
1991Value *IncomingCodeReplacerVal =
nullptr;
1993// Ignore incoming values from outside of the extracted region. 1997// Ensure that there is only one incoming value from codeReplacer. 1998if (!IncomingCodeReplacerVal) {
2003"PHI has two incompatbile incoming values from codeRepl");
2007for (
unsigned i = 0, e = outputs.size(); i != e; ++i) {
2009 std::vector<User *>
Users(outputs[i]->user_begin(), outputs[i]->user_end());
2012if (inst->
getParent()->getParent() == oldFunction)
2017// Update the branch weights for the exit block. 2018if (BFI && ExtractedFuncRetVals.size() > 1)
2019 calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
2026auto *
I = dyn_cast_or_null<CallInst>(AssumeVH);
2030// There shouldn't be any llvm.assume intrinsics in the new function. 2031if (
I->getFunction() != &OldFunc)
2034// There shouldn't be any stale affected values in the assumption cache 2035// that were previously in the old function, but that have now been moved 2036// to the new function. 2038auto *AffectedCI = dyn_cast_or_null<CallInst>(AffectedValVH);
2041if (AffectedCI->getFunction() != &OldFunc)
2043auto *AssumedInst = cast<Instruction>(AffectedCI->getOperand(0));
2044if (AssumedInst->getFunction() != &OldFunc)
2052 ExcludeArgsFromAggregate.
insert(Arg);
AMDGPU Mark last scratch load
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
Analysis containing CSE Info
static void eraseDebugIntrinsicsWithNonLocalRefs(Function &F)
Erase debug info intrinsics which refer to values in F but aren't in F.
static SetVector< BasicBlock * > buildExtractionBlockSet(ArrayRef< BasicBlock * > BBs, DominatorTree *DT, bool AllowVarArgs, bool AllowAlloca)
Build a set of blocks to extract if the input blocks are viable.
static void applyFirstDebugLoc(Function *oldFunction, ArrayRef< BasicBlock * > Blocks, Instruction *BranchI)
If the original function has debug info, we have to add a debug location to the new branch instructio...
static bool definedInRegion(const SetVector< BasicBlock * > &Blocks, Value *V)
definedInRegion - Return true if the specified value is defined in the extracted region.
static bool definedInCaller(const SetVector< BasicBlock * > &Blocks, Value *V)
definedInCaller - Return true if the specified value is defined in the function being code extracted,...
static bool isBlockValidForExtraction(const BasicBlock &BB, const SetVector< BasicBlock * > &Result, bool AllowVarArgs, bool AllowAlloca)
Test whether a block is valid for extraction.
static BasicBlock * getCommonExitBlock(const SetVector< BasicBlock * > &Blocks)
static void eraseLifetimeMarkersOnInputs(const SetVector< BasicBlock * > &Blocks, const SetVector< Value * > &SunkAllocas, SetVector< Value * > &LifetimesStart)
Erase lifetime.start markers which reference inputs to the extraction region, and insert the referenc...
static void fixupDebugInfoPostExtraction(Function &OldFunc, Function &NewFunc, CallInst &TheCall)
Fix up the debug info in the old and new functions by pointing line locations and debug intrinsics to...
static cl::opt< bool > AggregateArgsOpt("aggregate-extracted-args", cl::Hidden, cl::desc("Aggregate arguments to code-extracted functions"))
static void insertLifetimeMarkersSurroundingCall(Module *M, ArrayRef< Value * > LifetimesStart, ArrayRef< Value * > LifetimesEnd, CallInst *TheCall)
Insert lifetime start/end markers surrounding the call to the new function for objects defined in the...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Given that RA is a live value
This file defines the DenseMap class.
DenseMap< Block *, BlockRelaxAux > Blocks
static Function * getFunction(Constant *C)
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
iv Induction Variable Users
Move duplicate certain instructions close to their use
uint64_t IntrinsicInst * II
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static SymbolRef::Type getType(const Symbol *Sym)
This class represents a conversion between pointers from one address space to another.
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
bool empty() const
empty - Check if the array is empty.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptions()
Access the list of assumption handles currently tracked for this function.
void unregisterAssumption(AssumeInst *CI)
Remove an @llvm.assume intrinsic from this function's cache if it has been added to the cache earlier...
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
AttributeSet getFnAttrs() const
The function attributes are returned.
@ TombstoneKey
Use as Tombstone key for DenseMap of AttrKind.
@ None
No attributes have been set.
@ EmptyKey
Use as Empty key for DenseMap of AttrKind.
@ EndAttrKinds
Sentinel value useful for loops.
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
iterator_range< filter_iterator< BasicBlock::const_iterator, std::function< bool(const Instruction &)> > > instructionsWithoutDebug(bool SkipPseudoOp=true) const
Return a const iterator range over the instructions in the block, skipping any debug instructions.
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
const Function * getParent() const
Return the enclosing method, or null if none.
InstListType::iterator iterator
Instruction iterators...
LLVMContext & getContext() const
Get the context in which this basic block lives.
bool IsNewDbgInfoFormat
Flag recording whether or not this block stores debug-info in the form of intrinsic instructions (fal...
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
std::optional< uint64_t > getProfileCountFromFreq(BlockFrequency Freq) const
Returns the estimated profile count of Freq.
void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq)
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
Conditional or Unconditional Branch instruction.
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
Analysis providing branch probability information.
void setEdgeProbability(const BasicBlock *Src, const SmallVectorImpl< BranchProbability > &Probs)
Set the raw probabilities for all edges from the given block.
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
static BranchProbability getUnknown()
static BranchProbability getZero()
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Adds the attribute to the indicated argument.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This is the base class for all instructions that perform data casts.
static CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
A cache for the CodeExtractor analysis.
ArrayRef< AllocaInst * > getAllocas() const
Get the allocas in the function at the time the analysis was created.
CodeExtractorAnalysisCache(Function &F)
bool doesBlockContainClobberOfAddr(BasicBlock &BB, AllocaInst *Addr) const
Check whether BB contains an instruction thought to load from, store to, or otherwise clobber the all...
CodeExtractor(ArrayRef< BasicBlock * > BBs, DominatorTree *DT=nullptr, bool AggregateArgs=false, BlockFrequencyInfo *BFI=nullptr, BranchProbabilityInfo *BPI=nullptr, AssumptionCache *AC=nullptr, bool AllowVarArgs=false, bool AllowAlloca=false, BasicBlock *AllocationBlock=nullptr, std::string Suffix="", bool ArgsInZeroAddressSpace=false)
Create a code extractor for a sequence of blocks.
BasicBlock * findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock)
Find or create a block within the outline region for placing hoisted code.
void findAllocas(const CodeExtractorAnalysisCache &CEAC, ValueSet &SinkCands, ValueSet &HoistCands, BasicBlock *&ExitBlock) const
Find the set of allocas whose life ranges are contained within the outlined region.
Function * extractCodeRegion(const CodeExtractorAnalysisCache &CEAC)
Perform the extraction, returning the new function.
static bool verifyAssumptionCache(const Function &OldFunc, const Function &NewFunc, AssumptionCache *AC)
Verify that assumption cache isn't stale after a region is extracted.
void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs, const ValueSet &Allocas, bool CollectGlobalInputs=false) const
Compute the set of input values and output values for the code.
bool isEligible() const
Test whether this code extractor is eligible.
void excludeArgFromAggregate(Value *Arg)
Exclude a value from aggregate argument passing when extracting a code region, passing it instead as ...
bool isLegalToShrinkwrapLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC, Instruction *AllocaAddr) const
Check if life time marker nodes can be hoisted/sunk into the outline region.
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
DISubroutineType * createSubroutineType(DITypeRefArray ParameterTypes, DINode::DIFlags Flags=DINode::FlagZero, unsigned CC=0)
Create subroutine type.
void finalizeSubprogram(DISubprogram *SP)
Finalize a specific subprogram - no new variables may be added to this subprogram afterwards.
DISubprogram * createFunction(DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File, unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine, DINode::DIFlags Flags=DINode::FlagZero, DISubprogram::DISPFlags SPFlags=DISubprogram::SPFlagZero, DITemplateParameterArray TParams=nullptr, DISubprogram *Decl=nullptr, DITypeArray ThrownTypes=nullptr, DINodeArray Annotations=nullptr, StringRef TargetFuncName="")
Create a new descriptor for the specified subprogram.
DITypeRefArray getOrCreateTypeArray(ArrayRef< Metadata * > Elements)
Get a DITypeRefArray, create one if required.
DILocalVariable * createAutoVariable(DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo, DIType *Ty, bool AlwaysPreserve=false, DINode::DIFlags Flags=DINode::FlagZero, uint32_t AlignInBits=0)
Create a new descriptor for an auto variable.
StringRef getName() const
DILocalScope * getScope() const
Get the local scope for this label.
static DILocalScope * cloneScopeForSubprogram(DILocalScope &RootScope, DISubprogram &NewSP, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Traverses the scope chain rooted at RootScope until it hits a Subprogram, recreating the chain with "...
Tagged DWARF-like metadata node.
StringRef getName() const
DISPFlags
Debug info subprogram flags.
A parsed version of the target data layout string in and methods for querying it.
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
This is the common base class for debug info intrinsics for variables.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
void setVariable(DILocalVariable *NewVar)
Value * getAddress() const
DILocalVariable * getVariable() const
iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
static DebugLoc replaceInlinedAtSubprogram(const DebugLoc &DL, DISubprogram &NewSP, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Rebuild the entire inline-at chain by replacing the subprogram at the end of the chain with NewSP.
DILocation * getInlinedAt() const
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Class to represent profile counts.
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
const BasicBlock & getEntryBlock() const
DISubprogram * getSubprogram() const
Get the attached subprogram.
bool IsNewDbgInfoFormat
Is this function using intrinsics to record the position of debugging information,...
bool hasPersonalityFn() const
Check whether this function has a personality function.
Constant * getPersonalityFn() const
Get the personality function associated with this function.
void setPersonalityFn(Constant *Fn)
AttributeList getAttributes() const
Return the attribute list for this Function.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
adds the attribute to the list of attributes for the given arg.
Function::iterator insert(Function::iterator Position, BasicBlock *BB)
Insert BB in the basic block list at Position.
bool doesNotReturn() const
Determine if the function cannot return.
Argument * getArg(unsigned i) const
void setEntryCount(ProfileCount Count, const DenseSet< GlobalValue::GUID > *Imports=nullptr)
Set the entry count for this function.
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
@ InternalLinkage
Rename collisions when linking (static functions).
bool isLifetimeStartOrEnd() const LLVM_READONLY
Return true if the instruction is a llvm.lifetime.start or llvm.lifetime.end marker.
unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
const Function * getFunction() const
Return the function this instruction belongs to.
BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
A wrapper class for inspecting calls to intrinsic functions.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
Value * getPointerOperand()
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
Root of the metadata hierarchy.
A Module instance is used to store all the information related to an LLVM module.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Return a value (possibly void), from a function.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
A vector that has set insertion semantics.
ArrayRef< value_type > getArrayRef() const
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
std::string str() const
str - Get the contents as an std::string.
constexpr bool empty() const
empty - Check if the string is empty.
Class to represent struct types.
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Type * getElementType(unsigned N) const
BasicBlock * getSuccessor(unsigned idx) const
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, InsertPosition InsertBefore=nullptr)
void setCondition(Value *V)
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
void setDefaultDest(BasicBlock *DefaultCase)
Value * getCondition() const
CaseIt removeCase(CaseIt I)
This method removes the specified case and its successor from the switch instruction.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
static IntegerType * getInt1Ty(LLVMContext &C)
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt16Ty(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isVoidTy() const
Return true if this is 'void'.
This function has undefined behavior.
bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
user_iterator user_begin()
void setName(const Twine &Name)
Change the name of the value.
const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVMContext & getContext() const
All values hold a context through their type.
StringRef getName() const
Return a constant reference to the value's name.
void dump() const
Support for debugging, callable in GDB: V->dump()
const ParentTy * getParent() const
self_iterator getIterator()
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
void remapAssignID(DenseMap< DIAssignID *, DIAssignID * > &Map, Instruction &I)
Replace DIAssignID uses and attachments with IDs from Map.
NodeAddr< PhiNode * > Phi
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
bool stripDebugInfo(Function &F)
Function::ProfileCount ProfileCount
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
void findDbgUsers(SmallVectorImpl< DbgVariableIntrinsic * > &DbgInsts, Value *V, SmallVectorImpl< DbgVariableRecord * > *DbgVariableRecords=nullptr)
Finds the debug info intrinsics describing a value.
auto successors(const MachineBasicBlock *BB)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
auto predecessors(const MachineBasicBlock *BB)
void updateLoopMetadataDebugLocations(Instruction &I, function_ref< Metadata *(Metadata *)> Updater)
Update the debug locations contained within the MD_loop metadata attached to the instruction I,...
Representative of a block.
Distribution of unscaled probability weight.