+ // don't revisit nodes
+ if (Visited.count(BB)) return;
+
+ // mark as visited
+ Visited.insert(BB);
+
+ // keep track of the value of each variable we're watching.. how?
+ for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) {
+ Instruction *I = *II; //get the instruction
+
+ if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
+ Value *Ptr = LI->getPointerOperand();
+
+ if (AllocaInst *Src = dyn_cast<AllocaInst>(Ptr)) {
+ map<Instruction*, unsigned>::iterator AI = AllocaLookup.find(Src);
+ if (AI != AllocaLookup.end()) {
+ Value *V = IncomingVals[AI->second];
+
+ // walk the use list of this load and replace all uses with r
+ LI->replaceAllUsesWith(V);
+ KillList.push_back(LI); // Mark the load to be deleted
+ }
+ }
+ } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
+ // delete this instruction and mark the name as the current holder of the
+ // value
+ Value *Ptr = SI->getPointerOperand();
+ if (AllocaInst *Dest = dyn_cast<AllocaInst>(Ptr)) {
+ map<Instruction *, unsigned>::iterator ai = AllocaLookup.find(Dest);
+ if (ai != AllocaLookup.end()) {
+ // what value were we writing?
+ IncomingVals[ai->second] = SI->getOperand(0);
+ KillList.push_back(SI); // Mark the store to be deleted
+ }
+ }
+
+ } else if (TerminatorInst *TI = dyn_cast<TerminatorInst>(I)) {
+ // Recurse across our successors
+ for (unsigned i = 0; i != TI->getNumSuccessors(); i++) {
+ vector<Value*> OutgoingVals(IncomingVals);
+ Traverse(TI->getSuccessor(i), BB, OutgoingVals, Visited);
+ }
+ }
+ }