blob: bfd7c6f60652b11ea4224525afccdb9568a2621d [file] [log] [blame]
Tanya Lattner658c5bc2003-05-31 20:01:37 +00001//===- CloneTrace.cpp - Clone a trace -------------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Tanya Lattner6074d2f2003-05-30 15:50:18 +00009//
Chris Lattner23b4c682004-02-04 01:19:43 +000010// This file implements the CloneTrace interface, which is used when writing
11// runtime optimizations. It takes a vector of basic blocks clones the basic
12// blocks, removes internal phi nodes, adds it to the same function as the
13// original (although there is no jump to it) and returns the new vector of
14// basic blocks.
Tanya Lattner6074d2f2003-05-30 15:50:18 +000015//
16//===----------------------------------------------------------------------===//
17
Brian Gaekec58a7f42004-05-19 09:08:14 +000018#include "llvm/Analysis/Trace.h"
Tanya Lattner658c5bc2003-05-31 20:01:37 +000019#include "llvm/Transforms/Utils/Cloning.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000020#include "llvm/Instructions.h"
Tanya Lattner6074d2f2003-05-30 15:50:18 +000021#include "llvm/Function.h"
Brian Gaekec58a7f42004-05-19 09:08:14 +000022#include "ValueMapper.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000023using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000024
Tanya Lattner6074d2f2003-05-30 15:50:18 +000025//Clones the trace (a vector of basic blocks)
Chris Lattnerf7703df2004-01-09 06:12:26 +000026std::vector<BasicBlock *>
27llvm::CloneTrace(const std::vector<BasicBlock*> &origTrace) {
Tanya Lattner6074d2f2003-05-30 15:50:18 +000028 std::vector<BasicBlock *> clonedTrace;
29 std::map<const Value*, Value*> ValueMap;
30
31 //First, loop over all the Basic Blocks in the trace and copy
32 //them using CloneBasicBlock. Also fix the phi nodes during
33 //this loop. To fix the phi nodes, we delete incoming branches
34 //that are not in the trace.
35 for(std::vector<BasicBlock *>::const_iterator T = origTrace.begin(),
36 End = origTrace.end(); T != End; ++T) {
37
38 //Clone Basic Block
Chris Lattner23b4c682004-02-04 01:19:43 +000039 BasicBlock *clonedBlock =
40 CloneBasicBlock(*T, ValueMap, ".tr", (*T)->getParent());
Tanya Lattner6074d2f2003-05-30 15:50:18 +000041
42 //Add it to our new trace
43 clonedTrace.push_back(clonedBlock);
44
45 //Add this new mapping to our Value Map
46 ValueMap[*T] = clonedBlock;
47
Tanya Lattner6074d2f2003-05-30 15:50:18 +000048 //Loop over the phi instructions and delete operands
49 //that are from blocks not in the trace
50 //only do this if we are NOT the first block
51 if(T != origTrace.begin()) {
52 for (BasicBlock::iterator I = clonedBlock->begin();
53 PHINode *PN = dyn_cast<PHINode>(I); ++I) {
54 //get incoming value for the previous BB
55 Value *V = PN->getIncomingValueForBlock(*(T-1));
56 assert(V && "No incoming value from a BasicBlock in our trace!");
57
58 //remap our phi node to point to incoming value
59 ValueMap[*&I] = V;
60
61 //remove phi node
62 clonedBlock->getInstList().erase(PN);
63 }
64 }
65 }
66
67 //Second loop to do the remapping
68 for(std::vector<BasicBlock *>::const_iterator BB = clonedTrace.begin(),
69 BE = clonedTrace.end(); BB != BE; ++BB) {
70 for(BasicBlock::iterator I = (*BB)->begin(); I != (*BB)->end(); ++I) {
71
72 //Loop over all the operands of the instruction
73 for(unsigned op=0, E = I->getNumOperands(); op != E; ++op) {
74 const Value *Op = I->getOperand(op);
75
76 //Get it out of the value map
77 Value *V = ValueMap[Op];
78
79 //If not in the value map, then its outside our trace so ignore
80 if(V != 0)
81 I->setOperand(op,V);
82 }
83 }
84 }
85
86 //return new vector of basic blocks
87 return clonedTrace;
88}
Brian Gaekec58a7f42004-05-19 09:08:14 +000089
90/// CloneTraceInto - Clone T into NewFunc. Original<->clone mapping is
91/// saved in ValueMap.
92///
93void llvm::CloneTraceInto(Function *NewFunc, Trace &T,
94 std::map<const Value*, Value*> &ValueMap,
95 const char *NameSuffix) {
96 assert(NameSuffix && "NameSuffix cannot be null!");
97
98 // Loop over all of the basic blocks in the trace, cloning them as
99 // appropriate.
100 //
101 for (Trace::const_iterator BI = T.begin(), BE = T.end(); BI != BE; ++BI) {
102 const BasicBlock *BB = *BI;
103
104 // Create a new basic block and copy instructions into it!
105 BasicBlock *CBB = CloneBasicBlock(BB, ValueMap, NameSuffix, NewFunc);
106 ValueMap[BB] = CBB; // Add basic block mapping.
107 }
108
109 // Loop over all of the instructions in the new function, fixing up operand
110 // references as we go. This uses ValueMap to do all the hard work.
111 //
112 for (Function::iterator BB =
113 cast<BasicBlock>(ValueMap[T.getEntryBasicBlock()]),
114 BE = NewFunc->end(); BB != BE; ++BB)
115 // Loop over all instructions, fixing each one as we find it...
116 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
117 RemapInstruction(II, ValueMap);
118}
119