llvm.org GIT mirror llvm / 497b958
Fold a loop for array processing in ComputeLinearIndex When processing an array, every Elt has the same layout, it is useless to recursively call each ComputeLinearIndex on each element. Just do it once and multiply by the number of elements. Differential Revision: http://reviews.llvm.org/D6832 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225949 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 5 years ago
2 changed file(s) with 27 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
3030 class SelectionDAG;
3131 struct EVT;
3232
33 /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence
34 /// of insertvalue or extractvalue indices that identify a member, return
35 /// the linearized index of the start of the member.
33 /// \brief Compute the linearized index of a member in a nested
34 /// aggregate/struct/array.
3635 ///
36 /// Given an LLVM IR aggregate type and a sequence of insertvalue or
37 /// extractvalue indices that identify a member, return the linearized index of
38 /// the start of the member, i.e the number of element in memory before the
39 /// seeked one. This is disconnected from the number of bytes.
40 ///
41 /// \param Ty is the type indexed by \p Indices.
42 /// \param Indices is an optional pointer in the indices list to the current
43 /// index.
44 /// \param IndicesEnd is the end of the indices list.
45 /// \param CurIndex is the current index in the recursion.
46 ///
47 /// \returns \p CurIndex plus the linear index in \p Ty the indices list.
3748 unsigned ComputeLinearIndex(Type *Ty,
3849 const unsigned *Indices,
3950 const unsigned *IndicesEnd,
2929
3030 using namespace llvm;
3131
32 /// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence
33 /// of insertvalue or extractvalue indices that identify a member, return
34 /// the linearized index of the start of the member.
35 ///
32 /// Compute the linearized index of a member in a nested aggregate/struct/array
33 /// by recursing and accumulating CurIndex as long as there are indices in the
34 /// index list.
3635 unsigned llvm::ComputeLinearIndex(Type *Ty,
3736 const unsigned *Indices,
3837 const unsigned *IndicesEnd,
5655 // Given an array type, recursively traverse the elements.
5756 else if (ArrayType *ATy = dyn_cast(Ty)) {
5857 Type *EltTy = ATy->getElementType();
59 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
60 if (Indices && *Indices == i)
61 return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex);
62 CurIndex = ComputeLinearIndex(EltTy, nullptr, nullptr, CurIndex);
58 unsigned NumElts = ATy->getNumElements();
59 // Compute the Linear offset when jumping one element of the array
60 unsigned EltLinearOffset = ComputeLinearIndex(EltTy, nullptr, nullptr, 0);
61 if (Indices && *Indices < NumElts) {
62 // If the indice is inside the array, compute the index to the requested
63 // elt and recurse inside the element with the end of the indices list
64 CurIndex += EltLinearOffset* *Indices;
65 return ComputeLinearIndex(EltTy, Indices+1, IndicesEnd, CurIndex);
6366 }
67 // Out of bound? Assert instead?
68 CurIndex += EltLinearOffset*NumElts;
6469 return CurIndex;
6570 }
6671 // We haven't found the type we're looking for, so keep searching.