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