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