Skip to content

Commit 79b1bc8

Browse files
committed
Replace m_And() with a general SimplifyMultipleUseDemandedBits call
1 parent 60bfef2 commit 79b1bc8

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,6 +2772,7 @@ static Instruction *foldSelectWithClampedShift(SelectInst &SI,
27722772

27732773
auto MatchClampedShift = [&](Value *V, Value *Amt) -> BinaryOperator * {
27742774
Value *X, *Limit;
2775+
Instruction *I;
27752776

27762777
// Fold (select (icmp_ugt A, BW-1), TrueVal, (shift X, (umin A, C)))
27772778
// --> (select (icmp_ugt A, BW-1), TrueVal, (shift X, A))
@@ -2789,13 +2790,15 @@ static Instruction *foldSelectWithClampedShift(SelectInst &SI,
27892790
// --> (select (icmp_ugt A, BW-1), (shift X, A), FalseVal)
27902791
// Fold (select (icmp_ult A, BW), (shift X, (and A, C)), FalseVal)
27912792
// --> (select (icmp_ult A, BW), (shift X, A), FalseVal)
2792-
// iff Pow2 element width and C masks all amt bits.
2793+
// iff Pow2 element width we just demand the amt mask bits.
27932794
if (isPowerOf2_64(BW) &&
2794-
match(V, m_OneUse(m_Shift(m_Value(X),
2795-
m_And(m_Specific(Amt), m_Value(Limit)))))) {
2796-
KnownBits KnownLimit = IC.computeKnownBits(Limit, 0, &SI);
2797-
if (KnownLimit.countMinTrailingOnes() >= Log2_64(BW))
2798-
return cast<BinaryOperator>(V);
2795+
match(V, m_OneUse(m_Shift(m_Value(X), m_Instruction(I))))) {
2796+
KnownBits Known(BW);
2797+
APInt DemandedBits = APInt::getLowBitsSet(BW, Log2_64(BW));
2798+
if (Value *NewAmt = IC.SimplifyMultipleUseDemandedBits(
2799+
I, DemandedBits, Known, /*Depth=*/0,
2800+
IC.getSimplifyQuery().getWithInstruction(I)))
2801+
return Amt == NewAmt ? cast<BinaryOperator>(V) : nullptr;
27992802
}
28002803

28012804
return nullptr;

llvm/test/Transforms/InstCombine/select-shift-clamp.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ define <2 x i32> @select_uge_lshr_clamp_umin_v2i32(<2 x i32> %a0, <2 x i32> %a1,
175175
; CHECK-LABEL: @select_uge_lshr_clamp_umin_v2i32(
176176
; CHECK-NEXT: [[A1:%.*]] = freeze <2 x i32> [[A3:%.*]]
177177
; CHECK-NEXT: [[S:%.*]] = lshr <2 x i32> [[A0:%.*]], [[A1]]
178-
; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i32> [[A1]], <i32 31, i32 31>
178+
; CHECK-NEXT: [[C:%.*]] = icmp ugt <2 x i32> [[A1]], splat (i32 31)
179179
; CHECK-NEXT: [[R:%.*]] = select <2 x i1> [[C]], <2 x i32> [[A2:%.*]], <2 x i32> [[S]]
180180
; CHECK-NEXT: ret <2 x i32> [[R]]
181181
;
@@ -223,8 +223,8 @@ define i17 @select_uge_lshr_clamp_umin_i17_badlimit(i17 %a0, i17 %a1, i17 %a2) {
223223
define range(i64 0, -9223372036854775807) <4 x i64> @PR109888(<4 x i64> %0) {
224224
; CHECK-LABEL: @PR109888(
225225
; CHECK-NEXT: [[TMP0:%.*]] = freeze <4 x i64> [[TMP1:%.*]]
226-
; CHECK-NEXT: [[TMP2:%.*]] = shl nuw <4 x i64> <i64 1, i64 1, i64 1, i64 1>, [[TMP0]]
227-
; CHECK-NEXT: [[C:%.*]] = icmp ult <4 x i64> [[TMP0]], <i64 64, i64 64, i64 64, i64 64>
226+
; CHECK-NEXT: [[TMP2:%.*]] = shl nuw <4 x i64> splat (i64 1), [[TMP0]]
227+
; CHECK-NEXT: [[C:%.*]] = icmp ult <4 x i64> [[TMP0]], splat (i64 64)
228228
; CHECK-NEXT: [[R:%.*]] = select <4 x i1> [[C]], <4 x i64> [[TMP2]], <4 x i64> zeroinitializer
229229
; CHECK-NEXT: ret <4 x i64> [[R]]
230230
;

0 commit comments

Comments
 (0)