Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 1 | //===-- ElimAvailExtern.cpp - DCE unreachable internal functions |
| 2 | //----------------===// |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | // |
| 11 | // This transform is designed to eliminate available external global |
| 12 | // definitions from the program, turning them into declarations. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/Transforms/IPO.h" |
| 17 | #include "llvm/ADT/Statistic.h" |
| 18 | #include "llvm/IR/Constants.h" |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/GlobalStatus.h" |
| 21 | #include "llvm/Pass.h" |
| 22 | using namespace llvm; |
| 23 | |
| 24 | #define DEBUG_TYPE "elim-avail-extern" |
| 25 | |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 26 | STATISTIC(NumFunctions, "Number of functions removed"); |
| 27 | STATISTIC(NumVariables, "Number of global variables removed"); |
| 28 | |
| 29 | namespace { |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 30 | struct EliminateAvailableExternally : public ModulePass { |
| 31 | static char ID; // Pass identification, replacement for typeid |
| 32 | EliminateAvailableExternally() : ModulePass(ID) { |
| 33 | initializeEliminateAvailableExternallyPass( |
| 34 | *PassRegistry::getPassRegistry()); |
| 35 | } |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 36 | |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 37 | // run - Do the EliminateAvailableExternally pass on the specified module, |
| 38 | // optionally updating the specified callgraph to reflect the changes. |
| 39 | // |
| 40 | bool runOnModule(Module &M) override; |
| 41 | }; |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | char EliminateAvailableExternally::ID = 0; |
| 45 | INITIALIZE_PASS(EliminateAvailableExternally, "elim-avail-extern", |
| 46 | "Eliminate Available Externally Globals", false, false) |
| 47 | |
| 48 | ModulePass *llvm::createEliminateAvailableExternallyPass() { |
| 49 | return new EliminateAvailableExternally(); |
| 50 | } |
| 51 | |
| 52 | bool EliminateAvailableExternally::runOnModule(Module &M) { |
| 53 | bool Changed = false; |
| 54 | |
| 55 | // Drop initializers of available externally global variables. |
Teresa Johnson | f72278f | 2015-11-02 18:02:11 +0000 | [diff] [blame] | 56 | for (GlobalVariable &GV : M.globals()) { |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 57 | if (!GV.hasAvailableExternallyLinkage()) |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 58 | continue; |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 59 | if (GV.hasInitializer()) { |
| 60 | Constant *Init = GV.getInitializer(); |
| 61 | GV.setInitializer(nullptr); |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 62 | if (isSafeToDestroyConstant(Init)) |
| 63 | Init->destroyConstant(); |
| 64 | } |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 65 | GV.removeDeadConstantUsers(); |
| 66 | GV.setLinkage(GlobalValue::ExternalLinkage); |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 67 | NumVariables++; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 68 | Changed = true; |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Drop the bodies of available externally functions. |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 72 | for (Function &F : M) { |
| 73 | if (!F.hasAvailableExternallyLinkage()) |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 74 | continue; |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 75 | if (!F.isDeclaration()) |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 76 | // This will set the linkage to external |
Yaron Keren | 771e319 | 2015-09-04 20:24:24 +0000 | [diff] [blame] | 77 | F.deleteBody(); |
| 78 | F.removeDeadConstantUsers(); |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 79 | NumFunctions++; |
Teresa Johnson | c7ed52f | 2015-11-03 00:14:15 +0000 | [diff] [blame] | 80 | Changed = true; |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 83 | return Changed; |
| 84 | } |