LLVM  10.0.0svn
IntrinsicInst.cpp
Go to the documentation of this file.
1 //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
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 // This file implements methods that make it really easy to deal with intrinsic
10 // functions.
11 //
12 // All intrinsic function calls are instances of the call instruction, so these
13 // are all subclasses of the CallInst class. Note that none of these classes
14 // has state or virtual methods, which is an important part of this gross/neat
15 // hack working.
16 //
17 // In some cases, arguments to intrinsics need to be generic and are defined as
18 // type pointer to empty struct { }*. To access the real item of interest the
19 // cast instruction needs to be stripped away.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/ADT/StringSwitch.h"
26 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/IR/Module.h"
32 using namespace llvm;
33 
34 //===----------------------------------------------------------------------===//
35 /// DbgVariableIntrinsic - This is the common base class for debug info
36 /// intrinsics for variables.
37 ///
38 
40  Value *Op = getArgOperand(0);
41  if (AllowNullOp && !Op)
42  return nullptr;
43 
44  auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
45  if (auto *V = dyn_cast<ValueAsMetadata>(MD))
46  return V->getValue();
47 
48  // When the value goes to null, it gets replaced by an empty MDNode.
49  assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
50  return nullptr;
51 }
52 
54  if (auto Fragment = getExpression()->getFragmentInfo())
55  return Fragment->SizeInBits;
56  return getVariable()->getSizeInBits();
57 }
58 
60  StringRef Name) {
61  assert(Name.startswith("llvm."));
62 
63  // Do successive binary searches of the dotted name components. For
64  // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
65  // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
66  // "llvm.gc.experimental.statepoint", and then we will stop as the range is
67  // size 1. During the search, we can skip the prefix that we already know is
68  // identical. By using strncmp we consider names with differing suffixes to
69  // be part of the equal range.
70  size_t CmpStart = 0;
71  size_t CmpEnd = 4; // Skip the "llvm" component.
72  const char *const *Low = NameTable.begin();
73  const char *const *High = NameTable.end();
74  const char *const *LastLow = Low;
75  while (CmpEnd < Name.size() && High - Low > 0) {
76  CmpStart = CmpEnd;
77  CmpEnd = Name.find('.', CmpStart + 1);
78  CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
79  auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
80  return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
81  };
82  LastLow = Low;
83  std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
84  }
85  if (High - Low > 0)
86  LastLow = Low;
87 
88  if (LastLow == NameTable.end())
89  return -1;
90  StringRef NameFound = *LastLow;
91  if (Name == NameFound ||
92  (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
93  return LastLow - NameTable.begin();
94  return -1;
95 }
96 
99  return const_cast<Value *>(getArgOperand(4));
100  }
101  const Module *M = getModule();
103  return ConstantInt::get(Type::getInt64Ty(Context), 1);
104 }
105 
108  unsigned NumOperands = getNumArgOperands();
109  Metadata *MD =
110  dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 2))->getMetadata();
111  if (!MD || !isa<MDString>(MD))
112  return None;
113  return StrToRoundingMode(cast<MDString>(MD)->getString());
114 }
115 
118  // For dynamic rounding mode, we use round to nearest but we will set the
119  // 'exact' SDNodeFlag so that the value will not be rounded.
120  return StringSwitch<Optional<RoundingMode>>(RoundingArg)
121  .Case("round.dynamic", rmDynamic)
122  .Case("round.tonearest", rmToNearest)
123  .Case("round.downward", rmDownward)
124  .Case("round.upward", rmUpward)
125  .Case("round.towardzero", rmTowardZero)
126  .Default(None);
127 }
128 
131  Optional<StringRef> RoundingStr = None;
132  switch (UseRounding) {
134  RoundingStr = "round.dynamic";
135  break;
137  RoundingStr = "round.tonearest";
138  break;
140  RoundingStr = "round.downward";
141  break;
143  RoundingStr = "round.upward";
144  break;
146  RoundingStr = "round.towardzero";
147  break;
148  }
149  return RoundingStr;
150 }
151 
154  unsigned NumOperands = getNumArgOperands();
155  Metadata *MD =
156  dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 1))->getMetadata();
157  if (!MD || !isa<MDString>(MD))
158  return None;
159  return StrToExceptionBehavior(cast<MDString>(MD)->getString());
160 }
161 
164  return StringSwitch<Optional<ExceptionBehavior>>(ExceptionArg)
165  .Case("fpexcept.ignore", ebIgnore)
166  .Case("fpexcept.maytrap", ebMayTrap)
167  .Case("fpexcept.strict", ebStrict)
168  .Default(None);
169 }
170 
173  Optional<StringRef> ExceptStr = None;
174  switch (UseExcept) {
176  ExceptStr = "fpexcept.strict";
177  break;
179  ExceptStr = "fpexcept.ignore";
180  break;
182  ExceptStr = "fpexcept.maytrap";
183  break;
184  }
185  return ExceptStr;
186 }
187 
189  switch (getIntrinsicID()) {
190  default:
191  return false;
192  case Intrinsic::experimental_constrained_fptrunc:
193  case Intrinsic::experimental_constrained_fpext:
194  case Intrinsic::experimental_constrained_sqrt:
195  case Intrinsic::experimental_constrained_sin:
196  case Intrinsic::experimental_constrained_cos:
197  case Intrinsic::experimental_constrained_exp:
198  case Intrinsic::experimental_constrained_exp2:
199  case Intrinsic::experimental_constrained_log:
200  case Intrinsic::experimental_constrained_log10:
201  case Intrinsic::experimental_constrained_log2:
202  case Intrinsic::experimental_constrained_rint:
203  case Intrinsic::experimental_constrained_nearbyint:
204  case Intrinsic::experimental_constrained_ceil:
205  case Intrinsic::experimental_constrained_floor:
206  case Intrinsic::experimental_constrained_round:
207  case Intrinsic::experimental_constrained_trunc:
208  return true;
209  }
210 }
211 
213  switch (getIntrinsicID()) {
214  default:
215  return false;
216  case Intrinsic::experimental_constrained_fma:
217  return true;
218  }
219 }
220 
222  switch (getIntrinsicID()) {
223  case Intrinsic::uadd_with_overflow:
224  case Intrinsic::sadd_with_overflow:
225  case Intrinsic::uadd_sat:
226  case Intrinsic::sadd_sat:
227  return Instruction::Add;
228  case Intrinsic::usub_with_overflow:
229  case Intrinsic::ssub_with_overflow:
230  case Intrinsic::usub_sat:
231  case Intrinsic::ssub_sat:
232  return Instruction::Sub;
233  case Intrinsic::umul_with_overflow:
234  case Intrinsic::smul_with_overflow:
235  return Instruction::Mul;
236  default:
237  llvm_unreachable("Invalid intrinsic");
238  }
239 }
240 
242  switch (getIntrinsicID()) {
243  case Intrinsic::sadd_with_overflow:
244  case Intrinsic::ssub_with_overflow:
245  case Intrinsic::smul_with_overflow:
246  case Intrinsic::sadd_sat:
247  case Intrinsic::ssub_sat:
248  return true;
249  default:
250  return false;
251  }
252 }
253 
255  if (isSigned())
257  else
259 }
ExceptionBehavior
Specifies the required exception behavior.
This corresponds to "fpexcept.strict".
static Optional< RoundingMode > StrToRoundingMode(StringRef)
Returns a valid RoundingMode enumerator when given a string that is valid as input in constrained int...
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
iterator begin() const
Definition: ArrayRef.h:136
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
This corresponds to "fpround.upward".
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
Optional< ExceptionBehavior > getExceptionBehavior() const
This corresponds to "fpround.downward".
This file contains the declarations for metadata subclasses.
This corresponds to "fpexcept.maytrap".
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:176
This corresponds to "fpexcept.ignore".
unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
uint64_t High
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
Optional< uint64_t > getFragmentSizeInBits() const
Get the size (in bits) of the variable, or fragment of the variable that is described.
This corresponds to "fpround.dynamic".
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1241
static Optional< StringRef > ExceptionBehaviorToStr(ExceptionBehavior)
For any ExceptionBehavior enumerator, returns a string valid as input in constrained intrinsic except...
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:234
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isSigned() const
Whether the intrinsic is signed or unsigned.
DIExpression * getExpression() const
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:285
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:173
static bool classof(const IntrinsicInst *I)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Optional< RoundingMode > getRoundingMode() const
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:50
unsigned getNumOperands() const
Definition: User.h:191
This corresponds to "fpround.tozero".
Optional< uint64_t > getSizeInBits() const
Determines the size of the variable&#39;s type.
Module.h This file contains the declarations for the Module class.
iterator end() const
Definition: ArrayRef.h:137
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:640
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:55
static Optional< StringRef > RoundingModeToStr(RoundingMode)
For any RoundingMode enumerator, returns a string valid as input in constrained intrinsic rounding mo...
Value * getVariableLocation(bool AllowNullOp=true) const
Get the location corresponding to the variable referenced by the debug info intrinsic.
unsigned getNumArgOperands() const
Definition: InstrTypes.h:1239
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
static const size_t npos
Definition: StringRef.h:50
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
DILocalVariable * getVariable() const
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:122
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
static Optional< ExceptionBehavior > StrToExceptionBehavior(StringRef)
Returns a valid ExceptionBehavior enumerator when given a string valid as input in constrained intrin...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
int lookupLLVMIntrinsicByName(ArrayRef< const char *> NameTable, StringRef Name)
Looks up Name in NameTable via binary search.
Use & Op()
Definition: User.h:133
This corresponds to "fpround.tonearest".
RoundingMode
Specifies the rounding mode to be assumed.
Root of the metadata hierarchy.
Definition: Metadata.h:57