blob: 3f7d8dbbfa20f9a70c045e77520eb092ac10e520 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===- TransformInternals.cpp - Implement shared functions for transforms -===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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 Lattner89a1c802001-11-26 16:59:47 +000017#include "llvm/Analysis/Expressions.h"
Chris Lattner0ac54292002-04-09 19:08:28 +000018#include "llvm/Function.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/Instructions.h"
Chris Lattner559d5192004-01-09 05:53:38 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner8e2e5f72002-09-16 18:32:33 +000022static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
Chris Lattner16125fb2003-04-24 18:25:27 +000023 std::vector<Value*> &Indices,
24 const TargetData &TD) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000025 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
26 const StructLayout *SL = TD.getStructLayout(STy);
27
28 // This loop terminates always on a 0 <= i < MemberOffsets.size()
29 unsigned i;
30 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
31 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
32 break;
33
34 assert(Offset >= SL->MemberOffsets[i] &&
35 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
36
37 // Make sure to save the current index...
Chris Lattner28977af2004-04-05 01:30:19 +000038 Indices.push_back(ConstantUInt::get(Type::UIntTy, i));
Chris Lattner9e77f7e2002-03-21 06:27:20 +000039 Offset = SL->MemberOffsets[i];
40 return STy->getContainedType(i);
41}
42
43
Chris Lattnerc0b90e72001-11-08 20:19:56 +000044// 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.
49//
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
54//
Chris Lattner559d5192004-01-09 05:53:38 +000055const Type *llvm::getStructOffsetType(const Type *Ty, unsigned &Offset,
56 std::vector<Value*> &Indices,
57 const TargetData &TD, bool StopEarly) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000058 if (Offset == 0 && StopEarly && !Indices.empty())
Chris Lattnerc0b90e72001-11-08 20:19:56 +000059 return Ty; // Return the leaf type
Chris Lattnerc0b90e72001-11-08 20:19:56 +000060
Chris Lattner8e2e5f72002-09-16 18:32:33 +000061 uint64_t ThisOffset;
Chris Lattner89a1c802001-11-26 16:59:47 +000062 const Type *NextType;
63 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattnerd21cd802004-02-09 04:37:31 +000064 if (STy->getNumElements()) {
Chris Lattner94931012003-10-17 18:03:54 +000065 Offset = 0;
66 return STy;
67 }
68
Chris Lattner9e77f7e2002-03-21 06:27:20 +000069 ThisOffset = Offset;
Chris Lattner16125fb2003-04-24 18:25:27 +000070 NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
Chris Lattner7991c282001-12-14 16:38:59 +000071 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
Chris Lattnere32487e2003-06-07 21:45:42 +000072 assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
73 "Offset not in composite!");
Chris Lattnerc0b90e72001-11-08 20:19:56 +000074
Chris Lattner89a1c802001-11-26 16:59:47 +000075 NextType = ATy->getElementType();
76 unsigned ChildSize = TD.getTypeSize(NextType);
Chris Lattner28977af2004-04-05 01:30:19 +000077 if (ConstantSInt::isValueValidForType(Type::IntTy, Offset/ChildSize))
78 Indices.push_back(ConstantSInt::get(Type::IntTy, Offset/ChildSize));
79 else
80 Indices.push_back(ConstantSInt::get(Type::LongTy, 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 Lattner89a1c802001-11-26 16:59:47 +000087 unsigned SubOffs = 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 Lattner89a1c802001-11-26 16:59:47 +000090 Offset = ThisOffset + SubOffs;
Chris Lattnerc0b90e72001-11-08 20:19:56 +000091 return LeafTy;
92}
Chris Lattner89a1c802001-11-26 16:59:47 +000093
Misha Brukmanf117cc92003-05-20 18:45:36 +000094// ConvertibleToGEP - This function returns true if the specified value V is
Chris Lattner89a1c802001-11-26 16:59:47 +000095// 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
98//
Chris Lattner559d5192004-01-09 05:53:38 +000099const Type *llvm::ConvertibleToGEP(const Type *Ty, Value *OffsetVal,
100 std::vector<Value*> &Indices,
101 const TargetData &TD,
102 BasicBlock::iterator *BI) {
Chris Lattner28977af2004-04-05 01:30:19 +0000103 return 0;
Chris Lattner89a1c802001-11-26 16:59:47 +0000104}
Brian Gaeked0fde302003-11-11 22:41:34 +0000105