Implement the trivial cases in InstCombine/store.ll
[oota-llvm.git] / lib / Transforms / Scalar / SimplifyCFG.cpp
index 5a8d428153821d25a8e96ad074f0b01b56f9d96a..88b625becf9e04d40d04ef103fe1a807ad3ed144 100644 (file)
 
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Transforms/Utils/Local.h"
+#include "llvm/Constants.h"
+#include "llvm/Instructions.h"
 #include "llvm/Module.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Pass.h"
-#include "Support/Statistic.h"
+#include "llvm/ADT/Statistic.h"
 #include <set>
-
-namespace llvm {
+using namespace llvm;
 
 namespace {
   Statistic<> NumSimpl("cfgsimplify", "Number of blocks simplified");
@@ -38,7 +39,7 @@ namespace {
 }
 
 // Public interface to the CFGSimplification pass
-FunctionPass *createCFGSimplificationPass() {
+FunctionPass *llvm::createCFGSimplificationPass() {
   return new CFGSimplifyPass();
 }
 
@@ -46,6 +47,30 @@ static bool MarkAliveBlocks(BasicBlock *BB, std::set<BasicBlock*> &Reachable) {
   if (Reachable.count(BB)) return false;
   Reachable.insert(BB);
 
+  // Do a quick scan of the basic block, turning any obviously unreachable
+  // instructions into LLVM unreachable insts.  The instruction combining pass
+  // canonnicalizes unreachable insts into stores to null or undef.
+  for (BasicBlock::iterator BBI = BB->begin(), E = BB->end(); BBI != E; ++BBI)
+    if (StoreInst *SI = dyn_cast<StoreInst>(BBI))
+      if (isa<ConstantPointerNull>(SI->getOperand(1)) ||
+          isa<UndefValue>(SI->getOperand(1))) {
+        // Loop over all of the successors, removing BB's entry from any PHI
+        // nodes.
+        for (succ_iterator I = succ_begin(BB), SE = succ_end(BB); I != SE; ++I)
+          (*I)->removePredecessor(BB);
+
+        new UnreachableInst(SI);
+
+        // All instructions after this are dead.
+        for (; BBI != E; ) {
+          if (!BBI->use_empty())
+            BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
+          BB->getInstList().erase(BBI++);
+        }
+        break;
+      }
+
+
   bool Changed = ConstantFoldTerminator(BB);
   for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI)
     MarkAliveBlocks(*SI, Reachable);
@@ -103,5 +128,3 @@ bool CFGSimplifyPass::runOnFunction(Function &F) {
 
   return Changed;
 }
-
-} // End llvm namespace