From faf4d59137e85f917f868e784c8d83ccc29c4b7f Mon Sep 17 00:00:00 2001 From: Michael Ilseman Date: Thu, 5 Dec 2013 00:32:09 +0000 Subject: [PATCH] Use present fast-math flags when applicable in CreateBinOp We were previously not adding fast-math flags through CreateBinOp() when it happened to be making a floating point binary operator. This patch updates it to do so similarly to directly calling CreateF*(). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@196438 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/IRBuilder.h | 8 ++++++-- unittests/IR/IRBuilderTest.cpp | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/include/llvm/IR/IRBuilder.h b/include/llvm/IR/IRBuilder.h index 56e818417aa..c9c11be89d6 100644 --- a/include/llvm/IR/IRBuilder.h +++ b/include/llvm/IR/IRBuilder.h @@ -832,11 +832,15 @@ public: } Value *CreateBinOp(Instruction::BinaryOps Opc, - Value *LHS, Value *RHS, const Twine &Name = "") { + Value *LHS, Value *RHS, const Twine &Name = "", + MDNode *FPMathTag = 0) { if (Constant *LC = dyn_cast(LHS)) if (Constant *RC = dyn_cast(RHS)) return Insert(Folder.CreateBinOp(Opc, LC, RC), Name); - return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name); + llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS); + if (isa(BinOp)) + BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF); + return Insert(BinOp, Name); } Value *CreateNeg(Value *V, const Twine &Name = "", diff --git a/unittests/IR/IRBuilderTest.cpp b/unittests/IR/IRBuilderTest.cpp index 2f390f7f75a..fcb56779073 100644 --- a/unittests/IR/IRBuilderTest.cpp +++ b/unittests/IR/IRBuilderTest.cpp @@ -147,6 +147,13 @@ TEST_F(IRBuilderTest, FastMathFlags) { FAdd = cast(F); EXPECT_TRUE(FAdd->hasNoNaNs()); + // Now, try it with CreateBinOp + F = Builder.CreateBinOp(Instruction::FAdd, F, F); + EXPECT_TRUE(Builder.getFastMathFlags().any()); + ASSERT_TRUE(isa(F)); + FAdd = cast(F); + EXPECT_TRUE(FAdd->hasNoNaNs()); + F = Builder.CreateFDiv(F, F); EXPECT_TRUE(Builder.getFastMathFlags().any()); EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra); -- 2.34.1