LLVM 24.0.0git
ConstantFPRange.h
Go to the documentation of this file.
1//===- ConstantFPRange.h - Represent a range for floating-point -*- 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// Represent a range of possible values that may occur when the program is run
10// for a floating-point value. This keeps track of a lower and upper bound for
11// the constant.
12//
13// Range = [Lower, Upper] U (MayBeQNaN ? QNaN : {}) U (MayBeSNaN ? SNaN : {})
14// Specifically, [inf, -inf] represents an empty set.
15// Note:
16// 1. Bounds are inclusive.
17// 2. -0 is considered to be less than 0. That is, range [0, 0] doesn't contain
18// -0.
19// 3. Currently wrapping ranges are not supported.
20//
21//===----------------------------------------------------------------------===//
22
23#ifndef LLVM_IR_CONSTANTFPRANGE_H
24#define LLVM_IR_CONSTANTFPRANGE_H
25
26#include "llvm/ADT/APFloat.h"
29#include <optional>
30
31namespace llvm {
32
33class raw_ostream;
34
35/// This class represents a range of floating-point values.
36class [[nodiscard]] ConstantFPRange {
37 APFloat Lower, Upper;
38 bool MayBeQNaN : 1;
39 bool MayBeSNaN : 1;
40
41 /// Create empty constant range with same semantics.
42 ConstantFPRange getEmpty() const {
43 return ConstantFPRange(getSemantics(), /*IsFullSet=*/false);
44 }
45
46 /// Create full constant range with same semantics.
47 ConstantFPRange getFull() const {
48 return ConstantFPRange(getSemantics(), /*IsFullSet=*/true);
49 }
50
51 void makeEmpty();
52 void makeFull();
53
54 /// Initialize a full or empty set for the specified semantics.
55 LLVM_ABI explicit ConstantFPRange(const fltSemantics &Sem, bool IsFullSet);
56
57public:
58 /// Initialize a range to hold the single specified value.
59 LLVM_ABI explicit ConstantFPRange(const APFloat &Value);
60
61 /// Initialize a range of values explicitly.
62 /// Note: If \p LowerVal is greater than \p UpperVal, please use the canonical
63 /// form [Inf, -Inf].
64 LLVM_ABI ConstantFPRange(APFloat LowerVal, APFloat UpperVal, bool MayBeQNaN,
65 bool MayBeSNaN);
66
67 /// Create empty constant range with the given semantics.
68 static ConstantFPRange getEmpty(const fltSemantics &Sem) {
69 return ConstantFPRange(Sem, /*IsFullSet=*/false);
70 }
71
72 /// Create full constant range with the given semantics.
73 static ConstantFPRange getFull(const fltSemantics &Sem) {
74 return ConstantFPRange(Sem, /*IsFullSet=*/true);
75 }
76
77 /// Helper for (-inf, inf) to represent all finite values.
78 LLVM_ABI static ConstantFPRange getFinite(const fltSemantics &Sem);
79
80 /// Helper for [-inf, inf] to represent all non-NaN values.
81 LLVM_ABI static ConstantFPRange getNonNaN(const fltSemantics &Sem);
82
83 /// Create a range which doesn't contain NaNs.
84 static ConstantFPRange getNonNaN(APFloat LowerVal, APFloat UpperVal) {
85 return ConstantFPRange(std::move(LowerVal), std::move(UpperVal),
86 /*MayBeQNaN=*/false, /*MayBeSNaN=*/false);
87 }
88
89 /// Create a range which may contain NaNs.
90 static ConstantFPRange getMayBeNaN(APFloat LowerVal, APFloat UpperVal) {
91 return ConstantFPRange(std::move(LowerVal), std::move(UpperVal),
92 /*MayBeQNaN=*/true, /*MayBeSNaN=*/true);
93 }
94
95 /// Create a range which only contains NaNs.
96 LLVM_ABI static ConstantFPRange getNaNOnly(const fltSemantics &Sem,
97 bool MayBeQNaN, bool MayBeSNaN);
98
99 /// Produce the smallest range such that all values that may satisfy the given
100 /// predicate with any value contained within Other is contained in the
101 /// returned range. Formally, this returns a superset of
102 /// 'union over all y in Other . { x : fcmp op x y is true }'. If the exact
103 /// answer is not representable as a ConstantFPRange, the return value will be
104 /// a proper superset of the above.
105 ///
106 /// Example: Pred = ole and Other = float [2, 5] returns Result = [-inf, 5]
108 makeAllowedFCmpRegion(FCmpInst::Predicate Pred, const ConstantFPRange &Other);
109
110 /// Produce the largest range such that all values in the returned range
111 /// satisfy the given predicate with all values contained within Other.
112 /// Formally, this returns a subset of
113 /// 'intersection over all y in Other . { x : fcmp op x y is true }'. If the
114 /// exact answer is not representable as a ConstantFPRange, the return value
115 /// will be a proper subset of the above.
116 ///
117 /// Example: Pred = ole and Other = float [2, 5] returns [-inf, 2]
119 makeSatisfyingFCmpRegion(FCmpInst::Predicate Pred,
120 const ConstantFPRange &Other);
121
122 /// Produce the exact range such that all values in the returned range satisfy
123 /// the given predicate with any value contained within Other. Formally, this
124 /// returns { x : fcmp op x Other is true }.
125 ///
126 /// Example: Pred = olt and Other = float 3 returns [-inf, 3)
127 /// If the exact answer is not representable as a ConstantFPRange, returns
128 /// std::nullopt.
129 LLVM_ABI static std::optional<ConstantFPRange>
130 makeExactFCmpRegion(FCmpInst::Predicate Pred, const APFloat &Other);
131
132 /// Does the predicate \p Pred hold between ranges this and \p Other?
133 /// NOTE: false does not mean that inverse predicate holds!
135 const ConstantFPRange &Other) const;
136
137 /// Return the lower value for this range.
138 const APFloat &getLower() const { return Lower; }
139
140 /// Return the upper value for this range.
141 const APFloat &getUpper() const { return Upper; }
142
143 bool containsNaN() const { return MayBeQNaN || MayBeSNaN; }
144 bool containsQNaN() const { return MayBeQNaN; }
145 bool containsSNaN() const { return MayBeSNaN; }
146 LLVM_ABI bool isNaNOnly() const;
147
148 /// Get the semantics of this ConstantFPRange.
149 const fltSemantics &getSemantics() const { return Lower.getSemantics(); }
150
151 /// Return true if this set contains all of the elements possible
152 /// for this data-type.
153 LLVM_ABI bool isFullSet() const;
154
155 /// Return true if this set contains no members.
156 LLVM_ABI bool isEmptySet() const;
157
158 /// Return true if the specified value is in the set.
159 LLVM_ABI bool contains(const APFloat &Val) const;
160
161 /// Return true if the other range is a subset of this one.
162 LLVM_ABI bool contains(const ConstantFPRange &CR) const;
163
164 /// If this set contains a single element, return it, otherwise return null.
165 /// If \p ExcludesNaN is true, return the non-NaN single element.
166 LLVM_ABI const APFloat *getSingleElement(bool ExcludesNaN = false) const;
167
168 /// Return true if this set contains exactly one member.
169 /// If \p ExcludesNaN is true, return true if this set contains exactly one
170 /// non-NaN member.
171 bool isSingleElement(bool ExcludesNaN = false) const {
172 return getSingleElement(ExcludesNaN) != nullptr;
173 }
174
175 /// Return true if the sign bit of all values in this range is 1.
176 /// Return false if the sign bit of all values in this range is 0.
177 /// Otherwise, return std::nullopt.
178 LLVM_ABI std::optional<bool> getSignBit() const;
179
180 /// Return true if this range is equal to another range.
181 LLVM_ABI bool operator==(const ConstantFPRange &CR) const;
182 /// Return true if this range is not equal to another range.
183 bool operator!=(const ConstantFPRange &CR) const { return !operator==(CR); }
184
185 /// Return the FPClassTest which will return true for the value.
186 LLVM_ABI FPClassTest classify() const;
187
188 /// Print out the bounds to a stream.
189 LLVM_ABI void print(raw_ostream &OS) const;
190
191 /// Allow printing from a debugger easily.
192 LLVM_ABI void dump() const;
193
194 /// Return the range that results from the intersection of this range with
195 /// another range.
196 LLVM_ABI ConstantFPRange intersectWith(const ConstantFPRange &CR) const;
197
198 /// Return the smallest range that results from the union of this range
199 /// with another range. The resultant range is guaranteed to include the
200 /// elements of both sets, but may contain more.
201 LLVM_ABI ConstantFPRange unionWith(const ConstantFPRange &CR) const;
202
203 /// Calculate absolute value range.
205
206 /// Calculate range of negated values.
207 LLVM_ABI ConstantFPRange negate() const;
208
209 /// Get the range without NaNs. It is useful when we apply nnan flag to range
210 /// of operands/results.
211 ConstantFPRange getWithoutNaN() const {
212 return ConstantFPRange(Lower, Upper, false, false);
213 }
214
215 /// Get the range without infinities. It is useful when we apply ninf flag to
216 /// range of operands/results.
217 LLVM_ABI ConstantFPRange getWithoutInf() const;
218
219 /// Return a new range in the specified format with the specified rounding
220 /// mode.
222 cast(const fltSemantics &DstSem,
224
225 /// Return a new range representing the possible values resulting
226 /// from an addition of a value in this range and a value in \p Other.
228
229 /// Return a new range representing the possible values resulting
230 /// from a subtraction of a value in this range and a value in \p Other.
232
233 /// Return a new range representing the possible values resulting
234 /// from a multiplication of a value in this range and a value in \p Other.
236
237 /// Return a new range representing the possible values resulting
238 /// from a division of a value in this range and a value in
239 /// \p Other.
241
242 /// Flush denormal values to zero according to the specified mode.
243 /// For dynamic mode, we return the union of all possible results.
244 LLVM_ABI void flushDenormals(DenormalMode::DenormalModeKind Mode);
245};
246
248 CR.print(OS);
249 return OS;
250}
251
252} // end namespace llvm
253
254#endif // LLVM_IR_CONSTANTFPRANGE_H
SDValue Pred
This file declares a class to represent arbitrary precision floating point values and provide a varie...
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_ABI
Definition Compiler.h:211
bool operator==(const MergedFunctionsInfo &LHS, const MergedFunctionsInfo &RHS)
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")))
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
llvm::RoundingMode roundingMode
IEEE-754R 4.3: Rounding-direction attributes.
Definition APFloat.h:377
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:379
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
This class represents a range of floating-point values.
static ConstantFPRange getMayBeNaN(APFloat LowerVal, APFloat UpperVal)
Create a range which may contain NaNs.
static ConstantFPRange getFull(const fltSemantics &Sem)
Create full constant range with the given semantics.
ConstantFPRange getWithoutNaN() const
Get the range without NaNs.
LLVM_ABI const APFloat * getSingleElement(bool ExcludesNaN=false) const
If this set contains a single element, return it, otherwise return null.
bool isSingleElement(bool ExcludesNaN=false) const
Return true if this set contains exactly one member.
LLVM_ABI void print(raw_ostream &OS) const
Print out the bounds to a stream.
bool operator!=(const ConstantFPRange &CR) const
Return true if this range is not equal to another range.
static ConstantFPRange getEmpty(const fltSemantics &Sem)
Create empty constant range with the given semantics.
static ConstantFPRange getNonNaN(APFloat LowerVal, APFloat UpperVal)
Create a range which doesn't contain NaNs.
const APFloat & getUpper() const
Return the upper value for this range.
const APFloat & getLower() const
Return the lower value for this range.
const fltSemantics & getSemantics() const
Get the semantics of this ConstantFPRange.
LLVM Value Representation.
Definition Value.h:75
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.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
APFloat abs(APFloat X)
Returns the absolute value of the argument.
Definition APFloat.h:1769
decltype(auto) getSingleElement(ContainerTy &&C)
Asserts that the given container has a single element and returns that element.
Definition STLExtras.h:309
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
@ Other
Any other memory.
Definition ModRef.h:68
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
DenormalModeKind
Represent handled modes for denormal (aka subnormal) modes in the floating point environment.