1 //===- TransformInternals.cpp - Implement shared functions for transforms -===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines shared functions used by the different components of the
11 // Transforms library.
13 //===----------------------------------------------------------------------===//
15 #include "TransformInternals.h"
16 #include "llvm/Type.h"
17 #include "llvm/Function.h"
18 #include "llvm/Instructions.h"
21 static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
22 std::vector<Value*> &Indices,
23 const TargetData &TD) {
24 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
25 const StructLayout *SL = TD.getStructLayout(STy);
27 // This loop terminates always on a 0 <= i < MemberOffsets.size()
29 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
30 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
33 assert(Offset >= SL->MemberOffsets[i] &&
34 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
36 // Make sure to save the current index...
37 Indices.push_back(ConstantUInt::get(Type::UIntTy, i));
38 Offset = SL->MemberOffsets[i];
39 return STy->getContainedType(i);
43 // getStructOffsetType - Return a vector of offsets that are to be used to index
44 // into the specified struct type to get as close as possible to index as we
45 // can. Note that it is possible that we cannot get exactly to Offset, in which
46 // case we update offset to be the offset we actually obtained. The resultant
47 // leaf type is returned.
49 // If StopEarly is set to true (the default), the first object with the
50 // specified type is returned, even if it is a struct type itself. In this
51 // case, this routine will not drill down to the leaf type. Set StopEarly to
52 // false if you want a leaf
54 const Type *llvm::getStructOffsetType(const Type *Ty, unsigned &Offset,
55 std::vector<Value*> &Indices,
56 const TargetData &TD, bool StopEarly) {
57 if (Offset == 0 && StopEarly && !Indices.empty())
58 return Ty; // Return the leaf type
62 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
63 if (STy->getNumElements()) {
69 NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
70 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
71 assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
72 "Offset not in composite!");
74 NextType = ATy->getElementType();
75 unsigned ChildSize = (unsigned)TD.getTypeSize(NextType);
76 if (ConstantSInt::isValueValidForType(Type::IntTy, Offset/ChildSize))
77 Indices.push_back(ConstantSInt::get(Type::IntTy, Offset/ChildSize));
79 Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize));
80 ThisOffset = (Offset/ChildSize)*ChildSize;
82 Offset = 0; // Return the offset that we were able to achieve
83 return Ty; // Return the leaf type
86 unsigned SubOffs = unsigned(Offset - ThisOffset);
87 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
88 Indices, TD, StopEarly);
89 Offset = unsigned(ThisOffset + SubOffs);