LLVM  7.0.0svn
IntrinsicInst.cpp
Go to the documentation of this file.
1 //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements methods that make it really easy to deal with intrinsic
11 // functions.
12 //
13 // All intrinsic function calls are instances of the call instruction, so these
14 // are all subclasses of the CallInst class. Note that none of these classes
15 // has state or virtual methods, which is an important part of this gross/neat
16 // hack working.
17 //
18 // In some cases, arguments to intrinsics need to be generic and are defined as
19 // type pointer to empty struct { }*. To access the real item of interest the
20 // cast instruction needs to be stripped away.
21 //
22 //===----------------------------------------------------------------------===//
23 
24 #include "llvm/IR/IntrinsicInst.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 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
36 ///
37 
38 Value *DbgInfoIntrinsic::getVariableLocation(bool AllowNullOp) const {
39  Value *Op = getArgOperand(0);
40  if (AllowNullOp && !Op)
41  return nullptr;
42 
43  auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
44  if (auto *V = dyn_cast<ValueAsMetadata>(MD))
45  return V->getValue();
46 
47  // When the value goes to null, it gets replaced by an empty MDNode.
48  assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
49  return nullptr;
50 }
51 
53  if (auto Fragment = getExpression()->getFragmentInfo())
54  return Fragment->SizeInBits;
55  return getVariable()->getSizeInBits();
56 }
57 
59  StringRef Name) {
60  assert(Name.startswith("llvm."));
61 
62  // Do successive binary searches of the dotted name components. For
63  // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
64  // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
65  // "llvm.gc.experimental.statepoint", and then we will stop as the range is
66  // size 1. During the search, we can skip the prefix that we already know is
67  // identical. By using strncmp we consider names with differing suffixes to
68  // be part of the equal range.
69  size_t CmpStart = 0;
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  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  dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 2))->getMetadata();
110  if (!MD || !isa<MDString>(MD))
111  return rmInvalid;
112  StringRef RoundingArg = cast<MDString>(MD)->getString();
113 
114  // For dynamic rounding mode, we use round to nearest but we will set the
115  // 'exact' SDNodeFlag so that the value will not be rounded.
116  return StringSwitch<RoundingMode>(RoundingArg)
117  .Case("round.dynamic", rmDynamic)
118  .Case("round.tonearest", rmToNearest)
119  .Case("round.downward", rmDownward)
120  .Case("round.upward", rmUpward)
121  .Case("round.towardzero", rmTowardZero)
122  .Default(rmInvalid);
123 }
124 
127  unsigned NumOperands = getNumArgOperands();
128  Metadata *MD =
129  dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 1))->getMetadata();
130  if (!MD || !isa<MDString>(MD))
131  return ebInvalid;
132  StringRef ExceptionArg = cast<MDString>(MD)->getString();
133  return StringSwitch<ExceptionBehavior>(ExceptionArg)
134  .Case("fpexcept.ignore", ebIgnore)
135  .Case("fpexcept.maytrap", ebMayTrap)
136  .Case("fpexcept.strict", ebStrict)
137  .Default(ebInvalid);
138 }
139 
141  switch (getIntrinsicID()) {
142  default:
143  return false;
144  case Intrinsic::experimental_constrained_sqrt:
145  case Intrinsic::experimental_constrained_sin:
146  case Intrinsic::experimental_constrained_cos:
147  case Intrinsic::experimental_constrained_exp:
148  case Intrinsic::experimental_constrained_exp2:
149  case Intrinsic::experimental_constrained_log:
150  case Intrinsic::experimental_constrained_log10:
151  case Intrinsic::experimental_constrained_log2:
152  case Intrinsic::experimental_constrained_rint:
153  case Intrinsic::experimental_constrained_nearbyint:
154  return true;
155  }
156 }
157 
159  switch (getIntrinsicID()) {
160  default:
161  return false;
162  case Intrinsic::experimental_constrained_fma:
163  return true;
164  }
165 }
166 
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
iterator begin() const
Definition: ArrayRef.h:137
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
This file contains the declarations for metadata subclasses.
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:177
uint64_t High
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:242
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(T Value)
Definition: StringSwitch.h:203
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
RoundingMode getRoundingMode() const
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:200
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
ExceptionBehavior getExceptionBehavior() const
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
unsigned getNumOperands() const
Value * getVariableLocation(bool AllowNullOp=true) const
Get the location corresponding to the variable referenced by the debug info intrinsic.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DILocalVariable * getVariable() const
Definition: IntrinsicInst.h:80
DIExpression * getExpression() const
Definition: IntrinsicInst.h:84
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:172
static bool classof(const IntrinsicInst *I)
Optional< uint64_t > getFragmentSizeInBits() const
Get the size (in bits) of the variable, or fragment of the variable that is described.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:51
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:138
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:611
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:56
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
static const size_t npos
Definition: StringRef.h:51
unsigned getNumArgOperands() const
Return the number of call arguments.
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:323
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
LLVM Value Representation.
Definition: Value.h:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
int lookupLLVMIntrinsicByName(ArrayRef< const char *> NameTable, StringRef Name)
Looks up Name in NameTable via binary search.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:298
Root of the metadata hierarchy.
Definition: Metadata.h:58