Fix ODR violations: a virtual function must be defined, even if it's never
[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
40   // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
41   // an overridden method that's not deleted in the base class. Cannot leave
42   // this unimplemented because that leads to an ODR-violation.
43   ~Operator();
44
45 public:
46   /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
47   ///
48   unsigned getOpcode() const {
49     if (const Instruction *I = dyn_cast<Instruction>(this))
50       return I->getOpcode();
51     return cast<ConstantExpr>(this)->getOpcode();
52   }
53
54   /// getOpcode - If V is an Instruction or ConstantExpr, return its
55   /// opcode. Otherwise return UserOp1.
56   ///
57   static unsigned getOpcode(const Value *V) {
58     if (const Instruction *I = dyn_cast<Instruction>(V))
59       return I->getOpcode();
60     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
61       return CE->getOpcode();
62     return Instruction::UserOp1;
63   }
64
65   static inline bool classof(const Instruction *) { return true; }
66   static inline bool classof(const ConstantExpr *) { return true; }
67   static inline bool classof(const Value *V) {
68     return isa<Instruction>(V) || isa<ConstantExpr>(V);
69   }
70 };
71
72 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
73 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
74 /// despite that operator having the potential for overflow.
75 ///
76 class OverflowingBinaryOperator : public Operator {
77 public:
78   enum {
79     NoUnsignedWrap = (1 << 0),
80     NoSignedWrap   = (1 << 1)
81   };
82
83 private:
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   friend class BinaryOperator;
136   friend class ConstantExpr;
137   void setIsExact(bool B) {
138     SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
139   }
140   
141 public:
142   /// isExact - Test whether this division is known to be exact, with
143   /// zero remainder.
144   bool isExact() const {
145     return SubclassOptionalData & IsExact;
146   }
147   
148   static bool isPossiblyExactOpcode(unsigned OpC) {
149     return OpC == Instruction::SDiv ||
150            OpC == Instruction::UDiv ||
151            OpC == Instruction::AShr ||
152            OpC == Instruction::LShr;
153   }
154   static inline bool classof(const ConstantExpr *CE) {
155     return isPossiblyExactOpcode(CE->getOpcode());
156   }
157   static inline bool classof(const Instruction *I) {
158     return isPossiblyExactOpcode(I->getOpcode());
159   }
160   static inline bool classof(const Value *V) {
161     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
162            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
163   }
164 };
165
166 /// FPMathOperator - Utility class for floating point operations which can have
167 /// information about relaxed accuracy requirements attached to them.
168 class FPMathOperator : public Operator {
169 public:
170
171   /// \brief Get the maximum error permitted by this operation in ULPs.  An
172   /// accuracy of 0.0 means that the operation should be performed with the
173   /// default precision.
174   float getFPAccuracy() const;
175
176   static inline bool classof(const Instruction *I) {
177     return I->getType()->isFPOrFPVectorTy();
178   }
179   static inline bool classof(const Value *V) {
180     return isa<Instruction>(V) && classof(cast<Instruction>(V));
181   }
182 };
183
184   
185 /// ConcreteOperator - A helper template for defining operators for individual
186 /// opcodes.
187 template<typename SuperClass, unsigned Opc>
188 class ConcreteOperator : public SuperClass {
189 public:
190   static inline bool classof(const Instruction *I) {
191     return I->getOpcode() == Opc;
192   }
193   static inline bool classof(const ConstantExpr *CE) {
194     return CE->getOpcode() == Opc;
195   }
196   static inline bool classof(const Value *V) {
197     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
198            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
199   }
200 };
201
202 class AddOperator
203   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
204 };
205 class SubOperator
206   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
207 };
208 class MulOperator
209   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
210 };
211 class ShlOperator
212   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
213 };
214
215
216 class SDivOperator
217   : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
218 };
219 class UDivOperator
220   : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
221 };
222 class AShrOperator
223   : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
224 };
225 class LShrOperator
226   : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
227 };
228
229
230
231 class GEPOperator
232   : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
233   enum {
234     IsInBounds = (1 << 0)
235   };
236
237   friend class GetElementPtrInst;
238   friend class ConstantExpr;
239   void setIsInBounds(bool B) {
240     SubclassOptionalData =
241       (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
242   }
243
244 public:
245   /// isInBounds - Test whether this is an inbounds GEP, as defined
246   /// by LangRef.html.
247   bool isInBounds() const {
248     return SubclassOptionalData & IsInBounds;
249   }
250
251   inline op_iterator       idx_begin()       { return op_begin()+1; }
252   inline const_op_iterator idx_begin() const { return op_begin()+1; }
253   inline op_iterator       idx_end()         { return op_end(); }
254   inline const_op_iterator idx_end()   const { return op_end(); }
255
256   Value *getPointerOperand() {
257     return getOperand(0);
258   }
259   const Value *getPointerOperand() const {
260     return getOperand(0);
261   }
262   static unsigned getPointerOperandIndex() {
263     return 0U;                      // get index for modifying correct operand
264   }
265
266   /// getPointerOperandType - Method to return the pointer operand as a
267   /// PointerType.
268   Type *getPointerOperandType() const {
269     return getPointerOperand()->getType();
270   }
271
272   /// getPointerAddressSpace - Method to return the address space of the
273   /// pointer operand.
274   unsigned getPointerAddressSpace() const {
275     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
276   }
277
278   unsigned getNumIndices() const {  // Note: always non-negative
279     return getNumOperands() - 1;
280   }
281
282   bool hasIndices() const {
283     return getNumOperands() > 1;
284   }
285
286   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
287   /// zeros.  If so, the result pointer and the first operand have the same
288   /// value, just potentially different types.
289   bool hasAllZeroIndices() const {
290     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
291       if (ConstantInt *C = dyn_cast<ConstantInt>(I))
292         if (C->isZero())
293           continue;
294       return false;
295     }
296     return true;
297   }
298
299   /// hasAllConstantIndices - Return true if all of the indices of this GEP are
300   /// constant integers.  If so, the result pointer and the first operand have
301   /// a constant offset between them.
302   bool hasAllConstantIndices() const {
303     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
304       if (!isa<ConstantInt>(I))
305         return false;
306     }
307     return true;
308   }
309 };
310
311 } // End llvm namespace
312
313 #endif