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