Fixed/added namespace ending comments using clang-tidy. NFC
[oota-llvm.git] / include / llvm / IR / 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_IR_OPERATOR_H
16 #define LLVM_IR_OPERATOR_H
17
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/Type.h"
23
24 namespace llvm {
25
26 class GetElementPtrInst;
27 class BinaryOperator;
28 class ConstantExpr;
29
30 /// This is a utility class that provides an abstraction for the common
31 /// functionality between Instructions and ConstantExprs.
32 class Operator : public User {
33 private:
34   // The Operator class is intended to be used as a utility, and is never itself
35   // instantiated.
36   void *operator new(size_t, unsigned) = delete;
37   void *operator new(size_t s) = delete;
38   Operator() = delete;
39
40 protected:
41   // NOTE: Cannot use = delete 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() override;
45
46 public:
47   /// Return the opcode for this Instruction or ConstantExpr.
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   /// If V is an Instruction or ConstantExpr, return its opcode.
55   /// Otherwise return UserOp1.
56   static unsigned getOpcode(const Value *V) {
57     if (const Instruction *I = dyn_cast<Instruction>(V))
58       return I->getOpcode();
59     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
60       return CE->getOpcode();
61     return Instruction::UserOp1;
62   }
63
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 /// Utility class for integer arithmetic operators which may exhibit overflow -
72 /// Add, Sub, and Mul. It does not include SDiv, despite that operator having
73 /// the potential for overflow.
74 class OverflowingBinaryOperator : public Operator {
75 public:
76   enum {
77     NoUnsignedWrap = (1 << 0),
78     NoSignedWrap   = (1 << 1)
79   };
80
81 private:
82   friend class BinaryOperator;
83   friend class ConstantExpr;
84   void setHasNoUnsignedWrap(bool B) {
85     SubclassOptionalData =
86       (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
87   }
88   void setHasNoSignedWrap(bool B) {
89     SubclassOptionalData =
90       (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
91   }
92
93 public:
94   /// Test whether this operation is known to never
95   /// undergo unsigned overflow, aka the nuw property.
96   bool hasNoUnsignedWrap() const {
97     return SubclassOptionalData & NoUnsignedWrap;
98   }
99
100   /// Test whether this operation is known to never
101   /// undergo signed overflow, aka the nsw property.
102   bool hasNoSignedWrap() const {
103     return (SubclassOptionalData & NoSignedWrap) != 0;
104   }
105
106   static inline bool classof(const Instruction *I) {
107     return I->getOpcode() == Instruction::Add ||
108            I->getOpcode() == Instruction::Sub ||
109            I->getOpcode() == Instruction::Mul ||
110            I->getOpcode() == Instruction::Shl;
111   }
112   static inline bool classof(const ConstantExpr *CE) {
113     return CE->getOpcode() == Instruction::Add ||
114            CE->getOpcode() == Instruction::Sub ||
115            CE->getOpcode() == Instruction::Mul ||
116            CE->getOpcode() == Instruction::Shl;
117   }
118   static inline bool classof(const Value *V) {
119     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
120            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
121   }
122 };
123
124 /// A udiv or sdiv instruction, which can be marked as "exact",
125 /// indicating that no bits are destroyed.
126 class PossiblyExactOperator : public Operator {
127 public:
128   enum {
129     IsExact = (1 << 0)
130   };
131
132 private:
133   friend class BinaryOperator;
134   friend class ConstantExpr;
135   void setIsExact(bool B) {
136     SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
137   }
138
139 public:
140   /// Test whether this division is known to be exact, with zero remainder.
141   bool isExact() const {
142     return SubclassOptionalData & IsExact;
143   }
144
145   static bool isPossiblyExactOpcode(unsigned OpC) {
146     return OpC == Instruction::SDiv ||
147            OpC == Instruction::UDiv ||
148            OpC == Instruction::AShr ||
149            OpC == Instruction::LShr;
150   }
151   static inline bool classof(const ConstantExpr *CE) {
152     return isPossiblyExactOpcode(CE->getOpcode());
153   }
154   static inline bool classof(const Instruction *I) {
155     return isPossiblyExactOpcode(I->getOpcode());
156   }
157   static inline bool classof(const Value *V) {
158     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
159            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
160   }
161 };
162
163 /// Convenience struct for specifying and reasoning about fast-math flags.
164 class FastMathFlags {
165 private:
166   friend class FPMathOperator;
167   unsigned Flags;
168   FastMathFlags(unsigned F) : Flags(F) { }
169
170 public:
171   enum {
172     UnsafeAlgebra   = (1 << 0),
173     NoNaNs          = (1 << 1),
174     NoInfs          = (1 << 2),
175     NoSignedZeros   = (1 << 3),
176     AllowReciprocal = (1 << 4)
177   };
178
179   FastMathFlags() : Flags(0)
180   { }
181
182   /// Whether any flag is set
183   bool any() const { return Flags != 0; }
184
185   /// Set all the flags to false
186   void clear() { Flags = 0; }
187
188   /// Flag queries
189   bool noNaNs() const          { return 0 != (Flags & NoNaNs); }
190   bool noInfs() const          { return 0 != (Flags & NoInfs); }
191   bool noSignedZeros() const   { return 0 != (Flags & NoSignedZeros); }
192   bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
193   bool unsafeAlgebra() const   { return 0 != (Flags & UnsafeAlgebra); }
194
195   /// Flag setters
196   void setNoNaNs()          { Flags |= NoNaNs; }
197   void setNoInfs()          { Flags |= NoInfs; }
198   void setNoSignedZeros()   { Flags |= NoSignedZeros; }
199   void setAllowReciprocal() { Flags |= AllowReciprocal; }
200   void setUnsafeAlgebra() {
201     Flags |= UnsafeAlgebra;
202     setNoNaNs();
203     setNoInfs();
204     setNoSignedZeros();
205     setAllowReciprocal();
206   }
207
208   void operator&=(const FastMathFlags &OtherFlags) {
209     Flags &= OtherFlags.Flags;
210   }
211 };
212
213
214 /// 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 multiple fast-math flags.
255   /// FMF is a mask of the bits to set.
256   void setFastMathFlags(FastMathFlags FMF) {
257     SubclassOptionalData |= FMF.Flags;
258   }
259
260   /// Convenience function for copying all fast-math flags.
261   /// All values in FMF are transferred to this operator.
262   void copyFastMathFlags(FastMathFlags FMF) {
263     SubclassOptionalData = FMF.Flags;
264   }
265
266 public:
267   /// Test whether this operation is permitted to be
268   /// algebraically transformed, aka the 'A' fast-math property.
269   bool hasUnsafeAlgebra() const {
270     return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
271   }
272
273   /// Test whether this operation's arguments and results are to be
274   /// treated as non-NaN, aka the 'N' fast-math property.
275   bool hasNoNaNs() const {
276     return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
277   }
278
279   /// Test whether this operation's arguments and results are to be
280   /// treated as NoN-Inf, aka the 'I' fast-math property.
281   bool hasNoInfs() const {
282     return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
283   }
284
285   /// Test whether this operation can treat the sign of zero
286   /// as insignificant, aka the 'S' fast-math property.
287   bool hasNoSignedZeros() const {
288     return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
289   }
290
291   /// Test whether this operation is permitted to use
292   /// reciprocal instead of division, aka the 'R' fast-math property.
293   bool hasAllowReciprocal() const {
294     return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
295   }
296
297   /// Convenience function for getting all the fast-math flags
298   FastMathFlags getFastMathFlags() const {
299     return FastMathFlags(SubclassOptionalData);
300   }
301
302   /// \brief Get the maximum error permitted by this operation in ULPs.  An
303   /// accuracy of 0.0 means that the operation should be performed with the
304   /// default precision.
305   float getFPAccuracy() const;
306
307   static inline bool classof(const Instruction *I) {
308     return I->getType()->isFPOrFPVectorTy();
309   }
310   static inline bool classof(const Value *V) {
311     return isa<Instruction>(V) && classof(cast<Instruction>(V));
312   }
313 };
314
315
316 /// A helper template for defining operators for individual opcodes.
317 template<typename SuperClass, unsigned Opc>
318 class ConcreteOperator : public SuperClass {
319 public:
320   static inline bool classof(const Instruction *I) {
321     return I->getOpcode() == Opc;
322   }
323   static inline bool classof(const ConstantExpr *CE) {
324     return CE->getOpcode() == Opc;
325   }
326   static inline bool classof(const Value *V) {
327     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
328            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
329   }
330 };
331
332 class AddOperator
333   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
334 };
335 class SubOperator
336   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
337 };
338 class MulOperator
339   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
340 };
341 class ShlOperator
342   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
343 };
344
345
346 class SDivOperator
347   : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
348 };
349 class UDivOperator
350   : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
351 };
352 class AShrOperator
353   : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
354 };
355 class LShrOperator
356   : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
357 };
358
359
360 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
361
362
363 class GEPOperator
364   : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
365   enum {
366     IsInBounds = (1 << 0)
367   };
368
369   friend class GetElementPtrInst;
370   friend class ConstantExpr;
371   void setIsInBounds(bool B) {
372     SubclassOptionalData =
373       (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
374   }
375
376 public:
377   /// Test whether this is an inbounds GEP, as defined by LangRef.html.
378   bool isInBounds() const {
379     return SubclassOptionalData & IsInBounds;
380   }
381
382   inline op_iterator       idx_begin()       { return op_begin()+1; }
383   inline const_op_iterator idx_begin() const { return op_begin()+1; }
384   inline op_iterator       idx_end()         { return op_end(); }
385   inline const_op_iterator idx_end()   const { return op_end(); }
386
387   Value *getPointerOperand() {
388     return getOperand(0);
389   }
390   const Value *getPointerOperand() const {
391     return getOperand(0);
392   }
393   static unsigned getPointerOperandIndex() {
394     return 0U;                      // get index for modifying correct operand
395   }
396
397   /// Method to return the pointer operand as a PointerType.
398   Type *getPointerOperandType() const {
399     return getPointerOperand()->getType();
400   }
401
402   Type *getSourceElementType() const;
403
404   /// Method to return the address space of the pointer operand.
405   unsigned getPointerAddressSpace() const {
406     return getPointerOperandType()->getPointerAddressSpace();
407   }
408
409   unsigned getNumIndices() const {  // Note: always non-negative
410     return getNumOperands() - 1;
411   }
412
413   bool hasIndices() const {
414     return getNumOperands() > 1;
415   }
416
417   /// Return true if all of the indices of this GEP are zeros.
418   /// If so, the result pointer and the first operand have the same
419   /// value, just potentially different types.
420   bool hasAllZeroIndices() const {
421     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
422       if (ConstantInt *C = dyn_cast<ConstantInt>(I))
423         if (C->isZero())
424           continue;
425       return false;
426     }
427     return true;
428   }
429
430   /// Return true if all of the indices of this GEP are constant integers.
431   /// If so, the result pointer and the first operand have
432   /// a constant offset between them.
433   bool hasAllConstantIndices() const {
434     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
435       if (!isa<ConstantInt>(I))
436         return false;
437     }
438     return true;
439   }
440
441   /// \brief Accumulate the constant address offset of this GEP if possible.
442   ///
443   /// This routine accepts an APInt into which it will accumulate the constant
444   /// offset of this GEP if the GEP is in fact constant. If the GEP is not
445   /// all-constant, it returns false and the value of the offset APInt is
446   /// undefined (it is *not* preserved!). The APInt passed into this routine
447   /// must be at exactly as wide as the IntPtr type for the address space of the
448   /// base GEP pointer.
449   bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
450 };
451
452 class PtrToIntOperator
453     : public ConcreteOperator<Operator, Instruction::PtrToInt> {
454   friend class PtrToInt;
455   friend class ConstantExpr;
456
457 public:
458   Value *getPointerOperand() {
459     return getOperand(0);
460   }
461   const Value *getPointerOperand() const {
462     return getOperand(0);
463   }
464   static unsigned getPointerOperandIndex() {
465     return 0U;                      // get index for modifying correct operand
466   }
467
468   /// Method to return the pointer operand as a PointerType.
469   Type *getPointerOperandType() const {
470     return getPointerOperand()->getType();
471   }
472
473   /// Method to return the address space of the pointer operand.
474   unsigned getPointerAddressSpace() const {
475     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
476   }
477 };
478
479 class BitCastOperator
480     : public ConcreteOperator<Operator, Instruction::BitCast> {
481   friend class BitCastInst;
482   friend class ConstantExpr;
483
484 public:
485   Type *getSrcTy() const {
486     return getOperand(0)->getType();
487   }
488
489   Type *getDestTy() const {
490     return getType();
491   }
492 };
493
494 } // namespace llvm
495
496 #endif