Skip to content

Commit 441c9d6

Browse files
committed
[InstCombine] Dropping redundant masking before left-shift [4/5] (PR42563)
Summary: If we have some pattern that leaves only some low bits set, and then performs left-shift of those bits, if none of the bits that are left after the final shift are modified by the mask, we can omit the mask. There are many variants to this pattern: e. `((x << MaskShAmt) l>> MaskShAmt) << ShiftShAmt` All these patterns can be simplified to just: `x << ShiftShAmt` iff: e. `(ShiftShAmt-MaskShAmt) s>= 0` (i.e. `ShiftShAmt u>= MaskShAmt`) alive proofs: e: https://rise4fun.com/Alive/0FT For now let's start with patterns where both shift amounts are variable, with trivial constant "offset" between them, since i believe this is both simplest to handle and i think this is most common. But again, there are likely other variants where we could use ValueTracking/ConstantRange to handle more cases. https://bugs.llvm.org/show_bug.cgi?id=42563 Differential Revision: https://reviews.llvm.org/D64521 llvm-svn: 366539
1 parent 3c212ce commit 441c9d6

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ reassociateShiftAmtsOfTwoSameDirectionShifts(BinaryOperator *Sh0,
7474
// b) (x & (~(-1 << MaskShAmt))) << ShiftShAmt
7575
// c) (x & (-1 >> MaskShAmt)) << ShiftShAmt
7676
// d) (x & ((-1 << MaskShAmt) >> MaskShAmt)) << ShiftShAmt
77+
// e) ((x << MaskShAmt) l>> MaskShAmt) << ShiftShAmt
7778
// All these patterns can be simplified to just:
7879
// x << ShiftShAmt
7980
// iff:
80-
// a,b) (MaskShAmt+ShiftShAmt) u>= bitwidth(x)
81-
// c,d) (ShiftShAmt-MaskShAmt) s>= 0 (i.e. ShiftShAmt u>= MaskShAmt)
81+
// a,b) (MaskShAmt+ShiftShAmt) u>= bitwidth(x)
82+
// c,d,e) (ShiftShAmt-MaskShAmt) s>= 0 (i.e. ShiftShAmt u>= MaskShAmt)
8283
static Instruction *
8384
dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
8485
const SimplifyQuery &SQ) {
@@ -115,7 +116,9 @@ dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
115116
APInt(BitWidth, BitWidth))))
116117
return nullptr;
117118
// All good, we can do this fold.
118-
} else if (match(Masked, m_c_And(m_CombineOr(MaskC, MaskD), m_Value(X)))) {
119+
} else if (match(Masked, m_c_And(m_CombineOr(MaskC, MaskD), m_Value(X))) ||
120+
match(Masked, m_LShr(m_Shl(m_Value(X), m_Value(MaskShAmt)),
121+
m_Deferred(MaskShAmt)))) {
119122
// Can we simplify (ShiftShAmt-MaskShAmt) ?
120123
Value *ShAmtsDiff =
121124
SimplifySubInst(ShiftShAmt, MaskShAmt, /*IsNSW=*/false, /*IsNUW=*/false,

llvm/test/Transforms/InstCombine/redundant-left-shift-input-masking-variant-e.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ define i32 @t0_basic(i32 %x, i32 %nbits) {
2121
; CHECK-NEXT: [[T1:%.*]] = lshr i32 [[T0]], [[NBITS]]
2222
; CHECK-NEXT: call void @use32(i32 [[T0]])
2323
; CHECK-NEXT: call void @use32(i32 [[T1]])
24-
; CHECK-NEXT: [[T2:%.*]] = shl i32 [[T1]], [[NBITS]]
24+
; CHECK-NEXT: [[T2:%.*]] = shl i32 [[X]], [[NBITS]]
2525
; CHECK-NEXT: ret i32 [[T2]]
2626
;
2727
%t0 = shl i32 %x, %nbits
@@ -40,7 +40,7 @@ define i32 @t1_bigger_shift(i32 %x, i32 %nbits) {
4040
; CHECK-NEXT: call void @use32(i32 [[T0]])
4141
; CHECK-NEXT: call void @use32(i32 [[T1]])
4242
; CHECK-NEXT: call void @use32(i32 [[T2]])
43-
; CHECK-NEXT: [[T3:%.*]] = shl i32 [[T1]], [[T2]]
43+
; CHECK-NEXT: [[T3:%.*]] = shl i32 [[X]], [[T2]]
4444
; CHECK-NEXT: ret i32 [[T3]]
4545
;
4646
%t0 = shl i32 %x, %nbits
@@ -65,7 +65,7 @@ define <3 x i32> @t2_vec_splat(<3 x i32> %x, <3 x i32> %nbits) {
6565
; CHECK-NEXT: call void @use3xi32(<3 x i32> [[T0]])
6666
; CHECK-NEXT: call void @use3xi32(<3 x i32> [[T1]])
6767
; CHECK-NEXT: call void @use3xi32(<3 x i32> [[T2]])
68-
; CHECK-NEXT: [[T3:%.*]] = shl <3 x i32> [[T1]], [[T2]]
68+
; CHECK-NEXT: [[T3:%.*]] = shl <3 x i32> [[X]], [[T2]]
6969
; CHECK-NEXT: ret <3 x i32> [[T3]]
7070
;
7171
%t0 = shl <3 x i32> %x, %nbits
@@ -86,7 +86,7 @@ define <3 x i32> @t3_vec_nonsplat(<3 x i32> %x, <3 x i32> %nbits) {
8686
; CHECK-NEXT: call void @use3xi32(<3 x i32> [[T0]])
8787
; CHECK-NEXT: call void @use3xi32(<3 x i32> [[T1]])
8888
; CHECK-NEXT: call void @use3xi32(<3 x i32> [[T2]])
89-
; CHECK-NEXT: [[T3:%.*]] = shl <3 x i32> [[T1]], [[T2]]
89+
; CHECK-NEXT: [[T3:%.*]] = shl <3 x i32> [[X]], [[T2]]
9090
; CHECK-NEXT: ret <3 x i32> [[T3]]
9191
;
9292
%t0 = shl <3 x i32> %x, %nbits
@@ -107,7 +107,7 @@ define <3 x i32> @t4_vec_undef(<3 x i32> %x, <3 x i32> %nbits) {
107107
; CHECK-NEXT: call void @use3xi32(<3 x i32> [[T0]])
108108
; CHECK-NEXT: call void @use3xi32(<3 x i32> [[T1]])
109109
; CHECK-NEXT: call void @use3xi32(<3 x i32> [[T2]])
110-
; CHECK-NEXT: [[T3:%.*]] = shl <3 x i32> [[T1]], [[T2]]
110+
; CHECK-NEXT: [[T3:%.*]] = shl <3 x i32> [[X]], [[T2]]
111111
; CHECK-NEXT: ret <3 x i32> [[T3]]
112112
;
113113
%t0 = shl <3 x i32> %x, %nbits
@@ -128,7 +128,7 @@ define i32 @t5_nuw(i32 %x, i32 %nbits) {
128128
; CHECK-NEXT: [[T1:%.*]] = lshr i32 [[T0]], [[NBITS]]
129129
; CHECK-NEXT: call void @use32(i32 [[T0]])
130130
; CHECK-NEXT: call void @use32(i32 [[T1]])
131-
; CHECK-NEXT: [[T2:%.*]] = shl nuw i32 [[T1]], [[NBITS]]
131+
; CHECK-NEXT: [[T2:%.*]] = shl i32 [[X]], [[NBITS]]
132132
; CHECK-NEXT: ret i32 [[T2]]
133133
;
134134
%t0 = shl i32 %x, %nbits
@@ -145,7 +145,7 @@ define i32 @t6_nsw(i32 %x, i32 %nbits) {
145145
; CHECK-NEXT: [[T1:%.*]] = lshr i32 [[T0]], [[NBITS]]
146146
; CHECK-NEXT: call void @use32(i32 [[T0]])
147147
; CHECK-NEXT: call void @use32(i32 [[T1]])
148-
; CHECK-NEXT: [[T2:%.*]] = shl nsw i32 [[T1]], [[NBITS]]
148+
; CHECK-NEXT: [[T2:%.*]] = shl i32 [[X]], [[NBITS]]
149149
; CHECK-NEXT: ret i32 [[T2]]
150150
;
151151
%t0 = shl i32 %x, %nbits
@@ -162,7 +162,7 @@ define i32 @t7_nuw_nsw(i32 %x, i32 %nbits) {
162162
; CHECK-NEXT: [[T1:%.*]] = lshr i32 [[T0]], [[NBITS]]
163163
; CHECK-NEXT: call void @use32(i32 [[T0]])
164164
; CHECK-NEXT: call void @use32(i32 [[T1]])
165-
; CHECK-NEXT: [[T2:%.*]] = shl nuw nsw i32 [[T1]], [[NBITS]]
165+
; CHECK-NEXT: [[T2:%.*]] = shl i32 [[X]], [[NBITS]]
166166
; CHECK-NEXT: ret i32 [[T2]]
167167
;
168168
%t0 = shl i32 %x, %nbits

0 commit comments

Comments
 (0)