[IR] Add UnaryOperator::CreateFNegFMF(...)
authorCameron McInally <cameron.mcinally@nyu.edu>
Fri, 7 Jun 2019 18:59:51 +0000 (18:59 +0000)
committerCameron McInally <cameron.mcinally@nyu.edu>
Fri, 7 Jun 2019 18:59:51 +0000 (18:59 +0000)
Differential Revision: https://reviews.llvm.org/D62705

llvm-svn: 362828

llvm/include/llvm/IR/InstrTypes.h
llvm/unittests/IR/InstructionsTest.cpp

index 237929f5e609d75e1e7a40095b506ba339d5d453..ca419b50da6b72654dda8d9b40cd86b5f137daa1 100644 (file)
@@ -154,6 +154,20 @@ public:
   }
 #include "llvm/IR/Instruction.def"
 
+  static UnaryOperator *CreateWithCopiedFlags(UnaryOps Opc,
+                                              Value *V,
+                                              Instruction *CopyO,
+                                              const Twine &Name = "") {
+    UnaryOperator *UO = Create(Opc, V, Name);
+    UO->copyIRFlags(CopyO);
+    return UO;
+  }
+
+  static UnaryOperator *CreateFNegFMF(Value *Op, Instruction *FMFSource,
+                                      const Twine &Name = "") {
+    return CreateWithCopiedFlags(Instruction::FNeg, Op, FMFSource, Name);
+  }
+
   UnaryOps getOpcode() const {
     return static_cast<UnaryOps>(Instruction::getOpcode());
   }
@@ -269,7 +283,7 @@ public:
   static BinaryOperator *CreateFNegFMF(Value *Op, Instruction *FMFSource,
                                        const Twine &Name = "") {
     Value *Zero = ConstantFP::getNegativeZero(Op->getType());
-    return CreateWithCopiedFlags(Instruction::FSub, Zero, Op, FMFSource);
+    return CreateWithCopiedFlags(Instruction::FSub, Zero, Op, FMFSource, Name);
   }
 
   static BinaryOperator *CreateNSW(BinaryOps Opc, Value *V1, Value *V2,
index 70d51e5fc6d1013074600971baba72ac3ac2d3da..7edd3a98bfb579c36280d37edae9cfdb37a79486 100644 (file)
@@ -1081,5 +1081,23 @@ TEST(InstructionsTest, PhiIsNotFPMathOperator) {
   I->deleteValue();
 }
 
+TEST(InstructionsTest, FNegInstruction) {
+  LLVMContext Context;
+  Type *FltTy = Type::getFloatTy(Context);
+  Constant *One = ConstantFP::get(FltTy, 1.0);
+  BinaryOperator *FAdd = BinaryOperator::CreateFAdd(One, One);
+  FAdd->setHasNoNaNs(true);
+  UnaryOperator *FNeg = UnaryOperator::CreateFNegFMF(One, FAdd);
+  EXPECT_TRUE(FNeg->hasNoNaNs());
+  EXPECT_FALSE(FNeg->hasNoInfs());
+  EXPECT_FALSE(FNeg->hasNoSignedZeros());
+  EXPECT_FALSE(FNeg->hasAllowReciprocal());
+  EXPECT_FALSE(FNeg->hasAllowContract());
+  EXPECT_FALSE(FNeg->hasAllowReassoc());
+  EXPECT_FALSE(FNeg->hasApproxFunc());
+  FAdd->deleteValue();
+  FNeg->deleteValue();
+}
+
 } // end anonymous namespace
 } // end namespace llvm