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