llvm.org GIT mirror llvm / 8883c43
Make it possible to indicate relaxed floating point requirements at the IR level through the use of 'fpmath' metadata. Currently this only provides a 'fpaccuracy' value, which may be a number in ULPs or the keyword 'fast', however the intent is that this will be extended with additional information about NaN's, infinities etc later. No optimizations have been hooked up to this so far. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154822 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 8 years ago
10 changed file(s) with 286 addition(s) and 90 deletion(s). Raw diff Collapse all Expand all
30053005
30063006
30073007

fpmath metadata may be attached to any instruction of floating point

3008 type. It can be used to express the maximum acceptable relative error in the
3009 result of that instruction, in ULPs, thus potentially allowing the compiler
3010 to use a more efficient but less accurate method of computing it.
3011 ULP is defined as follows:

3008 type. It can be used to express the maximum acceptable error in the result of
3009 that instruction, in ULPs, thus potentially allowing the compiler to use a
3010 more efficient but less accurate method of computing it. The number of ULPs
3011 may also be the string "fast", which tells the compiler that speed
3012 matters more than accuracy, so any fairly accurate method of computation is
3013 fine as long as it is quick. ULP is defined as follows:

30123014
30133015
30143016
30203022
30213023
30223024
3023

The metadata node shall consist of a single non-negative floating

3024 point number representing the maximum relative error. For example,
3025 2.5 ULP:

3025

The metadata node shall consist of a single positive floating point number

3026 representing the maximum relative error, or the string "fast".
3027 For example:

30263028
30273029
30283030

                  
                
3029 !0 = metadata !{ float 2.5 }
3031 !0 = metadata !{ float 2.5 } ; maximum acceptable inaccuracy is 2.5 ULPs
3032 !1 = metadata !{ !metadata !"fast" } ; potentially unbounded inaccuracy
30303033
30313034
30323035
488488
  • The unwind instruction is now gone. With the introduction of the
  • 489489 new exception handling system in LLVM 3.0, the unwind instruction
    490490 became obsolete.
    491
  • Floating point instructions can be annotated with fpmath metadata
  • 492 to indicate relaxed precision requirements, such as those corresponding to
    493 "fast math".
    491494
  • ....
  • 492495
    493496
    1414 #ifndef LLVM_OPERATOR_H
    1515 #define LLVM_OPERATOR_H
    1616
    17 #include "llvm/Constants.h"
    1718 #include "llvm/Instruction.h"
    18 #include "llvm/Constants.h"
    19 #include "llvm/Type.h"
    1920
    2021 namespace llvm {
    2122
    161162 (isa(V) && classof(cast(V)));
    162163 }
    163164 };
    164
    165
    166 /// FPMathOperator - Utility class for floating point operations which can have
    167 /// information about relaxed accuracy requirements attached to them.
    168 class FPMathOperator : public Operator {
    169 private:
    170 ~FPMathOperator(); // do not implement
    171
    172 public:
    173
    174 /// \brief Get the maximum error permitted by this operation in ULPs. An
    175 /// accuracy of 0.0 means that the operation should be performed with the
    176 /// default precision. A huge value is returned if the accuracy is 'fast'.
    177 float getFPAccuracy() const;
    178
    179 /// \brief Return true if the accuracy is 'fast'. This indicates that speed
    180 /// is more important than accuracy.
    181 bool isFastFPAccuracy() const;
    182
    183 static inline bool classof(const FPMathOperator *) { return true; }
    184 static inline bool classof(const Instruction *I) {
    185 return I->getType()->isFPOrFPVectorTy();
    186 }
    187 static inline bool classof(const Value *V) {
    188 return isa(V) && classof(cast(V));
    189 }
    190 };
    165191
    166192
    167193 /// ConcreteOperator - A helper template for defining operators for individual
    1616
    1717 #include "llvm/Instructions.h"
    1818 #include "llvm/BasicBlock.h"
    19 #include "llvm/LLVMContext.h"
    1920 #include "llvm/ADT/ArrayRef.h"
    2021 #include "llvm/ADT/StringRef.h"
    2122 #include "llvm/ADT/Twine.h"
    330331 typename Inserter = IRBuilderDefaultInserter >
    331332 class IRBuilder : public IRBuilderBase, public Inserter {
    332333 T Folder;
    334 MDNode *DefaultFPMathTag;
    333335 public:
    334 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
    335 : IRBuilderBase(C), Inserter(I), Folder(F) {
    336 }
    337
    338 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder() {
    339 }
    340
    341 explicit IRBuilder(BasicBlock *TheBB, const T &F)
    342 : IRBuilderBase(TheBB->getContext()), Folder(F) {
    336 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
    337 MDNode *FPMathTag = 0)
    338 : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag) {
    339 }
    340
    341 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0) : IRBuilderBase(C),
    342 Folder(), DefaultFPMathTag(FPMathTag) {
    343 }
    344
    345 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
    346 : IRBuilderBase(TheBB->getContext()), Folder(F),
    347 DefaultFPMathTag(FPMathTag) {
    343348 SetInsertPoint(TheBB);
    344349 }
    345350
    346 explicit IRBuilder(BasicBlock *TheBB)
    347 : IRBuilderBase(TheBB->getContext()), Folder() {
    351 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
    352 : IRBuilderBase(TheBB->getContext()), Folder(),
    353 DefaultFPMathTag(FPMathTag) {
    348354 SetInsertPoint(TheBB);
    349355 }
    350356
    351 explicit IRBuilder(Instruction *IP)
    352 : IRBuilderBase(IP->getContext()), Folder() {
    357 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
    358 : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
    353359 SetInsertPoint(IP);
    354360 SetCurrentDebugLocation(IP->getDebugLoc());
    355361 }
    356362
    357 explicit IRBuilder(Use &U)
    358 : IRBuilderBase(U->getContext()), Folder() {
    363 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
    364 : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
    359365 SetInsertPoint(U);
    360366 SetCurrentDebugLocation(cast(U.getUser())->getDebugLoc());
    361367 }
    362368
    363 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
    364 : IRBuilderBase(TheBB->getContext()), Folder(F) {
    369 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
    370 MDNode *FPMathTag = 0)
    371 : IRBuilderBase(TheBB->getContext()), Folder(F),
    372 DefaultFPMathTag(FPMathTag) {
    365373 SetInsertPoint(TheBB, IP);
    366374 }
    367375
    368 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
    369 : IRBuilderBase(TheBB->getContext()), Folder() {
    376 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
    377 : IRBuilderBase(TheBB->getContext()), Folder(),
    378 DefaultFPMathTag(FPMathTag) {
    370379 SetInsertPoint(TheBB, IP);
    371380 }
    372381
    373382 /// getFolder - Get the constant folder being used.
    374383 const T &getFolder() { return Folder; }
    384
    385 /// getDefaultFPMathTag - Get the floating point math metadata being used.
    386 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
    387
    388 /// SetDefaultFPMathTag - Set the floating point math metadata to be used.
    389 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
    375390
    376391 /// isNamePreserving - Return true if this builder is configured to actually
    377392 /// add the requested names to IR created through it.
    495510 if (HasNSW) BO->setHasNoSignedWrap();
    496511 return BO;
    497512 }
    513
    514 Instruction *AddFPMathTag(Instruction *I, MDNode *FPMathTag) const {
    515 if (!FPMathTag)
    516 FPMathTag = DefaultFPMathTag;
    517 if (FPMathTag)
    518 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
    519 return I;
    520 }
    498521 public:
    499522 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
    500523 bool HasNUW = false, bool HasNSW = false) {
    510533 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
    511534 return CreateAdd(LHS, RHS, Name, true, false);
    512535 }
    513 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
    536 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
    537 MDNode *FPMathTag = 0) {
    514538 if (Constant *LC = dyn_cast(LHS))
    515539 if (Constant *RC = dyn_cast(RHS))
    516540 return Insert(Folder.CreateFAdd(LC, RC), Name);
    517 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
    541 return Insert(AddFPMathTag(BinaryOperator::CreateFAdd(LHS, RHS),
    542 FPMathTag), Name);
    518543 }
    519544 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
    520545 bool HasNUW = false, bool HasNSW = false) {
    530555 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
    531556 return CreateSub(LHS, RHS, Name, true, false);
    532557 }
    533 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
    558 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
    559 MDNode *FPMathTag = 0) {
    534560 if (Constant *LC = dyn_cast(LHS))
    535561 if (Constant *RC = dyn_cast(RHS))
    536562 return Insert(Folder.CreateFSub(LC, RC), Name);
    537 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
    563 return Insert(AddFPMathTag(BinaryOperator::CreateFSub(LHS, RHS),
    564 FPMathTag), Name);
    538565 }
    539566 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
    540567 bool HasNUW = false, bool HasNSW = false) {
    550577 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
    551578 return CreateMul(LHS, RHS, Name, true, false);
    552579 }
    553 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
    580 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
    581 MDNode *FPMathTag = 0) {
    554582 if (Constant *LC = dyn_cast(LHS))
    555583 if (Constant *RC = dyn_cast(RHS))
    556584 return Insert(Folder.CreateFMul(LC, RC), Name);
    557 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
    585 return Insert(AddFPMathTag(BinaryOperator::CreateFMul(LHS, RHS),
    586 FPMathTag), Name);
    558587 }
    559588 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
    560589 bool isExact = false) {
    580609 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
    581610 return CreateSDiv(LHS, RHS, Name, true);
    582611 }
    583 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
    612 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
    613 MDNode *FPMathTag = 0) {
    584614 if (Constant *LC = dyn_cast(LHS))
    585615 if (Constant *RC = dyn_cast(RHS))
    586616 return Insert(Folder.CreateFDiv(LC, RC), Name);
    587 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
    617 return Insert(AddFPMathTag(BinaryOperator::CreateFDiv(LHS, RHS),
    618 FPMathTag), Name);
    588619 }
    589620 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
    590621 if (Constant *LC = dyn_cast(LHS))
    598629 return Insert(Folder.CreateSRem(LC, RC), Name);
    599630 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
    600631 }
    601 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
    632 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
    633 MDNode *FPMathTag = 0) {
    602634 if (Constant *LC = dyn_cast(LHS))
    603635 if (Constant *RC = dyn_cast(RHS))
    604636 return Insert(Folder.CreateFRem(LC, RC), Name);
    605 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
    637 return Insert(AddFPMathTag(BinaryOperator::CreateFRem(LHS, RHS),
    638 FPMathTag), Name);
    606639 }
    607640
    608641 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
    728761 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
    729762 return CreateNeg(V, Name, true, false);
    730763 }
    731 Value *CreateFNeg(Value *V, const Twine &Name = "") {
    764 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
    732765 if (Constant *VC = dyn_cast(V))
    733766 return Insert(Folder.CreateFNeg(VC), Name);
    734 return Insert(BinaryOperator::CreateFNeg(V), Name);
    767 return Insert(AddFPMathTag(BinaryOperator::CreateFNeg(V), FPMathTag), Name);
    735768 }
    736769 Value *CreateNot(Value *V, const Twine &Name = "") {
    737770 if (Constant *VC = dyn_cast(V))
    2525 class MDBuilder {
    2626 LLVMContext &Context;
    2727
    28 MDString *getFastString() {
    29 return createString("fast");
    30 }
    2831 public:
    2932 MDBuilder(LLVMContext &context) : Context(context) {}
    3033
    31 /// CreateString - Return the given string as metadata.
    32 MDString *CreateString(StringRef Str) {
    34 /// \brief Return the given string as metadata.
    35 MDString *createString(StringRef Str) {
    3336 return MDString::get(Context, Str);
    3437 }
    38
    39 //===------------------------------------------------------------------===//
    40 // FPMath metadata.
    41 //===------------------------------------------------------------------===//
    42
    43 /// \brief Return metadata with appropriate settings for 'fast math'.
    44 MDNode *createFastFPMath() {
    45 return MDNode::get(Context, getFastString());
    46 }
    47
    48 /// \brief Return metadata with the given settings. Special values for the
    49 /// Accuracy parameter are 0.0, which means the default (maximal precision)
    50 /// setting; and negative values which all mean 'fast'.
    51 MDNode *createFPMath(float Accuracy) {
    52 if (Accuracy == 0.0)
    53 return 0;
    54 if (Accuracy < 0.0)
    55 return MDNode::get(Context, getFastString());
    56 assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
    57 Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
    58 return MDNode::get(Context, Op);
    59 }
    60
    3561
    3662 //===------------------------------------------------------------------===//
    3763 // Range metadata.
    3864 //===------------------------------------------------------------------===//
    3965
    40 /// CreateRange - Return metadata describing the range [Lo, Hi).
    41 MDNode *CreateRange(const APInt &Lo, const APInt &Hi) {
    66 /// \brief Return metadata describing the range [Lo, Hi).
    67 MDNode *createRange(const APInt &Lo, const APInt &Hi) {
    4268 assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");
    4369 // If the range is everything then it is useless.
    4470 if (Hi == Lo)
    5581 // TBAA metadata.
    5682 //===------------------------------------------------------------------===//
    5783
    58 /// CreateAnonymousTBAARoot - Return metadata appropriate for a TBAA root
    59 /// node. Each returned node is distinct from all other metadata and will
    60 /// never be identified (uniqued) with anything else.
    61 MDNode *CreateAnonymousTBAARoot() {
    84 /// \brief Return metadata appropriate for a TBAA root node. Each returned
    85 /// node is distinct from all other metadata and will never be identified
    86 /// (uniqued) with anything else.
    87 MDNode *createAnonymousTBAARoot() {
    6288 // To ensure uniqueness the root node is self-referential.
    6389 MDNode *Dummy = MDNode::getTemporary(Context, ArrayRef());
    6490 MDNode *Root = MDNode::get(Context, Dummy);
    7399 return Root;
    74100 }
    75101
    76 /// CreateTBAARoot - Return metadata appropriate for a TBAA root node with
    77 /// the given name. This may be identified (uniqued) with other roots with
    78 /// the same name.
    79 MDNode *CreateTBAARoot(StringRef Name) {
    80 return MDNode::get(Context, CreateString(Name));
    102 /// \brief Return metadata appropriate for a TBAA root node with the given
    103 /// name. This may be identified (uniqued) with other roots with the same
    104 /// name.
    105 MDNode *createTBAARoot(StringRef Name) {
    106 return MDNode::get(Context, createString(Name));
    81107 }
    82108
    83 /// CreateTBAANode - Return metadata for a non-root TBAA node with the given
    84 /// name, parent in the TBAA tree, and value for 'pointsToConstantMemory'.
    85 MDNode *CreateTBAANode(StringRef Name, MDNode *Parent,
    109 /// \brief Return metadata for a non-root TBAA node with the given name,
    110 /// parent in the TBAA tree, and value for 'pointsToConstantMemory'.
    111 MDNode *createTBAANode(StringRef Name, MDNode *Parent,
    86112 bool isConstant = false) {
    87113 if (isConstant) {
    88114 Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
    89 Value *Ops[3] = { CreateString(Name), Parent, Flags };
    115 Value *Ops[3] = { createString(Name), Parent, Flags };
    90116 return MDNode::get(Context, Ops);
    91117 } else {
    92 Value *Ops[2] = { CreateString(Name), Parent };
    118 Value *Ops[2] = { createString(Name), Parent };
    93119 return MDNode::get(Context, Ops);
    94120 }
    95121 }
    20002000 bool BinaryOperator::isExact() const {
    20012001 return cast(this)->isExact();
    20022002 }
    2003
    2004 //===----------------------------------------------------------------------===//
    2005 // FPMathOperator Class
    2006 //===----------------------------------------------------------------------===//
    2007
    2008 /// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
    2009 /// An accuracy of 0.0 means that the operation should be performed with the
    2010 /// default precision. A huge value is returned if the accuracy is 'fast'.
    2011 float FPMathOperator::getFPAccuracy() const {
    2012 const MDNode *MD =
    2013 cast(this)->getMetadata(LLVMContext::MD_fpmath);
    2014 if (!MD)
    2015 return 0.0;
    2016 Value *Op = MD->getOperand(0);
    2017 if (const ConstantFP *Accuracy = dyn_cast(Op))
    2018 return Accuracy->getValueAPF().convertToFloat();
    2019 // If it's not a floating point number then it must be 'fast'.
    2020 assert(isa(Op) && cast(Op)->getString() == "fast" &&
    2021 "Expected the 'fast' keyword!");
    2022 return HUGE_VALF;
    2023 }
    2024
    2025 /// isFastFPAccuracy - Return true if the accuracy is 'fast'. This says that
    2026 /// speed is more important than accuracy.
    2027 bool FPMathOperator::isFastFPAccuracy() const {
    2028 const MDNode *MD =
    2029 cast(this)->getMetadata(LLVMContext::MD_fpmath);
    2030 if (!MD)
    2031 return false;
    2032 Value *Op = MD->getOperand(0);
    2033 if (isa(Op))
    2034 return false;
    2035 // If it's not a floating point number then it must be 'fast'.
    2036 assert(isa(Op) && cast(Op)->getString() == "fast" &&
    2037 "Expected the 'fast' keyword!");
    2038 return true;
    2039 }
    2040
    20032041
    20042042 //===----------------------------------------------------------------------===//
    20052043 // CastInst Class
    16561656 Assert1(I.getType()->isFPOrFPVectorTy(),
    16571657 "fpmath requires a floating point result!", &I);
    16581658 Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
    1659 ConstantFP *Op = dyn_cast_or_null(MD->getOperand(0));
    1660 Assert1(Op, "fpmath ULPs not a floating point number!", &I);
    1661 APFloat ULPs = Op->getValueAPF();
    1662 Assert1(ULPs.isNormal() || ULPs.isZero(),
    1663 "fpmath ULPs not a normal number!", &I);
    1664 Assert1(!ULPs.isNegative(), "fpmath ULPs is negative!", &I);
    1659 Value *Op0 = MD->getOperand(0);
    1660 if (ConstantFP *CFP0 = dyn_cast_or_null(Op0)) {
    1661 APFloat Accuracy = CFP0->getValueAPF();
    1662 Assert1(Accuracy.isNormal() && !Accuracy.isNegative(),
    1663 "fpmath accuracy not a positive number!", &I);
    1664 } else if (MDString *S0 = dyn_cast_or_null(Op0)) {
    1665 Assert1(S0->getString() == "fast", "wrong fpmath accuracy keyword!", &I);
    1666 } else {
    1667 Assert1(false, "invalid fpmath accuracy!", &I);
    1668 }
    16651669 }
    16661670
    16671671 MDNode *MD = I.getMetadata(LLVMContext::MD_range);
    0 ; RUN: not llvm-as < %s |& FileCheck %s
    11
    2 define void @foo(i32 %i, float %f, <2 x float> %g) {
    2 define void @fpmath1(i32 %i, float %f, <2 x float> %g) {
    33 %s = add i32 %i, %i, !fpmath !0
    44 ; CHECK: fpmath requires a floating point result!
    55 %t = fadd float %f, %f, !fpmath !1
    77 %u = fadd float %f, %f, !fpmath !2
    88 ; CHECK: fpmath takes one operand!
    99 %v = fadd float %f, %f, !fpmath !3
    10 ; CHECK: fpmath ULPs not a floating point number!
    10 ; CHECK: invalid fpmath accuracy!
    1111 %w = fadd float %f, %f, !fpmath !0
    1212 ; Above line is correct.
    1313 %w2 = fadd <2 x float> %g, %g, !fpmath !0
    1414 ; Above line is correct.
    1515 %x = fadd float %f, %f, !fpmath !4
    16 ; CHECK: fpmath ULPs is negative!
    16 ; CHECK: fpmath accuracy not a positive number!
    1717 %y = fadd float %f, %f, !fpmath !5
    18 ; CHECK: fpmath ULPs is negative!
    18 ; CHECK: fpmath accuracy not a positive number!
    1919 %z = fadd float %f, %f, !fpmath !6
    20 ; CHECK: fpmath ULPs not a normal number!
    20 ; CHECK: fpmath accuracy not a positive number!
    21 ret void
    22 }
    23
    24 define void @fpmath2(float %f, <2 x float> %g) {
    25 %w = fadd float %f, %f, !fpmath !7
    26 ; Above line is correct.
    27 %w2 = fadd <2 x float> %g, %g, !fpmath !7
    28 ; Above line is correct.
    29 %x = fadd float %f, %f, !fpmath !8
    30 ; CHECK: wrong fpmath accuracy keyword!
    2131 ret void
    2232 }
    2333
    2636 !2 = metadata !{ float 1.0, float 1.0 }
    2737 !3 = metadata !{ i32 1 }
    2838 !4 = metadata !{ float -1.0 }
    29 !5 = metadata !{ float -0.0 }
    39 !5 = metadata !{ float 0.0 }
    3040 !6 = metadata !{ float 0x7FFFFFFF00000000 }
    41 !7 = metadata !{ metadata !"fast" }
    42 !8 = metadata !{ metadata !"slow" }
    77 //===----------------------------------------------------------------------===//
    88
    99 #include "gtest/gtest.h"
    10 #include "llvm/Operator.h"
    11 #include "llvm/Support/IRBuilder.h"
    1012 #include "llvm/Support/MDBuilder.h"
    1113 using namespace llvm;
    1214
    1719 LLVMContext Context;
    1820 };
    1921
    20 TEST_F(MDBuilderTest, CreateString) {
    22 TEST_F(MDBuilderTest, createString) {
    2123 MDBuilder MDHelper(Context);
    22 MDString *Str0 = MDHelper.CreateString("");
    23 MDString *Str1 = MDHelper.CreateString("string");
    24 MDString *Str0 = MDHelper.createString("");
    25 MDString *Str1 = MDHelper.createString("string");
    2426 EXPECT_EQ(Str0->getString(), StringRef(""));
    2527 EXPECT_EQ(Str1->getString(), StringRef("string"));
    2628 }
    27
    28 TEST_F(MDBuilderTest, CreateRangeMetadata) {
    29 TEST_F(MDBuilderTest, createFastFPMath) {
    30 MDBuilder MDHelper(Context);
    31 MDNode *MD = MDHelper.createFastFPMath();
    32 EXPECT_NE(MD, (MDNode *)0);
    33 EXPECT_EQ(MD->getNumOperands(), 1U);
    34 Value *Op = MD->getOperand(0);
    35 EXPECT_TRUE(isa(Op));
    36 EXPECT_EQ(cast(Op)->getString(), "fast");
    37 }
    38 TEST_F(MDBuilderTest, createFPMath) {
    39 MDBuilder MDHelper(Context);
    40 MDNode *MD0 = MDHelper.createFPMath(0.0);
    41 MDNode *MD1 = MDHelper.createFPMath(1.0);
    42 MDNode *MDF = MDHelper.createFPMath(-1.0);
    43 MDNode *MDF2 = MDHelper.createFastFPMath();
    44 EXPECT_EQ(MD0, (MDNode *)0);
    45 EXPECT_NE(MD1, (MDNode *)0);
    46 EXPECT_EQ(MDF, MDF2);
    47 EXPECT_EQ(MD1->getNumOperands(), 1U);
    48 Value *Op = MD1->getOperand(0);
    49 EXPECT_TRUE(isa(Op));
    50 EXPECT_TRUE(Op->getType()->isFloatingPointTy());
    51 ConstantFP *Val = cast(Op);
    52 EXPECT_TRUE(Val->isExactlyValue(1.0));
    53 }
    54 TEST_F(MDBuilderTest, createRangeMetadata) {
    2955 MDBuilder MDHelper(Context);
    3056 APInt A(8, 1), B(8, 2);
    31 MDNode *R0 = MDHelper.CreateRange(A, A);
    32 MDNode *R1 = MDHelper.CreateRange(A, B);
    57 MDNode *R0 = MDHelper.createRange(A, A);
    58 MDNode *R1 = MDHelper.createRange(A, B);
    3359 EXPECT_EQ(R0, (MDNode *)0);
    3460 EXPECT_NE(R1, (MDNode *)0);
    3561 EXPECT_EQ(R1->getNumOperands(), 2U);
    4066 EXPECT_EQ(C0->getValue(), A);
    4167 EXPECT_EQ(C1->getValue(), B);
    4268 }
    43 TEST_F(MDBuilderTest, CreateAnonymousTBAARoot) {
    69 TEST_F(MDBuilderTest, createAnonymousTBAARoot) {
    4470 MDBuilder MDHelper(Context);
    45 MDNode *R0 = MDHelper.CreateAnonymousTBAARoot();
    46 MDNode *R1 = MDHelper.CreateAnonymousTBAARoot();
    71 MDNode *R0 = MDHelper.createAnonymousTBAARoot();
    72 MDNode *R1 = MDHelper.createAnonymousTBAARoot();
    4773 EXPECT_NE(R0, R1);
    4874 EXPECT_GE(R0->getNumOperands(), 1U);
    4975 EXPECT_GE(R1->getNumOperands(), 1U);
    5278 EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
    5379 EXPECT_TRUE(R1->getNumOperands() == 1 || R1->getOperand(1) == 0);
    5480 }
    55 TEST_F(MDBuilderTest, CreateTBAARoot) {
    81 TEST_F(MDBuilderTest, createTBAARoot) {
    5682 MDBuilder MDHelper(Context);
    57 MDNode *R0 = MDHelper.CreateTBAARoot("Root");
    58 MDNode *R1 = MDHelper.CreateTBAARoot("Root");
    83 MDNode *R0 = MDHelper.createTBAARoot("Root");
    84 MDNode *R1 = MDHelper.createTBAARoot("Root");
    5985 EXPECT_EQ(R0, R1);
    6086 EXPECT_GE(R0->getNumOperands(), 1U);
    6187 EXPECT_TRUE(isa(R0->getOperand(0)));
    6288 EXPECT_EQ(cast(R0->getOperand(0))->getString(), "Root");
    6389 EXPECT_TRUE(R0->getNumOperands() == 1 || R0->getOperand(1) == 0);
    6490 }
    65 TEST_F(MDBuilderTest, CreateTBAANode) {
    91 TEST_F(MDBuilderTest, createTBAANode) {
    6692 MDBuilder MDHelper(Context);
    67 MDNode *R = MDHelper.CreateTBAARoot("Root");
    68 MDNode *N0 = MDHelper.CreateTBAANode("Node", R);
    69 MDNode *N1 = MDHelper.CreateTBAANode("edoN", R);
    70 MDNode *N2 = MDHelper.CreateTBAANode("Node", R, true);
    71 MDNode *N3 = MDHelper.CreateTBAANode("Node", R);
    93 MDNode *R = MDHelper.createTBAARoot("Root");
    94 MDNode *N0 = MDHelper.createTBAANode("Node", R);
    95 MDNode *N1 = MDHelper.createTBAANode("edoN", R);
    96 MDNode *N2 = MDHelper.createTBAANode("Node", R, true);
    97 MDNode *N3 = MDHelper.createTBAANode("Node", R);
    7298 EXPECT_EQ(N0, N3);
    7399 EXPECT_NE(N0, N1);
    74100 EXPECT_NE(N0, N2);
    1111 #include "llvm/Constants.h"
    1212 #include "llvm/DerivedTypes.h"
    1313 #include "llvm/LLVMContext.h"
    14 #include "llvm/Operator.h"
    1415 #include "llvm/ADT/STLExtras.h"
    1516 #include "llvm/Analysis/ValueTracking.h"
    17 #include "llvm/Support/MDBuilder.h"
    18 #include "llvm/Support/IRBuilder.h"
    1619 #include "llvm/Target/TargetData.h"
    1720 #include "gtest/gtest.h"
    1821
    225228 delete PtrVecB;
    226229 }
    227230
    231 TEST(InstructionsTest, FPMathOperator) {
    232 LLVMContext &Context = getGlobalContext();
    233 IRBuilder<> Builder(Context);
    234 MDBuilder MDHelper(Context);
    235 Instruction *I = Builder.CreatePHI(Builder.getDoubleTy(), 0);
    236 MDNode *MD1 = MDHelper.createFPMath(1.0);
    237 MDNode *MDF = MDHelper.createFastFPMath();
    238 Value *V1 = Builder.CreateFAdd(I, I, "", MD1);
    239 Value *VF = Builder.CreateFAdd(I, I, "", MDF);
    240 EXPECT_TRUE(isa(V1));
    241 EXPECT_TRUE(isa(VF));
    242 FPMathOperator *O1 = cast(V1);
    243 FPMathOperator *OF = cast(VF);
    244 EXPECT_FALSE(O1->isFastFPAccuracy());
    245 EXPECT_TRUE(OF->isFastFPAccuracy());
    246 EXPECT_EQ(O1->getFPAccuracy(), 1.0);
    247 EXPECT_GT(OF->getFPAccuracy(), 999.0);
    248 delete V1;
    249 delete VF;
    250 delete I;
    251 }
    252
    228253 } // end anonymous namespace
    229254 } // end namespace llvm