Now that dominator tree children are built in determinstic order, this horrible code
[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/Analysis/Expressions.h"
18 #include "llvm/Function.h"
19 #include "llvm/iOther.h"
20 using namespace llvm;
21
22 static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
23                                        std::vector<Value*> &Indices,
24                                        const TargetData &TD) {
25   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...
38   Indices.push_back(ConstantUInt::get(Type::UIntTy, i));
39   Offset = SL->MemberOffsets[i];
40   return STy->getContainedType(i);
41 }
42
43
44 // 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 //
55 const Type *llvm::getStructOffsetType(const Type *Ty, unsigned &Offset,
56                                       std::vector<Value*> &Indices,
57                                       const TargetData &TD, bool StopEarly) {
58   if (Offset == 0 && StopEarly && !Indices.empty())
59     return Ty;    // Return the leaf type
60
61   uint64_t ThisOffset;
62   const Type *NextType;
63   if (const StructType *STy = dyn_cast<StructType>(Ty)) {
64     if (STy->getNumElements()) {
65       Offset = 0;
66       return STy;
67     }
68
69     ThisOffset = Offset;
70     NextType = getStructOffsetStep(STy, ThisOffset, Indices, TD);
71   } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
72     assert(Offset == 0 || Offset < TD.getTypeSize(ATy) &&
73            "Offset not in composite!");
74
75     NextType = ATy->getElementType();
76     unsigned ChildSize = TD.getTypeSize(NextType);
77     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));
81     ThisOffset = (Offset/ChildSize)*ChildSize;
82   } else {
83     Offset = 0;   // Return the offset that we were able to achieve
84     return Ty;    // Return the leaf type
85   }
86
87   unsigned SubOffs = Offset - ThisOffset;
88   const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
89                                            Indices, TD, StopEarly);
90   Offset = ThisOffset + SubOffs;
91   return LeafTy;
92 }
93
94 // ConvertibleToGEP - This function returns true if the specified value V is
95 // 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 //
99 const Type *llvm::ConvertibleToGEP(const Type *Ty, Value *OffsetVal,
100                                    std::vector<Value*> &Indices,
101                                    const TargetData &TD,
102                                    BasicBlock::iterator *BI) {
103   return 0;
104 }
105