Add in some interfaces that will allow easier access to the pointer address space.
[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/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instruction.h"
21 #include "llvm/Type.h"
22
23 namespace llvm {
24
25 class GetElementPtrInst;
26 class BinaryOperator;
27 class ConstantExpr;
28
29 /// Operator - This is a utility class that provides an abstraction for the
30 /// common functionality between Instructions and ConstantExprs.
31 ///
32 class Operator : public User {
33 private:
34   // Do not implement any of these. The Operator class is intended to be used
35   // as a utility, and is never itself instantiated.
36   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
37   void *operator new(size_t s) LLVM_DELETED_FUNCTION;
38   Operator() LLVM_DELETED_FUNCTION;
39   // NOTE: cannot use LLVM_DELETED_FUNCTION because gcc errors when deleting
40   // an override of a non-deleted function.
41   ~Operator();
42
43 public:
44   /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
45   ///
46   unsigned getOpcode() const {
47     if (const Instruction *I = dyn_cast<Instruction>(this))
48       return I->getOpcode();
49     return cast<ConstantExpr>(this)->getOpcode();
50   }
51
52   /// getOpcode - If V is an Instruction or ConstantExpr, return its
53   /// opcode. Otherwise return UserOp1.
54   ///
55   static unsigned getOpcode(const Value *V) {
56     if (const Instruction *I = dyn_cast<Instruction>(V))
57       return I->getOpcode();
58     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
59       return CE->getOpcode();
60     return Instruction::UserOp1;
61   }
62
63   static inline bool classof(const Operator *) { return true; }
64   static inline bool classof(const Instruction *) { return true; }
65   static inline bool classof(const ConstantExpr *) { return true; }
66   static inline bool classof(const Value *V) {
67     return isa<Instruction>(V) || isa<ConstantExpr>(V);
68   }
69 };
70
71 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
72 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
73 /// despite that operator having the potential for overflow.
74 ///
75 class OverflowingBinaryOperator : public Operator {
76 public:
77   enum {
78     NoUnsignedWrap = (1 << 0),
79     NoSignedWrap   = (1 << 1)
80   };
81
82 private:
83   ~OverflowingBinaryOperator(); // DO NOT IMPLEMENT
84
85   friend class BinaryOperator;
86   friend class ConstantExpr;
87   void setHasNoUnsignedWrap(bool B) {
88     SubclassOptionalData =
89       (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
90   }
91   void setHasNoSignedWrap(bool B) {
92     SubclassOptionalData =
93       (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
94   }
95
96 public:
97   /// hasNoUnsignedWrap - Test whether this operation is known to never
98   /// undergo unsigned overflow, aka the nuw property.
99   bool hasNoUnsignedWrap() const {
100     return SubclassOptionalData & NoUnsignedWrap;
101   }
102
103   /// hasNoSignedWrap - Test whether this operation is known to never
104   /// undergo signed overflow, aka the nsw property.
105   bool hasNoSignedWrap() const {
106     return (SubclassOptionalData & NoSignedWrap) != 0;
107   }
108
109   static inline bool classof(const OverflowingBinaryOperator *) { return true; }
110   static inline bool classof(const Instruction *I) {
111     return I->getOpcode() == Instruction::Add ||
112            I->getOpcode() == Instruction::Sub ||
113            I->getOpcode() == Instruction::Mul ||
114            I->getOpcode() == Instruction::Shl;
115   }
116   static inline bool classof(const ConstantExpr *CE) {
117     return CE->getOpcode() == Instruction::Add ||
118            CE->getOpcode() == Instruction::Sub ||
119            CE->getOpcode() == Instruction::Mul ||
120            CE->getOpcode() == Instruction::Shl;
121   }
122   static inline bool classof(const Value *V) {
123     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
124            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
125   }
126 };
127
128 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
129 /// "exact", indicating that no bits are destroyed.
130 class PossiblyExactOperator : public Operator {
131 public:
132   enum {
133     IsExact = (1 << 0)
134   };
135   
136 private:
137   ~PossiblyExactOperator(); // DO NOT IMPLEMENT
138
139   friend class BinaryOperator;
140   friend class ConstantExpr;
141   void setIsExact(bool B) {
142     SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
143   }
144   
145 public:
146   /// isExact - Test whether this division is known to be exact, with
147   /// zero remainder.
148   bool isExact() const {
149     return SubclassOptionalData & IsExact;
150   }
151   
152   static bool isPossiblyExactOpcode(unsigned OpC) {
153     return OpC == Instruction::SDiv ||
154            OpC == Instruction::UDiv ||
155            OpC == Instruction::AShr ||
156            OpC == Instruction::LShr;
157   }
158   static inline bool classof(const ConstantExpr *CE) {
159     return isPossiblyExactOpcode(CE->getOpcode());
160   }
161   static inline bool classof(const Instruction *I) {
162     return isPossiblyExactOpcode(I->getOpcode());
163   }
164   static inline bool classof(const Value *V) {
165     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
166            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
167   }
168 };
169
170 /// FPMathOperator - Utility class for floating point operations which can have
171 /// information about relaxed accuracy requirements attached to them.
172 class FPMathOperator : public Operator {
173 private:
174   ~FPMathOperator(); // DO NOT IMPLEMENT
175
176 public:
177
178   /// \brief Get the maximum error permitted by this operation in ULPs.  An
179   /// accuracy of 0.0 means that the operation should be performed with the
180   /// default precision.
181   float getFPAccuracy() const;
182
183   static inline bool classof(const FPMathOperator *) { return true; }
184   static inline bool classof(const Instruction *I) {
185     return I->getType()->isFPOrFPVectorTy();
186   }
187   static inline bool classof(const Value *V) {
188     return isa<Instruction>(V) && classof(cast<Instruction>(V));
189   }
190 };
191
192   
193 /// ConcreteOperator - A helper template for defining operators for individual
194 /// opcodes.
195 template<typename SuperClass, unsigned Opc>
196 class ConcreteOperator : public SuperClass {
197   ~ConcreteOperator() LLVM_DELETED_FUNCTION;
198 public:
199   static inline bool classof(const ConcreteOperator<SuperClass, Opc> *) {
200     return true;
201   }
202   static inline bool classof(const Instruction *I) {
203     return I->getOpcode() == Opc;
204   }
205   static inline bool classof(const ConstantExpr *CE) {
206     return CE->getOpcode() == Opc;
207   }
208   static inline bool classof(const Value *V) {
209     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
210            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
211   }
212 };
213
214 class AddOperator
215   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
216   ~AddOperator() LLVM_DELETED_FUNCTION;
217 };
218 class SubOperator
219   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
220   ~SubOperator() LLVM_DELETED_FUNCTION;
221 };
222 class MulOperator
223   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
224   ~MulOperator() LLVM_DELETED_FUNCTION;
225 };
226 class ShlOperator
227   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
228   ~ShlOperator() LLVM_DELETED_FUNCTION;
229 };
230
231   
232 class SDivOperator
233   : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
234   ~SDivOperator() LLVM_DELETED_FUNCTION;
235 };
236 class UDivOperator
237   : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
238   ~UDivOperator() LLVM_DELETED_FUNCTION;
239 };
240 class AShrOperator
241   : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
242   ~AShrOperator() LLVM_DELETED_FUNCTION;
243 };
244 class LShrOperator
245   : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
246   ~LShrOperator() LLVM_DELETED_FUNCTION;
247 };
248   
249   
250   
251 class GEPOperator
252   : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
253   ~GEPOperator() LLVM_DELETED_FUNCTION;
254
255   enum {
256     IsInBounds = (1 << 0)
257   };
258
259   friend class GetElementPtrInst;
260   friend class ConstantExpr;
261   void setIsInBounds(bool B) {
262     SubclassOptionalData =
263       (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
264   }
265
266 public:
267   /// isInBounds - Test whether this is an inbounds GEP, as defined
268   /// by LangRef.html.
269   bool isInBounds() const {
270     return SubclassOptionalData & IsInBounds;
271   }
272
273   inline op_iterator       idx_begin()       { return op_begin()+1; }
274   inline const_op_iterator idx_begin() const { return op_begin()+1; }
275   inline op_iterator       idx_end()         { return op_end(); }
276   inline const_op_iterator idx_end()   const { return op_end(); }
277
278   Value *getPointerOperand() {
279     return getOperand(0);
280   }
281   const Value *getPointerOperand() const {
282     return getOperand(0);
283   }
284   static unsigned getPointerOperandIndex() {
285     return 0U;                      // get index for modifying correct operand
286   }
287
288   /// getPointerOperandType - Method to return the pointer operand as a
289   /// PointerType.
290   Type *getPointerOperandType() const {
291     return getPointerOperand()->getType();
292   }
293
294   /// getPointerAddressSpace - Method to return the address space of the
295   /// pointer operand.
296   unsigned getPointerAddressSpace() const {
297     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
298   }
299
300   unsigned getNumIndices() const {  // Note: always non-negative
301     return getNumOperands() - 1;
302   }
303
304   bool hasIndices() const {
305     return getNumOperands() > 1;
306   }
307
308   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
309   /// zeros.  If so, the result pointer and the first operand have the same
310   /// value, just potentially different types.
311   bool hasAllZeroIndices() const {
312     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
313       if (ConstantInt *C = dyn_cast<ConstantInt>(I))
314         if (C->isZero())
315           continue;
316       return false;
317     }
318     return true;
319   }
320
321   /// hasAllConstantIndices - Return true if all of the indices of this GEP are
322   /// constant integers.  If so, the result pointer and the first operand have
323   /// a constant offset between them.
324   bool hasAllConstantIndices() const {
325     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
326       if (!isa<ConstantInt>(I))
327         return false;
328     }
329     return true;
330   }
331 };
332
333 } // End llvm namespace
334
335 #endif