[IRBuilder] Add CreateFNegFMF(...) to the IRBuilder

Differential Revision: https://reviews.llvm.org/D62521

llvm-svn: 362947
This commit is contained in:
Cameron McInally 2019-06-10 15:07:29 +00:00
parent a984404f6b
commit 8cd25d462d
2 changed files with 24 additions and 1 deletions

View File

@ -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<Constant>(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) {

View File

@ -206,12 +206,22 @@ TEST_F(IRBuilderTest, UnaryOperators) {
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) {