FMF.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/FMF.h - Fast math flags subclass -------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the fast math flags.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_IR_FMF_H
  18. #define LLVM_IR_FMF_H
  19. #include "llvm/Support/raw_ostream.h"
  20. namespace llvm {
  21. /// Convenience struct for specifying and reasoning about fast-math flags.
  22. class FastMathFlags {
  23. private:
  24. friend class FPMathOperator;
  25. unsigned Flags = 0;
  26. FastMathFlags(unsigned F) {
  27. // If all 7 bits are set, turn this into -1. If the number of bits grows,
  28. // this must be updated. This is intended to provide some forward binary
  29. // compatibility insurance for the meaning of 'fast' in case bits are added.
  30. if (F == 0x7F) Flags = ~0U;
  31. else Flags = F;
  32. }
  33. public:
  34. // This is how the bits are used in Value::SubclassOptionalData so they
  35. // should fit there too.
  36. // WARNING: We're out of space. SubclassOptionalData only has 7 bits. New
  37. // functionality will require a change in how this information is stored.
  38. enum {
  39. AllowReassoc = (1 << 0),
  40. NoNaNs = (1 << 1),
  41. NoInfs = (1 << 2),
  42. NoSignedZeros = (1 << 3),
  43. AllowReciprocal = (1 << 4),
  44. AllowContract = (1 << 5),
  45. ApproxFunc = (1 << 6)
  46. };
  47. FastMathFlags() = default;
  48. static FastMathFlags getFast() {
  49. FastMathFlags FMF;
  50. FMF.setFast();
  51. return FMF;
  52. }
  53. bool any() const { return Flags != 0; }
  54. bool none() const { return Flags == 0; }
  55. bool all() const { return Flags == ~0U; }
  56. void clear() { Flags = 0; }
  57. void set() { Flags = ~0U; }
  58. /// Flag queries
  59. bool allowReassoc() const { return 0 != (Flags & AllowReassoc); }
  60. bool noNaNs() const { return 0 != (Flags & NoNaNs); }
  61. bool noInfs() const { return 0 != (Flags & NoInfs); }
  62. bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
  63. bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
  64. bool allowContract() const { return 0 != (Flags & AllowContract); }
  65. bool approxFunc() const { return 0 != (Flags & ApproxFunc); }
  66. /// 'Fast' means all bits are set.
  67. bool isFast() const { return all(); }
  68. /// Flag setters
  69. void setAllowReassoc(bool B = true) {
  70. Flags = (Flags & ~AllowReassoc) | B * AllowReassoc;
  71. }
  72. void setNoNaNs(bool B = true) {
  73. Flags = (Flags & ~NoNaNs) | B * NoNaNs;
  74. }
  75. void setNoInfs(bool B = true) {
  76. Flags = (Flags & ~NoInfs) | B * NoInfs;
  77. }
  78. void setNoSignedZeros(bool B = true) {
  79. Flags = (Flags & ~NoSignedZeros) | B * NoSignedZeros;
  80. }
  81. void setAllowReciprocal(bool B = true) {
  82. Flags = (Flags & ~AllowReciprocal) | B * AllowReciprocal;
  83. }
  84. void setAllowContract(bool B = true) {
  85. Flags = (Flags & ~AllowContract) | B * AllowContract;
  86. }
  87. void setApproxFunc(bool B = true) {
  88. Flags = (Flags & ~ApproxFunc) | B * ApproxFunc;
  89. }
  90. void setFast(bool B = true) { B ? set() : clear(); }
  91. void operator&=(const FastMathFlags &OtherFlags) {
  92. Flags &= OtherFlags.Flags;
  93. }
  94. void operator|=(const FastMathFlags &OtherFlags) {
  95. Flags |= OtherFlags.Flags;
  96. }
  97. bool operator!=(const FastMathFlags &OtherFlags) const {
  98. return Flags != OtherFlags.Flags;
  99. }
  100. /// Print fast-math flags to \p O.
  101. void print(raw_ostream &O) const;
  102. };
  103. inline raw_ostream &operator<<(raw_ostream &O, FastMathFlags FMF) {
  104. FMF.print(O);
  105. return O;
  106. }
  107. } // end namespace llvm
  108. #endif // LLVM_IR_FMF_H
  109. #ifdef __GNUC__
  110. #pragma GCC diagnostic pop
  111. #endif