blob: 2f2f77f975f7301387722b7e1cfdb42136fe0a95 [file] [log] [blame]
Chris Lattnerd32a9612001-11-01 02:42:08 +00001//===- LevelRaise.cpp - Code to change LLVM to higher level -----------------=//
2//
3// This file implements the 'raising' part of the LevelChange API. This is
4// useful because, in general, it makes the LLVM code terser and easier to
5// analyze. Note that it is good to run DCE after doing this transformation.
6//
7// Eliminate silly things in the source that do not effect the level, but do
8// clean up the code:
9// * Casts of casts
10// - getelementptr/load & getelementptr/store are folded into a direct
11// load or store
12// - Convert this code (for both alloca and malloc):
13// %reg110 = shl uint %n, ubyte 2 ;;<uint>
14// %reg108 = alloca ubyte, uint %reg110 ;;<ubyte*>
15// %cast76 = cast ubyte* %reg108 to uint* ;;<uint*>
16// To: %cast76 = alloca uint, uint %n
17// Convert explicit addressing to use getelementptr instruction where possible
18// - ...
19//
20// Convert explicit addressing on pointers to use getelementptr instruction.
21// - If a pointer is used by arithmetic operation, insert an array casted
22// version into the source program, only for the following pointer types:
23// * Method argument pointers
24// - Pointers returned by alloca or malloc
25// - Pointers returned by function calls
26// - If a pointer is indexed with a value scaled by a constant size equal
27// to the element size of the array, the expression is replaced with a
28// getelementptr instruction.
29//
30//===----------------------------------------------------------------------===//
31
32#include "llvm/Transforms/LevelChange.h"
33#include "llvm/Method.h"
34#include "llvm/Support/STLExtras.h"
35#include "llvm/iOther.h"
36#include "llvm/iMemory.h"
37#include "llvm/ConstPoolVals.h"
38#include "llvm/Target/TargetData.h"
Chris Lattnerdedee7b2001-11-01 05:57:59 +000039#include "llvm/Optimizations/ConstantHandling.h"
Chris Lattner68b07b72001-11-01 07:00:51 +000040#include "llvm/Optimizations/DCE.h"
Chris Lattnerd32a9612001-11-01 02:42:08 +000041#include <map>
42#include <algorithm>
43
44#include "llvm/Assembly/Writer.h"
45
46//#define DEBUG_PEEPHOLE_INSTS 1
47
48#ifdef DEBUG_PEEPHOLE_INSTS
49#define PRINT_PEEPHOLE(ID, NUM, I) \
50 cerr << "Inst P/H " << ID << "[" << NUM << "] " << I;
51#else
52#define PRINT_PEEPHOLE(ID, NUM, I)
53#endif
54
55#define PRINT_PEEPHOLE1(ID, I1) do { PRINT_PEEPHOLE(ID, 0, I1); } while (0)
56#define PRINT_PEEPHOLE2(ID, I1, I2) \
57 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); } while (0)
58#define PRINT_PEEPHOLE3(ID, I1, I2, I3) \
59 do { PRINT_PEEPHOLE(ID, 0, I1); PRINT_PEEPHOLE(ID, 1, I2); \
60 PRINT_PEEPHOLE(ID, 2, I3); } while (0)
61
62
63// TargetData Hack: Eventually we will have annotations given to us by the
64// backend so that we know stuff about type size and alignments. For now
65// though, just use this, because it happens to match the model that GCC uses.
66//
67const TargetData TD("LevelRaise: Should be GCC though!");
68
69
70// losslessCastableTypes - Return true if the types are bitwise equivalent.
71// This predicate returns true if it is possible to cast from one type to
72// another without gaining or losing precision, or altering the bits in any way.
73//
74static bool losslessCastableTypes(const Type *T1, const Type *T2) {
Chris Lattnerdedee7b2001-11-01 05:57:59 +000075 if (!T1->isPrimitiveType() && !isa<PointerType>(T1)) return false;
76 if (!T2->isPrimitiveType() && !isa<PointerType>(T2)) return false;
Chris Lattnerd32a9612001-11-01 02:42:08 +000077
78 if (T1->getPrimitiveID() == T2->getPrimitiveID())
79 return true; // Handles identity cast, and cast of differing pointer types
80
81 // Now we know that they are two differing primitive or pointer types
82 switch (T1->getPrimitiveID()) {
83 case Type::UByteTyID: return T2 == Type::SByteTy;
84 case Type::SByteTyID: return T2 == Type::UByteTy;
85 case Type::UShortTyID: return T2 == Type::ShortTy;
86 case Type::ShortTyID: return T2 == Type::UShortTy;
87 case Type::UIntTyID: return T2 == Type::IntTy;
88 case Type::IntTyID: return T2 == Type::UIntTy;
89 case Type::ULongTyID:
90 case Type::LongTyID:
91 case Type::PointerTyID:
92 return T2 == Type::ULongTy || T2 == Type::LongTy ||
93 T2->getPrimitiveID() == Type::PointerTyID;
94 default:
95 return false; // Other types have no identity values
96 }
97}
98
99
100// isReinterpretingCast - Return true if the cast instruction specified will
101// cause the operand to be "reinterpreted". A value is reinterpreted if the
102// cast instruction would cause the underlying bits to change.
103//
104static inline bool isReinterpretingCast(const CastInst *CI) {
105 return !losslessCastableTypes(CI->getOperand(0)->getType(), CI->getType());
106}
107
108
109// getPointedToStruct - If the argument is a pointer type, and the pointed to
110// value is a struct type, return the struct type, else return null.
111//
112static const StructType *getPointedToStruct(const Type *Ty) {
113 const PointerType *PT = dyn_cast<PointerType>(Ty);
114 return PT ? dyn_cast<StructType>(PT->getValueType()) : 0;
115}
116
117
118// getStructOffsetType - Return a vector of offsets that are to be used to index
119// into the specified struct type to get as close as possible to index as we
120// can. Note that it is possible that we cannot get exactly to Offset, in which
121// case we update offset to be the offset we actually obtained. The resultant
122// leaf type is returned.
123//
124static const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
125 vector<ConstPoolVal*> &Offsets) {
126 if (!isa<StructType>(Ty)) {
127 Offset = 0; // Return the offset that we were able to acheive
128 return Ty; // Return the leaf type
129 }
130
131 assert(Offset < TD.getTypeSize(Ty) && "Offset not in struct!");
132 const StructType *STy = cast<StructType>(Ty);
133 const StructLayout *SL = TD.getStructLayout(STy);
134
135 // This loop terminates always on a 0 <= i < MemberOffsets.size()
136 unsigned i;
137 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
138 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
139 break;
140
Chris Lattner68b07b72001-11-01 07:00:51 +0000141 assert(Offset >= SL->MemberOffsets[i] &&
142 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
Chris Lattnerd32a9612001-11-01 02:42:08 +0000143
144 // Make sure to save the current index...
145 Offsets.push_back(ConstPoolUInt::get(Type::UByteTy, i));
146
147 unsigned SubOffs = Offset - SL->MemberOffsets[i];
148 const Type *LeafTy = getStructOffsetType(STy->getElementTypes()[i], SubOffs,
149 Offsets);
150 Offset = SL->MemberOffsets[i] + SubOffs;
151 return LeafTy;
152}
153
154
155
156// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
157// with a value, then remove and delete the original instruction.
158//
159static void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
160 BasicBlock::iterator &BI, Value *V) {
161 Instruction *I = *BI;
162 // Replaces all of the uses of the instruction with uses of the value
163 I->replaceAllUsesWith(V);
164
165 // Remove the unneccesary instruction now...
166 BIL.remove(BI);
167
168 // Make sure to propogate a name if there is one already...
169 if (I->hasName() && !V->hasName())
170 V->setName(I->getName(), BIL.getParent()->getSymbolTable());
171
172 // Remove the dead instruction now...
173 delete I;
174}
175
176
177// ReplaceInstWithInst - Replace the instruction specified by BI with the
178// instruction specified by I. The original instruction is deleted and BI is
179// updated to point to the new instruction.
180//
181static void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
182 BasicBlock::iterator &BI, Instruction *I) {
183 assert(I->getParent() == 0 &&
184 "ReplaceInstWithInst: Instruction already inserted into basic block!");
185
186 // Insert the new instruction into the basic block...
187 BI = BIL.insert(BI, I)+1;
188
189 // Replace all uses of the old instruction, and delete it.
190 ReplaceInstWithValue(BIL, BI, I);
191
192 // Reexamine the instruction just inserted next time around the cleanup pass
193 // loop.
194 --BI;
195}
196
197
Chris Lattnerb980e182001-11-04 21:32:11 +0000198
199typedef map<const Value*, const Type*> ValueTypeCache;
200typedef map<const Value*, Value*> ValueMapCache;
201
202
203
Chris Lattnerd32a9612001-11-01 02:42:08 +0000204// ExpressionConvertableToType - Return true if it is possible
205static bool ExpressionConvertableToType(Value *V, const Type *Ty) {
206 Instruction *I = dyn_cast<Instruction>(V);
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000207 if (I == 0) {
208 // It's not an instruction, check to see if it's a constant... all constants
209 // can be converted to an equivalent value (except pointers, they can't be
210 // const prop'd in general).
211 //
212 if (isa<ConstPoolVal>(V) &&
213 !isa<PointerType>(V->getType()) && !isa<PointerType>(Ty)) return true;
214
215 return false; // Otherwise, we can't convert!
216 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000217 if (I->getType() == Ty) return false; // Expression already correct type!
218
219 switch (I->getOpcode()) {
220 case Instruction::Cast:
221 // We can convert the expr if the cast destination type is losslessly
222 // convertable to the requested type.
223 return losslessCastableTypes(Ty, I->getType());
224
225 case Instruction::Add:
226 case Instruction::Sub:
227 return ExpressionConvertableToType(I->getOperand(0), Ty) &&
228 ExpressionConvertableToType(I->getOperand(1), Ty);
Chris Lattnerb9693952001-11-04 07:42:17 +0000229 case Instruction::Shr:
230 if (Ty->isSigned() != V->getType()->isSigned()) return false;
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000231 // FALL THROUGH
232 case Instruction::Shl:
Chris Lattnerb9693952001-11-04 07:42:17 +0000233 return ExpressionConvertableToType(I->getOperand(0), Ty);
234
235 case Instruction::Load: {
236 LoadInst *LI = cast<LoadInst>(I);
237 if (LI->hasIndices()) return false;
238 return ExpressionConvertableToType(LI->getPtrOperand(),
239 PointerType::get(Ty));
240 }
241 case Instruction::GetElementPtr: {
242 // GetElementPtr's are directly convertable to a pointer type if they have
243 // a number of zeros at the end. Because removing these values does not
244 // change the logical offset of the GEP, it is okay and fair to remove them.
245 // This can change this:
246 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
247 // %t2 = cast %List * * %t1 to %List *
248 // into
249 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
250 //
251 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
252 const PointerType *PTy = dyn_cast<PointerType>(Ty);
253 if (!PTy) return false;
254
255 // Check to see if there are zero elements that we can remove from the
256 // index array. If there are, check to see if removing them causes us to
257 // get to the right type...
258 //
Chris Lattner8e7f4092001-11-04 08:08:34 +0000259 vector<ConstPoolVal*> Indices = GEP->getIndices();
Chris Lattnerb9693952001-11-04 07:42:17 +0000260 const Type *BaseType = GEP->getPtrOperand()->getType();
261
262 while (Indices.size() &&
263 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
264 Indices.pop_back();
265 const Type *ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices,
266 true);
267 if (ElTy == PTy->getValueType())
268 return true; // Found a match!!
269 }
270 break; // No match, maybe next time.
271 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000272 }
273 return false;
274}
275
276
Chris Lattnerdedee7b2001-11-01 05:57:59 +0000277static Value *ConvertExpressionToType(Value *V, const Type *Ty) {
278 assert(ExpressionConvertableToType(V, Ty) && "Value is not convertable!");
279 Instruction *I = dyn_cast<Instruction>(V);
280 if (I == 0)
281 if (ConstPoolVal *CPV = cast<ConstPoolVal>(V)) {
282 // Constants are converted by constant folding the cast that is required.
283 // We assume here that all casts are implemented for constant prop.
284 Value *Result = opt::ConstantFoldCastInstruction(CPV, Ty);
285 if (!Result) cerr << "Couldn't fold " << CPV << " to " << Ty << endl;
286 assert(Result && "ConstantFoldCastInstruction Failed!!!");
287 return Result;
288 }
289
290
Chris Lattnerd32a9612001-11-01 02:42:08 +0000291 BasicBlock *BB = I->getParent();
292 BasicBlock::InstListType &BIL = BB->getInstList();
293 string Name = I->getName(); if (!Name.empty()) I->setName("");
294 Instruction *Res; // Result of conversion
295
296 //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl;
297
298 switch (I->getOpcode()) {
299 case Instruction::Cast:
300 Res = new CastInst(I->getOperand(0), Ty, Name);
301 break;
302
303 case Instruction::Add:
304 case Instruction::Sub:
305 Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
306 ConvertExpressionToType(I->getOperand(0), Ty),
307 ConvertExpressionToType(I->getOperand(1), Ty),
308 Name);
309 break;
310
311 case Instruction::Shl:
312 case Instruction::Shr:
313 Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(),
314 ConvertExpressionToType(I->getOperand(0), Ty),
315 I->getOperand(1), Name);
316 break;
317
Chris Lattnerb9693952001-11-04 07:42:17 +0000318 case Instruction::Load: {
319 LoadInst *LI = cast<LoadInst>(I);
320 assert(!LI->hasIndices());
321 Res = new LoadInst(ConvertExpressionToType(LI->getPtrOperand(),
322 PointerType::get(Ty)), Name);
323 break;
324 }
325
326 case Instruction::GetElementPtr: {
327 // GetElementPtr's are directly convertable to a pointer type if they have
328 // a number of zeros at the end. Because removing these values does not
329 // change the logical offset of the GEP, it is okay and fair to remove them.
330 // This can change this:
331 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
332 // %t2 = cast %List * * %t1 to %List *
333 // into
334 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
335 //
336 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
337
338 // Check to see if there are zero elements that we can remove from the
339 // index array. If there are, check to see if removing them causes us to
340 // get to the right type...
341 //
Chris Lattner8e7f4092001-11-04 08:08:34 +0000342 vector<ConstPoolVal*> Indices = GEP->getIndices();
Chris Lattnerb9693952001-11-04 07:42:17 +0000343 const Type *BaseType = GEP->getPtrOperand()->getType();
344 const Type *PVTy = cast<PointerType>(Ty)->getValueType();
345 Res = 0;
346 while (Indices.size() &&
347 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
348 Indices.pop_back();
349 if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
350 if (Indices.size() == 0) {
351 Res = new CastInst(GEP->getPtrOperand(), BaseType); // NOOP
352 } else {
353 Res = new GetElementPtrInst(GEP->getPtrOperand(), Indices, Name);
354 }
355 break;
356 }
357 }
358 assert(Res && "Didn't find match!");
359 break; // No match, maybe next time.
360 }
361
Chris Lattnerd32a9612001-11-01 02:42:08 +0000362 default:
363 assert(0 && "Expression convertable, but don't know how to convert?");
364 return 0;
365 }
366
367 BasicBlock::iterator It = find(BIL.begin(), BIL.end(), I);
368 assert(It != BIL.end() && "Instruction not in own basic block??");
369 BIL.insert(It, Res);
370
371 //cerr << "RInst: " << Res << "BB After: " << BB << endl << endl;
372
373 return Res;
374}
375
Chris Lattnerb980e182001-11-04 21:32:11 +0000376static inline const Type *getTy(const Value *V, ValueTypeCache &CT) {
377 ValueTypeCache::iterator I = CT.find(V);
378 if (I == CT.end()) return V->getType();
379 return I->second;
380}
Chris Lattnerd32a9612001-11-01 02:42:08 +0000381
382
Chris Lattnerb980e182001-11-04 21:32:11 +0000383static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
384 ValueTypeCache &ConvertedTypes);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000385
386// RetValConvertableToType - Return true if it is possible
Chris Lattnerb980e182001-11-04 21:32:11 +0000387static bool RetValConvertableToType(Value *V, const Type *Ty,
388 ValueTypeCache &ConvertedTypes) {
389 ValueTypeCache::iterator I = ConvertedTypes.find(V);
390 if (I != ConvertedTypes.end()) return I->second == Ty;
391 ConvertedTypes[V] = Ty;
392
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000393 // It is safe to convert the specified value to the specified type IFF all of
394 // the uses of the value can be converted to accept the new typed value.
395 //
396 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
Chris Lattnerb980e182001-11-04 21:32:11 +0000397 if (!OperandConvertableToType(*I, V, Ty, ConvertedTypes))
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000398 return false;
399
400 return true;
401}
402
403
Chris Lattnerb980e182001-11-04 21:32:11 +0000404// OperandConvertableToType - Return true if it is possible to convert operand
405// V of User (instruction) U to the specified type. This is true iff it is
406// possible to change the specified instruction to accept this. CTMap is a map
407// of converted types, so that circular definitions will see the future type of
408// the expression, not the static current type.
409//
410static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
411 ValueTypeCache &CTMap) {
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000412 assert(V->getType() != Ty &&
413 "OperandConvertableToType: Operand is already right type!");
414 Instruction *I = dyn_cast<Instruction>(U);
415 if (I == 0) return false; // We can't convert!
416
417 switch (I->getOpcode()) {
418 case Instruction::Cast:
419 assert(I->getOperand(0) == V);
420 // We can convert the expr if the cast destination type is losslessly
421 // convertable to the requested type.
422 return losslessCastableTypes(Ty, I->getOperand(0)->getType());
423
424 case Instruction::Add:
425 case Instruction::Sub: {
426 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
Chris Lattnerb980e182001-11-04 21:32:11 +0000427 return RetValConvertableToType(I, Ty, CTMap) &&
428 ExpressionConvertableToType(OtherOp, Ty);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000429 }
430 case Instruction::SetEQ:
431 case Instruction::SetNE: {
432 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
433 return ExpressionConvertableToType(OtherOp, Ty);
434 }
435 case Instruction::Shr:
436 if (Ty->isSigned() != V->getType()->isSigned()) return false;
437 // FALL THROUGH
438 case Instruction::Shl:
439 assert(I->getOperand(0) == V);
Chris Lattnerb980e182001-11-04 21:32:11 +0000440 return RetValConvertableToType(I, Ty, CTMap);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000441
442 case Instruction::Load:
443 assert(I->getOperand(0) == V);
444 if (const PointerType *PT = dyn_cast<PointerType>(Ty)) {
445 LoadInst *LI = cast<LoadInst>(I);
446 if (LI->hasIndices() ||
447 TD.getTypeSize(PT->getValueType()) != TD.getTypeSize(LI->getType()))
448 return false;
449
Chris Lattnerb980e182001-11-04 21:32:11 +0000450 return RetValConvertableToType(LI, PT->getValueType(), CTMap);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000451 }
452 return false;
453
454 case Instruction::Store: {
455 StoreInst *SI = cast<StoreInst>(I);
456 if (SI->hasIndices()) return false;
457
458 if (V == I->getOperand(0)) {
459 // Can convert the store if we can convert the pointer operand to match
460 // the new value type...
461 return ExpressionConvertableToType(I->getOperand(1),PointerType::get(Ty));
462 } else if (const PointerType *PT = dyn_cast<PointerType>(Ty)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000463 if (isa<ArrayType>(PT->getValueType()))
464 return false; // Avoid getDataSize on unsized array type!
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000465 assert(V == I->getOperand(1));
466
467 // Must move the same amount of data...
468 if (TD.getTypeSize(PT->getValueType()) !=
469 TD.getTypeSize(I->getOperand(0)->getType())) return false;
470
471 // Can convert store if the incoming value is convertable...
472 return ExpressionConvertableToType(I->getOperand(0), PT->getValueType());
473 }
474 return false;
475 }
476
477
478#if 0
479 case Instruction::GetElementPtr: {
480 // GetElementPtr's are directly convertable to a pointer type if they have
481 // a number of zeros at the end. Because removing these values does not
482 // change the logical offset of the GEP, it is okay and fair to remove them.
483 // This can change this:
484 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
485 // %t2 = cast %List * * %t1 to %List *
486 // into
487 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
488 //
489 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
490 const PointerType *PTy = dyn_cast<PointerType>(Ty);
491 if (!PTy) return false;
492
493 // Check to see if there are zero elements that we can remove from the
494 // index array. If there are, check to see if removing them causes us to
495 // get to the right type...
496 //
497 vector<ConstPoolVal*> Indices = GEP->getIndices();
498 const Type *BaseType = GEP->getPtrOperand()->getType();
499
500 while (Indices.size() &&
501 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
502 Indices.pop_back();
503 const Type *ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices,
504 true);
505 if (ElTy == PTy->getValueType())
506 return true; // Found a match!!
507 }
508 break; // No match, maybe next time.
509 }
510#endif
511 }
512 return false;
513}
514
515
516
517
518
519
Chris Lattnerb980e182001-11-04 21:32:11 +0000520static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
521 ValueMapCache &VMC);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000522
523// RetValConvertableToType - Return true if it is possible
Chris Lattnerb980e182001-11-04 21:32:11 +0000524static void ConvertUsersType(Value *V, Value *NewVal, ValueMapCache &VMC) {
525
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000526 // It is safe to convert the specified value to the specified type IFF all of
527 // the uses of the value can be converted to accept the new typed value.
528 //
529 while (!V->use_empty()) {
530 unsigned OldSize = V->use_size();
Chris Lattnerb980e182001-11-04 21:32:11 +0000531 ConvertOperandToType(V->use_back(), V, NewVal, VMC);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000532 assert(V->use_size() != OldSize && "Use didn't detatch from value!");
533 }
534}
535
536
537
Chris Lattnerb980e182001-11-04 21:32:11 +0000538static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
539 ValueMapCache &VMC) {
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000540 Instruction *I = cast<Instruction>(U); // Only Instructions convertable
541
542 BasicBlock *BB = I->getParent();
543 BasicBlock::InstListType &BIL = BB->getInstList();
544 string Name = I->getName(); if (!Name.empty()) I->setName("");
545 Instruction *Res; // Result of conversion
546
547 //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl;
548
549 switch (I->getOpcode()) {
550 case Instruction::Cast:
551 assert(I->getOperand(0) == OldVal);
552 Res = new CastInst(NewVal, I->getType(), Name);
553 break;
554
555 case Instruction::Add:
556 case Instruction::Sub:
557 case Instruction::SetEQ:
558 case Instruction::SetNE: {
559 unsigned OtherIdx = (OldVal == I->getOperand(0)) ? 1 : 0;
560 Value *OtherOp = I->getOperand(OtherIdx);
561 Value *NewOther = ConvertExpressionToType(OtherOp, NewVal->getType());
562
563 Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
564 OtherIdx == 0 ? NewOther : NewVal,
565 OtherIdx == 1 ? NewOther : NewVal,
566 Name);
567 break;
568 }
569 case Instruction::Shl:
570 case Instruction::Shr:
571 assert(I->getOperand(0) == OldVal);
572 Res = new ShiftInst(cast<ShiftInst>(I)->getOpcode(), NewVal,
573 I->getOperand(1), Name);
574 break;
575
576 case Instruction::Load:
577 assert(I->getOperand(0) == OldVal);
578 Res = new LoadInst(NewVal, Name);
579 break;
580
581 case Instruction::Store: {
582 if (I->getOperand(0) == OldVal) { // Replace the source value
583 Value *NewPtr =
584 ConvertExpressionToType(I->getOperand(1),
585 PointerType::get(NewVal->getType()));
586 Res = new StoreInst(NewVal, NewPtr);
587 } else { // Replace the source pointer
588 const Type *ValType =cast<PointerType>(NewVal->getType())->getValueType();
589 Value *NewV = ConvertExpressionToType(I->getOperand(0), ValType);
590 Res = new StoreInst(NewV, NewVal);
591 }
592 break;
593 }
594
595#if 0
596 case Instruction::GetElementPtr: {
597 // GetElementPtr's are directly convertable to a pointer type if they have
598 // a number of zeros at the end. Because removing these values does not
599 // change the logical offset of the GEP, it is okay and fair to remove them.
600 // This can change this:
601 // %t1 = getelementptr %Hosp * %hosp, ubyte 4, ubyte 0 ; <%List **>
602 // %t2 = cast %List * * %t1 to %List *
603 // into
604 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
605 //
606 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
607
608 // Check to see if there are zero elements that we can remove from the
609 // index array. If there are, check to see if removing them causes us to
610 // get to the right type...
611 //
612 vector<ConstPoolVal*> Indices = GEP->getIndices();
613 const Type *BaseType = GEP->getPtrOperand()->getType();
614 const Type *PVTy = cast<PointerType>(Ty)->getValueType();
615 Res = 0;
616 while (Indices.size() &&
617 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
618 Indices.pop_back();
619 if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
620 if (Indices.size() == 0) {
621 Res = new CastInst(GEP->getPtrOperand(), BaseType); // NOOP
622 } else {
623 Res = new GetElementPtrInst(GEP->getPtrOperand(), Indices, Name);
624 }
625 break;
626 }
627 }
628 assert(Res && "Didn't find match!");
629 break; // No match, maybe next time.
630 }
631#endif
632
633 default:
634 assert(0 && "Expression convertable, but don't know how to convert?");
635 return;
636 }
637
638 BasicBlock::iterator It = find(BIL.begin(), BIL.end(), I);
639 assert(It != BIL.end() && "Instruction not in own basic block??");
640 BIL.insert(It, Res); // Keep It pointing to old instruction
641
Chris Lattnerb980e182001-11-04 21:32:11 +0000642#if DEBUG_PEEPHOLE_INSTS
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000643 cerr << "In: " << I << "Out: " << Res;
Chris Lattnerb980e182001-11-04 21:32:11 +0000644#endif
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000645
646 //cerr << "RInst: " << Res << "BB After: " << BB << endl << endl;
647
648 if (I->getType() != Res->getType())
Chris Lattnerb980e182001-11-04 21:32:11 +0000649 ConvertUsersType(I, Res, VMC);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000650 else
651 I->replaceAllUsesWith(Res);
652
653 // Now we just need to remove the old instruction so we don't get infinite
654 // loops. Note that we cannot use DCE because DCE won't remove a store
655 // instruction, for example.
656 assert(I->use_size() == 0 && "Uses of Instruction remain!!!");
657
658 It = find(BIL.begin(), BIL.end(), I);
659 assert(It != BIL.end() && "Instruction no longer in basic block??");
660 delete BIL.remove(It);
661}
662
663
664
665
666
667
668
669
670
671
672
673
674
675
Chris Lattnerd32a9612001-11-01 02:42:08 +0000676// DoInsertArrayCast - If the argument value has a pointer type, and if the
677// argument value is used as an array, insert a cast before the specified
678// basic block iterator that casts the value to an array pointer. Return the
679// new cast instruction (in the CastResult var), or null if no cast is inserted.
680//
681static bool DoInsertArrayCast(Method *CurMeth, Value *V, BasicBlock *BB,
682 BasicBlock::iterator &InsertBefore,
683 CastInst *&CastResult) {
684 const PointerType *ThePtrType = dyn_cast<PointerType>(V->getType());
685 if (!ThePtrType) return false;
686 bool InsertCast = false;
687
688 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
689 Instruction *Inst = cast<Instruction>(*I);
690 switch (Inst->getOpcode()) {
691 default: break; // Not an interesting use...
692 case Instruction::Add: // It's being used as an array index!
693 //case Instruction::Sub:
694 InsertCast = true;
695 break;
696 case Instruction::Cast: // There is already a cast instruction!
697 if (const PointerType *PT = dyn_cast<const PointerType>(Inst->getType()))
698 if (const ArrayType *AT = dyn_cast<const ArrayType>(PT->getValueType()))
699 if (AT->getElementType() == ThePtrType->getValueType()) {
700 // Cast already exists! Return the existing one!
701 CastResult = cast<CastInst>(Inst);
702 return false; // No changes made to program though...
703 }
704 break;
705 }
706 }
707
708 if (!InsertCast) return false; // There is no reason to insert a cast!
709
710 // Insert a cast!
711 const Type *ElTy = ThePtrType->getValueType();
712 const PointerType *DestTy = PointerType::get(ArrayType::get(ElTy));
713
714 CastResult = new CastInst(V, DestTy);
715 BB->getInstList().insert(InsertBefore, CastResult);
716 //cerr << "Inserted cast: " << CastResult;
717 return true; // Made a change!
718}
719
720
721// DoInsertArrayCasts - Loop over all "incoming" values in the specified method,
722// inserting a cast for pointer values that are used as arrays. For our
723// purposes, an incoming value is considered to be either a value that is
724// either a method parameter, a value created by alloca or malloc, or a value
725// returned from a function call. All casts are kept attached to their original
726// values through the PtrCasts map.
727//
728static bool DoInsertArrayCasts(Method *M, map<Value*, CastInst*> &PtrCasts) {
729 assert(!M->isExternal() && "Can't handle external methods!");
730
731 // Insert casts for all arguments to the function...
732 bool Changed = false;
733 BasicBlock *CurBB = M->front();
734 BasicBlock::iterator It = CurBB->begin();
735 for (Method::ArgumentListType::iterator AI = M->getArgumentList().begin(),
736 AE = M->getArgumentList().end(); AI != AE; ++AI) {
737 CastInst *TheCast = 0;
738 if (DoInsertArrayCast(M, *AI, CurBB, It, TheCast)) {
739 It = CurBB->begin(); // We might have just invalidated the iterator!
740 Changed = true; // Yes we made a change
741 ++It; // Insert next cast AFTER this one...
742 }
743
744 if (TheCast) // Is there a cast associated with this value?
745 PtrCasts[*AI] = TheCast; // Yes, add it to the map...
746 }
747
748 // TODO: insert casts for alloca, malloc, and function call results. Also,
749 // look for pointers that already have casts, to add to the map.
750
751 return Changed;
752}
753
754
755
756
757// DoElminatePointerArithmetic - Loop over each incoming pointer variable,
758// replacing indexing arithmetic with getelementptr calls.
759//
760static bool DoEliminatePointerArithmetic(const pair<Value*, CastInst*> &Val) {
761 Value *V = Val.first; // The original pointer
762 CastInst *CV = Val.second; // The array casted version of the pointer...
763
764 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
765 Instruction *Inst = cast<Instruction>(*I);
766 if (Inst->getOpcode() != Instruction::Add)
767 continue; // We only care about add instructions
768
769 BinaryOperator *Add = cast<BinaryOperator>(Inst);
770
771 // Make sure the array is the first operand of the add expression...
772 if (Add->getOperand(0) != V)
773 Add->swapOperands();
774
775 // Get the amount added to the pointer value...
776 Value *AddAmount = Add->getOperand(1);
777
778
779 }
780 return false;
781}
782
783
784// Peephole Malloc instructions: we take a look at the use chain of the
785// malloc instruction, and try to find out if the following conditions hold:
786// 1. The malloc is of the form: 'malloc [sbyte], uint <constant>'
Chris Lattnerbacec7b2001-11-04 22:11:10 +0000787// 2. The only users of the malloc are cast & add instructions
Chris Lattnerd32a9612001-11-01 02:42:08 +0000788// 3. Of the cast instructions, there is only one destination pointer type
789// [RTy] where the size of the pointed to object is equal to the number
790// of bytes allocated.
791//
792// If these conditions hold, we convert the malloc to allocate an [RTy]
793// element. This should be extended in the future to handle arrays. TODO
794//
795static bool PeepholeMallocInst(BasicBlock *BB, BasicBlock::iterator &BI) {
796 MallocInst *MI = cast<MallocInst>(*BI);
797 if (!MI->isArrayAllocation()) return false; // No array allocation?
798
799 ConstPoolUInt *Amt = dyn_cast<ConstPoolUInt>(MI->getArraySize());
800 if (Amt == 0 || MI->getAllocatedType() != ArrayType::get(Type::SByteTy))
801 return false;
802
803 // Get the number of bytes allocated...
804 unsigned Size = Amt->getValue();
805 const Type *ResultTy = 0;
806
807 // Loop over all of the uses of the malloc instruction, inspecting casts.
808 for (Value::use_iterator I = MI->use_begin(), E = MI->use_end();
809 I != E; ++I) {
Chris Lattnerbacec7b2001-11-04 22:11:10 +0000810 if (!isa<CastInst>(*I) && !isa<BinaryOperator>(*I)) {
Chris Lattnerd32a9612001-11-01 02:42:08 +0000811 //cerr << "\tnon" << *I;
812 return false; // A non cast user?
813 }
Chris Lattnerbacec7b2001-11-04 22:11:10 +0000814 if (CastInst *CI = dyn_cast<CastInst>(*I)) {
815 //cerr << "\t" << CI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000816
Chris Lattnerbacec7b2001-11-04 22:11:10 +0000817 // We only work on casts to pointer types for sure, be conservative
818 if (!isa<PointerType>(CI->getType())) {
819 cerr << "Found cast of malloc value to non pointer type:\n" << CI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000820 return false;
821 }
Chris Lattnerbacec7b2001-11-04 22:11:10 +0000822
823 const Type *DestTy = cast<PointerType>(CI->getType())->getValueType();
824 if (TD.getTypeSize(DestTy) == Size && DestTy != ResultTy) {
825 // Does the size of the allocated type match the number of bytes
826 // allocated?
827 //
828 if (ResultTy == 0) {
829 ResultTy = DestTy; // Keep note of this for future uses...
830 } else {
831 // It's overdefined! We don't know which type to convert to!
832 return false;
833 }
834 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000835 }
836 }
837
838 // If we get this far, we have either found, or not, a type that is cast to
839 // that is of the same size as the malloc instruction.
840 if (!ResultTy) return false;
841
842 PRINT_PEEPHOLE1("mall-refine:in ", MI);
843 ReplaceInstWithInst(BB->getInstList(), BI,
844 MI = new MallocInst(PointerType::get(ResultTy)));
845 PRINT_PEEPHOLE1("mall-refine:out", MI);
846 return true;
847}
848
849
Chris Lattnerb9693952001-11-04 07:42:17 +0000850// Peephole optimize the following instructions:
851// %t1 = cast int (uint) * %reg111 to uint (...) *
852// %t2 = call uint (...) * %cast111( uint %key )
853//
854// Into: %t3 = call int (uint) * %reg111( uint %key )
855// %t2 = cast int %t3 to uint
856//
857static bool PeepholeCallInst(BasicBlock *BB, BasicBlock::iterator &BI) {
858 CallInst *CI = cast<CallInst>(*BI);
859 return false;
860}
861
Chris Lattnerd32a9612001-11-01 02:42:08 +0000862
863static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
864 Instruction *I = *BI;
Chris Lattnerd32a9612001-11-01 02:42:08 +0000865
866 if (CastInst *CI = dyn_cast<CastInst>(I)) {
867 Value *Src = CI->getOperand(0);
868 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
869 const Type *DestTy = CI->getType();
870
Chris Lattnere99c66b2001-11-01 17:05:27 +0000871 // Peephole optimize the following instruction:
872 // %V2 = cast <ty> %V to <ty>
873 //
874 // Into: <nothing>
875 //
876 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
Chris Lattnerd32a9612001-11-01 02:42:08 +0000877 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
878 CI->replaceAllUsesWith(Src);
879 if (!Src->hasName() && CI->hasName()) {
880 string Name = CI->getName();
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000881 CI->setName("");
882 Src->setName(Name, BB->getParent()->getSymbolTable());
Chris Lattnerd32a9612001-11-01 02:42:08 +0000883 }
884 return true;
885 }
886
Chris Lattnere99c66b2001-11-01 17:05:27 +0000887 // Peephole optimize the following instructions:
888 // %tmp = cast <ty> %V to <ty2>
889 // %V = cast <ty2> %tmp to <ty3> ; Where ty & ty2 are same size
890 //
891 // Into: cast <ty> %V to <ty3>
892 //
Chris Lattnerd32a9612001-11-01 02:42:08 +0000893 if (SrcI)
894 if (CastInst *CSrc = dyn_cast<CastInst>(SrcI))
895 if (isReinterpretingCast(CI) + isReinterpretingCast(CSrc) < 2) {
896 // We can only do c-c elimination if, at most, one cast does a
897 // reinterpretation of the input data.
898 //
899 // If legal, make this cast refer the the original casts argument!
900 //
901 PRINT_PEEPHOLE2("cast-cast:in ", CI, CSrc);
902 CI->setOperand(0, CSrc->getOperand(0));
903 PRINT_PEEPHOLE1("cast-cast:out", CI);
904 return true;
905 }
906
907 // Check to see if it's a cast of an instruction that does not depend on the
908 // specific type of the operands to do it's job.
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000909 if (!isReinterpretingCast(CI)) {
Chris Lattnerb980e182001-11-04 21:32:11 +0000910 ValueTypeCache ConvertedTypes;
911 if (RetValConvertableToType(CI, Src->getType(), ConvertedTypes)) {
Chris Lattnerbacec7b2001-11-04 22:11:10 +0000912 PRINT_PEEPHOLE2("CAST-DEST-EXPR-CONV:in ", CI, Src);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000913
Chris Lattnerb980e182001-11-04 21:32:11 +0000914 ValueMapCache ValueMap;
915 ConvertUsersType(CI, Src, ValueMap);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000916 if (!Src->hasName() && CI->hasName()) {
917 string Name = CI->getName(); CI->setName("");
918 Src->setName(Name, BB->getParent()->getSymbolTable());
919 }
920 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
Chris Lattnerbacec7b2001-11-04 22:11:10 +0000921 PRINT_PEEPHOLE1("CAST-DEST-EXPR-CONV:out", I);
Chris Lattnerf3b976e2001-11-04 20:21:12 +0000922 return true;
923 }
Chris Lattnerd32a9612001-11-01 02:42:08 +0000924 }
925
Chris Lattnere99c66b2001-11-01 17:05:27 +0000926 // Check to see if we are casting from a structure pointer to a pointer to
927 // the first element of the structure... to avoid munching other peepholes,
928 // we only let this happen if there are no add uses of the cast.
929 //
930 // Peephole optimize the following instructions:
931 // %t1 = cast {<...>} * %StructPtr to <ty> *
932 //
933 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
934 // %t1 = cast <eltype> * %t1 to <ty> *
935 //
936 if (const StructType *STy = getPointedToStruct(Src->getType()))
937 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
938
939 // Loop over uses of the cast, checking for add instructions. If an add
940 // exists, this is probably a part of a more complex GEP, so we don't
941 // want to mess around with the cast.
942 //
943 bool HasAddUse = false;
944 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
945 I != E; ++I)
946 if (isa<Instruction>(*I) &&
947 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
948 HasAddUse = true; break;
949 }
950
951 // If it doesn't have an add use, check to see if the dest type is
952 // losslessly convertable to one of the types in the start of the struct
953 // type.
954 //
955 if (!HasAddUse) {
956 const Type *DestPointedTy = DestPTy->getValueType();
957 unsigned Depth = 1;
958 const StructType *CurSTy = STy;
959 const Type *ElTy = 0;
960 while (CurSTy) {
961
962 // Check for a zero element struct type... if we have one, bail.
963 if (CurSTy->getElementTypes().size() == 0) break;
964
965 // Grab the first element of the struct type, which must lie at
966 // offset zero in the struct.
967 //
968 ElTy = CurSTy->getElementTypes()[0];
969
970 // Did we find what we're looking for?
971 if (losslessCastableTypes(ElTy, DestPointedTy)) break;
972
973 // Nope, go a level deeper.
974 ++Depth;
975 CurSTy = dyn_cast<StructType>(ElTy);
976 ElTy = 0;
977 }
978
979 // Did we find what we were looking for? If so, do the transformation
980 if (ElTy) {
981 PRINT_PEEPHOLE1("cast-for-first:in", CI);
982
983 // Build the index vector, full of all zeros
984 vector<ConstPoolVal *> Indices(Depth,
985 ConstPoolUInt::get(Type::UByteTy,0));
986
987 // Insert the new T cast instruction... stealing old T's name
988 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
989 CI->getName());
990 CI->setName("");
991 BI = BB->getInstList().insert(BI, GEP)+1;
992
993 // Make the old cast instruction reference the new GEP instead of
994 // the old src value.
995 //
996 CI->setOperand(0, GEP);
997
998 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
999 return true;
1000 }
1001 }
1002 }
1003
1004
Chris Lattnerd32a9612001-11-01 02:42:08 +00001005 } else if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
1006 if (PeepholeMallocInst(BB, BI)) return true;
Chris Lattner8d38e542001-11-01 03:12:34 +00001007
Chris Lattnerb9693952001-11-04 07:42:17 +00001008 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
1009 if (PeepholeCallInst(BB, BI)) return true;
1010
Chris Lattner8d38e542001-11-01 03:12:34 +00001011 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1012 Value *Val = SI->getOperand(0);
1013 Value *Pointer = SI->getPtrOperand();
1014
Chris Lattnerdedee7b2001-11-01 05:57:59 +00001015 // Peephole optimize the following instructions:
1016 // %t1 = getelementptr {<...>} * %StructPtr, <element indices>
1017 // store <elementty> %v, <elementty> * %t1
1018 //
1019 // Into: store <elementty> %v, {<...>} * %StructPtr, <element indices>
1020 //
Chris Lattner8d38e542001-11-01 03:12:34 +00001021 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Pointer)) {
1022 PRINT_PEEPHOLE2("gep-store:in", GEP, SI);
1023 ReplaceInstWithInst(BB->getInstList(), BI,
1024 SI = new StoreInst(Val, GEP->getPtrOperand(),
Chris Lattner8e7f4092001-11-04 08:08:34 +00001025 GEP->getIndices()));
Chris Lattner8d38e542001-11-01 03:12:34 +00001026 PRINT_PEEPHOLE1("gep-store:out", SI);
1027 return true;
1028 }
Chris Lattnerdedee7b2001-11-01 05:57:59 +00001029
1030 // Peephole optimize the following instructions:
1031 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
1032 // store <T2> %V, <T2>* %t
1033 //
1034 // Into:
1035 // %t = cast <T2> %V to <T1>
1036 // store <T1> %t2, <T1>* %P
1037 //
1038 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
1039 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
1040 if (PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
1041 if (losslessCastableTypes(Val->getType(), // convertable types!
1042 CSPT->getValueType()) &&
1043 !SI->hasIndices()) { // No subscripts yet!
1044 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
1045
1046 // Insert the new T cast instruction... stealing old T's name
1047 CastInst *NCI = new CastInst(Val, CSPT->getValueType(),
1048 CI->getName());
1049 CI->setName("");
1050 BI = BB->getInstList().insert(BI, NCI)+1;
1051
1052 // Replace the old store with a new one!
1053 ReplaceInstWithInst(BB->getInstList(), BI,
1054 SI = new StoreInst(NCI, CastSrc));
1055 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
1056 return true;
1057 }
1058
Chris Lattner8d38e542001-11-01 03:12:34 +00001059
1060 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1061 Value *Pointer = LI->getPtrOperand();
1062
Chris Lattnerdedee7b2001-11-01 05:57:59 +00001063 // Peephole optimize the following instructions:
1064 // %t1 = getelementptr {<...>} * %StructPtr, <element indices>
1065 // %V = load <elementty> * %t1
1066 //
1067 // Into: load {<...>} * %StructPtr, <element indices>
1068 //
Chris Lattner8d38e542001-11-01 03:12:34 +00001069 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Pointer)) {
1070 PRINT_PEEPHOLE2("gep-load:in", GEP, LI);
1071 ReplaceInstWithInst(BB->getInstList(), BI,
1072 LI = new LoadInst(GEP->getPtrOperand(),
Chris Lattner8e7f4092001-11-04 08:08:34 +00001073 GEP->getIndices()));
Chris Lattner8d38e542001-11-01 03:12:34 +00001074 PRINT_PEEPHOLE1("gep-load:out", LI);
1075 return true;
1076 }
Chris Lattnerd32a9612001-11-01 02:42:08 +00001077 } else if (I->getOpcode() == Instruction::Add &&
1078 isa<CastInst>(I->getOperand(1))) {
1079
1080 // Peephole optimize the following instructions:
1081 // %t1 = cast ulong <const int> to {<...>} *
1082 // %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
1083 //
1084 // or
1085 // %t1 = cast {<...>}* %SP to int*
1086 // %t5 = cast ulong <const int> to int*
1087 // %t2 = add int* %t1, %t5 ;; int is same size as field
1088 //
1089 // Into: %t3 = getelementptr {<...>} * %SP, <element indices>
1090 // %t2 = cast <eltype> * %t3 to {<...>}*
1091 //
1092 Value *AddOp1 = I->getOperand(0);
1093 CastInst *AddOp2 = cast<CastInst>(I->getOperand(1));
1094 ConstPoolUInt *OffsetV = dyn_cast<ConstPoolUInt>(AddOp2->getOperand(0));
1095 unsigned Offset = OffsetV ? OffsetV->getValue() : 0;
1096 Value *SrcPtr; // Of type pointer to struct...
1097 const StructType *StructTy;
1098
1099 if ((StructTy = getPointedToStruct(AddOp1->getType()))) {
1100 SrcPtr = AddOp1; // Handle the first case...
1101 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
1102 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
1103 StructTy = getPointedToStruct(SrcPtr->getType());
1104 }
1105
1106 // Only proceed if we have detected all of our conditions successfully...
1107 if (Offset && StructTy && SrcPtr && Offset < TD.getTypeSize(StructTy)) {
1108 const StructLayout *SL = TD.getStructLayout(StructTy);
1109 vector<ConstPoolVal*> Offsets;
1110 unsigned ActualOffset = Offset;
1111 const Type *ElTy = getStructOffsetType(StructTy, ActualOffset, Offsets);
1112
1113 if (getPointedToStruct(AddOp1->getType())) { // case 1
1114 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, I);
1115 } else {
1116 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, I);
1117 }
1118
1119 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Offsets);
1120 BI = BB->getInstList().insert(BI, GEP)+1;
1121
1122 assert(Offset-ActualOffset == 0 &&
1123 "GEP to middle of element not implemented yet!");
1124
1125 ReplaceInstWithInst(BB->getInstList(), BI,
1126 I = new CastInst(GEP, I->getType()));
1127 PRINT_PEEPHOLE2("add-to-gep:out", GEP, I);
1128 return true;
1129 }
1130 }
1131
1132 return false;
1133}
1134
1135
1136
1137
1138static bool DoRaisePass(Method *M) {
1139 bool Changed = false;
1140 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
1141 BasicBlock *BB = *MI;
1142 BasicBlock::InstListType &BIL = BB->getInstList();
1143
1144 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
Chris Lattner68b07b72001-11-01 07:00:51 +00001145 if (opt::DeadCodeElimination::dceInstruction(BIL, BI) ||
1146 PeepholeOptimize(BB, BI))
Chris Lattnerd32a9612001-11-01 02:42:08 +00001147 Changed = true;
1148 else
1149 ++BI;
1150 }
1151 }
1152 return Changed;
1153}
1154
1155
1156// RaisePointerReferences::doit - Raise a method representation to a higher
1157// level.
1158//
1159bool RaisePointerReferences::doit(Method *M) {
1160 if (M->isExternal()) return false;
1161 bool Changed = false;
1162
Chris Lattner68b07b72001-11-01 07:00:51 +00001163#ifdef DEBUG_PEEPHOLE_INSTS
1164 cerr << "\n\n\nStarting to work on Method '" << M->getName() << "'\n";
1165#endif
1166
Chris Lattnerd32a9612001-11-01 02:42:08 +00001167 while (DoRaisePass(M)) Changed = true;
1168
1169 // PtrCasts - Keep a mapping between the pointer values (the key of the
1170 // map), and the cast to array pointer (the value) in this map. This is
1171 // used when converting pointer math into array addressing.
1172 //
1173 map<Value*, CastInst*> PtrCasts;
1174
1175 // Insert casts for all incoming pointer values. Keep track of those casts
1176 // and the identified incoming values in the PtrCasts map.
1177 //
1178 Changed |= DoInsertArrayCasts(M, PtrCasts);
1179
1180 // Loop over each incoming pointer variable, replacing indexing arithmetic
1181 // with getelementptr calls.
1182 //
1183 Changed |= reduce_apply_bool(PtrCasts.begin(), PtrCasts.end(),
1184 ptr_fun(DoEliminatePointerArithmetic));
1185
1186 return Changed;
1187}