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