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#include
68
69using namespace llvm;
72
73#define DEBUG_TYPE "code-extractor"
74
75
76
77
78
81 cl::desc("Aggregate arguments to code-extracted functions"));
82
83
86 bool AllowVarArgs, bool AllowAlloca) {
87
89 return false;
90
91
92
95
98
99 while (!ToVisit.empty()) {
101 if (!Visited.insert(Curr).second)
102 continue;
103 if (isa(Curr))
104 return false;
105
106 if (isa(Curr) && cast(Curr)->getParent() != &BB)
107 continue;
108
109 for (auto const &U : Curr->operands()) {
110 if (auto *UU = dyn_cast(U))
112 }
113 }
114
115
116
118 if (isa(I)) {
119 if (!AllowAlloca)
120 return false;
121 continue;
122 }
123
124 if (const auto *II = dyn_cast(I)) {
125
126
127 if (auto *UBB = II->getUnwindDest())
128 if (!Result.count(UBB))
129 return false;
130 continue;
131 }
132
133
134
135 if (const auto *CSI = dyn_cast(I)) {
136 if (auto *UBB = CSI->getUnwindDest())
137 if (!Result.count(UBB))
138 return false;
139 for (const auto *HBB : CSI->handlers())
140 if (!Result.count(const_cast<BasicBlock*>(HBB)))
141 return false;
142 continue;
143 }
144
145
146
147 if (const auto *CPI = dyn_cast(I)) {
148 for (const auto *U : CPI->users())
149 if (const auto *CRI = dyn_cast(U))
150 if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
151 return false;
152 continue;
153 }
154
155
156
157
158 if (const auto *CPI = dyn_cast(I)) {
159 for (const auto *U : CPI->users())
160 if (const auto *CRI = dyn_cast(U))
161 if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
162 return false;
163 continue;
164 }
165 if (const auto *CRI = dyn_cast(I)) {
166 if (auto *UBB = CRI->getUnwindDest())
167 if (!Result.count(UBB))
168 return false;
169 continue;
170 }
171
172 if (const CallInst *CI = dyn_cast(I)) {
173 if (const Function *F = CI->getCalledFunction()) {
174 auto IID = F->getIntrinsicID();
175 if (IID == Intrinsic::vastart) {
176 if (AllowVarArgs)
177 continue;
178 else
179 return false;
180 }
181
182
183
184 if (IID == Intrinsic::eh_typeid_for)
185 return false;
186 }
187 }
188 }
189
190 return true;
191}
192
193
196 bool AllowVarArgs, bool AllowAlloca) {
197 assert(!BBs.empty() && "The set of blocks to extract must be non-empty");
199
200
201
203
205 continue;
206
207 if (!Result.insert(BB))
208 llvm_unreachable("Repeated basic blocks in extraction input");
209 }
210
211 LLVM_DEBUG(dbgs() << "Region front block: " << Result.front()->getName()
212 << '\n');
213
214 for (auto *BB : Result) {
216 return {};
217
218
219 if (BB == Result.front()) {
220 if (BB->isEHPad()) {
221 LLVM_DEBUG(dbgs() << "The first block cannot be an unwind block\n");
222 return {};
223 }
224 continue;
225 }
226
227
228
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()
235 << "\n");
236 return {};
237 }
238 }
239
240 return Result;
241}
242
246 bool AllowVarArgs, bool AllowAlloca,
247 BasicBlock *AllocationBlock, std::string Suffix,
248 bool ArgsInZeroAddressSpace)
249 : DT(DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
250 BPI(BPI), AC(AC), AllocationBlock(AllocationBlock),
251 AllowVarArgs(AllowVarArgs),
253 Suffix(Suffix), ArgsInZeroAddressSpace(ArgsInZeroAddressSpace) {}
254
255
256
258 if (Instruction *I = dyn_cast(V))
259 if (Blocks.count(I->getParent()))
260 return true;
261 return false;
262}
263
264
265
266
268 if (isa(V)) return true;
269 if (Instruction *I = dyn_cast(V))
270 if (.count(I->getParent()))
271 return true;
272 return false;
273}
274
276 BasicBlock *CommonExitBlock = nullptr;
279
280 if (Blocks.count(Succ))
281 continue;
282 if (!CommonExitBlock) {
283 CommonExitBlock = Succ;
284 continue;
285 }
286 if (CommonExitBlock != Succ)
287 return true;
288 }
289 return false;
290 };
291
293 return nullptr;
294
295 return CommonExitBlock;
296}
297
300 for (Instruction &II : BB.instructionsWithoutDebug())
301 if (auto *AI = dyn_cast(&II))
302 Allocas.push_back(AI);
303
304 findSideEffectInfoForBlock(BB);
305 }
306}
307
308void CodeExtractorAnalysisCache::findSideEffectInfoForBlock(BasicBlock &BB) {
310 unsigned Opcode = II.getOpcode();
311 Value *MemAddr = nullptr;
312 switch (Opcode) {
313 case Instruction::Store:
314 case Instruction::Load: {
315 if (Opcode == Instruction::Store) {
317 MemAddr = SI->getPointerOperand();
318 } else {
321 }
322
323 if (isa(MemAddr))
324 break;
326 if (!isa(Base)) {
327 SideEffectingBlocks.insert(&BB);
328 return;
329 }
330 BaseMemAddrs[&BB].insert(Base);
331 break;
332 }
333 default: {
335 if (IntrInst) {
337 break;
338 SideEffectingBlocks.insert(&BB);
339 return;
340 }
341
342 if (II.mayHaveSideEffects()) {
343 SideEffectingBlocks.insert(&BB);
344 return;
345 }
346 }
347 }
348 }
349}
350
353 if (SideEffectingBlocks.count(&BB))
354 return true;
355 auto It = BaseMemAddrs.find(&BB);
356 if (It != BaseMemAddrs.end())
357 return It->second.count(Addr);
358 return false;
359}
360
363 AllocaInst *AI = cast(Addr->stripInBoundsConstantOffsets());
366 if (Blocks.count(&BB))
367 continue;
369 return false;
370 }
371 return true;
372}
373
376 BasicBlock *SinglePredFromOutlineRegion = nullptr;
377 assert(!Blocks.count(CommonExitBlock) &&
378 "Expect a block outside the region!");
379 for (auto *Pred : predecessors(CommonExitBlock)) {
380 if (!Blocks.count(Pred))
381 continue;
382 if (!SinglePredFromOutlineRegion) {
383 SinglePredFromOutlineRegion = Pred;
384 } else if (SinglePredFromOutlineRegion != Pred) {
385 SinglePredFromOutlineRegion = nullptr;
386 break;
387 }
388 }
389
390 if (SinglePredFromOutlineRegion)
391 return SinglePredFromOutlineRegion;
392
393#ifndef NDEBUG
394 auto getFirstPHI = [](BasicBlock *BB) {
396 PHINode *FirstPhi = nullptr;
398 PHINode *Phi = dyn_cast(I);
399 if (!Phi)
400 break;
401 if (!FirstPhi) {
402 FirstPhi = Phi;
403 break;
404 }
405 }
406 return FirstPhi;
407 };
408
409
410 assert(!getFirstPHI(CommonExitBlock) && "Phi not expected");
411#endif
412
415
418 if (Blocks.count(Pred))
419 continue;
421 }
422
423 Blocks.insert(CommonExitBlock);
424 return CommonExitBlock;
425}
426
427
428
429
430
431CodeExtractor::LifetimeMarkerInfo
435 LifetimeMarkerInfo Info;
436
437 for (User *U : Addr->users()) {
438 IntrinsicInst *IntrInst = dyn_cast(U);
439 if (IntrInst) {
440
441
442 if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start) {
443 if (Info.LifeStart)
444 return {};
445 Info.LifeStart = IntrInst;
446 continue;
447 }
448 if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) {
449 if (Info.LifeEnd)
450 return {};
451 Info.LifeEnd = IntrInst;
452 continue;
453 }
454
455
456 if (isa(IntrInst))
457 continue;
458 }
459
461 return {};
462 }
463
464 if (.LifeStart ||
.LifeEnd)
465 return {};
466
469
470 if ((Info.SinkLifeStart || Info.HoistLifeEnd) &&
472 return {};
473
474
475 if (Info.HoistLifeEnd && !ExitBlock)
476 return {};
477
479}
480
486
487 auto moveOrIgnoreLifetimeMarkers =
488 [&](const LifetimeMarkerInfo &LMI) -> bool {
489 if (!LMI.LifeStart)
490 return false;
491 if (LMI.SinkLifeStart) {
492 LLVM_DEBUG(dbgs() << "Sinking lifetime.start: " << *LMI.LifeStart
493 << "\n");
494 SinkCands.insert(LMI.LifeStart);
495 }
496 if (LMI.HoistLifeEnd) {
497 LLVM_DEBUG(dbgs() << "Hoisting lifetime.end: " << *LMI.LifeEnd << "\n");
498 HoistCands.insert(LMI.LifeEnd);
499 }
500 return true;
501 };
502
503
504
507 if (Blocks.count(BB))
508 continue;
509
510
511
513 if (AIFunc != Func)
514 continue;
515
516 LifetimeMarkerInfo MarkerInfo = getLifetimeMarkers(CEAC, AI, ExitBlock);
517 bool Moved = moveOrIgnoreLifetimeMarkers(MarkerInfo);
518 if (Moved) {
519 LLVM_DEBUG(dbgs() << "Sinking alloca: " << *AI << "\n");
520 SinkCands.insert(AI);
521 continue;
522 }
523
524
525
526
527
529 for (User *U : AI->users()) {
531 continue;
532
533 if (U->stripInBoundsConstantOffsets() != AI)
534 continue;
535
536 Instruction *Bitcast = cast(U);
537 for (User *BU : Bitcast->users()) {
538 IntrinsicInst *IntrInst = dyn_cast(BU);
539 if (!IntrInst)
540 continue;
541
543 continue;
544
546 continue;
547
548 LLVM_DEBUG(dbgs() << "Replace use of extracted region bitcast"
549 << *Bitcast << " in out-of-region lifetime marker "
550 << *IntrInst << "\n");
551 LifetimeBitcastUsers.push_back(IntrInst);
552 }
553 }
554
555 for (Instruction *I : LifetimeBitcastUsers) {
561 I->replaceUsesOfWith(I->getOperand(1), CastI);
562 }
563
564
567 for (User *U : AI->users()) {
568 if (U->stripInBoundsConstantOffsets() == AI) {
569 Instruction *Bitcast = cast(U);
570 LifetimeMarkerInfo LMI = getLifetimeMarkers(CEAC, Bitcast, ExitBlock);
571 if (LMI.LifeStart) {
573 BitcastLifetimeInfo.push_back(LMI);
574 continue;
575 }
576 }
577
578
580 Bitcasts.clear();
581 break;
582 }
583 }
584
585
586 if (Bitcasts.empty())
587 continue;
588
589 LLVM_DEBUG(dbgs() << "Sinking alloca (via bitcast): " << *AI << "\n");
590 SinkCands.insert(AI);
591 for (unsigned I = 0, E = Bitcasts.size(); I != E; ++I) {
593 const LifetimeMarkerInfo &LMI = BitcastLifetimeInfo[I];
594 assert(LMI.LifeStart &&
595 "Unsafe to sink bitcast without lifetime markers");
596 moveOrIgnoreLifetimeMarkers(LMI);
598 LLVM_DEBUG(dbgs() << "Sinking bitcast-of-alloca: " << *BitcastAddr
599 << "\n");
600 SinkCands.insert(BitcastAddr);
601 }
602 }
603 }
604}
605
607 if (Blocks.empty())
608 return false;
609 BasicBlock *Header = *Blocks.begin();
610 Function *F = Header->getParent();
611
612
613
614 if (AllowVarArgs && F->getFunctionType()->isVarArg()) {
615 auto containsVarArgIntrinsic = [](const Instruction &I) {
616 if (const CallInst *CI = dyn_cast(&I))
617 if (const Function *Callee = CI->getCalledFunction())
618 return Callee->getIntrinsicID() == Intrinsic::vastart ||
619 Callee->getIntrinsicID() == Intrinsic::vaend;
620 return false;
621 };
622
623 for (auto &BB : *F) {
624 if (Blocks.count(&BB))
625 continue;
626 if (llvm::any_of(BB, containsVarArgIntrinsic))
627 return false;
628 }
629 }
630
631
632
636 if ()
637 continue;
638 bool IsSave = II->getIntrinsicID() == Intrinsic::stacksave;
639 bool IsRestore = II->getIntrinsicID() == Intrinsic::stackrestore;
640 if (IsSave && any_of(II->users(), [&Blks = this->Blocks](User *U) {
641 return !definedInRegion(Blks, U);
642 }))
643 return false;
645 return false;
646 }
647 }
648 return true;
649}
650
653 bool CollectGlobalInputs) const {
655
656
658 for (auto &OI : II.operands()) {
660 if (!SinkCands.count(V) &&
662 (CollectGlobalInputs && llvm::isallvm::GlobalVariable(V))))
664 }
665
666 for (User *U : II.users())
669 break;
670 }
671 }
672 }
673}
674
675
676
677
678void CodeExtractor::severSplitPHINodesOfEntry(BasicBlock *&Header) {
679 unsigned NumPredsFromRegion = 0;
680 unsigned NumPredsOutsideRegion = 0;
681
682 if (Header != &Header->getParent()->getEntryBlock()) {
683 PHINode *PN = dyn_cast(Header->begin());
684 if (!PN) return;
685
686
687
688
691 ++NumPredsFromRegion;
692 else
693 ++NumPredsOutsideRegion;
694
695
696
697 if (NumPredsOutsideRegion <= 1) return;
698 }
699
700
701
702
703
705
706
707
709 Blocks.remove(OldPred);
710 Blocks.insert(NewBB);
711 Header = NewBB;
712
713
714
715 if (NumPredsFromRegion) {
716 PHINode *PN = cast(OldPred->begin());
717
718
723 }
724
725
726
728 for (AfterPHIs = OldPred->begin(); isa(AfterPHIs); ++AfterPHIs) {
729 PHINode *PN = cast(AfterPHIs);
730
731
737
738
739
744 --i;
745 }
746 }
747 }
748 }
749}
750
751
752
753
754
755void CodeExtractor::severSplitPHINodesOfExits() {
756 for (BasicBlock *ExitBB : ExtractedFuncRetVals) {
758
759 for (PHINode &PN : ExitBB->phis()) {
760
765
766
767
768
769 if (IncomingVals.size() <= 1)
770 continue;
771
772
773
774 if (!NewBB) {
776 ExitBB->getName() + ".split",
777 ExitBB->getParent(), ExitBB);
781 if (Blocks.count(PredBB))
782 PredBB->getTerminator()->replaceUsesOfWith(ExitBB, NewBB);
784 Blocks.insert(NewBB);
785 }
786
787
791 for (unsigned i : IncomingVals)
793 for (unsigned i : reverse(IncomingVals))
796 }
797 }
798}
799
800void CodeExtractor::splitReturnBlocks() {
802 if (ReturnInst *RI = dyn_cast(Block->getTerminator())) {
804 Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret");
805 if (DT) {
806
807
810 OldNode->end());
811
813
816 }
817 }
818}
819
820Function *CodeExtractor::constructFunctionDeclaration(
821 const ValueSet &inputs, const ValueSet &outputs, BlockFrequency EntryFreq,
823 LLVM_DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
824 LLVM_DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
825
828
829
830 std::vector<Type *> ParamTy;
831 std::vector<Type *> AggParamTy;
833
834
837 if (AggregateArgs && !ExcludeArgsFromAggregate.contains(value)) {
838 AggParamTy.push_back(value->getType());
839 StructValues.insert(value);
840 } else
841 ParamTy.push_back(value->getType());
842 }
843
844
845 for (Value *output : outputs) {
846 LLVM_DEBUG(dbgs() << "instr used in func: " << *output << "\n");
847 if (AggregateArgs && !ExcludeArgsFromAggregate.contains(output)) {
848 AggParamTy.push_back(output->getType());
849 StructValues.insert(output);
850 } else
851 ParamTy.push_back(
853 }
854
856 (ParamTy.size() + AggParamTy.size()) ==
857 (inputs.size() + outputs.size()) &&
858 "Number of scalar and aggregate params does not match inputs, outputs");
859 assert((StructValues.empty() || AggregateArgs) &&
860 "Expeced StructValues only with AggregateArgs set");
861
862
863 if (!AggParamTy.empty()) {
866 M->getContext(), ArgsInZeroAddressSpace ? 0 : DL.getAllocaAddrSpace()));
867 }
868
871 dbgs() << "Function type: " << *RetTy << " f(";
872 for (Type *i : ParamTy)
873 dbgs() << *i << ", ";
874 dbgs() << ")\n";
875 });
876
878 RetTy, ParamTy, AllowVarArgs && oldFunction->isVarArg());
879
880
884
885
888
889
890
891
892
893
894
895
897 if (Attr.isStringAttribute()) {
898 if (Attr.getKindAsString() == "thunk")
899 continue;
900 } else
901 switch (Attr.getKindAsEnum()) {
902
903
904 case Attribute::AllocSize:
905 case Attribute::Builtin:
906 case Attribute::Convergent:
907 case Attribute::JumpTable:
908 case Attribute::Naked:
909 case Attribute::NoBuiltin:
910 case Attribute::NoMerge:
911 case Attribute::NoReturn:
912 case Attribute::NoSync:
913 case Attribute::ReturnsTwice:
914 case Attribute::Speculatable:
915 case Attribute::StackAlignment:
916 case Attribute::WillReturn:
917 case Attribute::AllocKind:
918 case Attribute::PresplitCoroutine:
919 case Attribute::Memory:
920 case Attribute::NoFPClass:
921 case Attribute::CoroDestroyOnlyWhenComplete:
922 case Attribute::CoroElideSafe:
923 case Attribute::NoDivergenceSource:
924 continue;
925
926 case Attribute::AlwaysInline:
927 case Attribute::Cold:
928 case Attribute::DisableSanitizerInstrumentation:
929 case Attribute::FnRetThunkExtern:
930 case Attribute::Hot:
931 case Attribute::HybridPatchable:
932 case Attribute::NoRecurse:
933 case Attribute::InlineHint:
934 case Attribute::MinSize:
935 case Attribute::NoCallback:
936 case Attribute::NoDuplicate:
937 case Attribute::NoFree:
938 case Attribute::NoImplicitFloat:
939 case Attribute::NoInline:
940 case Attribute::NonLazyBind:
941 case Attribute::NoRedZone:
942 case Attribute::NoUnwind:
943 case Attribute::NoSanitizeBounds:
944 case Attribute::NoSanitizeCoverage:
945 case Attribute::NullPointerIsValid:
946 case Attribute::OptimizeForDebugging:
947 case Attribute::OptForFuzzing:
948 case Attribute::OptimizeNone:
949 case Attribute::OptimizeForSize:
950 case Attribute::SafeStack:
951 case Attribute::ShadowCallStack:
952 case Attribute::SanitizeAddress:
953 case Attribute::SanitizeMemory:
954 case Attribute::SanitizeNumericalStability:
955 case Attribute::SanitizeThread:
956 case Attribute::SanitizeType:
957 case Attribute::SanitizeHWAddress:
958 case Attribute::SanitizeMemTag:
959 case Attribute::SanitizeRealtime:
960 case Attribute::SanitizeRealtimeBlocking:
961 case Attribute::SpeculativeLoadHardening:
962 case Attribute::StackProtect:
963 case Attribute::StackProtectReq:
964 case Attribute::StackProtectStrong:
965 case Attribute::StrictFP:
966 case Attribute::UWTable:
967 case Attribute::VScaleRange:
968 case Attribute::NoCfCheck:
969 case Attribute::MustProgress:
970 case Attribute::NoProfile:
971 case Attribute::SkipProfile:
972 break;
973
974 case Attribute::Alignment:
975 case Attribute::AllocatedPointer:
976 case Attribute::AllocAlign:
977 case Attribute::ByVal:
978 case Attribute::Captures:
979 case Attribute::Dereferenceable:
980 case Attribute::DereferenceableOrNull:
981 case Attribute::ElementType:
982 case Attribute::InAlloca:
983 case Attribute::InReg:
984 case Attribute::Nest:
985 case Attribute::NoAlias:
986 case Attribute::NoCapture:
987 case Attribute::NoUndef:
988 case Attribute::NonNull:
989 case Attribute::Preallocated:
990 case Attribute::ReadNone:
991 case Attribute::ReadOnly:
992 case Attribute::Returned:
993 case Attribute::SExt:
994 case Attribute::StructRet:
995 case Attribute::SwiftError:
996 case Attribute::SwiftSelf:
997 case Attribute::SwiftAsync:
998 case Attribute::ZExt:
999 case Attribute::ImmArg:
1000 case Attribute::ByRef:
1001 case Attribute::WriteOnly:
1002 case Attribute::Writable:
1003 case Attribute::DeadOnUnwind:
1004 case Attribute::Range:
1005 case Attribute::Initializes:
1006 case Attribute::NoExt:
1007
1013 }
1014
1016 }
1017
1018
1019
1021
1022
1023 ScalarAI = newFunction->arg_begin();
1024 for (Value *input : inputs) {
1025 if (StructValues.contains(input))
1026 continue;
1027
1028 ScalarAI->setName(input->getName());
1029 if (input->isSwiftError())
1031 Attribute::SwiftError);
1032 ++ScalarAI;
1033 }
1034 for (Value *output : outputs) {
1035 if (StructValues.contains(output))
1036 continue;
1037
1038 ScalarAI->setName(output->getName() + ".out");
1039 ++ScalarAI;
1040 }
1041
1042
1043 if (BFI) {
1045 if (Count.has_value())
1048 }
1049
1050 return newFunction;
1051}
1052
1053
1054
1055
1056
1063 if (.getDebugLoc())
1064 return false;
1065
1066
1068 return false;
1070 return true;
1071 });
1072 });
1073 }
1074}
1075
1076
1077
1078
1079
1080
1081
1088 if ( ||
->isLifetimeStartOrEnd())
1089 continue;
1090
1091
1092
1093
1094 Value *Mem = II->getOperand(1)->stripInBoundsOffsets();
1096 continue;
1097
1098 if (II->getIntrinsicID() == Intrinsic::lifetime_start)
1099 LifetimesStart.insert(Mem);
1100 II->eraseFromParent();
1101 }
1102 }
1103}
1104
1105
1106
1113
1114
1115
1117 bool InsertBefore) {
1118 for (Value *Mem : Objects) {
1119 assert((!isa(Mem) || cast(Mem)->getFunction() ==
1121 "Input memory not defined in original function");
1122
1126 if (InsertBefore)
1128 else
1129 Marker->insertBefore(Term);
1130 }
1131 };
1132
1133 if (!LifetimesStart.empty()) {
1134 insertMarkers(Intrinsic::lifetime_start, LifetimesStart,
1135 true);
1136 }
1137
1138 if (!LifetimesEnd.empty()) {
1139 insertMarkers(Intrinsic::lifetime_end, LifetimesEnd,
1140 false);
1141 }
1142}
1143
1144void CodeExtractor::moveCodeToFunction(Function *newFunction) {
1145 auto newFuncIt = newFunction->begin();
1147
1148 Block->removeFromParent();
1149
1150
1151
1152
1153
1154
1155 newFuncIt = newFunction->insert(std::next(newFuncIt), Block);
1156 }
1157}
1158
1159void CodeExtractor::calculateNewCallTerminatorWeights(
1165
1166
1169
1170
1171 Distribution BranchDist;
1172
1175
1176
1177 for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) {
1178 BlockNode ExitNode(i);
1180 if (ExitFreq != 0)
1181 BranchDist.addExit(ExitNode, ExitFreq);
1182 else
1184 }
1185
1186
1187 if (BranchDist.Total == 0) {
1189 return;
1190 }
1191
1192
1193 BranchDist.normalize();
1194
1195
1196 for (unsigned I = 0, E = BranchDist.Weights.size(); I < E; ++I) {
1197 const auto &Weight = BranchDist.Weights[I];
1198
1199
1200 BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
1202 EdgeProbabilities[Weight.TargetNode.Index] = BP;
1203 }
1206 LLVMContext::MD_prof,
1208}
1209
1210
1211
1216 findDbgUsers(DbgUsers, &I, &DbgVariableRecords);
1218 if (DVI->getFunction() != &F)
1219 DVI->eraseFromParent();
1221 if (DVR->getFunction() != &F)
1222 DVR->eraseFromParent();
1223 }
1224}
1225
1226
1227
1228
1233
1234 if (!OldSP) {
1235
1237
1239 return;
1240 }
1241
1242
1243
1244
1245 assert(OldSP->getUnit() && "Missing compile unit for subprogram");
1247 OldSP->getUnit());
1250 DISubprogram::SPFlagOptimized |
1251 DISubprogram::SPFlagLocalToUnit;
1254 0, SPType, 0, DINode::FlagZero, SPFlags);
1256
1257 auto IsInvalidLocation = [&NewFunc](Value *Location) {
1258
1259
1260 if (!Location ||
1261 (!isa(Location) && !isa(Location)))
1262 return true;
1263 Instruction *LocationInst = dyn_cast(Location);
1264 return LocationInst && LocationInst->getFunction() != &NewFunc;
1265 };
1266
1267
1268
1269
1270
1271
1272
1277
1278 auto GetUpdatedDIVariable = [&](DILocalVariable *OldVar) {
1279 DINode *&NewVar = RemappedMetadata[OldVar];
1280 if (!NewVar) {
1282 *OldVar->getScope(), *NewSP, Ctx, Cache);
1284 NewScope, OldVar->getName(), OldVar->getFile(), OldVar->getLine(),
1285 OldVar->getType(), false, DINode::FlagZero,
1286 OldVar->getAlignInBits());
1287 }
1288 return cast(NewVar);
1289 };
1290
1291 auto UpdateDbgLabel = [&](auto *LabelRecord) {
1292
1293
1294 if (LabelRecord->getDebugLoc().getInlinedAt())
1295 return;
1296 DILabel *OldLabel = LabelRecord->getLabel();
1297 DINode *&NewLabel = RemappedMetadata[OldLabel];
1298 if (!NewLabel) {
1300 *OldLabel->getScope(), *NewSP, Ctx, Cache);
1303 }
1304 LabelRecord->setLabel(cast(NewLabel));
1305 };
1306
1307 auto UpdateDbgRecordsOnInst = [&](Instruction &I) -> void {
1308 for (DbgRecord &DR : I.getDbgRecordRange()) {
1309 if (DbgLabelRecord *DLR = dyn_cast(&DR)) {
1310 UpdateDbgLabel(DLR);
1311 continue;
1312 }
1313
1315
1316
1319 continue;
1320 }
1323 continue;
1324 }
1327 }
1328 };
1329
1331 UpdateDbgRecordsOnInst(I);
1332
1333 auto *DII = dyn_cast(&I);
1334 if (!DII)
1335 continue;
1336
1337
1338
1339 if (auto *DLI = dyn_cast(&I)) {
1340 UpdateDbgLabel(DLI);
1341 continue;
1342 }
1343
1344 auto *DVI = cast(DII);
1345
1346 if (any_of(DVI->location_ops(), IsInvalidLocation)) {
1347 DebugIntrinsicsToDelete.push_back(DVI);
1348 continue;
1349 }
1350
1351 if (auto *DAI = dyn_cast(DVI);
1352 DAI && IsInvalidLocation(DAI->getAddress())) {
1353 DebugIntrinsicsToDelete.push_back(DVI);
1354 continue;
1355 }
1356
1357
1358 if (!DVI->getDebugLoc().getInlinedAt())
1359 DVI->setVariable(GetUpdatedDIVariable(DVI->getVariable()));
1360 }
1361
1362 for (auto *DII : DebugIntrinsicsToDelete)
1363 DII->eraseFromParent();
1364 for (auto *DVR : DVRsToDelete)
1365 DVR->getMarker()->MarkedInstr->dropOneDbgRecord(DVR);
1367
1368
1369
1372 if (const DebugLoc &DL = I.getDebugLoc())
1373 I.setDebugLoc(
1375 for (DbgRecord &DR : I.getDbgRecordRange())
1377 *NewSP, Ctx, Cache));
1378
1379
1380 auto updateLoopInfoLoc = [&Ctx, &Cache, NewSP](Metadata *MD) -> Metadata * {
1381 if (auto *Loc = dyn_cast_or_null(MD))
1383 return MD;
1384 };
1387 }
1390
1392}
1393
1398}
1399
1404 return nullptr;
1405
1406
1407
1410
1411 normalizeCFGForExtraction(header);
1412
1413
1414
1417 if (auto *AI = dyn_cast(&I)) {
1418 if (AC)
1420 AI->eraseFromParent();
1421 }
1422 }
1423 }
1424
1425 ValueSet SinkingCands, HoistingCands;
1427 findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit);
1428 assert(HoistingCands.empty() || CommonExit);
1429
1430
1432
1433
1434
1435
1436
1439
1440 if (!HoistingCands.empty()) {
1442 Instruction *TI = HoistToBlock->getTerminator();
1443 for (auto *II : HoistingCands)
1445 computeExtractedFuncRetVals();
1446 }
1447
1448
1449
1450
1451
1454 if (BFI) {
1455 assert(BPI && "Both BPI and BFI are required to preserve profile info");
1457 if (Blocks.count(Pred))
1458 continue;
1459 EntryFreq +=
1461 }
1462
1463 for (BasicBlock *Succ : ExtractedFuncRetVals) {
1466 continue;
1467
1468
1471 }
1472 }
1473 }
1474
1475
1476
1478 while (ReplIP && Blocks.count(ReplIP))
1480
1481
1482 std::string SuffixToUse =
1483 Suffix.empty()
1485 : Suffix;
1486
1489 Function *newFunction = constructFunctionDeclaration(
1490 inputs, outputs, EntryFreq, oldFunction->getName() + "." + SuffixToUse,
1491 StructValues, StructTy);
1493
1494 emitFunctionBody(inputs, outputs, StructValues, newFunction, StructTy, header,
1495 SinkingCands);
1496
1497 std::vector<Value *> Reloads;
1498 CallInst *TheCall = emitReplacerCall(
1499 inputs, outputs, StructValues, newFunction, StructTy, oldFunction, ReplIP,
1500 EntryFreq, LifetimesStart.getArrayRef(), Reloads);
1501
1502 insertReplacerCall(oldFunction, header, TheCall->getParent(), outputs,
1503 Reloads, ExitWeights);
1504
1506
1508 newFunction->dump();
1510 });
1515 return newFunction;
1516}
1517
1518void CodeExtractor::normalizeCFGForExtraction(BasicBlock *&header) {
1519
1520
1521 splitReturnBlocks();
1522
1523
1524 severSplitPHINodesOfEntry(header);
1525
1526
1527
1528
1529
1530 computeExtractedFuncRetVals();
1531 severSplitPHINodesOfExits();
1532}
1533
1534void CodeExtractor::computeExtractedFuncRetVals() {
1535 ExtractedFuncRetVals.clear();
1536
1540 if (Blocks.count(Succ))
1541 continue;
1542
1543 bool IsNew = ExitBlocks.insert(Succ).second;
1544 if (IsNew)
1545 ExtractedFuncRetVals.push_back(Succ);
1546 }
1547 }
1548}
1549
1550Type *CodeExtractor::getSwitchType() {
1552
1553 assert(ExtractedFuncRetVals.size() < 0xffff &&
1554 "too many exit blocks for switch");
1555 switch (ExtractedFuncRetVals.size()) {
1556 case 0:
1557 case 1:
1559 case 2:
1560
1562 default:
1564 }
1565}
1566
1567void CodeExtractor::emitFunctionBody(
1568 const ValueSet &inputs, const ValueSet &outputs,
1569 const ValueSet &StructValues, Function *newFunction,
1570 StructType *StructArgTy, BasicBlock *header, const ValueSet &SinkingCands) {
1573
1574
1575
1579
1580
1581
1582
1583 for (auto *II : SinkingCands) {
1584 if (!isa(II)) {
1585 cast(II)->moveBefore(*newFuncRoot,
1587 }
1588 }
1589 for (auto *II : SinkingCands) {
1590 if (auto *AI = dyn_cast(II)) {
1592 }
1593 }
1594
1596 Argument *AggArg = StructValues.empty()
1597 ? nullptr
1599
1600
1601
1603 for (unsigned i = 0, e = inputs.size(), aggIdx = 0; i != e; ++i) {
1604 Value *RewriteVal;
1605 if (StructValues.contains(inputs[i])) {
1610 StructArgTy, AggArg, Idx, "gep_" + inputs[i]->getName(), newFuncRoot);
1612 "loadgep_" + inputs[i]->getName(), newFuncRoot);
1613 ++aggIdx;
1614 } else
1615 RewriteVal = &*ScalarAI++;
1616
1617 NewValues.push_back(RewriteVal);
1618 }
1619
1620 moveCodeToFunction(newFunction);
1621
1622 for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
1623 Value *RewriteVal = NewValues[i];
1624
1625 std::vector<User *> Users(inputs[i]->user_begin(), inputs[i]->user_end());
1628 if (Blocks.count(inst->getParent()))
1629 inst->replaceUsesOfWith(inputs[i], RewriteVal);
1630 }
1631
1632
1633
1634
1635
1636
1637 std::map<BasicBlock *, BasicBlock *> ExitBlockMap;
1638
1639
1640
1641 for (auto P : enumerate(ExtractedFuncRetVals)) {
1643 size_t SuccNum = P.index();
1644
1646 Context, OldTarget->getName() + ".exitStub", newFunction);
1647 ExitBlockMap[OldTarget] = NewTarget;
1648
1649 Value *brVal = nullptr;
1651 assert(ExtractedFuncRetVals.size() < 0xffff &&
1652 "too many exit blocks for switch");
1653 switch (ExtractedFuncRetVals.size()) {
1654 case 0:
1655 case 1:
1656
1657 break;
1658 case 2:
1659 brVal = ConstantInt::get(RetTy, !SuccNum);
1660 break;
1661 default:
1662 brVal = ConstantInt::get(RetTy, SuccNum);
1663 break;
1664 }
1665
1667 }
1668
1671 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1673 continue;
1675
1676 BasicBlock *NewTarget = ExitBlockMap[OldTarget];
1677 assert(NewTarget && "Unknown target block!");
1678
1679
1681 }
1682 }
1683
1684
1685
1691 }
1692
1693
1696
1697
1698
1699
1700 ScalarAI = newFunction->arg_begin();
1701 unsigned AggIdx = 0;
1702
1703 for (Value *Input : inputs) {
1704 if (StructValues.contains(Input))
1705 ++AggIdx;
1706 else
1707 ++ScalarAI;
1708 }
1709
1710 for (Value *Output : outputs) {
1711
1712
1713
1714
1716 if (auto *InvokeI = dyn_cast(Output))
1717 InsertPt = InvokeI->getNormalDest()->getFirstInsertionPt();
1718 else if (auto *Phi = dyn_cast(Output))
1719 InsertPt = Phi->getParent()->getFirstInsertionPt();
1720 else if (auto *OutI = dyn_cast(Output))
1721 InsertPt = std::next(OutI->getIterator());
1722 else {
1723
1724 if (StructValues.contains(Output))
1725 ++AggIdx;
1726 else
1727 ++ScalarAI;
1728 continue;
1729 }
1730
1731 assert((InsertPt->getFunction() == newFunction ||
1732 Blocks.count(InsertPt->getParent())) &&
1733 "InsertPt should be in new function");
1734
1735 if (StructValues.contains(Output)) {
1736 assert(AggArg && "Number of aggregate output arguments should match "
1737 "the number of defined values");
1742 StructArgTy, AggArg, Idx, "gep_" + Output->getName(), InsertPt);
1744 ++AggIdx;
1745 } else {
1747 "Number of scalar output arguments should match "
1748 "the number of defined values");
1749 new StoreInst(Output, &*ScalarAI, InsertPt);
1750 ++ScalarAI;
1751 }
1752 }
1753
1754 if (ExtractedFuncRetVals.empty()) {
1755
1756
1757
1760 return isa(Term) || isa(Term);
1761 }))
1763 }
1764}
1765
1766CallInst *CodeExtractor::emitReplacerCall(
1767 const ValueSet &inputs, const ValueSet &outputs,
1768 const ValueSet &StructValues, Function *newFunction,
1771 std::vector<Value *> &Reloads) {
1775
1776
1781 AllocationBlock ? AllocationBlock : &oldFunction->getEntryBlock();
1783
1784
1785 if (BFI)
1787
1788 std::vector<Value *> params;
1789
1790
1791 for (Value *input : inputs) {
1792 if (StructValues.contains(input))
1793 continue;
1794
1795 params.push_back(input);
1796 }
1797
1798
1799 std::vector<Value *> ReloadOutputs;
1800 for (Value *output : outputs) {
1801 if (StructValues.contains(output))
1802 continue;
1803
1805 output->getType(), DL.getAllocaAddrSpace(), nullptr,
1807 params.push_back(alloca);
1808 ReloadOutputs.push_back(alloca);
1809 }
1810
1812 if (!StructValues.empty()) {
1813 Struct = new AllocaInst(StructArgTy, DL.getAllocaAddrSpace(), nullptr,
1815 if (ArgsInZeroAddressSpace && DL.getAllocaAddrSpace() != 0) {
1817 Struct, PointerType ::get(Context, 0), "structArg.ascast");
1818 StructSpaceCast->insertAfter(Struct);
1819 params.push_back(StructSpaceCast);
1820 } else {
1821 params.push_back(Struct);
1822 }
1823
1824 unsigned AggIdx = 0;
1825 for (Value *input : inputs) {
1826 if (!StructValues.contains(input))
1827 continue;
1828
1833 StructArgTy, Struct, Idx, "gep_" + input->getName());
1834 GEP->insertInto(codeReplacer, codeReplacer->end());
1836
1837 ++AggIdx;
1838 }
1839 }
1840
1841
1843 newFunction, params, ExtractedFuncRetVals.size() > 1 ? "targetBlock" : "",
1844 codeReplacer);
1845
1846
1847 unsigned ParamIdx = 0;
1848 unsigned AggIdx = 0;
1849 for (auto input : inputs) {
1850 if (StructValues.contains(input)) {
1851 ++AggIdx;
1852 } else {
1853 if (input->isSwiftError())
1854 call->addParamAttr(ParamIdx, Attribute::SwiftError);
1855 ++ParamIdx;
1856 }
1857 }
1858
1859
1860
1861
1862
1866 }
1867
1868
1869
1870 for (unsigned i = 0, e = outputs.size(), scalarIdx = 0; i != e; ++i) {
1871 Value *Output = nullptr;
1872 if (StructValues.contains(outputs[i])) {
1877 StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName());
1878 GEP->insertInto(codeReplacer, codeReplacer->end());
1879 Output = GEP;
1880 ++AggIdx;
1881 } else {
1882 Output = ReloadOutputs[scalarIdx];
1883 ++scalarIdx;
1884 }
1887 outputs[i]->getName() + ".reload", codeReplacer);
1888 Reloads.push_back(load);
1889 }
1890
1891
1894 codeReplacer, 0, codeReplacer);
1895 for (auto P : enumerate(ExtractedFuncRetVals)) {
1897 size_t SuccNum = P.index();
1898
1900 OldTarget);
1901 }
1902
1903
1904 Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType();
1905 switch (ExtractedFuncRetVals.size()) {
1906 case 0:
1907
1908
1909
1911
1913 } else if (OldFnRetTy->isVoidTy()) {
1914
1916 TheSwitch->getIterator());
1918
1921 } else {
1922
1923
1926 }
1927
1929 break;
1930 case 1:
1931
1932
1935 break;
1936 case 2:
1937
1938
1939
1943 break;
1944 default:
1945
1946
1949 TheSwitch->getSuccessor(ExtractedFuncRetVals.size()));
1950
1953 break;
1954 }
1955
1956
1957
1959
1960
1961
1963 {}, call);
1964
1965 return call;
1966}
1967
1968void CodeExtractor::insertReplacerCall(
1972
1973
1974
1975
1977 for (auto &U : Users)
1978
1979
1980 if (Instruction *I = dyn_cast(U))
1981 if (I->isTerminator() && I->getFunction() == oldFunction &&
1982 .count(I->getParent()))
1983 I->replaceUsesOfWith(header, codeReplacer);
1984
1985
1986
1987
1988
1989 for (BasicBlock *ExitBB : ExtractedFuncRetVals)
1990 for (PHINode &PN : ExitBB->phis()) {
1991 Value *IncomingCodeReplacerVal = nullptr;
1993
1995 continue;
1996
1997
1998 if (!IncomingCodeReplacerVal) {
2001 } else
2003 "PHI has two incompatbile incoming values from codeRepl");
2004 }
2005 }
2006
2007 for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
2009 std::vector<User *> Users(outputs[i]->user_begin(), outputs[i]->user_end());
2011 Instruction *inst = cast(U);
2012 if (inst->getParent()->getParent() == oldFunction)
2014 }
2015 }
2016
2017
2018 if (BFI && ExtractedFuncRetVals.size() > 1)
2019 calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
2020}
2021
2025 for (auto AssumeVH : AC->assumptions()) {
2026 auto *I = dyn_cast_or_null(AssumeVH);
2027 if ()
2028 continue;
2029
2030
2031 if (I->getFunction() != &OldFunc)
2032 return true;
2033
2034
2035
2036
2037 for (auto AffectedValVH : AC->assumptionsFor(I->getOperand(0))) {
2038 auto *AffectedCI = dyn_cast_or_null(AffectedValVH);
2039 if (!AffectedCI)
2040 continue;
2041 if (AffectedCI->getFunction() != &OldFunc)
2042 return true;
2043 auto *AssumedInst = cast(AffectedCI->getOperand(0));
2044 if (AssumedInst->getFunction() != &OldFunc)
2045 return true;
2046 }
2047 }
2048 return false;
2049}
2050
2052 ExcludeArgsFromAggregate.insert(Arg);
2053}
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...
Module.h This file contains the declarations for the Module class.
iv Induction Variable Users
Move duplicate certain instructions close to their use
uint64_t IntrinsicInst * II
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file 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.
Conditional or Unconditional Branch instruction.
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
Analysis providing branch probability information.
void setEdgeProbability(const BasicBlock *Src, const SmallVectorImpl< BranchProbability > &Probs)
Set the raw probabilities for all edges from the given block.
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
static BranchProbability getUnknown()
static BranchProbability getZero()
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
Adds the attribute to the indicated argument.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This is the base class for all instructions that perform data casts.
static CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
DISubroutineType * createSubroutineType(DITypeRefArray ParameterTypes, DINode::DIFlags Flags=DINode::FlagZero, unsigned CC=0)
Create subroutine type.
void finalizeSubprogram(DISubprogram *SP)
Finalize a specific subprogram - no new variables may be added to this subprogram afterwards.
DISubprogram * createFunction(DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File, unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine, DINode::DIFlags Flags=DINode::FlagZero, DISubprogram::DISPFlags SPFlags=DISubprogram::SPFlagZero, DITemplateParameterArray TParams=nullptr, DISubprogram *Decl=nullptr, DITypeArray ThrownTypes=nullptr, DINodeArray Annotations=nullptr, StringRef TargetFuncName="")
Create a new descriptor for the specified subprogram.
DITypeRefArray getOrCreateTypeArray(ArrayRef< Metadata * > Elements)
Get a DITypeRefArray, create one if required.
DILocalVariable * createAutoVariable(DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo, DIType *Ty, bool AlwaysPreserve=false, DINode::DIFlags Flags=DINode::FlagZero, uint32_t AlignInBits=0)
Create a new descriptor for an auto variable.
StringRef getName() const
DILocalScope * getScope() const
Get the local scope for this label.
static DILocalScope * cloneScopeForSubprogram(DILocalScope &RootScope, DISubprogram &NewSP, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Traverses the scope chain rooted at RootScope until it hits a Subprogram, recreating the chain with "...
Tagged DWARF-like metadata node.
StringRef getName() const
DISPFlags
Debug info subprogram flags.
A parsed version of the target data layout string in and methods for querying it.
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
This is the common base class for debug info intrinsics for variables.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
void setVariable(DILocalVariable *NewVar)
Value * getAddress() const
DILocalVariable * getVariable() const
iterator_range< location_op_iterator > location_ops() const
Get the locations corresponding to the variable referenced by the debug info intrinsic.
static DebugLoc replaceInlinedAtSubprogram(const DebugLoc &DL, DISubprogram &NewSP, LLVMContext &Ctx, DenseMap< const MDNode *, MDNode * > &Cache)
Rebuild the entire inline-at chain by replacing the subprogram at the end of the chain with NewSP.
DILocation * getInlinedAt() const
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Class to represent profile counts.
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
const BasicBlock & getEntryBlock() const
DISubprogram * getSubprogram() const
Get the attached subprogram.
bool IsNewDbgInfoFormat
Is this function using intrinsics to record the position of debugging information,...
bool hasPersonalityFn() const
Check whether this function has a personality function.
Constant * getPersonalityFn() const
Get the personality function associated with this function.
void setPersonalityFn(Constant *Fn)
AttributeList getAttributes() const
Return the attribute list for this Function.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
adds the attribute to the list of attributes for the given arg.
Function::iterator insert(Function::iterator Position, BasicBlock *BB)
Insert BB in the basic block list at Position.
bool doesNotReturn() const
Determine if the function cannot return.
Argument * getArg(unsigned i) const
void setEntryCount(ProfileCount Count, const DenseSet< GlobalValue::GUID > *Imports=nullptr)
Set the entry count for this function.
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
unsigned getAddressSpace() const
Module * getParent()
Get the module that this global value is contained inside of...
@ InternalLinkage
Rename collisions when linking (static functions).
bool isLifetimeStartOrEnd() const LLVM_READONLY
Return true if the instruction is a llvm.lifetime.start or llvm.lifetime.end marker.
unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
const Function * getFunction() const
Return the function this instruction belongs to.
BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
A wrapper class for inspecting calls to intrinsic functions.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
Value * getPointerOperand()
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
A Module instance is used to store all the information related to an LLVM module.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Return a value (possibly void), from a function.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
A vector that has set insertion semantics.
ArrayRef< value_type > getArrayRef() const
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
bool contains(const key_type &key) const
Check if the SetVector contains the given key.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
std::string str() const
str - Get the contents as an std::string.
constexpr bool empty() const
empty - Check if the string is empty.
Class to represent struct types.
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Type * getElementType(unsigned N) const
BasicBlock * getSuccessor(unsigned idx) const
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, InsertPosition InsertBefore=nullptr)
void setCondition(Value *V)
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
void setDefaultDest(BasicBlock *DefaultCase)
Value * getCondition() const
CaseIt removeCase(CaseIt I)
This method removes the specified case and its successor from the switch instruction.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
static IntegerType * getInt1Ty(LLVMContext &C)
static Type * getVoidTy(LLVMContext &C)
static IntegerType * getInt16Ty(LLVMContext &C)
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isVoidTy() const
Return true if this is 'void'.
This function has undefined behavior.
bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
user_iterator user_begin()
void setName(const Twine &Name)
Change the name of the value.
const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVMContext & getContext() const
All values hold a context through their type.
StringRef getName() const
Return a constant reference to the value's name.
void dump() const
Support for debugging, callable in GDB: V->dump()
const ParentTy * getParent() const
self_iterator getIterator()
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
Look up the Function declaration of the intrinsic id in the Module M.
void remapAssignID(DenseMap< DIAssignID *, DIAssignID * > &Map, Instruction &I)
Replace DIAssignID uses and attachments with IDs from Map.
NodeAddr< PhiNode * > Phi
This is an optimization pass for GlobalISel generic memory operations.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
bool stripDebugInfo(Function &F)
Function::ProfileCount ProfileCount
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
void findDbgUsers(SmallVectorImpl< DbgVariableIntrinsic * > &DbgInsts, Value *V, SmallVectorImpl< DbgVariableRecord * > *DbgVariableRecords=nullptr)
Finds the debug info intrinsics describing a value.
auto successors(const MachineBasicBlock *BB)
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
auto predecessors(const MachineBasicBlock *BB)
void updateLoopMetadataDebugLocations(Instruction &I, function_ref< Metadata *(Metadata *)> Updater)
Update the debug locations contained within the MD_loop metadata attached to the instruction I,...
Representative of a block.
Distribution of unscaled probability weight.