Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 1 | //===-- 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" |
| 9 | #include "llvm/Method.h" |
| 10 | #include "llvm/Type.h" |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame^] | 11 | #include "llvm/ConstPoolVals.h" |
Chris Lattner | 59cd9f1 | 2001-11-04 23:24:06 +0000 | [diff] [blame] | 12 | |
| 13 | // TargetData Hack: Eventually we will have annotations given to us by the |
| 14 | // backend so that we know stuff about type size and alignments. For now |
| 15 | // though, just use this, because it happens to match the model that GCC uses. |
| 16 | // |
| 17 | const TargetData TD("LevelRaise: Should be GCC though!"); |
| 18 | |
| 19 | // losslessCastableTypes - Return true if the types are bitwise equivalent. |
| 20 | // This predicate returns true if it is possible to cast from one type to |
| 21 | // another without gaining or losing precision, or altering the bits in any way. |
| 22 | // |
| 23 | bool losslessCastableTypes(const Type *T1, const Type *T2) { |
| 24 | if (!T1->isPrimitiveType() && !T1->isPointerType()) return false; |
| 25 | if (!T2->isPrimitiveType() && !T2->isPointerType()) return false; |
| 26 | |
| 27 | if (T1->getPrimitiveID() == T2->getPrimitiveID()) |
| 28 | return true; // Handles identity cast, and cast of differing pointer types |
| 29 | |
| 30 | // Now we know that they are two differing primitive or pointer types |
| 31 | switch (T1->getPrimitiveID()) { |
| 32 | case Type::UByteTyID: return T2 == Type::SByteTy; |
| 33 | case Type::SByteTyID: return T2 == Type::UByteTy; |
| 34 | case Type::UShortTyID: return T2 == Type::ShortTy; |
| 35 | case Type::ShortTyID: return T2 == Type::UShortTy; |
| 36 | case Type::UIntTyID: return T2 == Type::IntTy; |
| 37 | case Type::IntTyID: return T2 == Type::UIntTy; |
| 38 | case Type::ULongTyID: |
| 39 | case Type::LongTyID: |
| 40 | case Type::PointerTyID: |
| 41 | return T2 == Type::ULongTy || T2 == Type::LongTy || |
| 42 | T2->getPrimitiveID() == Type::PointerTyID; |
| 43 | default: |
| 44 | return false; // Other types have no identity values |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | |
| 49 | // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 50 | // with a value, then remove and delete the original instruction. |
| 51 | // |
| 52 | void ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 53 | BasicBlock::iterator &BI, Value *V) { |
| 54 | Instruction *I = *BI; |
| 55 | // Replaces all of the uses of the instruction with uses of the value |
| 56 | I->replaceAllUsesWith(V); |
| 57 | |
| 58 | // Remove the unneccesary instruction now... |
| 59 | BIL.remove(BI); |
| 60 | |
| 61 | // Make sure to propogate a name if there is one already... |
| 62 | if (I->hasName() && !V->hasName()) |
| 63 | V->setName(I->getName(), BIL.getParent()->getSymbolTable()); |
| 64 | |
| 65 | // Remove the dead instruction now... |
| 66 | delete I; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | // ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 71 | // instruction specified by I. The original instruction is deleted and BI is |
| 72 | // updated to point to the new instruction. |
| 73 | // |
| 74 | void ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 75 | BasicBlock::iterator &BI, Instruction *I) { |
| 76 | assert(I->getParent() == 0 && |
| 77 | "ReplaceInstWithInst: Instruction already inserted into basic block!"); |
| 78 | |
| 79 | // Insert the new instruction into the basic block... |
| 80 | BI = BIL.insert(BI, I)+1; |
| 81 | |
| 82 | // Replace all uses of the old instruction, and delete it. |
| 83 | ReplaceInstWithValue(BIL, BI, I); |
| 84 | |
| 85 | // Reexamine the instruction just inserted next time around the cleanup pass |
| 86 | // loop. |
| 87 | --BI; |
| 88 | } |
| 89 | |
| 90 | |
Chris Lattner | c0b90e7 | 2001-11-08 20:19:56 +0000 | [diff] [blame^] | 91 | // getStructOffsetType - Return a vector of offsets that are to be used to index |
| 92 | // into the specified struct type to get as close as possible to index as we |
| 93 | // can. Note that it is possible that we cannot get exactly to Offset, in which |
| 94 | // case we update offset to be the offset we actually obtained. The resultant |
| 95 | // leaf type is returned. |
| 96 | // |
| 97 | // If StopEarly is set to true (the default), the first object with the |
| 98 | // specified type is returned, even if it is a struct type itself. In this |
| 99 | // case, this routine will not drill down to the leaf type. Set StopEarly to |
| 100 | // false if you want a leaf |
| 101 | // |
| 102 | const Type *getStructOffsetType(const Type *Ty, unsigned &Offset, |
| 103 | vector<ConstPoolVal*> &Offsets, |
| 104 | bool StopEarly = true) { |
| 105 | if (!isa<StructType>(Ty) || (Offset == 0 && StopEarly)) { |
| 106 | Offset = 0; // Return the offset that we were able to acheive |
| 107 | return Ty; // Return the leaf type |
| 108 | } |
| 109 | |
| 110 | assert(Offset < TD.getTypeSize(Ty) && "Offset not in struct!"); |
| 111 | const StructType *STy = cast<StructType>(Ty); |
| 112 | const StructLayout *SL = TD.getStructLayout(STy); |
| 113 | |
| 114 | // This loop terminates always on a 0 <= i < MemberOffsets.size() |
| 115 | unsigned i; |
| 116 | for (i = 0; i < SL->MemberOffsets.size()-1; ++i) |
| 117 | if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1]) |
| 118 | break; |
| 119 | |
| 120 | assert(Offset >= SL->MemberOffsets[i] && |
| 121 | (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1])); |
| 122 | |
| 123 | // Make sure to save the current index... |
| 124 | Offsets.push_back(ConstPoolUInt::get(Type::UByteTy, i)); |
| 125 | |
| 126 | unsigned SubOffs = Offset - SL->MemberOffsets[i]; |
| 127 | const Type *LeafTy = getStructOffsetType(STy->getElementTypes()[i], SubOffs, |
| 128 | Offsets); |
| 129 | Offset = SL->MemberOffsets[i] + SubOffs; |
| 130 | return LeafTy; |
| 131 | } |