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