xref: /llvm-project/llvm/lib/Transforms/Utils/CodeExtractor.cpp (revision 5f8f34e459b60efb332337e7cfe902a7cabe4096)
1 //===- CodeExtractor.cpp - Pull code region into a new function -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the interface to tear out a code region, such as an
11 // individual loop or a parallel section, into a new function, replacing it with
12 // a call to the new function.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Transforms/Utils/CodeExtractor.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Analysis/BlockFrequencyInfo.h"
25 #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
26 #include "llvm/Analysis/BranchProbabilityInfo.h"
27 #include "llvm/Analysis/LoopInfo.h"
28 #include "llvm/IR/Argument.h"
29 #include "llvm/IR/Attributes.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/CFG.h"
32 #include "llvm/IR/Constant.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/DataLayout.h"
35 #include "llvm/IR/DerivedTypes.h"
36 #include "llvm/IR/Dominators.h"
37 #include "llvm/IR/Function.h"
38 #include "llvm/IR/GlobalValue.h"
39 #include "llvm/IR/InstrTypes.h"
40 #include "llvm/IR/Instruction.h"
41 #include "llvm/IR/Instructions.h"
42 #include "llvm/IR/IntrinsicInst.h"
43 #include "llvm/IR/Intrinsics.h"
44 #include "llvm/IR/LLVMContext.h"
45 #include "llvm/IR/MDBuilder.h"
46 #include "llvm/IR/Module.h"
47 #include "llvm/IR/Type.h"
48 #include "llvm/IR/User.h"
49 #include "llvm/IR/Value.h"
50 #include "llvm/IR/Verifier.h"
51 #include "llvm/Pass.h"
52 #include "llvm/Support/BlockFrequency.h"
53 #include "llvm/Support/BranchProbability.h"
54 #include "llvm/Support/Casting.h"
55 #include "llvm/Support/CommandLine.h"
56 #include "llvm/Support/Debug.h"
57 #include "llvm/Support/ErrorHandling.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
60 #include <cassert>
61 #include <cstdint>
62 #include <iterator>
63 #include <map>
64 #include <set>
65 #include <utility>
66 #include <vector>
67 
68 using namespace llvm;
69 using ProfileCount = Function::ProfileCount;
70 
71 #define DEBUG_TYPE "code-extractor"
72 
73 // Provide a command-line option to aggregate function arguments into a struct
74 // for functions produced by the code extractor. This is useful when converting
75 // extracted functions to pthread-based code, as only one argument (void*) can
76 // be passed in to pthread_create().
77 static cl::opt<bool>
78 AggregateArgsOpt("aggregate-extracted-args", cl::Hidden,
79                  cl::desc("Aggregate arguments to code-extracted functions"));
80 
81 /// Test whether a block is valid for extraction.
82 bool CodeExtractor::isBlockValidForExtraction(const BasicBlock &BB,
83                                               bool AllowVarArgs) {
84   // Landing pads must be in the function where they were inserted for cleanup.
85   if (BB.isEHPad())
86     return false;
87   // taking the address of a basic block moved to another function is illegal
88   if (BB.hasAddressTaken())
89     return false;
90 
91   // don't hoist code that uses another basicblock address, as it's likely to
92   // lead to unexpected behavior, like cross-function jumps
93   SmallPtrSet<User const *, 16> Visited;
94   SmallVector<User const *, 16> ToVisit;
95 
96   for (Instruction const &Inst : BB)
97     ToVisit.push_back(&Inst);
98 
99   while (!ToVisit.empty()) {
100     User const *Curr = ToVisit.pop_back_val();
101     if (!Visited.insert(Curr).second)
102       continue;
103     if (isa<BlockAddress const>(Curr))
104       return false; // even a reference to self is likely to be not compatible
105 
106     if (isa<Instruction>(Curr) && cast<Instruction>(Curr)->getParent() != &BB)
107       continue;
108 
109     for (auto const &U : Curr->operands()) {
110       if (auto *UU = dyn_cast<User>(U))
111         ToVisit.push_back(UU);
112     }
113   }
114 
115   // Don't hoist code containing allocas or invokes. If explicitly requested,
116   // allow vastart.
117   for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
118     if (isa<AllocaInst>(I) || isa<InvokeInst>(I))
119       return false;
120     if (const CallInst *CI = dyn_cast<CallInst>(I))
121       if (const Function *F = CI->getCalledFunction())
122         if (F->getIntrinsicID() == Intrinsic::vastart) {
123           if (AllowVarArgs)
124             continue;
125           else
126             return false;
127         }
128   }
129 
130   return true;
131 }
132 
133 /// Build a set of blocks to extract if the input blocks are viable.
134 static SetVector<BasicBlock *>
135 buildExtractionBlockSet(ArrayRef<BasicBlock *> BBs, DominatorTree *DT,
136                         bool AllowVarArgs) {
137   assert(!BBs.empty() && "The set of blocks to extract must be non-empty");
138   SetVector<BasicBlock *> Result;
139 
140   // Loop over the blocks, adding them to our set-vector, and aborting with an
141   // empty set if we encounter invalid blocks.
142   for (BasicBlock *BB : BBs) {
143     // If this block is dead, don't process it.
144     if (DT && !DT->isReachableFromEntry(BB))
145       continue;
146 
147     if (!Result.insert(BB))
148       llvm_unreachable("Repeated basic blocks in extraction input");
149     if (!CodeExtractor::isBlockValidForExtraction(*BB, AllowVarArgs)) {
150       Result.clear();
151       return Result;
152     }
153   }
154 
155 #ifndef NDEBUG
156   for (SetVector<BasicBlock *>::iterator I = std::next(Result.begin()),
157                                          E = Result.end();
158        I != E; ++I)
159     for (pred_iterator PI = pred_begin(*I), PE = pred_end(*I);
160          PI != PE; ++PI)
161       assert(Result.count(*PI) &&
162              "No blocks in this region may have entries from outside the region"
163              " except for the first block!");
164 #endif
165 
166   return Result;
167 }
168 
169 CodeExtractor::CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT,
170                              bool AggregateArgs, BlockFrequencyInfo *BFI,
171                              BranchProbabilityInfo *BPI, bool AllowVarArgs)
172     : DT(DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
173       BPI(BPI), AllowVarArgs(AllowVarArgs),
174       Blocks(buildExtractionBlockSet(BBs, DT, AllowVarArgs)) {}
175 
176 CodeExtractor::CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs,
177                              BlockFrequencyInfo *BFI,
178                              BranchProbabilityInfo *BPI)
179     : DT(&DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
180       BPI(BPI), AllowVarArgs(false),
181       Blocks(buildExtractionBlockSet(L.getBlocks(), &DT,
182                                      /* AllowVarArgs */ false)) {}
183 
184 /// definedInRegion - Return true if the specified value is defined in the
185 /// extracted region.
186 static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) {
187   if (Instruction *I = dyn_cast<Instruction>(V))
188     if (Blocks.count(I->getParent()))
189       return true;
190   return false;
191 }
192 
193 /// definedInCaller - Return true if the specified value is defined in the
194 /// function being code extracted, but not in the region being extracted.
195 /// These values must be passed in as live-ins to the function.
196 static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) {
197   if (isa<Argument>(V)) return true;
198   if (Instruction *I = dyn_cast<Instruction>(V))
199     if (!Blocks.count(I->getParent()))
200       return true;
201   return false;
202 }
203 
204 static BasicBlock *getCommonExitBlock(const SetVector<BasicBlock *> &Blocks) {
205   BasicBlock *CommonExitBlock = nullptr;
206   auto hasNonCommonExitSucc = [&](BasicBlock *Block) {
207     for (auto *Succ : successors(Block)) {
208       // Internal edges, ok.
209       if (Blocks.count(Succ))
210         continue;
211       if (!CommonExitBlock) {
212         CommonExitBlock = Succ;
213         continue;
214       }
215       if (CommonExitBlock == Succ)
216         continue;
217 
218       return true;
219     }
220     return false;
221   };
222 
223   if (any_of(Blocks, hasNonCommonExitSucc))
224     return nullptr;
225 
226   return CommonExitBlock;
227 }
228 
229 bool CodeExtractor::isLegalToShrinkwrapLifetimeMarkers(
230     Instruction *Addr) const {
231   AllocaInst *AI = cast<AllocaInst>(Addr->stripInBoundsConstantOffsets());
232   Function *Func = (*Blocks.begin())->getParent();
233   for (BasicBlock &BB : *Func) {
234     if (Blocks.count(&BB))
235       continue;
236     for (Instruction &II : BB) {
237       if (isa<DbgInfoIntrinsic>(II))
238         continue;
239 
240       unsigned Opcode = II.getOpcode();
241       Value *MemAddr = nullptr;
242       switch (Opcode) {
243       case Instruction::Store:
244       case Instruction::Load: {
245         if (Opcode == Instruction::Store) {
246           StoreInst *SI = cast<StoreInst>(&II);
247           MemAddr = SI->getPointerOperand();
248         } else {
249           LoadInst *LI = cast<LoadInst>(&II);
250           MemAddr = LI->getPointerOperand();
251         }
252         // Global variable can not be aliased with locals.
253         if (dyn_cast<Constant>(MemAddr))
254           break;
255         Value *Base = MemAddr->stripInBoundsConstantOffsets();
256         if (!dyn_cast<AllocaInst>(Base) || Base == AI)
257           return false;
258         break;
259       }
260       default: {
261         IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(&II);
262         if (IntrInst) {
263           if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start ||
264               IntrInst->getIntrinsicID() == Intrinsic::lifetime_end)
265             break;
266           return false;
267         }
268         // Treat all the other cases conservatively if it has side effects.
269         if (II.mayHaveSideEffects())
270           return false;
271       }
272       }
273     }
274   }
275 
276   return true;
277 }
278 
279 BasicBlock *
280 CodeExtractor::findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock) {
281   BasicBlock *SinglePredFromOutlineRegion = nullptr;
282   assert(!Blocks.count(CommonExitBlock) &&
283          "Expect a block outside the region!");
284   for (auto *Pred : predecessors(CommonExitBlock)) {
285     if (!Blocks.count(Pred))
286       continue;
287     if (!SinglePredFromOutlineRegion) {
288       SinglePredFromOutlineRegion = Pred;
289     } else if (SinglePredFromOutlineRegion != Pred) {
290       SinglePredFromOutlineRegion = nullptr;
291       break;
292     }
293   }
294 
295   if (SinglePredFromOutlineRegion)
296     return SinglePredFromOutlineRegion;
297 
298 #ifndef NDEBUG
299   auto getFirstPHI = [](BasicBlock *BB) {
300     BasicBlock::iterator I = BB->begin();
301     PHINode *FirstPhi = nullptr;
302     while (I != BB->end()) {
303       PHINode *Phi = dyn_cast<PHINode>(I);
304       if (!Phi)
305         break;
306       if (!FirstPhi) {
307         FirstPhi = Phi;
308         break;
309       }
310     }
311     return FirstPhi;
312   };
313   // If there are any phi nodes, the single pred either exists or has already
314   // be created before code extraction.
315   assert(!getFirstPHI(CommonExitBlock) && "Phi not expected");
316 #endif
317 
318   BasicBlock *NewExitBlock = CommonExitBlock->splitBasicBlock(
319       CommonExitBlock->getFirstNonPHI()->getIterator());
320 
321   for (auto PI = pred_begin(CommonExitBlock), PE = pred_end(CommonExitBlock);
322        PI != PE;) {
323     BasicBlock *Pred = *PI++;
324     if (Blocks.count(Pred))
325       continue;
326     Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock);
327   }
328   // Now add the old exit block to the outline region.
329   Blocks.insert(CommonExitBlock);
330   return CommonExitBlock;
331 }
332 
333 void CodeExtractor::findAllocas(ValueSet &SinkCands, ValueSet &HoistCands,
334                                 BasicBlock *&ExitBlock) const {
335   Function *Func = (*Blocks.begin())->getParent();
336   ExitBlock = getCommonExitBlock(Blocks);
337 
338   for (BasicBlock &BB : *Func) {
339     if (Blocks.count(&BB))
340       continue;
341     for (Instruction &II : BB) {
342       auto *AI = dyn_cast<AllocaInst>(&II);
343       if (!AI)
344         continue;
345 
346       // Find the pair of life time markers for address 'Addr' that are either
347       // defined inside the outline region or can legally be shrinkwrapped into
348       // the outline region. If there are not other untracked uses of the
349       // address, return the pair of markers if found; otherwise return a pair
350       // of nullptr.
351       auto GetLifeTimeMarkers =
352           [&](Instruction *Addr, bool &SinkLifeStart,
353               bool &HoistLifeEnd) -> std::pair<Instruction *, Instruction *> {
354         Instruction *LifeStart = nullptr, *LifeEnd = nullptr;
355 
356         for (User *U : Addr->users()) {
357           IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(U);
358           if (IntrInst) {
359             if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start) {
360               // Do not handle the case where AI has multiple start markers.
361               if (LifeStart)
362                 return std::make_pair<Instruction *>(nullptr, nullptr);
363               LifeStart = IntrInst;
364             }
365             if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) {
366               if (LifeEnd)
367                 return std::make_pair<Instruction *>(nullptr, nullptr);
368               LifeEnd = IntrInst;
369             }
370             continue;
371           }
372           // Find untracked uses of the address, bail.
373           if (!definedInRegion(Blocks, U))
374             return std::make_pair<Instruction *>(nullptr, nullptr);
375         }
376 
377         if (!LifeStart || !LifeEnd)
378           return std::make_pair<Instruction *>(nullptr, nullptr);
379 
380         SinkLifeStart = !definedInRegion(Blocks, LifeStart);
381         HoistLifeEnd = !definedInRegion(Blocks, LifeEnd);
382         // Do legality Check.
383         if ((SinkLifeStart || HoistLifeEnd) &&
384             !isLegalToShrinkwrapLifetimeMarkers(Addr))
385           return std::make_pair<Instruction *>(nullptr, nullptr);
386 
387         // Check to see if we have a place to do hoisting, if not, bail.
388         if (HoistLifeEnd && !ExitBlock)
389           return std::make_pair<Instruction *>(nullptr, nullptr);
390 
391         return std::make_pair(LifeStart, LifeEnd);
392       };
393 
394       bool SinkLifeStart = false, HoistLifeEnd = false;
395       auto Markers = GetLifeTimeMarkers(AI, SinkLifeStart, HoistLifeEnd);
396 
397       if (Markers.first) {
398         if (SinkLifeStart)
399           SinkCands.insert(Markers.first);
400         SinkCands.insert(AI);
401         if (HoistLifeEnd)
402           HoistCands.insert(Markers.second);
403         continue;
404       }
405 
406       // Follow the bitcast.
407       Instruction *MarkerAddr = nullptr;
408       for (User *U : AI->users()) {
409         if (U->stripInBoundsConstantOffsets() == AI) {
410           SinkLifeStart = false;
411           HoistLifeEnd = false;
412           Instruction *Bitcast = cast<Instruction>(U);
413           Markers = GetLifeTimeMarkers(Bitcast, SinkLifeStart, HoistLifeEnd);
414           if (Markers.first) {
415             MarkerAddr = Bitcast;
416             continue;
417           }
418         }
419 
420         // Found unknown use of AI.
421         if (!definedInRegion(Blocks, U)) {
422           MarkerAddr = nullptr;
423           break;
424         }
425       }
426 
427       if (MarkerAddr) {
428         if (SinkLifeStart)
429           SinkCands.insert(Markers.first);
430         if (!definedInRegion(Blocks, MarkerAddr))
431           SinkCands.insert(MarkerAddr);
432         SinkCands.insert(AI);
433         if (HoistLifeEnd)
434           HoistCands.insert(Markers.second);
435       }
436     }
437   }
438 }
439 
440 void CodeExtractor::findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
441                                       const ValueSet &SinkCands) const {
442   for (BasicBlock *BB : Blocks) {
443     // If a used value is defined outside the region, it's an input.  If an
444     // instruction is used outside the region, it's an output.
445     for (Instruction &II : *BB) {
446       for (User::op_iterator OI = II.op_begin(), OE = II.op_end(); OI != OE;
447            ++OI) {
448         Value *V = *OI;
449         if (!SinkCands.count(V) && definedInCaller(Blocks, V))
450           Inputs.insert(V);
451       }
452 
453       for (User *U : II.users())
454         if (!definedInRegion(Blocks, U)) {
455           Outputs.insert(&II);
456           break;
457         }
458     }
459   }
460 }
461 
462 /// severSplitPHINodes - If a PHI node has multiple inputs from outside of the
463 /// region, we need to split the entry block of the region so that the PHI node
464 /// is easier to deal with.
465 void CodeExtractor::severSplitPHINodes(BasicBlock *&Header) {
466   unsigned NumPredsFromRegion = 0;
467   unsigned NumPredsOutsideRegion = 0;
468 
469   if (Header != &Header->getParent()->getEntryBlock()) {
470     PHINode *PN = dyn_cast<PHINode>(Header->begin());
471     if (!PN) return;  // No PHI nodes.
472 
473     // If the header node contains any PHI nodes, check to see if there is more
474     // than one entry from outside the region.  If so, we need to sever the
475     // header block into two.
476     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
477       if (Blocks.count(PN->getIncomingBlock(i)))
478         ++NumPredsFromRegion;
479       else
480         ++NumPredsOutsideRegion;
481 
482     // If there is one (or fewer) predecessor from outside the region, we don't
483     // need to do anything special.
484     if (NumPredsOutsideRegion <= 1) return;
485   }
486 
487   // Otherwise, we need to split the header block into two pieces: one
488   // containing PHI nodes merging values from outside of the region, and a
489   // second that contains all of the code for the block and merges back any
490   // incoming values from inside of the region.
491   BasicBlock *NewBB = SplitBlock(Header, Header->getFirstNonPHI(), DT);
492 
493   // We only want to code extract the second block now, and it becomes the new
494   // header of the region.
495   BasicBlock *OldPred = Header;
496   Blocks.remove(OldPred);
497   Blocks.insert(NewBB);
498   Header = NewBB;
499 
500   // Okay, now we need to adjust the PHI nodes and any branches from within the
501   // region to go to the new header block instead of the old header block.
502   if (NumPredsFromRegion) {
503     PHINode *PN = cast<PHINode>(OldPred->begin());
504     // Loop over all of the predecessors of OldPred that are in the region,
505     // changing them to branch to NewBB instead.
506     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
507       if (Blocks.count(PN->getIncomingBlock(i))) {
508         TerminatorInst *TI = PN->getIncomingBlock(i)->getTerminator();
509         TI->replaceUsesOfWith(OldPred, NewBB);
510       }
511 
512     // Okay, everything within the region is now branching to the right block, we
513     // just have to update the PHI nodes now, inserting PHI nodes into NewBB.
514     BasicBlock::iterator AfterPHIs;
515     for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
516       PHINode *PN = cast<PHINode>(AfterPHIs);
517       // Create a new PHI node in the new region, which has an incoming value
518       // from OldPred of PN.
519       PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion,
520                                        PN->getName() + ".ce", &NewBB->front());
521       PN->replaceAllUsesWith(NewPN);
522       NewPN->addIncoming(PN, OldPred);
523 
524       // Loop over all of the incoming value in PN, moving them to NewPN if they
525       // are from the extracted region.
526       for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
527         if (Blocks.count(PN->getIncomingBlock(i))) {
528           NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i));
529           PN->removeIncomingValue(i);
530           --i;
531         }
532       }
533     }
534   }
535 }
536 
537 void CodeExtractor::splitReturnBlocks() {
538   for (BasicBlock *Block : Blocks)
539     if (ReturnInst *RI = dyn_cast<ReturnInst>(Block->getTerminator())) {
540       BasicBlock *New =
541           Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret");
542       if (DT) {
543         // Old dominates New. New node dominates all other nodes dominated
544         // by Old.
545         DomTreeNode *OldNode = DT->getNode(Block);
546         SmallVector<DomTreeNode *, 8> Children(OldNode->begin(),
547                                                OldNode->end());
548 
549         DomTreeNode *NewNode = DT->addNewBlock(New, Block);
550 
551         for (DomTreeNode *I : Children)
552           DT->changeImmediateDominator(I, NewNode);
553       }
554     }
555 }
556 
557 /// constructFunction - make a function based on inputs and outputs, as follows:
558 /// f(in0, ..., inN, out0, ..., outN)
559 Function *CodeExtractor::constructFunction(const ValueSet &inputs,
560                                            const ValueSet &outputs,
561                                            BasicBlock *header,
562                                            BasicBlock *newRootNode,
563                                            BasicBlock *newHeader,
564                                            Function *oldFunction,
565                                            Module *M) {
566   DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
567   DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
568 
569   // This function returns unsigned, outputs will go back by reference.
570   switch (NumExitBlocks) {
571   case 0:
572   case 1: RetTy = Type::getVoidTy(header->getContext()); break;
573   case 2: RetTy = Type::getInt1Ty(header->getContext()); break;
574   default: RetTy = Type::getInt16Ty(header->getContext()); break;
575   }
576 
577   std::vector<Type *> paramTy;
578 
579   // Add the types of the input values to the function's argument list
580   for (Value *value : inputs) {
581     DEBUG(dbgs() << "value used in func: " << *value << "\n");
582     paramTy.push_back(value->getType());
583   }
584 
585   // Add the types of the output values to the function's argument list.
586   for (Value *output : outputs) {
587     DEBUG(dbgs() << "instr used in func: " << *output << "\n");
588     if (AggregateArgs)
589       paramTy.push_back(output->getType());
590     else
591       paramTy.push_back(PointerType::getUnqual(output->getType()));
592   }
593 
594   DEBUG({
595     dbgs() << "Function type: " << *RetTy << " f(";
596     for (Type *i : paramTy)
597       dbgs() << *i << ", ";
598     dbgs() << ")\n";
599   });
600 
601   StructType *StructTy;
602   if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
603     StructTy = StructType::get(M->getContext(), paramTy);
604     paramTy.clear();
605     paramTy.push_back(PointerType::getUnqual(StructTy));
606   }
607   FunctionType *funcType =
608                   FunctionType::get(RetTy, paramTy,
609                                     AllowVarArgs && oldFunction->isVarArg());
610 
611   // Create the new function
612   Function *newFunction = Function::Create(funcType,
613                                            GlobalValue::InternalLinkage,
614                                            oldFunction->getName() + "_" +
615                                            header->getName(), M);
616   // If the old function is no-throw, so is the new one.
617   if (oldFunction->doesNotThrow())
618     newFunction->setDoesNotThrow();
619 
620   // Inherit the uwtable attribute if we need to.
621   if (oldFunction->hasUWTable())
622     newFunction->setHasUWTable();
623 
624   // Inherit all of the target dependent attributes and white-listed
625   // target independent attributes.
626   //  (e.g. If the extracted region contains a call to an x86.sse
627   //  instruction we need to make sure that the extracted region has the
628   //  "target-features" attribute allowing it to be lowered.
629   // FIXME: This should be changed to check to see if a specific
630   //           attribute can not be inherited.
631   for (const auto &Attr : oldFunction->getAttributes().getFnAttributes()) {
632     if (Attr.isStringAttribute()) {
633       if (Attr.getKindAsString() == "thunk")
634         continue;
635     } else
636       switch (Attr.getKindAsEnum()) {
637       // Those attributes cannot be propagated safely. Explicitly list them
638       // here so we get a warning if new attributes are added. This list also
639       // includes non-function attributes.
640       case Attribute::Alignment:
641       case Attribute::AllocSize:
642       case Attribute::ArgMemOnly:
643       case Attribute::Builtin:
644       case Attribute::ByVal:
645       case Attribute::Convergent:
646       case Attribute::Dereferenceable:
647       case Attribute::DereferenceableOrNull:
648       case Attribute::InAlloca:
649       case Attribute::InReg:
650       case Attribute::InaccessibleMemOnly:
651       case Attribute::InaccessibleMemOrArgMemOnly:
652       case Attribute::JumpTable:
653       case Attribute::Naked:
654       case Attribute::Nest:
655       case Attribute::NoAlias:
656       case Attribute::NoBuiltin:
657       case Attribute::NoCapture:
658       case Attribute::NoReturn:
659       case Attribute::None:
660       case Attribute::NonNull:
661       case Attribute::ReadNone:
662       case Attribute::ReadOnly:
663       case Attribute::Returned:
664       case Attribute::ReturnsTwice:
665       case Attribute::SExt:
666       case Attribute::Speculatable:
667       case Attribute::StackAlignment:
668       case Attribute::StructRet:
669       case Attribute::SwiftError:
670       case Attribute::SwiftSelf:
671       case Attribute::WriteOnly:
672       case Attribute::ZExt:
673       case Attribute::EndAttrKinds:
674         continue;
675       // Those attributes should be safe to propagate to the extracted function.
676       case Attribute::AlwaysInline:
677       case Attribute::Cold:
678       case Attribute::NoRecurse:
679       case Attribute::InlineHint:
680       case Attribute::MinSize:
681       case Attribute::NoDuplicate:
682       case Attribute::NoImplicitFloat:
683       case Attribute::NoInline:
684       case Attribute::NonLazyBind:
685       case Attribute::NoRedZone:
686       case Attribute::NoUnwind:
687       case Attribute::OptForFuzzing:
688       case Attribute::OptimizeNone:
689       case Attribute::OptimizeForSize:
690       case Attribute::SafeStack:
691       case Attribute::ShadowCallStack:
692       case Attribute::SanitizeAddress:
693       case Attribute::SanitizeMemory:
694       case Attribute::SanitizeThread:
695       case Attribute::SanitizeHWAddress:
696       case Attribute::StackProtect:
697       case Attribute::StackProtectReq:
698       case Attribute::StackProtectStrong:
699       case Attribute::StrictFP:
700       case Attribute::UWTable:
701       case Attribute::NoCfCheck:
702         break;
703       }
704 
705     newFunction->addFnAttr(Attr);
706   }
707   newFunction->getBasicBlockList().push_back(newRootNode);
708 
709   // Create an iterator to name all of the arguments we inserted.
710   Function::arg_iterator AI = newFunction->arg_begin();
711 
712   // Rewrite all users of the inputs in the extracted region to use the
713   // arguments (or appropriate addressing into struct) instead.
714   for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
715     Value *RewriteVal;
716     if (AggregateArgs) {
717       Value *Idx[2];
718       Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext()));
719       Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i);
720       TerminatorInst *TI = newFunction->begin()->getTerminator();
721       GetElementPtrInst *GEP = GetElementPtrInst::Create(
722           StructTy, &*AI, Idx, "gep_" + inputs[i]->getName(), TI);
723       RewriteVal = new LoadInst(GEP, "loadgep_" + inputs[i]->getName(), TI);
724     } else
725       RewriteVal = &*AI++;
726 
727     std::vector<User *> Users(inputs[i]->user_begin(), inputs[i]->user_end());
728     for (User *use : Users)
729       if (Instruction *inst = dyn_cast<Instruction>(use))
730         if (Blocks.count(inst->getParent()))
731           inst->replaceUsesOfWith(inputs[i], RewriteVal);
732   }
733 
734   // Set names for input and output arguments.
735   if (!AggregateArgs) {
736     AI = newFunction->arg_begin();
737     for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI)
738       AI->setName(inputs[i]->getName());
739     for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI)
740       AI->setName(outputs[i]->getName()+".out");
741   }
742 
743   // Rewrite branches to basic blocks outside of the loop to new dummy blocks
744   // within the new function. This must be done before we lose track of which
745   // blocks were originally in the code region.
746   std::vector<User *> Users(header->user_begin(), header->user_end());
747   for (unsigned i = 0, e = Users.size(); i != e; ++i)
748     // The BasicBlock which contains the branch is not in the region
749     // modify the branch target to a new block
750     if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Users[i]))
751       if (!Blocks.count(TI->getParent()) &&
752           TI->getParent()->getParent() == oldFunction)
753         TI->replaceUsesOfWith(header, newHeader);
754 
755   return newFunction;
756 }
757 
758 /// emitCallAndSwitchStatement - This method sets up the caller side by adding
759 /// the call instruction, splitting any PHI nodes in the header block as
760 /// necessary.
761 void CodeExtractor::
762 emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
763                            ValueSet &inputs, ValueSet &outputs) {
764   // Emit a call to the new function, passing in: *pointer to struct (if
765   // aggregating parameters), or plan inputs and allocated memory for outputs
766   std::vector<Value *> params, StructValues, ReloadOutputs, Reloads;
767 
768   Module *M = newFunction->getParent();
769   LLVMContext &Context = M->getContext();
770   const DataLayout &DL = M->getDataLayout();
771 
772   // Add inputs as params, or to be filled into the struct
773   for (Value *input : inputs)
774     if (AggregateArgs)
775       StructValues.push_back(input);
776     else
777       params.push_back(input);
778 
779   // Create allocas for the outputs
780   for (Value *output : outputs) {
781     if (AggregateArgs) {
782       StructValues.push_back(output);
783     } else {
784       AllocaInst *alloca =
785         new AllocaInst(output->getType(), DL.getAllocaAddrSpace(),
786                        nullptr, output->getName() + ".loc",
787                        &codeReplacer->getParent()->front().front());
788       ReloadOutputs.push_back(alloca);
789       params.push_back(alloca);
790     }
791   }
792 
793   StructType *StructArgTy = nullptr;
794   AllocaInst *Struct = nullptr;
795   if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
796     std::vector<Type *> ArgTypes;
797     for (ValueSet::iterator v = StructValues.begin(),
798            ve = StructValues.end(); v != ve; ++v)
799       ArgTypes.push_back((*v)->getType());
800 
801     // Allocate a struct at the beginning of this function
802     StructArgTy = StructType::get(newFunction->getContext(), ArgTypes);
803     Struct = new AllocaInst(StructArgTy, DL.getAllocaAddrSpace(), nullptr,
804                             "structArg",
805                             &codeReplacer->getParent()->front().front());
806     params.push_back(Struct);
807 
808     for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
809       Value *Idx[2];
810       Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
811       Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i);
812       GetElementPtrInst *GEP = GetElementPtrInst::Create(
813           StructArgTy, Struct, Idx, "gep_" + StructValues[i]->getName());
814       codeReplacer->getInstList().push_back(GEP);
815       StoreInst *SI = new StoreInst(StructValues[i], GEP);
816       codeReplacer->getInstList().push_back(SI);
817     }
818   }
819 
820   // Emit the call to the function
821   CallInst *call = CallInst::Create(newFunction, params,
822                                     NumExitBlocks > 1 ? "targetBlock" : "");
823   // Add debug location to the new call, if the original function has debug
824   // info. In that case, the terminator of the entry block of the extracted
825   // function contains the first debug location of the extracted function,
826   // set in extractCodeRegion.
827   if (codeReplacer->getParent()->getSubprogram()) {
828     if (auto DL = newFunction->getEntryBlock().getTerminator()->getDebugLoc())
829       call->setDebugLoc(DL);
830   }
831   codeReplacer->getInstList().push_back(call);
832 
833   Function::arg_iterator OutputArgBegin = newFunction->arg_begin();
834   unsigned FirstOut = inputs.size();
835   if (!AggregateArgs)
836     std::advance(OutputArgBegin, inputs.size());
837 
838   // Reload the outputs passed in by reference.
839   Function::arg_iterator OAI = OutputArgBegin;
840   for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
841     Value *Output = nullptr;
842     if (AggregateArgs) {
843       Value *Idx[2];
844       Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
845       Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i);
846       GetElementPtrInst *GEP = GetElementPtrInst::Create(
847           StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName());
848       codeReplacer->getInstList().push_back(GEP);
849       Output = GEP;
850     } else {
851       Output = ReloadOutputs[i];
852     }
853     LoadInst *load = new LoadInst(Output, outputs[i]->getName()+".reload");
854     Reloads.push_back(load);
855     codeReplacer->getInstList().push_back(load);
856     std::vector<User *> Users(outputs[i]->user_begin(), outputs[i]->user_end());
857     for (unsigned u = 0, e = Users.size(); u != e; ++u) {
858       Instruction *inst = cast<Instruction>(Users[u]);
859       if (!Blocks.count(inst->getParent()))
860         inst->replaceUsesOfWith(outputs[i], load);
861     }
862 
863     // Store to argument right after the definition of output value.
864     auto *OutI = dyn_cast<Instruction>(outputs[i]);
865     if (!OutI)
866       continue;
867     // Find proper insertion point.
868     Instruction *InsertPt = OutI->getNextNode();
869     // Let's assume that there is no other guy interleave non-PHI in PHIs.
870     if (isa<PHINode>(InsertPt))
871       InsertPt = InsertPt->getParent()->getFirstNonPHI();
872 
873     assert(OAI != newFunction->arg_end() &&
874            "Number of output arguments should match "
875            "the amount of defined values");
876     if (AggregateArgs) {
877       Value *Idx[2];
878       Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
879       Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i);
880       GetElementPtrInst *GEP = GetElementPtrInst::Create(
881           StructArgTy, &*OAI, Idx, "gep_" + outputs[i]->getName(), InsertPt);
882       new StoreInst(outputs[i], GEP, InsertPt);
883       // Since there should be only one struct argument aggregating
884       // all the output values, we shouldn't increment OAI, which always
885       // points to the struct argument, in this case.
886     } else {
887       new StoreInst(outputs[i], &*OAI, InsertPt);
888       ++OAI;
889     }
890   }
891 
892   // Now we can emit a switch statement using the call as a value.
893   SwitchInst *TheSwitch =
894       SwitchInst::Create(Constant::getNullValue(Type::getInt16Ty(Context)),
895                          codeReplacer, 0, codeReplacer);
896 
897   // Since there may be multiple exits from the original region, make the new
898   // function return an unsigned, switch on that number.  This loop iterates
899   // over all of the blocks in the extracted region, updating any terminator
900   // instructions in the to-be-extracted region that branch to blocks that are
901   // not in the region to be extracted.
902   std::map<BasicBlock *, BasicBlock *> ExitBlockMap;
903 
904   unsigned switchVal = 0;
905   for (BasicBlock *Block : Blocks) {
906     TerminatorInst *TI = Block->getTerminator();
907     for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
908       if (!Blocks.count(TI->getSuccessor(i))) {
909         BasicBlock *OldTarget = TI->getSuccessor(i);
910         // add a new basic block which returns the appropriate value
911         BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
912         if (!NewTarget) {
913           // If we don't already have an exit stub for this non-extracted
914           // destination, create one now!
915           NewTarget = BasicBlock::Create(Context,
916                                          OldTarget->getName() + ".exitStub",
917                                          newFunction);
918           unsigned SuccNum = switchVal++;
919 
920           Value *brVal = nullptr;
921           switch (NumExitBlocks) {
922           case 0:
923           case 1: break;  // No value needed.
924           case 2:         // Conditional branch, return a bool
925             brVal = ConstantInt::get(Type::getInt1Ty(Context), !SuccNum);
926             break;
927           default:
928             brVal = ConstantInt::get(Type::getInt16Ty(Context), SuccNum);
929             break;
930           }
931 
932           ReturnInst::Create(Context, brVal, NewTarget);
933 
934           // Update the switch instruction.
935           TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context),
936                                               SuccNum),
937                              OldTarget);
938         }
939 
940         // rewrite the original branch instruction with this new target
941         TI->setSuccessor(i, NewTarget);
942       }
943   }
944 
945   // Now that we've done the deed, simplify the switch instruction.
946   Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType();
947   switch (NumExitBlocks) {
948   case 0:
949     // There are no successors (the block containing the switch itself), which
950     // means that previously this was the last part of the function, and hence
951     // this should be rewritten as a `ret'
952 
953     // Check if the function should return a value
954     if (OldFnRetTy->isVoidTy()) {
955       ReturnInst::Create(Context, nullptr, TheSwitch);  // Return void
956     } else if (OldFnRetTy == TheSwitch->getCondition()->getType()) {
957       // return what we have
958       ReturnInst::Create(Context, TheSwitch->getCondition(), TheSwitch);
959     } else {
960       // Otherwise we must have code extracted an unwind or something, just
961       // return whatever we want.
962       ReturnInst::Create(Context,
963                          Constant::getNullValue(OldFnRetTy), TheSwitch);
964     }
965 
966     TheSwitch->eraseFromParent();
967     break;
968   case 1:
969     // Only a single destination, change the switch into an unconditional
970     // branch.
971     BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch);
972     TheSwitch->eraseFromParent();
973     break;
974   case 2:
975     BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2),
976                        call, TheSwitch);
977     TheSwitch->eraseFromParent();
978     break;
979   default:
980     // Otherwise, make the default destination of the switch instruction be one
981     // of the other successors.
982     TheSwitch->setCondition(call);
983     TheSwitch->setDefaultDest(TheSwitch->getSuccessor(NumExitBlocks));
984     // Remove redundant case
985     TheSwitch->removeCase(SwitchInst::CaseIt(TheSwitch, NumExitBlocks-1));
986     break;
987   }
988 }
989 
990 void CodeExtractor::moveCodeToFunction(Function *newFunction) {
991   Function *oldFunc = (*Blocks.begin())->getParent();
992   Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList();
993   Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList();
994 
995   for (BasicBlock *Block : Blocks) {
996     // Delete the basic block from the old function, and the list of blocks
997     oldBlocks.remove(Block);
998 
999     // Insert this basic block into the new function
1000     newBlocks.push_back(Block);
1001   }
1002 }
1003 
1004 void CodeExtractor::calculateNewCallTerminatorWeights(
1005     BasicBlock *CodeReplacer,
1006     DenseMap<BasicBlock *, BlockFrequency> &ExitWeights,
1007     BranchProbabilityInfo *BPI) {
1008   using Distribution = BlockFrequencyInfoImplBase::Distribution;
1009   using BlockNode = BlockFrequencyInfoImplBase::BlockNode;
1010 
1011   // Update the branch weights for the exit block.
1012   TerminatorInst *TI = CodeReplacer->getTerminator();
1013   SmallVector<unsigned, 8> BranchWeights(TI->getNumSuccessors(), 0);
1014 
1015   // Block Frequency distribution with dummy node.
1016   Distribution BranchDist;
1017 
1018   // Add each of the frequencies of the successors.
1019   for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) {
1020     BlockNode ExitNode(i);
1021     uint64_t ExitFreq = ExitWeights[TI->getSuccessor(i)].getFrequency();
1022     if (ExitFreq != 0)
1023       BranchDist.addExit(ExitNode, ExitFreq);
1024     else
1025       BPI->setEdgeProbability(CodeReplacer, i, BranchProbability::getZero());
1026   }
1027 
1028   // Check for no total weight.
1029   if (BranchDist.Total == 0)
1030     return;
1031 
1032   // Normalize the distribution so that they can fit in unsigned.
1033   BranchDist.normalize();
1034 
1035   // Create normalized branch weights and set the metadata.
1036   for (unsigned I = 0, E = BranchDist.Weights.size(); I < E; ++I) {
1037     const auto &Weight = BranchDist.Weights[I];
1038 
1039     // Get the weight and update the current BFI.
1040     BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
1041     BranchProbability BP(Weight.Amount, BranchDist.Total);
1042     BPI->setEdgeProbability(CodeReplacer, Weight.TargetNode.Index, BP);
1043   }
1044   TI->setMetadata(
1045       LLVMContext::MD_prof,
1046       MDBuilder(TI->getContext()).createBranchWeights(BranchWeights));
1047 }
1048 
1049 Function *CodeExtractor::extractCodeRegion() {
1050   if (!isEligible())
1051     return nullptr;
1052 
1053   // Assumption: this is a single-entry code region, and the header is the first
1054   // block in the region.
1055   BasicBlock *header = *Blocks.begin();
1056   Function *oldFunction = header->getParent();
1057 
1058   // For functions with varargs, check that varargs handling is only done in the
1059   // outlined function, i.e vastart and vaend are only used in outlined blocks.
1060   if (AllowVarArgs && oldFunction->getFunctionType()->isVarArg()) {
1061     auto containsVarArgIntrinsic = [](Instruction &I) {
1062       if (const CallInst *CI = dyn_cast<CallInst>(&I))
1063         if (const Function *F = CI->getCalledFunction())
1064           return F->getIntrinsicID() == Intrinsic::vastart ||
1065                  F->getIntrinsicID() == Intrinsic::vaend;
1066       return false;
1067     };
1068 
1069     for (auto &BB : *oldFunction) {
1070       if (Blocks.count(&BB))
1071         continue;
1072       if (llvm::any_of(BB, containsVarArgIntrinsic))
1073         return nullptr;
1074     }
1075   }
1076   ValueSet inputs, outputs, SinkingCands, HoistingCands;
1077   BasicBlock *CommonExit = nullptr;
1078 
1079   // Calculate the entry frequency of the new function before we change the root
1080   //   block.
1081   BlockFrequency EntryFreq;
1082   if (BFI) {
1083     assert(BPI && "Both BPI and BFI are required to preserve profile info");
1084     for (BasicBlock *Pred : predecessors(header)) {
1085       if (Blocks.count(Pred))
1086         continue;
1087       EntryFreq +=
1088           BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, header);
1089     }
1090   }
1091 
1092   // If we have to split PHI nodes or the entry block, do so now.
1093   severSplitPHINodes(header);
1094 
1095   // If we have any return instructions in the region, split those blocks so
1096   // that the return is not in the region.
1097   splitReturnBlocks();
1098 
1099   // This takes place of the original loop
1100   BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(),
1101                                                 "codeRepl", oldFunction,
1102                                                 header);
1103 
1104   // The new function needs a root node because other nodes can branch to the
1105   // head of the region, but the entry node of a function cannot have preds.
1106   BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(),
1107                                                "newFuncRoot");
1108   auto *BranchI = BranchInst::Create(header);
1109   // If the original function has debug info, we have to add a debug location
1110   // to the new branch instruction from the artificial entry block.
1111   // We use the debug location of the first instruction in the extracted
1112   // blocks, as there is no other equivalent line in the source code.
1113   if (oldFunction->getSubprogram()) {
1114     any_of(Blocks, [&BranchI](const BasicBlock *BB) {
1115       return any_of(*BB, [&BranchI](const Instruction &I) {
1116         if (!I.getDebugLoc())
1117           return false;
1118         BranchI->setDebugLoc(I.getDebugLoc());
1119         return true;
1120       });
1121     });
1122   }
1123   newFuncRoot->getInstList().push_back(BranchI);
1124 
1125   findAllocas(SinkingCands, HoistingCands, CommonExit);
1126   assert(HoistingCands.empty() || CommonExit);
1127 
1128   // Find inputs to, outputs from the code region.
1129   findInputsOutputs(inputs, outputs, SinkingCands);
1130 
1131   // Now sink all instructions which only have non-phi uses inside the region
1132   for (auto *II : SinkingCands)
1133     cast<Instruction>(II)->moveBefore(*newFuncRoot,
1134                                       newFuncRoot->getFirstInsertionPt());
1135 
1136   if (!HoistingCands.empty()) {
1137     auto *HoistToBlock = findOrCreateBlockForHoisting(CommonExit);
1138     Instruction *TI = HoistToBlock->getTerminator();
1139     for (auto *II : HoistingCands)
1140       cast<Instruction>(II)->moveBefore(TI);
1141   }
1142 
1143   // Calculate the exit blocks for the extracted region and the total exit
1144   // weights for each of those blocks.
1145   DenseMap<BasicBlock *, BlockFrequency> ExitWeights;
1146   SmallPtrSet<BasicBlock *, 1> ExitBlocks;
1147   for (BasicBlock *Block : Blocks) {
1148     for (succ_iterator SI = succ_begin(Block), SE = succ_end(Block); SI != SE;
1149          ++SI) {
1150       if (!Blocks.count(*SI)) {
1151         // Update the branch weight for this successor.
1152         if (BFI) {
1153           BlockFrequency &BF = ExitWeights[*SI];
1154           BF += BFI->getBlockFreq(Block) * BPI->getEdgeProbability(Block, *SI);
1155         }
1156         ExitBlocks.insert(*SI);
1157       }
1158     }
1159   }
1160   NumExitBlocks = ExitBlocks.size();
1161 
1162   // Construct new function based on inputs/outputs & add allocas for all defs.
1163   Function *newFunction = constructFunction(inputs, outputs, header,
1164                                             newFuncRoot,
1165                                             codeReplacer, oldFunction,
1166                                             oldFunction->getParent());
1167 
1168   // Update the entry count of the function.
1169   if (BFI) {
1170     auto Count = BFI->getProfileCountFromFreq(EntryFreq.getFrequency());
1171     if (Count.hasValue())
1172       newFunction->setEntryCount(
1173           ProfileCount(Count.getValue(), Function::PCT_Real)); // FIXME
1174     BFI->setBlockFreq(codeReplacer, EntryFreq.getFrequency());
1175   }
1176 
1177   emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
1178 
1179   moveCodeToFunction(newFunction);
1180 
1181   // Update the branch weights for the exit block.
1182   if (BFI && NumExitBlocks > 1)
1183     calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
1184 
1185   // Loop over all of the PHI nodes in the header block, and change any
1186   // references to the old incoming edge to be the new incoming edge.
1187   for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) {
1188     PHINode *PN = cast<PHINode>(I);
1189     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1190       if (!Blocks.count(PN->getIncomingBlock(i)))
1191         PN->setIncomingBlock(i, newFuncRoot);
1192   }
1193 
1194   // Look at all successors of the codeReplacer block.  If any of these blocks
1195   // had PHI nodes in them, we need to update the "from" block to be the code
1196   // replacer, not the original block in the extracted region.
1197   std::vector<BasicBlock *> Succs(succ_begin(codeReplacer),
1198                                   succ_end(codeReplacer));
1199   for (unsigned i = 0, e = Succs.size(); i != e; ++i)
1200     for (BasicBlock::iterator I = Succs[i]->begin(); isa<PHINode>(I); ++I) {
1201       PHINode *PN = cast<PHINode>(I);
1202       std::set<BasicBlock*> ProcessedPreds;
1203       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1204         if (Blocks.count(PN->getIncomingBlock(i))) {
1205           if (ProcessedPreds.insert(PN->getIncomingBlock(i)).second)
1206             PN->setIncomingBlock(i, codeReplacer);
1207           else {
1208             // There were multiple entries in the PHI for this block, now there
1209             // is only one, so remove the duplicated entries.
1210             PN->removeIncomingValue(i, false);
1211             --i; --e;
1212           }
1213         }
1214     }
1215 
1216   DEBUG(if (verifyFunction(*newFunction))
1217         report_fatal_error("verifyFunction failed!"));
1218   return newFunction;
1219 }
1220