llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

SCEV ignores nuw/nsw when computing MaxBackedgeTakenCount

Open DaniilSuchkov opened this issue 5 years ago • 1 comments

Bugzilla Link 50028
Version trunk
OS Windows NT

Extended Description

define void @​foo(i32 %n) { entry: br label %loop

loop: %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] %i.next = add nuw nsw i32 %i, 1 %innercmp = icmp ult i32 %i, %n br i1 %innercmp, label %loop, label %exit

exit: ret void }

Running opt -passes='print' gives us the following result:

Printing analysis 'Scalar Evolution Analysis' for function 'foo': Classifying expressions for: @​foo %i = phi i32 [ 0, %entry ], [ %i.next, %loop ] --> {0,+,1}<%loop> U: [0,-2147483648) S: [0,-2147483648) Exits: %n LoopDispositions: { %loop: Computable } %i.next = add nuw nsw i32 %i, 1 --> {1,+,1}<%loop> U: [1,0) S: [1,0) Exits: (1 + %n) LoopDispositions: { %loop: Computable } Determining loop execution counts for: @​foo Loop %loop: backedge-taken count is %n Loop %loop: max backedge-taken count is -1 Loop %loop: Predicated backedge-taken count is %n Predicates:

Loop %loop: Trip multiple is 1

SCEV understands that %i won't exceed 2^31, but says that the backedge can be taken up to 2^32-1 times. Since it's an upper-bound estimate, it's correct, yet suboptimal.

DaniilSuchkov avatar Apr 19 '21 17:04 DaniilSuchkov

I guess current algorithm is just looking at the comparison and doesn't try to sharpen facts using branching by UB. It looks like a valid improvement to me.

I'm curious, do you know any transform that fails because of this? I believe the late tendency is to not use max taken count and try to reason about symbolic exact instead.

llvmbot avatar Apr 20 '21 04:04 llvmbot