LLVM API Documentation

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