[PATCH] D62521: [IRBuilder] Add CreateFNegFMF(...) to the IRBuilder

Cameron McInally via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 28 06:46:05 PDT 2019


cameron.mcinally created this revision.
cameron.mcinally added reviewers: spatel, arsenm, craig.topper, andrew.w.kaylor, kpn.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

@craig.topper brought up the question -- do we want to call `UnaryOperator::CreateFNeg(...)` or `BinaryOperator::CreateFNeg(...)` here. My current thinking is that we want to call the UnaryOperator flavor, since this is a new entry point. Using the UnaryOperator is the end goal and there will be no regressions from this decision. This will minimize the work going forward.

I'm open to other opinions though...


https://reviews.llvm.org/D62521

Files:
  llvm/include/llvm/IR/IRBuilder.h
  llvm/unittests/IR/IRBuilderTest.cpp


Index: llvm/unittests/IR/IRBuilderTest.cpp
===================================================================
--- llvm/unittests/IR/IRBuilderTest.cpp
+++ llvm/unittests/IR/IRBuilderTest.cpp
@@ -206,12 +206,22 @@
   IRBuilder<NoFolder> Builder(BB);
   Value *V = Builder.CreateLoad(GV->getValueType(), GV);
 
-  // Test CreateUnOp
+  // Test CreateUnOp(X)
   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 CreateFNegFMF(X)
+  Instruction *I = cast<Instruction>(V);
+  I->setHasNoSignedZeros(true);
+  I->setHasNoNaNs(true);
+  Value *VFMF = Builder.CreateFNegFMF(V, I);
+  Instruction *IFMF = cast<Instruction>(VFMF);
+  EXPECT_TRUE(IFMF->hasNoSignedZeros());
+  EXPECT_TRUE(IFMF->hasNoNaNs());
+  EXPECT_FALSE(IFMF->hasAllowReassoc());
 }
 
 TEST_F(IRBuilderTest, FastMathFlags) {
Index: llvm/include/llvm/IR/IRBuilder.h
===================================================================
--- llvm/include/llvm/IR/IRBuilder.h
+++ llvm/include/llvm/IR/IRBuilder.h
@@ -1372,6 +1372,17 @@
     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<Constant>(V))
+     return Insert(Folder.CreateFNeg(VC), Name);
+   return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr,
+                            FMFSource->getFastMathFlags()),
+                 Name);
+  }
+
   Value *CreateUnOp(Instruction::UnaryOps Opc,
                     Value *V, const Twine &Name = "",
                     MDNode *FPMathTag = nullptr) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D62521.201653.patch
Type: text/x-patch
Size: 1842 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190528/9b302aad/attachment.bin>


More information about the llvm-commits mailing list