This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Improve known bits detection for PHI recurrences
Needs ReviewPublic

Authored by jmolloy on Jan 12 2016, 5:04 AM.

Details

Reviewers
hfinkel
sanjoy
Summary

Proving many facts about PHIs in ValueTracking is difficult due to its context-free nature (no caching). However, it is possible to prove that a PHI is non-negative (MSB is known zero) in many real-world situations.

Often, induction variables are very simple. We already have some support for looking at trivial induction variables (simple adds for example) in order to prove low-order bits are always zero due to stride.

This patch introduces more analysis for PHIs. Specifically, if a two-entry PHI has a non-negative constant integer in one operand it will try and trace the other operand through certain instructions until it hopefully finds the PHI again.

The set of instructions that will be recursed through is deliberately small - add, mul and select. On top of this small set, we will only ever follow a linear chain (an add/mul with a non-negative constant integer as the RHS operand). Because we only ever follow a linear chain of simple instructions the search space can never expand too much which also allows us to set a non-tiny search limit.

Diff Detail

Repository
rL LLVM

Event Timeline

jmolloy updated this revision to Diff 44620.Jan 12 2016, 5:04 AM
jmolloy retitled this revision from to [ValueTracking] Improve known bits detection for PHI recurrences.
jmolloy updated this object.
jmolloy added reviewers: hfinkel, sanjoy.
jmolloy set the repository for this revision to rL LLVM.
jmolloy added a subscriber: llvm-commits.
sanjoy requested changes to this revision.Jan 17 2016, 11:15 AM
sanjoy edited edge metadata.
sanjoy added inline comments.
lib/Analysis/ValueTracking.cpp
1461

Minor and subject to your discretion, but summarizing here what you've matched so far may be helpful for reading:

P = phi(NewL `op` P, R)

I'm also not sold on the name NewL -- what does "New" signify?

1472

Minor, but for multiplications I think we can actually use KnownZero2.countTrailingOnes() here (sometimes better and never worse than the min) -- do you mind adding a FIXME / TODO?

Edit: I noticed that this change doesn't really change this bit of code in a semantic way, so adding the TODO / FIXME isn't necessary.

1479

Since you're using the pattern few times here, I think it'll be cleaner if you add an m_NonNegative to PatterMatch.h and use that here and later (there already exist matchers like m_Power2 etc.).

1496

Use m_Value() (without the Dummy) since you want to ignore whatever you matched.

This revision now requires changes to proceed.Jan 17 2016, 11:15 AM
mcrosier resigned from this revision.May 19 2016, 9:15 AM
mcrosier removed a reviewer: mcrosier.

Feel free to add me back, if you decide to pursue this further, James.

mcrosier removed a subscriber: mcrosier.May 19 2016, 9:15 AM
mssimpso resigned from this revision.May 26 2016, 7:22 AM
mssimpso removed a reviewer: mssimpso.
sanjoy resigned from this revision.Jan 29 2022, 5:25 PM
This revision now requires review to proceed.Jan 29 2022, 5:25 PM