[PATCH] [IndVarSimplify] Widen loop compares for unsigned IVs that have a uniform step of one.

Andrew Trick atrick at apple.com
Mon Sep 29 18:48:18 PDT 2014

At the LLVM IR level, there is no assumption about signed or unsigned arithmetic wrapping. Either SCEV must prove a value does not wrap based on loop conditions, or it must be inferred from the add operation's nsw or nuw attributes. By the time WidenLoopCompare has been called, SCEV has proven that the IV recurrence does not wrap in the signed or unsigned sense (depending on IsSigned).

Maybe it helps to think about it this way: You could have safely implemented compare widening by first transforming the IR as follows:

%i0 = phi i8 [%i1, %Loop, ...]
%i1 = add nuw i8 %i0, 2
%exti = zext i8 %i to i64
cmp i64 %exti, zext(k)

Then indvars would come along and widen the IV:

%i0 = phi i64 [%i1, %Loop, ...]
%i1 = add nuw i64 %i0, 2
cmp i64 %i1, zext(k)

But you're widening the compare after WidenIV has already proven that this zero extend would be unnecessary:
%exti = zext i8 %i to i64

So you just need to extend the constant.


More information about the llvm-commits mailing list