This is an archive of the discontinued LLVM Phabricator instance.

Teach IndVarSimplify to add nuw and nsw to operations that provably don't overflow.
ClosedPublic

Authored by sanjoy on Dec 21 2014, 1:37 AM.

Details

Summary

This patch teaches IndVarSimplify to add nuw and nsw to certain kinds of operations that provably don't overflow. For example, we can prove %civ.inc below does not sign-overflow. With this change, IndVarSimplify changes %civ.inc to an add nsw.

define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
 entry:
  %length = load i32* %length_ptr, !range !0
  %len.sub.1 = sub i32 %length, 1
  %upper = icmp slt i32 %init, %len.sub.1
  br i1 %upper, label %loop, label %exit

 loop:
  %civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
  %civ.inc = add i32 %civ, 1
  %cmp = icmp slt i32 %civ.inc, %length
  br i1 %cmp, label %latch, label %break

 latch:
  store i32 0, i32* %array
  %check = icmp slt i32 %civ.inc, %len.sub.1
  br i1 %check, label %loop, label %break

 break:
  ret i32 %civ.inc

 exit:
  ret i32 42
}

Diff Detail

Repository
rL LLVM

Event Timeline

sanjoy updated this revision to Diff 17535.Dec 21 2014, 1:37 AM
sanjoy retitled this revision from to Teach IndVarSimplify to add nuw and nsw to operations that provably don't overflow..
sanjoy updated this object.
sanjoy edited the test plan for this revision. (Show Details)
sanjoy added reviewers: atrick, majnemer, hfinkel.
sanjoy added a subscriber: Unknown Object (MLST).
hfinkel accepted this revision.Dec 23 2014, 3:17 PM
hfinkel edited edge metadata.

We normally don't add otherwise-provable nsw/nuw unless is specifically enables some other optimization where these facts are difficult to prove. As you might know, there are some optimizations that are valid on overflowing operators that are not valid on non-overflowing ones, so adding nsw/nuw is, generally speaking, not a pure win.

That having been said, I don't see this being a particular issue for induction-variable increment operations on canonical loops, and so this likely makes sense as part of the loop canonical form (I can imagine targets taking advantage of these flags during instruction selection, for example). I'd like you to further comment on your motivation for doing this, but LGTM.

This revision is now accepted and ready to land.Dec 23 2014, 3:17 PM

Hi Hal,

Thank you for the review.

This is the context: http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-December/079885.html This patch tries to fix point (1) as mentioned in the mail

I am not aware of transforms that are valid only on overflowing operators: as far as I can tell, adding nsw / nuw is a strict increase in the amount of information there is available about an instruction. Can you give me some pointers on where to look for examples? Don't bother if you don't have them handy, I'll look for them myself later.

As an aside, I'll be on vacation starting a few hours from now, so I won't be able to check this in before the first week of 2015.

  • Original Message -----

From: "Sanjoy Das" <sanjoy@playingwithpointers.com>
To: sanjoy@playingwithpointers.com, atrick@apple.com, "david majnemer" <david.majnemer@gmail.com>, hfinkel@anl.gov
Cc: llvm-commits@cs.uiuc.edu
Sent: Tuesday, December 23, 2014 6:22:31 PM
Subject: Re: [PATCH] Teach IndVarSimplify to add nuw and nsw to operations that provably don't overflow.

Hi Hal,

Thank you for the review.

This is the context:
http://lists.cs.uiuc.edu/pipermail/llvmdev/2014-December/079885.html
This patch tries to fix point (1) as mentioned in the mail

I am not aware of transforms that are valid only on overflowing
operators: as far as I can tell, adding nsw / nuw is a strict
increase in the amount of information there is available about an
instruction. Can you give me some pointers on where to look for
examples? Don't bother if you don't have them handy, I'll look for
them myself later.

We really need to add this to an FAQ somewhere, it comes up a lot...

For example, see slides 20-31 of http://llvm.org/devmtg/2014-10/Slides/Menendez-Alive.pdf -- shows a reassociation example valid only without the no-wrap flags. There are other examples too that have been discussed on the mailing list.

As an aside, I'll be on vacation starting a few hours from now, so I
won't be able to check this in before the first week of 2015.

Okay; enjoy your vacation!

-Hal

http://reviews.llvm.org/D6748

EMAIL PREFERENCES

http://reviews.llvm.org/settings/panel/emailpreferences/
This revision was automatically updated to reflect the committed changes.