IntrinsicInst.h revision 824b958e6fb1236e92e4d07f3acf18fca107cdc0
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:
212      case Intrinsic::memmove:
213      case Intrinsic::memset:
214        return true;
215      default: return false;
216      }
217    }
218    static inline bool classof(const Value *V) {
219      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
220    }
221  };
222
223
224  /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
225  ///
226  struct MemCpyInst : public MemIntrinsic {
227    /// get* - Return the arguments to the instruction.
228    ///
229    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
230
231    /// getSource - This is just like getRawSource, but it strips off any cast
232    /// instructions that feed it, giving the original input.  The returned
233    /// value is guaranteed to be a pointer.
234    Value *getSource() const { return getRawSource()->stripPointerCasts(); }
235
236
237    void setSource(Value *Ptr) {
238      assert(getRawSource()->getType() == Ptr->getType() &&
239             "setSource called with pointer of wrong type!");
240      setOperand(2, Ptr);
241    }
242
243    // Methods for support type inquiry through isa, cast, and dyn_cast:
244    static inline bool classof(const MemCpyInst *) { return true; }
245    static inline bool classof(const IntrinsicInst *I) {
246      return I->getIntrinsicID() == Intrinsic::memcpy;
247    }
248    static inline bool classof(const Value *V) {
249      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
250    }
251  };
252
253  /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
254  ///
255  struct MemMoveInst : public MemIntrinsic {
256    /// get* - Return the arguments to the instruction.
257    ///
258    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
259
260    /// getSource - This is just like getRawSource, but it strips off any cast
261    /// instructions that feed it, giving the original input.  The returned
262    /// value is guaranteed to be a pointer.
263    Value *getSource() const { return getRawSource()->stripPointerCasts(); }
264
265    void setSource(Value *Ptr) {
266      assert(getRawSource()->getType() == Ptr->getType() &&
267             "setSource called with pointer of wrong type!");
268      setOperand(2, Ptr);
269    }
270
271    // Methods for support type inquiry through isa, cast, and dyn_cast:
272    static inline bool classof(const MemMoveInst *) { return true; }
273    static inline bool classof(const IntrinsicInst *I) {
274      return I->getIntrinsicID() == Intrinsic::memmove;
275    }
276    static inline bool classof(const Value *V) {
277      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
278    }
279  };
280
281  /// MemSetInst - This class wraps the llvm.memset intrinsic.
282  ///
283  struct MemSetInst : public MemIntrinsic {
284    /// get* - Return the arguments to the instruction.
285    ///
286    Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
287
288    void setValue(Value *Val) {
289      assert(getValue()->getType() == Val->getType() &&
290             "setSource called with pointer of wrong type!");
291      setOperand(2, Val);
292    }
293
294    // Methods for support type inquiry through isa, cast, and dyn_cast:
295    static inline bool classof(const MemSetInst *) { return true; }
296    static inline bool classof(const IntrinsicInst *I) {
297      return I->getIntrinsicID() == Intrinsic::memset;
298    }
299    static inline bool classof(const Value *V) {
300      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
301    }
302  };
303
304  /// EHSelectorInst - This represents the llvm.eh.selector instruction.
305  ///
306  struct EHSelectorInst : public IntrinsicInst {
307    // Methods for support type inquiry through isa, cast, and dyn_cast:
308    static inline bool classof(const EHSelectorInst *) { return true; }
309    static inline bool classof(const IntrinsicInst *I) {
310      return I->getIntrinsicID() == Intrinsic::eh_selector_i32 ||
311             I->getIntrinsicID() == Intrinsic::eh_selector_i64;
312    }
313    static inline bool classof(const Value *V) {
314      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
315    }
316  };
317
318}
319
320#endif
321