Tanya Lattner | e2d74c1 | 2003-05-31 20:01:37 +0000 | [diff] [blame] | 1 | //===- CloneTrace.cpp - Clone a trace -------------------------------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Lattner | 71e56e2 | 2003-05-30 15:50:18 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the CloneTrace interface, which is used |
| 11 | // when writing runtime optimizations. It takes a vector of basic blocks |
Tanya Lattner | e2d74c1 | 2003-05-31 20:01:37 +0000 | [diff] [blame] | 12 | // 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 Lattner | 71e56e2 | 2003-05-30 15:50:18 +0000 | [diff] [blame] | 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Tanya Lattner | e2d74c1 | 2003-05-31 20:01:37 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Utils/Cloning.h" |
Tanya Lattner | 71e56e2 | 2003-05-30 15:50:18 +0000 | [diff] [blame] | 19 | #include "llvm/iPHINode.h" |
| 20 | #include "llvm/Function.h" |
Tanya Lattner | 71e56e2 | 2003-05-30 15:50:18 +0000 | [diff] [blame] | 21 | |
| 22 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 23 | namespace llvm { |
| 24 | |
Tanya Lattner | 71e56e2 | 2003-05-30 15:50:18 +0000 | [diff] [blame] | 25 | //Clones the trace (a vector of basic blocks) |
Tanya Lattner | e2d74c1 | 2003-05-31 20:01:37 +0000 | [diff] [blame] | 26 | std::vector<BasicBlock *> CloneTrace(const std::vector<BasicBlock*> &origTrace) { |
Tanya Lattner | 71e56e2 | 2003-05-30 15:50:18 +0000 | [diff] [blame] | 27 | |
| 28 | 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 |
| 39 | BasicBlock *clonedBlock = CloneBasicBlock(*T, ValueMap); |
| 40 | |
| 41 | //Add it to our new trace |
| 42 | clonedTrace.push_back(clonedBlock); |
| 43 | |
| 44 | //Add this new mapping to our Value Map |
| 45 | ValueMap[*T] = clonedBlock; |
| 46 | |
| 47 | //Add this cloned BB to the old BB's function |
| 48 | (*T)->getParent()->getBasicBlockList().push_back(clonedBlock); |
| 49 | |
| 50 | //Loop over the phi instructions and delete operands |
| 51 | //that are from blocks not in the trace |
| 52 | //only do this if we are NOT the first block |
| 53 | if(T != origTrace.begin()) { |
| 54 | for (BasicBlock::iterator I = clonedBlock->begin(); |
| 55 | PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 56 | //get incoming value for the previous BB |
| 57 | Value *V = PN->getIncomingValueForBlock(*(T-1)); |
| 58 | assert(V && "No incoming value from a BasicBlock in our trace!"); |
| 59 | |
| 60 | //remap our phi node to point to incoming value |
| 61 | ValueMap[*&I] = V; |
| 62 | |
| 63 | //remove phi node |
| 64 | clonedBlock->getInstList().erase(PN); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | //Second loop to do the remapping |
| 70 | for(std::vector<BasicBlock *>::const_iterator BB = clonedTrace.begin(), |
| 71 | BE = clonedTrace.end(); BB != BE; ++BB) { |
| 72 | for(BasicBlock::iterator I = (*BB)->begin(); I != (*BB)->end(); ++I) { |
| 73 | |
| 74 | //Loop over all the operands of the instruction |
| 75 | for(unsigned op=0, E = I->getNumOperands(); op != E; ++op) { |
| 76 | const Value *Op = I->getOperand(op); |
| 77 | |
| 78 | //Get it out of the value map |
| 79 | Value *V = ValueMap[Op]; |
| 80 | |
| 81 | //If not in the value map, then its outside our trace so ignore |
| 82 | if(V != 0) |
| 83 | I->setOperand(op,V); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | //return new vector of basic blocks |
| 89 | return clonedTrace; |
| 90 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame^] | 91 | |
| 92 | } // End llvm namespace |