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