Add some useful accessors
[oota-llvm.git] / include / llvm / iOther.h
1 //===-- llvm/iOther.h - "Other" instruction node definitions -----*- C++ -*--=//
2 //
3 // This file contains the declarations for instructions that fall into the 
4 // grandios 'other' catagory...
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_IOTHER_H
9 #define LLVM_IOTHER_H
10
11 #include "llvm/InstrTypes.h"
12 #include "llvm/Method.h"
13
14 //===----------------------------------------------------------------------===//
15 //                               PHINode Class
16 //===----------------------------------------------------------------------===//
17
18 // PHINode - The PHINode class is used to represent the magical mystical PHI
19 // node, that can not exist in nature, but can be synthesized in a computer
20 // scientist's overactive imagination.
21 //
22 class PHINode : public Instruction {
23   PHINode(const PHINode &PN);
24 public:
25   PHINode(const Type *Ty, const string &Name = "");
26
27   virtual Instruction *clone() const { return new PHINode(*this); }
28   virtual const char *getOpcodeName() const { return "phi"; }
29
30   // getNumIncomingValues - Return the number of incoming edges the PHI node has
31   inline unsigned getNumIncomingValues() const { return Operands.size()/2; }
32
33   // getIncomingValue - Return incoming value #x
34   inline const Value *getIncomingValue(unsigned i) const {
35     return Operands[i*2];
36   }
37   inline Value *getIncomingValue(unsigned i) {
38     return Operands[i*2];
39   }
40   inline void setIncomingValue(unsigned i, Value *V) {
41     Operands[i*2] = V;
42   }
43
44   // getIncomingBlock - Return incoming basic block #x
45   inline const BasicBlock *getIncomingBlock(unsigned i) const { 
46     return cast<const BasicBlock>(Operands[i*2+1]);
47   }
48   inline BasicBlock *getIncomingBlock(unsigned i) { 
49     return cast<BasicBlock>(Operands[i*2+1]);
50   }
51
52   // addIncoming - Add an incoming value to the end of the PHI list
53   void addIncoming(Value *D, BasicBlock *BB);
54
55   // removeIncomingValue - Remove an incoming value.  This is useful if a
56   // predecessor basic block is deleted.  The value removed is returned.
57   Value *removeIncomingValue(const BasicBlock *BB);
58
59
60   // Methods for support type inquiry through isa, cast, and dyn_cast:
61   static inline bool classof(const PHINode *) { return true; }
62   static inline bool classof(const Instruction *I) {
63     return I->getOpcode() == Instruction::PHINode; 
64   }
65   static inline bool classof(const Value *V) {
66     return isa<Instruction>(V) && classof(cast<Instruction>(V));
67   }
68 };
69
70
71 //===----------------------------------------------------------------------===//
72 //                                 CastInst Class
73 //===----------------------------------------------------------------------===//
74
75 // CastInst - This class represents a cast from Operand[0] to the type of
76 // the instruction (i->getType()).
77 //
78 class CastInst : public Instruction {
79   CastInst(const CastInst &CI) : Instruction(CI.getType(), Cast) {
80     Operands.reserve(1);
81     Operands.push_back(Use(CI.Operands[0], this));
82   }
83 public:
84   CastInst(Value *S, const Type *Ty, const string &Name = "")
85     : Instruction(Ty, Cast, Name) {
86     Operands.reserve(1);
87     Operands.push_back(Use(S, this));
88   }
89
90   virtual Instruction *clone() const { return new CastInst(*this); }
91   virtual const char *getOpcodeName() const { return "cast"; }
92
93   // Methods for support type inquiry through isa, cast, and dyn_cast:
94   static inline bool classof(const CastInst *) { return true; }
95   static inline bool classof(const Instruction *I) {
96     return I->getOpcode() == Cast;
97   }
98   static inline bool classof(const Value *V) {
99     return isa<Instruction>(V) && classof(cast<Instruction>(V));
100   }
101 };
102
103
104 //===----------------------------------------------------------------------===//
105 //                           MethodArgument Class
106 //===----------------------------------------------------------------------===//
107
108 class MethodArgument : public Value {  // Defined in the InstrType.cpp file
109   Method *Parent;
110
111   friend class ValueHolder<MethodArgument,Method,Method>;
112   inline void setParent(Method *parent) { Parent = parent; }
113
114 public:
115   MethodArgument(const Type *Ty, const string &Name = "") 
116     : Value(Ty, Value::MethodArgumentVal, Name) {
117     Parent = 0;
118   }
119
120   // Specialize setName to handle symbol table majik...
121   virtual void setName(const string &name, SymbolTable *ST = 0);
122
123   inline const Method *getParent() const { return Parent; }
124   inline       Method *getParent()       { return Parent; }
125
126   // Methods for support type inquiry through isa, cast, and dyn_cast:
127   static inline bool classof(const MethodArgument *) { return true; }
128   static inline bool classof(const Value *V) {
129     return V->getValueType() == MethodArgumentVal;
130   }
131 };
132
133
134 //===----------------------------------------------------------------------===//
135 //             Classes to function calls and method invocations
136 //===----------------------------------------------------------------------===//
137
138 class CallInst : public Instruction {
139   CallInst(const CallInst &CI);
140 public:
141   CallInst(Value *Meth, const vector<Value*> &params, const string &Name = "");
142
143   virtual const char *getOpcodeName() const { return "call"; }
144
145   virtual Instruction *clone() const { return new CallInst(*this); }
146   bool hasSideEffects() const { return true; }
147
148   const Method *getCalledMethod() const {
149     return dyn_cast<Method>(Operands[0]);
150   }
151   Method *getCalledMethod() {
152     return dyn_cast<Method>(Operands[0]); 
153   }
154
155   // getCalledValue - Get a pointer to a method that is invoked by this inst.
156   inline const Value *getCalledValue() const { return Operands[0]; }
157   inline       Value *getCalledValue()       { return Operands[0]; }
158
159   // Methods for support type inquiry through isa, cast, and dyn_cast:
160   static inline bool classof(const CallInst *) { return true; }
161   static inline bool classof(const Instruction *I) {
162     return I->getOpcode() == Instruction::Call; 
163   }
164   static inline bool classof(const Value *V) {
165     return isa<Instruction>(V) && classof(cast<Instruction>(V));
166   }
167 };
168
169
170 //===----------------------------------------------------------------------===//
171 //                                 ShiftInst Class
172 //===----------------------------------------------------------------------===//
173
174 // ShiftInst - This class represents left and right shift instructions.
175 //
176 class ShiftInst : public Instruction {
177   ShiftInst(const ShiftInst &SI) : Instruction(SI.getType(), SI.getOpcode()) {
178     Operands.reserve(2);
179     Operands.push_back(Use(SI.Operands[0], this));
180     Operands.push_back(Use(SI.Operands[1], this));
181   }
182 public:
183   ShiftInst(OtherOps Opcode, Value *S, Value *SA, const string &Name = "")
184     : Instruction(S->getType(), Opcode, Name) {
185     assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
186     Operands.reserve(2);
187     Operands.push_back(Use(S, this));
188     Operands.push_back(Use(SA, this));
189   }
190
191   OtherOps getOpcode() const { return (OtherOps)Instruction::getOpcode(); }
192
193   virtual Instruction *clone() const { return new ShiftInst(*this); }
194   virtual const char *getOpcodeName() const {
195     return getOpcode() == Shl ? "shl" : "shr"; 
196   }
197
198   // Methods for support type inquiry through isa, cast, and dyn_cast:
199   static inline bool classof(const ShiftInst *) { return true; }
200   static inline bool classof(const Instruction *I) {
201     return (I->getOpcode() == Instruction::Shr) | 
202            (I->getOpcode() == Instruction::Shl);
203   }
204   static inline bool classof(const Value *V) {
205     return isa<Instruction>(V) && classof(cast<Instruction>(V));
206   }
207 };
208
209 #endif