LLVM 17.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 {
19
20/// Convenience struct for specifying and reasoning about fast-math flags.
22private:
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
35public:
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
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define F(x, y, z)
Definition: MD5.cpp:55
@ Flags
Definition: TextStubV5.cpp:93
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:170
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:21
void setFast(bool B=true)
Definition: FMF.h:98
bool any() const
Definition: FMF.h:58
void setAllowContract(bool B=true)
Definition: FMF.h:92
bool noSignedZeros() const
Definition: FMF.h:69
void clear()
Definition: FMF.h:62
bool noInfs() const
Definition: FMF.h:68
bool none() const
Definition: FMF.h:59
bool all() const
Definition: FMF.h:60
void setAllowReciprocal(bool B=true)
Definition: FMF.h:89
bool allowReciprocal() const
Definition: FMF.h:70
void operator|=(const FastMathFlags &OtherFlags)
Definition: FMF.h:103
void print(raw_ostream &O) const
Print fast-math flags to O.
Definition: Operator.cpp:235
void set()
Definition: FMF.h:63
void setNoSignedZeros(bool B=true)
Definition: FMF.h:86
bool allowReassoc() const
Flag queries.
Definition: FMF.h:66
bool approxFunc() const
Definition: FMF.h:72
void setNoNaNs(bool B=true)
Definition: FMF.h:80
void setAllowReassoc(bool B=true)
Flag setters.
Definition: FMF.h:77
bool noNaNs() const
Definition: FMF.h:67
void setApproxFunc(bool B=true)
Definition: FMF.h:95
static FastMathFlags getFast()
Definition: FMF.h:52
void setNoInfs(bool B=true)
Definition: FMF.h:83
FastMathFlags()=default
bool allowContract() const
Definition: FMF.h:71
bool operator!=(const FastMathFlags &OtherFlags) const
Definition: FMF.h:106
bool isFast() const
'Fast' means all bits are set.
Definition: FMF.h:74
void operator&=(const FastMathFlags &OtherFlags)
Definition: FMF.h:100
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:292