1 //===-- TransformInternals.cpp - Implement shared functions for transforms --=//
3 // This file defines shared functions used by the different components of the
6 //===----------------------------------------------------------------------===//
8 #include "TransformInternals.h"
9 #include "llvm/Method.h"
10 #include "llvm/Type.h"
11 #include "llvm/ConstPoolVals.h"
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.
17 const TargetData TD("LevelRaise: Should be GCC though!");
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.
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;
27 if (T1->getPrimitiveID() == T2->getPrimitiveID())
28 return true; // Handles identity cast, and cast of differing pointer types
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;
40 case Type::PointerTyID:
41 return T2 == Type::ULongTy || T2 == Type::LongTy ||
42 T2->getPrimitiveID() == Type::PointerTyID;
44 return false; // Other types have no identity values
49 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
50 // with a value, then remove and delete the original instruction.
52 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
53 BasicBlock::iterator &BI, Value *V) {
55 // Replaces all of the uses of the instruction with uses of the value
56 I->replaceAllUsesWith(V);
58 // Remove the unneccesary instruction now...
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());
65 // Remove the dead instruction now...
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.
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!");
79 // Insert the new instruction into the basic block...
80 BI = BIL.insert(BI, I)+1;
82 // Replace all uses of the old instruction, and delete it.
83 ReplaceInstWithValue(BIL, BI, I);
85 // Reexamine the instruction just inserted next time around the cleanup pass
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.
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
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 && !Offsets.empty())) {
106 Offset = 0; // Return the offset that we were able to acheive
107 return Ty; // Return the leaf type
110 assert(Offset < TD.getTypeSize(Ty) && "Offset not in struct!");
111 const StructType *STy = cast<StructType>(Ty);
112 const StructLayout *SL = TD.getStructLayout(STy);
114 // This loop terminates always on a 0 <= i < MemberOffsets.size()
116 for (i = 0; i < SL->MemberOffsets.size()-1; ++i)
117 if (Offset >= SL->MemberOffsets[i] && Offset < SL->MemberOffsets[i+1])
120 assert(Offset >= SL->MemberOffsets[i] &&
121 (i == SL->MemberOffsets.size()-1 || Offset < SL->MemberOffsets[i+1]));
123 // Make sure to save the current index...
124 Offsets.push_back(ConstPoolUInt::get(Type::UByteTy, i));
126 unsigned SubOffs = Offset - SL->MemberOffsets[i];
127 const Type *LeafTy = getStructOffsetType(STy->getElementTypes()[i], SubOffs,
129 Offset = SL->MemberOffsets[i] + SubOffs;