cec9144e35d1f7bb4c014d641aa0c17fff8a0a89
[oota-llvm.git] / include / llvm / Instruction.h
1 //===-- llvm/Instruction.h - Instruction class definition -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the Instruction class, which is the
11 // base class for all of the LLVM instructions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_INSTRUCTION_H
16 #define LLVM_INSTRUCTION_H
17
18 #include "llvm/User.h"
19 #include "llvm/ADT/ilist_node.h"
20
21 namespace llvm {
22
23 template<typename ValueSubClass, typename ItemParentClass>
24   class SymbolTableListTraits;
25
26 class Instruction : public User, public ilist_node<Instruction> {
27   void operator=(const Instruction &);     // Do not implement
28   Instruction(const Instruction &);        // Do not implement
29
30   BasicBlock *Parent;
31
32   friend class SymbolTableListTraits<Instruction, BasicBlock>;
33   void setParent(BasicBlock *P);
34 protected:
35   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
36               Instruction *InsertBefore = 0);
37   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
38               BasicBlock *InsertAtEnd);
39 public:
40   // Out of line virtual method, so the vtable, etc has a home.
41   ~Instruction();
42   
43   /// mayWriteToMemory - Return true if this instruction may modify memory.
44   ///
45   bool mayWriteToMemory() const;
46
47   /// mayReadFromMemory - Return true if this instruction may read memory.
48   ///
49   bool mayReadFromMemory() const;
50   
51   /// clone() - Create a copy of 'this' instruction that is identical in all
52   /// ways except the following:
53   ///   * The instruction has no parent
54   ///   * The instruction has no name
55   ///
56   virtual Instruction *clone() const = 0;
57
58   /// isIdenticalTo - Return true if the specified instruction is exactly
59   /// identical to the current one.  This means that all operands match and any
60   /// extra information (e.g. load is volatile) agree.
61   bool isIdenticalTo(Instruction *I) const;
62
63   /// This function determines if the specified instruction executes the same
64   /// operation as the current one. This means that the opcodes, type, operand
65   /// types and any other factors affecting the operation must be the same. This
66   /// is similar to isIdenticalTo except the operands themselves don't have to
67   /// be identical.
68   /// @returns true if the specified instruction is the same operation as
69   /// the current one.
70   /// @brief Determine if one instruction is the same operation as another.
71   bool isSameOperationAs(Instruction *I) const;
72
73   /// isUsedOutsideOfBlock - Return true if there are any uses of this
74   /// instruction in blocks other than the specified block.  Note that PHI nodes
75   /// are considered to evaluate their operands in the corresponding predecessor
76   /// block.
77   bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
78   
79   
80   /// use_back - Specialize the methods defined in Value, as we know that an
81   /// instruction can only be used by other instructions.
82   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
83   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
84   
85   // Accessor methods...
86   //
87   inline const BasicBlock *getParent() const { return Parent; }
88   inline       BasicBlock *getParent()       { return Parent; }
89
90   /// removeFromParent - This method unlinks 'this' from the containing basic
91   /// block, but does not delete it.
92   ///
93   void removeFromParent();
94
95   /// eraseFromParent - This method unlinks 'this' from the containing basic
96   /// block and deletes it.
97   ///
98   void eraseFromParent();
99
100   /// insertBefore - Insert an unlinked instructions into a basic block
101   /// immediately before the specified instruction.
102   void insertBefore(Instruction *InsertPos);
103
104   /// moveBefore - Unlink this instruction from its current basic block and
105   /// insert it into the basic block that MovePos lives in, right before
106   /// MovePos.
107   void moveBefore(Instruction *MovePos);
108
109   // ---------------------------------------------------------------------------
110   /// Subclass classification... getOpcode() returns a member of
111   /// one of the enums that is coming soon (down below)...
112   ///
113   unsigned getOpcode() const { return getValueID() - InstructionVal; }
114   const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
115   bool isTerminator() const { return isTerminator(getOpcode()); }
116   bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
117   bool isShift() { return isShift(getOpcode()); }
118   bool isCast() const { return isCast(getOpcode()); }
119   
120   
121   
122   static const char* getOpcodeName(unsigned OpCode);
123
124   static inline bool isTerminator(unsigned OpCode) {
125     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
126   }
127
128   static inline bool isBinaryOp(unsigned Opcode) {
129     return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
130   }
131
132   /// @brief Determine if the Opcode is one of the shift instructions.
133   static inline bool isShift(unsigned Opcode) {
134     return Opcode >= Shl && Opcode <= AShr;
135   }
136
137   /// isLogicalShift - Return true if this is a logical shift left or a logical
138   /// shift right.
139   inline bool isLogicalShift() const {
140     return getOpcode() == Shl || getOpcode() == LShr;
141   }
142
143   /// isLogicalShift - Return true if this is a logical shift left or a logical
144   /// shift right.
145   inline bool isArithmeticShift() const {
146     return getOpcode() == AShr;
147   }
148
149   /// @brief Determine if the OpCode is one of the CastInst instructions.
150   static inline bool isCast(unsigned OpCode) {
151     return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
152   }
153
154   /// isAssociative - Return true if the instruction is associative:
155   ///
156   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
157   ///
158   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
159   /// not applied to floating point types.
160   ///
161   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
162   static bool isAssociative(unsigned op, const Type *Ty);
163
164   /// isCommutative - Return true if the instruction is commutative:
165   ///
166   ///   Commutative operators satisfy: (x op y) === (y op x)
167   ///
168   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
169   /// applied to any type.
170   ///
171   bool isCommutative() const { return isCommutative(getOpcode()); }
172   static bool isCommutative(unsigned op);
173
174   /// isTrappingInstruction - Return true if the instruction may trap.
175   ///
176   bool isTrapping() const {
177     return isTrapping(getOpcode());
178   }
179   static bool isTrapping(unsigned op);
180
181   /// Methods for support type inquiry through isa, cast, and dyn_cast:
182   static inline bool classof(const Instruction *) { return true; }
183   static inline bool classof(const Value *V) {
184     return V->getValueID() >= Value::InstructionVal;
185   }
186
187   //----------------------------------------------------------------------
188   // Exported enumerations...
189   //
190   enum TermOps {       // These terminate basic blocks
191 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
192 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
193 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
194 #include "llvm/Instruction.def"
195   };
196
197   enum BinaryOps {
198 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
199 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
200 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
201 #include "llvm/Instruction.def"
202   };
203
204   enum MemoryOps {
205 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
206 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
207 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
208 #include "llvm/Instruction.def"
209   };
210
211   enum CastOps {
212 #define  FIRST_CAST_INST(N)             CastOpsBegin = N,
213 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
214 #define   LAST_CAST_INST(N)             CastOpsEnd = N+1
215 #include "llvm/Instruction.def"
216   };
217
218   enum OtherOps {
219 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
220 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
221 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
222 #include "llvm/Instruction.def"
223   };
224 };
225
226 } // End llvm namespace
227
228 #endif