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