Avoid implicitly depending on Instructions.h.
[oota-llvm.git] / include / llvm / Operator.h
1 //===-- llvm/Operator.h - Operator utility subclass -------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines various classes for working with Instructions and
11 // ConstantExprs.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_OPERATOR_H
16 #define LLVM_OPERATOR_H
17
18 #include "llvm/Instruction.h"
19 #include "llvm/Constants.h"
20
21 namespace llvm {
22
23 class GetElementPtrInst;
24
25 /// Operator - This is a utility class that provides an abstraction for the
26 /// common functionality between Instructions and ConstantExprs.
27 ///
28 class Operator : public User {
29 private:
30   // Do not implement any of these. The Operator class is intended to be used
31   // as a utility, and is never itself instantiated.
32   void *operator new(size_t, unsigned);
33   void *operator new(size_t s);
34   Operator();
35   ~Operator();
36
37 public:
38   /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
39   ///
40   unsigned getOpcode() const {
41     if (const Instruction *I = dyn_cast<Instruction>(this))
42       return I->getOpcode();
43     return cast<ConstantExpr>(this)->getOpcode();
44   }
45
46   /// getOpcode - If V is an Instruction or ConstantExpr, return its
47   /// opcode. Otherwise return UserOp1.
48   ///
49   static unsigned getOpcode(const Value *V) {
50     if (const Instruction *I = dyn_cast<Instruction>(V))
51       return I->getOpcode();
52     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
53       return CE->getOpcode();
54     return Instruction::UserOp1;
55   }
56
57   static inline bool classof(const Operator *) { return true; }
58   static inline bool classof(const Instruction *I) { return true; }
59   static inline bool classof(const ConstantExpr *I) { return true; }
60   static inline bool classof(const Value *V) {
61     return isa<Instruction>(V) || isa<ConstantExpr>(V);
62   }
63 };
64
65 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
66 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
67 /// despite that operator having the potential for overflow.
68 ///
69 class OverflowingBinaryOperator : public Operator {
70 public:
71   /// hasNoUnsignedOverflow - Test whether this operation is known to never
72   /// undergo unsigned overflow.
73   bool hasNoUnsignedOverflow() const {
74     return SubclassOptionalData & (1 << 0);
75   }
76   void setHasNoUnsignedOverflow(bool B) {
77     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
78   }
79
80   /// hasNoSignedOverflow - Test whether this operation is known to never
81   /// undergo signed overflow.
82   bool hasNoSignedOverflow() const {
83     return SubclassOptionalData & (1 << 1);
84   }
85   void setHasNoSignedOverflow(bool B) {
86     SubclassOptionalData = (SubclassOptionalData & ~(1 << 1)) | (B << 1);
87   }
88
89   static inline bool classof(const OverflowingBinaryOperator *) { return true; }
90   static inline bool classof(const Instruction *I) {
91     return I->getOpcode() == Instruction::Add ||
92            I->getOpcode() == Instruction::Sub ||
93            I->getOpcode() == Instruction::Mul;
94   }
95   static inline bool classof(const ConstantExpr *CE) {
96     return CE->getOpcode() == Instruction::Add ||
97            CE->getOpcode() == Instruction::Sub ||
98            CE->getOpcode() == Instruction::Mul;
99   }
100   static inline bool classof(const Value *V) {
101     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
102            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
103   }
104 };
105
106 /// AddOperator - Utility class for integer addition operators.
107 ///
108 class AddOperator : public OverflowingBinaryOperator {
109 public:
110   static inline bool classof(const AddOperator *) { return true; }
111   static inline bool classof(const Instruction *I) {
112     return I->getOpcode() == Instruction::Add;
113   }
114   static inline bool classof(const ConstantExpr *CE) {
115     return CE->getOpcode() == Instruction::Add;
116   }
117   static inline bool classof(const Value *V) {
118     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
119            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
120   }
121 };
122
123 /// SubOperator - Utility class for integer subtraction operators.
124 ///
125 class SubOperator : public OverflowingBinaryOperator {
126 public:
127   static inline bool classof(const SubOperator *) { return true; }
128   static inline bool classof(const Instruction *I) {
129     return I->getOpcode() == Instruction::Sub;
130   }
131   static inline bool classof(const ConstantExpr *CE) {
132     return CE->getOpcode() == Instruction::Sub;
133   }
134   static inline bool classof(const Value *V) {
135     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
136            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
137   }
138 };
139
140 /// MulOperator - Utility class for integer multiplication operators.
141 ///
142 class MulOperator : public OverflowingBinaryOperator {
143 public:
144   static inline bool classof(const MulOperator *) { return true; }
145   static inline bool classof(const Instruction *I) {
146     return I->getOpcode() == Instruction::Mul;
147   }
148   static inline bool classof(const ConstantExpr *CE) {
149     return CE->getOpcode() == Instruction::Mul;
150   }
151   static inline bool classof(const Value *V) {
152     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
153            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
154   }
155 };
156
157 /// SDivOperator - An Operator with opcode Instruction::SDiv.
158 ///
159 class SDivOperator : public Operator {
160 public:
161   /// isExact - Test whether this division is known to be exact, with
162   /// zero remainder.
163   bool isExact() const {
164     return SubclassOptionalData & (1 << 0);
165   }
166   void setIsExact(bool B) {
167     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
168   }
169
170   // Methods for support type inquiry through isa, cast, and dyn_cast:
171   static inline bool classof(const SDivOperator *) { return true; }
172   static inline bool classof(const ConstantExpr *CE) {
173     return CE->getOpcode() == Instruction::SDiv;
174   }
175   static inline bool classof(const Instruction *I) {
176     return I->getOpcode() == Instruction::SDiv;
177   }
178   static inline bool classof(const Value *V) {
179     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
180            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
181   }
182 };
183
184 class GEPOperator : public Operator {
185 public:
186   /// isInBounds - Test whether this is an inbounds GEP, as defined
187   /// by LangRef.html.
188   bool isInBounds() const {
189     return SubclassOptionalData & (1 << 0);
190   }
191   void setIsInBounds(bool B) {
192     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
193   }
194
195   inline op_iterator       idx_begin()       { return op_begin()+1; }
196   inline const_op_iterator idx_begin() const { return op_begin()+1; }
197   inline op_iterator       idx_end()         { return op_end(); }
198   inline const_op_iterator idx_end()   const { return op_end(); }
199
200   Value *getPointerOperand() {
201     return getOperand(0);
202   }
203   const Value *getPointerOperand() const {
204     return getOperand(0);
205   }
206   static unsigned getPointerOperandIndex() {
207     return 0U;                      // get index for modifying correct operand
208   }
209
210   /// getPointerOperandType - Method to return the pointer operand as a
211   /// PointerType.
212   const PointerType *getPointerOperandType() const {
213     return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
214   }
215
216   unsigned getNumIndices() const {  // Note: always non-negative
217     return getNumOperands() - 1;
218   }
219
220   bool hasIndices() const {
221     return getNumOperands() > 1;
222   }
223
224   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
225   /// zeros.  If so, the result pointer and the first operand have the same
226   /// value, just potentially different types.
227   bool hasAllZeroIndices() const {
228     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
229       if (Constant *C = dyn_cast<Constant>(I))
230         if (C->isNullValue())
231           continue;
232       return false;
233     }
234     return true;
235   }
236
237   // Methods for support type inquiry through isa, cast, and dyn_cast:
238   static inline bool classof(const GEPOperator *) { return true; }
239   static inline bool classof(const GetElementPtrInst *) { return true; }
240   static inline bool classof(const ConstantExpr *CE) {
241     return CE->getOpcode() == Instruction::GetElementPtr;
242   }
243   static inline bool classof(const Instruction *I) {
244     return I->getOpcode() == Instruction::GetElementPtr;
245   }
246   static inline bool classof(const Value *V) {
247     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
248            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
249   }
250 };
251
252 } // End llvm namespace
253
254 #endif