[LLVMdev] Preserving NSW/NUW bits

Chad Rosier mcrosier at codeaurora.org
Tue Sep 2 15:22:50 PDT 2014


David/All,
Just a quick question about NSW/NUW bits, if you've got a second.  I
noticed you've been doing a little work on this as of late.

I have a bit of code that looks like the following:

  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %2 = add i64 %indvars.iv.next, -1
  %tmp = trunc i64 %2 to i32
  %cmp = icmp slt i32 %tmp, %0
  br i1 %cmp, label %for.body, label %for.end.loopexit

I'm trying to fold the 2nd add instruction into the compare by changing
the condition from from 'slt' to 'sle':

  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %tmp = trunc i64 %indvars.iv.next to i32
  %cmp = icmp sle i32 %tmp, %0
  br i1 %cmp, label %for.body, label %for.end.loopexit

However, AFAICT the NSW bits must be set.  In what cases can we propagate
the NSW bit from the first add to the second, if any?  If a case exists,
can this be handled by the WillNotOverflowSignedAdd function?

 Chad




More information about the llvm-dev mailing list