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