@@ -1165,9 +1165,8 @@ static Instruction *canonicalizeAbsNabs(SelectInst &Sel, ICmpInst &Cmp,
1165
1165
// /
1166
1166
// / We can't replace %sel with %add unless we strip away the flags.
1167
1167
// / TODO: Wrapping flags could be preserved in some cases with better analysis.
1168
- static Instruction *foldSelectValueEquivalence (SelectInst &Sel, ICmpInst &Cmp,
1169
- const SimplifyQuery &Q,
1170
- InstCombiner &IC) {
1168
+ Instruction *InstCombinerImpl::foldSelectValueEquivalence (SelectInst &Sel,
1169
+ ICmpInst &Cmp) {
1171
1170
if (!Cmp.isEquality ())
1172
1171
return nullptr ;
1173
1172
@@ -1179,18 +1178,20 @@ static Instruction *foldSelectValueEquivalence(SelectInst &Sel, ICmpInst &Cmp,
1179
1178
Swapped = true ;
1180
1179
}
1181
1180
1182
- // In X == Y ? f(X) : Z, try to evaluate f(X) and replace the operand.
1183
- // Take care to avoid replacing X == Y ? X : Z with X == Y ? Y : Z, as that
1184
- // would lead to an infinite replacement cycle.
1181
+ // In X == Y ? f(X) : Z, try to evaluate f(Y) and replace the operand.
1182
+ // Make sure Y cannot be undef though, as we might pick different values for
1183
+ // undef in the icmp and in f(Y). Additionally, take care to avoid replacing
1184
+ // X == Y ? X : Z with X == Y ? Y : Z, as that would lead to an infinite
1185
+ // replacement cycle.
1185
1186
Value *CmpLHS = Cmp.getOperand (0 ), *CmpRHS = Cmp.getOperand (1 );
1186
- if (TrueVal != CmpLHS)
1187
- if (Value *V = SimplifyWithOpReplaced (TrueVal, CmpLHS, CmpRHS, Q ,
1187
+ if (TrueVal != CmpLHS && isGuaranteedNotToBeUndefOrPoison (CmpRHS, &Sel, &DT) )
1188
+ if (Value *V = SimplifyWithOpReplaced (TrueVal, CmpLHS, CmpRHS, SQ ,
1188
1189
/* AllowRefinement */ true ))
1189
- return IC. replaceOperand (Sel, Swapped ? 2 : 1 , V);
1190
- if (TrueVal != CmpRHS)
1191
- if (Value *V = SimplifyWithOpReplaced (TrueVal, CmpRHS, CmpLHS, Q ,
1190
+ return replaceOperand (Sel, Swapped ? 2 : 1 , V);
1191
+ if (TrueVal != CmpRHS && isGuaranteedNotToBeUndefOrPoison (CmpLHS, &Sel, &DT) )
1192
+ if (Value *V = SimplifyWithOpReplaced (TrueVal, CmpRHS, CmpLHS, SQ ,
1192
1193
/* AllowRefinement */ true ))
1193
- return IC. replaceOperand (Sel, Swapped ? 2 : 1 , V);
1194
+ return replaceOperand (Sel, Swapped ? 2 : 1 , V);
1194
1195
1195
1196
auto *FalseInst = dyn_cast<Instruction>(FalseVal);
1196
1197
if (!FalseInst)
@@ -1215,11 +1216,11 @@ static Instruction *foldSelectValueEquivalence(SelectInst &Sel, ICmpInst &Cmp,
1215
1216
// We have an 'EQ' comparison, so the select's false value will propagate.
1216
1217
// Example:
1217
1218
// (X == 42) ? 43 : (X + 1) --> (X == 42) ? (X + 1) : (X + 1) --> X + 1
1218
- if (SimplifyWithOpReplaced (FalseVal, CmpLHS, CmpRHS, Q ,
1219
+ if (SimplifyWithOpReplaced (FalseVal, CmpLHS, CmpRHS, SQ ,
1219
1220
/* AllowRefinement */ false ) == TrueVal ||
1220
- SimplifyWithOpReplaced (FalseVal, CmpRHS, CmpLHS, Q ,
1221
+ SimplifyWithOpReplaced (FalseVal, CmpRHS, CmpLHS, SQ ,
1221
1222
/* AllowRefinement */ false ) == TrueVal) {
1222
- return IC. replaceInstUsesWith (Sel, FalseVal);
1223
+ return replaceInstUsesWith (Sel, FalseVal);
1223
1224
}
1224
1225
1225
1226
// Restore poison-generating flags if the transform did not apply.
@@ -1455,7 +1456,7 @@ tryToReuseConstantFromSelectInComparison(SelectInst &Sel, ICmpInst &Cmp,
1455
1456
// / Visit a SelectInst that has an ICmpInst as its first operand.
1456
1457
Instruction *InstCombinerImpl::foldSelectInstWithICmp (SelectInst &SI,
1457
1458
ICmpInst *ICI) {
1458
- if (Instruction *NewSel = foldSelectValueEquivalence (SI, *ICI, SQ, * this ))
1459
+ if (Instruction *NewSel = foldSelectValueEquivalence (SI, *ICI))
1459
1460
return NewSel;
1460
1461
1461
1462
if (Instruction *NewSel = canonicalizeMinMaxWithConstant (SI, *ICI, *this ))
0 commit comments