1 //===- CleanupGCCOutput.cpp - Cleanup GCC Output ----------------------------=//
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:
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.
13 // Note: This code produces dead declarations, it is a good idea to run DCE
16 //===----------------------------------------------------------------------===//
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"
24 static const Type *PtrArrSByte = 0; // '[sbyte]*' type
25 static const Type *PtrSByte = 0; // 'sbyte*' type
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;
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;
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.
44 bool CleanupGCCOutput::doPassInitialization(Module *M) {
47 if (PtrArrSByte == 0) {
48 PtrArrSByte = PointerType::get(ArrayType::get(Type::SByteTy));
49 PtrSByte = PointerType::get(Type::SByteTy);
52 if (M->hasSymbolTable()) {
53 SymbolTable *ST = M->getSymbolTable();
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.
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
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
74 // Check the symbol table for superfluous type entries...
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!
86 Plane.erase(PI); // Alas, GCC 2.95.3 doesn't *SIGH*
99 // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
100 // with a value, then remove and delete the original instruction.
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);
108 // Remove the unneccesary instruction now...
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());
115 // Remove the dead instruction now...
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.
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!");
129 // Insert the new instruction into the basic block...
130 BI = BIL.insert(BI, I)+1;
132 // Replace all uses of the old instruction, and delete it.
133 ReplaceInstWithValue(BIL, BI, I);
135 // Reexamine the instruction just inserted next time around the cleanup pass
141 // doOneCleanupPass - Do one pass over the input method, fixing stuff up.
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();
149 for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
150 Instruction *I = *BI;
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),
157 BI = BIL.insert(BI, MallocI)+1;
158 ReplaceInstWithInst(BIL, BI, new CastInst(MallocI, PtrSByte));
160 continue; // Skip the ++BI
161 } else if (CI->getCalledValue() == Free) { // Replace call to free?
162 ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
164 continue; // Skip the ++BI
178 // doPerMethodWork - This method simplifies the specified method hopefully.
180 bool CleanupGCCOutput::doPerMethodWork(Method *M) {
181 bool Changed = false;
182 while (doOneCleanupPass(M)) Changed = true;