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 /// This is a utility class that provides an abstraction for the common
00032 /// functionality between Instructions and ConstantExprs.
00033 class Operator : public User {
00034 private:
00035   // The Operator class is intended to be used as a utility, and is never itself
00036   // instantiated.
00037   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
00038   void *operator new(size_t s) LLVM_DELETED_FUNCTION;
00039   Operator() LLVM_DELETED_FUNCTION;
00040 
00041 protected:
00042   // NOTE: Cannot use LLVM_DELETED_FUNCTION because it's not legal to delete
00043   // an overridden method that's not deleted in the base class. Cannot leave
00044   // this unimplemented because that leads to an ODR-violation.
00045   ~Operator();
00046 
00047 public:
00048   /// Return the opcode for this Instruction or ConstantExpr.
00049   unsigned getOpcode() const {
00050     if (const Instruction *I = dyn_cast<Instruction>(this))
00051       return I->getOpcode();
00052     return cast<ConstantExpr>(this)->getOpcode();
00053   }
00054 
00055   /// If V is an Instruction or ConstantExpr, return its opcode.
00056   /// Otherwise return UserOp1.
00057   static unsigned getOpcode(const Value *V) {
00058     if (const Instruction *I = dyn_cast<Instruction>(V))
00059       return I->getOpcode();
00060     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00061       return CE->getOpcode();
00062     return Instruction::UserOp1;
00063   }
00064 
00065   static inline bool classof(const Instruction *) { return true; }
00066   static inline bool classof(const ConstantExpr *) { return true; }
00067   static inline bool classof(const Value *V) {
00068     return isa<Instruction>(V) || isa<ConstantExpr>(V);
00069   }
00070 };
00071 
00072 /// Utility class for integer arithmetic operators which may exhibit overflow -
00073 /// Add, Sub, and Mul. It does not include SDiv, despite that operator having
00074 /// the potential for overflow.
00075 class OverflowingBinaryOperator : public Operator {
00076 public:
00077   enum {
00078     NoUnsignedWrap = (1 << 0),
00079     NoSignedWrap   = (1 << 1)
00080   };
00081 
00082 private:
00083   friend class BinaryOperator;
00084   friend class ConstantExpr;
00085   void setHasNoUnsignedWrap(bool B) {
00086     SubclassOptionalData =
00087       (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
00088   }
00089   void setHasNoSignedWrap(bool B) {
00090     SubclassOptionalData =
00091       (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
00092   }
00093 
00094 public:
00095   /// Test whether this operation is known to never
00096   /// undergo unsigned overflow, aka the nuw property.
00097   bool hasNoUnsignedWrap() const {
00098     return SubclassOptionalData & NoUnsignedWrap;
00099   }
00100 
00101   /// Test whether this operation is known to never
00102   /// undergo signed overflow, aka the nsw property.
00103   bool hasNoSignedWrap() const {
00104     return (SubclassOptionalData & NoSignedWrap) != 0;
00105   }
00106 
00107   static inline bool classof(const Instruction *I) {
00108     return I->getOpcode() == Instruction::Add ||
00109            I->getOpcode() == Instruction::Sub ||
00110            I->getOpcode() == Instruction::Mul ||
00111            I->getOpcode() == Instruction::Shl;
00112   }
00113   static inline bool classof(const ConstantExpr *CE) {
00114     return CE->getOpcode() == Instruction::Add ||
00115            CE->getOpcode() == Instruction::Sub ||
00116            CE->getOpcode() == Instruction::Mul ||
00117            CE->getOpcode() == Instruction::Shl;
00118   }
00119   static inline bool classof(const Value *V) {
00120     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00121            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00122   }
00123 };
00124 
00125 /// A udiv or sdiv instruction, which can be marked as "exact",
00126 /// indicating that no bits are destroyed.
00127 class PossiblyExactOperator : public Operator {
00128 public:
00129   enum {
00130     IsExact = (1 << 0)
00131   };
00132 
00133 private:
00134   friend class BinaryOperator;
00135   friend class ConstantExpr;
00136   void setIsExact(bool B) {
00137     SubclassOptionalData = (SubclassOptionalData & ~IsExact) | (B * IsExact);
00138   }
00139 
00140 public:
00141   /// Test whether this division is known to be exact, with zero remainder.
00142   bool isExact() const {
00143     return SubclassOptionalData & IsExact;
00144   }
00145 
00146   static bool isPossiblyExactOpcode(unsigned OpC) {
00147     return OpC == Instruction::SDiv ||
00148            OpC == Instruction::UDiv ||
00149            OpC == Instruction::AShr ||
00150            OpC == Instruction::LShr;
00151   }
00152   static inline bool classof(const ConstantExpr *CE) {
00153     return isPossiblyExactOpcode(CE->getOpcode());
00154   }
00155   static inline bool classof(const Instruction *I) {
00156     return isPossiblyExactOpcode(I->getOpcode());
00157   }
00158   static inline bool classof(const Value *V) {
00159     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00160            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00161   }
00162 };
00163 
00164 /// Convenience struct for specifying and reasoning about fast-math flags.
00165 class FastMathFlags {
00166 private:
00167   friend class FPMathOperator;
00168   unsigned Flags;
00169   FastMathFlags(unsigned F) : Flags(F) { }
00170 
00171 public:
00172   enum {
00173     UnsafeAlgebra   = (1 << 0),
00174     NoNaNs          = (1 << 1),
00175     NoInfs          = (1 << 2),
00176     NoSignedZeros   = (1 << 3),
00177     AllowReciprocal = (1 << 4)
00178   };
00179 
00180   FastMathFlags() : Flags(0)
00181   { }
00182 
00183   /// Whether any flag is set
00184   bool any() { return Flags != 0; }
00185 
00186   /// Set all the flags to false
00187   void clear() { Flags = 0; }
00188 
00189   /// Flag queries
00190   bool noNaNs()          { return 0 != (Flags & NoNaNs); }
00191   bool noInfs()          { return 0 != (Flags & NoInfs); }
00192   bool noSignedZeros()   { return 0 != (Flags & NoSignedZeros); }
00193   bool allowReciprocal() { return 0 != (Flags & AllowReciprocal); }
00194   bool unsafeAlgebra()   { return 0 != (Flags & UnsafeAlgebra); }
00195 
00196   /// Flag setters
00197   void setNoNaNs()          { Flags |= NoNaNs; }
00198   void setNoInfs()          { Flags |= NoInfs; }
00199   void setNoSignedZeros()   { Flags |= NoSignedZeros; }
00200   void setAllowReciprocal() { Flags |= AllowReciprocal; }
00201   void setUnsafeAlgebra() {
00202     Flags |= UnsafeAlgebra;
00203     setNoNaNs();
00204     setNoInfs();
00205     setNoSignedZeros();
00206     setAllowReciprocal();
00207   }
00208 
00209   void operator&=(const FastMathFlags &OtherFlags) {
00210     Flags &= OtherFlags.Flags;
00211   }
00212 };
00213 
00214 
00215 /// Utility class for floating point operations which can have
00216 /// information about relaxed accuracy requirements attached to them.
00217 class FPMathOperator : public Operator {
00218 private:
00219   friend class Instruction;
00220 
00221   void setHasUnsafeAlgebra(bool B) {
00222     SubclassOptionalData =
00223       (SubclassOptionalData & ~FastMathFlags::UnsafeAlgebra) |
00224       (B * FastMathFlags::UnsafeAlgebra);
00225 
00226     // Unsafe algebra implies all the others
00227     if (B) {
00228       setHasNoNaNs(true);
00229       setHasNoInfs(true);
00230       setHasNoSignedZeros(true);
00231       setHasAllowReciprocal(true);
00232     }
00233   }
00234   void setHasNoNaNs(bool B) {
00235     SubclassOptionalData =
00236       (SubclassOptionalData & ~FastMathFlags::NoNaNs) |
00237       (B * FastMathFlags::NoNaNs);
00238   }
00239   void setHasNoInfs(bool B) {
00240     SubclassOptionalData =
00241       (SubclassOptionalData & ~FastMathFlags::NoInfs) |
00242       (B * FastMathFlags::NoInfs);
00243   }
00244   void setHasNoSignedZeros(bool B) {
00245     SubclassOptionalData =
00246       (SubclassOptionalData & ~FastMathFlags::NoSignedZeros) |
00247       (B * FastMathFlags::NoSignedZeros);
00248   }
00249   void setHasAllowReciprocal(bool B) {
00250     SubclassOptionalData =
00251       (SubclassOptionalData & ~FastMathFlags::AllowReciprocal) |
00252       (B * FastMathFlags::AllowReciprocal);
00253   }
00254 
00255   /// Convenience function for setting multiple fast-math flags.
00256   /// FMF is a mask of the bits to set.
00257   void setFastMathFlags(FastMathFlags FMF) {
00258     SubclassOptionalData |= FMF.Flags;
00259   }
00260 
00261   /// Convenience function for copying all fast-math flags.
00262   /// All values in FMF are transferred to this operator.
00263   void copyFastMathFlags(FastMathFlags FMF) {
00264     SubclassOptionalData = FMF.Flags;
00265   }
00266 
00267 public:
00268   /// Test whether this operation is permitted to be
00269   /// algebraically transformed, aka the 'A' fast-math property.
00270   bool hasUnsafeAlgebra() const {
00271     return (SubclassOptionalData & FastMathFlags::UnsafeAlgebra) != 0;
00272   }
00273 
00274   /// Test whether this operation's arguments and results are to be
00275   /// treated as non-NaN, aka the 'N' fast-math property.
00276   bool hasNoNaNs() const {
00277     return (SubclassOptionalData & FastMathFlags::NoNaNs) != 0;
00278   }
00279 
00280   /// Test whether this operation's arguments and results are to be
00281   /// treated as NoN-Inf, aka the 'I' fast-math property.
00282   bool hasNoInfs() const {
00283     return (SubclassOptionalData & FastMathFlags::NoInfs) != 0;
00284   }
00285 
00286   /// Test whether this operation can treat the sign of zero
00287   /// as insignificant, aka the 'S' fast-math property.
00288   bool hasNoSignedZeros() const {
00289     return (SubclassOptionalData & FastMathFlags::NoSignedZeros) != 0;
00290   }
00291 
00292   /// Test whether this operation is permitted to use
00293   /// reciprocal instead of division, aka the 'R' fast-math property.
00294   bool hasAllowReciprocal() const {
00295     return (SubclassOptionalData & FastMathFlags::AllowReciprocal) != 0;
00296   }
00297 
00298   /// Convenience function for getting all the fast-math flags
00299   FastMathFlags getFastMathFlags() const {
00300     return FastMathFlags(SubclassOptionalData);
00301   }
00302 
00303   /// \brief Get the maximum error permitted by this operation in ULPs.  An
00304   /// accuracy of 0.0 means that the operation should be performed with the
00305   /// default precision.
00306   float getFPAccuracy() const;
00307 
00308   static inline bool classof(const Instruction *I) {
00309     return I->getType()->isFPOrFPVectorTy();
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   /// Method to return the address space of the pointer operand.
00404   unsigned getPointerAddressSpace() const {
00405     return getPointerOperandType()->getPointerAddressSpace();
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   /// Return true if all of the indices of this GEP are zeros.
00417   /// 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   /// Return true if all of the indices of this GEP are constant integers.
00430   /// 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   /// Method to return the pointer operand as a PointerType.
00497   Type *getPointerOperandType() const {
00498     return getPointerOperand()->getType();
00499   }
00500 
00501   /// Method to return the address space of the pointer operand.
00502   unsigned getPointerAddressSpace() const {
00503     return cast<PointerType>(getPointerOperandType())->getAddressSpace();
00504   }
00505 };
00506 
00507 
00508 } // End llvm namespace
00509 
00510 #endif