Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 1 | //===- ElimAvailExtern.cpp - DCE unreachable internal functions -----------===// |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This transform is designed to eliminate available external global |
| 10 | // definitions from the program, turning them into declarations. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 14 | #include "llvm/Transforms/IPO/ElimAvailExtern.h" |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Constant.h" |
| 17 | #include "llvm/IR/Function.h" |
| 18 | #include "llvm/IR/GlobalValue.h" |
| 19 | #include "llvm/IR/GlobalVariable.h" |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame^] | 21 | #include "llvm/InitializePasses.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/IPO.h" |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 25 | |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | #define DEBUG_TYPE "elim-avail-extern" |
| 29 | |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 30 | STATISTIC(NumFunctions, "Number of functions removed"); |
| 31 | STATISTIC(NumVariables, "Number of global variables removed"); |
| 32 | |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 33 | static bool eliminateAvailableExternally(Module &M) { |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 34 | bool Changed = false; |
| 35 | |
| 36 | // Drop initializers of available externally global variables. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 37 | for (GlobalVariable &GV : M.globals()) { |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 38 | if (!GV.hasAvailableExternallyLinkage()) |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 39 | continue; |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 40 | if (GV.hasInitializer()) { |
| 41 | Constant *Init = GV.getInitializer(); |
| 42 | GV.setInitializer(nullptr); |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 43 | if (isSafeToDestroyConstant(Init)) |
| 44 | Init->destroyConstant(); |
| 45 | } |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 46 | GV.removeDeadConstantUsers(); |
| 47 | GV.setLinkage(GlobalValue::ExternalLinkage); |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 48 | NumVariables++; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 49 | Changed = true; |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Drop the bodies of available externally functions. |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 53 | for (Function &F : M) { |
| 54 | if (!F.hasAvailableExternallyLinkage()) |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 55 | continue; |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 56 | if (!F.isDeclaration()) |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 57 | // This will set the linkage to external |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 58 | F.deleteBody(); |
| 59 | F.removeDeadConstantUsers(); |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 60 | NumFunctions++; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 61 | Changed = true; |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 64 | return Changed; |
| 65 | } |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 66 | |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 67 | PreservedAnalyses |
| 68 | EliminateAvailableExternallyPass::run(Module &M, ModuleAnalysisManager &) { |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 69 | if (!eliminateAvailableExternally(M)) |
| 70 | return PreservedAnalyses::all(); |
| 71 | return PreservedAnalyses::none(); |
| 72 | } |
| 73 | |
| 74 | namespace { |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 75 | |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 76 | struct EliminateAvailableExternallyLegacyPass : public ModulePass { |
| 77 | static char ID; // Pass identification, replacement for typeid |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 78 | |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 79 | EliminateAvailableExternallyLegacyPass() : ModulePass(ID) { |
| 80 | initializeEliminateAvailableExternallyLegacyPassPass( |
| 81 | *PassRegistry::getPassRegistry()); |
| 82 | } |
| 83 | |
| 84 | // run - Do the EliminateAvailableExternally pass on the specified module, |
| 85 | // optionally updating the specified callgraph to reflect the changes. |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 86 | bool runOnModule(Module &M) override { |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 87 | if (skipModule(M)) |
| 88 | return false; |
| 89 | return eliminateAvailableExternally(M); |
| 90 | } |
| 91 | }; |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 92 | |
| 93 | } // end anonymous namespace |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 94 | |
| 95 | char EliminateAvailableExternallyLegacyPass::ID = 0; |
Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 96 | |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 97 | INITIALIZE_PASS(EliminateAvailableExternallyLegacyPass, "elim-avail-extern", |
| 98 | "Eliminate Available Externally Globals", false, false) |
| 99 | |
| 100 | ModulePass *llvm::createEliminateAvailableExternallyPass() { |
| 101 | return new EliminateAvailableExternallyLegacyPass(); |
| 102 | } |