1 //===-- TransformInternals.cpp - Implement shared functions for transforms --=//
3 // This file defines shared functions used by the different components of the
6 //===----------------------------------------------------------------------===//
8 #include "TransformInternals.h"
10 #include "llvm/Analysis/Expressions.h"
11 #include "llvm/Function.h"
12 #include "llvm/iOther.h"
14 // TargetData Hack: Eventually we will have annotations given to us by the
15 // backend so that we know stuff about type size and alignments. For now
16 // though, just use this, because it happens to match the model that GCC uses.
18 const TargetData TD("LevelRaise: Should be GCC though!");
21 static const Type *getStructOffsetStep(const StructType *STy, uint64_t &Offset,
22 std::vector<Value*> &Indices) {
23 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
24 const StructLayout *SL = TD.getStructLayout(STy);
26 // This loop terminates always on a 0 <= i < MemberOffsets.size()
28 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
29 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
32 assert(Offset >= SL->MemberOffsets[i] &&
33 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
35 // Make sure to save the current index...
36 Indices.push_back(ConstantUInt::get(Type::UByteTy, i));
37 Offset = SL->MemberOffsets[i];
38 return STy->getContainedType(i);
42 // getStructOffsetType - Return a vector of offsets that are to be used to index
43 // into the specified struct type to get as close as possible to index as we
44 // can. Note that it is possible that we cannot get exactly to Offset, in which
45 // case we update offset to be the offset we actually obtained. The resultant
46 // leaf type is returned.
48 // If StopEarly is set to true (the default), the first object with the
49 // specified type is returned, even if it is a struct type itself. In this
50 // case, this routine will not drill down to the leaf type. Set StopEarly to
51 // false if you want a leaf
53 const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
54 std::vector<Value*> &Indices,
56 if (Offset == 0 && StopEarly && !Indices.empty())
57 return Ty; // Return the leaf type
61 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
63 NextType = getStructOffsetStep(STy, ThisOffset, Indices);
64 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
65 assert(Offset < TD.getTypeSize(ATy) && "Offset not in composite!");
67 NextType = ATy->getElementType();
68 unsigned ChildSize = TD.getTypeSize(NextType);
69 Indices.push_back(ConstantSInt::get(Type::LongTy, Offset/ChildSize));
70 ThisOffset = (Offset/ChildSize)*ChildSize;
72 Offset = 0; // Return the offset that we were able to acheive
73 return Ty; // Return the leaf type
76 unsigned SubOffs = Offset - ThisOffset;
77 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
79 Offset = ThisOffset + SubOffs;
83 // ConvertableToGEP - This function returns true if the specified value V is
84 // a valid index into a pointer of type Ty. If it is valid, Idx is filled in
85 // with the values that would be appropriate to make this a getelementptr
86 // instruction. The type returned is the root type that the GEP would point to
88 const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
89 std::vector<Value*> &Indices,
90 BasicBlock::iterator *BI) {
91 const CompositeType *CompTy = dyn_cast<CompositeType>(Ty);
92 if (CompTy == 0) return 0;
94 // See if the cast is of an integer expression that is either a constant,
95 // or a value scaled by some amount with a possible offset.
97 ExprType Expr = ClassifyExpression(OffsetVal);
99 // Get the offset and scale values if they exists...
100 // A scale of zero with Expr.Var != 0 means a scale of 1.
102 int64_t Offset = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
103 int64_t Scale = Expr.Scale ? getConstantValue(Expr.Scale) : 0;
105 if (Expr.Var && Scale == 0) Scale = 1; // Scale != 0 if Expr.Var != 0
107 // Loop over the Scale and Offset values, filling in the Indices vector for
108 // our final getelementptr instruction.
110 const Type *NextTy = CompTy;
112 if (!isa<CompositeType>(NextTy))
113 return 0; // Type must not be ready for processing...
114 CompTy = cast<CompositeType>(NextTy);
116 if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) {
117 // Step into the appropriate element of the structure...
118 uint64_t ActualOffset = (Offset < 0) ? 0 : (uint64_t)Offset;
119 NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices);
120 Offset -= ActualOffset;
122 const Type *ElTy = cast<SequentialType>(CompTy)->getElementType();
123 if (!ElTy->isSized())
124 return 0; // Type is unreasonable... escape!
125 unsigned ElSize = TD.getTypeSize(ElTy);
126 int64_t ElSizeS = ElSize;
128 // See if the user is indexing into a different cell of this array...
129 if (Scale && (Scale >= ElSizeS || -Scale >= ElSizeS)) {
130 // A scale n*ElSize might occur if we are not stepping through
131 // array by one. In this case, we will have to insert math to munge
134 int64_t ScaleAmt = Scale/ElSizeS;
135 if (Scale-ScaleAmt*ElSizeS)
136 return 0; // Didn't scale by a multiple of element size, bail out
137 Scale = 0; // Scale is consumed
139 int64_t Index = Offset/ElSize; // is zero unless Offset > ElSize
140 Offset -= Index*ElSize; // Consume part of the offset
142 if (BI) { // Generate code?
143 BasicBlock *BB = (*BI)->getParent();
144 if (Expr.Var->getType() != Type::LongTy)
145 Expr.Var = new CastInst(Expr.Var, Type::LongTy,
146 Expr.Var->getName()+"-idxcast", *BI);
148 if (ScaleAmt && ScaleAmt != 1) {
149 // If we have to scale up our index, do so now
150 Value *ScaleAmtVal = ConstantSInt::get(Type::LongTy, ScaleAmt);
151 Expr.Var = BinaryOperator::create(Instruction::Mul, Expr.Var,
153 Expr.Var->getName()+"-scale",*BI);
156 if (Index) { // Add an offset to the index
157 Value *IndexAmt = ConstantSInt::get(Type::LongTy, Index);
158 Expr.Var = BinaryOperator::create(Instruction::Add, Expr.Var,
160 Expr.Var->getName()+"-offset",
165 Indices.push_back(Expr.Var);
167 } else if (Offset >= (int64_t)ElSize || -Offset >= (int64_t)ElSize) {
168 // Calculate the index that we are entering into the array cell with
169 uint64_t Index = Offset/ElSize;
170 Indices.push_back(ConstantSInt::get(Type::LongTy, Index));
171 Offset -= (int64_t)(Index*ElSize); // Consume part of the offset
173 } else if (isa<ArrayType>(CompTy) || Indices.empty()) {
174 // Must be indexing a small amount into the first cell of the array
175 // Just index into element zero of the array here.
177 Indices.push_back(ConstantSInt::get(Type::LongTy, 0));
179 return 0; // Hrm. wierd, can't handle this case. Bail
183 } while (Offset || Scale); // Go until we're done!