LLVM 23.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#include <cassert>
18
19namespace llvm {
20class raw_ostream;
21
22/// Convenience struct for specifying and reasoning about fast-math flags.
24private:
25 friend class FPMathOperator;
26
27 unsigned Flags = 0;
28
29public:
30 // This is how the bits are used in Value::SubclassOptionalData so they
31 // should fit there too.
32 // WARNING: We're out of space. SubclassOptionalData only has 7 bits. New
33 // functionality will require a change in how this information is stored.
34 enum {
35 AllowReassoc = (1 << 0),
36 NoNaNs = (1 << 1),
37 NoInfs = (1 << 2),
38 NoSignedZeros = (1 << 3),
39 AllowReciprocal = (1 << 4),
40 AllowContract = (1 << 5),
41 ApproxFunc = (1 << 6),
42 FlagEnd = (1 << 7)
43 };
44
45 FastMathFlags(unsigned F) : Flags(F) {
46 assert(((F & 0xff) == F) && "Flags value is not legal!");
47 }
48
49 constexpr static unsigned AllFlagsMask = FlagEnd - 1;
50
51 FastMathFlags() = default;
52
54 FastMathFlags FMF;
55 FMF.setFast();
56 return FMF;
57 }
58
59 bool any() const { return Flags != 0; }
60 bool none() const { return Flags == 0; }
61 bool all() const { return Flags == AllFlagsMask; }
62
63 void clear() { Flags = 0; }
64 void set() { Flags = AllFlagsMask; }
65
66 /// Flag queries
67 bool allowReassoc() const { return 0 != (Flags & AllowReassoc); }
68 bool noNaNs() const { return 0 != (Flags & NoNaNs); }
69 bool noInfs() const { return 0 != (Flags & NoInfs); }
70 bool noSignedZeros() const { return 0 != (Flags & NoSignedZeros); }
71 bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
72 bool allowContract() const { return 0 != (Flags & AllowContract); }
73 bool approxFunc() const { return 0 != (Flags & ApproxFunc); }
74 /// 'Fast' means all bits are set.
75 bool isFast() const { return all(); }
76
77 /// Flag setters
78 void setAllowReassoc(bool B = true) {
79 Flags = (Flags & ~AllowReassoc) | B * AllowReassoc;
80 }
81 void setNoNaNs(bool B = true) {
82 Flags = (Flags & ~NoNaNs) | B * NoNaNs;
83 }
84 void setNoInfs(bool B = true) {
85 Flags = (Flags & ~NoInfs) | B * NoInfs;
86 }
87 void setNoSignedZeros(bool B = true) {
88 Flags = (Flags & ~NoSignedZeros) | B * NoSignedZeros;
89 }
90 void setAllowReciprocal(bool B = true) {
91 Flags = (Flags & ~AllowReciprocal) | B * AllowReciprocal;
92 }
93 void setAllowContract(bool B = true) {
94 Flags = (Flags & ~AllowContract) | B * AllowContract;
95 }
96 void setApproxFunc(bool B = true) {
97 Flags = (Flags & ~ApproxFunc) | B * ApproxFunc;
98 }
99 void setFast(bool B = true) { B ? set() : clear(); }
100
101 void operator&=(const FastMathFlags &OtherFlags) {
102 Flags &= OtherFlags.Flags;
103 }
104 void operator|=(const FastMathFlags &OtherFlags) {
105 Flags |= OtherFlags.Flags;
106 }
107 bool operator!=(const FastMathFlags &OtherFlags) const {
108 return Flags != OtherFlags.Flags;
109 }
110
111 bool operator==(const FastMathFlags &OtherFlags) const {
112 return Flags == OtherFlags.Flags;
113 }
114
115 /// Print fast-math flags to \p O.
116 LLVM_ABI void print(raw_ostream &O) const;
117
118 /// Intersect rewrite-based flags
121 const unsigned RewriteMask =
123 return FastMathFlags(RewriteMask & LHS.Flags & RHS.Flags);
124 }
125
126 /// Union value flags
128 const unsigned ValueMask = NoNaNs | NoInfs | NoSignedZeros;
129 return FastMathFlags(ValueMask & (LHS.Flags | RHS.Flags));
130 }
131};
132
134 LHS |= RHS;
135 return LHS;
136}
137
139 LHS &= RHS;
140 return LHS;
141}
142
144 FMF.print(O);
145 return O;
146}
147
148} // end namespace llvm
149
150#endif // LLVM_IR_FMF_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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:54
Value * RHS
Value * LHS
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
void setFast(bool B=true)
Definition FMF.h:99
LLVM_ABI void print(raw_ostream &O) const
Print fast-math flags to O.
Definition Operator.cpp:283
static FastMathFlags intersectRewrite(FastMathFlags LHS, FastMathFlags RHS)
Intersect rewrite-based flags.
Definition FMF.h:119
bool any() const
Definition FMF.h:59
void setAllowContract(bool B=true)
Definition FMF.h:93
friend class FPMathOperator
Definition FMF.h:25
bool noSignedZeros() const
Definition FMF.h:70
void clear()
Definition FMF.h:63
bool noInfs() const
Definition FMF.h:69
bool none() const
Definition FMF.h:60
bool all() const
Definition FMF.h:61
void setAllowReciprocal(bool B=true)
Definition FMF.h:90
bool operator==(const FastMathFlags &OtherFlags) const
Definition FMF.h:111
bool allowReciprocal() const
Definition FMF.h:71
void operator|=(const FastMathFlags &OtherFlags)
Definition FMF.h:104
static FastMathFlags unionValue(FastMathFlags LHS, FastMathFlags RHS)
Union value flags.
Definition FMF.h:127
void setNoSignedZeros(bool B=true)
Definition FMF.h:87
bool allowReassoc() const
Flag queries.
Definition FMF.h:67
static constexpr unsigned AllFlagsMask
Definition FMF.h:49
bool approxFunc() const
Definition FMF.h:73
void setNoNaNs(bool B=true)
Definition FMF.h:81
void setAllowReassoc(bool B=true)
Flag setters.
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:68
void setApproxFunc(bool B=true)
Definition FMF.h:96
static FastMathFlags getFast()
Definition FMF.h:53
void setNoInfs(bool B=true)
Definition FMF.h:84
FastMathFlags()=default
bool allowContract() const
Definition FMF.h:72
bool operator!=(const FastMathFlags &OtherFlags) const
Definition FMF.h:107
FastMathFlags(unsigned F)
Definition FMF.h:45
bool isFast() const
'Fast' means all bits are set.
Definition FMF.h:75
void operator&=(const FastMathFlags &OtherFlags)
Definition FMF.h:101
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 Types.h:26
APInt operator&(APInt a, const APInt &b)
Definition APInt.h:2138
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
APInt operator|(APInt a, const APInt &b)
Definition APInt.h:2158