2003-10-13 03:32:08 +00:00
|
|
|
//===- TransformInternals.cpp - Implement shared functions for transforms -===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-04 23:24:06 +00:00
|
|
|
//
|
|
|
|
// This file defines shared functions used by the different components of the
|
|
|
|
// Transforms library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "TransformInternals.h"
|
|
|
|
#include "llvm/Type.h"
|
2002-04-09 19:08:28 +00:00
|
|
|
#include "llvm/Function.h"
|
2004-07-29 12:17:34 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2004-01-09 05:53:38 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-09-16 18:32:33 +00:00
|
|
|
static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
|
2003-04-24 18:25:27 +00:00
|
|
|
std::vector<Value*> &Indices,
|
|
|
|
const TargetData &TD) {
|
2002-03-21 06:27:20 +00:00
|
|
|
assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
|
|
|
|
const StructLayout *SL = TD.getStructLayout(STy);
|
|
|
|
|
|
|
|
// This loop terminates always on a 0 <= i < MemberOffsets.size()
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
|
|
|
|
if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
|
|
|
|
break;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-03-21 06:27:20 +00:00
|
|
|
assert(Offset >= SL->MemberOffsets[i] &&
|
|
|
|
(i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2002-03-21 06:27:20 +00:00
|
|
|
// Make sure to save the current index...
|
2004-04-05 01:30:19 +00:00
|
|
|
Indices.push_back(ConstantUInt::get(Type::UIntTy, i));
|
2002-03-21 06:27:20 +00:00
|
|
|
Offset = SL->MemberOffsets[i];
|
|
|
|
return STy->getContainedType(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-08 20:19:56 +00:00
|
|
|
// getStructOffsetType - Return a vector of offsets that are to be used to index
|
|
|
|
// into the specified struct type to get as close as possible to index as we
|
|
|
|
// can. Note that it is possible that we cannot get exactly to Offset, in which
|
|
|
|
// case we update offset to be the offset we actually obtained. The resultant
|
|
|
|
// leaf type is returned.
|
|
|
|
//
|
|
|
|
// If StopEarly is set to true (the default), the first object with the
|
|
|
|
// specified type is returned, even if it is a struct type itself. In this
|
|
|
|
// case, this routine will not drill down to the leaf type. Set StopEarly to
|
|
|
|
// false if you want a leaf
|
|
|
|
//
|
2004-01-09 05:53:38 +00:00
|
|
|
const Type *llvm::getStructOffsetType(const Type *Ty, unsigned &Offset,
|
|
|
|
std::vector<Value*> &Indices,
|
|
|
|
const TargetData &TD, bool StopEarly) {
|
2002-03-21 06:27:20 +00:00
|
|
|
if (Offset == 0 && StopEarly && !Indices.empty())
|
2001-11-08 20:19:56 +00:00
|
|
|
return Ty; // Return the leaf type
|
|
|
|
|
2002-09-16 18:32:33 +00:00
|
|
|
uint64_t ThisOffset;
|
2001-11-26 16:59:47 +00:00
|
|
|
const Type *NextType;
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
|
2004-02-09 04:37:31 +00:00
|
|
|
if (STy->getNumElements()) {
|
2003-10-17 18:03:54 +00:00
|
|
|
Offset = 0;
|
|
|
|
return STy;
|
|
|
|
}
|
|
|
|
|
2002-03-21 06:27:20 +00:00
|
|
|
ThisOffset = Offset;
|
2003-04-24 18:25:27 +00:00
|
|
|
NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
|
2001-12-14 16:38:59 +00:00
|
|
|
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
2003-06-07 21:45:42 +00:00
|
|
|
assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
|
|
|
|
"Offset not in composite!");
|
2001-11-26 16:59:47 +00:00
|
|
|
|
|
|
|
NextType = ATy->getElementType();
|
2005-01-08 19:48:40 +00:00
|
|
|
unsigned ChildSize = (unsigned)TD.getTypeSize(NextType);
|
2004-04-05 01:30:19 +00:00
|
|
|
if (ConstantSInt::isValueValidForType(Type::IntTy, Offset/ChildSize))
|
|
|
|
Indices.push_back(ConstantSInt::get(Type::IntTy, Offset/ChildSize));
|
|
|
|
else
|
|
|
|
Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize));
|
2001-11-26 16:59:47 +00:00
|
|
|
ThisOffset = (Offset/ChildSize)*ChildSize;
|
2001-12-14 16:38:59 +00:00
|
|
|
} else {
|
2003-10-10 17:57:28 +00:00
|
|
|
Offset = 0; // Return the offset that we were able to achieve
|
2001-12-14 16:38:59 +00:00
|
|
|
return Ty; // Return the leaf type
|
2001-11-26 16:59:47 +00:00
|
|
|
}
|
2001-11-08 20:19:56 +00:00
|
|
|
|
2005-01-08 19:48:40 +00:00
|
|
|
unsigned SubOffs = unsigned(Offset - ThisOffset);
|
2002-03-07 21:18:00 +00:00
|
|
|
const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
|
2003-04-24 18:25:27 +00:00
|
|
|
Indices, TD, StopEarly);
|
2005-01-08 19:48:40 +00:00
|
|
|
Offset = unsigned(ThisOffset + SubOffs);
|
2001-11-08 20:19:56 +00:00
|
|
|
return LeafTy;
|
|
|
|
}
|