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