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"
12 // TargetData Hack: Eventually we will have annotations given to us by the
13 // backend so that we know stuff about type size and alignments. For now
14 // though, just use this, because it happens to match the model that GCC uses.
16 const TargetData TD("LevelRaise: Should be GCC though!");
18 // losslessCastableTypes - Return true if the types are bitwise equivalent.
19 // This predicate returns true if it is possible to cast from one type to
20 // another without gaining or losing precision, or altering the bits in any way.
22 bool losslessCastableTypes(const Type *T1, const Type *T2) {
23 if (!T1->isPrimitiveType() && !T1->isPointerType()) return false;
24 if (!T2->isPrimitiveType() && !T2->isPointerType()) return false;
26 if (T1->getPrimitiveID() == T2->getPrimitiveID())
27 return true; // Handles identity cast, and cast of differing pointer types
29 // Now we know that they are two differing primitive or pointer types
30 switch (T1->getPrimitiveID()) {
31 case Type::UByteTyID: return T2 == Type::SByteTy;
32 case Type::SByteTyID: return T2 == Type::UByteTy;
33 case Type::UShortTyID: return T2 == Type::ShortTy;
34 case Type::ShortTyID: return T2 == Type::UShortTy;
35 case Type::UIntTyID: return T2 == Type::IntTy;
36 case Type::IntTyID: return T2 == Type::UIntTy;
39 case Type::PointerTyID:
40 return T2 == Type::ULongTy || T2 == Type::LongTy ||
41 T2->getPrimitiveID() == Type::PointerTyID;
43 return false; // Other types have no identity values
48 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
49 // with a value, then remove and delete the original instruction.
51 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
52 BasicBlock::iterator &BI, Value *V) {
54 // Replaces all of the uses of the instruction with uses of the value
55 I->replaceAllUsesWith(V);
57 // Remove the unneccesary instruction now...
60 // Make sure to propogate a name if there is one already...
61 if (I->hasName() && !V->hasName())
62 V->setName(I->getName(), BIL.getParent()->getSymbolTable());
64 // Remove the dead instruction now...
69 // ReplaceInstWithInst - Replace the instruction specified by BI with the
70 // instruction specified by I. The original instruction is deleted and BI is
71 // updated to point to the new instruction.
73 void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
74 BasicBlock::iterator &BI, Instruction *I) {
75 assert(I->getParent() == 0 &&
76 "ReplaceInstWithInst: Instruction already inserted into basic block!");
78 // Insert the new instruction into the basic block...
79 BI = BIL.insert(BI, I)+1;
81 // Replace all uses of the old instruction, and delete it.
82 ReplaceInstWithValue(BIL, BI, I);
84 // Reexamine the instruction just inserted next time around the cleanup pass