Skip to content

Commit 62bd944

Browse files
committed
[SCEV] Use fact that B >u 0 for A <u B in applyLoopGuards.
If LHS <u RHS holds, RHS should be guaranteed to be > 0. By using using 'umax(RHS, 1) -1' instead of 'RHS - 1' the results in applyLoopGuards can be improved in some cases. Note that the TODO for the tests mentioned the max BTC being 11, but unless I am missing something 10 should be correct. https://alive2.llvm.org/ce/z/44nP7F Reviewed By: mkazantsev Differential Revision: https://reviews.llvm.org/D126503
1 parent 55eea6e commit 62bd944

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15052,10 +15052,14 @@ const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
1505215052

1505315053
const SCEV *RewrittenRHS = nullptr;
1505415054
switch (Predicate) {
15055-
case CmpInst::ICMP_ULT:
15055+
case CmpInst::ICMP_ULT: {
15056+
if (RHS->getType()->isPointerTy())
15057+
break;
15058+
const SCEV *One = getOne(RHS->getType());
1505615059
RewrittenRHS =
15057-
getUMinExpr(RewrittenLHS, getMinusSCEV(RHS, getOne(RHS->getType())));
15060+
getUMinExpr(RewrittenLHS, getMinusSCEV(getUMaxExpr(RHS, One), One));
1505815061
break;
15062+
}
1505915063
case CmpInst::ICMP_SLT:
1506015064
RewrittenRHS =
1506115065
getSMinExpr(RewrittenLHS, getMinusSCEV(RHS, getOne(RHS->getType())));

llvm/test/Analysis/ScalarEvolution/max-backedge-taken-count-guard-info.ll

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,18 @@ exit:
308308
ret void
309309
}
310310

311-
; TODO: Currently we miss getting the tightest constant max backedge-taken count (11).
312311
define void @test_multiple_var_guards_order1(ptr nocapture %a, i64 %i, i64 %N) {
313312
; CHECK-LABEL: 'test_multiple_var_guards_order1'
314313
; CHECK-NEXT: Classifying expressions for: @test_multiple_var_guards_order1
315314
; CHECK-NEXT: %iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ]
316-
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,-9223372036854775808) S: [0,-9223372036854775808) Exits: %i LoopDispositions: { %loop: Computable }
315+
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,11) S: [0,11) Exits: %i LoopDispositions: { %loop: Computable }
317316
; CHECK-NEXT: %idx = getelementptr inbounds i32, ptr %a, i64 %iv
318317
; CHECK-NEXT: --> {%a,+,4}<nuw><%loop> U: full-set S: full-set Exits: ((4 * %i) + %a) LoopDispositions: { %loop: Computable }
319318
; CHECK-NEXT: %iv.next = add nuw nsw i64 %iv, 1
320-
; CHECK-NEXT: --> {1,+,1}<nuw><%loop> U: [1,0) S: [1,0) Exits: (1 + %i) LoopDispositions: { %loop: Computable }
319+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,12) S: [1,12) Exits: (1 + %i) LoopDispositions: { %loop: Computable }
321320
; CHECK-NEXT: Determining loop execution counts for: @test_multiple_var_guards_order1
322321
; CHECK-NEXT: Loop %loop: backedge-taken count is %i
323-
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is -1
322+
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is 10
324323
; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is %i
325324
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is %i
326325
; CHECK-NEXT: Predicates:
@@ -346,19 +345,18 @@ exit:
346345
ret void
347346
}
348347

349-
; TODO: Currently we miss getting the tightest constant max backedge-taken count (11).
350348
define void @test_multiple_var_guards_order2(ptr nocapture %a, i64 %i, i64 %N) {
351349
; CHECK-LABEL: 'test_multiple_var_guards_order2'
352350
; CHECK-NEXT: Classifying expressions for: @test_multiple_var_guards_order2
353351
; CHECK-NEXT: %iv = phi i64 [ %iv.next, %loop ], [ 0, %guardbb ]
354-
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,-9223372036854775808) S: [0,-9223372036854775808) Exits: %i LoopDispositions: { %loop: Computable }
352+
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,11) S: [0,11) Exits: %i LoopDispositions: { %loop: Computable }
355353
; CHECK-NEXT: %idx = getelementptr inbounds i32, ptr %a, i64 %iv
356354
; CHECK-NEXT: --> {%a,+,4}<nuw><%loop> U: full-set S: full-set Exits: ((4 * %i) + %a) LoopDispositions: { %loop: Computable }
357355
; CHECK-NEXT: %iv.next = add nuw nsw i64 %iv, 1
358-
; CHECK-NEXT: --> {1,+,1}<nuw><%loop> U: [1,0) S: [1,0) Exits: (1 + %i) LoopDispositions: { %loop: Computable }
356+
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,12) S: [1,12) Exits: (1 + %i) LoopDispositions: { %loop: Computable }
359357
; CHECK-NEXT: Determining loop execution counts for: @test_multiple_var_guards_order2
360358
; CHECK-NEXT: Loop %loop: backedge-taken count is %i
361-
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is -1
359+
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is 10
362360
; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is %i
363361
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is %i
364362
; CHECK-NEXT: Predicates:
@@ -393,10 +391,10 @@ define void @test_multiple_var_guards_cycle(ptr nocapture %a, i64 %i, i64 %N) {
393391
; CHECK-NEXT: %idx = getelementptr inbounds i32, ptr %a, i64 %iv
394392
; CHECK-NEXT: --> {%a,+,4}<nuw><%loop> U: full-set S: full-set Exits: ((4 * %N) + %a) LoopDispositions: { %loop: Computable }
395393
; CHECK-NEXT: %iv.next = add nuw nsw i64 %iv, 1
396-
; CHECK-NEXT: --> {1,+,1}<nuw><%loop> U: [1,0) S: [1,0) Exits: (1 + %N) LoopDispositions: { %loop: Computable }
394+
; CHECK-NEXT: --> {1,+,1}<nuw><%loop> U: [1,-1) S: [1,-1) Exits: (1 + %N) LoopDispositions: { %loop: Computable }
397395
; CHECK-NEXT: Determining loop execution counts for: @test_multiple_var_guards_cycle
398396
; CHECK-NEXT: Loop %loop: backedge-taken count is %N
399-
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is -1
397+
; CHECK-NEXT: Loop %loop: constant max backedge-taken count is -3
400398
; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is %N
401399
; CHECK-NEXT: Loop %loop: Predicated backedge-taken count is %N
402400
; CHECK-NEXT: Predicates:

0 commit comments

Comments
 (0)