1 //===- LevelRaise.cpp - Code to change LLVM to higher level -----------------=//
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.
7 // Eliminate silly things in the source that do not effect the level, but do
10 // - getelementptr/load & getelementptr/store are folded into a direct
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
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.
30 //===----------------------------------------------------------------------===//
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"
39 #include "llvm/Optimizations/ConstantHandling.h"
40 #include "llvm/Optimizations/DCE.h"
44 #include "llvm/Assembly/Writer.h"
46 //#define DEBUG_PEEPHOLE_INSTS 1
48 #ifdef DEBUG_PEEPHOLE_INSTS
49 #define PRINT_PEEPHOLE(ID, NUM, I) \
50 cerr << "Inst P/H " << ID << "[" << NUM << "] " << I;
52 #define PRINT_PEEPHOLE(ID, NUM, I)
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)
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.
67 const TargetData TD("LevelRaise: Should be GCC though!");
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.
74 static bool losslessCastableTypes(const Type *T1, const Type *T2) {
75 if (!T1->isPrimitiveType() && !isa<PointerType>(T1)) return false;
76 if (!T2->isPrimitiveType() && !isa<PointerType>(T2)) return false;
78 if (T1->getPrimitiveID() == T2->getPrimitiveID())
79 return true; // Handles identity cast, and cast of differing pointer types
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;
91 case Type::PointerTyID:
92 return T2 == Type::ULongTy || T2 == Type::LongTy ||
93 T2->getPrimitiveID() == Type::PointerTyID;
95 return false; // Other types have no identity values
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.
104 static inline bool isReinterpretingCast(const CastInst *CI) {
105 return !losslessCastableTypes(CI->getOperand(0)->getType(), CI->getType());
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.
112 static const StructType *getPointedToStruct(const Type *Ty) {
113 const PointerType *PT = dyn_cast<PointerType>(Ty);
114 return PT ? dyn_cast<StructType>(PT->getValueType()) : 0;
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.
124 static 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
131 assert(Offset < TD.getTypeSize(Ty) && "Offset not in struct!");
132 const StructType *STy = cast<StructType>(Ty);
133 const StructLayout *SL = TD.getStructLayout(STy);
135 // This loop terminates always on a 0 <= i < MemberOffsets.size()
137 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
138 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
141 assert(Offset >= SL->MemberOffsets[i] &&
142 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
144 // Make sure to save the current index...
145 Offsets.push_back(ConstPoolUInt::get(Type::UByteTy, i));
147 unsigned SubOffs = Offset - SL->MemberOffsets[i];
148 const Type *LeafTy = getStructOffsetType(STy->getElementTypes()[i], SubOffs,
150 Offset = SL->MemberOffsets[i] + SubOffs;
156 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
157 // with a value, then remove and delete the original instruction.
159 static 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);
165 // Remove the unneccesary instruction now...
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());
172 // Remove the dead instruction now...
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.
181 static void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
182 BasicBlock::iterator &BI, Instruction *I) {
183 assert(I->getParent() == 0 &&
184 "ReplaceInstWithInst: Instruction already inserted into basic block!");
186 // Insert the new instruction into the basic block...
187 BI = BIL.insert(BI, I)+1;
189 // Replace all uses of the old instruction, and delete it.
190 ReplaceInstWithValue(BIL, BI, I);
192 // Reexamine the instruction just inserted next time around the cleanup pass
199 typedef map<const Value*, const Type*> ValueTypeCache;
200 typedef map<const Value*, Value*> ValueMapCache;
204 // ExpressionConvertableToType - Return true if it is possible
205 static bool ExpressionConvertableToType(Value *V, const Type *Ty) {
206 Instruction *I = dyn_cast<Instruction>(V);
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).
212 if (isa<ConstPoolVal>(V) &&
213 !isa<PointerType>(V->getType()) && !isa<PointerType>(Ty)) return true;
215 return false; // Otherwise, we can't convert!
217 if (I->getType() == Ty) return false; // Expression already correct type!
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());
225 case Instruction::Add:
226 case Instruction::Sub:
227 return ExpressionConvertableToType(I->getOperand(0), Ty) &&
228 ExpressionConvertableToType(I->getOperand(1), Ty);
229 case Instruction::Shr:
230 if (Ty->isSigned() != V->getType()->isSigned()) return false;
232 case Instruction::Shl:
233 return ExpressionConvertableToType(I->getOperand(0), Ty);
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));
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 *
249 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
251 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
252 const PointerType *PTy = dyn_cast<PointerType>(Ty);
253 if (!PTy) return false;
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...
259 vector<ConstPoolVal*> Indices = GEP->getIndices();
260 const Type *BaseType = GEP->getPtrOperand()->getType();
262 while (Indices.size() &&
263 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
265 const Type *ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices,
267 if (ElTy == PTy->getValueType())
268 return true; // Found a match!!
270 break; // No match, maybe next time.
277 static Value *ConvertExpressionToType(Value *V, const Type *Ty) {
278 assert(ExpressionConvertableToType(V, Ty) && "Value is not convertable!");
279 Instruction *I = dyn_cast<Instruction>(V);
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!!!");
291 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
296 //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl;
298 switch (I->getOpcode()) {
299 case Instruction::Cast:
300 Res = new CastInst(I->getOperand(0), Ty, Name);
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),
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);
318 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);
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 *
334 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
336 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
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...
342 vector<ConstPoolVal*> Indices = GEP->getIndices();
343 const Type *BaseType = GEP->getPtrOperand()->getType();
344 const Type *PVTy = cast<PointerType>(Ty)->getValueType();
346 while (Indices.size() &&
347 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
349 if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
350 if (Indices.size() == 0) {
351 Res = new CastInst(GEP->getPtrOperand(), BaseType); // NOOP
353 Res = new GetElementPtrInst(GEP->getPtrOperand(), Indices, Name);
358 assert(Res && "Didn't find match!");
359 break; // No match, maybe next time.
363 assert(0 && "Expression convertable, but don't know how to convert?");
367 BasicBlock::iterator It = find(BIL.begin(), BIL.end(), I);
368 assert(It != BIL.end() && "Instruction not in own basic block??");
371 //cerr << "RInst: " << Res << "BB After: " << BB << endl << endl;
376 static inline const Type *getTy(const Value *V, ValueTypeCache &CT) {
377 ValueTypeCache::iterator I = CT.find(V);
378 if (I == CT.end()) return V->getType();
383 static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
384 ValueTypeCache &ConvertedTypes);
386 // RetValConvertableToType - Return true if it is possible
387 static 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;
393 // 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.
396 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
397 if (!OperandConvertableToType(*I, V, Ty, ConvertedTypes))
404 // 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.
410 static bool OperandConvertableToType(User *U, Value *V, const Type *Ty,
411 ValueTypeCache &CTMap) {
412 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!
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());
424 case Instruction::Add:
425 case Instruction::Sub: {
426 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
427 return RetValConvertableToType(I, Ty, CTMap) &&
428 ExpressionConvertableToType(OtherOp, Ty);
430 case Instruction::SetEQ:
431 case Instruction::SetNE: {
432 Value *OtherOp = I->getOperand((V == I->getOperand(0)) ? 1 : 0);
433 return ExpressionConvertableToType(OtherOp, Ty);
435 case Instruction::Shr:
436 if (Ty->isSigned() != V->getType()->isSigned()) return false;
438 case Instruction::Shl:
439 assert(I->getOperand(0) == V);
440 return RetValConvertableToType(I, Ty, CTMap);
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()))
450 return RetValConvertableToType(LI, PT->getValueType(), CTMap);
454 case Instruction::Store: {
455 StoreInst *SI = cast<StoreInst>(I);
456 if (SI->hasIndices()) return false;
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)) {
463 if (isa<ArrayType>(PT->getValueType()))
464 return false; // Avoid getDataSize on unsized array type!
465 assert(V == I->getOperand(1));
467 // Must move the same amount of data...
468 if (TD.getTypeSize(PT->getValueType()) !=
469 TD.getTypeSize(I->getOperand(0)->getType())) return false;
471 // Can convert store if the incoming value is convertable...
472 return ExpressionConvertableToType(I->getOperand(0), PT->getValueType());
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 *
487 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
489 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
490 const PointerType *PTy = dyn_cast<PointerType>(Ty);
491 if (!PTy) return false;
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...
497 vector<ConstPoolVal*> Indices = GEP->getIndices();
498 const Type *BaseType = GEP->getPtrOperand()->getType();
500 while (Indices.size() &&
501 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
503 const Type *ElTy = GetElementPtrInst::getIndexedType(BaseType, Indices,
505 if (ElTy == PTy->getValueType())
506 return true; // Found a match!!
508 break; // No match, maybe next time.
520 static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
523 // RetValConvertableToType - Return true if it is possible
524 static void ConvertUsersType(Value *V, Value *NewVal, ValueMapCache &VMC) {
526 // 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.
529 while (!V->use_empty()) {
530 unsigned OldSize = V->use_size();
531 ConvertOperandToType(V->use_back(), V, NewVal, VMC);
532 assert(V->use_size() != OldSize && "Use didn't detatch from value!");
538 static void ConvertOperandToType(User *U, Value *OldVal, Value *NewVal,
539 ValueMapCache &VMC) {
540 Instruction *I = cast<Instruction>(U); // Only Instructions convertable
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
547 //cerr << endl << endl << "Type:\t" << Ty << "\nInst: " << I << "BB Before: " << BB << endl;
549 switch (I->getOpcode()) {
550 case Instruction::Cast:
551 assert(I->getOperand(0) == OldVal);
552 Res = new CastInst(NewVal, I->getType(), Name);
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());
563 Res = BinaryOperator::create(cast<BinaryOperator>(I)->getOpcode(),
564 OtherIdx == 0 ? NewOther : NewVal,
565 OtherIdx == 1 ? NewOther : NewVal,
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);
576 case Instruction::Load:
577 assert(I->getOperand(0) == OldVal);
578 Res = new LoadInst(NewVal, Name);
581 case Instruction::Store: {
582 if (I->getOperand(0) == OldVal) { // Replace the source value
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);
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 *
604 // %t2 = getelementptr %Hosp * %hosp, ubyte 4 ; <%List *>
606 GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
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...
612 vector<ConstPoolVal*> Indices = GEP->getIndices();
613 const Type *BaseType = GEP->getPtrOperand()->getType();
614 const Type *PVTy = cast<PointerType>(Ty)->getValueType();
616 while (Indices.size() &&
617 cast<ConstPoolUInt>(Indices.back())->getValue() == 0) {
619 if (GetElementPtrInst::getIndexedType(BaseType, Indices, true) == PVTy) {
620 if (Indices.size() == 0) {
621 Res = new CastInst(GEP->getPtrOperand(), BaseType); // NOOP
623 Res = new GetElementPtrInst(GEP->getPtrOperand(), Indices, Name);
628 assert(Res && "Didn't find match!");
629 break; // No match, maybe next time.
634 assert(0 && "Expression convertable, but don't know how to convert?");
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
642 #if DEBUG_PEEPHOLE_INSTS
643 cerr << "In: " << I << "Out: " << Res;
646 //cerr << "RInst: " << Res << "BB After: " << BB << endl << endl;
648 if (I->getType() != Res->getType())
649 ConvertUsersType(I, Res, VMC);
651 I->replaceAllUsesWith(Res);
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!!!");
658 It = find(BIL.begin(), BIL.end(), I);
659 assert(It != BIL.end() && "Instruction no longer in basic block??");
660 delete BIL.remove(It);
676 // 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.
681 static 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;
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:
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...
708 if (!InsertCast) return false; // There is no reason to insert a cast!
711 const Type *ElTy = ThePtrType->getValueType();
712 const PointerType *DestTy = PointerType::get(ArrayType::get(ElTy));
714 CastResult = new CastInst(V, DestTy);
715 BB->getInstList().insert(InsertBefore, CastResult);
716 //cerr << "Inserted cast: " << CastResult;
717 return true; // Made a change!
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.
728 static bool DoInsertArrayCasts(Method *M, map<Value*, CastInst*> &PtrCasts) {
729 assert(!M->isExternal() && "Can't handle external methods!");
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...
744 if (TheCast) // Is there a cast associated with this value?
745 PtrCasts[*AI] = TheCast; // Yes, add it to the map...
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.
757 // DoElminatePointerArithmetic - Loop over each incoming pointer variable,
758 // replacing indexing arithmetic with getelementptr calls.
760 static 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...
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
769 BinaryOperator *Add = cast<BinaryOperator>(Inst);
771 // Make sure the array is the first operand of the add expression...
772 if (Add->getOperand(0) != V)
775 // Get the amount added to the pointer value...
776 Value *AddAmount = Add->getOperand(1);
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>'
787 // 2. The only users of the malloc are cast instructions
788 // 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.
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
795 static bool PeepholeMallocInst(BasicBlock *BB, BasicBlock::iterator &BI) {
796 MallocInst *MI = cast<MallocInst>(*BI);
797 if (!MI->isArrayAllocation()) return false; // No array allocation?
799 ConstPoolUInt *Amt = dyn_cast<ConstPoolUInt>(MI->getArraySize());
800 if (Amt == 0 || MI->getAllocatedType() != ArrayType::get(Type::SByteTy))
803 // Get the number of bytes allocated...
804 unsigned Size = Amt->getValue();
805 const Type *ResultTy = 0;
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();
810 if (!isa<CastInst>(*I)) {
811 //cerr << "\tnon" << *I;
812 return false; // A non cast user?
814 CastInst *CI = cast<CastInst>(*I);
815 //cerr << "\t" << CI;
817 // 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;
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
829 ResultTy = DestTy; // Keep note of this for future uses...
831 // It's overdefined! We don't know which type to convert to!
837 // If we get this far, we have either found, or not, a type that is cast to
838 // that is of the same size as the malloc instruction.
839 if (!ResultTy) return false;
841 PRINT_PEEPHOLE1("mall-refine:in ", MI);
842 ReplaceInstWithInst(BB->getInstList(), BI,
843 MI = new MallocInst(PointerType::get(ResultTy)));
844 PRINT_PEEPHOLE1("mall-refine:out", MI);
849 // Peephole optimize the following instructions:
850 // %t1 = cast int (uint) * %reg111 to uint (...) *
851 // %t2 = call uint (...) * %cast111( uint %key )
853 // Into: %t3 = call int (uint) * %reg111( uint %key )
854 // %t2 = cast int %t3 to uint
856 static bool PeepholeCallInst(BasicBlock *BB, BasicBlock::iterator &BI) {
857 CallInst *CI = cast<CallInst>(*BI);
862 static bool PeepholeOptimize(BasicBlock *BB, BasicBlock::iterator &BI) {
863 Instruction *I = *BI;
865 if (CastInst *CI = dyn_cast<CastInst>(I)) {
866 Value *Src = CI->getOperand(0);
867 Instruction *SrcI = dyn_cast<Instruction>(Src); // Nonnull if instr source
868 const Type *DestTy = CI->getType();
870 // Peephole optimize the following instruction:
871 // %V2 = cast <ty> %V to <ty>
875 if (DestTy == Src->getType()) { // Check for a cast to same type as src!!
876 PRINT_PEEPHOLE1("cast-of-self-ty", CI);
877 CI->replaceAllUsesWith(Src);
878 if (!Src->hasName() && CI->hasName()) {
879 string Name = CI->getName();
881 Src->setName(Name, BB->getParent()->getSymbolTable());
886 // Peephole optimize the following instructions:
887 // %tmp = cast <ty> %V to <ty2>
888 // %V = cast <ty2> %tmp to <ty3> ; Where ty & ty2 are same size
890 // Into: cast <ty> %V to <ty3>
893 if (CastInst *CSrc = dyn_cast<CastInst>(SrcI))
894 if (isReinterpretingCast(CI) + isReinterpretingCast(CSrc) < 2) {
895 // We can only do c-c elimination if, at most, one cast does a
896 // reinterpretation of the input data.
898 // If legal, make this cast refer the the original casts argument!
900 PRINT_PEEPHOLE2("cast-cast:in ", CI, CSrc);
901 CI->setOperand(0, CSrc->getOperand(0));
902 PRINT_PEEPHOLE1("cast-cast:out", CI);
906 // Check to see if it's a cast of an instruction that does not depend on the
907 // specific type of the operands to do it's job.
908 if (!isReinterpretingCast(CI)) {
909 ValueTypeCache ConvertedTypes;
910 if (RetValConvertableToType(CI, Src->getType(), ConvertedTypes)) {
911 PRINT_PEEPHOLE2("EXPR-CONV:in ", CI, Src);
913 ValueMapCache ValueMap;
914 ConvertUsersType(CI, Src, ValueMap);
915 if (!Src->hasName() && CI->hasName()) {
916 string Name = CI->getName(); CI->setName("");
917 Src->setName(Name, BB->getParent()->getSymbolTable());
919 BI = BB->begin(); // Rescan basic block. BI might be invalidated.
920 PRINT_PEEPHOLE1("EXPR-CONV:out", I);
925 // Check to see if we are casting from a structure pointer to a pointer to
926 // the first element of the structure... to avoid munching other peepholes,
927 // we only let this happen if there are no add uses of the cast.
929 // Peephole optimize the following instructions:
930 // %t1 = cast {<...>} * %StructPtr to <ty> *
932 // Into: %t2 = getelementptr {<...>} * %StructPtr, <0, 0, 0, ...>
933 // %t1 = cast <eltype> * %t1 to <ty> *
935 if (const StructType *STy = getPointedToStruct(Src->getType()))
936 if (const PointerType *DestPTy = dyn_cast<PointerType>(DestTy)) {
938 // Loop over uses of the cast, checking for add instructions. If an add
939 // exists, this is probably a part of a more complex GEP, so we don't
940 // want to mess around with the cast.
942 bool HasAddUse = false;
943 for (Value::use_iterator I = CI->use_begin(), E = CI->use_end();
945 if (isa<Instruction>(*I) &&
946 cast<Instruction>(*I)->getOpcode() == Instruction::Add) {
947 HasAddUse = true; break;
950 // If it doesn't have an add use, check to see if the dest type is
951 // losslessly convertable to one of the types in the start of the struct
955 const Type *DestPointedTy = DestPTy->getValueType();
957 const StructType *CurSTy = STy;
958 const Type *ElTy = 0;
961 // Check for a zero element struct type... if we have one, bail.
962 if (CurSTy->getElementTypes().size() == 0) break;
964 // Grab the first element of the struct type, which must lie at
965 // offset zero in the struct.
967 ElTy = CurSTy->getElementTypes()[0];
969 // Did we find what we're looking for?
970 if (losslessCastableTypes(ElTy, DestPointedTy)) break;
972 // Nope, go a level deeper.
974 CurSTy = dyn_cast<StructType>(ElTy);
978 // Did we find what we were looking for? If so, do the transformation
980 PRINT_PEEPHOLE1("cast-for-first:in", CI);
982 // Build the index vector, full of all zeros
983 vector<ConstPoolVal *> Indices(Depth,
984 ConstPoolUInt::get(Type::UByteTy,0));
986 // Insert the new T cast instruction... stealing old T's name
987 GetElementPtrInst *GEP = new GetElementPtrInst(Src, Indices,
990 BI = BB->getInstList().insert(BI, GEP)+1;
992 // Make the old cast instruction reference the new GEP instead of
993 // the old src value.
995 CI->setOperand(0, GEP);
997 PRINT_PEEPHOLE2("cast-for-first:out", GEP, CI);
1004 } else if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
1005 if (PeepholeMallocInst(BB, BI)) return true;
1007 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
1008 if (PeepholeCallInst(BB, BI)) return true;
1010 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1011 Value *Val = SI->getOperand(0);
1012 Value *Pointer = SI->getPtrOperand();
1014 // Peephole optimize the following instructions:
1015 // %t1 = getelementptr {<...>} * %StructPtr, <element indices>
1016 // store <elementty> %v, <elementty> * %t1
1018 // Into: store <elementty> %v, {<...>} * %StructPtr, <element indices>
1020 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Pointer)) {
1021 PRINT_PEEPHOLE2("gep-store:in", GEP, SI);
1022 ReplaceInstWithInst(BB->getInstList(), BI,
1023 SI = new StoreInst(Val, GEP->getPtrOperand(),
1024 GEP->getIndices()));
1025 PRINT_PEEPHOLE1("gep-store:out", SI);
1029 // Peephole optimize the following instructions:
1030 // %t = cast <T1>* %P to <T2> * ;; If T1 is losslessly convertable to T2
1031 // store <T2> %V, <T2>* %t
1034 // %t = cast <T2> %V to <T1>
1035 // store <T1> %t2, <T1>* %P
1037 if (CastInst *CI = dyn_cast<CastInst>(Pointer))
1038 if (Value *CastSrc = CI->getOperand(0)) // CSPT = CastSrcPointerType
1039 if (PointerType *CSPT = dyn_cast<PointerType>(CastSrc->getType()))
1040 if (losslessCastableTypes(Val->getType(), // convertable types!
1041 CSPT->getValueType()) &&
1042 !SI->hasIndices()) { // No subscripts yet!
1043 PRINT_PEEPHOLE3("st-src-cast:in ", Pointer, Val, SI);
1045 // Insert the new T cast instruction... stealing old T's name
1046 CastInst *NCI = new CastInst(Val, CSPT->getValueType(),
1049 BI = BB->getInstList().insert(BI, NCI)+1;
1051 // Replace the old store with a new one!
1052 ReplaceInstWithInst(BB->getInstList(), BI,
1053 SI = new StoreInst(NCI, CastSrc));
1054 PRINT_PEEPHOLE3("st-src-cast:out", NCI, CastSrc, SI);
1059 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1060 Value *Pointer = LI->getPtrOperand();
1062 // Peephole optimize the following instructions:
1063 // %t1 = getelementptr {<...>} * %StructPtr, <element indices>
1064 // %V = load <elementty> * %t1
1066 // Into: load {<...>} * %StructPtr, <element indices>
1068 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Pointer)) {
1069 PRINT_PEEPHOLE2("gep-load:in", GEP, LI);
1070 ReplaceInstWithInst(BB->getInstList(), BI,
1071 LI = new LoadInst(GEP->getPtrOperand(),
1072 GEP->getIndices()));
1073 PRINT_PEEPHOLE1("gep-load:out", LI);
1076 } else if (I->getOpcode() == Instruction::Add &&
1077 isa<CastInst>(I->getOperand(1))) {
1079 // Peephole optimize the following instructions:
1080 // %t1 = cast ulong <const int> to {<...>} *
1081 // %t2 = add {<...>} * %SP, %t1 ;; Constant must be 2nd operand
1084 // %t1 = cast {<...>}* %SP to int*
1085 // %t5 = cast ulong <const int> to int*
1086 // %t2 = add int* %t1, %t5 ;; int is same size as field
1088 // Into: %t3 = getelementptr {<...>} * %SP, <element indices>
1089 // %t2 = cast <eltype> * %t3 to {<...>}*
1091 Value *AddOp1 = I->getOperand(0);
1092 CastInst *AddOp2 = cast<CastInst>(I->getOperand(1));
1093 ConstPoolUInt *OffsetV = dyn_cast<ConstPoolUInt>(AddOp2->getOperand(0));
1094 unsigned Offset = OffsetV ? OffsetV->getValue() : 0;
1095 Value *SrcPtr; // Of type pointer to struct...
1096 const StructType *StructTy;
1098 if ((StructTy = getPointedToStruct(AddOp1->getType()))) {
1099 SrcPtr = AddOp1; // Handle the first case...
1100 } else if (CastInst *AddOp1c = dyn_cast<CastInst>(AddOp1)) {
1101 SrcPtr = AddOp1c->getOperand(0); // Handle the second case...
1102 StructTy = getPointedToStruct(SrcPtr->getType());
1105 // Only proceed if we have detected all of our conditions successfully...
1106 if (Offset && StructTy && SrcPtr && Offset < TD.getTypeSize(StructTy)) {
1107 const StructLayout *SL = TD.getStructLayout(StructTy);
1108 vector<ConstPoolVal*> Offsets;
1109 unsigned ActualOffset = Offset;
1110 const Type *ElTy = getStructOffsetType(StructTy, ActualOffset, Offsets);
1112 if (getPointedToStruct(AddOp1->getType())) { // case 1
1113 PRINT_PEEPHOLE2("add-to-gep1:in", AddOp2, I);
1115 PRINT_PEEPHOLE3("add-to-gep2:in", AddOp1, AddOp2, I);
1118 GetElementPtrInst *GEP = new GetElementPtrInst(SrcPtr, Offsets);
1119 BI = BB->getInstList().insert(BI, GEP)+1;
1121 assert(Offset-ActualOffset == 0 &&
1122 "GEP to middle of element not implemented yet!");
1124 ReplaceInstWithInst(BB->getInstList(), BI,
1125 I = new CastInst(GEP, I->getType()));
1126 PRINT_PEEPHOLE2("add-to-gep:out", GEP, I);
1137 static bool DoRaisePass(Method *M) {
1138 bool Changed = false;
1139 for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
1140 BasicBlock *BB = *MI;
1141 BasicBlock::InstListType &BIL = BB->getInstList();
1143 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
1144 if (opt::DeadCodeElimination::dceInstruction(BIL, BI) ||
1145 PeepholeOptimize(BB, BI))
1155 // RaisePointerReferences::doit - Raise a method representation to a higher
1158 bool RaisePointerReferences::doit(Method *M) {
1159 if (M->isExternal()) return false;
1160 bool Changed = false;
1162 #ifdef DEBUG_PEEPHOLE_INSTS
1163 cerr << "\n\n\nStarting to work on Method '" << M->getName() << "'\n";
1166 while (DoRaisePass(M)) Changed = true;
1168 // PtrCasts - Keep a mapping between the pointer values (the key of the
1169 // map), and the cast to array pointer (the value) in this map. This is
1170 // used when converting pointer math into array addressing.
1172 map<Value*, CastInst*> PtrCasts;
1174 // Insert casts for all incoming pointer values. Keep track of those casts
1175 // and the identified incoming values in the PtrCasts map.
1177 Changed |= DoInsertArrayCasts(M, PtrCasts);
1179 // Loop over each incoming pointer variable, replacing indexing arithmetic
1180 // with getelementptr calls.
1182 Changed |= reduce_apply_bool(PtrCasts.begin(), PtrCasts.end(),
1183 ptr_fun(DoEliminatePointerArithmetic));