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