blob: 94a4750a2d40a8604eafd019bd6f122b6fc65dfc [file] [log] [blame]
Chris Lattner16bfdb52002-03-29 19:03:54 +00001
Chris Lattner16667512002-11-19 20:59:41 +00002
3
Chris Lattner16bfdb52002-03-29 19:03:54 +00004// FIXME: document
5
Chris Lattner16667512002-11-19 20:59:41 +00006#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattnerb1120052002-11-19 21:54:07 +00007#include "llvm/iTerminators.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +00008#include "llvm/Function.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +00009#include <map>
10
Chris Lattnerf9986852002-04-27 07:27:19 +000011// FIXME: This should be merged with FunctionInlining
Chris Lattner16bfdb52002-03-29 19:03:54 +000012
13// RemapInstruction - Convert the instruction operands from referencing the
14// current values into those specified by ValueMap.
15//
16static inline void RemapInstruction(Instruction *I,
17 std::map<const Value *, Value*> &ValueMap) {
18 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
19 const Value *Op = I->getOperand(op);
20 Value *V = ValueMap[Op];
21 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
22 continue; // Globals and constants don't get relocated
23
24#ifndef NDEBUG
25 if (!V) {
Anand Shukla2ac04a02002-06-25 21:18:19 +000026 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
27 std::cerr << "\nInst = " << I;
Chris Lattner16bfdb52002-03-29 19:03:54 +000028 }
29#endif
30 assert(V && "Referenced value not in value map!");
31 I->setOperand(op, V);
32 }
33}
34
35// Clone OldFunc into NewFunc, transforming the old arguments into references to
36// ArgMap values.
37//
38void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
Chris Lattnerb1120052002-11-19 21:54:07 +000039 const std::vector<Value*> &ArgMap,
40 std::vector<ReturnInst*> &Returns,
41 const char *NameSuffix) {
42 assert(NameSuffix && "NameSuffix cannot be null!");
Chris Lattnerfda72b12002-06-25 16:12:52 +000043 assert(OldFunc->asize() == ArgMap.size() &&
Chris Lattner16bfdb52002-03-29 19:03:54 +000044 "Improper number of argument values to map specified!");
45
46 // Keep a mapping between the original function's values and the new
47 // duplicated code's values. This includes all of: Function arguments,
48 // instruction values, constant pool entries, and basic blocks.
49 //
50 std::map<const Value *, Value*> ValueMap;
51
52 // Add all of the function arguments to the mapping...
Chris Lattnerfda72b12002-06-25 16:12:52 +000053 unsigned i = 0;
54 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
55 I != E; ++I, ++i)
56 ValueMap[I] = ArgMap[i];
Chris Lattner16bfdb52002-03-29 19:03:54 +000057
58
59 // Loop over all of the basic blocks in the function, cloning them as
Chris Lattnerb1120052002-11-19 21:54:07 +000060 // appropriate. Note that we save BE this way in order to handle cloning of
61 // recursive functions into themselves.
Chris Lattner16bfdb52002-03-29 19:03:54 +000062 //
63 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
64 BI != BE; ++BI) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000065 const BasicBlock &BB = *BI;
Chris Lattner16bfdb52002-03-29 19:03:54 +000066
67 // Create a new basic block to copy instructions into!
Chris Lattnerb1120052002-11-19 21:54:07 +000068 BasicBlock *CBB = new BasicBlock("", NewFunc);
69 if (BB.hasName()) CBB->setName(BB.getName()+NameSuffix);
Chris Lattnerfda72b12002-06-25 16:12:52 +000070 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattner16bfdb52002-03-29 19:03:54 +000071
72 // Loop over all instructions copying them over...
Chris Lattnerfda72b12002-06-25 16:12:52 +000073 for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
Chris Lattner16bfdb52002-03-29 19:03:54 +000074 II != IE; ++II) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000075 Instruction *NewInst = II->clone();
Chris Lattnerb1120052002-11-19 21:54:07 +000076 if (II->hasName())
77 NewInst->setName(II->getName()+NameSuffix); // Name is not cloned...
Chris Lattner16bfdb52002-03-29 19:03:54 +000078 CBB->getInstList().push_back(NewInst);
Chris Lattnerfda72b12002-06-25 16:12:52 +000079 ValueMap[II] = NewInst; // Add instruction map to value.
Chris Lattner16bfdb52002-03-29 19:03:54 +000080 }
Chris Lattnerb1120052002-11-19 21:54:07 +000081
82 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
83 Returns.push_back(RI);
Chris Lattner16bfdb52002-03-29 19:03:54 +000084 }
85
86 // Loop over all of the instructions in the function, fixing up operand
87 // references as we go. This uses ValueMap to do all the hard work.
88 //
Chris Lattnerfda72b12002-06-25 16:12:52 +000089 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
90 BB != BE; ++BB) {
Chris Lattner16bfdb52002-03-29 19:03:54 +000091 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
92
93 // Loop over all instructions, fixing each one as we find it...
Chris Lattnerfda72b12002-06-25 16:12:52 +000094 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
95 RemapInstruction(II, ValueMap);
Chris Lattner16bfdb52002-03-29 19:03:54 +000096 }
97}