73#define DEBUG_TYPE "code-extractor"
81 cl::desc(
"Aggregate arguments to code-extracted functions"));
86 bool AllowVarArgs,
bool AllowAlloca) {
99 while (!ToVisit.
empty()) {
101 if (!Visited.
insert(Curr).second)
103 if (isa<BlockAddress const>(Curr))
106 if (isa<Instruction>(Curr) && cast<Instruction>(Curr)->
getParent() != &BB)
109 for (
auto const &U : Curr->
operands()) {
110 if (
auto *UU = dyn_cast<User>(U))
118 if (isa<AllocaInst>(
I)) {
124 if (
const auto *
II = dyn_cast<InvokeInst>(
I)) {
127 if (
auto *UBB =
II->getUnwindDest())
128 if (!Result.count(UBB))
135 if (
const auto *CSI = dyn_cast<CatchSwitchInst>(
I)) {
136 if (
auto *UBB = CSI->getUnwindDest())
137 if (!Result.count(UBB))
139 for (
const auto *HBB : CSI->handlers())
140 if (!Result.count(
const_cast<BasicBlock*
>(HBB)))
147 if (
const auto *CPI = dyn_cast<CatchPadInst>(
I)) {
148 for (
const auto *U : CPI->users())
149 if (
const auto *CRI = dyn_cast<CatchReturnInst>(U))
150 if (!Result.count(
const_cast<BasicBlock*
>(CRI->getParent())))
158 if (
const auto *CPI = dyn_cast<CleanupPadInst>(
I)) {
159 for (
const auto *U : CPI->users())
160 if (
const auto *CRI = dyn_cast<CleanupReturnInst>(U))
161 if (!Result.count(
const_cast<BasicBlock*
>(CRI->getParent())))
165 if (
const auto *CRI = dyn_cast<CleanupReturnInst>(
I)) {
166 if (
auto *UBB = CRI->getUnwindDest())
167 if (!Result.count(UBB))
172 if (
const CallInst *CI = dyn_cast<CallInst>(
I)) {
173 if (
const Function *
F = CI->getCalledFunction()) {
174 auto IID =
F->getIntrinsicID();
175 if (IID == Intrinsic::vastart) {
184 if (IID == Intrinsic::eh_typeid_for)
196 bool AllowVarArgs,
bool AllowAlloca) {
197 assert(!BBs.
empty() &&
"The set of blocks to extract must be non-empty");
207 if (!Result.insert(BB))
211 LLVM_DEBUG(
dbgs() <<
"Region front block: " << Result.front()->getName()
214 for (
auto *BB : Result) {
219 if (BB == Result.front()) {
221 LLVM_DEBUG(
dbgs() <<
"The first block cannot be an unwind block\n");
230 if (!Result.count(PBB)) {
231 LLVM_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()
246 bool AllowVarArgs,
bool AllowAlloca,
247 BasicBlock *AllocationBlock, std::string Suffix,
248 bool ArgsInZeroAddressSpace)
250 BPI(BPI), AC(AC), AllocationBlock(AllocationBlock),
251 AllowVarArgs(AllowVarArgs),
253 Suffix(Suffix), ArgsInZeroAddressSpace(ArgsInZeroAddressSpace) {}
259 if (
Blocks.count(
I->getParent()))
268 if (isa<Argument>(V))
return true;
270 if (!
Blocks.count(
I->getParent()))
282 if (!CommonExitBlock) {
283 CommonExitBlock = Succ;
286 if (CommonExitBlock != Succ)
295 return CommonExitBlock;
301 if (
auto *AI = dyn_cast<AllocaInst>(&
II))
302 Allocas.push_back(AI);
304 findSideEffectInfoForBlock(BB);
308void CodeExtractorAnalysisCache::findSideEffectInfoForBlock(
BasicBlock &BB) {
310 unsigned Opcode =
II.getOpcode();
311 Value *MemAddr =
nullptr;
313 case Instruction::Store:
314 case Instruction::Load: {
315 if (Opcode == Instruction::Store) {
317 MemAddr = SI->getPointerOperand();
323 if (isa<Constant>(MemAddr))
326 if (!isa<AllocaInst>(
Base)) {
327 SideEffectingBlocks.insert(&BB);
330 BaseMemAddrs[&BB].insert(
Base);
338 SideEffectingBlocks.insert(&BB);
342 if (
II.mayHaveSideEffects()) {
343 SideEffectingBlocks.insert(&BB);
353 if (SideEffectingBlocks.count(&BB))
355 auto It = BaseMemAddrs.find(&BB);
356 if (It != BaseMemAddrs.end())
357 return It->second.count(
Addr);
363 AllocaInst *AI = cast<AllocaInst>(
Addr->stripInBoundsConstantOffsets());
366 if (Blocks.count(&BB))
376 BasicBlock *SinglePredFromOutlineRegion =
nullptr;
377 assert(!Blocks.count(CommonExitBlock) &&
378 "Expect a block outside the region!");
380 if (!Blocks.count(Pred))
382 if (!SinglePredFromOutlineRegion) {
383 SinglePredFromOutlineRegion = Pred;
384 }
else if (SinglePredFromOutlineRegion != Pred) {
385 SinglePredFromOutlineRegion =
nullptr;
390 if (SinglePredFromOutlineRegion)
391 return SinglePredFromOutlineRegion;
397 while (
I != BB->
end()) {
398 PHINode *Phi = dyn_cast<PHINode>(
I);
410 assert(!getFirstPHI(CommonExitBlock) &&
"Phi not expected");
418 if (Blocks.count(Pred))
423 Blocks.insert(CommonExitBlock);
424 OldTargets.push_back(NewExitBlock);
425 return CommonExitBlock;
432CodeExtractor::LifetimeMarkerInfo
436 LifetimeMarkerInfo
Info;
446 Info.LifeStart = IntrInst;
452 Info.LifeEnd = IntrInst;
457 if (isa<DbgInfoIntrinsic>(IntrInst))
465 if (!
Info.LifeStart || !
Info.LifeEnd)
471 if ((
Info.SinkLifeStart ||
Info.HoistLifeEnd) &&
476 if (
Info.HoistLifeEnd && !ExitBlock)
488 auto moveOrIgnoreLifetimeMarkers =
489 [&](
const LifetimeMarkerInfo &LMI) ->
bool {
492 if (LMI.SinkLifeStart) {
495 SinkCands.
insert(LMI.LifeStart);
497 if (LMI.HoistLifeEnd) {
498 LLVM_DEBUG(
dbgs() <<
"Hoisting lifetime.end: " << *LMI.LifeEnd <<
"\n");
499 HoistCands.
insert(LMI.LifeEnd);
508 if (Blocks.count(BB))
517 LifetimeMarkerInfo MarkerInfo = getLifetimeMarkers(CEAC, AI, ExitBlock);
518 bool Moved = moveOrIgnoreLifetimeMarkers(MarkerInfo);
530 for (
User *U : AI->users()) {
534 if (U->stripInBoundsConstantOffsets() != AI)
538 for (
User *BU : Bitcast->users()) {
550 << *Bitcast <<
" in out-of-region lifetime marker "
551 << *IntrInst <<
"\n");
552 LifetimeBitcastUsers.
push_back(IntrInst);
562 I->replaceUsesOfWith(
I->getOperand(1), CastI);
568 for (
User *U : AI->users()) {
569 if (U->stripInBoundsConstantOffsets() == AI) {
571 LifetimeMarkerInfo LMI = getLifetimeMarkers(CEAC, Bitcast, ExitBlock);
587 if (Bitcasts.
empty())
590 LLVM_DEBUG(
dbgs() <<
"Sinking alloca (via bitcast): " << *AI <<
"\n");
592 for (
unsigned I = 0, E = Bitcasts.
size();
I != E; ++
I) {
594 const LifetimeMarkerInfo &LMI = BitcastLifetimeInfo[
I];
596 "Unsafe to sink bitcast without lifetime markers");
597 moveOrIgnoreLifetimeMarkers(LMI);
599 LLVM_DEBUG(
dbgs() <<
"Sinking bitcast-of-alloca: " << *BitcastAddr
601 SinkCands.
insert(BitcastAddr);
615 if (AllowVarArgs &&
F->getFunctionType()->isVarArg()) {
616 auto containsVarArgIntrinsic = [](
const Instruction &
I) {
617 if (
const CallInst *CI = dyn_cast<CallInst>(&
I))
618 if (
const Function *Callee = CI->getCalledFunction())
619 return Callee->getIntrinsicID() == Intrinsic::vastart ||
620 Callee->getIntrinsicID() == Intrinsic::vaend;
624 for (
auto &BB : *
F) {
625 if (Blocks.count(&BB))
640 for (
auto &OI :
II.operands()) {
646 for (
User *U :
II.users())
658void CodeExtractor::severSplitPHINodesOfEntry(
BasicBlock *&Header) {
659 unsigned NumPredsFromRegion = 0;
660 unsigned NumPredsOutsideRegion = 0;
662 if (Header != &Header->getParent()->getEntryBlock()) {
663 PHINode *PN = dyn_cast<PHINode>(Header->begin());
671 ++NumPredsFromRegion;
673 ++NumPredsOutsideRegion;
677 if (NumPredsOutsideRegion <= 1)
return;
689 Blocks.remove(OldPred);
690 Blocks.insert(NewBB);
695 if (NumPredsFromRegion) {
708 for (AfterPHIs = OldPred->
begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
709 PHINode *PN = cast<PHINode>(AfterPHIs);
735void CodeExtractor::severSplitPHINodesOfExits(
740 for (
PHINode &PN : ExitBB->phis()) {
750 if (IncomingVals.
size() <= 1)
757 ExitBB->getName() +
".split",
758 ExitBB->getParent(), ExitBB);
762 if (Blocks.count(PredBB))
763 PredBB->getTerminator()->replaceUsesOfWith(ExitBB, NewBB);
765 Blocks.insert(NewBB);
772 for (
unsigned i : IncomingVals)
774 for (
unsigned i :
reverse(IncomingVals))
781void CodeExtractor::splitReturnBlocks() {
785 Block->splitBasicBlock(RI->getIterator(),
Block->getName() +
".ret");
803Function *CodeExtractor::constructFunction(
const ValueSet &inputs,
804 const ValueSet &outputs,
814 switch (NumExitBlocks) {
821 std::vector<Type *> ParamTy;
822 std::vector<Type *> AggParamTy;
823 ValueSet StructValues;
829 if (AggregateArgs && !ExcludeArgsFromAggregate.
contains(
value)) {
830 AggParamTy.push_back(
value->getType());
831 StructValues.insert(
value);
833 ParamTy.push_back(
value->getType());
837 for (
Value *output : outputs) {
839 if (AggregateArgs && !ExcludeArgsFromAggregate.
contains(output)) {
840 AggParamTy.push_back(output->getType());
841 StructValues.insert(output);
848 (ParamTy.size() + AggParamTy.size()) ==
849 (inputs.size() + outputs.size()) &&
850 "Number of scalar and aggregate params does not match inputs, outputs");
851 assert((StructValues.empty() || AggregateArgs) &&
852 "Expeced StructValues only with AggregateArgs set");
855 size_t NumScalarParams = ParamTy.size();
857 if (AggregateArgs && !AggParamTy.empty()) {
860 StructTy, ArgsInZeroAddressSpace ? 0 :
DL.getAllocaAddrSpace()));
864 dbgs() <<
"Function type: " << *RetTy <<
" f(";
865 for (
Type *i : ParamTy)
866 dbgs() << *i <<
", ";
871 RetTy, ParamTy, AllowVarArgs && oldFunction->
isVarArg());
873 std::string SuffixToUse =
880 oldFunction->
getName() +
"." + SuffixToUse, M);
891 if (Attr.isStringAttribute()) {
892 if (Attr.getKindAsString() ==
"thunk")
895 switch (Attr.getKindAsEnum()) {
898 case Attribute::AllocSize:
899 case Attribute::Builtin:
900 case Attribute::Convergent:
901 case Attribute::JumpTable:
902 case Attribute::Naked:
903 case Attribute::NoBuiltin:
904 case Attribute::NoMerge:
905 case Attribute::NoReturn:
906 case Attribute::NoSync:
907 case Attribute::ReturnsTwice:
908 case Attribute::Speculatable:
909 case Attribute::StackAlignment:
910 case Attribute::WillReturn:
911 case Attribute::AllocKind:
912 case Attribute::PresplitCoroutine:
913 case Attribute::Memory:
914 case Attribute::NoFPClass:
915 case Attribute::CoroDestroyOnlyWhenComplete:
918 case Attribute::AlwaysInline:
919 case Attribute::Cold:
920 case Attribute::DisableSanitizerInstrumentation:
921 case Attribute::FnRetThunkExtern:
923 case Attribute::HybridPatchable:
924 case Attribute::NoRecurse:
925 case Attribute::InlineHint:
926 case Attribute::MinSize:
927 case Attribute::NoCallback:
928 case Attribute::NoDuplicate:
929 case Attribute::NoFree:
930 case Attribute::NoImplicitFloat:
931 case Attribute::NoInline:
932 case Attribute::NonLazyBind:
933 case Attribute::NoRedZone:
934 case Attribute::NoUnwind:
935 case Attribute::NoSanitizeBounds:
936 case Attribute::NoSanitizeCoverage:
937 case Attribute::NullPointerIsValid:
938 case Attribute::OptimizeForDebugging:
939 case Attribute::OptForFuzzing:
940 case Attribute::OptimizeNone:
941 case Attribute::OptimizeForSize:
942 case Attribute::SafeStack:
943 case Attribute::ShadowCallStack:
944 case Attribute::SanitizeAddress:
945 case Attribute::SanitizeMemory:
946 case Attribute::SanitizeNumericalStability:
947 case Attribute::SanitizeThread:
948 case Attribute::SanitizeHWAddress:
949 case Attribute::SanitizeMemTag:
950 case Attribute::SanitizeRealtime:
951 case Attribute::SpeculativeLoadHardening:
952 case Attribute::StackProtect:
953 case Attribute::StackProtectReq:
954 case Attribute::StackProtectStrong:
955 case Attribute::StrictFP:
956 case Attribute::UWTable:
957 case Attribute::VScaleRange:
958 case Attribute::NoCfCheck:
959 case Attribute::MustProgress:
960 case Attribute::NoProfile:
961 case Attribute::SkipProfile:
964 case Attribute::Alignment:
965 case Attribute::AllocatedPointer:
966 case Attribute::AllocAlign:
967 case Attribute::ByVal:
968 case Attribute::Dereferenceable:
969 case Attribute::DereferenceableOrNull:
970 case Attribute::ElementType:
971 case Attribute::InAlloca:
972 case Attribute::InReg:
973 case Attribute::Nest:
974 case Attribute::NoAlias:
975 case Attribute::NoCapture:
976 case Attribute::NoUndef:
977 case Attribute::NonNull:
978 case Attribute::Preallocated:
979 case Attribute::ReadNone:
980 case Attribute::ReadOnly:
981 case Attribute::Returned:
982 case Attribute::SExt:
983 case Attribute::StructRet:
984 case Attribute::SwiftError:
985 case Attribute::SwiftSelf:
986 case Attribute::SwiftAsync:
987 case Attribute::ZExt:
988 case Attribute::ImmArg:
989 case Attribute::ByRef:
990 case Attribute::WriteOnly:
991 case Attribute::Writable:
992 case Attribute::DeadOnUnwind:
993 case Attribute::Range:
994 case Attribute::Initializes:
1006 if (NumExitBlocks == 0) {
1012 return isa<ReturnInst>(Term) || isa<ResumeInst>(Term);
1017 newFunction->
insert(newFunction->
end(), newRootNode);
1026 for (
unsigned i = 0, e = inputs.size(), aggIdx = 0; i != e; ++i) {
1028 if (AggregateArgs && StructValues.contains(inputs[i])) {
1034 StructTy, &*AggAI,
Idx,
"gep_" + inputs[i]->
getName(), TI);
1036 "loadgep_" + inputs[i]->getName(), TI);
1039 RewriteVal = &*ScalarAI++;
1041 std::vector<User *>
Users(inputs[i]->user_begin(), inputs[i]->user_end());
1044 if (
Blocks.count(inst->getParent()))
1045 inst->replaceUsesOfWith(inputs[i], RewriteVal);
1049 if (NumScalarParams) {
1051 for (
unsigned i = 0, e = inputs.size(); i != e; ++i, ++ScalarAI)
1052 if (!StructValues.contains(inputs[i]))
1054 for (
unsigned i = 0, e = outputs.size(); i != e; ++i, ++ScalarAI)
1055 if (!StructValues.contains(outputs[i]))
1063 for (
auto &U :
Users)
1067 if (
I->isTerminator() &&
I->getFunction() == oldFunction &&
1068 !
Blocks.count(
I->getParent()))
1069 I->replaceUsesOfWith(header, newHeader);
1085 auto *
II = dyn_cast<IntrinsicInst>(&
I);
1086 if (!
II || !
II->isLifetimeStartOrEnd())
1092 Value *Mem =
II->getOperand(1)->stripInBoundsOffsets();
1096 if (
II->getIntrinsicID() == Intrinsic::lifetime_start)
1097 LifetimesStart.
insert(Mem);
1098 II->eraseFromParent();
1115 bool InsertBefore) {
1116 for (
Value *Mem : Objects) {
1119 "Input memory not defined in original function");
1126 Marker->insertBefore(Term);
1130 if (!LifetimesStart.
empty()) {
1131 insertMarkers(Intrinsic::lifetime_start, LifetimesStart,
1135 if (!LifetimesEnd.
empty()) {
1136 insertMarkers(Intrinsic::lifetime_end, LifetimesEnd,
1147 ValueSet &outputs) {
1150 std::vector<Value *> params, ReloadOutputs, Reloads;
1151 ValueSet StructValues;
1159 unsigned ScalarInputArgNo = 0;
1161 for (
Value *input : inputs) {
1162 if (AggregateArgs && !ExcludeArgsFromAggregate.
contains(input))
1163 StructValues.
insert(input);
1165 params.push_back(input);
1166 if (input->isSwiftError())
1167 SwiftErrorArgs.
push_back(ScalarInputArgNo);
1173 unsigned ScalarOutputArgNo = 0;
1174 for (
Value *output : outputs) {
1175 if (AggregateArgs && !ExcludeArgsFromAggregate.
contains(output)) {
1176 StructValues.insert(output);
1179 new AllocaInst(output->getType(),
DL.getAllocaAddrSpace(),
1180 nullptr, output->
getName() +
".loc",
1182 ReloadOutputs.push_back(alloca);
1183 params.push_back(alloca);
1184 ++ScalarOutputArgNo;
1190 unsigned NumAggregatedInputs = 0;
1191 if (AggregateArgs && !StructValues.empty()) {
1192 std::vector<Type *> ArgTypes;
1193 for (
Value *V : StructValues)
1194 ArgTypes.push_back(
V->getType());
1199 StructArgTy,
DL.getAllocaAddrSpace(),
nullptr,
"structArg",
1203 if (ArgsInZeroAddressSpace &&
DL.getAllocaAddrSpace() != 0) {
1205 Struct, PointerType ::get(Context, 0),
"structArg.ascast");
1206 StructSpaceCast->insertAfter(
Struct);
1207 params.push_back(StructSpaceCast);
1209 params.push_back(
Struct);
1212 for (
unsigned i = 0, e = StructValues.size(); i != e; ++i) {
1213 if (inputs.contains(StructValues[i])) {
1219 GEP->insertInto(codeReplacer, codeReplacer->
end());
1221 NumAggregatedInputs++;
1228 NumExitBlocks > 1 ?
"targetBlock" :
"");
1240 for (
unsigned SwiftErrArgNo : SwiftErrorArgs) {
1241 call->
addParamAttr(SwiftErrArgNo, Attribute::SwiftError);
1242 newFunction->
addParamAttr(SwiftErrArgNo, Attribute::SwiftError);
1247 for (
unsigned i = 0, e = outputs.size(), scalarIdx = 0,
1248 aggIdx = NumAggregatedInputs;
1250 Value *Output =
nullptr;
1251 if (AggregateArgs && StructValues.contains(outputs[i])) {
1257 GEP->insertInto(codeReplacer, codeReplacer->
end());
1261 Output = ReloadOutputs[scalarIdx];
1265 outputs[i]->
getName() +
".reload",
1267 Reloads.push_back(
load);
1268 std::vector<User *>
Users(outputs[i]->user_begin(), outputs[i]->user_end());
1279 codeReplacer, 0, codeReplacer);
1286 std::map<BasicBlock *, BasicBlock *> ExitBlockMap;
1290 unsigned switchVal = 0;
1292 if (
Blocks.count(OldTarget))
1294 BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
1301 OldTarget->getName() +
".exitStub",
1303 unsigned SuccNum = switchVal++;
1305 Value *brVal =
nullptr;
1306 assert(NumExitBlocks < 0xffff &&
"too many exit blocks for switch");
1307 switch (NumExitBlocks) {
1333 BasicBlock *NewTarget = ExitBlockMap[OldTarget];
1334 assert(NewTarget &&
"Unknown target block!");
1345 std::advance(ScalarOutputArgBegin, ScalarInputArgNo);
1347 std::advance(AggOutputArgBegin, ScalarInputArgNo + ScalarOutputArgNo);
1349 for (
unsigned i = 0, e = outputs.size(), aggIdx = NumAggregatedInputs; i != e;
1351 auto *OutI = dyn_cast<Instruction>(outputs[i]);
1359 if (
auto *InvokeI = dyn_cast<InvokeInst>(OutI))
1360 InsertPt = InvokeI->getNormalDest()->getFirstInsertionPt();
1361 else if (
auto *Phi = dyn_cast<PHINode>(OutI))
1362 InsertPt =
Phi->getParent()->getFirstInsertionPt();
1364 InsertPt = std::next(OutI->getIterator());
1366 assert((InsertPt->getFunction() == newFunction ||
1367 Blocks.count(InsertPt->getParent())) &&
1368 "InsertPt should be in new function");
1369 if (AggregateArgs && StructValues.contains(outputs[i])) {
1371 "Number of aggregate output arguments should match "
1372 "the number of defined values");
1377 StructArgTy, &*AggOutputArgBegin,
Idx,
"gep_" + outputs[i]->
getName(),
1386 "Number of scalar output arguments should match "
1387 "the number of defined values");
1388 new StoreInst(outputs[i], &*ScalarOutputArgBegin, InsertPt);
1389 ++ScalarOutputArgBegin;
1394 Type *OldFnRetTy = TheSwitch->
getParent()->getParent()->getReturnType();
1395 switch (NumExitBlocks) {
1403 }
else if (OldFnRetTy->
isVoidTy()) {
1448void CodeExtractor::moveCodeToFunction(
Function *newFunction) {
1452 Block->removeFromParent();
1459 newFuncIt = newFunction->
insert(std::next(newFuncIt),
Block);
1463void CodeExtractor::calculateNewCallTerminatorWeights(
1475 Distribution BranchDist;
1482 BlockNode ExitNode(i);
1485 BranchDist.addExit(ExitNode, ExitFreq);
1491 if (BranchDist.Total == 0) {
1497 BranchDist.normalize();
1500 for (
unsigned I = 0, E = BranchDist.Weights.size();
I < E; ++
I) {
1501 const auto &Weight = BranchDist.Weights[
I];
1504 BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
1506 EdgeProbabilities[Weight.TargetNode.Index] = BP;
1510 LLVMContext::MD_prof,
1522 if (DVI->getFunction() != &
F)
1523 DVI->eraseFromParent();
1525 if (DVR->getFunction() != &
F)
1526 DVR->eraseFromParent();
1549 assert(OldSP->getUnit() &&
"Missing compile unit for subprogram");
1555 DISubprogram::SPFlagOptimized |
1556 DISubprogram::SPFlagLocalToUnit;
1559 0, SPType, 0, DINode::FlagZero, SPFlags);
1562 auto IsInvalidLocation = [&NewFunc](
Value *Location) {
1566 (!isa<Constant>(Location) && !isa<Instruction>(Location)))
1568 Instruction *LocationInst = dyn_cast<Instruction>(Location);
1569 return LocationInst && LocationInst->
getFunction() != &NewFunc;
1584 DINode *&NewVar = RemappedMetadata[OldVar];
1587 *OldVar->getScope(), *NewSP, Ctx, Cache);
1589 NewScope, OldVar->
getName(), OldVar->getFile(), OldVar->getLine(),
1590 OldVar->getType(),
false, DINode::FlagZero,
1591 OldVar->getAlignInBits());
1593 return cast<DILocalVariable>(NewVar);
1596 auto UpdateDbgLabel = [&](
auto *LabelRecord) {
1599 if (LabelRecord->getDebugLoc().getInlinedAt())
1601 DILabel *OldLabel = LabelRecord->getLabel();
1602 DINode *&NewLabel = RemappedMetadata[OldLabel];
1605 *OldLabel->
getScope(), *NewSP, Ctx, Cache);
1609 LabelRecord->setLabel(cast<DILabel>(NewLabel));
1612 auto UpdateDbgRecordsOnInst = [&](
Instruction &
I) ->
void {
1613 for (
DbgRecord &DR :
I.getDbgRecordRange()) {
1615 UpdateDbgLabel(DLR);
1636 UpdateDbgRecordsOnInst(
I);
1638 auto *DII = dyn_cast<DbgInfoIntrinsic>(&
I);
1644 if (
auto *DLI = dyn_cast<DbgLabelInst>(&
I)) {
1645 UpdateDbgLabel(DLI);
1649 auto *DVI = cast<DbgVariableIntrinsic>(DII);
1651 if (
any_of(DVI->location_ops(), IsInvalidLocation)) {
1656 if (
auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);
1657 DAI && IsInvalidLocation(DAI->getAddress())) {
1663 if (!DVI->getDebugLoc().getInlinedAt())
1664 DVI->setVariable(GetUpdatedDIVariable(DVI->getVariable()));
1667 for (
auto *DII : DebugIntrinsicsToDelete)
1668 DII->eraseFromParent();
1669 for (
auto *DVR : DVRsToDelete)
1670 DVR->getMarker()->MarkedInstr->dropOneDbgRecord(DVR);
1682 *NewSP, Ctx, Cache));
1685 auto updateLoopInfoLoc = [&Ctx, &Cache, NewSP](
Metadata *MD) ->
Metadata * {
1686 if (
auto *Loc = dyn_cast_or_null<DILocation>(MD))
1720 assert(BPI &&
"Both BPI and BFI are required to preserve profile info");
1733 if (
auto *AI = dyn_cast<AssumeInst>(&
I)) {
1736 AI->eraseFromParent();
1743 splitReturnBlocks();
1751 if (!
Blocks.count(Succ)) {
1761 NumExitBlocks = ExitBlocks.
size();
1765 if (!
Blocks.contains(OldTarget))
1766 OldTargets.push_back(OldTarget);
1770 severSplitPHINodesOfEntry(header);
1771 severSplitPHINodesOfExits(ExitBlocks);
1775 "codeRepl", oldFunction,
1793 if (!
I.getDebugLoc())
1797 if (isa<DbgInfoIntrinsic>(
I))
1799 BranchI->setDebugLoc(
I.getDebugLoc());
1804 BranchI->insertInto(newFuncRoot, newFuncRoot->
end());
1806 ValueSet SinkingCands, HoistingCands;
1808 findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit);
1818 for (
auto *
II : SinkingCands) {
1819 if (
auto *AI = dyn_cast<AllocaInst>(
II)) {
1821 if (!FirstSunkAlloca)
1822 FirstSunkAlloca = AI;
1825 assert((SinkingCands.
empty() || FirstSunkAlloca) &&
1826 "Did not expect a sink candidate without any allocas");
1827 for (
auto *
II : SinkingCands) {
1828 if (!isa<AllocaInst>(
II)) {
1829 cast<Instruction>(
II)->moveAfter(FirstSunkAlloca);
1833 if (!HoistingCands.
empty()) {
1836 for (
auto *
II : HoistingCands)
1849 constructFunction(inputs, outputs, header, newFuncRoot, codeReplacer,
1862 emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
1864 moveCodeToFunction(newFunction);
1876 if (BFI && NumExitBlocks > 1)
1877 calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
1889 for (
PHINode &PN : ExitBB->phis()) {
1890 Value *IncomingCodeReplacerVal =
nullptr;
1897 if (!IncomingCodeReplacerVal) {
1902 "PHI has two incompatbile incoming values from codeRepl");
1909 newFunction->
dump();
1923 auto *
I = dyn_cast_or_null<CallInst>(AssumeVH);
1928 if (
I->getFunction() != &OldFunc)
1935 auto *AffectedCI = dyn_cast_or_null<CallInst>(AffectedValVH);
1938 if (AffectedCI->getFunction() != &OldFunc)
1940 auto *AssumedInst = cast<Instruction>(AffectedCI->getOperand(0));
1941 if (AssumedInst->getFunction() != &OldFunc)
1949 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
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...
iv Induction Variable Users
Move duplicate certain instructions close to their use
Module.h This file contains the declarations for the Module class.
uint64_t IntrinsicInst * II
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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
Iterator returning form of getFirstNonPHI.
InstListType::const_iterator const_iterator
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
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.
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.
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
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
const BasicBlock & front() 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.
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 ...
InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
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.
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 size() const
Determine the number of elements in the SetVector.
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.
iterator insert(iterator I, T &&Elt)
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.
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()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
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.
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.