LLVM  9.0.0svn
ElimAvailExtern.cpp
Go to the documentation of this file.
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
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/Pass.h"
22 #include "llvm/Transforms/IPO.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "elim-avail-extern"
28 
29 STATISTIC(NumFunctions, "Number of functions removed");
30 STATISTIC(NumVariables, "Number of global variables removed");
31 
33  bool Changed = false;
34 
35  // Drop initializers of available externally global variables.
36  for (GlobalVariable &GV : M.globals()) {
37  if (!GV.hasAvailableExternallyLinkage())
38  continue;
39  if (GV.hasInitializer()) {
40  Constant *Init = GV.getInitializer();
41  GV.setInitializer(nullptr);
42  if (isSafeToDestroyConstant(Init))
43  Init->destroyConstant();
44  }
45  GV.removeDeadConstantUsers();
46  GV.setLinkage(GlobalValue::ExternalLinkage);
47  NumVariables++;
48  Changed = true;
49  }
50 
51  // Drop the bodies of available externally functions.
52  for (Function &F : M) {
53  if (!F.hasAvailableExternallyLinkage())
54  continue;
55  if (!F.isDeclaration())
56  // This will set the linkage to external
57  F.deleteBody();
58  F.removeDeadConstantUsers();
59  NumFunctions++;
60  Changed = true;
61  }
62 
63  return Changed;
64 }
65 
69  return PreservedAnalyses::all();
70  return PreservedAnalyses::none();
71 }
72 
73 namespace {
74 
75 struct EliminateAvailableExternallyLegacyPass : public ModulePass {
76  static char ID; // Pass identification, replacement for typeid
77 
78  EliminateAvailableExternallyLegacyPass() : ModulePass(ID) {
81  }
82 
83  // run - Do the EliminateAvailableExternally pass on the specified module,
84  // optionally updating the specified callgraph to reflect the changes.
85  bool runOnModule(Module &M) override {
86  if (skipModule(M))
87  return false;
89  }
90 };
91 
92 } // end anonymous namespace
93 
95 
96 INITIALIZE_PASS(EliminateAvailableExternallyLegacyPass, "elim-avail-extern",
97  "Eliminate Available Externally Globals", false, false)
98 
100  return new EliminateAvailableExternallyLegacyPass();
101 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
Externally visible function.
Definition: GlobalValue.h:48
STATISTIC(NumFunctions, "Total number of functions")
F(f)
ModulePass * createEliminateAvailableExternallyPass()
This transform is designed to eliminate available external globals (functions or global variables) ...
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:156
static bool eliminateAvailableExternally(Module &M)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
void initializeEliminateAvailableExternallyLegacyPassPass(PassRegistry &)
This is an important base class in LLVM.
Definition: Constant.h:41
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
Module.h This file contains the declarations for the Module class.
INITIALIZE_PASS(EliminateAvailableExternallyLegacyPass, "elim-avail-extern", "Eliminate Available Externally Globals", false, false) ModulePass *llvm
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
void destroyConstant()
Called if some element of this constant is no longer valid.
Definition: Constants.cpp:361
iterator_range< global_iterator > globals()
Definition: Module.h:586
A container for analyses that lazily runs them and caches their results.