Now that the SparcV9 specific MachineCodeForInstruction class uses it's own
[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   BasicBlock *Parent;
31   Instruction *Prev, *Next; // Next and Prev links for our intrusive linked list
32
33   void setNext(Instruction *N) { Next = N; }
34   void setPrev(Instruction *N) { Prev = N; }
35
36   friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
37                                      ilist_traits<Instruction> >;
38   void setParent(BasicBlock *P);
39   void init();
40
41 private:
42   // FIXME: This is a dirty hack.  Setcc instructions shouldn't encode the CC
43   // into the opcode field.  When they don't, this will be unneeded.
44   void setOpcode(unsigned NewOpcode);
45   friend class BinaryOperator;
46 protected:
47   Instruction(const Type *Ty, unsigned iType, const std::string &Name = "",
48               Instruction *InsertBefore = 0);
49   Instruction(const Type *Ty, unsigned iType, const std::string &Name,
50               BasicBlock *InsertAtEnd);
51 public:
52
53   ~Instruction() {
54     assert(Parent == 0 && "Instruction still linked in the program!");
55   }
56
57   // Specialize setName to handle symbol table majik...
58   virtual void setName(const std::string &name, SymbolTable *ST = 0);
59   
60   /// clone() - Create a copy of 'this' instruction that is identical in all
61   /// ways except the following:
62   ///   * The instruction has no parent
63   ///   * The instruction has no name
64   ///
65   virtual Instruction *clone() const = 0;
66   
67   // Accessor methods...
68   //
69   inline const BasicBlock *getParent() const { return Parent; }
70   inline       BasicBlock *getParent()       { return Parent; }
71
72   // getNext/Prev - Return the next or previous instruction in the list.  The
73   // last node in the list is a terminator instruction.
74         Instruction *getNext()       { return Next; }
75   const Instruction *getNext() const { return Next; }
76         Instruction *getPrev()       { return Prev; }
77   const Instruction *getPrev() const { return Prev; }
78
79   /// mayWriteToMemory - Return true if this instruction may modify memory.
80   ///
81   virtual bool mayWriteToMemory() const { return false; }
82
83   // ---------------------------------------------------------------------------
84   /// Subclass classification... getOpcode() returns a member of 
85   /// one of the enums that is coming soon (down below)...
86   ///
87   unsigned getOpcode() const { return getValueType() - InstructionVal; }
88   virtual const char *getOpcodeName() const {
89     return getOpcodeName(getOpcode());
90   }
91   static const char* getOpcodeName(unsigned OpCode);
92
93   static inline bool isTerminator(unsigned OpCode) {
94     return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
95   }
96
97   inline bool isTerminator() const {   // Instance of TerminatorInst?
98     return isTerminator(getOpcode());
99   }
100
101   inline bool isBinaryOp() const {
102     return getOpcode() >= BinaryOpsBegin && getOpcode() < BinaryOpsEnd;
103   }
104
105   /// isAssociative - Return true if the instruction is associative:
106   ///
107   ///   Associative operators satisfy:  x op (y op z) === (x op y) op z
108   ///
109   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative, when
110   /// not applied to floating point types.
111   ///
112   bool isAssociative() const { return isAssociative(getOpcode(), getType()); }
113   static bool isAssociative(unsigned op, const Type *Ty);
114
115   /// isCommutative - Return true if the instruction is commutative:
116   ///
117   ///   Commutative operators satisfy: (x op y) === (y op x)
118   ///
119   /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
120   /// applied to any type.
121   ///
122   bool isCommutative() const { return isCommutative(getOpcode()); }
123   static bool isCommutative(unsigned op);
124
125   /// isRelational - Return true if the instruction is a Set* instruction:
126   ///
127   bool isRelational() const { return isRelational(getOpcode()); }
128   static bool isRelational(unsigned op);
129
130
131   /// isTrappingInstruction - Return true if the instruction may trap.
132   ///
133   bool isTrapping() const {
134     return isTrapping(getOpcode()); 
135   }
136   static bool isTrapping(unsigned op);
137   
138   virtual void print(std::ostream &OS) const { print(OS, 0); }
139   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
140
141   /// Methods for support type inquiry through isa, cast, and dyn_cast:
142   static inline bool classof(const Instruction *I) { return true; }
143   static inline bool classof(const Value *V) {
144     return V->getValueType() >= Value::InstructionVal;
145   }
146   
147   //----------------------------------------------------------------------
148   // Exported enumerations...
149   //
150   enum TermOps {       // These terminate basic blocks
151 #define  FIRST_TERM_INST(N)             TermOpsBegin = N,
152 #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
153 #define   LAST_TERM_INST(N)             TermOpsEnd = N+1,
154 #include "llvm/Instruction.def"
155   };
156
157   enum BinaryOps {
158 #define  FIRST_BINARY_INST(N)             BinaryOpsBegin = N,
159 #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
160 #define   LAST_BINARY_INST(N)             BinaryOpsEnd = N+1,
161 #include "llvm/Instruction.def"
162   };
163
164   enum MemoryOps {
165 #define  FIRST_MEMORY_INST(N)             MemoryOpsBegin = N,
166 #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
167 #define   LAST_MEMORY_INST(N)             MemoryOpsEnd = N+1,
168 #include "llvm/Instruction.def"
169   };
170
171   enum OtherOps {
172 #define  FIRST_OTHER_INST(N)             OtherOpsBegin = N,
173 #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
174 #define   LAST_OTHER_INST(N)             OtherOpsEnd = N+1,
175 #include "llvm/Instruction.def"
176   };
177 };
178
179 } // End llvm namespace
180
181 #endif