diff options
| author | Tanya Lattner <tonic@nondot.org> | 2003-05-30 15:50:18 +0000 | 
|---|---|---|
| committer | Tanya Lattner <tonic@nondot.org> | 2003-05-30 15:50:18 +0000 | 
| commit | 6074d2f37abaaa2957b4575d36fd9cd87d8ee08f (patch) | |
| tree | cefcd505f7e5cf5211ce6e2392dfb5d70334ac66 /lib/Transforms/Utils/CloneTrace.cpp | |
| parent | 2c49fc023ee0885c8c577829cd40c00ef48581fc (diff) | |
Added the CloneTrace function which clones traces. It takes a vector of basic blocks, removes
internal phi nodes, and returns a new vector of basic blocks.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6431 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils/CloneTrace.cpp')
| -rw-r--r-- | lib/Transforms/Utils/CloneTrace.cpp | 83 | 
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/Transforms/Utils/CloneTrace.cpp b/lib/Transforms/Utils/CloneTrace.cpp new file mode 100644 index 0000000000..e8578c512d --- /dev/null +++ b/lib/Transforms/Utils/CloneTrace.cpp @@ -0,0 +1,83 @@ +//===- CloneTrace.cpp - Clone a trace ---------===// +// +// This file implements the CloneTrace interface, which is used  +// when writing runtime optimizations. It takes a vector of basic blocks +// removes internal phi nodes, clones the basic blocks, and returns the new +// vector of basic blocks. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Instruction.h" +#include "llvm/BasicBlock.h" +#include "llvm/iPHINode.h" +#include "llvm/Function.h" +#include "llvm/Transforms/Utils/Cloning.h" +#include <map> + + +//Clones the trace (a vector of basic blocks) +std::vector<BasicBlock *> CloneTrace(std::vector<BasicBlock*> &origTrace) { + +  std::vector<BasicBlock *> clonedTrace; +  std::map<const Value*, Value*> ValueMap; +   +  //First, loop over all the Basic Blocks in the trace and copy +  //them using CloneBasicBlock. Also fix the phi nodes during +  //this loop. To fix the phi nodes, we delete incoming branches +  //that are not in the trace. +  for(std::vector<BasicBlock *>::const_iterator T = origTrace.begin(), +	End = origTrace.end(); T != End; ++T) { + +    //Clone Basic Block +    BasicBlock *clonedBlock = CloneBasicBlock(*T, ValueMap); +     +    //Add it to our new trace +    clonedTrace.push_back(clonedBlock); + +    //Add this new mapping to our Value Map +    ValueMap[*T] = clonedBlock; + +    //Add this cloned BB to the old BB's function +    (*T)->getParent()->getBasicBlockList().push_back(clonedBlock); + +    //Loop over the phi instructions and delete operands +    //that are from blocks not in the trace +    //only do this if we are NOT the first block +    if(T != origTrace.begin()) { +      for (BasicBlock::iterator I = clonedBlock->begin(); +	   PHINode *PN = dyn_cast<PHINode>(I); ++I) { +	//get incoming value for the previous BB +	Value *V = PN->getIncomingValueForBlock(*(T-1)); +	assert(V && "No incoming value from a BasicBlock in our trace!"); +	 +	//remap our phi node to point to incoming value +	ValueMap[*&I] = V; +	 +	//remove phi node +	clonedBlock->getInstList().erase(PN); +      } +    } +  } + +  //Second loop to do the remapping +  for(std::vector<BasicBlock *>::const_iterator BB = clonedTrace.begin(), +	BE = clonedTrace.end(); BB != BE; ++BB) { +    for(BasicBlock::iterator I = (*BB)->begin(); I != (*BB)->end(); ++I) { +       +      //Loop over all the operands of the instruction +      for(unsigned op=0, E = I->getNumOperands(); op != E; ++op) { +	const Value *Op = I->getOperand(op); +	 +	//Get it out of the value map +	Value *V = ValueMap[Op]; + +	//If not in the value map, then its outside our trace so ignore +	if(V != 0) +	  I->setOperand(op,V); +      } +    } +  } +   +  //return new vector of basic blocks +  return clonedTrace; +}  | 
