23bc5462a8ca18725c0ff62b91d581e0c044a7c7
[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 #include <vector>
14
15 //===----------------------------------------------------------------------===//
16 //                               PHINode Class
17 //===----------------------------------------------------------------------===//
18
19 // PHINode - The PHINode class is used to represent the magical mystical PHI
20 // node, that can not exist in nature, but can be synthesized in a computer
21 // scientist's overactive imagination.
22 //
23 class PHINode : public Instruction {
24   typedef pair<Use,BasicBlockUse> PairTy;
25   vector<PairTy> IncomingValues;
26
27   PHINode(const PHINode &PN);
28 public:
29   PHINode(const Type *Ty, const string &Name = "");
30   inline ~PHINode() { dropAllReferences(); }
31
32   virtual Instruction *clone() const { return new PHINode(*this); }
33
34   // Implement all of the functionality required by User...
35   //
36   virtual void dropAllReferences();
37   virtual const Value *getOperand(unsigned i) const {
38     if (i >= IncomingValues.size()*2) return 0;
39     if (i & 1) return IncomingValues[i/2].second;
40     else       return IncomingValues[i/2].first;
41   }
42   inline Value *getOperand(unsigned i) {
43     return (Value*)((const PHINode*)this)->getOperand(i);
44   }
45   virtual unsigned getNumOperands() const { return IncomingValues.size()*2; }
46   virtual bool setOperand(unsigned i, Value *Val);
47   virtual string getOpcode() const { return "phi"; }
48
49   // getNumIncomingValues - Return the number of incoming edges the PHI node has
50   inline unsigned getNumIncomingValues() const { return IncomingValues.size(); }
51
52   // getIncomingValue - Return incoming value #x
53   inline Value *getIncomingValue(unsigned i) const { 
54     return IncomingValues[i].first; 
55   }
56
57   // getIncomingBlock - Return incoming basic block #x
58   inline BasicBlock *getIncomingBlock(unsigned i) const { 
59     return IncomingValues[i].second; 
60   }
61
62   // addIncoming - Add an incoming value to the end of the PHI list
63   void addIncoming(Value *D, BasicBlock *BB);
64
65   // removeIncomingValue - Remove an incoming value.  This is useful if a
66   // predecessor basic block is deleted.  The value removed is returned.
67   Value *removeIncomingValue(const BasicBlock *BB);
68 };
69
70
71 //===----------------------------------------------------------------------===//
72 //                           MethodArgument Class
73 //===----------------------------------------------------------------------===//
74
75 class MethodArgument : public Value {  // Defined in the InstrType.cpp file
76   Method *Parent;
77
78   friend class ValueHolder<MethodArgument,Method>;
79   inline void setParent(Method *parent) { Parent = parent; }
80
81 public:
82   MethodArgument(const Type *Ty, const string &Name = "") 
83     : Value(Ty, Value::MethodArgumentVal, Name) {
84     Parent = 0;
85   }
86
87   // Specialize setName to handle symbol table majik...
88   virtual void setName(const string &name);
89
90   inline const Method *getParent() const { return Parent; }
91   inline       Method *getParent()       { return Parent; }
92 };
93
94
95 //===----------------------------------------------------------------------===//
96 //             Classes to function calls and method invocations
97 //===----------------------------------------------------------------------===//
98
99 class CallInst : public Instruction {
100   MethodUse M;
101   vector<Use> Params;
102   CallInst(const CallInst &CI);
103 public:
104   CallInst(Method *M, vector<Value*> &params, const string &Name = "");
105   inline ~CallInst() { dropAllReferences(); }
106
107   virtual string getOpcode() const { return "call"; }
108
109   virtual Instruction *clone() const { return new CallInst(*this); }
110   bool hasSideEffects() const { return true; }
111
112
113   const Method *getCalledMethod() const { return M; }
114         Method *getCalledMethod()       { return M; }
115
116   // Implement all of the functionality required by Instruction...
117   //
118   virtual void dropAllReferences();
119   virtual const Value *getOperand(unsigned i) const { 
120     return i == 0 ? M : ((i <= Params.size()) ? Params[i-1] : 0);
121   }
122   inline Value *getOperand(unsigned i) {
123     return (Value*)((const CallInst*)this)->getOperand(i);
124   }
125   virtual unsigned getNumOperands() const { return Params.size()+1; }
126
127   virtual bool setOperand(unsigned i, Value *Val);
128 };
129
130 #endif