[llvm] c1bb4a8 - [SCEVExpander] Deduplicate min/max expansion code (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed May 11 03:13:43 PDT 2022
Author: Nikita Popov
Date: 2022-05-11T12:11:11+02:00
New Revision: c1bb4a881efe7d15083fd9a453c82d92ad663878
URL: https://github.com/llvm/llvm-project/commit/c1bb4a881efe7d15083fd9a453c82d92ad663878
DIFF: https://github.com/llvm/llvm-project/commit/c1bb4a881efe7d15083fd9a453c82d92ad663878.diff
LOG: [SCEVExpander] Deduplicate min/max expansion code (NFC)
Added:
Modified:
llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
index d9a02e5940259..0161fa0c813a1 100644
--- a/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
+++ b/llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h
@@ -448,13 +448,8 @@ class SCEVExpander : public SCEVVisitor<SCEVExpander, Value *> {
/// Determine the most "relevant" loop for the given SCEV.
const Loop *getRelevantLoop(const SCEV *);
- Value *expandSMaxExpr(const SCEVNAryExpr *S);
-
- Value *expandUMaxExpr(const SCEVNAryExpr *S);
-
- Value *expandSMinExpr(const SCEVNAryExpr *S);
-
- Value *expandUMinExpr(const SCEVNAryExpr *S);
+ Value *expandMinMaxExpr(const SCEVNAryExpr *S, Intrinsic::ID IntrinID,
+ Twine Name);
Value *visitConstant(const SCEVConstant *S) { return S->getValue(); }
diff --git a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
index e6bfe6f28810c..208bd846405c1 100644
--- a/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ b/llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -1670,72 +1670,20 @@ Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
return Builder.CreateSExt(V, Ty);
}
-Value *SCEVExpander::expandSMaxExpr(const SCEVNAryExpr *S) {
- Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
- Type *Ty = LHS->getType();
- for (int i = S->getNumOperands()-2; i >= 0; --i) {
- Value *RHS = expandCodeForImpl(S->getOperand(i), Ty, false);
- Value *Sel;
- if (Ty->isIntegerTy())
- Sel = Builder.CreateIntrinsic(Intrinsic::smax, {Ty}, {LHS, RHS},
- /*FMFSource=*/nullptr, "smax");
- else {
- Value *ICmp = Builder.CreateICmpSGT(LHS, RHS);
- Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax");
- }
- LHS = Sel;
- }
- return LHS;
-}
-
-Value *SCEVExpander::expandUMaxExpr(const SCEVNAryExpr *S) {
- Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
- Type *Ty = LHS->getType();
- for (int i = S->getNumOperands()-2; i >= 0; --i) {
- Value *RHS = expandCodeForImpl(S->getOperand(i), Ty, false);
- Value *Sel;
- if (Ty->isIntegerTy())
- Sel = Builder.CreateIntrinsic(Intrinsic::umax, {Ty}, {LHS, RHS},
- /*FMFSource=*/nullptr, "umax");
- else {
- Value *ICmp = Builder.CreateICmpUGT(LHS, RHS);
- Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax");
- }
- LHS = Sel;
- }
- return LHS;
-}
-
-Value *SCEVExpander::expandSMinExpr(const SCEVNAryExpr *S) {
- Value *LHS = expand(S->getOperand(S->getNumOperands() - 1));
- Type *Ty = LHS->getType();
- for (int i = S->getNumOperands() - 2; i >= 0; --i) {
- Value *RHS = expandCodeForImpl(S->getOperand(i), Ty, false);
- Value *Sel;
- if (Ty->isIntegerTy())
- Sel = Builder.CreateIntrinsic(Intrinsic::smin, {Ty}, {LHS, RHS},
- /*FMFSource=*/nullptr, "smin");
- else {
- Value *ICmp = Builder.CreateICmpSLT(LHS, RHS);
- Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smin");
- }
- LHS = Sel;
- }
- return LHS;
-}
-
-Value *SCEVExpander::expandUMinExpr(const SCEVNAryExpr *S) {
+Value *SCEVExpander::expandMinMaxExpr(const SCEVNAryExpr *S,
+ Intrinsic::ID IntrinID, Twine Name) {
Value *LHS = expand(S->getOperand(S->getNumOperands() - 1));
Type *Ty = LHS->getType();
for (int i = S->getNumOperands() - 2; i >= 0; --i) {
Value *RHS = expandCodeForImpl(S->getOperand(i), Ty, false);
Value *Sel;
if (Ty->isIntegerTy())
- Sel = Builder.CreateIntrinsic(Intrinsic::umin, {Ty}, {LHS, RHS},
- /*FMFSource=*/nullptr, "umin");
+ Sel = Builder.CreateIntrinsic(IntrinID, {Ty}, {LHS, RHS},
+ /*FMFSource=*/nullptr, Name);
else {
- Value *ICmp = Builder.CreateICmpULT(LHS, RHS);
- Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umin");
+ Value *ICmp =
+ Builder.CreateICmp(MinMaxIntrinsic::getPredicate(IntrinID), LHS, RHS);
+ Sel = Builder.CreateSelect(ICmp, LHS, RHS, Name);
}
LHS = Sel;
}
@@ -1743,19 +1691,19 @@ Value *SCEVExpander::expandUMinExpr(const SCEVNAryExpr *S) {
}
Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
- return expandSMaxExpr(S);
+ return expandMinMaxExpr(S, Intrinsic::smax, "smax");
}
Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
- return expandUMaxExpr(S);
+ return expandMinMaxExpr(S, Intrinsic::umax, "umax");
}
Value *SCEVExpander::visitSMinExpr(const SCEVSMinExpr *S) {
- return expandSMinExpr(S);
+ return expandMinMaxExpr(S, Intrinsic::smin, "smin");
}
Value *SCEVExpander::visitUMinExpr(const SCEVUMinExpr *S) {
- return expandUMinExpr(S);
+ return expandMinMaxExpr(S, Intrinsic::umin, "umin");
}
Value *SCEVExpander::visitSequentialUMinExpr(const SCEVSequentialUMinExpr *S) {
@@ -1772,7 +1720,7 @@ Value *SCEVExpander::visitSequentialUMinExpr(const SCEVSequentialUMinExpr *S) {
Value *AnyOpIsZero = Builder.CreateLogicalOr(OpIsZero);
- Value *NaiveUMin = expandUMinExpr(S);
+ Value *NaiveUMin = expandMinMaxExpr(S, Intrinsic::umin, "umin");
return Builder.CreateSelect(AnyOpIsZero, SaturationPoint, NaiveUMin);
}
More information about the llvm-commits
mailing list