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 /// Flag bits.
31 enum {
32 AllowReassoc = (1 << 0),
33 NoNaNs = (1 << 1),
34 NoInfs = (1 << 2),
35 NoSignedZeros = (1 << 3),
36 AllowReciprocal = (1 << 4),
37 AllowContract = (1 << 5),
38 ApproxFunc = (1 << 6),
39 FlagEnd = (1 << 7)
40 };
41
42 FastMathFlags(unsigned F) : Flags(F) {
43 assert(((F & 0xff) == F) && "Flags value is not legal!");
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 bool operator==(const FastMathFlags &OtherFlags) const {
109 return Flags == OtherFlags.Flags;
110 }
111
112 /// Print fast-math flags to \p O.
113 LLVM_ABI void print(raw_ostream &O) const;
114
115 /// Intersect rewrite-based flags
118 const unsigned RewriteMask =
120 return FastMathFlags(RewriteMask & LHS.Flags & RHS.Flags);
121 }
122
123 /// Union value flags
125 const unsigned ValueMask = NoNaNs | NoInfs | NoSignedZeros;
126 return FastMathFlags(ValueMask & (LHS.Flags | RHS.Flags));
127 }
128};
129
131 LHS |= RHS;
132 return LHS;
133}
134
136 LHS &= RHS;
137 return LHS;
138}
139
141 FMF.print(O);
142 return O;
143}
144
145} // end namespace llvm
146
147#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:96
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:116
bool any() const
Definition FMF.h:56
void setAllowContract(bool B=true)
Definition FMF.h:90
friend class FPMathOperator
Definition FMF.h:25
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 operator==(const FastMathFlags &OtherFlags) const
Definition FMF.h:108
bool allowReciprocal() const
Definition FMF.h:68
void operator|=(const FastMathFlags &OtherFlags)
Definition FMF.h:101
static FastMathFlags unionValue(FastMathFlags LHS, FastMathFlags RHS)
Union value flags.
Definition FMF.h:124
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
FastMathFlags(unsigned F)
Definition FMF.h:42
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.
APInt operator&(APInt a, const APInt &b)
Definition APInt.h:2152
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
APInt operator|(APInt a, const APInt &b)
Definition APInt.h:2172