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 { |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 33 | class CodeExtractor { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 34 | typedef std::vector<Value*> Values; |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 35 | std::set<BasicBlock*> BlocksToExtract; |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 36 | DominatorSet *DS; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 37 | public: |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 38 | CodeExtractor(DominatorSet *ds = 0) : DS(ds) {} |
| 39 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 40 | Function *ExtractCodeRegion(const std::vector<BasicBlock*> &code); |
| 41 | |
| 42 | private: |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 43 | void findInputsOutputs(Values &inputs, Values &outputs, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 44 | BasicBlock *newHeader, |
| 45 | BasicBlock *newRootNode); |
| 46 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 47 | Function *constructFunction(const Values &inputs, |
| 48 | const Values &outputs, |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 49 | BasicBlock *header, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 50 | BasicBlock *newRootNode, BasicBlock *newHeader, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 51 | Function *oldFunction, Module *M); |
| 52 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 53 | void moveCodeToFunction(Function *newFunction); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 54 | |
| 55 | void emitCallAndSwitchStatement(Function *newFunction, |
| 56 | BasicBlock *newHeader, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 57 | Values &inputs, |
| 58 | Values &outputs); |
| 59 | |
| 60 | }; |
| 61 | } |
| 62 | |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 63 | void CodeExtractor::findInputsOutputs(Values &inputs, Values &outputs, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 64 | BasicBlock *newHeader, |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 65 | BasicBlock *newRootNode) { |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 66 | for (std::set<BasicBlock*>::const_iterator ci = BlocksToExtract.begin(), |
| 67 | ce = BlocksToExtract.end(); ci != ce; ++ci) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 68 | BasicBlock *BB = *ci; |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 69 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 70 | // If a used value is defined outside the region, it's an input. If an |
| 71 | // instruction is used outside the region, it's an output. |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 72 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 73 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 74 | Value *V = PN->getIncomingValue(i); |
| 75 | if (!BlocksToExtract.count(PN->getIncomingBlock(i)) && |
| 76 | (isa<Instruction>(V) || isa<Argument>(V))) |
| 77 | inputs.push_back(V); |
Chris Lattner | 232155d | 2004-03-18 05:56:32 +0000 | [diff] [blame] | 78 | else if (Instruction *opI = dyn_cast<Instruction>(V)) { |
| 79 | if (!BlocksToExtract.count(opI->getParent())) |
| 80 | inputs.push_back(opI); |
| 81 | } else if (isa<Argument>(V)) |
| 82 | inputs.push_back(V); |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 84 | } else { |
| 85 | // All other instructions go through the generic input finder |
| 86 | // Loop over the operands of each instruction (inputs) |
| 87 | for (User::op_iterator op = I->op_begin(), opE = I->op_end(); |
| 88 | op != opE; ++op) |
| 89 | if (Instruction *opI = dyn_cast<Instruction>(*op)) { |
| 90 | // Check if definition of this operand is within the loop |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 91 | if (!BlocksToExtract.count(opI->getParent())) |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 92 | inputs.push_back(opI); |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 93 | } else if (isa<Argument>(*op)) { |
| 94 | inputs.push_back(*op); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 95 | } |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Consider uses of this instruction (outputs) |
| 99 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 100 | UI != E; ++UI) |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 101 | if (!BlocksToExtract.count(cast<Instruction>(*UI)->getParent())) { |
| 102 | outputs.push_back(I); |
| 103 | break; |
| 104 | } |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 105 | } // for: insts |
| 106 | } // for: basic blocks |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 109 | /// constructFunction - make a function based on inputs and outputs, as follows: |
| 110 | /// f(in0, ..., inN, out0, ..., outN) |
| 111 | /// |
| 112 | Function *CodeExtractor::constructFunction(const Values &inputs, |
| 113 | const Values &outputs, |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 114 | BasicBlock *header, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 115 | BasicBlock *newRootNode, |
| 116 | BasicBlock *newHeader, |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 117 | Function *oldFunction, |
| 118 | Module *M) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 119 | DEBUG(std::cerr << "inputs: " << inputs.size() << "\n"); |
| 120 | DEBUG(std::cerr << "outputs: " << outputs.size() << "\n"); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 121 | |
| 122 | // This function returns unsigned, outputs will go back by reference. |
| 123 | Type *retTy = Type::UShortTy; |
| 124 | std::vector<const Type*> paramTy; |
| 125 | |
| 126 | // Add the types of the input values to the function's argument list |
| 127 | for (Values::const_iterator i = inputs.begin(), |
| 128 | e = inputs.end(); i != e; ++i) { |
| 129 | const Value *value = *i; |
| 130 | DEBUG(std::cerr << "value used in func: " << value << "\n"); |
| 131 | paramTy.push_back(value->getType()); |
| 132 | } |
| 133 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 134 | // Add the types of the output values to the function's argument list. |
| 135 | for (Values::const_iterator I = outputs.begin(), E = outputs.end(); |
| 136 | I != E; ++I) { |
| 137 | DEBUG(std::cerr << "instr used in func: " << *I << "\n"); |
| 138 | paramTy.push_back(PointerType::get((*I)->getType())); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | DEBUG(std::cerr << "Function type: " << retTy << " f("); |
| 142 | for (std::vector<const Type*>::iterator i = paramTy.begin(), |
| 143 | e = paramTy.end(); i != e; ++i) |
Chris Lattner | fb87cde | 2004-03-15 01:26:44 +0000 | [diff] [blame] | 144 | DEBUG(std::cerr << *i << ", "); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 145 | DEBUG(std::cerr << ")\n"); |
| 146 | |
| 147 | const FunctionType *funcType = FunctionType::get(retTy, paramTy, false); |
| 148 | |
| 149 | // Create the new function |
| 150 | Function *newFunction = new Function(funcType, |
| 151 | GlobalValue::InternalLinkage, |
| 152 | oldFunction->getName() + "_code", M); |
| 153 | newFunction->getBasicBlockList().push_back(newRootNode); |
| 154 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 155 | // Create an iterator to name all of the arguments we inserted. |
| 156 | Function::aiterator AI = newFunction->abegin(); |
| 157 | |
| 158 | // Rewrite all users of the inputs in the extracted region to use the |
| 159 | // arguments instead. |
| 160 | for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI) { |
| 161 | AI->setName(inputs[i]->getName()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 162 | std::vector<User*> Users(inputs[i]->use_begin(), inputs[i]->use_end()); |
| 163 | for (std::vector<User*>::iterator use = Users.begin(), useE = Users.end(); |
Chris Lattner | 3684469 | 2004-03-14 03:17:22 +0000 | [diff] [blame] | 164 | use != useE; ++use) |
| 165 | if (Instruction* inst = dyn_cast<Instruction>(*use)) |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 166 | if (BlocksToExtract.count(inst->getParent())) |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 167 | inst->replaceUsesOfWith(inputs[i], AI); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 170 | // Set names for all of the output arguments. |
| 171 | for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI) |
| 172 | AI->setName(outputs[i]->getName()+".out"); |
| 173 | |
| 174 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 175 | // Rewrite branches to basic blocks outside of the loop to new dummy blocks |
| 176 | // within the new function. This must be done before we lose track of which |
| 177 | // blocks were originally in the code region. |
| 178 | std::vector<User*> Users(header->use_begin(), header->use_end()); |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 179 | for (unsigned i = 0, e = Users.size(); i != e; ++i) |
| 180 | // The BasicBlock which contains the branch is not in the region |
| 181 | // modify the branch target to a new block |
| 182 | if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Users[i])) |
| 183 | if (!BlocksToExtract.count(TI->getParent()) && |
| 184 | TI->getParent()->getParent() == oldFunction) |
| 185 | TI->replaceUsesOfWith(header, newHeader); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 186 | |
| 187 | return newFunction; |
| 188 | } |
| 189 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 190 | void CodeExtractor::moveCodeToFunction(Function *newFunction) { |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 191 | Function *oldFunc = (*BlocksToExtract.begin())->getParent(); |
Chris Lattner | 4fca71e | 2004-03-14 04:01:47 +0000 | [diff] [blame] | 192 | Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList(); |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 193 | Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList(); |
Chris Lattner | 4fca71e | 2004-03-14 04:01:47 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 195 | for (std::set<BasicBlock*>::const_iterator i = BlocksToExtract.begin(), |
| 196 | e = BlocksToExtract.end(); i != e; ++i) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 197 | // 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] | 198 | oldBlocks.remove(*i); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 199 | |
| 200 | // Insert this basic block into the new function |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 201 | newBlocks.push_back(*i); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | void |
| 206 | CodeExtractor::emitCallAndSwitchStatement(Function *newFunction, |
| 207 | BasicBlock *codeReplacer, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 208 | Values &inputs, |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 209 | Values &outputs) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 210 | // Emit a call to the new function, passing allocated memory for outputs and |
| 211 | // just plain inputs for non-scalars |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 212 | std::vector<Value*> params(inputs); |
| 213 | |
Chris Lattner | d8017a3 | 2004-03-18 04:12:05 +0000 | [diff] [blame] | 214 | // Get an iterator to the first output argument. |
| 215 | Function::aiterator OutputArgBegin = newFunction->abegin(); |
| 216 | std::advance(OutputArgBegin, inputs.size()); |
| 217 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 218 | for (unsigned i = 0, e = outputs.size(); i != e; ++i) { |
| 219 | Value *Output = outputs[i]; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 220 | // Create allocas for scalar outputs |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 221 | AllocaInst *alloca = |
| 222 | new AllocaInst(outputs[i]->getType(), 0, Output->getName()+".loc", |
| 223 | codeReplacer->getParent()->begin()->begin()); |
| 224 | params.push_back(alloca); |
| 225 | |
| 226 | LoadInst *load = new LoadInst(alloca, Output->getName()+".reload"); |
| 227 | codeReplacer->getInstList().push_back(load); |
| 228 | std::vector<User*> Users(outputs[i]->use_begin(), outputs[i]->use_end()); |
| 229 | for (unsigned u = 0, e = Users.size(); u != e; ++u) { |
| 230 | Instruction *inst = cast<Instruction>(Users[u]); |
| 231 | if (!BlocksToExtract.count(inst->getParent())) |
| 232 | inst->replaceUsesOfWith(outputs[i], load); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 235 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 236 | CallInst *call = new CallInst(newFunction, params, "targetBlock"); |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 237 | codeReplacer->getInstList().push_front(call); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 238 | |
| 239 | // 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] | 240 | SwitchInst *TheSwitch = new SwitchInst(call, codeReplacer, codeReplacer); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 241 | |
| 242 | // 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] | 243 | // function return an unsigned, switch on that number. This loop iterates |
| 244 | // over all of the blocks in the extracted region, updating any terminator |
| 245 | // instructions in the to-be-extracted region that branch to blocks that are |
| 246 | // not in the region to be extracted. |
| 247 | std::map<BasicBlock*, BasicBlock*> ExitBlockMap; |
| 248 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 249 | unsigned switchVal = 0; |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 250 | for (std::set<BasicBlock*>::const_iterator i = BlocksToExtract.begin(), |
| 251 | e = BlocksToExtract.end(); i != e; ++i) { |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 252 | TerminatorInst *TI = (*i)->getTerminator(); |
| 253 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 254 | if (!BlocksToExtract.count(TI->getSuccessor(i))) { |
| 255 | BasicBlock *OldTarget = TI->getSuccessor(i); |
| 256 | // add a new basic block which returns the appropriate value |
| 257 | BasicBlock *&NewTarget = ExitBlockMap[OldTarget]; |
| 258 | if (!NewTarget) { |
| 259 | // If we don't already have an exit stub for this non-extracted |
| 260 | // destination, create one now! |
| 261 | NewTarget = new BasicBlock(OldTarget->getName() + ".exitStub", |
| 262 | newFunction); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 263 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 264 | ConstantUInt *brVal = ConstantUInt::get(Type::UShortTy, switchVal++); |
| 265 | ReturnInst *NTRet = new ReturnInst(brVal, NewTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 266 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 267 | // Update the switch instruction. |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 268 | TheSwitch->addCase(brVal, OldTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 269 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 270 | // Restore values just before we exit |
| 271 | // FIXME: Use a GetElementPtr to bunch the outputs in a struct |
Chris Lattner | d8017a3 | 2004-03-18 04:12:05 +0000 | [diff] [blame] | 272 | Function::aiterator OAI = OutputArgBegin; |
| 273 | for (unsigned out = 0, e = outputs.size(); out != e; ++out, ++OAI) |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 274 | if (!DS || |
| 275 | DS->dominates(cast<Instruction>(outputs[out])->getParent(), |
| 276 | TI->getParent())) |
Chris Lattner | d8017a3 | 2004-03-18 04:12:05 +0000 | [diff] [blame] | 277 | new StoreInst(outputs[out], OAI, NTRet); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 278 | } |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 279 | |
| 280 | // rewrite the original branch instruction with this new target |
| 281 | TI->setSuccessor(i, NewTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 282 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 283 | } |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 284 | |
| 285 | // Now that we've done the deed, make the default destination of the switch |
| 286 | // instruction be one of the exit blocks of the region. |
| 287 | if (TheSwitch->getNumSuccessors() > 1) { |
| 288 | // FIXME: this is broken w.r.t. PHI nodes, but the old code was more broken. |
| 289 | // This edge is not traversable. |
| 290 | TheSwitch->setSuccessor(0, TheSwitch->getSuccessor(1)); |
| 291 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
| 295 | /// ExtractRegion - Removes a loop from a function, replaces it with a call to |
| 296 | /// new function. Returns pointer to the new function. |
| 297 | /// |
| 298 | /// algorithm: |
| 299 | /// |
| 300 | /// find inputs and outputs for the region |
| 301 | /// |
| 302 | /// for inputs: add to function as args, map input instr* to arg# |
| 303 | /// for outputs: add allocas for scalars, |
| 304 | /// add to func as args, map output instr* to arg# |
| 305 | /// |
| 306 | /// rewrite func to use argument #s instead of instr* |
| 307 | /// |
| 308 | /// for each scalar output in the function: at every exit, store intermediate |
| 309 | /// computed result back into memory. |
| 310 | /// |
| 311 | Function *CodeExtractor::ExtractCodeRegion(const std::vector<BasicBlock*> &code) |
| 312 | { |
| 313 | // 1) Find inputs, outputs |
| 314 | // 2) Construct new function |
| 315 | // * Add allocas for defs, pass as args by reference |
| 316 | // * Pass in uses as args |
| 317 | // 3) Move code region, add call instr to func |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 318 | // |
| 319 | BlocksToExtract.insert(code.begin(), code.end()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 320 | |
| 321 | Values inputs, outputs; |
| 322 | |
| 323 | // 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] | 324 | // block in the region. |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 325 | BasicBlock *header = code[0]; |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 326 | for (unsigned i = 1, e = code.size(); i != e; ++i) |
| 327 | for (pred_iterator PI = pred_begin(code[i]), E = pred_end(code[i]); |
| 328 | PI != E; ++PI) |
| 329 | assert(BlocksToExtract.count(*PI) && |
| 330 | "No blocks in this region may have entries from outside the region" |
| 331 | " except for the first block!"); |
| 332 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 333 | Function *oldFunction = header->getParent(); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 334 | |
| 335 | // This takes place of the original loop |
| 336 | BasicBlock *codeReplacer = new BasicBlock("codeRepl", oldFunction); |
| 337 | |
| 338 | // The new function needs a root node because other nodes can branch to the |
| 339 | // head of the loop, and the root cannot have predecessors |
| 340 | BasicBlock *newFuncRoot = new BasicBlock("newFuncRoot"); |
| 341 | newFuncRoot->getInstList().push_back(new BranchInst(header)); |
| 342 | |
| 343 | // Find inputs to, outputs from the code region |
| 344 | // |
| 345 | // If one of the inputs is coming from a different basic block and it's in a |
| 346 | // phi node, we need to rewrite the phi node: |
| 347 | // |
| 348 | // * All the inputs which involve basic blocks OUTSIDE of this region go into |
| 349 | // a NEW phi node that takes care of finding which value really came in. |
| 350 | // The result of this phi is passed to the function as an argument. |
| 351 | // |
| 352 | // * All the other phi values stay. |
| 353 | // |
| 354 | // FIXME: PHI nodes' incoming blocks aren't being rewritten to accomodate for |
| 355 | // blocks moving to a new function. |
| 356 | // SOLUTION: move Phi nodes out of the loop header into the codeReplacer, pass |
| 357 | // the values as parameters to the function |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 358 | findInputsOutputs(inputs, outputs, codeReplacer, newFuncRoot); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 359 | |
| 360 | // Step 2: Construct new function based on inputs/outputs, |
| 361 | // Add allocas for all defs |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 362 | Function *newFunction = constructFunction(inputs, outputs, code[0], |
| 363 | newFuncRoot, |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 364 | codeReplacer, oldFunction, |
| 365 | oldFunction->getParent()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 366 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 367 | emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 368 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 369 | moveCodeToFunction(newFunction); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 370 | |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 371 | // Loop over all of the PHI nodes in the entry block (code[0]), and change any |
| 372 | // references to the old incoming edge to be the new incoming edge. |
| 373 | for (BasicBlock::iterator I = code[0]->begin(); |
| 374 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 375 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 376 | if (!BlocksToExtract.count(PN->getIncomingBlock(i))) |
| 377 | PN->setIncomingBlock(i, newFuncRoot); |
| 378 | |
Chris Lattner | acd7598 | 2004-03-18 05:38:31 +0000 | [diff] [blame] | 379 | // Look at all successors of the codeReplacer block. If any of these blocks |
| 380 | // had PHI nodes in them, we need to update the "from" block to be the code |
| 381 | // replacer, not the original block in the extracted region. |
| 382 | std::vector<BasicBlock*> Succs(succ_begin(codeReplacer), |
| 383 | succ_end(codeReplacer)); |
| 384 | for (unsigned i = 0, e = Succs.size(); i != e; ++i) |
| 385 | for (BasicBlock::iterator I = Succs[i]->begin(); |
| 386 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 387 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 388 | if (BlocksToExtract.count(PN->getIncomingBlock(i))) |
| 389 | PN->setIncomingBlock(i, codeReplacer); |
| 390 | |
| 391 | |
Chris Lattner | 3684469 | 2004-03-14 03:17:22 +0000 | [diff] [blame] | 392 | DEBUG(if (verifyFunction(*newFunction)) abort()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 393 | return newFunction; |
| 394 | } |
| 395 | |
Misha Brukman | f44acae | 2004-03-02 00:20:57 +0000 | [diff] [blame] | 396 | /// ExtractCodeRegion - slurp a sequence of basic blocks into a brand new |
| 397 | /// function |
| 398 | /// |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 399 | Function* llvm::ExtractCodeRegion(DominatorSet &DS, |
| 400 | const std::vector<BasicBlock*> &code) { |
| 401 | return CodeExtractor(&DS).ExtractCodeRegion(code); |
Misha Brukman | f44acae | 2004-03-02 00:20:57 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 404 | /// ExtractBasicBlock - slurp a natural loop into a brand new function |
| 405 | /// |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 406 | Function* llvm::ExtractLoop(DominatorSet &DS, Loop *L) { |
| 407 | return CodeExtractor(&DS).ExtractCodeRegion(L->getBlocks()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 410 | /// ExtractBasicBlock - slurp a basic block into a brand new function |
| 411 | /// |
| 412 | Function* llvm::ExtractBasicBlock(BasicBlock *BB) { |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 413 | std::vector<BasicBlock*> Blocks; |
| 414 | Blocks.push_back(BB); |
Chris Lattner | 4fca71e | 2004-03-14 04:01:47 +0000 | [diff] [blame] | 415 | return CodeExtractor().ExtractCodeRegion(Blocks); |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 416 | } |