Skip to content

Commit

Permalink
[InstCombine] allow matching vector splat constants in foldLogOpOfMas…
Browse files Browse the repository at this point in the history
…kedICmps()

This is NFC-intended for scalar code. There are still unnecessary
m_ConstantInt restrictions in surrounding code, so this is not a
complete fix.

This prevents regressions seen with a planned follow-on to D111410.
  • Loading branch information
rotateright committed Oct 13, 2021
1 parent d67022f commit 905d170
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 53 deletions.
57 changes: 27 additions & 30 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,15 @@ enum MaskedICmpType {
/// satisfies.
static unsigned getMaskedICmpType(Value *A, Value *B, Value *C,
ICmpInst::Predicate Pred) {
ConstantInt *ACst = dyn_cast<ConstantInt>(A);
ConstantInt *BCst = dyn_cast<ConstantInt>(B);
ConstantInt *CCst = dyn_cast<ConstantInt>(C);
const APInt *ConstA = nullptr, *ConstB = nullptr, *ConstC = nullptr;
match(A, m_APInt(ConstA));
match(B, m_APInt(ConstB));
match(C, m_APInt(ConstC));
bool IsEq = (Pred == ICmpInst::ICMP_EQ);
bool IsAPow2 = (ACst && !ACst->isZero() && ACst->getValue().isPowerOf2());
bool IsBPow2 = (BCst && !BCst->isZero() && BCst->getValue().isPowerOf2());
bool IsAPow2 = ConstA && ConstA->isPowerOf2();
bool IsBPow2 = ConstB && ConstB->isPowerOf2();
unsigned MaskVal = 0;
if (CCst && CCst->isZero()) {
if (ConstC && ConstC->isZero()) {
// if C is zero, then both A and B qualify as mask
MaskVal |= (IsEq ? (Mask_AllZeros | AMask_Mixed | BMask_Mixed)
: (Mask_NotAllZeros | AMask_NotMixed | BMask_NotMixed));
Expand All @@ -211,7 +212,7 @@ static unsigned getMaskedICmpType(Value *A, Value *B, Value *C,
if (IsAPow2)
MaskVal |= (IsEq ? (Mask_NotAllZeros | AMask_NotMixed)
: (Mask_AllZeros | AMask_Mixed));
} else if (ACst && CCst && ConstantExpr::getAnd(ACst, CCst) == CCst) {
} else if (ConstA && ConstC && ConstC->isSubsetOf(*ConstA)) {
MaskVal |= (IsEq ? AMask_Mixed : AMask_NotMixed);
}

Expand All @@ -221,7 +222,7 @@ static unsigned getMaskedICmpType(Value *A, Value *B, Value *C,
if (IsBPow2)
MaskVal |= (IsEq ? (Mask_NotAllZeros | BMask_NotMixed)
: (Mask_AllZeros | BMask_Mixed));
} else if (BCst && CCst && ConstantExpr::getAnd(BCst, CCst) == CCst) {
} else if (ConstB && ConstC && ConstC->isSubsetOf(*ConstB)) {
MaskVal |= (IsEq ? BMask_Mixed : BMask_NotMixed);
}

Expand Down Expand Up @@ -269,9 +270,9 @@ getMaskedTypeForICmpPair(Value *&A, Value *&B, Value *&C,
ICmpInst *RHS,
ICmpInst::Predicate &PredL,
ICmpInst::Predicate &PredR) {
// vectors are not (yet?) supported. Don't support pointers either.
if (!LHS->getOperand(0)->getType()->isIntegerTy() ||
!RHS->getOperand(0)->getType()->isIntegerTy())
// Don't allow pointers. Splat vectors are fine.
if (!LHS->getOperand(0)->getType()->isIntOrIntVectorTy() ||
!RHS->getOperand(0)->getType()->isIntOrIntVectorTy())
return None;

// Here comes the tricky part:
Expand Down Expand Up @@ -619,8 +620,8 @@ static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
// Remaining cases assume at least that B and D are constant, and depend on
// their actual values. This isn't strictly necessary, just a "handle the
// easy cases for now" decision.
ConstantInt *BCst, *DCst;
if (!match(B, m_ConstantInt(BCst)) || !match(D, m_ConstantInt(DCst)))
const APInt *ConstB, *ConstD;
if (!match(B, m_APInt(ConstB)) || !match(D, m_APInt(ConstD)))
return nullptr;

if (Mask & (Mask_NotAllZeros | BMask_NotAllOnes)) {
Expand All @@ -629,11 +630,10 @@ static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
// -> (icmp ne (A & B), 0) or (icmp ne (A & D), 0)
// Only valid if one of the masks is a superset of the other (check "B&D" is
// the same as either B or D).
APInt NewMask = BCst->getValue() & DCst->getValue();

if (NewMask == BCst->getValue())
APInt NewMask = *ConstB & *ConstD;
if (NewMask == *ConstB)
return LHS;
else if (NewMask == DCst->getValue())
else if (NewMask == *ConstD)
return RHS;
}

Expand All @@ -642,11 +642,10 @@ static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
// -> (icmp ne (A & B), A) or (icmp ne (A & D), A)
// Only valid if one of the masks is a superset of the other (check "B|D" is
// the same as either B or D).
APInt NewMask = BCst->getValue() | DCst->getValue();

if (NewMask == BCst->getValue())
APInt NewMask = *ConstB | *ConstD;
if (NewMask == *ConstB)
return LHS;
else if (NewMask == DCst->getValue())
else if (NewMask == *ConstD)
return RHS;
}

Expand All @@ -661,23 +660,21 @@ static Value *foldLogOpOfMaskedICmps(ICmpInst *LHS, ICmpInst *RHS, bool IsAnd,
// We can't simply use C and E because we might actually handle
// (icmp ne (A & B), B) & (icmp eq (A & D), D)
// with B and D, having a single bit set.
ConstantInt *CCst, *ECst;
if (!match(C, m_ConstantInt(CCst)) || !match(E, m_ConstantInt(ECst)))
const APInt *OldConstC, *OldConstE;
if (!match(C, m_APInt(OldConstC)) || !match(E, m_APInt(OldConstE)))
return nullptr;
if (PredL != NewCC)
CCst = cast<ConstantInt>(ConstantExpr::getXor(BCst, CCst));
if (PredR != NewCC)
ECst = cast<ConstantInt>(ConstantExpr::getXor(DCst, ECst));

const APInt ConstC = PredL != NewCC ? *ConstB ^ *OldConstC : *OldConstC;
const APInt ConstE = PredR != NewCC ? *ConstD ^ *OldConstE : *OldConstE;

// If there is a conflict, we should actually return a false for the
// whole construct.
if (((BCst->getValue() & DCst->getValue()) &
(CCst->getValue() ^ ECst->getValue())).getBoolValue())
if (((*ConstB & *ConstD) & (ConstC ^ ConstE)).getBoolValue())
return ConstantInt::get(LHS->getType(), !IsAnd);

Value *NewOr1 = Builder.CreateOr(B, D);
Value *NewOr2 = ConstantExpr::getOr(CCst, ECst);
Value *NewAnd = Builder.CreateAnd(A, NewOr1);
Constant *NewOr2 = ConstantInt::get(A->getType(), ConstC | ConstE);
return Builder.CreateICmp(NewCC, NewAnd, NewOr2);
}

Expand Down
9 changes: 3 additions & 6 deletions llvm/test/Transforms/InstCombine/bit-checks.ll
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,9 @@ define i32 @main4(i32 %argc) {

define <2 x i32> @main4_splat(<2 x i32> %argc) {
; CHECK-LABEL: @main4_splat(
; CHECK-NEXT: [[AND:%.*]] = and <2 x i32> [[ARGC:%.*]], <i32 7, i32 7>
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne <2 x i32> [[AND]], <i32 7, i32 7>
; CHECK-NEXT: [[AND2:%.*]] = and <2 x i32> [[ARGC]], <i32 48, i32 48>
; CHECK-NEXT: [[TOBOOL3:%.*]] = icmp ne <2 x i32> [[AND2]], <i32 48, i32 48>
; CHECK-NEXT: [[NOT_AND_COND:%.*]] = or <2 x i1> [[TOBOOL]], [[TOBOOL3]]
; CHECK-NEXT: [[STOREMERGE:%.*]] = zext <2 x i1> [[NOT_AND_COND]] to <2 x i32>
; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[ARGC:%.*]], <i32 55, i32 55>
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne <2 x i32> [[TMP1]], <i32 55, i32 55>
; CHECK-NEXT: [[STOREMERGE:%.*]] = zext <2 x i1> [[TMP2]] to <2 x i32>
; CHECK-NEXT: ret <2 x i32> [[STOREMERGE]]
;
%and = and <2 x i32> %argc, <i32 7, i32 7>
Expand Down
5 changes: 1 addition & 4 deletions llvm/test/Transforms/InstCombine/icmp-logical.ll
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ define <2 x i1> @masked_and_notallzeroes_splat(<2 x i32> %A) {
; CHECK-LABEL: @masked_and_notallzeroes_splat(
; CHECK-NEXT: [[MASK1:%.*]] = and <2 x i32> [[A:%.*]], <i32 7, i32 7>
; CHECK-NEXT: [[TST1:%.*]] = icmp ne <2 x i32> [[MASK1]], zeroinitializer
; CHECK-NEXT: [[MASK2:%.*]] = and <2 x i32> [[A]], <i32 39, i32 39>
; CHECK-NEXT: [[TST2:%.*]] = icmp ne <2 x i32> [[MASK2]], zeroinitializer
; CHECK-NEXT: [[RES:%.*]] = and <2 x i1> [[TST1]], [[TST2]]
; CHECK-NEXT: ret <2 x i1> [[RES]]
; CHECK-NEXT: ret <2 x i1> [[TST1]]
;
%mask1 = and <2 x i32> %A, <i32 7, i32 7>
%tst1 = icmp ne <2 x i32> %mask1, <i32 0, i32 0>
Expand Down
7 changes: 3 additions & 4 deletions llvm/test/Transforms/InstCombine/onehot_merge.ll
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,10 @@ define i1 @foo1_or_signbit_lshr_without_shifting_signbit_both_sides(i32 %k, i32
define <2 x i1> @foo1_or_signbit_lshr_without_shifting_signbit_both_sides_splat(<2 x i32> %k, <2 x i32> %c1, <2 x i32> %c2) {
; CHECK-LABEL: @foo1_or_signbit_lshr_without_shifting_signbit_both_sides_splat(
; CHECK-NEXT: [[T0:%.*]] = shl <2 x i32> [[K:%.*]], [[C1:%.*]]
; CHECK-NEXT: [[T1:%.*]] = icmp slt <2 x i32> [[T0]], zeroinitializer
; CHECK-NEXT: [[T2:%.*]] = shl <2 x i32> [[K]], [[C2:%.*]]
; CHECK-NEXT: [[T3:%.*]] = icmp slt <2 x i32> [[T2]], zeroinitializer
; CHECK-NEXT: [[OR:%.*]] = and <2 x i1> [[T1]], [[T3]]
; CHECK-NEXT: ret <2 x i1> [[OR]]
; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[T0]], [[T2]]
; CHECK-NEXT: [[TMP2:%.*]] = icmp slt <2 x i32> [[TMP1]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[TMP2]]
;
%t0 = shl <2 x i32> %k, %c1
%t1 = icmp slt <2 x i32> %t0, zeroinitializer
Expand Down
7 changes: 2 additions & 5 deletions llvm/test/Transforms/InstCombine/or.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1459,11 +1459,8 @@ define i1 @cmp_overlap(i32 %x) {

define <2 x i1> @cmp_overlap_splat(<2 x i5> %x) {
; CHECK-LABEL: @cmp_overlap_splat(
; CHECK-NEXT: [[ISNEG:%.*]] = icmp slt <2 x i5> [[X:%.*]], zeroinitializer
; CHECK-NEXT: [[NOTSUB:%.*]] = add <2 x i5> [[X]], <i5 -1, i5 -1>
; CHECK-NEXT: [[ISNOTNEG:%.*]] = icmp slt <2 x i5> [[NOTSUB]], zeroinitializer
; CHECK-NEXT: [[R:%.*]] = or <2 x i1> [[ISNEG]], [[ISNOTNEG]]
; CHECK-NEXT: ret <2 x i1> [[R]]
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i5> [[X:%.*]], <i5 1, i5 1>
; CHECK-NEXT: ret <2 x i1> [[TMP1]]
;
%isneg = icmp slt <2 x i5> %x, zeroinitializer
%negx = sub <2 x i5> zeroinitializer, %x
Expand Down
7 changes: 3 additions & 4 deletions llvm/test/Transforms/InstCombine/sign-test-and-or.ll
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ define i1 @test1(i32 %a, i32 %b) {

define <2 x i1> @test1_splat(<2 x i32> %a, <2 x i32> %b) {
; CHECK-LABEL: @test1_splat(
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[A:%.*]], zeroinitializer
; CHECK-NEXT: [[TMP2:%.*]] = icmp slt <2 x i32> [[B:%.*]], zeroinitializer
; CHECK-NEXT: [[OR_COND:%.*]] = or <2 x i1> [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret <2 x i1> [[OR_COND]]
; CHECK-NEXT: [[TMP1:%.*]] = or <2 x i32> [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[TMP2:%.*]] = icmp slt <2 x i32> [[TMP1]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[TMP2]]
;
%1 = icmp slt <2 x i32> %a, zeroinitializer
%2 = icmp slt <2 x i32> %b, zeroinitializer
Expand Down

0 comments on commit 905d170

Please sign in to comment.