Reorganize FastMathFlags to be a wrapper around unsigned, and streamline some interfaces.
[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 class FastMathFlags {
169 private:
170   friend class FPMathOperator;
171   unsigned Flags;
172   FastMathFlags(unsigned F) : Flags(F) { }
173
174 public:
175   enum {
176     UnsafeAlgebra   = (1 << 0),
177     NoNaNs          = (1 << 1),
178     NoInfs          = (1 << 2),
179     NoSignedZeros   = (1 << 3),
180     AllowReciprocal = (1 << 4)
181   };
182
183   FastMathFlags() : Flags(0)
184   { }
185
186   /// Whether any flag is set
187   bool any() { return Flags != 0; }
188
189   /// Set all the flags to false
190   void clear() { Flags = 0; }
191
192   /// Flag queries
193   bool noNaNs()          { return 0 != (Flags & NoNaNs); }
194   bool noInfs()          { return 0 != (Flags & NoInfs); }
195   bool noSignedZeros()   { return 0 != (Flags & NoSignedZeros); }
196   bool allowReciprocal() { return 0 != (Flags & AllowReciprocal); }
197   bool unsafeAlgebra()   { return 0 != (Flags & UnsafeAlgebra); }
198
199   /// Flag setters
200   void setNoNaNs()          { Flags |= NoNaNs; }
201   void setNoInfs()          { Flags |= NoInfs; }
202   void setNoSignedZeros()   { Flags |= NoSignedZeros; }
203   void setAllowReciprocal() { Flags |= AllowReciprocal; }
204   void setUnsafeAlgebra() {
205     Flags |= UnsafeAlgebra;
206     setNoNaNs();
207     setNoInfs();
208     setNoSignedZeros();
209     setAllowReciprocal();
210   }
211 };
212
213
214 /// FPMathOperator - Utility class for floating point operations which can have
215 /// information about relaxed accuracy requirements attached to them.
216 class FPMathOperator : public Operator {
217 private:
218   friend class Instruction;
219
220   void setHasUnsafeAlgebra(bool B) {
221     SubclassOptionalData =
222       (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
223       (B * FastMathFlags::UnsafeAlgebra);
224
225     // Unsafe algebra implies all the others
226     if (B) {
227       setHasNoNaNs(true);
228       setHasNoInfs(true);
229       setHasNoSignedZeros(true);
230       setHasAllowReciprocal(true);
231     }
232   }
233   void setHasNoNaNs(bool B) {
234     SubclassOptionalData =
235       (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
236       (B * FastMathFlags::NoNaNs);
237   }
238   void setHasNoInfs(bool B) {
239     SubclassOptionalData =
240       (SubclassOptionalData & ~FastMathFlags::NoInfs) |
241       (B * FastMathFlags::NoInfs);
242   }
243   void setHasNoSignedZeros(bool B) {
244     SubclassOptionalData =
245       (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
246       (B * FastMathFlags::NoSignedZeros);
247   }
248   void setHasAllowReciprocal(bool B) {
249     SubclassOptionalData =
250       (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
251       (B * FastMathFlags::AllowReciprocal);
252   }
253
254   /// Convenience function for setting all the fast-math flags
255   void setFastMathFlags(FastMathFlags FMF) {
256     SubclassOptionalData |= FMF.Flags;
257   }
258
259 public:
260   /// Test whether this operation is permitted to be
261   /// algebraically transformed, aka the 'A' fast-math property.
262   bool hasUnsafeAlgebra() const {
263     return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
264   }
265
266   /// Test whether this operation's arguments and results are to be
267   /// treated as non-NaN, aka the 'N' fast-math property.
268   bool hasNoNaNs() const {
269     return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
270   }
271
272   /// Test whether this operation's arguments and results are to be
273   /// treated as NoN-Inf, aka the 'I' fast-math property.
274   bool hasNoInfs() const {
275     return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
276   }
277
278   /// Test whether this operation can treat the sign of zero
279   /// as insignificant, aka the 'S' fast-math property.
280   bool hasNoSignedZeros() const {
281     return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
282   }
283
284   /// Test whether this operation is permitted to use
285   /// reciprocal instead of division, aka the 'R' fast-math property.
286   bool hasAllowReciprocal() const {
287     return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
288   }
289
290   /// Convenience function for getting all the fast-math flags
291   FastMathFlags getFastMathFlags() const {
292     return FastMathFlags(SubclassOptionalData);
293   }
294
295   /// \brief Get the maximum error permitted by this operation in ULPs.  An
296   /// accuracy of 0.0 means that the operation should be performed with the
297   /// default precision.
298   float getFPAccuracy() const;
299
300   static inline bool classof(const Instruction *I) {
301     return I->getType()->isFPOrFPVectorTy();
302   }
303   static inline bool classof(const Value *V) {
304     return isa<Instruction>(V) && classof(cast<Instruction>(V));
305   }
306 };
307
308
309 /// ConcreteOperator - A helper template for defining operators for individual
310 /// opcodes.
311 template<typename SuperClass, unsigned Opc>
312 class ConcreteOperator : public SuperClass {
313 public:
314   static inline bool classof(const Instruction *I) {
315     return I->getOpcode() == Opc;
316   }
317   static inline bool classof(const ConstantExpr *CE) {
318     return CE->getOpcode() == Opc;
319   }
320   static inline bool classof(const Value *V) {
321     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
322            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
323   }
324 };
325
326 class AddOperator
327   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
328 };
329 class SubOperator
330   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
331 };
332 class MulOperator
333   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
334 };
335 class ShlOperator
336   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
337 };
338
339
340 class SDivOperator
341   : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
342 };
343 class UDivOperator
344   : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
345 };
346 class AShrOperator
347   : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
348 };
349 class LShrOperator
350   : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
351 };
352
353
354
355 class GEPOperator
356   : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
357   enum {
358     IsInBounds = (1 << 0)
359   };
360
361   friend class GetElementPtrInst;
362   friend class ConstantExpr;
363   void setIsInBounds(bool B) {
364     SubclassOptionalData =
365       (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
366   }
367
368 public:
369   /// isInBounds - Test whether this is an inbounds GEP, as defined
370   /// by LangRef.html.
371   bool isInBounds() const {
372     return SubclassOptionalData & IsInBounds;
373   }
374
375   inline op_iterator       idx_begin()       { return op_begin()+1; }
376   inline const_op_iterator idx_begin() const { return op_begin()+1; }
377   inline op_iterator       idx_end()         { return op_end(); }
378   inline const_op_iterator idx_end()   const { return op_end(); }
379
380   Value *getPointerOperand() {
381     return getOperand(0);
382   }
383   const Value *getPointerOperand() const {
384     return getOperand(0);
385   }
386   static unsigned getPointerOperandIndex() {
387     return 0U;                      // get index for modifying correct operand
388   }
389
390   /// getPointerOperandType - Method to return the pointer operand as a
391   /// PointerType.
392   Type *getPointerOperandType() const {
393     return getPointerOperand()->getType();
394   }
395
396   /// getPointerAddressSpace - Method to return the address space of the
397   /// pointer operand.
398   unsigned getPointerAddressSpace() const {
399     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
400   }
401
402   unsigned getNumIndices() const {  // Note: always non-negative
403     return getNumOperands() - 1;
404   }
405
406   bool hasIndices() const {
407     return getNumOperands() > 1;
408   }
409
410   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
411   /// zeros.  If so, the result pointer and the first operand have the same
412   /// value, just potentially different types.
413   bool hasAllZeroIndices() const {
414     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
415       if (ConstantInt *C = dyn_cast<ConstantInt>(I))
416         if (C->isZero())
417           continue;
418       return false;
419     }
420     return true;
421   }
422
423   /// hasAllConstantIndices - Return true if all of the indices of this GEP are
424   /// constant integers.  If so, the result pointer and the first operand have
425   /// a constant offset between them.
426   bool hasAllConstantIndices() const {
427     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
428       if (!isa<ConstantInt>(I))
429         return false;
430     }
431     return true;
432   }
433 };
434
435 } // End llvm namespace
436
437 #endif