synchronizeICache removeed from TargetJITInfo.
[oota-llvm.git] / lib / Transforms / TransformInternals.cpp
1 //===- TransformInternals.cpp - Implement shared functions for transforms -===//
2 //
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 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines shared functions used by the different components of the
11 //  Transforms library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "TransformInternals.h"
16 #include "llvm/Type.h"
17 #include "llvm/Function.h"
18 #include "llvm/Instructions.h"
19 using namespace llvm;
20
21 static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
22                                        std::vector<Value*> &Indices,
23                                        const TargetData &TD) {
24   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;
32
33   assert(Offset >= SL->MemberOffsets[i] &&
34          (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
35
36   // Make sure to save the current index...
37   Indices.push_back(ConstantUInt::get(Type::UIntTy, i));
38   Offset = SL->MemberOffsets[i];
39   return STy->getContainedType(i);
40 }
41
42
43 // 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 //
54 const Type *llvm::getStructOffsetType(const Type *Ty, unsigned &Offset,
55                                       std::vector<Value*> &Indices,
56                                       const TargetData &TD, bool StopEarly) {
57   if (Offset == 0 && StopEarly && !Indices.empty())
58     return Ty;    // Return the leaf type
59
60   uint64_t ThisOffset;
61   const Type *NextType;
62   if (const StructType *STy = dyn_cast<StructType>(Ty)) {
63     if (STy->getNumElements()) {
64       Offset = 0;
65       return STy;
66     }
67
68     ThisOffset = Offset;
69     NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
70   } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
71     assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
72            "Offset not in composite!");
73
74     NextType = ATy->getElementType();
75     unsigned ChildSize = (unsigned)TD.getTypeSize(NextType);
76     if (ConstantSInt::isValueValidForType(Type::IntTy, Offset/ChildSize))
77       Indices.push_back(ConstantSInt::get(Type::IntTy, Offset/ChildSize));
78     else
79       Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize));
80     ThisOffset = (Offset/ChildSize)*ChildSize;
81   } else {
82     Offset = 0;   // Return the offset that we were able to achieve
83     return Ty;    // Return the leaf type
84   }
85
86   unsigned SubOffs = unsigned(Offset - ThisOffset);
87   const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
88                                            Indices, TD, StopEarly);
89   Offset = unsigned(ThisOffset + SubOffs);
90   return LeafTy;
91 }