Skip to content

Commit 568db84

Browse files
committed
[InstCombine] Refactor canonicalizeSPF to support decomposed select. NFC.
See also #76621
1 parent c7aa985 commit 568db84

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,14 +1171,15 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
11711171
return nullptr;
11721172
}
11731173

1174-
static Instruction *canonicalizeSPF(SelectInst &Sel, ICmpInst &Cmp,
1175-
InstCombinerImpl &IC) {
1174+
static Value *canonicalizeSPF(ICmpInst &Cmp, Value *TrueVal, Value *FalseVal,
1175+
InstCombinerImpl &IC) {
11761176
Value *LHS, *RHS;
11771177
// TODO: What to do with pointer min/max patterns?
1178-
if (!Sel.getType()->isIntOrIntVectorTy())
1178+
if (!TrueVal->getType()->isIntOrIntVectorTy())
11791179
return nullptr;
11801180

1181-
SelectPatternFlavor SPF = matchSelectPattern(&Sel, LHS, RHS).Flavor;
1181+
SelectPatternFlavor SPF =
1182+
matchDecomposedSelectPattern(&Cmp, TrueVal, FalseVal, LHS, RHS).Flavor;
11821183
if (SPF == SelectPatternFlavor::SPF_ABS ||
11831184
SPF == SelectPatternFlavor::SPF_NABS) {
11841185
if (!Cmp.hasOneUse() && !RHS->hasOneUse())
@@ -1188,13 +1189,13 @@ static Instruction *canonicalizeSPF(SelectInst &Sel, ICmpInst &Cmp,
11881189
bool IntMinIsPoison = SPF == SelectPatternFlavor::SPF_ABS &&
11891190
match(RHS, m_NSWNeg(m_Specific(LHS)));
11901191
Constant *IntMinIsPoisonC =
1191-
ConstantInt::get(Type::getInt1Ty(Sel.getContext()), IntMinIsPoison);
1192+
ConstantInt::get(Type::getInt1Ty(Cmp.getContext()), IntMinIsPoison);
11921193
Instruction *Abs =
11931194
IC.Builder.CreateBinaryIntrinsic(Intrinsic::abs, LHS, IntMinIsPoisonC);
11941195

11951196
if (SPF == SelectPatternFlavor::SPF_NABS)
1196-
return BinaryOperator::CreateNeg(Abs); // Always without NSW flag!
1197-
return IC.replaceInstUsesWith(Sel, Abs);
1197+
return IC.Builder.CreateNeg(Abs); // Always without NSW flag!
1198+
return Abs;
11981199
}
11991200

12001201
if (SelectPatternResult::isMinOrMax(SPF)) {
@@ -1215,8 +1216,7 @@ static Instruction *canonicalizeSPF(SelectInst &Sel, ICmpInst &Cmp,
12151216
default:
12161217
llvm_unreachable("Unexpected SPF");
12171218
}
1218-
return IC.replaceInstUsesWith(
1219-
Sel, IC.Builder.CreateBinaryIntrinsic(IntrinsicID, LHS, RHS));
1219+
return IC.Builder.CreateBinaryIntrinsic(IntrinsicID, LHS, RHS);
12201220
}
12211221

12221222
return nullptr;
@@ -1677,8 +1677,9 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
16771677
if (Instruction *NewSel = foldSelectValueEquivalence(SI, *ICI))
16781678
return NewSel;
16791679

1680-
if (Instruction *NewSPF = canonicalizeSPF(SI, *ICI, *this))
1681-
return NewSPF;
1680+
if (Value *V =
1681+
canonicalizeSPF(*ICI, SI.getTrueValue(), SI.getFalseValue(), *this))
1682+
return replaceInstUsesWith(SI, V);
16821683

16831684
if (Value *V = foldSelectInstWithICmpConst(SI, ICI, Builder))
16841685
return replaceInstUsesWith(SI, V);

0 commit comments

Comments
 (0)