llvm.org GIT mirror llvm / 05c5b3f
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272237 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 3 years ago
3 changed file(s) with 28 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
71987198 // compute the backedge count. In this case, the step may not divide the
71997199 // distance, but we don't care because if the condition is "missed" the loop
72007200 // will have undefined behavior due to wrapping.
7201 if (ControlsExit && AddRec->hasNoSelfWrap()) {
7201 if (ControlsExit && AddRec->hasNoSelfWrap() &&
7202 loopHasNoAbnormalExits(AddRec->getLoop())) {
72027203 const SCEV *Exact =
72037204 getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
72047205 return ExitLimit(Exact, Exact, P);
8888
8989 ; Function Attrs: nounwind
9090 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) #0
91
92 declare void @may_exit() nounwind
93
94 define void @pr28012(i32 %n) {
95 ; CHECK-LABEL: Classifying expressions for: @pr28012
96 ; CHECK: Loop %loop: backedge-taken count is -1431655751
97 ; CHECK: Loop %loop: max backedge-taken count is -1431655751
98 ; CHECK: Loop %loop: Predicated backedge-taken count is -1431655751
99
100 entry:
101 br label %loop
102
103 loop:
104 %iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
105 %iv.inc = add nsw i32 %iv, 3
106 call void @may_exit()
107 %becond = icmp ne i32 %iv.inc, 46
108 br i1 %becond, label %loop, label %leave
109
110 leave:
111 ret void
112 }
2323 %add2 = add nsw i32 %i.08, 2
2424 %call3 = tail call i32 @foo(i32 %add2) #1
2525 %add3 = add nsw i32 %i.08, 3
26 %exitcond = icmp eq i32 %add3, 500
26 %exitcond = icmp sge i32 %add3, 500
2727 br i1 %exitcond, label %for.end, label %for.body
2828
2929 ; CHECK-LABEL: @bar
3232 ; CHECK: %indvar = phi i32 [ %indvar.next, %for.body ], [ 0, %entry ]
3333 ; CHECK: %call = tail call i32 @foo(i32 %indvar) #1
3434 ; CHECK: %indvar.next = add i32 %indvar, 1
35 ; CHECK: %exitcond1 = icmp eq i32 %indvar, 497
35 ; CHECK: %exitcond1 = icmp eq i32 %indvar, 500
3636 ; CHECK: br i1 %exitcond1, label %for.end, label %for.body
3737
3838 ; CHECK: ret
523523
524524 %add3 = add nsw i32 %i.08, 3
525525
526 %exitcond = icmp eq i32 %add3, 500
526 %exitcond = icmp sge i32 %add3, 500
527527 br i1 %exitcond, label %for.end, label %for.body
528528
529529 ; CHECK-LABEL: @bar2
535535 ; CHECK: %tmp3 = add i32 %tmp2, %tmp1
536536 ; CHECK: %call = tail call i32 @foo(i32 %tmp3) #1
537537 ; CHECK: %indvar.next = add i32 %indvar, 1
538 ; CHECK: %exitcond1 = icmp eq i32 %indvar, 497
538 ; CHECK: %exitcond1 = icmp eq i32 %indvar, 500
539539 ; CHECK: br i1 %exitcond1, label %for.end, label %for.body
540540
541541 ; CHECK: ret