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