Skip to content

Commit 72b8d41

Browse files
committed
[InstCombine] Shift amount reassociation in bittest (PR42399)
Summary: Given pattern: `icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0` we should move shifts to the same hand of 'and', i.e. rewrite as `icmp eq/ne (and (x shift (Q+K)), y), 0` iff `(Q+K) u< bitwidth(x)` It might be tempting to not restrict this to situations where we know we'd fold two shifts together, but i'm not sure what rules should there be to avoid endless combine loops. We pick the same shift that was originally used to shift the variable we picked to shift: https://rise4fun.com/Alive/6x1v Should fix [[ https://bugs.llvm.org/show_bug.cgi?id=42399 | PR42399]]. Reviewers: spatel, nikic, RKSimon Reviewed By: spatel Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63829 llvm-svn: 364791
1 parent 5abf80c commit 72b8d41

File tree

2 files changed

+148
-108
lines changed

2 files changed

+148
-108
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3270,6 +3270,63 @@ foldICmpWithTruncSignExtendedVal(ICmpInst &I,
32703270
return T1;
32713271
}
32723272

3273+
// Given pattern:
3274+
// icmp eq/ne (and ((x shift Q), (y oppositeshift K))), 0
3275+
// we should move shifts to the same hand of 'and', i.e. rewrite as
3276+
// icmp eq/ne (and (x shift (Q+K)), y), 0 iff (Q+K) u< bitwidth(x)
3277+
// We are only interested in opposite logical shifts here.
3278+
// If we can, we want to end up creating 'lshr' shift.
3279+
static Value *
3280+
foldShiftIntoShiftInAnotherHandOfAndInICmp(ICmpInst &I, const SimplifyQuery SQ,
3281+
InstCombiner::BuilderTy &Builder) {
3282+
if (!I.isEquality() || !match(I.getOperand(1), m_Zero()) ||
3283+
!I.getOperand(0)->hasOneUse())
3284+
return nullptr;
3285+
3286+
auto m_AnyLogicalShift = m_LogicalShift(m_Value(), m_Value());
3287+
auto m_AnyLShr = m_LShr(m_Value(), m_Value());
3288+
3289+
// Look for an 'and' of two (opposite) logical shifts.
3290+
// Pick the single-use shift as XShift.
3291+
Value *XShift, *YShift;
3292+
if (!match(I.getOperand(0),
3293+
m_c_And(m_OneUse(m_CombineAnd(m_AnyLogicalShift, m_Value(XShift))),
3294+
m_CombineAnd(m_AnyLogicalShift, m_Value(YShift)))))
3295+
return nullptr;
3296+
3297+
// If YShift is a single-use 'lshr', swap the shifts around.
3298+
if (match(YShift, m_OneUse(m_AnyLShr)))
3299+
std::swap(XShift, YShift);
3300+
3301+
// The shifts must be in opposite directions.
3302+
Instruction::BinaryOps XShiftOpcode =
3303+
cast<BinaryOperator>(XShift)->getOpcode();
3304+
if (XShiftOpcode == cast<BinaryOperator>(YShift)->getOpcode())
3305+
return nullptr; // Do not care about same-direction shifts here.
3306+
3307+
Value *X, *XShAmt, *Y, *YShAmt;
3308+
match(XShift, m_BinOp(m_Value(X), m_Value(XShAmt)));
3309+
match(YShift, m_BinOp(m_Value(Y), m_Value(YShAmt)));
3310+
3311+
// Can we fold (XShAmt+YShAmt) ?
3312+
Value *NewShAmt = SimplifyBinOp(Instruction::BinaryOps::Add, XShAmt, YShAmt,
3313+
SQ.getWithInstruction(&I));
3314+
if (!NewShAmt)
3315+
return nullptr;
3316+
// Is the new shift amount smaller than the bit width?
3317+
// FIXME: could also rely on ConstantRange.
3318+
unsigned BitWidth = X->getType()->getScalarSizeInBits();
3319+
if (!match(NewShAmt, m_SpecificInt_ULT(APInt(BitWidth, BitWidth))))
3320+
return nullptr;
3321+
// All good, we can do this fold. The shift is the same that was for X.
3322+
Value *T0 = XShiftOpcode == Instruction::BinaryOps::LShr
3323+
? Builder.CreateLShr(X, NewShAmt)
3324+
: Builder.CreateShl(X, NewShAmt);
3325+
Value *T1 = Builder.CreateAnd(T0, Y);
3326+
return Builder.CreateICmp(I.getPredicate(), T1,
3327+
Constant::getNullValue(X->getType()));
3328+
}
3329+
32733330
/// Try to fold icmp (binop), X or icmp X, (binop).
32743331
/// TODO: A large part of this logic is duplicated in InstSimplify's
32753332
/// simplifyICmpWithBinOp(). We should be able to share that and avoid the code
@@ -3625,6 +3682,9 @@ Instruction *InstCombiner::foldICmpBinOp(ICmpInst &I) {
36253682
if (Value *V = foldICmpWithTruncSignExtendedVal(I, Builder))
36263683
return replaceInstUsesWith(I, V);
36273684

3685+
if (Value *V = foldShiftIntoShiftInAnotherHandOfAndInICmp(I, SQ, Builder))
3686+
return replaceInstUsesWith(I, V);
3687+
36283688
return nullptr;
36293689
}
36303690

0 commit comments

Comments
 (0)