LLVM 24.0.0git
CodeExtractor.cpp
Go to the documentation of this file.
1//===- CodeExtractor.cpp - Pull code region into a new function -----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the interface to tear out a code region, such as an
10// individual loop or a parallel section, into a new function, replacing it with
11// a call to the new function.
12//
13//===----------------------------------------------------------------------===//
14
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SetVector.h"
26#include "llvm/IR/Argument.h"
27#include "llvm/IR/Attributes.h"
28#include "llvm/IR/CFG.h"
29#include "llvm/IR/Constant.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DIBuilder.h"
32#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/DebugInfo.h"
36#include "llvm/IR/Dominators.h"
37#include "llvm/IR/Function.h"
38#include "llvm/IR/GlobalValue.h"
40#include "llvm/IR/InstrTypes.h"
41#include "llvm/IR/Instruction.h"
44#include "llvm/IR/Intrinsics.h"
45#include "llvm/IR/LLVMContext.h"
46#include "llvm/IR/MDBuilder.h"
47#include "llvm/IR/Module.h"
49#include "llvm/IR/Type.h"
50#include "llvm/IR/User.h"
51#include "llvm/IR/Value.h"
52#include "llvm/IR/Verifier.h"
57#include "llvm/Support/Debug.h"
61#include <cassert>
62#include <cstdint>
63#include <iterator>
64#include <map>
65#include <vector>
66
67using namespace llvm;
68using namespace llvm::PatternMatch;
69
70#define DEBUG_TYPE "code-extractor"
71
72// Provide a command-line option to aggregate function arguments into a struct
73// for functions produced by the code extractor. This is useful when converting
74// extracted functions to pthread-based code, as only one argument (void*) can
75// be passed in to pthread_create().
76static cl::opt<bool>
77AggregateArgsOpt("aggregate-extracted-args", cl::Hidden,
78 cl::desc("Aggregate arguments to code-extracted functions"));
79
80/// Test whether a block is valid for extraction.
82 const SetVector<BasicBlock *> &Result,
83 bool AllowVarArgs, bool AllowAlloca) {
84 // taking the address of a basic block moved to another function is illegal
85 if (BB.hasAddressTaken())
86 return false;
87
88 // don't hoist code that uses another basicblock address, as it's likely to
89 // lead to unexpected behavior, like cross-function jumps
92
93 while (!ToVisit.empty()) {
94 User const *Curr = ToVisit.pop_back_val();
95 if (!Visited.insert(Curr).second)
96 continue;
98 return false; // even a reference to self is likely to be not compatible
99
100 if (isa<Instruction>(Curr) && cast<Instruction>(Curr)->getParent() != &BB)
101 continue;
102
103 for (auto const &U : Curr->operands()) {
104 if (auto *UU = dyn_cast<User>(U))
105 ToVisit.push_back(UU);
106 }
107 }
108
109 // If explicitly requested, allow vastart and alloca. For invoke instructions
110 // verify that extraction is valid.
111 for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
112 if (isa<AllocaInst>(I)) {
113 if (!AllowAlloca)
114 return false;
115 continue;
116 }
117
118 if (const auto *II = dyn_cast<InvokeInst>(I)) {
119 // Unwind destination (either a landingpad, catchswitch, or cleanuppad)
120 // must be a part of the subgraph which is being extracted.
121 if (auto *UBB = II->getUnwindDest())
122 if (!Result.count(UBB))
123 return false;
124 continue;
125 }
126
127 // All catch handlers of a catchswitch instruction as well as the unwind
128 // destination must be in the subgraph.
129 if (const auto *CSI = dyn_cast<CatchSwitchInst>(I)) {
130 if (auto *UBB = CSI->getUnwindDest())
131 if (!Result.count(UBB))
132 return false;
133 for (const auto *HBB : CSI->handlers())
134 if (!Result.count(const_cast<BasicBlock*>(HBB)))
135 return false;
136 continue;
137 }
138
139 // Make sure that entire catch handler is within subgraph. It is sufficient
140 // to check that catch return's block is in the list.
141 if (const auto *CPI = dyn_cast<CatchPadInst>(I)) {
142 for (const auto *U : CPI->users())
143 if (const auto *CRI = dyn_cast<CatchReturnInst>(U))
144 if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
145 return false;
146 continue;
147 }
148
149 // And do similar checks for cleanup handler - the entire handler must be
150 // in subgraph which is going to be extracted. For cleanup return should
151 // additionally check that the unwind destination is also in the subgraph.
152 if (const auto *CPI = dyn_cast<CleanupPadInst>(I)) {
153 for (const auto *U : CPI->users())
154 if (const auto *CRI = dyn_cast<CleanupReturnInst>(U))
155 if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
156 return false;
157 continue;
158 }
159 if (const auto *CRI = dyn_cast<CleanupReturnInst>(I)) {
160 if (auto *UBB = CRI->getUnwindDest())
161 if (!Result.count(UBB))
162 return false;
163 continue;
164 }
165
166 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
167 // musttail calls have several restrictions, generally enforcing matching
168 // calling conventions between the caller parent and musttail callee.
169 // We can't usually honor them, because the extracted function has a
170 // different signature altogether, taking inputs/outputs and returning
171 // a control-flow identifier rather than the actual return value.
172 if (CI->isMustTailCall())
173 return false;
174
175 if (const Function *F = CI->getCalledFunction()) {
176 auto IID = F->getIntrinsicID();
177 if (IID == Intrinsic::vastart) {
178 if (AllowVarArgs)
179 continue;
180 else
181 return false;
182 }
183
184 // Currently, we miscompile outlined copies of eh_typid_for. There are
185 // proposals for fixing this in llvm.org/PR39545.
186 if (IID == Intrinsic::eh_typeid_for)
187 return false;
188 }
189 }
190 }
191
192 return true;
193}
194
195/// Build a set of blocks to extract if the input blocks are viable.
198 bool AllowVarArgs, bool AllowAlloca) {
199 assert(!BBs.empty() && "The set of blocks to extract must be non-empty");
201
202 // Loop over the blocks, adding them to our set-vector, and aborting with an
203 // empty set if we encounter invalid blocks.
204 for (BasicBlock *BB : BBs) {
205 // If this block is dead, don't process it.
206 if (DT && !DT->isReachableFromEntry(BB))
207 continue;
208
209 if (!Result.insert(BB))
210 llvm_unreachable("Repeated basic blocks in extraction input");
211 }
212
213 LLVM_DEBUG(dbgs() << "Region front block: " << Result.front()->getName()
214 << '\n');
215
216 for (auto *BB : Result) {
217 if (!isBlockValidForExtraction(*BB, Result, AllowVarArgs, AllowAlloca))
218 return {};
219
220 // Make sure that the first block is not a landing pad.
221 if (BB == Result.front()) {
222 if (BB->isEHPad()) {
223 LLVM_DEBUG(dbgs() << "The first block cannot be an unwind block\n");
224 return {};
225 }
226 continue;
227 }
228
229 // All blocks other than the first must not have predecessors outside of
230 // the subgraph which is being extracted.
231 for (auto *PBB : predecessors(BB))
232 if (!Result.count(PBB)) {
233 LLVM_DEBUG(dbgs() << "No blocks in this region may have entries from "
234 "outside the region except for the first block!\n"
235 << "Problematic source BB: " << BB->getName() << "\n"
236 << "Problematic destination BB: " << PBB->getName()
237 << "\n");
238 return {};
239 }
240 }
241
242 return Result;
243}
244
245/// isAlignmentPreservedForAddrCast - Return true if the cast operation
246/// for specified target preserves original alignment
247static bool isAlignmentPreservedForAddrCast(const Triple &TargetTriple) {
248 switch (TargetTriple.getArch()) {
251 return true;
252 // TODO: Add other architectures for which we are certain that alignment
253 // is preserved during address space cast operations.
254 default:
255 return false;
256 }
257 return false;
258}
259
261 bool AggregateArgs, BlockFrequencyInfo *BFI,
263 bool AllowVarArgs, bool AllowAlloca,
264 BasicBlock *AllocationBlock,
265 ArrayRef<BasicBlock *> DeallocationBlocks,
266 std::string Suffix, bool ArgsInZeroAddressSpace,
267 bool VoidReturnWithSingleOutput)
268 : DT(DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
269 BPI(BPI), AC(AC), AllocationBlock(AllocationBlock),
270 DeallocationBlocks(DeallocationBlocks), AllowVarArgs(AllowVarArgs),
271 Blocks(buildExtractionBlockSet(BBs, DT, AllowVarArgs, AllowAlloca)),
272 Suffix(Suffix), ArgsInZeroAddressSpace(ArgsInZeroAddressSpace),
273 VoidReturnWithSingleOutput(VoidReturnWithSingleOutput) {}
274
275/// definedInRegion - Return true if the specified value is defined in the
276/// extracted region.
277static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) {
279 if (Blocks.count(I->getParent()))
280 return true;
281 return false;
282}
283
284/// definedInCaller - Return true if the specified value is defined in the
285/// function being code extracted, but not in the region being extracted.
286/// These values must be passed in as live-ins to the function.
287static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) {
288 if (isa<Argument>(V)) return true;
290 if (!Blocks.count(I->getParent()))
291 return true;
292 return false;
293}
294
296 BasicBlock *CommonExitBlock = nullptr;
297 auto hasNonCommonExitSucc = [&](BasicBlock *Block) {
298 for (auto *Succ : successors(Block)) {
299 // Internal edges, ok.
300 if (Blocks.count(Succ))
301 continue;
302 if (!CommonExitBlock) {
303 CommonExitBlock = Succ;
304 continue;
305 }
306 if (CommonExitBlock != Succ)
307 return true;
308 }
309 return false;
310 };
311
312 if (any_of(Blocks, hasNonCommonExitSucc))
313 return nullptr;
314
315 return CommonExitBlock;
316}
317
319 for (BasicBlock &BB : F) {
320 for (Instruction &II : BB)
321 if (auto *AI = dyn_cast<AllocaInst>(&II))
322 Allocas.push_back(AI);
323
324 findSideEffectInfoForBlock(BB);
325 }
326}
327
328void CodeExtractorAnalysisCache::findSideEffectInfoForBlock(BasicBlock &BB) {
329 for (Instruction &II : BB) {
330 unsigned Opcode = II.getOpcode();
331 Value *MemAddr = nullptr;
332 switch (Opcode) {
333 case Instruction::Store:
334 case Instruction::Load: {
335 if (Opcode == Instruction::Store) {
337 MemAddr = SI->getPointerOperand();
338 } else {
339 LoadInst *LI = cast<LoadInst>(&II);
340 MemAddr = LI->getPointerOperand();
341 }
342 // Global variable can not be aliased with locals.
343 if (isa<Constant>(MemAddr))
344 break;
346 if (!isa<AllocaInst>(Base)) {
347 SideEffectingBlocks.insert(&BB);
348 return;
349 }
350 BaseMemAddrs[&BB].insert(Base);
351 break;
352 }
353 default: {
354 IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(&II);
355 if (IntrInst) {
356 if (IntrInst->isLifetimeStartOrEnd() || isa<PseudoProbeInst>(IntrInst))
357 break;
358 SideEffectingBlocks.insert(&BB);
359 return;
360 }
361 // Treat all the other cases conservatively if it has side effects.
362 if (II.mayHaveSideEffects()) {
363 SideEffectingBlocks.insert(&BB);
364 return;
365 }
366 }
367 }
368 }
369}
370
372 BasicBlock &BB, AllocaInst *Addr) const {
373 if (SideEffectingBlocks.count(&BB))
374 return true;
375 auto It = BaseMemAddrs.find(&BB);
376 if (It != BaseMemAddrs.end())
377 return It->second.count(Addr);
378 return false;
379}
380
382 const CodeExtractorAnalysisCache &CEAC, Instruction *Addr) const {
384 Function *Func = (*Blocks.begin())->getParent();
385 for (BasicBlock &BB : *Func) {
386 if (Blocks.count(&BB))
387 continue;
388 if (CEAC.doesBlockContainClobberOfAddr(BB, AI))
389 return false;
390 }
391 return true;
392}
393
396 BasicBlock *SinglePredFromOutlineRegion = nullptr;
397 assert(!Blocks.count(CommonExitBlock) &&
398 "Expect a block outside the region!");
399 for (auto *Pred : predecessors(CommonExitBlock)) {
400 if (!Blocks.count(Pred))
401 continue;
402 if (!SinglePredFromOutlineRegion) {
403 SinglePredFromOutlineRegion = Pred;
404 } else if (SinglePredFromOutlineRegion != Pred) {
405 SinglePredFromOutlineRegion = nullptr;
406 break;
407 }
408 }
409
410 if (SinglePredFromOutlineRegion)
411 return SinglePredFromOutlineRegion;
412
413#ifndef NDEBUG
414 auto getFirstPHI = [](BasicBlock *BB) {
415 BasicBlock::iterator I = BB->begin();
416 PHINode *FirstPhi = nullptr;
417 while (I != BB->end()) {
419 if (!Phi)
420 break;
421 if (!FirstPhi) {
422 FirstPhi = Phi;
423 break;
424 }
425 }
426 return FirstPhi;
427 };
428 // If there are any phi nodes, the single pred either exists or has already
429 // be created before code extraction.
430 assert(!getFirstPHI(CommonExitBlock) && "Phi not expected");
431#endif
432
433 BasicBlock *NewExitBlock =
434 CommonExitBlock->splitBasicBlock(CommonExitBlock->getFirstNonPHIIt());
435
436 for (BasicBlock *Pred :
437 llvm::make_early_inc_range(predecessors(CommonExitBlock))) {
438 if (Blocks.count(Pred))
439 continue;
440 Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock);
441 }
442 // Now add the old exit block to the outline region.
443 Blocks.insert(CommonExitBlock);
444 return CommonExitBlock;
445}
446
448 Type *VarType, const Twine &Name,
449 AddrSpaceCastInst **CastedAlloc) {
450 const DataLayout &DL = AllocaIP.getBlock()->getModule()->getDataLayout();
451 Instruction *Alloca = new AllocaInst(VarType, DL.getAllocaAddrSpace(),
452 nullptr, Name, AllocaIP.getPoint());
453
454 if (CastedAlloc && ArgsInZeroAddressSpace && DL.getAllocaAddrSpace() != 0) {
455 *CastedAlloc = new AddrSpaceCastInst(
456 Alloca, PointerType::get(AllocaIP.getBlock()->getContext(), 0),
457 Name + ".ascast");
458 (*CastedAlloc)->insertAfter(Alloca->getIterator());
459 }
460 return Alloca;
461}
462
464 Type *) {
465 // Default alloca instructions created by allocateVar are released implicitly.
466 return nullptr;
467}
468
469// Find the pair of life time markers for address 'Addr' that are either
470// defined inside the outline region or can legally be shrinkwrapped into the
471// outline region. If there are not other untracked uses of the address, return
472// the pair of markers if found; otherwise return a pair of nullptr.
473CodeExtractor::LifetimeMarkerInfo
474CodeExtractor::getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
475 Instruction *Addr,
476 BasicBlock *ExitBlock) const {
477 LifetimeMarkerInfo Info;
478
479 for (User *U : Addr->users()) {
481 if (IntrInst) {
482 // We don't model addresses with multiple start/end markers, but the
483 // markers do not need to be in the region.
484 if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start) {
485 if (Info.LifeStart)
486 return {};
487 Info.LifeStart = IntrInst;
488 continue;
489 }
490 if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) {
491 if (Info.LifeEnd)
492 return {};
493 Info.LifeEnd = IntrInst;
494 continue;
495 }
496 }
497 // Find untracked uses of the address, bail.
498 if (!definedInRegion(Blocks, U))
499 return {};
500 }
501
502 if (!Info.LifeStart || !Info.LifeEnd)
503 return {};
504
505 Info.SinkLifeStart = !definedInRegion(Blocks, Info.LifeStart);
506 Info.HoistLifeEnd = !definedInRegion(Blocks, Info.LifeEnd);
507 // Do legality check.
508 if ((Info.SinkLifeStart || Info.HoistLifeEnd) &&
510 return {};
511
512 // Check to see if we have a place to do hoisting, if not, bail.
513 if (Info.HoistLifeEnd && !ExitBlock)
514 return {};
515
516 return Info;
517}
518
520 ValueSet &SinkCands, ValueSet &HoistCands,
521 BasicBlock *&ExitBlock) const {
522 Function *Func = (*Blocks.begin())->getParent();
523 ExitBlock = getCommonExitBlock(Blocks);
524
525 auto moveOrIgnoreLifetimeMarkers =
526 [&](const LifetimeMarkerInfo &LMI) -> bool {
527 if (!LMI.LifeStart)
528 return false;
529 if (LMI.SinkLifeStart) {
530 LLVM_DEBUG(dbgs() << "Sinking lifetime.start: " << *LMI.LifeStart
531 << "\n");
532 SinkCands.insert(LMI.LifeStart);
533 }
534 if (LMI.HoistLifeEnd) {
535 LLVM_DEBUG(dbgs() << "Hoisting lifetime.end: " << *LMI.LifeEnd << "\n");
536 HoistCands.insert(LMI.LifeEnd);
537 }
538 return true;
539 };
540
541 // Look up allocas in the original function in CodeExtractorAnalysisCache, as
542 // this is much faster than walking all the instructions.
543 for (AllocaInst *AI : CEAC.getAllocas()) {
544 BasicBlock *BB = AI->getParent();
545 if (Blocks.count(BB))
546 continue;
547
548 // As a prior call to extractCodeRegion() may have shrinkwrapped the alloca,
549 // check whether it is actually still in the original function.
550 Function *AIFunc = BB->getParent();
551 if (AIFunc != Func)
552 continue;
553
554 LifetimeMarkerInfo MarkerInfo = getLifetimeMarkers(CEAC, AI, ExitBlock);
555 bool Moved = moveOrIgnoreLifetimeMarkers(MarkerInfo);
556 if (Moved) {
557 LLVM_DEBUG(dbgs() << "Sinking alloca: " << *AI << "\n");
558 SinkCands.insert(AI);
559 continue;
560 }
561
562 // Find bitcasts in the outlined region that have lifetime marker users
563 // outside that region. Replace the lifetime marker use with an
564 // outside region bitcast to avoid unnecessary alloca/reload instructions
565 // and extra lifetime markers.
566 SmallVector<Instruction *, 2> LifetimeBitcastUsers;
567 for (User *U : AI->users()) {
568 if (!definedInRegion(Blocks, U))
569 continue;
570
571 if (U->stripInBoundsConstantOffsets() != AI)
572 continue;
573
574 Instruction *Bitcast = cast<Instruction>(U);
575 for (User *BU : Bitcast->users()) {
576 auto *IntrInst = dyn_cast<LifetimeIntrinsic>(BU);
577 if (!IntrInst)
578 continue;
579
580 if (definedInRegion(Blocks, IntrInst))
581 continue;
582
583 LLVM_DEBUG(dbgs() << "Replace use of extracted region bitcast"
584 << *Bitcast << " in out-of-region lifetime marker "
585 << *IntrInst << "\n");
586 LifetimeBitcastUsers.push_back(IntrInst);
587 }
588 }
589
590 for (Instruction *I : LifetimeBitcastUsers) {
591 Module *M = AIFunc->getParent();
592 LLVMContext &Ctx = M->getContext();
593 auto *Int8PtrTy = PointerType::getUnqual(Ctx);
594 CastInst *CastI =
595 CastInst::CreatePointerCast(AI, Int8PtrTy, "lt.cast", I->getIterator());
596 I->replaceUsesOfWith(I->getOperand(1), CastI);
597 }
598
599 // Follow any bitcasts.
601 SmallVector<LifetimeMarkerInfo, 2> BitcastLifetimeInfo;
602 for (User *U : AI->users()) {
603 if (U->stripInBoundsConstantOffsets() == AI) {
604 Instruction *Bitcast = cast<Instruction>(U);
605 LifetimeMarkerInfo LMI = getLifetimeMarkers(CEAC, Bitcast, ExitBlock);
606 if (LMI.LifeStart) {
607 Bitcasts.push_back(Bitcast);
608 BitcastLifetimeInfo.push_back(LMI);
609 continue;
610 }
611 }
612
613 // Found unknown use of AI.
614 if (!definedInRegion(Blocks, U)) {
615 Bitcasts.clear();
616 break;
617 }
618 }
619
620 // Either no bitcasts reference the alloca or there are unknown uses.
621 if (Bitcasts.empty())
622 continue;
623
624 LLVM_DEBUG(dbgs() << "Sinking alloca (via bitcast): " << *AI << "\n");
625 SinkCands.insert(AI);
626 for (unsigned I = 0, E = Bitcasts.size(); I != E; ++I) {
627 Instruction *BitcastAddr = Bitcasts[I];
628 const LifetimeMarkerInfo &LMI = BitcastLifetimeInfo[I];
629 assert(LMI.LifeStart &&
630 "Unsafe to sink bitcast without lifetime markers");
631 moveOrIgnoreLifetimeMarkers(LMI);
632 if (!definedInRegion(Blocks, BitcastAddr)) {
633 LLVM_DEBUG(dbgs() << "Sinking bitcast-of-alloca: " << *BitcastAddr
634 << "\n");
635 SinkCands.insert(BitcastAddr);
636 }
637 }
638 }
639}
640
642 if (Blocks.empty())
643 return false;
644 BasicBlock *Header = *Blocks.begin();
645 Function *F = Header->getParent();
646
647 // For functions with varargs, check that varargs handling is only done in the
648 // outlined function, i.e vastart and vaend are only used in outlined blocks.
649 if (AllowVarArgs && F->getFunctionType()->isVarArg()) {
650 auto containsVarArgIntrinsic = [](const Instruction &I) {
651 if (const CallInst *CI = dyn_cast<CallInst>(&I))
652 if (const Function *Callee = CI->getCalledFunction())
653 return Callee->getIntrinsicID() == Intrinsic::vastart ||
654 Callee->getIntrinsicID() == Intrinsic::vaend;
655 return false;
656 };
657
658 for (auto &BB : *F) {
659 if (Blocks.count(&BB))
660 continue;
661 if (llvm::any_of(BB, containsVarArgIntrinsic))
662 return false;
663 }
664 }
665 // stacksave as input implies stackrestore in the outlined function.
666 // This can confuse prolog epilog insertion phase.
667 // stacksave's uses must not cross outlined function.
668 for (BasicBlock *BB : Blocks) {
669 for (Instruction &I : *BB) {
671 if (!II)
672 continue;
673 bool IsSave = II->getIntrinsicID() == Intrinsic::stacksave;
674 bool IsRestore = II->getIntrinsicID() == Intrinsic::stackrestore;
675 if (IsSave && any_of(II->users(), [&Blks = this->Blocks](User *U) {
676 return !definedInRegion(Blks, U);
677 }))
678 return false;
679 if (IsRestore && !definedInRegion(Blocks, II->getArgOperand(0)))
680 return false;
681 }
682 }
683 return true;
684}
685
686void CodeExtractor::findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
687 const ValueSet &SinkCands,
688 bool CollectGlobalInputs) {
689 for (BasicBlock *BB : Blocks) {
690 // If a used value is defined outside the region, it's an input. If an
691 // instruction is used outside the region, it's an output.
692 for (Instruction &II : *BB) {
693 for (auto &OI : II.operands()) {
694 Value *V = OI;
695 if (!SinkCands.count(V) &&
696 (definedInCaller(Blocks, V) ||
697 (CollectGlobalInputs && llvm::isa<llvm::GlobalVariable>(V))))
698 Inputs.insert(V);
699 }
700
701 for (User *U : II.users())
702 if (!definedInRegion(Blocks, U)) {
703 Outputs.insert(&II);
704 break;
705 }
706 }
707 }
708
709 // Reset stale state from any prior call in HotColdSplitting; the CFG may
710 // have changed since.
711 FuncRetVal = nullptr;
712 if (!VoidReturnWithSingleOutput && !AggregateArgs && Outputs.size() == 1 &&
713 getCommonExitBlock(Blocks)) {
714 FuncRetVal = Outputs[0];
715 Outputs.clear();
716 }
717}
718
719/// severSplitPHINodesOfEntry - If a PHI node has multiple inputs from outside
720/// of the region, we need to split the entry block of the region so that the
721/// PHI node is easier to deal with.
722void CodeExtractor::severSplitPHINodesOfEntry(BasicBlock *&Header) {
723 unsigned NumPredsFromRegion = 0;
724 unsigned NumPredsOutsideRegion = 0;
725
726 if (Header != &Header->getParent()->getEntryBlock()) {
727 PHINode *PN = dyn_cast<PHINode>(Header->begin());
728 if (!PN) return; // No PHI nodes.
729
730 // If the header node contains any PHI nodes, check to see if there is more
731 // than one entry from outside the region. If so, we need to sever the
732 // header block into two.
733 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
734 if (Blocks.count(PN->getIncomingBlock(i)))
735 ++NumPredsFromRegion;
736 else
737 ++NumPredsOutsideRegion;
738
739 // If there is one (or fewer) predecessor from outside the region, we don't
740 // need to do anything special.
741 if (NumPredsOutsideRegion <= 1) return;
742 }
743
744 // Otherwise, we need to split the header block into two pieces: one
745 // containing PHI nodes merging values from outside of the region, and a
746 // second that contains all of the code for the block and merges back any
747 // incoming values from inside of the region.
748 BasicBlock *NewBB = SplitBlock(Header, Header->getFirstNonPHIIt(), DT);
749
750 // We only want to code extract the second block now, and it becomes the new
751 // header of the region.
752 BasicBlock *OldPred = Header;
753 Blocks.remove(OldPred);
754 Blocks.insert(NewBB);
755 Header = NewBB;
756
757 // Okay, now we need to adjust the PHI nodes and any branches from within the
758 // region to go to the new header block instead of the old header block.
759 if (NumPredsFromRegion) {
760 PHINode *PN = cast<PHINode>(OldPred->begin());
761 // Loop over all of the predecessors of OldPred that are in the region,
762 // changing them to branch to NewBB instead.
763 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
764 if (Blocks.count(PN->getIncomingBlock(i))) {
766 TI->replaceUsesOfWith(OldPred, NewBB);
767 }
768
769 // Okay, everything within the region is now branching to the right block, we
770 // just have to update the PHI nodes now, inserting PHI nodes into NewBB.
771 BasicBlock::iterator AfterPHIs;
772 for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
773 PHINode *PN = cast<PHINode>(AfterPHIs);
774 // Create a new PHI node in the new region, which has an incoming value
775 // from OldPred of PN.
776 PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion,
777 PN->getName() + ".ce");
778 NewPN->insertBefore(NewBB->begin());
779 PN->replaceAllUsesWith(NewPN);
780 NewPN->addIncoming(PN, OldPred);
781
782 // Loop over all of the incoming value in PN, moving them to NewPN if they
783 // are from the extracted region.
784 PN->removeIncomingValueIf([&](unsigned i) {
785 if (Blocks.count(PN->getIncomingBlock(i))) {
786 NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i));
787 return true;
788 }
789 return false;
790 });
791 }
792 }
793}
794
795/// severSplitPHINodesOfExits - if PHI nodes in exit blocks have inputs from
796/// outlined region, we split these PHIs on two: one with inputs from region
797/// and other with remaining incoming blocks; then first PHIs are placed in
798/// outlined region.
799void CodeExtractor::severSplitPHINodesOfExits() {
800 for (BasicBlock *ExitBB : ExtractedFuncRetVals) {
801 BasicBlock *NewBB = nullptr;
802
803 for (PHINode &PN : ExitBB->phis()) {
804 // Find all incoming values from the outlining region.
805 SmallVector<unsigned, 2> IncomingVals;
806 for (unsigned i = 0; i < PN.getNumIncomingValues(); ++i)
807 if (Blocks.count(PN.getIncomingBlock(i)))
808 IncomingVals.push_back(i);
809
810 // Do not process PHI if there is one (or fewer) predecessor from region.
811 // If PHI has exactly one predecessor from region, only this one incoming
812 // will be replaced on codeRepl block, so it should be safe to skip PHI.
813 if (IncomingVals.size() <= 1)
814 continue;
815
816 // Create block for new PHIs and add it to the list of outlined if it
817 // wasn't done before.
818 if (!NewBB) {
819 NewBB = BasicBlock::Create(ExitBB->getContext(),
820 ExitBB->getName() + ".split",
821 ExitBB->getParent(), ExitBB);
823 for (BasicBlock *PredBB : Preds)
824 if (Blocks.count(PredBB))
825 PredBB->getTerminator()->replaceUsesOfWith(ExitBB, NewBB);
826 UncondBrInst::Create(ExitBB, NewBB);
827 Blocks.insert(NewBB);
828 }
829
830 // Split this PHI.
831 PHINode *NewPN = PHINode::Create(PN.getType(), IncomingVals.size(),
832 PN.getName() + ".ce");
833 NewPN->insertBefore(NewBB->getFirstNonPHIIt());
834 for (unsigned i : IncomingVals)
835 NewPN->addIncoming(PN.getIncomingValue(i), PN.getIncomingBlock(i));
836 for (unsigned i : reverse(IncomingVals))
837 PN.removeIncomingValue(i, false);
838 PN.addIncoming(NewPN, NewBB);
839 }
840 }
841}
842
843void CodeExtractor::splitReturnBlocks() {
844 for (BasicBlock *Block : Blocks)
845 if (ReturnInst *RI = dyn_cast<ReturnInst>(Block->getTerminator())) {
846 BasicBlock *New =
847 Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret");
848 if (DT) {
849 // Old dominates New. New node dominates all other nodes dominated
850 // by Old.
851 DomTreeNode *OldNode = DT->getNode(Block);
853 OldNode->end());
854
855 DomTreeNode *NewNode = DT->addNewBlock(New, Block);
856
857 for (DomTreeNode *I : Children)
858 DT->changeImmediateDominator(I, NewNode);
859 }
860 }
861}
862
863Function *CodeExtractor::constructFunctionDeclaration(
864 const ValueSet &inputs, const ValueSet &outputs, BlockFrequency EntryFreq,
865 const Twine &Name, ValueSet &StructValues, StructType *&StructTy) {
866 LLVM_DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
867 LLVM_DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
868
869 Function *oldFunction = Blocks.front()->getParent();
870 Module *M = Blocks.front()->getModule();
871
872 // Assemble the function's parameter lists.
873 std::vector<Type *> ParamTy;
874 std::vector<Type *> AggParamTy;
875 const DataLayout &DL = M->getDataLayout();
876
877 // Add the types of the input values to the function's argument list
878 for (Value *value : inputs) {
879 LLVM_DEBUG(dbgs() << "value used in func: " << *value << "\n");
880 if (AggregateArgs && !ExcludeArgsFromAggregate.contains(value)) {
881 AggParamTy.push_back(value->getType());
882 StructValues.insert(value);
883 } else
884 ParamTy.push_back(value->getType());
885 }
886
887 // Add the types of the output values to the function's argument list.
888 for (Value *output : outputs) {
889 LLVM_DEBUG(dbgs() << "instr used in func: " << *output << "\n");
890 if (AggregateArgs && !ExcludeArgsFromAggregate.contains(output)) {
891 AggParamTy.push_back(output->getType());
892 StructValues.insert(output);
893 } else
894 ParamTy.push_back(
895 PointerType::get(output->getContext(), DL.getAllocaAddrSpace()));
896 }
897
898 assert(
899 (ParamTy.size() + AggParamTy.size()) ==
900 (inputs.size() + outputs.size()) &&
901 "Number of scalar and aggregate params does not match inputs, outputs");
902 assert((StructValues.empty() || AggregateArgs) &&
903 "Expeced StructValues only with AggregateArgs set");
904
905 // Concatenate scalar and aggregate params in ParamTy.
906 if (!AggParamTy.empty()) {
907 StructTy = StructType::get(M->getContext(), AggParamTy);
908 ParamTy.push_back(PointerType::get(
909 M->getContext(), ArgsInZeroAddressSpace ? 0 : DL.getAllocaAddrSpace()));
910 }
911
912 Type *RetTy = FuncRetVal ? FuncRetVal->getType() : getSwitchType();
913 LLVM_DEBUG({
914 dbgs() << "Function type: " << *RetTy << " f(";
915 for (Type *i : ParamTy)
916 dbgs() << *i << ", ";
917 dbgs() << ")\n";
918 });
919
920 FunctionType *funcType = FunctionType::get(
921 RetTy, ParamTy, AllowVarArgs && oldFunction->isVarArg());
922
923 // Create the new function
924 Function *newFunction =
926 oldFunction->getAddressSpace(), Name, M);
927
928 // Propagate personality info to the new function if there is one.
929 if (oldFunction->hasPersonalityFn())
930 newFunction->setPersonalityFn(oldFunction->getPersonalityFn());
931
932 // Inherit all of the target dependent attributes and white-listed
933 // target independent attributes.
934 // (e.g. If the extracted region contains a call to an x86.sse
935 // instruction we need to make sure that the extracted region has the
936 // "target-features" attribute allowing it to be lowered.
937 // FIXME: This should be changed to check to see if a specific
938 // attribute can not be inherited.
939 for (const auto &Attr : oldFunction->getAttributes().getFnAttrs()) {
940 if (Attr.isStringAttribute()) {
941 if (Attr.getKindAsString() == "thunk")
942 continue;
943 } else
944 switch (Attr.getKindAsEnum()) {
945 // Those attributes cannot be propagated safely. Explicitly list them
946 // here so we get a warning if new attributes are added.
947 case Attribute::AllocSize:
948 case Attribute::Builtin:
949 case Attribute::Convergent:
950 case Attribute::JumpTable:
951 case Attribute::Naked:
952 case Attribute::NoBuiltin:
953 case Attribute::NoMerge:
954 case Attribute::NoReturn:
955 case Attribute::NoSync:
956 case Attribute::ReturnsTwice:
957 case Attribute::Speculatable:
958 case Attribute::StackAlignment:
959 case Attribute::WillReturn:
960 case Attribute::AllocKind:
961 case Attribute::PresplitCoroutine:
962 case Attribute::Memory:
963 case Attribute::NoFPClass:
964 case Attribute::CoroDestroyOnlyWhenComplete:
965 case Attribute::CoroElideSafe:
966 case Attribute::NoDivergenceSource:
967 case Attribute::NoCreateUndefOrPoison:
968 continue;
969 // Those attributes should be safe to propagate to the extracted function.
970 case Attribute::AlwaysInline:
971 case Attribute::Cold:
972 case Attribute::DisableSanitizerInstrumentation:
973 case Attribute::Flatten:
974 case Attribute::FnRetThunkExtern:
975 case Attribute::Hot:
976 case Attribute::HybridPatchable:
977 case Attribute::NoRecurse:
978 case Attribute::InlineHint:
979 case Attribute::MinSize:
980 case Attribute::NoCallback:
981 case Attribute::NoDuplicate:
982 case Attribute::NoFree:
983 case Attribute::NoImplicitFloat:
984 case Attribute::NoInline:
985 case Attribute::NoIPA:
986 case Attribute::NoOutline:
987 case Attribute::NonLazyBind:
988 case Attribute::NoRedZone:
989 case Attribute::NoUnwind:
990 case Attribute::NoSanitizeBounds:
991 case Attribute::NoSanitizeCoverage:
992 case Attribute::NullPointerIsValid:
993 case Attribute::OptimizeForDebugging:
994 case Attribute::OptForFuzzing:
995 case Attribute::OptimizeNone:
996 case Attribute::OptimizeForSize:
997 case Attribute::SafeStack:
998 case Attribute::ShadowCallStack:
999 case Attribute::SanitizeAddress:
1000 case Attribute::SanitizeMemory:
1001 case Attribute::SanitizeNumericalStability:
1002 case Attribute::SanitizeThread:
1003 case Attribute::SanitizeType:
1004 case Attribute::SanitizeHWAddress:
1005 case Attribute::SanitizeMemTag:
1006 case Attribute::SanitizeRealtime:
1007 case Attribute::SanitizeRealtimeBlocking:
1008 case Attribute::SanitizeAllocToken:
1009 case Attribute::SpeculativeLoadHardening:
1010 case Attribute::StackProtect:
1011 case Attribute::StackProtectReq:
1012 case Attribute::StackProtectStrong:
1013 case Attribute::StrictFP:
1014 case Attribute::UWTable:
1015 case Attribute::VScaleRange:
1016 case Attribute::NoCfCheck:
1017 case Attribute::MustProgress:
1018 case Attribute::NoProfile:
1019 case Attribute::SkipProfile:
1020 case Attribute::DenormalFPEnv:
1021 break;
1022 // These attributes cannot be applied to functions.
1023 case Attribute::Alignment:
1024 case Attribute::AllocatedPointer:
1025 case Attribute::AllocAlign:
1026 case Attribute::ByVal:
1027 case Attribute::Captures:
1028 case Attribute::Dereferenceable:
1029 case Attribute::DereferenceableOrNull:
1030 case Attribute::ElementType:
1031 case Attribute::InAlloca:
1032 case Attribute::InReg:
1033 case Attribute::Nest:
1034 case Attribute::NoAlias:
1035 case Attribute::NoUndef:
1036 case Attribute::NonNull:
1037 case Attribute::Preallocated:
1038 case Attribute::ReadNone:
1039 case Attribute::ReadOnly:
1040 case Attribute::Returned:
1041 case Attribute::SExt:
1042 case Attribute::StructRet:
1043 case Attribute::SwiftError:
1044 case Attribute::SwiftSelf:
1045 case Attribute::SwiftAsync:
1046 case Attribute::ZExt:
1047 case Attribute::ImmArg:
1048 case Attribute::ByRef:
1049 case Attribute::WriteOnly:
1050 case Attribute::Writable:
1051 case Attribute::DeadOnUnwind:
1052 case Attribute::Range:
1053 case Attribute::Initializes:
1054 case Attribute::NoExt:
1055 // These are not really attributes.
1056 case Attribute::None:
1060 case Attribute::DeadOnReturn:
1061 llvm_unreachable("Not a function attribute");
1062 }
1063
1064 newFunction->addFnAttr(Attr);
1065 }
1066
1067 // Create scalar and aggregate iterators to name all of the arguments we
1068 // inserted.
1069 Function::arg_iterator ScalarAI = newFunction->arg_begin();
1070
1071 // Set names and attributes for input and output arguments.
1072 ScalarAI = newFunction->arg_begin();
1073 for (Value *input : inputs) {
1074 if (StructValues.contains(input))
1075 continue;
1076
1077 ScalarAI->setName(input->getName());
1078 if (input->isSwiftError())
1079 newFunction->addParamAttr(ScalarAI - newFunction->arg_begin(),
1080 Attribute::SwiftError);
1081 ++ScalarAI;
1082 }
1083 for (Value *output : outputs) {
1084 if (StructValues.contains(output))
1085 continue;
1086
1087 ScalarAI->setName(output->getName() + ".out");
1088 ++ScalarAI;
1089 }
1090
1091 // Update the entry count of the function.
1092 if (BFI) {
1093 auto Count = BFI->getProfileCountFromFreq(EntryFreq);
1094 if (Count.has_value())
1095 newFunction->setEntryCount(*Count);
1096 }
1097
1098 return newFunction;
1099}
1100
1101/// If the original function has debug info, we have to add a debug location
1102/// to the new branch instruction from the artificial entry block.
1103/// We use the debug location of the first instruction in the extracted
1104/// blocks, as there is no other equivalent line in the source code.
1105static void applyFirstDebugLoc(Function *oldFunction,
1107 Instruction *BranchI) {
1108 if (oldFunction->getSubprogram()) {
1109 any_of(Blocks, [&BranchI](const BasicBlock *BB) {
1110 return any_of(*BB, [&BranchI](const Instruction &I) {
1111 if (!I.getDebugLoc())
1112 return false;
1113 BranchI->setDebugLoc(I.getDebugLoc());
1114 return true;
1115 });
1116 });
1117 }
1118}
1119
1120/// Erase lifetime.start markers which reference inputs to the extraction
1121/// region, and insert the referenced memory into \p LifetimesStart.
1122///
1123/// The extraction region is defined by a set of blocks (\p Blocks), and a set
1124/// of allocas which will be moved from the caller function into the extracted
1125/// function (\p SunkAllocas).
1127 const SetVector<Value *> &SunkAllocas,
1128 SetVector<Value *> &LifetimesStart) {
1129 for (BasicBlock *BB : Blocks) {
1132 if (!II)
1133 continue;
1134
1135 // Get the memory operand of the lifetime marker. If the underlying
1136 // object is a sunk alloca, or is otherwise defined in the extraction
1137 // region, the lifetime marker must not be erased.
1138 Value *Mem = II->getOperand(0);
1139 if (SunkAllocas.count(Mem) || definedInRegion(Blocks, Mem))
1140 continue;
1141
1142 if (II->getIntrinsicID() == Intrinsic::lifetime_start)
1143 LifetimesStart.insert(Mem);
1144 II->eraseFromParent();
1145 }
1146 }
1147}
1148
1149/// Insert lifetime start/end markers surrounding the call to the new function
1150/// for objects defined in the caller.
1152 Module *M, ArrayRef<Value *> LifetimesStart, ArrayRef<Value *> LifetimesEnd,
1153 CallInst *TheCall) {
1154 Instruction *Term = TheCall->getParent()->getTerminator();
1155
1156 // Emit lifetime markers for the pointers given in \p Objects. Insert the
1157 // markers before the call if \p InsertBefore, and after the call otherwise.
1158 auto insertMarkers = [&](Intrinsic::ID MarkerFunc, ArrayRef<Value *> Objects,
1159 bool InsertBefore) {
1160 for (Value *Mem : Objects) {
1162 TheCall->getFunction()) &&
1163 "Input memory not defined in original function");
1164
1165 Function *Func =
1166 Intrinsic::getOrInsertDeclaration(M, MarkerFunc, Mem->getType());
1167 auto Marker = CallInst::Create(Func, Mem);
1168 if (InsertBefore)
1169 Marker->insertBefore(TheCall->getIterator());
1170 else
1171 Marker->insertBefore(Term->getIterator());
1172 }
1173 };
1174
1175 if (!LifetimesStart.empty()) {
1176 insertMarkers(Intrinsic::lifetime_start, LifetimesStart,
1177 /*InsertBefore=*/true);
1178 }
1179
1180 if (!LifetimesEnd.empty()) {
1181 insertMarkers(Intrinsic::lifetime_end, LifetimesEnd,
1182 /*InsertBefore=*/false);
1183 }
1184}
1185
1186void CodeExtractor::moveCodeToFunction(Function *newFunction) {
1187 auto newFuncIt = newFunction->begin();
1188 for (BasicBlock *Block : Blocks) {
1189 // Delete the basic block from the old function, and the list of blocks
1190 Block->removeFromParent();
1191
1192 // Insert this basic block into the new function
1193 // Insert the original blocks after the entry block created
1194 // for the new function. The entry block may be followed
1195 // by a set of exit blocks at this point, but these exit
1196 // blocks better be placed at the end of the new function.
1197 newFuncIt = newFunction->insert(std::next(newFuncIt), Block);
1198 }
1199}
1200
1201void CodeExtractor::calculateNewCallTerminatorWeights(
1202 BasicBlock *CodeReplacer,
1203 const DenseMap<BasicBlock *, BlockFrequency> &ExitWeights,
1204 BranchProbabilityInfo *BPI) {
1205 using Distribution = BlockFrequencyInfoImplBase::Distribution;
1206 using BlockNode = BlockFrequencyInfoImplBase::BlockNode;
1207
1208 // Update the branch weights for the exit block.
1209 Instruction *TI = CodeReplacer->getTerminator();
1210 SmallVector<unsigned, 8> BranchWeights(TI->getNumSuccessors(), 0);
1211
1212 // Block Frequency distribution with dummy node.
1213 Distribution BranchDist;
1214
1215 SmallVector<BranchProbability, 4> EdgeProbabilities(
1217
1218 // Add each of the frequencies of the successors.
1219 for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) {
1220 BlockNode ExitNode(i);
1221 uint64_t ExitFreq = ExitWeights.lookup(TI->getSuccessor(i)).getFrequency();
1222 if (ExitFreq != 0)
1223 BranchDist.addExit(ExitNode, ExitFreq);
1224 else
1225 EdgeProbabilities[i] = BranchProbability::getZero();
1226 }
1227
1228 // Check for no total weight.
1229 if (BranchDist.Total == 0) {
1230 BPI->setEdgeProbability(CodeReplacer, EdgeProbabilities);
1231 return;
1232 }
1233
1234 // Normalize the distribution so that they can fit in unsigned.
1235 BranchDist.normalize();
1236
1237 // Create normalized branch weights and set the metadata.
1238 for (unsigned I = 0, E = BranchDist.Weights.size(); I < E; ++I) {
1239 const auto &Weight = BranchDist.Weights[I];
1240
1241 // Get the weight and update the current BFI.
1242 BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
1243 BranchProbability BP(Weight.Amount, BranchDist.Total);
1244 EdgeProbabilities[Weight.TargetNode.Index] = BP;
1245 }
1246 BPI->setEdgeProbability(CodeReplacer, EdgeProbabilities);
1247 TI->setMetadata(
1248 LLVMContext::MD_prof,
1249 MDBuilder(TI->getContext()).createBranchWeights(BranchWeights));
1250}
1251
1252/// Erase debug info intrinsics which refer to values in \p F but aren't in
1253/// \p F.
1255 for (Instruction &I : instructions(F)) {
1256 SmallVector<DbgVariableRecord *, 4> DbgVariableRecords;
1257 findDbgUsers(&I, DbgVariableRecords);
1258 for (DbgVariableRecord *DVR : DbgVariableRecords)
1259 if (DVR->getFunction() != &F)
1260 DVR->eraseFromParent();
1261 }
1262}
1263
1264/// Fix up the debug info in the old and new functions. Following changes are
1265/// done.
1266/// 1. If a debug record points to a value that has been replaced, update the
1267/// record to use the new value.
1268/// 2. If an Input value that has been replaced was used as a location of a
1269/// debug record in the Parent function, then materealize a similar record in
1270/// the new function.
1271/// 3. Point line locations and debug intrinsics to the new subprogram scope
1272/// 4. Remove intrinsics which point to values outside of the new function.
1273static void fixupDebugInfoPostExtraction(Function &OldFunc, Function &NewFunc,
1274 CallInst &TheCall,
1275 const SetVector<Value *> &Inputs,
1276 ArrayRef<Value *> NewValues) {
1277 DISubprogram *OldSP = OldFunc.getSubprogram();
1278 LLVMContext &Ctx = OldFunc.getContext();
1279
1280 if (!OldSP) {
1281 // Erase any debug info the new function contains.
1282 stripDebugInfo(NewFunc);
1283 // Make sure the old function doesn't contain any non-local metadata refs.
1285 return;
1286 }
1287
1288 // Create a subprogram for the new function. Leave out a description of the
1289 // function arguments, as the parameters don't correspond to anything at the
1290 // source level.
1291 assert(OldSP->getUnit() && "Missing compile unit for subprogram");
1292 DIBuilder DIB(*OldFunc.getParent(), /*AllowUnresolved=*/false,
1293 OldSP->getUnit());
1294 auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray({}));
1295 DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagDefinition |
1296 DISubprogram::SPFlagOptimized |
1297 DISubprogram::SPFlagLocalToUnit;
1298 auto NewSP = DIB.createFunction(
1299 OldSP->getUnit(), NewFunc.getName(), NewFunc.getName(), OldSP->getFile(),
1300 /*LineNo=*/0, SPType, /*ScopeLine=*/0, DINode::FlagZero, SPFlags);
1301 NewFunc.setSubprogram(NewSP);
1302
1303 auto UpdateOrInsertDebugRecord = [&](auto *DR, Value *OldLoc, Value *NewLoc,
1304 DIExpression *Expr, bool Declare) {
1305 if (DR->getParent()->getParent() == &NewFunc) {
1306 DR->replaceVariableLocationOp(OldLoc, NewLoc);
1307 return;
1308 }
1309 if (Declare) {
1310 DIB.insertDeclare(NewLoc, DR->getVariable(), Expr, DR->getDebugLoc(),
1311 &NewFunc.getEntryBlock());
1312 return;
1313 }
1314 DIB.insertDbgValue(NewLoc, DR->getVariable(), Expr, DR->getDebugLoc(),
1315 NewFunc.getEntryBlock().getTerminator()->getIterator());
1316 };
1317 for (auto [Input, NewVal] : zip_equal(Inputs, NewValues)) {
1319 findDbgUsers(Input, DPUsers);
1320 DIExpression *Expr = DIB.createExpression();
1321
1322 // Iterate the debud users of the Input values. If they are in the extracted
1323 // function then update their location with the new value. If they are in
1324 // the parent function then create a similar debug record.
1325 for (auto *DVR : DPUsers)
1326 UpdateOrInsertDebugRecord(DVR, Input, NewVal, Expr, DVR->isDbgDeclare());
1327 }
1328
1329 auto IsInvalidLocation = [&NewFunc](Value *Location) {
1330 // Location is invalid if it isn't a constant, an instruction or an
1331 // argument, or is an instruction/argument but isn't in the new function.
1332 if (!Location || (!isa<Constant>(Location) && !isa<Argument>(Location) &&
1333 !isa<Instruction>(Location)))
1334 return true;
1335
1336 if (Argument *Arg = dyn_cast<Argument>(Location))
1337 return Arg->getParent() != &NewFunc;
1338 if (Instruction *LocationInst = dyn_cast<Instruction>(Location))
1339 return LocationInst->getFunction() != &NewFunc;
1340 return false;
1341 };
1342
1343 // Debug intrinsics in the new function need to be updated in one of two
1344 // ways:
1345 // 1) They need to be deleted, because they describe a value in the old
1346 // function.
1347 // 2) They need to point to fresh metadata, e.g. because they currently
1348 // point to a variable in the wrong scope.
1349 SmallDenseMap<DINode *, DINode *> RemappedMetadata;
1352
1353 auto GetUpdatedDIVariable = [&](DILocalVariable *OldVar) {
1354 DINode *&NewVar = RemappedMetadata[OldVar];
1355 if (!NewVar) {
1357 *OldVar->getScope(), *NewSP, Ctx, Cache);
1358 NewVar = DIB.createAutoVariable(
1359 NewScope, OldVar->getName(), OldVar->getFile(), OldVar->getLine(),
1360 OldVar->getType(), /*AlwaysPreserve=*/false, DINode::FlagZero,
1361 OldVar->getAlignInBits());
1362 }
1363 return cast<DILocalVariable>(NewVar);
1364 };
1365
1366 auto UpdateDbgLabel = [&](auto *LabelRecord) {
1367 // Point the label record to a fresh label within the new function if
1368 // the record was not inlined from some other function.
1369 if (LabelRecord->getDebugLoc().getInlinedAt())
1370 return;
1371 DILabel *OldLabel = LabelRecord->getLabel();
1372 DINode *&NewLabel = RemappedMetadata[OldLabel];
1373 if (!NewLabel) {
1375 *OldLabel->getScope(), *NewSP, Ctx, Cache);
1376 NewLabel =
1377 DILabel::get(Ctx, NewScope, OldLabel->getName(), OldLabel->getFile(),
1378 OldLabel->getLine(), OldLabel->getColumn(),
1379 OldLabel->isArtificial(), OldLabel->getCoroSuspendIdx());
1380 }
1381 LabelRecord->setLabel(cast<DILabel>(NewLabel));
1382 };
1383
1384 auto UpdateDbgRecordsOnInst = [&](Instruction &I) -> void {
1385 for (DbgRecord &DR : I.getDbgRecordRange()) {
1386 if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1387 UpdateDbgLabel(DLR);
1388 continue;
1389 }
1390
1392 // If any of the used locations are invalid, delete the record.
1393 if (any_of(DVR.location_ops(), IsInvalidLocation)) {
1394 DVRsToDelete.push_back(&DVR);
1395 continue;
1396 }
1397
1398 // DbgAssign intrinsics have an extra Value argument:
1399 if (DVR.isDbgAssign() && IsInvalidLocation(DVR.getAddress())) {
1400 DVRsToDelete.push_back(&DVR);
1401 continue;
1402 }
1403
1404 // If the variable was in the scope of the old function, i.e. it was not
1405 // inlined, point the intrinsic to a fresh variable within the new
1406 // function.
1407 if (!DVR.getDebugLoc().getInlinedAt())
1408 DVR.setVariable(GetUpdatedDIVariable(DVR.getVariable()));
1409 }
1410 };
1411
1412 for (Instruction &I : instructions(NewFunc))
1413 UpdateDbgRecordsOnInst(I);
1414
1415 for (auto *DVR : DVRsToDelete)
1416 DVR->getMarker()->MarkedInstr->dropOneDbgRecord(DVR);
1417 DIB.finalizeSubprogram(NewSP);
1418
1419 // Fix up the scope information attached to the line locations and the
1420 // debug assignment metadata in the new function.
1422 for (Instruction &I : instructions(NewFunc)) {
1423 if (const DebugLoc &DL = I.getDebugLoc())
1424 I.setDebugLoc(
1425 DebugLoc::replaceInlinedAtSubprogram(DL, *NewSP, Ctx, Cache));
1426 for (DbgRecord &DR : I.getDbgRecordRange())
1427 DR.setDebugLoc(DebugLoc::replaceInlinedAtSubprogram(DR.getDebugLoc(),
1428 *NewSP, Ctx, Cache));
1429
1430 // Loop info metadata may contain line locations. Fix them up.
1431 auto updateLoopInfoLoc = [&Ctx, &Cache, NewSP](Metadata *MD) -> Metadata * {
1432 if (auto *Loc = dyn_cast_or_null<DILocation>(MD))
1433 return DebugLoc::replaceInlinedAtSubprogram(Loc, *NewSP, Ctx, Cache);
1434 return MD;
1435 };
1436 updateLoopMetadataDebugLocations(I, updateLoopInfoLoc);
1437 at::remapAssignID(AssignmentIDMap, I);
1438 }
1439 if (!TheCall.getDebugLoc())
1440 TheCall.setDebugLoc(DILocation::get(Ctx, 0, 0, OldSP));
1441
1443}
1444
1445Function *
1447 ValueSet Inputs, Outputs;
1448 return extractCodeRegion(CEAC, Inputs, Outputs);
1449}
1450
1451Function *
1453 ValueSet &inputs, ValueSet &outputs) {
1454 if (!isEligible())
1455 return nullptr;
1456
1457 // Assumption: this is a single-entry code region, and the header is the first
1458 // block in the region.
1459 BasicBlock *header = *Blocks.begin();
1460 Function *oldFunction = header->getParent();
1461
1462 normalizeCFGForExtraction(header);
1463
1464 // Remove @llvm.assume calls that will be moved to the new function from the
1465 // old function's assumption cache.
1466 for (BasicBlock *Block : Blocks) {
1468 if (auto *AI = dyn_cast<AssumeInst>(&I)) {
1469 if (AC)
1470 AC->unregisterAssumption(AI);
1471 AI->eraseFromParent();
1472 }
1473 }
1474 }
1475
1476 ValueSet SinkingCands, HoistingCands;
1477 BasicBlock *CommonExit = nullptr;
1478 findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit);
1479 assert(HoistingCands.empty() || CommonExit);
1480
1481 // Find inputs to, outputs from the code region.
1482 findInputsOutputs(inputs, outputs, SinkingCands);
1483
1484 // Collect objects which are inputs to the extraction region and also
1485 // referenced by lifetime start markers within it. The effects of these
1486 // markers must be replicated in the calling function to prevent the stack
1487 // coloring pass from merging slots which store input objects.
1488 ValueSet LifetimesStart;
1489 eraseLifetimeMarkersOnInputs(Blocks, SinkingCands, LifetimesStart);
1490
1491 if (!HoistingCands.empty()) {
1492 auto *HoistToBlock = findOrCreateBlockForHoisting(CommonExit);
1493 Instruction *TI = HoistToBlock->getTerminator();
1494 for (auto *II : HoistingCands)
1496 computeExtractedFuncRetVals();
1497 }
1498
1499 // CFG/ExitBlocks must not change hereafter
1500
1501 // Calculate the entry frequency of the new function before we change the root
1502 // block.
1503 BlockFrequency EntryFreq;
1505 if (BFI) {
1506 assert(BPI && "Both BPI and BFI are required to preserve profile info");
1507 for (BasicBlock *Pred : predecessors(header)) {
1508 if (Blocks.count(Pred))
1509 continue;
1510 EntryFreq +=
1511 BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, header);
1512 }
1513
1514 for (BasicBlock *Succ : ExtractedFuncRetVals) {
1515 for (BasicBlock *Block : predecessors(Succ)) {
1516 if (!Blocks.count(Block))
1517 continue;
1518
1519 // Update the branch weight for this successor.
1520 BlockFrequency &BF = ExitWeights[Succ];
1521 BF += BFI->getBlockFreq(Block) * BPI->getEdgeProbability(Block, Succ);
1522 }
1523 }
1524 }
1525
1526 // Determine position for the replacement code. Do so before header is moved
1527 // to the new function.
1528 BasicBlock *ReplIP = header;
1529 while (ReplIP && Blocks.count(ReplIP))
1530 ReplIP = ReplIP->getNextNode();
1531
1532 // Construct new function based on inputs/outputs & add allocas for all defs.
1533 std::string SuffixToUse =
1534 Suffix.empty()
1535 ? (header->getName().empty() ? "extracted" : header->getName().str())
1536 : Suffix;
1537
1538 ValueSet StructValues;
1539 StructType *StructTy = nullptr;
1540 Function *newFunction = constructFunctionDeclaration(
1541 inputs, outputs, EntryFreq, oldFunction->getName() + "." + SuffixToUse,
1542 StructValues, StructTy);
1543 SmallVector<Value *> NewValues;
1544
1545 emitFunctionBody(inputs, outputs, StructValues, newFunction, StructTy, header,
1546 SinkingCands, NewValues);
1547
1548 std::vector<Value *> Reloads;
1549 CallInst *TheCall = emitReplacerCall(
1550 inputs, outputs, StructValues, newFunction, StructTy, oldFunction, ReplIP,
1551 EntryFreq, LifetimesStart.getArrayRef(), Reloads);
1552
1553 insertReplacerCall(oldFunction, header, TheCall, outputs, Reloads,
1554 ExitWeights);
1555
1556 fixupDebugInfoPostExtraction(*oldFunction, *newFunction, *TheCall, inputs,
1557 NewValues);
1558
1559 LLVM_DEBUG(llvm::dbgs() << "After extractCodeRegion - newFunction:\n");
1560 LLVM_DEBUG(newFunction->dump());
1561 LLVM_DEBUG(llvm::dbgs() << "After extractCodeRegion - oldFunction:\n");
1562 LLVM_DEBUG(oldFunction->dump());
1563 LLVM_DEBUG(if (AC && verifyAssumptionCache(*oldFunction, *newFunction, AC))
1564 report_fatal_error("Stale Asumption cache for old Function!"));
1565 return newFunction;
1566}
1567
1568void CodeExtractor::normalizeCFGForExtraction(BasicBlock *&header) {
1569 // If we have any return instructions in the region, split those blocks so
1570 // that the return is not in the region.
1571 splitReturnBlocks();
1572
1573 // If we have to split PHI nodes of the entry or exit blocks, do so now.
1574 severSplitPHINodesOfEntry(header);
1575
1576 // If a PHI in an exit block has multiple incoming values from the outlined
1577 // region, create a new PHI for those values within the region such that only
1578 // PHI itself becomes an output value, not each of its incoming values
1579 // individually.
1580 computeExtractedFuncRetVals();
1581 severSplitPHINodesOfExits();
1582}
1583
1584void CodeExtractor::computeExtractedFuncRetVals() {
1585 ExtractedFuncRetVals.clear();
1586
1588 for (BasicBlock *Block : Blocks) {
1589 for (BasicBlock *Succ : successors(Block)) {
1590 if (Blocks.count(Succ))
1591 continue;
1592
1593 bool IsNew = ExitBlocks.insert(Succ).second;
1594 if (IsNew)
1595 ExtractedFuncRetVals.push_back(Succ);
1596 }
1597 }
1598}
1599
1600Type *CodeExtractor::getSwitchType() {
1601 LLVMContext &Context = Blocks.front()->getContext();
1602
1603 assert(ExtractedFuncRetVals.size() < 0xffff &&
1604 "too many exit blocks for switch");
1605 switch (ExtractedFuncRetVals.size()) {
1606 case 0:
1607 case 1:
1608 return Type::getVoidTy(Context);
1609 case 2:
1610 // Conditional branch, return a bool
1611 return Type::getInt1Ty(Context);
1612 default:
1613 return Type::getInt16Ty(Context);
1614 }
1615}
1616
1617void CodeExtractor::emitFunctionBody(
1618 const ValueSet &inputs, const ValueSet &outputs,
1619 const ValueSet &StructValues, Function *newFunction,
1620 StructType *StructArgTy, BasicBlock *header, const ValueSet &SinkingCands,
1621 SmallVectorImpl<Value *> &NewValues) {
1622 Function *oldFunction = header->getParent();
1623 LLVMContext &Context = oldFunction->getContext();
1624
1625 // The new function needs a root node because other nodes can branch to the
1626 // head of the region, but the entry node of a function cannot have preds.
1627 BasicBlock *newFuncRoot =
1628 BasicBlock::Create(Context, "newFuncRoot", newFunction);
1629
1630 // Now sink all instructions which only have non-phi uses inside the region.
1631 // Group the allocas at the start of the block, so that any bitcast uses of
1632 // the allocas are well-defined.
1633 for (auto *II : SinkingCands) {
1634 if (!isa<AllocaInst>(II)) {
1635 cast<Instruction>(II)->moveBefore(*newFuncRoot,
1636 newFuncRoot->getFirstInsertionPt());
1637 }
1638 }
1639 for (auto *II : SinkingCands) {
1640 if (auto *AI = dyn_cast<AllocaInst>(II)) {
1641 AI->moveBefore(*newFuncRoot, newFuncRoot->getFirstInsertionPt());
1642 }
1643 }
1644
1645 Function::arg_iterator ScalarAI = newFunction->arg_begin();
1646 Argument *AggArg = StructValues.empty()
1647 ? nullptr
1648 : newFunction->getArg(newFunction->arg_size() - 1);
1649
1650 // Rewrite all users of the inputs in the extracted region to use the
1651 // arguments (or appropriate addressing into struct) instead.
1652 for (unsigned i = 0, e = inputs.size(), aggIdx = 0; i != e; ++i) {
1653 Value *RewriteVal;
1654 if (StructValues.contains(inputs[i])) {
1655 Value *Idx[2];
1657 Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), aggIdx);
1658 GetElementPtrInst *GEP = GetElementPtrInst::Create(
1659 StructArgTy, AggArg, Idx, "gep_" + inputs[i]->getName(), newFuncRoot);
1660 LoadInst *LoadGEP =
1661 new LoadInst(StructArgTy->getElementType(aggIdx), GEP,
1662 "loadgep_" + inputs[i]->getName(), newFuncRoot);
1663 // If we load pointer, we can add optional !align metadata
1664 // The existence of the !align metadata on the instruction tells
1665 // the optimizer that the value loaded is known to be aligned to
1666 // a boundary specified by the integer value in the metadata node.
1667 // Example:
1668 // %res = load ptr, ptr %input, align 8, !align !align_md_node
1669 // ^ ^
1670 // | |
1671 // alignment of %input address |
1672 // |
1673 // alignment of %res object
1674 if (StructArgTy->getElementType(aggIdx)->isPointerTy()) {
1675 unsigned AlignmentValue;
1676 const Triple &TargetTriple =
1677 newFunction->getParent()->getTargetTriple();
1678 const DataLayout &DL = header->getDataLayout();
1679 // Pointers without casting can provide more information about
1680 // alignment. Use pointers without casts if given target preserves
1681 // alignment information for cast the operation.
1682 if (isAlignmentPreservedForAddrCast(TargetTriple))
1683 AlignmentValue =
1684 inputs[i]->stripPointerCasts()->getPointerAlignment(DL).value();
1685 else
1686 AlignmentValue = inputs[i]->getPointerAlignment(DL).value();
1687 MDBuilder MDB(header->getContext());
1688 LoadGEP->setMetadata(
1689 LLVMContext::MD_align,
1691 header->getContext(),
1692 MDB.createConstant(ConstantInt::get(
1693 Type::getInt64Ty(header->getContext()), AlignmentValue))));
1694 }
1695 RewriteVal = LoadGEP;
1696 ++aggIdx;
1697 } else
1698 RewriteVal = &*ScalarAI++;
1699
1700 NewValues.push_back(RewriteVal);
1701 }
1702
1703 moveCodeToFunction(newFunction);
1704
1705 for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
1706 Value *RewriteVal = NewValues[i];
1707
1708 std::vector<User *> Users(inputs[i]->user_begin(), inputs[i]->user_end());
1709 for (User *use : Users)
1710 if (Instruction *inst = dyn_cast<Instruction>(use))
1711 if (Blocks.count(inst->getParent()))
1712 inst->replaceUsesOfWith(inputs[i], RewriteVal);
1713 }
1714
1715 // Since there may be multiple exits from the original region, make the new
1716 // function return an unsigned, switch on that number. This loop iterates
1717 // over all of the blocks in the extracted region, updating any terminator
1718 // instructions in the to-be-extracted region that branch to blocks that are
1719 // not in the region to be extracted.
1720 std::map<BasicBlock *, BasicBlock *> ExitBlockMap;
1721
1722 // Iterate over the previously collected targets, and create new blocks inside
1723 // the function to branch to.
1724 for (auto P : enumerate(ExtractedFuncRetVals)) {
1725 BasicBlock *OldTarget = P.value();
1726 size_t SuccNum = P.index();
1727
1728 BasicBlock *NewTarget = BasicBlock::Create(
1729 Context, OldTarget->getName() + ".exitStub", newFunction);
1730 ExitBlockMap[OldTarget] = NewTarget;
1731
1732 Value *brVal = nullptr;
1733 Type *RetTy = FuncRetVal ? FuncRetVal->getType() : getSwitchType();
1734 assert(ExtractedFuncRetVals.size() < 0xffff &&
1735 "too many exit blocks for switch");
1736 switch (ExtractedFuncRetVals.size()) {
1737 case 0:
1738 // No value needed.
1739 break;
1740 case 1:
1741 if (FuncRetVal)
1742 brVal = FuncRetVal;
1743 break;
1744 case 2: // Conditional branch, return a bool
1745 brVal = ConstantInt::get(RetTy, !SuccNum);
1746 break;
1747 default:
1748 brVal = ConstantInt::get(RetTy, SuccNum);
1749 break;
1750 }
1751
1752 ReturnInst::Create(Context, brVal, NewTarget);
1753 }
1754
1755 for (BasicBlock *Block : Blocks) {
1756 Instruction *TI = Block->getTerminator();
1757 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1758 if (Blocks.count(TI->getSuccessor(i)))
1759 continue;
1760 BasicBlock *OldTarget = TI->getSuccessor(i);
1761 // add a new basic block which returns the appropriate value
1762 BasicBlock *NewTarget = ExitBlockMap[OldTarget];
1763 assert(NewTarget && "Unknown target block!");
1764
1765 // rewrite the original branch instruction with this new target
1766 TI->setSuccessor(i, NewTarget);
1767 }
1768 }
1769
1770 // Loop over all of the PHI nodes in the header and exit blocks, and change
1771 // any references to the old incoming edge to be the new incoming edge.
1772 for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) {
1773 PHINode *PN = cast<PHINode>(I);
1774 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1775 if (!Blocks.count(PN->getIncomingBlock(i)))
1776 PN->setIncomingBlock(i, newFuncRoot);
1777 }
1778
1779 // Connect newFunction entry block to new header.
1780 UncondBrInst *BranchI = UncondBrInst::Create(header, newFuncRoot);
1781 applyFirstDebugLoc(oldFunction, Blocks.getArrayRef(), BranchI);
1782
1783 // Store the arguments right after the definition of output value.
1784 // This should be proceeded after creating exit stubs to be ensure that invoke
1785 // result restore will be placed in the outlined function.
1786 ScalarAI = newFunction->arg_begin();
1787 unsigned AggIdx = 0;
1788
1789 for (Value *Input : inputs) {
1790 if (StructValues.contains(Input))
1791 ++AggIdx;
1792 else
1793 ++ScalarAI;
1794 }
1795
1796 for (Value *Output : outputs) {
1797 // Find proper insertion point.
1798 // In case Output is an invoke, we insert the store at the beginning in the
1799 // 'normal destination' BB. Otherwise we insert the store right after
1800 // Output.
1801 BasicBlock::iterator InsertPt;
1802 if (auto *InvokeI = dyn_cast<InvokeInst>(Output))
1803 InsertPt = InvokeI->getNormalDest()->getFirstInsertionPt();
1804 else if (auto *Phi = dyn_cast<PHINode>(Output))
1805 InsertPt = Phi->getParent()->getFirstInsertionPt();
1806 else if (auto *OutI = dyn_cast<Instruction>(Output))
1807 InsertPt = std::next(OutI->getIterator());
1808 else {
1809 // Globals don't need to be updated, just advance to the next argument.
1810 if (StructValues.contains(Output))
1811 ++AggIdx;
1812 else
1813 ++ScalarAI;
1814 continue;
1815 }
1816
1817 assert((InsertPt->getFunction() == newFunction ||
1818 Blocks.count(InsertPt->getParent())) &&
1819 "InsertPt should be in new function");
1820
1821 if (StructValues.contains(Output)) {
1822 assert(AggArg && "Number of aggregate output arguments should match "
1823 "the number of defined values");
1824 Value *Idx[2];
1826 Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), AggIdx);
1827 GetElementPtrInst *GEP = GetElementPtrInst::Create(
1828 StructArgTy, AggArg, Idx, "gep_" + Output->getName(), InsertPt);
1829 new StoreInst(Output, GEP, InsertPt);
1830 ++AggIdx;
1831 } else {
1832 assert(ScalarAI != newFunction->arg_end() &&
1833 "Number of scalar output arguments should match "
1834 "the number of defined values");
1835 new StoreInst(Output, &*ScalarAI, InsertPt);
1836 ++ScalarAI;
1837 }
1838 }
1839
1840 if (ExtractedFuncRetVals.empty()) {
1841 // Mark the new function `noreturn` if applicable. Terminators which resume
1842 // exception propagation are treated as returning instructions. This is to
1843 // avoid inserting traps after calls to outlined functions which unwind.
1844 if (none_of(Blocks, [](const BasicBlock *BB) {
1845 const Instruction *Term = BB->getTerminator();
1846 return isa<ReturnInst>(Term) || isa<ResumeInst>(Term);
1847 }))
1848 newFunction->setDoesNotReturn();
1849 }
1850}
1851
1852CallInst *CodeExtractor::emitReplacerCall(
1853 const ValueSet &inputs, const ValueSet &outputs,
1854 const ValueSet &StructValues, Function *newFunction,
1855 StructType *StructArgTy, Function *oldFunction, BasicBlock *ReplIP,
1856 BlockFrequency EntryFreq, ArrayRef<Value *> LifetimesStart,
1857 std::vector<Value *> &Reloads) {
1858 LLVMContext &Context = oldFunction->getContext();
1859 Module *M = oldFunction->getParent();
1860
1861 // This takes place of the original loop
1862 BasicBlock *codeReplacer =
1863 BasicBlock::Create(Context, "codeRepl", oldFunction, ReplIP);
1864 if (AllocationBlock)
1865 assert(AllocationBlock->getParent() == oldFunction &&
1866 "AllocationBlock is not in the same function");
1867 BasicBlock *AllocaBlock =
1868 AllocationBlock ? AllocationBlock : &oldFunction->getEntryBlock();
1869
1870 // Update the entry count of the function.
1871 if (BFI)
1872 BFI->setBlockFreq(codeReplacer, EntryFreq);
1873
1874 std::vector<Value *> params;
1875
1876 // Add inputs as params, or to be filled into the struct
1877 for (Value *input : inputs) {
1878 if (StructValues.contains(input))
1879 continue;
1880
1881 params.push_back(input);
1882 }
1883
1884 // Create allocas for the outputs
1885 std::vector<Value *> ReloadOutputs;
1886 for (Value *output : outputs) {
1887 if (StructValues.contains(output))
1888 continue;
1889
1890 Value *OutAlloc =
1891 allocateVar(IRBuilder<>::InsertPoint(
1892 AllocaBlock, AllocaBlock->getFirstInsertionPt()),
1893 output->getType(), output->getName() + ".loc");
1894 params.push_back(OutAlloc);
1895 ReloadOutputs.push_back(OutAlloc);
1896 }
1897
1898 Instruction *Struct = nullptr;
1899 if (!StructValues.empty()) {
1900 AddrSpaceCastInst *StructSpaceCast = nullptr;
1901 Struct = allocateVar(IRBuilder<>::InsertPoint(
1902 AllocaBlock, AllocaBlock->getFirstInsertionPt()),
1903 StructArgTy, "structArg", &StructSpaceCast);
1904 if (StructSpaceCast)
1905 params.push_back(StructSpaceCast);
1906 else
1907 params.push_back(Struct);
1908
1909 unsigned AggIdx = 0;
1910 for (Value *input : inputs) {
1911 if (!StructValues.contains(input))
1912 continue;
1913
1914 Value *Idx[2];
1916 Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), AggIdx);
1917 GetElementPtrInst *GEP = GetElementPtrInst::Create(
1918 StructArgTy, Struct, Idx, "gep_" + input->getName());
1919 GEP->insertInto(codeReplacer, codeReplacer->end());
1920 new StoreInst(input, GEP, codeReplacer);
1921
1922 ++AggIdx;
1923 }
1924 }
1925
1926 // Emit the call to the function
1927 CallInst *call = CallInst::Create(
1928 newFunction, params, ExtractedFuncRetVals.size() > 1 ? "targetBlock" : "",
1929 codeReplacer);
1930
1931 // Set swifterror parameter attributes.
1932 unsigned ParamIdx = 0;
1933 unsigned AggIdx = 0;
1934 for (auto input : inputs) {
1935 if (StructValues.contains(input)) {
1936 ++AggIdx;
1937 } else {
1938 if (input->isSwiftError())
1939 call->addParamAttr(ParamIdx, Attribute::SwiftError);
1940 ++ParamIdx;
1941 }
1942 }
1943
1944 // Add debug location to the new call, if the original function has debug
1945 // info. In that case, the terminator of the entry block of the extracted
1946 // function contains the first debug location of the extracted function,
1947 // set in extractCodeRegion.
1948 if (codeReplacer->getParent()->getSubprogram()) {
1949 if (auto DL = newFunction->getEntryBlock().getTerminator()->getDebugLoc())
1950 call->setDebugLoc(DL);
1951 }
1952
1953 // Reload the outputs passed in by reference, use the struct if output is in
1954 // the aggregate or reload from the scalar argument.
1955 for (unsigned i = 0, e = outputs.size(), scalarIdx = 0; i != e; ++i) {
1956 Value *Output = nullptr;
1957 if (StructValues.contains(outputs[i])) {
1958 Value *Idx[2];
1960 Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), AggIdx);
1961 GetElementPtrInst *GEP = GetElementPtrInst::Create(
1962 StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName());
1963 GEP->insertInto(codeReplacer, codeReplacer->end());
1964 Output = GEP;
1965 ++AggIdx;
1966 } else {
1967 Output = ReloadOutputs[scalarIdx];
1968 ++scalarIdx;
1969 }
1970 LoadInst *load =
1971 new LoadInst(outputs[i]->getType(), Output,
1972 outputs[i]->getName() + ".reload", codeReplacer);
1973 Reloads.push_back(load);
1974 }
1975
1976 // Now we can emit a switch statement using the call as a value.
1977 SwitchInst *TheSwitch =
1979 codeReplacer, 0, codeReplacer);
1980 for (auto P : enumerate(ExtractedFuncRetVals)) {
1981 BasicBlock *OldTarget = P.value();
1982 size_t SuccNum = P.index();
1983
1984 TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context), SuccNum),
1985 OldTarget);
1986 }
1987
1988 // Now that we've done the deed, simplify the switch instruction.
1989 Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType();
1990 switch (ExtractedFuncRetVals.size()) {
1991 case 0:
1992 // There are no successors (the block containing the switch itself), which
1993 // means that previously this was the last part of the function, and hence
1994 // this should be rewritten as a `ret` or `unreachable`.
1995 if (newFunction->doesNotReturn()) {
1996 // If fn is no return, end with an unreachable terminator.
1997 (void)new UnreachableInst(Context, TheSwitch->getIterator());
1998 } else if (OldFnRetTy->isVoidTy()) {
1999 // We have no return value.
2000 ReturnInst::Create(Context, nullptr,
2001 TheSwitch->getIterator()); // Return void
2002 } else if (OldFnRetTy == TheSwitch->getCondition()->getType()) {
2003 // return what we have
2005 TheSwitch->getIterator());
2006 } else {
2007 // Otherwise we must have code extracted an unwind or something, just
2008 // return whatever we want.
2010 TheSwitch->getIterator());
2011 }
2012
2013 TheSwitch->eraseFromParent();
2014 break;
2015 case 1:
2016 // Only a single destination, change the switch into an unconditional
2017 // branch.
2018 UncondBrInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getIterator());
2019 TheSwitch->eraseFromParent();
2020 break;
2021 case 2:
2022 // Only two destinations, convert to a condition branch.
2023 // Remark: This also swaps the target branches:
2024 // 0 -> false -> getSuccessor(2); 1 -> true -> getSuccessor(1)
2025 CondBrInst::Create(call, TheSwitch->getSuccessor(1),
2026 TheSwitch->getSuccessor(2), TheSwitch->getIterator());
2027 TheSwitch->eraseFromParent();
2028 break;
2029 default:
2030 // Otherwise, make the default destination of the switch instruction be one
2031 // of the other successors.
2032 TheSwitch->setCondition(call);
2033 TheSwitch->setDefaultDest(
2034 TheSwitch->getSuccessor(ExtractedFuncRetVals.size()));
2035 // Remove redundant case
2036 TheSwitch->removeCase(
2037 SwitchInst::CaseIt(TheSwitch, ExtractedFuncRetVals.size() - 1));
2038 break;
2039 }
2040
2041 // Insert lifetime markers around the reloads of any output values. The
2042 // allocas output values are stored in are only in-use in the codeRepl block.
2043 insertLifetimeMarkersSurroundingCall(M, ReloadOutputs, ReloadOutputs, call);
2044
2045 // Replicate the effects of any lifetime start/end markers which referenced
2046 // input objects in the extraction region by placing markers around the call.
2047 insertLifetimeMarkersSurroundingCall(oldFunction->getParent(), LifetimesStart,
2048 {}, call);
2049
2050 // Deallocate intermediate variables if they need explicit deallocation.
2051 auto deallocVars = [&](BasicBlock *DeallocBlock,
2052 BasicBlock::iterator DeallocIP) {
2053 int Index = 0;
2054 for (Value *Output : outputs) {
2055 if (!StructValues.contains(Output))
2056 deallocateVar(IRBuilder<>::InsertPoint(DeallocBlock, DeallocIP),
2057 ReloadOutputs[Index++], Output->getType());
2058 }
2059
2060 if (Struct)
2061 deallocateVar(IRBuilder<>::InsertPoint(DeallocBlock, DeallocIP), Struct,
2062 StructArgTy);
2063 };
2064
2065 if (DeallocationBlocks.empty()) {
2066 deallocVars(codeReplacer, codeReplacer->end());
2067 } else {
2068 for (BasicBlock *DeallocationBlock : DeallocationBlocks)
2069 deallocVars(DeallocationBlock, DeallocationBlock->getFirstInsertionPt());
2070 }
2071
2072 return call;
2073}
2074
2075void CodeExtractor::insertReplacerCall(
2076 Function *oldFunction, BasicBlock *header, CallInst *ReplacerCall,
2077 const ValueSet &outputs, ArrayRef<Value *> Reloads,
2078 const DenseMap<BasicBlock *, BlockFrequency> &ExitWeights) {
2079
2080 // Rewrite branches to basic blocks outside of the loop to new dummy blocks
2081 // within the new function. This must be done before we lose track of which
2082 // blocks were originally in the code region.
2083 BasicBlock *codeReplacer = ReplacerCall->getParent();
2084 std::vector<User *> Users(header->user_begin(), header->user_end());
2085 for (auto &U : Users)
2086 // The BasicBlock which contains the branch is not in the region
2087 // modify the branch target to a new block
2088 if (Instruction *I = dyn_cast<Instruction>(U))
2089 if (I->isTerminator() && I->getFunction() == oldFunction &&
2090 !Blocks.count(I->getParent()))
2091 I->replaceUsesOfWith(header, codeReplacer);
2092
2093 // When moving the code region it is sufficient to replace all uses to the
2094 // extracted function values. Since the original definition's block
2095 // dominated its use, it will also be dominated by codeReplacer's switch
2096 // which joined multiple exit blocks.
2097 for (BasicBlock *ExitBB : ExtractedFuncRetVals)
2098 for (PHINode &PN : ExitBB->phis()) {
2099 Value *IncomingCodeReplacerVal = nullptr;
2100 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
2101 // Ignore incoming values from outside of the extracted region.
2102 if (!Blocks.count(PN.getIncomingBlock(i)))
2103 continue;
2104
2105 // Ensure that there is only one incoming value from codeReplacer.
2106 if (!IncomingCodeReplacerVal) {
2107 PN.setIncomingBlock(i, codeReplacer);
2108 IncomingCodeReplacerVal = PN.getIncomingValue(i);
2109 } else
2110 assert(IncomingCodeReplacerVal == PN.getIncomingValue(i) &&
2111 "PHI has two incompatbile incoming values from codeRepl");
2112 }
2113 }
2114
2115 for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
2116 Value *load = Reloads[i];
2117 std::vector<User *> Users(outputs[i]->user_begin(), outputs[i]->user_end());
2118 for (User *U : Users) {
2119 Instruction *inst = cast<Instruction>(U);
2120 if (inst->getParent()->getParent() == oldFunction)
2121 inst->replaceUsesOfWith(outputs[i], load);
2122 }
2123 }
2124
2125 if (FuncRetVal)
2126 FuncRetVal->replaceUsesWithIf(ReplacerCall, [&](Use &U) {
2127 return cast<Instruction>(U.getUser())->getFunction() == oldFunction;
2128 });
2129
2130 // Update the branch weights for the exit block.
2131 if (BFI && ExtractedFuncRetVals.size() > 1)
2132 calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
2133}
2134
2136 const Function &NewFunc,
2137 AssumptionCache *AC) {
2138 for (auto AssumeVH : AC->assumptions()) {
2139 auto *I = dyn_cast_or_null<CallInst>(AssumeVH);
2140 if (!I)
2141 continue;
2142
2143 // There shouldn't be any llvm.assume intrinsics in the new function.
2144 if (I->getFunction() != &OldFunc)
2145 return true;
2146
2147 // There shouldn't be any stale affected values in the assumption cache
2148 // that were previously in the old function, but that have now been moved
2149 // to the new function.
2150 for (auto AffectedValVH : AC->assumptionsFor(I->getOperand(0))) {
2151 auto *AffectedCI = dyn_cast_or_null<CallInst>(AffectedValVH);
2152 if (!AffectedCI)
2153 continue;
2154 if (AffectedCI->getFunction() != &OldFunc)
2155 return true;
2156 auto *AssumedInst = cast<Instruction>(AffectedCI->getOperand(0));
2157 if (AssumedInst->getFunction() != &OldFunc)
2158 return true;
2159 }
2160 }
2161 return false;
2162}
2163
2165 ExcludeArgsFromAggregate.insert(Arg);
2166}
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")
static void eraseDebugIntrinsicsWithNonLocalRefs(Function &F)
Erase debug info intrinsics which refer to values in F but aren't in F.
static SetVector< BasicBlock * > buildExtractionBlockSet(ArrayRef< BasicBlock * > BBs, DominatorTree *DT, bool AllowVarArgs, bool AllowAlloca)
Build a set of blocks to extract if the input blocks are viable.
static void applyFirstDebugLoc(Function *oldFunction, ArrayRef< BasicBlock * > Blocks, Instruction *BranchI)
If the original function has debug info, we have to add a debug location to the new branch instructio...
static bool definedInRegion(const SetVector< BasicBlock * > &Blocks, Value *V)
definedInRegion - Return true if the specified value is defined in the extracted region.
static bool definedInCaller(const SetVector< BasicBlock * > &Blocks, Value *V)
definedInCaller - Return true if the specified value is defined in the function being code extracted,...
static bool isBlockValidForExtraction(const BasicBlock &BB, const SetVector< BasicBlock * > &Result, bool AllowVarArgs, bool AllowAlloca)
Test whether a block is valid for extraction.
static BasicBlock * getCommonExitBlock(const SetVector< BasicBlock * > &Blocks)
static void eraseLifetimeMarkersOnInputs(const SetVector< BasicBlock * > &Blocks, const SetVector< Value * > &SunkAllocas, SetVector< Value * > &LifetimesStart)
Erase lifetime.start markers which reference inputs to the extraction region, and insert the referenc...
static bool isAlignmentPreservedForAddrCast(const Triple &TargetTriple)
isAlignmentPreservedForAddrCast - Return true if the cast operation for specified target preserves or...
static cl::opt< bool > AggregateArgsOpt("aggregate-extracted-args", cl::Hidden, cl::desc("Aggregate arguments to code-extracted functions"))
static void insertLifetimeMarkersSurroundingCall(Module *M, ArrayRef< Value * > LifetimesStart, ArrayRef< Value * > LifetimesEnd, CallInst *TheCall)
Insert lifetime start/end markers surrounding the call to the new function for objects defined in the...
static void fixupDebugInfoPostExtraction(Function &OldFunc, Function &NewFunc, CallInst &TheCall, const SetVector< Value * > &Inputs, ArrayRef< Value * > NewValues)
Fix up the debug info in the old and new functions.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
Hexagon Common GEP
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
Definition IVUsers.cpp:48
Move duplicate certain instructions close to their use
Definition Localizer.cpp:33
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
uint64_t IntrinsicInst * II
#define P(N)
static StringRef getName(Value *V)
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
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.
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.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
A cache of @llvm.assume calls within a function.
@ TombstoneKey
Use as Tombstone key for DenseMap of AttrKind.
Definition Attributes.h:131
@ None
No attributes have been set.
Definition Attributes.h:126
@ EmptyKey
Use as Empty key for DenseMap of AttrKind.
Definition Attributes.h:130
@ EndAttrKinds
Sentinel value useful for loops.
Definition Attributes.h:129
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:474
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
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...
LLVM_ABI BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
bool empty() const
Definition BasicBlock.h:483
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition BasicBlock.h:687
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
Definition BasicBlock.h:171
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
LLVM_ABI const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Analysis providing branch probability information.
static constexpr BranchProbability getUnknown()
static constexpr 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.
Definition InstrTypes.h:512
static LLVM_ABI CastInst * CreatePointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a BitCast, AddrSpaceCast or a PtrToInt cast instruction.
A cache for the CodeExtractor analysis.
ArrayRef< AllocaInst * > getAllocas() const
Get the allocas in the function at the time the analysis was created.
LLVM_ABI CodeExtractorAnalysisCache(Function &F)
LLVM_ABI bool doesBlockContainClobberOfAddr(BasicBlock &BB, AllocaInst *Addr) const
Check whether BB contains an instruction thought to load from, store to, or otherwise clobber the all...
BasicBlock * findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock)
Find or create a block within the outline region for placing hoisted code.
void findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs, const ValueSet &Allocas, bool CollectGlobalInputs=false)
Compute the set of input values and output values for the code.
void findAllocas(const CodeExtractorAnalysisCache &CEAC, ValueSet &SinkCands, ValueSet &HoistCands, BasicBlock *&ExitBlock) const
Find the set of allocas whose life ranges are contained within the outlined region.
virtual Instruction * allocateVar(IRBuilder<>::InsertPoint AllocaIP, Type *VarType, const Twine &Name=Twine(""), AddrSpaceCastInst **CastedAlloc=nullptr)
Allocate an intermediate variable at the specified point.
CodeExtractor(ArrayRef< BasicBlock * > BBs, DominatorTree *DT=nullptr, bool AggregateArgs=false, BlockFrequencyInfo *BFI=nullptr, BranchProbabilityInfo *BPI=nullptr, AssumptionCache *AC=nullptr, bool AllowVarArgs=false, bool AllowAlloca=false, BasicBlock *AllocationBlock=nullptr, ArrayRef< BasicBlock * > DeallocationBlocks={}, std::string Suffix="", bool ArgsInZeroAddressSpace=false, bool VoidReturnWithSingleOutput=true)
Create a code extractor for a sequence of blocks.
Function * extractCodeRegion(const CodeExtractorAnalysisCache &CEAC)
Perform the extraction, returning the new function.
static bool verifyAssumptionCache(const Function &OldFunc, const Function &NewFunc, AssumptionCache *AC)
Verify that assumption cache isn't stale after a region is extracted.
virtual Instruction * deallocateVar(IRBuilder<>::InsertPoint DeallocIP, Value *Var, Type *VarType)
Deallocate a previously-allocated intermediate variable at the specified point.
bool isEligible() const
Test whether this code extractor is eligible.
void excludeArgFromAggregate(Value *Arg)
Exclude a value from aggregate argument passing when extracting a code region, passing it instead as ...
bool isLegalToShrinkwrapLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC, Instruction *AllocaAddr) const
Check if life time marker nodes can be hoisted/sunk into the outline region.
static CondBrInst * Create(Value *Cond, BasicBlock *IfTrue, BasicBlock *IfFalse, InsertPosition InsertBefore=nullptr)
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI void finalizeSubprogram(DISubprogram *SP)
Finalize a specific subprogram - no new variables may be added to this subprogram afterwards.
Definition DIBuilder.cpp:54
LLVM_ABI DISubroutineType * createSubroutineType(DITypeArray ParameterTypes, DINode::DIFlags Flags=DINode::FlagZero, unsigned CC=0)
Create subroutine type.
LLVM_ABI DbgRecord * insertDbgValue(llvm::Value *Val, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, InsertPosition InsertPt)
Insert a new dbg_value record.
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 DITypeArray getOrCreateTypeArray(ArrayRef< Metadata * > Elements)
Get a DITypeArray, create one if required.
LLVM_ABI DbgRecord * insertDeclare(Value *Storage, DILocalVariable *VarInfo, DIExpression *Expr, const DILocation *DL, BasicBlock *InsertAtEnd)
Insert a new dbg_declare record.
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.
DWARF expression.
DIFile * getFile() const
StringRef getName() const
unsigned getLine() const
bool isArtificial() const
unsigned getColumn() const
DILocalScope * getScope() const
Get the local scope for this label.
std::optional< unsigned > getCoroSuspendIdx() const
A scope for locals.
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
DIFile * getFile() const
Subprogram description. Uses SubclassData1.
DISPFlags
Debug info subprogram flags.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
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.
A debug info location.
Definition DebugLoc.h:126
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.
Definition DebugLoc.cpp:89
LLVM_ABI DILocation * getInlinedAt() const
Definition DebugLoc.cpp:58
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:250
iterator begin() const
iterator end() const
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
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.
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition Function.cpp:637
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:168
const BasicBlock & getEntryBlock() const
Definition Function.h:786
Argument * arg_iterator
Definition Function.h:73
DISubprogram * getSubprogram() const
Get the attached subprogram.
void setDoesNotReturn()
Definition Function.h:568
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition Function.h:882
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.
Definition Function.h:328
arg_iterator arg_end()
Definition Function.h:854
const Function & getFunction() const
Definition Function.h:166
iterator begin()
Definition Function.h:830
arg_iterator arg_begin()
Definition Function.h:845
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:353
void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind)
adds the attribute to the list of attributes for the given arg.
Definition Function.cpp:665
Function::iterator insert(Function::iterator Position, BasicBlock *BB)
Insert BB in the basic block list at Position.
Definition Function.h:732
void setEntryCount(uint64_t Count, const DenseSet< GlobalValue::GUID > *Imports=nullptr)
Set the entry count for this function.
bool doesNotReturn() const
Determine if the function cannot return.
Definition Function.h:565
size_t arg_size() const
Definition Function.h:878
Argument * getArg(unsigned i) const
Definition Function.h:863
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition Function.h:229
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).
Definition GlobalValue.h:60
InsertPoint - A saved insertion point.
Definition IRBuilder.h:246
BasicBlock * getBlock() const
Definition IRBuilder.h:261
BasicBlock::iterator getPoint() const
Definition IRBuilder.h:262
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.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1567
Root of the metadata hierarchy.
Definition Metadata.h:64
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
const Triple & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition Module.h:323
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition Module.h:320
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
LLVM_ABI void removeIncomingValueIf(function_ref< bool(unsigned)> Predicate, bool DeletePHIIfEmpty=true)
Remove all incoming values for which the predicate returns true.
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(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
Definition Type.cpp:911
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
A vector that has set insertion semantics.
Definition SetVector.h:57
ArrayRef< value_type > getArrayRef() const
Definition SetVector.h:91
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
size_type count(const_arg_type key) const
Count the number of elements of a given key in the SetVector.
Definition SetVector.h:268
void clear()
Completely clear the SetVector.
Definition SetVector.h:273
bool empty() const
Determine if the SetVector is empty or not.
Definition SetVector.h:100
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
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
Get the contents as an std::string.
Definition StringRef.h:222
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.
Definition Type.cpp:477
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.
Definition Triple.h:48
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:512
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:310
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:309
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:282
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:308
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:306
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
static UncondBrInst * Create(BasicBlock *Target, InsertPosition InsertBefore=nullptr)
op_range operands()
Definition User.h:267
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:25
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
user_iterator user_begin()
Definition Value.h:402
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:394
LLVM_ABI const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
Definition Value.cpp:725
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
iterator_range< user_iterator > users()
Definition Value.h:426
user_iterator user_end()
Definition Value.h:410
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
LLVM_ABI void dump() const
Support for debugging, callable in GDB: V->dump()
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
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
Definition RDFGraph.h:390
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
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.
Definition STLExtras.h:840
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
LLVM_ABI bool stripDebugInfo(Function &F)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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...
Definition STLExtras.h:633
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:65
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the specified block at the specified instruction.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto predecessors(const MachineBasicBlock *BB)
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:368
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.