blob: 442ff03df3d2022f06a5a2b7aef27a88e09940a4 [file] [log] [blame]
Chris Lattner8bce9882002-11-19 22:04:49 +00001//===- CloneFunction.cpp - Clone a function into another function ---------===//
2//
3// This file implements the CloneFunctionInto interface, which is used as the
4// low-level function cloner. This is used by the CloneFunction and function
5// inliner to do the dirty work of copying the body of a function around.
6//
7//===----------------------------------------------------------------------===//
Chris Lattner16bfdb52002-03-29 19:03:54 +00008
Chris Lattner16667512002-11-19 20:59:41 +00009#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattnerb1120052002-11-19 21:54:07 +000010#include "llvm/iTerminators.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +000011#include "llvm/Function.h"
Chris Lattner16bfdb52002-03-29 19:03:54 +000012#include <map>
13
Chris Lattner16bfdb52002-03-29 19:03:54 +000014// RemapInstruction - Convert the instruction operands from referencing the
15// current values into those specified by ValueMap.
16//
17static inline void RemapInstruction(Instruction *I,
18 std::map<const Value *, Value*> &ValueMap) {
19 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
20 const Value *Op = I->getOperand(op);
21 Value *V = ValueMap[Op];
22 if (!V && (isa<GlobalValue>(Op) || isa<Constant>(Op)))
23 continue; // Globals and constants don't get relocated
24
25#ifndef NDEBUG
26 if (!V) {
Anand Shukla2ac04a02002-06-25 21:18:19 +000027 std::cerr << "Val = \n" << Op << "Addr = " << (void*)Op;
28 std::cerr << "\nInst = " << I;
Chris Lattner16bfdb52002-03-29 19:03:54 +000029 }
30#endif
31 assert(V && "Referenced value not in value map!");
32 I->setOperand(op, V);
33 }
34}
35
36// Clone OldFunc into NewFunc, transforming the old arguments into references to
37// ArgMap values.
38//
39void CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
Chris Lattnerb1120052002-11-19 21:54:07 +000040 const std::vector<Value*> &ArgMap,
41 std::vector<ReturnInst*> &Returns,
42 const char *NameSuffix) {
43 assert(NameSuffix && "NameSuffix cannot be null!");
Chris Lattnerfda72b12002-06-25 16:12:52 +000044 assert(OldFunc->asize() == ArgMap.size() &&
Chris Lattner16bfdb52002-03-29 19:03:54 +000045 "Improper number of argument values to map specified!");
46
47 // Keep a mapping between the original function's values and the new
48 // duplicated code's values. This includes all of: Function arguments,
49 // instruction values, constant pool entries, and basic blocks.
50 //
51 std::map<const Value *, Value*> ValueMap;
52
53 // Add all of the function arguments to the mapping...
Chris Lattnerfda72b12002-06-25 16:12:52 +000054 unsigned i = 0;
55 for (Function::const_aiterator I = OldFunc->abegin(), E = OldFunc->aend();
56 I != E; ++I, ++i)
57 ValueMap[I] = ArgMap[i];
Chris Lattner16bfdb52002-03-29 19:03:54 +000058
59
60 // Loop over all of the basic blocks in the function, cloning them as
Chris Lattnerb1120052002-11-19 21:54:07 +000061 // appropriate. Note that we save BE this way in order to handle cloning of
62 // recursive functions into themselves.
Chris Lattner16bfdb52002-03-29 19:03:54 +000063 //
64 for (Function::const_iterator BI = OldFunc->begin(), BE = OldFunc->end();
65 BI != BE; ++BI) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000066 const BasicBlock &BB = *BI;
Chris Lattner16bfdb52002-03-29 19:03:54 +000067
68 // Create a new basic block to copy instructions into!
Chris Lattnerb1120052002-11-19 21:54:07 +000069 BasicBlock *CBB = new BasicBlock("", NewFunc);
70 if (BB.hasName()) CBB->setName(BB.getName()+NameSuffix);
Chris Lattnerfda72b12002-06-25 16:12:52 +000071 ValueMap[&BB] = CBB; // Add basic block mapping.
Chris Lattner16bfdb52002-03-29 19:03:54 +000072
73 // Loop over all instructions copying them over...
Chris Lattnerfda72b12002-06-25 16:12:52 +000074 for (BasicBlock::const_iterator II = BB.begin(), IE = BB.end();
Chris Lattner16bfdb52002-03-29 19:03:54 +000075 II != IE; ++II) {
Chris Lattnerfda72b12002-06-25 16:12:52 +000076 Instruction *NewInst = II->clone();
Chris Lattnerb1120052002-11-19 21:54:07 +000077 if (II->hasName())
78 NewInst->setName(II->getName()+NameSuffix); // Name is not cloned...
Chris Lattner16bfdb52002-03-29 19:03:54 +000079 CBB->getInstList().push_back(NewInst);
Chris Lattnerfda72b12002-06-25 16:12:52 +000080 ValueMap[II] = NewInst; // Add instruction map to value.
Chris Lattner16bfdb52002-03-29 19:03:54 +000081 }
Chris Lattnerb1120052002-11-19 21:54:07 +000082
83 if (ReturnInst *RI = dyn_cast<ReturnInst>(CBB->getTerminator()))
84 Returns.push_back(RI);
Chris Lattner16bfdb52002-03-29 19:03:54 +000085 }
86
87 // Loop over all of the instructions in the function, fixing up operand
88 // references as we go. This uses ValueMap to do all the hard work.
89 //
Chris Lattnerfda72b12002-06-25 16:12:52 +000090 for (Function::const_iterator BB = OldFunc->begin(), BE = OldFunc->end();
91 BB != BE; ++BB) {
Chris Lattner16bfdb52002-03-29 19:03:54 +000092 BasicBlock *NBB = cast<BasicBlock>(ValueMap[BB]);
93
94 // Loop over all instructions, fixing each one as we find it...
Chris Lattnerfda72b12002-06-25 16:12:52 +000095 for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); ++II)
96 RemapInstruction(II, ValueMap);
Chris Lattner16bfdb52002-03-29 19:03:54 +000097 }
98}