+// RemovePredecessorFromBlock - This function is called when we are about
+// to remove a predecessor from a basic block. This function takes care of
+// removing the predecessor from the PHI nodes in BB so that after the pred
+// is removed, the number of PHI slots per bb is equal to the number of
+// predecessors.
+//
+static void RemovePredecessorFromBlock(BasicBlock *BB, BasicBlock *Pred) {
+ BasicBlock::pred_iterator PI(BB->pred_begin()), EI(BB->pred_end());
+ unsigned pred_idx = 0, max_idx;
+
+ cerr << "RPFB: " << Pred << "From Block: " << BB;
+
+ // Find out what index the predecessor is...
+ for (; *PI != BB; ++PI, ++pred_idx) {
+ assert(PI != EI && "Pred is not a predecessor of BB!");
+ }
+
+ // Loop over the rest of the predecssors until we run out, or until we find
+ // out that there are more than 2 predecessors.
+ for (max_idx = pred_idx; PI != EI && max_idx < 2; ++PI, ++max_idx) /*empty*/;
+
+ // If there are exactly two predecessors, then we want to nuke the PHI nodes
+ // altogether.
+ bool NukePHIs = max_idx == 1;
+
+ // Okay, now we know that we need to remove predecessor #pred_idx from all
+ // PHI nodes. Iterate over each PHI node fixing them up
+ BasicBlock::InstListType::iterator II(BB->getInstList().begin());
+ for (; (*II)->getInstType() == Instruction::PHINode; ++II) {
+ PHINode *PN = (PHINode*)*II;
+ PN->removeIncomingValue(pred_idx);
+
+ if (NukePHIs) { // Destroy the PHI altogether??
+ assert(PN->getOperand(1) == 0 && "PHI node should only have one value!");
+ Value *V = PN->getOperand(0);
+
+ PN->replaceAllUsesWith(V); // Replace PHI node with its single value.
+ delete BB->getInstList().remove(II);
+ }
+ }
+}
+
+// PropogatePredecessors - This gets "Succ" ready to have the predecessors from
+// "BB". This is a little tricky because "Succ" has PHI nodes, which need to
+// have extra slots added to them to hold the merge edges from BB's
+// predecessors.
+//
+// Assumption: BB is the single predecessor of Succ.
+//
+static void PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
+ assert(BB && Succ && *Succ->pred_begin() == BB && "BB is only pred of Succ" &&
+ ++Succ->pred_begin() == Succ->pred_end());
+
+ // If there is more than one predecessor, and there are PHI nodes in
+ // the successor, then we need to add incoming edges for the PHI nodes
+ BasicBlock::pred_iterator PI(BB->pred_begin());
+ for (; PI != BB->pred_end(); ++PI) {
+ // TODO:
+ }
+}
+