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/Analysis/Expressions.h"
18 #include "llvm/Function.h"
19 #include "llvm/Instructions.h"
22 static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
23 std::vector<Value*> &Indices,
24 const TargetData &TD) {
25 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
26 const StructLayout *SL = TD.getStructLayout(STy);
28 // This loop terminates always on a 0 <= i < MemberOffsets.size()
30 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
31 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
34 assert(Offset >= SL->MemberOffsets[i] &&
35 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
37 // Make sure to save the current index...
38 Indices.push_back(ConstantUInt::get(Type::UIntTy, i));
39 Offset = SL->MemberOffsets[i];
40 return STy->getContainedType(i);
44 // getStructOffsetType - Return a vector of offsets that are to be used to index
45 // into the specified struct type to get as close as possible to index as we
46 // can. Note that it is possible that we cannot get exactly to Offset, in which
47 // case we update offset to be the offset we actually obtained. The resultant
48 // leaf type is returned.
50 // If StopEarly is set to true (the default), the first object with the
51 // specified type is returned, even if it is a struct type itself. In this
52 // case, this routine will not drill down to the leaf type. Set StopEarly to
53 // false if you want a leaf
55 const Type *llvm::getStructOffsetType(const Type *Ty, unsigned &Offset,
56 std::vector<Value*> &Indices,
57 const TargetData &TD, bool StopEarly) {
58 if (Offset == 0 && StopEarly && !Indices.empty())
59 return Ty; // Return the leaf type
63 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
64 if (STy->getNumElements()) {
70 NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
71 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
72 assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
73 "Offset not in composite!");
75 NextType = ATy->getElementType();
76 unsigned ChildSize = (unsigned)TD.getTypeSize(NextType);
77 if (ConstantSInt::isValueValidForType(Type::IntTy, Offset/ChildSize))
78 Indices.push_back(ConstantSInt::get(Type::IntTy, Offset/ChildSize));
80 Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize));
81 ThisOffset = (Offset/ChildSize)*ChildSize;
83 Offset = 0; // Return the offset that we were able to achieve
84 return Ty; // Return the leaf type
87 unsigned SubOffs = unsigned(Offset - ThisOffset);
88 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
89 Indices, TD, StopEarly);
90 Offset = unsigned(ThisOffset + SubOffs);
94 // ConvertibleToGEP - This function returns true if the specified value V is
95 // a valid index into a pointer of type Ty. If it is valid, Idx is filled in
96 // with the values that would be appropriate to make this a getelementptr
97 // instruction. The type returned is the root type that the GEP would point to
99 const Type *llvm::ConvertibleToGEP(const Type *Ty, Value *OffsetVal,
100 std::vector<Value*> &Indices,
101 const TargetData &TD,
102 BasicBlock::iterator *BI) {