LLVM  3.7.0
GetElementPtrTypeIterator.h
Go to the documentation of this file.
1 //===- GetElementPtrTypeIterator.h ------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements an iterator for walking through the types indexed by
11 // getelementptr instructions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_GETELEMENTPTRTYPEITERATOR_H
16 #define LLVM_IR_GETELEMENTPTRTYPEITERATOR_H
17 
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Operator.h"
20 #include "llvm/IR/User.h"
22 
23 namespace llvm {
24  template<typename ItTy = User::const_op_iterator>
26  : public std::iterator<std::forward_iterator_tag, Type *, ptrdiff_t> {
27  typedef std::iterator<std::forward_iterator_tag,
28  Type *, ptrdiff_t> super;
29 
30  ItTy OpIt;
32  unsigned AddrSpace;
34  public:
35 
36  static generic_gep_type_iterator begin(Type *Ty, ItTy It) {
38  I.CurTy.setPointer(Ty);
39  I.OpIt = It;
40  return I;
41  }
42  static generic_gep_type_iterator begin(Type *Ty, unsigned AddrSpace,
43  ItTy It) {
45  I.CurTy.setPointer(Ty);
46  I.CurTy.setInt(true);
47  I.AddrSpace = AddrSpace;
48  I.OpIt = It;
49  return I;
50  }
51  static generic_gep_type_iterator end(ItTy It) {
53  I.OpIt = It;
54  return I;
55  }
56 
57  bool operator==(const generic_gep_type_iterator& x) const {
58  return OpIt == x.OpIt;
59  }
60  bool operator!=(const generic_gep_type_iterator& x) const {
61  return !operator==(x);
62  }
63 
64  Type *operator*() const {
65  if (CurTy.getInt())
66  return CurTy.getPointer()->getPointerTo(AddrSpace);
67  return CurTy.getPointer();
68  }
69 
70  Type *getIndexedType() const {
71  if (CurTy.getInt())
72  return CurTy.getPointer();
73  CompositeType *CT = cast<CompositeType>(CurTy.getPointer());
74  return CT->getTypeAtIndex(getOperand());
75  }
76 
77  // This is a non-standard operator->. It allows you to call methods on the
78  // current type directly.
79  Type *operator->() const { return operator*(); }
80 
81  Value *getOperand() const { return *OpIt; }
82 
84  if (CurTy.getInt()) {
85  CurTy.setInt(false);
86  } else if (CompositeType *CT =
87  dyn_cast<CompositeType>(CurTy.getPointer())) {
88  CurTy.setPointer(CT->getTypeAtIndex(getOperand()));
89  } else {
90  CurTy.setPointer(nullptr);
91  }
92  ++OpIt;
93  return *this;
94  }
95 
96  generic_gep_type_iterator operator++(int) { // Postincrement
97  generic_gep_type_iterator tmp = *this; ++*this; return tmp;
98  }
99  };
100 
102 
104  auto *GEPOp = cast<GEPOperator>(GEP);
106  GEPOp->getSourceElementType(),
107  cast<PointerType>(GEPOp->getPointerOperandType()->getScalarType())
108  ->getAddressSpace(),
109  GEP->op_begin() + 1);
110  }
112  return gep_type_iterator::end(GEP->op_end());
113  }
115  auto &GEPOp = cast<GEPOperator>(GEP);
117  GEPOp.getSourceElementType(),
118  cast<PointerType>(GEPOp.getPointerOperandType()->getScalarType())
119  ->getAddressSpace(),
120  GEP.op_begin() + 1);
121  }
123  return gep_type_iterator::end(GEP.op_end());
124  }
125 
126  template<typename T>
127  inline generic_gep_type_iterator<const T *>
130  }
131 
132  template<typename T>
133  inline generic_gep_type_iterator<const T *>
136  }
137 } // end namespace llvm
138 
139 #endif
generic_gep_type_iterator gep_type_iterator
iterator end() const
Definition: ArrayRef.h:123
gep_type_iterator gep_type_end(const User *GEP)
Hexagon Common GEP
op_iterator op_begin()
Definition: User.h:183
bool operator==(const generic_gep_type_iterator &x) const
generic_gep_type_iterator & operator++()
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
static generic_gep_type_iterator end(ItTy It)
PointerIntPair - This class implements a pair of a pointer and small integer.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
op_iterator op_end()
Definition: User.h:185
Type * getTypeAtIndex(const Value *V)
getTypeAtIndex - Given an index value into the type, return the type of the element.
Definition: Type.cpp:634
iterator begin() const
Definition: ArrayRef.h:122
static generic_gep_type_iterator begin(Type *Ty, ItTy It)
static generic_gep_type_iterator begin(Type *Ty, unsigned AddrSpace, ItTy It)
CompositeType - Common super class of ArrayType, StructType, PointerType and VectorType.
Definition: DerivedTypes.h:148
generic_gep_type_iterator operator++(int)
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
bool operator!=(const generic_gep_type_iterator &x) const
gep_type_iterator gep_type_begin(const User *GEP)