From 8f2996fbdf7cd651b3434da35b3bdccc3308b5cc Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Thu, 22 Feb 2018 17:33:20 +0000 Subject: [PATCH] [IRBuilder] add creators for FP with FMF; NFCI Also, add a helper for the constant folder to reduce duplication. It seems out-of-place for and/or to be doing simplifications here? Otherwise, I could have used the helper on those opcodes too. llvm-svn: 325808 --- llvm/include/llvm/IR/IRBuilder.h | 165 ++++++++++++++++++------------- 1 file changed, 98 insertions(+), 67 deletions(-) diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h index ab9b5c549df9..db02978eeee9 100644 --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -917,17 +917,23 @@ private: return BO; } - Instruction *AddFPMathAttributes(Instruction *I, - MDNode *FPMathTag, - FastMathFlags FMF) const { - if (!FPMathTag) - FPMathTag = DefaultFPMathTag; - if (FPMathTag) - I->setMetadata(LLVMContext::MD_fpmath, FPMathTag); + Instruction *setFPAttrs(Instruction *I, MDNode *FPMD, + FastMathFlags FMF) const { + if (!FPMD) + FPMD = DefaultFPMathTag; + if (FPMD) + I->setMetadata(LLVMContext::MD_fpmath, FPMD); I->setFastMathFlags(FMF); return I; } + Value *foldConstant(Instruction::BinaryOps Opc, Value *L, + Value *R, const Twine &Name = nullptr) const { + auto *LC = dyn_cast(L); + auto *RC = dyn_cast(R); + return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr; + } + public: Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "", bool HasNUW = false, bool HasNSW = false) { @@ -943,14 +949,6 @@ public: Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { return CreateAdd(LHS, RHS, Name, true, false); } - Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "", - MDNode *FPMathTag = nullptr) { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Insert(Folder.CreateFAdd(LC, RC), Name); - return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS), - FPMathTag, FMF), Name); - } Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "", bool HasNUW = false, bool HasNSW = false) { if (Constant *LC = dyn_cast(LHS)) @@ -965,14 +963,6 @@ public: Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") { return CreateSub(LHS, RHS, Name, true, false); } - Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "", - MDNode *FPMathTag = nullptr) { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Insert(Folder.CreateFSub(LC, RC), Name); - return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS), - FPMathTag, FMF), Name); - } Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "", bool HasNUW = false, bool HasNSW = false) { if (Constant *LC = dyn_cast(LHS)) @@ -987,14 +977,6 @@ public: Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") { return CreateMul(LHS, RHS, Name, true, false); } - Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "", - MDNode *FPMathTag = nullptr) { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Insert(Folder.CreateFMul(LC, RC), Name); - return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS), - FPMathTag, FMF), Name); - } Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "", bool isExact = false) { if (Constant *LC = dyn_cast(LHS)) @@ -1019,35 +1001,14 @@ public: Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") { return CreateSDiv(LHS, RHS, Name, true); } - Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "", - MDNode *FPMathTag = nullptr) { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Insert(Folder.CreateFDiv(LC, RC), Name); - return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS), - FPMathTag, FMF), Name); - } Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Insert(Folder.CreateURem(LC, RC), Name); + if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V; return Insert(BinaryOperator::CreateURem(LHS, RHS), Name); } Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Insert(Folder.CreateSRem(LC, RC), Name); + if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V; return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name); } - Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "", - MDNode *FPMathTag = nullptr) { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Insert(Folder.CreateFRem(LC, RC), Name); - return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS), - FPMathTag, FMF), Name); - } - Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "", bool HasNUW = false, bool HasNSW = false) { if (Constant *LC = dyn_cast(LHS)) @@ -1136,9 +1097,7 @@ public: } Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Insert(Folder.CreateXor(LC, RC), Name); + if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V; return Insert(BinaryOperator::CreateXor(LHS, RHS), Name); } Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") { @@ -1147,16 +1106,89 @@ public: Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") { return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); } + Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "", + MDNode *FPMD = nullptr) { + if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF); + return Insert(I, Name); + } + /// Copy fast-math-flags from an instruction rather than using the builder's + /// default FMF. + Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource, + const Twine &Name = "") { + if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr, + FMFSource->getFastMathFlags()); + return Insert(I, Name); + } + Value *CreateFSub(Value *L, Value *R, const Twine &Name = "", + MDNode *FPMD = nullptr) { + if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF); + return Insert(I, Name); + } + /// Copy fast-math-flags from an instruction rather than using the builder's + /// default FMF. + Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource, + const Twine &Name = "") { + if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr, + FMFSource->getFastMathFlags()); + return Insert(I, Name); + } + Value *CreateFMul(Value *L, Value *R, const Twine &Name = "", + MDNode *FPMD = nullptr) { + if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF); + return Insert(I, Name); + } + /// Copy fast-math-flags from an instruction rather than using the builder's + /// default FMF. + Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource, + const Twine &Name = "") { + if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr, + FMFSource->getFastMathFlags()); + return Insert(I, Name); + } + Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "", + MDNode *FPMD = nullptr) { + if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF); + return Insert(I, Name); + } + /// Copy fast-math-flags from an instruction rather than using the builder's + /// default FMF. + Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource, + const Twine &Name = "") { + if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr, + FMFSource->getFastMathFlags()); + return Insert(I, Name); + } + Value *CreateFRem(Value *L, Value *R, const Twine &Name = "", + MDNode *FPMD = nullptr) { + if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF); + return Insert(I, Name); + } + /// Copy fast-math-flags from an instruction rather than using the builder's + /// default FMF. + Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource, + const Twine &Name = "") { + if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V; + Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr, + FMFSource->getFastMathFlags()); + return Insert(I, Name); + } Value *CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name = "", MDNode *FPMathTag = nullptr) { - if (Constant *LC = dyn_cast(LHS)) - if (Constant *RC = dyn_cast(RHS)) - return Insert(Folder.CreateBinOp(Opc, LC, RC), Name); + if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V; Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS); if (isa(BinOp)) - BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF); + BinOp = setFPAttrs(BinOp, FPMathTag, FMF); return Insert(BinOp, Name); } @@ -1179,8 +1211,8 @@ public: MDNode *FPMathTag = nullptr) { if (Constant *VC = dyn_cast(V)) return Insert(Folder.CreateFNeg(VC), Name); - return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V), - FPMathTag, FMF), Name); + return Insert(setFPAttrs(BinaryOperator::CreateFNeg(V), FPMathTag, FMF), + Name); } Value *CreateNot(Value *V, const Twine &Name = "") { if (Constant *VC = dyn_cast(V)) @@ -1686,8 +1718,7 @@ public: if (Constant *LC = dyn_cast(LHS)) if (Constant *RC = dyn_cast(RHS)) return Insert(Folder.CreateFCmp(P, LC, RC), Name); - return Insert(AddFPMathAttributes(new FCmpInst(P, LHS, RHS), - FPMathTag, FMF), Name); + return Insert(setFPAttrs(new FCmpInst(P, LHS, RHS), FPMathTag, FMF), Name); } //===--------------------------------------------------------------------===// @@ -1711,7 +1742,7 @@ public: MDNode *FPMathTag = nullptr) { CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles); if (isa(CI)) - CI = cast(AddFPMathAttributes(CI, FPMathTag, FMF)); + CI = cast(setFPAttrs(CI, FPMathTag, FMF)); return Insert(CI, Name); } @@ -1720,7 +1751,7 @@ public: const Twine &Name = "", MDNode *FPMathTag = nullptr) { CallInst *CI = CallInst::Create(Callee, Args, OpBundles); if (isa(CI)) - CI = cast(AddFPMathAttributes(CI, FPMathTag, FMF)); + CI = cast(setFPAttrs(CI, FPMathTag, FMF)); return Insert(CI, Name); }