xref: /src/contrib/llvm-project/llvm/lib/Transforms/Utils/CodeExtractor.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1009b1c42SEd Schouten //===- CodeExtractor.cpp - Pull code region into a new function -----------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9009b1c42SEd Schouten // This file implements the interface to tear out a code region, such as an
10009b1c42SEd Schouten // individual loop or a parallel section, into a new function, replacing it with
11009b1c42SEd Schouten // a call to the new function.
12009b1c42SEd Schouten //
13009b1c42SEd Schouten //===----------------------------------------------------------------------===//
14009b1c42SEd Schouten 
1558b69754SDimitry Andric #include "llvm/Transforms/Utils/CodeExtractor.h"
16044eb2f6SDimitry Andric #include "llvm/ADT/ArrayRef.h"
17044eb2f6SDimitry Andric #include "llvm/ADT/DenseMap.h"
184a16efa3SDimitry Andric #include "llvm/ADT/STLExtras.h"
195ca98fd9SDimitry Andric #include "llvm/ADT/SetVector.h"
20044eb2f6SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
21044eb2f6SDimitry Andric #include "llvm/ADT/SmallVector.h"
22e6d15924SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
23b915e9e0SDimitry Andric #include "llvm/Analysis/BlockFrequencyInfo.h"
24b915e9e0SDimitry Andric #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
25b915e9e0SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
26009b1c42SEd Schouten #include "llvm/Analysis/LoopInfo.h"
27044eb2f6SDimitry Andric #include "llvm/IR/Argument.h"
28044eb2f6SDimitry Andric #include "llvm/IR/Attributes.h"
29044eb2f6SDimitry Andric #include "llvm/IR/BasicBlock.h"
30044eb2f6SDimitry Andric #include "llvm/IR/CFG.h"
31044eb2f6SDimitry Andric #include "llvm/IR/Constant.h"
324a16efa3SDimitry Andric #include "llvm/IR/Constants.h"
33cfca06d7SDimitry Andric #include "llvm/IR/DIBuilder.h"
34044eb2f6SDimitry Andric #include "llvm/IR/DataLayout.h"
35ecbca9f5SDimitry Andric #include "llvm/IR/DebugInfo.h"
36cfca06d7SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
374a16efa3SDimitry Andric #include "llvm/IR/DerivedTypes.h"
385ca98fd9SDimitry Andric #include "llvm/IR/Dominators.h"
39044eb2f6SDimitry Andric #include "llvm/IR/Function.h"
40044eb2f6SDimitry Andric #include "llvm/IR/GlobalValue.h"
41cfca06d7SDimitry Andric #include "llvm/IR/InstIterator.h"
42044eb2f6SDimitry Andric #include "llvm/IR/InstrTypes.h"
43044eb2f6SDimitry Andric #include "llvm/IR/Instruction.h"
444a16efa3SDimitry Andric #include "llvm/IR/Instructions.h"
45f382538dSDimitry Andric #include "llvm/IR/IntrinsicInst.h"
464a16efa3SDimitry Andric #include "llvm/IR/Intrinsics.h"
474a16efa3SDimitry Andric #include "llvm/IR/LLVMContext.h"
48b915e9e0SDimitry Andric #include "llvm/IR/MDBuilder.h"
494a16efa3SDimitry Andric #include "llvm/IR/Module.h"
50e6d15924SDimitry Andric #include "llvm/IR/PatternMatch.h"
51044eb2f6SDimitry Andric #include "llvm/IR/Type.h"
52044eb2f6SDimitry Andric #include "llvm/IR/User.h"
53044eb2f6SDimitry Andric #include "llvm/IR/Value.h"
545ca98fd9SDimitry Andric #include "llvm/IR/Verifier.h"
55b915e9e0SDimitry Andric #include "llvm/Support/BlockFrequency.h"
56044eb2f6SDimitry Andric #include "llvm/Support/BranchProbability.h"
57044eb2f6SDimitry Andric #include "llvm/Support/Casting.h"
58009b1c42SEd Schouten #include "llvm/Support/CommandLine.h"
59009b1c42SEd Schouten #include "llvm/Support/Debug.h"
6059850d08SRoman Divacky #include "llvm/Support/ErrorHandling.h"
6159850d08SRoman Divacky #include "llvm/Support/raw_ostream.h"
624a16efa3SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
63044eb2f6SDimitry Andric #include <cassert>
64044eb2f6SDimitry Andric #include <cstdint>
65044eb2f6SDimitry Andric #include <iterator>
66044eb2f6SDimitry Andric #include <map>
67044eb2f6SDimitry Andric #include <utility>
68044eb2f6SDimitry Andric #include <vector>
69044eb2f6SDimitry Andric 
70009b1c42SEd Schouten using namespace llvm;
71e6d15924SDimitry Andric using namespace llvm::PatternMatch;
72eb11fae6SDimitry Andric using ProfileCount = Function::ProfileCount;
73009b1c42SEd Schouten 
745ca98fd9SDimitry Andric #define DEBUG_TYPE "code-extractor"
755ca98fd9SDimitry Andric 
76009b1c42SEd Schouten // Provide a command-line option to aggregate function arguments into a struct
77009b1c42SEd Schouten // for functions produced by the code extractor. This is useful when converting
78009b1c42SEd Schouten // extracted functions to pthread-based code, as only one argument (void*) can
79009b1c42SEd Schouten // be passed in to pthread_create().
80009b1c42SEd Schouten static cl::opt<bool>
81009b1c42SEd Schouten AggregateArgsOpt("aggregate-extracted-args", cl::Hidden,
82009b1c42SEd Schouten                  cl::desc("Aggregate arguments to code-extracted functions"));
83009b1c42SEd Schouten 
84eb11fae6SDimitry Andric /// Test whether a block is valid for extraction.
isBlockValidForExtraction(const BasicBlock & BB,const SetVector<BasicBlock * > & Result,bool AllowVarArgs,bool AllowAlloca)85eb11fae6SDimitry Andric static bool isBlockValidForExtraction(const BasicBlock &BB,
86eb11fae6SDimitry Andric                                       const SetVector<BasicBlock *> &Result,
87eb11fae6SDimitry Andric                                       bool AllowVarArgs, bool AllowAlloca) {
889df3605dSDimitry Andric   // taking the address of a basic block moved to another function is illegal
899df3605dSDimitry Andric   if (BB.hasAddressTaken())
909df3605dSDimitry Andric     return false;
919df3605dSDimitry Andric 
929df3605dSDimitry Andric   // don't hoist code that uses another basicblock address, as it's likely to
939df3605dSDimitry Andric   // lead to unexpected behavior, like cross-function jumps
949df3605dSDimitry Andric   SmallPtrSet<User const *, 16> Visited;
959df3605dSDimitry Andric   SmallVector<User const *, 16> ToVisit;
969df3605dSDimitry Andric 
979df3605dSDimitry Andric   for (Instruction const &Inst : BB)
989df3605dSDimitry Andric     ToVisit.push_back(&Inst);
999df3605dSDimitry Andric 
1009df3605dSDimitry Andric   while (!ToVisit.empty()) {
1019df3605dSDimitry Andric     User const *Curr = ToVisit.pop_back_val();
1029df3605dSDimitry Andric     if (!Visited.insert(Curr).second)
1039df3605dSDimitry Andric       continue;
1049df3605dSDimitry Andric     if (isa<BlockAddress const>(Curr))
1059df3605dSDimitry Andric       return false; // even a reference to self is likely to be not compatible
1069df3605dSDimitry Andric 
1079df3605dSDimitry Andric     if (isa<Instruction>(Curr) && cast<Instruction>(Curr)->getParent() != &BB)
1089df3605dSDimitry Andric       continue;
1099df3605dSDimitry Andric 
1109df3605dSDimitry Andric     for (auto const &U : Curr->operands()) {
1119df3605dSDimitry Andric       if (auto *UU = dyn_cast<User>(U))
1129df3605dSDimitry Andric         ToVisit.push_back(UU);
1139df3605dSDimitry Andric     }
1149df3605dSDimitry Andric   }
115009b1c42SEd Schouten 
116eb11fae6SDimitry Andric   // If explicitly requested, allow vastart and alloca. For invoke instructions
117eb11fae6SDimitry Andric   // verify that extraction is valid.
11858b69754SDimitry Andric   for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
119eb11fae6SDimitry Andric     if (isa<AllocaInst>(I)) {
120eb11fae6SDimitry Andric        if (!AllowAlloca)
12158b69754SDimitry Andric          return false;
122eb11fae6SDimitry Andric        continue;
123eb11fae6SDimitry Andric     }
124eb11fae6SDimitry Andric 
125eb11fae6SDimitry Andric     if (const auto *II = dyn_cast<InvokeInst>(I)) {
126eb11fae6SDimitry Andric       // Unwind destination (either a landingpad, catchswitch, or cleanuppad)
127eb11fae6SDimitry Andric       // must be a part of the subgraph which is being extracted.
128eb11fae6SDimitry Andric       if (auto *UBB = II->getUnwindDest())
129eb11fae6SDimitry Andric         if (!Result.count(UBB))
130eb11fae6SDimitry Andric           return false;
131eb11fae6SDimitry Andric       continue;
132eb11fae6SDimitry Andric     }
133eb11fae6SDimitry Andric 
134eb11fae6SDimitry Andric     // All catch handlers of a catchswitch instruction as well as the unwind
135eb11fae6SDimitry Andric     // destination must be in the subgraph.
136eb11fae6SDimitry Andric     if (const auto *CSI = dyn_cast<CatchSwitchInst>(I)) {
137eb11fae6SDimitry Andric       if (auto *UBB = CSI->getUnwindDest())
138eb11fae6SDimitry Andric         if (!Result.count(UBB))
139eb11fae6SDimitry Andric           return false;
140e3b55780SDimitry Andric       for (const auto *HBB : CSI->handlers())
141eb11fae6SDimitry Andric         if (!Result.count(const_cast<BasicBlock*>(HBB)))
142eb11fae6SDimitry Andric           return false;
143eb11fae6SDimitry Andric       continue;
144eb11fae6SDimitry Andric     }
145eb11fae6SDimitry Andric 
146eb11fae6SDimitry Andric     // Make sure that entire catch handler is within subgraph. It is sufficient
147eb11fae6SDimitry Andric     // to check that catch return's block is in the list.
148eb11fae6SDimitry Andric     if (const auto *CPI = dyn_cast<CatchPadInst>(I)) {
149eb11fae6SDimitry Andric       for (const auto *U : CPI->users())
150eb11fae6SDimitry Andric         if (const auto *CRI = dyn_cast<CatchReturnInst>(U))
151eb11fae6SDimitry Andric           if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
152eb11fae6SDimitry Andric             return false;
153eb11fae6SDimitry Andric       continue;
154eb11fae6SDimitry Andric     }
155eb11fae6SDimitry Andric 
156eb11fae6SDimitry Andric     // And do similar checks for cleanup handler - the entire handler must be
157eb11fae6SDimitry Andric     // in subgraph which is going to be extracted. For cleanup return should
158eb11fae6SDimitry Andric     // additionally check that the unwind destination is also in the subgraph.
159eb11fae6SDimitry Andric     if (const auto *CPI = dyn_cast<CleanupPadInst>(I)) {
160eb11fae6SDimitry Andric       for (const auto *U : CPI->users())
161eb11fae6SDimitry Andric         if (const auto *CRI = dyn_cast<CleanupReturnInst>(U))
162eb11fae6SDimitry Andric           if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
163eb11fae6SDimitry Andric             return false;
164eb11fae6SDimitry Andric       continue;
165eb11fae6SDimitry Andric     }
166eb11fae6SDimitry Andric     if (const auto *CRI = dyn_cast<CleanupReturnInst>(I)) {
167eb11fae6SDimitry Andric       if (auto *UBB = CRI->getUnwindDest())
168eb11fae6SDimitry Andric         if (!Result.count(UBB))
169eb11fae6SDimitry Andric           return false;
170eb11fae6SDimitry Andric       continue;
171eb11fae6SDimitry Andric     }
172eb11fae6SDimitry Andric 
173d8e91e46SDimitry Andric     if (const CallInst *CI = dyn_cast<CallInst>(I)) {
174d8e91e46SDimitry Andric       if (const Function *F = CI->getCalledFunction()) {
175d8e91e46SDimitry Andric         auto IID = F->getIntrinsicID();
176d8e91e46SDimitry Andric         if (IID == Intrinsic::vastart) {
177044eb2f6SDimitry Andric           if (AllowVarArgs)
178044eb2f6SDimitry Andric             continue;
179044eb2f6SDimitry Andric           else
18058b69754SDimitry Andric             return false;
18158b69754SDimitry Andric         }
182d8e91e46SDimitry Andric 
183d8e91e46SDimitry Andric         // Currently, we miscompile outlined copies of eh_typid_for. There are
184d8e91e46SDimitry Andric         // proposals for fixing this in llvm.org/PR39545.
185d8e91e46SDimitry Andric         if (IID == Intrinsic::eh_typeid_for)
186d8e91e46SDimitry Andric           return false;
187d8e91e46SDimitry Andric       }
188d8e91e46SDimitry Andric     }
189044eb2f6SDimitry Andric   }
190009b1c42SEd Schouten 
19158b69754SDimitry Andric   return true;
19258b69754SDimitry Andric }
193009b1c42SEd Schouten 
194eb11fae6SDimitry Andric /// Build a set of blocks to extract if the input blocks are viable.
19512f3ca4cSDimitry Andric static SetVector<BasicBlock *>
buildExtractionBlockSet(ArrayRef<BasicBlock * > BBs,DominatorTree * DT,bool AllowVarArgs,bool AllowAlloca)196044eb2f6SDimitry Andric buildExtractionBlockSet(ArrayRef<BasicBlock *> BBs, DominatorTree *DT,
197eb11fae6SDimitry Andric                         bool AllowVarArgs, bool AllowAlloca) {
19812f3ca4cSDimitry Andric   assert(!BBs.empty() && "The set of blocks to extract must be non-empty");
19958b69754SDimitry Andric   SetVector<BasicBlock *> Result;
20058b69754SDimitry Andric 
20158b69754SDimitry Andric   // Loop over the blocks, adding them to our set-vector, and aborting with an
20258b69754SDimitry Andric   // empty set if we encounter invalid blocks.
20312f3ca4cSDimitry Andric   for (BasicBlock *BB : BBs) {
20412f3ca4cSDimitry Andric     // If this block is dead, don't process it.
20512f3ca4cSDimitry Andric     if (DT && !DT->isReachableFromEntry(BB))
20612f3ca4cSDimitry Andric       continue;
20712f3ca4cSDimitry Andric 
20812f3ca4cSDimitry Andric     if (!Result.insert(BB))
20912f3ca4cSDimitry Andric       llvm_unreachable("Repeated basic blocks in extraction input");
21012f3ca4cSDimitry Andric   }
21158b69754SDimitry Andric 
212e6d15924SDimitry Andric   LLVM_DEBUG(dbgs() << "Region front block: " << Result.front()->getName()
213e6d15924SDimitry Andric                     << '\n');
214e6d15924SDimitry Andric 
215eb11fae6SDimitry Andric   for (auto *BB : Result) {
216eb11fae6SDimitry Andric     if (!isBlockValidForExtraction(*BB, Result, AllowVarArgs, AllowAlloca))
217eb11fae6SDimitry Andric       return {};
218eb11fae6SDimitry Andric 
219eb11fae6SDimitry Andric     // Make sure that the first block is not a landing pad.
220eb11fae6SDimitry Andric     if (BB == Result.front()) {
221eb11fae6SDimitry Andric       if (BB->isEHPad()) {
222eb11fae6SDimitry Andric         LLVM_DEBUG(dbgs() << "The first block cannot be an unwind block\n");
223eb11fae6SDimitry Andric         return {};
224eb11fae6SDimitry Andric       }
225eb11fae6SDimitry Andric       continue;
226eb11fae6SDimitry Andric     }
227eb11fae6SDimitry Andric 
228eb11fae6SDimitry Andric     // All blocks other than the first must not have predecessors outside of
229eb11fae6SDimitry Andric     // the subgraph which is being extracted.
230eb11fae6SDimitry Andric     for (auto *PBB : predecessors(BB))
231eb11fae6SDimitry Andric       if (!Result.count(PBB)) {
232e6d15924SDimitry Andric         LLVM_DEBUG(dbgs() << "No blocks in this region may have entries from "
233e6d15924SDimitry Andric                              "outside the region except for the first block!\n"
234e6d15924SDimitry Andric                           << "Problematic source BB: " << BB->getName() << "\n"
235e6d15924SDimitry Andric                           << "Problematic destination BB: " << PBB->getName()
236e6d15924SDimitry Andric                           << "\n");
237eb11fae6SDimitry Andric         return {};
238eb11fae6SDimitry Andric       }
239eb11fae6SDimitry Andric   }
24058b69754SDimitry Andric 
24158b69754SDimitry Andric   return Result;
24258b69754SDimitry Andric }
24358b69754SDimitry Andric 
CodeExtractor(ArrayRef<BasicBlock * > BBs,DominatorTree * DT,bool AggregateArgs,BlockFrequencyInfo * BFI,BranchProbabilityInfo * BPI,AssumptionCache * AC,bool AllowVarArgs,bool AllowAlloca,BasicBlock * AllocationBlock,std::string Suffix,bool ArgsInZeroAddressSpace)24458b69754SDimitry Andric CodeExtractor::CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT,
245b915e9e0SDimitry Andric                              bool AggregateArgs, BlockFrequencyInfo *BFI,
246e6d15924SDimitry Andric                              BranchProbabilityInfo *BPI, AssumptionCache *AC,
247e6d15924SDimitry Andric                              bool AllowVarArgs, bool AllowAlloca,
248b1c73532SDimitry Andric                              BasicBlock *AllocationBlock, std::string Suffix,
249b1c73532SDimitry Andric                              bool ArgsInZeroAddressSpace)
250b915e9e0SDimitry Andric     : DT(DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
251145449b1SDimitry Andric       BPI(BPI), AC(AC), AllocationBlock(AllocationBlock),
252145449b1SDimitry Andric       AllowVarArgs(AllowVarArgs),
253d8e91e46SDimitry Andric       Blocks(buildExtractionBlockSet(BBs, DT, AllowVarArgs, AllowAlloca)),
254b1c73532SDimitry Andric       Suffix(Suffix), ArgsInZeroAddressSpace(ArgsInZeroAddressSpace) {}
25558b69754SDimitry Andric 
CodeExtractor(DominatorTree & DT,Loop & L,bool AggregateArgs,BlockFrequencyInfo * BFI,BranchProbabilityInfo * BPI,AssumptionCache * AC,std::string Suffix)256b915e9e0SDimitry Andric CodeExtractor::CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs,
257b915e9e0SDimitry Andric                              BlockFrequencyInfo *BFI,
258e6d15924SDimitry Andric                              BranchProbabilityInfo *BPI, AssumptionCache *AC,
259e6d15924SDimitry Andric                              std::string Suffix)
260b915e9e0SDimitry Andric     : DT(&DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
261145449b1SDimitry Andric       BPI(BPI), AC(AC), AllocationBlock(nullptr), AllowVarArgs(false),
262044eb2f6SDimitry Andric       Blocks(buildExtractionBlockSet(L.getBlocks(), &DT,
263eb11fae6SDimitry Andric                                      /* AllowVarArgs */ false,
264d8e91e46SDimitry Andric                                      /* AllowAlloca */ false)),
265d8e91e46SDimitry Andric       Suffix(Suffix) {}
26658b69754SDimitry Andric 
267009b1c42SEd Schouten /// definedInRegion - Return true if the specified value is defined in the
268009b1c42SEd Schouten /// extracted region.
definedInRegion(const SetVector<BasicBlock * > & Blocks,Value * V)26958b69754SDimitry Andric static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) {
270009b1c42SEd Schouten   if (Instruction *I = dyn_cast<Instruction>(V))
27158b69754SDimitry Andric     if (Blocks.count(I->getParent()))
272009b1c42SEd Schouten       return true;
273009b1c42SEd Schouten   return false;
274009b1c42SEd Schouten }
275009b1c42SEd Schouten 
276009b1c42SEd Schouten /// definedInCaller - Return true if the specified value is defined in the
277009b1c42SEd Schouten /// function being code extracted, but not in the region being extracted.
278009b1c42SEd Schouten /// These values must be passed in as live-ins to the function.
definedInCaller(const SetVector<BasicBlock * > & Blocks,Value * V)27958b69754SDimitry Andric static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) {
280009b1c42SEd Schouten   if (isa<Argument>(V)) return true;
281009b1c42SEd Schouten   if (Instruction *I = dyn_cast<Instruction>(V))
28258b69754SDimitry Andric     if (!Blocks.count(I->getParent()))
283009b1c42SEd Schouten       return true;
284009b1c42SEd Schouten   return false;
285009b1c42SEd Schouten }
286009b1c42SEd Schouten 
getCommonExitBlock(const SetVector<BasicBlock * > & Blocks)2877c7aba6eSDimitry Andric static BasicBlock *getCommonExitBlock(const SetVector<BasicBlock *> &Blocks) {
2887c7aba6eSDimitry Andric   BasicBlock *CommonExitBlock = nullptr;
2897c7aba6eSDimitry Andric   auto hasNonCommonExitSucc = [&](BasicBlock *Block) {
2907c7aba6eSDimitry Andric     for (auto *Succ : successors(Block)) {
2917c7aba6eSDimitry Andric       // Internal edges, ok.
2927c7aba6eSDimitry Andric       if (Blocks.count(Succ))
2937c7aba6eSDimitry Andric         continue;
2947c7aba6eSDimitry Andric       if (!CommonExitBlock) {
2957c7aba6eSDimitry Andric         CommonExitBlock = Succ;
2967c7aba6eSDimitry Andric         continue;
2977c7aba6eSDimitry Andric       }
2981d5ae102SDimitry Andric       if (CommonExitBlock != Succ)
2997c7aba6eSDimitry Andric         return true;
3007c7aba6eSDimitry Andric     }
3017c7aba6eSDimitry Andric     return false;
3027c7aba6eSDimitry Andric   };
3037c7aba6eSDimitry Andric 
3047c7aba6eSDimitry Andric   if (any_of(Blocks, hasNonCommonExitSucc))
3057c7aba6eSDimitry Andric     return nullptr;
3067c7aba6eSDimitry Andric 
3077c7aba6eSDimitry Andric   return CommonExitBlock;
3087c7aba6eSDimitry Andric }
3097c7aba6eSDimitry Andric 
CodeExtractorAnalysisCache(Function & F)3101d5ae102SDimitry Andric CodeExtractorAnalysisCache::CodeExtractorAnalysisCache(Function &F) {
3111d5ae102SDimitry Andric   for (BasicBlock &BB : F) {
3121d5ae102SDimitry Andric     for (Instruction &II : BB.instructionsWithoutDebug())
3131d5ae102SDimitry Andric       if (auto *AI = dyn_cast<AllocaInst>(&II))
3141d5ae102SDimitry Andric         Allocas.push_back(AI);
3157c7aba6eSDimitry Andric 
3161d5ae102SDimitry Andric     findSideEffectInfoForBlock(BB);
3171d5ae102SDimitry Andric   }
3181d5ae102SDimitry Andric }
3191d5ae102SDimitry Andric 
findSideEffectInfoForBlock(BasicBlock & BB)3201d5ae102SDimitry Andric void CodeExtractorAnalysisCache::findSideEffectInfoForBlock(BasicBlock &BB) {
3211d5ae102SDimitry Andric   for (Instruction &II : BB.instructionsWithoutDebug()) {
3227c7aba6eSDimitry Andric     unsigned Opcode = II.getOpcode();
3237c7aba6eSDimitry Andric     Value *MemAddr = nullptr;
3247c7aba6eSDimitry Andric     switch (Opcode) {
3257c7aba6eSDimitry Andric     case Instruction::Store:
3267c7aba6eSDimitry Andric     case Instruction::Load: {
3277c7aba6eSDimitry Andric       if (Opcode == Instruction::Store) {
3287c7aba6eSDimitry Andric         StoreInst *SI = cast<StoreInst>(&II);
3297c7aba6eSDimitry Andric         MemAddr = SI->getPointerOperand();
3307c7aba6eSDimitry Andric       } else {
3317c7aba6eSDimitry Andric         LoadInst *LI = cast<LoadInst>(&II);
3327c7aba6eSDimitry Andric         MemAddr = LI->getPointerOperand();
3337c7aba6eSDimitry Andric       }
3347c7aba6eSDimitry Andric       // Global variable can not be aliased with locals.
335344a3780SDimitry Andric       if (isa<Constant>(MemAddr))
3367c7aba6eSDimitry Andric         break;
3377c7aba6eSDimitry Andric       Value *Base = MemAddr->stripInBoundsConstantOffsets();
3381d5ae102SDimitry Andric       if (!isa<AllocaInst>(Base)) {
3391d5ae102SDimitry Andric         SideEffectingBlocks.insert(&BB);
3401d5ae102SDimitry Andric         return;
3411d5ae102SDimitry Andric       }
3421d5ae102SDimitry Andric       BaseMemAddrs[&BB].insert(Base);
3437c7aba6eSDimitry Andric       break;
3447c7aba6eSDimitry Andric     }
3457c7aba6eSDimitry Andric     default: {
3467c7aba6eSDimitry Andric       IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(&II);
3477c7aba6eSDimitry Andric       if (IntrInst) {
348d8e91e46SDimitry Andric         if (IntrInst->isLifetimeStartOrEnd())
3497c7aba6eSDimitry Andric           break;
3501d5ae102SDimitry Andric         SideEffectingBlocks.insert(&BB);
3511d5ae102SDimitry Andric         return;
3527c7aba6eSDimitry Andric       }
3537c7aba6eSDimitry Andric       // Treat all the other cases conservatively if it has side effects.
3541d5ae102SDimitry Andric       if (II.mayHaveSideEffects()) {
3551d5ae102SDimitry Andric         SideEffectingBlocks.insert(&BB);
3561d5ae102SDimitry Andric         return;
3571d5ae102SDimitry Andric       }
3587c7aba6eSDimitry Andric     }
3597c7aba6eSDimitry Andric     }
3607c7aba6eSDimitry Andric   }
3617c7aba6eSDimitry Andric }
3627c7aba6eSDimitry Andric 
doesBlockContainClobberOfAddr(BasicBlock & BB,AllocaInst * Addr) const3631d5ae102SDimitry Andric bool CodeExtractorAnalysisCache::doesBlockContainClobberOfAddr(
3641d5ae102SDimitry Andric     BasicBlock &BB, AllocaInst *Addr) const {
3651d5ae102SDimitry Andric   if (SideEffectingBlocks.count(&BB))
3661d5ae102SDimitry Andric     return true;
3671d5ae102SDimitry Andric   auto It = BaseMemAddrs.find(&BB);
3681d5ae102SDimitry Andric   if (It != BaseMemAddrs.end())
3691d5ae102SDimitry Andric     return It->second.count(Addr);
3701d5ae102SDimitry Andric   return false;
3711d5ae102SDimitry Andric }
3721d5ae102SDimitry Andric 
isLegalToShrinkwrapLifetimeMarkers(const CodeExtractorAnalysisCache & CEAC,Instruction * Addr) const3731d5ae102SDimitry Andric bool CodeExtractor::isLegalToShrinkwrapLifetimeMarkers(
3741d5ae102SDimitry Andric     const CodeExtractorAnalysisCache &CEAC, Instruction *Addr) const {
3751d5ae102SDimitry Andric   AllocaInst *AI = cast<AllocaInst>(Addr->stripInBoundsConstantOffsets());
3761d5ae102SDimitry Andric   Function *Func = (*Blocks.begin())->getParent();
3771d5ae102SDimitry Andric   for (BasicBlock &BB : *Func) {
3781d5ae102SDimitry Andric     if (Blocks.count(&BB))
3791d5ae102SDimitry Andric       continue;
3801d5ae102SDimitry Andric     if (CEAC.doesBlockContainClobberOfAddr(BB, AI))
3811d5ae102SDimitry Andric       return false;
3821d5ae102SDimitry Andric   }
3837c7aba6eSDimitry Andric   return true;
3847c7aba6eSDimitry Andric }
3857c7aba6eSDimitry Andric 
3867c7aba6eSDimitry Andric BasicBlock *
findOrCreateBlockForHoisting(BasicBlock * CommonExitBlock)3877c7aba6eSDimitry Andric CodeExtractor::findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock) {
3887c7aba6eSDimitry Andric   BasicBlock *SinglePredFromOutlineRegion = nullptr;
3897c7aba6eSDimitry Andric   assert(!Blocks.count(CommonExitBlock) &&
3907c7aba6eSDimitry Andric          "Expect a block outside the region!");
3917c7aba6eSDimitry Andric   for (auto *Pred : predecessors(CommonExitBlock)) {
3927c7aba6eSDimitry Andric     if (!Blocks.count(Pred))
3937c7aba6eSDimitry Andric       continue;
3947c7aba6eSDimitry Andric     if (!SinglePredFromOutlineRegion) {
3957c7aba6eSDimitry Andric       SinglePredFromOutlineRegion = Pred;
3967c7aba6eSDimitry Andric     } else if (SinglePredFromOutlineRegion != Pred) {
3977c7aba6eSDimitry Andric       SinglePredFromOutlineRegion = nullptr;
3987c7aba6eSDimitry Andric       break;
3997c7aba6eSDimitry Andric     }
4007c7aba6eSDimitry Andric   }
4017c7aba6eSDimitry Andric 
4027c7aba6eSDimitry Andric   if (SinglePredFromOutlineRegion)
4037c7aba6eSDimitry Andric     return SinglePredFromOutlineRegion;
4047c7aba6eSDimitry Andric 
4057c7aba6eSDimitry Andric #ifndef NDEBUG
4067c7aba6eSDimitry Andric   auto getFirstPHI = [](BasicBlock *BB) {
4077c7aba6eSDimitry Andric     BasicBlock::iterator I = BB->begin();
4087c7aba6eSDimitry Andric     PHINode *FirstPhi = nullptr;
4097c7aba6eSDimitry Andric     while (I != BB->end()) {
4107c7aba6eSDimitry Andric       PHINode *Phi = dyn_cast<PHINode>(I);
4117c7aba6eSDimitry Andric       if (!Phi)
4127c7aba6eSDimitry Andric         break;
4137c7aba6eSDimitry Andric       if (!FirstPhi) {
4147c7aba6eSDimitry Andric         FirstPhi = Phi;
4157c7aba6eSDimitry Andric         break;
4167c7aba6eSDimitry Andric       }
4177c7aba6eSDimitry Andric     }
4187c7aba6eSDimitry Andric     return FirstPhi;
4197c7aba6eSDimitry Andric   };
4207c7aba6eSDimitry Andric   // If there are any phi nodes, the single pred either exists or has already
4217c7aba6eSDimitry Andric   // be created before code extraction.
4227c7aba6eSDimitry Andric   assert(!getFirstPHI(CommonExitBlock) && "Phi not expected");
4237c7aba6eSDimitry Andric #endif
4247c7aba6eSDimitry Andric 
4257c7aba6eSDimitry Andric   BasicBlock *NewExitBlock = CommonExitBlock->splitBasicBlock(
4267c7aba6eSDimitry Andric       CommonExitBlock->getFirstNonPHI()->getIterator());
4277c7aba6eSDimitry Andric 
428344a3780SDimitry Andric   for (BasicBlock *Pred :
429344a3780SDimitry Andric        llvm::make_early_inc_range(predecessors(CommonExitBlock))) {
4307c7aba6eSDimitry Andric     if (Blocks.count(Pred))
4317c7aba6eSDimitry Andric       continue;
4327c7aba6eSDimitry Andric     Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock);
4337c7aba6eSDimitry Andric   }
4347c7aba6eSDimitry Andric   // Now add the old exit block to the outline region.
4357c7aba6eSDimitry Andric   Blocks.insert(CommonExitBlock);
436c0981da4SDimitry Andric   OldTargets.push_back(NewExitBlock);
4377c7aba6eSDimitry Andric   return CommonExitBlock;
4387c7aba6eSDimitry Andric }
4397c7aba6eSDimitry Andric 
440e6d15924SDimitry Andric // Find the pair of life time markers for address 'Addr' that are either
441e6d15924SDimitry Andric // defined inside the outline region or can legally be shrinkwrapped into the
442e6d15924SDimitry Andric // outline region. If there are not other untracked uses of the address, return
443e6d15924SDimitry Andric // the pair of markers if found; otherwise return a pair of nullptr.
444e6d15924SDimitry Andric CodeExtractor::LifetimeMarkerInfo
getLifetimeMarkers(const CodeExtractorAnalysisCache & CEAC,Instruction * Addr,BasicBlock * ExitBlock) const4451d5ae102SDimitry Andric CodeExtractor::getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
4461d5ae102SDimitry Andric                                   Instruction *Addr,
447e6d15924SDimitry Andric                                   BasicBlock *ExitBlock) const {
448e6d15924SDimitry Andric   LifetimeMarkerInfo Info;
449e6d15924SDimitry Andric 
450e6d15924SDimitry Andric   for (User *U : Addr->users()) {
451e6d15924SDimitry Andric     IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(U);
452e6d15924SDimitry Andric     if (IntrInst) {
453cfca06d7SDimitry Andric       // We don't model addresses with multiple start/end markers, but the
454cfca06d7SDimitry Andric       // markers do not need to be in the region.
455e6d15924SDimitry Andric       if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start) {
456e6d15924SDimitry Andric         if (Info.LifeStart)
457e6d15924SDimitry Andric           return {};
458e6d15924SDimitry Andric         Info.LifeStart = IntrInst;
459cfca06d7SDimitry Andric         continue;
460e6d15924SDimitry Andric       }
461e6d15924SDimitry Andric       if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) {
462e6d15924SDimitry Andric         if (Info.LifeEnd)
463e6d15924SDimitry Andric           return {};
464e6d15924SDimitry Andric         Info.LifeEnd = IntrInst;
465cfca06d7SDimitry Andric         continue;
466e6d15924SDimitry Andric       }
467cfca06d7SDimitry Andric       // At this point, permit debug uses outside of the region.
468cfca06d7SDimitry Andric       // This is fixed in a later call to fixupDebugInfoPostExtraction().
469cfca06d7SDimitry Andric       if (isa<DbgInfoIntrinsic>(IntrInst))
470e6d15924SDimitry Andric         continue;
471e6d15924SDimitry Andric     }
472e6d15924SDimitry Andric     // Find untracked uses of the address, bail.
473e6d15924SDimitry Andric     if (!definedInRegion(Blocks, U))
474e6d15924SDimitry Andric       return {};
475e6d15924SDimitry Andric   }
476e6d15924SDimitry Andric 
477e6d15924SDimitry Andric   if (!Info.LifeStart || !Info.LifeEnd)
478e6d15924SDimitry Andric     return {};
479e6d15924SDimitry Andric 
480e6d15924SDimitry Andric   Info.SinkLifeStart = !definedInRegion(Blocks, Info.LifeStart);
481e6d15924SDimitry Andric   Info.HoistLifeEnd = !definedInRegion(Blocks, Info.LifeEnd);
482e6d15924SDimitry Andric   // Do legality check.
483e6d15924SDimitry Andric   if ((Info.SinkLifeStart || Info.HoistLifeEnd) &&
4841d5ae102SDimitry Andric       !isLegalToShrinkwrapLifetimeMarkers(CEAC, Addr))
485e6d15924SDimitry Andric     return {};
486e6d15924SDimitry Andric 
487e6d15924SDimitry Andric   // Check to see if we have a place to do hoisting, if not, bail.
488e6d15924SDimitry Andric   if (Info.HoistLifeEnd && !ExitBlock)
489e6d15924SDimitry Andric     return {};
490e6d15924SDimitry Andric 
491e6d15924SDimitry Andric   return Info;
492e6d15924SDimitry Andric }
493e6d15924SDimitry Andric 
findAllocas(const CodeExtractorAnalysisCache & CEAC,ValueSet & SinkCands,ValueSet & HoistCands,BasicBlock * & ExitBlock) const4941d5ae102SDimitry Andric void CodeExtractor::findAllocas(const CodeExtractorAnalysisCache &CEAC,
4951d5ae102SDimitry Andric                                 ValueSet &SinkCands, ValueSet &HoistCands,
4967c7aba6eSDimitry Andric                                 BasicBlock *&ExitBlock) const {
4977c7aba6eSDimitry Andric   Function *Func = (*Blocks.begin())->getParent();
4987c7aba6eSDimitry Andric   ExitBlock = getCommonExitBlock(Blocks);
4997c7aba6eSDimitry Andric 
500e6d15924SDimitry Andric   auto moveOrIgnoreLifetimeMarkers =
501e6d15924SDimitry Andric       [&](const LifetimeMarkerInfo &LMI) -> bool {
502e6d15924SDimitry Andric     if (!LMI.LifeStart)
503e6d15924SDimitry Andric       return false;
504e6d15924SDimitry Andric     if (LMI.SinkLifeStart) {
505e6d15924SDimitry Andric       LLVM_DEBUG(dbgs() << "Sinking lifetime.start: " << *LMI.LifeStart
506e6d15924SDimitry Andric                         << "\n");
507e6d15924SDimitry Andric       SinkCands.insert(LMI.LifeStart);
508e6d15924SDimitry Andric     }
509e6d15924SDimitry Andric     if (LMI.HoistLifeEnd) {
510e6d15924SDimitry Andric       LLVM_DEBUG(dbgs() << "Hoisting lifetime.end: " << *LMI.LifeEnd << "\n");
511e6d15924SDimitry Andric       HoistCands.insert(LMI.LifeEnd);
512e6d15924SDimitry Andric     }
513e6d15924SDimitry Andric     return true;
514e6d15924SDimitry Andric   };
515e6d15924SDimitry Andric 
5161d5ae102SDimitry Andric   // Look up allocas in the original function in CodeExtractorAnalysisCache, as
5171d5ae102SDimitry Andric   // this is much faster than walking all the instructions.
5181d5ae102SDimitry Andric   for (AllocaInst *AI : CEAC.getAllocas()) {
5191d5ae102SDimitry Andric     BasicBlock *BB = AI->getParent();
5201d5ae102SDimitry Andric     if (Blocks.count(BB))
521f382538dSDimitry Andric       continue;
522f382538dSDimitry Andric 
5231d5ae102SDimitry Andric     // As a prior call to extractCodeRegion() may have shrinkwrapped the alloca,
5241d5ae102SDimitry Andric     // check whether it is actually still in the original function.
5251d5ae102SDimitry Andric     Function *AIFunc = BB->getParent();
5261d5ae102SDimitry Andric     if (AIFunc != Func)
5271d5ae102SDimitry Andric       continue;
5281d5ae102SDimitry Andric 
5291d5ae102SDimitry Andric     LifetimeMarkerInfo MarkerInfo = getLifetimeMarkers(CEAC, AI, ExitBlock);
530e6d15924SDimitry Andric     bool Moved = moveOrIgnoreLifetimeMarkers(MarkerInfo);
531e6d15924SDimitry Andric     if (Moved) {
532e6d15924SDimitry Andric       LLVM_DEBUG(dbgs() << "Sinking alloca: " << *AI << "\n");
533f382538dSDimitry Andric       SinkCands.insert(AI);
534f382538dSDimitry Andric       continue;
535f382538dSDimitry Andric     }
536f382538dSDimitry Andric 
537b60736ecSDimitry Andric     // Find bitcasts in the outlined region that have lifetime marker users
538b60736ecSDimitry Andric     // outside that region. Replace the lifetime marker use with an
539b60736ecSDimitry Andric     // outside region bitcast to avoid unnecessary alloca/reload instructions
540b60736ecSDimitry Andric     // and extra lifetime markers.
541b60736ecSDimitry Andric     SmallVector<Instruction *, 2> LifetimeBitcastUsers;
542b60736ecSDimitry Andric     for (User *U : AI->users()) {
543b60736ecSDimitry Andric       if (!definedInRegion(Blocks, U))
544b60736ecSDimitry Andric         continue;
545b60736ecSDimitry Andric 
546b60736ecSDimitry Andric       if (U->stripInBoundsConstantOffsets() != AI)
547b60736ecSDimitry Andric         continue;
548b60736ecSDimitry Andric 
549b60736ecSDimitry Andric       Instruction *Bitcast = cast<Instruction>(U);
550b60736ecSDimitry Andric       for (User *BU : Bitcast->users()) {
551b60736ecSDimitry Andric         IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(BU);
552b60736ecSDimitry Andric         if (!IntrInst)
553b60736ecSDimitry Andric           continue;
554b60736ecSDimitry Andric 
555b60736ecSDimitry Andric         if (!IntrInst->isLifetimeStartOrEnd())
556b60736ecSDimitry Andric           continue;
557b60736ecSDimitry Andric 
558b60736ecSDimitry Andric         if (definedInRegion(Blocks, IntrInst))
559b60736ecSDimitry Andric           continue;
560b60736ecSDimitry Andric 
561b60736ecSDimitry Andric         LLVM_DEBUG(dbgs() << "Replace use of extracted region bitcast"
562b60736ecSDimitry Andric                           << *Bitcast << " in out-of-region lifetime marker "
563b60736ecSDimitry Andric                           << *IntrInst << "\n");
564b60736ecSDimitry Andric         LifetimeBitcastUsers.push_back(IntrInst);
565b60736ecSDimitry Andric       }
566b60736ecSDimitry Andric     }
567b60736ecSDimitry Andric 
568b60736ecSDimitry Andric     for (Instruction *I : LifetimeBitcastUsers) {
569b60736ecSDimitry Andric       Module *M = AIFunc->getParent();
570b60736ecSDimitry Andric       LLVMContext &Ctx = M->getContext();
571b1c73532SDimitry Andric       auto *Int8PtrTy = PointerType::getUnqual(Ctx);
572b60736ecSDimitry Andric       CastInst *CastI =
573ac9a064cSDimitry Andric           CastInst::CreatePointerCast(AI, Int8PtrTy, "lt.cast", I->getIterator());
574b60736ecSDimitry Andric       I->replaceUsesOfWith(I->getOperand(1), CastI);
575b60736ecSDimitry Andric     }
576b60736ecSDimitry Andric 
577e6d15924SDimitry Andric     // Follow any bitcasts.
578e6d15924SDimitry Andric     SmallVector<Instruction *, 2> Bitcasts;
579e6d15924SDimitry Andric     SmallVector<LifetimeMarkerInfo, 2> BitcastLifetimeInfo;
580f382538dSDimitry Andric     for (User *U : AI->users()) {
5817c7aba6eSDimitry Andric       if (U->stripInBoundsConstantOffsets() == AI) {
582f382538dSDimitry Andric         Instruction *Bitcast = cast<Instruction>(U);
5831d5ae102SDimitry Andric         LifetimeMarkerInfo LMI = getLifetimeMarkers(CEAC, Bitcast, ExitBlock);
584e6d15924SDimitry Andric         if (LMI.LifeStart) {
585e6d15924SDimitry Andric           Bitcasts.push_back(Bitcast);
586e6d15924SDimitry Andric           BitcastLifetimeInfo.push_back(LMI);
587f382538dSDimitry Andric           continue;
588f382538dSDimitry Andric         }
589f382538dSDimitry Andric       }
5907c7aba6eSDimitry Andric 
5917c7aba6eSDimitry Andric       // Found unknown use of AI.
592f382538dSDimitry Andric       if (!definedInRegion(Blocks, U)) {
593e6d15924SDimitry Andric         Bitcasts.clear();
594f382538dSDimitry Andric         break;
595f382538dSDimitry Andric       }
596f382538dSDimitry Andric     }
5977c7aba6eSDimitry Andric 
598e6d15924SDimitry Andric     // Either no bitcasts reference the alloca or there are unknown uses.
599e6d15924SDimitry Andric     if (Bitcasts.empty())
600e6d15924SDimitry Andric       continue;
601e6d15924SDimitry Andric 
602e6d15924SDimitry Andric     LLVM_DEBUG(dbgs() << "Sinking alloca (via bitcast): " << *AI << "\n");
603f382538dSDimitry Andric     SinkCands.insert(AI);
604e6d15924SDimitry Andric     for (unsigned I = 0, E = Bitcasts.size(); I != E; ++I) {
605e6d15924SDimitry Andric       Instruction *BitcastAddr = Bitcasts[I];
606e6d15924SDimitry Andric       const LifetimeMarkerInfo &LMI = BitcastLifetimeInfo[I];
607e6d15924SDimitry Andric       assert(LMI.LifeStart &&
608e6d15924SDimitry Andric              "Unsafe to sink bitcast without lifetime markers");
609e6d15924SDimitry Andric       moveOrIgnoreLifetimeMarkers(LMI);
610e6d15924SDimitry Andric       if (!definedInRegion(Blocks, BitcastAddr)) {
611e6d15924SDimitry Andric         LLVM_DEBUG(dbgs() << "Sinking bitcast-of-alloca: " << *BitcastAddr
612e6d15924SDimitry Andric                           << "\n");
613e6d15924SDimitry Andric         SinkCands.insert(BitcastAddr);
614e6d15924SDimitry Andric       }
615f382538dSDimitry Andric     }
616f382538dSDimitry Andric   }
617f382538dSDimitry Andric }
6181d5ae102SDimitry Andric 
isEligible() const6191d5ae102SDimitry Andric bool CodeExtractor::isEligible() const {
6201d5ae102SDimitry Andric   if (Blocks.empty())
6211d5ae102SDimitry Andric     return false;
6221d5ae102SDimitry Andric   BasicBlock *Header = *Blocks.begin();
6231d5ae102SDimitry Andric   Function *F = Header->getParent();
6241d5ae102SDimitry Andric 
6251d5ae102SDimitry Andric   // For functions with varargs, check that varargs handling is only done in the
6261d5ae102SDimitry Andric   // outlined function, i.e vastart and vaend are only used in outlined blocks.
6271d5ae102SDimitry Andric   if (AllowVarArgs && F->getFunctionType()->isVarArg()) {
6281d5ae102SDimitry Andric     auto containsVarArgIntrinsic = [](const Instruction &I) {
6291d5ae102SDimitry Andric       if (const CallInst *CI = dyn_cast<CallInst>(&I))
6301d5ae102SDimitry Andric         if (const Function *Callee = CI->getCalledFunction())
6311d5ae102SDimitry Andric           return Callee->getIntrinsicID() == Intrinsic::vastart ||
6321d5ae102SDimitry Andric                  Callee->getIntrinsicID() == Intrinsic::vaend;
6331d5ae102SDimitry Andric       return false;
6341d5ae102SDimitry Andric     };
6351d5ae102SDimitry Andric 
6361d5ae102SDimitry Andric     for (auto &BB : *F) {
6371d5ae102SDimitry Andric       if (Blocks.count(&BB))
6381d5ae102SDimitry Andric         continue;
6391d5ae102SDimitry Andric       if (llvm::any_of(BB, containsVarArgIntrinsic))
6401d5ae102SDimitry Andric         return false;
6411d5ae102SDimitry Andric     }
6421d5ae102SDimitry Andric   }
6431d5ae102SDimitry Andric   return true;
644f382538dSDimitry Andric }
645f382538dSDimitry Andric 
findInputsOutputs(ValueSet & Inputs,ValueSet & Outputs,const ValueSet & SinkCands) const646f382538dSDimitry Andric void CodeExtractor::findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
647f382538dSDimitry Andric                                       const ValueSet &SinkCands) const {
64801095a5dSDimitry Andric   for (BasicBlock *BB : Blocks) {
64958b69754SDimitry Andric     // If a used value is defined outside the region, it's an input.  If an
65058b69754SDimitry Andric     // instruction is used outside the region, it's an output.
65101095a5dSDimitry Andric     for (Instruction &II : *BB) {
6521d5ae102SDimitry Andric       for (auto &OI : II.operands()) {
6531d5ae102SDimitry Andric         Value *V = OI;
654f382538dSDimitry Andric         if (!SinkCands.count(V) && definedInCaller(Blocks, V))
655f382538dSDimitry Andric           Inputs.insert(V);
656f382538dSDimitry Andric       }
657009b1c42SEd Schouten 
65801095a5dSDimitry Andric       for (User *U : II.users())
6595ca98fd9SDimitry Andric         if (!definedInRegion(Blocks, U)) {
66001095a5dSDimitry Andric           Outputs.insert(&II);
66158b69754SDimitry Andric           break;
66258b69754SDimitry Andric         }
66358b69754SDimitry Andric     }
66458b69754SDimitry Andric   }
665009b1c42SEd Schouten }
666009b1c42SEd Schouten 
667d8e91e46SDimitry Andric /// severSplitPHINodesOfEntry - If a PHI node has multiple inputs from outside
668d8e91e46SDimitry Andric /// of the region, we need to split the entry block of the region so that the
669d8e91e46SDimitry Andric /// PHI node is easier to deal with.
severSplitPHINodesOfEntry(BasicBlock * & Header)670d8e91e46SDimitry Andric void CodeExtractor::severSplitPHINodesOfEntry(BasicBlock *&Header) {
6716b943ff3SDimitry Andric   unsigned NumPredsFromRegion = 0;
672009b1c42SEd Schouten   unsigned NumPredsOutsideRegion = 0;
673009b1c42SEd Schouten 
674009b1c42SEd Schouten   if (Header != &Header->getParent()->getEntryBlock()) {
675009b1c42SEd Schouten     PHINode *PN = dyn_cast<PHINode>(Header->begin());
676009b1c42SEd Schouten     if (!PN) return;  // No PHI nodes.
677009b1c42SEd Schouten 
678009b1c42SEd Schouten     // If the header node contains any PHI nodes, check to see if there is more
679009b1c42SEd Schouten     // than one entry from outside the region.  If so, we need to sever the
680009b1c42SEd Schouten     // header block into two.
681009b1c42SEd Schouten     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
68258b69754SDimitry Andric       if (Blocks.count(PN->getIncomingBlock(i)))
6836b943ff3SDimitry Andric         ++NumPredsFromRegion;
684009b1c42SEd Schouten       else
685009b1c42SEd Schouten         ++NumPredsOutsideRegion;
686009b1c42SEd Schouten 
687009b1c42SEd Schouten     // If there is one (or fewer) predecessor from outside the region, we don't
688009b1c42SEd Schouten     // need to do anything special.
689009b1c42SEd Schouten     if (NumPredsOutsideRegion <= 1) return;
690009b1c42SEd Schouten   }
691009b1c42SEd Schouten 
692009b1c42SEd Schouten   // Otherwise, we need to split the header block into two pieces: one
693009b1c42SEd Schouten   // containing PHI nodes merging values from outside of the region, and a
694009b1c42SEd Schouten   // second that contains all of the code for the block and merges back any
695009b1c42SEd Schouten   // incoming values from inside of the region.
696044eb2f6SDimitry Andric   BasicBlock *NewBB = SplitBlock(Header, Header->getFirstNonPHI(), DT);
697009b1c42SEd Schouten 
698009b1c42SEd Schouten   // We only want to code extract the second block now, and it becomes the new
699009b1c42SEd Schouten   // header of the region.
700009b1c42SEd Schouten   BasicBlock *OldPred = Header;
70158b69754SDimitry Andric   Blocks.remove(OldPred);
70258b69754SDimitry Andric   Blocks.insert(NewBB);
703009b1c42SEd Schouten   Header = NewBB;
704009b1c42SEd Schouten 
705009b1c42SEd Schouten   // Okay, now we need to adjust the PHI nodes and any branches from within the
706009b1c42SEd Schouten   // region to go to the new header block instead of the old header block.
7076b943ff3SDimitry Andric   if (NumPredsFromRegion) {
708009b1c42SEd Schouten     PHINode *PN = cast<PHINode>(OldPred->begin());
709009b1c42SEd Schouten     // Loop over all of the predecessors of OldPred that are in the region,
710009b1c42SEd Schouten     // changing them to branch to NewBB instead.
711009b1c42SEd Schouten     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
71258b69754SDimitry Andric       if (Blocks.count(PN->getIncomingBlock(i))) {
713d8e91e46SDimitry Andric         Instruction *TI = PN->getIncomingBlock(i)->getTerminator();
714009b1c42SEd Schouten         TI->replaceUsesOfWith(OldPred, NewBB);
715009b1c42SEd Schouten       }
716009b1c42SEd Schouten 
7176b943ff3SDimitry Andric     // Okay, everything within the region is now branching to the right block, we
718009b1c42SEd Schouten     // just have to update the PHI nodes now, inserting PHI nodes into NewBB.
71912f3ca4cSDimitry Andric     BasicBlock::iterator AfterPHIs;
720009b1c42SEd Schouten     for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
721009b1c42SEd Schouten       PHINode *PN = cast<PHINode>(AfterPHIs);
722009b1c42SEd Schouten       // Create a new PHI node in the new region, which has an incoming value
723009b1c42SEd Schouten       // from OldPred of PN.
7246b943ff3SDimitry Andric       PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion,
725b1c73532SDimitry Andric                                        PN->getName() + ".ce");
726b1c73532SDimitry Andric       NewPN->insertBefore(NewBB->begin());
72712f3ca4cSDimitry Andric       PN->replaceAllUsesWith(NewPN);
728009b1c42SEd Schouten       NewPN->addIncoming(PN, OldPred);
729009b1c42SEd Schouten 
730009b1c42SEd Schouten       // Loop over all of the incoming value in PN, moving them to NewPN if they
731009b1c42SEd Schouten       // are from the extracted region.
732009b1c42SEd Schouten       for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
73358b69754SDimitry Andric         if (Blocks.count(PN->getIncomingBlock(i))) {
734009b1c42SEd Schouten           NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i));
735009b1c42SEd Schouten           PN->removeIncomingValue(i);
736009b1c42SEd Schouten           --i;
737009b1c42SEd Schouten         }
738009b1c42SEd Schouten       }
739009b1c42SEd Schouten     }
740009b1c42SEd Schouten   }
741009b1c42SEd Schouten }
742009b1c42SEd Schouten 
743d8e91e46SDimitry Andric /// severSplitPHINodesOfExits - if PHI nodes in exit blocks have inputs from
744d8e91e46SDimitry Andric /// outlined region, we split these PHIs on two: one with inputs from region
745d8e91e46SDimitry Andric /// and other with remaining incoming blocks; then first PHIs are placed in
746d8e91e46SDimitry Andric /// outlined region.
severSplitPHINodesOfExits(const SetVector<BasicBlock * > & Exits)747d8e91e46SDimitry Andric void CodeExtractor::severSplitPHINodesOfExits(
748ac9a064cSDimitry Andric     const SetVector<BasicBlock *> &Exits) {
749d8e91e46SDimitry Andric   for (BasicBlock *ExitBB : Exits) {
750d8e91e46SDimitry Andric     BasicBlock *NewBB = nullptr;
751d8e91e46SDimitry Andric 
752d8e91e46SDimitry Andric     for (PHINode &PN : ExitBB->phis()) {
753d8e91e46SDimitry Andric       // Find all incoming values from the outlining region.
754d8e91e46SDimitry Andric       SmallVector<unsigned, 2> IncomingVals;
755d8e91e46SDimitry Andric       for (unsigned i = 0; i < PN.getNumIncomingValues(); ++i)
756d8e91e46SDimitry Andric         if (Blocks.count(PN.getIncomingBlock(i)))
757d8e91e46SDimitry Andric           IncomingVals.push_back(i);
758d8e91e46SDimitry Andric 
759d8e91e46SDimitry Andric       // Do not process PHI if there is one (or fewer) predecessor from region.
760d8e91e46SDimitry Andric       // If PHI has exactly one predecessor from region, only this one incoming
761d8e91e46SDimitry Andric       // will be replaced on codeRepl block, so it should be safe to skip PHI.
762d8e91e46SDimitry Andric       if (IncomingVals.size() <= 1)
763d8e91e46SDimitry Andric         continue;
764d8e91e46SDimitry Andric 
765d8e91e46SDimitry Andric       // Create block for new PHIs and add it to the list of outlined if it
766d8e91e46SDimitry Andric       // wasn't done before.
767d8e91e46SDimitry Andric       if (!NewBB) {
768d8e91e46SDimitry Andric         NewBB = BasicBlock::Create(ExitBB->getContext(),
769d8e91e46SDimitry Andric                                    ExitBB->getName() + ".split",
770d8e91e46SDimitry Andric                                    ExitBB->getParent(), ExitBB);
771b1c73532SDimitry Andric         NewBB->IsNewDbgInfoFormat = ExitBB->IsNewDbgInfoFormat;
772b60736ecSDimitry Andric         SmallVector<BasicBlock *, 4> Preds(predecessors(ExitBB));
773d8e91e46SDimitry Andric         for (BasicBlock *PredBB : Preds)
774d8e91e46SDimitry Andric           if (Blocks.count(PredBB))
775d8e91e46SDimitry Andric             PredBB->getTerminator()->replaceUsesOfWith(ExitBB, NewBB);
776d8e91e46SDimitry Andric         BranchInst::Create(ExitBB, NewBB);
777d8e91e46SDimitry Andric         Blocks.insert(NewBB);
778d8e91e46SDimitry Andric       }
779d8e91e46SDimitry Andric 
780d8e91e46SDimitry Andric       // Split this PHI.
781b1c73532SDimitry Andric       PHINode *NewPN = PHINode::Create(PN.getType(), IncomingVals.size(),
782b1c73532SDimitry Andric                                        PN.getName() + ".ce");
783b1c73532SDimitry Andric       NewPN->insertBefore(NewBB->getFirstNonPHIIt());
784d8e91e46SDimitry Andric       for (unsigned i : IncomingVals)
785d8e91e46SDimitry Andric         NewPN->addIncoming(PN.getIncomingValue(i), PN.getIncomingBlock(i));
786d8e91e46SDimitry Andric       for (unsigned i : reverse(IncomingVals))
787d8e91e46SDimitry Andric         PN.removeIncomingValue(i, false);
788d8e91e46SDimitry Andric       PN.addIncoming(NewPN, NewBB);
789d8e91e46SDimitry Andric     }
790d8e91e46SDimitry Andric   }
791d8e91e46SDimitry Andric }
792d8e91e46SDimitry Andric 
splitReturnBlocks()793009b1c42SEd Schouten void CodeExtractor::splitReturnBlocks() {
79401095a5dSDimitry Andric   for (BasicBlock *Block : Blocks)
79501095a5dSDimitry Andric     if (ReturnInst *RI = dyn_cast<ReturnInst>(Block->getTerminator())) {
796dd58ef01SDimitry Andric       BasicBlock *New =
79701095a5dSDimitry Andric           Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret");
79859850d08SRoman Divacky       if (DT) {
799cf099d11SDimitry Andric         // Old dominates New. New node dominates all other nodes dominated
80059850d08SRoman Divacky         // by Old.
80101095a5dSDimitry Andric         DomTreeNode *OldNode = DT->getNode(Block);
80201095a5dSDimitry Andric         SmallVector<DomTreeNode *, 8> Children(OldNode->begin(),
80301095a5dSDimitry Andric                                                OldNode->end());
80459850d08SRoman Divacky 
80501095a5dSDimitry Andric         DomTreeNode *NewNode = DT->addNewBlock(New, Block);
80659850d08SRoman Divacky 
80701095a5dSDimitry Andric         for (DomTreeNode *I : Children)
80801095a5dSDimitry Andric           DT->changeImmediateDominator(I, NewNode);
80959850d08SRoman Divacky       }
81059850d08SRoman Divacky     }
811009b1c42SEd Schouten }
812009b1c42SEd Schouten 
813009b1c42SEd Schouten /// constructFunction - make a function based on inputs and outputs, as follows:
814009b1c42SEd Schouten /// f(in0, ..., inN, out0, ..., outN)
constructFunction(const ValueSet & inputs,const ValueSet & outputs,BasicBlock * header,BasicBlock * newRootNode,BasicBlock * newHeader,Function * oldFunction,Module * M)81558b69754SDimitry Andric Function *CodeExtractor::constructFunction(const ValueSet &inputs,
81658b69754SDimitry Andric                                            const ValueSet &outputs,
817009b1c42SEd Schouten                                            BasicBlock *header,
818009b1c42SEd Schouten                                            BasicBlock *newRootNode,
819009b1c42SEd Schouten                                            BasicBlock *newHeader,
820009b1c42SEd Schouten                                            Function *oldFunction,
821009b1c42SEd Schouten                                            Module *M) {
822eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
823eb11fae6SDimitry Andric   LLVM_DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
824009b1c42SEd Schouten 
825009b1c42SEd Schouten   // This function returns unsigned, outputs will go back by reference.
826009b1c42SEd Schouten   switch (NumExitBlocks) {
827009b1c42SEd Schouten   case 0:
82859850d08SRoman Divacky   case 1: RetTy = Type::getVoidTy(header->getContext()); break;
82959850d08SRoman Divacky   case 2: RetTy = Type::getInt1Ty(header->getContext()); break;
83059850d08SRoman Divacky   default: RetTy = Type::getInt16Ty(header->getContext()); break;
831009b1c42SEd Schouten   }
832009b1c42SEd Schouten 
8336f8fc217SDimitry Andric   std::vector<Type *> ParamTy;
8346f8fc217SDimitry Andric   std::vector<Type *> AggParamTy;
8356f8fc217SDimitry Andric   ValueSet StructValues;
836e3b55780SDimitry Andric   const DataLayout &DL = M->getDataLayout();
837009b1c42SEd Schouten 
838009b1c42SEd Schouten   // Add the types of the input values to the function's argument list
83901095a5dSDimitry Andric   for (Value *value : inputs) {
840eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "value used in func: " << *value << "\n");
8416f8fc217SDimitry Andric     if (AggregateArgs && !ExcludeArgsFromAggregate.contains(value)) {
8426f8fc217SDimitry Andric       AggParamTy.push_back(value->getType());
8436f8fc217SDimitry Andric       StructValues.insert(value);
8446f8fc217SDimitry Andric     } else
8456f8fc217SDimitry Andric       ParamTy.push_back(value->getType());
846009b1c42SEd Schouten   }
847009b1c42SEd Schouten 
848009b1c42SEd Schouten   // Add the types of the output values to the function's argument list.
84901095a5dSDimitry Andric   for (Value *output : outputs) {
850eb11fae6SDimitry Andric     LLVM_DEBUG(dbgs() << "instr used in func: " << *output << "\n");
8516f8fc217SDimitry Andric     if (AggregateArgs && !ExcludeArgsFromAggregate.contains(output)) {
8526f8fc217SDimitry Andric       AggParamTy.push_back(output->getType());
8536f8fc217SDimitry Andric       StructValues.insert(output);
8546f8fc217SDimitry Andric     } else
855e3b55780SDimitry Andric       ParamTy.push_back(
856e3b55780SDimitry Andric           PointerType::get(output->getType(), DL.getAllocaAddrSpace()));
8576f8fc217SDimitry Andric   }
8586f8fc217SDimitry Andric 
8596f8fc217SDimitry Andric   assert(
8606f8fc217SDimitry Andric       (ParamTy.size() + AggParamTy.size()) ==
8616f8fc217SDimitry Andric           (inputs.size() + outputs.size()) &&
8626f8fc217SDimitry Andric       "Number of scalar and aggregate params does not match inputs, outputs");
863ecbca9f5SDimitry Andric   assert((StructValues.empty() || AggregateArgs) &&
864ecbca9f5SDimitry Andric          "Expeced StructValues only with AggregateArgs set");
8656f8fc217SDimitry Andric 
8666f8fc217SDimitry Andric   // Concatenate scalar and aggregate params in ParamTy.
8676f8fc217SDimitry Andric   size_t NumScalarParams = ParamTy.size();
8686f8fc217SDimitry Andric   StructType *StructTy = nullptr;
8696f8fc217SDimitry Andric   if (AggregateArgs && !AggParamTy.empty()) {
8706f8fc217SDimitry Andric     StructTy = StructType::get(M->getContext(), AggParamTy);
871b1c73532SDimitry Andric     ParamTy.push_back(PointerType::get(
872b1c73532SDimitry Andric         StructTy, ArgsInZeroAddressSpace ? 0 : DL.getAllocaAddrSpace()));
873009b1c42SEd Schouten   }
874009b1c42SEd Schouten 
875eb11fae6SDimitry Andric   LLVM_DEBUG({
87601095a5dSDimitry Andric     dbgs() << "Function type: " << *RetTy << " f(";
8776f8fc217SDimitry Andric     for (Type *i : ParamTy)
87801095a5dSDimitry Andric       dbgs() << *i << ", ";
87901095a5dSDimitry Andric     dbgs() << ")\n";
88001095a5dSDimitry Andric   });
881009b1c42SEd Schouten 
8826f8fc217SDimitry Andric   FunctionType *funcType = FunctionType::get(
8836f8fc217SDimitry Andric       RetTy, ParamTy, AllowVarArgs && oldFunction->isVarArg());
884009b1c42SEd Schouten 
885d8e91e46SDimitry Andric   std::string SuffixToUse =
886d8e91e46SDimitry Andric       Suffix.empty()
887d8e91e46SDimitry Andric           ? (header->getName().empty() ? "extracted" : header->getName().str())
888d8e91e46SDimitry Andric           : Suffix;
889009b1c42SEd Schouten   // Create the new function
890d8e91e46SDimitry Andric   Function *newFunction = Function::Create(
891d8e91e46SDimitry Andric       funcType, GlobalValue::InternalLinkage, oldFunction->getAddressSpace(),
892d8e91e46SDimitry Andric       oldFunction->getName() + "." + SuffixToUse, M);
893b1c73532SDimitry Andric   newFunction->IsNewDbgInfoFormat = oldFunction->IsNewDbgInfoFormat;
894b915e9e0SDimitry Andric 
895eb11fae6SDimitry Andric   // Inherit all of the target dependent attributes and white-listed
896eb11fae6SDimitry Andric   // target independent attributes.
897b915e9e0SDimitry Andric   //  (e.g. If the extracted region contains a call to an x86.sse
898b915e9e0SDimitry Andric   //  instruction we need to make sure that the extracted region has the
899b915e9e0SDimitry Andric   //  "target-features" attribute allowing it to be lowered.
900b915e9e0SDimitry Andric   // FIXME: This should be changed to check to see if a specific
901b915e9e0SDimitry Andric   //           attribute can not be inherited.
902c0981da4SDimitry Andric   for (const auto &Attr : oldFunction->getAttributes().getFnAttrs()) {
903eb11fae6SDimitry Andric     if (Attr.isStringAttribute()) {
904eb11fae6SDimitry Andric       if (Attr.getKindAsString() == "thunk")
905eb11fae6SDimitry Andric         continue;
906eb11fae6SDimitry Andric     } else
907eb11fae6SDimitry Andric       switch (Attr.getKindAsEnum()) {
908eb11fae6SDimitry Andric       // Those attributes cannot be propagated safely. Explicitly list them
9096f8fc217SDimitry Andric       // here so we get a warning if new attributes are added.
910eb11fae6SDimitry Andric       case Attribute::AllocSize:
911eb11fae6SDimitry Andric       case Attribute::Builtin:
912eb11fae6SDimitry Andric       case Attribute::Convergent:
913eb11fae6SDimitry Andric       case Attribute::JumpTable:
914eb11fae6SDimitry Andric       case Attribute::Naked:
915eb11fae6SDimitry Andric       case Attribute::NoBuiltin:
916cfca06d7SDimitry Andric       case Attribute::NoMerge:
917eb11fae6SDimitry Andric       case Attribute::NoReturn:
918e6d15924SDimitry Andric       case Attribute::NoSync:
919eb11fae6SDimitry Andric       case Attribute::ReturnsTwice:
920eb11fae6SDimitry Andric       case Attribute::Speculatable:
921eb11fae6SDimitry Andric       case Attribute::StackAlignment:
922e6d15924SDimitry Andric       case Attribute::WillReturn:
923145449b1SDimitry Andric       case Attribute::AllocKind:
924145449b1SDimitry Andric       case Attribute::PresplitCoroutine:
925e3b55780SDimitry Andric       case Attribute::Memory:
9267fa27ce4SDimitry Andric       case Attribute::NoFPClass:
927b1c73532SDimitry Andric       case Attribute::CoroDestroyOnlyWhenComplete:
928eb11fae6SDimitry Andric         continue;
929eb11fae6SDimitry Andric       // Those attributes should be safe to propagate to the extracted function.
930eb11fae6SDimitry Andric       case Attribute::AlwaysInline:
931eb11fae6SDimitry Andric       case Attribute::Cold:
932c0981da4SDimitry Andric       case Attribute::DisableSanitizerInstrumentation:
9331f917f69SDimitry Andric       case Attribute::FnRetThunkExtern:
934b60736ecSDimitry Andric       case Attribute::Hot:
935ac9a064cSDimitry Andric       case Attribute::HybridPatchable:
936eb11fae6SDimitry Andric       case Attribute::NoRecurse:
937eb11fae6SDimitry Andric       case Attribute::InlineHint:
938eb11fae6SDimitry Andric       case Attribute::MinSize:
939b60736ecSDimitry Andric       case Attribute::NoCallback:
940eb11fae6SDimitry Andric       case Attribute::NoDuplicate:
941e6d15924SDimitry Andric       case Attribute::NoFree:
942eb11fae6SDimitry Andric       case Attribute::NoImplicitFloat:
943eb11fae6SDimitry Andric       case Attribute::NoInline:
944eb11fae6SDimitry Andric       case Attribute::NonLazyBind:
945eb11fae6SDimitry Andric       case Attribute::NoRedZone:
946eb11fae6SDimitry Andric       case Attribute::NoUnwind:
947145449b1SDimitry Andric       case Attribute::NoSanitizeBounds:
948344a3780SDimitry Andric       case Attribute::NoSanitizeCoverage:
949cfca06d7SDimitry Andric       case Attribute::NullPointerIsValid:
950b1c73532SDimitry Andric       case Attribute::OptimizeForDebugging:
951eb11fae6SDimitry Andric       case Attribute::OptForFuzzing:
952eb11fae6SDimitry Andric       case Attribute::OptimizeNone:
953eb11fae6SDimitry Andric       case Attribute::OptimizeForSize:
954eb11fae6SDimitry Andric       case Attribute::SafeStack:
955eb11fae6SDimitry Andric       case Attribute::ShadowCallStack:
956eb11fae6SDimitry Andric       case Attribute::SanitizeAddress:
957eb11fae6SDimitry Andric       case Attribute::SanitizeMemory:
958ac9a064cSDimitry Andric       case Attribute::SanitizeNumericalStability:
959eb11fae6SDimitry Andric       case Attribute::SanitizeThread:
960eb11fae6SDimitry Andric       case Attribute::SanitizeHWAddress:
961e6d15924SDimitry Andric       case Attribute::SanitizeMemTag:
962d8e91e46SDimitry Andric       case Attribute::SpeculativeLoadHardening:
963eb11fae6SDimitry Andric       case Attribute::StackProtect:
964eb11fae6SDimitry Andric       case Attribute::StackProtectReq:
965eb11fae6SDimitry Andric       case Attribute::StackProtectStrong:
966eb11fae6SDimitry Andric       case Attribute::StrictFP:
967eb11fae6SDimitry Andric       case Attribute::UWTable:
968344a3780SDimitry Andric       case Attribute::VScaleRange:
969eb11fae6SDimitry Andric       case Attribute::NoCfCheck:
970b60736ecSDimitry Andric       case Attribute::MustProgress:
971b60736ecSDimitry Andric       case Attribute::NoProfile:
972e3b55780SDimitry Andric       case Attribute::SkipProfile:
973eb11fae6SDimitry Andric         break;
9746f8fc217SDimitry Andric       // These attributes cannot be applied to functions.
9756f8fc217SDimitry Andric       case Attribute::Alignment:
976145449b1SDimitry Andric       case Attribute::AllocatedPointer:
977145449b1SDimitry Andric       case Attribute::AllocAlign:
9786f8fc217SDimitry Andric       case Attribute::ByVal:
9796f8fc217SDimitry Andric       case Attribute::Dereferenceable:
9806f8fc217SDimitry Andric       case Attribute::DereferenceableOrNull:
9816f8fc217SDimitry Andric       case Attribute::ElementType:
9826f8fc217SDimitry Andric       case Attribute::InAlloca:
9836f8fc217SDimitry Andric       case Attribute::InReg:
9846f8fc217SDimitry Andric       case Attribute::Nest:
9856f8fc217SDimitry Andric       case Attribute::NoAlias:
9866f8fc217SDimitry Andric       case Attribute::NoCapture:
9876f8fc217SDimitry Andric       case Attribute::NoUndef:
9886f8fc217SDimitry Andric       case Attribute::NonNull:
9896f8fc217SDimitry Andric       case Attribute::Preallocated:
990e3b55780SDimitry Andric       case Attribute::ReadNone:
991e3b55780SDimitry Andric       case Attribute::ReadOnly:
9926f8fc217SDimitry Andric       case Attribute::Returned:
9936f8fc217SDimitry Andric       case Attribute::SExt:
9946f8fc217SDimitry Andric       case Attribute::StructRet:
9956f8fc217SDimitry Andric       case Attribute::SwiftError:
9966f8fc217SDimitry Andric       case Attribute::SwiftSelf:
9976f8fc217SDimitry Andric       case Attribute::SwiftAsync:
9986f8fc217SDimitry Andric       case Attribute::ZExt:
9996f8fc217SDimitry Andric       case Attribute::ImmArg:
10006f8fc217SDimitry Andric       case Attribute::ByRef:
1001e3b55780SDimitry Andric       case Attribute::WriteOnly:
1002b1c73532SDimitry Andric       case Attribute::Writable:
1003312c0ed1SDimitry Andric       case Attribute::DeadOnUnwind:
1004ac9a064cSDimitry Andric       case Attribute::Range:
1005ac9a064cSDimitry Andric       case Attribute::Initializes:
10066f8fc217SDimitry Andric       //  These are not really attributes.
10076f8fc217SDimitry Andric       case Attribute::None:
10086f8fc217SDimitry Andric       case Attribute::EndAttrKinds:
10096f8fc217SDimitry Andric       case Attribute::EmptyKey:
10106f8fc217SDimitry Andric       case Attribute::TombstoneKey:
10116f8fc217SDimitry Andric         llvm_unreachable("Not a function attribute");
1012eb11fae6SDimitry Andric       }
1013b915e9e0SDimitry Andric 
1014eb11fae6SDimitry Andric     newFunction->addFnAttr(Attr);
1015eb11fae6SDimitry Andric   }
1016ac9a064cSDimitry Andric 
1017ac9a064cSDimitry Andric   if (NumExitBlocks == 0) {
1018ac9a064cSDimitry Andric     // Mark the new function `noreturn` if applicable. Terminators which resume
1019ac9a064cSDimitry Andric     // exception propagation are treated as returning instructions. This is to
1020ac9a064cSDimitry Andric     // avoid inserting traps after calls to outlined functions which unwind.
1021ac9a064cSDimitry Andric     if (none_of(Blocks, [](const BasicBlock *BB) {
1022ac9a064cSDimitry Andric           const Instruction *Term = BB->getTerminator();
1023ac9a064cSDimitry Andric           return isa<ReturnInst>(Term) || isa<ResumeInst>(Term);
1024ac9a064cSDimitry Andric         }))
1025ac9a064cSDimitry Andric       newFunction->setDoesNotReturn();
1026ac9a064cSDimitry Andric   }
1027ac9a064cSDimitry Andric 
1028e3b55780SDimitry Andric   newFunction->insert(newFunction->end(), newRootNode);
1029009b1c42SEd Schouten 
10306f8fc217SDimitry Andric   // Create scalar and aggregate iterators to name all of the arguments we
10316f8fc217SDimitry Andric   // inserted.
10326f8fc217SDimitry Andric   Function::arg_iterator ScalarAI = newFunction->arg_begin();
10336f8fc217SDimitry Andric   Function::arg_iterator AggAI = std::next(ScalarAI, NumScalarParams);
1034009b1c42SEd Schouten 
1035009b1c42SEd Schouten   // Rewrite all users of the inputs in the extracted region to use the
1036009b1c42SEd Schouten   // arguments (or appropriate addressing into struct) instead.
10376f8fc217SDimitry Andric   for (unsigned i = 0, e = inputs.size(), aggIdx = 0; i != e; ++i) {
1038009b1c42SEd Schouten     Value *RewriteVal;
10396f8fc217SDimitry Andric     if (AggregateArgs && StructValues.contains(inputs[i])) {
1040009b1c42SEd Schouten       Value *Idx[2];
104159850d08SRoman Divacky       Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext()));
10426f8fc217SDimitry Andric       Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), aggIdx);
1043ac9a064cSDimitry Andric       BasicBlock::iterator TI = newFunction->begin()->getTerminator()->getIterator();
10445a5ac124SDimitry Andric       GetElementPtrInst *GEP = GetElementPtrInst::Create(
10456f8fc217SDimitry Andric           StructTy, &*AggAI, Idx, "gep_" + inputs[i]->getName(), TI);
10466f8fc217SDimitry Andric       RewriteVal = new LoadInst(StructTy->getElementType(aggIdx), GEP,
1047e6d15924SDimitry Andric                                 "loadgep_" + inputs[i]->getName(), TI);
10486f8fc217SDimitry Andric       ++aggIdx;
1049009b1c42SEd Schouten     } else
10506f8fc217SDimitry Andric       RewriteVal = &*ScalarAI++;
1051009b1c42SEd Schouten 
10525ca98fd9SDimitry Andric     std::vector<User *> Users(inputs[i]->user_begin(), inputs[i]->user_end());
105301095a5dSDimitry Andric     for (User *use : Users)
105401095a5dSDimitry Andric       if (Instruction *inst = dyn_cast<Instruction>(use))
105558b69754SDimitry Andric         if (Blocks.count(inst->getParent()))
1056009b1c42SEd Schouten           inst->replaceUsesOfWith(inputs[i], RewriteVal);
1057009b1c42SEd Schouten   }
1058009b1c42SEd Schouten 
1059009b1c42SEd Schouten   // Set names for input and output arguments.
10606f8fc217SDimitry Andric   if (NumScalarParams) {
10616f8fc217SDimitry Andric     ScalarAI = newFunction->arg_begin();
10626f8fc217SDimitry Andric     for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++ScalarAI)
10636f8fc217SDimitry Andric       if (!StructValues.contains(inputs[i]))
10646f8fc217SDimitry Andric         ScalarAI->setName(inputs[i]->getName());
10656f8fc217SDimitry Andric     for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++ScalarAI)
10666f8fc217SDimitry Andric       if (!StructValues.contains(outputs[i]))
10676f8fc217SDimitry Andric         ScalarAI->setName(outputs[i]->getName() + ".out");
1068009b1c42SEd Schouten   }
1069009b1c42SEd Schouten 
1070009b1c42SEd Schouten   // Rewrite branches to basic blocks outside of the loop to new dummy blocks
1071009b1c42SEd Schouten   // within the new function. This must be done before we lose track of which
1072009b1c42SEd Schouten   // blocks were originally in the code region.
10735ca98fd9SDimitry Andric   std::vector<User *> Users(header->user_begin(), header->user_end());
10741d5ae102SDimitry Andric   for (auto &U : Users)
1075009b1c42SEd Schouten     // The BasicBlock which contains the branch is not in the region
1076009b1c42SEd Schouten     // modify the branch target to a new block
10771d5ae102SDimitry Andric     if (Instruction *I = dyn_cast<Instruction>(U))
10781d5ae102SDimitry Andric       if (I->isTerminator() && I->getFunction() == oldFunction &&
10791d5ae102SDimitry Andric           !Blocks.count(I->getParent()))
1080d8e91e46SDimitry Andric         I->replaceUsesOfWith(header, newHeader);
1081009b1c42SEd Schouten 
1082009b1c42SEd Schouten   return newFunction;
1083009b1c42SEd Schouten }
1084009b1c42SEd Schouten 
1085e6d15924SDimitry Andric /// Erase lifetime.start markers which reference inputs to the extraction
1086e6d15924SDimitry Andric /// region, and insert the referenced memory into \p LifetimesStart.
1087e6d15924SDimitry Andric ///
1088e6d15924SDimitry Andric /// The extraction region is defined by a set of blocks (\p Blocks), and a set
1089e6d15924SDimitry Andric /// of allocas which will be moved from the caller function into the extracted
1090e6d15924SDimitry Andric /// function (\p SunkAllocas).
eraseLifetimeMarkersOnInputs(const SetVector<BasicBlock * > & Blocks,const SetVector<Value * > & SunkAllocas,SetVector<Value * > & LifetimesStart)1091e6d15924SDimitry Andric static void eraseLifetimeMarkersOnInputs(const SetVector<BasicBlock *> &Blocks,
1092e6d15924SDimitry Andric                                          const SetVector<Value *> &SunkAllocas,
1093e6d15924SDimitry Andric                                          SetVector<Value *> &LifetimesStart) {
1094e6d15924SDimitry Andric   for (BasicBlock *BB : Blocks) {
1095c0981da4SDimitry Andric     for (Instruction &I : llvm::make_early_inc_range(*BB)) {
1096c0981da4SDimitry Andric       auto *II = dyn_cast<IntrinsicInst>(&I);
1097e6d15924SDimitry Andric       if (!II || !II->isLifetimeStartOrEnd())
1098e6d15924SDimitry Andric         continue;
1099e6d15924SDimitry Andric 
1100e6d15924SDimitry Andric       // Get the memory operand of the lifetime marker. If the underlying
1101e6d15924SDimitry Andric       // object is a sunk alloca, or is otherwise defined in the extraction
1102e6d15924SDimitry Andric       // region, the lifetime marker must not be erased.
1103e6d15924SDimitry Andric       Value *Mem = II->getOperand(1)->stripInBoundsOffsets();
1104e6d15924SDimitry Andric       if (SunkAllocas.count(Mem) || definedInRegion(Blocks, Mem))
1105e6d15924SDimitry Andric         continue;
1106e6d15924SDimitry Andric 
1107e6d15924SDimitry Andric       if (II->getIntrinsicID() == Intrinsic::lifetime_start)
1108e6d15924SDimitry Andric         LifetimesStart.insert(Mem);
1109e6d15924SDimitry Andric       II->eraseFromParent();
1110e6d15924SDimitry Andric     }
1111e6d15924SDimitry Andric   }
1112e6d15924SDimitry Andric }
1113e6d15924SDimitry Andric 
1114e6d15924SDimitry Andric /// Insert lifetime start/end markers surrounding the call to the new function
1115e6d15924SDimitry Andric /// for objects defined in the caller.
insertLifetimeMarkersSurroundingCall(Module * M,ArrayRef<Value * > LifetimesStart,ArrayRef<Value * > LifetimesEnd,CallInst * TheCall)1116e6d15924SDimitry Andric static void insertLifetimeMarkersSurroundingCall(
1117e6d15924SDimitry Andric     Module *M, ArrayRef<Value *> LifetimesStart, ArrayRef<Value *> LifetimesEnd,
1118e6d15924SDimitry Andric     CallInst *TheCall) {
1119e6d15924SDimitry Andric   LLVMContext &Ctx = M->getContext();
1120e6d15924SDimitry Andric   auto NegativeOne = ConstantInt::getSigned(Type::getInt64Ty(Ctx), -1);
1121e6d15924SDimitry Andric   Instruction *Term = TheCall->getParent()->getTerminator();
1122e6d15924SDimitry Andric 
1123e6d15924SDimitry Andric   // Emit lifetime markers for the pointers given in \p Objects. Insert the
1124e6d15924SDimitry Andric   // markers before the call if \p InsertBefore, and after the call otherwise.
11257fa27ce4SDimitry Andric   auto insertMarkers = [&](Intrinsic::ID MarkerFunc, ArrayRef<Value *> Objects,
1126e6d15924SDimitry Andric                            bool InsertBefore) {
1127e6d15924SDimitry Andric     for (Value *Mem : Objects) {
1128e6d15924SDimitry Andric       assert((!isa<Instruction>(Mem) || cast<Instruction>(Mem)->getFunction() ==
1129e6d15924SDimitry Andric                                             TheCall->getFunction()) &&
1130e6d15924SDimitry Andric              "Input memory not defined in original function");
1131e6d15924SDimitry Andric 
11327fa27ce4SDimitry Andric       Function *Func = Intrinsic::getDeclaration(M, MarkerFunc, Mem->getType());
11337fa27ce4SDimitry Andric       auto Marker = CallInst::Create(Func, {NegativeOne, Mem});
1134e6d15924SDimitry Andric       if (InsertBefore)
1135e6d15924SDimitry Andric         Marker->insertBefore(TheCall);
1136e6d15924SDimitry Andric       else
1137e6d15924SDimitry Andric         Marker->insertBefore(Term);
1138e6d15924SDimitry Andric     }
1139e6d15924SDimitry Andric   };
1140e6d15924SDimitry Andric 
1141e6d15924SDimitry Andric   if (!LifetimesStart.empty()) {
11427fa27ce4SDimitry Andric     insertMarkers(Intrinsic::lifetime_start, LifetimesStart,
11437fa27ce4SDimitry Andric                   /*InsertBefore=*/true);
1144e6d15924SDimitry Andric   }
1145e6d15924SDimitry Andric 
1146e6d15924SDimitry Andric   if (!LifetimesEnd.empty()) {
11477fa27ce4SDimitry Andric     insertMarkers(Intrinsic::lifetime_end, LifetimesEnd,
11487fa27ce4SDimitry Andric                   /*InsertBefore=*/false);
1149e6d15924SDimitry Andric   }
1150e6d15924SDimitry Andric }
1151e6d15924SDimitry Andric 
1152009b1c42SEd Schouten /// emitCallAndSwitchStatement - This method sets up the caller side by adding
1153009b1c42SEd Schouten /// the call instruction, splitting any PHI nodes in the header block as
1154009b1c42SEd Schouten /// necessary.
emitCallAndSwitchStatement(Function * newFunction,BasicBlock * codeReplacer,ValueSet & inputs,ValueSet & outputs)1155d8e91e46SDimitry Andric CallInst *CodeExtractor::emitCallAndSwitchStatement(Function *newFunction,
1156d8e91e46SDimitry Andric                                                     BasicBlock *codeReplacer,
1157d8e91e46SDimitry Andric                                                     ValueSet &inputs,
1158d8e91e46SDimitry Andric                                                     ValueSet &outputs) {
1159009b1c42SEd Schouten   // Emit a call to the new function, passing in: *pointer to struct (if
1160009b1c42SEd Schouten   // aggregating parameters), or plan inputs and allocated memory for outputs
11616f8fc217SDimitry Andric   std::vector<Value *> params, ReloadOutputs, Reloads;
11626f8fc217SDimitry Andric   ValueSet StructValues;
116359850d08SRoman Divacky 
116471d5a254SDimitry Andric   Module *M = newFunction->getParent();
116571d5a254SDimitry Andric   LLVMContext &Context = M->getContext();
116671d5a254SDimitry Andric   const DataLayout &DL = M->getDataLayout();
1167d8e91e46SDimitry Andric   CallInst *call = nullptr;
1168009b1c42SEd Schouten 
1169009b1c42SEd Schouten   // Add inputs as params, or to be filled into the struct
11706f8fc217SDimitry Andric   unsigned ScalarInputArgNo = 0;
1171e6d15924SDimitry Andric   SmallVector<unsigned, 1> SwiftErrorArgs;
1172e6d15924SDimitry Andric   for (Value *input : inputs) {
11736f8fc217SDimitry Andric     if (AggregateArgs && !ExcludeArgsFromAggregate.contains(input))
11746f8fc217SDimitry Andric       StructValues.insert(input);
1175e6d15924SDimitry Andric     else {
117601095a5dSDimitry Andric       params.push_back(input);
1177e6d15924SDimitry Andric       if (input->isSwiftError())
11786f8fc217SDimitry Andric         SwiftErrorArgs.push_back(ScalarInputArgNo);
1179e6d15924SDimitry Andric     }
11806f8fc217SDimitry Andric     ++ScalarInputArgNo;
1181e6d15924SDimitry Andric   }
1182009b1c42SEd Schouten 
1183009b1c42SEd Schouten   // Create allocas for the outputs
11846f8fc217SDimitry Andric   unsigned ScalarOutputArgNo = 0;
118501095a5dSDimitry Andric   for (Value *output : outputs) {
11866f8fc217SDimitry Andric     if (AggregateArgs && !ExcludeArgsFromAggregate.contains(output)) {
11876f8fc217SDimitry Andric       StructValues.insert(output);
1188009b1c42SEd Schouten     } else {
1189009b1c42SEd Schouten       AllocaInst *alloca =
119071d5a254SDimitry Andric         new AllocaInst(output->getType(), DL.getAllocaAddrSpace(),
119171d5a254SDimitry Andric                        nullptr, output->getName() + ".loc",
1192ac9a064cSDimitry Andric                        codeReplacer->getParent()->front().begin());
1193009b1c42SEd Schouten       ReloadOutputs.push_back(alloca);
1194009b1c42SEd Schouten       params.push_back(alloca);
11956f8fc217SDimitry Andric       ++ScalarOutputArgNo;
1196009b1c42SEd Schouten     }
1197009b1c42SEd Schouten   }
1198009b1c42SEd Schouten 
11995a5ac124SDimitry Andric   StructType *StructArgTy = nullptr;
12005ca98fd9SDimitry Andric   AllocaInst *Struct = nullptr;
12016f8fc217SDimitry Andric   unsigned NumAggregatedInputs = 0;
12026f8fc217SDimitry Andric   if (AggregateArgs && !StructValues.empty()) {
1203411bd29eSDimitry Andric     std::vector<Type *> ArgTypes;
1204344a3780SDimitry Andric     for (Value *V : StructValues)
1205344a3780SDimitry Andric       ArgTypes.push_back(V->getType());
1206009b1c42SEd Schouten 
1207009b1c42SEd Schouten     // Allocate a struct at the beginning of this function
12085a5ac124SDimitry Andric     StructArgTy = StructType::get(newFunction->getContext(), ArgTypes);
1209145449b1SDimitry Andric     Struct = new AllocaInst(
1210145449b1SDimitry Andric         StructArgTy, DL.getAllocaAddrSpace(), nullptr, "structArg",
1211ac9a064cSDimitry Andric         AllocationBlock ? AllocationBlock->getFirstInsertionPt()
1212ac9a064cSDimitry Andric                         : codeReplacer->getParent()->front().begin());
1213009b1c42SEd Schouten 
1214b1c73532SDimitry Andric     if (ArgsInZeroAddressSpace && DL.getAllocaAddrSpace() != 0) {
1215b1c73532SDimitry Andric       auto *StructSpaceCast = new AddrSpaceCastInst(
1216b1c73532SDimitry Andric           Struct, PointerType ::get(Context, 0), "structArg.ascast");
1217b1c73532SDimitry Andric       StructSpaceCast->insertAfter(Struct);
1218b1c73532SDimitry Andric       params.push_back(StructSpaceCast);
1219b1c73532SDimitry Andric     } else {
1220b1c73532SDimitry Andric       params.push_back(Struct);
1221b1c73532SDimitry Andric     }
12226f8fc217SDimitry Andric     // Store aggregated inputs in the struct.
12236f8fc217SDimitry Andric     for (unsigned i = 0, e = StructValues.size(); i != e; ++i) {
12246f8fc217SDimitry Andric       if (inputs.contains(StructValues[i])) {
1225009b1c42SEd Schouten         Value *Idx[2];
122659850d08SRoman Divacky         Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
122759850d08SRoman Divacky         Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i);
12285a5ac124SDimitry Andric         GetElementPtrInst *GEP = GetElementPtrInst::Create(
12295a5ac124SDimitry Andric             StructArgTy, Struct, Idx, "gep_" + StructValues[i]->getName());
1230e3b55780SDimitry Andric         GEP->insertInto(codeReplacer, codeReplacer->end());
1231cfca06d7SDimitry Andric         new StoreInst(StructValues[i], GEP, codeReplacer);
12326f8fc217SDimitry Andric         NumAggregatedInputs++;
12336f8fc217SDimitry Andric       }
1234009b1c42SEd Schouten     }
1235009b1c42SEd Schouten   }
1236009b1c42SEd Schouten 
1237009b1c42SEd Schouten   // Emit the call to the function
1238d8e91e46SDimitry Andric   call = CallInst::Create(newFunction, params,
1239009b1c42SEd Schouten                           NumExitBlocks > 1 ? "targetBlock" : "");
1240044eb2f6SDimitry Andric   // Add debug location to the new call, if the original function has debug
1241044eb2f6SDimitry Andric   // info. In that case, the terminator of the entry block of the extracted
1242044eb2f6SDimitry Andric   // function contains the first debug location of the extracted function,
1243044eb2f6SDimitry Andric   // set in extractCodeRegion.
1244044eb2f6SDimitry Andric   if (codeReplacer->getParent()->getSubprogram()) {
1245044eb2f6SDimitry Andric     if (auto DL = newFunction->getEntryBlock().getTerminator()->getDebugLoc())
1246044eb2f6SDimitry Andric       call->setDebugLoc(DL);
1247044eb2f6SDimitry Andric   }
1248e3b55780SDimitry Andric   call->insertInto(codeReplacer, codeReplacer->end());
1249009b1c42SEd Schouten 
1250e6d15924SDimitry Andric   // Set swifterror parameter attributes.
1251e6d15924SDimitry Andric   for (unsigned SwiftErrArgNo : SwiftErrorArgs) {
1252e6d15924SDimitry Andric     call->addParamAttr(SwiftErrArgNo, Attribute::SwiftError);
1253e6d15924SDimitry Andric     newFunction->addParamAttr(SwiftErrArgNo, Attribute::SwiftError);
1254e6d15924SDimitry Andric   }
1255e6d15924SDimitry Andric 
12566f8fc217SDimitry Andric   // Reload the outputs passed in by reference, use the struct if output is in
12576f8fc217SDimitry Andric   // the aggregate or reload from the scalar argument.
12586f8fc217SDimitry Andric   for (unsigned i = 0, e = outputs.size(), scalarIdx = 0,
12596f8fc217SDimitry Andric                 aggIdx = NumAggregatedInputs;
12606f8fc217SDimitry Andric        i != e; ++i) {
12615ca98fd9SDimitry Andric     Value *Output = nullptr;
12626f8fc217SDimitry Andric     if (AggregateArgs && StructValues.contains(outputs[i])) {
1263009b1c42SEd Schouten       Value *Idx[2];
126459850d08SRoman Divacky       Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
12656f8fc217SDimitry Andric       Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), aggIdx);
12665a5ac124SDimitry Andric       GetElementPtrInst *GEP = GetElementPtrInst::Create(
12675a5ac124SDimitry Andric           StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName());
1268e3b55780SDimitry Andric       GEP->insertInto(codeReplacer, codeReplacer->end());
1269009b1c42SEd Schouten       Output = GEP;
12706f8fc217SDimitry Andric       ++aggIdx;
1271009b1c42SEd Schouten     } else {
12726f8fc217SDimitry Andric       Output = ReloadOutputs[scalarIdx];
12736f8fc217SDimitry Andric       ++scalarIdx;
1274009b1c42SEd Schouten     }
1275e6d15924SDimitry Andric     LoadInst *load = new LoadInst(outputs[i]->getType(), Output,
1276cfca06d7SDimitry Andric                                   outputs[i]->getName() + ".reload",
1277cfca06d7SDimitry Andric                                   codeReplacer);
127859850d08SRoman Divacky     Reloads.push_back(load);
12795ca98fd9SDimitry Andric     std::vector<User *> Users(outputs[i]->user_begin(), outputs[i]->user_end());
1280e3b55780SDimitry Andric     for (User *U : Users) {
1281e3b55780SDimitry Andric       Instruction *inst = cast<Instruction>(U);
128258b69754SDimitry Andric       if (!Blocks.count(inst->getParent()))
1283009b1c42SEd Schouten         inst->replaceUsesOfWith(outputs[i], load);
1284009b1c42SEd Schouten     }
1285009b1c42SEd Schouten   }
1286009b1c42SEd Schouten 
1287009b1c42SEd Schouten   // Now we can emit a switch statement using the call as a value.
1288009b1c42SEd Schouten   SwitchInst *TheSwitch =
128959850d08SRoman Divacky       SwitchInst::Create(Constant::getNullValue(Type::getInt16Ty(Context)),
1290009b1c42SEd Schouten                          codeReplacer, 0, codeReplacer);
1291009b1c42SEd Schouten 
1292009b1c42SEd Schouten   // Since there may be multiple exits from the original region, make the new
1293009b1c42SEd Schouten   // function return an unsigned, switch on that number.  This loop iterates
1294009b1c42SEd Schouten   // over all of the blocks in the extracted region, updating any terminator
1295009b1c42SEd Schouten   // instructions in the to-be-extracted region that branch to blocks that are
1296009b1c42SEd Schouten   // not in the region to be extracted.
1297009b1c42SEd Schouten   std::map<BasicBlock *, BasicBlock *> ExitBlockMap;
1298009b1c42SEd Schouten 
1299c0981da4SDimitry Andric   // Iterate over the previously collected targets, and create new blocks inside
1300c0981da4SDimitry Andric   // the function to branch to.
1301009b1c42SEd Schouten   unsigned switchVal = 0;
1302c0981da4SDimitry Andric   for (BasicBlock *OldTarget : OldTargets) {
1303c0981da4SDimitry Andric     if (Blocks.count(OldTarget))
1304c0981da4SDimitry Andric       continue;
1305009b1c42SEd Schouten     BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
1306c0981da4SDimitry Andric     if (NewTarget)
1307c0981da4SDimitry Andric       continue;
1308c0981da4SDimitry Andric 
1309009b1c42SEd Schouten     // If we don't already have an exit stub for this non-extracted
1310009b1c42SEd Schouten     // destination, create one now!
131159850d08SRoman Divacky     NewTarget = BasicBlock::Create(Context,
131259850d08SRoman Divacky                                     OldTarget->getName() + ".exitStub",
1313009b1c42SEd Schouten                                     newFunction);
1314009b1c42SEd Schouten     unsigned SuccNum = switchVal++;
1315009b1c42SEd Schouten 
13165ca98fd9SDimitry Andric     Value *brVal = nullptr;
1317c0981da4SDimitry Andric     assert(NumExitBlocks < 0xffff && "too many exit blocks for switch");
1318009b1c42SEd Schouten     switch (NumExitBlocks) {
1319009b1c42SEd Schouten     case 0:
1320009b1c42SEd Schouten     case 1: break;  // No value needed.
1321009b1c42SEd Schouten     case 2:         // Conditional branch, return a bool
132259850d08SRoman Divacky       brVal = ConstantInt::get(Type::getInt1Ty(Context), !SuccNum);
1323009b1c42SEd Schouten       break;
1324009b1c42SEd Schouten     default:
132559850d08SRoman Divacky       brVal = ConstantInt::get(Type::getInt16Ty(Context), SuccNum);
1326009b1c42SEd Schouten       break;
1327009b1c42SEd Schouten     }
1328009b1c42SEd Schouten 
1329044eb2f6SDimitry Andric     ReturnInst::Create(Context, brVal, NewTarget);
1330009b1c42SEd Schouten 
1331009b1c42SEd Schouten     // Update the switch instruction.
133259850d08SRoman Divacky     TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context),
133359850d08SRoman Divacky                                         SuccNum),
1334009b1c42SEd Schouten                         OldTarget);
1335009b1c42SEd Schouten   }
1336009b1c42SEd Schouten 
1337c0981da4SDimitry Andric   for (BasicBlock *Block : Blocks) {
1338c0981da4SDimitry Andric     Instruction *TI = Block->getTerminator();
1339c0981da4SDimitry Andric     for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
1340c0981da4SDimitry Andric       if (Blocks.count(TI->getSuccessor(i)))
1341c0981da4SDimitry Andric         continue;
1342c0981da4SDimitry Andric       BasicBlock *OldTarget = TI->getSuccessor(i);
1343c0981da4SDimitry Andric       // add a new basic block which returns the appropriate value
1344c0981da4SDimitry Andric       BasicBlock *NewTarget = ExitBlockMap[OldTarget];
1345c0981da4SDimitry Andric       assert(NewTarget && "Unknown target block!");
1346c0981da4SDimitry Andric 
1347009b1c42SEd Schouten       // rewrite the original branch instruction with this new target
1348009b1c42SEd Schouten       TI->setSuccessor(i, NewTarget);
1349009b1c42SEd Schouten    }
1350009b1c42SEd Schouten   }
1351009b1c42SEd Schouten 
1352e6d15924SDimitry Andric   // Store the arguments right after the definition of output value.
1353e6d15924SDimitry Andric   // This should be proceeded after creating exit stubs to be ensure that invoke
1354e6d15924SDimitry Andric   // result restore will be placed in the outlined function.
13556f8fc217SDimitry Andric   Function::arg_iterator ScalarOutputArgBegin = newFunction->arg_begin();
13566f8fc217SDimitry Andric   std::advance(ScalarOutputArgBegin, ScalarInputArgNo);
13576f8fc217SDimitry Andric   Function::arg_iterator AggOutputArgBegin = newFunction->arg_begin();
13586f8fc217SDimitry Andric   std::advance(AggOutputArgBegin, ScalarInputArgNo + ScalarOutputArgNo);
13596f8fc217SDimitry Andric 
13606f8fc217SDimitry Andric   for (unsigned i = 0, e = outputs.size(), aggIdx = NumAggregatedInputs; i != e;
13616f8fc217SDimitry Andric        ++i) {
1362e6d15924SDimitry Andric     auto *OutI = dyn_cast<Instruction>(outputs[i]);
1363e6d15924SDimitry Andric     if (!OutI)
1364e6d15924SDimitry Andric       continue;
1365e6d15924SDimitry Andric 
1366e6d15924SDimitry Andric     // Find proper insertion point.
1367e6d15924SDimitry Andric     BasicBlock::iterator InsertPt;
1368e6d15924SDimitry Andric     // In case OutI is an invoke, we insert the store at the beginning in the
1369e6d15924SDimitry Andric     // 'normal destination' BB. Otherwise we insert the store right after OutI.
1370e6d15924SDimitry Andric     if (auto *InvokeI = dyn_cast<InvokeInst>(OutI))
1371e6d15924SDimitry Andric       InsertPt = InvokeI->getNormalDest()->getFirstInsertionPt();
1372e6d15924SDimitry Andric     else if (auto *Phi = dyn_cast<PHINode>(OutI))
1373e6d15924SDimitry Andric       InsertPt = Phi->getParent()->getFirstInsertionPt();
1374e6d15924SDimitry Andric     else
1375e6d15924SDimitry Andric       InsertPt = std::next(OutI->getIterator());
1376e6d15924SDimitry Andric 
1377ac9a064cSDimitry Andric     assert((InsertPt->getFunction() == newFunction ||
1378ac9a064cSDimitry Andric             Blocks.count(InsertPt->getParent())) &&
1379e6d15924SDimitry Andric            "InsertPt should be in new function");
13806f8fc217SDimitry Andric     if (AggregateArgs && StructValues.contains(outputs[i])) {
13816f8fc217SDimitry Andric       assert(AggOutputArgBegin != newFunction->arg_end() &&
13826f8fc217SDimitry Andric              "Number of aggregate output arguments should match "
13836f8fc217SDimitry Andric              "the number of defined values");
1384e6d15924SDimitry Andric       Value *Idx[2];
1385e6d15924SDimitry Andric       Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
13866f8fc217SDimitry Andric       Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), aggIdx);
1387e6d15924SDimitry Andric       GetElementPtrInst *GEP = GetElementPtrInst::Create(
13886f8fc217SDimitry Andric           StructArgTy, &*AggOutputArgBegin, Idx, "gep_" + outputs[i]->getName(),
1389ac9a064cSDimitry Andric           InsertPt);
1390ac9a064cSDimitry Andric       new StoreInst(outputs[i], GEP, InsertPt);
13916f8fc217SDimitry Andric       ++aggIdx;
1392e6d15924SDimitry Andric       // Since there should be only one struct argument aggregating
13936f8fc217SDimitry Andric       // all the output values, we shouldn't increment AggOutputArgBegin, which
13946f8fc217SDimitry Andric       // always points to the struct argument, in this case.
1395e6d15924SDimitry Andric     } else {
13966f8fc217SDimitry Andric       assert(ScalarOutputArgBegin != newFunction->arg_end() &&
13976f8fc217SDimitry Andric              "Number of scalar output arguments should match "
13986f8fc217SDimitry Andric              "the number of defined values");
1399ac9a064cSDimitry Andric       new StoreInst(outputs[i], &*ScalarOutputArgBegin, InsertPt);
14006f8fc217SDimitry Andric       ++ScalarOutputArgBegin;
1401e6d15924SDimitry Andric     }
1402e6d15924SDimitry Andric   }
1403e6d15924SDimitry Andric 
1404009b1c42SEd Schouten   // Now that we've done the deed, simplify the switch instruction.
140530815c53SDimitry Andric   Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType();
1406009b1c42SEd Schouten   switch (NumExitBlocks) {
1407009b1c42SEd Schouten   case 0:
1408009b1c42SEd Schouten     // There are no successors (the block containing the switch itself), which
1409009b1c42SEd Schouten     // means that previously this was the last part of the function, and hence
1410ac9a064cSDimitry Andric     // this should be rewritten as a `ret` or `unreachable`.
1411ac9a064cSDimitry Andric     if (newFunction->doesNotReturn()) {
1412ac9a064cSDimitry Andric       // If fn is no return, end with an unreachable terminator.
1413ac9a064cSDimitry Andric       (void)new UnreachableInst(Context, TheSwitch->getIterator());
1414ac9a064cSDimitry Andric     } else if (OldFnRetTy->isVoidTy()) {
1415ac9a064cSDimitry Andric       // We have no return value.
1416ac9a064cSDimitry Andric       ReturnInst::Create(Context, nullptr,
1417ac9a064cSDimitry Andric                          TheSwitch->getIterator()); // Return void
1418009b1c42SEd Schouten     } else if (OldFnRetTy == TheSwitch->getCondition()->getType()) {
1419009b1c42SEd Schouten       // return what we have
1420ac9a064cSDimitry Andric       ReturnInst::Create(Context, TheSwitch->getCondition(),
1421ac9a064cSDimitry Andric                          TheSwitch->getIterator());
1422009b1c42SEd Schouten     } else {
1423009b1c42SEd Schouten       // Otherwise we must have code extracted an unwind or something, just
1424009b1c42SEd Schouten       // return whatever we want.
1425ac9a064cSDimitry Andric       ReturnInst::Create(Context, Constant::getNullValue(OldFnRetTy),
1426ac9a064cSDimitry Andric                          TheSwitch->getIterator());
1427009b1c42SEd Schouten     }
1428009b1c42SEd Schouten 
1429009b1c42SEd Schouten     TheSwitch->eraseFromParent();
1430009b1c42SEd Schouten     break;
1431009b1c42SEd Schouten   case 1:
1432009b1c42SEd Schouten     // Only a single destination, change the switch into an unconditional
1433009b1c42SEd Schouten     // branch.
1434ac9a064cSDimitry Andric     BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getIterator());
1435009b1c42SEd Schouten     TheSwitch->eraseFromParent();
1436009b1c42SEd Schouten     break;
1437009b1c42SEd Schouten   case 2:
1438009b1c42SEd Schouten     BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2),
1439ac9a064cSDimitry Andric                        call, TheSwitch->getIterator());
1440009b1c42SEd Schouten     TheSwitch->eraseFromParent();
1441009b1c42SEd Schouten     break;
1442009b1c42SEd Schouten   default:
1443009b1c42SEd Schouten     // Otherwise, make the default destination of the switch instruction be one
1444009b1c42SEd Schouten     // of the other successors.
144563faed5bSDimitry Andric     TheSwitch->setCondition(call);
144663faed5bSDimitry Andric     TheSwitch->setDefaultDest(TheSwitch->getSuccessor(NumExitBlocks));
144763faed5bSDimitry Andric     // Remove redundant case
1448f8af5cf6SDimitry Andric     TheSwitch->removeCase(SwitchInst::CaseIt(TheSwitch, NumExitBlocks-1));
1449009b1c42SEd Schouten     break;
1450009b1c42SEd Schouten   }
1451d8e91e46SDimitry Andric 
1452e6d15924SDimitry Andric   // Insert lifetime markers around the reloads of any output values. The
1453e6d15924SDimitry Andric   // allocas output values are stored in are only in-use in the codeRepl block.
1454e6d15924SDimitry Andric   insertLifetimeMarkersSurroundingCall(M, ReloadOutputs, ReloadOutputs, call);
1455e6d15924SDimitry Andric 
1456d8e91e46SDimitry Andric   return call;
1457009b1c42SEd Schouten }
1458009b1c42SEd Schouten 
moveCodeToFunction(Function * newFunction)1459009b1c42SEd Schouten void CodeExtractor::moveCodeToFunction(Function *newFunction) {
1460c0981da4SDimitry Andric   auto newFuncIt = newFunction->front().getIterator();
146101095a5dSDimitry Andric   for (BasicBlock *Block : Blocks) {
1462009b1c42SEd Schouten     // Delete the basic block from the old function, and the list of blocks
1463e3b55780SDimitry Andric     Block->removeFromParent();
1464009b1c42SEd Schouten 
1465009b1c42SEd Schouten     // Insert this basic block into the new function
1466c0981da4SDimitry Andric     // Insert the original blocks after the entry block created
1467c0981da4SDimitry Andric     // for the new function. The entry block may be followed
1468c0981da4SDimitry Andric     // by a set of exit blocks at this point, but these exit
1469c0981da4SDimitry Andric     // blocks better be placed at the end of the new function.
1470e3b55780SDimitry Andric     newFuncIt = newFunction->insert(std::next(newFuncIt), Block);
1471009b1c42SEd Schouten   }
1472009b1c42SEd Schouten }
1473009b1c42SEd Schouten 
calculateNewCallTerminatorWeights(BasicBlock * CodeReplacer,DenseMap<BasicBlock *,BlockFrequency> & ExitWeights,BranchProbabilityInfo * BPI)1474b915e9e0SDimitry Andric void CodeExtractor::calculateNewCallTerminatorWeights(
1475b915e9e0SDimitry Andric     BasicBlock *CodeReplacer,
1476b915e9e0SDimitry Andric     DenseMap<BasicBlock *, BlockFrequency> &ExitWeights,
1477b915e9e0SDimitry Andric     BranchProbabilityInfo *BPI) {
1478044eb2f6SDimitry Andric   using Distribution = BlockFrequencyInfoImplBase::Distribution;
1479044eb2f6SDimitry Andric   using BlockNode = BlockFrequencyInfoImplBase::BlockNode;
1480b915e9e0SDimitry Andric 
1481b915e9e0SDimitry Andric   // Update the branch weights for the exit block.
1482d8e91e46SDimitry Andric   Instruction *TI = CodeReplacer->getTerminator();
1483b915e9e0SDimitry Andric   SmallVector<unsigned, 8> BranchWeights(TI->getNumSuccessors(), 0);
1484b915e9e0SDimitry Andric 
1485b915e9e0SDimitry Andric   // Block Frequency distribution with dummy node.
1486b915e9e0SDimitry Andric   Distribution BranchDist;
1487b915e9e0SDimitry Andric 
1488cfca06d7SDimitry Andric   SmallVector<BranchProbability, 4> EdgeProbabilities(
1489cfca06d7SDimitry Andric       TI->getNumSuccessors(), BranchProbability::getUnknown());
1490cfca06d7SDimitry Andric 
1491b915e9e0SDimitry Andric   // Add each of the frequencies of the successors.
1492b915e9e0SDimitry Andric   for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) {
1493b915e9e0SDimitry Andric     BlockNode ExitNode(i);
1494b915e9e0SDimitry Andric     uint64_t ExitFreq = ExitWeights[TI->getSuccessor(i)].getFrequency();
1495b915e9e0SDimitry Andric     if (ExitFreq != 0)
1496b915e9e0SDimitry Andric       BranchDist.addExit(ExitNode, ExitFreq);
1497b915e9e0SDimitry Andric     else
1498cfca06d7SDimitry Andric       EdgeProbabilities[i] = BranchProbability::getZero();
1499b915e9e0SDimitry Andric   }
1500b915e9e0SDimitry Andric 
1501b915e9e0SDimitry Andric   // Check for no total weight.
1502cfca06d7SDimitry Andric   if (BranchDist.Total == 0) {
1503cfca06d7SDimitry Andric     BPI->setEdgeProbability(CodeReplacer, EdgeProbabilities);
1504b915e9e0SDimitry Andric     return;
1505cfca06d7SDimitry Andric   }
1506b915e9e0SDimitry Andric 
1507b915e9e0SDimitry Andric   // Normalize the distribution so that they can fit in unsigned.
1508b915e9e0SDimitry Andric   BranchDist.normalize();
1509b915e9e0SDimitry Andric 
1510b915e9e0SDimitry Andric   // Create normalized branch weights and set the metadata.
1511b915e9e0SDimitry Andric   for (unsigned I = 0, E = BranchDist.Weights.size(); I < E; ++I) {
1512b915e9e0SDimitry Andric     const auto &Weight = BranchDist.Weights[I];
1513b915e9e0SDimitry Andric 
1514b915e9e0SDimitry Andric     // Get the weight and update the current BFI.
1515b915e9e0SDimitry Andric     BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
1516b915e9e0SDimitry Andric     BranchProbability BP(Weight.Amount, BranchDist.Total);
1517cfca06d7SDimitry Andric     EdgeProbabilities[Weight.TargetNode.Index] = BP;
1518b915e9e0SDimitry Andric   }
1519cfca06d7SDimitry Andric   BPI->setEdgeProbability(CodeReplacer, EdgeProbabilities);
1520b915e9e0SDimitry Andric   TI->setMetadata(
1521b915e9e0SDimitry Andric       LLVMContext::MD_prof,
1522b915e9e0SDimitry Andric       MDBuilder(TI->getContext()).createBranchWeights(BranchWeights));
1523b915e9e0SDimitry Andric }
1524b915e9e0SDimitry Andric 
1525cfca06d7SDimitry Andric /// Erase debug info intrinsics which refer to values in \p F but aren't in
1526cfca06d7SDimitry Andric /// \p F.
eraseDebugIntrinsicsWithNonLocalRefs(Function & F)1527cfca06d7SDimitry Andric static void eraseDebugIntrinsicsWithNonLocalRefs(Function &F) {
1528cfca06d7SDimitry Andric   for (Instruction &I : instructions(F)) {
1529cfca06d7SDimitry Andric     SmallVector<DbgVariableIntrinsic *, 4> DbgUsers;
1530ac9a064cSDimitry Andric     SmallVector<DbgVariableRecord *, 4> DbgVariableRecords;
1531ac9a064cSDimitry Andric     findDbgUsers(DbgUsers, &I, &DbgVariableRecords);
1532cfca06d7SDimitry Andric     for (DbgVariableIntrinsic *DVI : DbgUsers)
1533cfca06d7SDimitry Andric       if (DVI->getFunction() != &F)
1534cfca06d7SDimitry Andric         DVI->eraseFromParent();
1535ac9a064cSDimitry Andric     for (DbgVariableRecord *DVR : DbgVariableRecords)
1536ac9a064cSDimitry Andric       if (DVR->getFunction() != &F)
1537ac9a064cSDimitry Andric         DVR->eraseFromParent();
1538cfca06d7SDimitry Andric   }
1539cfca06d7SDimitry Andric }
1540cfca06d7SDimitry Andric 
1541cfca06d7SDimitry Andric /// Fix up the debug info in the old and new functions by pointing line
1542cfca06d7SDimitry Andric /// locations and debug intrinsics to the new subprogram scope, and by deleting
1543cfca06d7SDimitry Andric /// intrinsics which point to values outside of the new function.
fixupDebugInfoPostExtraction(Function & OldFunc,Function & NewFunc,CallInst & TheCall)1544cfca06d7SDimitry Andric static void fixupDebugInfoPostExtraction(Function &OldFunc, Function &NewFunc,
1545cfca06d7SDimitry Andric                                          CallInst &TheCall) {
1546cfca06d7SDimitry Andric   DISubprogram *OldSP = OldFunc.getSubprogram();
1547cfca06d7SDimitry Andric   LLVMContext &Ctx = OldFunc.getContext();
1548cfca06d7SDimitry Andric 
1549cfca06d7SDimitry Andric   if (!OldSP) {
1550cfca06d7SDimitry Andric     // Erase any debug info the new function contains.
1551cfca06d7SDimitry Andric     stripDebugInfo(NewFunc);
1552cfca06d7SDimitry Andric     // Make sure the old function doesn't contain any non-local metadata refs.
1553cfca06d7SDimitry Andric     eraseDebugIntrinsicsWithNonLocalRefs(NewFunc);
1554cfca06d7SDimitry Andric     return;
1555cfca06d7SDimitry Andric   }
1556cfca06d7SDimitry Andric 
1557cfca06d7SDimitry Andric   // Create a subprogram for the new function. Leave out a description of the
1558cfca06d7SDimitry Andric   // function arguments, as the parameters don't correspond to anything at the
1559cfca06d7SDimitry Andric   // source level.
1560cfca06d7SDimitry Andric   assert(OldSP->getUnit() && "Missing compile unit for subprogram");
1561b60736ecSDimitry Andric   DIBuilder DIB(*OldFunc.getParent(), /*AllowUnresolved=*/false,
1562cfca06d7SDimitry Andric                 OldSP->getUnit());
1563e3b55780SDimitry Andric   auto SPType =
1564e3b55780SDimitry Andric       DIB.createSubroutineType(DIB.getOrCreateTypeArray(std::nullopt));
1565cfca06d7SDimitry Andric   DISubprogram::DISPFlags SPFlags = DISubprogram::SPFlagDefinition |
1566cfca06d7SDimitry Andric                                     DISubprogram::SPFlagOptimized |
1567cfca06d7SDimitry Andric                                     DISubprogram::SPFlagLocalToUnit;
1568cfca06d7SDimitry Andric   auto NewSP = DIB.createFunction(
1569cfca06d7SDimitry Andric       OldSP->getUnit(), NewFunc.getName(), NewFunc.getName(), OldSP->getFile(),
1570cfca06d7SDimitry Andric       /*LineNo=*/0, SPType, /*ScopeLine=*/0, DINode::FlagZero, SPFlags);
1571cfca06d7SDimitry Andric   NewFunc.setSubprogram(NewSP);
1572cfca06d7SDimitry Andric 
1573b1c73532SDimitry Andric   auto IsInvalidLocation = [&NewFunc](Value *Location) {
1574b1c73532SDimitry Andric     // Location is invalid if it isn't a constant or an instruction, or is an
1575b1c73532SDimitry Andric     // instruction but isn't in the new function.
1576b1c73532SDimitry Andric     if (!Location ||
1577b1c73532SDimitry Andric         (!isa<Constant>(Location) && !isa<Instruction>(Location)))
1578b1c73532SDimitry Andric       return true;
1579b1c73532SDimitry Andric     Instruction *LocationInst = dyn_cast<Instruction>(Location);
1580b1c73532SDimitry Andric     return LocationInst && LocationInst->getFunction() != &NewFunc;
1581b1c73532SDimitry Andric   };
1582b1c73532SDimitry Andric 
1583cfca06d7SDimitry Andric   // Debug intrinsics in the new function need to be updated in one of two
1584cfca06d7SDimitry Andric   // ways:
1585cfca06d7SDimitry Andric   //  1) They need to be deleted, because they describe a value in the old
1586cfca06d7SDimitry Andric   //     function.
1587cfca06d7SDimitry Andric   //  2) They need to point to fresh metadata, e.g. because they currently
1588cfca06d7SDimitry Andric   //     point to a variable in the wrong scope.
1589cfca06d7SDimitry Andric   SmallDenseMap<DINode *, DINode *> RemappedMetadata;
1590cfca06d7SDimitry Andric   SmallVector<Instruction *, 4> DebugIntrinsicsToDelete;
1591ac9a064cSDimitry Andric   SmallVector<DbgVariableRecord *, 4> DVRsToDelete;
1592e3b55780SDimitry Andric   DenseMap<const MDNode *, MDNode *> Cache;
1593b1c73532SDimitry Andric 
1594b1c73532SDimitry Andric   auto GetUpdatedDIVariable = [&](DILocalVariable *OldVar) {
1595b1c73532SDimitry Andric     DINode *&NewVar = RemappedMetadata[OldVar];
1596b1c73532SDimitry Andric     if (!NewVar) {
1597b1c73532SDimitry Andric       DILocalScope *NewScope = DILocalScope::cloneScopeForSubprogram(
1598b1c73532SDimitry Andric           *OldVar->getScope(), *NewSP, Ctx, Cache);
1599b1c73532SDimitry Andric       NewVar = DIB.createAutoVariable(
1600b1c73532SDimitry Andric           NewScope, OldVar->getName(), OldVar->getFile(), OldVar->getLine(),
1601b1c73532SDimitry Andric           OldVar->getType(), /*AlwaysPreserve=*/false, DINode::FlagZero,
1602b1c73532SDimitry Andric           OldVar->getAlignInBits());
1603b1c73532SDimitry Andric     }
1604b1c73532SDimitry Andric     return cast<DILocalVariable>(NewVar);
1605b1c73532SDimitry Andric   };
1606b1c73532SDimitry Andric 
1607ac9a064cSDimitry Andric   auto UpdateDbgLabel = [&](auto *LabelRecord) {
1608ac9a064cSDimitry Andric     // Point the label record to a fresh label within the new function if
1609ac9a064cSDimitry Andric     // the record was not inlined from some other function.
1610ac9a064cSDimitry Andric     if (LabelRecord->getDebugLoc().getInlinedAt())
1611ac9a064cSDimitry Andric       return;
1612ac9a064cSDimitry Andric     DILabel *OldLabel = LabelRecord->getLabel();
1613ac9a064cSDimitry Andric     DINode *&NewLabel = RemappedMetadata[OldLabel];
1614ac9a064cSDimitry Andric     if (!NewLabel) {
1615ac9a064cSDimitry Andric       DILocalScope *NewScope = DILocalScope::cloneScopeForSubprogram(
1616ac9a064cSDimitry Andric           *OldLabel->getScope(), *NewSP, Ctx, Cache);
1617ac9a064cSDimitry Andric       NewLabel = DILabel::get(Ctx, NewScope, OldLabel->getName(),
1618ac9a064cSDimitry Andric                               OldLabel->getFile(), OldLabel->getLine());
1619ac9a064cSDimitry Andric     }
1620ac9a064cSDimitry Andric     LabelRecord->setLabel(cast<DILabel>(NewLabel));
1621ac9a064cSDimitry Andric   };
1622ac9a064cSDimitry Andric 
1623ac9a064cSDimitry Andric   auto UpdateDbgRecordsOnInst = [&](Instruction &I) -> void {
1624ac9a064cSDimitry Andric     for (DbgRecord &DR : I.getDbgRecordRange()) {
1625ac9a064cSDimitry Andric       if (DbgLabelRecord *DLR = dyn_cast<DbgLabelRecord>(&DR)) {
1626ac9a064cSDimitry Andric         UpdateDbgLabel(DLR);
1627ac9a064cSDimitry Andric         continue;
1628ac9a064cSDimitry Andric       }
1629ac9a064cSDimitry Andric 
1630ac9a064cSDimitry Andric       DbgVariableRecord &DVR = cast<DbgVariableRecord>(DR);
1631b1c73532SDimitry Andric       // Apply the two updates that dbg.values get: invalid operands, and
1632b1c73532SDimitry Andric       // variable metadata fixup.
1633ac9a064cSDimitry Andric       if (any_of(DVR.location_ops(), IsInvalidLocation)) {
1634ac9a064cSDimitry Andric         DVRsToDelete.push_back(&DVR);
1635b1c73532SDimitry Andric         continue;
1636b1c73532SDimitry Andric       }
1637ac9a064cSDimitry Andric       if (DVR.isDbgAssign() && IsInvalidLocation(DVR.getAddress())) {
1638ac9a064cSDimitry Andric         DVRsToDelete.push_back(&DVR);
16394df029ccSDimitry Andric         continue;
16404df029ccSDimitry Andric       }
1641ac9a064cSDimitry Andric       if (!DVR.getDebugLoc().getInlinedAt())
1642ac9a064cSDimitry Andric         DVR.setVariable(GetUpdatedDIVariable(DVR.getVariable()));
1643b1c73532SDimitry Andric     }
1644b1c73532SDimitry Andric   };
1645b1c73532SDimitry Andric 
1646cfca06d7SDimitry Andric   for (Instruction &I : instructions(NewFunc)) {
1647ac9a064cSDimitry Andric     UpdateDbgRecordsOnInst(I);
1648b1c73532SDimitry Andric 
1649cfca06d7SDimitry Andric     auto *DII = dyn_cast<DbgInfoIntrinsic>(&I);
1650cfca06d7SDimitry Andric     if (!DII)
1651cfca06d7SDimitry Andric       continue;
1652cfca06d7SDimitry Andric 
1653e3b55780SDimitry Andric     // Point the intrinsic to a fresh label within the new function if the
1654e3b55780SDimitry Andric     // intrinsic was not inlined from some other function.
1655cfca06d7SDimitry Andric     if (auto *DLI = dyn_cast<DbgLabelInst>(&I)) {
1656ac9a064cSDimitry Andric       UpdateDbgLabel(DLI);
1657cfca06d7SDimitry Andric       continue;
1658cfca06d7SDimitry Andric     }
1659cfca06d7SDimitry Andric 
1660344a3780SDimitry Andric     auto *DVI = cast<DbgVariableIntrinsic>(DII);
1661344a3780SDimitry Andric     // If any of the used locations are invalid, delete the intrinsic.
1662344a3780SDimitry Andric     if (any_of(DVI->location_ops(), IsInvalidLocation)) {
1663cfca06d7SDimitry Andric       DebugIntrinsicsToDelete.push_back(DVI);
1664cfca06d7SDimitry Andric       continue;
1665cfca06d7SDimitry Andric     }
1666b1c73532SDimitry Andric     // DbgAssign intrinsics have an extra Value argument:
1667b1c73532SDimitry Andric     if (auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);
1668b1c73532SDimitry Andric         DAI && IsInvalidLocation(DAI->getAddress())) {
1669b1c73532SDimitry Andric       DebugIntrinsicsToDelete.push_back(DVI);
1670b1c73532SDimitry Andric       continue;
1671b1c73532SDimitry Andric     }
1672e3b55780SDimitry Andric     // If the variable was in the scope of the old function, i.e. it was not
1673e3b55780SDimitry Andric     // inlined, point the intrinsic to a fresh variable within the new function.
1674b1c73532SDimitry Andric     if (!DVI->getDebugLoc().getInlinedAt())
1675b1c73532SDimitry Andric       DVI->setVariable(GetUpdatedDIVariable(DVI->getVariable()));
1676e3b55780SDimitry Andric   }
1677e3b55780SDimitry Andric 
1678cfca06d7SDimitry Andric   for (auto *DII : DebugIntrinsicsToDelete)
1679cfca06d7SDimitry Andric     DII->eraseFromParent();
1680ac9a064cSDimitry Andric   for (auto *DVR : DVRsToDelete)
1681ac9a064cSDimitry Andric     DVR->getMarker()->MarkedInstr->dropOneDbgRecord(DVR);
1682cfca06d7SDimitry Andric   DIB.finalizeSubprogram(NewSP);
1683cfca06d7SDimitry Andric 
1684ac9a064cSDimitry Andric   // Fix up the scope information attached to the line locations and the
1685ac9a064cSDimitry Andric   // debug assignment metadata in the new function.
1686ac9a064cSDimitry Andric   DenseMap<DIAssignID *, DIAssignID *> AssignmentIDMap;
1687cfca06d7SDimitry Andric   for (Instruction &I : instructions(NewFunc)) {
1688cfca06d7SDimitry Andric     if (const DebugLoc &DL = I.getDebugLoc())
1689e3b55780SDimitry Andric       I.setDebugLoc(
1690e3b55780SDimitry Andric           DebugLoc::replaceInlinedAtSubprogram(DL, *NewSP, Ctx, Cache));
1691ac9a064cSDimitry Andric     for (DbgRecord &DR : I.getDbgRecordRange())
1692ac9a064cSDimitry Andric       DR.setDebugLoc(DebugLoc::replaceInlinedAtSubprogram(DR.getDebugLoc(),
1693ac9a064cSDimitry Andric                                                           *NewSP, Ctx, Cache));
1694cfca06d7SDimitry Andric 
1695cfca06d7SDimitry Andric     // Loop info metadata may contain line locations. Fix them up.
1696e3b55780SDimitry Andric     auto updateLoopInfoLoc = [&Ctx, &Cache, NewSP](Metadata *MD) -> Metadata * {
1697344a3780SDimitry Andric       if (auto *Loc = dyn_cast_or_null<DILocation>(MD))
1698e3b55780SDimitry Andric         return DebugLoc::replaceInlinedAtSubprogram(Loc, *NewSP, Ctx, Cache);
1699344a3780SDimitry Andric       return MD;
1700cfca06d7SDimitry Andric     };
1701cfca06d7SDimitry Andric     updateLoopMetadataDebugLocations(I, updateLoopInfoLoc);
1702ac9a064cSDimitry Andric     at::remapAssignID(AssignmentIDMap, I);
1703cfca06d7SDimitry Andric   }
1704cfca06d7SDimitry Andric   if (!TheCall.getDebugLoc())
1705b60736ecSDimitry Andric     TheCall.setDebugLoc(DILocation::get(Ctx, 0, 0, OldSP));
1706cfca06d7SDimitry Andric 
1707cfca06d7SDimitry Andric   eraseDebugIntrinsicsWithNonLocalRefs(NewFunc);
1708cfca06d7SDimitry Andric }
1709cfca06d7SDimitry Andric 
17101d5ae102SDimitry Andric Function *
extractCodeRegion(const CodeExtractorAnalysisCache & CEAC)17111d5ae102SDimitry Andric CodeExtractor::extractCodeRegion(const CodeExtractorAnalysisCache &CEAC) {
1712c0981da4SDimitry Andric   ValueSet Inputs, Outputs;
1713c0981da4SDimitry Andric   return extractCodeRegion(CEAC, Inputs, Outputs);
1714c0981da4SDimitry Andric }
1715c0981da4SDimitry Andric 
1716c0981da4SDimitry Andric Function *
extractCodeRegion(const CodeExtractorAnalysisCache & CEAC,ValueSet & inputs,ValueSet & outputs)1717c0981da4SDimitry Andric CodeExtractor::extractCodeRegion(const CodeExtractorAnalysisCache &CEAC,
1718c0981da4SDimitry Andric                                  ValueSet &inputs, ValueSet &outputs) {
171958b69754SDimitry Andric   if (!isEligible())
17205ca98fd9SDimitry Andric     return nullptr;
1721009b1c42SEd Schouten 
1722009b1c42SEd Schouten   // Assumption: this is a single-entry code region, and the header is the first
1723009b1c42SEd Schouten   // block in the region.
172458b69754SDimitry Andric   BasicBlock *header = *Blocks.begin();
1725044eb2f6SDimitry Andric   Function *oldFunction = header->getParent();
1726044eb2f6SDimitry Andric 
1727b915e9e0SDimitry Andric   // Calculate the entry frequency of the new function before we change the root
1728b915e9e0SDimitry Andric   //   block.
1729b915e9e0SDimitry Andric   BlockFrequency EntryFreq;
1730b915e9e0SDimitry Andric   if (BFI) {
1731b915e9e0SDimitry Andric     assert(BPI && "Both BPI and BFI are required to preserve profile info");
1732b915e9e0SDimitry Andric     for (BasicBlock *Pred : predecessors(header)) {
1733b915e9e0SDimitry Andric       if (Blocks.count(Pred))
1734b915e9e0SDimitry Andric         continue;
1735b915e9e0SDimitry Andric       EntryFreq +=
1736b915e9e0SDimitry Andric           BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, header);
1737b915e9e0SDimitry Andric     }
1738b915e9e0SDimitry Andric   }
1739b915e9e0SDimitry Andric 
17407fa27ce4SDimitry Andric   // Remove @llvm.assume calls that will be moved to the new function from the
17417fa27ce4SDimitry Andric   // old function's assumption cache.
1742cfca06d7SDimitry Andric   for (BasicBlock *Block : Blocks) {
1743c0981da4SDimitry Andric     for (Instruction &I : llvm::make_early_inc_range(*Block)) {
17447fa27ce4SDimitry Andric       if (auto *AI = dyn_cast<AssumeInst>(&I)) {
1745cfca06d7SDimitry Andric         if (AC)
17467fa27ce4SDimitry Andric           AC->unregisterAssumption(AI);
17477fa27ce4SDimitry Andric         AI->eraseFromParent();
1748cfca06d7SDimitry Andric       }
1749cfca06d7SDimitry Andric     }
17501d5ae102SDimitry Andric   }
17511d5ae102SDimitry Andric 
1752009b1c42SEd Schouten   // If we have any return instructions in the region, split those blocks so
1753009b1c42SEd Schouten   // that the return is not in the region.
1754009b1c42SEd Schouten   splitReturnBlocks();
1755009b1c42SEd Schouten 
1756d8e91e46SDimitry Andric   // Calculate the exit blocks for the extracted region and the total exit
1757d8e91e46SDimitry Andric   // weights for each of those blocks.
1758d8e91e46SDimitry Andric   DenseMap<BasicBlock *, BlockFrequency> ExitWeights;
1759ac9a064cSDimitry Andric   SetVector<BasicBlock *> ExitBlocks;
1760d8e91e46SDimitry Andric   for (BasicBlock *Block : Blocks) {
1761344a3780SDimitry Andric     for (BasicBlock *Succ : successors(Block)) {
1762344a3780SDimitry Andric       if (!Blocks.count(Succ)) {
1763d8e91e46SDimitry Andric         // Update the branch weight for this successor.
1764d8e91e46SDimitry Andric         if (BFI) {
1765344a3780SDimitry Andric           BlockFrequency &BF = ExitWeights[Succ];
1766344a3780SDimitry Andric           BF += BFI->getBlockFreq(Block) * BPI->getEdgeProbability(Block, Succ);
1767d8e91e46SDimitry Andric         }
1768344a3780SDimitry Andric         ExitBlocks.insert(Succ);
1769d8e91e46SDimitry Andric       }
1770d8e91e46SDimitry Andric     }
1771d8e91e46SDimitry Andric   }
1772d8e91e46SDimitry Andric   NumExitBlocks = ExitBlocks.size();
1773d8e91e46SDimitry Andric 
1774c0981da4SDimitry Andric   for (BasicBlock *Block : Blocks) {
17754df029ccSDimitry Andric     for (BasicBlock *OldTarget : successors(Block))
17764df029ccSDimitry Andric       if (!Blocks.contains(OldTarget))
1777c0981da4SDimitry Andric         OldTargets.push_back(OldTarget);
1778c0981da4SDimitry Andric   }
1779c0981da4SDimitry Andric 
1780d8e91e46SDimitry Andric   // If we have to split PHI nodes of the entry or exit blocks, do so now.
1781d8e91e46SDimitry Andric   severSplitPHINodesOfEntry(header);
1782d8e91e46SDimitry Andric   severSplitPHINodesOfExits(ExitBlocks);
1783d8e91e46SDimitry Andric 
1784009b1c42SEd Schouten   // This takes place of the original loop
178559850d08SRoman Divacky   BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(),
178659850d08SRoman Divacky                                                 "codeRepl", oldFunction,
1787009b1c42SEd Schouten                                                 header);
1788b1c73532SDimitry Andric   codeReplacer->IsNewDbgInfoFormat = oldFunction->IsNewDbgInfoFormat;
1789009b1c42SEd Schouten 
1790009b1c42SEd Schouten   // The new function needs a root node because other nodes can branch to the
1791009b1c42SEd Schouten   // head of the region, but the entry node of a function cannot have preds.
179259850d08SRoman Divacky   BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(),
179359850d08SRoman Divacky                                                "newFuncRoot");
1794b1c73532SDimitry Andric   newFuncRoot->IsNewDbgInfoFormat = oldFunction->IsNewDbgInfoFormat;
1795b1c73532SDimitry Andric 
1796044eb2f6SDimitry Andric   auto *BranchI = BranchInst::Create(header);
1797044eb2f6SDimitry Andric   // If the original function has debug info, we have to add a debug location
1798044eb2f6SDimitry Andric   // to the new branch instruction from the artificial entry block.
1799044eb2f6SDimitry Andric   // We use the debug location of the first instruction in the extracted
1800044eb2f6SDimitry Andric   // blocks, as there is no other equivalent line in the source code.
1801044eb2f6SDimitry Andric   if (oldFunction->getSubprogram()) {
1802044eb2f6SDimitry Andric     any_of(Blocks, [&BranchI](const BasicBlock *BB) {
1803044eb2f6SDimitry Andric       return any_of(*BB, [&BranchI](const Instruction &I) {
1804044eb2f6SDimitry Andric         if (!I.getDebugLoc())
1805044eb2f6SDimitry Andric           return false;
1806ac9a064cSDimitry Andric         // Don't use source locations attached to debug-intrinsics: they could
1807ac9a064cSDimitry Andric         // be from completely unrelated scopes.
1808ac9a064cSDimitry Andric         if (isa<DbgInfoIntrinsic>(I))
1809ac9a064cSDimitry Andric           return false;
1810044eb2f6SDimitry Andric         BranchI->setDebugLoc(I.getDebugLoc());
1811044eb2f6SDimitry Andric         return true;
1812044eb2f6SDimitry Andric       });
1813044eb2f6SDimitry Andric     });
1814044eb2f6SDimitry Andric   }
1815e3b55780SDimitry Andric   BranchI->insertInto(newFuncRoot, newFuncRoot->end());
1816009b1c42SEd Schouten 
1817c0981da4SDimitry Andric   ValueSet SinkingCands, HoistingCands;
18181d5ae102SDimitry Andric   BasicBlock *CommonExit = nullptr;
18191d5ae102SDimitry Andric   findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit);
18207c7aba6eSDimitry Andric   assert(HoistingCands.empty() || CommonExit);
1821f382538dSDimitry Andric 
1822009b1c42SEd Schouten   // Find inputs to, outputs from the code region.
1823f382538dSDimitry Andric   findInputsOutputs(inputs, outputs, SinkingCands);
1824f382538dSDimitry Andric 
1825e6d15924SDimitry Andric   // Now sink all instructions which only have non-phi uses inside the region.
1826e6d15924SDimitry Andric   // Group the allocas at the start of the block, so that any bitcast uses of
1827e6d15924SDimitry Andric   // the allocas are well-defined.
1828e6d15924SDimitry Andric   AllocaInst *FirstSunkAlloca = nullptr;
1829e6d15924SDimitry Andric   for (auto *II : SinkingCands) {
1830e6d15924SDimitry Andric     if (auto *AI = dyn_cast<AllocaInst>(II)) {
1831e6d15924SDimitry Andric       AI->moveBefore(*newFuncRoot, newFuncRoot->getFirstInsertionPt());
1832e6d15924SDimitry Andric       if (!FirstSunkAlloca)
1833e6d15924SDimitry Andric         FirstSunkAlloca = AI;
1834e6d15924SDimitry Andric     }
1835e6d15924SDimitry Andric   }
1836e6d15924SDimitry Andric   assert((SinkingCands.empty() || FirstSunkAlloca) &&
1837e6d15924SDimitry Andric          "Did not expect a sink candidate without any allocas");
1838e6d15924SDimitry Andric   for (auto *II : SinkingCands) {
1839e6d15924SDimitry Andric     if (!isa<AllocaInst>(II)) {
1840e6d15924SDimitry Andric       cast<Instruction>(II)->moveAfter(FirstSunkAlloca);
1841e6d15924SDimitry Andric     }
1842e6d15924SDimitry Andric   }
1843009b1c42SEd Schouten 
18447c7aba6eSDimitry Andric   if (!HoistingCands.empty()) {
18457c7aba6eSDimitry Andric     auto *HoistToBlock = findOrCreateBlockForHoisting(CommonExit);
18467c7aba6eSDimitry Andric     Instruction *TI = HoistToBlock->getTerminator();
18477c7aba6eSDimitry Andric     for (auto *II : HoistingCands)
18487c7aba6eSDimitry Andric       cast<Instruction>(II)->moveBefore(TI);
18497c7aba6eSDimitry Andric   }
18507c7aba6eSDimitry Andric 
1851d8e91e46SDimitry Andric   // Collect objects which are inputs to the extraction region and also
1852e6d15924SDimitry Andric   // referenced by lifetime start markers within it. The effects of these
1853d8e91e46SDimitry Andric   // markers must be replicated in the calling function to prevent the stack
1854d8e91e46SDimitry Andric   // coloring pass from merging slots which store input objects.
1855e6d15924SDimitry Andric   ValueSet LifetimesStart;
1856e6d15924SDimitry Andric   eraseLifetimeMarkersOnInputs(Blocks, SinkingCands, LifetimesStart);
185758b69754SDimitry Andric 
1858009b1c42SEd Schouten   // Construct new function based on inputs/outputs & add allocas for all defs.
1859d8e91e46SDimitry Andric   Function *newFunction =
1860d8e91e46SDimitry Andric       constructFunction(inputs, outputs, header, newFuncRoot, codeReplacer,
1861d8e91e46SDimitry Andric                         oldFunction, oldFunction->getParent());
1862009b1c42SEd Schouten 
1863b915e9e0SDimitry Andric   // Update the entry count of the function.
1864b915e9e0SDimitry Andric   if (BFI) {
1865b1c73532SDimitry Andric     auto Count = BFI->getProfileCountFromFreq(EntryFreq);
1866145449b1SDimitry Andric     if (Count)
1867eb11fae6SDimitry Andric       newFunction->setEntryCount(
1868e3b55780SDimitry Andric           ProfileCount(*Count, Function::PCT_Real)); // FIXME
1869b1c73532SDimitry Andric     BFI->setBlockFreq(codeReplacer, EntryFreq);
1870b915e9e0SDimitry Andric   }
1871b915e9e0SDimitry Andric 
1872d8e91e46SDimitry Andric   CallInst *TheCall =
1873009b1c42SEd Schouten       emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
1874009b1c42SEd Schouten 
1875009b1c42SEd Schouten   moveCodeToFunction(newFunction);
1876009b1c42SEd Schouten 
1877d8e91e46SDimitry Andric   // Replicate the effects of any lifetime start/end markers which referenced
1878d8e91e46SDimitry Andric   // input objects in the extraction region by placing markers around the call.
1879e6d15924SDimitry Andric   insertLifetimeMarkersSurroundingCall(
1880e6d15924SDimitry Andric       oldFunction->getParent(), LifetimesStart.getArrayRef(), {}, TheCall);
1881d8e91e46SDimitry Andric 
1882eb11fae6SDimitry Andric   // Propagate personality info to the new function if there is one.
1883eb11fae6SDimitry Andric   if (oldFunction->hasPersonalityFn())
1884eb11fae6SDimitry Andric     newFunction->setPersonalityFn(oldFunction->getPersonalityFn());
1885eb11fae6SDimitry Andric 
1886b915e9e0SDimitry Andric   // Update the branch weights for the exit block.
1887b915e9e0SDimitry Andric   if (BFI && NumExitBlocks > 1)
1888b915e9e0SDimitry Andric     calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
1889b915e9e0SDimitry Andric 
1890d8e91e46SDimitry Andric   // Loop over all of the PHI nodes in the header and exit blocks, and change
1891d8e91e46SDimitry Andric   // any references to the old incoming edge to be the new incoming edge.
1892009b1c42SEd Schouten   for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) {
1893009b1c42SEd Schouten     PHINode *PN = cast<PHINode>(I);
1894009b1c42SEd Schouten     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
189558b69754SDimitry Andric       if (!Blocks.count(PN->getIncomingBlock(i)))
1896009b1c42SEd Schouten         PN->setIncomingBlock(i, newFuncRoot);
1897009b1c42SEd Schouten   }
1898009b1c42SEd Schouten 
1899d8e91e46SDimitry Andric   for (BasicBlock *ExitBB : ExitBlocks)
1900d8e91e46SDimitry Andric     for (PHINode &PN : ExitBB->phis()) {
1901d8e91e46SDimitry Andric       Value *IncomingCodeReplacerVal = nullptr;
1902d8e91e46SDimitry Andric       for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1903d8e91e46SDimitry Andric         // Ignore incoming values from outside of the extracted region.
1904d8e91e46SDimitry Andric         if (!Blocks.count(PN.getIncomingBlock(i)))
1905d8e91e46SDimitry Andric           continue;
1906d8e91e46SDimitry Andric 
1907d8e91e46SDimitry Andric         // Ensure that there is only one incoming value from codeReplacer.
1908d8e91e46SDimitry Andric         if (!IncomingCodeReplacerVal) {
1909d8e91e46SDimitry Andric           PN.setIncomingBlock(i, codeReplacer);
1910d8e91e46SDimitry Andric           IncomingCodeReplacerVal = PN.getIncomingValue(i);
1911d8e91e46SDimitry Andric         } else
1912d8e91e46SDimitry Andric           assert(IncomingCodeReplacerVal == PN.getIncomingValue(i) &&
1913d8e91e46SDimitry Andric                  "PHI has two incompatbile incoming values from codeRepl");
1914009b1c42SEd Schouten       }
1915009b1c42SEd Schouten     }
1916009b1c42SEd Schouten 
1917cfca06d7SDimitry Andric   fixupDebugInfoPostExtraction(*oldFunction, *newFunction, *TheCall);
1918d8e91e46SDimitry Andric 
1919d8e91e46SDimitry Andric   LLVM_DEBUG(if (verifyFunction(*newFunction, &errs())) {
1920d8e91e46SDimitry Andric     newFunction->dump();
1921d8e91e46SDimitry Andric     report_fatal_error("verification of newFunction failed!");
1922d8e91e46SDimitry Andric   });
1923d8e91e46SDimitry Andric   LLVM_DEBUG(if (verifyFunction(*oldFunction))
1924d8e91e46SDimitry Andric              report_fatal_error("verification of oldFunction failed!"));
1925cfca06d7SDimitry Andric   LLVM_DEBUG(if (AC && verifyAssumptionCache(*oldFunction, *newFunction, AC))
19261d5ae102SDimitry Andric                  report_fatal_error("Stale Asumption cache for old Function!"));
1927009b1c42SEd Schouten   return newFunction;
1928009b1c42SEd Schouten }
19291d5ae102SDimitry Andric 
verifyAssumptionCache(const Function & OldFunc,const Function & NewFunc,AssumptionCache * AC)1930cfca06d7SDimitry Andric bool CodeExtractor::verifyAssumptionCache(const Function &OldFunc,
1931cfca06d7SDimitry Andric                                           const Function &NewFunc,
19321d5ae102SDimitry Andric                                           AssumptionCache *AC) {
19331d5ae102SDimitry Andric   for (auto AssumeVH : AC->assumptions()) {
19347fa27ce4SDimitry Andric     auto *I = dyn_cast_or_null<CallInst>(AssumeVH);
1935cfca06d7SDimitry Andric     if (!I)
1936cfca06d7SDimitry Andric       continue;
1937cfca06d7SDimitry Andric 
1938cfca06d7SDimitry Andric     // There shouldn't be any llvm.assume intrinsics in the new function.
1939cfca06d7SDimitry Andric     if (I->getFunction() != &OldFunc)
19401d5ae102SDimitry Andric       return true;
1941cfca06d7SDimitry Andric 
1942cfca06d7SDimitry Andric     // There shouldn't be any stale affected values in the assumption cache
1943cfca06d7SDimitry Andric     // that were previously in the old function, but that have now been moved
1944cfca06d7SDimitry Andric     // to the new function.
1945cfca06d7SDimitry Andric     for (auto AffectedValVH : AC->assumptionsFor(I->getOperand(0))) {
19467fa27ce4SDimitry Andric       auto *AffectedCI = dyn_cast_or_null<CallInst>(AffectedValVH);
1947cfca06d7SDimitry Andric       if (!AffectedCI)
1948cfca06d7SDimitry Andric         continue;
1949cfca06d7SDimitry Andric       if (AffectedCI->getFunction() != &OldFunc)
1950cfca06d7SDimitry Andric         return true;
1951b60736ecSDimitry Andric       auto *AssumedInst = cast<Instruction>(AffectedCI->getOperand(0));
1952cfca06d7SDimitry Andric       if (AssumedInst->getFunction() != &OldFunc)
1953cfca06d7SDimitry Andric         return true;
1954cfca06d7SDimitry Andric     }
19551d5ae102SDimitry Andric   }
19561d5ae102SDimitry Andric   return false;
19571d5ae102SDimitry Andric }
19586f8fc217SDimitry Andric 
excludeArgFromAggregate(Value * Arg)19596f8fc217SDimitry Andric void CodeExtractor::excludeArgFromAggregate(Value *Arg) {
19606f8fc217SDimitry Andric   ExcludeArgsFromAggregate.insert(Arg);
19616f8fc217SDimitry Andric }
1962