blob: 808ea48e673adf175bdda017f4c08ca64a1bf0e9 [file] [log] [blame]
Chris Lattnerfa703a42002-03-29 19:03:54 +00001
2// FIXME: document
3
4#include "llvm/Transforms/CloneFunction.h"
5#include "llvm/Function.h"
6#include "llvm/BasicBlock.h"
7#include "llvm/Instruction.h"
8#include <map>
9
Chris Lattner483e14e2002-04-27 07:27:19 +000010// FIXME: This should be merged with FunctionInlining
Chris Lattnerfa703a42002-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) {
25 cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
26 cerr << "\nInst = " << I;
27 }
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) {
39 assert(OldFunc->getArgumentList().empty() ||
40 !NewFunc->getArgumentList().empty() &&
41 "Synthesization of arguments is not implemented yet!");
42 assert(OldFunc->getArgumentList().size() == ArgMap.size() &&
43 "Improper number of argument values to map specified!");
44
45 // Keep a mapping between the original function's values and the new
46 // duplicated code's values. This includes all of: Function arguments,
47 // instruction values, constant pool entries, and basic blocks.
48 //
49 std::map<const Value *, Value*> ValueMap;
50
51 // Add all of the function arguments to the mapping...
52 for (unsigned i = 0, e = ArgMap.size(); i != e; ++i)
53 ValueMap[(Value*)OldFunc->getArgumentList()[i]] = ArgMap[i];
54
55
56 // Loop over all of the basic blocks in the function, cloning them as
57 // appropriate.
58 //
59 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
60 BI != BE; ++BI) {
61 const BasicBlock *BB = *BI;
62 assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
63
64 // Create a new basic block to copy instructions into!
65 BasicBlock *CBB = new BasicBlock(BB->getName(), NewFunc);
66 ValueMap[BB] = CBB; // Add basic block mapping.
67
68 // Loop over all instructions copying them over...
69 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end();
70 II != IE; ++II) {
71 Instruction *NewInst = (*II)->clone();
72 NewInst->setName((*II)->getName()); // Name is not cloned...
73 CBB->getInstList().push_back(NewInst);
74 ValueMap[*II] = NewInst; // Add instruction map to value.
75 }
76 }
77
78 // Loop over all of the instructions in the function, fixing up operand
79 // references as we go. This uses ValueMap to do all the hard work.
80 //
81 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
82 BI != BE; ++BI) {
83 const BasicBlock *BB = *BI;
84 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
85
86 // Loop over all instructions, fixing each one as we find it...
87 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
88 RemapInstruction(*II, ValueMap);
89 }
90}