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" |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 20 | #include "llvm/Intrinsics.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
| 22 | #include "llvm/Pass.h" |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/Dominators.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 3684469 | 2004-03-14 03:17:22 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/Verifier.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 27 | #include "Support/CommandLine.h" |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 28 | #include "Support/Debug.h" |
| 29 | #include "Support/StringExtras.h" |
| 30 | #include <algorithm> |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 31 | #include <set> |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 34 | // Provide a command-line option to aggregate function arguments into a struct |
| 35 | // for functions produced by the code extrator. This is useful when converting |
| 36 | // extracted functions to pthread-based code, as only one argument (void*) can |
| 37 | // be passed in to pthread_create(). |
| 38 | static cl::opt<bool> |
| 39 | AggregateArgsOpt("aggregate-extracted-args", cl::Hidden, |
| 40 | cl::desc("Aggregate arguments to code-extracted functions")); |
| 41 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 42 | namespace { |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 43 | class CodeExtractor { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 44 | typedef std::vector<Value*> Values; |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 45 | std::set<BasicBlock*> BlocksToExtract; |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 46 | DominatorSet *DS; |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 47 | bool AggregateArgs; |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 48 | unsigned NumExitBlocks; |
| 49 | const Type *RetTy; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 50 | public: |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 51 | CodeExtractor(DominatorSet *ds = 0, bool AggArgs = false) |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 52 | : DS(ds), AggregateArgs(AggregateArgsOpt), NumExitBlocks(~0U) {} |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 53 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 54 | Function *ExtractCodeRegion(const std::vector<BasicBlock*> &code); |
| 55 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 56 | bool isEligible(const std::vector<BasicBlock*> &code); |
| 57 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 58 | private: |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 59 | /// definedInRegion - Return true if the specified value is defined in the |
| 60 | /// extracted region. |
| 61 | bool definedInRegion(Value *V) const { |
| 62 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 63 | if (BlocksToExtract.count(I->getParent())) |
| 64 | return true; |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | /// definedInCaller - Return true if the specified value is defined in the |
| 69 | /// function being code extracted, but not in the region being extracted. |
| 70 | /// These values must be passed in as live-ins to the function. |
| 71 | bool definedInCaller(Value *V) const { |
| 72 | if (isa<Argument>(V)) return true; |
| 73 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 74 | if (!BlocksToExtract.count(I->getParent())) |
| 75 | return true; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | void severSplitPHINodes(BasicBlock *&Header); |
| 80 | void findInputsOutputs(Values &inputs, Values &outputs); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 81 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 82 | Function *constructFunction(const Values &inputs, |
| 83 | const Values &outputs, |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 84 | BasicBlock *header, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 85 | BasicBlock *newRootNode, BasicBlock *newHeader, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 86 | Function *oldFunction, Module *M); |
| 87 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 88 | void moveCodeToFunction(Function *newFunction); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 89 | |
| 90 | void emitCallAndSwitchStatement(Function *newFunction, |
| 91 | BasicBlock *newHeader, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 92 | Values &inputs, |
| 93 | Values &outputs); |
| 94 | |
| 95 | }; |
| 96 | } |
| 97 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 98 | /// severSplitPHINodes - If a PHI node has multiple inputs from outside of the |
| 99 | /// region, we need to split the entry block of the region so that the PHI node |
| 100 | /// is easier to deal with. |
| 101 | void CodeExtractor::severSplitPHINodes(BasicBlock *&Header) { |
| 102 | |
| 103 | |
| 104 | |
| 105 | } |
| 106 | |
| 107 | // findInputsOutputs - Find inputs to, outputs from the code region. |
| 108 | // |
| 109 | void CodeExtractor::findInputsOutputs(Values &inputs, Values &outputs) { |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 110 | std::set<BasicBlock*> ExitBlocks; |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 111 | for (std::set<BasicBlock*>::const_iterator ci = BlocksToExtract.begin(), |
| 112 | ce = BlocksToExtract.end(); ci != ce; ++ci) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 113 | BasicBlock *BB = *ci; |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 114 | |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 115 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 116 | // If a used value is defined outside the region, it's an input. If an |
| 117 | // instruction is used outside the region, it's an output. |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 118 | for (User::op_iterator O = I->op_begin(), E = I->op_end(); O != E; ++O) |
| 119 | if (definedInCaller(*O)) |
| 120 | inputs.push_back(*O); |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 122 | // Consider uses of this instruction (outputs). |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 123 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 124 | UI != E; ++UI) |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 125 | if (!definedInRegion(*UI)) { |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 126 | outputs.push_back(I); |
| 127 | break; |
| 128 | } |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 129 | } // for: insts |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 131 | // Keep track of the exit blocks from the region. |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 132 | TerminatorInst *TI = BB->getTerminator(); |
| 133 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 134 | if (!BlocksToExtract.count(TI->getSuccessor(i))) |
| 135 | ExitBlocks.insert(TI->getSuccessor(i)); |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 136 | } // for: basic blocks |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 137 | |
| 138 | NumExitBlocks = ExitBlocks.size(); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 141 | /// constructFunction - make a function based on inputs and outputs, as follows: |
| 142 | /// f(in0, ..., inN, out0, ..., outN) |
| 143 | /// |
| 144 | Function *CodeExtractor::constructFunction(const Values &inputs, |
| 145 | const Values &outputs, |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 146 | BasicBlock *header, |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 147 | BasicBlock *newRootNode, |
| 148 | BasicBlock *newHeader, |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 149 | Function *oldFunction, |
| 150 | Module *M) { |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 151 | DEBUG(std::cerr << "inputs: " << inputs.size() << "\n"); |
| 152 | DEBUG(std::cerr << "outputs: " << outputs.size() << "\n"); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 153 | |
| 154 | // This function returns unsigned, outputs will go back by reference. |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 155 | switch (NumExitBlocks) { |
| 156 | case 0: |
| 157 | case 1: RetTy = Type::VoidTy; break; |
| 158 | case 2: RetTy = Type::BoolTy; break; |
| 159 | default: RetTy = Type::UShortTy; break; |
| 160 | } |
| 161 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 162 | std::vector<const Type*> paramTy; |
| 163 | |
| 164 | // Add the types of the input values to the function's argument list |
| 165 | for (Values::const_iterator i = inputs.begin(), |
| 166 | e = inputs.end(); i != e; ++i) { |
| 167 | const Value *value = *i; |
| 168 | DEBUG(std::cerr << "value used in func: " << value << "\n"); |
| 169 | paramTy.push_back(value->getType()); |
| 170 | } |
| 171 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 172 | // Add the types of the output values to the function's argument list. |
| 173 | for (Values::const_iterator I = outputs.begin(), E = outputs.end(); |
| 174 | I != E; ++I) { |
| 175 | DEBUG(std::cerr << "instr used in func: " << *I << "\n"); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 176 | if (AggregateArgs) |
| 177 | paramTy.push_back((*I)->getType()); |
| 178 | else |
| 179 | paramTy.push_back(PointerType::get((*I)->getType())); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 182 | DEBUG(std::cerr << "Function type: " << RetTy << " f("); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 183 | DEBUG(for (std::vector<const Type*>::iterator i = paramTy.begin(), |
| 184 | e = paramTy.end(); i != e; ++i) std::cerr << *i << ", "); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 185 | DEBUG(std::cerr << ")\n"); |
| 186 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 187 | if (AggregateArgs && (inputs.size() + outputs.size() > 0)) { |
| 188 | PointerType *StructPtr = PointerType::get(StructType::get(paramTy)); |
| 189 | paramTy.clear(); |
| 190 | paramTy.push_back(StructPtr); |
| 191 | } |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 192 | const FunctionType *funcType = FunctionType::get(RetTy, paramTy, false); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 193 | |
| 194 | // Create the new function |
| 195 | Function *newFunction = new Function(funcType, |
| 196 | GlobalValue::InternalLinkage, |
| 197 | oldFunction->getName() + "_code", M); |
| 198 | newFunction->getBasicBlockList().push_back(newRootNode); |
| 199 | |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 200 | // Create an iterator to name all of the arguments we inserted. |
| 201 | Function::aiterator AI = newFunction->abegin(); |
| 202 | |
| 203 | // Rewrite all users of the inputs in the extracted region to use the |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 204 | // arguments (or appropriate addressing into struct) instead. |
| 205 | for (unsigned i = 0, e = inputs.size(); i != e; ++i) { |
| 206 | Value *RewriteVal; |
| 207 | if (AggregateArgs) { |
| 208 | std::vector<Value*> Indices; |
| 209 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
| 210 | Indices.push_back(ConstantUInt::get(Type::UIntTy, i)); |
| 211 | std::string GEPname = "gep_" + inputs[i]->getName(); |
| 212 | TerminatorInst *TI = newFunction->begin()->getTerminator(); |
| 213 | GetElementPtrInst *GEP = new GetElementPtrInst(AI, Indices, GEPname, TI); |
| 214 | RewriteVal = new LoadInst(GEP, "load" + GEPname, TI); |
| 215 | } else |
| 216 | RewriteVal = AI++; |
| 217 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 218 | std::vector<User*> Users(inputs[i]->use_begin(), inputs[i]->use_end()); |
| 219 | for (std::vector<User*>::iterator use = Users.begin(), useE = Users.end(); |
Chris Lattner | 3684469 | 2004-03-14 03:17:22 +0000 | [diff] [blame] | 220 | use != useE; ++use) |
| 221 | if (Instruction* inst = dyn_cast<Instruction>(*use)) |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 222 | if (BlocksToExtract.count(inst->getParent())) |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 223 | inst->replaceUsesOfWith(inputs[i], RewriteVal); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 226 | // Set names for input and output arguments. |
| 227 | if (!AggregateArgs) { |
| 228 | AI = newFunction->abegin(); |
| 229 | for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI) |
| 230 | AI->setName(inputs[i]->getName()); |
| 231 | for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI) |
| 232 | AI->setName(outputs[i]->getName()+".out"); |
| 233 | } |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 234 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 235 | // Rewrite branches to basic blocks outside of the loop to new dummy blocks |
| 236 | // within the new function. This must be done before we lose track of which |
| 237 | // blocks were originally in the code region. |
| 238 | std::vector<User*> Users(header->use_begin(), header->use_end()); |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 239 | for (unsigned i = 0, e = Users.size(); i != e; ++i) |
| 240 | // The BasicBlock which contains the branch is not in the region |
| 241 | // modify the branch target to a new block |
| 242 | if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Users[i])) |
| 243 | if (!BlocksToExtract.count(TI->getParent()) && |
| 244 | TI->getParent()->getParent() == oldFunction) |
| 245 | TI->replaceUsesOfWith(header, newHeader); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 246 | |
| 247 | return newFunction; |
| 248 | } |
| 249 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 250 | /// emitCallAndSwitchStatement - This method sets up the caller side by adding |
| 251 | /// the call instruction, splitting any PHI nodes in the header block as |
| 252 | /// necessary. |
| 253 | void CodeExtractor:: |
| 254 | emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer, |
| 255 | Values &inputs, Values &outputs) { |
| 256 | // Emit a call to the new function, passing in: *pointer to struct (if |
| 257 | // aggregating parameters), or plan inputs and allocated memory for outputs |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 258 | std::vector<Value*> params, StructValues, ReloadOutputs; |
Chris Lattner | d8017a3 | 2004-03-18 04:12:05 +0000 | [diff] [blame] | 259 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 260 | // Add inputs as params, or to be filled into the struct |
| 261 | for (Values::iterator i = inputs.begin(), e = inputs.end(); i != e; ++i) |
| 262 | if (AggregateArgs) |
| 263 | StructValues.push_back(*i); |
| 264 | else |
| 265 | params.push_back(*i); |
| 266 | |
| 267 | // Create allocas for the outputs |
| 268 | for (Values::iterator i = outputs.begin(), e = outputs.end(); i != e; ++i) { |
| 269 | if (AggregateArgs) { |
| 270 | StructValues.push_back(*i); |
| 271 | } else { |
| 272 | AllocaInst *alloca = |
| 273 | new AllocaInst((*i)->getType(), 0, (*i)->getName()+".loc", |
| 274 | codeReplacer->getParent()->begin()->begin()); |
| 275 | ReloadOutputs.push_back(alloca); |
| 276 | params.push_back(alloca); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | AllocaInst *Struct = 0; |
| 281 | if (AggregateArgs && (inputs.size() + outputs.size() > 0)) { |
| 282 | std::vector<const Type*> ArgTypes; |
| 283 | for (Values::iterator v = StructValues.begin(), |
| 284 | ve = StructValues.end(); v != ve; ++v) |
| 285 | ArgTypes.push_back((*v)->getType()); |
| 286 | |
| 287 | // Allocate a struct at the beginning of this function |
| 288 | Type *StructArgTy = StructType::get(ArgTypes); |
| 289 | Struct = |
| 290 | new AllocaInst(StructArgTy, 0, "structArg", |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 291 | codeReplacer->getParent()->begin()->begin()); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 292 | params.push_back(Struct); |
| 293 | |
| 294 | for (unsigned i = 0, e = inputs.size(); i != e; ++i) { |
| 295 | std::vector<Value*> Indices; |
| 296 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
| 297 | Indices.push_back(ConstantUInt::get(Type::UIntTy, i)); |
| 298 | GetElementPtrInst *GEP = |
| 299 | new GetElementPtrInst(Struct, Indices, |
| 300 | "gep_" + StructValues[i]->getName(), 0); |
| 301 | codeReplacer->getInstList().push_back(GEP); |
| 302 | StoreInst *SI = new StoreInst(StructValues[i], GEP); |
| 303 | codeReplacer->getInstList().push_back(SI); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // Emit the call to the function |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 308 | CallInst *call = new CallInst(newFunction, params, |
| 309 | NumExitBlocks > 1 ? "targetBlock": ""); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 310 | codeReplacer->getInstList().push_back(call); |
| 311 | |
| 312 | Function::aiterator OutputArgBegin = newFunction->abegin(); |
| 313 | unsigned FirstOut = inputs.size(); |
| 314 | if (!AggregateArgs) |
| 315 | std::advance(OutputArgBegin, inputs.size()); |
| 316 | |
| 317 | // Reload the outputs passed in by reference |
| 318 | for (unsigned i = 0, e = outputs.size(); i != e; ++i) { |
| 319 | Value *Output = 0; |
| 320 | if (AggregateArgs) { |
| 321 | std::vector<Value*> Indices; |
| 322 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
| 323 | Indices.push_back(ConstantUInt::get(Type::UIntTy, FirstOut + i)); |
| 324 | GetElementPtrInst *GEP |
| 325 | = new GetElementPtrInst(Struct, Indices, |
| 326 | "gep_reload_" + outputs[i]->getName(), 0); |
| 327 | codeReplacer->getInstList().push_back(GEP); |
| 328 | Output = GEP; |
| 329 | } else { |
| 330 | Output = ReloadOutputs[i]; |
| 331 | } |
| 332 | LoadInst *load = new LoadInst(Output, outputs[i]->getName()+".reload"); |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 333 | codeReplacer->getInstList().push_back(load); |
| 334 | std::vector<User*> Users(outputs[i]->use_begin(), outputs[i]->use_end()); |
| 335 | for (unsigned u = 0, e = Users.size(); u != e; ++u) { |
| 336 | Instruction *inst = cast<Instruction>(Users[u]); |
| 337 | if (!BlocksToExtract.count(inst->getParent())) |
| 338 | inst->replaceUsesOfWith(outputs[i], load); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 341 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 342 | // Now we can emit a switch statement using the call as a value. |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 343 | SwitchInst *TheSwitch = |
| 344 | new SwitchInst(ConstantUInt::getNullValue(Type::UShortTy), |
| 345 | codeReplacer, codeReplacer); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 346 | |
| 347 | // 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] | 348 | // function return an unsigned, switch on that number. This loop iterates |
| 349 | // over all of the blocks in the extracted region, updating any terminator |
| 350 | // instructions in the to-be-extracted region that branch to blocks that are |
| 351 | // not in the region to be extracted. |
| 352 | std::map<BasicBlock*, BasicBlock*> ExitBlockMap; |
| 353 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 354 | unsigned switchVal = 0; |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 355 | for (std::set<BasicBlock*>::const_iterator i = BlocksToExtract.begin(), |
| 356 | e = BlocksToExtract.end(); i != e; ++i) { |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 357 | TerminatorInst *TI = (*i)->getTerminator(); |
| 358 | for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) |
| 359 | if (!BlocksToExtract.count(TI->getSuccessor(i))) { |
| 360 | BasicBlock *OldTarget = TI->getSuccessor(i); |
| 361 | // add a new basic block which returns the appropriate value |
| 362 | BasicBlock *&NewTarget = ExitBlockMap[OldTarget]; |
| 363 | if (!NewTarget) { |
| 364 | // If we don't already have an exit stub for this non-extracted |
| 365 | // destination, create one now! |
| 366 | NewTarget = new BasicBlock(OldTarget->getName() + ".exitStub", |
| 367 | newFunction); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 368 | unsigned SuccNum = switchVal++; |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 369 | |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 370 | Value *brVal = 0; |
| 371 | switch (NumExitBlocks) { |
| 372 | case 0: |
| 373 | case 1: break; // No value needed. |
| 374 | case 2: // Conditional branch, return a bool |
| 375 | brVal = SuccNum ? ConstantBool::False : ConstantBool::True; |
| 376 | break; |
| 377 | default: |
| 378 | brVal = ConstantUInt::get(Type::UShortTy, SuccNum); |
| 379 | break; |
| 380 | } |
| 381 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 382 | ReturnInst *NTRet = new ReturnInst(brVal, NewTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 383 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 384 | // Update the switch instruction. |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 385 | TheSwitch->addCase(ConstantUInt::get(Type::UShortTy, SuccNum), |
| 386 | OldTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 387 | |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 388 | // Restore values just before we exit |
Chris Lattner | d8017a3 | 2004-03-18 04:12:05 +0000 | [diff] [blame] | 389 | Function::aiterator OAI = OutputArgBegin; |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 390 | for (unsigned out = 0, e = outputs.size(); out != e; ++out) { |
| 391 | // For an invoke, the normal destination is the only one that is |
| 392 | // dominated by the result of the invocation |
| 393 | BasicBlock *DefBlock = cast<Instruction>(outputs[out])->getParent(); |
| 394 | if (InvokeInst *Invoke = dyn_cast<InvokeInst>(outputs[out])) |
| 395 | DefBlock = Invoke->getNormalDest(); |
| 396 | if (!DS || DS->dominates(DefBlock, TI->getParent())) |
| 397 | if (AggregateArgs) { |
| 398 | std::vector<Value*> Indices; |
| 399 | Indices.push_back(Constant::getNullValue(Type::UIntTy)); |
| 400 | Indices.push_back(ConstantUInt::get(Type::UIntTy,FirstOut+out)); |
| 401 | GetElementPtrInst *GEP = |
| 402 | new GetElementPtrInst(OAI, Indices, |
| 403 | "gep_" + outputs[out]->getName(), |
| 404 | NTRet); |
| 405 | new StoreInst(outputs[out], GEP, NTRet); |
| 406 | } else |
| 407 | new StoreInst(outputs[out], OAI, NTRet); |
| 408 | // Advance output iterator even if we don't emit a store |
| 409 | if (!AggregateArgs) ++OAI; |
| 410 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 411 | } |
Chris Lattner | b4d8bf3 | 2004-03-14 23:05:49 +0000 | [diff] [blame] | 412 | |
| 413 | // rewrite the original branch instruction with this new target |
| 414 | TI->setSuccessor(i, NewTarget); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 415 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 416 | } |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 3d1ca67 | 2004-05-12 03:22:33 +0000 | [diff] [blame] | 418 | // Now that we've done the deed, simplify the switch instruction. |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 419 | switch (NumExitBlocks) { |
| 420 | case 0: |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 421 | // There is only 1 successor (the block containing the switch itself), which |
| 422 | // means that previously this was the last part of the function, and hence |
| 423 | // this should be rewritten as a `ret' |
| 424 | |
| 425 | // Check if the function should return a value |
| 426 | if (TheSwitch->getParent()->getParent()->getReturnType() != Type::VoidTy && |
| 427 | TheSwitch->getParent()->getParent()->getReturnType() == |
| 428 | TheSwitch->getCondition()->getType()) |
| 429 | // return what we have |
| 430 | new ReturnInst(TheSwitch->getCondition(), TheSwitch); |
| 431 | else |
| 432 | // just return |
| 433 | new ReturnInst(0, TheSwitch); |
| 434 | |
| 435 | TheSwitch->getParent()->getInstList().erase(TheSwitch); |
Chris Lattner | ffc4926 | 2004-05-12 04:14:24 +0000 | [diff] [blame] | 436 | break; |
| 437 | case 1: |
| 438 | // Only a single destination, change the switch into an unconditional |
| 439 | // branch. |
| 440 | new BranchInst(TheSwitch->getSuccessor(1), TheSwitch); |
| 441 | TheSwitch->getParent()->getInstList().erase(TheSwitch); |
| 442 | break; |
| 443 | case 2: |
| 444 | new BranchInst(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2), |
| 445 | call, TheSwitch); |
| 446 | TheSwitch->getParent()->getInstList().erase(TheSwitch); |
| 447 | break; |
| 448 | default: |
| 449 | // Otherwise, make the default destination of the switch instruction be one |
| 450 | // of the other successors. |
| 451 | TheSwitch->setOperand(0, call); |
| 452 | TheSwitch->setSuccessor(0, TheSwitch->getSuccessor(NumExitBlocks)); |
| 453 | TheSwitch->removeCase(NumExitBlocks); // Remove redundant case |
| 454 | break; |
Chris Lattner | 5b2072e | 2004-03-14 23:43:24 +0000 | [diff] [blame] | 455 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 458 | void CodeExtractor::moveCodeToFunction(Function *newFunction) { |
| 459 | Function *oldFunc = (*BlocksToExtract.begin())->getParent(); |
| 460 | Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList(); |
| 461 | Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList(); |
| 462 | |
| 463 | for (std::set<BasicBlock*>::const_iterator i = BlocksToExtract.begin(), |
| 464 | e = BlocksToExtract.end(); i != e; ++i) { |
| 465 | // Delete the basic block from the old function, and the list of blocks |
| 466 | oldBlocks.remove(*i); |
| 467 | |
| 468 | // Insert this basic block into the new function |
| 469 | newBlocks.push_back(*i); |
| 470 | } |
| 471 | } |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 472 | |
| 473 | /// ExtractRegion - Removes a loop from a function, replaces it with a call to |
| 474 | /// new function. Returns pointer to the new function. |
| 475 | /// |
| 476 | /// algorithm: |
| 477 | /// |
| 478 | /// find inputs and outputs for the region |
| 479 | /// |
| 480 | /// for inputs: add to function as args, map input instr* to arg# |
| 481 | /// for outputs: add allocas for scalars, |
| 482 | /// add to func as args, map output instr* to arg# |
| 483 | /// |
| 484 | /// rewrite func to use argument #s instead of instr* |
| 485 | /// |
| 486 | /// for each scalar output in the function: at every exit, store intermediate |
| 487 | /// computed result back into memory. |
| 488 | /// |
| 489 | Function *CodeExtractor::ExtractCodeRegion(const std::vector<BasicBlock*> &code) |
| 490 | { |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 491 | if (!isEligible(code)) |
| 492 | return 0; |
| 493 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 494 | // 1) Find inputs, outputs |
| 495 | // 2) Construct new function |
| 496 | // * Add allocas for defs, pass as args by reference |
| 497 | // * Pass in uses as args |
| 498 | // 3) Move code region, add call instr to func |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 499 | // |
| 500 | BlocksToExtract.insert(code.begin(), code.end()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 501 | |
| 502 | Values inputs, outputs; |
| 503 | |
| 504 | // 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] | 505 | // block in the region. |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 506 | BasicBlock *header = code[0]; |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 507 | |
| 508 | // If we have to split PHI nodes, do so now. |
| 509 | severSplitPHINodes(header); |
| 510 | |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 511 | for (unsigned i = 1, e = code.size(); i != e; ++i) |
| 512 | for (pred_iterator PI = pred_begin(code[i]), E = pred_end(code[i]); |
| 513 | PI != E; ++PI) |
| 514 | assert(BlocksToExtract.count(*PI) && |
| 515 | "No blocks in this region may have entries from outside the region" |
| 516 | " except for the first block!"); |
| 517 | |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 518 | Function *oldFunction = header->getParent(); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 519 | |
| 520 | // This takes place of the original loop |
| 521 | BasicBlock *codeReplacer = new BasicBlock("codeRepl", oldFunction); |
| 522 | |
| 523 | // The new function needs a root node because other nodes can branch to the |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 524 | // head of the region, but the entry node of a function cannot have preds. |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 525 | BasicBlock *newFuncRoot = new BasicBlock("newFuncRoot"); |
| 526 | newFuncRoot->getInstList().push_back(new BranchInst(header)); |
| 527 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 528 | // Find inputs to, outputs from the code region. |
| 529 | findInputsOutputs(inputs, outputs); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 530 | |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 531 | // Construct new function based on inputs/outputs & add allocas for all defs. |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 532 | Function *newFunction = constructFunction(inputs, outputs, code[0], |
| 533 | newFuncRoot, |
Chris Lattner | 73ab1fa | 2004-03-15 01:18:23 +0000 | [diff] [blame] | 534 | codeReplacer, oldFunction, |
| 535 | oldFunction->getParent()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 537 | emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 538 | |
Chris Lattner | 9c431f6 | 2004-03-14 22:34:55 +0000 | [diff] [blame] | 539 | moveCodeToFunction(newFunction); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 540 | |
Chris Lattner | 320d59f | 2004-03-18 05:28:49 +0000 | [diff] [blame] | 541 | // Loop over all of the PHI nodes in the entry block (code[0]), and change any |
| 542 | // references to the old incoming edge to be the new incoming edge. |
| 543 | for (BasicBlock::iterator I = code[0]->begin(); |
| 544 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 545 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 546 | if (!BlocksToExtract.count(PN->getIncomingBlock(i))) |
| 547 | PN->setIncomingBlock(i, newFuncRoot); |
| 548 | |
Chris Lattner | acd7598 | 2004-03-18 05:38:31 +0000 | [diff] [blame] | 549 | // Look at all successors of the codeReplacer block. If any of these blocks |
| 550 | // had PHI nodes in them, we need to update the "from" block to be the code |
| 551 | // replacer, not the original block in the extracted region. |
| 552 | std::vector<BasicBlock*> Succs(succ_begin(codeReplacer), |
| 553 | succ_end(codeReplacer)); |
| 554 | for (unsigned i = 0, e = Succs.size(); i != e; ++i) |
| 555 | for (BasicBlock::iterator I = Succs[i]->begin(); |
| 556 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 557 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 558 | if (BlocksToExtract.count(PN->getIncomingBlock(i))) |
| 559 | PN->setIncomingBlock(i, codeReplacer); |
| 560 | |
| 561 | |
Chris Lattner | 3684469 | 2004-03-14 03:17:22 +0000 | [diff] [blame] | 562 | DEBUG(if (verifyFunction(*newFunction)) abort()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 563 | return newFunction; |
| 564 | } |
| 565 | |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 566 | bool CodeExtractor::isEligible(const std::vector<BasicBlock*> &code) { |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 567 | // Deny code region if it contains allocas or vastarts. |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 568 | for (std::vector<BasicBlock*>::const_iterator BB = code.begin(), e=code.end(); |
| 569 | BB != e; ++BB) |
| 570 | for (BasicBlock::const_iterator I = (*BB)->begin(), Ie = (*BB)->end(); |
| 571 | I != Ie; ++I) |
| 572 | if (isa<AllocaInst>(*I)) |
| 573 | return false; |
Chris Lattner | 3b2917b | 2004-05-12 06:01:40 +0000 | [diff] [blame^] | 574 | else if (const CallInst *CI = dyn_cast<CallInst>(I)) |
| 575 | if (const Function *F = CI->getCalledFunction()) |
| 576 | if (F->getIntrinsicID() == Intrinsic::vastart) |
| 577 | return false; |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 578 | return true; |
| 579 | } |
| 580 | |
| 581 | |
Misha Brukman | f44acae | 2004-03-02 00:20:57 +0000 | [diff] [blame] | 582 | /// ExtractCodeRegion - slurp a sequence of basic blocks into a brand new |
| 583 | /// function |
| 584 | /// |
Chris Lattner | 37de257 | 2004-03-18 03:49:40 +0000 | [diff] [blame] | 585 | Function* llvm::ExtractCodeRegion(DominatorSet &DS, |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 586 | const std::vector<BasicBlock*> &code, |
| 587 | bool AggregateArgs) { |
| 588 | return CodeExtractor(&DS, AggregateArgs).ExtractCodeRegion(code); |
Misha Brukman | f44acae | 2004-03-02 00:20:57 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 591 | /// ExtractBasicBlock - slurp a natural loop into a brand new function |
| 592 | /// |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 593 | Function* llvm::ExtractLoop(DominatorSet &DS, Loop *L, bool AggregateArgs) { |
| 594 | return CodeExtractor(&DS, AggregateArgs).ExtractCodeRegion(L->getBlocks()); |
Misha Brukman | caa1a5a | 2004-02-28 03:26:20 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 597 | /// ExtractBasicBlock - slurp a basic block into a brand new function |
| 598 | /// |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 599 | Function* llvm::ExtractBasicBlock(BasicBlock *BB, bool AggregateArgs) { |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 600 | std::vector<BasicBlock*> Blocks; |
| 601 | Blocks.push_back(BB); |
Misha Brukman | 3596f0a | 2004-04-23 23:54:17 +0000 | [diff] [blame] | 602 | return CodeExtractor(0, AggregateArgs).ExtractCodeRegion(Blocks); |
Misha Brukman | 5af2be7 | 2004-03-01 18:28:34 +0000 | [diff] [blame] | 603 | } |