ed0357f823bf27de4eda5c797b7bef621b3ef9d2
[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 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 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
20 namespace llvm {
21
22 struct AssemblyAnnotationWriter;
23 class BinaryOperator;
24
25 template<typename SC> struct ilist_traits;
26 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
27          typename SubClass> class SymbolTableListTraits;
28
29 class Instruction : public User {
30   void operator=(const Instruction &);     // Do not implement
31   Instruction(const Instruction &);        // Do not implement
32
33   BasicBlock *Parent;
34   Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
35
36   void setNext(Instruction *N) { Next = N; }
37   void setPrev(Instruction *N) { Prev = N; }
38
39   friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
40                                      ilist_traits<Instruction> >;
41   void setParent(BasicBlock *P);
42 protected:
43   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
44               Instruction *InsertBefore = 0);
45   Instruction(const Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
46               BasicBlock *InsertAtEnd);
47 public:
48   // Out of line virtual method, so the vtable, etc has a home.
49   ~Instruction();
50   
51   /// mayWriteToMemory - Return true if this instruction may modify memory.
52   ///
53   bool mayWriteToMemory() const;
54
55   /// clone() - Create a copy of 'this' instruction that is identical in all
56   /// ways except the following:
57   ///   * The instruction has no parent
58   ///   * The instruction has no name
59   ///
60   virtual Instruction *clone() const = 0;
61
62   /// isIdenticalTo - Return true if the specified instruction is exactly
63   /// identical to the current one.  This means that all operands match and any
64   /// extra information (e.g. load is volatile) agree.
65   bool isIdenticalTo(Instruction *I) const;
66
67   /// This function determines if the specified instruction executes the same
68   /// operation as the current one. This means that the opcodes, type, operand
69   /// types and any other factors affecting the operation must be the same. This
70   /// is similar to isIdenticalTo except the operands themselves don't have to
71   /// be identical.
72   /// @returns true if the specified instruction is the same operation as
73   /// the current one.
74   /// @brief Determine if one instruction is the same operation as another.
75   bool isSameOperationAs(Instruction *I) const;
76
77   /// use_back - Specialize the methods defined in Value, as we know that an
78   /// instruction can only be used by other instructions.
79   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
80   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
81   
82   // Accessor methods...
83   //
84   inline const BasicBlock *getParent() const { return Parent; }
85   inline       BasicBlock *getParent()       { return Parent; }
86
87   // getNext/Prev - Return the next or previous instruction in the list.  The
88   // last node in the list is a terminator instruction.
89         Instruction *getNext()       { return Next; }
90   const Instruction *getNext() const { return Next; }
91         Instruction *getPrev()       { return Prev; }
92   const Instruction *getPrev() const { return Prev; }
93
94   /// removeFromParent - This method unlinks 'this' from the containing basic
95   /// block, but does not delete it.
96   ///
97   void removeFromParent();
98
99   /// eraseFromParent - This method unlinks 'this' from the containing basic
100   /// block and deletes it.
101   ///
102   void eraseFromParent();
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 {
115     return getOpcodeName(getOpcode());
116   }
117   static const char* getOpcodeName(unsigned OpCode);
118
119   static inline bool isTerminator(unsigned OpCode) {
120     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
121   }
122
123   inline bool isTerminator() const {   // Instance of TerminatorInst?
124     return isTerminator(getOpcode());
125   }
126
127   inline bool isBinaryOp() const {
128     return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd;
129   }
130
131   /// @brief Determine if the Opcode is one of the shift instructions.
132   static inline bool isShift(unsigned Opcode) {
133     return Opcode >= Shl && Opcode <= AShr;
134   }
135
136   /// @brief Determine if the instruction's opcode is one of the shift 
137   /// instructions.
138   inline bool isShift() { return isShift(getOpcode()); }
139
140   /// isLogicalShift - Return true if this is a logical shift left or a logical
141   /// shift right.
142   inline bool isLogicalShift() {
143     return getOpcode() == Shl || getOpcode() == LShr;
144   }
145
146   /// isLogicalShift - Return true if this is a logical shift left or a logical
147   /// shift right.
148   inline bool isArithmeticShift() {
149     return getOpcode() == AShr;
150   }
151
152   /// @brief Determine if the OpCode is one of the CastInst instructions.
153   static inline bool isCast(unsigned OpCode) {
154     return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
155   }
156
157   /// @brief Determine if this is one of the CastInst instructions.
158   inline bool isCast() const {
159     return isCast(getOpcode());
160   }
161
162   /// isAssociative - Return true if the instruction is associative:
163   ///
164   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
165   ///
166   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
167   /// not applied to floating point types.
168   ///
169   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
170   static bool isAssociative(unsigned op, const Type *Ty);
171
172   /// isCommutative - Return true if the instruction is commutative:
173   ///
174   ///   Commutative operators satisfy: (x op y) === (y op x)
175   ///
176   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
177   /// applied to any type.
178   ///
179   bool isCommutative() const { return isCommutative(getOpcode()); }
180   static bool isCommutative(unsigned op);
181
182   /// isTrappingInstruction - Return true if the instruction may trap.
183   ///
184   bool isTrapping() const {
185     return isTrapping(getOpcode());
186   }
187   static bool isTrapping(unsigned op);
188
189   virtual void print(std::ostream &OS) const { print(OS, 0); }
190   void print(std::ostream *OS) const { if (OS) print(*OS); }
191   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
192
193   /// Methods for support type inquiry through isa, cast, and dyn_cast:
194   static inline bool classof(const Instruction *) { return true; }
195   static inline bool classof(const Value *V) {
196     return V->getValueID() >= Value::InstructionVal;
197   }
198
199   //----------------------------------------------------------------------
200   // Exported enumerations...
201   //
202   enum TermOps {       // These terminate basic blocks
203 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
204 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
205 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
206 #include "llvm/Instruction.def"
207   };
208
209   enum BinaryOps {
210 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
211 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
212 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
213 #include "llvm/Instruction.def"
214   };
215
216   enum MemoryOps {
217 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
218 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
219 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
220 #include "llvm/Instruction.def"
221   };
222
223   enum CastOps {
224 #define  FIRST_CAST_INST(N)             CastOpsBegin = N,
225 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
226 #define   LAST_CAST_INST(N)             CastOpsEnd = N+1
227 #include "llvm/Instruction.def"
228   };
229
230   enum OtherOps {
231 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
232 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
233 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
234 #include "llvm/Instruction.def"
235   };
236 };
237
238 } // End llvm namespace
239
240 #endif