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