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       I->getOpcode() == Instruction::FCmp;
00310   }
00311   static inline bool classof(const Value *V) {
00312     return isa<Instruction>(V) && classof(cast<Instruction>(V));
00313   }
00314 };
00315 
00316 
00317 /// A helper template for defining operators for individual opcodes.
00318 template<typename SuperClass, unsigned Opc>
00319 class ConcreteOperator : public SuperClass {
00320 public:
00321   static inline bool classof(const Instruction *I) {
00322     return I->getOpcode() == Opc;
00323   }
00324   static inline bool classof(const ConstantExpr *CE) {
00325     return CE->getOpcode() == Opc;
00326   }
00327   static inline bool classof(const Value *V) {
00328     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00329            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00330   }
00331 };
00332 
00333 class AddOperator
00334   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Add> {
00335 };
00336 class SubOperator
00337   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Sub> {
00338 };
00339 class MulOperator
00340   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Mul> {
00341 };
00342 class ShlOperator
00343   : public ConcreteOperator<OverflowingBinaryOperator, Instruction::Shl> {
00344 };
00345 
00346 
00347 class SDivOperator
00348   : public ConcreteOperator<PossiblyExactOperator, Instruction::SDiv> {
00349 };
00350 class UDivOperator
00351   : public ConcreteOperator<PossiblyExactOperator, Instruction::UDiv> {
00352 };
00353 class AShrOperator
00354   : public ConcreteOperator<PossiblyExactOperator, Instruction::AShr> {
00355 };
00356 class LShrOperator
00357   : public ConcreteOperator<PossiblyExactOperator, Instruction::LShr> {
00358 };
00359 
00360 
00361 class ZExtOperator : public ConcreteOperator<Operator, Instruction::ZExt> {};
00362 
00363 
00364 class GEPOperator
00365   : public ConcreteOperator<Operator, Instruction::GetElementPtr> {
00366   enum {
00367     IsInBounds = (1 << 0)
00368   };
00369 
00370   friend class GetElementPtrInst;
00371   friend class ConstantExpr;
00372   void setIsInBounds(bool B) {
00373     SubclassOptionalData =
00374       (SubclassOptionalData & ~IsInBounds) | (B * IsInBounds);
00375   }
00376 
00377 public:
00378   /// Test whether this is an inbounds GEP, as defined by LangRef.html.
00379   bool isInBounds() const {
00380     return SubclassOptionalData & IsInBounds;
00381   }
00382 
00383   inline op_iterator       idx_begin()       { return op_begin()+1; }
00384   inline const_op_iterator idx_begin() const { return op_begin()+1; }
00385   inline op_iterator       idx_end()         { return op_end(); }
00386   inline const_op_iterator idx_end()   const { return op_end(); }
00387 
00388   Value *getPointerOperand() {
00389     return getOperand(0);
00390   }
00391   const Value *getPointerOperand() const {
00392     return getOperand(0);
00393   }
00394   static unsigned getPointerOperandIndex() {
00395     return 0U;                      // get index for modifying correct operand
00396   }
00397 
00398   /// Method to return the pointer operand as a PointerType.
00399   Type *getPointerOperandType() const {
00400     return getPointerOperand()->getType();
00401   }
00402 
00403   Type *getSourceElementType() const;
00404   Type *getResultElementType() const;
00405 
00406   /// Method to return the address space of the pointer operand.
00407   unsigned getPointerAddressSpace() const {
00408     return getPointerOperandType()->getPointerAddressSpace();
00409   }
00410 
00411   unsigned getNumIndices() const {  // Note: always non-negative
00412     return getNumOperands() - 1;
00413   }
00414 
00415   bool hasIndices() const {
00416     return getNumOperands() > 1;
00417   }
00418 
00419   /// Return true if all of the indices of this GEP are zeros.
00420   /// If so, the result pointer and the first operand have the same
00421   /// value, just potentially different types.
00422   bool hasAllZeroIndices() const {
00423     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
00424       if (ConstantInt *C = dyn_cast<ConstantInt>(I))
00425         if (C->isZero())
00426           continue;
00427       return false;
00428     }
00429     return true;
00430   }
00431 
00432   /// Return true if all of the indices of this GEP are constant integers.
00433   /// If so, the result pointer and the first operand have
00434   /// a constant offset between them.
00435   bool hasAllConstantIndices() const {
00436     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
00437       if (!isa<ConstantInt>(I))
00438         return false;
00439     }
00440     return true;
00441   }
00442 
00443   /// \brief Accumulate the constant address offset of this GEP if possible.
00444   ///
00445   /// This routine accepts an APInt into which it will accumulate the constant
00446   /// offset of this GEP if the GEP is in fact constant. If the GEP is not
00447   /// all-constant, it returns false and the value of the offset APInt is
00448   /// undefined (it is *not* preserved!). The APInt passed into this routine
00449   /// must be at exactly as wide as the IntPtr type for the address space of the
00450   /// base GEP pointer.
00451   bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
00452 };
00453 
00454 class PtrToIntOperator
00455     : public ConcreteOperator<Operator, Instruction::PtrToInt> {
00456   friend class PtrToInt;
00457   friend class ConstantExpr;
00458 
00459 public:
00460   Value *getPointerOperand() {
00461     return getOperand(0);
00462   }
00463   const Value *getPointerOperand() const {
00464     return getOperand(0);
00465   }
00466   static unsigned getPointerOperandIndex() {
00467     return 0U;                      // get index for modifying correct operand
00468   }
00469 
00470   /// Method to return the pointer operand as a PointerType.
00471   Type *getPointerOperandType() const {
00472     return getPointerOperand()->getType();
00473   }
00474 
00475   /// Method to return the address space of the pointer operand.
00476   unsigned getPointerAddressSpace() const {
00477     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
00478   }
00479 };
00480 
00481 class BitCastOperator
00482     : public ConcreteOperator<Operator, Instruction::BitCast> {
00483   friend class BitCastInst;
00484   friend class ConstantExpr;
00485 
00486 public:
00487   Type *getSrcTy() const {
00488     return getOperand(0)->getType();
00489   }
00490 
00491   Type *getDestTy() const {
00492     return getType();
00493   }
00494 };
00495 
00496 } // End llvm namespace
00497 
00498 #endif