LLVM: lib/Transforms/Utils/CodeExtractor.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
62#include
63#include
64#include
65#include
66#include
67
68using namespace llvm;
71
72#define DEBUG_TYPE "code-extractor"
73
74
75
76
77
80 cl::desc("Aggregate arguments to code-extracted functions"));
81
82
85 bool AllowVarArgs, bool AllowAlloca) {
86
88 return false;
89
90
91
94
95 while (!ToVisit.empty()) {
97 if (!Visited.insert(Curr).second)
98 continue;
100 return false;
101
103 continue;
104
105 for (auto const &U : Curr->operands()) {
108 }
109 }
110
111
112
115 if (!AllowAlloca)
116 return false;
117 continue;
118 }
119
121
122
123 if (auto *UBB = II->getUnwindDest())
124 if (!Result.count(UBB))
125 return false;
126 continue;
127 }
128
129
130
132 if (auto *UBB = CSI->getUnwindDest())
133 if (!Result.count(UBB))
134 return false;
135 for (const auto *HBB : CSI->handlers())
136 if (!Result.count(const_cast<BasicBlock*>(HBB)))
137 return false;
138 continue;
139 }
140
141
142
144 for (const auto *U : CPI->users())
146 if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
147 return false;
148 continue;
149 }
150
151
152
153
155 for (const auto *U : CPI->users())
157 if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
158 return false;
159 continue;
160 }
162 if (auto *UBB = CRI->getUnwindDest())
163 if (!Result.count(UBB))
164 return false;
165 continue;
166 }
167
169
170
171
172
173
174 if (CI->isMustTailCall())
175 return false;
176
177 if (const Function *F = CI->getCalledFunction()) {
178 auto IID = F->getIntrinsicID();
179 if (IID == Intrinsic::vastart) {
180 if (AllowVarArgs)
181 continue;
182 else
183 return false;
184 }
185
186
187
188 if (IID == Intrinsic::eh_typeid_for)
189 return false;
190 }
191 }
192 }
193
194 return true;
195}
196
197
200 bool AllowVarArgs, bool AllowAlloca) {
201 assert(!BBs.empty() && "The set of blocks to extract must be non-empty");
203
204
205
207
209 continue;
210
211 if (!Result.insert(BB))
212 llvm_unreachable("Repeated basic blocks in extraction input");
213 }
214
215 LLVM_DEBUG(dbgs() << "Region front block: " << Result.front()->getName()
216 << '\n');
217
218 for (auto *BB : Result) {
220 return {};
221
222
223 if (BB == Result.front()) {
224 if (BB->isEHPad()) {
225 LLVM_DEBUG(dbgs() << "The first block cannot be an unwind block\n");
226 return {};
227 }
228 continue;
229 }
230
231
232
234 if (!Result.count(PBB)) {
235 LLVM_DEBUG(dbgs() << "No blocks in this region may have entries from "
236 "outside the region except for the first block!\n"
237 << "Problematic source BB: " << BB->getName() << "\n"
238 << "Problematic destination BB: " << PBB->getName()
239 << "\n");
240 return {};
241 }
242 }
243
244 return Result;
245}
246
247
248
250 switch (TargetTriple.getArch()) {
253 return true;
254
255
256 default:
257 return false;
258 }
259 return false;
260}
261
265 bool AllowVarArgs, bool AllowAlloca,
266 BasicBlock *AllocationBlock, std::string Suffix,
267 bool ArgsInZeroAddressSpace)
268 : DT(DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
269 BPI(BPI), AC(AC), AllocationBlock(AllocationBlock),
270 AllowVarArgs(AllowVarArgs),
272 Suffix(Suffix), ArgsInZeroAddressSpace(ArgsInZeroAddressSpace) {}
273
274
275
278 if (Blocks.count(I->getParent()))
279 return true;
280 return false;
281}
282
283
284
285
289 if (!Blocks.count(I->getParent()))
290 return true;
291 return false;
292}
293
295 BasicBlock *CommonExitBlock = nullptr;
298
299 if (Blocks.count(Succ))
300 continue;
301 if (!CommonExitBlock) {
302 CommonExitBlock = Succ;
303 continue;
304 }
305 if (CommonExitBlock != Succ)
306 return true;
307 }
308 return false;
309 };
310
311 if (any_of(Blocks, hasNonCommonExitSucc))
312 return nullptr;
313
314 return CommonExitBlock;
315}
316
319 for (Instruction &II : BB.instructionsWithoutDebug())
321 Allocas.push_back(AI);
322
323 findSideEffectInfoForBlock(BB);
324 }
325}
326
327void CodeExtractorAnalysisCache::findSideEffectInfoForBlock(BasicBlock &BB) {
329 unsigned Opcode = II.getOpcode();
330 Value *MemAddr = nullptr;
331 switch (Opcode) {
332 case Instruction::Store:
333 case Instruction::Load: {
334 if (Opcode == Instruction::Store) {
336 MemAddr = SI->getPointerOperand();
337 } else {
340 }
341
343 break;
346 SideEffectingBlocks.insert(&BB);
347 return;
348 }
349 BaseMemAddrs[&BB].insert(Base);
350 break;
351 }
352 default: {
354 if (IntrInst) {
356 break;
357 SideEffectingBlocks.insert(&BB);
358 return;
359 }
360
361 if (II.mayHaveSideEffects()) {
362 SideEffectingBlocks.insert(&BB);
363 return;
364 }
365 }
366 }
367 }
368}
369
372 if (SideEffectingBlocks.count(&BB))
373 return true;
374 auto It = BaseMemAddrs.find(&BB);
375 if (It != BaseMemAddrs.end())
376 return It->second.count(Addr);
377 return false;
378}
379
383 Function *Func = (*Blocks.begin())->getParent();
385 if (Blocks.count(&BB))
386 continue;
388 return false;
389 }
390 return true;
391}
392
395 BasicBlock *SinglePredFromOutlineRegion = nullptr;
396 assert(!Blocks.count(CommonExitBlock) &&
397 "Expect a block outside the region!");
398 for (auto *Pred : predecessors(CommonExitBlock)) {
399 if (!Blocks.count(Pred))
400 continue;
401 if (!SinglePredFromOutlineRegion) {
402 SinglePredFromOutlineRegion = Pred;
403 } else if (SinglePredFromOutlineRegion != Pred) {
404 SinglePredFromOutlineRegion = nullptr;
405 break;
406 }
407 }
408
409 if (SinglePredFromOutlineRegion)
410 return SinglePredFromOutlineRegion;
411
412#ifndef NDEBUG
413 auto getFirstPHI = [](BasicBlock *BB) {
415 PHINode *FirstPhi = nullptr;
418 if (!Phi)
419 break;
420 if (!FirstPhi) {
421 FirstPhi = Phi;
422 break;
423 }
424 }
425 return FirstPhi;
426 };
427
428
429 assert(!getFirstPHI(CommonExitBlock) && "Phi not expected");
430#endif
431
434
437 if (Blocks.count(Pred))
438 continue;
439 Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock);
440 }
441
442 Blocks.insert(CommonExitBlock);
443 return CommonExitBlock;
444}
445
446
447
448
449
450CodeExtractor::LifetimeMarkerInfo
454 LifetimeMarkerInfo Info;
455
458 if (IntrInst) {
459
460
461 if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start) {
462 if (Info.LifeStart)
463 return {};
464 Info.LifeStart = IntrInst;
465 continue;
466 }
467 if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) {
468 if (Info.LifeEnd)
469 return {};
470 Info.LifeEnd = IntrInst;
471 continue;
472 }
473 }
474
476 return {};
477 }
478
479 if (.LifeStart ||
.LifeEnd)
480 return {};
481
484
485 if ((Info.SinkLifeStart || Info.HoistLifeEnd) &&
487 return {};
488
489
490 if (Info.HoistLifeEnd && !ExitBlock)
491 return {};
492
494}
495
497 ValueSet &SinkCands, ValueSet &HoistCands,
499 Function *Func = (*Blocks.begin())->getParent();
501
502 auto moveOrIgnoreLifetimeMarkers =
503 [&](const LifetimeMarkerInfo &LMI) -> bool {
504 if (!LMI.LifeStart)
505 return false;
506 if (LMI.SinkLifeStart) {
507 LLVM_DEBUG(dbgs() << "Sinking lifetime.start: " << *LMI.LifeStart
508 << "\n");
509 SinkCands.insert(LMI.LifeStart);
510 }
511 if (LMI.HoistLifeEnd) {
512 LLVM_DEBUG(dbgs() << "Hoisting lifetime.end: " << *LMI.LifeEnd << "\n");
513 HoistCands.insert(LMI.LifeEnd);
514 }
515 return true;
516 };
517
518
519
522 if (Blocks.count(BB))
523 continue;
524
525
526
528 if (AIFunc != Func)
529 continue;
530
531 LifetimeMarkerInfo MarkerInfo = getLifetimeMarkers(CEAC, AI, ExitBlock);
532 bool Moved = moveOrIgnoreLifetimeMarkers(MarkerInfo);
533 if (Moved) {
534 LLVM_DEBUG(dbgs() << "Sinking alloca: " << *AI << "\n");
535 SinkCands.insert(AI);
536 continue;
537 }
538
539
540
541
542
546 continue;
547
548 if (U->stripInBoundsConstantOffsets() != AI)
549 continue;
550
552 for (User *BU : Bitcast->users()) {
554 if (!IntrInst)
555 continue;
556
558 continue;
559
560 LLVM_DEBUG(dbgs() << "Replace use of extracted region bitcast"
561 << *Bitcast << " in out-of-region lifetime marker "
562 << *IntrInst << "\n");
563 LifetimeBitcastUsers.push_back(IntrInst);
564 }
565 }
566
567 for (Instruction *I : LifetimeBitcastUsers) {
573 I->replaceUsesOfWith(I->getOperand(1), CastI);
574 }
575
576
580 if (U->stripInBoundsConstantOffsets() == AI) {
582 LifetimeMarkerInfo LMI = getLifetimeMarkers(CEAC, Bitcast, ExitBlock);
583 if (LMI.LifeStart) {
585 BitcastLifetimeInfo.push_back(LMI);
586 continue;
587 }
588 }
589
590
592 Bitcasts.clear();
593 break;
594 }
595 }
596
597
598 if (Bitcasts.empty())
599 continue;
600
601 LLVM_DEBUG(dbgs() << "Sinking alloca (via bitcast): " << *AI << "\n");
602 SinkCands.insert(AI);
603 for (unsigned I = 0, E = Bitcasts.size(); I != E; ++I) {
605 const LifetimeMarkerInfo &LMI = BitcastLifetimeInfo[I];
606 assert(LMI.LifeStart &&
607 "Unsafe to sink bitcast without lifetime markers");
608 moveOrIgnoreLifetimeMarkers(LMI);
610 LLVM_DEBUG(dbgs() << "Sinking bitcast-of-alloca: " << *BitcastAddr
611 << "\n");
612 SinkCands.insert(BitcastAddr);
613 }
614 }
615 }
616}
617
619 if (Blocks.empty())
620 return false;
622 Function *F = Header->getParent();
623
624
625
626 if (AllowVarArgs && F->getFunctionType()->isVarArg()) {
627 auto containsVarArgIntrinsic = [](const Instruction &I) {
629 if (const Function *Callee = CI->getCalledFunction())
630 return Callee->getIntrinsicID() == Intrinsic::vastart ||
631 Callee->getIntrinsicID() == Intrinsic::vaend;
632 return false;
633 };
634
635 for (auto &BB : *F) {
636 if (Blocks.count(&BB))
637 continue;
638 if (llvm::any_of(BB, containsVarArgIntrinsic))
639 return false;
640 }
641 }
642
643
644
648 if ()
649 continue;
650 bool IsSave = II->getIntrinsicID() == Intrinsic::stacksave;
651 bool IsRestore = II->getIntrinsicID() == Intrinsic::stackrestore;
652 if (IsSave && any_of(II->users(), [&Blks = this->Blocks](User *U) {
653 return !definedInRegion(Blks, U);
654 }))
655 return false;
657 return false;
658 }
659 }
660 return true;
661}
662
664 const ValueSet &SinkCands,
665 bool CollectGlobalInputs) const {
667
668
670 for (auto &OI : II.operands()) {
672 if (!SinkCands.count(V) &&
676 }
677
678 for (User *U : II.users())
681 break;
682 }
683 }
684 }
685}
686
687
688
689
690void CodeExtractor::severSplitPHINodesOfEntry(BasicBlock *&Header) {
691 unsigned NumPredsFromRegion = 0;
692 unsigned NumPredsOutsideRegion = 0;
693
694 if (Header != &Header->getParent()->getEntryBlock()) {
696 if (!PN) return;
697
698
699
700
703 ++NumPredsFromRegion;
704 else
705 ++NumPredsOutsideRegion;
706
707
708
709 if (NumPredsOutsideRegion <= 1) return;
710 }
711
712
713
714
715
717
718
719
721 Blocks.remove(OldPred);
722 Blocks.insert(NewBB);
723 Header = NewBB;
724
725
726
727 if (NumPredsFromRegion) {
729
730
735 }
736
737
738
740 for (AfterPHIs = OldPred->begin(); isa(AfterPHIs); ++AfterPHIs) {
742
743
749
750
751
756 --i;
757 }
758 }
759 }
760 }
761}
762
763
764
765
766
767void CodeExtractor::severSplitPHINodesOfExits() {
768 for (BasicBlock *ExitBB : ExtractedFuncRetVals) {
770
771 for (PHINode &PN : ExitBB->phis()) {
772
773 SmallVector<unsigned, 2> IncomingVals;
777
778
779
780
781 if (IncomingVals.size() <= 1)
782 continue;
783
784
785
786 if (!NewBB) {
788 ExitBB->getName() + ".split",
789 ExitBB->getParent(), ExitBB);
791 for (BasicBlock *PredBB : Preds)
792 if (Blocks.count(PredBB))
793 PredBB->getTerminator()->replaceUsesOfWith(ExitBB, NewBB);
795 Blocks.insert(NewBB);
796 }
797
798
802 for (unsigned i : IncomingVals)
804 for (unsigned i : reverse(IncomingVals))
807 }
808 }
809}
810
811void CodeExtractor::splitReturnBlocks() {
812 for (BasicBlock *Block : Blocks)
815 Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret");
816 if (DT) {
817
818
821 OldNode->end());
822
824
826 DT->changeImmediateDominator(I, NewNode);
827 }
828 }
829}
830
831Function *CodeExtractor::constructFunctionDeclaration(
832 const ValueSet &inputs, const ValueSet &outputs, BlockFrequency EntryFreq,
833 const Twine &Name, ValueSet &StructValues, StructType *&StructTy) {
834 LLVM_DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
835 LLVM_DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
836
837 Function *oldFunction = Blocks.front()->getParent();
838 Module *M = Blocks.front()->getModule();
839
840
841 std::vector<Type *> ParamTy;
842 std::vector<Type *> AggParamTy;
843 const DataLayout &DL = M->getDataLayout();
844
845
846 for (Value *value : inputs) {
847 LLVM_DEBUG(dbgs() << "value used in func: " << *value << "\n");
848 if (AggregateArgs && !ExcludeArgsFromAggregate.contains(value)) {
849 AggParamTy.push_back(value->getType());
850 StructValues.insert(value);
851 } else
852 ParamTy.push_back(value->getType());
853 }
854
855
856 for (Value *output : outputs) {
857 LLVM_DEBUG(dbgs() << "instr used in func: " << *output << "\n");
858 if (AggregateArgs && !ExcludeArgsFromAggregate.contains(output)) {
859 AggParamTy.push_back(output->getType());
860 StructValues.insert(output);
861 } else
862 ParamTy.push_back(
864 }
865
867 (ParamTy.size() + AggParamTy.size()) ==
868 (inputs.size() + outputs.size()) &&
869 "Number of scalar and aggregate params does not match inputs, outputs");
870 assert((StructValues.empty() || AggregateArgs) &&
871 "Expeced StructValues only with AggregateArgs set");
872
873
874 if (!AggParamTy.empty()) {
877 M->getContext(), ArgsInZeroAddressSpace ? 0 : DL.getAllocaAddrSpace()));
878 }
879
880 Type *RetTy = getSwitchType();
882 dbgs() << "Function type: " << *RetTy << " f(";
883 for (Type *i : ParamTy)
884 dbgs() << *i << ", ";
885 dbgs() << ")\n";
886 });
887
889 RetTy, ParamTy, AllowVarArgs && oldFunction->isVarArg());
890
891
895
896
899
900
901
902
903
904
905
906
907 for (const auto &Attr : oldFunction->getAttributes().getFnAttrs()) {
908 if (Attr.isStringAttribute()) {
909 if (Attr.getKindAsString() == "thunk")
910 continue;
911 } else
912 switch (Attr.getKindAsEnum()) {
913
914
915 case Attribute::AllocSize:
916 case Attribute::Builtin:
917 case Attribute::Convergent:
918 case Attribute::JumpTable:
919 case Attribute::Naked:
920 case Attribute::NoBuiltin:
921 case Attribute::NoMerge:
922 case Attribute::NoReturn:
923 case Attribute::NoSync:
924 case Attribute::ReturnsTwice:
925 case Attribute::Speculatable:
926 case Attribute::StackAlignment:
927 case Attribute::WillReturn:
928 case Attribute::AllocKind:
929 case Attribute::PresplitCoroutine:
930 case Attribute::Memory:
931 case Attribute::NoFPClass:
932 case Attribute::CoroDestroyOnlyWhenComplete:
933 case Attribute::CoroElideSafe:
934 case Attribute::NoDivergenceSource:
935 case Attribute::NoCreateUndefOrPoison:
936 continue;
937
938 case Attribute::AlwaysInline:
939 case Attribute::Cold:
940 case Attribute::DisableSanitizerInstrumentation:
941 case Attribute::FnRetThunkExtern:
942 case Attribute::Hot:
943 case Attribute::HybridPatchable:
944 case Attribute::NoRecurse:
945 case Attribute::InlineHint:
946 case Attribute::MinSize:
947 case Attribute::NoCallback:
948 case Attribute::NoDuplicate:
949 case Attribute::NoFree:
950 case Attribute::NoImplicitFloat:
951 case Attribute::NoInline:
952 case Attribute::NonLazyBind:
953 case Attribute::NoRedZone:
954 case Attribute::NoUnwind:
955 case Attribute::NoSanitizeBounds:
956 case Attribute::NoSanitizeCoverage:
957 case Attribute::NullPointerIsValid:
958 case Attribute::OptimizeForDebugging:
959 case Attribute::OptForFuzzing:
960 case Attribute::OptimizeNone:
961 case Attribute::OptimizeForSize:
962 case Attribute::SafeStack:
963 case Attribute::ShadowCallStack:
964 case Attribute::SanitizeAddress:
965 case Attribute::SanitizeMemory:
966 case Attribute::SanitizeNumericalStability:
967 case Attribute::SanitizeThread:
968 case Attribute::SanitizeType:
969 case Attribute::SanitizeHWAddress:
970 case Attribute::SanitizeMemTag:
971 case Attribute::SanitizeRealtime:
972 case Attribute::SanitizeRealtimeBlocking:
973 case Attribute::SanitizeAllocToken:
974 case Attribute::SpeculativeLoadHardening:
975 case Attribute::StackProtect:
976 case Attribute::StackProtectReq:
977 case Attribute::StackProtectStrong:
978 case Attribute::StrictFP:
979 case Attribute::UWTable:
980 case Attribute::VScaleRange:
981 case Attribute::NoCfCheck:
982 case Attribute::MustProgress:
983 case Attribute::NoProfile:
984 case Attribute::SkipProfile:
985 break;
986
987 case Attribute::Alignment:
988 case Attribute::AllocatedPointer:
989 case Attribute::AllocAlign:
990 case Attribute::ByVal:
991 case Attribute::Captures:
992 case Attribute::Dereferenceable:
993 case Attribute::DereferenceableOrNull:
994 case Attribute::ElementType:
995 case Attribute::InAlloca:
996 case Attribute::InReg:
997 case Attribute::Nest:
998 case Attribute::NoAlias:
999 case Attribute::NoUndef:
1000 case Attribute::NonNull:
1001 case Attribute::Preallocated:
1002 case Attribute::ReadNone:
1003 case Attribute::ReadOnly:
1004 case Attribute::Returned:
1005 case Attribute::SExt:
1006 case Attribute::StructRet:
1007 case Attribute::SwiftError:
1008 case Attribute::SwiftSelf:
1009 case Attribute::SwiftAsync:
1010 case Attribute::ZExt:
1011 case Attribute::ImmArg:
1012 case Attribute::ByRef:
1013 case Attribute::WriteOnly:
1014 case Attribute::Writable:
1015 case Attribute::DeadOnUnwind:
1016 case Attribute::Range:
1017 case Attribute::Initializes:
1018 case Attribute::NoExt:
1019
1024 case Attribute::DeadOnReturn:
1026 }
1027
1029 }
1030
1031
1032
1034
1035
1036 ScalarAI = newFunction->arg_begin();
1037 for (Value *input : inputs) {
1038 if (StructValues.contains(input))
1039 continue;
1040
1041 ScalarAI->setName(input->getName());
1042 if (input->isSwiftError())
1044 Attribute::SwiftError);
1045 ++ScalarAI;
1046 }
1047 for (Value *output : outputs) {
1048 if (StructValues.contains(output))
1049 continue;
1050
1051 ScalarAI->setName(output->getName() + ".out");
1052 ++ScalarAI;
1053 }
1054
1055
1056 if (BFI) {
1057 auto Count = BFI->getProfileCountFromFreq(EntryFreq);
1058 if (Count.has_value())
1061 }
1062
1063 return newFunction;
1064}
1065
1066
1067
1068
1069
1076 if (.getDebugLoc())
1077 return false;
1079 return true;
1080 });
1081 });
1082 }
1083}
1084
1085
1086
1087
1088
1089
1090
1097 if ()
1098 continue;
1099
1100
1101
1102
1103 Value *Mem = II->getOperand(0);
1105 continue;
1106
1107 if (II->getIntrinsicID() == Intrinsic::lifetime_start)
1108 LifetimesStart.insert(Mem);
1109 II->eraseFromParent();
1110 }
1111 }
1112}
1113
1114
1115
1120
1121
1122
1124 bool InsertBefore) {
1125 for (Value *Mem : Objects) {
1128 "Input memory not defined in original function");
1129
1133 if (InsertBefore)
1134 Marker->insertBefore(TheCall->getIterator());
1135 else
1136 Marker->insertBefore(Term->getIterator());
1137 }
1138 };
1139
1140 if (!LifetimesStart.empty()) {
1141 insertMarkers(Intrinsic::lifetime_start, LifetimesStart,
1142 true);
1143 }
1144
1145 if (!LifetimesEnd.empty()) {
1146 insertMarkers(Intrinsic::lifetime_end, LifetimesEnd,
1147 false);
1148 }
1149}
1150
1151void CodeExtractor::moveCodeToFunction(Function *newFunction) {
1152 auto newFuncIt = newFunction->begin();
1153 for (BasicBlock *Block : Blocks) {
1154
1155 Block->removeFromParent();
1156
1157
1158
1159
1160
1161
1162 newFuncIt = newFunction->insert(std::next(newFuncIt), Block);
1163 }
1164}
1165
1166void CodeExtractor::calculateNewCallTerminatorWeights(
1170 using Distribution = BlockFrequencyInfoImplBase::Distribution;
1171 using BlockNode = BlockFrequencyInfoImplBase::BlockNode;
1172
1173
1175 SmallVector<unsigned, 8> BranchWeights(TI->getNumSuccessors(), 0);
1176
1177
1178 Distribution BranchDist;
1179
1182
1183
1184 for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) {
1185 BlockNode ExitNode(i);
1186 uint64_t ExitFreq = ExitWeights.lookup(TI->getSuccessor(i)).getFrequency();
1187 if (ExitFreq != 0)
1188 BranchDist.addExit(ExitNode, ExitFreq);
1189 else
1191 }
1192
1193
1194 if (BranchDist.Total == 0) {
1195 BPI->setEdgeProbability(CodeReplacer, EdgeProbabilities);
1196 return;
1197 }
1198
1199
1200 BranchDist.normalize();
1201
1202
1203 for (unsigned I = 0, E = BranchDist.Weights.size(); I < E; ++I) {
1204 const auto &Weight = BranchDist.Weights[I];
1205
1206
1207 BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
1208 BranchProbability BP(Weight.Amount, BranchDist.Total);
1209 EdgeProbabilities[Weight.TargetNode.Index] = BP;
1210 }
1211 BPI->setEdgeProbability(CodeReplacer, EdgeProbabilities);
1213 LLVMContext::MD_prof,
1214 MDBuilder(TI->getContext()).createBranchWeights(BranchWeights));
1215}
1216
1217
1218
1224 if (DVR->getFunction() != &F)
1225 DVR->eraseFromParent();
1226 }
1227}
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1244
1245 if (!OldSP) {
1246
1248
1250 return;
1251 }
1252
1253
1254
1255
1256 assert(OldSP->getUnit() && "Missing compile unit for subprogram");
1258 OldSP->getUnit());
1261 DISubprogram::SPFlagOptimized |
1262 DISubprogram::SPFlagLocalToUnit;
1265 0, SPType, 0, DINode::FlagZero, SPFlags);
1267
1268 auto UpdateOrInsertDebugRecord = [&](auto *DR, Value *OldLoc, Value *NewLoc,
1270 if (DR->getParent()->getParent() == &NewFunc) {
1271 DR->replaceVariableLocationOp(OldLoc, NewLoc);
1272 return;
1273 }
1274 if (Declare) {
1275 DIB.insertDeclare(NewLoc, DR->getVariable(), Expr, DR->getDebugLoc(),
1277 return;
1278 }
1280 NewLoc, DR->getVariable(), Expr, DR->getDebugLoc(),
1282 };
1283 for (auto [Input, NewVal] : zip_equal(Inputs, NewValues)) {
1287
1288
1289
1290
1291 for (auto *DVR : DPUsers)
1292 UpdateOrInsertDebugRecord(DVR, Input, NewVal, Expr, DVR->isDbgDeclare());
1293 }
1294
1295 auto IsInvalidLocation = [&NewFunc](Value *Location) {
1296
1297
1300 return true;
1301
1303 return Arg->getParent() != &NewFunc;
1305 return LocationInst->getFunction() != &NewFunc;
1306 return false;
1307 };
1308
1309
1310
1311
1312
1313
1314
1318
1319 auto GetUpdatedDIVariable = [&](DILocalVariable *OldVar) {
1320 DINode *&NewVar = RemappedMetadata[OldVar];
1321 if (!NewVar) {
1323 *OldVar->getScope(), *NewSP, Ctx, Cache);
1325 NewScope, OldVar->getName(), OldVar->getFile(), OldVar->getLine(),
1326 OldVar->getType(), false, DINode::FlagZero,
1327 OldVar->getAlignInBits());
1328 }
1330 };
1331
1332 auto UpdateDbgLabel = [&](auto *LabelRecord) {
1333
1334
1335 if (LabelRecord->getDebugLoc().getInlinedAt())
1336 return;
1337 DILabel *OldLabel = LabelRecord->getLabel();
1338 DINode *&NewLabel = RemappedMetadata[OldLabel];
1339 if (!NewLabel) {
1341 *OldLabel->getScope(), *NewSP, Ctx, Cache);
1342 NewLabel =
1346 }
1348 };
1349
1350 auto UpdateDbgRecordsOnInst = [&](Instruction &I) -> void {
1351 for (DbgRecord &DR : I.getDbgRecordRange()) {
1353 UpdateDbgLabel(DLR);
1354 continue;
1355 }
1356
1358
1361 continue;
1362 }
1363
1364
1367 continue;
1368 }
1369
1370
1371
1372
1375 }
1376 };
1377
1379 UpdateDbgRecordsOnInst(I);
1380
1381 for (auto *DVR : DVRsToDelete)
1382 DVR->getMarker()->MarkedInstr->dropOneDbgRecord(DVR);
1384
1385
1386
1389 if (const DebugLoc &DL = I.getDebugLoc())
1390 I.setDebugLoc(
1392 for (DbgRecord &DR : I.getDbgRecordRange())
1394 *NewSP, Ctx, Cache));
1395
1396
1397 auto updateLoopInfoLoc = [&Ctx, &Cache, NewSP](Metadata *MD) -> Metadata * {
1400 return MD;
1401 };
1404 }
1407
1409}
1410
1413 ValueSet Inputs, Outputs;
1415}
1416
1419 ValueSet &inputs, ValueSet &outputs) {
1421 return nullptr;
1422
1423
1424
1427
1428 normalizeCFGForExtraction(header);
1429
1430
1431
1435 if (AC)
1436 AC->unregisterAssumption(AI);
1437 AI->eraseFromParent();
1438 }
1439 }
1440 }
1441
1442 ValueSet SinkingCands, HoistingCands;
1444 findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit);
1445 assert(HoistingCands.empty() || CommonExit);
1446
1447
1449
1450
1451
1452
1453
1454 ValueSet LifetimesStart;
1456
1457 if (!HoistingCands.empty()) {
1459 Instruction *TI = HoistToBlock->getTerminator();
1460 for (auto *II : HoistingCands)
1462 computeExtractedFuncRetVals();
1463 }
1464
1465
1466
1467
1468
1471 if (BFI) {
1472 assert(BPI && "Both BPI and BFI are required to preserve profile info");
1474 if (Blocks.count(Pred))
1475 continue;
1476 EntryFreq +=
1477 BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, header);
1478 }
1479
1480 for (BasicBlock *Succ : ExtractedFuncRetVals) {
1482 if (!Blocks.count(Block))
1483 continue;
1484
1485
1487 BF += BFI->getBlockFreq(Block) * BPI->getEdgeProbability(Block, Succ);
1488 }
1489 }
1490 }
1491
1492
1493
1495 while (ReplIP && Blocks.count(ReplIP))
1497
1498
1499 std::string SuffixToUse =
1501 ? (header->getName().empty() ? "extracted" : header->getName().str())
1502 : Suffix;
1503
1504 ValueSet StructValues;
1506 Function *newFunction = constructFunctionDeclaration(
1507 inputs, outputs, EntryFreq, oldFunction->getName() + "." + SuffixToUse,
1508 StructValues, StructTy);
1510
1511 emitFunctionBody(inputs, outputs, StructValues, newFunction, StructTy, header,
1512 SinkingCands, NewValues);
1513
1514 std::vector<Value *> Reloads;
1515 CallInst *TheCall = emitReplacerCall(
1516 inputs, outputs, StructValues, newFunction, StructTy, oldFunction, ReplIP,
1517 EntryFreq, LifetimesStart.getArrayRef(), Reloads);
1518
1519 insertReplacerCall(oldFunction, header, TheCall->getParent(), outputs,
1520 Reloads, ExitWeights);
1521
1523 NewValues);
1524
1531 return newFunction;
1532}
1533
1534void CodeExtractor::normalizeCFGForExtraction(BasicBlock *&header) {
1535
1536
1537 splitReturnBlocks();
1538
1539
1540 severSplitPHINodesOfEntry(header);
1541
1542
1543
1544
1545
1546 computeExtractedFuncRetVals();
1547 severSplitPHINodesOfExits();
1548}
1549
1550void CodeExtractor::computeExtractedFuncRetVals() {
1551 ExtractedFuncRetVals.clear();
1552
1556 if (Blocks.count(Succ))
1557 continue;
1558
1559 bool IsNew = ExitBlocks.insert(Succ).second;
1560 if (IsNew)
1561 ExtractedFuncRetVals.push_back(Succ);
1562 }
1563 }
1564}
1565
1566Type *CodeExtractor::getSwitchType() {
1568
1569 assert(ExtractedFuncRetVals.size() < 0xffff &&
1570 "too many exit blocks for switch");
1571 switch (ExtractedFuncRetVals.size()) {
1572 case 0:
1573 case 1:
1575 case 2:
1576
1578 default:
1580 }
1581}
1582
1583void CodeExtractor::emitFunctionBody(
1584 const ValueSet &inputs, const ValueSet &outputs,
1585 const ValueSet &StructValues, Function *newFunction,
1590
1591
1592
1595
1596
1597
1598
1599 for (auto *II : SinkingCands) {
1603 }
1604 }
1605 for (auto *II : SinkingCands) {
1608 }
1609 }
1610
1612 Argument *AggArg = StructValues.empty()
1613 ? nullptr
1615
1616
1617
1618 for (unsigned i = 0, e = inputs.size(), aggIdx = 0; i != e; ++i) {
1619 Value *RewriteVal;
1620 if (StructValues.contains(inputs[i])) {
1625 StructArgTy, AggArg, Idx, "gep_" + inputs[i]->getName(), newFuncRoot);
1626 LoadInst *LoadGEP =
1628 "loadgep_" + inputs[i]->getName(), newFuncRoot);
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1641 unsigned AlignmentValue;
1642 const Triple &TargetTriple =
1645
1646
1647
1649 AlignmentValue =
1650 inputs[i]->stripPointerCasts()->getPointerAlignment(DL).value();
1651 else
1652 AlignmentValue = inputs[i]->getPointerAlignment(DL).value();
1653 MDBuilder MDB(header->getContext());
1655 LLVMContext::MD_align,
1658 MDB.createConstant(ConstantInt::get(
1660 }
1661 RewriteVal = LoadGEP;
1662 ++aggIdx;
1663 } else
1664 RewriteVal = &*ScalarAI++;
1665
1666 NewValues.push_back(RewriteVal);
1667 }
1668
1669 moveCodeToFunction(newFunction);
1670
1671 for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
1672 Value *RewriteVal = NewValues[i];
1673
1674 std::vector<User *> Users(inputs[i]->user_begin(), inputs[i]->user_end());
1677 if (Blocks.count(inst->getParent()))
1678 inst->replaceUsesOfWith(inputs[i], RewriteVal);
1679 }
1680
1681
1682
1683
1684
1685
1686 std::map<BasicBlock *, BasicBlock *> ExitBlockMap;
1687
1688
1689
1690 for (auto P : enumerate(ExtractedFuncRetVals)) {
1692 size_t SuccNum = P.index();
1693
1695 Context, OldTarget->getName() + ".exitStub", newFunction);
1696 ExitBlockMap[OldTarget] = NewTarget;
1697
1698 Value *brVal = nullptr;
1699 Type *RetTy = getSwitchType();
1700 assert(ExtractedFuncRetVals.size() < 0xffff &&
1701 "too many exit blocks for switch");
1702 switch (ExtractedFuncRetVals.size()) {
1703 case 0:
1704 case 1:
1705
1706 break;
1707 case 2:
1708 brVal = ConstantInt::get(RetTy, !SuccNum);
1709 break;
1710 default:
1711 brVal = ConstantInt::get(RetTy, SuccNum);
1712 break;
1713 }
1714
1716 }
1717
1718 for (BasicBlock *Block : Blocks) {
1720 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1722 continue;
1724
1725 BasicBlock *NewTarget = ExitBlockMap[OldTarget];
1726 assert(NewTarget && "Unknown target block!");
1727
1728
1730 }
1731 }
1732
1733
1734
1740 }
1741
1742
1745
1746
1747
1748
1749 ScalarAI = newFunction->arg_begin();
1750 unsigned AggIdx = 0;
1751
1752 for (Value *Input : inputs) {
1753 if (StructValues.contains(Input))
1754 ++AggIdx;
1755 else
1756 ++ScalarAI;
1757 }
1758
1759 for (Value *Output : outputs) {
1760
1761
1762
1763
1766 InsertPt = InvokeI->getNormalDest()->getFirstInsertionPt();
1768 InsertPt = Phi->getParent()->getFirstInsertionPt();
1770 InsertPt = std::next(OutI->getIterator());
1771 else {
1772
1773 if (StructValues.contains(Output))
1774 ++AggIdx;
1775 else
1776 ++ScalarAI;
1777 continue;
1778 }
1779
1780 assert((InsertPt->getFunction() == newFunction ||
1781 Blocks.count(InsertPt->getParent())) &&
1782 "InsertPt should be in new function");
1783
1784 if (StructValues.contains(Output)) {
1785 assert(AggArg && "Number of aggregate output arguments should match "
1786 "the number of defined values");
1791 StructArgTy, AggArg, Idx, "gep_" + Output->getName(), InsertPt);
1792 new StoreInst(Output, GEP, InsertPt);
1793 ++AggIdx;
1794 } else {
1796 "Number of scalar output arguments should match "
1797 "the number of defined values");
1798 new StoreInst(Output, &*ScalarAI, InsertPt);
1799 ++ScalarAI;
1800 }
1801 }
1802
1803 if (ExtractedFuncRetVals.empty()) {
1804
1805
1806
1807 if (none_of(Blocks, [](const BasicBlock *BB) {
1810 }))
1812 }
1813}
1814
1815CallInst *CodeExtractor::emitReplacerCall(
1816 const ValueSet &inputs, const ValueSet &outputs,
1817 const ValueSet &StructValues, Function *newFunction,
1820 std::vector<Value *> &Reloads) {
1823 const DataLayout &DL = M->getDataLayout();
1824
1825
1828 if (AllocationBlock)
1829 assert(AllocationBlock->getParent() == oldFunction &&
1830 "AllocationBlock is not in the same function");
1832 AllocationBlock ? AllocationBlock : &oldFunction->getEntryBlock();
1833
1834
1835 if (BFI)
1836 BFI->setBlockFreq(codeReplacer, EntryFreq);
1837
1838 std::vector<Value *> params;
1839
1840
1841 for (Value *input : inputs) {
1842 if (StructValues.contains(input))
1843 continue;
1844
1845 params.push_back(input);
1846 }
1847
1848
1849 std::vector<Value *> ReloadOutputs;
1850 for (Value *output : outputs) {
1851 if (StructValues.contains(output))
1852 continue;
1853
1854 AllocaInst *alloca = new AllocaInst(
1855 output->getType(), DL.getAllocaAddrSpace(), nullptr,
1857 params.push_back(alloca);
1858 ReloadOutputs.push_back(alloca);
1859 }
1860
1861 AllocaInst *Struct = nullptr;
1862 if (!StructValues.empty()) {
1863 Struct = new AllocaInst(StructArgTy, DL.getAllocaAddrSpace(), nullptr,
1865 if (ArgsInZeroAddressSpace && DL.getAllocaAddrSpace() != 0) {
1866 auto *StructSpaceCast = new AddrSpaceCastInst(
1867 Struct, PointerType ::get(Context, 0), "structArg.ascast");
1868 StructSpaceCast->insertAfter(Struct->getIterator());
1869 params.push_back(StructSpaceCast);
1870 } else {
1871 params.push_back(Struct);
1872 }
1873
1874 unsigned AggIdx = 0;
1875 for (Value *input : inputs) {
1876 if (!StructValues.contains(input))
1877 continue;
1878
1883 StructArgTy, Struct, Idx, "gep_" + input->getName());
1884 GEP->insertInto(codeReplacer, codeReplacer->end());
1885 new StoreInst(input, GEP, codeReplacer);
1886
1887 ++AggIdx;
1888 }
1889 }
1890
1891
1893 newFunction, params, ExtractedFuncRetVals.size() > 1 ? "targetBlock" : "",
1894 codeReplacer);
1895
1896
1897 unsigned ParamIdx = 0;
1898 unsigned AggIdx = 0;
1899 for (auto input : inputs) {
1900 if (StructValues.contains(input)) {
1901 ++AggIdx;
1902 } else {
1903 if (input->isSwiftError())
1904 call->addParamAttr(ParamIdx, Attribute::SwiftError);
1905 ++ParamIdx;
1906 }
1907 }
1908
1909
1910
1911
1912
1916 }
1917
1918
1919
1920 for (unsigned i = 0, e = outputs.size(), scalarIdx = 0; i != e; ++i) {
1921 Value *Output = nullptr;
1922 if (StructValues.contains(outputs[i])) {
1927 StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName());
1928 GEP->insertInto(codeReplacer, codeReplacer->end());
1929 Output = GEP;
1930 ++AggIdx;
1931 } else {
1932 Output = ReloadOutputs[scalarIdx];
1933 ++scalarIdx;
1934 }
1935 LoadInst *load =
1936 new LoadInst(outputs[i]->getType(), Output,
1937 outputs[i]->getName() + ".reload", codeReplacer);
1938 Reloads.push_back(load);
1939 }
1940
1941
1942 SwitchInst *TheSwitch =
1944 codeReplacer, 0, codeReplacer);
1945 for (auto P : enumerate(ExtractedFuncRetVals)) {
1947 size_t SuccNum = P.index();
1948
1950 OldTarget);
1951 }
1952
1953
1954 Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType();
1955 switch (ExtractedFuncRetVals.size()) {
1956 case 0:
1957
1958
1959
1961
1963 } else if (OldFnRetTy->isVoidTy()) {
1964
1966 TheSwitch->getIterator());
1968
1971 } else {
1972
1973
1976 }
1977
1979 break;
1980 case 1:
1981
1982
1985 break;
1986 case 2:
1987
1988
1989
1993 break;
1994 default:
1995
1996
1999 TheSwitch->getSuccessor(ExtractedFuncRetVals.size()));
2000
2003 break;
2004 }
2005
2006
2007
2009
2010
2011
2013 {}, call);
2014
2015 return call;
2016}
2017
2018void CodeExtractor::insertReplacerCall(
2022
2023
2024
2025
2027 for (auto &U : Users)
2028
2029
2031 if (I->isTerminator() && I->getFunction() == oldFunction &&
2032 !Blocks.count(I->getParent()))
2033 I->replaceUsesOfWith(header, codeReplacer);
2034
2035
2036
2037
2038
2039 for (BasicBlock *ExitBB : ExtractedFuncRetVals)
2040 for (PHINode &PN : ExitBB->phis()) {
2041 Value *IncomingCodeReplacerVal = nullptr;
2043
2045 continue;
2046
2047
2048 if (!IncomingCodeReplacerVal) {
2051 } else
2053 "PHI has two incompatbile incoming values from codeRepl");
2054 }
2055 }
2056
2057 for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
2059 std::vector<User *> Users(outputs[i]->user_begin(), outputs[i]->user_end());
2060 for (User *U : Users) {
2062 if (inst->getParent()->getParent() == oldFunction)
2064 }
2065 }
2066
2067
2068 if (BFI && ExtractedFuncRetVals.size() > 1)
2069 calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
2070}
2071
2075 for (auto AssumeVH : AC->assumptions()) {
2077 if ()
2078 continue;
2079
2080
2081 if (I->getFunction() != &OldFunc)
2082 return true;
2083
2084
2085
2086
2087 for (auto AffectedValVH : AC->assumptionsFor(I->getOperand(0))) {
2089 if (!AffectedCI)
2090 continue;
2091 if (AffectedCI->getFunction() != &OldFunc)
2092 return true;
2093 auto *AssumedInst = cast(AffectedCI->getOperand(0));
2094 if (AssumedInst->getFunction() != &OldFunc)
2095 return true;
2096 }
2097 }
2098 return false;
2099}
2100
2102 ExcludeArgsFromAggregate.insert(Arg);
2103}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
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
Machine Check Debug Module
uint64_t IntrinsicInst * II
static StringRef getName(Value *V)
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)
static Function * getFunction(FunctionType *Ty, const Twine &Name, Module *M)
The Input class is used to parse a yaml document into in-memory structs and vectors.
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.
@ 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.
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Function * getParent() const
Return the enclosing method, or null if none.
LLVM_ABI 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,...
LLVM_ABI 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.
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
InstListType::iterator iterator
Instruction iterators...
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
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...
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
Analysis providing branch probability information.
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 LLVM_ABI CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI DISubroutineType * createSubroutineType(DITypeRefArray ParameterTypes, DINode::DIFlags Flags=DINode::FlagZero, unsigned CC=0)
Create subroutine type.
LLVM_ABI void finalizeSubprogram(DISubprogram *SP)
Finalize a specific subprogram - no new variables may be added to this subprogram afterwards.
LLVM_ABI 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="", bool UseKeyInstructions=false)
Create a new descriptor for the specified subprogram.
LLVM_ABI DbgInstPtr insertDeclare(llvm::Value *Storage, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, BasicBlock *InsertAtEnd)
Insert a new llvm.dbg.declare intrinsic call.
LLVM_ABI DbgInstPtr insertDbgValueIntrinsic(llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, InsertPosition InsertPt)
Insert a new llvm.dbg.value intrinsic call.
LLVM_ABI DITypeRefArray getOrCreateTypeArray(ArrayRef< Metadata * > Elements)
Get a DITypeRefArray, create one if required.
LLVM_ABI DIExpression * createExpression(ArrayRef< uint64_t > Addr={})
Create a new descriptor for the specified variable which has a complex address expression for its add...
LLVM_ABI 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
bool isArtificial() const
unsigned getColumn() const
DILocalScope * getScope() const
Get the local scope for this label.
std::optional< unsigned > getCoroSuspendIdx() const
static LLVM_ABI 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.
LLVM_ABI StringRef getName() const
Subprogram description. Uses SubclassData1.
DISPFlags
Debug info subprogram flags.
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
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI Value * getAddress() const
void setVariable(DILocalVariable *NewVar)
DILocalVariable * getVariable() const
LLVM_ABI iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
static LLVM_ABI 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.
LLVM_ABI 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...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
static LLVM_ABI 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 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.
const Function & getFunction() const
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.
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).
LLVM_ABI bool isLifetimeStartOrEnd() const LLVM_READONLY
Return true if the instruction is a llvm.lifetime.start or llvm.lifetime.end marker.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
LLVM_ABI 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.
LLVM_ABI void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
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()
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
LLVM_ABI 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.
const Triple & getTargetTriple() const
Get the target triple which is a string describing the target host.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingBlock(unsigned i, BasicBlock *BB)
LLVM_ABI 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 * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
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_arg_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.
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.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
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.
Class to represent struct types.
static LLVM_ABI 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)
LLVM_ABI void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
CaseIteratorImpl< CaseHandle > CaseIt
void setDefaultDest(BasicBlock *DefaultCase)
Value * getCondition() const
LLVM_ABI CaseIt removeCase(CaseIt I)
This method removes the specified case and its successor from the switch instruction.
Triple - Helper class for working with autoconf configuration names.
ArchType getArch() const
Get the parsed architecture type of this triple.
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 LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
bool isPointerTy() const
True if this is an instance of PointerType.
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
bool isVoidTy() const
Return true if this is 'void'.
LLVM_ABI 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()
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
LLVM_ABI const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
iterator_range< user_iterator > users()
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
LLVM_ABI 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.
@ BasicBlock
Various leaf nodes.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
LLVM_ABI void remapAssignID(DenseMap< DIAssignID *, DIAssignID * > &Map, Instruction &I)
Replace DIAssignID uses and attachments with IDs from Map.
NodeAddr< PhiNode * > Phi
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
LLVM_ABI bool stripDebugInfo(Function &F)
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
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...
DomTreeNodeBase< BasicBlock > DomTreeNode
auto dyn_cast_or_null(const Y &Val)
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)
LLVM_ABI 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.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
FunctionAddr VTableAddr Count
Function::ProfileCount ProfileCount
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
LLVM_ABI 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)
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
LLVM_ABI void updateLoopMetadataDebugLocations(Instruction &I, function_ref< Metadata *(Metadata *)> Updater)
Update the debug locations contained within the MD_loop metadata attached to the instruction I,...
LLVM_ABI void findDbgUsers(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the debug info records describing a value.