Use present fast-math flags when applicable in CreateBinOp
authorMichael Ilseman <milseman@apple.com>
Thu, 5 Dec 2013 00:32:09 +0000 (00:32 +0000)
committerMichael Ilseman <milseman@apple.com>
Thu, 5 Dec 2013 00:32:09 +0000 (00:32 +0000)
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
unittests/IR/IRBuilderTest.cpp

index 56e818417aabab48bdffd8cdd7c817b29542aa44..c9c11be89d630b7a11a516e121c8eb573b200ced 100644 (file)
@@ -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<Constant>(LHS))
       if (Constant *RC = dyn_cast<Constant>(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<FPMathOperator>(BinOp))
+      BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
+    return Insert(BinOp, Name);
   }
 
   Value *CreateNeg(Value *V, const Twine &Name = "",
index 2f390f7f75a2fef51861c3090cb2ae80741c0cff..fcb567790734e5a570fbbc221043b09dbbb2d9a7 100644 (file)
@@ -147,6 +147,13 @@ TEST_F(IRBuilderTest, FastMathFlags) {
   FAdd = cast<Instruction>(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<Instruction>(F));
+  FAdd = cast<Instruction>(F);
+  EXPECT_TRUE(FAdd->hasNoNaNs());
+
   F = Builder.CreateFDiv(F, F);
   EXPECT_TRUE(Builder.getFastMathFlags().any());
   EXPECT_TRUE(Builder.getFastMathFlags().UnsafeAlgebra);