[llvm] r361816 - [IRBuilder] Add CreateUnOp(...) to the IRBuilder to support unary FNeg

Cameron McInally via llvm-commits llvm-commits at lists.llvm.org
Tue May 28 06:00:52 PDT 2019


Author: mcinally
Date: Tue May 28 06:00:52 2019
New Revision: 361816

URL: http://llvm.org/viewvc/llvm-project?rev=361816&view=rev
Log:
[IRBuilder] Add CreateUnOp(...) to the IRBuilder to support unary FNeg

Also update UnaryOperator to support isa, cast, and dyn_cast.

Differential Revision: https://reviews.llvm.org/D62417

Modified:
    llvm/trunk/include/llvm/Analysis/TargetFolder.h
    llvm/trunk/include/llvm/IR/ConstantFolder.h
    llvm/trunk/include/llvm/IR/IRBuilder.h
    llvm/trunk/include/llvm/IR/InstrTypes.h
    llvm/trunk/include/llvm/IR/NoFolder.h
    llvm/trunk/unittests/IR/IRBuilderTest.cpp

Modified: llvm/trunk/include/llvm/Analysis/TargetFolder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/TargetFolder.h?rev=361816&r1=361815&r2=361816&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Analysis/TargetFolder.h (original)
+++ llvm/trunk/include/llvm/Analysis/TargetFolder.h Tue May 28 06:00:52 2019
@@ -124,6 +124,10 @@ public:
     return Fold(ConstantExpr::getNot(C));
   }
 
+  Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
+    return Fold(ConstantExpr::get(Opc, C));
+  }
+
   //===--------------------------------------------------------------------===//
   // Memory Instructions
   //===--------------------------------------------------------------------===//

Modified: llvm/trunk/include/llvm/IR/ConstantFolder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/ConstantFolder.h?rev=361816&r1=361815&r2=361816&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/ConstantFolder.h (original)
+++ llvm/trunk/include/llvm/IR/ConstantFolder.h Tue May 28 06:00:52 2019
@@ -134,6 +134,10 @@ public:
     return ConstantExpr::getNot(C);
   }
 
+  Constant *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
+    return ConstantExpr::get(Opc, C);
+  }
+
   //===--------------------------------------------------------------------===//
   // Memory Instructions
   //===--------------------------------------------------------------------===//

Modified: llvm/trunk/include/llvm/IR/IRBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/IRBuilder.h?rev=361816&r1=361815&r2=361816&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/IRBuilder.h (original)
+++ llvm/trunk/include/llvm/IR/IRBuilder.h Tue May 28 06:00:52 2019
@@ -1372,6 +1372,17 @@ public:
     return Insert(BinaryOperator::CreateNot(V), Name);
   }
 
+  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);
+    Instruction *UnOp = UnaryOperator::Create(Opc, V);
+    if (isa<FPMathOperator>(UnOp))
+      UnOp = setFPAttrs(UnOp, FPMathTag, FMF);
+    return Insert(UnOp, Name);
+  }
+
   //===--------------------------------------------------------------------===//
   // Instruction creation methods: Memory Instructions
   //===--------------------------------------------------------------------===//

Modified: llvm/trunk/include/llvm/IR/InstrTypes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/InstrTypes.h?rev=361816&r1=361815&r2=361816&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/InstrTypes.h (original)
+++ llvm/trunk/include/llvm/IR/InstrTypes.h Tue May 28 06:00:52 2019
@@ -77,7 +77,8 @@ public:
 
   // Methods for support type inquiry through isa, cast, and dyn_cast:
   static bool classof(const Instruction *I) {
-    return I->getOpcode() == Instruction::Alloca ||
+    return I->isUnaryOp() ||
+           I->getOpcode() == Instruction::Alloca ||
            I->getOpcode() == Instruction::Load ||
            I->getOpcode() == Instruction::VAArg ||
            I->getOpcode() == Instruction::ExtractValue ||
@@ -156,6 +157,14 @@ public:
   UnaryOps getOpcode() const {
     return static_cast<UnaryOps>(Instruction::getOpcode());
   }
+
+  // Methods for support type inquiry through isa, cast, and dyn_cast:
+  static bool classof(const Instruction *I) {
+    return I->isUnaryOp();
+  }
+  static bool classof(const Value *V) {
+    return isa<Instruction>(V) && classof(cast<Instruction>(V));
+  }
 };
 
 //===----------------------------------------------------------------------===//

Modified: llvm/trunk/include/llvm/IR/NoFolder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/NoFolder.h?rev=361816&r1=361815&r2=361816&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/NoFolder.h (original)
+++ llvm/trunk/include/llvm/IR/NoFolder.h Tue May 28 06:00:52 2019
@@ -203,6 +203,10 @@ public:
     return BinaryOperator::CreateNot(C);
   }
 
+  Instruction *CreateUnOp(Instruction::UnaryOps Opc, Constant *C) const {
+    return UnaryOperator::Create(Opc, C);
+  }
+
   //===--------------------------------------------------------------------===//
   // Memory Instructions
   //===--------------------------------------------------------------------===//

Modified: llvm/trunk/unittests/IR/IRBuilderTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/IRBuilderTest.cpp?rev=361816&r1=361815&r2=361816&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/IRBuilderTest.cpp (original)
+++ llvm/trunk/unittests/IR/IRBuilderTest.cpp Tue May 28 06:00:52 2019
@@ -202,6 +202,18 @@ TEST_F(IRBuilderTest, GetIntTy) {
   delete DL;
 }
 
+TEST_F(IRBuilderTest, UnaryOperators) {
+  IRBuilder<NoFolder> Builder(BB);
+  Value *V = Builder.CreateLoad(GV->getValueType(), GV);
+
+  // Test CreateUnOp
+  Value *U = Builder.CreateUnOp(Instruction::FNeg, V);
+  ASSERT_TRUE(isa<Instruction>(U));
+  ASSERT_TRUE(isa<FPMathOperator>(U));
+  ASSERT_TRUE(isa<UnaryOperator>(U));
+  ASSERT_FALSE(isa<BinaryOperator>(U));
+}
+
 TEST_F(IRBuilderTest, FastMathFlags) {
   IRBuilder<> Builder(BB);
   Value *F, *FC;




More information about the llvm-commits mailing list