LLVM 20.0.0git
FPEnv.cpp
Go to the documentation of this file.
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"
17#include "llvm/IR/Instruction.h"
19#include "llvm/IR/Intrinsics.h"
20#include <optional>
21
22namespace llvm {
23
24std::optional<RoundingMode> convertStrToRoundingMode(StringRef RoundingArg) {
25 // For dynamic rounding mode, we use round to nearest but we will set the
26 // 'exact' SDNodeFlag so that the value will not be rounded.
28 .Case("round.dynamic", RoundingMode::Dynamic)
29 .Case("round.tonearest", RoundingMode::NearestTiesToEven)
30 .Case("round.tonearestaway", RoundingMode::NearestTiesToAway)
31 .Case("round.downward", RoundingMode::TowardNegative)
32 .Case("round.upward", RoundingMode::TowardPositive)
33 .Case("round.towardzero", RoundingMode::TowardZero)
34 .Default(std::nullopt);
35}
36
37std::optional<StringRef> convertRoundingModeToStr(RoundingMode UseRounding) {
38 std::optional<StringRef> RoundingStr;
39 switch (UseRounding) {
41 RoundingStr = "round.dynamic";
42 break;
44 RoundingStr = "round.tonearest";
45 break;
47 RoundingStr = "round.tonearestaway";
48 break;
50 RoundingStr = "round.downward";
51 break;
53 RoundingStr = "round.upward";
54 break;
56 RoundingStr = "round.towardzero";
57 break;
58 default:
59 break;
60 }
61 return RoundingStr;
62}
63
64std::optional<fp::ExceptionBehavior>
67 .Case("fpexcept.ignore", fp::ebIgnore)
68 .Case("fpexcept.maytrap", fp::ebMayTrap)
69 .Case("fpexcept.strict", fp::ebStrict)
70 .Default(std::nullopt);
71}
72
73std::optional<StringRef>
75 std::optional<StringRef> ExceptStr;
76 switch (UseExcept) {
77 case fp::ebStrict:
78 ExceptStr = "fpexcept.strict";
79 break;
80 case fp::ebIgnore:
81 ExceptStr = "fpexcept.ignore";
82 break;
83 case fp::ebMayTrap:
84 ExceptStr = "fpexcept.maytrap";
85 break;
86 }
87 return ExceptStr;
88}
89
92 switch (Instr.getOpcode()) {
93 case Instruction::FCmp:
94 // Unlike other instructions FCmp can be mapped to one of two intrinsic
95 // functions. We choose the non-signaling variant.
96 IID = Intrinsic::experimental_constrained_fcmp;
97 break;
98
99 // Instructions
100#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
101 case Instruction::NAME: \
102 IID = Intrinsic::INTRINSIC; \
103 break;
104#define FUNCTION(NAME, NARG, ROUND_MODE, INTRINSIC)
105#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)
106#include "llvm/IR/ConstrainedOps.def"
107
108 // Intrinsic calls.
109 case Instruction::Call:
110 if (auto *IntrinCall = dyn_cast<IntrinsicInst>(&Instr)) {
111 switch (IntrinCall->getIntrinsicID()) {
112#define FUNCTION(NAME, NARG, ROUND_MODE, INTRINSIC) \
113 case Intrinsic::NAME: \
114 IID = Intrinsic::INTRINSIC; \
115 break;
116#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC)
117#define CMP_INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN)
118#include "llvm/IR/ConstrainedOps.def"
119 default:
120 break;
121 }
122 }
123 break;
124 default:
125 break;
126 }
127
128 return IID;
129}
130
131} // namespace llvm
This file contains the declarations of entities that describe floating point environment and related ...
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
ExceptionBehavior
Exception behavior used for floating point operations.
Definition: FPEnv.h:38
@ ebStrict
This corresponds to "fpexcept.strict".
Definition: FPEnv.h:41
@ ebMayTrap
This corresponds to "fpexcept.maytrap".
Definition: FPEnv.h:40
@ ebIgnore
This corresponds to "fpexcept.ignore".
Definition: FPEnv.h:39
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::optional< StringRef > convertRoundingModeToStr(RoundingMode)
For any RoundingMode enumerator, returns a string valid as input in constrained intrinsic rounding mo...
Definition: FPEnv.cpp:37
std::optional< StringRef > convertExceptionBehaviorToStr(fp::ExceptionBehavior)
For any ExceptionBehavior enumerator, returns a string valid as input in constrained intrinsic except...
Definition: FPEnv.cpp:74
Intrinsic::ID getConstrainedIntrinsicID(const Instruction &Instr)
Returns constrained intrinsic id to represent the given instruction in strictfp function.
Definition: FPEnv.cpp:90
std::optional< fp::ExceptionBehavior > convertStrToExceptionBehavior(StringRef)
Returns a valid ExceptionBehavior enumerator when given a string valid as input in constrained intrin...
Definition: FPEnv.cpp:65
RoundingMode
Rounding mode.
@ TowardZero
roundTowardZero.
@ NearestTiesToEven
roundTiesToEven.
@ Dynamic
Denotes mode unknown at compile time.
@ TowardPositive
roundTowardPositive.
@ NearestTiesToAway
roundTiesToAway.
@ TowardNegative
roundTowardNegative.
std::optional< RoundingMode > convertStrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
Definition: FPEnv.cpp:24