blob: 9d995464bf8dc48ccd08304971b60ebbfcf10e2e [file] [log] [blame]
Tanya Lattnere2d74c12003-05-31 20:01:37 +00001//===- CloneTrace.cpp - Clone a trace -------------------------------------===//
John Criswell482202a2003-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 Lattner71e56e22003-05-30 15:50:18 +00009//
10// This file implements the CloneTrace interface, which is used
11// when writing runtime optimizations. It takes a vector of basic blocks
Tanya Lattnere2d74c12003-05-31 20:01:37 +000012// clones the basic blocks, removes internal phi nodes, adds it to the
13// same function as the original (although there is no jump to it) and
14// returns the new vector of basic blocks.
Tanya Lattner71e56e22003-05-30 15:50:18 +000015//
16//===----------------------------------------------------------------------===//
17
Tanya Lattnere2d74c12003-05-31 20:01:37 +000018#include "llvm/Transforms/Utils/Cloning.h"
Tanya Lattner71e56e22003-05-30 15:50:18 +000019#include "llvm/iPHINode.h"
20#include "llvm/Function.h"
Tanya Lattner71e56e22003-05-30 15:50:18 +000021
22
23//Clones the trace (a vector of basic blocks)
Tanya Lattnere2d74c12003-05-31 20:01:37 +000024std::vector<BasicBlock *> CloneTrace(const std::vector<BasicBlock*> &origTrace) {
Tanya Lattner71e56e22003-05-30 15:50:18 +000025
26 std::vector<BasicBlock *> clonedTrace;
27 std::map<const Value*, Value*> ValueMap;
28
29 //First, loop over all the Basic Blocks in the trace and copy
30 //them using CloneBasicBlock. Also fix the phi nodes during
31 //this loop. To fix the phi nodes, we delete incoming branches
32 //that are not in the trace.
33 for(std::vector<BasicBlock *>::const_iterator T = origTrace.begin(),
34 End = origTrace.end(); T != End; ++T) {
35
36 //Clone Basic Block
37 BasicBlock *clonedBlock = CloneBasicBlock(*T, ValueMap);
38
39 //Add it to our new trace
40 clonedTrace.push_back(clonedBlock);
41
42 //Add this new mapping to our Value Map
43 ValueMap[*T] = clonedBlock;
44
45 //Add this cloned BB to the old BB's function
46 (*T)->getParent()->getBasicBlockList().push_back(clonedBlock);
47
48 //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}