IntrinsicInst.h revision 5d0cacdbb6577f2449986f345858db17dc1bcf59
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_INTRINSICINST_H
25#define LLVM_INTRINSICINST_H
26
27#include "llvm/Constants.h"
28#include "llvm/Metadata.h"
29#include "llvm/Function.h"
30#include "llvm/Instructions.h"
31#include "llvm/Intrinsics.h"
32
33namespace llvm {
34  /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic
35  /// functions.  This allows the standard isa/dyncast/cast functionality to
36  /// work with calls to intrinsic functions.
37  class IntrinsicInst : public CallInst {
38    IntrinsicInst();                      // DO NOT IMPLEMENT
39    IntrinsicInst(const IntrinsicInst&);  // DO NOT IMPLEMENT
40    void operator=(const IntrinsicInst&); // DO NOT IMPLEMENT
41  public:
42    /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
43    ///
44    Intrinsic::ID getIntrinsicID() const {
45      return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
46    }
47
48    // Methods for support type inquiry through isa, cast, and dyn_cast:
49    static inline bool classof(const IntrinsicInst *) { return true; }
50    static inline bool classof(const CallInst *I) {
51      if (const Function *CF = I->getCalledFunction())
52        return CF->getIntrinsicID() != 0;
53      return false;
54    }
55    static inline bool classof(const Value *V) {
56      return isa<CallInst>(V) && classof(cast<CallInst>(V));
57    }
58  };
59
60  /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
61  ///
62  struct DbgInfoIntrinsic : public IntrinsicInst {
63
64    // Methods for support type inquiry through isa, cast, and dyn_cast:
65    static inline bool classof(const DbgInfoIntrinsic *) { return true; }
66    static inline bool classof(const IntrinsicInst *I) {
67      switch (I->getIntrinsicID()) {
68      case Intrinsic::dbg_stoppoint:
69      case Intrinsic::dbg_func_start:
70      case Intrinsic::dbg_region_start:
71      case Intrinsic::dbg_region_end:
72      case Intrinsic::dbg_declare:
73      case Intrinsic::dbg_value:
74        return true;
75      default: return false;
76      }
77    }
78    static inline bool classof(const Value *V) {
79      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
80    }
81
82    static Value *StripCast(Value *C);
83  };
84
85  /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
86  ///
87  struct DbgStopPointInst : public DbgInfoIntrinsic {
88    Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
89    Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
90    MDNode *getContext() const {
91      return cast<MDNode>(getOperand(3));
92    }
93
94    unsigned getLine() const {
95      return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
96    }
97    unsigned getColumn() const {
98      return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
99    }
100
101    Value *getFileName() const;
102    Value *getDirectory() const;
103
104    // Methods for support type inquiry through isa, cast, and dyn_cast:
105    static inline bool classof(const DbgStopPointInst *) { return true; }
106    static inline bool classof(const IntrinsicInst *I) {
107      return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
108    }
109    static inline bool classof(const Value *V) {
110      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
111    }
112  };
113
114  /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
115  ///
116  struct DbgFuncStartInst : public DbgInfoIntrinsic {
117    MDNode *getSubprogram() const { return cast<MDNode>(getOperand(1)); }
118
119    // Methods for support type inquiry through isa, cast, and dyn_cast:
120    static inline bool classof(const DbgFuncStartInst *) { return true; }
121    static inline bool classof(const IntrinsicInst *I) {
122      return I->getIntrinsicID() == Intrinsic::dbg_func_start;
123    }
124    static inline bool classof(const Value *V) {
125      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
126    }
127  };
128
129  /// DbgRegionStartInst - This represents the llvm.dbg.region.start
130  /// instruction.
131  struct DbgRegionStartInst : public DbgInfoIntrinsic {
132    MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
133
134    // Methods for support type inquiry through isa, cast, and dyn_cast:
135    static inline bool classof(const DbgRegionStartInst *) { return true; }
136    static inline bool classof(const IntrinsicInst *I) {
137      return I->getIntrinsicID() == Intrinsic::dbg_region_start;
138    }
139    static inline bool classof(const Value *V) {
140      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
141    }
142  };
143
144  /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
145  ///
146  struct DbgRegionEndInst : public DbgInfoIntrinsic {
147    MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
148
149    // Methods for support type inquiry through isa, cast, and dyn_cast:
150    static inline bool classof(const DbgRegionEndInst *) { return true; }
151    static inline bool classof(const IntrinsicInst *I) {
152      return I->getIntrinsicID() == Intrinsic::dbg_region_end;
153    }
154    static inline bool classof(const Value *V) {
155      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
156    }
157  };
158
159  /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
160  ///
161  struct DbgDeclareInst : public DbgInfoIntrinsic {
162    Value *getAddress()  const { return getOperand(1); }
163    MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
164
165    // Methods for support type inquiry through isa, cast, and dyn_cast:
166    static inline bool classof(const DbgDeclareInst *) { return true; }
167    static inline bool classof(const IntrinsicInst *I) {
168      return I->getIntrinsicID() == Intrinsic::dbg_declare;
169    }
170    static inline bool classof(const Value *V) {
171      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
172    }
173  };
174
175  /// DbgValueInst - This represents the llvm.dbg.value instruction.
176  ///
177  struct DbgValueInst : public DbgInfoIntrinsic {
178    Value *getValue() const {
179      return cast<MDNode>(getOperand(1))->getOperand(0);
180    }
181    Value *getOffset() const { return getOperand(2); }
182    MDNode *getVariable() const { return cast<MDNode>(getOperand(3)); }
183
184    // Methods for support type inquiry through isa, cast, and dyn_cast:
185    static inline bool classof(const DbgValueInst *) { return true; }
186    static inline bool classof(const IntrinsicInst *I) {
187      return I->getIntrinsicID() == Intrinsic::dbg_value;
188    }
189    static inline bool classof(const Value *V) {
190      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
191    }
192  };
193
194  /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
195  ///
196  struct MemIntrinsic : public IntrinsicInst {
197    Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
198
199    Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
200    ConstantInt *getAlignmentCst() const {
201      return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
202    }
203
204    unsigned getAlignment() const {
205      return getAlignmentCst()->getZExtValue();
206    }
207
208    /// getDest - This is just like getRawDest, but it strips off any cast
209    /// instructions that feed it, giving the original input.  The returned
210    /// value is guaranteed to be a pointer.
211    Value *getDest() const { return getRawDest()->stripPointerCasts(); }
212
213    /// set* - Set the specified arguments of the instruction.
214    ///
215    void setDest(Value *Ptr) {
216      assert(getRawDest()->getType() == Ptr->getType() &&
217             "setDest called with pointer of wrong type!");
218      setOperand(1, Ptr);
219    }
220
221    void setLength(Value *L) {
222      assert(getLength()->getType() == L->getType() &&
223             "setLength called with value of wrong type!");
224      setOperand(3, L);
225    }
226
227    void setAlignment(Constant* A) {
228      setOperand(4, A);
229    }
230
231    const Type *getAlignmentType() const {
232      return getOperand(4)->getType();
233    }
234
235    // Methods for support type inquiry through isa, cast, and dyn_cast:
236    static inline bool classof(const MemIntrinsic *) { return true; }
237    static inline bool classof(const IntrinsicInst *I) {
238      switch (I->getIntrinsicID()) {
239      case Intrinsic::memcpy:
240      case Intrinsic::memmove:
241      case Intrinsic::memset:
242        return true;
243      default: return false;
244      }
245    }
246    static inline bool classof(const Value *V) {
247      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
248    }
249  };
250
251  /// MemSetInst - This class wraps the llvm.memset intrinsic.
252  ///
253  struct MemSetInst : public MemIntrinsic {
254    /// get* - Return the arguments to the instruction.
255    ///
256    Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
257
258    void setValue(Value *Val) {
259      assert(getValue()->getType() == Val->getType() &&
260             "setSource called with pointer of wrong type!");
261      setOperand(2, Val);
262    }
263
264    // Methods for support type inquiry through isa, cast, and dyn_cast:
265    static inline bool classof(const MemSetInst *) { return true; }
266    static inline bool classof(const IntrinsicInst *I) {
267      return I->getIntrinsicID() == Intrinsic::memset;
268    }
269    static inline bool classof(const Value *V) {
270      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
271    }
272  };
273
274  /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
275  ///
276  struct MemTransferInst : public MemIntrinsic {
277    /// get* - Return the arguments to the instruction.
278    ///
279    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
280
281    /// getSource - This is just like getRawSource, but it strips off any cast
282    /// instructions that feed it, giving the original input.  The returned
283    /// value is guaranteed to be a pointer.
284    Value *getSource() const { return getRawSource()->stripPointerCasts(); }
285
286    void setSource(Value *Ptr) {
287      assert(getRawSource()->getType() == Ptr->getType() &&
288             "setSource called with pointer of wrong type!");
289      setOperand(2, Ptr);
290    }
291
292    // Methods for support type inquiry through isa, cast, and dyn_cast:
293    static inline bool classof(const MemTransferInst *) { return true; }
294    static inline bool classof(const IntrinsicInst *I) {
295      return I->getIntrinsicID() == Intrinsic::memcpy ||
296             I->getIntrinsicID() == Intrinsic::memmove;
297    }
298    static inline bool classof(const Value *V) {
299      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
300    }
301  };
302
303
304  /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
305  ///
306  struct MemCpyInst : public MemTransferInst {
307    // Methods for support type inquiry through isa, cast, and dyn_cast:
308    static inline bool classof(const MemCpyInst *) { return true; }
309    static inline bool classof(const IntrinsicInst *I) {
310      return I->getIntrinsicID() == Intrinsic::memcpy;
311    }
312    static inline bool classof(const Value *V) {
313      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
314    }
315  };
316
317  /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
318  ///
319  struct MemMoveInst : public MemTransferInst {
320    // Methods for support type inquiry through isa, cast, and dyn_cast:
321    static inline bool classof(const MemMoveInst *) { return true; }
322    static inline bool classof(const IntrinsicInst *I) {
323      return I->getIntrinsicID() == Intrinsic::memmove;
324    }
325    static inline bool classof(const Value *V) {
326      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
327    }
328  };
329
330  /// EHSelectorInst - This represents the llvm.eh.selector instruction.
331  ///
332  struct EHSelectorInst : public IntrinsicInst {
333    // Methods for support type inquiry through isa, cast, and dyn_cast:
334    static inline bool classof(const EHSelectorInst *) { return true; }
335    static inline bool classof(const IntrinsicInst *I) {
336      return I->getIntrinsicID() == Intrinsic::eh_selector;
337    }
338    static inline bool classof(const Value *V) {
339      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
340    }
341  };
342
343  /// MemoryUseIntrinsic - This is the common base class for the memory use
344  /// marker intrinsics.
345  ///
346  struct MemoryUseIntrinsic : public IntrinsicInst {
347
348    // Methods for support type inquiry through isa, cast, and dyn_cast:
349    static inline bool classof(const MemoryUseIntrinsic *) { return true; }
350    static inline bool classof(const IntrinsicInst *I) {
351      switch (I->getIntrinsicID()) {
352      case Intrinsic::lifetime_start:
353      case Intrinsic::lifetime_end:
354      case Intrinsic::invariant_start:
355      case Intrinsic::invariant_end:
356        return true;
357      default: return false;
358      }
359    }
360    static inline bool classof(const Value *V) {
361      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
362    }
363  };
364
365}
366
367#endif
368