IntrinsicInst.h revision 9769ab22265b313171d201b5928688524a01bd87
1//===-- llvm/InstrinsicInst.h - Intrinsic Instruction Wrappers --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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  class IntrinsicInst : public CallInst {
34    IntrinsicInst();                      // DO NOT IMPLEMENT
35    IntrinsicInst(const IntrinsicInst&);  // DO NOT IMPLEMENT
36    void operator=(const IntrinsicInst&); // DO NOT IMPLEMENT
37  public:
38
39    /// StripPointerCasts - This static method strips off any unneeded pointer
40    /// casts from the specified value, returning the original uncasted value.
41    /// Note that the returned value is guaranteed to have pointer type.
42    static Value *StripPointerCasts(Value *Ptr);
43  };
44
45  /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
46  ///
47  struct DbgInfoIntrinsic : public IntrinsicInst {
48
49    Value *getChain() const { return const_cast<Value*>(getOperand(1)); }
50
51    // Methods for support type inquiry through isa, cast, and dyn_cast:
52    static inline bool classof(const DbgInfoIntrinsic *) { return true; }
53    static inline bool classof(const CallInst *I) {
54      if (const Function *CF = I->getCalledFunction())
55        switch (CF->getIntrinsicID()) {
56	case Intrinsic::dbg_stoppoint:
57	case Intrinsic::dbg_region_start:
58	case Intrinsic::dbg_region_end:
59	case Intrinsic::dbg_func_start:
60	case Intrinsic::dbg_declare:
61          return true;
62        default: break;
63        }
64      return false;
65    }
66    static inline bool classof(const Value *V) {
67      return isa<CallInst>(V) && classof(cast<CallInst>(V));
68    }
69  };
70
71
72  /// DbgStopPointInst - This represent llvm.dbg.stoppoint instructions.
73  ///
74  struct DbgStopPointInst : public DbgInfoIntrinsic {
75
76    unsigned getLineNo() const {
77      return unsigned(cast<ConstantInt>(getOperand(2))->getRawValue());
78    }
79    unsigned getColNo() const {
80      return unsigned(cast<ConstantInt>(getOperand(3))->getRawValue());
81    }
82    Value *getContext() const { return const_cast<Value*>(getOperand(4)); }
83
84
85    // Methods for support type inquiry through isa, cast, and dyn_cast:
86    static inline bool classof(const DbgStopPointInst *) { return true; }
87    static inline bool classof(const CallInst *I) {
88      if (const Function *CF = I->getCalledFunction())
89        return CF->getIntrinsicID() == Intrinsic::dbg_stoppoint;
90      return false;
91    }
92    static inline bool classof(const Value *V) {
93      return isa<CallInst>(V) && classof(cast<CallInst>(V));
94    }
95  };
96
97  /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
98  ///
99  struct MemIntrinsic : public IntrinsicInst {
100    Value *getRawDest() const { return const_cast<Value*>(getOperand(1)); }
101
102    Value *getLength() const { return const_cast<Value*>(getOperand(3)); }
103    ConstantInt *getAlignment() const {
104      return cast<ConstantInt>(const_cast<Value*>(getOperand(4)));
105    }
106
107    /// getDest - This is just like getRawDest, but it strips off any cast
108    /// instructions that feed it, giving the original input.  The returned
109    /// value is guaranteed to be a pointer.
110    Value *getDest() const { return StripPointerCasts(getRawDest()); }
111
112    /// set* - Set the specified arguments of the instruction.
113    ///
114    void setDest(Value *Ptr) {
115      assert(getRawDest()->getType() == Ptr->getType() &&
116             "setDest called with pointer of wrong type!");
117      setOperand(1, Ptr);
118    }
119
120    void setLength(Value *L) {
121      assert(getLength()->getType() == L->getType() &&
122             "setLength called with value of wrong type!");
123      setOperand(3, L);
124    }
125    void setAlignment(ConstantInt *A) {
126      assert(getAlignment()->getType() == A->getType() &&
127             "setAlignment called with value of wrong type!");
128      setOperand(4, A);
129    }
130
131    // Methods for support type inquiry through isa, cast, and dyn_cast:
132    static inline bool classof(const MemIntrinsic *) { return true; }
133    static inline bool classof(const CallInst *I) {
134      if (const Function *CF = I->getCalledFunction())
135        switch (CF->getIntrinsicID()) {
136        case Intrinsic::memcpy:
137        case Intrinsic::memmove:
138        case Intrinsic::memset:
139          return true;
140        default: break;
141        }
142      return false;
143    }
144    static inline bool classof(const Value *V) {
145      return isa<CallInst>(V) && classof(cast<CallInst>(V));
146    }
147  };
148
149
150  /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
151  ///
152  struct MemCpyInst : public MemIntrinsic {
153    /// get* - Return the arguments to the instruction.
154    ///
155    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
156
157    /// getSource - This is just like getRawSource, but it strips off any cast
158    /// instructions that feed it, giving the original input.  The returned
159    /// value is guaranteed to be a pointer.
160    Value *getSource() const { return StripPointerCasts(getRawSource()); }
161
162
163    void setSource(Value *Ptr) {
164      assert(getRawSource()->getType() == Ptr->getType() &&
165             "setSource called with pointer of wrong type!");
166      setOperand(2, Ptr);
167    }
168
169    // Methods for support type inquiry through isa, cast, and dyn_cast:
170    static inline bool classof(const MemCpyInst *) { return true; }
171    static inline bool classof(const MemIntrinsic *I) {
172      return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memcpy;
173    }
174    static inline bool classof(const CallInst *I) {
175      if (const Function *CF = I->getCalledFunction())
176        if (CF->getIntrinsicID() == Intrinsic::memcpy)
177          return true;
178      return false;
179    }
180    static inline bool classof(const Value *V) {
181      return isa<CallInst>(V) && classof(cast<CallInst>(V));
182    }
183  };
184
185  /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
186  ///
187  struct MemMoveInst : public MemIntrinsic {
188    /// get* - Return the arguments to the instruction.
189    ///
190    Value *getRawSource() const { return const_cast<Value*>(getOperand(2)); }
191
192    /// getSource - This is just like getRawSource, but it strips off any cast
193    /// instructions that feed it, giving the original input.  The returned
194    /// value is guaranteed to be a pointer.
195    Value *getSource() const { return StripPointerCasts(getRawSource()); }
196
197    void setSource(Value *Ptr) {
198      assert(getRawSource()->getType() == Ptr->getType() &&
199             "setSource called with pointer of wrong type!");
200      setOperand(2, Ptr);
201    }
202
203    // Methods for support type inquiry through isa, cast, and dyn_cast:
204    static inline bool classof(const MemMoveInst *) { return true; }
205    static inline bool classof(const MemIntrinsic *I) {
206      return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memmove;
207    }
208    static inline bool classof(const CallInst *I) {
209      if (const Function *CF = I->getCalledFunction())
210        if (CF->getIntrinsicID() == Intrinsic::memmove)
211          return true;
212      return false;
213    }
214    static inline bool classof(const Value *V) {
215      return isa<CallInst>(V) && classof(cast<CallInst>(V));
216    }
217  };
218
219  /// MemSetInst - This class wraps the llvm.memcpy intrinsic.
220  ///
221  struct MemSetInst : public MemIntrinsic {
222    /// get* - Return the arguments to the instruction.
223    ///
224    Value *getValue() const { return const_cast<Value*>(getOperand(2)); }
225
226    void setValue(Value *Val) {
227      assert(getValue()->getType() == Val->getType() &&
228             "setSource called with pointer of wrong type!");
229      setOperand(2, Val);
230    }
231
232    // Methods for support type inquiry through isa, cast, and dyn_cast:
233    static inline bool classof(const MemSetInst *) { return true; }
234    static inline bool classof(const MemIntrinsic *I) {
235      return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memset;
236    }
237    static inline bool classof(const CallInst *I) {
238      if (const Function *CF = I->getCalledFunction())
239        if (CF->getIntrinsicID() == Intrinsic::memset)
240          return true;
241      return false;
242    }
243    static inline bool classof(const Value *V) {
244      return isa<CallInst>(V) && classof(cast<CallInst>(V));
245    }
246  };
247}
248
249#endif
250