diff --git a/lib/Transforms/Scalar/LICM.cpp b/lib/Transforms/Scalar/LICM.cpp index 8795cd853fa..582948ea14b 100644 --- a/lib/Transforms/Scalar/LICM.cpp +++ b/lib/Transforms/Scalar/LICM.cpp @@ -618,6 +618,11 @@ bool LICM::isGuaranteedToExecute(Instruction &Inst) { if (!DT->dominates(Inst.getParent(), ExitBlocks[i])) return false; + // As a degenerate case, if the loop is statically infinite then we haven't + // proven anything since there are no exit blocks. + if (ExitBlocks.empty()) + return false; + return true; } diff --git a/test/Transforms/LICM/speculate.ll b/test/Transforms/LICM/speculate.ll index 507b193e6b1..4c4d036b7db 100644 --- a/test/Transforms/LICM/speculate.ll +++ b/test/Transforms/LICM/speculate.ll @@ -165,3 +165,25 @@ for.inc: ; preds = %if.then, %for.body for.end: ; preds = %for.inc, %entry ret void } + +; SDiv is unsafe to speculate inside an infinite loop. + +define void @unsafe_sdiv_c(i64 %a, i64 %b, i64* %p) { +entry: +; CHECK: entry: +; CHECK-NOT: sdiv +; CHECK: br label %for.body + br label %for.body + +for.body: + %c = icmp eq i64 %b, 0 + br i1 %c, label %backedge, label %if.then + +if.then: + %d = sdiv i64 %a, %b + store i64 %d, i64* %p + br label %backedge + +backedge: + br label %for.body +}