1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
//===- ConstantFPRange.cpp - ConstantFPRange implementation ---------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/ConstantFPRange.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
using namespace llvm;
void ConstantFPRange::makeEmpty() {
auto &Sem = Lower.getSemantics();
Lower = APFloat::getInf(Sem, /*Negative=*/false);
Upper = APFloat::getInf(Sem, /*Negative=*/true);
MayBeQNaN = false;
MayBeSNaN = false;
}
void ConstantFPRange::makeFull() {
auto &Sem = Lower.getSemantics();
Lower = APFloat::getInf(Sem, /*Negative=*/true);
Upper = APFloat::getInf(Sem, /*Negative=*/false);
MayBeQNaN = true;
MayBeSNaN = true;
}
bool ConstantFPRange::isNaNOnly() const {
return Lower.isPosInfinity() && Upper.isNegInfinity();
}
ConstantFPRange::ConstantFPRange(const fltSemantics &Sem, bool IsFullSet)
: Lower(Sem, APFloat::uninitialized), Upper(Sem, APFloat::uninitialized) {
Lower = APFloat::getInf(Sem, /*Negative=*/IsFullSet);
Upper = APFloat::getInf(Sem, /*Negative=*/!IsFullSet);
MayBeQNaN = IsFullSet;
MayBeSNaN = IsFullSet;
}
ConstantFPRange::ConstantFPRange(const APFloat &Value)
: Lower(Value.getSemantics(), APFloat::uninitialized),
Upper(Value.getSemantics(), APFloat::uninitialized) {
if (Value.isNaN()) {
makeEmpty();
bool IsSNaN = Value.isSignaling();
MayBeQNaN = !IsSNaN;
MayBeSNaN = IsSNaN;
} else {
Lower = Upper = Value;
MayBeQNaN = MayBeSNaN = false;
}
}
// We treat that -0 is less than 0 here.
static APFloat::cmpResult strictCompare(const APFloat &LHS,
const APFloat &RHS) {
assert(!LHS.isNaN() && !RHS.isNaN() && "Unordered compare");
if (LHS.isZero() && RHS.isZero()) {
if (LHS.isNegative() == RHS.isNegative())
return APFloat::cmpEqual;
return LHS.isNegative() ? APFloat::cmpLessThan : APFloat::cmpGreaterThan;
}
return LHS.compare(RHS);
}
static bool isNonCanonicalEmptySet(const APFloat &Lower, const APFloat &Upper) {
return strictCompare(Lower, Upper) == APFloat::cmpGreaterThan &&
!(Lower.isInfinity() && Upper.isInfinity());
}
static void canonicalizeRange(APFloat &Lower, APFloat &Upper) {
if (isNonCanonicalEmptySet(Lower, Upper)) {
Lower = APFloat::getInf(Lower.getSemantics(), /*Negative=*/false);
Upper = APFloat::getInf(Upper.getSemantics(), /*Negative=*/true);
}
}
ConstantFPRange::ConstantFPRange(APFloat LowerVal, APFloat UpperVal,
bool MayBeQNaNVal, bool MayBeSNaNVal)
: Lower(std::move(LowerVal)), Upper(std::move(UpperVal)),
MayBeQNaN(MayBeQNaNVal), MayBeSNaN(MayBeSNaNVal) {
assert(&Lower.getSemantics() == &Upper.getSemantics() &&
"Should only use the same semantics");
assert(!isNonCanonicalEmptySet(Lower, Upper) && "Non-canonical form");
}
ConstantFPRange ConstantFPRange::getFinite(const fltSemantics &Sem) {
return ConstantFPRange(APFloat::getLargest(Sem, /*Negative=*/true),
APFloat::getLargest(Sem, /*Negative=*/false),
/*MayBeQNaN=*/false, /*MayBeSNaN=*/false);
}
ConstantFPRange ConstantFPRange::getNaNOnly(const fltSemantics &Sem,
bool MayBeQNaN, bool MayBeSNaN) {
return ConstantFPRange(APFloat::getInf(Sem, /*Negative=*/false),
APFloat::getInf(Sem, /*Negative=*/true), MayBeQNaN,
MayBeSNaN);
}
ConstantFPRange ConstantFPRange::getNonNaN(const fltSemantics &Sem) {
return ConstantFPRange(APFloat::getInf(Sem, /*Negative=*/true),
APFloat::getInf(Sem, /*Negative=*/false),
/*MayBeQNaN=*/false, /*MayBeSNaN=*/false);
}
/// Return true for ULT/UGT/OLT/OGT
static bool fcmpPredExcludesEqual(FCmpInst::Predicate Pred) {
return !(Pred & FCmpInst::FCMP_OEQ);
}
/// Return [-inf, V) or [-inf, V]
static ConstantFPRange makeLessThan(APFloat V, FCmpInst::Predicate Pred) {
const fltSemantics &Sem = V.getSemantics();
if (fcmpPredExcludesEqual(Pred)) {
if (V.isNegInfinity())
return ConstantFPRange::getEmpty(Sem);
V.next(/*nextDown=*/true);
}
return ConstantFPRange::getNonNaN(APFloat::getInf(Sem, /*Negative=*/true),
std::move(V));
}
/// Return (V, +inf] or [V, +inf]
static ConstantFPRange makeGreaterThan(APFloat V, FCmpInst::Predicate Pred) {
const fltSemantics &Sem = V.getSemantics();
if (fcmpPredExcludesEqual(Pred)) {
if (V.isPosInfinity())
return ConstantFPRange::getEmpty(Sem);
V.next(/*nextDown=*/false);
}
return ConstantFPRange::getNonNaN(std::move(V),
APFloat::getInf(Sem, /*Negative=*/false));
}
/// Make sure that +0/-0 are both included in the range.
static ConstantFPRange extendZeroIfEqual(const ConstantFPRange &CR,
FCmpInst::Predicate Pred) {
if (fcmpPredExcludesEqual(Pred))
return CR;
APFloat Lower = CR.getLower();
APFloat Upper = CR.getUpper();
if (Lower.isPosZero())
Lower = APFloat::getZero(Lower.getSemantics(), /*Negative=*/true);
if (Upper.isNegZero())
Upper = APFloat::getZero(Upper.getSemantics(), /*Negative=*/false);
return ConstantFPRange(std::move(Lower), std::move(Upper), CR.containsQNaN(),
CR.containsSNaN());
}
static ConstantFPRange setNaNField(const ConstantFPRange &CR,
FCmpInst::Predicate Pred) {
bool ContainsNaN = FCmpInst::isUnordered(Pred);
return ConstantFPRange(CR.getLower(), CR.getUpper(),
/*MayBeQNaN=*/ContainsNaN, /*MayBeSNaN=*/ContainsNaN);
}
ConstantFPRange
ConstantFPRange::makeAllowedFCmpRegion(FCmpInst::Predicate Pred,
const ConstantFPRange &Other) {
if (Other.isEmptySet())
return Other;
if (Other.containsNaN() && FCmpInst::isUnordered(Pred))
return getFull(Other.getSemantics());
if (Other.isNaNOnly() && FCmpInst::isOrdered(Pred))
return getEmpty(Other.getSemantics());
switch (Pred) {
case FCmpInst::FCMP_TRUE:
return getFull(Other.getSemantics());
case FCmpInst::FCMP_FALSE:
return getEmpty(Other.getSemantics());
case FCmpInst::FCMP_ORD:
return getNonNaN(Other.getSemantics());
case FCmpInst::FCMP_UNO:
return getNaNOnly(Other.getSemantics(), /*MayBeQNaN=*/true,
/*MayBeSNaN=*/true);
case FCmpInst::FCMP_OEQ:
case FCmpInst::FCMP_UEQ:
return setNaNField(extendZeroIfEqual(Other, Pred), Pred);
case FCmpInst::FCMP_ONE:
case FCmpInst::FCMP_UNE:
if (const APFloat *SingleElement =
Other.getSingleElement(/*ExcludesNaN=*/true)) {
const fltSemantics &Sem = SingleElement->getSemantics();
if (SingleElement->isPosInfinity())
return setNaNField(
getNonNaN(APFloat::getInf(Sem, /*Negative=*/true),
APFloat::getLargest(Sem, /*Negative=*/false)),
Pred);
if (SingleElement->isNegInfinity())
return setNaNField(
getNonNaN(APFloat::getLargest(Sem, /*Negative=*/true),
APFloat::getInf(Sem, /*Negative=*/false)),
Pred);
}
return Pred == FCmpInst::FCMP_ONE ? getNonNaN(Other.getSemantics())
: getFull(Other.getSemantics());
case FCmpInst::FCMP_OLT:
case FCmpInst::FCMP_OLE:
case FCmpInst::FCMP_ULT:
case FCmpInst::FCMP_ULE:
return setNaNField(
extendZeroIfEqual(makeLessThan(Other.getUpper(), Pred), Pred), Pred);
case FCmpInst::FCMP_OGT:
case FCmpInst::FCMP_OGE:
case FCmpInst::FCMP_UGT:
case FCmpInst::FCMP_UGE:
return setNaNField(
extendZeroIfEqual(makeGreaterThan(Other.getLower(), Pred), Pred), Pred);
default:
llvm_unreachable("Unexpected predicate");
}
}
ConstantFPRange
ConstantFPRange::makeSatisfyingFCmpRegion(FCmpInst::Predicate Pred,
const ConstantFPRange &Other) {
if (Other.isEmptySet())
return getFull(Other.getSemantics());
if (Other.containsNaN() && FCmpInst::isOrdered(Pred))
return getEmpty(Other.getSemantics());
if (Other.isNaNOnly() && FCmpInst::isUnordered(Pred))
return getFull(Other.getSemantics());
switch (Pred) {
case FCmpInst::FCMP_TRUE:
return getFull(Other.getSemantics());
case FCmpInst::FCMP_FALSE:
return getEmpty(Other.getSemantics());
case FCmpInst::FCMP_ORD:
return getNonNaN(Other.getSemantics());
case FCmpInst::FCMP_UNO:
return getNaNOnly(Other.getSemantics(), /*MayBeQNaN=*/true,
/*MayBeSNaN=*/true);
case FCmpInst::FCMP_OEQ:
case FCmpInst::FCMP_UEQ:
return setNaNField(Other.isSingleElement(/*ExcludesNaN=*/true) ||
((Other.classify() & ~fcNan) == fcZero)
? extendZeroIfEqual(Other, Pred)
: getEmpty(Other.getSemantics()),
Pred);
case FCmpInst::FCMP_ONE:
case FCmpInst::FCMP_UNE:
return getEmpty(Other.getSemantics());
case FCmpInst::FCMP_OLT:
case FCmpInst::FCMP_OLE:
case FCmpInst::FCMP_ULT:
case FCmpInst::FCMP_ULE:
return setNaNField(
extendZeroIfEqual(makeLessThan(Other.getLower(), Pred), Pred), Pred);
case FCmpInst::FCMP_OGT:
case FCmpInst::FCMP_OGE:
case FCmpInst::FCMP_UGT:
case FCmpInst::FCMP_UGE:
return setNaNField(
extendZeroIfEqual(makeGreaterThan(Other.getUpper(), Pred), Pred), Pred);
default:
llvm_unreachable("Unexpected predicate");
}
}
std::optional<ConstantFPRange>
ConstantFPRange::makeExactFCmpRegion(FCmpInst::Predicate Pred,
const APFloat &Other) {
if ((Pred == FCmpInst::FCMP_UNE || Pred == FCmpInst::FCMP_ONE) &&
!Other.isNaN())
return std::nullopt;
return makeSatisfyingFCmpRegion(Pred, ConstantFPRange(Other));
}
bool ConstantFPRange::fcmp(FCmpInst::Predicate Pred,
const ConstantFPRange &Other) const {
return makeSatisfyingFCmpRegion(Pred, Other).contains(*this);
}
bool ConstantFPRange::isFullSet() const {
return Lower.isNegInfinity() && Upper.isPosInfinity() && MayBeQNaN &&
MayBeSNaN;
}
bool ConstantFPRange::isEmptySet() const {
return Lower.isPosInfinity() && Upper.isNegInfinity() && !MayBeQNaN &&
!MayBeSNaN;
}
bool ConstantFPRange::contains(const APFloat &Val) const {
assert(&getSemantics() == &Val.getSemantics() &&
"Should only use the same semantics");
if (Val.isNaN())
return Val.isSignaling() ? MayBeSNaN : MayBeQNaN;
return strictCompare(Lower, Val) != APFloat::cmpGreaterThan &&
strictCompare(Val, Upper) != APFloat::cmpGreaterThan;
}
bool ConstantFPRange::contains(const ConstantFPRange &CR) const {
assert(&getSemantics() == &CR.getSemantics() &&
"Should only use the same semantics");
if (CR.MayBeQNaN && !MayBeQNaN)
return false;
if (CR.MayBeSNaN && !MayBeSNaN)
return false;
return strictCompare(Lower, CR.Lower) != APFloat::cmpGreaterThan &&
strictCompare(CR.Upper, Upper) != APFloat::cmpGreaterThan;
}
const APFloat *ConstantFPRange::getSingleElement(bool ExcludesNaN) const {
if (!ExcludesNaN && (MayBeSNaN || MayBeQNaN))
return nullptr;
return Lower.bitwiseIsEqual(Upper) ? &Lower : nullptr;
}
std::optional<bool> ConstantFPRange::getSignBit() const {
if (!MayBeSNaN && !MayBeQNaN && Lower.isNegative() == Upper.isNegative())
return Lower.isNegative();
return std::nullopt;
}
bool ConstantFPRange::operator==(const ConstantFPRange &CR) const {
if (MayBeSNaN != CR.MayBeSNaN || MayBeQNaN != CR.MayBeQNaN)
return false;
return Lower.bitwiseIsEqual(CR.Lower) && Upper.bitwiseIsEqual(CR.Upper);
}
FPClassTest ConstantFPRange::classify() const {
uint32_t Mask = fcNone;
if (MayBeSNaN)
Mask |= fcSNan;
if (MayBeQNaN)
Mask |= fcQNan;
if (!isNaNOnly()) {
FPClassTest LowerMask = Lower.classify();
FPClassTest UpperMask = Upper.classify();
assert(LowerMask <= UpperMask && "Range is nan-only.");
// Set all bits from log2(LowerMask) to log2(UpperMask).
Mask |= (UpperMask << 1) - LowerMask;
}
return static_cast<FPClassTest>(Mask);
}
void ConstantFPRange::print(raw_ostream &OS) const {
if (isFullSet())
OS << "full-set";
else if (isEmptySet())
OS << "empty-set";
else {
bool NaNOnly = isNaNOnly();
if (!NaNOnly)
OS << '[' << Lower << ", " << Upper << ']';
if (MayBeSNaN || MayBeQNaN) {
if (!NaNOnly)
OS << " with ";
if (MayBeSNaN && MayBeQNaN)
OS << "NaN";
else if (MayBeSNaN)
OS << "SNaN";
else if (MayBeQNaN)
OS << "QNaN";
}
}
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void ConstantFPRange::dump() const { print(dbgs()); }
#endif
ConstantFPRange
ConstantFPRange::intersectWith(const ConstantFPRange &CR) const {
assert(&getSemantics() == &CR.getSemantics() &&
"Should only use the same semantics");
APFloat NewLower = maxnum(Lower, CR.Lower);
APFloat NewUpper = minnum(Upper, CR.Upper);
canonicalizeRange(NewLower, NewUpper);
return ConstantFPRange(std::move(NewLower), std::move(NewUpper),
MayBeQNaN & CR.MayBeQNaN, MayBeSNaN & CR.MayBeSNaN);
}
ConstantFPRange ConstantFPRange::unionWith(const ConstantFPRange &CR) const {
assert(&getSemantics() == &CR.getSemantics() &&
"Should only use the same semantics");
return ConstantFPRange(minnum(Lower, CR.Lower), maxnum(Upper, CR.Upper),
MayBeQNaN | CR.MayBeQNaN, MayBeSNaN | CR.MayBeSNaN);
}
|