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