blob: 0a2c41f78560b0354a4f66a6c5502b2d47e96e88 [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 Lattner16bfdb52002-03-29 19:03:54 +00007#include "llvm/Function.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +00008#include <map>
9
Chris Lattnerf9986852002-04-27 07:27:19 +000010// FIXME: This should be merged with FunctionInlining
Chris Lattner16bfdb52002-03-29 19:03:54 +000011
12// RemapInstruction - Convert the instruction operands from referencing the
13// current values into those specified by ValueMap.
14//
15static inline void RemapInstruction(Instruction *I,
16 std::map<const Value *, Value*> &ValueMap) {
17 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
18 const Value *Op = I->getOperand(op);
19 Value *V = ValueMap[Op];
20 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
21 continue; // Globals and constants don't get relocated
22
23#ifndef NDEBUG
24 if (!V) {
Anand Shukla2ac04a02002-06-25 21:18:19 +000025 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
26 std::cerr << "\nInst = " << I;
Chris Lattner16bfdb52002-03-29 19:03:54 +000027 }
28#endif
29 assert(V && "Referenced value not in value map!");
30 I->setOperand(op, V);
31 }
32}
33
34// Clone OldFunc into NewFunc, transforming the old arguments into references to
35// ArgMap values.
36//
37void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
38 const std::vector<Value*> &ArgMap) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000039 assert(OldFunc->aempty() || !NewFunc->aempty() &&
Chris Lattner16bfdb52002-03-29 19:03:54 +000040 "Synthesization of arguments is not implemented yet!");
Chris Lattnerfda72b12002-06-25 16:12:52 +000041 assert(OldFunc->asize() == ArgMap.size() &&
Chris Lattner16bfdb52002-03-29 19:03:54 +000042 "Improper number of argument values to map specified!");
43
44 // Keep a mapping between the original function's values and the new
45 // duplicated code's values. This includes all of: Function arguments,
46 // instruction values, constant pool entries, and basic blocks.
47 //
48 std::map<const Value *, Value*> ValueMap;
49
50 // Add all of the function arguments to the mapping...
Chris Lattnerfda72b12002-06-25 16:12:52 +000051 unsigned i = 0;
52 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
53 I != E; ++I, ++i)
54 ValueMap[I] = ArgMap[i];
Chris Lattner16bfdb52002-03-29 19:03:54 +000055
56
57 // Loop over all of the basic blocks in the function, cloning them as
58 // appropriate.
59 //
60 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
61 BI != BE; ++BI) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000062 const BasicBlock &BB = *BI;
63 assert(BB.getTerminator() && "BasicBlock doesn't have terminator!?!?");
Chris Lattner16bfdb52002-03-29 19:03:54 +000064
65 // Create a new basic block to copy instructions into!
Chris Lattnerfda72b12002-06-25 16:12:52 +000066 BasicBlock *CBB = new BasicBlock(BB.getName(), NewFunc);
67 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattner16bfdb52002-03-29 19:03:54 +000068
69 // Loop over all instructions copying them over...
Chris Lattnerfda72b12002-06-25 16:12:52 +000070 for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
Chris Lattner16bfdb52002-03-29 19:03:54 +000071 II != IE; ++II) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000072 Instruction *NewInst = II->clone();
73 NewInst->setName(II->getName()); // Name is not cloned...
Chris Lattner16bfdb52002-03-29 19:03:54 +000074 CBB->getInstList().push_back(NewInst);
Chris Lattnerfda72b12002-06-25 16:12:52 +000075 ValueMap[II] = NewInst; // Add instruction map to value.
Chris Lattner16bfdb52002-03-29 19:03:54 +000076 }
77 }
78
79 // Loop over all of the instructions in the function, fixing up operand
80 // references as we go. This uses ValueMap to do all the hard work.
81 //
Chris Lattnerfda72b12002-06-25 16:12:52 +000082 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
83 BB != BE; ++BB) {
Chris Lattner16bfdb52002-03-29 19:03:54 +000084 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
85
86 // Loop over all instructions, fixing each one as we find it...
Chris Lattnerfda72b12002-06-25 16:12:52 +000087 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
88 RemapInstruction(II, ValueMap);
Chris Lattner16bfdb52002-03-29 19:03:54 +000089 }
90}