blob: f912d11500ef6a280f8d59027b3a12d79ba2c50d [file] [log] [blame]
Chris Lattner16bfdb52002-03-29 19:03:54 +00001
2// FIXME: document
3
Chris Lattner7608a462002-05-07 18:36:35 +00004#include "llvm/Transforms/Utils/CloneFunction.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +00005#include "llvm/Function.h"
6#include "llvm/BasicBlock.h"
7#include "llvm/Instruction.h"
8#include <map>
Anand Shukla2ac04a02002-06-25 21:18:19 +00009#include <iostream>
Chris Lattner16bfdb52002-03-29 19:03:54 +000010
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,
39 const std::vector<Value*> &ArgMap) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000040 assert(OldFunc->aempty() || !NewFunc->aempty() &&
Chris Lattner16bfdb52002-03-29 19:03:54 +000041 "Synthesization of arguments is not implemented yet!");
Chris Lattnerfda72b12002-06-25 16:12:52 +000042 assert(OldFunc->asize() == ArgMap.size() &&
Chris Lattner16bfdb52002-03-29 19:03:54 +000043 "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...
Chris Lattnerfda72b12002-06-25 16:12:52 +000052 unsigned i = 0;
53 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
54 I != E; ++I, ++i)
55 ValueMap[I] = ArgMap[i];
Chris Lattner16bfdb52002-03-29 19:03:54 +000056
57
58 // Loop over all of the basic blocks in the function, cloning them as
59 // appropriate.
60 //
61 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
62 BI != BE; ++BI) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000063 const BasicBlock &BB = *BI;
64 assert(BB.getTerminator() && "BasicBlock doesn't have terminator!?!?");
Chris Lattner16bfdb52002-03-29 19:03:54 +000065
66 // Create a new basic block to copy instructions into!
Chris Lattnerfda72b12002-06-25 16:12:52 +000067 BasicBlock *CBB = new BasicBlock(BB.getName(), NewFunc);
68 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattner16bfdb52002-03-29 19:03:54 +000069
70 // Loop over all instructions copying them over...
Chris Lattnerfda72b12002-06-25 16:12:52 +000071 for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
Chris Lattner16bfdb52002-03-29 19:03:54 +000072 II != IE; ++II) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000073 Instruction *NewInst = II->clone();
74 NewInst->setName(II->getName()); // Name is not cloned...
Chris Lattner16bfdb52002-03-29 19:03:54 +000075 CBB->getInstList().push_back(NewInst);
Chris Lattnerfda72b12002-06-25 16:12:52 +000076 ValueMap[II] = NewInst; // Add instruction map to value.
Chris Lattner16bfdb52002-03-29 19:03:54 +000077 }
78 }
79
80 // Loop over all of the instructions in the function, fixing up operand
81 // references as we go. This uses ValueMap to do all the hard work.
82 //
Chris Lattnerfda72b12002-06-25 16:12:52 +000083 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
84 BB != BE; ++BB) {
Chris Lattner16bfdb52002-03-29 19:03:54 +000085 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
86
87 // Loop over all instructions, fixing each one as we find it...
Chris Lattnerfda72b12002-06-25 16:12:52 +000088 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
89 RemapInstruction(II, ValueMap);
Chris Lattner16bfdb52002-03-29 19:03:54 +000090 }
91}