Home | History | Annotate | Download | only in Utils
      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/SetVector.h"
     18 #include "llvm/ADT/STLExtras.h"
     19 #include "llvm/ADT/StringExtras.h"
     20 #include "llvm/Analysis/Dominators.h"
     21 #include "llvm/Analysis/LoopInfo.h"
     22 #include "llvm/Analysis/RegionInfo.h"
     23 #include "llvm/Analysis/RegionIterator.h"
     24 #include "llvm/Analysis/Verifier.h"
     25 #include "llvm/IR/Constants.h"
     26 #include "llvm/IR/DerivedTypes.h"
     27 #include "llvm/IR/Instructions.h"
     28 #include "llvm/IR/Intrinsics.h"
     29 #include "llvm/IR/LLVMContext.h"
     30 #include "llvm/IR/Module.h"
     31 #include "llvm/Pass.h"
     32 #include "llvm/Support/CommandLine.h"
     33 #include "llvm/Support/Debug.h"
     34 #include "llvm/Support/ErrorHandling.h"
     35 #include "llvm/Support/raw_ostream.h"
     36 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
     37 #include <algorithm>
     38 #include <set>
     39 using namespace llvm;
     40 
     41 // Provide a command-line option to aggregate function arguments into a struct
     42 // for functions produced by the code extractor. This is useful when converting
     43 // extracted functions to pthread-based code, as only one argument (void*) can
     44 // be passed in to pthread_create().
     45 static cl::opt<bool>
     46 AggregateArgsOpt("aggregate-extracted-args", cl::Hidden,
     47                  cl::desc("Aggregate arguments to code-extracted functions"));
     48 
     49 /// \brief Test whether a block is valid for extraction.
     50 static bool isBlockValidForExtraction(const BasicBlock &BB) {
     51   // Landing pads must be in the function where they were inserted for cleanup.
     52   if (BB.isLandingPad())
     53     return false;
     54 
     55   // Don't hoist code containing allocas, invokes, or vastarts.
     56   for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
     57     if (isa<AllocaInst>(I) || isa<InvokeInst>(I))
     58       return false;
     59     if (const CallInst *CI = dyn_cast<CallInst>(I))
     60       if (const Function *F = CI->getCalledFunction())
     61         if (F->getIntrinsicID() == Intrinsic::vastart)
     62           return false;
     63   }
     64 
     65   return true;
     66 }
     67 
     68 /// \brief Build a set of blocks to extract if the input blocks are viable.
     69 template <typename IteratorT>
     70 static SetVector<BasicBlock *> buildExtractionBlockSet(IteratorT BBBegin,
     71                                                        IteratorT BBEnd) {
     72   SetVector<BasicBlock *> Result;
     73 
     74   assert(BBBegin != BBEnd);
     75 
     76   // Loop over the blocks, adding them to our set-vector, and aborting with an
     77   // empty set if we encounter invalid blocks.
     78   for (IteratorT I = BBBegin, E = BBEnd; I != E; ++I) {
     79     if (!Result.insert(*I))
     80       llvm_unreachable("Repeated basic blocks in extraction input");
     81 
     82     if (!isBlockValidForExtraction(**I)) {
     83       Result.clear();
     84       return Result;
     85     }
     86   }
     87 
     88 #ifndef NDEBUG
     89   for (SetVector<BasicBlock *>::iterator I = llvm::next(Result.begin()),
     90                                          E = Result.end();
     91        I != E; ++I)
     92     for (pred_iterator PI = pred_begin(*I), PE = pred_end(*I);
     93          PI != PE; ++PI)
     94       assert(Result.count(*PI) &&
     95              "No blocks in this region may have entries from outside the region"
     96              " except for the first block!");
     97 #endif
     98 
     99   return Result;
    100 }
    101 
    102 /// \brief Helper to call buildExtractionBlockSet with an ArrayRef.
    103 static SetVector<BasicBlock *>
    104 buildExtractionBlockSet(ArrayRef<BasicBlock *> BBs) {
    105   return buildExtractionBlockSet(BBs.begin(), BBs.end());
    106 }
    107 
    108 /// \brief Helper to call buildExtractionBlockSet with a RegionNode.
    109 static SetVector<BasicBlock *>
    110 buildExtractionBlockSet(const RegionNode &RN) {
    111   if (!RN.isSubRegion())
    112     // Just a single BasicBlock.
    113     return buildExtractionBlockSet(RN.getNodeAs<BasicBlock>());
    114 
    115   const Region &R = *RN.getNodeAs<Region>();
    116 
    117   return buildExtractionBlockSet(R.block_begin(), R.block_end());
    118 }
    119 
    120 CodeExtractor::CodeExtractor(BasicBlock *BB, bool AggregateArgs)
    121   : DT(0), AggregateArgs(AggregateArgs||AggregateArgsOpt),
    122     Blocks(buildExtractionBlockSet(BB)), NumExitBlocks(~0U) {}
    123 
    124 CodeExtractor::CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT,
    125                              bool AggregateArgs)
    126   : DT(DT), AggregateArgs(AggregateArgs||AggregateArgsOpt),
    127     Blocks(buildExtractionBlockSet(BBs)), NumExitBlocks(~0U) {}
    128 
    129 CodeExtractor::CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs)
    130   : DT(&DT), AggregateArgs(AggregateArgs||AggregateArgsOpt),
    131     Blocks(buildExtractionBlockSet(L.getBlocks())), NumExitBlocks(~0U) {}
    132 
    133 CodeExtractor::CodeExtractor(DominatorTree &DT, const RegionNode &RN,
    134                              bool AggregateArgs)
    135   : DT(&DT), AggregateArgs(AggregateArgs||AggregateArgsOpt),
    136     Blocks(buildExtractionBlockSet(RN)), NumExitBlocks(~0U) {}
    137 
    138 /// definedInRegion - Return true if the specified value is defined in the
    139 /// extracted region.
    140 static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) {
    141   if (Instruction *I = dyn_cast<Instruction>(V))
    142     if (Blocks.count(I->getParent()))
    143       return true;
    144   return false;
    145 }
    146 
    147 /// definedInCaller - Return true if the specified value is defined in the
    148 /// function being code extracted, but not in the region being extracted.
    149 /// These values must be passed in as live-ins to the function.
    150 static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) {
    151   if (isa<Argument>(V)) return true;
    152   if (Instruction *I = dyn_cast<Instruction>(V))
    153     if (!Blocks.count(I->getParent()))
    154       return true;
    155   return false;
    156 }
    157 
    158 void CodeExtractor::findInputsOutputs(ValueSet &Inputs,
    159                                       ValueSet &Outputs) const {
    160   for (SetVector<BasicBlock *>::const_iterator I = Blocks.begin(),
    161                                                E = Blocks.end();
    162        I != E; ++I) {
    163     BasicBlock *BB = *I;
    164 
    165     // If a used value is defined outside the region, it's an input.  If an
    166     // instruction is used outside the region, it's an output.
    167     for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
    168          II != IE; ++II) {
    169       for (User::op_iterator OI = II->op_begin(), OE = II->op_end();
    170            OI != OE; ++OI)
    171         if (definedInCaller(Blocks, *OI))
    172           Inputs.insert(*OI);
    173 
    174       for (Value::use_iterator UI = II->use_begin(), UE = II->use_end();
    175            UI != UE; ++UI)
    176         if (!definedInRegion(Blocks, *UI)) {
    177           Outputs.insert(II);
    178           break;
    179         }
    180     }
    181   }
    182 }
    183 
    184 /// severSplitPHINodes - If a PHI node has multiple inputs from outside of the
    185 /// region, we need to split the entry block of the region so that the PHI node
    186 /// is easier to deal with.
    187 void CodeExtractor::severSplitPHINodes(BasicBlock *&Header) {
    188   unsigned NumPredsFromRegion = 0;
    189   unsigned NumPredsOutsideRegion = 0;
    190 
    191   if (Header != &Header->getParent()->getEntryBlock()) {
    192     PHINode *PN = dyn_cast<PHINode>(Header->begin());
    193     if (!PN) return;  // No PHI nodes.
    194 
    195     // If the header node contains any PHI nodes, check to see if there is more
    196     // than one entry from outside the region.  If so, we need to sever the
    197     // header block into two.
    198     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
    199       if (Blocks.count(PN->getIncomingBlock(i)))
    200         ++NumPredsFromRegion;
    201       else
    202         ++NumPredsOutsideRegion;
    203 
    204     // If there is one (or fewer) predecessor from outside the region, we don't
    205     // need to do anything special.
    206     if (NumPredsOutsideRegion <= 1) return;
    207   }
    208 
    209   // Otherwise, we need to split the header block into two pieces: one
    210   // containing PHI nodes merging values from outside of the region, and a
    211   // second that contains all of the code for the block and merges back any
    212   // incoming values from inside of the region.
    213   BasicBlock::iterator AfterPHIs = Header->getFirstNonPHI();
    214   BasicBlock *NewBB = Header->splitBasicBlock(AfterPHIs,
    215                                               Header->getName()+".ce");
    216 
    217   // We only want to code extract the second block now, and it becomes the new
    218   // header of the region.
    219   BasicBlock *OldPred = Header;
    220   Blocks.remove(OldPred);
    221   Blocks.insert(NewBB);
    222   Header = NewBB;
    223 
    224   // Okay, update dominator sets. The blocks that dominate the new one are the
    225   // blocks that dominate TIBB plus the new block itself.
    226   if (DT)
    227     DT->splitBlock(NewBB);
    228 
    229   // Okay, now we need to adjust the PHI nodes and any branches from within the
    230   // region to go to the new header block instead of the old header block.
    231   if (NumPredsFromRegion) {
    232     PHINode *PN = cast<PHINode>(OldPred->begin());
    233     // Loop over all of the predecessors of OldPred that are in the region,
    234     // changing them to branch to NewBB instead.
    235     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
    236       if (Blocks.count(PN->getIncomingBlock(i))) {
    237         TerminatorInst *TI = PN->getIncomingBlock(i)->getTerminator();
    238         TI->replaceUsesOfWith(OldPred, NewBB);
    239       }
    240 
    241     // Okay, everything within the region is now branching to the right block, we
    242     // just have to update the PHI nodes now, inserting PHI nodes into NewBB.
    243     for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
    244       PHINode *PN = cast<PHINode>(AfterPHIs);
    245       // Create a new PHI node in the new region, which has an incoming value
    246       // from OldPred of PN.
    247       PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion,
    248                                        PN->getName()+".ce", NewBB->begin());
    249       NewPN->addIncoming(PN, OldPred);
    250 
    251       // Loop over all of the incoming value in PN, moving them to NewPN if they
    252       // are from the extracted region.
    253       for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
    254         if (Blocks.count(PN->getIncomingBlock(i))) {
    255           NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i));
    256           PN->removeIncomingValue(i);
    257           --i;
    258         }
    259       }
    260     }
    261   }
    262 }
    263 
    264 void CodeExtractor::splitReturnBlocks() {
    265   for (SetVector<BasicBlock *>::iterator I = Blocks.begin(), E = Blocks.end();
    266        I != E; ++I)
    267     if (ReturnInst *RI = dyn_cast<ReturnInst>((*I)->getTerminator())) {
    268       BasicBlock *New = (*I)->splitBasicBlock(RI, (*I)->getName()+".ret");
    269       if (DT) {
    270         // Old dominates New. New node dominates all other nodes dominated
    271         // by Old.
    272         DomTreeNode *OldNode = DT->getNode(*I);
    273         SmallVector<DomTreeNode*, 8> Children;
    274         for (DomTreeNode::iterator DI = OldNode->begin(), DE = OldNode->end();
    275              DI != DE; ++DI)
    276           Children.push_back(*DI);
    277 
    278         DomTreeNode *NewNode = DT->addNewBlock(New, *I);
    279 
    280         for (SmallVector<DomTreeNode*, 8>::iterator I = Children.begin(),
    281                E = Children.end(); I != E; ++I)
    282           DT->changeImmediateDominator(*I, NewNode);
    283       }
    284     }
    285 }
    286 
    287 /// constructFunction - make a function based on inputs and outputs, as follows:
    288 /// f(in0, ..., inN, out0, ..., outN)
    289 ///
    290 Function *CodeExtractor::constructFunction(const ValueSet &inputs,
    291                                            const ValueSet &outputs,
    292                                            BasicBlock *header,
    293                                            BasicBlock *newRootNode,
    294                                            BasicBlock *newHeader,
    295                                            Function *oldFunction,
    296                                            Module *M) {
    297   DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
    298   DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
    299 
    300   // This function returns unsigned, outputs will go back by reference.
    301   switch (NumExitBlocks) {
    302   case 0:
    303   case 1: RetTy = Type::getVoidTy(header->getContext()); break;
    304   case 2: RetTy = Type::getInt1Ty(header->getContext()); break;
    305   default: RetTy = Type::getInt16Ty(header->getContext()); break;
    306   }
    307 
    308   std::vector<Type*> paramTy;
    309 
    310   // Add the types of the input values to the function's argument list
    311   for (ValueSet::const_iterator i = inputs.begin(), e = inputs.end();
    312        i != e; ++i) {
    313     const Value *value = *i;
    314     DEBUG(dbgs() << "value used in func: " << *value << "\n");
    315     paramTy.push_back(value->getType());
    316   }
    317 
    318   // Add the types of the output values to the function's argument list.
    319   for (ValueSet::const_iterator I = outputs.begin(), E = outputs.end();
    320        I != E; ++I) {
    321     DEBUG(dbgs() << "instr used in func: " << **I << "\n");
    322     if (AggregateArgs)
    323       paramTy.push_back((*I)->getType());
    324     else
    325       paramTy.push_back(PointerType::getUnqual((*I)->getType()));
    326   }
    327 
    328   DEBUG(dbgs() << "Function type: " << *RetTy << " f(");
    329   for (std::vector<Type*>::iterator i = paramTy.begin(),
    330          e = paramTy.end(); i != e; ++i)
    331     DEBUG(dbgs() << **i << ", ");
    332   DEBUG(dbgs() << ")\n");
    333 
    334   if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
    335     PointerType *StructPtr =
    336            PointerType::getUnqual(StructType::get(M->getContext(), paramTy));
    337     paramTy.clear();
    338     paramTy.push_back(StructPtr);
    339   }
    340   FunctionType *funcType =
    341                   FunctionType::get(RetTy, paramTy, false);
    342 
    343   // Create the new function
    344   Function *newFunction = Function::Create(funcType,
    345                                            GlobalValue::InternalLinkage,
    346                                            oldFunction->getName() + "_" +
    347                                            header->getName(), M);
    348   // If the old function is no-throw, so is the new one.
    349   if (oldFunction->doesNotThrow())
    350     newFunction->setDoesNotThrow();
    351 
    352   newFunction->getBasicBlockList().push_back(newRootNode);
    353 
    354   // Create an iterator to name all of the arguments we inserted.
    355   Function::arg_iterator AI = newFunction->arg_begin();
    356 
    357   // Rewrite all users of the inputs in the extracted region to use the
    358   // arguments (or appropriate addressing into struct) instead.
    359   for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
    360     Value *RewriteVal;
    361     if (AggregateArgs) {
    362       Value *Idx[2];
    363       Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext()));
    364       Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i);
    365       TerminatorInst *TI = newFunction->begin()->getTerminator();
    366       GetElementPtrInst *GEP =
    367         GetElementPtrInst::Create(AI, Idx, "gep_" + inputs[i]->getName(), TI);
    368       RewriteVal = new LoadInst(GEP, "loadgep_" + inputs[i]->getName(), TI);
    369     } else
    370       RewriteVal = AI++;
    371 
    372     std::vector<User*> Users(inputs[i]->use_begin(), inputs[i]->use_end());
    373     for (std::vector<User*>::iterator use = Users.begin(), useE = Users.end();
    374          use != useE; ++use)
    375       if (Instruction* inst = dyn_cast<Instruction>(*use))
    376         if (Blocks.count(inst->getParent()))
    377           inst->replaceUsesOfWith(inputs[i], RewriteVal);
    378   }
    379 
    380   // Set names for input and output arguments.
    381   if (!AggregateArgs) {
    382     AI = newFunction->arg_begin();
    383     for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI)
    384       AI->setName(inputs[i]->getName());
    385     for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI)
    386       AI->setName(outputs[i]->getName()+".out");
    387   }
    388 
    389   // Rewrite branches to basic blocks outside of the loop to new dummy blocks
    390   // within the new function. This must be done before we lose track of which
    391   // blocks were originally in the code region.
    392   std::vector<User*> Users(header->use_begin(), header->use_end());
    393   for (unsigned i = 0, e = Users.size(); i != e; ++i)
    394     // The BasicBlock which contains the branch is not in the region
    395     // modify the branch target to a new block
    396     if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Users[i]))
    397       if (!Blocks.count(TI->getParent()) &&
    398           TI->getParent()->getParent() == oldFunction)
    399         TI->replaceUsesOfWith(header, newHeader);
    400 
    401   return newFunction;
    402 }
    403 
    404 /// FindPhiPredForUseInBlock - Given a value and a basic block, find a PHI
    405 /// that uses the value within the basic block, and return the predecessor
    406 /// block associated with that use, or return 0 if none is found.
    407 static BasicBlock* FindPhiPredForUseInBlock(Value* Used, BasicBlock* BB) {
    408   for (Value::use_iterator UI = Used->use_begin(),
    409        UE = Used->use_end(); UI != UE; ++UI) {
    410      PHINode *P = dyn_cast<PHINode>(*UI);
    411      if (P && P->getParent() == BB)
    412        return P->getIncomingBlock(UI);
    413   }
    414 
    415   return 0;
    416 }
    417 
    418 /// emitCallAndSwitchStatement - This method sets up the caller side by adding
    419 /// the call instruction, splitting any PHI nodes in the header block as
    420 /// necessary.
    421 void CodeExtractor::
    422 emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
    423                            ValueSet &inputs, ValueSet &outputs) {
    424   // Emit a call to the new function, passing in: *pointer to struct (if
    425   // aggregating parameters), or plan inputs and allocated memory for outputs
    426   std::vector<Value*> params, StructValues, ReloadOutputs, Reloads;
    427 
    428   LLVMContext &Context = newFunction->getContext();
    429 
    430   // Add inputs as params, or to be filled into the struct
    431   for (ValueSet::iterator i = inputs.begin(), e = inputs.end(); i != e; ++i)
    432     if (AggregateArgs)
    433       StructValues.push_back(*i);
    434     else
    435       params.push_back(*i);
    436 
    437   // Create allocas for the outputs
    438   for (ValueSet::iterator i = outputs.begin(), e = outputs.end(); i != e; ++i) {
    439     if (AggregateArgs) {
    440       StructValues.push_back(*i);
    441     } else {
    442       AllocaInst *alloca =
    443         new AllocaInst((*i)->getType(), 0, (*i)->getName()+".loc",
    444                        codeReplacer->getParent()->begin()->begin());
    445       ReloadOutputs.push_back(alloca);
    446       params.push_back(alloca);
    447     }
    448   }
    449 
    450   AllocaInst *Struct = 0;
    451   if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
    452     std::vector<Type*> ArgTypes;
    453     for (ValueSet::iterator v = StructValues.begin(),
    454            ve = StructValues.end(); v != ve; ++v)
    455       ArgTypes.push_back((*v)->getType());
    456 
    457     // Allocate a struct at the beginning of this function
    458     Type *StructArgTy = StructType::get(newFunction->getContext(), ArgTypes);
    459     Struct =
    460       new AllocaInst(StructArgTy, 0, "structArg",
    461                      codeReplacer->getParent()->begin()->begin());
    462     params.push_back(Struct);
    463 
    464     for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
    465       Value *Idx[2];
    466       Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
    467       Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i);
    468       GetElementPtrInst *GEP =
    469         GetElementPtrInst::Create(Struct, Idx,
    470                                   "gep_" + StructValues[i]->getName());
    471       codeReplacer->getInstList().push_back(GEP);
    472       StoreInst *SI = new StoreInst(StructValues[i], GEP);
    473       codeReplacer->getInstList().push_back(SI);
    474     }
    475   }
    476 
    477   // Emit the call to the function
    478   CallInst *call = CallInst::Create(newFunction, params,
    479                                     NumExitBlocks > 1 ? "targetBlock" : "");
    480   codeReplacer->getInstList().push_back(call);
    481 
    482   Function::arg_iterator OutputArgBegin = newFunction->arg_begin();
    483   unsigned FirstOut = inputs.size();
    484   if (!AggregateArgs)
    485     std::advance(OutputArgBegin, inputs.size());
    486 
    487   // Reload the outputs passed in by reference
    488   for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
    489     Value *Output = 0;
    490     if (AggregateArgs) {
    491       Value *Idx[2];
    492       Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
    493       Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i);
    494       GetElementPtrInst *GEP
    495         = GetElementPtrInst::Create(Struct, Idx,
    496                                     "gep_reload_" + outputs[i]->getName());
    497       codeReplacer->getInstList().push_back(GEP);
    498       Output = GEP;
    499     } else {
    500       Output = ReloadOutputs[i];
    501     }
    502     LoadInst *load = new LoadInst(Output, outputs[i]->getName()+".reload");
    503     Reloads.push_back(load);
    504     codeReplacer->getInstList().push_back(load);
    505     std::vector<User*> Users(outputs[i]->use_begin(), outputs[i]->use_end());
    506     for (unsigned u = 0, e = Users.size(); u != e; ++u) {
    507       Instruction *inst = cast<Instruction>(Users[u]);
    508       if (!Blocks.count(inst->getParent()))
    509         inst->replaceUsesOfWith(outputs[i], load);
    510     }
    511   }
    512 
    513   // Now we can emit a switch statement using the call as a value.
    514   SwitchInst *TheSwitch =
    515       SwitchInst::Create(Constant::getNullValue(Type::getInt16Ty(Context)),
    516                          codeReplacer, 0, codeReplacer);
    517 
    518   // Since there may be multiple exits from the original region, make the new
    519   // function return an unsigned, switch on that number.  This loop iterates
    520   // over all of the blocks in the extracted region, updating any terminator
    521   // instructions in the to-be-extracted region that branch to blocks that are
    522   // not in the region to be extracted.
    523   std::map<BasicBlock*, BasicBlock*> ExitBlockMap;
    524 
    525   unsigned switchVal = 0;
    526   for (SetVector<BasicBlock*>::const_iterator i = Blocks.begin(),
    527          e = Blocks.end(); i != e; ++i) {
    528     TerminatorInst *TI = (*i)->getTerminator();
    529     for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
    530       if (!Blocks.count(TI->getSuccessor(i))) {
    531         BasicBlock *OldTarget = TI->getSuccessor(i);
    532         // add a new basic block which returns the appropriate value
    533         BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
    534         if (!NewTarget) {
    535           // If we don't already have an exit stub for this non-extracted
    536           // destination, create one now!
    537           NewTarget = BasicBlock::Create(Context,
    538                                          OldTarget->getName() + ".exitStub",
    539                                          newFunction);
    540           unsigned SuccNum = switchVal++;
    541 
    542           Value *brVal = 0;
    543           switch (NumExitBlocks) {
    544           case 0:
    545           case 1: break;  // No value needed.
    546           case 2:         // Conditional branch, return a bool
    547             brVal = ConstantInt::get(Type::getInt1Ty(Context), !SuccNum);
    548             break;
    549           default:
    550             brVal = ConstantInt::get(Type::getInt16Ty(Context), SuccNum);
    551             break;
    552           }
    553 
    554           ReturnInst *NTRet = ReturnInst::Create(Context, brVal, NewTarget);
    555 
    556           // Update the switch instruction.
    557           TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context),
    558                                               SuccNum),
    559                              OldTarget);
    560 
    561           // Restore values just before we exit
    562           Function::arg_iterator OAI = OutputArgBegin;
    563           for (unsigned out = 0, e = outputs.size(); out != e; ++out) {
    564             // For an invoke, the normal destination is the only one that is
    565             // dominated by the result of the invocation
    566             BasicBlock *DefBlock = cast<Instruction>(outputs[out])->getParent();
    567 
    568             bool DominatesDef = true;
    569 
    570             if (InvokeInst *Invoke = dyn_cast<InvokeInst>(outputs[out])) {
    571               DefBlock = Invoke->getNormalDest();
    572 
    573               // Make sure we are looking at the original successor block, not
    574               // at a newly inserted exit block, which won't be in the dominator
    575               // info.
    576               for (std::map<BasicBlock*, BasicBlock*>::iterator I =
    577                      ExitBlockMap.begin(), E = ExitBlockMap.end(); I != E; ++I)
    578                 if (DefBlock == I->second) {
    579                   DefBlock = I->first;
    580                   break;
    581                 }
    582 
    583               // In the extract block case, if the block we are extracting ends
    584               // with an invoke instruction, make sure that we don't emit a
    585               // store of the invoke value for the unwind block.
    586               if (!DT && DefBlock != OldTarget)
    587                 DominatesDef = false;
    588             }
    589 
    590             if (DT) {
    591               DominatesDef = DT->dominates(DefBlock, OldTarget);
    592 
    593               // If the output value is used by a phi in the target block,
    594               // then we need to test for dominance of the phi's predecessor
    595               // instead.  Unfortunately, this a little complicated since we
    596               // have already rewritten uses of the value to uses of the reload.
    597               BasicBlock* pred = FindPhiPredForUseInBlock(Reloads[out],
    598                                                           OldTarget);
    599               if (pred && DT && DT->dominates(DefBlock, pred))
    600                 DominatesDef = true;
    601             }
    602 
    603             if (DominatesDef) {
    604               if (AggregateArgs) {
    605                 Value *Idx[2];
    606                 Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
    607                 Idx[1] = ConstantInt::get(Type::getInt32Ty(Context),
    608                                           FirstOut+out);
    609                 GetElementPtrInst *GEP =
    610                   GetElementPtrInst::Create(OAI, Idx,
    611                                             "gep_" + outputs[out]->getName(),
    612                                             NTRet);
    613                 new StoreInst(outputs[out], GEP, NTRet);
    614               } else {
    615                 new StoreInst(outputs[out], OAI, NTRet);
    616               }
    617             }
    618             // Advance output iterator even if we don't emit a store
    619             if (!AggregateArgs) ++OAI;
    620           }
    621         }
    622 
    623         // rewrite the original branch instruction with this new target
    624         TI->setSuccessor(i, NewTarget);
    625       }
    626   }
    627 
    628   // Now that we've done the deed, simplify the switch instruction.
    629   Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType();
    630   switch (NumExitBlocks) {
    631   case 0:
    632     // There are no successors (the block containing the switch itself), which
    633     // means that previously this was the last part of the function, and hence
    634     // this should be rewritten as a `ret'
    635 
    636     // Check if the function should return a value
    637     if (OldFnRetTy->isVoidTy()) {
    638       ReturnInst::Create(Context, 0, TheSwitch);  // Return void
    639     } else if (OldFnRetTy == TheSwitch->getCondition()->getType()) {
    640       // return what we have
    641       ReturnInst::Create(Context, TheSwitch->getCondition(), TheSwitch);
    642     } else {
    643       // Otherwise we must have code extracted an unwind or something, just
    644       // return whatever we want.
    645       ReturnInst::Create(Context,
    646                          Constant::getNullValue(OldFnRetTy), TheSwitch);
    647     }
    648 
    649     TheSwitch->eraseFromParent();
    650     break;
    651   case 1:
    652     // Only a single destination, change the switch into an unconditional
    653     // branch.
    654     BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch);
    655     TheSwitch->eraseFromParent();
    656     break;
    657   case 2:
    658     BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2),
    659                        call, TheSwitch);
    660     TheSwitch->eraseFromParent();
    661     break;
    662   default:
    663     // Otherwise, make the default destination of the switch instruction be one
    664     // of the other successors.
    665     TheSwitch->setCondition(call);
    666     TheSwitch->setDefaultDest(TheSwitch->getSuccessor(NumExitBlocks));
    667     // Remove redundant case
    668     SwitchInst::CaseIt ToBeRemoved(TheSwitch, NumExitBlocks-1);
    669     TheSwitch->removeCase(ToBeRemoved);
    670     break;
    671   }
    672 }
    673 
    674 void CodeExtractor::moveCodeToFunction(Function *newFunction) {
    675   Function *oldFunc = (*Blocks.begin())->getParent();
    676   Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList();
    677   Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList();
    678 
    679   for (SetVector<BasicBlock*>::const_iterator i = Blocks.begin(),
    680          e = Blocks.end(); i != e; ++i) {
    681     // Delete the basic block from the old function, and the list of blocks
    682     oldBlocks.remove(*i);
    683 
    684     // Insert this basic block into the new function
    685     newBlocks.push_back(*i);
    686   }
    687 }
    688 
    689 Function *CodeExtractor::extractCodeRegion() {
    690   if (!isEligible())
    691     return 0;
    692 
    693   ValueSet inputs, outputs;
    694 
    695   // Assumption: this is a single-entry code region, and the header is the first
    696   // block in the region.
    697   BasicBlock *header = *Blocks.begin();
    698 
    699   // If we have to split PHI nodes or the entry block, do so now.
    700   severSplitPHINodes(header);
    701 
    702   // If we have any return instructions in the region, split those blocks so
    703   // that the return is not in the region.
    704   splitReturnBlocks();
    705 
    706   Function *oldFunction = header->getParent();
    707 
    708   // This takes place of the original loop
    709   BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(),
    710                                                 "codeRepl", oldFunction,
    711                                                 header);
    712 
    713   // The new function needs a root node because other nodes can branch to the
    714   // head of the region, but the entry node of a function cannot have preds.
    715   BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(),
    716                                                "newFuncRoot");
    717   newFuncRoot->getInstList().push_back(BranchInst::Create(header));
    718 
    719   // Find inputs to, outputs from the code region.
    720   findInputsOutputs(inputs, outputs);
    721 
    722   SmallPtrSet<BasicBlock *, 1> ExitBlocks;
    723   for (SetVector<BasicBlock *>::iterator I = Blocks.begin(), E = Blocks.end();
    724        I != E; ++I)
    725     for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
    726       if (!Blocks.count(*SI))
    727         ExitBlocks.insert(*SI);
    728   NumExitBlocks = ExitBlocks.size();
    729 
    730   // Construct new function based on inputs/outputs & add allocas for all defs.
    731   Function *newFunction = constructFunction(inputs, outputs, header,
    732                                             newFuncRoot,
    733                                             codeReplacer, oldFunction,
    734                                             oldFunction->getParent());
    735 
    736   emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
    737 
    738   moveCodeToFunction(newFunction);
    739 
    740   // Loop over all of the PHI nodes in the header block, and change any
    741   // references to the old incoming edge to be the new incoming edge.
    742   for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) {
    743     PHINode *PN = cast<PHINode>(I);
    744     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
    745       if (!Blocks.count(PN->getIncomingBlock(i)))
    746         PN->setIncomingBlock(i, newFuncRoot);
    747   }
    748 
    749   // Look at all successors of the codeReplacer block.  If any of these blocks
    750   // had PHI nodes in them, we need to update the "from" block to be the code
    751   // replacer, not the original block in the extracted region.
    752   std::vector<BasicBlock*> Succs(succ_begin(codeReplacer),
    753                                  succ_end(codeReplacer));
    754   for (unsigned i = 0, e = Succs.size(); i != e; ++i)
    755     for (BasicBlock::iterator I = Succs[i]->begin(); isa<PHINode>(I); ++I) {
    756       PHINode *PN = cast<PHINode>(I);
    757       std::set<BasicBlock*> ProcessedPreds;
    758       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
    759         if (Blocks.count(PN->getIncomingBlock(i))) {
    760           if (ProcessedPreds.insert(PN->getIncomingBlock(i)).second)
    761             PN->setIncomingBlock(i, codeReplacer);
    762           else {
    763             // There were multiple entries in the PHI for this block, now there
    764             // is only one, so remove the duplicated entries.
    765             PN->removeIncomingValue(i, false);
    766             --i; --e;
    767           }
    768         }
    769     }
    770 
    771   //cerr << "NEW FUNCTION: " << *newFunction;
    772   //  verifyFunction(*newFunction);
    773 
    774   //  cerr << "OLD FUNCTION: " << *oldFunction;
    775   //  verifyFunction(*oldFunction);
    776 
    777   DEBUG(if (verifyFunction(*newFunction))
    778         report_fatal_error("verifyFunction failed!"));
    779   return newFunction;
    780 }
    781