Skip to content

Commit f5bcc06

Browse files
committed
match correct mask only
1 parent 9542631 commit f5bcc06

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,37 +1350,38 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
13501350
};
13511351

13521352
// Special case for reconstructing across a select:
1353-
// (Cond ? V1 : (X & Mask)) op
1353+
// (Cond ? V1 : (X & Mask)) |
13541354
// zext (Cond ? V2 : trunc X)
1355-
// -> (Cond ? (V1 op zext V2) : ((X & Mask) op zext trunc X))
1355+
// -> (Cond ? (V1 | zext V2) : X)
13561356
auto foldReconstruction = [&](Value *V1, Value *Masked,
13571357
Value *ZExtSel) -> Value * {
13581358
if (Opcode != Instruction::Or)
13591359
return nullptr;
13601360

13611361
Value *X;
1362-
if (!match(Masked, m_OneUse(m_And(m_Value(X), m_Constant()))))
1362+
const APInt *C;
1363+
if (!match(Masked, m_OneUse(m_And(m_Value(X), m_APInt(C)))))
13631364
return nullptr;
13641365

13651366
Value *V2, *Trunc;
13661367
if (!match(ZExtSel, m_ZExt(m_OneUse(m_Select(m_Specific(Cond), m_Value(V2),
13671368
m_Value(Trunc))))))
13681369
return nullptr;
13691370

1371+
if (*C != APInt::getBitsSetFrom(X->getType()->getScalarSizeInBits(),
1372+
Trunc->getType()->getScalarSizeInBits())) {
1373+
return nullptr;
1374+
}
1375+
13701376
if (!match(Trunc, m_Trunc(m_Specific(X))))
13711377
return nullptr;
13721378

13731379
Value *ZExtTrue = Builder.CreateZExt(V2, V1->getType());
1374-
Value *True;
1375-
if (!(True = simplifyBinOp(Opcode, V1, ZExtTrue, FMF, Q)))
1376-
True = Builder.CreateOr(V1, ZExtTrue);
1377-
1378-
Value *ZExtFalse = Builder.CreateZExt(Trunc, V1->getType());
1379-
Value *False;
1380-
if (!(False = simplifyBinOp(Opcode, Masked, ZExtFalse, FMF, Q)))
1381-
False = Builder.CreateOr(Masked, ZExtFalse);
1380+
Value *True = simplifyBinOp(Opcode, V1, ZExtTrue, FMF, Q);
1381+
if (!True)
1382+
return nullptr;
13821383

1383-
return Builder.CreateSelect(Cond, True, False, I.getName());
1384+
return Builder.CreateSelect(Cond, True, X, I.getName());
13841385
};
13851386

13861387
if (LHSIsSelect && RHSIsSelect && A == D) {

llvm/test/Transforms/InstCombine/select-reconstruction.ll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@ define i40 @select_reconstruction_any_cmp_val(i40 %arg0, i8 %arg1) {
3737
ret i40 %recomb
3838
}
3939

40+
; negative test
4041
define i40 @select_reconstruction_257_mask(i40 %arg0) {
4142
; CHECK-LABEL: define i40 @select_reconstruction_257_mask(
4243
; CHECK-SAME: i40 [[ARG0:%.*]]) {
4344
; CHECK-NEXT: [[TMP1:%.*]] = trunc i40 [[ARG0]] to i8
4445
; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i8 [[TMP1]], 2
4546
; CHECK-NEXT: [[TMP3:%.*]] = and i40 [[ARG0]], -257
47+
; CHECK-NEXT: [[SELECT_LOW:%.*]] = select i1 [[TMP2]], i8 0, i8 [[TMP1]]
4648
; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP2]], i40 0, i40 [[TMP3]]
47-
; CHECK-NEXT: ret i40 [[TMP4]]
49+
; CHECK-NEXT: [[ZEXT_LOW:%.*]] = zext i8 [[SELECT_LOW]] to i40
50+
; CHECK-NEXT: [[RECOMB:%.*]] = or disjoint i40 [[TMP4]], [[ZEXT_LOW]]
51+
; CHECK-NEXT: ret i40 [[RECOMB]]
4852
;
4953
%low = trunc i40 %arg0 to i8
5054
%is_low_two = icmp eq i8 %low, 2

0 commit comments

Comments
 (0)