Temporarily restore the scavenger implicit_def checking code. MachineOperand isUndef...
[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   /// clone() - Create a copy of 'this' instruction that is identical in all
44   /// ways except the following:
45   ///   * The instruction has no parent
46   ///   * The instruction has no name
47   ///
48   virtual Instruction *clone() const = 0;
49
50   /// isIdenticalTo - Return true if the specified instruction is exactly
51   /// identical to the current one.  This means that all operands match and any
52   /// extra information (e.g. load is volatile) agree.
53   bool isIdenticalTo(const Instruction *I) const;
54
55   /// This function determines if the specified instruction executes the same
56   /// operation as the current one. This means that the opcodes, type, operand
57   /// types and any other factors affecting the operation must be the same. This
58   /// is similar to isIdenticalTo except the operands themselves don't have to
59   /// be identical.
60   /// @returns true if the specified instruction is the same operation as
61   /// the current one.
62   /// @brief Determine if one instruction is the same operation as another.
63   bool isSameOperationAs(const Instruction *I) const;
64
65   /// isUsedOutsideOfBlock - Return true if there are any uses of this
66   /// instruction in blocks other than the specified block.  Note that PHI nodes
67   /// are considered to evaluate their operands in the corresponding predecessor
68   /// block.
69   bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
70   
71   
72   /// use_back - Specialize the methods defined in Value, as we know that an
73   /// instruction can only be used by other instructions.
74   Instruction       *use_back()       { return cast<Instruction>(*use_begin());}
75   const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
76   
77   // Accessor methods...
78   //
79   inline const BasicBlock *getParent() const { return Parent; }
80   inline       BasicBlock *getParent()       { return Parent; }
81
82   /// removeFromParent - This method unlinks 'this' from the containing basic
83   /// block, but does not delete it.
84   ///
85   void removeFromParent();
86
87   /// eraseFromParent - This method unlinks 'this' from the containing basic
88   /// block and deletes it.
89   ///
90   void eraseFromParent();
91
92   /// insertBefore - Insert an unlinked instructions into a basic block
93   /// immediately before the specified instruction.
94   void insertBefore(Instruction *InsertPos);
95
96   /// insertAfter - Insert an unlinked instructions into a basic block
97   /// immediately after the specified instruction.
98   void insertAfter(Instruction *InsertPos);
99
100   /// moveBefore - Unlink this instruction from its current basic block and
101   /// insert it into the basic block that MovePos lives in, right before
102   /// MovePos.
103   void moveBefore(Instruction *MovePos);
104
105   // ---------------------------------------------------------------------------
106   /// Subclass classification... getOpcode() returns a member of
107   /// one of the enums that is coming soon (down below)...
108   ///
109   unsigned getOpcode() const { return getValueID() - InstructionVal; }
110   const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
111   bool isTerminator() const { return isTerminator(getOpcode()); }
112   bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
113   bool isShift() { return isShift(getOpcode()); }
114   bool isCast() const { return isCast(getOpcode()); }
115   
116   
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   static inline bool isBinaryOp(unsigned Opcode) {
125     return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
126   }
127
128   /// @brief Determine if the Opcode is one of the shift instructions.
129   static inline bool isShift(unsigned Opcode) {
130     return Opcode >= Shl && Opcode <= AShr;
131   }
132
133   /// isLogicalShift - Return true if this is a logical shift left or a logical
134   /// shift right.
135   inline bool isLogicalShift() const {
136     return getOpcode() == Shl || getOpcode() == LShr;
137   }
138
139   /// isArithmeticShift - Return true if this is an arithmetic shift right.
140   inline bool isArithmeticShift() const {
141     return getOpcode() == AShr;
142   }
143
144   /// @brief Determine if the OpCode is one of the CastInst instructions.
145   static inline bool isCast(unsigned OpCode) {
146     return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
147   }
148
149   /// isAssociative - Return true if the instruction is associative:
150   ///
151   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
152   ///
153   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
154   /// not applied to floating point types.
155   ///
156   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
157   static bool isAssociative(unsigned op, const Type *Ty);
158
159   /// isCommutative - Return true if the instruction is commutative:
160   ///
161   ///   Commutative operators satisfy: (x op y) === (y op x)
162   ///
163   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
164   /// applied to any type.
165   ///
166   bool isCommutative() const { return isCommutative(getOpcode()); }
167   static bool isCommutative(unsigned op);
168
169   /// isTrapping - Return true if the instruction may trap.
170   ///
171   bool isTrapping() const {
172     return isTrapping(getOpcode());
173   }
174   static bool isTrapping(unsigned op);
175
176   /// mayWriteToMemory - Return true if this instruction may modify memory.
177   ///
178   bool mayWriteToMemory() const;
179
180   /// mayReadFromMemory - Return true if this instruction may read memory.
181   ///
182   bool mayReadFromMemory() const;
183
184   /// mayThrow - Return true if this instruction may throw an exception.
185   ///
186   bool mayThrow() const;
187
188   /// mayHaveSideEffects - Return true if the instruction may have side effects.
189   ///
190   bool mayHaveSideEffects() const {
191     return mayWriteToMemory() || mayThrow();
192   }
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->getValueID() >= 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 // Instruction* is only 4-byte aligned.
240 template<>
241 class PointerLikeTypeTraits<Instruction*> {
242   typedef Instruction* PT;
243 public:
244   static inline void *getAsVoidPointer(PT P) { return P; }
245   static inline PT getFromVoidPointer(void *P) {
246     return static_cast<PT>(P);
247   }
248   enum { NumLowBitsAvailable = 2 };
249 };
250   
251 } // End llvm namespace
252
253 #endif