LLVM  3.7.0
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 
15 #include "llvm/Transforms/IPO.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/Module.h"
22 #include "llvm/Pass.h"
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "elim-avail-extern"
26 
27 STATISTIC(NumFunctions, "Number of functions removed");
28 STATISTIC(NumVariables, "Number of global variables removed");
29 
30 namespace {
31  struct EliminateAvailableExternally : public ModulePass {
32  static char ID; // Pass identification, replacement for typeid
33  EliminateAvailableExternally() : ModulePass(ID) {
36  }
37 
38  // run - Do the EliminateAvailableExternally pass on the specified module,
39  // optionally updating the specified callgraph to reflect the changes.
40  //
41  bool runOnModule(Module &M) override;
42  };
43 }
44 
46 INITIALIZE_PASS(EliminateAvailableExternally, "elim-avail-extern",
47  "Eliminate Available Externally Globals", false, false)
48 
50  return new EliminateAvailableExternally();
51 }
52 
53 bool EliminateAvailableExternally::runOnModule(Module &M) {
54  bool Changed = false;
55 
56  // Drop initializers of available externally global variables.
58  I != E; ++I) {
59  if (!I->hasAvailableExternallyLinkage())
60  continue;
61  if (I->hasInitializer()) {
62  Constant *Init = I->getInitializer();
63  I->setInitializer(nullptr);
64  if (isSafeToDestroyConstant(Init))
65  Init->destroyConstant();
66  }
67  I->removeDeadConstantUsers();
68  I->setLinkage(GlobalValue::ExternalLinkage);
69  NumVariables++;
70  }
71 
72  // Drop the bodies of available externally functions.
73  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
74  if (!I->hasAvailableExternallyLinkage())
75  continue;
76  if (!I->isDeclaration())
77  // This will set the linkage to external
78  I->deleteBody();
79  I->removeDeadConstantUsers();
80  NumFunctions++;
81  }
82 
83  return Changed;
84 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
void initializeEliminateAvailableExternallyPass(PassRegistry &)
Externally visible function.
Definition: GlobalValue.h:40
ModulePass * createEliminateAvailableExternallyPass()
This transform is designed to eliminate available external globals (functions or global variables) ...
global_iterator global_begin()
Definition: Module.h:552
INITIALIZE_PASS(EliminateAvailableExternally,"elim-avail-extern","Eliminate Available Externally Globals", false, false) ModulePass *llvm
This is an important base class in LLVM.
Definition: Constant.h:41
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
global_iterator global_end()
Definition: Module.h:554
Module.h This file contains the declarations for the Module class.
iterator end()
Definition: Module.h:571
#define I(x, y, z)
Definition: MD5.cpp:54
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:236
iterator begin()
Definition: Module.h:569
void destroyConstant()
Called if some element of this constant is no longer valid.
Definition: Constants.cpp:279