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, unsigned AddrSpace,
00037                                            ItTy It) {
00038       generic_gep_type_iterator I;
00039       I.CurTy.setPointer(Ty);
00040       I.CurTy.setInt(true);
00041       I.AddrSpace = AddrSpace;
00042       I.OpIt = It;
00043       return I;
00044     }
00045     static generic_gep_type_iterator end(ItTy It) {
00046       generic_gep_type_iterator I;
00047       I.OpIt = It;
00048       return I;
00049     }
00050 
00051     bool operator==(const generic_gep_type_iterator& x) const {
00052       return OpIt == x.OpIt;
00053     }
00054     bool operator!=(const generic_gep_type_iterator& x) const {
00055       return !operator==(x);
00056     }
00057 
00058     Type *operator*() const {
00059       if (CurTy.getInt())
00060         return CurTy.getPointer()->getPointerTo(AddrSpace);
00061       return CurTy.getPointer();
00062     }
00063 
00064     Type *getIndexedType() const {
00065       if (CurTy.getInt())
00066         return CurTy.getPointer();
00067       CompositeType *CT = cast<CompositeType>(CurTy.getPointer());
00068       return CT->getTypeAtIndex(getOperand());
00069     }
00070 
00071     // This is a non-standard operator->.  It allows you to call methods on the
00072     // current type directly.
00073     Type *operator->() const { return operator*(); }
00074 
00075     Value *getOperand() const { return const_cast<Value *>(&**OpIt); }
00076 
00077     generic_gep_type_iterator& operator++() {   // Preincrement
00078       if (CurTy.getInt()) {
00079         CurTy.setInt(false);
00080       } else if (CompositeType *CT =
00081                      dyn_cast<CompositeType>(CurTy.getPointer())) {
00082         CurTy.setPointer(CT->getTypeAtIndex(getOperand()));
00083       } else {
00084         CurTy.setPointer(nullptr);
00085       }
00086       ++OpIt;
00087       return *this;
00088     }
00089 
00090     generic_gep_type_iterator operator++(int) { // Postincrement
00091       generic_gep_type_iterator tmp = *this; ++*this; return tmp;
00092     }
00093   };
00094 
00095   typedef generic_gep_type_iterator<> gep_type_iterator;
00096 
00097   inline gep_type_iterator gep_type_begin(const User *GEP) {
00098     auto *GEPOp = cast<GEPOperator>(GEP);
00099     return gep_type_iterator::begin(
00100         GEPOp->getSourceElementType(),
00101         cast<PointerType>(GEPOp->getPointerOperandType()->getScalarType())
00102             ->getAddressSpace(),
00103         GEP->op_begin() + 1);
00104   }
00105   inline gep_type_iterator gep_type_end(const User *GEP) {
00106     return gep_type_iterator::end(GEP->op_end());
00107   }
00108   inline gep_type_iterator gep_type_begin(const User &GEP) {
00109     auto &GEPOp = cast<GEPOperator>(GEP);
00110     return gep_type_iterator::begin(
00111         GEPOp.getSourceElementType(),
00112         cast<PointerType>(GEPOp.getPointerOperandType()->getScalarType())
00113             ->getAddressSpace(),
00114         GEP.op_begin() + 1);
00115   }
00116   inline gep_type_iterator gep_type_end(const User &GEP) {
00117     return gep_type_iterator::end(GEP.op_end());
00118   }
00119 
00120   template<typename T>
00121   inline generic_gep_type_iterator<const T *>
00122   gep_type_begin(Type *Op0, unsigned AS, ArrayRef<T> A) {
00123     return generic_gep_type_iterator<const T *>::begin(Op0, AS, A.begin());
00124   }
00125 
00126   template<typename T>
00127   inline generic_gep_type_iterator<const T *>
00128   gep_type_end(Type * /*Op0*/, unsigned /*AS*/, ArrayRef<T> A) {
00129     return generic_gep_type_iterator<const T *>::end(A.end());
00130   }
00131 } // end namespace llvm
00132 
00133 #endif