[PATCH] D127469: [InstCombine] Optimize lshr+shl+and conversion pattern
chenglin.bi via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 9 20:09:57 PDT 2022
bcl5980 created this revision.
bcl5980 added reviewers: spatel, RKSimon, craig.topper, nikic.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
bcl5980 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
if `C1` and `C3` are pow2 and `Log2(C3) >= C2`:
((C1 >> X) << C2) & C3 -> X == (Log2(C1)+C2-Log2(C3)) ? C3 : 0
https://alive2.llvm.org/ce/z/zvrkKF
https://reviews.llvm.org/D127469
Files:
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1908,25 +1908,40 @@
Constant *C1, *C2;
const APInt *C3 = C;
Value *X;
- if (C3->isPowerOf2() &&
- match(Op0, m_OneUse(m_LShr(m_Shl(m_ImmConstant(C1), m_Value(X)),
- m_ImmConstant(C2)))) &&
- match(C1, m_Power2())) {
- Constant *Log2C1 = ConstantExpr::getExactLogBase2(C1);
+ if (C3->isPowerOf2()) {
Constant *Log2C3 = ConstantInt::get(Ty, C3->countTrailingZeros());
- Constant *LshrC = ConstantExpr::getAdd(C2, Log2C3);
- KnownBits KnownLShrc = computeKnownBits(LshrC, 0, nullptr);
- if (KnownLShrc.getMaxValue().ult(Width)) {
- // iff C1,C3 is pow2 and C2 + cttz(C3) < BitWidth:
- // ((C1 << X) >> C2) & C3 -> X == (cttz(C3)+C2-cttz(C1)) ? C3 : 0
- Constant *CmpC = ConstantExpr::getSub(LshrC, Log2C1);
- Value *Cmp = Builder.CreateICmpEQ(X, CmpC);
- return SelectInst::Create(Cmp, ConstantInt::get(Ty, *C3),
- ConstantInt::getNullValue(Ty));
+ if (match(Op0, m_OneUse(m_LShr(m_Shl(m_ImmConstant(C1), m_Value(X)),
+ m_ImmConstant(C2)))) &&
+ match(C1, m_Power2())) {
+ Constant *Log2C1 = ConstantExpr::getExactLogBase2(C1);
+ Constant *LshrC = ConstantExpr::getAdd(C2, Log2C3);
+ KnownBits KnownLShrc = computeKnownBits(LshrC, 0, nullptr);
+ if (KnownLShrc.getMaxValue().ult(Width)) {
+ // iff C1,C3 is pow2 and C2 + cttz(C3) < BitWidth:
+ // ((C1 << X) >> C2) & C3 -> X == (cttz(C3)+C2-cttz(C1)) ? C3 : 0
+ Constant *CmpC = ConstantExpr::getSub(LshrC, Log2C1);
+ Value *Cmp = Builder.CreateICmpEQ(X, CmpC);
+ return SelectInst::Create(Cmp, ConstantInt::get(Ty, *C3),
+ ConstantInt::getNullValue(Ty));
+ }
+ }
+
+ if (match(Op0, m_OneUse(m_Shl(m_LShr(m_ImmConstant(C1), m_Value(X)),
+ m_ImmConstant(C2)))) &&
+ match(C1, m_Power2())) {
+ Constant *Log2C1 = ConstantExpr::getExactLogBase2(C1);
+ Constant *Cmp =
+ ConstantExpr::getCompare(ICmpInst::ICMP_ULT, Log2C3, C2);
+ if (Cmp->isZeroValue()) {
+ // iff C1,C3 is pow2 and Log2(C3) >= C2:
+ // ((C1 >> X) << C2) & C3 -> X == (cttz(C1)+C2-cttz(C3)) ? C3 : 0
+ Constant *ShlC = ConstantExpr::getAdd(C2, Log2C1);
+ Constant *CmpC = ConstantExpr::getSub(ShlC, Log2C3);
+ Value *Cmp = Builder.CreateICmpEQ(X, CmpC);
+ return SelectInst::Create(Cmp, ConstantInt::get(Ty, *C3),
+ ConstantInt::getNullValue(Ty));
+ }
}
- // TODO: Symmetrical case
- // iff C1,C3 is pow2 and Log2(C3) >= C2:
- // ((C1 >> X) << C2) & C3 -> X == (cttz(C1)+C2-cttz(C3)) ? C3 : 0
}
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127469.435776.patch
Type: text/x-patch
Size: 3106 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220610/df695320/attachment.bin>
More information about the llvm-commits
mailing list