+ BasicBlock *BB = TI->getParent();
+
+ // Get the destination block of this edge...
+ BasicBlock *OldSucc = TI->getSuccessor(SuccNo);
+
+ // Make sure that the block ends with a conditional branch and is simple
+ // enough for use to be able to revector over.
+ BranchInst *BI = dyn_cast<BranchInst>(OldSucc->getTerminator());
+ if (BI == 0 || !BI->isConditional() || !isBlockSimpleEnough(OldSucc))
+ return false;
+
+ // We can only forward the branch over the block if the block ends with a
+ // cmp we can determine the outcome for.
+ //
+ // FIXME: we can make this more generic. Code below already handles more
+ // generic case.
+ if (!isa<CmpInst>(BI->getCondition()))
+ return false;
+
+ // Make a new RegionInfo structure so that we can simulate the effect of the
+ // PHI nodes in the block we are skipping over...
+ //
+ RegionInfo NewRI(RI);
+
+ // Remove value information for all of the values we are simulating... to make
+ // sure we don't have any stale information.
+ for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end(); I!=E; ++I)
+ if (I->getType() != Type::VoidTy)
+ NewRI.removeValueInfo(I);
+
+ // Put the newly discovered information into the RegionInfo...
+ for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end(); I!=E; ++I)
+ if (PHINode *PN = dyn_cast<PHINode>(I)) {
+ int OpNum = PN->getBasicBlockIndex(BB);
+ assert(OpNum != -1 && "PHI doesn't have incoming edge for predecessor!?");
+ PropagateEquality(PN, PN->getIncomingValue(OpNum), NewRI);
+ } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) {
+ Relation::KnownResult Res = getCmpResult(CI, NewRI);
+ if (Res == Relation::Unknown) return false;
+ PropagateEquality(CI, ConstantBool::get(Res), NewRI);
+ } else {
+ assert(isa<BranchInst>(*I) && "Unexpected instruction type!");
+ }
+
+ // Compute the facts implied by what we have discovered...
+ ComputeReplacements(NewRI);
+
+ ValueInfo &PredicateVI = NewRI.getValueInfo(BI->getCondition());
+ if (PredicateVI.getReplacement() &&
+ isa<Constant>(PredicateVI.getReplacement()) &&
+ !isa<GlobalValue>(PredicateVI.getReplacement())) {
+ ConstantBool *CB = cast<ConstantBool>(PredicateVI.getReplacement());
+
+ // Forward to the successor that corresponds to the branch we will take.
+ ForwardSuccessorTo(TI, SuccNo, BI->getSuccessor(!CB->getValue()), NewRI);
+ return true;
+ }
+
+ return false;
+}
+
+static Value *getReplacementOrValue(Value *V, RegionInfo &RI) {
+ if (const ValueInfo *VI = RI.requestValueInfo(V))
+ if (Value *Repl = VI->getReplacement())
+ return Repl;
+ return V;
+}
+
+/// ForwardSuccessorTo - We have found that we can forward successor # 'SuccNo'
+/// of Terminator 'TI' to the 'Dest' BasicBlock. This method performs the
+/// mechanics of updating SSA information and revectoring the branch.
+///
+void CEE::ForwardSuccessorTo(TerminatorInst *TI, unsigned SuccNo,
+ BasicBlock *Dest, RegionInfo &RI) {
+ // If there are any PHI nodes in the Dest BB, we must duplicate the entry
+ // in the PHI node for the old successor to now include an entry from the
+ // current basic block.
+ //
+ BasicBlock *OldSucc = TI->getSuccessor(SuccNo);
+ BasicBlock *BB = TI->getParent();
+
+ DOUT << "Forwarding branch in basic block %" << BB->getName()
+ << " from block %" << OldSucc->getName() << " to block %"
+ << Dest->getName() << "\n"
+ << "Before forwarding: " << *BB->getParent();
+
+ // Because we know that there cannot be critical edges in the flow graph, and
+ // that OldSucc has multiple outgoing edges, this means that Dest cannot have
+ // multiple incoming edges.
+ //
+#ifndef NDEBUG
+ pred_iterator DPI = pred_begin(Dest); ++DPI;
+ assert(DPI == pred_end(Dest) && "Critical edge found!!");
+#endif
+
+ // Loop over any PHI nodes in the destination, eliminating them, because they
+ // may only have one input.
+ //
+ while (PHINode *PN = dyn_cast<PHINode>(&Dest->front())) {
+ assert(PN->getNumIncomingValues() == 1 && "Crit edge found!");
+ // Eliminate the PHI node
+ PN->replaceAllUsesWith(PN->getIncomingValue(0));
+ Dest->getInstList().erase(PN);
+ }
+
+ // If there are values defined in the "OldSucc" basic block, we need to insert
+ // PHI nodes in the regions we are dealing with to emulate them. This can
+ // insert dead phi nodes, but it is more trouble to see if they are used than
+ // to just blindly insert them.
+ //
+ if (EF->dominates(OldSucc, Dest)) {
+ // RegionExitBlocks - Find all of the blocks that are not dominated by Dest,
+ // but have predecessors that are. Additionally, prune down the set to only
+ // include blocks that are dominated by OldSucc as well.
+ //
+ std::vector<BasicBlock*> RegionExitBlocks;
+ CalculateRegionExitBlocks(Dest, OldSucc, RegionExitBlocks);
+
+ for (BasicBlock::iterator I = OldSucc->begin(), E = OldSucc->end();
+ I != E; ++I)
+ if (I->getType() != Type::VoidTy) {
+ // Create and insert the PHI node into the top of Dest.
+ PHINode *NewPN = new PHINode(I->getType(), I->getName()+".fw_merge",
+ Dest->begin());
+ // There is definitely an edge from OldSucc... add the edge now
+ NewPN->addIncoming(I, OldSucc);
+
+ // There is also an edge from BB now, add the edge with the calculated
+ // value from the RI.
+ NewPN->addIncoming(getReplacementOrValue(I, RI), BB);
+
+ // Make everything in the Dest region use the new PHI node now...
+ ReplaceUsesOfValueInRegion(I, NewPN, Dest);
+
+ // Make sure that exits out of the region dominated by NewPN get PHI
+ // nodes that merge the values as appropriate.
+ InsertRegionExitMerges(NewPN, I, RegionExitBlocks);
+ }
+ }
+
+ // If there were PHI nodes in OldSucc, we need to remove the entry for this
+ // edge from the PHI node, and we need to replace any references to the PHI
+ // node with a new value.
+ //
+ for (BasicBlock::iterator I = OldSucc->begin(); isa<PHINode>(I); ) {
+ PHINode *PN = cast<PHINode>(I);
+
+ // Get the value flowing across the old edge and remove the PHI node entry
+ // for this edge: we are about to remove the edge! Don't remove the PHI
+ // node yet though if this is the last edge into it.
+ Value *EdgeValue = PN->removeIncomingValue(BB, false);
+
+ // Make sure that anything that used to use PN now refers to EdgeValue
+ ReplaceUsesOfValueInRegion(PN, EdgeValue, Dest);
+
+ // If there is only one value left coming into the PHI node, replace the PHI
+ // node itself with the one incoming value left.
+ //
+ if (PN->getNumIncomingValues() == 1) {
+ assert(PN->getNumIncomingValues() == 1);
+ PN->replaceAllUsesWith(PN->getIncomingValue(0));
+ PN->getParent()->getInstList().erase(PN);
+ I = OldSucc->begin();
+ } else if (PN->getNumIncomingValues() == 0) { // Nuke the PHI
+ // If we removed the last incoming value to this PHI, nuke the PHI node
+ // now.
+ PN->replaceAllUsesWith(Constant::getNullValue(PN->getType()));
+ PN->getParent()->getInstList().erase(PN);
+ I = OldSucc->begin();
+ } else {
+ ++I; // Otherwise, move on to the next PHI node
+ }
+ }
+
+ // Actually revector the branch now...
+ TI->setSuccessor(SuccNo, Dest);
+
+ // If we just introduced a critical edge in the flow graph, make sure to break
+ // it right away...
+ SplitCriticalEdge(TI, SuccNo, this);
+
+ // Make sure that we don't introduce critical edges from oldsucc now!
+ for (unsigned i = 0, e = OldSucc->getTerminator()->getNumSuccessors();
+ i != e; ++i)
+ SplitCriticalEdge(OldSucc->getTerminator(), i, this);
+
+ // Since we invalidated the CFG, recalculate the dominator set so that it is
+ // useful for later processing!
+ // FIXME: This is much worse than it really should be!
+ //EF->recalculate();
+
+ DOUT << "After forwarding: " << *BB->getParent();
+}
+
+/// ReplaceUsesOfValueInRegion - This method replaces all uses of Orig with uses
+/// of New. It only affects instructions that are defined in basic blocks that
+/// are dominated by Head.
+///
+void CEE::ReplaceUsesOfValueInRegion(Value *Orig, Value *New,
+ BasicBlock *RegionDominator) {
+ assert(Orig != New && "Cannot replace value with itself");
+ std::vector<Instruction*> InstsToChange;
+ std::vector<PHINode*> PHIsToChange;
+ InstsToChange.reserve(Orig->getNumUses());
+
+ // Loop over instructions adding them to InstsToChange vector, this allows us
+ // an easy way to avoid invalidating the use_iterator at a bad time.
+ for (Value::use_iterator I = Orig->use_begin(), E = Orig->use_end();
+ I != E; ++I)
+ if (Instruction *User = dyn_cast<Instruction>(*I))
+ if (EF->dominates(RegionDominator, User->getParent()))
+ InstsToChange.push_back(User);
+ else if (PHINode *PN = dyn_cast<PHINode>(User)) {
+ PHIsToChange.push_back(PN);
+ }
+
+ // PHIsToChange contains PHI nodes that use Orig that do not live in blocks
+ // dominated by orig. If the block the value flows in from is dominated by
+ // RegionDominator, then we rewrite the PHI
+ for (unsigned i = 0, e = PHIsToChange.size(); i != e; ++i) {
+ PHINode *PN = PHIsToChange[i];
+ for (unsigned j = 0, e = PN->getNumIncomingValues(); j != e; ++j)
+ if (PN->getIncomingValue(j) == Orig &&
+ EF->dominates(RegionDominator, PN->getIncomingBlock(j)))
+ PN->setIncomingValue(j, New);
+ }
+
+ // Loop over the InstsToChange list, replacing all uses of Orig with uses of
+ // New. This list contains all of the instructions in our region that use
+ // Orig.
+ for (unsigned i = 0, e = InstsToChange.size(); i != e; ++i)
+ if (PHINode *PN = dyn_cast<PHINode>(InstsToChange[i])) {
+ // PHINodes must be handled carefully. If the PHI node itself is in the
+ // region, we have to make sure to only do the replacement for incoming
+ // values that correspond to basic blocks in the region.
+ for (unsigned j = 0, e = PN->getNumIncomingValues(); j != e; ++j)
+ if (PN->getIncomingValue(j) == Orig &&
+ EF->dominates(RegionDominator, PN->getIncomingBlock(j)))
+ PN->setIncomingValue(j, New);
+
+ } else {
+ InstsToChange[i]->replaceUsesOfWith(Orig, New);