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 CmpEnd = 4; // Skip the "llvm" component.
71  const char *const *Low = NameTable.begin();
72  const char *const *High = NameTable.end();
73  const char *const *LastLow = Low;
74  while (CmpEnd < Name.size() && High - Low > 0) {
75  size_t CmpStart = CmpEnd;
76  CmpEnd = Name.find('.', CmpStart + 1);
77  CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
78  auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
79  return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
80  };
81  LastLow = Low;
82  std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
83  }
84  if (High - Low > 0)
85  LastLow = Low;
86 
87  if (LastLow == NameTable.end())
88  return -1;
89  StringRef NameFound = *LastLow;
90  if (Name == NameFound ||
91  (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
92  return LastLow - NameTable.begin();
93  return -1;
94 }
95 
98  return const_cast<Value *>(getArgOperand(4));
99  }
100  const Module *M = getModule();
102  return ConstantInt::get(Type::getInt64Ty(Context), 1);
103 }
104 
107  unsigned NumOperands = getNumArgOperands();
108  Metadata *MD =
109  cast<MetadataAsValue>(getArgOperand(NumOperands - 2))->getMetadata();
110  if (!MD || !isa<MDString>(MD))
111  return None;
112  return StrToRoundingMode(cast<MDString>(MD)->getString());
113 }
114 
117  // For dynamic rounding mode, we use round to nearest but we will set the
118  // 'exact' SDNodeFlag so that the value will not be rounded.
119  return StringSwitch<Optional<RoundingMode>>(RoundingArg)
120  .Case("round.dynamic", rmDynamic)
121  .Case("round.tonearest", rmToNearest)
122  .Case("round.downward", rmDownward)
123  .Case("round.upward", rmUpward)
124  .Case("round.towardzero", rmTowardZero)
125  .Default(None);
126 }
127 
130  Optional<StringRef> RoundingStr = None;
131  switch (UseRounding) {
133  RoundingStr = "round.dynamic";
134  break;
136  RoundingStr = "round.tonearest";
137  break;
139  RoundingStr = "round.downward";
140  break;
142  RoundingStr = "round.upward";
143  break;
145  RoundingStr = "round.towardzero";
146  break;
147  }
148  return RoundingStr;
149 }
150 
153  unsigned NumOperands = getNumArgOperands();
154  Metadata *MD =
155  cast<MetadataAsValue>(getArgOperand(NumOperands - 1))->getMetadata();
156  if (!MD || !isa<MDString>(MD))
157  return None;
158  return StrToExceptionBehavior(cast<MDString>(MD)->getString());
159 }
160 
163  return StringSwitch<Optional<ExceptionBehavior>>(ExceptionArg)
164  .Case("fpexcept.ignore", ebIgnore)
165  .Case("fpexcept.maytrap", ebMayTrap)
166  .Case("fpexcept.strict", ebStrict)
167  .Default(None);
168 }
169 
172  Optional<StringRef> ExceptStr = None;
173  switch (UseExcept) {
175  ExceptStr = "fpexcept.strict";
176  break;
178  ExceptStr = "fpexcept.ignore";
179  break;
181  ExceptStr = "fpexcept.maytrap";
182  break;
183  }
184  return ExceptStr;
185 }
186 
188  switch (getIntrinsicID()) {
189  default:
190  return false;
191  case Intrinsic::experimental_constrained_fptosi:
192  case Intrinsic::experimental_constrained_fptoui:
193  case Intrinsic::experimental_constrained_fptrunc:
194  case Intrinsic::experimental_constrained_fpext:
195  case Intrinsic::experimental_constrained_sqrt:
196  case Intrinsic::experimental_constrained_sin:
197  case Intrinsic::experimental_constrained_cos:
198  case Intrinsic::experimental_constrained_exp:
199  case Intrinsic::experimental_constrained_exp2:
200  case Intrinsic::experimental_constrained_log:
201  case Intrinsic::experimental_constrained_log10:
202  case Intrinsic::experimental_constrained_log2:
203  case Intrinsic::experimental_constrained_lrint:
204  case Intrinsic::experimental_constrained_llrint:
205  case Intrinsic::experimental_constrained_rint:
206  case Intrinsic::experimental_constrained_nearbyint:
207  case Intrinsic::experimental_constrained_ceil:
208  case Intrinsic::experimental_constrained_floor:
209  case Intrinsic::experimental_constrained_lround:
210  case Intrinsic::experimental_constrained_llround:
211  case Intrinsic::experimental_constrained_round:
212  case Intrinsic::experimental_constrained_trunc:
213  return true;
214  }
215 }
216 
218  switch (getIntrinsicID()) {
219  default:
220  return false;
221  case Intrinsic::experimental_constrained_fma:
222  return true;
223  }
224 }
225 
227  switch (getIntrinsicID()) {
228  case Intrinsic::uadd_with_overflow:
229  case Intrinsic::sadd_with_overflow:
230  case Intrinsic::uadd_sat:
231  case Intrinsic::sadd_sat:
232  return Instruction::Add;
233  case Intrinsic::usub_with_overflow:
234  case Intrinsic::ssub_with_overflow:
235  case Intrinsic::usub_sat:
236  case Intrinsic::ssub_sat:
237  return Instruction::Sub;
238  case Intrinsic::umul_with_overflow:
239  case Intrinsic::smul_with_overflow:
240  return Instruction::Mul;
241  default:
242  llvm_unreachable("Invalid intrinsic");
243  }
244 }
245 
247  switch (getIntrinsicID()) {
248  case Intrinsic::sadd_with_overflow:
249  case Intrinsic::ssub_with_overflow:
250  case Intrinsic::smul_with_overflow:
251  case Intrinsic::sadd_sat:
252  case Intrinsic::ssub_sat:
253  return true;
254  default:
255  return false;
256  }
257 }
258 
260  if (isSigned())
262  else
264 }
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:270
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:181
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:244
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:144
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:299
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:653
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
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:136
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