@@ -1288,40 +1288,51 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
1288
1288
Swapped = true ;
1289
1289
}
1290
1290
1291
- // In X == Y ? f(X) : Z, try to evaluate f(Y) and replace the operand.
1292
- // Make sure Y cannot be undef though, as we might pick different values for
1293
- // undef in the icmp and in f(Y). Additionally, take care to avoid replacing
1294
- // X == Y ? X : Z with X == Y ? Y : Z, as that would lead to an infinite
1295
- // replacement cycle.
1296
1291
Value *CmpLHS = Cmp.getOperand (0 ), *CmpRHS = Cmp.getOperand (1 );
1297
- if (TrueVal != CmpLHS &&
1298
- isGuaranteedNotToBeUndefOrPoison (CmpRHS, SQ.AC , &Sel, &DT)) {
1299
- if (Value *V = simplifyWithOpReplaced (TrueVal, CmpLHS, CmpRHS, SQ,
1300
- /* AllowRefinement */ true ))
1301
- // Require either the replacement or the simplification result to be a
1302
- // constant to avoid infinite loops.
1303
- // FIXME: Make this check more precise.
1304
- if (isa<Constant>(CmpRHS) || isa<Constant>(V))
1292
+ auto ReplaceOldOpWithNewOp = [&](Value *OldOp,
1293
+ Value *NewOp) -> Instruction * {
1294
+ // In X == Y ? f(X) : Z, try to evaluate f(Y) and replace the operand.
1295
+ // Take care to avoid replacing X == Y ? X : Z with X == Y ? Y : Z, as that
1296
+ // would lead to an infinite replacement cycle.
1297
+ // If we will be able to evaluate f(Y) to a constant, we can allow undef,
1298
+ // otherwise Y cannot be undef as we might pick different values for undef
1299
+ // in the icmp and in f(Y).
1300
+ if (TrueVal == OldOp)
1301
+ return nullptr ;
1302
+
1303
+ if (Value *V = simplifyWithOpReplaced (TrueVal, OldOp, NewOp, SQ,
1304
+ /* AllowRefinement= */ true )) {
1305
+ // Need some guarantees about the new simplified op to ensure we don't inf
1306
+ // loop.
1307
+ // If we simplify to a constant, replace.
1308
+ if (match (V, m_ImmConstant ()))
1305
1309
return replaceOperand (Sel, Swapped ? 2 : 1 , V);
1306
1310
1311
+ // If NewOp is a constant and OldOp is not replace iff NewOp doesn't
1312
+ // contain and undef/poison elements.
1313
+ if (match (NewOp, m_ImmConstant ()) &&
1314
+ isGuaranteedNotToBeUndefOrPoison (NewOp, SQ.AC , &Sel, &DT))
1315
+ return replaceOperand (Sel, Swapped ? 2 : 1 , V);
1316
+ }
1317
+
1307
1318
// Even if TrueVal does not simplify, we can directly replace a use of
1308
1319
// CmpLHS with CmpRHS, as long as the instruction is not used anywhere
1309
1320
// else and is safe to speculatively execute (we may end up executing it
1310
1321
// with different operands, which should not cause side-effects or trigger
1311
1322
// undefined behavior). Only do this if CmpRHS is a constant, as
1312
1323
// profitability is not clear for other cases.
1313
1324
// FIXME: Support vectors.
1314
- if (match (CmpRHS, m_ImmConstant ()) && ! match (CmpLHS , m_ImmConstant ()) &&
1315
- !Cmp.getType ()->isVectorTy ())
1316
- if (replaceInInstruction (TrueVal, CmpLHS, CmpRHS ))
1325
+ if (OldOp == CmpLHS && match (NewOp , m_ImmConstant ()) &&
1326
+ !match (OldOp, m_ImmConstant ()) && ! Cmp.getType ()->isVectorTy ())
1327
+ if (replaceInInstruction (TrueVal, OldOp, NewOp ))
1317
1328
return &Sel;
1318
- }
1319
- if (TrueVal != CmpRHS &&
1320
- isGuaranteedNotToBeUndefOrPoison (CmpLHS, SQ. AC , &Sel, &DT))
1321
- if (Value *V = simplifyWithOpReplaced (TrueVal, CmpRHS, CmpLHS, SQ,
1322
- /* AllowRefinement */ true ))
1323
- if (isa<Constant>(CmpLHS) || isa<Constant>(V ))
1324
- return replaceOperand (Sel, Swapped ? 2 : 1 , V) ;
1329
+ return nullptr ;
1330
+ };
1331
+
1332
+ if (Instruction *R = ReplaceOldOpWithNewOp ( CmpLHS, CmpRHS))
1333
+ return R;
1334
+ if (Instruction *R = ReplaceOldOpWithNewOp (CmpRHS, CmpLHS ))
1335
+ return R ;
1325
1336
1326
1337
auto *FalseInst = dyn_cast<Instruction>(FalseVal);
1327
1338
if (!FalseInst)
0 commit comments