LLVM  6.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"
27 #include "llvm/IR/GlobalVariable.h"
28 #include "llvm/IR/Metadata.h"
29 #include "llvm/IR/Module.h"
31 using namespace llvm;
32 
33 //===----------------------------------------------------------------------===//
34 /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
35 ///
36 
37 Value *DbgInfoIntrinsic::getVariableLocation(bool AllowNullOp) const {
38  Value *Op = getArgOperand(0);
39  if (AllowNullOp && !Op)
40  return nullptr;
41 
42  auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
43  if (auto *V = dyn_cast<ValueAsMetadata>(MD))
44  return V->getValue();
45 
46  // When the value goes to null, it gets replaced by an empty MDNode.
47  assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
48  return nullptr;
49 }
50 
52  StringRef Name) {
53  assert(Name.startswith("llvm."));
54 
55  // Do successive binary searches of the dotted name components. For
56  // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
57  // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
58  // "llvm.gc.experimental.statepoint", and then we will stop as the range is
59  // size 1. During the search, we can skip the prefix that we already know is
60  // identical. By using strncmp we consider names with differing suffixes to
61  // be part of the equal range.
62  size_t CmpStart = 0;
63  size_t CmpEnd = 4; // Skip the "llvm" component.
64  const char *const *Low = NameTable.begin();
65  const char *const *High = NameTable.end();
66  const char *const *LastLow = Low;
67  while (CmpEnd < Name.size() && High - Low > 0) {
68  CmpStart = CmpEnd;
69  CmpEnd = Name.find('.', CmpStart + 1);
70  CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
71  auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
72  return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
73  };
74  LastLow = Low;
75  std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
76  }
77  if (High - Low > 0)
78  LastLow = Low;
79 
80  if (LastLow == NameTable.end())
81  return -1;
82  StringRef NameFound = *LastLow;
83  if (Name == NameFound ||
84  (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
85  return LastLow - NameTable.begin();
86  return -1;
87 }
88 
91  return const_cast<Value *>(getArgOperand(4));
92  }
93  const Module *M = getModule();
95  return ConstantInt::get(Type::getInt64Ty(Context), 1);
96 }
97 
100  unsigned NumOperands = getNumArgOperands();
101  Metadata *MD =
102  dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 2))->getMetadata();
103  if (!MD || !isa<MDString>(MD))
104  return rmInvalid;
105  StringRef RoundingArg = cast<MDString>(MD)->getString();
106 
107  // For dynamic rounding mode, we use round to nearest but we will set the
108  // 'exact' SDNodeFlag so that the value will not be rounded.
109  return StringSwitch<RoundingMode>(RoundingArg)
110  .Case("round.dynamic", rmDynamic)
111  .Case("round.tonearest", rmToNearest)
112  .Case("round.downward", rmDownward)
113  .Case("round.upward", rmUpward)
114  .Case("round.towardzero", rmTowardZero)
115  .Default(rmInvalid);
116 }
117 
120  unsigned NumOperands = getNumArgOperands();
121  Metadata *MD =
122  dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 1))->getMetadata();
123  if (!MD || !isa<MDString>(MD))
124  return ebInvalid;
125  StringRef ExceptionArg = cast<MDString>(MD)->getString();
126  return StringSwitch<ExceptionBehavior>(ExceptionArg)
127  .Case("fpexcept.ignore", ebIgnore)
128  .Case("fpexcept.maytrap", ebMayTrap)
129  .Case("fpexcept.strict", ebStrict)
130  .Default(ebInvalid);
131 }
132 
134  switch (getIntrinsicID()) {
135  default:
136  return false;
137  case Intrinsic::experimental_constrained_sqrt:
138  case Intrinsic::experimental_constrained_sin:
139  case Intrinsic::experimental_constrained_cos:
140  case Intrinsic::experimental_constrained_exp:
141  case Intrinsic::experimental_constrained_exp2:
142  case Intrinsic::experimental_constrained_log:
143  case Intrinsic::experimental_constrained_log10:
144  case Intrinsic::experimental_constrained_log2:
145  case Intrinsic::experimental_constrained_rint:
146  case Intrinsic::experimental_constrained_nearbyint:
147  return true;
148  }
149 }
150 
152  switch (getIntrinsicID()) {
153  default:
154  return false;
155  case Intrinsic::experimental_constrained_fma:
156  return true;
157  }
158 }
159 
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
unsigned getNumArgOperands() const
Return the number of call arguments.
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:237
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:74
RoundingMode getRoundingMode() const
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:194
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
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...
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:172
static bool classof(const IntrinsicInst *I)
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
Definition: StringSwitch.h:244
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:51
unsigned getNumOperands() const
Definition: User.h:176
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:560
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:57
static const size_t npos
Definition: StringRef.h:51
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
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())
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.
Use & Op()
Definition: User.h:118
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