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