LLVM  15.0.0git
FMF.h
Go to the documentation of this file.
1 //===-- llvm/FMF.h - Fast math flags subclass -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the fast math flags.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_IR_FMF_H
14 #define LLVM_IR_FMF_H
15 
17 
18 namespace llvm {
19 
20 /// Convenience struct for specifying and reasoning about fast-math flags.
22 private:
23  friend class FPMathOperator;
24 
25  unsigned Flags = 0;
26 
27  FastMathFlags(unsigned F) {
28  // If all 7 bits are set, turn this into -1. If the number of bits grows,
29  // this must be updated. This is intended to provide some forward binary
30  // compatibility insurance for the meaning of 'fast' in case bits are added.
31  if (F == 0x7F) Flags = ~0U;
32  else Flags = F;
33  }
34 
35 public:
36  // This is how the bits are used in Value::SubclassOptionalData so they
37  // should fit there too.
38  // WARNING: We're out of space. SubclassOptionalData only has 7 bits. New
39  // functionality will require a change in how this information is stored.
40  enum {
41  AllowReassoc = (1 << 0),
42  NoNaNs = (1 << 1),
43  NoInfs = (1 << 2),
44  NoSignedZeros = (1 << 3),
45  AllowReciprocal = (1 << 4),
46  AllowContract = (1 << 5),
47  ApproxFunc = (1 << 6)
48  };
49 
50  FastMathFlags() = default;
51 
53  FastMathFlags FMF;
54  FMF.setFast();
55  return FMF;
56  }
57 
58  bool any() const { return Flags != 0; }
59  bool none() const { return Flags == 0; }
60  bool all() const { return Flags == ~0U; }
61 
62  void clear() { Flags = 0; }
63  void set() { Flags = ~0U; }
64 
65  /// Flag queries
66  bool allowReassoc() const { return 0 != (Flags & AllowReassoc); }
67  bool noNaNs() const { return 0 != (Flags & NoNaNs); }
68  bool noInfs() const { return 0 != (Flags & NoInfs); }
69  bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
70  bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
71  bool allowContract() const { return 0 != (Flags & AllowContract); }
72  bool approxFunc() const { return 0 != (Flags & ApproxFunc); }
73  /// 'Fast' means all bits are set.
74  bool isFast() const { return all(); }
75 
76  /// Flag setters
77  void setAllowReassoc(bool B = true) {
78  Flags = (Flags & ~AllowReassoc) | B * AllowReassoc;
79  }
80  void setNoNaNs(bool B = true) {
81  Flags = (Flags & ~NoNaNs) | B * NoNaNs;
82  }
83  void setNoInfs(bool B = true) {
84  Flags = (Flags & ~NoInfs) | B * NoInfs;
85  }
86  void setNoSignedZeros(bool B = true) {
87  Flags = (Flags & ~NoSignedZeros) | B * NoSignedZeros;
88  }
89  void setAllowReciprocal(bool B = true) {
90  Flags = (Flags & ~AllowReciprocal) | B * AllowReciprocal;
91  }
92  void setAllowContract(bool B = true) {
93  Flags = (Flags & ~AllowContract) | B * AllowContract;
94  }
95  void setApproxFunc(bool B = true) {
96  Flags = (Flags & ~ApproxFunc) | B * ApproxFunc;
97  }
98  void setFast(bool B = true) { B ? set() : clear(); }
99 
100  void operator&=(const FastMathFlags &OtherFlags) {
101  Flags &= OtherFlags.Flags;
102  }
103  void operator|=(const FastMathFlags &OtherFlags) {
104  Flags |= OtherFlags.Flags;
105  }
106  bool operator!=(const FastMathFlags &OtherFlags) const {
107  return Flags != OtherFlags.Flags;
108  }
109 
110  /// Print fast-math flags to \p O.
111  void print(raw_ostream &O) const;
112 };
113 
115  FMF.print(O);
116  return O;
117 }
118 
119 } // end namespace llvm
120 
121 #endif // LLVM_IR_FMF_H
llvm::FastMathFlags::NoSignedZeros
@ NoSignedZeros
Definition: FMF.h:44
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::FastMathFlags::getFast
static FastMathFlags getFast()
Definition: FMF.h:52
llvm::FastMathFlags::setNoSignedZeros
void setNoSignedZeros(bool B=true)
Definition: FMF.h:86
llvm::FastMathFlags::setAllowReciprocal
void setAllowReciprocal(bool B=true)
Definition: FMF.h:89
llvm::FastMathFlags::noSignedZeros
bool noSignedZeros() const
Definition: FMF.h:69
llvm::FastMathFlags::operator!=
bool operator!=(const FastMathFlags &OtherFlags) const
Definition: FMF.h:106
llvm::FastMathFlags::operator&=
void operator&=(const FastMathFlags &OtherFlags)
Definition: FMF.h:100
llvm::FastMathFlags::none
bool none() const
Definition: FMF.h:59
llvm::FastMathFlags
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:21
llvm::FastMathFlags::noNaNs
bool noNaNs() const
Definition: FMF.h:67
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::FastMathFlags::FastMathFlags
FastMathFlags()=default
llvm::FastMathFlags::setApproxFunc
void setApproxFunc(bool B=true)
Definition: FMF.h:95
llvm::FastMathFlags::any
bool any() const
Definition: FMF.h:58
llvm::FastMathFlags::approxFunc
bool approxFunc() const
Definition: FMF.h:72
llvm::FastMathFlags::print
void print(raw_ostream &O) const
Print fast-math flags to O.
Definition: Operator.cpp:230
llvm::FastMathFlags::allowReassoc
bool allowReassoc() const
Flag queries.
Definition: FMF.h:66
llvm::FastMathFlags::allowReciprocal
bool allowReciprocal() const
Definition: FMF.h:70
llvm::FastMathFlags::AllowReassoc
@ AllowReassoc
Definition: FMF.h:41
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:230
llvm::FastMathFlags::operator|=
void operator|=(const FastMathFlags &OtherFlags)
Definition: FMF.h:103
llvm::FastMathFlags::NoNaNs
@ NoNaNs
Definition: FMF.h:42
llvm::FastMathFlags::setAllowContract
void setAllowContract(bool B=true)
Definition: FMF.h:92
llvm::FastMathFlags::set
void set()
Definition: FMF.h:63
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:240
llvm::FastMathFlags::noInfs
bool noInfs() const
Definition: FMF.h:68
llvm::FastMathFlags::NoInfs
@ NoInfs
Definition: FMF.h:43
llvm::FPMathOperator
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:167
llvm::FastMathFlags::setAllowReassoc
void setAllowReassoc(bool B=true)
Flag setters.
Definition: FMF.h:77
llvm::FastMathFlags::setFast
void setFast(bool B=true)
Definition: FMF.h:98
llvm::FastMathFlags::setNoInfs
void setNoInfs(bool B=true)
Definition: FMF.h:83
llvm::FastMathFlags::clear
void clear()
Definition: FMF.h:62
llvm::FastMathFlags::isFast
bool isFast() const
'Fast' means all bits are set.
Definition: FMF.h:74
llvm::FastMathFlags::AllowReciprocal
@ AllowReciprocal
Definition: FMF.h:45
llvm::FastMathFlags::all
bool all() const
Definition: FMF.h:60
llvm::FastMathFlags::allowContract
bool allowContract() const
Definition: FMF.h:71
raw_ostream.h
llvm::FastMathFlags::AllowContract
@ AllowContract
Definition: FMF.h:46
llvm::FastMathFlags::ApproxFunc
@ ApproxFunc
Definition: FMF.h:47
llvm::FastMathFlags::setNoNaNs
void setNoNaNs(bool B=true)
Definition: FMF.h:80