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