e7eef2d21fbc3a832e4e9e9913ac7b04d75ae084
[oota-llvm.git] / lib / Transforms / TransformInternals.cpp
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"
11
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.
15 //
16 const TargetData TD("LevelRaise: Should be GCC though!");
17
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.
21 //
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;
25
26   if (T1->getPrimitiveID() == T2->getPrimitiveID())
27     return true;  // Handles identity cast, and cast of differing pointer types
28
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;
37   case Type::ULongTyID:
38   case Type::LongTyID:
39   case Type::PointerTyID:
40     return T2 == Type::ULongTy || T2 == Type::LongTy ||
41            T2->getPrimitiveID() == Type::PointerTyID;
42   default:
43     return false;  // Other types have no identity values
44   }
45 }
46
47
48 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
49 // with a value, then remove and delete the original instruction.
50 //
51 void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
52                           BasicBlock::iterator &BI, Value *V) {
53   Instruction *I = *BI;
54   // Replaces all of the uses of the instruction with uses of the value
55   I->replaceAllUsesWith(V);
56
57   // Remove the unneccesary instruction now...
58   BIL.remove(BI);
59
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());
63
64   // Remove the dead instruction now...
65   delete I;
66 }
67
68
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.
72 //
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!");
77
78   // Insert the new instruction into the basic block...
79   BI = BIL.insert(BI, I)+1;
80
81   // Replace all uses of the old instruction, and delete it.
82   ReplaceInstWithValue(BIL, BI, I);
83
84   // Reexamine the instruction just inserted next time around the cleanup pass
85   // loop.
86   --BI;
87 }
88
89