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