[IRBuilder] Migrate div/rem to use fold infrastructure

Migrate udiv, sdiv, urem, and srem to use the FoldXYZ rather than
the CreateXYZ infrastructure.
This commit is contained in:
Nikita Popov 2022-06-29 13:04:15 +02:00
parent 9d2e830737
commit 66a16b2848
6 changed files with 108 additions and 82 deletions

View File

@ -59,6 +59,22 @@ public:
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 @@ public:
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);
}

View File

@ -74,6 +74,38 @@ public:
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 @@ public:
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));
}

View File

@ -63,6 +63,38 @@ public:
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 @@ public:
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);
}

View File

@ -1256,9 +1256,8 @@ public:
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 @@ public:
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 @@ public:
}
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);
}

View File

@ -38,6 +38,14 @@ public:
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 @@ public:
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;

View File

@ -52,6 +52,18 @@ public:
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 @@ public:
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);
}