IntrinsicInst.h revision dfe964ce8c367248e587f2d9ecc7fac5ee2c6fdc
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 *getAlignmentCst() const {
180      return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
181    }
182
183    unsigned getAlignment() const {
184      return getAlignmentCst()->getZExtValue();
185    }
186
187    /// getDest - This is just like getRawDest, but it strips off any cast
188    /// instructions that feed it, giving the original input.  The returned
189    /// value is guaranteed to be a pointer.
190    Value *getDest() const { return getRawDest()->stripPointerCasts(); }
191
192    /// set* - Set the specified arguments of the instruction.
193    ///
194    void setDest(Value *Ptr) {
195      assert(getRawDest()->getType() == Ptr->getType() &&
196             "setDest called with pointer of wrong type!");
197      setOperand(1, Ptr);
198    }
199
200    void setLength(Value *L) {
201      assert(getLength()->getType() == L->getType() &&
202             "setLength called with value of wrong type!");
203      setOperand(3, L);
204    }
205    void setAlignment(unsigned A) {
206      const Type *Int32Ty = getOperand(4)->getType();
207      setOperand(4, ConstantInt::get(Int32Ty, A));
208    }
209
210    // Methods for support type inquiry through isa, cast, and dyn_cast:
211    static inline bool classof(const MemIntrinsic *) { return true; }
212    static inline bool classof(const IntrinsicInst *I) {
213      switch (I->getIntrinsicID()) {
214      case Intrinsic::memcpy:
215      case Intrinsic::memmove:
216      case Intrinsic::memset:
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  /// MemSetInst - This class wraps the llvm.memset intrinsic.
227  ///
228  struct MemSetInst : public MemIntrinsic {
229    /// get* - Return the arguments to the instruction.
230    ///
231    Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
232
233    void setValue(Value *Val) {
234      assert(getValue()->getType() == Val->getType() &&
235             "setSource called with pointer of wrong type!");
236      setOperand(2, Val);
237    }
238
239    // Methods for support type inquiry through isa, cast, and dyn_cast:
240    static inline bool classof(const MemSetInst *) { return true; }
241    static inline bool classof(const IntrinsicInst *I) {
242      return I->getIntrinsicID() == Intrinsic::memset;
243    }
244    static inline bool classof(const Value *V) {
245      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
246    }
247  };
248
249  /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
250  ///
251  struct MemTransferInst : public MemIntrinsic {
252    /// get* - Return the arguments to the instruction.
253    ///
254    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
255
256    /// getSource - This is just like getRawSource, but it strips off any cast
257    /// instructions that feed it, giving the original input.  The returned
258    /// value is guaranteed to be a pointer.
259    Value *getSource() const { return getRawSource()->stripPointerCasts(); }
260
261    void setSource(Value *Ptr) {
262      assert(getRawSource()->getType() == Ptr->getType() &&
263             "setSource called with pointer of wrong type!");
264      setOperand(2, Ptr);
265    }
266
267    // Methods for support type inquiry through isa, cast, and dyn_cast:
268    static inline bool classof(const MemTransferInst *) { return true; }
269    static inline bool classof(const IntrinsicInst *I) {
270      return I->getIntrinsicID() == Intrinsic::memcpy ||
271             I->getIntrinsicID() == Intrinsic::memmove;
272    }
273    static inline bool classof(const Value *V) {
274      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
275    }
276  };
277
278
279  /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
280  ///
281  struct MemCpyInst : public MemTransferInst {
282    // Methods for support type inquiry through isa, cast, and dyn_cast:
283    static inline bool classof(const MemCpyInst *) { return true; }
284    static inline bool classof(const IntrinsicInst *I) {
285      return I->getIntrinsicID() == Intrinsic::memcpy;
286    }
287    static inline bool classof(const Value *V) {
288      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
289    }
290  };
291
292  /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
293  ///
294  struct MemMoveInst : public MemTransferInst {
295    // Methods for support type inquiry through isa, cast, and dyn_cast:
296    static inline bool classof(const MemMoveInst *) { return true; }
297    static inline bool classof(const IntrinsicInst *I) {
298      return I->getIntrinsicID() == Intrinsic::memmove;
299    }
300    static inline bool classof(const Value *V) {
301      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
302    }
303  };
304
305  /// EHSelectorInst - This represents the llvm.eh.selector instruction.
306  ///
307  struct EHSelectorInst : public IntrinsicInst {
308    // Methods for support type inquiry through isa, cast, and dyn_cast:
309    static inline bool classof(const EHSelectorInst *) { return true; }
310    static inline bool classof(const IntrinsicInst *I) {
311      return I->getIntrinsicID() == Intrinsic::eh_selector_i32 ||
312             I->getIntrinsicID() == Intrinsic::eh_selector_i64;
313    }
314    static inline bool classof(const Value *V) {
315      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
316    }
317  };
318
319}
320
321#endif
322