blob: e5a88c592f5d0ce13130f85db0d74350b58dcfab [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 Lattner7991c282001-12-14 16:38:59 +000013#include <algorithm>
Chris Lattner59cd9f12001-11-04 23:24:06 +000014
15// TargetData Hack: Eventually we will have annotations given to us by the
16// backend so that we know stuff about type size and alignments. For now
17// though, just use this, because it happens to match the model that GCC uses.
18//
19const TargetData TD("LevelRaise: Should be GCC though!");
20
Chris Lattner59cd9f12001-11-04 23:24:06 +000021// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
22// with a value, then remove and delete the original instruction.
23//
24void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
25 BasicBlock::iterator &BI, Value *V) {
26 Instruction *I = *BI;
27 // Replaces all of the uses of the instruction with uses of the value
28 I->replaceAllUsesWith(V);
29
30 // Remove the unneccesary instruction now...
31 BIL.remove(BI);
32
33 // Make sure to propogate a name if there is one already...
34 if (I->hasName() && !V->hasName())
35 V->setName(I->getName(), BIL.getParent()->getSymbolTable());
36
37 // Remove the dead instruction now...
38 delete I;
39}
40
41
42// ReplaceInstWithInst - Replace the instruction specified by BI with the
43// instruction specified by I. The original instruction is deleted and BI is
44// updated to point to the new instruction.
45//
46void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
47 BasicBlock::iterator &BI, Instruction *I) {
48 assert(I->getParent() == 0 &&
49 "ReplaceInstWithInst: Instruction already inserted into basic block!");
50
51 // Insert the new instruction into the basic block...
Chris Lattnerc4db7542002-03-11 22:21:04 +000052 BI = BIL.insert(BI, I)+1; // Increment BI to point to instruction to delete
Chris Lattner59cd9f12001-11-04 23:24:06 +000053
54 // Replace all uses of the old instruction, and delete it.
55 ReplaceInstWithValue(BIL, BI, I);
56
Chris Lattnerc4db7542002-03-11 22:21:04 +000057 // Move BI back to point to the newly inserted instruction
Chris Lattner59cd9f12001-11-04 23:24:06 +000058 --BI;
59}
60
Chris Lattner7991c282001-12-14 16:38:59 +000061void ReplaceInstWithInst(Instruction *From, Instruction *To) {
62 BasicBlock *BB = From->getParent();
63 BasicBlock::InstListType &BIL = BB->getInstList();
64 BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), From);
65 assert(BI != BIL.end() && "Inst not in it's parents BB!");
66 ReplaceInstWithInst(BIL, BI, To);
67}
68
Chris Lattner8e865422002-03-21 06:24:00 +000069// InsertInstBeforeInst - Insert 'NewInst' into the basic block that 'Existing'
70// is already in, and put it right before 'Existing'. This instruction should
71// only be used when there is no iterator to Existing already around. The
72// returned iterator points to the new instruction.
73//
74BasicBlock::iterator InsertInstBeforeInst(Instruction *NewInst,
75 Instruction *Existing) {
76 BasicBlock *BB = Existing->getParent();
77 BasicBlock::InstListType &BIL = BB->getInstList();
78 BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), Existing);
79 assert(BI != BIL.end() && "Inst not in it's parents BB!");
80 return BIL.insert(BI, NewInst);
81}
82
Chris Lattner7991c282001-12-14 16:38:59 +000083
Chris Lattner59cd9f12001-11-04 23:24:06 +000084
Chris Lattner9e77f7e2002-03-21 06:27:20 +000085static const Type *getStructOffsetStep(const StructType *STy, unsigned &Offset,
86 std::vector<Value*> &Indices) {
87 assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
88 const StructLayout *SL = TD.getStructLayout(STy);
89
90 // This loop terminates always on a 0 <= i < MemberOffsets.size()
91 unsigned i;
92 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
93 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
94 break;
95
96 assert(Offset >= SL->MemberOffsets[i] &&
97 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
98
99 // Make sure to save the current index...
100 Indices.push_back(ConstantUInt::get(Type::UByteTy, i));
101 Offset = SL->MemberOffsets[i];
102 return STy->getContainedType(i);
103}
104
105
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000106// getStructOffsetType - Return a vector of offsets that are to be used to index
107// into the specified struct type to get as close as possible to index as we
108// can. Note that it is possible that we cannot get exactly to Offset, in which
109// case we update offset to be the offset we actually obtained. The resultant
110// leaf type is returned.
111//
112// If StopEarly is set to true (the default), the first object with the
113// specified type is returned, even if it is a struct type itself. In this
114// case, this routine will not drill down to the leaf type. Set StopEarly to
115// false if you want a leaf
116//
117const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000118 std::vector<Value*> &Indices,
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000119 bool StopEarly = true) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000120 if (Offset == 0 && StopEarly && !Indices.empty())
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000121 return Ty; // Return the leaf type
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000122
Chris Lattner89a1c802001-11-26 16:59:47 +0000123 unsigned ThisOffset;
124 const Type *NextType;
125 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000126 ThisOffset = Offset;
127 NextType = getStructOffsetStep(STy, ThisOffset, Indices);
Chris Lattner7991c282001-12-14 16:38:59 +0000128 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
129 assert(Offset < TD.getTypeSize(ATy) && "Offset not in composite!");
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000130
Chris Lattner89a1c802001-11-26 16:59:47 +0000131 NextType = ATy->getElementType();
132 unsigned ChildSize = TD.getTypeSize(NextType);
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000133 Indices.push_back(ConstantUInt::get(Type::UIntTy, Offset/ChildSize));
Chris Lattner89a1c802001-11-26 16:59:47 +0000134 ThisOffset = (Offset/ChildSize)*ChildSize;
Chris Lattner7991c282001-12-14 16:38:59 +0000135 } else {
136 Offset = 0; // Return the offset that we were able to acheive
137 return Ty; // Return the leaf type
Chris Lattner89a1c802001-11-26 16:59:47 +0000138 }
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000139
Chris Lattner89a1c802001-11-26 16:59:47 +0000140 unsigned SubOffs = Offset - ThisOffset;
Chris Lattner4736d062002-03-07 21:18:00 +0000141 const Type *LeafTy = getStructOffsetType(NextType, SubOffs,
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000142 Indices, StopEarly);
Chris Lattner89a1c802001-11-26 16:59:47 +0000143 Offset = ThisOffset + SubOffs;
Chris Lattnerc0b90e72001-11-08 20:19:56 +0000144 return LeafTy;
145}
Chris Lattner89a1c802001-11-26 16:59:47 +0000146
147// ConvertableToGEP - This function returns true if the specified value V is
148// a valid index into a pointer of type Ty. If it is valid, Idx is filled in
149// with the values that would be appropriate to make this a getelementptr
150// instruction. The type returned is the root type that the GEP would point to
151//
152const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
Chris Lattner697954c2002-01-20 22:54:45 +0000153 std::vector<Value*> &Indices,
Chris Lattner89a1c802001-11-26 16:59:47 +0000154 BasicBlock::iterator *BI = 0) {
Chris Lattner7991c282001-12-14 16:38:59 +0000155 const CompositeType *CompTy = dyn_cast<CompositeType>(Ty);
Chris Lattner89a1c802001-11-26 16:59:47 +0000156 if (CompTy == 0) return 0;
157
158 // See if the cast is of an integer expression that is either a constant,
159 // or a value scaled by some amount with a possible offset.
160 //
161 analysis::ExprType Expr = analysis::ClassifyExpression(OffsetVal);
162
Chris Lattner169bffe2002-04-16 22:10:36 +0000163 // Get the offset and scale values if they exists...
Chris Lattner99fb91c2002-03-21 03:04:38 +0000164 // A scale of zero with Expr.Var != 0 means a scale of 1.
165 //
Chris Lattner169bffe2002-04-16 22:10:36 +0000166 int Offset = Expr.Offset ? getConstantValue(Expr.Offset) : 0;
167 int Scale = Expr.Scale ? getConstantValue(Expr.Scale) : 0;
Chris Lattner89a1c802001-11-26 16:59:47 +0000168
Chris Lattner99fb91c2002-03-21 03:04:38 +0000169 if (Expr.Var && Scale == 0) Scale = 1; // Scale != 0 if Expr.Var != 0
170
Chris Lattner89a1c802001-11-26 16:59:47 +0000171 // Loop over the Scale and Offset values, filling in the Indices vector for
172 // our final getelementptr instruction.
173 //
174 const Type *NextTy = CompTy;
175 do {
176 if (!isa<CompositeType>(NextTy))
177 return 0; // Type must not be ready for processing...
178 CompTy = cast<CompositeType>(NextTy);
179
180 if (const StructType *StructTy = dyn_cast<StructType>(CompTy)) {
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000181 // Step into the appropriate element of the structure...
Chris Lattner169bffe2002-04-16 22:10:36 +0000182 unsigned ActualOffset = (Offset < 0) ? 0 : (unsigned)Offset;
Chris Lattner9e77f7e2002-03-21 06:27:20 +0000183 NextTy = getStructOffsetStep(StructTy, ActualOffset, Indices);
Chris Lattner89a1c802001-11-26 16:59:47 +0000184 Offset -= ActualOffset;
185 } else {
Chris Lattner7991c282001-12-14 16:38:59 +0000186 const Type *ElTy = cast<SequentialType>(CompTy)->getElementType();
Chris Lattner99fb91c2002-03-21 03:04:38 +0000187 if (!ElTy->isSized())
188 return 0; // Type is unreasonable... escape!
Chris Lattner89a1c802001-11-26 16:59:47 +0000189 unsigned ElSize = TD.getTypeSize(ElTy);
Chris Lattner99fb91c2002-03-21 03:04:38 +0000190 int ElSizeS = (int)ElSize;
Chris Lattner89a1c802001-11-26 16:59:47 +0000191
192 // See if the user is indexing into a different cell of this array...
Chris Lattner99fb91c2002-03-21 03:04:38 +0000193 if (Scale && (Scale >= ElSizeS || -Scale >= ElSizeS)) {
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000194 // A scale n*ElSize might occur if we are not stepping through
195 // array by one. In this case, we will have to insert math to munge
196 // the index.
197 //
Chris Lattner99fb91c2002-03-21 03:04:38 +0000198 int ScaleAmt = Scale/ElSizeS;
199 if (Scale-ScaleAmt*ElSizeS)
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000200 return 0; // Didn't scale by a multiple of element size, bail out
Chris Lattner7991c282001-12-14 16:38:59 +0000201 Scale = 0; // Scale is consumed
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000202
Chris Lattnerca180c72002-04-09 20:53:36 +0000203 int Index = Offset/ElSize; // is zero unless Offset > ElSize
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000204 Offset -= Index*ElSize; // Consume part of the offset
205
206 if (BI) { // Generate code?
207 BasicBlock *BB = (**BI)->getParent();
208 if (Expr.Var->getType() != Type::UIntTy) {
209 CastInst *IdxCast = new CastInst(Expr.Var, Type::UIntTy);
210 if (Expr.Var->hasName())
211 IdxCast->setName(Expr.Var->getName()+"-idxcast");
212 *BI = BB->getInstList().insert(*BI, IdxCast)+1;
213 Expr.Var = IdxCast;
214 }
215
Chris Lattner7c3f4152001-12-07 04:26:02 +0000216 if (ScaleAmt && ScaleAmt != 1) {
217 // If we have to scale up our index, do so now
Chris Lattner99fb91c2002-03-21 03:04:38 +0000218 Value *ScaleAmtVal = ConstantUInt::get(Type::UIntTy,
219 (unsigned)ScaleAmt);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000220 Instruction *Scaler = BinaryOperator::create(Instruction::Mul,
Chris Lattner99fb91c2002-03-21 03:04:38 +0000221 Expr.Var, ScaleAmtVal);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000222 if (Expr.Var->hasName())
223 Scaler->setName(Expr.Var->getName()+"-scale");
224
225 *BI = BB->getInstList().insert(*BI, Scaler)+1;
226 Expr.Var = Scaler;
227 }
228
229 if (Index) { // Add an offset to the index
Chris Lattnerca180c72002-04-09 20:53:36 +0000230 Value *IndexAmt = ConstantUInt::get(Type::UIntTy, (unsigned)Index);
Chris Lattner2dc48bd2001-12-05 19:41:16 +0000231 Instruction *Offseter = BinaryOperator::create(Instruction::Add,
232 Expr.Var, IndexAmt);
233 if (Expr.Var->hasName())
234 Offseter->setName(Expr.Var->getName()+"-offset");
235 *BI = BB->getInstList().insert(*BI, Offseter)+1;
236 Expr.Var = Offseter;
237 }
238 }
239
240 Indices.push_back(Expr.Var);
Chris Lattner99fb91c2002-03-21 03:04:38 +0000241 Expr.Var = 0;
Chris Lattnerca180c72002-04-09 20:53:36 +0000242 } else if (Offset >= (int)ElSize || -Offset >= (int)ElSize) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000243 // Calculate the index that we are entering into the array cell with
244 unsigned Index = Offset/ElSize;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000245 Indices.push_back(ConstantUInt::get(Type::UIntTy, Index));
Chris Lattnerca180c72002-04-09 20:53:36 +0000246 Offset -= (int)(Index*ElSize); // Consume part of the offset
Chris Lattner89a1c802001-11-26 16:59:47 +0000247
Chris Lattner754cf412002-03-14 16:37:04 +0000248 } else if (isa<ArrayType>(CompTy) || Indices.empty()) {
Chris Lattner89a1c802001-11-26 16:59:47 +0000249 // Must be indexing a small amount into the first cell of the array
250 // Just index into element zero of the array here.
251 //
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000252 Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
Chris Lattner7991c282001-12-14 16:38:59 +0000253 } else {
254 return 0; // Hrm. wierd, can't handle this case. Bail
Chris Lattner89a1c802001-11-26 16:59:47 +0000255 }
256 NextTy = ElTy;
257 }
258 } while (Offset || Scale); // Go until we're done!
259
260 return NextTy;
261}