Fast-math comments and convenience method
[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 protected:
41   // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
42   // an overridden method that's not deleted in the base class. Cannot leave
43   // this unimplemented because that leads to an ODR-violation.
44   ~Operator();
45
46 public:
47   /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
48   ///
49   unsigned getOpcode() const {
50     if (const Instruction *I = dyn_cast<Instruction>(this))
51       return I->getOpcode();
52     return cast<ConstantExpr>(this)->getOpcode();
53   }
54
55   /// getOpcode - If V is an Instruction or ConstantExpr, return its
56   /// opcode. Otherwise return UserOp1.
57   ///
58   static unsigned getOpcode(const Value *V) {
59     if (const Instruction *I = dyn_cast<Instruction>(V))
60       return I->getOpcode();
61     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
62       return CE->getOpcode();
63     return Instruction::UserOp1;
64   }
65
66   static inline bool classof(const Instruction *) { return true; }
67   static inline bool classof(const ConstantExpr *) { return true; }
68   static inline bool classof(const Value *V) {
69     return isa<Instruction>(V) || isa<ConstantExpr>(V);
70   }
71 };
72
73 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
74 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
75 /// despite that operator having the potential for overflow.
76 ///
77 class OverflowingBinaryOperator : public Operator {
78 public:
79   enum {
80     NoUnsignedWrap = (1 << 0),
81     NoSignedWrap   = (1 << 1)
82   };
83
84 private:
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 Instruction *I) {
110     return I->getOpcode() == Instruction::Add ||
111            I->getOpcode() == Instruction::Sub ||
112            I->getOpcode() == Instruction::Mul ||
113            I->getOpcode() == Instruction::Shl;
114   }
115   static inline bool classof(const ConstantExpr *CE) {
116     return CE->getOpcode() == Instruction::Add ||
117            CE->getOpcode() == Instruction::Sub ||
118            CE->getOpcode() == Instruction::Mul ||
119            CE->getOpcode() == Instruction::Shl;
120   }
121   static inline bool classof(const Value *V) {
122     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
123            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
124   }
125 };
126
127 /// PossiblyExactOperator - A udiv or sdiv instruction, which can be marked as
128 /// "exact", indicating that no bits are destroyed.
129 class PossiblyExactOperator : public Operator {
130 public:
131   enum {
132     IsExact = (1 << 0)
133   };
134
135 private:
136   friend class BinaryOperator;
137   friend class ConstantExpr;
138   void setIsExact(bool B) {
139     SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
140   }
141
142 public:
143   /// isExact - Test whether this division is known to be exact, with
144   /// zero remainder.
145   bool isExact() const {
146     return SubclassOptionalData & IsExact;
147   }
148
149   static bool isPossiblyExactOpcode(unsigned OpC) {
150     return OpC == Instruction::SDiv ||
151            OpC == Instruction::UDiv ||
152            OpC == Instruction::AShr ||
153            OpC == Instruction::LShr;
154   }
155   static inline bool classof(const ConstantExpr *CE) {
156     return isPossiblyExactOpcode(CE->getOpcode());
157   }
158   static inline bool classof(const Instruction *I) {
159     return isPossiblyExactOpcode(I->getOpcode());
160   }
161   static inline bool classof(const Value *V) {
162     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
163            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
164   }
165 };
166
167 /// Convenience struct for specifying and reasoning about fast-math flags.
168 struct FastMathFlags {
169   bool UnsafeAlgebra   : 1;
170   bool NoNaNs          : 1;
171   bool NoInfs          : 1;
172   bool NoSignedZeros   : 1;
173   bool AllowReciprocal : 1;
174
175   FastMathFlags() : UnsafeAlgebra(false), NoNaNs(false), NoInfs(false),
176                     NoSignedZeros(false), AllowReciprocal(false)
177   { }
178
179   /// Whether any flag is set
180   bool any() {
181     return UnsafeAlgebra || NoNaNs || NoInfs || NoSignedZeros ||
182       AllowReciprocal;
183   }
184
185   /// Set all the flags to false
186   void clear() {
187     UnsafeAlgebra = NoNaNs = NoInfs = NoSignedZeros = AllowReciprocal = false;
188   }
189 };
190
191
192 /// FPMathOperator - Utility class for floating point operations which can have
193 /// information about relaxed accuracy requirements attached to them.
194 class FPMathOperator : public Operator {
195 public:
196   enum {
197     UnsafeAlgebra   = (1 << 0),
198     NoNaNs          = (1 << 1),
199     NoInfs          = (1 << 2),
200     NoSignedZeros   = (1 << 3),
201     AllowReciprocal = (1 << 4)
202   };
203
204 private:
205   friend class Instruction;
206
207   void setHasUnsafeAlgebra(bool B) {
208     SubclassOptionalData =
209       (SubclassOptionalData & ~UnsafeAlgebra) | (B * UnsafeAlgebra);
210
211     // Unsafe algebra implies all the others
212     if (B) {
213       setHasNoNaNs(true);
214       setHasNoInfs(true);
215       setHasNoSignedZeros(true);
216       setHasAllowReciprocal(true);
217     }
218   }
219   void setHasNoNaNs(bool B) {
220     SubclassOptionalData =
221       (SubclassOptionalData & ~NoNaNs) | (B * NoNaNs);
222   }
223   void setHasNoInfs(bool B) {
224     SubclassOptionalData =
225       (SubclassOptionalData & ~NoInfs) | (B * NoInfs);
226   }
227   void setHasNoSignedZeros(bool B) {
228     SubclassOptionalData =
229       (SubclassOptionalData & ~NoSignedZeros) | (B * NoSignedZeros);
230   }
231   void setHasAllowReciprocal(bool B) {
232     SubclassOptionalData =
233       (SubclassOptionalData & ~AllowReciprocal) | (B * AllowReciprocal);
234   }
235
236   /// Convenience function for setting all the fast-math flags
237   void setFastMathFlags(FastMathFlags FMF) {
238     if (FMF.UnsafeAlgebra) {
239       // Set all the bits to true
240       setHasUnsafeAlgebra(true);
241       return;
242     }
243
244     setHasUnsafeAlgebra(FMF.UnsafeAlgebra);
245     setHasNoNaNs(FMF.NoNaNs);
246     setHasNoInfs(FMF.NoInfs);
247     setHasNoSignedZeros(FMF.NoSignedZeros);
248     setHasAllowReciprocal(FMF.AllowReciprocal);
249   }
250
251 public:
252   /// Test whether this operation is permitted to be
253   /// algebraically transformed, aka the 'A' fast-math property.
254   bool hasUnsafeAlgebra() const {
255     return (SubclassOptionalData & UnsafeAlgebra) != 0;
256   }
257
258   /// Test whether this operation's arguments and results are to be
259   /// treated as non-NaN, aka the 'N' fast-math property.
260   bool hasNoNaNs() const {
261     return (SubclassOptionalData & NoNaNs) != 0;
262   }
263
264   /// Test whether this operation's arguments and results are to be
265   /// treated as NoN-Inf, aka the 'I' fast-math property.
266   bool hasNoInfs() const {
267     return (SubclassOptionalData & NoInfs) != 0;
268   }
269
270   /// Test whether this operation can treat the sign of zero
271   /// as insignificant, aka the 'S' fast-math property.
272   bool hasNoSignedZeros() const {
273     return (SubclassOptionalData & NoSignedZeros) != 0;
274   }
275
276   /// Test whether this operation is permitted to use
277   /// reciprocal instead of division, aka the 'R' fast-math property.
278   bool hasAllowReciprocal() const {
279     return (SubclassOptionalData & AllowReciprocal) != 0;
280   }
281
282   /// Convenience function for getting all the fast-math flags
283   FastMathFlags getFastMathFlags() const {
284     FastMathFlags FMF;
285     FMF.UnsafeAlgebra   = hasUnsafeAlgebra();
286     FMF.NoNaNs          = hasNoNaNs();
287     FMF.NoInfs          = hasNoInfs();
288     FMF.NoSignedZeros   = hasNoSignedZeros();
289     FMF.AllowReciprocal = hasAllowReciprocal();
290     return FMF;
291   }
292
293
294   /// \brief Get the maximum error permitted by this operation in ULPs.  An
295   /// accuracy of 0.0 means that the operation should be performed with the
296   /// default precision.
297   float getFPAccuracy() const;
298
299   static inline bool classof(const Instruction *I) {
300     return I->getType()->isFPOrFPVectorTy();
301   }
302   static inline bool classof(const Value *V) {
303     return isa<Instruction>(V) && classof(cast<Instruction>(V));
304   }
305 };
306
307
308 /// ConcreteOperator - A helper template for defining operators for individual
309 /// opcodes.
310 template<typename SuperClass, unsigned Opc>
311 class ConcreteOperator : public SuperClass {
312 public:
313   static inline bool classof(const Instruction *I) {
314     return I->getOpcode() == Opc;
315   }
316   static inline bool classof(const ConstantExpr *CE) {
317     return CE->getOpcode() == Opc;
318   }
319   static inline bool classof(const Value *V) {
320     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
321            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
322   }
323 };
324
325 class AddOperator
326   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
327 };
328 class SubOperator
329   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
330 };
331 class MulOperator
332   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
333 };
334 class ShlOperator
335   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
336 };
337
338
339 class SDivOperator
340   : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
341 };
342 class UDivOperator
343   : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
344 };
345 class AShrOperator
346   : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
347 };
348 class LShrOperator
349   : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
350 };
351
352
353
354 class GEPOperator
355   : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
356   enum {
357     IsInBounds = (1 << 0)
358   };
359
360   friend class GetElementPtrInst;
361   friend class ConstantExpr;
362   void setIsInBounds(bool B) {
363     SubclassOptionalData =
364       (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
365   }
366
367 public:
368   /// isInBounds - Test whether this is an inbounds GEP, as defined
369   /// by LangRef.html.
370   bool isInBounds() const {
371     return SubclassOptionalData & IsInBounds;
372   }
373
374   inline op_iterator       idx_begin()       { return op_begin()+1; }
375   inline const_op_iterator idx_begin() const { return op_begin()+1; }
376   inline op_iterator       idx_end()         { return op_end(); }
377   inline const_op_iterator idx_end()   const { return op_end(); }
378
379   Value *getPointerOperand() {
380     return getOperand(0);
381   }
382   const Value *getPointerOperand() const {
383     return getOperand(0);
384   }
385   static unsigned getPointerOperandIndex() {
386     return 0U;                      // get index for modifying correct operand
387   }
388
389   /// getPointerOperandType - Method to return the pointer operand as a
390   /// PointerType.
391   Type *getPointerOperandType() const {
392     return getPointerOperand()->getType();
393   }
394
395   /// getPointerAddressSpace - Method to return the address space of the
396   /// pointer operand.
397   unsigned getPointerAddressSpace() const {
398     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
399   }
400
401   unsigned getNumIndices() const {  // Note: always non-negative
402     return getNumOperands() - 1;
403   }
404
405   bool hasIndices() const {
406     return getNumOperands() > 1;
407   }
408
409   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
410   /// zeros.  If so, the result pointer and the first operand have the same
411   /// value, just potentially different types.
412   bool hasAllZeroIndices() const {
413     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
414       if (ConstantInt *C = dyn_cast<ConstantInt>(I))
415         if (C->isZero())
416           continue;
417       return false;
418     }
419     return true;
420   }
421
422   /// hasAllConstantIndices - Return true if all of the indices of this GEP are
423   /// constant integers.  If so, the result pointer and the first operand have
424   /// a constant offset between them.
425   bool hasAllConstantIndices() const {
426     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
427       if (!isa<ConstantInt>(I))
428         return false;
429     }
430     return true;
431   }
432 };
433
434 } // End llvm namespace
435
436 #endif