This method doesn't need to be virtual, thanks to Reid for pointing this out.
[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
56   ~Instruction() {
57     assert(Parent == 0 && "Instruction still linked in the program!");
58   }
59
60   /// mayWriteToMemory - Return true if this instruction may modify memory.
61   ///
62   virtual bool mayWriteToMemory() const { return false; }
63
64   /// clone() - Create a copy of 'this' instruction that is identical in all
65   /// ways except the following:
66   ///   * The instruction has no parent
67   ///   * The instruction has no name
68   ///
69   virtual Instruction *clone() const = 0;
70
71   /// isIdenticalTo - Return true if the specified instruction is exactly
72   /// identical to the current one.  This means that all operands match and any
73   /// extra information (e.g. load is volatile) agree.
74   bool isIdenticalTo(Instruction *I) const;
75
76
77   // Accessor methods...
78   //
79   inline const BasicBlock *getParent() const { return Parent; }
80   inline       BasicBlock *getParent()       { return Parent; }
81
82   // getNext/Prev - Return the next or previous instruction in the list.  The
83   // last node in the list is a terminator instruction.
84         Instruction *getNext()       { return Next; }
85   const Instruction *getNext() const { return Next; }
86         Instruction *getPrev()       { return Prev; }
87   const Instruction *getPrev() const { return Prev; }
88
89   /// removeFromParent - This method unlinks 'this' from the containing basic
90   /// block, but does not delete it.
91   ///
92   void removeFromParent();
93
94   /// eraseFromParent - This method unlinks 'this' from the containing basic
95   /// block and deletes it.
96   ///
97   void eraseFromParent();
98
99   /// moveBefore - Unlink this instruction from its current basic block and
100   /// insert it into the basic block that MovePos lives in, right before
101   /// MovePos.
102   void moveBefore(Instruction *MovePos);
103
104   // ---------------------------------------------------------------------------
105   /// Subclass classification... getOpcode() returns a member of
106   /// one of the enums that is coming soon (down below)...
107   ///
108   unsigned getOpcode() const { return getValueType() - InstructionVal; }
109   const char *getOpcodeName() const {
110     return getOpcodeName(getOpcode());
111   }
112   static const char* getOpcodeName(unsigned OpCode);
113
114   static inline bool isTerminator(unsigned OpCode) {
115     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
116   }
117
118   inline bool isTerminator() const {   // Instance of TerminatorInst?
119     return isTerminator(getOpcode());
120   }
121
122   inline bool isBinaryOp() const {
123     return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd;
124   }
125
126   /// isAssociative - Return true if the instruction is associative:
127   ///
128   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
129   ///
130   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
131   /// not applied to floating point types.
132   ///
133   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
134   static bool isAssociative(unsigned op, const Type *Ty);
135
136   /// isCommutative - Return true if the instruction is commutative:
137   ///
138   ///   Commutative operators satisfy: (x op y) === (y op x)
139   ///
140   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
141   /// applied to any type.
142   ///
143   bool isCommutative() const { return isCommutative(getOpcode()); }
144   static bool isCommutative(unsigned op);
145
146   /// isRelational - Return true if the instruction is a Set* instruction:
147   ///
148   bool isRelational() const { return isRelational(getOpcode()); }
149   static bool isRelational(unsigned op);
150
151
152   /// isTrappingInstruction - Return true if the instruction may trap.
153   ///
154   bool isTrapping() const {
155     return isTrapping(getOpcode());
156   }
157   static bool isTrapping(unsigned op);
158
159   virtual void print(std::ostream &OS) const { print(OS, 0); }
160   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
161
162   /// Methods for support type inquiry through isa, cast, and dyn_cast:
163   static inline bool classof(const Instruction *) { return true; }
164   static inline bool classof(const Value *V) {
165     return V->getValueType() >= Value::InstructionVal;
166   }
167
168   //----------------------------------------------------------------------
169   // Exported enumerations...
170   //
171   enum TermOps {       // These terminate basic blocks
172 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
173 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
174 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1
175 #include "llvm/Instruction.def"
176   };
177
178   enum BinaryOps {
179 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
180 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
181 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1
182 #include "llvm/Instruction.def"
183   };
184
185   enum MemoryOps {
186 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
187 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
188 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1
189 #include "llvm/Instruction.def"
190   };
191
192   enum OtherOps {
193 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
194 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
195 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1
196 #include "llvm/Instruction.def"
197   };
198 };
199
200 } // End llvm namespace
201
202 #endif