Workaround for a compiler bug (see <rdar://problem/5852746>). Once that bug is
[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
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   /// isUsedOutsideOfBlock - Return true if there are any uses of this
76   /// instruction in blocks other than the specified block.  Note that PHI nodes
77   /// are considered to evaluate their operands in the corresponding predecessor
78   /// block.
79   bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
80   
81   
82   /// use_back - Specialize the methods defined in Value, as we know that an
83   /// instruction can only be used by other instructions.
84   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
85   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
86   
87   // Accessor methods...
88   //
89   inline const BasicBlock *getParent() const { return Parent; }
90   inline       BasicBlock *getParent()       { return Parent; }
91
92   /// removeFromParent - This method unlinks 'this' from the containing basic
93   /// block, but does not delete it.
94   ///
95   void removeFromParent();
96
97   /// eraseFromParent - This method unlinks 'this' from the containing basic
98   /// block and deletes it.
99   ///
100   void eraseFromParent();
101
102   /// moveBefore - Unlink this instruction from its current basic block and
103   /// insert it into the basic block that MovePos lives in, right before
104   /// MovePos.
105   void moveBefore(Instruction *MovePos);
106
107   // ---------------------------------------------------------------------------
108   /// Subclass classification... getOpcode() returns a member of
109   /// one of the enums that is coming soon (down below)...
110   ///
111   unsigned getOpcode() const { return getValueID() - InstructionVal; }
112   const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
113   bool isTerminator() const { return isTerminator(getOpcode()); }
114   bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
115   bool isShift() { return isShift(getOpcode()); }
116   bool isCast() const { return isCast(getOpcode()); }
117   
118   
119   
120   static const char* getOpcodeName(unsigned OpCode);
121
122   static inline bool isTerminator(unsigned OpCode) {
123     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
124   }
125
126   static inline bool isBinaryOp(unsigned Opcode) {
127     return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
128   }
129
130   /// @brief Determine if the Opcode is one of the shift instructions.
131   static inline bool isShift(unsigned Opcode) {
132     return Opcode >= Shl && Opcode <= AShr;
133   }
134
135   /// isLogicalShift - Return true if this is a logical shift left or a logical
136   /// shift right.
137   inline bool isLogicalShift() const {
138     return getOpcode() == Shl || getOpcode() == LShr;
139   }
140
141   /// isLogicalShift - Return true if this is a logical shift left or a logical
142   /// shift right.
143   inline bool isArithmeticShift() const {
144     return getOpcode() == AShr;
145   }
146
147   /// @brief Determine if the OpCode is one of the CastInst instructions.
148   static inline bool isCast(unsigned OpCode) {
149     return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
150   }
151
152   /// isAssociative - Return true if the instruction is associative:
153   ///
154   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
155   ///
156   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
157   /// not applied to floating point types.
158   ///
159   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
160   static bool isAssociative(unsigned op, const Type *Ty);
161
162   /// isCommutative - Return true if the instruction is commutative:
163   ///
164   ///   Commutative operators satisfy: (x op y) === (y op x)
165   ///
166   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
167   /// applied to any type.
168   ///
169   bool isCommutative() const { return isCommutative(getOpcode()); }
170   static bool isCommutative(unsigned op);
171
172   /// isTrappingInstruction - Return true if the instruction may trap.
173   ///
174   bool isTrapping() const {
175     return isTrapping(getOpcode());
176   }
177   static bool isTrapping(unsigned op);
178
179   virtual void print(std::ostream &OS) const { print(OS, 0); }
180   void print(std::ostream *OS) const { if (OS) print(*OS); }
181   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
182
183   /// Methods for support type inquiry through isa, cast, and dyn_cast:
184   static inline bool classof(const Instruction *) { return true; }
185   static inline bool classof(const Value *V) {
186     return V->getValueID() >= Value::InstructionVal;
187   }
188
189   //----------------------------------------------------------------------
190   // Exported enumerations...
191   //
192   enum TermOps {       // These terminate basic blocks
193 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
194 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
195 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
196 #include "llvm/Instruction.def"
197   };
198
199   enum BinaryOps {
200 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
201 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
202 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
203 #include "llvm/Instruction.def"
204   };
205
206   enum MemoryOps {
207 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
208 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
209 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
210 #include "llvm/Instruction.def"
211   };
212
213   enum CastOps {
214 #define  FIRST_CAST_INST(N)             CastOpsBegin = N,
215 #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
216 #define   LAST_CAST_INST(N)             CastOpsEnd = N+1
217 #include "llvm/Instruction.def"
218   };
219
220   enum OtherOps {
221 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
222 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
223 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
224 #include "llvm/Instruction.def"
225   };
226   
227 private:
228   // getNext/Prev - Return the next or previous instruction in the list.  The
229   // last node in the list is a terminator instruction.
230   Instruction *getNext()             { return Next; }
231   const Instruction *getNext() const { return Next; }
232   Instruction *getPrev()             { return Prev; }
233   const Instruction *getPrev() const { return Prev; }
234 };
235
236 } // End llvm namespace
237
238 #endif