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