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_OPERATOR_H
00016 #define LLVM_OPERATOR_H
00017 
00018 #include "llvm/Instruction.h"
00019 #include "llvm/Constants.h"
00020 
00021 namespace llvm {
00022 
00023 class GetElementPtrInst;
00024 
00025 /// Operator - This is a utility class that provides an abstraction for the
00026 /// common functionality between Instructions and ConstantExprs.
00027 ///
00028 class Operator : public User {
00029 private:
00030   // Do not implement any of these. The Operator class is intended to be used
00031   // as a utility, and is never itself instantiated.
00032   void *operator new(size_t, unsigned);
00033   void *operator new(size_t s);
00034   Operator();
00035   ~Operator();
00036 
00037 public:
00038   /// getOpcode - Return the opcode for this Instruction or ConstantExpr.
00039   ///
00040   unsigned getOpcode() const {
00041     if (const Instruction *I = dyn_cast<Instruction>(this))
00042       return I->getOpcode();
00043     return cast<ConstantExpr>(this)->getOpcode();
00044   }
00045 
00046   /// getOpcode - If V is an Instruction or ConstantExpr, return its
00047   /// opcode. Otherwise return UserOp1.
00048   ///
00049   static unsigned getOpcode(const Value *V) {
00050     if (const Instruction *I = dyn_cast<Instruction>(V))
00051       return I->getOpcode();
00052     if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
00053       return CE->getOpcode();
00054     return Instruction::UserOp1;
00055   }
00056 
00057   static inline bool classof(const Operator *) { return true; }
00058   static inline bool classof(const Instruction *I) { return true; }
00059   static inline bool classof(const ConstantExpr *I) { return true; }
00060   static inline bool classof(const Value *V) {
00061     return isa<Instruction>(V) || isa<ConstantExpr>(V);
00062   }
00063 };
00064 
00065 /// OverflowingBinaryOperator - Utility class for integer arithmetic operators
00066 /// which may exhibit overflow - Add, Sub, and Mul. It does not include SDiv,
00067 /// despite that operator having the potential for overflow.
00068 ///
00069 class OverflowingBinaryOperator : public Operator {
00070   ~OverflowingBinaryOperator(); // do not implement
00071 public:
00072   /// hasNoUnsignedWrap - Test whether this operation is known to never
00073   /// undergo unsigned overflow, aka the nuw property.
00074   bool hasNoUnsignedWrap() const {
00075     return SubclassOptionalData & (1 << 0);
00076   }
00077   void setHasNoUnsignedWrap(bool B) {
00078     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
00079   }
00080 
00081   /// hasNoSignedWrap - Test whether this operation is known to never
00082   /// undergo signed overflow, aka the nsw property.
00083   bool hasNoSignedWrap() const {
00084     return SubclassOptionalData & (1 << 1);
00085   }
00086   void setHasNoSignedWrap(bool B) {
00087     SubclassOptionalData = (SubclassOptionalData & ~(1 << 1)) | (B << 1);
00088   }
00089 
00090   static inline bool classof(const OverflowingBinaryOperator *) { return true; }
00091   static inline bool classof(const Instruction *I) {
00092     return I->getOpcode() == Instruction::Add ||
00093            I->getOpcode() == Instruction::Sub ||
00094            I->getOpcode() == Instruction::Mul;
00095   }
00096   static inline bool classof(const ConstantExpr *CE) {
00097     return CE->getOpcode() == Instruction::Add ||
00098            CE->getOpcode() == Instruction::Sub ||
00099            CE->getOpcode() == Instruction::Mul;
00100   }
00101   static inline bool classof(const Value *V) {
00102     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00103            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00104   }
00105 };
00106 
00107 /// AddOperator - Utility class for integer addition operators.
00108 ///
00109 class AddOperator : public OverflowingBinaryOperator {
00110   ~AddOperator(); // do not implement
00111 public:
00112   static inline bool classof(const AddOperator *) { return true; }
00113   static inline bool classof(const Instruction *I) {
00114     return I->getOpcode() == Instruction::Add;
00115   }
00116   static inline bool classof(const ConstantExpr *CE) {
00117     return CE->getOpcode() == Instruction::Add;
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 /// SubOperator - Utility class for integer subtraction operators.
00126 ///
00127 class SubOperator : public OverflowingBinaryOperator {
00128   ~SubOperator(); // do not implement
00129 public:
00130   static inline bool classof(const SubOperator *) { return true; }
00131   static inline bool classof(const Instruction *I) {
00132     return I->getOpcode() == Instruction::Sub;
00133   }
00134   static inline bool classof(const ConstantExpr *CE) {
00135     return CE->getOpcode() == Instruction::Sub;
00136   }
00137   static inline bool classof(const Value *V) {
00138     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00139            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00140   }
00141 };
00142 
00143 /// MulOperator - Utility class for integer multiplication operators.
00144 ///
00145 class MulOperator : public OverflowingBinaryOperator {
00146   ~MulOperator(); // do not implement
00147 public:
00148   static inline bool classof(const MulOperator *) { return true; }
00149   static inline bool classof(const Instruction *I) {
00150     return I->getOpcode() == Instruction::Mul;
00151   }
00152   static inline bool classof(const ConstantExpr *CE) {
00153     return CE->getOpcode() == Instruction::Mul;
00154   }
00155   static inline bool classof(const Value *V) {
00156     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00157            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00158   }
00159 };
00160 
00161 /// SDivOperator - An Operator with opcode Instruction::SDiv.
00162 ///
00163 class SDivOperator : public Operator {
00164   ~SDivOperator(); // do not implement
00165 public:
00166   /// isExact - Test whether this division is known to be exact, with
00167   /// zero remainder.
00168   bool isExact() const {
00169     return SubclassOptionalData & (1 << 0);
00170   }
00171   void setIsExact(bool B) {
00172     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
00173   }
00174 
00175   // Methods for support type inquiry through isa, cast, and dyn_cast:
00176   static inline bool classof(const SDivOperator *) { return true; }
00177   static inline bool classof(const ConstantExpr *CE) {
00178     return CE->getOpcode() == Instruction::SDiv;
00179   }
00180   static inline bool classof(const Instruction *I) {
00181     return I->getOpcode() == Instruction::SDiv;
00182   }
00183   static inline bool classof(const Value *V) {
00184     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00185            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00186   }
00187 };
00188 
00189 class GEPOperator : public Operator {
00190   ~GEPOperator(); // do not implement
00191 public:
00192   /// isInBounds - Test whether this is an inbounds GEP, as defined
00193   /// by LangRef.html.
00194   bool isInBounds() const {
00195     return SubclassOptionalData & (1 << 0);
00196   }
00197   void setIsInBounds(bool B) {
00198     SubclassOptionalData = (SubclassOptionalData & ~(1 << 0)) | (B << 0);
00199   }
00200 
00201   inline op_iterator       idx_begin()       { return op_begin()+1; }
00202   inline const_op_iterator idx_begin() const { return op_begin()+1; }
00203   inline op_iterator       idx_end()         { return op_end(); }
00204   inline const_op_iterator idx_end()   const { return op_end(); }
00205 
00206   Value *getPointerOperand() {
00207     return getOperand(0);
00208   }
00209   const Value *getPointerOperand() const {
00210     return getOperand(0);
00211   }
00212   static unsigned getPointerOperandIndex() {
00213     return 0U;                      // get index for modifying correct operand
00214   }
00215 
00216   /// getPointerOperandType - Method to return the pointer operand as a
00217   /// PointerType.
00218   const PointerType *getPointerOperandType() const {
00219     return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
00220   }
00221 
00222   unsigned getNumIndices() const {  // Note: always non-negative
00223     return getNumOperands() - 1;
00224   }
00225 
00226   bool hasIndices() const {
00227     return getNumOperands() > 1;
00228   }
00229 
00230   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
00231   /// zeros.  If so, the result pointer and the first operand have the same
00232   /// value, just potentially different types.
00233   bool hasAllZeroIndices() const {
00234     for (const_op_iterator I = idx_begin(), E = idx_end(); I != E; ++I) {
00235       if (Constant *C = dyn_cast<Constant>(I))
00236         if (C->isNullValue())
00237           continue;
00238       return false;
00239     }
00240     return true;
00241   }
00242 
00243   // Methods for support type inquiry through isa, cast, and dyn_cast:
00244   static inline bool classof(const GEPOperator *) { return true; }
00245   static inline bool classof(const GetElementPtrInst *) { return true; }
00246   static inline bool classof(const ConstantExpr *CE) {
00247     return CE->getOpcode() == Instruction::GetElementPtr;
00248   }
00249   static inline bool classof(const Instruction *I) {
00250     return I->getOpcode() == Instruction::GetElementPtr;
00251   }
00252   static inline bool classof(const Value *V) {
00253     return (isa<Instruction>(V) && classof(cast<Instruction>(V))) ||
00254            (isa<ConstantExpr>(V) && classof(cast<ConstantExpr>(V)));
00255   }
00256 };
00257 
00258 } // End llvm namespace
00259 
00260 #endif