IntrinsicInst.h revision a3da922a27da1b5db04bbbe6cbf4848a688b6786
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        return true;
74      default: return false;
75      }
76    }
77    static inline bool classof(const Value *V) {
78      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
79    }
80
81    static Value *StripCast(Value *C);
82  };
83
84  /// DbgStopPointInst - This represents the llvm.dbg.stoppoint instruction.
85  ///
86  struct DbgStopPointInst : public DbgInfoIntrinsic {
87    Value *getLineValue() const { return const_cast<Value*>(getOperand(1)); }
88    Value *getColumnValue() const { return const_cast<Value*>(getOperand(2)); }
89    MDNode *getContext() const {
90      return cast<MDNode>(getOperand(3));
91    }
92
93    unsigned getLine() const {
94      return unsigned(cast<ConstantInt>(getOperand(1))->getZExtValue());
95    }
96    unsigned getColumn() const {
97      return unsigned(cast<ConstantInt>(getOperand(2))->getZExtValue());
98    }
99
100    Value* getFileName() const;
101    Value* getDirectory() const;
102
103    // Methods for support type inquiry through isa, cast, and dyn_cast:
104    static inline bool classof(const DbgStopPointInst *) { return true; }
105    static inline bool classof(const IntrinsicInst *I) {
106      return I->getIntrinsicID() == Intrinsic::dbg_stoppoint;
107    }
108    static inline bool classof(const Value *V) {
109      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
110    }
111  };
112
113  /// DbgFuncStartInst - This represents the llvm.dbg.func.start instruction.
114  ///
115  struct DbgFuncStartInst : public DbgInfoIntrinsic {
116    MDNode *getSubprogram() const { return cast<MDNode>(getOperand(1)); }
117
118    // Methods for support type inquiry through isa, cast, and dyn_cast:
119    static inline bool classof(const DbgFuncStartInst *) { return true; }
120    static inline bool classof(const IntrinsicInst *I) {
121      return I->getIntrinsicID() == Intrinsic::dbg_func_start;
122    }
123    static inline bool classof(const Value *V) {
124      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
125    }
126  };
127
128  /// DbgRegionStartInst - This represents the llvm.dbg.region.start
129  /// instruction.
130  struct DbgRegionStartInst : public DbgInfoIntrinsic {
131    MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
132
133    // Methods for support type inquiry through isa, cast, and dyn_cast:
134    static inline bool classof(const DbgRegionStartInst *) { return true; }
135    static inline bool classof(const IntrinsicInst *I) {
136      return I->getIntrinsicID() == Intrinsic::dbg_region_start;
137    }
138    static inline bool classof(const Value *V) {
139      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
140    }
141  };
142
143  /// DbgRegionEndInst - This represents the llvm.dbg.region.end instruction.
144  ///
145  struct DbgRegionEndInst : public DbgInfoIntrinsic {
146    MDNode *getContext() const { return cast<MDNode>(getOperand(1)); }
147
148    // Methods for support type inquiry through isa, cast, and dyn_cast:
149    static inline bool classof(const DbgRegionEndInst *) { return true; }
150    static inline bool classof(const IntrinsicInst *I) {
151      return I->getIntrinsicID() == Intrinsic::dbg_region_end;
152    }
153    static inline bool classof(const Value *V) {
154      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
155    }
156  };
157
158  /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
159  ///
160  struct DbgDeclareInst : public DbgInfoIntrinsic {
161    Value *getAddress()  const { return getOperand(1); }
162    MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
163
164    // Methods for support type inquiry through isa, cast, and dyn_cast:
165    static inline bool classof(const DbgDeclareInst *) { return true; }
166    static inline bool classof(const IntrinsicInst *I) {
167      return I->getIntrinsicID() == Intrinsic::dbg_declare;
168    }
169    static inline bool classof(const Value *V) {
170      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
171    }
172  };
173
174  /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
175  ///
176  struct MemIntrinsic : public IntrinsicInst {
177    Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
178
179    Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
180    ConstantInt *getAlignmentCst() const {
181      return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
182    }
183
184    unsigned getAlignment() const {
185      return getAlignmentCst()->getZExtValue();
186    }
187
188    /// getDest - This is just like getRawDest, but it strips off any cast
189    /// instructions that feed it, giving the original input.  The returned
190    /// value is guaranteed to be a pointer.
191    Value *getDest() const { return getRawDest()->stripPointerCasts(); }
192
193    /// set* - Set the specified arguments of the instruction.
194    ///
195    void setDest(Value *Ptr) {
196      assert(getRawDest()->getType() == Ptr->getType() &&
197             "setDest called with pointer of wrong type!");
198      setOperand(1, Ptr);
199    }
200
201    void setLength(Value *L) {
202      assert(getLength()->getType() == L->getType() &&
203             "setLength called with value of wrong type!");
204      setOperand(3, L);
205    }
206
207    void setAlignment(Constant* A) {
208      setOperand(4, A);
209    }
210
211    const Type *getAlignmentType() const {
212      return getOperand(4)->getType();
213    }
214
215    // Methods for support type inquiry through isa, cast, and dyn_cast:
216    static inline bool classof(const MemIntrinsic *) { return true; }
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  /// MemSetInst - This class wraps the llvm.memset intrinsic.
232  ///
233  struct MemSetInst : public MemIntrinsic {
234    /// get* - Return the arguments to the instruction.
235    ///
236    Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
237
238    void setValue(Value *Val) {
239      assert(getValue()->getType() == Val->getType() &&
240             "setSource called with pointer of wrong type!");
241      setOperand(2, Val);
242    }
243
244    // Methods for support type inquiry through isa, cast, and dyn_cast:
245    static inline bool classof(const MemSetInst *) { return true; }
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  /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
255  ///
256  struct MemTransferInst : public MemIntrinsic {
257    /// get* - Return the arguments to the instruction.
258    ///
259    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
260
261    /// getSource - This is just like getRawSource, but it strips off any cast
262    /// instructions that feed it, giving the original input.  The returned
263    /// value is guaranteed to be a pointer.
264    Value *getSource() const { return getRawSource()->stripPointerCasts(); }
265
266    void setSource(Value *Ptr) {
267      assert(getRawSource()->getType() == Ptr->getType() &&
268             "setSource called with pointer of wrong type!");
269      setOperand(2, Ptr);
270    }
271
272    // Methods for support type inquiry through isa, cast, and dyn_cast:
273    static inline bool classof(const MemTransferInst *) { return true; }
274    static inline bool classof(const IntrinsicInst *I) {
275      return I->getIntrinsicID() == Intrinsic::memcpy ||
276             I->getIntrinsicID() == Intrinsic::memmove;
277    }
278    static inline bool classof(const Value *V) {
279      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
280    }
281  };
282
283
284  /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
285  ///
286  struct MemCpyInst : public MemTransferInst {
287    // Methods for support type inquiry through isa, cast, and dyn_cast:
288    static inline bool classof(const MemCpyInst *) { return true; }
289    static inline bool classof(const IntrinsicInst *I) {
290      return I->getIntrinsicID() == Intrinsic::memcpy;
291    }
292    static inline bool classof(const Value *V) {
293      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
294    }
295  };
296
297  /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
298  ///
299  struct MemMoveInst : public MemTransferInst {
300    // Methods for support type inquiry through isa, cast, and dyn_cast:
301    static inline bool classof(const MemMoveInst *) { return true; }
302    static inline bool classof(const IntrinsicInst *I) {
303      return I->getIntrinsicID() == Intrinsic::memmove;
304    }
305    static inline bool classof(const Value *V) {
306      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
307    }
308  };
309
310  /// EHSelectorInst - This represents the llvm.eh.selector instruction.
311  ///
312  struct EHSelectorInst : public IntrinsicInst {
313    // Methods for support type inquiry through isa, cast, and dyn_cast:
314    static inline bool classof(const EHSelectorInst *) { return true; }
315    static inline bool classof(const IntrinsicInst *I) {
316      return I->getIntrinsicID() == Intrinsic::eh_selector;
317    }
318    static inline bool classof(const Value *V) {
319      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
320    }
321  };
322
323  /// MemoryUseIntrinsic - This is the common base class for the memory use
324  /// marker intrinsics.
325  ///
326  struct MemoryUseIntrinsic : public IntrinsicInst {
327
328    // Methods for support type inquiry through isa, cast, and dyn_cast:
329    static inline bool classof(const MemoryUseIntrinsic *) { return true; }
330    static inline bool classof(const IntrinsicInst *I) {
331      switch (I->getIntrinsicID()) {
332      case Intrinsic::lifetime_start:
333      case Intrinsic::lifetime_end:
334      case Intrinsic::invariant_start:
335      case Intrinsic::invariant_end:
336        return true;
337      default: return false;
338      }
339    }
340    static inline bool classof(const Value *V) {
341      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
342    }
343  };
344
345}
346
347#endif
348