LLVM  14.0.0git
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/InitializePasses.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);
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 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
llvm::Function
Definition: Function.h:62
Pass.h
Statistic.h
INITIALIZE_PASS
INITIALIZE_PASS(EliminateAvailableExternallyLegacyPass, "elim-avail-extern", "Eliminate Available Externally Globals", false, false) ModulePass *llvm
Definition: ElimAvailExtern.cpp:97
llvm::GlobalVariable
Definition: GlobalVariable.h:40
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:158
ElimAvailExtern.h
Module.h
F
#define F(x, y, z)
Definition: MD5.cpp:56
GlobalValue.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::STATISTIC
STATISTIC(NumFunctions, "Total number of functions")
llvm::isSafeToDestroyConstant
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
Definition: GlobalStatus.cpp:44
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
IPO.h
GlobalStatus.h
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::Init
Definition: Record.h:275
Constant.h
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
eliminateAvailableExternally
static bool eliminateAvailableExternally(Module &M)
Definition: ElimAvailExtern.cpp:33
GlobalVariable.h
Function.h
llvm::GlobalValue::ExternalLinkage
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:48
llvm::createEliminateAvailableExternallyPass
ModulePass * createEliminateAvailableExternallyPass()
This transform is designed to eliminate available external globals (functions or global variables)
llvm::EliminateAvailableExternallyPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
Definition: ElimAvailExtern.cpp:68
llvm::initializeEliminateAvailableExternallyLegacyPassPass
void initializeEliminateAvailableExternallyLegacyPassPass(PassRegistry &)
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38