New header. Classes can be added as needed.
[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   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
46   /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
47   ///
48   struct MemIntrinsic : public IntrinsicInst {
49     Value *getRawDest() const { return const_cast<Value*>(getOperand(0)); }
50
51     Value *getLength() const { return const_cast<Value*>(getOperand(2)); }
52     ConstantInt *getAlignment() const {
53       return cast<ConstantInt>(const_cast<Value*>(getOperand(3)));
54     }
55
56     /// getDest - This is just like getRawDest, but it strips off any cast
57     /// instructions that feed it, giving the original input.  The returned
58     /// value is guaranteed to be a pointer.
59     Value *getDest() const { return StripPointerCasts(getRawDest()); }
60
61     /// set* - Set the specified arguments of the instruction.
62     ///
63     void setDest(Value *Ptr) {
64       assert(getRawDest()->getType() == Ptr->getType() &&
65              "setDest called with pointer of wrong type!");
66       setOperand(0, Ptr);
67     }
68
69     void setLength(Value *L) {
70       assert(getLength()->getType() == L->getType() &&
71              "setLength called with value of wrong type!");
72       setOperand(2, L);
73     }
74     void setAlignment(ConstantInt *A) {
75       assert(getAlignment()->getType() == A->getType() &&
76              "setAlignment called with value of wrong type!");
77       setOperand(3, A);
78     }
79
80     // Methods for support type inquiry through isa, cast, and dyn_cast:
81     static inline bool classof(const MemIntrinsic *) { return true; }
82     static inline bool classof(const CallInst *I) {
83       if (const Function *CF = I->getCalledFunction())
84         switch (CF->getIntrinsicID()) {
85         case Intrinsic::memcpy:
86         case Intrinsic::memmove:
87         case Intrinsic::memset:
88           return true;
89         default: break;
90         }
91       return false;
92     }
93     static inline bool classof(const Value *V) {
94       return isa<CallInst>(V) && classof(cast<CallInst>(V));
95     }
96   };
97
98
99   /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
100   ///
101   struct MemCpyInst : public MemIntrinsic {
102     /// get* - Return the arguments to the instruction.
103     ///
104     Value *getRawSource() const { return const_cast<Value*>(getOperand(1)); }
105
106     /// getSource - This is just like getRawSource, but it strips off any cast
107     /// instructions that feed it, giving the original input.  The returned
108     /// value is guaranteed to be a pointer.
109     Value *getSource() const { return StripPointerCasts(getRawSource()); }
110
111     
112     void setSource(Value *Ptr) {
113       assert(getRawSource()->getType() == Ptr->getType() &&
114              "setSource called with pointer of wrong type!");
115       setOperand(1, Ptr);
116     }
117
118     // Methods for support type inquiry through isa, cast, and dyn_cast:
119     static inline bool classof(const MemCpyInst *) { return true; }
120     static inline bool classof(const MemIntrinsic *I) {
121       return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memcpy;
122     }
123     static inline bool classof(const CallInst *I) {
124       if (const Function *CF = I->getCalledFunction())
125         if (CF->getIntrinsicID() == Intrinsic::memcpy)
126           return true;
127       return false;
128     }
129     static inline bool classof(const Value *V) {
130       return isa<CallInst>(V) && classof(cast<CallInst>(V));
131     }
132   };
133
134   /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
135   ///
136   struct MemMoveInst : public MemIntrinsic {
137     /// get* - Return the arguments to the instruction.
138     ///
139     Value *getRawSource() const { return const_cast<Value*>(getOperand(1)); }
140
141     /// getSource - This is just like getRawSource, but it strips off any cast
142     /// instructions that feed it, giving the original input.  The returned
143     /// value is guaranteed to be a pointer.
144     Value *getSource() const { return StripPointerCasts(getRawSource()); }
145
146     void setSource(Value *Ptr) {
147       assert(getRawSource()->getType() == Ptr->getType() &&
148              "setSource called with pointer of wrong type!");
149       setOperand(1, Ptr);
150     }
151
152     // Methods for support type inquiry through isa, cast, and dyn_cast:
153     static inline bool classof(const MemMoveInst *) { return true; }
154     static inline bool classof(const MemIntrinsic *I) {
155       return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memmove;
156     }
157     static inline bool classof(const CallInst *I) {
158       if (const Function *CF = I->getCalledFunction())
159         if (CF->getIntrinsicID() == Intrinsic::memmove)
160           return true;
161       return false;
162     }
163     static inline bool classof(const Value *V) {
164       return isa<CallInst>(V) && classof(cast<CallInst>(V));
165     }
166   };
167
168   /// MemSetInst - This class wraps the llvm.memcpy intrinsic.
169   ///
170   struct MemSetInst : public MemIntrinsic {
171     /// get* - Return the arguments to the instruction.
172     ///
173     Value *getValue() const { return const_cast<Value*>(getOperand(1)); }
174
175     void setValue(Value *Val) {
176       assert(getValue()->getType() == Val->getType() &&
177              "setSource called with pointer of wrong type!");
178       setOperand(1, Val);
179     }
180
181     // Methods for support type inquiry through isa, cast, and dyn_cast:
182     static inline bool classof(const MemSetInst *) { return true; }
183     static inline bool classof(const MemIntrinsic *I) {
184       return I->getCalledFunction()->getIntrinsicID() == Intrinsic::memset;
185     }
186     static inline bool classof(const CallInst *I) {
187       if (const Function *CF = I->getCalledFunction())
188         if (CF->getIntrinsicID() == Intrinsic::memset)
189           return true;
190       return false;
191     }
192     static inline bool classof(const Value *V) {
193       return isa<CallInst>(V) && classof(cast<CallInst>(V));
194     }
195   };
196 }
197
198 #endif