diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 7f4133630d8..596c5643e54 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -1932,7 +1932,18 @@ bool llvm::SimplifyCFG(BasicBlock *BB) { while (Unreachable != BB->begin()) { BasicBlock::iterator BBI = Unreachable; --BBI; + // Do not delete instructions that can have side effects, like calls + // (which may never return) and volatile loads and stores. if (isa(BBI)) break; + + if (StoreInst *SI = dyn_cast(BBI)) + if (SI->isVolatile()) + break; + + if (LoadInst *LI = dyn_cast(BBI)) + if (LI->isVolatile()) + break; + // Delete this instruction BB->getInstList().erase(BBI); Changed = true; diff --git a/test/Transforms/SimplifyCFG/trapping-load-unreachable.ll b/test/Transforms/SimplifyCFG/trapping-load-unreachable.ll new file mode 100644 index 00000000000..a73a82b3697 --- /dev/null +++ b/test/Transforms/SimplifyCFG/trapping-load-unreachable.ll @@ -0,0 +1,19 @@ +; RUN: llvm-as < %s | opt -simplifycfg | llvm-dis | grep {volatile load} +; PR2967 + +target datalayout = +"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32" +target triple = "i386-pc-linux-gnu" + +define void @foo(i32 %x) nounwind { +entry: + %0 = icmp eq i32 %x, 0 ; [#uses=1] + br i1 %0, label %bb, label %return + +bb: ; preds = %entry + %1 = volatile load i32* null ; [#uses=0] + unreachable + br label %return +return: ; preds = %entry + ret void +}