LCOV - code coverage report
Current view: top level - include/llvm/IR - Operator.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 116 120 96.7 %
Date: 2017-09-14 15:23:50 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13