@@ -1262,27 +1262,23 @@ bool InstCombinerImpl::replaceInInstruction(Value *V, Value *Old, Value *New,
1262
1262
// /
1263
1263
// / We can't replace %sel with %add unless we strip away the flags.
1264
1264
// / TODO: Wrapping flags could be preserved in some cases with better analysis.
1265
- Instruction *InstCombinerImpl::foldSelectValueEquivalence (SelectInst &Sel,
1266
- ICmpInst &Cmp ) {
1267
- if (!Cmp. isEquality ())
1265
+ Instruction *InstCombinerImpl::foldSelectValueEquivalence (
1266
+ SelectInst &Sel, ICmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS ) {
1267
+ if (!ICmpInst:: isEquality (Pred ))
1268
1268
return nullptr ;
1269
1269
1270
1270
// Canonicalize the pattern to ICMP_EQ by swapping the select operands.
1271
1271
Value *TrueVal = Sel.getTrueValue (), *FalseVal = Sel.getFalseValue ();
1272
1272
bool Swapped = false ;
1273
- if (Cmp. getPredicate () == ICmpInst::ICMP_NE) {
1273
+ if (Pred == ICmpInst::ICMP_NE) {
1274
1274
std::swap (TrueVal, FalseVal);
1275
1275
Swapped = true ;
1276
1276
}
1277
1277
1278
1278
// In X == Y ? f(X) : Z, try to evaluate f(Y) and replace the operand.
1279
1279
// Make sure Y cannot be undef though, as we might pick different values for
1280
- // undef in the icmp and in f(Y). Additionally, take care to avoid replacing
1281
- // X == Y ? X : Z with X == Y ? Y : Z, as that would lead to an infinite
1282
- // replacement cycle.
1283
- Value *CmpLHS = Cmp.getOperand (0 ), *CmpRHS = Cmp.getOperand (1 );
1284
- if (TrueVal != CmpLHS &&
1285
- isGuaranteedNotToBeUndefOrPoison (CmpRHS, SQ.AC , &Sel, &DT)) {
1280
+ // undef in the icmp and in f(Y).
1281
+ if (isGuaranteedNotToBeUndefOrPoison (CmpRHS, SQ.AC , &Sel, &DT)) {
1286
1282
if (Value *V = simplifyWithOpReplaced (TrueVal, CmpLHS, CmpRHS, SQ,
1287
1283
/* AllowRefinement */ true ))
1288
1284
// Require either the replacement or the simplification result to be a
@@ -1299,7 +1295,7 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
1299
1295
// profitability is not clear for other cases.
1300
1296
// FIXME: Support vectors.
1301
1297
if (match (CmpRHS, m_ImmConstant ()) && !match (CmpLHS, m_ImmConstant ()) &&
1302
- !Cmp. getType ()->isVectorTy ())
1298
+ !CmpLHS-> getType ()->isVectorTy ())
1303
1299
if (replaceInInstruction (TrueVal, CmpLHS, CmpRHS))
1304
1300
return &Sel;
1305
1301
}
@@ -1680,7 +1676,8 @@ static Value *foldSelectInstWithICmpConst(SelectInst &SI, ICmpInst *ICI,
1680
1676
// / Visit a SelectInst that has an ICmpInst as its first operand.
1681
1677
Instruction *InstCombinerImpl::foldSelectInstWithICmp (SelectInst &SI,
1682
1678
ICmpInst *ICI) {
1683
- if (Instruction *NewSel = foldSelectValueEquivalence (SI, *ICI))
1679
+ if (Instruction *NewSel = foldSelectValueEquivalence (
1680
+ SI, ICI->getPredicate (), ICI->getOperand (0 ), ICI->getOperand (1 )))
1684
1681
return NewSel;
1685
1682
1686
1683
if (Value *V =
@@ -3376,21 +3373,15 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
3376
3373
if (Instruction *I = canonicalizeScalarSelectOfVecs (SI, *this ))
3377
3374
return I;
3378
3375
3379
- // If the type of select is not an integer type or if the condition and
3380
- // the selection type are not both scalar nor both vector types, there is no
3381
- // point in attempting to match these patterns.
3382
3376
Type *CondType = CondVal->getType ();
3383
- if (!isa<Constant>(CondVal) && SelType->isIntOrIntVectorTy () &&
3384
- CondType->isVectorTy () == SelType->isVectorTy ()) {
3385
- if (Value *S = simplifyWithOpReplaced (TrueVal, CondVal,
3386
- ConstantInt::getTrue (CondType), SQ,
3387
- /* AllowRefinement */ true ))
3388
- return replaceOperand (SI, 1 , S);
3377
+ if (!isa<Constant>(CondVal)) {
3378
+ if (Instruction *I = foldSelectValueEquivalence (
3379
+ SI, ICmpInst::ICMP_EQ, CondVal, ConstantInt::getTrue (CondType)))
3380
+ return I;
3389
3381
3390
- if (Value *S = simplifyWithOpReplaced (FalseVal, CondVal,
3391
- ConstantInt::getFalse (CondType), SQ,
3392
- /* AllowRefinement */ true ))
3393
- return replaceOperand (SI, 2 , S);
3382
+ if (Instruction *I = foldSelectValueEquivalence (
3383
+ SI, ICmpInst::ICMP_NE, CondVal, ConstantInt::getFalse (CondType)))
3384
+ return I;
3394
3385
}
3395
3386
3396
3387
if (Instruction *R = foldSelectOfBools (SI))
0 commit comments