diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp index b979f3328d4..15f072dbeb7 100644 --- a/lib/Analysis/ScalarEvolution.cpp +++ b/lib/Analysis/ScalarEvolution.cpp @@ -3101,9 +3101,16 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) { return getUnknown(V); unsigned Opcode = Instruction::UserOp1; - if (Instruction *I = dyn_cast(V)) + if (Instruction *I = dyn_cast(V)) { Opcode = I->getOpcode(); - else if (ConstantExpr *CE = dyn_cast(V)) + + // Don't attempt to analyze instructions in blocks that aren't + // reachable. Such instructions don't matter, and they aren't required + // to obey basic rules for definitions dominating uses which this + // analysis depends on. + if (!DT->isReachableFromEntry(I->getParent())) + return getUnknown(V); + } else if (ConstantExpr *CE = dyn_cast(V)) Opcode = CE->getOpcode(); else if (ConstantInt *CI = dyn_cast(V)) return getConstant(CI); diff --git a/test/Analysis/ScalarEvolution/unreachable-code.ll b/test/Analysis/ScalarEvolution/unreachable-code.ll new file mode 100644 index 00000000000..51d93981800 --- /dev/null +++ b/test/Analysis/ScalarEvolution/unreachable-code.ll @@ -0,0 +1,13 @@ +; RUN: opt < %s -analyze -scalar-evolution | FileCheck %s + +; CHECK: %t = add i64 %t, 1 +; CHECK: --> %t + +define void @foo() { +entry: + ret void + +dead: + %t = add i64 %t, 1 + ret void +}