Skip to content

Commit c6013f7

Browse files
committed
Revert "[InstCombine] fold cast of right-shift if high bits are not demanded"
This reverts commit 2f6b073. This caused several bots to hit an infinite loop at stage 2, so it needs to be reverted while figuring out how to fix that.
1 parent 1ee851c commit c6013f7

File tree

2 files changed

+21
-55
lines changed

2 files changed

+21
-55
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -385,26 +385,8 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask,
385385
Known = KnownBits::commonBits(LHSKnown, RHSKnown);
386386
break;
387387
}
388+
case Instruction::ZExt:
388389
case Instruction::Trunc: {
389-
// If we do not demand the high bits of a right-shifted and truncated value,
390-
// then we may be able to truncate it before the shift.
391-
Value *X;
392-
const APInt *C;
393-
if (match(I->getOperand(0), m_OneUse(m_LShr(m_Value(X), m_APInt(C))))) {
394-
// The shift amount must be valid (not poison) in the narrow type, and
395-
// it must not be greater than the high bits demanded of the result.
396-
if (C->ult(I->getType()->getScalarSizeInBits()) &&
397-
C->ule(DemandedMask.countLeadingZeros())) {
398-
// trunc (lshr X, C) --> lshr (trunc X), C
399-
IRBuilderBase::InsertPointGuard Guard(Builder);
400-
Builder.SetInsertPoint(I);
401-
Value *Trunc = Builder.CreateTrunc(X, I->getType());
402-
return Builder.CreateLShr(Trunc, C->getZExtValue());
403-
}
404-
}
405-
}
406-
LLVM_FALLTHROUGH;
407-
case Instruction::ZExt: {
408390
unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
409391

410392
APInt InputDemandedMask = DemandedMask.zextOrTrunc(SrcBitWidth);

llvm/test/Transforms/InstCombine/trunc-demand.ll

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ declare void @use8(i8)
66

77
define i6 @trunc_lshr(i8 %x) {
88
; CHECK-LABEL: @trunc_lshr(
9-
; CHECK-NEXT: [[TMP1:%.*]] = trunc i8 [[X:%.*]] to i6
10-
; CHECK-NEXT: [[TMP2:%.*]] = lshr i6 [[TMP1]], 2
11-
; CHECK-NEXT: [[R:%.*]] = and i6 [[TMP2]], 14
9+
; CHECK-NEXT: [[S:%.*]] = lshr i8 [[X:%.*]], 2
10+
; CHECK-NEXT: [[T:%.*]] = trunc i8 [[S]] to i6
11+
; CHECK-NEXT: [[R:%.*]] = and i6 [[T]], 14
1212
; CHECK-NEXT: ret i6 [[R]]
1313
;
1414
%s = lshr i8 %x, 2
@@ -17,22 +17,19 @@ define i6 @trunc_lshr(i8 %x) {
1717
ret i6 %r
1818
}
1919

20-
; The 'and' is eliminated.
21-
2220
define i6 @trunc_lshr_exact_mask(i8 %x) {
2321
; CHECK-LABEL: @trunc_lshr_exact_mask(
24-
; CHECK-NEXT: [[TMP1:%.*]] = trunc i8 [[X:%.*]] to i6
25-
; CHECK-NEXT: [[TMP2:%.*]] = lshr i6 [[TMP1]], 2
26-
; CHECK-NEXT: ret i6 [[TMP2]]
22+
; CHECK-NEXT: [[S:%.*]] = lshr i8 [[X:%.*]], 2
23+
; CHECK-NEXT: [[T:%.*]] = trunc i8 [[S]] to i6
24+
; CHECK-NEXT: [[R:%.*]] = and i6 [[T]], 15
25+
; CHECK-NEXT: ret i6 [[R]]
2726
;
2827
%s = lshr i8 %x, 2
2928
%t = trunc i8 %s to i6
3029
%r = and i6 %t, 15
3130
ret i6 %r
3231
}
3332

34-
; negative test - a high bit of x is in the result
35-
3633
define i6 @trunc_lshr_big_mask(i8 %x) {
3734
; CHECK-LABEL: @trunc_lshr_big_mask(
3835
; CHECK-NEXT: [[S:%.*]] = lshr i8 [[X:%.*]], 2
@@ -46,8 +43,6 @@ define i6 @trunc_lshr_big_mask(i8 %x) {
4643
ret i6 %r
4744
}
4845

49-
; negative test - too many uses
50-
5146
define i6 @trunc_lshr_use1(i8 %x) {
5247
; CHECK-LABEL: @trunc_lshr_use1(
5348
; CHECK-NEXT: [[S:%.*]] = lshr i8 [[X:%.*]], 2
@@ -63,8 +58,6 @@ define i6 @trunc_lshr_use1(i8 %x) {
6358
ret i6 %r
6459
}
6560

66-
; negative test - too many uses
67-
6861
define i6 @trunc_lshr_use2(i8 %x) {
6962
; CHECK-LABEL: @trunc_lshr_use2(
7063
; CHECK-NEXT: [[S:%.*]] = lshr i8 [[X:%.*]], 2
@@ -80,13 +73,11 @@ define i6 @trunc_lshr_use2(i8 %x) {
8073
ret i6 %r
8174
}
8275

83-
; Splat vectors are ok.
84-
8576
define <2 x i7> @trunc_lshr_vec_splat(<2 x i16> %x) {
8677
; CHECK-LABEL: @trunc_lshr_vec_splat(
87-
; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i16> [[X:%.*]] to <2 x i7>
88-
; CHECK-NEXT: [[TMP2:%.*]] = lshr <2 x i7> [[TMP1]], <i7 5, i7 5>
89-
; CHECK-NEXT: [[R:%.*]] = and <2 x i7> [[TMP2]], <i7 1, i7 1>
78+
; CHECK-NEXT: [[S:%.*]] = lshr <2 x i16> [[X:%.*]], <i16 5, i16 5>
79+
; CHECK-NEXT: [[T:%.*]] = trunc <2 x i16> [[S]] to <2 x i7>
80+
; CHECK-NEXT: [[R:%.*]] = and <2 x i7> [[T]], <i7 1, i7 1>
9081
; CHECK-NEXT: ret <2 x i7> [[R]]
9182
;
9283
%s = lshr <2 x i16> %x, <i16 5, i16 5>
@@ -95,22 +86,19 @@ define <2 x i7> @trunc_lshr_vec_splat(<2 x i16> %x) {
9586
ret <2 x i7> %r
9687
}
9788

98-
; The 'and' is eliminated.
99-
10089
define <2 x i7> @trunc_lshr_vec_splat_exact_mask(<2 x i16> %x) {
10190
; CHECK-LABEL: @trunc_lshr_vec_splat_exact_mask(
102-
; CHECK-NEXT: [[TMP1:%.*]] = trunc <2 x i16> [[X:%.*]] to <2 x i7>
103-
; CHECK-NEXT: [[TMP2:%.*]] = lshr <2 x i7> [[TMP1]], <i7 6, i7 6>
104-
; CHECK-NEXT: ret <2 x i7> [[TMP2]]
91+
; CHECK-NEXT: [[S:%.*]] = lshr <2 x i16> [[X:%.*]], <i16 6, i16 6>
92+
; CHECK-NEXT: [[T:%.*]] = trunc <2 x i16> [[S]] to <2 x i7>
93+
; CHECK-NEXT: [[R:%.*]] = and <2 x i7> [[T]], <i7 1, i7 1>
94+
; CHECK-NEXT: ret <2 x i7> [[R]]
10595
;
10696
%s = lshr <2 x i16> %x, <i16 6, i16 6>
10797
%t = trunc <2 x i16> %s to <2 x i7>
10898
%r = and <2 x i7> %t, <i7 1, i7 1>
10999
ret <2 x i7> %r
110100
}
111101

112-
; negative test - the shift is too big for the narrow type
113-
114102
define <2 x i7> @trunc_lshr_big_shift(<2 x i16> %x) {
115103
; CHECK-LABEL: @trunc_lshr_big_shift(
116104
; CHECK-NEXT: [[S:%.*]] = lshr <2 x i16> [[X:%.*]], <i16 7, i16 7>
@@ -124,13 +112,11 @@ define <2 x i7> @trunc_lshr_big_shift(<2 x i16> %x) {
124112
ret <2 x i7> %r
125113
}
126114

127-
; High bits could also be set rather than cleared.
128-
129115
define i6 @or_trunc_lshr(i8 %x) {
130116
; CHECK-LABEL: @or_trunc_lshr(
131-
; CHECK-NEXT: [[TMP1:%.*]] = trunc i8 [[X:%.*]] to i6
132-
; CHECK-NEXT: [[TMP2:%.*]] = lshr i6 [[TMP1]], 1
133-
; CHECK-NEXT: [[R:%.*]] = or i6 [[TMP2]], -32
117+
; CHECK-NEXT: [[S:%.*]] = lshr i8 [[X:%.*]], 1
118+
; CHECK-NEXT: [[T:%.*]] = trunc i8 [[S]] to i6
119+
; CHECK-NEXT: [[R:%.*]] = or i6 [[T]], -32
134120
; CHECK-NEXT: ret i6 [[R]]
135121
;
136122
%s = lshr i8 %x, 1
@@ -141,9 +127,9 @@ define i6 @or_trunc_lshr(i8 %x) {
141127

142128
define i6 @or_trunc_lshr_more(i8 %x) {
143129
; CHECK-LABEL: @or_trunc_lshr_more(
144-
; CHECK-NEXT: [[TMP1:%.*]] = trunc i8 [[X:%.*]] to i6
145-
; CHECK-NEXT: [[TMP2:%.*]] = lshr i6 [[TMP1]], 4
146-
; CHECK-NEXT: [[R:%.*]] = or i6 [[TMP2]], -4
130+
; CHECK-NEXT: [[S:%.*]] = lshr i8 [[X:%.*]], 4
131+
; CHECK-NEXT: [[T:%.*]] = trunc i8 [[S]] to i6
132+
; CHECK-NEXT: [[R:%.*]] = or i6 [[T]], -4
147133
; CHECK-NEXT: ret i6 [[R]]
148134
;
149135
%s = lshr i8 %x, 4
@@ -152,8 +138,6 @@ define i6 @or_trunc_lshr_more(i8 %x) {
152138
ret i6 %r
153139
}
154140

155-
; negative test - need all high bits to be undemanded
156-
157141
define i6 @or_trunc_lshr_small_mask(i8 %x) {
158142
; CHECK-LABEL: @or_trunc_lshr_small_mask(
159143
; CHECK-NEXT: [[S:%.*]] = lshr i8 [[X:%.*]], 4

0 commit comments

Comments
 (0)