From 0f0b06d84f46c73ce768109bde23ccf37510e693 Mon Sep 17 00:00:00 2001 From: Cameron McInally Date: Mon, 10 Jun 2019 15:07:29 +0000 Subject: [PATCH] [IRBuilder] Add CreateFNegFMF(...) to the IRBuilder Differential Revision: https://reviews.llvm.org/D62521 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@362947 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/IRBuilder.h | 13 +++++++++++++ unittests/IR/IRBuilderTest.cpp | 12 +++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/llvm/IR/IRBuilder.h b/include/llvm/IR/IRBuilder.h index 6cda431a3b8..636fa2cee2b 100644 --- a/include/llvm/IR/IRBuilder.h +++ b/include/llvm/IR/IRBuilder.h @@ -1372,6 +1372,19 @@ public: return Insert(BinaryOperator::CreateNot(V), Name); } + /// Copy fast-math-flags from an instruction rather than using the builder's + /// default FMF. + Value *CreateFNegFMF(Value *V, Instruction *FMFSource, + const Twine &Name = "") { + if (auto *VC = dyn_cast(V)) + return Insert(Folder.CreateFNeg(VC), Name); + // TODO: This should return UnaryOperator::CreateFNeg(...) once we are + // confident that they are optimized sufficiently. + return Insert(setFPAttrs(BinaryOperator::CreateFNeg(V), nullptr, + FMFSource->getFastMathFlags()), + Name); + } + Value *CreateUnOp(Instruction::UnaryOps Opc, Value *V, const Twine &Name = "", MDNode *FPMathTag = nullptr) { diff --git a/unittests/IR/IRBuilderTest.cpp b/unittests/IR/IRBuilderTest.cpp index 51397ec745f..c7368e2038b 100644 --- a/unittests/IR/IRBuilderTest.cpp +++ b/unittests/IR/IRBuilderTest.cpp @@ -206,12 +206,22 @@ TEST_F(IRBuilderTest, UnaryOperators) { IRBuilder Builder(BB); Value *V = Builder.CreateLoad(GV->getValueType(), GV); - // Test CreateUnOp + // Test CreateUnOp(X) Value *U = Builder.CreateUnOp(Instruction::FNeg, V); ASSERT_TRUE(isa(U)); ASSERT_TRUE(isa(U)); ASSERT_TRUE(isa(U)); ASSERT_FALSE(isa(U)); + + // Test CreateFNegFMF(X) + Instruction *I = cast(V); + I->setHasNoSignedZeros(true); + I->setHasNoNaNs(true); + Value *VFMF = Builder.CreateFNegFMF(V, I); + Instruction *IFMF = cast(VFMF); + EXPECT_TRUE(IFMF->hasNoSignedZeros()); + EXPECT_TRUE(IFMF->hasNoNaNs()); + EXPECT_FALSE(IFMF->hasAllowReassoc()); } TEST_F(IRBuilderTest, FastMathFlags) { -- 2.11.0