blob: a4b3609942129a5718d0fb7a1b3ba988c0ce70b2 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- TransformInternals.cpp - Implement shared functions for transforms -===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner59cd9f12001-11-04 23:24:06 +00009//
10// This file defines shared functions used by the different components of the
11// Transforms library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "TransformInternals.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000016#include "llvm/Type.h"
Chris Lattner0ac54292002-04-09 19:08:28 +000017#include "llvm/Function.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner559d5192004-01-09 05:53:38 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner8e2e5f72002-09-16 18:32:33 +000021static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
Chris Lattner16125fb2003-04-24 18:25:27 +000022 std::vector<Value*> &Indices,
23 const TargetData &TD) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000024 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
25 const StructLayout *SL = TD.getStructLayout(STy);
26
27 // This loop terminates always on a 0 <= i < MemberOffsets.size()
28 unsigned i;
29 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
30 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
31 break;
Misha Brukmanfd939082005-04-21 23:48:37 +000032
Chris Lattner9e77f7e2002-03-21 06:27:20 +000033 assert(Offset >= SL->MemberOffsets[i] &&
34 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
Misha Brukmanfd939082005-04-21 23:48:37 +000035
Chris Lattner9e77f7e2002-03-21 06:27:20 +000036 // Make sure to save the current index...
Reid Spencerc5b206b2006-12-31 05:48:39 +000037 Indices.push_back(ConstantInt::get(Type::Int32Ty, i));
Chris Lattner9e77f7e2002-03-21 06:27:20 +000038 Offset = SL->MemberOffsets[i];
39 return STy->getContainedType(i);
40}
41
42
Chris Lattnerc0b90e72001-11-08 20:19:56 +000043// 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.
48//
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
53//
Chris Lattner559d5192004-01-09 05:53:38 +000054const Type *llvm::getStructOffsetType(const Type *Ty, unsigned &Offset,
55 std::vector<Value*> &Indices,
56 const TargetData &TD, bool StopEarly) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000057 if (Offset == 0 && StopEarly && !Indices.empty())
Chris Lattnerc0b90e72001-11-08 20:19:56 +000058 return Ty; // Return the leaf type
Chris Lattnerc0b90e72001-11-08 20:19:56 +000059
Chris Lattner8e2e5f72002-09-16 18:32:33 +000060 uint64_t ThisOffset;
Chris Lattner89a1c802001-11-26 16:59:47 +000061 const Type *NextType;
62 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd21cd802004-02-09 04:37:31 +000063 if (STy->getNumElements()) {
Chris Lattner94931012003-10-17 18:03:54 +000064 Offset = 0;
65 return STy;
66 }
67
Chris Lattner9e77f7e2002-03-21 06:27:20 +000068 ThisOffset = Offset;
Chris Lattner16125fb2003-04-24 18:25:27 +000069 NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
Chris Lattner7991c282001-12-14 16:38:59 +000070 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnere32487e2003-06-07 21:45:42 +000071 assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
72 "Offset not in composite!");
Chris Lattnerc0b90e72001-11-08 20:19:56 +000073
Chris Lattner89a1c802001-11-26 16:59:47 +000074 NextType = ATy->getElementType();
Chris Lattner22bc9342005-01-08 19:48:40 +000075 unsigned ChildSize = (unsigned)TD.getTypeSize(NextType);
Reid Spencerc5b206b2006-12-31 05:48:39 +000076 if (ConstantInt::isValueValidForType(Type::Int32Ty,
Reid Spencerb83eb642006-10-20 07:07:24 +000077 uint64_t(Offset/ChildSize)))
Reid Spencerc5b206b2006-12-31 05:48:39 +000078 Indices.push_back(ConstantInt::get(Type::Int32Ty, Offset/ChildSize));
Chris Lattner28977af2004-04-05 01:30:19 +000079 else
Reid Spencerc5b206b2006-12-31 05:48:39 +000080 Indices.push_back(ConstantInt::get(Type::Int64Ty, Offset/ChildSize));
Chris Lattner89a1c802001-11-26 16:59:47 +000081 ThisOffset = (Offset/ChildSize)*ChildSize;
Chris Lattner7991c282001-12-14 16:38:59 +000082 } else {
Misha Brukmancf00c4a2003-10-10 17:57:28 +000083 Offset = 0; // Return the offset that we were able to achieve
Chris Lattner7991c282001-12-14 16:38:59 +000084 return Ty; // Return the leaf type
Chris Lattner89a1c802001-11-26 16:59:47 +000085 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +000086
Chris Lattner22bc9342005-01-08 19:48:40 +000087 unsigned SubOffs = unsigned(Offset - ThisOffset);
Chris Lattner4736d062002-03-07 21:18:00 +000088 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
Chris Lattner16125fb2003-04-24 18:25:27 +000089 Indices, TD, StopEarly);
Chris Lattner22bc9342005-01-08 19:48:40 +000090 Offset = unsigned(ThisOffset + SubOffs);
Chris Lattnerc0b90e72001-11-08 20:19:56 +000091 return LeafTy;
92}