Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Fri Jul 18 2025 16:24:10 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp