blob: 4e131b4a11ada922a74a2985c449010b36756588 [file] [log] [blame]
Misha Brukmancaa1a5a2004-02-28 03:26:20 +00001//===- 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 Lattnercee34042004-03-18 03:15:29 +000016#include "llvm/Transforms/Utils/FunctionUtils.h"
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000017#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 Lattner37de2572004-03-18 03:49:40 +000022#include "llvm/Analysis/Dominators.h"
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000023#include "llvm/Analysis/LoopInfo.h"
Chris Lattner36844692004-03-14 03:17:22 +000024#include "llvm/Analysis/Verifier.h"
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000025#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000026#include "Support/Debug.h"
27#include "Support/StringExtras.h"
28#include <algorithm>
Chris Lattner9c431f62004-03-14 22:34:55 +000029#include <set>
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000030using namespace llvm;
31
32namespace {
Chris Lattner37de2572004-03-18 03:49:40 +000033 class CodeExtractor {
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000034 typedef std::vector<Value*> Values;
Chris Lattner9c431f62004-03-14 22:34:55 +000035 std::set<BasicBlock*> BlocksToExtract;
Chris Lattner37de2572004-03-18 03:49:40 +000036 DominatorSet *DS;
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000037 public:
Chris Lattner37de2572004-03-18 03:49:40 +000038 CodeExtractor(DominatorSet *ds = 0) : DS(ds) {}
39
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000040 Function *ExtractCodeRegion(const std::vector<BasicBlock*> &code);
41
42 private:
Chris Lattner9c431f62004-03-14 22:34:55 +000043 void findInputsOutputs(Values &inputs, Values &outputs,
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000044 BasicBlock *newHeader,
45 BasicBlock *newRootNode);
46
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000047 Function *constructFunction(const Values &inputs,
48 const Values &outputs,
Chris Lattner320d59f2004-03-18 05:28:49 +000049 BasicBlock *header,
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000050 BasicBlock *newRootNode, BasicBlock *newHeader,
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000051 Function *oldFunction, Module *M);
52
Chris Lattner9c431f62004-03-14 22:34:55 +000053 void moveCodeToFunction(Function *newFunction);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000054
55 void emitCallAndSwitchStatement(Function *newFunction,
56 BasicBlock *newHeader,
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000057 Values &inputs,
58 Values &outputs);
59
60 };
61}
62
Chris Lattner73ab1fa2004-03-15 01:18:23 +000063void CodeExtractor::findInputsOutputs(Values &inputs, Values &outputs,
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000064 BasicBlock *newHeader,
Chris Lattner73ab1fa2004-03-15 01:18:23 +000065 BasicBlock *newRootNode) {
Chris Lattner9c431f62004-03-14 22:34:55 +000066 for (std::set<BasicBlock*>::const_iterator ci = BlocksToExtract.begin(),
67 ce = BlocksToExtract.end(); ci != ce; ++ci) {
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000068 BasicBlock *BB = *ci;
Chris Lattner73ab1fa2004-03-15 01:18:23 +000069 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 Lattner320d59f2004-03-18 05:28:49 +000072 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);
78 }
Chris Lattner73ab1fa2004-03-15 01:18:23 +000079 } else {
80 // All other instructions go through the generic input finder
81 // Loop over the operands of each instruction (inputs)
82 for (User::op_iterator op = I->op_begin(), opE = I->op_end();
83 op != opE; ++op)
84 if (Instruction *opI = dyn_cast<Instruction>(*op)) {
85 // Check if definition of this operand is within the loop
Chris Lattnerfb87cde2004-03-15 01:26:44 +000086 if (!BlocksToExtract.count(opI->getParent()))
Chris Lattner73ab1fa2004-03-15 01:18:23 +000087 inputs.push_back(opI);
Chris Lattner73ab1fa2004-03-15 01:18:23 +000088 } else if (isa<Argument>(*op)) {
89 inputs.push_back(*op);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +000090 }
Chris Lattner73ab1fa2004-03-15 01:18:23 +000091 }
92
93 // Consider uses of this instruction (outputs)
94 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
95 UI != E; ++UI)
Chris Lattner37de2572004-03-18 03:49:40 +000096 if (!BlocksToExtract.count(cast<Instruction>(*UI)->getParent())) {
97 outputs.push_back(I);
98 break;
99 }
Chris Lattner73ab1fa2004-03-15 01:18:23 +0000100 } // for: insts
101 } // for: basic blocks
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000102}
103
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000104/// constructFunction - make a function based on inputs and outputs, as follows:
105/// f(in0, ..., inN, out0, ..., outN)
106///
107Function *CodeExtractor::constructFunction(const Values &inputs,
108 const Values &outputs,
Chris Lattner320d59f2004-03-18 05:28:49 +0000109 BasicBlock *header,
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000110 BasicBlock *newRootNode,
111 BasicBlock *newHeader,
Chris Lattner320d59f2004-03-18 05:28:49 +0000112 Function *oldFunction,
113 Module *M) {
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000114 DEBUG(std::cerr << "inputs: " << inputs.size() << "\n");
115 DEBUG(std::cerr << "outputs: " << outputs.size() << "\n");
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000116
117 // This function returns unsigned, outputs will go back by reference.
118 Type *retTy = Type::UShortTy;
119 std::vector<const Type*> paramTy;
120
121 // Add the types of the input values to the function's argument list
122 for (Values::const_iterator i = inputs.begin(),
123 e = inputs.end(); i != e; ++i) {
124 const Value *value = *i;
125 DEBUG(std::cerr << "value used in func: " << value << "\n");
126 paramTy.push_back(value->getType());
127 }
128
Chris Lattner37de2572004-03-18 03:49:40 +0000129 // Add the types of the output values to the function's argument list.
130 for (Values::const_iterator I = outputs.begin(), E = outputs.end();
131 I != E; ++I) {
132 DEBUG(std::cerr << "instr used in func: " << *I << "\n");
133 paramTy.push_back(PointerType::get((*I)->getType()));
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000134 }
135
136 DEBUG(std::cerr << "Function type: " << retTy << " f(");
137 for (std::vector<const Type*>::iterator i = paramTy.begin(),
138 e = paramTy.end(); i != e; ++i)
Chris Lattnerfb87cde2004-03-15 01:26:44 +0000139 DEBUG(std::cerr << *i << ", ");
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000140 DEBUG(std::cerr << ")\n");
141
142 const FunctionType *funcType = FunctionType::get(retTy, paramTy, false);
143
144 // Create the new function
145 Function *newFunction = new Function(funcType,
146 GlobalValue::InternalLinkage,
147 oldFunction->getName() + "_code", M);
148 newFunction->getBasicBlockList().push_back(newRootNode);
149
Chris Lattner37de2572004-03-18 03:49:40 +0000150 // Create an iterator to name all of the arguments we inserted.
151 Function::aiterator AI = newFunction->abegin();
152
153 // Rewrite all users of the inputs in the extracted region to use the
154 // arguments instead.
155 for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI) {
156 AI->setName(inputs[i]->getName());
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000157 std::vector<User*> Users(inputs[i]->use_begin(), inputs[i]->use_end());
158 for (std::vector<User*>::iterator use = Users.begin(), useE = Users.end();
Chris Lattner36844692004-03-14 03:17:22 +0000159 use != useE; ++use)
160 if (Instruction* inst = dyn_cast<Instruction>(*use))
Chris Lattner9c431f62004-03-14 22:34:55 +0000161 if (BlocksToExtract.count(inst->getParent()))
Chris Lattner37de2572004-03-18 03:49:40 +0000162 inst->replaceUsesOfWith(inputs[i], AI);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000163 }
164
Chris Lattner37de2572004-03-18 03:49:40 +0000165 // Set names for all of the output arguments.
166 for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI)
167 AI->setName(outputs[i]->getName()+".out");
168
169
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000170 // Rewrite branches to basic blocks outside of the loop to new dummy blocks
171 // within the new function. This must be done before we lose track of which
172 // blocks were originally in the code region.
173 std::vector<User*> Users(header->use_begin(), header->use_end());
Chris Lattner320d59f2004-03-18 05:28:49 +0000174 for (unsigned i = 0, e = Users.size(); i != e; ++i)
175 // The BasicBlock which contains the branch is not in the region
176 // modify the branch target to a new block
177 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Users[i]))
178 if (!BlocksToExtract.count(TI->getParent()) &&
179 TI->getParent()->getParent() == oldFunction)
180 TI->replaceUsesOfWith(header, newHeader);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000181
182 return newFunction;
183}
184
Chris Lattnerb4d8bf32004-03-14 23:05:49 +0000185void CodeExtractor::moveCodeToFunction(Function *newFunction) {
Chris Lattner9c431f62004-03-14 22:34:55 +0000186 Function *oldFunc = (*BlocksToExtract.begin())->getParent();
Chris Lattner4fca71e2004-03-14 04:01:47 +0000187 Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList();
Chris Lattnerb4d8bf32004-03-14 23:05:49 +0000188 Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList();
Chris Lattner4fca71e2004-03-14 04:01:47 +0000189
Chris Lattner9c431f62004-03-14 22:34:55 +0000190 for (std::set<BasicBlock*>::const_iterator i = BlocksToExtract.begin(),
191 e = BlocksToExtract.end(); i != e; ++i) {
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000192 // Delete the basic block from the old function, and the list of blocks
Chris Lattner9c431f62004-03-14 22:34:55 +0000193 oldBlocks.remove(*i);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000194
195 // Insert this basic block into the new function
Chris Lattner9c431f62004-03-14 22:34:55 +0000196 newBlocks.push_back(*i);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000197 }
198}
199
200void
201CodeExtractor::emitCallAndSwitchStatement(Function *newFunction,
202 BasicBlock *codeReplacer,
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000203 Values &inputs,
Chris Lattner37de2572004-03-18 03:49:40 +0000204 Values &outputs) {
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000205 // Emit a call to the new function, passing allocated memory for outputs and
206 // just plain inputs for non-scalars
Chris Lattner5b2072e2004-03-14 23:43:24 +0000207 std::vector<Value*> params(inputs);
208
Chris Lattnerd8017a32004-03-18 04:12:05 +0000209 // Get an iterator to the first output argument.
210 Function::aiterator OutputArgBegin = newFunction->abegin();
211 std::advance(OutputArgBegin, inputs.size());
212
Chris Lattner37de2572004-03-18 03:49:40 +0000213 for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
214 Value *Output = outputs[i];
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000215 // Create allocas for scalar outputs
Chris Lattner37de2572004-03-18 03:49:40 +0000216 AllocaInst *alloca =
217 new AllocaInst(outputs[i]->getType(), 0, Output->getName()+".loc",
218 codeReplacer->getParent()->begin()->begin());
219 params.push_back(alloca);
220
221 LoadInst *load = new LoadInst(alloca, Output->getName()+".reload");
222 codeReplacer->getInstList().push_back(load);
223 std::vector<User*> Users(outputs[i]->use_begin(), outputs[i]->use_end());
224 for (unsigned u = 0, e = Users.size(); u != e; ++u) {
225 Instruction *inst = cast<Instruction>(Users[u]);
226 if (!BlocksToExtract.count(inst->getParent()))
227 inst->replaceUsesOfWith(outputs[i], load);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000228 }
229 }
Chris Lattnerb4d8bf32004-03-14 23:05:49 +0000230
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000231 CallInst *call = new CallInst(newFunction, params, "targetBlock");
Chris Lattner5b2072e2004-03-14 23:43:24 +0000232 codeReplacer->getInstList().push_front(call);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000233
234 // Now we can emit a switch statement using the call as a value.
Chris Lattner5b2072e2004-03-14 23:43:24 +0000235 SwitchInst *TheSwitch = new SwitchInst(call, codeReplacer, codeReplacer);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000236
237 // Since there may be multiple exits from the original region, make the new
Chris Lattnerb4d8bf32004-03-14 23:05:49 +0000238 // function return an unsigned, switch on that number. This loop iterates
239 // over all of the blocks in the extracted region, updating any terminator
240 // instructions in the to-be-extracted region that branch to blocks that are
241 // not in the region to be extracted.
242 std::map<BasicBlock*, BasicBlock*> ExitBlockMap;
243
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000244 unsigned switchVal = 0;
Chris Lattner9c431f62004-03-14 22:34:55 +0000245 for (std::set<BasicBlock*>::const_iterator i = BlocksToExtract.begin(),
246 e = BlocksToExtract.end(); i != e; ++i) {
Chris Lattnerb4d8bf32004-03-14 23:05:49 +0000247 TerminatorInst *TI = (*i)->getTerminator();
248 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
249 if (!BlocksToExtract.count(TI->getSuccessor(i))) {
250 BasicBlock *OldTarget = TI->getSuccessor(i);
251 // add a new basic block which returns the appropriate value
252 BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
253 if (!NewTarget) {
254 // If we don't already have an exit stub for this non-extracted
255 // destination, create one now!
256 NewTarget = new BasicBlock(OldTarget->getName() + ".exitStub",
257 newFunction);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000258
Chris Lattnerb4d8bf32004-03-14 23:05:49 +0000259 ConstantUInt *brVal = ConstantUInt::get(Type::UShortTy, switchVal++);
260 ReturnInst *NTRet = new ReturnInst(brVal, NewTarget);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000261
Chris Lattnerb4d8bf32004-03-14 23:05:49 +0000262 // Update the switch instruction.
Chris Lattner5b2072e2004-03-14 23:43:24 +0000263 TheSwitch->addCase(brVal, OldTarget);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000264
Chris Lattnerb4d8bf32004-03-14 23:05:49 +0000265 // Restore values just before we exit
266 // FIXME: Use a GetElementPtr to bunch the outputs in a struct
Chris Lattnerd8017a32004-03-18 04:12:05 +0000267 Function::aiterator OAI = OutputArgBegin;
268 for (unsigned out = 0, e = outputs.size(); out != e; ++out, ++OAI)
Chris Lattner37de2572004-03-18 03:49:40 +0000269 if (!DS ||
270 DS->dominates(cast<Instruction>(outputs[out])->getParent(),
271 TI->getParent()))
Chris Lattnerd8017a32004-03-18 04:12:05 +0000272 new StoreInst(outputs[out], OAI, NTRet);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000273 }
Chris Lattnerb4d8bf32004-03-14 23:05:49 +0000274
275 // rewrite the original branch instruction with this new target
276 TI->setSuccessor(i, NewTarget);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000277 }
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000278 }
Chris Lattner5b2072e2004-03-14 23:43:24 +0000279
280 // Now that we've done the deed, make the default destination of the switch
281 // instruction be one of the exit blocks of the region.
282 if (TheSwitch->getNumSuccessors() > 1) {
283 // FIXME: this is broken w.r.t. PHI nodes, but the old code was more broken.
284 // This edge is not traversable.
285 TheSwitch->setSuccessor(0, TheSwitch->getSuccessor(1));
286 }
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000287}
288
289
290/// ExtractRegion - Removes a loop from a function, replaces it with a call to
291/// new function. Returns pointer to the new function.
292///
293/// algorithm:
294///
295/// find inputs and outputs for the region
296///
297/// for inputs: add to function as args, map input instr* to arg#
298/// for outputs: add allocas for scalars,
299/// add to func as args, map output instr* to arg#
300///
301/// rewrite func to use argument #s instead of instr*
302///
303/// for each scalar output in the function: at every exit, store intermediate
304/// computed result back into memory.
305///
306Function *CodeExtractor::ExtractCodeRegion(const std::vector<BasicBlock*> &code)
307{
308 // 1) Find inputs, outputs
309 // 2) Construct new function
310 // * Add allocas for defs, pass as args by reference
311 // * Pass in uses as args
312 // 3) Move code region, add call instr to func
Chris Lattner9c431f62004-03-14 22:34:55 +0000313 //
314 BlocksToExtract.insert(code.begin(), code.end());
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000315
316 Values inputs, outputs;
317
318 // Assumption: this is a single-entry code region, and the header is the first
Chris Lattner73ab1fa2004-03-15 01:18:23 +0000319 // block in the region.
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000320 BasicBlock *header = code[0];
Chris Lattner73ab1fa2004-03-15 01:18:23 +0000321 for (unsigned i = 1, e = code.size(); i != e; ++i)
322 for (pred_iterator PI = pred_begin(code[i]), E = pred_end(code[i]);
323 PI != E; ++PI)
324 assert(BlocksToExtract.count(*PI) &&
325 "No blocks in this region may have entries from outside the region"
326 " except for the first block!");
327
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000328 Function *oldFunction = header->getParent();
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000329
330 // This takes place of the original loop
331 BasicBlock *codeReplacer = new BasicBlock("codeRepl", oldFunction);
332
333 // The new function needs a root node because other nodes can branch to the
334 // head of the loop, and the root cannot have predecessors
335 BasicBlock *newFuncRoot = new BasicBlock("newFuncRoot");
336 newFuncRoot->getInstList().push_back(new BranchInst(header));
337
338 // Find inputs to, outputs from the code region
339 //
340 // If one of the inputs is coming from a different basic block and it's in a
341 // phi node, we need to rewrite the phi node:
342 //
343 // * All the inputs which involve basic blocks OUTSIDE of this region go into
344 // a NEW phi node that takes care of finding which value really came in.
345 // The result of this phi is passed to the function as an argument.
346 //
347 // * All the other phi values stay.
348 //
349 // FIXME: PHI nodes' incoming blocks aren't being rewritten to accomodate for
350 // blocks moving to a new function.
351 // SOLUTION: move Phi nodes out of the loop header into the codeReplacer, pass
352 // the values as parameters to the function
Chris Lattner9c431f62004-03-14 22:34:55 +0000353 findInputsOutputs(inputs, outputs, codeReplacer, newFuncRoot);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000354
355 // Step 2: Construct new function based on inputs/outputs,
356 // Add allocas for all defs
Chris Lattner320d59f2004-03-18 05:28:49 +0000357 Function *newFunction = constructFunction(inputs, outputs, code[0],
358 newFuncRoot,
Chris Lattner73ab1fa2004-03-15 01:18:23 +0000359 codeReplacer, oldFunction,
360 oldFunction->getParent());
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000361
Chris Lattner9c431f62004-03-14 22:34:55 +0000362 emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000363
Chris Lattner9c431f62004-03-14 22:34:55 +0000364 moveCodeToFunction(newFunction);
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000365
Chris Lattner320d59f2004-03-18 05:28:49 +0000366 // Loop over all of the PHI nodes in the entry block (code[0]), and change any
367 // references to the old incoming edge to be the new incoming edge.
368 for (BasicBlock::iterator I = code[0]->begin();
369 PHINode *PN = dyn_cast<PHINode>(I); ++I)
370 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
371 if (!BlocksToExtract.count(PN->getIncomingBlock(i)))
372 PN->setIncomingBlock(i, newFuncRoot);
373
Chris Lattner36844692004-03-14 03:17:22 +0000374 DEBUG(if (verifyFunction(*newFunction)) abort());
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000375 return newFunction;
376}
377
Misha Brukmanf44acae2004-03-02 00:20:57 +0000378/// ExtractCodeRegion - slurp a sequence of basic blocks into a brand new
379/// function
380///
Chris Lattner37de2572004-03-18 03:49:40 +0000381Function* llvm::ExtractCodeRegion(DominatorSet &DS,
382 const std::vector<BasicBlock*> &code) {
383 return CodeExtractor(&DS).ExtractCodeRegion(code);
Misha Brukmanf44acae2004-03-02 00:20:57 +0000384}
385
Misha Brukman5af2be72004-03-01 18:28:34 +0000386/// ExtractBasicBlock - slurp a natural loop into a brand new function
387///
Chris Lattner37de2572004-03-18 03:49:40 +0000388Function* llvm::ExtractLoop(DominatorSet &DS, Loop *L) {
389 return CodeExtractor(&DS).ExtractCodeRegion(L->getBlocks());
Misha Brukmancaa1a5a2004-02-28 03:26:20 +0000390}
391
Misha Brukman5af2be72004-03-01 18:28:34 +0000392/// ExtractBasicBlock - slurp a basic block into a brand new function
393///
394Function* llvm::ExtractBasicBlock(BasicBlock *BB) {
Misha Brukman5af2be72004-03-01 18:28:34 +0000395 std::vector<BasicBlock*> Blocks;
396 Blocks.push_back(BB);
Chris Lattner4fca71e2004-03-14 04:01:47 +0000397 return CodeExtractor().ExtractCodeRegion(Blocks);
Misha Brukman5af2be72004-03-01 18:28:34 +0000398}