blob: 3f61864c9c53a918d3c16398fcd1ba4975a9638e [file] [log] [blame]
Chris Lattner59cd9f12001-11-04 23:24:06 +00001//===-- TransformInternals.cpp - Implement shared functions for transforms --=//
2//
3// This file defines shared functions used by the different components of the
4// Transforms library.
5//
6//===----------------------------------------------------------------------===//
7
8#include "TransformInternals.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +00009#include "llvm/Type.h"
Chris Lattner89a1c802001-11-26 16:59:47 +000010#include "llvm/Analysis/Expressions.h"
Chris Lattner0ac54292002-04-09 19:08:28 +000011#include "llvm/Function.h"
Chris Lattner89a1c802001-11-26 16:59:47 +000012#include "llvm/iOther.h"
Chris Lattner59cd9f12001-11-04 23:24:06 +000013
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.
17//
18const TargetData TD("LevelRaise: Should be GCC though!");
19
Chris Lattner59cd9f12001-11-04 23:24:06 +000020
Chris Lattner9e77f7e2002-03-21 06:27:20 +000021static const Type *getStructOffsetStep(const StructType *STy, unsigned &Offset,
22 std::vector<Value*> &Indices) {
23 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
24 const StructLayout *SL = TD.getStructLayout(STy);
25
26 // This loop terminates always on a 0 <= i < MemberOffsets.size()
27 unsigned i;
28 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
29 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
30 break;
31
32 assert(Offset >= SL->MemberOffsets[i] &&
33 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
34
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);
39}
40
41
Chris Lattnerc0b90e72001-11-08 20:19:56 +000042// 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.
47//
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
52//
53const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
Chris Lattner9e77f7e2002-03-21 06:27:20 +000054 std::vector<Value*> &Indices,
Chris Lattner0c0edf82002-07-25 06:17:51 +000055 bool StopEarly) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000056 if (Offset == 0 && StopEarly && !Indices.empty())
Chris Lattnerc0b90e72001-11-08 20:19:56 +000057 return Ty; // Return the leaf type
Chris Lattnerc0b90e72001-11-08 20:19:56 +000058
Chris Lattner89a1c802001-11-26 16:59:47 +000059 unsigned ThisOffset;
60 const Type *NextType;
61 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +000062 ThisOffset = Offset;
63 NextType = getStructOffsetStep(STy, ThisOffset, Indices);
Chris Lattner7991c282001-12-14 16:38:59 +000064 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
65 assert(Offset < TD.getTypeSize(ATy) && "Offset not in composite!");
Chris Lattnerc0b90e72001-11-08 20:19:56 +000066
Chris Lattner89a1c802001-11-26 16:59:47 +000067 NextType = ATy->getElementType();
68 unsigned ChildSize = TD.getTypeSize(NextType);
Chris Lattner9e77f7e2002-03-21 06:27:20 +000069 Indices.push_back(ConstantUInt::get(Type::UIntTy, Offset/ChildSize));
Chris Lattner89a1c802001-11-26 16:59:47 +000070 ThisOffset = (Offset/ChildSize)*ChildSize;
Chris Lattner7991c282001-12-14 16:38:59 +000071 } else {
72 Offset = 0; // Return the offset that we were able to acheive
73 return Ty; // Return the leaf type
Chris Lattner89a1c802001-11-26 16:59:47 +000074 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +000075
Chris Lattner89a1c802001-11-26 16:59:47 +000076 unsigned SubOffs = Offset - ThisOffset;
Chris Lattner4736d062002-03-07 21:18:00 +000077 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
Chris Lattner9e77f7e2002-03-21 06:27:20 +000078 Indices, StopEarly);
Chris Lattner89a1c802001-11-26 16:59:47 +000079 Offset = ThisOffset + SubOffs;
Chris Lattnerc0b90e72001-11-08 20:19:56 +000080 return LeafTy;
81}
Chris Lattner89a1c802001-11-26 16:59:47 +000082
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
87//
88const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
Chris Lattner697954c2002-01-20 22:54:45 +000089 std::vector<Value*> &Indices,
Chris Lattner0c0edf82002-07-25 06:17:51 +000090 BasicBlock::iterator *BI) {
Chris Lattner7991c282001-12-14 16:38:59 +000091 const CompositeType *CompTy = dyn_cast<CompositeType>(Ty);
Chris Lattner89a1c802001-11-26 16:59:47 +000092 if (CompTy == 0) return 0;
93
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.
96 //
Chris Lattnerc74cb862002-08-30 22:53:53 +000097 ExprType Expr = ClassifyExpression(OffsetVal);
Chris Lattner89a1c802001-11-26 16:59:47 +000098
Chris Lattner169bffe2002-04-16 22:10:36 +000099 // Get the offset and scale values if they exists...
Chris Lattner99fb91c2002-03-21 03:04:38 +0000100 // A scale of zero with Expr.Var != 0 means a scale of 1.
101 //
Chris Lattner169bffe2002-04-16 22:10:36 +0000102 int Offset = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
103 int Scale = Expr.Scale ? getConstantValue(Expr.Scale) : 0;
Chris Lattner89a1c802001-11-26 16:59:47 +0000104
Chris Lattner99fb91c2002-03-21 03:04:38 +0000105 if (Expr.Var && Scale == 0) Scale = 1; // Scale != 0 if Expr.Var != 0
106
Chris Lattner89a1c802001-11-26 16:59:47 +0000107 // Loop over the Scale and Offset values, filling in the Indices vector for
108 // our final getelementptr instruction.
109 //
110 const Type *NextTy = CompTy;
111 do {
112 if (!isa<CompositeType>(NextTy))
113 return 0; // Type must not be ready for processing...
114 CompTy = cast<CompositeType>(NextTy);
115
116 if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000117 // Step into the appropriate element of the structure...
Chris Lattner169bffe2002-04-16 22:10:36 +0000118 unsigned ActualOffset = (Offset < 0) ? 0 : (unsigned)Offset;
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000119 NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices);
Chris Lattner89a1c802001-11-26 16:59:47 +0000120 Offset -= ActualOffset;
121 } else {
Chris Lattner7991c282001-12-14 16:38:59 +0000122 const Type *ElTy = cast<SequentialType>(CompTy)->getElementType();
Chris Lattner99fb91c2002-03-21 03:04:38 +0000123 if (!ElTy->isSized())
124 return 0; // Type is unreasonable... escape!
Chris Lattner89a1c802001-11-26 16:59:47 +0000125 unsigned ElSize = TD.getTypeSize(ElTy);
Chris Lattner99fb91c2002-03-21 03:04:38 +0000126 int ElSizeS = (int)ElSize;
Chris Lattner89a1c802001-11-26 16:59:47 +0000127
128 // See if the user is indexing into a different cell of this array...
Chris Lattner99fb91c2002-03-21 03:04:38 +0000129 if (Scale && (Scale >= ElSizeS || -Scale >= ElSizeS)) {
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000130 // 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
132 // the index.
133 //
Chris Lattner99fb91c2002-03-21 03:04:38 +0000134 int ScaleAmt = Scale/ElSizeS;
135 if (Scale-ScaleAmt*ElSizeS)
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000136 return 0; // Didn't scale by a multiple of element size, bail out
Chris Lattner7991c282001-12-14 16:38:59 +0000137 Scale = 0; // Scale is consumed
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000138
Chris Lattnerca180c72002-04-09 20:53:36 +0000139 int Index = Offset/ElSize; // is zero unless Offset > ElSize
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000140 Offset -= Index*ElSize; // Consume part of the offset
141
142 if (BI) { // Generate code?
Chris Lattner7e708292002-06-25 16:13:24 +0000143 BasicBlock *BB = (*BI)->getParent();
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000144 if (Expr.Var->getType() != Type::UIntTy) {
145 CastInst *IdxCast = new CastInst(Expr.Var, Type::UIntTy);
146 if (Expr.Var->hasName())
147 IdxCast->setName(Expr.Var->getName()+"-idxcast");
Chris Lattner7e708292002-06-25 16:13:24 +0000148 *BI = ++BB->getInstList().insert(*BI, IdxCast);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000149 Expr.Var = IdxCast;
150 }
151
Chris Lattner7c3f4152001-12-07 04:26:02 +0000152 if (ScaleAmt && ScaleAmt != 1) {
153 // If we have to scale up our index, do so now
Chris Lattner99fb91c2002-03-21 03:04:38 +0000154 Value *ScaleAmtVal = ConstantUInt::get(Type::UIntTy,
155 (unsigned)ScaleAmt);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000156 Instruction *Scaler = BinaryOperator::create(Instruction::Mul,
Chris Lattner99fb91c2002-03-21 03:04:38 +0000157 Expr.Var, ScaleAmtVal);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000158 if (Expr.Var->hasName())
159 Scaler->setName(Expr.Var->getName()+"-scale");
160
Chris Lattner7e708292002-06-25 16:13:24 +0000161 *BI = ++BB->getInstList().insert(*BI, Scaler);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000162 Expr.Var = Scaler;
163 }
164
165 if (Index) { // Add an offset to the index
Chris Lattnerca180c72002-04-09 20:53:36 +0000166 Value *IndexAmt = ConstantUInt::get(Type::UIntTy, (unsigned)Index);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000167 Instruction *Offseter = BinaryOperator::create(Instruction::Add,
168 Expr.Var, IndexAmt);
169 if (Expr.Var->hasName())
170 Offseter->setName(Expr.Var->getName()+"-offset");
Chris Lattner7e708292002-06-25 16:13:24 +0000171 *BI = ++BB->getInstList().insert(*BI, Offseter);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000172 Expr.Var = Offseter;
173 }
174 }
175
176 Indices.push_back(Expr.Var);
Chris Lattner99fb91c2002-03-21 03:04:38 +0000177 Expr.Var = 0;
Chris Lattnerca180c72002-04-09 20:53:36 +0000178 } else if (Offset >= (int)ElSize || -Offset >= (int)ElSize) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000179 // Calculate the index that we are entering into the array cell with
180 unsigned Index = Offset/ElSize;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000181 Indices.push_back(ConstantUInt::get(Type::UIntTy, Index));
Chris Lattnerca180c72002-04-09 20:53:36 +0000182 Offset -= (int)(Index*ElSize); // Consume part of the offset
Chris Lattner89a1c802001-11-26 16:59:47 +0000183
Chris Lattner754cf412002-03-14 16:37:04 +0000184 } else if (isa<ArrayType>(CompTy) || Indices.empty()) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000185 // Must be indexing a small amount into the first cell of the array
186 // Just index into element zero of the array here.
187 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000188 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
Chris Lattner7991c282001-12-14 16:38:59 +0000189 } else {
190 return 0; // Hrm. wierd, can't handle this case. Bail
Chris Lattner89a1c802001-11-26 16:59:47 +0000191 }
192 NextTy = ElTy;
193 }
194 } while (Offset || Scale); // Go until we're done!
195
196 return NextTy;
197}