IntrinsicInst.h revision e754d3fb852abdeaf910c7331eed60f6303597c1
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  class DbgInfoIntrinsic : public IntrinsicInst {
62  public:
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_declare:
69      case Intrinsic::dbg_value:
70        return true;
71      default: return false;
72      }
73    }
74    static inline bool classof(const Value *V) {
75      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
76    }
77
78    static Value *StripCast(Value *C);
79  };
80
81  /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
82  ///
83  class DbgDeclareInst : public DbgInfoIntrinsic {
84  public:
85    Value *getAddress() const;
86    MDNode *getVariable() const { return cast<MDNode>(getOperand(2)); }
87
88    // Methods for support type inquiry through isa, cast, and dyn_cast:
89    static inline bool classof(const DbgDeclareInst *) { return true; }
90    static inline bool classof(const IntrinsicInst *I) {
91      return I->getIntrinsicID() == Intrinsic::dbg_declare;
92    }
93    static inline bool classof(const Value *V) {
94      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
95    }
96  };
97
98  /// DbgValueInst - This represents the llvm.dbg.value instruction.
99  ///
100  class DbgValueInst : public DbgInfoIntrinsic {
101  public:
102    const Value *getValue() const;
103    Value *getValue();
104    uint64_t getOffset() const {
105      return cast<ConstantInt>(
106                             const_cast<Value*>(getOperand(2)))->getZExtValue();
107    }
108    const MDNode *getVariable() const { return cast<MDNode>(getOperand(3)); }
109    MDNode *getVariable() { return cast<MDNode>(getOperand(3)); }
110
111    // Methods for support type inquiry through isa, cast, and dyn_cast:
112    static inline bool classof(const DbgValueInst *) { return true; }
113    static inline bool classof(const IntrinsicInst *I) {
114      return I->getIntrinsicID() == Intrinsic::dbg_value;
115    }
116    static inline bool classof(const Value *V) {
117      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
118    }
119  };
120
121  /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
122  ///
123  class MemIntrinsic : public IntrinsicInst {
124  public:
125    Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
126
127    Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
128    ConstantInt *getAlignmentCst() const {
129      return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
130    }
131
132    unsigned getAlignment() const {
133      return getAlignmentCst()->getZExtValue();
134    }
135
136    /// getDest - This is just like getRawDest, but it strips off any cast
137    /// instructions that feed it, giving the original input.  The returned
138    /// value is guaranteed to be a pointer.
139    Value *getDest() const { return getRawDest()->stripPointerCasts(); }
140
141    /// set* - Set the specified arguments of the instruction.
142    ///
143    void setDest(Value *Ptr) {
144      assert(getRawDest()->getType() == Ptr->getType() &&
145             "setDest called with pointer of wrong type!");
146      setOperand(1, Ptr);
147    }
148
149    void setLength(Value *L) {
150      assert(getLength()->getType() == L->getType() &&
151             "setLength called with value of wrong type!");
152      setOperand(3, L);
153    }
154
155    void setAlignment(Constant* A) {
156      setOperand(4, A);
157    }
158
159    const Type *getAlignmentType() const {
160      return getOperand(4)->getType();
161    }
162
163    // Methods for support type inquiry through isa, cast, and dyn_cast:
164    static inline bool classof(const MemIntrinsic *) { return true; }
165    static inline bool classof(const IntrinsicInst *I) {
166      switch (I->getIntrinsicID()) {
167      case Intrinsic::memcpy:
168      case Intrinsic::memmove:
169      case Intrinsic::memset:
170        return true;
171      default: return false;
172      }
173    }
174    static inline bool classof(const Value *V) {
175      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
176    }
177  };
178
179  /// MemSetInst - This class wraps the llvm.memset intrinsic.
180  ///
181  class MemSetInst : public MemIntrinsic {
182  public:
183    /// get* - Return the arguments to the instruction.
184    ///
185    Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
186
187    void setValue(Value *Val) {
188      assert(getValue()->getType() == Val->getType() &&
189             "setSource called with pointer of wrong type!");
190      setOperand(2, Val);
191    }
192
193    // Methods for support type inquiry through isa, cast, and dyn_cast:
194    static inline bool classof(const MemSetInst *) { return true; }
195    static inline bool classof(const IntrinsicInst *I) {
196      return I->getIntrinsicID() == Intrinsic::memset;
197    }
198    static inline bool classof(const Value *V) {
199      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
200    }
201  };
202
203  /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
204  ///
205  class MemTransferInst : public MemIntrinsic {
206  public:
207    /// get* - Return the arguments to the instruction.
208    ///
209    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
210
211    /// getSource - This is just like getRawSource, but it strips off any cast
212    /// instructions that feed it, giving the original input.  The returned
213    /// value is guaranteed to be a pointer.
214    Value *getSource() const { return getRawSource()->stripPointerCasts(); }
215
216    void setSource(Value *Ptr) {
217      assert(getRawSource()->getType() == Ptr->getType() &&
218             "setSource called with pointer of wrong type!");
219      setOperand(2, Ptr);
220    }
221
222    // Methods for support type inquiry through isa, cast, and dyn_cast:
223    static inline bool classof(const MemTransferInst *) { return true; }
224    static inline bool classof(const IntrinsicInst *I) {
225      return I->getIntrinsicID() == Intrinsic::memcpy ||
226             I->getIntrinsicID() == Intrinsic::memmove;
227    }
228    static inline bool classof(const Value *V) {
229      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
230    }
231  };
232
233
234  /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
235  ///
236  class MemCpyInst : public MemTransferInst {
237  public:
238    // Methods for support type inquiry through isa, cast, and dyn_cast:
239    static inline bool classof(const MemCpyInst *) { return true; }
240    static inline bool classof(const IntrinsicInst *I) {
241      return I->getIntrinsicID() == Intrinsic::memcpy;
242    }
243    static inline bool classof(const Value *V) {
244      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
245    }
246  };
247
248  /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
249  ///
250  class MemMoveInst : public MemTransferInst {
251  public:
252    // Methods for support type inquiry through isa, cast, and dyn_cast:
253    static inline bool classof(const MemMoveInst *) { return true; }
254    static inline bool classof(const IntrinsicInst *I) {
255      return I->getIntrinsicID() == Intrinsic::memmove;
256    }
257    static inline bool classof(const Value *V) {
258      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
259    }
260  };
261
262  /// EHSelectorInst - This represents the llvm.eh.selector instruction.
263  ///
264  class EHSelectorInst : public IntrinsicInst {
265  public:
266    // Methods for support type inquiry through isa, cast, and dyn_cast:
267    static inline bool classof(const EHSelectorInst *) { return true; }
268    static inline bool classof(const IntrinsicInst *I) {
269      return I->getIntrinsicID() == Intrinsic::eh_selector;
270    }
271    static inline bool classof(const Value *V) {
272      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
273    }
274  };
275
276  /// MemoryUseIntrinsic - This is the common base class for the memory use
277  /// marker intrinsics.
278  ///
279  class MemoryUseIntrinsic : public IntrinsicInst {
280  public:
281
282    // Methods for support type inquiry through isa, cast, and dyn_cast:
283    static inline bool classof(const MemoryUseIntrinsic *) { return true; }
284    static inline bool classof(const IntrinsicInst *I) {
285      switch (I->getIntrinsicID()) {
286      case Intrinsic::lifetime_start:
287      case Intrinsic::lifetime_end:
288      case Intrinsic::invariant_start:
289      case Intrinsic::invariant_end:
290        return true;
291      default: return false;
292      }
293    }
294    static inline bool classof(const Value *V) {
295      return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
296    }
297  };
298
299}
300
301#endif
302