LLVM 24.0.0git
FPFold.cpp
Go to the documentation of this file.
1//===- FPFold.cpp - Floating-point constant-folding helpers --------------===//
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
13#include <cassert>
14#include <utility>
15
16using namespace llvm;
17
18std::optional<APFloat> llvm::getFPValue(std::optional<FPFoldResult> Res) {
19 return llvm::transformOptional(std::move(Res), &FPFoldResult::Value);
20}
21
23 if (!Value.isDenormal())
24 return Value;
25
27 return APFloat::getZero(Value.getSemantics(), Value.isNegative());
29 return APFloat::getZero(Value.getSemantics());
30 return Value;
31}
32
33/// Return true if gradual-underflow rounding may have promoted a result from
34/// the subnormal range to the smallest normal value. When output denormals are
35/// flushed, folding such a result as an ordinary IEEE operation is unsafe: an
36/// FTZ operation may produce zero before gradual-underflow rounding can perform
37/// that promotion.
38static bool mayHaveBeenRoundedFromSubnormal(const APFloat &Result,
40 RoundingMode RM) {
41 if (!(Status & APFloat::opInexact) || !Result.isSmallestNormalized())
42 return false;
43
44 switch (RM) {
45 default:
46 llvm_unreachable("unknown rounding mode");
50 return true;
52 return !Result.isNegative();
54 return Result.isNegative();
56 return false;
57 }
58}
59
61 for (const APFloat &Arg : Args)
62 if (Arg.isSignaling())
64 return APFloat::opOK;
65}
66
67/// The internal helper that dispatches to APFloat, and always produces
68/// a status and result (so RM must be valid and non-dynamic if used, and
69/// input args must already be flushed if necessary).
70/// Result must be initialized with a copy of Args[0].
73 assert(!Args.empty() && Result.bitwiseIsEqual(Args[0]) &&
74 "Result must start as a copy of Args[0]");
75 if (Args.size() == 2) {
76 switch (Opcode) {
77 default:
78 llvm_unreachable("unsupported 2-arg FP folding op");
79 case FPOp::Add:
80 return Result.add(Args[1], RM);
81 case FPOp::Sub:
82 return Result.subtract(Args[1], RM);
83 case FPOp::Mul:
84 return Result.multiply(Args[1], RM);
85 case FPOp::Div:
86 return Result.divide(Args[1], RM);
87 case FPOp::FRem:
88 return Result.mod(Args[1]);
89 case FPOp::MinNum:
90 Result = minnum(Result, Args[1]);
91 return getSNaNStatus(Args);
92 case FPOp::MaxNum:
93 Result = maxnum(Result, Args[1]);
94 return getSNaNStatus(Args);
95 case FPOp::Minimum:
96 Result = minimum(Result, Args[1]);
97 return getSNaNStatus(Args);
98 case FPOp::Maximum:
99 Result = maximum(Result, Args[1]);
100 return getSNaNStatus(Args);
101 case FPOp::MinimumNum:
102 Result = minimumnum(Result, Args[1]);
103 return getSNaNStatus(Args);
104 case FPOp::MaximumNum:
105 Result = maximumnum(Result, Args[1]);
106 return getSNaNStatus(Args);
107 }
108 } else if (Args.size() == 1) {
109 switch (Opcode) {
110 default:
111 llvm_unreachable("unsupported single-arg FP folding op");
112 case FPOp::Ceil:
113 return Result.roundToIntegral(RoundingMode::TowardPositive);
114 case FPOp::Floor:
115 return Result.roundToIntegral(RoundingMode::TowardNegative);
116 case FPOp::Trunc:
117 return Result.roundToIntegral(RoundingMode::TowardZero);
118 case FPOp::Round:
119 return Result.roundToIntegral(RoundingMode::NearestTiesToAway);
120 case FPOp::RoundEven:
121 return Result.roundToIntegral(RoundingMode::NearestTiesToEven);
122 }
123 } else if (Args.size() == 3 && Opcode == FPOp::FMA) {
124 return Result.fusedMultiplyAdd(Args[1], Args[2], RM);
125 }
126 llvm_unreachable("unsupported FP folding op for given number of inputs");
127}
128
129/// Internal helper to fold or reject if we cannot give correct results.
130/// If RM is necessary for the operation, it must be valid and non-dynamic.
131/// Dynamic denormal modes reject the fold if subnormal inputs are present or
132/// if the output is subnormal and may need flushing.
133static std::optional<FPFoldResult> tryFoldFPOpImpl(FPOp Opcode,
135 RoundingMode RM,
136 DenormalMode Denorms) {
137 assert(!Args.empty() && Args.size() <= 3);
138 assert(Denorms.isValid() && "invalid denormal mode");
139
140 SmallVector<APFloat, 3> FlushedArgs;
141 for (unsigned I = 0; I < Args.size(); ++I) {
142 assert(&Args[I].getSemantics() == &Args[0].getSemantics());
143 if (Denorms.Input == DenormalMode::Dynamic && Args[I].isDenormal())
144 return std::nullopt;
145 FlushedArgs.push_back(flush(Args[I], Denorms.Input));
146 }
147
148 APFloat Result = FlushedArgs[0];
149 APFloat::opStatus Status = executeFold(Opcode, Result, FlushedArgs, RM);
150
151 if (Denorms.Output == DenormalMode::Dynamic && Result.isDenormal())
152 return std::nullopt;
153 if (Denorms.Output != DenormalMode::IEEE &&
155 return std::nullopt;
156 return FPFoldResult{flush(std::move(Result), Denorms.Output), Status};
157}
158
159// Fold FPOps that do not require a rounding mode (or use the default RN)
160std::optional<FPFoldResult> llvm::tryFoldFP(FPOp Opcode, ArrayRef<APFloat> Args,
161 DenormalMode Denorms) {
162 assert(!Args.empty());
164 Denorms);
165}
166
167// Fold FPOps that do require a rounding mode. Handle the possibility
168// that the RM is dynamic (fold with RN and allow the fold
169// only if no rounding is required).
170std::optional<FPFoldResult> llvm::tryFoldFPWithRM(FPOp Opcode,
172 RoundingMode RM,
173 DenormalMode Denorms) {
174 assert(RM != RoundingMode::Invalid && "invalid rounding mode");
175 assert(!Args.empty());
176 RoundingMode EvalRM =
178 auto Res = tryFoldFPOpImpl(Opcode, Args, EvalRM, Denorms);
179 if (RM == RoundingMode::Dynamic && Res && (Res->Status & APFloat::opInexact))
180 return std::nullopt;
181 return Res;
182}
183
184std::optional<APFloat::cmpResult> llvm::tryFoldFCmp(const APFloat &LHS,
185 const APFloat &RHS,
186 DenormalMode Denorms) {
187 assert(Denorms.isValid() && "invalid denormal mode");
188 if (Denorms.Input == DenormalMode::Dynamic &&
189 (LHS.isDenormal() || RHS.isDenormal()))
190 return std::nullopt;
191
192 APFloat LHSTmp = flush(LHS, Denorms.Input);
193 APFloat RHSTmp = flush(RHS, Denorms.Input);
194 return LHSTmp.compare(RHSTmp);
195}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static APFloat flush(APFloat Value, DenormalMode::DenormalModeKind Mode)
Definition FPFold.cpp:22
static bool mayHaveBeenRoundedFromSubnormal(const APFloat &Result, APFloat::opStatus Status, RoundingMode RM)
Return true if gradual-underflow rounding may have promoted a result from the subnormal range to the ...
Definition FPFold.cpp:38
static std::optional< FPFoldResult > tryFoldFPOpImpl(FPOp Opcode, ArrayRef< APFloat > Args, RoundingMode RM, DenormalMode Denorms)
Internal helper to fold or reject if we cannot give correct results.
Definition FPFold.cpp:133
static APFloat::opStatus getSNaNStatus(ArrayRef< APFloat > Args)
Definition FPFold.cpp:60
static APFloat::opStatus executeFold(FPOp Opcode, APFloat &Result, ArrayRef< APFloat > Args, RoundingMode RM)
The internal helper that dispatches to APFloat, and always produces a status and result (so RM must b...
Definition FPFold.cpp:71
This file provides fallible floating-point folding operations with explicit rounding and denormal mod...
#define I(x, y, z)
Definition MD5.cpp:57
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")))
This file contains library features backported from future STL versions.
This file defines the SmallVector class.
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:377
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1183
cmpResult compare(const APFloat &RHS) const
Definition APFloat.h:1526
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::optional< FPFoldResult > tryFoldFP(FPOp Opcode, ArrayRef< APFloat > Args, DenormalMode Denorms)
Try to evaluate a floating-point operation with the specified inputs.
Definition FPFold.cpp:160
LLVM_READONLY APFloat maximum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximum semantics.
Definition APFloat.h:1816
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 maxNum semantics.
Definition APFloat.h:1771
FPOp
The floating-point operations supported by the folding helpers.
Definition FPFold.h:25
@ MaximumNum
Definition FPFold.h:36
@ RoundEven
Definition FPFold.h:42
@ Maximum
Definition FPFold.h:34
@ Minimum
Definition FPFold.h:33
@ MinimumNum
Definition FPFold.h:35
LLVM_READONLY APFloat minimumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimumNumber semantics.
Definition APFloat.h:1802
LLVM_ABI std::optional< APFloat::cmpResult > tryFoldFCmp(const APFloat &LHS, const APFloat &RHS, DenormalMode Denorms)
Try to compare two floating-point values.
Definition FPFold.cpp:184
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE-754 2008 minNum semantics.
Definition APFloat.h:1752
RoundingMode
Rounding mode.
@ TowardZero
roundTowardZero.
@ NearestTiesToEven
roundTiesToEven.
@ Invalid
Denotes invalid value.
@ Dynamic
Denotes mode unknown at compile time.
@ TowardPositive
roundTowardPositive.
@ NearestTiesToAway
roundTiesToAway.
@ TowardNegative
roundTowardNegative.
constexpr std::optional< remove_cvref_t< std::invoke_result_t< Function, Value > > > transformOptional(Optional &&O, Function &&F)
LLVM_READONLY APFloat minimum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 minimum semantics.
Definition APFloat.h:1789
LLVM_READONLY APFloat maximumnum(const APFloat &A, const APFloat &B)
Implements IEEE 754-2019 maximumNumber semantics.
Definition APFloat.h:1829
LLVM_ABI std::optional< APFloat > getFPValue(std::optional< FPFoldResult > Res)
Return the value of a successful fold, or std::nullopt.
Definition FPFold.cpp:18
LLVM_ABI std::optional< FPFoldResult > tryFoldFPWithRM(FPOp Opcode, ArrayRef< APFloat > Args, RoundingMode RM, DenormalMode Denorms)
Try to evaluate a floating-point operation with an explicit rounding mode.
Definition FPFold.cpp:170
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
DenormalModeKind
Represent handled modes for denormal (aka subnormal) modes in the floating point environment.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
constexpr bool isValid() const
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
A unique floating-point result and the IEEE exceptions raised computing it.
Definition FPFold.h:46
APFloat Value
Definition FPFold.h:47