FPEnv.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- FPEnv.h ---- FP Environment ------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// @file
  15. /// This file contains the declarations of entities that describe floating
  16. /// point environment and related functions.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_IR_FPENV_H
  20. #define LLVM_IR_FPENV_H
  21. #include "llvm/ADT/FloatingPointMode.h"
  22. #include "llvm/IR/FMF.h"
  23. #include <optional>
  24. namespace llvm {
  25. class StringRef;
  26. namespace Intrinsic {
  27. typedef unsigned ID;
  28. }
  29. class Instruction;
  30. namespace fp {
  31. /// Exception behavior used for floating point operations.
  32. ///
  33. /// Each of these values correspond to some metadata argument value of a
  34. /// constrained floating point intrinsic. See the LLVM Language Reference Manual
  35. /// for details.
  36. enum ExceptionBehavior : uint8_t {
  37. ebIgnore, ///< This corresponds to "fpexcept.ignore".
  38. ebMayTrap, ///< This corresponds to "fpexcept.maytrap".
  39. ebStrict ///< This corresponds to "fpexcept.strict".
  40. };
  41. }
  42. /// Returns a valid RoundingMode enumerator when given a string
  43. /// that is valid as input in constrained intrinsic rounding mode
  44. /// metadata.
  45. std::optional<RoundingMode> convertStrToRoundingMode(StringRef);
  46. /// For any RoundingMode enumerator, returns a string valid as input in
  47. /// constrained intrinsic rounding mode metadata.
  48. std::optional<StringRef> convertRoundingModeToStr(RoundingMode);
  49. /// Returns a valid ExceptionBehavior enumerator when given a string
  50. /// valid as input in constrained intrinsic exception behavior metadata.
  51. std::optional<fp::ExceptionBehavior> convertStrToExceptionBehavior(StringRef);
  52. /// For any ExceptionBehavior enumerator, returns a string valid as
  53. /// input in constrained intrinsic exception behavior metadata.
  54. std::optional<StringRef> convertExceptionBehaviorToStr(fp::ExceptionBehavior);
  55. /// Returns true if the exception handling behavior and rounding mode
  56. /// match what is used in the default floating point environment.
  57. inline bool isDefaultFPEnvironment(fp::ExceptionBehavior EB, RoundingMode RM) {
  58. return EB == fp::ebIgnore && RM == RoundingMode::NearestTiesToEven;
  59. }
  60. /// Returns constrained intrinsic id to represent the given instruction in
  61. /// strictfp function. If the instruction is already a constrained intrinsic or
  62. /// does not have a constrained intrinsic counterpart, the function returns
  63. /// zero.
  64. Intrinsic::ID getConstrainedIntrinsicID(const Instruction &Instr);
  65. /// Returns true if the rounding mode RM may be QRM at compile time or
  66. /// at run time.
  67. inline bool canRoundingModeBe(RoundingMode RM, RoundingMode QRM) {
  68. return RM == QRM || RM == RoundingMode::Dynamic;
  69. }
  70. /// Returns true if the possibility of a signaling NaN can be safely
  71. /// ignored.
  72. inline bool canIgnoreSNaN(fp::ExceptionBehavior EB, FastMathFlags FMF) {
  73. return (EB == fp::ebIgnore || FMF.noNaNs());
  74. }
  75. }
  76. #endif
  77. #ifdef __GNUC__
  78. #pragma GCC diagnostic pop
  79. #endif