1 //===-- GlobalDCE.cpp - DCE unreachable internal functions ----------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This transform is designed to eliminate unreachable internal globals from the 10 // program. It uses an aggressive algorithm, searching out globals that are 11 // known to be alive. After it finds all of the globals which are needed, it 12 // deletes whatever is left over. This allows it to delete recursive chunks of 13 // the program which are unreachable. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/Transforms/IPO/GlobalDCE.h" 18 #include "llvm/ADT/SmallPtrSet.h" 19 #include "llvm/ADT/Statistic.h" 20 #include "llvm/Analysis/TypeMetadataUtils.h" 21 #include "llvm/IR/Instructions.h" 22 #include "llvm/IR/IntrinsicInst.h" 23 #include "llvm/IR/Module.h" 24 #include "llvm/IR/Operator.h" 25 #include "llvm/InitializePasses.h" 26 #include "llvm/Pass.h" 27 #include "llvm/Transforms/IPO.h" 28 #include "llvm/Transforms/Utils/CtorUtils.h" 29 #include "llvm/Transforms/Utils/GlobalStatus.h" 30 31 using namespace llvm; 32 33 #define DEBUG_TYPE "globaldce" 34 35 static cl::opt<bool> 36 ClEnableVFE("enable-vfe", cl::Hidden, cl::init(true), cl::ZeroOrMore, 37 cl::desc("Enable virtual function elimination")); 38 39 STATISTIC(NumAliases , "Number of global aliases removed"); 40 STATISTIC(NumFunctions, "Number of functions removed"); 41 STATISTIC(NumIFuncs, "Number of indirect functions removed"); 42 STATISTIC(NumVariables, "Number of global variables removed"); 43 STATISTIC(NumVFuncs, "Number of virtual functions removed"); 44 45 namespace { 46 class GlobalDCELegacyPass : public ModulePass { 47 public: 48 static char ID; // Pass identification, replacement for typeid 49 GlobalDCELegacyPass() : ModulePass(ID) { 50 initializeGlobalDCELegacyPassPass(*PassRegistry::getPassRegistry()); 51 } 52 53 // run - Do the GlobalDCE pass on the specified module, optionally updating 54 // the specified callgraph to reflect the changes. 55 // 56 bool runOnModule(Module &M) override { 57 if (skipModule(M)) 58 return false; 59 60 // We need a minimally functional dummy module analysis manager. It needs 61 // to at least know about the possibility of proxying a function analysis 62 // manager. 63 FunctionAnalysisManager DummyFAM; 64 ModuleAnalysisManager DummyMAM; 65 DummyMAM.registerPass( 66 [&] { return FunctionAnalysisManagerModuleProxy(DummyFAM); }); 67 68 auto PA = Impl.run(M, DummyMAM); 69 return !PA.areAllPreserved(); 70 } 71 72 private: 73 GlobalDCEPass Impl; 74 }; 75 } 76 77 char GlobalDCELegacyPass::ID = 0; 78 INITIALIZE_PASS(GlobalDCELegacyPass, "globaldce", 79 "Dead Global Elimination", false, false) 80 81 // Public interface to the GlobalDCEPass. 82 ModulePass *llvm::createGlobalDCEPass() { 83 return new GlobalDCELegacyPass(); 84 } 85 86 /// Returns true if F is effectively empty. 87 static bool isEmptyFunction(Function *F) { 88 BasicBlock &Entry = F->getEntryBlock(); 89 for (auto &I : Entry) { 90 if (isa<DbgInfoIntrinsic>(I)) 91 continue; 92 if (auto *RI = dyn_cast<ReturnInst>(&I)) 93 return !RI->getReturnValue(); 94 break; 95 } 96 return false; 97 } 98 99 /// Compute the set of GlobalValue that depends from V. 100 /// The recursion stops as soon as a GlobalValue is met. 101 void GlobalDCEPass::ComputeDependencies(Value *V, 102 SmallPtrSetImpl<GlobalValue *> &Deps) { 103 if (auto *I = dyn_cast<Instruction>(V)) { 104 Function *Parent = I->getParent()->getParent(); 105 Deps.insert(Parent); 106 } else if (auto *GV = dyn_cast<GlobalValue>(V)) { 107 Deps.insert(GV); 108 } else if (auto *CE = dyn_cast<Constant>(V)) { 109 // Avoid walking the whole tree of a big ConstantExprs multiple times. 110 auto Where = ConstantDependenciesCache.find(CE); 111 if (Where != ConstantDependenciesCache.end()) { 112 auto const &K = Where->second; 113 Deps.insert(K.begin(), K.end()); 114 } else { 115 SmallPtrSetImpl<GlobalValue *> &LocalDeps = ConstantDependenciesCache[CE]; 116 for (User *CEUser : CE->users()) 117 ComputeDependencies(CEUser, LocalDeps); 118 Deps.insert(LocalDeps.begin(), LocalDeps.end()); 119 } 120 } 121 } 122 123 void GlobalDCEPass::UpdateGVDependencies(GlobalValue &GV) { 124 SmallPtrSet<GlobalValue *, 8> Deps; 125 for (User *User : GV.users()) 126 ComputeDependencies(User, Deps); 127 Deps.erase(&GV); // Remove self-reference. 128 for (GlobalValue *GVU : Deps) { 129 // If this is a dep from a vtable to a virtual function, and we have 130 // complete information about all virtual call sites which could call 131 // though this vtable, then skip it, because the call site information will 132 // be more precise. 133 if (VFESafeVTables.count(GVU) && isa<Function>(&GV)) { 134 LLVM_DEBUG(dbgs() << "Ignoring dep " << GVU->getName() << " -> " 135 << GV.getName() << "\n"); 136 continue; 137 } 138 GVDependencies[GVU].insert(&GV); 139 } 140 } 141 142 /// Mark Global value as Live 143 void GlobalDCEPass::MarkLive(GlobalValue &GV, 144 SmallVectorImpl<GlobalValue *> *Updates) { 145 auto const Ret = AliveGlobals.insert(&GV); 146 if (!Ret.second) 147 return; 148 149 if (Updates) 150 Updates->push_back(&GV); 151 if (Comdat *C = GV.getComdat()) { 152 for (auto &&CM : make_range(ComdatMembers.equal_range(C))) { 153 MarkLive(*CM.second, Updates); // Recursion depth is only two because only 154 // globals in the same comdat are visited. 155 } 156 } 157 } 158 159 void GlobalDCEPass::ScanVTables(Module &M) { 160 SmallVector<MDNode *, 2> Types; 161 LLVM_DEBUG(dbgs() << "Building type info -> vtable map\n"); 162 163 auto *LTOPostLinkMD = 164 cast_or_null<ConstantAsMetadata>(M.getModuleFlag("LTOPostLink")); 165 bool LTOPostLink = 166 LTOPostLinkMD && 167 (cast<ConstantInt>(LTOPostLinkMD->getValue())->getZExtValue() != 0); 168 169 for (GlobalVariable &GV : M.globals()) { 170 Types.clear(); 171 GV.getMetadata(LLVMContext::MD_type, Types); 172 if (GV.isDeclaration() || Types.empty()) 173 continue; 174 175 // Use the typeid metadata on the vtable to build a mapping from typeids to 176 // the list of (GV, offset) pairs which are the possible vtables for that 177 // typeid. 178 for (MDNode *Type : Types) { 179 Metadata *TypeID = Type->getOperand(1).get(); 180 181 uint64_t Offset = 182 cast<ConstantInt>( 183 cast<ConstantAsMetadata>(Type->getOperand(0))->getValue()) 184 ->getZExtValue(); 185 186 TypeIdMap[TypeID].insert(std::make_pair(&GV, Offset)); 187 } 188 189 // If the type corresponding to the vtable is private to this translation 190 // unit, we know that we can see all virtual functions which might use it, 191 // so VFE is safe. 192 if (auto GO = dyn_cast<GlobalObject>(&GV)) { 193 GlobalObject::VCallVisibility TypeVis = GO->getVCallVisibility(); 194 if (TypeVis == GlobalObject::VCallVisibilityTranslationUnit || 195 (LTOPostLink && 196 TypeVis == GlobalObject::VCallVisibilityLinkageUnit)) { 197 LLVM_DEBUG(dbgs() << GV.getName() << " is safe for VFE\n"); 198 VFESafeVTables.insert(&GV); 199 } 200 } 201 } 202 } 203 204 void GlobalDCEPass::ScanVTableLoad(Function *Caller, Metadata *TypeId, 205 uint64_t CallOffset) { 206 for (auto &VTableInfo : TypeIdMap[TypeId]) { 207 GlobalVariable *VTable = VTableInfo.first; 208 uint64_t VTableOffset = VTableInfo.second; 209 210 Constant *Ptr = 211 getPointerAtOffset(VTable->getInitializer(), VTableOffset + CallOffset, 212 *Caller->getParent()); 213 if (!Ptr) { 214 LLVM_DEBUG(dbgs() << "can't find pointer in vtable!\n"); 215 VFESafeVTables.erase(VTable); 216 return; 217 } 218 219 auto Callee = dyn_cast<Function>(Ptr->stripPointerCasts()); 220 if (!Callee) { 221 LLVM_DEBUG(dbgs() << "vtable entry is not function pointer!\n"); 222 VFESafeVTables.erase(VTable); 223 return; 224 } 225 226 LLVM_DEBUG(dbgs() << "vfunc dep " << Caller->getName() << " -> " 227 << Callee->getName() << "\n"); 228 GVDependencies[Caller].insert(Callee); 229 } 230 } 231 232 void GlobalDCEPass::ScanTypeCheckedLoadIntrinsics(Module &M) { 233 LLVM_DEBUG(dbgs() << "Scanning type.checked.load intrinsics\n"); 234 Function *TypeCheckedLoadFunc = 235 M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load)); 236 237 if (!TypeCheckedLoadFunc) 238 return; 239 240 for (auto U : TypeCheckedLoadFunc->users()) { 241 auto CI = dyn_cast<CallInst>(U); 242 if (!CI) 243 continue; 244 245 auto *Offset = dyn_cast<ConstantInt>(CI->getArgOperand(1)); 246 Value *TypeIdValue = CI->getArgOperand(2); 247 auto *TypeId = cast<MetadataAsValue>(TypeIdValue)->getMetadata(); 248 249 if (Offset) { 250 ScanVTableLoad(CI->getFunction(), TypeId, Offset->getZExtValue()); 251 } else { 252 // type.checked.load with a non-constant offset, so assume every entry in 253 // every matching vtable is used. 254 for (auto &VTableInfo : TypeIdMap[TypeId]) { 255 VFESafeVTables.erase(VTableInfo.first); 256 } 257 } 258 } 259 } 260 261 void GlobalDCEPass::AddVirtualFunctionDependencies(Module &M) { 262 if (!ClEnableVFE) 263 return; 264 265 ScanVTables(M); 266 267 if (VFESafeVTables.empty()) 268 return; 269 270 ScanTypeCheckedLoadIntrinsics(M); 271 272 LLVM_DEBUG( 273 dbgs() << "VFE safe vtables:\n"; 274 for (auto *VTable : VFESafeVTables) 275 dbgs() << " " << VTable->getName() << "\n"; 276 ); 277 } 278 279 PreservedAnalyses GlobalDCEPass::run(Module &M, ModuleAnalysisManager &MAM) { 280 bool Changed = false; 281 282 // The algorithm first computes the set L of global variables that are 283 // trivially live. Then it walks the initialization of these variables to 284 // compute the globals used to initialize them, which effectively builds a 285 // directed graph where nodes are global variables, and an edge from A to B 286 // means B is used to initialize A. Finally, it propagates the liveness 287 // information through the graph starting from the nodes in L. Nodes note 288 // marked as alive are discarded. 289 290 // Remove empty functions from the global ctors list. 291 Changed |= optimizeGlobalCtorsList(M, isEmptyFunction); 292 293 // Collect the set of members for each comdat. 294 for (Function &F : M) 295 if (Comdat *C = F.getComdat()) 296 ComdatMembers.insert(std::make_pair(C, &F)); 297 for (GlobalVariable &GV : M.globals()) 298 if (Comdat *C = GV.getComdat()) 299 ComdatMembers.insert(std::make_pair(C, &GV)); 300 for (GlobalAlias &GA : M.aliases()) 301 if (Comdat *C = GA.getComdat()) 302 ComdatMembers.insert(std::make_pair(C, &GA)); 303 304 // Add dependencies between virtual call sites and the virtual functions they 305 // might call, if we have that information. 306 AddVirtualFunctionDependencies(M); 307 308 // Loop over the module, adding globals which are obviously necessary. 309 for (GlobalObject &GO : M.global_objects()) { 310 Changed |= RemoveUnusedGlobalValue(GO); 311 // Functions with external linkage are needed if they have a body. 312 // Externally visible & appending globals are needed, if they have an 313 // initializer. 314 if (!GO.isDeclaration()) 315 if (!GO.isDiscardableIfUnused()) 316 MarkLive(GO); 317 318 UpdateGVDependencies(GO); 319 } 320 321 // Compute direct dependencies of aliases. 322 for (GlobalAlias &GA : M.aliases()) { 323 Changed |= RemoveUnusedGlobalValue(GA); 324 // Externally visible aliases are needed. 325 if (!GA.isDiscardableIfUnused()) 326 MarkLive(GA); 327 328 UpdateGVDependencies(GA); 329 } 330 331 // Compute direct dependencies of ifuncs. 332 for (GlobalIFunc &GIF : M.ifuncs()) { 333 Changed |= RemoveUnusedGlobalValue(GIF); 334 // Externally visible ifuncs are needed. 335 if (!GIF.isDiscardableIfUnused()) 336 MarkLive(GIF); 337 338 UpdateGVDependencies(GIF); 339 } 340 341 // Propagate liveness from collected Global Values through the computed 342 // dependencies. 343 SmallVector<GlobalValue *, 8> NewLiveGVs{AliveGlobals.begin(), 344 AliveGlobals.end()}; 345 while (!NewLiveGVs.empty()) { 346 GlobalValue *LGV = NewLiveGVs.pop_back_val(); 347 for (auto *GVD : GVDependencies[LGV]) 348 MarkLive(*GVD, &NewLiveGVs); 349 } 350 351 // Now that all globals which are needed are in the AliveGlobals set, we loop 352 // through the program, deleting those which are not alive. 353 // 354 355 // The first pass is to drop initializers of global variables which are dead. 356 std::vector<GlobalVariable *> DeadGlobalVars; // Keep track of dead globals 357 for (GlobalVariable &GV : M.globals()) 358 if (!AliveGlobals.count(&GV)) { 359 DeadGlobalVars.push_back(&GV); // Keep track of dead globals 360 if (GV.hasInitializer()) { 361 Constant *Init = GV.getInitializer(); 362 GV.setInitializer(nullptr); 363 if (isSafeToDestroyConstant(Init)) 364 Init->destroyConstant(); 365 } 366 } 367 368 // The second pass drops the bodies of functions which are dead... 369 std::vector<Function *> DeadFunctions; 370 for (Function &F : M) 371 if (!AliveGlobals.count(&F)) { 372 DeadFunctions.push_back(&F); // Keep track of dead globals 373 if (!F.isDeclaration()) 374 F.deleteBody(); 375 } 376 377 // The third pass drops targets of aliases which are dead... 378 std::vector<GlobalAlias*> DeadAliases; 379 for (GlobalAlias &GA : M.aliases()) 380 if (!AliveGlobals.count(&GA)) { 381 DeadAliases.push_back(&GA); 382 GA.setAliasee(nullptr); 383 } 384 385 // The fourth pass drops targets of ifuncs which are dead... 386 std::vector<GlobalIFunc*> DeadIFuncs; 387 for (GlobalIFunc &GIF : M.ifuncs()) 388 if (!AliveGlobals.count(&GIF)) { 389 DeadIFuncs.push_back(&GIF); 390 GIF.setResolver(nullptr); 391 } 392 393 // Now that all interferences have been dropped, delete the actual objects 394 // themselves. 395 auto EraseUnusedGlobalValue = [&](GlobalValue *GV) { 396 RemoveUnusedGlobalValue(*GV); 397 GV->eraseFromParent(); 398 Changed = true; 399 }; 400 401 NumFunctions += DeadFunctions.size(); 402 for (Function *F : DeadFunctions) { 403 if (!F->use_empty()) { 404 // Virtual functions might still be referenced by one or more vtables, 405 // but if we've proven them to be unused then it's safe to replace the 406 // virtual function pointers with null, allowing us to remove the 407 // function itself. 408 ++NumVFuncs; 409 F->replaceNonMetadataUsesWith(ConstantPointerNull::get(F->getType())); 410 } 411 EraseUnusedGlobalValue(F); 412 } 413 414 NumVariables += DeadGlobalVars.size(); 415 for (GlobalVariable *GV : DeadGlobalVars) 416 EraseUnusedGlobalValue(GV); 417 418 NumAliases += DeadAliases.size(); 419 for (GlobalAlias *GA : DeadAliases) 420 EraseUnusedGlobalValue(GA); 421 422 NumIFuncs += DeadIFuncs.size(); 423 for (GlobalIFunc *GIF : DeadIFuncs) 424 EraseUnusedGlobalValue(GIF); 425 426 // Make sure that all memory is released 427 AliveGlobals.clear(); 428 ConstantDependenciesCache.clear(); 429 GVDependencies.clear(); 430 ComdatMembers.clear(); 431 TypeIdMap.clear(); 432 VFESafeVTables.clear(); 433 434 if (Changed) 435 return PreservedAnalyses::none(); 436 return PreservedAnalyses::all(); 437 } 438 439 // RemoveUnusedGlobalValue - Loop over all of the uses of the specified 440 // GlobalValue, looking for the constant pointer ref that may be pointing to it. 441 // If found, check to see if the constant pointer ref is safe to destroy, and if 442 // so, nuke it. This will reduce the reference count on the global value, which 443 // might make it deader. 444 // 445 bool GlobalDCEPass::RemoveUnusedGlobalValue(GlobalValue &GV) { 446 if (GV.use_empty()) 447 return false; 448 GV.removeDeadConstantUsers(); 449 return GV.use_empty(); 450 } 451