LLVM 22.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
18namespace llvm {
19class raw_ostream;
20
21/// Convenience struct for specifying and reasoning about fast-math flags.
23private:
24 friend class FPMathOperator;
25
26 unsigned Flags = 0;
27
28 FastMathFlags(unsigned F) : Flags(F) {}
29
30public:
31 // This is how the bits are used in Value::SubclassOptionalData so they
32 // should fit there too.
33 // WARNING: We're out of space. SubclassOptionalData only has 7 bits. New
34 // functionality will require a change in how this information is stored.
35 enum {
36 AllowReassoc = (1 << 0),
37 NoNaNs = (1 << 1),
38 NoInfs = (1 << 2),
39 NoSignedZeros = (1 << 3),
40 AllowReciprocal = (1 << 4),
41 AllowContract = (1 << 5),
42 ApproxFunc = (1 << 6),
43 FlagEnd = (1 << 7)
44 };
45
46 constexpr static unsigned AllFlagsMask = FlagEnd - 1;
47
48 FastMathFlags() = default;
49
51 FastMathFlags FMF;
52 FMF.setFast();
53 return FMF;
54 }
55
56 bool any() const { return Flags != 0; }
57 bool none() const { return Flags == 0; }
58 bool all() const { return Flags == AllFlagsMask; }
59
60 void clear() { Flags = 0; }
61 void set() { Flags = AllFlagsMask; }
62
63 /// Flag queries
64 bool allowReassoc() const { return 0 != (Flags & AllowReassoc); }
65 bool noNaNs() const { return 0 != (Flags & NoNaNs); }
66 bool noInfs() const { return 0 != (Flags & NoInfs); }
67 bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
68 bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
69 bool allowContract() const { return 0 != (Flags & AllowContract); }
70 bool approxFunc() const { return 0 != (Flags & ApproxFunc); }
71 /// 'Fast' means all bits are set.
72 bool isFast() const { return all(); }
73
74 /// Flag setters
75 void setAllowReassoc(bool B = true) {
76 Flags = (Flags & ~AllowReassoc) | B * AllowReassoc;
77 }
78 void setNoNaNs(bool B = true) {
79 Flags = (Flags & ~NoNaNs) | B * NoNaNs;
80 }
81 void setNoInfs(bool B = true) {
82 Flags = (Flags & ~NoInfs) | B * NoInfs;
83 }
84 void setNoSignedZeros(bool B = true) {
85 Flags = (Flags & ~NoSignedZeros) | B * NoSignedZeros;
86 }
87 void setAllowReciprocal(bool B = true) {
88 Flags = (Flags & ~AllowReciprocal) | B * AllowReciprocal;
89 }
90 void setAllowContract(bool B = true) {
91 Flags = (Flags & ~AllowContract) | B * AllowContract;
92 }
93 void setApproxFunc(bool B = true) {
94 Flags = (Flags & ~ApproxFunc) | B * ApproxFunc;
95 }
96 void setFast(bool B = true) { B ? set() : clear(); }
97
98 void operator&=(const FastMathFlags &OtherFlags) {
99 Flags &= OtherFlags.Flags;
100 }
101 void operator|=(const FastMathFlags &OtherFlags) {
102 Flags |= OtherFlags.Flags;
103 }
104 bool operator!=(const FastMathFlags &OtherFlags) const {
105 return Flags != OtherFlags.Flags;
106 }
107
108 /// Print fast-math flags to \p O.
109 LLVM_ABI void print(raw_ostream &O) const;
110
111 /// Intersect rewrite-based flags
114 const unsigned RewriteMask =
116 return FastMathFlags(RewriteMask & LHS.Flags & RHS.Flags);
117 }
118
119 /// Union value flags
121 const unsigned ValueMask = NoNaNs | NoInfs | NoSignedZeros;
122 return FastMathFlags(ValueMask & (LHS.Flags | RHS.Flags));
123 }
124};
125
127 LHS |= RHS;
128 return LHS;
129}
130
132 LHS &= RHS;
133 return LHS;
134}
135
137 FMF.print(O);
138 return O;
139}
140
141} // end namespace llvm
142
143#endif // LLVM_IR_FMF_H
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ABI
Definition: Compiler.h:213
#define F(x, y, z)
Definition: MD5.cpp:55
Value * RHS
Value * LHS
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:22
void setFast(bool B=true)
Definition: FMF.h:96
static FastMathFlags intersectRewrite(FastMathFlags LHS, FastMathFlags RHS)
Intersect rewrite-based flags.
Definition: FMF.h:112
bool any() const
Definition: FMF.h:56
void setAllowContract(bool B=true)
Definition: FMF.h:90
bool noSignedZeros() const
Definition: FMF.h:67
void clear()
Definition: FMF.h:60
bool noInfs() const
Definition: FMF.h:66
bool none() const
Definition: FMF.h:57
bool all() const
Definition: FMF.h:58
void setAllowReciprocal(bool B=true)
Definition: FMF.h:87
bool allowReciprocal() const
Definition: FMF.h:68
void operator|=(const FastMathFlags &OtherFlags)
Definition: FMF.h:101
LLVM_ABI void print(raw_ostream &O) const
Print fast-math flags to O.
Definition: Operator.cpp:271
void set()
Definition: FMF.h:61
static FastMathFlags unionValue(FastMathFlags LHS, FastMathFlags RHS)
Union value flags.
Definition: FMF.h:120
void setNoSignedZeros(bool B=true)
Definition: FMF.h:84
bool allowReassoc() const
Flag queries.
Definition: FMF.h:64
static constexpr unsigned AllFlagsMask
Definition: FMF.h:46
bool approxFunc() const
Definition: FMF.h:70
void setNoNaNs(bool B=true)
Definition: FMF.h:78
void setAllowReassoc(bool B=true)
Flag setters.
Definition: FMF.h:75
bool noNaNs() const
Definition: FMF.h:65
void setApproxFunc(bool B=true)
Definition: FMF.h:93
static FastMathFlags getFast()
Definition: FMF.h:50
void setNoInfs(bool B=true)
Definition: FMF.h:81
FastMathFlags()=default
bool allowContract() const
Definition: FMF.h:69
bool operator!=(const FastMathFlags &OtherFlags) const
Definition: FMF.h:104
bool isFast() const
'Fast' means all bits are set.
Definition: FMF.h:72
void operator&=(const FastMathFlags &OtherFlags)
Definition: FMF.h:98
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
APInt operator&(APInt a, const APInt &b)
Definition: APInt.h:2123
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312
APInt operator|(APInt a, const APInt &b)
Definition: APInt.h:2143