Propogate name to the malloc itself instead of to the cast
[oota-llvm.git] / lib / Transforms / IPO / DeadTypeElimination.cpp
1 //===- CleanupGCCOutput.cpp - Cleanup GCC Output ----------------------------=//
2 //
3 // This pass is used to cleanup the output of GCC.  GCC's output is
4 // unneccessarily gross for a couple of reasons. This pass does the following
5 // things to try to clean it up:
6 //
7 // * Eliminate names for GCC types that we know can't be needed by the user.
8 // - Eliminate names for types that are unused in the entire translation unit
9 //    but only if they do not name a structure type!
10 // - Replace calls to 'sbyte *%malloc(uint)' and 'void %free(sbyte *)' with
11 //   malloc and free instructions.
12 //
13 // Note:  This code produces dead declarations, it is a good idea to run DCE
14 //        after this pass.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "llvm/Transforms/CleanupGCCOutput.h"
19 #include "llvm/SymbolTable.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/iOther.h"
22 #include "llvm/iMemory.h"
23
24 static const Type *PtrArrSByte = 0; // '[sbyte]*' type
25 static const Type *PtrSByte = 0;    // 'sbyte*' type
26
27
28 static inline bool ShouldNukeSymtabEntry(const pair<string, Value*> &E) {
29   // Nuke all names for primitive types!
30   if (cast<Type>(E.second)->isPrimitiveType()) return true;
31
32   // The only types that could contain .'s in the program are things generated
33   // by GCC itself, including "complex.float" and friends.  Nuke them too.
34   if (E.first.find('.') != string::npos) return true;
35
36   return false;
37 }
38
39
40 // doPassInitialization - For this pass, it removes global symbol table
41 // entries for primitive types.  These are never used for linking in GCC and
42 // they make the output uglier to look at, so we nuke them.
43 //
44 bool CleanupGCCOutput::doPassInitialization(Module *M) {
45   bool Changed = false;
46
47   if (PtrArrSByte == 0) {
48     PtrArrSByte = PointerType::get(ArrayType::get(Type::SByteTy));
49     PtrSByte    = PointerType::get(Type::SByteTy);
50   }
51
52   if (M->hasSymbolTable()) {
53     SymbolTable *ST = M->getSymbolTable();
54
55     // Lookup %malloc and %free in the symbol table, for later use.  If they
56     // don't exist, or are not external, we do not worry about converting calls
57     // to that function into the appropriate instruction.
58     //
59     const PointerType *MallocType =   // Get the type for malloc
60       PointerType::get(MethodType::get(PointerType::get(Type::SByteTy),
61                                   vector<const Type*>(1, Type::UIntTy), false));
62     Malloc = cast_or_null<Method>(ST->lookup(MallocType, "malloc"));
63     if (Malloc && !Malloc->isExternal())
64       Malloc = 0;  // Don't mess with locally defined versions of the fn
65
66     const PointerType *FreeType =     // Get the type for free
67       PointerType::get(MethodType::get(Type::VoidTy,
68                vector<const Type*>(1, PointerType::get(Type::SByteTy)), false));
69     Free = cast_or_null<Method>(ST->lookup(FreeType, "free"));
70     if (Free && !Free->isExternal())
71       Free = 0;  // Don't mess with locally defined versions of the fn
72     
73
74     // Check the symbol table for superfluous type entries...
75     //
76     // Grab the 'type' plane of the module symbol...
77     SymbolTable::iterator STI = ST->find(Type::TypeTy);
78     if (STI != ST->end()) {
79       // Loop over all entries in the type plane...
80       SymbolTable::VarMap &Plane = STI->second;
81       for (SymbolTable::VarMap::iterator PI = Plane.begin(); PI != Plane.end();)
82         if (ShouldNukeSymtabEntry(*PI)) {    // Should we remove this entry?
83 #if MAP_IS_NOT_BRAINDEAD
84           PI = Plane.erase(PI);     // STD C++ Map should support this!
85 #else
86           Plane.erase(PI);          // Alas, GCC 2.95.3 doesn't  *SIGH*
87           PI = Plane.begin();
88 #endif
89           Changed = true;
90         } else {
91           ++PI;
92         }
93     }
94   }
95
96   return Changed;
97 }
98
99 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
100 // with a value, then remove and delete the original instruction.
101 //
102 static void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
103                                  BasicBlock::iterator &BI, Value *V) {
104   Instruction *I = *BI;
105   // Replaces all of the uses of the instruction with uses of the value
106   I->replaceAllUsesWith(V);
107
108   // Remove the unneccesary instruction now...
109   BIL.remove(BI);
110
111   // Make sure to propogate a name if there is one already...
112   if (I->hasName() && !V->hasName())
113     V->setName(I->getName(), BIL.getParent()->getSymbolTable());
114
115   // Remove the dead instruction now...
116   delete I;
117 }
118
119
120 // ReplaceInstWithInst - Replace the instruction specified by BI with the
121 // instruction specified by I.  The original instruction is deleted and BI is
122 // updated to point to the new instruction.
123 //
124 static void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
125                                 BasicBlock::iterator &BI, Instruction *I) {
126   assert(I->getParent() == 0 &&
127          "ReplaceInstWithInst: Instruction already inserted into basic block!");
128
129   // Insert the new instruction into the basic block...
130   BI = BIL.insert(BI, I)+1;
131
132   // Replace all uses of the old instruction, and delete it.
133   ReplaceInstWithValue(BIL, BI, I);
134
135   // Reexamine the instruction just inserted next time around the cleanup pass
136   // loop.
137   --BI;
138 }
139
140
141 // doOneCleanupPass - Do one pass over the input method, fixing stuff up.
142 //
143 bool CleanupGCCOutput::doOneCleanupPass(Method *M) {
144   bool Changed = false;
145   for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
146     BasicBlock *BB = *MI;
147     BasicBlock::InstListType &BIL = BB->getInstList();
148
149     for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
150       Instruction *I = *BI;
151
152       if (CallInst *CI = dyn_cast<CallInst>(I)) {
153         if (CI->getCalledValue() == Malloc) {      // Replace call to malloc?
154           MallocInst *MallocI = new MallocInst(PtrArrSByte, CI->getOperand(1),
155                                                CI->getName());
156           CI->setName("");
157           BI = BIL.insert(BI, MallocI)+1;
158           ReplaceInstWithInst(BIL, BI, new CastInst(MallocI, PtrSByte));
159           Changed = true;
160           continue;  // Skip the ++BI
161         } else if (CI->getCalledValue() == Free) { // Replace call to free?
162           ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
163           Changed = true;
164           continue;  // Skip the ++BI
165         }
166       }
167
168       ++BI;
169     }
170   }
171
172   return Changed;
173 }
174
175
176
177
178 // doPerMethodWork - This method simplifies the specified method hopefully.
179 //
180 bool CleanupGCCOutput::doPerMethodWork(Method *M) {
181   bool Changed = false;
182   while (doOneCleanupPass(M)) Changed = true;
183   return Changed;
184 }