LLVM API Documentation
00001 //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This transform is designed to eliminate unreachable internal globals from the 00011 // program. It uses an aggressive algorithm, searching out globals that are 00012 // known to be alive. After it finds all of the globals which are needed, it 00013 // deletes whatever is left over. This allows it to delete recursive chunks of 00014 // the program which are unreachable. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #define DEBUG_TYPE "globaldce" 00019 #include "llvm/Transforms/IPO.h" 00020 #include "llvm/Constants.h" 00021 #include "llvm/Module.h" 00022 #include "llvm/Pass.h" 00023 #include "llvm/ADT/Statistic.h" 00024 #include "llvm/Support/Compiler.h" 00025 #include <set> 00026 using namespace llvm; 00027 00028 STATISTIC(NumAliases , "Number of global aliases removed"); 00029 STATISTIC(NumFunctions, "Number of functions removed"); 00030 STATISTIC(NumVariables, "Number of global variables removed"); 00031 00032 namespace { 00033 struct VISIBILITY_HIDDEN GlobalDCE : public ModulePass { 00034 static char ID; // Pass identification, replacement for typeid 00035 GlobalDCE() : ModulePass(&ID) {} 00036 00037 // run - Do the GlobalDCE pass on the specified module, optionally updating 00038 // the specified callgraph to reflect the changes. 00039 // 00040 bool runOnModule(Module &M); 00041 00042 private: 00043 std::set<GlobalValue*> AliveGlobals; 00044 00045 /// GlobalIsNeeded - mark the specific global value as needed, and 00046 /// recursively mark anything that it uses as also needed. 00047 void GlobalIsNeeded(GlobalValue *GV); 00048 void MarkUsedGlobalsAsNeeded(Constant *C); 00049 00050 bool SafeToDestroyConstant(Constant* C); 00051 bool RemoveUnusedGlobalValue(GlobalValue &GV); 00052 }; 00053 } 00054 00055 char GlobalDCE::ID = 0; 00056 static RegisterPass<GlobalDCE> X("globaldce", "Dead Global Elimination"); 00057 00058 ModulePass *llvm::createGlobalDCEPass() { return new GlobalDCE(); } 00059 00060 bool GlobalDCE::runOnModule(Module &M) { 00061 bool Changed = false; 00062 // Loop over the module, adding globals which are obviously necessary. 00063 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { 00064 Changed |= RemoveUnusedGlobalValue(*I); 00065 // Functions with external linkage are needed if they have a body 00066 if ((!I->hasLocalLinkage() && !I->hasLinkOnceLinkage()) && 00067 !I->isDeclaration()) 00068 GlobalIsNeeded(I); 00069 } 00070 00071 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 00072 I != E; ++I) { 00073 Changed |= RemoveUnusedGlobalValue(*I); 00074 // Externally visible & appending globals are needed, if they have an 00075 // initializer. 00076 if ((!I->hasLocalLinkage() && !I->hasLinkOnceLinkage()) && 00077 !I->isDeclaration()) 00078 GlobalIsNeeded(I); 00079 } 00080 00081 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); 00082 I != E; ++I) { 00083 Changed |= RemoveUnusedGlobalValue(*I); 00084 // Externally visible aliases are needed. 00085 if (!I->hasInternalLinkage() && !I->hasLinkOnceLinkage()) 00086 GlobalIsNeeded(I); 00087 } 00088 00089 // Now that all globals which are needed are in the AliveGlobals set, we loop 00090 // through the program, deleting those which are not alive. 00091 // 00092 00093 // The first pass is to drop initializers of global variables which are dead. 00094 std::vector<GlobalVariable*> DeadGlobalVars; // Keep track of dead globals 00095 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) 00096 if (!AliveGlobals.count(I)) { 00097 DeadGlobalVars.push_back(I); // Keep track of dead globals 00098 I->setInitializer(0); 00099 } 00100 00101 // The second pass drops the bodies of functions which are dead... 00102 std::vector<Function*> DeadFunctions; 00103 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00104 if (!AliveGlobals.count(I)) { 00105 DeadFunctions.push_back(I); // Keep track of dead globals 00106 if (!I->isDeclaration()) 00107 I->deleteBody(); 00108 } 00109 00110 if (!DeadFunctions.empty()) { 00111 // Now that all interferences have been dropped, delete the actual objects 00112 // themselves. 00113 for (unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) { 00114 RemoveUnusedGlobalValue(*DeadFunctions[i]); 00115 M.getFunctionList().erase(DeadFunctions[i]); 00116 } 00117 NumFunctions += DeadFunctions.size(); 00118 Changed = true; 00119 } 00120 00121 if (!DeadGlobalVars.empty()) { 00122 for (unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) { 00123 RemoveUnusedGlobalValue(*DeadGlobalVars[i]); 00124 M.getGlobalList().erase(DeadGlobalVars[i]); 00125 } 00126 NumVariables += DeadGlobalVars.size(); 00127 Changed = true; 00128 } 00129 00130 // Now delete any dead aliases. 00131 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); I != E;) { 00132 Module::alias_iterator J = I++; 00133 if (!AliveGlobals.count(J)) { 00134 RemoveUnusedGlobalValue(*J); 00135 M.getAliasList().erase(J); 00136 ++NumAliases; 00137 Changed = true; 00138 } 00139 } 00140 00141 // Make sure that all memory is released 00142 AliveGlobals.clear(); 00143 return Changed; 00144 } 00145 00146 /// GlobalIsNeeded - the specific global value as needed, and 00147 /// recursively mark anything that it uses as also needed. 00148 void GlobalDCE::GlobalIsNeeded(GlobalValue *G) { 00149 std::set<GlobalValue*>::iterator I = AliveGlobals.find(G); 00150 00151 // If the global is already in the set, no need to reprocess it. 00152 if (I != AliveGlobals.end()) return; 00153 00154 // Otherwise insert it now, so we do not infinitely recurse 00155 AliveGlobals.insert(I, G); 00156 00157 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(G)) { 00158 // If this is a global variable, we must make sure to add any global values 00159 // referenced by the initializer to the alive set. 00160 if (GV->hasInitializer()) 00161 MarkUsedGlobalsAsNeeded(GV->getInitializer()); 00162 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) { 00163 // The target of a global alias is needed. 00164 MarkUsedGlobalsAsNeeded(GA->getAliasee()); 00165 } else { 00166 // Otherwise this must be a function object. We have to scan the body of 00167 // the function looking for constants and global values which are used as 00168 // operands. Any operands of these types must be processed to ensure that 00169 // any globals used will be marked as needed. 00170 Function *F = cast<Function>(G); 00171 // For all basic blocks... 00172 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) 00173 // For all instructions... 00174 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) 00175 // For all operands... 00176 for (User::op_iterator U = I->op_begin(), E = I->op_end(); U != E; ++U) 00177 if (GlobalValue *GV = dyn_cast<GlobalValue>(*U)) 00178 GlobalIsNeeded(GV); 00179 else if (Constant *C = dyn_cast<Constant>(*U)) 00180 MarkUsedGlobalsAsNeeded(C); 00181 } 00182 } 00183 00184 void GlobalDCE::MarkUsedGlobalsAsNeeded(Constant *C) { 00185 if (GlobalValue *GV = dyn_cast<GlobalValue>(C)) 00186 GlobalIsNeeded(GV); 00187 else { 00188 // Loop over all of the operands of the constant, adding any globals they 00189 // use to the list of needed globals. 00190 for (User::op_iterator I = C->op_begin(), E = C->op_end(); I != E; ++I) 00191 MarkUsedGlobalsAsNeeded(cast<Constant>(*I)); 00192 } 00193 } 00194 00195 // RemoveUnusedGlobalValue - Loop over all of the uses of the specified 00196 // GlobalValue, looking for the constant pointer ref that may be pointing to it. 00197 // If found, check to see if the constant pointer ref is safe to destroy, and if 00198 // so, nuke it. This will reduce the reference count on the global value, which 00199 // might make it deader. 00200 // 00201 bool GlobalDCE::RemoveUnusedGlobalValue(GlobalValue &GV) { 00202 if (GV.use_empty()) return false; 00203 GV.removeDeadConstantUsers(); 00204 return GV.use_empty(); 00205 } 00206 00207 // SafeToDestroyConstant - It is safe to destroy a constant iff it is only used 00208 // by constants itself. Note that constants cannot be cyclic, so this test is 00209 // pretty easy to implement recursively. 00210 // 00211 bool GlobalDCE::SafeToDestroyConstant(Constant *C) { 00212 for (Value::use_iterator I = C->use_begin(), E = C->use_end(); I != E; ++I) 00213 if (Constant *User = dyn_cast<Constant>(*I)) { 00214 if (!SafeToDestroyConstant(User)) return false; 00215 } else { 00216 return false; 00217 } 00218 return true; 00219 }