LLVM  mainline
GetElementPtrTypeIterator.h
Go to the documentation of this file.
00001 //===- GetElementPtrTypeIterator.h ------------------------------*- 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 implements an iterator for walking through the types indexed by
00011 // getelementptr instructions.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_IR_GETELEMENTPTRTYPEITERATOR_H
00016 #define LLVM_IR_GETELEMENTPTRTYPEITERATOR_H
00017 
00018 #include "llvm/IR/DerivedTypes.h"
00019 #include "llvm/IR/Operator.h"
00020 #include "llvm/IR/User.h"
00021 #include "llvm/ADT/PointerIntPair.h"
00022 
00023 namespace llvm {
00024   template<typename ItTy = User::const_op_iterator>
00025   class generic_gep_type_iterator
00026     : public std::iterator<std::forward_iterator_tag, Type *, ptrdiff_t> {
00027     typedef std::iterator<std::forward_iterator_tag,
00028                           Type *, ptrdiff_t> super;
00029 
00030     ItTy OpIt;
00031     PointerIntPair<Type *, 1> CurTy;
00032     unsigned AddrSpace;
00033     generic_gep_type_iterator() {}
00034   public:
00035 
00036     static generic_gep_type_iterator begin(Type *Ty, ItTy It) {
00037       generic_gep_type_iterator I;
00038       I.CurTy.setPointer(Ty);
00039       I.OpIt = It;
00040       return I;
00041     }
00042     static generic_gep_type_iterator begin(Type *Ty, unsigned AddrSpace,
00043                                            ItTy It) {
00044       generic_gep_type_iterator I;
00045       I.CurTy.setPointer(Ty);
00046       I.CurTy.setInt(true);
00047       I.AddrSpace = AddrSpace;
00048       I.OpIt = It;
00049       return I;
00050     }
00051     static generic_gep_type_iterator end(ItTy It) {
00052       generic_gep_type_iterator I;
00053       I.OpIt = It;
00054       return I;
00055     }
00056 
00057     bool operator==(const generic_gep_type_iterator& x) const {
00058       return OpIt == x.OpIt;
00059     }
00060     bool operator!=(const generic_gep_type_iterator& x) const {
00061       return !operator==(x);
00062     }
00063 
00064     Type *operator*() const {
00065       if (CurTy.getInt())
00066         return CurTy.getPointer()->getPointerTo(AddrSpace);
00067       return CurTy.getPointer();
00068     }
00069 
00070     Type *getIndexedType() const {
00071       if (CurTy.getInt())
00072         return CurTy.getPointer();
00073       CompositeType *CT = cast<CompositeType>(CurTy.getPointer());
00074       return CT->getTypeAtIndex(getOperand());
00075     }
00076 
00077     // This is a non-standard operator->.  It allows you to call methods on the
00078     // current type directly.
00079     Type *operator->() const { return operator*(); }
00080 
00081     Value *getOperand() const { return *OpIt; }
00082 
00083     generic_gep_type_iterator& operator++() {   // Preincrement
00084       if (CurTy.getInt()) {
00085         CurTy.setInt(false);
00086       } else if (CompositeType *CT =
00087                      dyn_cast<CompositeType>(CurTy.getPointer())) {
00088         CurTy.setPointer(CT->getTypeAtIndex(getOperand()));
00089       } else {
00090         CurTy.setPointer(nullptr);
00091       }
00092       ++OpIt;
00093       return *this;
00094     }
00095 
00096     generic_gep_type_iterator operator++(int) { // Postincrement
00097       generic_gep_type_iterator tmp = *this; ++*this; return tmp;
00098     }
00099   };
00100 
00101   typedef generic_gep_type_iterator<> gep_type_iterator;
00102 
00103   inline gep_type_iterator gep_type_begin(const User *GEP) {
00104     auto *GEPOp = cast<GEPOperator>(GEP);
00105     return gep_type_iterator::begin(
00106         GEPOp->getSourceElementType(),
00107         cast<PointerType>(GEPOp->getPointerOperandType()->getScalarType())
00108             ->getAddressSpace(),
00109         GEP->op_begin() + 1);
00110   }
00111   inline gep_type_iterator gep_type_end(const User *GEP) {
00112     return gep_type_iterator::end(GEP->op_end());
00113   }
00114   inline gep_type_iterator gep_type_begin(const User &GEP) {
00115     auto &GEPOp = cast<GEPOperator>(GEP);
00116     return gep_type_iterator::begin(
00117         GEPOp.getSourceElementType(),
00118         cast<PointerType>(GEPOp.getPointerOperandType()->getScalarType())
00119             ->getAddressSpace(),
00120         GEP.op_begin() + 1);
00121   }
00122   inline gep_type_iterator gep_type_end(const User &GEP) {
00123     return gep_type_iterator::end(GEP.op_end());
00124   }
00125 
00126   template<typename T>
00127   inline generic_gep_type_iterator<const T *>
00128   gep_type_begin(Type *Op0, ArrayRef<T> A) {
00129     return generic_gep_type_iterator<const T *>::begin(Op0, A.begin());
00130   }
00131 
00132   template<typename T>
00133   inline generic_gep_type_iterator<const T *>
00134   gep_type_end(Type * /*Op0*/, ArrayRef<T> A) {
00135     return generic_gep_type_iterator<const T *>::end(A.end());
00136   }
00137 } // end namespace llvm
00138 
00139 #endif