[llvm] 6d6983c - [IRBuilder] Migrate fneg to fold infrastructure

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 13 06:30:00 PDT 2022


Author: Nikita Popov
Date: 2022-07-13T15:29:52+02:00
New Revision: 6d6983ced944a81bc95f99939d3ebac3cc69d666

URL: https://github.com/llvm/llvm-project/commit/6d6983ced944a81bc95f99939d3ebac3cc69d666
DIFF: https://github.com/llvm/llvm-project/commit/6d6983ced944a81bc95f99939d3ebac3cc69d666.diff

LOG: [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.

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 d4ea7d73ec92..16bd9f765421 100644
--- a/llvm/include/llvm/Analysis/InstSimplifyFolder.h
+++ b/llvm/include/llvm/Analysis/InstSimplifyFolder.h
@@ -67,6 +67,11 @@ class InstSimplifyFolder final : public IRBuilderFolder {
     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);
   }
@@ -107,18 +112,6 @@ class InstSimplifyFolder final : public IRBuilderFolder {
     return simplifyShuffleVectorInst(V1, V2, Mask, RetTy, SQ);
   }
 
-  //===--------------------------------------------------------------------===//
-  // 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 75c0750a9e7b..5e7e14f226a4 100644
--- a/llvm/include/llvm/Analysis/TargetFolder.h
+++ b/llvm/include/llvm/Analysis/TargetFolder.h
@@ -107,6 +107,13 @@ class TargetFolder final : public IRBuilderFolder {
     return nullptr;
   }
 
+  Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
+                      FastMathFlags FMF) const override {
+    if (Constant *C = dyn_cast<Constant>(V))
+      return Fold(ConstantExpr::get(Opc, C));
+    return nullptr;
+  }
+
   Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
                  bool IsInBounds = false) const override {
     if (auto *PC = dyn_cast<Constant>(Ptr)) {
@@ -174,18 +181,6 @@ class TargetFolder final : public IRBuilderFolder {
     return nullptr;
   }
 
-  //===--------------------------------------------------------------------===//
-  // 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 7bc1e4194ad0..bd28ff87965d 100644
--- a/llvm/include/llvm/IR/ConstantFolder.h
+++ b/llvm/include/llvm/IR/ConstantFolder.h
@@ -88,6 +88,13 @@ class ConstantFolder final : public IRBuilderFolder {
     return FoldBinOp(Opc, LHS, RHS);
   }
 
+  Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
+                      FastMathFlags FMF) const override {
+    if (Constant *C = dyn_cast<Constant>(V))
+      return ConstantExpr::get(Opc, C);
+    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);
@@ -163,18 +170,6 @@ class ConstantFolder final : public IRBuilderFolder {
     return nullptr;
   }
 
-  //===--------------------------------------------------------------------===//
-  // 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 d8f08934b3d6..5e18b02bf511 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -1588,8 +1588,8 @@ class IRBuilderBase {
 
   Value *CreateFNeg(Value *V, const Twine &Name = "",
                     MDNode *FPMathTag = nullptr) {
-    if (auto *VC = dyn_cast<Constant>(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 @@ class IRBuilderBase {
   /// default FMF.
   Value *CreateFNegFMF(Value *V, Instruction *FMFSource,
                        const Twine &Name = "") {
-   if (auto *VC = dyn_cast<Constant>(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 @@ class IRBuilderBase {
   Value *CreateUnOp(Instruction::UnaryOps Opc,
                     Value *V, const Twine &Name = "",
                     MDNode *FPMathTag = nullptr) {
-    if (auto *VC = dyn_cast<Constant>(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<FPMathOperator>(UnOp))
       setFPAttrs(UnOp, FPMathTag, FMF);

diff  --git a/llvm/include/llvm/IR/IRBuilderFolder.h b/llvm/include/llvm/IR/IRBuilderFolder.h
index 9505f1e3be2a..b2b27235a1e6 100644
--- a/llvm/include/llvm/IR/IRBuilderFolder.h
+++ b/llvm/include/llvm/IR/IRBuilderFolder.h
@@ -45,6 +45,9 @@ class IRBuilderFolder {
   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;
 
@@ -67,13 +70,6 @@ class IRBuilderFolder {
   virtual Value *FoldShuffleVector(Value *V1, Value *V2,
                                    ArrayRef<int> 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 4e9f772dfdb6..56ccfc694c5f 100644
--- a/llvm/include/llvm/IR/NoFolder.h
+++ b/llvm/include/llvm/IR/NoFolder.h
@@ -65,6 +65,11 @@ class NoFolder final : public IRBuilderFolder {
     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;
   }
@@ -102,19 +107,6 @@ class NoFolder final : public IRBuilderFolder {
     return nullptr;
   }
 
-  //===--------------------------------------------------------------------===//
-  // 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
   //===--------------------------------------------------------------------===//


        


More information about the llvm-commits mailing list