]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/Operator.h
Update to ELF Tool Chain snapshot at r3561
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/ADT/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/IR/Type.h"
25 #include "llvm/IR/Value.h"
26 #include "llvm/Support/Casting.h"
27 #include <cstddef>
28
29 namespace llvm {
30
31 /// This is a utility class that provides an abstraction for the common
32 /// functionality between Instructions and ConstantExprs.
33 class Operator : public User {
34 protected:
35   // NOTE: Cannot use = delete because it's not legal to delete
36   // an overridden method that's not deleted in the base class. Cannot leave
37   // this unimplemented because that leads to an ODR-violation.
38   ~Operator() override;
39
40 public:
41   // The Operator class is intended to be used as a utility, and is never itself
42   // instantiated.
43   Operator() = delete;
44
45   void *operator new(size_t, unsigned) = delete;
46   void *operator new(size_t s) = delete;
47
48   /// Return the opcode for this Instruction or ConstantExpr.
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   /// If V is an Instruction or ConstantExpr, return its opcode.
56   /// Otherwise return UserOp1.
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 /// Utility class for integer arithmetic operators which may exhibit overflow -
73 /// Add, Sub, and Mul. It does not include SDiv, despite that operator having
74 /// the potential for overflow.
75 class OverflowingBinaryOperator : public Operator {
76 public:
77   enum {
78     NoUnsignedWrap = (1 << 0),
79     NoSignedWrap   = (1 << 1)
80   };
81
82 private:
83   friend class Instruction;
84   friend class ConstantExpr;
85
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   /// 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   /// 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 /// A udiv or sdiv instruction, which can be marked as "exact",
127 /// 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 Instruction;
136   friend class ConstantExpr;
137
138   void setIsExact(bool B) {
139     SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
140   }
141
142 public:
143   /// Test whether this division is known to be exact, with 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
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
172   unsigned Flags = 0;
173
174   FastMathFlags(unsigned F) : Flags(F) { }
175
176 public:
177   enum {
178     UnsafeAlgebra   = (1 << 0),
179     NoNaNs          = (1 << 1),
180     NoInfs          = (1 << 2),
181     NoSignedZeros   = (1 << 3),
182     AllowReciprocal = (1 << 4)
183   };
184
185   FastMathFlags() = default;
186
187   /// Whether any flag is set
188   bool any() const { return Flags != 0; }
189
190   /// Set all the flags to false
191   void clear() { Flags = 0; }
192
193   /// Flag queries
194   bool noNaNs() const          { return 0 != (Flags & NoNaNs); }
195   bool noInfs() const          { return 0 != (Flags & NoInfs); }
196   bool noSignedZeros() const   { return 0 != (Flags & NoSignedZeros); }
197   bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
198   bool unsafeAlgebra() const   { return 0 != (Flags & UnsafeAlgebra); }
199
200   /// Flag setters
201   void setNoNaNs()          { Flags |= NoNaNs; }
202   void setNoInfs()          { Flags |= NoInfs; }
203   void setNoSignedZeros()   { Flags |= NoSignedZeros; }
204   void setAllowReciprocal() { Flags |= AllowReciprocal; }
205   void setUnsafeAlgebra() {
206     Flags |= UnsafeAlgebra;
207     setNoNaNs();
208     setNoInfs();
209     setNoSignedZeros();
210     setAllowReciprocal();
211   }
212
213   void operator&=(const FastMathFlags &OtherFlags) {
214     Flags &= OtherFlags.Flags;
215   }
216 };
217
218 /// Utility class for floating point operations which can have
219 /// information about relaxed accuracy requirements attached to them.
220 class FPMathOperator : public Operator {
221 private:
222   friend class Instruction;
223
224   void setHasUnsafeAlgebra(bool B) {
225     SubclassOptionalData =
226       (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
227       (B * FastMathFlags::UnsafeAlgebra);
228
229     // Unsafe algebra implies all the others
230     if (B) {
231       setHasNoNaNs(true);
232       setHasNoInfs(true);
233       setHasNoSignedZeros(true);
234       setHasAllowReciprocal(true);
235     }
236   }
237
238   void setHasNoNaNs(bool B) {
239     SubclassOptionalData =
240       (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
241       (B * FastMathFlags::NoNaNs);
242   }
243
244   void setHasNoInfs(bool B) {
245     SubclassOptionalData =
246       (SubclassOptionalData & ~FastMathFlags::NoInfs) |
247       (B * FastMathFlags::NoInfs);
248   }
249
250   void setHasNoSignedZeros(bool B) {
251     SubclassOptionalData =
252       (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
253       (B * FastMathFlags::NoSignedZeros);
254   }
255
256   void setHasAllowReciprocal(bool B) {
257     SubclassOptionalData =
258       (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
259       (B * FastMathFlags::AllowReciprocal);
260   }
261
262   /// Convenience function for setting multiple fast-math flags.
263   /// FMF is a mask of the bits to set.
264   void setFastMathFlags(FastMathFlags FMF) {
265     SubclassOptionalData |= FMF.Flags;
266   }
267
268   /// Convenience function for copying all fast-math flags.
269   /// All values in FMF are transferred to this operator.
270   void copyFastMathFlags(FastMathFlags FMF) {
271     SubclassOptionalData = FMF.Flags;
272   }
273
274 public:
275   /// Test whether this operation is permitted to be
276   /// algebraically transformed, aka the 'A' fast-math property.
277   bool hasUnsafeAlgebra() const {
278     return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
279   }
280
281   /// Test whether this operation's arguments and results are to be
282   /// treated as non-NaN, aka the 'N' fast-math property.
283   bool hasNoNaNs() const {
284     return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
285   }
286
287   /// Test whether this operation's arguments and results are to be
288   /// treated as NoN-Inf, aka the 'I' fast-math property.
289   bool hasNoInfs() const {
290     return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
291   }
292
293   /// Test whether this operation can treat the sign of zero
294   /// as insignificant, aka the 'S' fast-math property.
295   bool hasNoSignedZeros() const {
296     return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
297   }
298
299   /// Test whether this operation is permitted to use
300   /// reciprocal instead of division, aka the 'R' fast-math property.
301   bool hasAllowReciprocal() const {
302     return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
303   }
304
305   /// Convenience function for getting all the fast-math flags
306   FastMathFlags getFastMathFlags() const {
307     return FastMathFlags(SubclassOptionalData);
308   }
309
310   /// Get the maximum error permitted by this operation in ULPs. An accuracy of
311   /// 0.0 means that the operation should be performed with the default
312   /// precision.
313   float getFPAccuracy() const;
314
315   static inline bool classof(const Instruction *I) {
316     return I->getType()->isFPOrFPVectorTy() ||
317       I->getOpcode() == Instruction::FCmp;
318   }
319   static inline bool classof(const Value *V) {
320     return isa<Instruction>(V) && classof(cast<Instruction>(V));
321   }
322 };
323
324 /// A helper template for defining operators for individual opcodes.
325 template<typename SuperClass, unsigned Opc>
326 class ConcreteOperator : public SuperClass {
327 public:
328   static inline bool classof(const Instruction *I) {
329     return I->getOpcode() == Opc;
330   }
331   static inline bool classof(const ConstantExpr *CE) {
332     return CE->getOpcode() == Opc;
333   }
334   static inline bool classof(const Value *V) {
335     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
336            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
337   }
338 };
339
340 class AddOperator
341   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
342 };
343 class SubOperator
344   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
345 };
346 class MulOperator
347   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
348 };
349 class ShlOperator
350   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
351 };
352
353 class SDivOperator
354   : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
355 };
356 class UDivOperator
357   : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
358 };
359 class AShrOperator
360   : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
361 };
362 class LShrOperator
363   : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
364 };
365
366 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
367
368 class GEPOperator
369   : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
370   friend class GetElementPtrInst;
371   friend class ConstantExpr;
372
373   enum {
374     IsInBounds = (1 << 0),
375     // InRangeIndex: bits 1-6
376   };
377
378   void setIsInBounds(bool B) {
379     SubclassOptionalData =
380       (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
381   }
382
383 public:
384   /// Test whether this is an inbounds GEP, as defined by LangRef.html.
385   bool isInBounds() const {
386     return SubclassOptionalData & IsInBounds;
387   }
388
389   /// Returns the offset of the index with an inrange attachment, or None if
390   /// none.
391   Optional<unsigned> getInRangeIndex() const {
392     if (SubclassOptionalData >> 1 == 0) return None;
393     return (SubclassOptionalData >> 1) - 1;
394   }
395
396   inline op_iterator       idx_begin()       { return op_begin()+1; }
397   inline const_op_iterator idx_begin() const { return op_begin()+1; }
398   inline op_iterator       idx_end()         { return op_end(); }
399   inline const_op_iterator idx_end()   const { return op_end(); }
400
401   Value *getPointerOperand() {
402     return getOperand(0);
403   }
404   const Value *getPointerOperand() const {
405     return getOperand(0);
406   }
407   static unsigned getPointerOperandIndex() {
408     return 0U;                      // get index for modifying correct operand
409   }
410
411   /// Method to return the pointer operand as a PointerType.
412   Type *getPointerOperandType() const {
413     return getPointerOperand()->getType();
414   }
415
416   Type *getSourceElementType() const;
417   Type *getResultElementType() const;
418
419   /// Method to return the address space of the pointer operand.
420   unsigned getPointerAddressSpace() const {
421     return getPointerOperandType()->getPointerAddressSpace();
422   }
423
424   unsigned getNumIndices() const {  // Note: always non-negative
425     return getNumOperands() - 1;
426   }
427
428   bool hasIndices() const {
429     return getNumOperands() > 1;
430   }
431
432   /// Return true if all of the indices of this GEP are zeros.
433   /// If so, the result pointer and the first operand have the same
434   /// value, just potentially different types.
435   bool hasAllZeroIndices() const {
436     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
437       if (ConstantInt *C = dyn_cast<ConstantInt>(I))
438         if (C->isZero())
439           continue;
440       return false;
441     }
442     return true;
443   }
444
445   /// Return true if all of the indices of this GEP are constant integers.
446   /// If so, the result pointer and the first operand have
447   /// a constant offset between them.
448   bool hasAllConstantIndices() const {
449     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
450       if (!isa<ConstantInt>(I))
451         return false;
452     }
453     return true;
454   }
455
456   /// \brief Accumulate the constant address offset of this GEP if possible.
457   ///
458   /// This routine accepts an APInt into which it will accumulate the constant
459   /// offset of this GEP if the GEP is in fact constant. If the GEP is not
460   /// all-constant, it returns false and the value of the offset APInt is
461   /// undefined (it is *not* preserved!). The APInt passed into this routine
462   /// must be at exactly as wide as the IntPtr type for the address space of the
463   /// base GEP pointer.
464   bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
465 };
466
467 class PtrToIntOperator
468     : public ConcreteOperator<Operator, Instruction::PtrToInt> {
469   friend class PtrToInt;
470   friend class ConstantExpr;
471
472 public:
473   Value *getPointerOperand() {
474     return getOperand(0);
475   }
476   const Value *getPointerOperand() const {
477     return getOperand(0);
478   }
479
480   static unsigned getPointerOperandIndex() {
481     return 0U;                      // get index for modifying correct operand
482   }
483
484   /// Method to return the pointer operand as a PointerType.
485   Type *getPointerOperandType() const {
486     return getPointerOperand()->getType();
487   }
488
489   /// Method to return the address space of the pointer operand.
490   unsigned getPointerAddressSpace() const {
491     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
492   }
493 };
494
495 class BitCastOperator
496     : public ConcreteOperator<Operator, Instruction::BitCast> {
497   friend class BitCastInst;
498   friend class ConstantExpr;
499
500 public:
501   Type *getSrcTy() const {
502     return getOperand(0)->getType();
503   }
504
505   Type *getDestTy() const {
506     return getType();
507   }
508 };
509
510 } // end namespace llvm
511
512 #endif // LLVM_IR_OPERATOR_H