LLVM  4.0.0
IntrinsicInst.h
Go to the documentation of this file.
1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===//
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 defines classes that make it really easy to deal with intrinsic
11 // functions with the isa/dyncast family of functions. In particular, this
12 // allows you to do things like:
13 //
14 // if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
15 // ... MCI->getDest() ... MCI->getSource() ...
16 //
17 // All intrinsic function calls are instances of the call instruction, so these
18 // are all subclasses of the CallInst class. Note that none of these classes
19 // has state or virtual methods, which is an important part of this gross/neat
20 // hack working.
21 //
22 //===----------------------------------------------------------------------===//
23 
24 #ifndef LLVM_IR_INTRINSICINST_H
25 #define LLVM_IR_INTRINSICINST_H
26 
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/GlobalVariable.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/Intrinsics.h"
33 #include "llvm/IR/Metadata.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/Support/Casting.h"
36 #include <cassert>
37 #include <cstdint>
38 
39 namespace llvm {
40 
41  /// A wrapper class for inspecting calls to intrinsic functions.
42  /// This allows the standard isa/dyncast/cast functionality to work with calls
43  /// to intrinsic functions.
44  class IntrinsicInst : public CallInst {
45  public:
46  IntrinsicInst() = delete;
47  IntrinsicInst(const IntrinsicInst &) = delete;
48  IntrinsicInst &operator=(const IntrinsicInst &) = delete;
49 
50  /// Return the intrinsic ID of this intrinsic.
53  }
54 
55  // Methods for support type inquiry through isa, cast, and dyn_cast:
56  static inline bool classof(const CallInst *I) {
57  if (const Function *CF = I->getCalledFunction())
58  return CF->isIntrinsic();
59  return false;
60  }
61  static inline bool classof(const Value *V) {
62  return isa<CallInst>(V) && classof(cast<CallInst>(V));
63  }
64  };
65 
66  /// This is the common base class for debug info intrinsics.
68  public:
69  /// Get the location corresponding to the variable referenced by the debug
70  /// info intrinsic. Depending on the intrinsic, this could be the
71  /// variable's value or its address.
72  Value *getVariableLocation(bool AllowNullOp = true) const;
73 
74  // Methods for support type inquiry through isa, cast, and dyn_cast:
75  static inline bool classof(const IntrinsicInst *I) {
76  switch (I->getIntrinsicID()) {
77  case Intrinsic::dbg_declare:
78  case Intrinsic::dbg_value:
79  return true;
80  default: return false;
81  }
82  }
83  static inline bool classof(const Value *V) {
84  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
85  }
86  };
87 
88  /// This represents the llvm.dbg.declare instruction.
90  public:
91  Value *getAddress() const { return getVariableLocation(); }
92 
94  return cast<DILocalVariable>(getRawVariable());
95  }
96 
98  return cast<DIExpression>(getRawExpression());
99  }
100 
102  return cast<MetadataAsValue>(getArgOperand(1))->getMetadata();
103  }
104 
106  return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
107  }
108 
109  // Methods for support type inquiry through isa, cast, and dyn_cast:
110  static inline bool classof(const IntrinsicInst *I) {
111  return I->getIntrinsicID() == Intrinsic::dbg_declare;
112  }
113  static inline bool classof(const Value *V) {
114  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
115  }
116  };
117 
118  /// This represents the llvm.dbg.value instruction.
120  public:
121  Value *getValue() const {
122  return getVariableLocation(/* AllowNullOp = */ false);
123  }
124 
125  uint64_t getOffset() const {
126  return cast<ConstantInt>(
127  const_cast<Value*>(getArgOperand(1)))->getZExtValue();
128  }
129 
131  return cast<DILocalVariable>(getRawVariable());
132  }
133 
135  return cast<DIExpression>(getRawExpression());
136  }
137 
139  return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
140  }
141 
143  return cast<MetadataAsValue>(getArgOperand(3))->getMetadata();
144  }
145 
146  // Methods for support type inquiry through isa, cast, and dyn_cast:
147  static inline bool classof(const IntrinsicInst *I) {
148  return I->getIntrinsicID() == Intrinsic::dbg_value;
149  }
150  static inline bool classof(const Value *V) {
151  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
152  }
153  };
154 
155  /// This is the common base class for memset/memcpy/memmove.
156  class MemIntrinsic : public IntrinsicInst {
157  public:
158  Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
159  const Use &getRawDestUse() const { return getArgOperandUse(0); }
161 
162  Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
163  const Use &getLengthUse() const { return getArgOperandUse(2); }
165 
167  return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
168  }
169 
170  unsigned getAlignment() const {
171  return getAlignmentCst()->getZExtValue();
172  }
173 
175  return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
176  }
177 
178  bool isVolatile() const {
179  return !getVolatileCst()->isZero();
180  }
181 
182  unsigned getDestAddressSpace() const {
183  return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
184  }
185 
186  /// This is just like getRawDest, but it strips off any cast
187  /// instructions that feed it, giving the original input. The returned
188  /// value is guaranteed to be a pointer.
189  Value *getDest() const { return getRawDest()->stripPointerCasts(); }
190 
191  /// Set the specified arguments of the instruction.
192  void setDest(Value *Ptr) {
193  assert(getRawDest()->getType() == Ptr->getType() &&
194  "setDest called with pointer of wrong type!");
195  setArgOperand(0, Ptr);
196  }
197 
198  void setLength(Value *L) {
199  assert(getLength()->getType() == L->getType() &&
200  "setLength called with value of wrong type!");
201  setArgOperand(2, L);
202  }
203 
205  setArgOperand(3, A);
206  }
207 
209  setArgOperand(4, V);
210  }
211 
213  return getArgOperand(3)->getType();
214  }
215 
216  // Methods for support type inquiry through isa, cast, and dyn_cast:
217  static inline bool classof(const IntrinsicInst *I) {
218  switch (I->getIntrinsicID()) {
219  case Intrinsic::memcpy:
220  case Intrinsic::memmove:
221  case Intrinsic::memset:
222  return true;
223  default: return false;
224  }
225  }
226  static inline bool classof(const Value *V) {
227  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
228  }
229  };
230 
231  /// This class wraps the llvm.memset intrinsic.
232  class MemSetInst : public MemIntrinsic {
233  public:
234  /// Return the arguments to the instruction.
235  Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
236  const Use &getValueUse() const { return getArgOperandUse(1); }
237  Use &getValueUse() { return getArgOperandUse(1); }
238 
239  void setValue(Value *Val) {
240  assert(getValue()->getType() == Val->getType() &&
241  "setValue called with value of wrong type!");
242  setArgOperand(1, Val);
243  }
244 
245  // Methods for support type inquiry through isa, cast, and dyn_cast:
246  static inline bool classof(const IntrinsicInst *I) {
247  return I->getIntrinsicID() == Intrinsic::memset;
248  }
249  static inline bool classof(const Value *V) {
250  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
251  }
252  };
253 
254  /// This class wraps the llvm.memcpy/memmove intrinsics.
255  class MemTransferInst : public MemIntrinsic {
256  public:
257  /// Return the arguments to the instruction.
258  Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
259  const Use &getRawSourceUse() const { return getArgOperandUse(1); }
261 
262  /// This is just like getRawSource, but it strips off any cast
263  /// instructions that feed it, giving the original input. The returned
264  /// value is guaranteed to be a pointer.
265  Value *getSource() const { return getRawSource()->stripPointerCasts(); }
266 
267  unsigned getSourceAddressSpace() const {
268  return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
269  }
270 
271  void setSource(Value *Ptr) {
272  assert(getRawSource()->getType() == Ptr->getType() &&
273  "setSource called with pointer of wrong type!");
274  setArgOperand(1, Ptr);
275  }
276 
277  // Methods for support type inquiry through isa, cast, and dyn_cast:
278  static inline bool classof(const IntrinsicInst *I) {
279  return I->getIntrinsicID() == Intrinsic::memcpy ||
280  I->getIntrinsicID() == Intrinsic::memmove;
281  }
282  static inline bool classof(const Value *V) {
283  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
284  }
285  };
286 
287  /// This class wraps the llvm.memcpy intrinsic.
288  class MemCpyInst : public MemTransferInst {
289  public:
290  // Methods for support type inquiry through isa, cast, and dyn_cast:
291  static inline bool classof(const IntrinsicInst *I) {
292  return I->getIntrinsicID() == Intrinsic::memcpy;
293  }
294  static inline bool classof(const Value *V) {
295  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
296  }
297  };
298 
299  /// This class wraps the llvm.memmove intrinsic.
300  class MemMoveInst : public MemTransferInst {
301  public:
302  // Methods for support type inquiry through isa, cast, and dyn_cast:
303  static inline bool classof(const IntrinsicInst *I) {
304  return I->getIntrinsicID() == Intrinsic::memmove;
305  }
306  static inline bool classof(const Value *V) {
307  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
308  }
309  };
310 
311  /// This represents the llvm.va_start intrinsic.
312  class VAStartInst : public IntrinsicInst {
313  public:
314  static inline bool classof(const IntrinsicInst *I) {
315  return I->getIntrinsicID() == Intrinsic::vastart;
316  }
317  static inline bool classof(const Value *V) {
318  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
319  }
320 
321  Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
322  };
323 
324  /// This represents the llvm.va_end intrinsic.
325  class VAEndInst : public IntrinsicInst {
326  public:
327  static inline bool classof(const IntrinsicInst *I) {
328  return I->getIntrinsicID() == Intrinsic::vaend;
329  }
330  static inline bool classof(const Value *V) {
331  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
332  }
333 
334  Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
335  };
336 
337  /// This represents the llvm.va_copy intrinsic.
338  class VACopyInst : public IntrinsicInst {
339  public:
340  static inline bool classof(const IntrinsicInst *I) {
341  return I->getIntrinsicID() == Intrinsic::vacopy;
342  }
343  static inline bool classof(const Value *V) {
344  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
345  }
346 
347  Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
348  Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
349  };
350 
351  /// This represents the llvm.instrprof_increment intrinsic.
353  public:
354  static inline bool classof(const IntrinsicInst *I) {
355  return I->getIntrinsicID() == Intrinsic::instrprof_increment;
356  }
357  static inline bool classof(const Value *V) {
358  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
359  }
360 
362  return cast<GlobalVariable>(
363  const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
364  }
365 
366  ConstantInt *getHash() const {
367  return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
368  }
369 
371  return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
372  }
373 
375  return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
376  }
377 
378  Value *getStep() const;
379  };
380 
382  public:
383  static inline bool classof(const IntrinsicInst *I) {
384  return I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
385  }
386  static inline bool classof(const Value *V) {
387  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
388  }
389  };
390 
391  /// This represents the llvm.instrprof_value_profile intrinsic.
393  public:
394  static inline bool classof(const IntrinsicInst *I) {
395  return I->getIntrinsicID() == Intrinsic::instrprof_value_profile;
396  }
397  static inline bool classof(const Value *V) {
398  return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
399  }
400 
402  return cast<GlobalVariable>(
403  const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
404  }
405 
406  ConstantInt *getHash() const {
407  return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
408  }
409 
411  return cast<Value>(const_cast<Value *>(getArgOperand(2)));
412  }
413 
415  return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
416  }
417 
418  // Returns the value site index.
420  return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4)));
421  }
422  };
423 
424 } // end namespace llvm
425 
426 #endif // LLVM_IR_INTRINSICINST_H
unsigned getAlignment() const
MachineLoop * L
Value * getDest() const
static bool classof(const Value *V)
IntrinsicInst & operator=(const IntrinsicInst &)=delete
static bool classof(const IntrinsicInst *I)
Metadata * getRawVariable() const
bool isVolatile() const
This represents the llvm.va_end intrinsic.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:51
ConstantInt * getAlignmentCst() const
Value * getValue() const
Return the arguments to the instruction.
This class represents a function call, abstracting a target machine's calling convention.
This file contains the declarations for metadata subclasses.
static bool classof(const CallInst *I)
Definition: IntrinsicInst.h:56
static bool classof(const IntrinsicInst *I)
ConstantInt * getIndex() const
static bool classof(const Value *V)
This class wraps the llvm.memset intrinsic.
void setVolatile(Constant *V)
void setValue(Value *Val)
const Use & getArgOperandUse(unsigned i) const
Wrappers for getting the Use of a call argument.
void setDest(Value *Ptr)
Set the specified arguments of the instruction.
void setAlignment(Constant *A)
uint64_t getOffset() const
ConstantInt * getHash() const
static bool classof(const IntrinsicInst *I)
Definition: IntrinsicInst.h:75
Value * getAddress() const
Definition: IntrinsicInst.h:91
Value * getArgList() const
This class wraps the llvm.memmove intrinsic.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
ConstantInt * getIndex() const
Type * getAlignmentType() const
static bool classof(const Value *V)
static bool classof(const IntrinsicInst *I)
ConstantInt * getNumCounters() const
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:154
This represents the llvm.va_start intrinsic.
Metadata * getRawExpression() const
static bool classof(const Value *V)
ConstantInt * getHash() const
static bool classof(const Value *V)
GlobalVariable * getName() const
const Use & getValueUse() const
Value * getArgList() const
static bool classof(const Value *V)
static bool classof(const IntrinsicInst *I)
GlobalVariable * getName() const
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
DIExpression * getExpression() const
Definition: IntrinsicInst.h:97
This is an important base class in LLVM.
Definition: Constant.h:42
Value * getSrc() const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Value * getRawDest() const
static bool classof(const Value *V)
Metadata * getRawVariable() const
ConstantInt * getVolatileCst() const
static bool classof(const Value *V)
Definition: IntrinsicInst.h:83
static bool classof(const IntrinsicInst *I)
static bool classof(const IntrinsicInst *I)
static bool classof(const Value *V)
static bool classof(const IntrinsicInst *I)
static bool classof(const IntrinsicInst *I)
const Use & getRawSourceUse() const
DIExpression * getExpression() const
This is the common base class for memset/memcpy/memmove.
This is the shared class of boolean and integer constants.
Definition: Constants.h:88
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions that feed it, giving the original input.
Metadata * getRawExpression() const
static bool classof(const Value *V)
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:175
This is the common base class for debug info intrinsics.
Definition: IntrinsicInst.h:67
static bool classof(const Value *V)
Value * getLength() const
static bool classof(const IntrinsicInst *I)
Value * stripPointerCasts()
Strip off pointer casts, all-zero GEPs, and aliases.
Definition: Value.cpp:490
This class wraps the llvm.memcpy intrinsic.
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:198
static bool classof(const Value *V)
DWARF expression.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:146
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
static bool classof(const Value *V)
Value * getValue() const
void setLength(Value *L)
This represents the llvm.instrprof_increment intrinsic.
Value * getSource() const
This is just like getRawSource, but it strips off any cast instructions that feed it...
This class wraps the llvm.memcpy/memmove intrinsics.
This represents the llvm.dbg.value instruction.
#define I(x, y, z)
Definition: MD5.cpp:54
void setArgOperand(unsigned i, Value *v)
void setSource(Value *Ptr)
static bool classof(const Value *V)
Value * getVariableLocation(bool AllowNullOp=true) const
Get the location corresponding to the variable referenced by the debug info intrinsic.
static bool classof(const Value *V)
Definition: IntrinsicInst.h:61
static bool classof(const IntrinsicInst *I)
Value * getRawSource() const
Return the arguments to the instruction.
This represents the llvm.va_copy intrinsic.
DILocalVariable * getVariable() const
Definition: IntrinsicInst.h:93
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:71
DILocalVariable * getVariable() const
unsigned getDestAddressSpace() const
ConstantInt * getValueKind() const
int * Ptr
const Use & getLengthUse() const
This represents the llvm.instrprof_value_profile intrinsic.
const Use & getRawDestUse() const
unsigned getSourceAddressSpace() const
This represents the llvm.dbg.declare instruction.
Definition: IntrinsicInst.h:89
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
Root of the metadata hierarchy.
Definition: Metadata.h:55
static bool classof(const IntrinsicInst *I)
static bool classof(const IntrinsicInst *I)
static bool classof(const IntrinsicInst *I)
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44