blob: 1cedfd872c6a3135c81cca54766719174fb10e0e [file] [log] [blame]
Tanya Lattner658c5bc2003-05-31 20:01:37 +00001//===- CloneTrace.cpp - Clone a trace -------------------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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"
Anton Korobeynikov344ef192007-11-09 12:27:04 +000022#include "llvm/Transforms/Utils/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;
Chris Lattner5e665f52007-02-03 00:08:31 +000029 DenseMap<const Value*, Value*> ValueMap;
Misha Brukmanfd939082005-04-21 23:48:37 +000030
Tanya Lattner6074d2f2003-05-30 15:50:18 +000031 //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.
Anton Korobeynikov20a990e2007-11-09 12:34:20 +000035 for (std::vector<BasicBlock *>::const_iterator T = origTrace.begin(),
Jeff Cohen9d809302005-04-23 21:38:35 +000036 End = origTrace.end(); T != End; ++T) {
Tanya Lattner6074d2f2003-05-30 15:50:18 +000037
38 //Clone Basic Block
Chris Lattner23b4c682004-02-04 01:19:43 +000039 BasicBlock *clonedBlock =
40 CloneBasicBlock(*T, ValueMap, ".tr", (*T)->getParent());
Misha Brukmanfd939082005-04-21 23:48:37 +000041
Tanya Lattner6074d2f2003-05-30 15:50:18 +000042 //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
Anton Korobeynikov20a990e2007-11-09 12:34:20 +000051 if (T != origTrace.begin()) {
Tanya Lattner6074d2f2003-05-30 15:50:18 +000052 for (BasicBlock::iterator I = clonedBlock->begin();
Reid Spencer2da5c3d2004-09-15 17:06:42 +000053 isa<PHINode>(I); ++I) {
54 PHINode *PN = cast<PHINode>(I);
55 //get incoming value for the previous BB
56 Value *V = PN->getIncomingValueForBlock(*(T-1));
57 assert(V && "No incoming value from a BasicBlock in our trace!");
Misha Brukmanfd939082005-04-21 23:48:37 +000058
Reid Spencer2da5c3d2004-09-15 17:06:42 +000059 //remap our phi node to point to incoming value
60 ValueMap[*&I] = V;
Misha Brukmanfd939082005-04-21 23:48:37 +000061
Reid Spencer2da5c3d2004-09-15 17:06:42 +000062 //remove phi node
63 clonedBlock->getInstList().erase(PN);
Tanya Lattner6074d2f2003-05-30 15:50:18 +000064 }
65 }
66 }
67
68 //Second loop to do the remapping
Anton Korobeynikov20a990e2007-11-09 12:34:20 +000069 for (std::vector<BasicBlock *>::const_iterator BB = clonedTrace.begin(),
Jeff Cohen9d809302005-04-23 21:38:35 +000070 BE = clonedTrace.end(); BB != BE; ++BB) {
Nick Lewycky529de8a2008-03-09 05:24:34 +000071
72 //Remap the unwind_to label
73 if (BasicBlock *UnwindDest = (*BB)->getUnwindDest())
74 (*BB)->setUnwindDest(cast<BasicBlock>(ValueMap[UnwindDest]));
75
Anton Korobeynikov20a990e2007-11-09 12:34:20 +000076 for (BasicBlock::iterator I = (*BB)->begin(); I != (*BB)->end(); ++I) {
Tanya Lattner6074d2f2003-05-30 15:50:18 +000077 //Loop over all the operands of the instruction
Anton Korobeynikov20a990e2007-11-09 12:34:20 +000078 for (unsigned op=0, E = I->getNumOperands(); op != E; ++op) {
79 const Value *Op = I->getOperand(op);
Tanya Lattner6074d2f2003-05-30 15:50:18 +000080
Anton Korobeynikov20a990e2007-11-09 12:34:20 +000081 //Get it out of the value map
82 Value *V = ValueMap[Op];
Jeff Cohen9d809302005-04-23 21:38:35 +000083
Anton Korobeynikov20a990e2007-11-09 12:34:20 +000084 //If not in the value map, then its outside our trace so ignore
85 if (V != 0)
86 I->setOperand(op,V);
Tanya Lattner6074d2f2003-05-30 15:50:18 +000087 }
88 }
89 }
Misha Brukmanfd939082005-04-21 23:48:37 +000090
Tanya Lattner6074d2f2003-05-30 15:50:18 +000091 //return new vector of basic blocks
92 return clonedTrace;
93}
Brian Gaekec58a7f42004-05-19 09:08:14 +000094
95/// CloneTraceInto - Clone T into NewFunc. Original<->clone mapping is
96/// saved in ValueMap.
97///
98void llvm::CloneTraceInto(Function *NewFunc, Trace &T,
Chris Lattner5e665f52007-02-03 00:08:31 +000099 DenseMap<const Value*, Value*> &ValueMap,
Brian Gaekec58a7f42004-05-19 09:08:14 +0000100 const char *NameSuffix) {
101 assert(NameSuffix && "NameSuffix cannot be null!");
102
103 // Loop over all of the basic blocks in the trace, cloning them as
104 // appropriate.
105 //
106 for (Trace::const_iterator BI = T.begin(), BE = T.end(); BI != BE; ++BI) {
107 const BasicBlock *BB = *BI;
108
109 // Create a new basic block and copy instructions into it!
110 BasicBlock *CBB = CloneBasicBlock(BB, ValueMap, NameSuffix, NewFunc);
111 ValueMap[BB] = CBB; // Add basic block mapping.
112 }
113
114 // Loop over all of the instructions in the new function, fixing up operand
115 // references as we go. This uses ValueMap to do all the hard work.
116 //
117 for (Function::iterator BB =
118 cast<BasicBlock>(ValueMap[T.getEntryBasicBlock()]),
119 BE = NewFunc->end(); BB != BE; ++BB)
120 // Loop over all instructions, fixing each one as we find it...
121 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
122 RemapInstruction(II, ValueMap);
123}
124