This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Added support to deduce PHI Nodes values being a power of 2
AbandonedPublic

Authored by huangjd on Apr 13 2022, 5:34 PM.

Details

Summary

Added support to deduce PHI Nodes values being a power of 2 in ValueTracking. This enables more optimizations in applications such as calculating alignment.

Diff Detail

Event Timeline

huangjd created this revision.Apr 13 2022, 5:34 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2022, 5:34 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
huangjd requested review of this revision.Apr 13 2022, 5:34 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2022, 5:34 PM

Can you add back the original list of reviewers and mark the places where the prior comments are addressed?

huangjd added inline comments.Apr 13 2022, 5:53 PM
llvm/lib/Analysis/ValueTracking.cpp
2028

Original comment from davidxl:
run a clang-format on the changed part.

2044

Original comment from davidxl:
typo: induction

llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
2

Original comment from xbolva00
Please use utils/update_test_checks.py

20

Original comment from davidxl:

Please also add tests for

  1. other operators for simple recurrence.
  2. for other type of recurrence (change at line 2177).
  3. chained phi case
  4. negative cases.
davidxl added inline comments.Apr 14 2022, 11:48 AM
llvm/lib/Analysis/ValueTracking.cpp
2058

is the power2 check for start done above already?

huangjd added inline comments.Apr 14 2022, 1:22 PM
llvm/lib/Analysis/ValueTracking.cpp
2058

This one checks for constant literal. The check before is for any expression that can be deduced to be a power of 2, which is not sufficient to be correct in this case because it may equal to sign bit (0x8000...) and sdiv of this number is no longer a power of 2

Carrot added inline comments.Apr 14 2022, 6:18 PM
llvm/lib/Analysis/ValueTracking.cpp
2173

Please add a comment here. I searched the source code for a while to understand its purpose.

The patch looks reasonable. Please wait for other reviewers (Sanjay ) to comment as well.

spatel added inline comments.Apr 19 2022, 8:28 AM
llvm/lib/Analysis/ValueTracking.cpp
2173

This request was not answered. We are using max depth to avoid a compile-time explosion because we are recursing on every phi operand?

2181–2187

It would be safer to split this patch into 2 parts. The first would be just this part (and eliminate adding isPowerOfTwoRecurrence until the next patch). We can't currently handle even minimal non-loop patterns like:

define i64 @known_power_of_two_urem_phi(i64 %x, i1 %b) {
entry:
  br i1 %b, label %t, label %f

t:                
  br label %end

f:           
  br label %end

end:             
  %p = phi i64 [ 4096, %t ], [ 4, %f ]
  %r = urem i64 %x, %p
  ret i64 %r
}
llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
7

If I understand correctly, there's no difference for this test - no phis, and it already works as expected.

Please pre-commit the tests with baseline CHECKs, so we just show test diffs in this review.

huangjd marked 2 inline comments as done.Apr 19 2022, 2:58 PM
huangjd added inline comments.
llvm/lib/Analysis/ValueTracking.cpp
2173

Looks like it should be just Depth instead, will fix it.

A similar appearance in line 2580 (isKnownNonZero) may be a bug then? This limits the level of recursion to be 2, instead of MaxAnalysisRecursionDepth.

nikic added inline comments.Apr 19 2022, 3:15 PM
llvm/lib/Analysis/ValueTracking.cpp
2173

This request was not answered. We are using max depth to avoid a compile-time explosion because we are recursing on every phi operand?

Yes. The depth limit is based on the assumption that instructions have few operands over which we recurse, generally resulting in up to 2^Depth recursive calls at most. For phis the branching factor may be arbitrarily high, resulting in Operands^Depth recursive calls.

The limit is not strictly necessary for the recurrence case though, and I don't think we apply it for recurrences in KnownBits/NonZero.

From the feedbacks, the action items are

  1. checkin baseine tests;
  2. split out recurrence handling from the main patch -- have it reviewed and committed
  3. send the recurrence handling patch, get it reviewed and committed (the depth may not be needed in this case)
spatel added a comment.May 4 2022, 3:44 AM

Abandon this patch? It is split up now:
D124885
D124889
D124890

huangjd abandoned this revision.May 4 2022, 11:11 AM