#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");
}
// Public interface to the CFGSimplification pass
-FunctionPass *createCFGSimplificationPass() {
+FunctionPass *llvm::createCFGSimplificationPass() {
return new CFGSimplifyPass();
}
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);
return Changed;
}
-
-} // End llvm namespace