Implement DCE of global values
[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   inline void setIncomingBlock(unsigned i, BasicBlock *BB) {
52     Operands[i*2+1] = BB;
53   }
54
55   // addIncoming - Add an incoming value to the end of the PHI list
56   void addIncoming(Value *D, BasicBlock *BB);
57
58   // removeIncomingValue - Remove an incoming value.  This is useful if a
59   // predecessor basic block is deleted.  The value removed is returned.
60   Value *removeIncomingValue(const BasicBlock *BB);
61
62   // getBasicBlockIndex - Return the first index of the specified basic 
63   // block in the value list for this PHI.  Returns -1 if no instance.
64   //
65   int getBasicBlockIndex(const BasicBlock *BB) const {
66     for (unsigned i = 0; i < Operands.size()/2; ++i) 
67       if (getIncomingBlock(i) == BB) return i;
68     return -1;
69   }
70
71   // Methods for support type inquiry through isa, cast, and dyn_cast:
72   static inline bool classof(const PHINode *) { return true; }
73   static inline bool classof(const Instruction *I) {
74     return I->getOpcode() == Instruction::PHINode; 
75   }
76   static inline bool classof(const Value *V) {
77     return isa<Instruction>(V) && classof(cast<Instruction>(V));
78   }
79 };
80
81
82 //===----------------------------------------------------------------------===//
83 //                                 CastInst Class
84 //===----------------------------------------------------------------------===//
85
86 // CastInst - This class represents a cast from Operand[0] to the type of
87 // the instruction (i->getType()).
88 //
89 class CastInst : public Instruction {
90   CastInst(const CastInst &CI) : Instruction(CI.getType(), Cast) {
91     Operands.reserve(1);
92     Operands.push_back(Use(CI.Operands[0], this));
93   }
94 public:
95   CastInst(Value *S, const Type *Ty, const string &Name = "")
96     : Instruction(Ty, Cast, Name) {
97     Operands.reserve(1);
98     Operands.push_back(Use(S, this));
99   }
100
101   virtual Instruction *clone() const { return new CastInst(*this); }
102   virtual const char *getOpcodeName() const { return "cast"; }
103
104   // Methods for support type inquiry through isa, cast, and dyn_cast:
105   static inline bool classof(const CastInst *) { return true; }
106   static inline bool classof(const Instruction *I) {
107     return I->getOpcode() == Cast;
108   }
109   static inline bool classof(const Value *V) {
110     return isa<Instruction>(V) && classof(cast<Instruction>(V));
111   }
112 };
113
114
115 //===----------------------------------------------------------------------===//
116 //                           MethodArgument Class
117 //===----------------------------------------------------------------------===//
118
119 class MethodArgument : public Value {  // Defined in the InstrType.cpp file
120   Method *Parent;
121
122   friend class ValueHolder<MethodArgument,Method,Method>;
123   inline void setParent(Method *parent) { Parent = parent; }
124
125 public:
126   MethodArgument(const Type *Ty, const string &Name = "") 
127     : Value(Ty, Value::MethodArgumentVal, Name) {
128     Parent = 0;
129   }
130
131   // Specialize setName to handle symbol table majik...
132   virtual void setName(const string &name, SymbolTable *ST = 0);
133
134   inline const Method *getParent() const { return Parent; }
135   inline       Method *getParent()       { return Parent; }
136
137   // Methods for support type inquiry through isa, cast, and dyn_cast:
138   static inline bool classof(const MethodArgument *) { return true; }
139   static inline bool classof(const Value *V) {
140     return V->getValueType() == MethodArgumentVal;
141   }
142 };
143
144
145 //===----------------------------------------------------------------------===//
146 //             Classes to function calls and method invocations
147 //===----------------------------------------------------------------------===//
148
149 class CallInst : public Instruction {
150   CallInst(const CallInst &CI);
151 public:
152   CallInst(Value *Meth, const vector<Value*> &params, const string &Name = "");
153
154   virtual const char *getOpcodeName() const { return "call"; }
155
156   virtual Instruction *clone() const { return new CallInst(*this); }
157   bool hasSideEffects() const { return true; }
158
159   const Method *getCalledMethod() const {
160     return dyn_cast<Method>(Operands[0]);
161   }
162   Method *getCalledMethod() {
163     return dyn_cast<Method>(Operands[0]); 
164   }
165
166   // getCalledValue - Get a pointer to a method that is invoked by this inst.
167   inline const Value *getCalledValue() const { return Operands[0]; }
168   inline       Value *getCalledValue()       { return Operands[0]; }
169
170   // Methods for support type inquiry through isa, cast, and dyn_cast:
171   static inline bool classof(const CallInst *) { return true; }
172   static inline bool classof(const Instruction *I) {
173     return I->getOpcode() == Instruction::Call; 
174   }
175   static inline bool classof(const Value *V) {
176     return isa<Instruction>(V) && classof(cast<Instruction>(V));
177   }
178 };
179
180
181 //===----------------------------------------------------------------------===//
182 //                                 ShiftInst Class
183 //===----------------------------------------------------------------------===//
184
185 // ShiftInst - This class represents left and right shift instructions.
186 //
187 class ShiftInst : public Instruction {
188   ShiftInst(const ShiftInst &SI) : Instruction(SI.getType(), SI.getOpcode()) {
189     Operands.reserve(2);
190     Operands.push_back(Use(SI.Operands[0], this));
191     Operands.push_back(Use(SI.Operands[1], this));
192   }
193 public:
194   ShiftInst(OtherOps Opcode, Value *S, Value *SA, const string &Name = "")
195     : Instruction(S->getType(), Opcode, Name) {
196     assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
197     Operands.reserve(2);
198     Operands.push_back(Use(S, this));
199     Operands.push_back(Use(SA, this));
200   }
201
202   OtherOps getOpcode() const { return (OtherOps)Instruction::getOpcode(); }
203
204   virtual Instruction *clone() const { return new ShiftInst(*this); }
205   virtual const char *getOpcodeName() const {
206     return getOpcode() == Shl ? "shl" : "shr"; 
207   }
208
209   // Methods for support type inquiry through isa, cast, and dyn_cast:
210   static inline bool classof(const ShiftInst *) { return true; }
211   static inline bool classof(const Instruction *I) {
212     return (I->getOpcode() == Instruction::Shr) | 
213            (I->getOpcode() == Instruction::Shl);
214   }
215   static inline bool classof(const Value *V) {
216     return isa<Instruction>(V) && classof(cast<Instruction>(V));
217   }
218 };
219
220 #endif