[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / lib / Transforms / IPO / ElimAvailExtern.cpp
blob7f138d206faca25bc3c27ca98774292706cd37e8
1 //===- ElimAvailExtern.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 available external global
10 // definitions from the program, turning them into declarations.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/IPO/ElimAvailExtern.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/IR/Constant.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/IR/GlobalVariable.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/InitializePasses.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Transforms/IPO.h"
24 #include "llvm/Transforms/Utils/GlobalStatus.h"
26 using namespace llvm;
28 #define DEBUG_TYPE "elim-avail-extern"
30 STATISTIC(NumFunctions, "Number of functions removed");
31 STATISTIC(NumVariables, "Number of global variables removed");
33 static bool eliminateAvailableExternally(Module &M) {
34 bool Changed = false;
36 // Drop initializers of available externally global variables.
37 for (GlobalVariable &GV : M.globals()) {
38 if (!GV.hasAvailableExternallyLinkage())
39 continue;
40 if (GV.hasInitializer()) {
41 Constant *Init = GV.getInitializer();
42 GV.setInitializer(nullptr);
43 if (isSafeToDestroyConstant(Init))
44 Init->destroyConstant();
46 GV.removeDeadConstantUsers();
47 GV.setLinkage(GlobalValue::ExternalLinkage);
48 NumVariables++;
49 Changed = true;
52 // Drop the bodies of available externally functions.
53 for (Function &F : M) {
54 if (!F.hasAvailableExternallyLinkage())
55 continue;
56 if (!F.isDeclaration())
57 // This will set the linkage to external
58 F.deleteBody();
59 F.removeDeadConstantUsers();
60 NumFunctions++;
61 Changed = true;
64 return Changed;
67 PreservedAnalyses
68 EliminateAvailableExternallyPass::run(Module &M, ModuleAnalysisManager &) {
69 if (!eliminateAvailableExternally(M))
70 return PreservedAnalyses::all();
71 return PreservedAnalyses::none();
74 namespace {
76 struct EliminateAvailableExternallyLegacyPass : public ModulePass {
77 static char ID; // Pass identification, replacement for typeid
79 EliminateAvailableExternallyLegacyPass() : ModulePass(ID) {
80 initializeEliminateAvailableExternallyLegacyPassPass(
81 *PassRegistry::getPassRegistry());
84 // run - Do the EliminateAvailableExternally pass on the specified module,
85 // optionally updating the specified callgraph to reflect the changes.
86 bool runOnModule(Module &M) override {
87 if (skipModule(M))
88 return false;
89 return eliminateAvailableExternally(M);
93 } // end anonymous namespace
95 char EliminateAvailableExternallyLegacyPass::ID = 0;
97 INITIALIZE_PASS(EliminateAvailableExternallyLegacyPass, "elim-avail-extern",
98 "Eliminate Available Externally Globals", false, false)
100 ModulePass *llvm::createEliminateAvailableExternallyPass() {
101 return new EliminateAvailableExternallyLegacyPass();