From 6d6983ced944a81bc95f99939d3ebac3cc69d666 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 13 Jul 2022 14:51:04 +0200 Subject: [PATCH] [IRBuilder] Migrate fneg to fold infrastructure Make use of a single FoldUnOpFMF() API, though in practice FNeg is the only unary operation that exists. This is likely NFC in practice, because users of InstSimplifyFolder don't create fneg. --- llvm/include/llvm/Analysis/InstSimplifyFolder.h | 17 +++++------------ llvm/include/llvm/Analysis/TargetFolder.h | 19 +++++++------------ llvm/include/llvm/IR/ConstantFolder.h | 19 +++++++------------ llvm/include/llvm/IR/IRBuilder.h | 16 ++++++++-------- llvm/include/llvm/IR/IRBuilderFolder.h | 10 +++------- llvm/include/llvm/IR/NoFolder.h | 18 +++++------------- 6 files changed, 35 insertions(+), 64 deletions(-) diff --git a/llvm/include/llvm/Analysis/InstSimplifyFolder.h b/llvm/include/llvm/Analysis/InstSimplifyFolder.h index d4ea7d7..16bd9f7 100644 --- a/llvm/include/llvm/Analysis/InstSimplifyFolder.h +++ b/llvm/include/llvm/Analysis/InstSimplifyFolder.h @@ -67,6 +67,11 @@ public: return simplifyBinOp(Opc, LHS, RHS, FMF, SQ); } + Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, + FastMathFlags FMF) const override { + return simplifyUnOp(Opc, V, FMF, SQ); + } + Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override { return simplifyICmpInst(P, LHS, RHS, SQ); } @@ -108,18 +113,6 @@ public: } //===--------------------------------------------------------------------===// - // Unary Operators - //===--------------------------------------------------------------------===// - - Value *CreateFNeg(Constant *C) const override { - return ConstFolder.CreateFNeg(C); - } - - Value *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override { - return ConstFolder.CreateUnOp(Opc, C); - } - - //===--------------------------------------------------------------------===// // Cast/Conversion Operators //===--------------------------------------------------------------------===// diff --git a/llvm/include/llvm/Analysis/TargetFolder.h b/llvm/include/llvm/Analysis/TargetFolder.h index 75c0750..5e7e14f 100644 --- a/llvm/include/llvm/Analysis/TargetFolder.h +++ b/llvm/include/llvm/Analysis/TargetFolder.h @@ -107,6 +107,13 @@ public: return nullptr; } + Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, + FastMathFlags FMF) const override { + if (Constant *C = dyn_cast(V)) + return Fold(ConstantExpr::get(Opc, C)); + return nullptr; + } + Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef IdxList, bool IsInBounds = false) const override { if (auto *PC = dyn_cast(Ptr)) { @@ -175,18 +182,6 @@ public: } //===--------------------------------------------------------------------===// - // Unary Operators - //===--------------------------------------------------------------------===// - - Constant *CreateFNeg(Constant *C) const override { - return Fold(ConstantExpr::getFNeg(C)); - } - - Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override { - return Fold(ConstantExpr::get(Opc, C)); - } - - //===--------------------------------------------------------------------===// // Cast/Conversion Operators //===--------------------------------------------------------------------===// diff --git a/llvm/include/llvm/IR/ConstantFolder.h b/llvm/include/llvm/IR/ConstantFolder.h index 7bc1e41..bd28ff8 100644 --- a/llvm/include/llvm/IR/ConstantFolder.h +++ b/llvm/include/llvm/IR/ConstantFolder.h @@ -88,6 +88,13 @@ public: return FoldBinOp(Opc, LHS, RHS); } + Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, + FastMathFlags FMF) const override { + if (Constant *C = dyn_cast(V)) + return ConstantExpr::get(Opc, C); + return nullptr; + } + Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override { auto *LC = dyn_cast(LHS); auto *RC = dyn_cast(RHS); @@ -164,18 +171,6 @@ public: } //===--------------------------------------------------------------------===// - // Unary Operators - //===--------------------------------------------------------------------===// - - Constant *CreateFNeg(Constant *C) const override { - return ConstantExpr::getFNeg(C); - } - - Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const override { - return ConstantExpr::get(Opc, C); - } - - //===--------------------------------------------------------------------===// // Cast/Conversion Operators //===--------------------------------------------------------------------===// diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h index d8f0893..5e18b02 100644 --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -1588,8 +1588,8 @@ public: Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = nullptr) { - if (auto *VC = dyn_cast(V)) - return Insert(Folder.CreateFNeg(VC), Name); + if (Value *Res = Folder.FoldUnOpFMF(Instruction::FNeg, V, FMF)) + return Res; return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF), Name); } @@ -1598,10 +1598,10 @@ public: /// default FMF. Value *CreateFNegFMF(Value *V, Instruction *FMFSource, const Twine &Name = "") { - if (auto *VC = dyn_cast(V)) - return Insert(Folder.CreateFNeg(VC), Name); - return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr, - FMFSource->getFastMathFlags()), + FastMathFlags FMF = FMFSource->getFastMathFlags(); + if (Value *Res = Folder.FoldUnOpFMF(Instruction::FNeg, V, FMF)) + return Res; + return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr, FMF), Name); } @@ -1612,8 +1612,8 @@ public: Value *CreateUnOp(Instruction::UnaryOps Opc, Value *V, const Twine &Name = "", MDNode *FPMathTag = nullptr) { - if (auto *VC = dyn_cast(V)) - return Insert(Folder.CreateUnOp(Opc, VC), Name); + if (Value *Res = Folder.FoldUnOpFMF(Opc, V, FMF)) + return Res; Instruction *UnOp = UnaryOperator::Create(Opc, V); if (isa(UnOp)) setFPAttrs(UnOp, FPMathTag, FMF); diff --git a/llvm/include/llvm/IR/IRBuilderFolder.h b/llvm/include/llvm/IR/IRBuilderFolder.h index 9505f1e..b2b2723 100644 --- a/llvm/include/llvm/IR/IRBuilderFolder.h +++ b/llvm/include/llvm/IR/IRBuilderFolder.h @@ -45,6 +45,9 @@ public: virtual Value *FoldBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, FastMathFlags FMF) const = 0; + virtual Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, + FastMathFlags FMF) const = 0; + virtual Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const = 0; @@ -68,13 +71,6 @@ public: ArrayRef Mask) const = 0; //===--------------------------------------------------------------------===// - // Unary Operators - //===--------------------------------------------------------------------===// - - virtual Value *CreateFNeg(Constant *C) const = 0; - virtual Value *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const = 0; - - //===--------------------------------------------------------------------===// // Cast/Conversion Operators //===--------------------------------------------------------------------===// diff --git a/llvm/include/llvm/IR/NoFolder.h b/llvm/include/llvm/IR/NoFolder.h index 4e9f772..56ccfc6 100644 --- a/llvm/include/llvm/IR/NoFolder.h +++ b/llvm/include/llvm/IR/NoFolder.h @@ -65,6 +65,11 @@ public: return nullptr; } + Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, + FastMathFlags FMF) const override { + return nullptr; + } + Value *FoldICmp(CmpInst::Predicate P, Value *LHS, Value *RHS) const override { return nullptr; } @@ -103,19 +108,6 @@ public: } //===--------------------------------------------------------------------===// - // Unary Operators - //===--------------------------------------------------------------------===// - - Instruction *CreateFNeg(Constant *C) const override { - return UnaryOperator::CreateFNeg(C); - } - - Instruction *CreateUnOp(Instruction::UnaryOps Opc, - Constant *C) const override { - return UnaryOperator::Create(Opc, C); - } - - //===--------------------------------------------------------------------===// // Cast/Conversion Operators //===--------------------------------------------------------------------===// -- 2.7.4