LLVM  6.0.0svn
ElimAvailExtern.cpp
Go to the documentation of this file.
1 //===- ElimAvailExtern.cpp - DCE unreachable internal functions -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This transform is designed to eliminate available external global
11 // definitions from the program, turning them into declarations.
12 //
13 //===----------------------------------------------------------------------===//
14 
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/Constant.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/GlobalVariable.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Transforms/IPO.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "elim-avail-extern"
29 
30 STATISTIC(NumFunctions, "Number of functions removed");
31 STATISTIC(NumVariables, "Number of global variables removed");
32 
34  bool Changed = false;
35 
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();
45  }
46  GV.removeDeadConstantUsers();
47  GV.setLinkage(GlobalValue::ExternalLinkage);
48  NumVariables++;
49  Changed = true;
50  }
51 
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;
62  }
63 
64  return Changed;
65 }
66 
70  return PreservedAnalyses::all();
71  return PreservedAnalyses::none();
72 }
73 
74 namespace {
75 
76 struct EliminateAvailableExternallyLegacyPass : public ModulePass {
77  static char ID; // Pass identification, replacement for typeid
78 
79  EliminateAvailableExternallyLegacyPass() : ModulePass(ID) {
82  }
83 
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;
90  }
91 };
92 
93 } // end anonymous namespace
94 
96 
97 INITIALIZE_PASS(EliminateAvailableExternallyLegacyPass, "elim-avail-extern",
98  "Eliminate Available Externally Globals", false, false)
99 
101  return new EliminateAvailableExternallyLegacyPass();
102 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
Externally visible function.
Definition: GlobalValue.h:49
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:42
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:225
void destroyConstant()
Called if some element of this constant is no longer valid.
Definition: Constants.cpp:300
iterator_range< global_iterator > globals()
Definition: Module.h:561
A container for analyses that lazily runs them and caches their results.