Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 1 | //===- CodeExtractor.cpp - Pull code region into a new function -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
Chris Lattner | cee3404 | 2004-03-18 03:15:29 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/FunctionUtils.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Pass.h" |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 22 | #include "llvm/Analysis/Dominators.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 3684469 | 2004-03-14 03:17:22 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/Verifier.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 26 | #include "Support/Debug.h" |
| 27 | #include "Support/StringExtras.h" |
| 28 | #include <algorithm> |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 29 | #include <set> |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 34 | /// getFunctionArg - Return a pointer to F's ARGNOth argument. |
| 35 | /// |
| 36 | Argument *getFunctionArg(Function *F, unsigned argno) { |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 37 | Function::aiterator I = F->abegin(); |
| 38 | std::advance(I, argno); |
| 39 | return I; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 42 | class CodeExtractor { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 43 | typedef std::vector<Value*> Values; |
| 44 | typedef std::vector<std::pair<unsigned, unsigned> > PhiValChangesTy; |
| 45 | typedef std::map<PHINode*, PhiValChangesTy> PhiVal2ArgTy; |
| 46 | PhiVal2ArgTy PhiVal2Arg; |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 47 | std::set<BasicBlock*> BlocksToExtract; |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 48 | DominatorSet *DS; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 49 | public: |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 50 | CodeExtractor(DominatorSet *ds = 0) : DS(ds) {} |
| 51 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 52 | Function *ExtractCodeRegion(const std::vector<BasicBlock*> &code); |
| 53 | |
| 54 | private: |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 55 | void findInputsOutputs(Values &inputs, Values &outputs, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 56 | BasicBlock *newHeader, |
| 57 | BasicBlock *newRootNode); |
| 58 | |
| 59 | void processPhiNodeInputs(PHINode *Phi, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 60 | Values &inputs, |
| 61 | BasicBlock *newHeader, |
| 62 | BasicBlock *newRootNode); |
| 63 | |
| 64 | void rewritePhiNodes(Function *F, BasicBlock *newFuncRoot); |
| 65 | |
| 66 | Function *constructFunction(const Values &inputs, |
| 67 | const Values &outputs, |
| 68 | BasicBlock *newRootNode, BasicBlock *newHeader, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 69 | Function *oldFunction, Module *M); |
| 70 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 71 | void moveCodeToFunction(Function *newFunction); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 72 | |
| 73 | void emitCallAndSwitchStatement(Function *newFunction, |
| 74 | BasicBlock *newHeader, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 75 | Values &inputs, |
| 76 | Values &outputs); |
| 77 | |
| 78 | }; |
| 79 | } |
| 80 | |
| 81 | void CodeExtractor::processPhiNodeInputs(PHINode *Phi, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 82 | Values &inputs, |
| 83 | BasicBlock *codeReplacer, |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 84 | BasicBlock *newFuncRoot) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 85 | // Separate incoming values and BasicBlocks as internal/external. We ignore |
| 86 | // the case where both the value and BasicBlock are internal, because we don't |
| 87 | // need to do a thing. |
| 88 | std::vector<unsigned> EValEBB; |
| 89 | std::vector<unsigned> EValIBB; |
| 90 | std::vector<unsigned> IValEBB; |
| 91 | |
| 92 | for (unsigned i = 0, e = Phi->getNumIncomingValues(); i != e; ++i) { |
| 93 | Value *phiVal = Phi->getIncomingValue(i); |
| 94 | if (Instruction *Inst = dyn_cast<Instruction>(phiVal)) { |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 95 | if (BlocksToExtract.count(Inst->getParent())) { |
| 96 | if (!BlocksToExtract.count(Phi->getIncomingBlock(i))) |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 97 | IValEBB.push_back(i); |
| 98 | } else { |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 99 | if (BlocksToExtract.count(Phi->getIncomingBlock(i))) |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 100 | EValIBB.push_back(i); |
| 101 | else |
| 102 | EValEBB.push_back(i); |
| 103 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 104 | } else if (Argument *Arg = dyn_cast<Argument>(phiVal)) { |
| 105 | // arguments are external |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 106 | if (BlocksToExtract.count(Phi->getIncomingBlock(i))) |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 107 | EValIBB.push_back(i); |
| 108 | else |
| 109 | EValEBB.push_back(i); |
| 110 | } else { |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 111 | // Globals/Constants are internal, but considered `external' if they are |
| 112 | // coming from an external block. |
| 113 | if (!BlocksToExtract.count(Phi->getIncomingBlock(i))) |
| 114 | EValEBB.push_back(i); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 118 | // Both value and block are external. Need to group all of these, have an |
| 119 | // external phi, pass the result as an argument, and have THIS phi use that |
| 120 | // result. |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 121 | if (EValEBB.size() > 0) { |
| 122 | if (EValEBB.size() == 1) { |
| 123 | // Now if it's coming from the newFuncRoot, it's that funky input |
| 124 | unsigned phiIdx = EValEBB[0]; |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 125 | if (!isa<Constant>(Phi->getIncomingValue(phiIdx))) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 126 | PhiVal2Arg[Phi].push_back(std::make_pair(phiIdx, inputs.size())); |
| 127 | // We can just pass this value in as argument |
| 128 | inputs.push_back(Phi->getIncomingValue(phiIdx)); |
| 129 | } |
| 130 | Phi->setIncomingBlock(phiIdx, newFuncRoot); |
| 131 | } else { |
| 132 | PHINode *externalPhi = new PHINode(Phi->getType(), "extPhi"); |
| 133 | codeReplacer->getInstList().insert(codeReplacer->begin(), externalPhi); |
| 134 | for (std::vector<unsigned>::iterator i = EValEBB.begin(), |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 135 | e = EValEBB.end(); i != e; ++i) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 136 | externalPhi->addIncoming(Phi->getIncomingValue(*i), |
| 137 | Phi->getIncomingBlock(*i)); |
| 138 | |
| 139 | // We make these values invalid instead of deleting them because that |
| 140 | // would shift the indices of other values... The fixPhiNodes should |
| 141 | // clean these phi nodes up later. |
| 142 | Phi->setIncomingValue(*i, 0); |
| 143 | Phi->setIncomingBlock(*i, 0); |
| 144 | } |
| 145 | PhiVal2Arg[Phi].push_back(std::make_pair(Phi->getNumIncomingValues(), |
| 146 | inputs.size())); |
| 147 | // We can just pass this value in as argument |
| 148 | inputs.push_back(externalPhi); |
| 149 | } |
| 150 | } |
| 151 | |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 152 | // When the value is external, but block internal... just pass it in as |
| 153 | // argument, no change to phi node |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 154 | for (std::vector<unsigned>::iterator i = EValIBB.begin(), |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 155 | e = EValIBB.end(); i != e; ++i) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 156 | // rewrite the phi input node to be an argument |
| 157 | PhiVal2Arg[Phi].push_back(std::make_pair(*i, inputs.size())); |
| 158 | inputs.push_back(Phi->getIncomingValue(*i)); |
| 159 | } |
| 160 | |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 161 | // Value internal, block external this can happen if we are extracting a part |
| 162 | // of a loop. |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 163 | for (std::vector<unsigned>::iterator i = IValEBB.begin(), |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 164 | e = IValEBB.end(); i != e; ++i) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 165 | assert(0 && "Cannot (YET) handle internal values via external blocks"); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 170 | void CodeExtractor::findInputsOutputs(Values &inputs, Values &outputs, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 171 | BasicBlock *newHeader, |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 172 | BasicBlock *newRootNode) { |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 173 | for (std::set<BasicBlock*>::const_iterator ci = BlocksToExtract.begin(), |
| 174 | ce = BlocksToExtract.end(); ci != ce; ++ci) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 175 | BasicBlock *BB = *ci; |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 176 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 177 | // If a used value is defined outside the region, it's an input. If an |
| 178 | // instruction is used outside the region, it's an output. |
| 179 | if (PHINode *Phi = dyn_cast<PHINode>(I)) { |
| 180 | processPhiNodeInputs(Phi, inputs, newHeader, newRootNode); |
| 181 | } else { |
| 182 | // All other instructions go through the generic input finder |
| 183 | // Loop over the operands of each instruction (inputs) |
| 184 | for (User::op_iterator op = I->op_begin(), opE = I->op_end(); |
| 185 | op != opE; ++op) |
| 186 | if (Instruction *opI = dyn_cast<Instruction>(*op)) { |
| 187 | // Check if definition of this operand is within the loop |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 188 | if (!BlocksToExtract.count(opI->getParent())) |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 189 | inputs.push_back(opI); |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 190 | } else if (isa<Argument>(*op)) { |
| 191 | inputs.push_back(*op); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 192 | } |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Consider uses of this instruction (outputs) |
| 196 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 197 | UI != E; ++UI) |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 198 | if (!BlocksToExtract.count(cast<Instruction>(*UI)->getParent())) { |
| 199 | outputs.push_back(I); |
| 200 | break; |
| 201 | } |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 202 | } // for: insts |
| 203 | } // for: basic blocks |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void CodeExtractor::rewritePhiNodes(Function *F, |
| 207 | BasicBlock *newFuncRoot) { |
| 208 | // Write any changes that were saved before: use function arguments as inputs |
| 209 | for (PhiVal2ArgTy::iterator i = PhiVal2Arg.begin(), e = PhiVal2Arg.end(); |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 210 | i != e; ++i) { |
| 211 | PHINode *phi = i->first; |
| 212 | PhiValChangesTy &values = i->second; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 213 | for (unsigned cIdx = 0, ce = values.size(); cIdx != ce; ++cIdx) |
| 214 | { |
| 215 | unsigned phiValueIdx = values[cIdx].first, argNum = values[cIdx].second; |
| 216 | if (phiValueIdx < phi->getNumIncomingValues()) |
| 217 | phi->setIncomingValue(phiValueIdx, getFunctionArg(F, argNum)); |
| 218 | else |
| 219 | phi->addIncoming(getFunctionArg(F, argNum), newFuncRoot); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // Delete any invalid Phi node inputs that were marked as NULL previously |
| 224 | for (PhiVal2ArgTy::iterator i = PhiVal2Arg.begin(), e = PhiVal2Arg.end(); |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 225 | i != e; ++i) { |
| 226 | PHINode *phi = i->first; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 227 | for (unsigned idx = 0, end = phi->getNumIncomingValues(); idx != end; ++idx) |
| 228 | { |
| 229 | if (phi->getIncomingValue(idx) == 0 && phi->getIncomingBlock(idx) == 0) { |
| 230 | phi->removeIncomingValue(idx); |
| 231 | --idx; |
| 232 | --end; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // We are done with the saved values |
| 238 | PhiVal2Arg.clear(); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /// constructFunction - make a function based on inputs and outputs, as follows: |
| 243 | /// f(in0, ..., inN, out0, ..., outN) |
| 244 | /// |
| 245 | Function *CodeExtractor::constructFunction(const Values &inputs, |
| 246 | const Values &outputs, |
| 247 | BasicBlock *newRootNode, |
| 248 | BasicBlock *newHeader, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 249 | Function *oldFunction, Module *M) { |
| 250 | DEBUG(std::cerr << "inputs: " << inputs.size() << "\n"); |
| 251 | DEBUG(std::cerr << "outputs: " << outputs.size() << "\n"); |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 252 | BasicBlock *header = *BlocksToExtract.begin(); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 253 | |
| 254 | // This function returns unsigned, outputs will go back by reference. |
| 255 | Type *retTy = Type::UShortTy; |
| 256 | std::vector<const Type*> paramTy; |
| 257 | |
| 258 | // Add the types of the input values to the function's argument list |
| 259 | for (Values::const_iterator i = inputs.begin(), |
| 260 | e = inputs.end(); i != e; ++i) { |
| 261 | const Value *value = *i; |
| 262 | DEBUG(std::cerr << "value used in func: " << value << "\n"); |
| 263 | paramTy.push_back(value->getType()); |
| 264 | } |
| 265 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 266 | // Add the types of the output values to the function's argument list. |
| 267 | for (Values::const_iterator I = outputs.begin(), E = outputs.end(); |
| 268 | I != E; ++I) { |
| 269 | DEBUG(std::cerr << "instr used in func: " << *I << "\n"); |
| 270 | paramTy.push_back(PointerType::get((*I)->getType())); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | DEBUG(std::cerr << "Function type: " << retTy << " f("); |
| 274 | for (std::vector<const Type*>::iterator i = paramTy.begin(), |
| 275 | e = paramTy.end(); i != e; ++i) |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 276 | DEBUG(std::cerr << *i << ", "); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 277 | DEBUG(std::cerr << ")\n"); |
| 278 | |
| 279 | const FunctionType *funcType = FunctionType::get(retTy, paramTy, false); |
| 280 | |
| 281 | // Create the new function |
| 282 | Function *newFunction = new Function(funcType, |
| 283 | GlobalValue::InternalLinkage, |
| 284 | oldFunction->getName() + "_code", M); |
| 285 | newFunction->getBasicBlockList().push_back(newRootNode); |
| 286 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 287 | // Create an iterator to name all of the arguments we inserted. |
| 288 | Function::aiterator AI = newFunction->abegin(); |
| 289 | |
| 290 | // Rewrite all users of the inputs in the extracted region to use the |
| 291 | // arguments instead. |
| 292 | for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI) { |
| 293 | AI->setName(inputs[i]->getName()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 294 | std::vector<User*> Users(inputs[i]->use_begin(), inputs[i]->use_end()); |
| 295 | for (std::vector<User*>::iterator use = Users.begin(), useE = Users.end(); |
Chris Lattner | 3684469 | 2004-03-14 03:17:22 +0000 | [diff] [blame] | 296 | use != useE; ++use) |
| 297 | if (Instruction* inst = dyn_cast<Instruction>(*use)) |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 298 | if (BlocksToExtract.count(inst->getParent())) |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 299 | inst->replaceUsesOfWith(inputs[i], AI); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 302 | // Set names for all of the output arguments. |
| 303 | for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI) |
| 304 | AI->setName(outputs[i]->getName()+".out"); |
| 305 | |
| 306 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 307 | // Rewrite branches to basic blocks outside of the loop to new dummy blocks |
| 308 | // within the new function. This must be done before we lose track of which |
| 309 | // blocks were originally in the code region. |
| 310 | std::vector<User*> Users(header->use_begin(), header->use_end()); |
| 311 | for (std::vector<User*>::iterator i = Users.begin(), e = Users.end(); |
| 312 | i != e; ++i) { |
| 313 | if (BranchInst *inst = dyn_cast<BranchInst>(*i)) { |
| 314 | BasicBlock *BB = inst->getParent(); |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 315 | if (!BlocksToExtract.count(BB) && BB->getParent() == oldFunction) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 316 | // The BasicBlock which contains the branch is not in the region |
| 317 | // modify the branch target to a new block |
| 318 | inst->replaceUsesOfWith(header, newHeader); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return newFunction; |
| 324 | } |
| 325 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 326 | void CodeExtractor::moveCodeToFunction(Function *newFunction) { |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 327 | Function *oldFunc = (*BlocksToExtract.begin())->getParent(); |
Chris Lattner | 4fca71e | 2004-03-14 04:01:47 +0000 | [diff] [blame] | 328 | Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList(); |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 329 | Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList(); |
Chris Lattner | 4fca71e | 2004-03-14 04:01:47 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 331 | for (std::set<BasicBlock*>::const_iterator i = BlocksToExtract.begin(), |
| 332 | e = BlocksToExtract.end(); i != e; ++i) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 333 | // Delete the basic block from the old function, and the list of blocks |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 334 | oldBlocks.remove(*i); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 335 | |
| 336 | // Insert this basic block into the new function |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 337 | newBlocks.push_back(*i); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | CodeExtractor::emitCallAndSwitchStatement(Function *newFunction, |
| 343 | BasicBlock *codeReplacer, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 344 | Values &inputs, |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 345 | Values &outputs) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 346 | // Emit a call to the new function, passing allocated memory for outputs and |
| 347 | // just plain inputs for non-scalars |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 348 | std::vector<Value*> params(inputs); |
| 349 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 350 | for (unsigned i = 0, e = outputs.size(); i != e; ++i) { |
| 351 | Value *Output = outputs[i]; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 352 | // Create allocas for scalar outputs |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 353 | AllocaInst *alloca = |
| 354 | new AllocaInst(outputs[i]->getType(), 0, Output->getName()+".loc", |
| 355 | codeReplacer->getParent()->begin()->begin()); |
| 356 | params.push_back(alloca); |
| 357 | |
| 358 | LoadInst *load = new LoadInst(alloca, Output->getName()+".reload"); |
| 359 | codeReplacer->getInstList().push_back(load); |
| 360 | std::vector<User*> Users(outputs[i]->use_begin(), outputs[i]->use_end()); |
| 361 | for (unsigned u = 0, e = Users.size(); u != e; ++u) { |
| 362 | Instruction *inst = cast<Instruction>(Users[u]); |
| 363 | if (!BlocksToExtract.count(inst->getParent())) |
| 364 | inst->replaceUsesOfWith(outputs[i], load); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 367 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 368 | CallInst *call = new CallInst(newFunction, params, "targetBlock"); |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 369 | codeReplacer->getInstList().push_front(call); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 370 | |
| 371 | // Now we can emit a switch statement using the call as a value. |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 372 | SwitchInst *TheSwitch = new SwitchInst(call, codeReplacer, codeReplacer); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 373 | |
| 374 | // Since there may be multiple exits from the original region, make the new |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 375 | // function return an unsigned, switch on that number. This loop iterates |
| 376 | // over all of the blocks in the extracted region, updating any terminator |
| 377 | // instructions in the to-be-extracted region that branch to blocks that are |
| 378 | // not in the region to be extracted. |
| 379 | std::map<BasicBlock*, BasicBlock*> ExitBlockMap; |
| 380 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 381 | unsigned switchVal = 0; |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 382 | for (std::set<BasicBlock*>::const_iterator i = BlocksToExtract.begin(), |
| 383 | e = BlocksToExtract.end(); i != e; ++i) { |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 384 | TerminatorInst *TI = (*i)->getTerminator(); |
| 385 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 386 | if (!BlocksToExtract.count(TI->getSuccessor(i))) { |
| 387 | BasicBlock *OldTarget = TI->getSuccessor(i); |
| 388 | // add a new basic block which returns the appropriate value |
| 389 | BasicBlock *&NewTarget = ExitBlockMap[OldTarget]; |
| 390 | if (!NewTarget) { |
| 391 | // If we don't already have an exit stub for this non-extracted |
| 392 | // destination, create one now! |
| 393 | NewTarget = new BasicBlock(OldTarget->getName() + ".exitStub", |
| 394 | newFunction); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 395 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 396 | ConstantUInt *brVal = ConstantUInt::get(Type::UShortTy, switchVal++); |
| 397 | ReturnInst *NTRet = new ReturnInst(brVal, NewTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 398 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 399 | // Update the switch instruction. |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 400 | TheSwitch->addCase(brVal, OldTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 401 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 402 | // Restore values just before we exit |
| 403 | // FIXME: Use a GetElementPtr to bunch the outputs in a struct |
| 404 | for (unsigned out = 0, e = outputs.size(); out != e; ++out) |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 405 | if (!DS || |
| 406 | DS->dominates(cast<Instruction>(outputs[out])->getParent(), |
| 407 | TI->getParent())) |
| 408 | new StoreInst(outputs[out], getFunctionArg(newFunction, out), |
| 409 | NTRet); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 410 | } |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 411 | |
| 412 | // rewrite the original branch instruction with this new target |
| 413 | TI->setSuccessor(i, NewTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 414 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 415 | } |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 416 | |
| 417 | // Now that we've done the deed, make the default destination of the switch |
| 418 | // instruction be one of the exit blocks of the region. |
| 419 | if (TheSwitch->getNumSuccessors() > 1) { |
| 420 | // FIXME: this is broken w.r.t. PHI nodes, but the old code was more broken. |
| 421 | // This edge is not traversable. |
| 422 | TheSwitch->setSuccessor(0, TheSwitch->getSuccessor(1)); |
| 423 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
| 427 | /// ExtractRegion - Removes a loop from a function, replaces it with a call to |
| 428 | /// new function. Returns pointer to the new function. |
| 429 | /// |
| 430 | /// algorithm: |
| 431 | /// |
| 432 | /// find inputs and outputs for the region |
| 433 | /// |
| 434 | /// for inputs: add to function as args, map input instr* to arg# |
| 435 | /// for outputs: add allocas for scalars, |
| 436 | /// add to func as args, map output instr* to arg# |
| 437 | /// |
| 438 | /// rewrite func to use argument #s instead of instr* |
| 439 | /// |
| 440 | /// for each scalar output in the function: at every exit, store intermediate |
| 441 | /// computed result back into memory. |
| 442 | /// |
| 443 | Function *CodeExtractor::ExtractCodeRegion(const std::vector<BasicBlock*> &code) |
| 444 | { |
| 445 | // 1) Find inputs, outputs |
| 446 | // 2) Construct new function |
| 447 | // * Add allocas for defs, pass as args by reference |
| 448 | // * Pass in uses as args |
| 449 | // 3) Move code region, add call instr to func |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 450 | // |
| 451 | BlocksToExtract.insert(code.begin(), code.end()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 452 | |
| 453 | Values inputs, outputs; |
| 454 | |
| 455 | // Assumption: this is a single-entry code region, and the header is the first |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 456 | // block in the region. |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 457 | BasicBlock *header = code[0]; |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 458 | for (unsigned i = 1, e = code.size(); i != e; ++i) |
| 459 | for (pred_iterator PI = pred_begin(code[i]), E = pred_end(code[i]); |
| 460 | PI != E; ++PI) |
| 461 | assert(BlocksToExtract.count(*PI) && |
| 462 | "No blocks in this region may have entries from outside the region" |
| 463 | " except for the first block!"); |
| 464 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 465 | Function *oldFunction = header->getParent(); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 466 | |
| 467 | // This takes place of the original loop |
| 468 | BasicBlock *codeReplacer = new BasicBlock("codeRepl", oldFunction); |
| 469 | |
| 470 | // The new function needs a root node because other nodes can branch to the |
| 471 | // head of the loop, and the root cannot have predecessors |
| 472 | BasicBlock *newFuncRoot = new BasicBlock("newFuncRoot"); |
| 473 | newFuncRoot->getInstList().push_back(new BranchInst(header)); |
| 474 | |
| 475 | // Find inputs to, outputs from the code region |
| 476 | // |
| 477 | // If one of the inputs is coming from a different basic block and it's in a |
| 478 | // phi node, we need to rewrite the phi node: |
| 479 | // |
| 480 | // * All the inputs which involve basic blocks OUTSIDE of this region go into |
| 481 | // a NEW phi node that takes care of finding which value really came in. |
| 482 | // The result of this phi is passed to the function as an argument. |
| 483 | // |
| 484 | // * All the other phi values stay. |
| 485 | // |
| 486 | // FIXME: PHI nodes' incoming blocks aren't being rewritten to accomodate for |
| 487 | // blocks moving to a new function. |
| 488 | // SOLUTION: move Phi nodes out of the loop header into the codeReplacer, pass |
| 489 | // the values as parameters to the function |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 490 | findInputsOutputs(inputs, outputs, codeReplacer, newFuncRoot); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 491 | |
| 492 | // Step 2: Construct new function based on inputs/outputs, |
| 493 | // Add allocas for all defs |
| 494 | Function *newFunction = constructFunction(inputs, outputs, newFuncRoot, |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 495 | codeReplacer, oldFunction, |
| 496 | oldFunction->getParent()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 497 | |
| 498 | rewritePhiNodes(newFunction, newFuncRoot); |
| 499 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 500 | emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 501 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 502 | moveCodeToFunction(newFunction); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 503 | |
Chris Lattner | 3684469 | 2004-03-14 03:17:22 +0000 | [diff] [blame] | 504 | DEBUG(if (verifyFunction(*newFunction)) abort()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 505 | return newFunction; |
| 506 | } |
| 507 | |
Misha Brukman | f44acae | 2004-03-02 00:20:57 +0000 | [diff] [blame] | 508 | /// ExtractCodeRegion - slurp a sequence of basic blocks into a brand new |
| 509 | /// function |
| 510 | /// |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 511 | Function* llvm::ExtractCodeRegion(DominatorSet &DS, |
| 512 | const std::vector<BasicBlock*> &code) { |
| 513 | return CodeExtractor(&DS).ExtractCodeRegion(code); |
Misha Brukman | f44acae | 2004-03-02 00:20:57 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 516 | /// ExtractBasicBlock - slurp a natural loop into a brand new function |
| 517 | /// |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame^] | 518 | Function* llvm::ExtractLoop(DominatorSet &DS, Loop *L) { |
| 519 | return CodeExtractor(&DS).ExtractCodeRegion(L->getBlocks()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 522 | /// ExtractBasicBlock - slurp a basic block into a brand new function |
| 523 | /// |
| 524 | Function* llvm::ExtractBasicBlock(BasicBlock *BB) { |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 525 | std::vector<BasicBlock*> Blocks; |
| 526 | Blocks.push_back(BB); |
Chris Lattner | 4fca71e | 2004-03-14 04:01:47 +0000 | [diff] [blame] | 527 | return CodeExtractor().ExtractCodeRegion(Blocks); |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 528 | } |