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