FPEnv.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===-- FPEnv.cpp ---- FP Environment -------------------------------------===//
  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 implementations of entities that describe floating
  11. /// point environment.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/FPEnv.h"
  15. #include "llvm/ADT/StringSwitch.h"
  16. #include "llvm/IR/Instruction.h"
  17. #include "llvm/IR/IntrinsicInst.h"
  18. #include "llvm/IR/Intrinsics.h"
  19. #include <optional>
  20. namespace llvm {
  21. std::optional<RoundingMode> convertStrToRoundingMode(StringRef RoundingArg) {
  22. // For dynamic rounding mode, we use round to nearest but we will set the
  23. // 'exact' SDNodeFlag so that the value will not be rounded.
  24. return StringSwitch<std::optional<RoundingMode>>(RoundingArg)
  25. .Case("round.dynamic", RoundingMode::Dynamic)
  26. .Case("round.tonearest", RoundingMode::NearestTiesToEven)
  27. .Case("round.tonearestaway", RoundingMode::NearestTiesToAway)
  28. .Case("round.downward", RoundingMode::TowardNegative)
  29. .Case("round.upward", RoundingMode::TowardPositive)
  30. .Case("round.towardzero", RoundingMode::TowardZero)
  31. .Default(std::nullopt);
  32. }
  33. std::optional<StringRef> convertRoundingModeToStr(RoundingMode UseRounding) {
  34. std::optional<StringRef> RoundingStr;
  35. switch (UseRounding) {
  36. case RoundingMode::Dynamic:
  37. RoundingStr = "round.dynamic";
  38. break;
  39. case RoundingMode::NearestTiesToEven:
  40. RoundingStr = "round.tonearest";
  41. break;
  42. case RoundingMode::NearestTiesToAway:
  43. RoundingStr = "round.tonearestaway";
  44. break;
  45. case RoundingMode::TowardNegative:
  46. RoundingStr = "round.downward";
  47. break;
  48. case RoundingMode::TowardPositive:
  49. RoundingStr = "round.upward";
  50. break;
  51. case RoundingMode::TowardZero:
  52. RoundingStr = "round.towardzero";
  53. break;
  54. default:
  55. break;
  56. }
  57. return RoundingStr;
  58. }
  59. std::optional<fp::ExceptionBehavior>
  60. convertStrToExceptionBehavior(StringRef ExceptionArg) {
  61. return StringSwitch<std::optional<fp::ExceptionBehavior>>(ExceptionArg)
  62. .Case("fpexcept.ignore", fp::ebIgnore)
  63. .Case("fpexcept.maytrap", fp::ebMayTrap)
  64. .Case("fpexcept.strict", fp::ebStrict)
  65. .Default(std::nullopt);
  66. }
  67. std::optional<StringRef>
  68. convertExceptionBehaviorToStr(fp::ExceptionBehavior UseExcept) {
  69. std::optional<StringRef> ExceptStr;
  70. switch (UseExcept) {
  71. case fp::ebStrict:
  72. ExceptStr = "fpexcept.strict";
  73. break;
  74. case fp::ebIgnore:
  75. ExceptStr = "fpexcept.ignore";
  76. break;
  77. case fp::ebMayTrap:
  78. ExceptStr = "fpexcept.maytrap";
  79. break;
  80. }
  81. return ExceptStr;
  82. }
  83. Intrinsic::ID getConstrainedIntrinsicID(const Instruction &Instr) {
  84. Intrinsic::ID IID = Intrinsic::not_intrinsic;
  85. switch (Instr.getOpcode()) {
  86. case Instruction::FCmp:
  87. // Unlike other instructions FCmp can be mapped to one of two intrinsic
  88. // functions. We choose the non-signaling variant.
  89. IID = Intrinsic::experimental_constrained_fcmp;
  90. break;
  91. // Instructions
  92. #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
  93. case Instruction::NAME: \
  94. IID = Intrinsic::INTRINSIC; \
  95. break;
  96. #define FUNCTION(NAME, NARG, ROUND_MODE, INTRINSIC)
  97. #define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)
  98. #include "llvm/IR/ConstrainedOps.def"
  99. // Intrinsic calls.
  100. case Instruction::Call:
  101. if (auto *IntrinCall = dyn_cast<IntrinsicInst>(&Instr)) {
  102. switch (IntrinCall->getIntrinsicID()) {
  103. #define FUNCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
  104. case Intrinsic::NAME: \
  105. IID = Intrinsic::INTRINSIC; \
  106. break;
  107. #define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)
  108. #define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)
  109. #include "llvm/IR/ConstrainedOps.def"
  110. default:
  111. break;
  112. }
  113. }
  114. break;
  115. default:
  116. break;
  117. }
  118. return IID;
  119. }
  120. } // namespace llvm