LLVM  mainline
Operator.h
Go to the documentation of this file.
00001 //===-- llvm/Operator.h - Operator utility subclass -------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines various classes for working with Instructions and
00011 // ConstantExprs.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_IR_OPERATOR_H
00016 #define LLVM_IR_OPERATOR_H
00017 
00018 #include "llvm/IR/Constants.h"
00019 #include "llvm/IR/DataLayout.h"
00020 #include "llvm/IR/DerivedTypes.h"
00021 #include "llvm/IR/Instruction.h"
00022 #include "llvm/IR/Type.h"
00023 
00024 namespace llvm {
00025 
00026 class GetElementPtrInst;
00027 class BinaryOperator;
00028 class ConstantExpr;
00029 
00030 /// This is a utility class that provides an abstraction for the common
00031 /// functionality between Instructions and ConstantExprs.
00032 class Operator : public User {
00033 private:
00034   // The Operator class is intended to be used as a utility, and is never itself
00035   // instantiated.
00036   void *operator new(size_t, unsigned) = delete;
00037   void *operator new(size_t s) = delete;
00038   Operator() = delete;
00039 
00040 protected:
00041   // NOTE: Cannot use = delete because it's not legal to delete
00042   // an overridden method that's not deleted in the base class. Cannot leave
00043   // this unimplemented because that leads to an ODR-violation.
00044   ~Operator() override;
00045 
00046 public:
00047   /// Return the opcode for this Instruction or ConstantExpr.
00048   unsigned getOpcode() const {
00049     if (const Instruction *I = dyn_cast<Instruction>(this))
00050       return I->getOpcode();
00051     return cast<ConstantExpr>(this)->getOpcode();
00052   }
00053 
00054   /// If V is an Instruction or ConstantExpr, return its opcode.
00055   /// Otherwise return UserOp1.
00056   static unsigned getOpcode(const Value *V) {
00057     if (const Instruction *I = dyn_cast<Instruction>(V))
00058       return I->getOpcode();
00059     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00060       return CE->getOpcode();
00061     return Instruction::UserOp1;
00062   }
00063 
00064   static inline bool classof(const Instruction *) { return true; }
00065   static inline bool classof(const ConstantExpr *) { return true; }
00066   static inline bool classof(const Value *V) {
00067     return isa<Instruction>(V) || isa<ConstantExpr>(V);
00068   }
00069 };
00070 
00071 /// Utility class for integer arithmetic operators which may exhibit overflow -
00072 /// Add, Sub, and Mul. It does not include SDiv, despite that operator having
00073 /// the potential for overflow.
00074 class OverflowingBinaryOperator : public Operator {
00075 public:
00076   enum {
00077     NoUnsignedWrap = (1 << 0),
00078     NoSignedWrap   = (1 << 1)
00079   };
00080 
00081 private:
00082   friend class BinaryOperator;
00083   friend class ConstantExpr;
00084   void setHasNoUnsignedWrap(bool B) {
00085     SubclassOptionalData =
00086       (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
00087   }
00088   void setHasNoSignedWrap(bool B) {
00089     SubclassOptionalData =
00090       (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
00091   }
00092 
00093 public:
00094   /// Test whether this operation is known to never
00095   /// undergo unsigned overflow, aka the nuw property.
00096   bool hasNoUnsignedWrap() const {
00097     return SubclassOptionalData & NoUnsignedWrap;
00098   }
00099 
00100   /// Test whether this operation is known to never
00101   /// undergo signed overflow, aka the nsw property.
00102   bool hasNoSignedWrap() const {
00103     return (SubclassOptionalData & NoSignedWrap) != 0;
00104   }
00105 
00106   static inline bool classof(const Instruction *I) {
00107     return I->getOpcode() == Instruction::Add ||
00108            I->getOpcode() == Instruction::Sub ||
00109            I->getOpcode() == Instruction::Mul ||
00110            I->getOpcode() == Instruction::Shl;
00111   }
00112   static inline bool classof(const ConstantExpr *CE) {
00113     return CE->getOpcode() == Instruction::Add ||
00114            CE->getOpcode() == Instruction::Sub ||
00115            CE->getOpcode() == Instruction::Mul ||
00116            CE->getOpcode() == Instruction::Shl;
00117   }
00118   static inline bool classof(const Value *V) {
00119     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00120            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00121   }
00122 };
00123 
00124 /// A udiv or sdiv instruction, which can be marked as "exact",
00125 /// indicating that no bits are destroyed.
00126 class PossiblyExactOperator : public Operator {
00127 public:
00128   enum {
00129     IsExact = (1 << 0)
00130   };
00131 
00132 private:
00133   friend class BinaryOperator;
00134   friend class ConstantExpr;
00135   void setIsExact(bool B) {
00136     SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
00137   }
00138 
00139 public:
00140   /// Test whether this division is known to be exact, with zero remainder.
00141   bool isExact() const {
00142     return SubclassOptionalData & IsExact;
00143   }
00144 
00145   static bool isPossiblyExactOpcode(unsigned OpC) {
00146     return OpC == Instruction::SDiv ||
00147            OpC == Instruction::UDiv ||
00148            OpC == Instruction::AShr ||
00149            OpC == Instruction::LShr;
00150   }
00151   static inline bool classof(const ConstantExpr *CE) {
00152     return isPossiblyExactOpcode(CE->getOpcode());
00153   }
00154   static inline bool classof(const Instruction *I) {
00155     return isPossiblyExactOpcode(I->getOpcode());
00156   }
00157   static inline bool classof(const Value *V) {
00158     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00159            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00160   }
00161 };
00162 
00163 /// Convenience struct for specifying and reasoning about fast-math flags.
00164 class FastMathFlags {
00165 private:
00166   friend class FPMathOperator;
00167   unsigned Flags;
00168   FastMathFlags(unsigned F) : Flags(F) { }
00169 
00170 public:
00171   enum {
00172     UnsafeAlgebra   = (1 << 0),
00173     NoNaNs          = (1 << 1),
00174     NoInfs          = (1 << 2),
00175     NoSignedZeros   = (1 << 3),
00176     AllowReciprocal = (1 << 4)
00177   };
00178 
00179   FastMathFlags() : Flags(0)
00180   { }
00181 
00182   /// Whether any flag is set
00183   bool any() const { return Flags != 0; }
00184 
00185   /// Set all the flags to false
00186   void clear() { Flags = 0; }
00187 
00188   /// Flag queries
00189   bool noNaNs() const          { return 0 != (Flags & NoNaNs); }
00190   bool noInfs() const          { return 0 != (Flags & NoInfs); }
00191   bool noSignedZeros() const   { return 0 != (Flags & NoSignedZeros); }
00192   bool allowReciprocal() const { return 0 != (Flags & AllowReciprocal); }
00193   bool unsafeAlgebra() const   { return 0 != (Flags & UnsafeAlgebra); }
00194 
00195   /// Flag setters
00196   void setNoNaNs()          { Flags |= NoNaNs; }
00197   void setNoInfs()          { Flags |= NoInfs; }
00198   void setNoSignedZeros()   { Flags |= NoSignedZeros; }
00199   void setAllowReciprocal() { Flags |= AllowReciprocal; }
00200   void setUnsafeAlgebra() {
00201     Flags |= UnsafeAlgebra;
00202     setNoNaNs();
00203     setNoInfs();
00204     setNoSignedZeros();
00205     setAllowReciprocal();
00206   }
00207 
00208   void operator&=(const FastMathFlags &OtherFlags) {
00209     Flags &= OtherFlags.Flags;
00210   }
00211 };
00212 
00213 
00214 /// Utility class for floating point operations which can have
00215 /// information about relaxed accuracy requirements attached to them.
00216 class FPMathOperator : public Operator {
00217 private:
00218   friend class Instruction;
00219 
00220   void setHasUnsafeAlgebra(bool B) {
00221     SubclassOptionalData =
00222       (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
00223       (B * FastMathFlags::UnsafeAlgebra);
00224 
00225     // Unsafe algebra implies all the others
00226     if (B) {
00227       setHasNoNaNs(true);
00228       setHasNoInfs(true);
00229       setHasNoSignedZeros(true);
00230       setHasAllowReciprocal(true);
00231     }
00232   }
00233   void setHasNoNaNs(bool B) {
00234     SubclassOptionalData =
00235       (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
00236       (B * FastMathFlags::NoNaNs);
00237   }
00238   void setHasNoInfs(bool B) {
00239     SubclassOptionalData =
00240       (SubclassOptionalData & ~FastMathFlags::NoInfs) |
00241       (B * FastMathFlags::NoInfs);
00242   }
00243   void setHasNoSignedZeros(bool B) {
00244     SubclassOptionalData =
00245       (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
00246       (B * FastMathFlags::NoSignedZeros);
00247   }
00248   void setHasAllowReciprocal(bool B) {
00249     SubclassOptionalData =
00250       (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
00251       (B * FastMathFlags::AllowReciprocal);
00252   }
00253 
00254   /// Convenience function for setting multiple fast-math flags.
00255   /// FMF is a mask of the bits to set.
00256   void setFastMathFlags(FastMathFlags FMF) {
00257     SubclassOptionalData |= FMF.Flags;
00258   }
00259 
00260   /// Convenience function for copying all fast-math flags.
00261   /// All values in FMF are transferred to this operator.
00262   void copyFastMathFlags(FastMathFlags FMF) {
00263     SubclassOptionalData = FMF.Flags;
00264   }
00265 
00266 public:
00267   /// Test whether this operation is permitted to be
00268   /// algebraically transformed, aka the 'A' fast-math property.
00269   bool hasUnsafeAlgebra() const {
00270     return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
00271   }
00272 
00273   /// Test whether this operation's arguments and results are to be
00274   /// treated as non-NaN, aka the 'N' fast-math property.
00275   bool hasNoNaNs() const {
00276     return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
00277   }
00278 
00279   /// Test whether this operation's arguments and results are to be
00280   /// treated as NoN-Inf, aka the 'I' fast-math property.
00281   bool hasNoInfs() const {
00282     return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
00283   }
00284 
00285   /// Test whether this operation can treat the sign of zero
00286   /// as insignificant, aka the 'S' fast-math property.
00287   bool hasNoSignedZeros() const {
00288     return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
00289   }
00290 
00291   /// Test whether this operation is permitted to use
00292   /// reciprocal instead of division, aka the 'R' fast-math property.
00293   bool hasAllowReciprocal() const {
00294     return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
00295   }
00296 
00297   /// Convenience function for getting all the fast-math flags
00298   FastMathFlags getFastMathFlags() const {
00299     return FastMathFlags(SubclassOptionalData);
00300   }
00301 
00302   /// \brief Get the maximum error permitted by this operation in ULPs.  An
00303   /// accuracy of 0.0 means that the operation should be performed with the
00304   /// default precision.
00305   float getFPAccuracy() const;
00306 
00307   static inline bool classof(const Instruction *I) {
00308     return I->getType()->isFPOrFPVectorTy();
00309   }
00310   static inline bool classof(const Value *V) {
00311     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00312   }
00313 };
00314 
00315 
00316 /// A helper template for defining operators for individual opcodes.
00317 template<typename SuperClass, unsigned Opc>
00318 class ConcreteOperator : public SuperClass {
00319 public:
00320   static inline bool classof(const Instruction *I) {
00321     return I->getOpcode() == Opc;
00322   }
00323   static inline bool classof(const ConstantExpr *CE) {
00324     return CE->getOpcode() == Opc;
00325   }
00326   static inline bool classof(const Value *V) {
00327     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00328            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00329   }
00330 };
00331 
00332 class AddOperator
00333   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
00334 };
00335 class SubOperator
00336   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
00337 };
00338 class MulOperator
00339   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
00340 };
00341 class ShlOperator
00342   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
00343 };
00344 
00345 
00346 class SDivOperator
00347   : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
00348 };
00349 class UDivOperator
00350   : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
00351 };
00352 class AShrOperator
00353   : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
00354 };
00355 class LShrOperator
00356   : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
00357 };
00358 
00359 
00360 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
00361 
00362 
00363 class GEPOperator
00364   : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
00365   enum {
00366     IsInBounds = (1 << 0)
00367   };
00368 
00369   friend class GetElementPtrInst;
00370   friend class ConstantExpr;
00371   void setIsInBounds(bool B) {
00372     SubclassOptionalData =
00373       (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
00374   }
00375 
00376 public:
00377   /// Test whether this is an inbounds GEP, as defined by LangRef.html.
00378   bool isInBounds() const {
00379     return SubclassOptionalData & IsInBounds;
00380   }
00381 
00382   inline op_iterator       idx_begin()       { return op_begin()+1; }
00383   inline const_op_iterator idx_begin() const { return op_begin()+1; }
00384   inline op_iterator       idx_end()         { return op_end(); }
00385   inline const_op_iterator idx_end()   const { return op_end(); }
00386 
00387   Value *getPointerOperand() {
00388     return getOperand(0);
00389   }
00390   const Value *getPointerOperand() const {
00391     return getOperand(0);
00392   }
00393   static unsigned getPointerOperandIndex() {
00394     return 0U;                      // get index for modifying correct operand
00395   }
00396 
00397   /// Method to return the pointer operand as a PointerType.
00398   Type *getPointerOperandType() const {
00399     return getPointerOperand()->getType();
00400   }
00401 
00402   Type *getSourceElementType() const;
00403 
00404   /// Method to return the address space of the pointer operand.
00405   unsigned getPointerAddressSpace() const {
00406     return getPointerOperandType()->getPointerAddressSpace();
00407   }
00408 
00409   unsigned getNumIndices() const {  // Note: always non-negative
00410     return getNumOperands() - 1;
00411   }
00412 
00413   bool hasIndices() const {
00414     return getNumOperands() > 1;
00415   }
00416 
00417   /// Return true if all of the indices of this GEP are zeros.
00418   /// If so, the result pointer and the first operand have the same
00419   /// value, just potentially different types.
00420   bool hasAllZeroIndices() const {
00421     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
00422       if (ConstantInt *C = dyn_cast<ConstantInt>(I))
00423         if (C->isZero())
00424           continue;
00425       return false;
00426     }
00427     return true;
00428   }
00429 
00430   /// Return true if all of the indices of this GEP are constant integers.
00431   /// If so, the result pointer and the first operand have
00432   /// a constant offset between them.
00433   bool hasAllConstantIndices() const {
00434     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
00435       if (!isa<ConstantInt>(I))
00436         return false;
00437     }
00438     return true;
00439   }
00440 
00441   /// \brief Accumulate the constant address offset of this GEP if possible.
00442   ///
00443   /// This routine accepts an APInt into which it will accumulate the constant
00444   /// offset of this GEP if the GEP is in fact constant. If the GEP is not
00445   /// all-constant, it returns false and the value of the offset APInt is
00446   /// undefined (it is *not* preserved!). The APInt passed into this routine
00447   /// must be at exactly as wide as the IntPtr type for the address space of the
00448   /// base GEP pointer.
00449   bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
00450 };
00451 
00452 class PtrToIntOperator
00453     : public ConcreteOperator<Operator, Instruction::PtrToInt> {
00454   friend class PtrToInt;
00455   friend class ConstantExpr;
00456 
00457 public:
00458   Value *getPointerOperand() {
00459     return getOperand(0);
00460   }
00461   const Value *getPointerOperand() const {
00462     return getOperand(0);
00463   }
00464   static unsigned getPointerOperandIndex() {
00465     return 0U;                      // get index for modifying correct operand
00466   }
00467 
00468   /// Method to return the pointer operand as a PointerType.
00469   Type *getPointerOperandType() const {
00470     return getPointerOperand()->getType();
00471   }
00472 
00473   /// Method to return the address space of the pointer operand.
00474   unsigned getPointerAddressSpace() const {
00475     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
00476   }
00477 };
00478 
00479 class BitCastOperator
00480     : public ConcreteOperator<Operator, Instruction::BitCast> {
00481   friend class BitCastInst;
00482   friend class ConstantExpr;
00483 
00484 public:
00485   Type *getSrcTy() const {
00486     return getOperand(0)->getType();
00487   }
00488 
00489   Type *getDestTy() const {
00490     return getType();
00491   }
00492 };
00493 
00494 } // End llvm namespace
00495 
00496 #endif