xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/FPEnv.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- FPEnv.h ---- FP Environment ------------------------------*- 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 /// @file
10 /// This file contains the declarations of entities that describe floating
11 /// point environment and related functions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_FPENV_H
16 #define LLVM_IR_FPENV_H
17 
18 #include "llvm/ADT/FloatingPointMode.h"
19 #include "llvm/IR/FMF.h"
20 #include "llvm/Support/Compiler.h"
21 #include <optional>
22 
23 namespace llvm {
24 class StringRef;
25 
26 namespace Intrinsic {
27 typedef unsigned ID;
28 }
29 
30 class Instruction;
31 
32 namespace fp {
33 
34 /// Exception behavior used for floating point operations.
35 ///
36 /// Each of these values correspond to some metadata argument value of a
37 /// constrained floating point intrinsic. See the LLVM Language Reference Manual
38 /// for details.
39 enum ExceptionBehavior : uint8_t {
40   ebIgnore,  ///< This corresponds to "fpexcept.ignore".
41   ebMayTrap, ///< This corresponds to "fpexcept.maytrap".
42   ebStrict   ///< This corresponds to "fpexcept.strict".
43 };
44 
45 }
46 
47 /// Returns a valid RoundingMode enumerator when given a string
48 /// that is valid as input in constrained intrinsic rounding mode
49 /// metadata.
50 LLVM_ABI std::optional<RoundingMode> convertStrToRoundingMode(StringRef);
51 
52 /// For any RoundingMode enumerator, returns a string valid as input in
53 /// constrained intrinsic rounding mode metadata.
54 LLVM_ABI std::optional<StringRef> convertRoundingModeToStr(RoundingMode);
55 
56 /// Returns a valid ExceptionBehavior enumerator when given a string
57 /// valid as input in constrained intrinsic exception behavior metadata.
58 LLVM_ABI std::optional<fp::ExceptionBehavior>
59     convertStrToExceptionBehavior(StringRef);
60 
61 /// For any ExceptionBehavior enumerator, returns a string valid as
62 /// input in constrained intrinsic exception behavior metadata.
63 LLVM_ABI std::optional<StringRef>
64     convertExceptionBehaviorToStr(fp::ExceptionBehavior);
65 
66 /// Returns true if the exception handling behavior and rounding mode
67 /// match what is used in the default floating point environment.
isDefaultFPEnvironment(fp::ExceptionBehavior EB,RoundingMode RM)68 inline bool isDefaultFPEnvironment(fp::ExceptionBehavior EB, RoundingMode RM) {
69   return EB == fp::ebIgnore && RM == RoundingMode::NearestTiesToEven;
70 }
71 
72 /// Returns constrained intrinsic id to represent the given instruction in
73 /// strictfp function. If the instruction is already a constrained intrinsic or
74 /// does not have a constrained intrinsic counterpart, the function returns
75 /// zero.
76 LLVM_ABI Intrinsic::ID getConstrainedIntrinsicID(const Instruction &Instr);
77 
78 /// Returns true if the rounding mode RM may be QRM at compile time or
79 /// at run time.
canRoundingModeBe(RoundingMode RM,RoundingMode QRM)80 inline bool canRoundingModeBe(RoundingMode RM, RoundingMode QRM) {
81   return RM == QRM || RM == RoundingMode::Dynamic;
82 }
83 
84 /// Returns true if the possibility of a signaling NaN can be safely
85 /// ignored.
canIgnoreSNaN(fp::ExceptionBehavior EB,FastMathFlags FMF)86 inline bool canIgnoreSNaN(fp::ExceptionBehavior EB, FastMathFlags FMF) {
87   return (EB == fp::ebIgnore || FMF.noNaNs());
88 }
89 }
90 #endif
91