[llvm] r272237 - Be wary of abnormal exits from loop when exploiting UB
Sanjoy Das via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 8 18:14:00 PDT 2016
Author: sanjoy
Date: Wed Jun 8 20:13:59 2016
New Revision: 272237
URL: http://llvm.org/viewvc/llvm-project?rev=272237&view=rev
Log:
Be wary of abnormal exits from loop when exploiting UB
We can safely rely on a NoWrap add recurrence causing UB down the road
only if we know the loop does not have a exit expressed in a way that is
opaque to ScalarEvolution (e.g. by a function call that conditionally
calls exit(0)).
I believe with this change PR28012 is fixed.
Note: I had to change some llvm-lit tests in LoopReroll, since it looks
like they were depending on this incorrect behavior.
Modified:
llvm/trunk/lib/Analysis/ScalarEvolution.cpp
llvm/trunk/test/Analysis/ScalarEvolution/trip-count.ll
llvm/trunk/test/Transforms/LoopReroll/basic.ll
Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=272237&r1=272236&r2=272237&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Wed Jun 8 20:13:59 2016
@@ -7199,7 +7199,8 @@ ScalarEvolution::howFarToZero(const SCEV
// compute the backedge count. In this case, the step may not divide the
// distance, but we don't care because if the condition is "missed" the loop
// will have undefined behavior due to wrapping.
- if (ControlsExit && AddRec->hasNoSelfWrap()) {
+ if (ControlsExit && AddRec->hasNoSelfWrap() &&
+ loopHasNoAbnormalExits(AddRec->getLoop())) {
const SCEV *Exact =
getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
return ExitLimit(Exact, Exact, P);
Modified: llvm/trunk/test/Analysis/ScalarEvolution/trip-count.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/trip-count.ll?rev=272237&r1=272236&r2=272237&view=diff
==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/trip-count.ll (original)
+++ llvm/trunk/test/Analysis/ScalarEvolution/trip-count.ll Wed Jun 8 20:13:59 2016
@@ -89,3 +89,25 @@ for.inc.1:
; Function Attrs: nounwind
declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) #0
+
+declare void @may_exit() nounwind
+
+define void @pr28012(i32 %n) {
+; CHECK-LABEL: Classifying expressions for: @pr28012
+; CHECK: Loop %loop: backedge-taken count is -1431655751
+; CHECK: Loop %loop: max backedge-taken count is -1431655751
+; CHECK: Loop %loop: Predicated backedge-taken count is -1431655751
+
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
+ %iv.inc = add nsw i32 %iv, 3
+ call void @may_exit()
+ %becond = icmp ne i32 %iv.inc, 46
+ br i1 %becond, label %loop, label %leave
+
+leave:
+ ret void
+}
Modified: llvm/trunk/test/Transforms/LoopReroll/basic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopReroll/basic.ll?rev=272237&r1=272236&r2=272237&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LoopReroll/basic.ll (original)
+++ llvm/trunk/test/Transforms/LoopReroll/basic.ll Wed Jun 8 20:13:59 2016
@@ -24,7 +24,7 @@ for.body:
%add2 = add nsw i32 %i.08, 2
%call3 = tail call i32 @foo(i32 %add2) #1
%add3 = add nsw i32 %i.08, 3
- %exitcond = icmp eq i32 %add3, 500
+ %exitcond = icmp sge i32 %add3, 500
br i1 %exitcond, label %for.end, label %for.body
; CHECK-LABEL: @bar
@@ -33,7 +33,7 @@ for.body:
; CHECK: %indvar = phi i32 [ %indvar.next, %for.body ], [ 0, %entry ]
; CHECK: %call = tail call i32 @foo(i32 %indvar) #1
; CHECK: %indvar.next = add i32 %indvar, 1
-; CHECK: %exitcond1 = icmp eq i32 %indvar, 497
+; CHECK: %exitcond1 = icmp eq i32 %indvar, 500
; CHECK: br i1 %exitcond1, label %for.end, label %for.body
; CHECK: ret
@@ -524,7 +524,7 @@ for.body:
%add3 = add nsw i32 %i.08, 3
- %exitcond = icmp eq i32 %add3, 500
+ %exitcond = icmp sge i32 %add3, 500
br i1 %exitcond, label %for.end, label %for.body
; CHECK-LABEL: @bar2
@@ -536,7 +536,7 @@ for.body:
; CHECK: %tmp3 = add i32 %tmp2, %tmp1
; CHECK: %call = tail call i32 @foo(i32 %tmp3) #1
; CHECK: %indvar.next = add i32 %indvar, 1
-; CHECK: %exitcond1 = icmp eq i32 %indvar, 497
+; CHECK: %exitcond1 = icmp eq i32 %indvar, 500
; CHECK: br i1 %exitcond1, label %for.end, label %for.body
; CHECK: ret
More information about the llvm-commits
mailing list