[llvm] 66a16b2 - [IRBuilder] Migrate div/rem to use fold infrastructure
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Jun 29 04:17:16 PDT 2022
Author: Nikita Popov
Date: 2022-06-29T13:17:02+02:00
New Revision: 66a16b2848b434e937d658f15fc1849650f52185
URL: https://github.com/llvm/llvm-project/commit/66a16b2848b434e937d658f15fc1849650f52185
DIFF: https://github.com/llvm/llvm-project/commit/66a16b2848b434e937d658f15fc1849650f52185.diff
LOG: [IRBuilder] Migrate div/rem to use fold infrastructure
Migrate udiv, sdiv, urem, and srem to use the FoldXYZ rather than
the CreateXYZ infrastructure.
Added:
Modified:
llvm/include/llvm/Analysis/InstSimplifyFolder.h
llvm/include/llvm/Analysis/TargetFolder.h
llvm/include/llvm/IR/ConstantFolder.h
llvm/include/llvm/IR/IRBuilder.h
llvm/include/llvm/IR/IRBuilderFolder.h
llvm/include/llvm/IR/NoFolder.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/Analysis/InstSimplifyFolder.h b/llvm/include/llvm/Analysis/InstSimplifyFolder.h
index cec38299245b..a67424f60536 100644
--- a/llvm/include/llvm/Analysis/InstSimplifyFolder.h
+++ b/llvm/include/llvm/Analysis/InstSimplifyFolder.h
@@ -59,6 +59,22 @@ class InstSimplifyFolder final : public IRBuilderFolder {
return simplifyOrInst(LHS, RHS, SQ);
}
+ Value *FoldUDiv(Value *LHS, Value *RHS, bool IsExact) const override {
+ return simplifyUDivInst(LHS, RHS, SQ);
+ }
+
+ Value *FoldSDiv(Value *LHS, Value *RHS, bool IsExact) const override {
+ return simplifySDivInst(LHS, RHS, SQ);
+ }
+
+ Value *FoldURem(Value *LHS, Value *RHS) const override {
+ return simplifyURemInst(LHS, RHS, SQ);
+ }
+
+ Value *FoldSRem(Value *LHS, Value *RHS) const override {
+ return simplifySRemInst(LHS, RHS, SQ);
+ }
+
Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
return simplifyICmpInst(P, LHS, RHS, SQ);
}
@@ -120,23 +136,9 @@ class InstSimplifyFolder final : public IRBuilderFolder {
Value *CreateFMul(Constant *LHS, Constant *RHS) const override {
return ConstFolder.CreateFMul(LHS, RHS);
}
- Value *CreateUDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const override {
- return ConstFolder.CreateUDiv(LHS, RHS, isExact);
- }
- Value *CreateSDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const override {
- return ConstFolder.CreateSDiv(LHS, RHS, isExact);
- }
Value *CreateFDiv(Constant *LHS, Constant *RHS) const override {
return ConstFolder.CreateFDiv(LHS, RHS);
}
- Value *CreateURem(Constant *LHS, Constant *RHS) const override {
- return ConstFolder.CreateURem(LHS, RHS);
- }
- Value *CreateSRem(Constant *LHS, Constant *RHS) const override {
- return ConstFolder.CreateSRem(LHS, RHS);
- }
Value *CreateFRem(Constant *LHS, Constant *RHS) const override {
return ConstFolder.CreateFRem(LHS, RHS);
}
diff --git a/llvm/include/llvm/Analysis/TargetFolder.h b/llvm/include/llvm/Analysis/TargetFolder.h
index 1ba8c6b633d7..1187e9cfd02a 100644
--- a/llvm/include/llvm/Analysis/TargetFolder.h
+++ b/llvm/include/llvm/Analysis/TargetFolder.h
@@ -74,6 +74,38 @@ class TargetFolder final : public IRBuilderFolder {
return nullptr;
}
+ Value *FoldUDiv(Value *LHS, Value *RHS, bool IsExact) const override {
+ auto *LC = dyn_cast<Constant>(LHS);
+ auto *RC = dyn_cast<Constant>(RHS);
+ if (LC && RC)
+ return Fold(ConstantExpr::getUDiv(LC, RC, IsExact));
+ return nullptr;
+ }
+
+ Value *FoldSDiv(Value *LHS, Value *RHS, bool IsExact) const override {
+ auto *LC = dyn_cast<Constant>(LHS);
+ auto *RC = dyn_cast<Constant>(RHS);
+ if (LC && RC)
+ return Fold(ConstantExpr::getSDiv(LC, RC, IsExact));
+ return nullptr;
+ }
+
+ Value *FoldURem(Value *LHS, Value *RHS) const override {
+ auto *LC = dyn_cast<Constant>(LHS);
+ auto *RC = dyn_cast<Constant>(RHS);
+ if (LC && RC)
+ return Fold(ConstantExpr::getURem(LC, RC));
+ return nullptr;
+ }
+
+ Value *FoldSRem(Value *LHS, Value *RHS) const override {
+ auto *LC = dyn_cast<Constant>(LHS);
+ auto *RC = dyn_cast<Constant>(RHS);
+ if (LC && RC)
+ return Fold(ConstantExpr::getSRem(LC, RC));
+ return nullptr;
+ }
+
Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
auto *LC = dyn_cast<Constant>(LHS);
auto *RC = dyn_cast<Constant>(RHS);
@@ -170,23 +202,9 @@ class TargetFolder final : public IRBuilderFolder {
Constant *CreateFMul(Constant *LHS, Constant *RHS) const override {
return Fold(ConstantExpr::getFMul(LHS, RHS));
}
- Constant *CreateUDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const override {
- return Fold(ConstantExpr::getUDiv(LHS, RHS, isExact));
- }
- Constant *CreateSDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const override {
- return Fold(ConstantExpr::getSDiv(LHS, RHS, isExact));
- }
Constant *CreateFDiv(Constant *LHS, Constant *RHS) const override {
return Fold(ConstantExpr::getFDiv(LHS, RHS));
}
- Constant *CreateURem(Constant *LHS, Constant *RHS) const override {
- return Fold(ConstantExpr::getURem(LHS, RHS));
- }
- Constant *CreateSRem(Constant *LHS, Constant *RHS) const override {
- return Fold(ConstantExpr::getSRem(LHS, RHS));
- }
Constant *CreateFRem(Constant *LHS, Constant *RHS) const override {
return Fold(ConstantExpr::getFRem(LHS, RHS));
}
diff --git a/llvm/include/llvm/IR/ConstantFolder.h b/llvm/include/llvm/IR/ConstantFolder.h
index bcccf99ee40c..6f5661d043a2 100644
--- a/llvm/include/llvm/IR/ConstantFolder.h
+++ b/llvm/include/llvm/IR/ConstantFolder.h
@@ -63,6 +63,38 @@ class ConstantFolder final : public IRBuilderFolder {
return nullptr;
}
+ Value *FoldUDiv(Value *LHS, Value *RHS, bool IsExact) const override {
+ auto *LC = dyn_cast<Constant>(LHS);
+ auto *RC = dyn_cast<Constant>(RHS);
+ if (LC && RC)
+ return ConstantExpr::getUDiv(LC, RC, IsExact);
+ return nullptr;
+ }
+
+ Value *FoldSDiv(Value *LHS, Value *RHS, bool IsExact) const override {
+ auto *LC = dyn_cast<Constant>(LHS);
+ auto *RC = dyn_cast<Constant>(RHS);
+ if (LC && RC)
+ return ConstantExpr::getSDiv(LC, RC, IsExact);
+ return nullptr;
+ }
+
+ Value *FoldURem(Value *LHS, Value *RHS) const override {
+ auto *LC = dyn_cast<Constant>(LHS);
+ auto *RC = dyn_cast<Constant>(RHS);
+ if (LC && RC)
+ return ConstantExpr::getURem(LC, RC);
+ return nullptr;
+ }
+
+ Value *FoldSRem(Value *LHS, Value *RHS) const override {
+ auto *LC = dyn_cast<Constant>(LHS);
+ auto *RC = dyn_cast<Constant>(RHS);
+ if (LC && RC)
+ return ConstantExpr::getSRem(LC, RC);
+ return nullptr;
+ }
+
Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
auto *LC = dyn_cast<Constant>(LHS);
auto *RC = dyn_cast<Constant>(RHS);
@@ -164,28 +196,10 @@ class ConstantFolder final : public IRBuilderFolder {
return ConstantExpr::getFMul(LHS, RHS);
}
- Constant *CreateUDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const override {
- return ConstantExpr::getUDiv(LHS, RHS, isExact);
- }
-
- Constant *CreateSDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const override {
- return ConstantExpr::getSDiv(LHS, RHS, isExact);
- }
-
Constant *CreateFDiv(Constant *LHS, Constant *RHS) const override {
return ConstantExpr::getFDiv(LHS, RHS);
}
- Constant *CreateURem(Constant *LHS, Constant *RHS) const override {
- return ConstantExpr::getURem(LHS, RHS);
- }
-
- Constant *CreateSRem(Constant *LHS, Constant *RHS) const override {
- return ConstantExpr::getSRem(LHS, RHS);
- }
-
Constant *CreateFRem(Constant *LHS, Constant *RHS) const override {
return ConstantExpr::getFRem(LHS, RHS);
}
diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 9c6e37fac8db..3267d999d6ff 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -1256,9 +1256,8 @@ class IRBuilderBase {
Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
bool isExact = false) {
- if (auto *LC = dyn_cast<Constant>(LHS))
- if (auto *RC = dyn_cast<Constant>(RHS))
- return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
+ if (Value *V = Folder.FoldUDiv(LHS, RHS, isExact))
+ return V;
if (!isExact)
return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
@@ -1270,9 +1269,8 @@ class IRBuilderBase {
Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
bool isExact = false) {
- if (auto *LC = dyn_cast<Constant>(LHS))
- if (auto *RC = dyn_cast<Constant>(RHS))
- return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
+ if (Value *V = Folder.FoldSDiv(LHS, RHS, isExact))
+ return V;
if (!isExact)
return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
@@ -1283,12 +1281,14 @@ class IRBuilderBase {
}
Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
- if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V;
+ if (Value *V = Folder.FoldURem(LHS, RHS))
+ return V;
return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
}
Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
- if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V;
+ if (Value *V = Folder.FoldSRem(LHS, RHS))
+ return V;
return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
}
diff --git a/llvm/include/llvm/IR/IRBuilderFolder.h b/llvm/include/llvm/IR/IRBuilderFolder.h
index 5c0d592e5859..38e150e1d955 100644
--- a/llvm/include/llvm/IR/IRBuilderFolder.h
+++ b/llvm/include/llvm/IR/IRBuilderFolder.h
@@ -38,6 +38,14 @@ class IRBuilderFolder {
virtual Value *FoldOr(Value *LHS, Value *RHS) const = 0;
+ virtual Value *FoldUDiv(Value *LHS, Value *RHS, bool IsExact) const = 0;
+
+ virtual Value *FoldSDiv(Value *LHS, Value *RHS, bool IsExact) const = 0;
+
+ virtual Value *FoldURem(Value *LHS, Value *RHS) const = 0;
+
+ virtual Value *FoldSRem(Value *LHS, Value *RHS) const = 0;
+
virtual Value *FoldICmp(CmpInst::Predicate P, Value *LHS,
Value *RHS) const = 0;
@@ -71,13 +79,7 @@ class IRBuilderFolder {
virtual Value *CreateMul(Constant *LHS, Constant *RHS,
bool HasNUW = false, bool HasNSW = false) const = 0;
virtual Value *CreateFMul(Constant *LHS, Constant *RHS) const = 0;
- virtual Value *CreateUDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const = 0;
- virtual Value *CreateSDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const = 0;
virtual Value *CreateFDiv(Constant *LHS, Constant *RHS) const = 0;
- virtual Value *CreateURem(Constant *LHS, Constant *RHS) const = 0;
- virtual Value *CreateSRem(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateFRem(Constant *LHS, Constant *RHS) const = 0;
virtual Value *CreateShl(Constant *LHS, Constant *RHS,
bool HasNUW = false, bool HasNSW = false) const = 0;
diff --git a/llvm/include/llvm/IR/NoFolder.h b/llvm/include/llvm/IR/NoFolder.h
index 0ad94413b20d..4b9e1834bf5d 100644
--- a/llvm/include/llvm/IR/NoFolder.h
+++ b/llvm/include/llvm/IR/NoFolder.h
@@ -52,6 +52,18 @@ class NoFolder final : public IRBuilderFolder {
Value *FoldOr(Value *LHS, Value *RHS) const override { return nullptr; }
+ Value *FoldUDiv(Value *LHS, Value *RHS, bool IsExact) const override {
+ return nullptr;
+ }
+
+ Value *FoldSDiv(Value *LHS, Value *RHS, bool IsExact) const override {
+ return nullptr;
+ }
+
+ Value *FoldURem(Value *LHS, Value *RHS) const override { return nullptr; }
+
+ Value *FoldSRem(Value *LHS, Value *RHS) const override { return nullptr; }
+
Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override {
return nullptr;
}
@@ -123,32 +135,10 @@ class NoFolder final : public IRBuilderFolder {
return BinaryOperator::CreateFMul(LHS, RHS);
}
- Instruction *CreateUDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const override {
- if (!isExact)
- return BinaryOperator::CreateUDiv(LHS, RHS);
- return BinaryOperator::CreateExactUDiv(LHS, RHS);
- }
-
- Instruction *CreateSDiv(Constant *LHS, Constant *RHS,
- bool isExact = false) const override {
- if (!isExact)
- return BinaryOperator::CreateSDiv(LHS, RHS);
- return BinaryOperator::CreateExactSDiv(LHS, RHS);
- }
-
Instruction *CreateFDiv(Constant *LHS, Constant *RHS) const override {
return BinaryOperator::CreateFDiv(LHS, RHS);
}
- Instruction *CreateURem(Constant *LHS, Constant *RHS) const override {
- return BinaryOperator::CreateURem(LHS, RHS);
- }
-
- Instruction *CreateSRem(Constant *LHS, Constant *RHS) const override {
- return BinaryOperator::CreateSRem(LHS, RHS);
- }
-
Instruction *CreateFRem(Constant *LHS, Constant *RHS) const override {
return BinaryOperator::CreateFRem(LHS, RHS);
}
More information about the llvm-commits
mailing list