- Fix SelectionDAG to generate correct CFGs.
- Add a basic machine-level dead block eliminator.

These two have to go together, since many other parts of the code generator are unable to handle the unreachable blocks otherwise created.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54333 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index 196d3bf..6299bc4 100644
--- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -1632,11 +1632,24 @@
   }
   SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), Cond,
                                  DAG.getBasicBlock(CB.TrueBB));
-  if (CB.FalseBB == NextBlock)
+  
+  // If the branch was constant folded, fix up the CFG.
+  if (BrCond.getOpcode() == ISD::BR) {
+    if (!DisableCorrectBranchFolding)
+      CurMBB->removeSuccessor(CB.FalseBB);
     DAG.setRoot(BrCond);
-  else
-    DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, 
-                            DAG.getBasicBlock(CB.FalseBB)));
+  } else {
+    // Otherwise, go ahead and insert the false branch.
+    if (BrCond == getControlRoot()) 
+      if (!DisableCorrectBranchFolding)
+        CurMBB->removeSuccessor(CB.TrueBB);
+    
+    if (CB.FalseBB == NextBlock)
+      DAG.setRoot(BrCond);
+    else
+      DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond, 
+                              DAG.getBasicBlock(CB.FalseBB)));
+  }
 }
 
 /// visitJumpTable - Emit JumpTable node in the current MBB