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
- Repository
- rG LLVM Github Monorepo
Event Timeline
Can you add back the original list of reviewers and mark the places where the prior comments are addressed?
llvm/lib/Analysis/ValueTracking.cpp | ||
---|---|---|
2028 | Original comment from davidxl: | |
2044 | Original comment from davidxl: | |
llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll | ||
2 | Original comment from xbolva00 | |
20 | Original comment from davidxl: Please also add tests for
|
llvm/lib/Analysis/ValueTracking.cpp | ||
---|---|---|
2058 | is the power2 check for start done above already? |
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 |
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.
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. |
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. |
llvm/lib/Analysis/ValueTracking.cpp | ||
---|---|---|
2173 |
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
- checkin baseine tests;
- split out recurrence handling from the main patch -- have it reviewed and committed
- send the recurrence handling patch, get it reviewed and committed (the depth may not be needed in this case)
Original comment from davidxl:
run a clang-format on the changed part.