LLVM 22.0.0git
KnownFPClass.h
Go to the documentation of this file.
1//===- llvm/Support/KnownFPClass.h - Stores known fpclass -------*- 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 contains a class for representing known fpclasses used by
10// computeKnownFPClass.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_KNOWNFPCLASS_H
15#define LLVM_SUPPORT_KNOWNFPCLASS_H
16
19#include <optional>
20
21namespace llvm {
22class APFloat;
23struct fltSemantics;
24
26 /// Floating-point classes the value could be one of.
28
29 /// std::nullopt if the sign bit is unknown, true if the sign bit is
30 /// definitely set or false if the sign bit is definitely unset.
31 std::optional<bool> SignBit;
32
33 KnownFPClass(FPClassTest Known = fcAllFlags, std::optional<bool> Sign = {})
34 : KnownFPClasses(Known), SignBit(Sign) {}
35 KnownFPClass(const APFloat &C);
36
38 return KnownFPClasses == Other.KnownFPClasses && SignBit == Other.SignBit;
39 }
40
41 /// Return true if it's known this can never be one of the mask entries.
42 bool isKnownNever(FPClassTest Mask) const {
43 return (KnownFPClasses & Mask) == fcNone;
44 }
45
46 bool isKnownAlways(FPClassTest Mask) const { return isKnownNever(~Mask); }
47
48 bool isUnknown() const { return KnownFPClasses == fcAllFlags && !SignBit; }
49
50 /// Return true if it's known this can never be a nan.
51 bool isKnownNeverNaN() const { return isKnownNever(fcNan); }
52
53 /// Return true if it's known this must always be a nan.
54 bool isKnownAlwaysNaN() const { return isKnownAlways(fcNan); }
55
56 /// Return true if it's known this can never be an infinity.
57 bool isKnownNeverInfinity() const { return isKnownNever(fcInf); }
58
59 /// Return true if it's known this can never be an infinity or nan
60 bool isKnownNeverInfOrNaN() const { return isKnownNever(fcInf | fcNan); }
61
62 /// Return true if it's known this can never be +infinity.
64
65 /// Return true if it's known this can never be -infinity.
67
68 /// Return true if it's known this can never be a subnormal
70
71 /// Return true if it's known this can never be a positive subnormal
73
74 /// Return true if it's known this can never be a negative subnormal
76
77 /// Return true if it's known this can never be a zero. This means a literal
78 /// [+-]0, and does not include denormal inputs implicitly treated as [+-]0.
79 bool isKnownNeverZero() const { return isKnownNever(fcZero); }
80
81 /// Return true if it's known this can never be a literal positive zero.
82 bool isKnownNeverPosZero() const { return isKnownNever(fcPosZero); }
83
84 /// Return true if it's known this can never be a negative zero. This means a
85 /// literal -0 and does not include denormal inputs implicitly treated as -0.
86 bool isKnownNeverNegZero() const { return isKnownNever(fcNegZero); }
87
88 /// Return true if it's known this can never be interpreted as a zero. This
89 /// extends isKnownNeverZero to cover the case where the assumed
90 /// floating-point mode for the function interprets denormals as zero.
92
93 /// Return true if it's known this can never be interpreted as a negative
94 /// zero.
96
97 /// Return true if it's known this can never be interpreted as a positive
98 /// zero.
100
105
106 /// Return true if we can prove that the analyzed floating-point value is
107 /// either NaN or never less than -0.0.
108 ///
109 /// NaN --> true
110 /// +0 --> true
111 /// -0 --> true
112 /// x > +0 --> true
113 /// x < -0 --> false
117
118 /// Return true if we can prove that the analyzed floating-point value is
119 /// either NaN or never greater than -0.0.
120 /// NaN --> true
121 /// +0 --> true
122 /// -0 --> true
123 /// x > +0 --> false
124 /// x < -0 --> true
128
129 /// Return true if it's know this can never be a negative value or a logical
130 /// 0.
131 ///
132 /// NaN --> true
133 /// x >= -0 --> false
134 /// nsub --> true if mode is ieee, false otherwise.
135 /// x < -0 --> true
139
141 return KnownFPClass(~(~KnownFPClasses & ~RHS.KnownFPClasses),
142 SignBit == RHS.SignBit ? SignBit : std::nullopt);
143 }
144
146 KnownFPClasses = KnownFPClasses | RHS.KnownFPClasses;
147
148 if (SignBit != RHS.SignBit)
149 SignBit = std::nullopt;
150 return *this;
151 }
152
153 void knownNot(FPClassTest RuleOut) {
154 KnownFPClasses = KnownFPClasses & ~RuleOut;
155 if (isKnownNever(fcNan) && !SignBit) {
157 SignBit = false;
158 else if (isKnownNever(fcPositive))
159 SignBit = true;
160 }
161 }
162
163 void fneg() {
165 if (SignBit)
166 SignBit = !*SignBit;
167 }
168
184
185 // Enum of min/max intrinsics to avoid dependency on IR.
194
198
199 /// Apply the canonicalize intrinsic to this value. This is essentially a
200 /// stronger form of propagateCanonicalizingSrc.
202 canonicalize(const KnownFPClass &Src,
204
205 /// Report known values for fmul
207 fmul(const KnownFPClass &LHS, const KnownFPClass &RHS,
209
210 // Special case of fmul x, x.
213 KnownFPClass Known = fmul(Src, Src, Mode);
214
215 // X * X is always non-negative or a NaN.
216 Known.knownNot(fcNegative);
217 return Known;
218 }
219
220 /// Report known values for exp, exp2 and exp10.
221 LLVM_ABI static KnownFPClass exp(const KnownFPClass &Src);
222
223 /// Return true if the sign bit must be 0, ignoring the sign of nans.
225
226 /// Assume the sign bit is zero.
229 SignBit = false;
230 }
231
232 /// Assume the sign bit is one.
235 SignBit = true;
236 }
237
238 void copysign(const KnownFPClass &Sign) {
239 // Don't know anything about the sign of the source. Expand the possible set
240 // to its opposite sign pair.
247 if (KnownFPClasses & fcInf)
249
250 // Sign bit is exactly preserved even for nans.
251 SignBit = Sign.SignBit;
252
253 // Clear sign bits based on the input sign mask.
254 if (Sign.isKnownNever(fcPositive | fcNan) || (SignBit && *SignBit))
256 if (Sign.isKnownNever(fcNegative | fcNan) || (SignBit && !*SignBit))
258 }
259
260 // Propagate knowledge that a non-NaN source implies the result can also not
261 // be a NaN. For unconstrained operations, signaling nans are not guaranteed
262 // to be quieted but cannot be introduced.
263 void propagateNaN(const KnownFPClass &Src, bool PreserveSign = false) {
264 if (Src.isKnownNever(fcNan)) {
266 if (PreserveSign)
267 SignBit = Src.SignBit;
268 } else if (Src.isKnownNever(fcSNan))
270 }
271
272 /// Propagate knowledge from a source value that could be a denormal or
273 /// zero. We have to be conservative since output flushing is not guaranteed,
274 /// so known-never-zero may not hold.
275 ///
276 /// This assumes a copy-like operation and will replace any currently known
277 /// information.
279
280 /// Report known classes if \p Src is evaluated through a potentially
281 /// canonicalizing operation. We can assume signaling nans will not be
282 /// introduced, but cannot assume a denormal will be flushed under FTZ/DAZ.
283 ///
284 /// This assumes a copy-like operation and will replace any currently known
285 /// information.
288
289 /// Propagate known class for log/log2/log10
292
293 /// Propagate known class for sqrt
296
297 /// Propagate known class for fpext.
298 static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc,
299 const fltSemantics &DstTy,
300 const fltSemantics &SrcTy);
301
302 void resetAll() { *this = KnownFPClass(); }
303};
304
306 LHS |= RHS;
307 return LHS;
308}
309
311 RHS |= LHS;
312 return std::move(RHS);
313}
314
315} // namespace llvm
316
317#endif
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:213
Utilities for dealing with flags related to floating point properties and mode controls.
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
Value * RHS
Value * LHS
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI FPClassTest fneg(FPClassTest Mask)
Return the test mask which returns true if the value's sign bit is flipped.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
@ Other
Any other memory.
Definition ModRef.h:68
APInt operator|(APInt a, const APInt &b)
Definition APInt.h:2144
Represent subnormal handling kind for floating point instruction inputs and outputs.
static constexpr DenormalMode getDynamic()
bool isKnownNeverInfOrNaN() const
Return true if it's known this can never be an infinity or nan.
bool isKnownAlwaysNaN() const
Return true if it's known this must always be a nan.
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
KnownFPClass(FPClassTest Known=fcAllFlags, std::optional< bool > Sign={})
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
static LLVM_ABI KnownFPClass fmul(const KnownFPClass &LHS, const KnownFPClass &RHS, DenormalMode Mode=DenormalMode::getDynamic())
Report known values for fmul.
bool isKnownNeverZero() const
Return true if it's known this can never be a zero.
void copysign(const KnownFPClass &Sign)
static KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
bool isKnownAlways(FPClassTest Mask) const
static LLVM_ABI KnownFPClass canonicalize(const KnownFPClass &Src, DenormalMode DenormMode=DenormalMode::getDynamic())
Apply the canonicalize intrinsic to this value.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a zero.
static LLVM_ABI KnownFPClass log(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for log/log2/log10.
LLVM_ABI void propagateDenormal(const KnownFPClass &Src, DenormalMode Mode)
Propagate knowledge from a source value that could be a denormal or zero.
KnownFPClass & operator|=(const KnownFPClass &RHS)
KnownFPClass intersectWith(const KnownFPClass &RHS)
static LLVM_ABI KnownFPClass minMaxLike(const KnownFPClass &LHS, const KnownFPClass &RHS, MinMaxKind Kind, DenormalMode DenormMode=DenormalMode::getDynamic())
bool isUnknown() const
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
static LLVM_ABI KnownFPClass fpext(const KnownFPClass &KnownSrc, const fltSemantics &DstTy, const fltSemantics &SrcTy)
Propagate known class for fpext.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
LLVM_ABI void propagateCanonicalizingSrc(const KnownFPClass &Src, DenormalMode Mode)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void signBitMustBeZero()
Assume the sign bit is zero.
static LLVM_ABI KnownFPClass sqrt(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
Propagate known class for sqrt.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
bool cannotBeOrderedGreaterEqZero(DenormalMode Mode) const
Return true if it's know this can never be a negative value or a logical 0.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a negative zero.
bool operator==(KnownFPClass Other) const
bool signBitIsZeroOrNaN() const
Return true if the sign bit must be 0, ignoring the sign of nans.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.