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