2003-10-13 05:32:08 +02:00
|
|
|
//===- TransformInternals.cpp - Implement shared functions for transforms -===//
|
2005-04-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02: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-22 01:48:37 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-05 00:24:06 +01: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 21:08:28 +02:00
|
|
|
#include "llvm/Function.h"
|
2004-07-29 14:17:34 +02:00
|
|
|
#include "llvm/Instructions.h"
|
2004-01-09 06:53:38 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-09-16 20:32:33 +02:00
|
|
|
static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
|
2003-04-24 20:25:27 +02:00
|
|
|
std::vector<Value*> &Indices,
|
|
|
|
const TargetData &TD) {
|
2002-03-21 07:27:20 +01: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-22 01:48:37 +02:00
|
|
|
|
2002-03-21 07:27:20 +01:00
|
|
|
assert(Offset >= SL->MemberOffsets[i] &&
|
|
|
|
(i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
|
2005-04-22 01:48:37 +02:00
|
|
|
|
2002-03-21 07:27:20 +01:00
|
|
|
// Make sure to save the current index...
|
2006-12-31 06:48:39 +01:00
|
|
|
Indices.push_back(ConstantInt::get(Type::Int32Ty, i));
|
2002-03-21 07:27:20 +01:00
|
|
|
Offset = SL->MemberOffsets[i];
|
|
|
|
return STy->getContainedType(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-08 21:19:56 +01: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 06:53:38 +01:00
|
|
|
const Type *llvm::getStructOffsetType(const Type *Ty, unsigned &Offset,
|
|
|
|
std::vector<Value*> &Indices,
|
|
|
|
const TargetData &TD, bool StopEarly) {
|
2002-03-21 07:27:20 +01:00
|
|
|
if (Offset == 0 && StopEarly && !Indices.empty())
|
2001-11-08 21:19:56 +01:00
|
|
|
return Ty; // Return the leaf type
|
|
|
|
|
2002-09-16 20:32:33 +02:00
|
|
|
uint64_t ThisOffset;
|
2001-11-26 17:59:47 +01:00
|
|
|
const Type *NextType;
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
|
2004-02-09 05:37:31 +01:00
|
|
|
if (STy->getNumElements()) {
|
2003-10-17 20:03:54 +02:00
|
|
|
Offset = 0;
|
|
|
|
return STy;
|
|
|
|
}
|
|
|
|
|
2002-03-21 07:27:20 +01:00
|
|
|
ThisOffset = Offset;
|
2003-04-24 20:25:27 +02:00
|
|
|
NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
|
2001-12-14 17:38:59 +01:00
|
|
|
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
2003-06-07 23:45:42 +02:00
|
|
|
assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
|
|
|
|
"Offset not in composite!");
|
2001-11-26 17:59:47 +01:00
|
|
|
|
|
|
|
NextType = ATy->getElementType();
|
2005-01-08 20:48:40 +01:00
|
|
|
unsigned ChildSize = (unsigned)TD.getTypeSize(NextType);
|
2006-12-31 06:48:39 +01:00
|
|
|
if (ConstantInt::isValueValidForType(Type::Int32Ty,
|
2006-10-20 09:07:24 +02:00
|
|
|
uint64_t(Offset/ChildSize)))
|
2006-12-31 06:48:39 +01:00
|
|
|
Indices.push_back(ConstantInt::get(Type::Int32Ty, Offset/ChildSize));
|
2004-04-05 03:30:19 +02:00
|
|
|
else
|
2006-12-31 06:48:39 +01:00
|
|
|
Indices.push_back(ConstantInt::get(Type::Int64Ty, Offset/ChildSize));
|
2001-11-26 17:59:47 +01:00
|
|
|
ThisOffset = (Offset/ChildSize)*ChildSize;
|
2001-12-14 17:38:59 +01:00
|
|
|
} else {
|
2003-10-10 19:57:28 +02:00
|
|
|
Offset = 0; // Return the offset that we were able to achieve
|
2001-12-14 17:38:59 +01:00
|
|
|
return Ty; // Return the leaf type
|
2001-11-26 17:59:47 +01:00
|
|
|
}
|
2001-11-08 21:19:56 +01:00
|
|
|
|
2005-01-08 20:48:40 +01:00
|
|
|
unsigned SubOffs = unsigned(Offset - ThisOffset);
|
2002-03-07 22:18:00 +01:00
|
|
|
const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
|
2003-04-24 20:25:27 +02:00
|
|
|
Indices, TD, StopEarly);
|
2005-01-08 20:48:40 +01:00
|
|
|
Offset = unsigned(ThisOffset + SubOffs);
|
2001-11-08 21:19:56 +01:00
|
|
|
return LeafTy;
|
|
|
|
}
|