diff options
author | William Huang <williamjhuang@google.com> | 2022-05-26 20:15:00 +0000 |
---|---|---|
committer | William Huang <williamjhuang@google.com> | 2022-05-26 20:30:31 +0000 |
commit | 35b0955aa59d3cf39678046ccff3728f6b7a91b0 (patch) | |
tree | 99a0550932d1094d7e4b64c0dc5651bf54f53986 /llvm/lib/Analysis/ValueTracking.cpp | |
parent | c4c750058f3e3b85c24a9e3ad356b76290e7c259 (diff) | |
download | llvm-35b0955aa59d3cf39678046ccff3728f6b7a91b0.zip llvm-35b0955aa59d3cf39678046ccff3728f6b7a91b0.tar.gz llvm-35b0955aa59d3cf39678046ccff3728f6b7a91b0.tar.bz2 |
[ValueTracking] Added support to deduce PHI Nodes values being a power of 2
Add Value Tracking support to deduce induction variable being a power of 2, allowing urem optimizations
Reviewed By: davidxl
Differential Revision: https://reviews.llvm.org/D126018
Diffstat (limited to 'llvm/lib/Analysis/ValueTracking.cpp')
-rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index c7586f2..473b3cb 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -2128,6 +2128,25 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, } } + // A PHI node is power of two if all incoming values are power of two. + if (const PHINode *PN = dyn_cast<PHINode>(V)) { + Query RecQ = Q; + + // Recursively check all incoming values. Limit recursion to 2 levels, so + // that search complexity is limited to number of operands^2. + unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1); + return llvm::all_of(PN->operands(), [&](const Use &U) { + // Value is power of 2 if it is coming from PHI node itself by induction. + if (U.get() == PN) + return true; + + // Change the context instruction to the incoming block where it is + // evaluated. + RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator(); + return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ); + }); + } + // An exact divide or right shift can only shift off zero bits, so the result // is a power of two only if the first operand is a power of two and not // copying a sign bit (sdiv int_min, 2). |