1 //===- TransformInternals.cpp - Implement shared functions for transforms -===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines shared functions used by the different components of the
11 // Transforms library.
13 //===----------------------------------------------------------------------===//
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"
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);
27 // This loop terminates always on a 0 <= i < MemberOffsets.size()
29 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
30 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
33 assert(Offset >= SL->MemberOffsets[i] &&
34 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
36 // Make sure to save the current index...
37 Indices.push_back(ConstantUInt::get(Type::UByteTy, i));
38 Offset = SL->MemberOffsets[i];
39 return STy->getContainedType(i);
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.
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
54 const Type *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
62 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
63 if (STy->getElementTypes().empty()) {
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!");
74 NextType = ATy->getElementType();
75 unsigned ChildSize = TD.getTypeSize(NextType);
76 Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize));
77 ThisOffset = (Offset/ChildSize)*ChildSize;
79 Offset = 0; // Return the offset that we were able to achieve
80 return Ty; // Return the leaf type
83 unsigned SubOffs = Offset - ThisOffset;
84 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
85 Indices, TD, StopEarly);
86 Offset = ThisOffset + SubOffs;
90 // ConvertibleToGEP - This function returns true if the specified value V is
91 // a valid index into a pointer of type Ty. If it is valid, Idx is filled in
92 // with the values that would be appropriate to make this a getelementptr
93 // instruction. The type returned is the root type that the GEP would point to
95 const Type *ConvertibleToGEP(const Type *Ty, Value *OffsetVal,
96 std::vector<Value*> &Indices,
98 BasicBlock::iterator *BI) {
99 const CompositeType *CompTy = dyn_cast<CompositeType>(Ty);
100 if (CompTy == 0) return 0;
102 // See if the cast is of an integer expression that is either a constant,
103 // or a value scaled by some amount with a possible offset.
105 ExprType Expr = ClassifyExpression(OffsetVal);
107 // Get the offset and scale values if they exists...
108 // A scale of zero with Expr.Var != 0 means a scale of 1.
110 int64_t Offset = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
111 int64_t Scale = Expr.Scale ? getConstantValue(Expr.Scale) : 0;
113 if (Expr.Var && Scale == 0) Scale = 1; // Scale != 0 if Expr.Var != 0
115 // Loop over the Scale and Offset values, filling in the Indices vector for
116 // our final getelementptr instruction.
118 const Type *NextTy = CompTy;
120 if (!isa<CompositeType>(NextTy))
121 return 0; // Type must not be ready for processing...
122 CompTy = cast<CompositeType>(NextTy);
124 if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) {
125 // Step into the appropriate element of the structure...
126 uint64_t ActualOffset = (Offset < 0) ? 0 : (uint64_t)Offset;
127 NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices, TD);
128 Offset -= ActualOffset;
130 const Type *ElTy = cast<SequentialType>(CompTy)->getElementType();
131 if (!ElTy->isSized() || (isa<PointerType>(CompTy) && !Indices.empty()))
132 return 0; // Type is unreasonable... escape!
133 unsigned ElSize = TD.getTypeSize(ElTy);
134 if (ElSize == 0) return 0; // Avoid division by zero...
135 int64_t ElSizeS = ElSize;
137 // See if the user is indexing into a different cell of this array...
138 if (Scale && (Scale >= ElSizeS || -Scale >= ElSizeS)) {
139 // A scale n*ElSize might occur if we are not stepping through
140 // array by one. In this case, we will have to insert math to munge
143 int64_t ScaleAmt = Scale/ElSizeS;
144 if (Scale-ScaleAmt*ElSizeS)
145 return 0; // Didn't scale by a multiple of element size, bail out
146 Scale = 0; // Scale is consumed
148 int64_t Index = Offset/ElSize; // is zero unless Offset > ElSize
149 Offset -= Index*ElSize; // Consume part of the offset
151 if (BI) { // Generate code?
152 BasicBlock *BB = (*BI)->getParent();
153 if (Expr.Var->getType() != Type::LongTy)
154 Expr.Var = new CastInst(Expr.Var, Type::LongTy,
155 Expr.Var->getName()+"-idxcast", *BI);
157 if (ScaleAmt && ScaleAmt != 1) {
158 // If we have to scale up our index, do so now
159 Value *ScaleAmtVal = ConstantSInt::get(Type::LongTy, ScaleAmt);
160 Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var,
162 Expr.Var->getName()+"-scale",*BI);
165 if (Index) { // Add an offset to the index
166 Value *IndexAmt = ConstantSInt::get(Type::LongTy, Index);
167 Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var,
169 Expr.Var->getName()+"-offset",
174 Indices.push_back(Expr.Var);
176 } else if (Offset >= (int64_t)ElSize || -Offset >= (int64_t)ElSize) {
177 // Calculate the index that we are entering into the array cell with
178 uint64_t Index = Offset/ElSize;
179 Indices.push_back(ConstantSInt::get(Type::LongTy, Index));
180 Offset -= (int64_t)(Index*ElSize); // Consume part of the offset
182 } else if (isa<ArrayType>(CompTy) || Indices.empty()) {
183 // Must be indexing a small amount into the first cell of the array
184 // Just index into element zero of the array here.
186 Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
188 return 0; // Hrm. wierd, can't handle this case. Bail
192 } while (Offset || Scale); // Go until we're done!