Skip to content

Commit 750b54f

Browse files
committed
[InstCombine] Improve coverage of foldSelectValueEquivalence for non-constants
There are a few more cases we can easily cover. - If f(Y) simplifies to Y - If replace X with Y makes X one-use (and make Y non one-use). These all require that Y is not undef or poison
1 parent c727c1b commit 750b54f

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,8 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
13001300
if (TrueVal == OldOp)
13011301
return nullptr;
13021302

1303+
bool NewOpNeverUndef = false;
1304+
13031305
if (Value *V = simplifyWithOpReplaced(TrueVal, OldOp, NewOp, SQ,
13041306
/* AllowRefinement=*/true)) {
13051307
// Need some guarantees about the new simplified op to ensure we don't inf
@@ -1312,11 +1314,52 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
13121314
return nullptr;
13131315
}
13141316

1315-
// If NewOp is a constant and OldOp is not replace iff NewOp doesn't
1316-
// contain and undef elements.
1317-
if (match(NewOp, m_ImmConstant()) &&
1318-
isGuaranteedNotToBeUndef(NewOp, SQ.AC, &Sel, &DT))
1317+
// We can't do any further replacement if NewOp may be undef/poison.
1318+
if (!isGuaranteedNotToBeUndefOrPoison(NewOp, SQ.AC, &Sel, &DT))
1319+
return nullptr;
1320+
1321+
// If NewOp is a constant, replace.
1322+
if (match(NewOp, m_ImmConstant()))
1323+
return replaceOperand(Sel, Swapped ? 2 : 1, V);
1324+
1325+
// If we simplified the TrueArm -> NewOp then replace.
1326+
// This handles things like `select`/`min`/`max`/`or`/`and`/etc...
1327+
if (NewOp == V)
13191328
return replaceOperand(Sel, Swapped ? 2 : 1, V);
1329+
1330+
NewOpNeverUndef = true;
1331+
}
1332+
1333+
// If we can't simplify, but we will either:
1334+
// 1) Create a new binop where both ops are NewOp i.e (add x, y) is "worse"
1335+
// than (add y, y) in this case, wait until the second call so we don't
1336+
// miss a one-use simplification.
1337+
// 2) Create a new one-use instruction.
1338+
// proceed.
1339+
if (TrueVal->hasOneUse() && isa<Instruction>(TrueVal)) {
1340+
bool BinOpMatch =
1341+
match(TrueVal, m_c_BinOp(m_Specific(OldOp), m_Specific(NewOp)));
1342+
bool NewOneUse = isa<Instruction>(OldOp) && OldOp->hasNUses(2) &&
1343+
(!isa<Instruction>(NewOp) || !NewOp->hasOneUse());
1344+
if (BinOpMatch || NewOneUse) {
1345+
bool Replaced = false;
1346+
auto *TrueIns = cast<Instruction>(TrueVal);
1347+
for (unsigned OpIdx = 0; OpIdx < TrueIns->getNumOperands(); ++OpIdx) {
1348+
if (TrueIns->getOperand(OpIdx) != OldOp)
1349+
continue;
1350+
// Need to ensure NewOp is noundef (same reason as above). Wait
1351+
// until the last moment to do this check as it can be relatively
1352+
// expensive.
1353+
if (!NewOpNeverUndef &&
1354+
!isGuaranteedNotToBeUndefOrPoison(NewOp, SQ.AC, &Sel, &DT))
1355+
break;
1356+
NewOpNeverUndef = true;
1357+
TrueIns->setOperand(OpIdx, NewOp);
1358+
Replaced = true;
1359+
}
1360+
if (Replaced)
1361+
return replaceOperand(Sel, Swapped ? 2 : 1, TrueIns);
1362+
}
13201363
}
13211364

13221365
// Even if TrueVal does not simplify, we can directly replace a use of

llvm/test/Transforms/InstCombine/select-cmp-eq-op-fold.ll

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ declare void @use.i8(i8)
66
define i8 @replace_with_y_noundef(i8 %x, i8 noundef %y, i8 %z) {
77
; CHECK-LABEL: @replace_with_y_noundef(
88
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
9-
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X]], [[Y]]
10-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[AND]], i8 [[Z:%.*]]
9+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[Y]], i8 [[Z:%.*]]
1110
; CHECK-NEXT: ret i8 [[SEL]]
1211
;
1312
%cmp = icmp eq i8 %x, %y
@@ -20,8 +19,7 @@ define i8 @replace_with_x_noundef(i8 noundef %x, i8 %y, i8 %z) {
2019
; CHECK-LABEL: @replace_with_x_noundef(
2120
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X:%.*]], [[Y:%.*]]
2221
; CHECK-NEXT: call void @use.i1(i1 [[CMP]])
23-
; CHECK-NEXT: [[AND:%.*]] = or i8 [[X]], [[Y]]
24-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[Z:%.*]], i8 [[AND]]
22+
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[Z:%.*]], i8 [[X]]
2523
; CHECK-NEXT: ret i8 [[SEL]]
2624
;
2725
%cmp = icmp ne i8 %x, %y
@@ -50,7 +48,7 @@ define i8 @replace_with_y_for_new_oneuse(i8 noundef %xx, i8 noundef %y, i8 %z) {
5048
; CHECK-LABEL: @replace_with_y_for_new_oneuse(
5149
; CHECK-NEXT: [[X:%.*]] = mul i8 [[XX:%.*]], 13
5250
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X]], [[Y:%.*]]
53-
; CHECK-NEXT: [[ADD:%.*]] = add nuw i8 [[X]], [[Y]]
51+
; CHECK-NEXT: [[ADD:%.*]] = shl nuw i8 [[Y]], 1
5452
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[ADD]], i8 [[Z:%.*]]
5553
; CHECK-NEXT: ret i8 [[SEL]]
5654
;
@@ -65,7 +63,7 @@ define i8 @replace_with_y_for_new_oneuse2(i8 %xx, i8 noundef %y, i8 %z, i8 %q) {
6563
; CHECK-LABEL: @replace_with_y_for_new_oneuse2(
6664
; CHECK-NEXT: [[X:%.*]] = mul i8 [[XX:%.*]], 13
6765
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X]], [[Y:%.*]]
68-
; CHECK-NEXT: [[ADD:%.*]] = add nuw i8 [[X]], [[Q:%.*]]
66+
; CHECK-NEXT: [[ADD:%.*]] = add nuw i8 [[Y]], [[Q:%.*]]
6967
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[ADD]], i8 [[Z:%.*]]
7068
; CHECK-NEXT: ret i8 [[SEL]]
7169
;
@@ -81,7 +79,7 @@ define i8 @replace_with_x_for_new_oneuse(i8 noundef %xx, i8 noundef %yy, i8 %z,
8179
; CHECK-NEXT: [[X:%.*]] = mul i8 [[XX:%.*]], 13
8280
; CHECK-NEXT: [[Y:%.*]] = add i8 [[YY:%.*]], [[W:%.*]]
8381
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X]], [[Y]]
84-
; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X]], [[Y]]
82+
; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X]], [[X]]
8583
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[MUL]], i8 [[Z:%.*]]
8684
; CHECK-NEXT: ret i8 [[SEL]]
8785
;
@@ -115,7 +113,7 @@ define i8 @replace_with_x_for_simple_binop(i8 noundef %xx, i8 %yy, i8 %z, i8 %w)
115113
; CHECK-NEXT: [[X:%.*]] = mul i8 [[XX:%.*]], 13
116114
; CHECK-NEXT: [[Y:%.*]] = add i8 [[YY:%.*]], [[W:%.*]]
117115
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X]], [[Y]]
118-
; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X]], [[Y]]
116+
; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[X]], [[X]]
119117
; CHECK-NEXT: call void @use.i8(i8 [[Y]])
120118
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[MUL]], i8 [[Z:%.*]]
121119
; CHECK-NEXT: ret i8 [[SEL]]
@@ -147,7 +145,7 @@ define i8 @replace_with_none_for_new_oneuse_fail_maybe_undef(i8 %xx, i8 %y, i8 %
147145
define i8 @replace_with_y_for_simple_binop(i8 %x, i8 noundef %y, i8 %z) {
148146
; CHECK-LABEL: @replace_with_y_for_simple_binop(
149147
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], [[Y:%.*]]
150-
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[X]], [[Y]]
148+
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i8 [[Y]], [[Y]]
151149
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i8 [[MUL]], i8 [[Z:%.*]]
152150
; CHECK-NEXT: ret i8 [[SEL]]
153151
;

0 commit comments

Comments
 (0)