Fast-math: Extend IRBuilder to have settable FastMathFlags to create instructions with

Also extended IRBuilder's documentation to mention the convenience state for DefaultFPMathTag and FastMathFlags that can be set.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168812 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Michael Ilseman 2012-11-28 21:16:19 +00:00
parent 0d38424bbe
commit 9992bf75fc

View File

@ -19,6 +19,7 @@
#include "llvm/BasicBlock.h"
#include "llvm/DataLayout.h"
#include "llvm/LLVMContext.h"
#include "llvm/Operator.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
@ -329,7 +330,10 @@ private:
///
/// Note that the builder does not expose the full generality of LLVM
/// instructions. For access to extra instruction properties, use the mutators
/// (e.g. setVolatile) on the instructions after they have been created.
/// (e.g. setVolatile) on the instructions after they have been
/// created. Convenience state exists to specify fast-math flags and fp-math
/// tags.
///
/// The first template argument handles whether or not to preserve names in the
/// final instruction output. This defaults to on. The second template argument
/// specifies a class to use for creating constants. This defaults to creating
@ -341,36 +345,40 @@ template<bool preserveNames = true, typename T = ConstantFolder,
class IRBuilder : public IRBuilderBase, public Inserter {
T Folder;
MDNode *DefaultFPMathTag;
FastMathFlags FMF;
public:
IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
MDNode *FPMathTag = 0)
: IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag) {
: IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag),
FMF() {
}
explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0) : IRBuilderBase(C),
Folder(), DefaultFPMathTag(FPMathTag) {
explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0)
: IRBuilderBase(C), Folder(), DefaultFPMathTag(FPMathTag), FMF() {
}
explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
: IRBuilderBase(TheBB->getContext()), Folder(F),
DefaultFPMathTag(FPMathTag) {
DefaultFPMathTag(FPMathTag), FMF() {
SetInsertPoint(TheBB);
}
explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
: IRBuilderBase(TheBB->getContext()), Folder(),
DefaultFPMathTag(FPMathTag) {
DefaultFPMathTag(FPMathTag), FMF() {
SetInsertPoint(TheBB);
}
explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
: IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
: IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
FMF() {
SetInsertPoint(IP);
SetCurrentDebugLocation(IP->getDebugLoc());
}
explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
: IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
: IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag),
FMF() {
SetInsertPoint(U);
SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
}
@ -378,13 +386,13 @@ public:
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
MDNode *FPMathTag = 0)
: IRBuilderBase(TheBB->getContext()), Folder(F),
DefaultFPMathTag(FPMathTag) {
DefaultFPMathTag(FPMathTag), FMF() {
SetInsertPoint(TheBB, IP);
}
IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
: IRBuilderBase(TheBB->getContext()), Folder(),
DefaultFPMathTag(FPMathTag) {
DefaultFPMathTag(FPMathTag), FMF() {
SetInsertPoint(TheBB, IP);
}
@ -394,9 +402,18 @@ public:
/// getDefaultFPMathTag - Get the floating point math metadata being used.
MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
/// Get the flags to be applied to created floating point ops
FastMathFlags getFastMathFlags() const { return FMF; }
/// Clear the fast-math flags.
void clearFastMathFlags() { FMF.clear(); }
/// SetDefaultFPMathTag - Set the floating point math metadata to be used.
void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
/// Set the fast-math flags to be used with generated fp-math operators
void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
/// isNamePreserving - Return true if this builder is configured to actually
/// add the requested names to IR created through it.
bool isNamePreserving() const { return preserveNames; }
@ -535,11 +552,14 @@ private:
return BO;
}
Instruction *AddFPMathTag(Instruction *I, MDNode *FPMathTag) const {
Instruction *AddFPMathAttributes(Instruction *I,
MDNode *FPMathTag,
FastMathFlags FMF) const {
if (!FPMathTag)
FPMathTag = DefaultFPMathTag;
if (FPMathTag)
I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
I->setFastMathFlags(FMF);
return I;
}
public:
@ -562,8 +582,8 @@ public:
if (Constant *LC = dyn_cast<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(RHS))
return Insert(Folder.CreateFAdd(LC, RC), Name);
return Insert(AddFPMathTag(BinaryOperator::CreateFAdd(LHS, RHS),
FPMathTag), 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) {
@ -584,8 +604,8 @@ public:
if (Constant *LC = dyn_cast<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(RHS))
return Insert(Folder.CreateFSub(LC, RC), Name);
return Insert(AddFPMathTag(BinaryOperator::CreateFSub(LHS, RHS),
FPMathTag), 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) {
@ -606,8 +626,8 @@ public:
if (Constant *LC = dyn_cast<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(RHS))
return Insert(Folder.CreateFMul(LC, RC), Name);
return Insert(AddFPMathTag(BinaryOperator::CreateFMul(LHS, RHS),
FPMathTag), Name);
return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
FPMathTag, FMF), Name);
}
Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
bool isExact = false) {
@ -638,8 +658,8 @@ public:
if (Constant *LC = dyn_cast<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(RHS))
return Insert(Folder.CreateFDiv(LC, RC), Name);
return Insert(AddFPMathTag(BinaryOperator::CreateFDiv(LHS, RHS),
FPMathTag), 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<Constant>(LHS))
@ -658,8 +678,8 @@ public:
if (Constant *LC = dyn_cast<Constant>(LHS))
if (Constant *RC = dyn_cast<Constant>(RHS))
return Insert(Folder.CreateFRem(LC, RC), Name);
return Insert(AddFPMathTag(BinaryOperator::CreateFRem(LHS, RHS),
FPMathTag), Name);
return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
FPMathTag, FMF), Name);
}
Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
@ -788,7 +808,8 @@ public:
Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
if (Constant *VC = dyn_cast<Constant>(V))
return Insert(Folder.CreateFNeg(VC), Name);
return Insert(AddFPMathTag(BinaryOperator::CreateFNeg(V), FPMathTag), Name);
return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
FPMathTag, FMF), Name);
}
Value *CreateNot(Value *V, const Twine &Name = "") {
if (Constant *VC = dyn_cast<Constant>(V))