1 //===-- ElimAvailExtern.cpp - DCE unreachable internal functions ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This transform is designed to eliminate available external global
11 // definitions from the program, turning them into declarations.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/IPO.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Transforms/Utils/GlobalStatus.h"
20 #include "llvm/Pass.h"
23 #define DEBUG_TYPE "elim-avail-extern"
25 STATISTIC(NumFunctions, "Number of functions removed");
26 STATISTIC(NumVariables, "Number of global variables removed");
29 struct EliminateAvailableExternally : public ModulePass {
30 static char ID; // Pass identification, replacement for typeid
31 EliminateAvailableExternally() : ModulePass(ID) {
32 initializeEliminateAvailableExternallyPass(
33 *PassRegistry::getPassRegistry());
36 // run - Do the EliminateAvailableExternally pass on the specified module,
37 // optionally updating the specified callgraph to reflect the changes.
39 bool runOnModule(Module &M) override;
43 char EliminateAvailableExternally::ID = 0;
44 INITIALIZE_PASS(EliminateAvailableExternally, "elim-avail-extern",
45 "Eliminate Available Externally Globals", false, false)
47 ModulePass *llvm::createEliminateAvailableExternallyPass() {
48 return new EliminateAvailableExternally();
51 bool EliminateAvailableExternally::runOnModule(Module &M) {
54 // Drop initializers of available externally global variables.
55 for (GlobalVariable &GV :M.globals()) {
56 if (!GV.hasAvailableExternallyLinkage())
58 if (GV.hasInitializer()) {
59 Constant *Init = GV.getInitializer();
60 GV.setInitializer(nullptr);
61 if (isSafeToDestroyConstant(Init))
62 Init->destroyConstant();
64 GV.removeDeadConstantUsers();
65 GV.setLinkage(GlobalValue::ExternalLinkage);
69 // Drop the bodies of available externally functions.
70 for (Function &F : M) {
71 if (!F.hasAvailableExternallyLinkage())
73 if (!F.isDeclaration())
74 // This will set the linkage to external
76 F.removeDeadConstantUsers();