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