18 #define DEBUG_TYPE "globaldce"
27 STATISTIC(NumAliases ,
"Number of global aliases removed");
28 STATISTIC(NumFunctions,
"Number of functions removed");
29 STATISTIC(NumVariables,
"Number of global variables removed");
41 bool runOnModule(
Module &M);
50 void MarkUsedGlobalsAsNeeded(
Constant *
C);
58 "Dead Global Elimination",
false,
false)
62 bool GlobalDCE::runOnModule(
Module &M) {
67 Changed |= RemoveUnusedGlobalValue(*
I);
69 if (!
I->isDiscardableIfUnused() &&
70 !
I->isDeclaration() && !
I->hasAvailableExternallyLinkage())
76 Changed |= RemoveUnusedGlobalValue(*
I);
79 if (!
I->isDiscardableIfUnused() &&
80 !
I->isDeclaration() && !
I->hasAvailableExternallyLinkage())
86 Changed |= RemoveUnusedGlobalValue(*
I);
88 if (!
I->isDiscardableIfUnused())
97 std::vector<GlobalVariable*> DeadGlobalVars;
100 if (!AliveGlobals.count(
I)) {
101 DeadGlobalVars.push_back(
I);
102 I->setInitializer(0);
106 std::vector<Function*> DeadFunctions;
108 if (!AliveGlobals.count(
I)) {
109 DeadFunctions.push_back(
I);
110 if (!
I->isDeclaration())
115 std::vector<GlobalAlias*> DeadAliases;
118 if (!AliveGlobals.count(
I)) {
119 DeadAliases.push_back(
I);
123 if (!DeadFunctions.empty()) {
126 for (
unsigned i = 0, e = DeadFunctions.size(); i != e; ++i) {
127 RemoveUnusedGlobalValue(*DeadFunctions[i]);
130 NumFunctions += DeadFunctions.size();
134 if (!DeadGlobalVars.empty()) {
135 for (
unsigned i = 0, e = DeadGlobalVars.size(); i != e; ++i) {
136 RemoveUnusedGlobalValue(*DeadGlobalVars[i]);
139 NumVariables += DeadGlobalVars.size();
144 if (!DeadAliases.empty()) {
145 for (
unsigned i = 0, e = DeadAliases.size(); i != e; ++i) {
146 RemoveUnusedGlobalValue(*DeadAliases[i]);
149 NumAliases += DeadAliases.size();
154 AliveGlobals.clear();
155 SeenConstants.clear();
164 if (!AliveGlobals.insert(G))
170 if (GV->hasInitializer())
171 MarkUsedGlobalsAsNeeded(GV->getInitializer());
172 }
else if (
GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) {
174 MarkUsedGlobalsAsNeeded(GA->getAliasee());
190 else if (
Constant *
C = dyn_cast<Constant>(*U))
191 MarkUsedGlobalsAsNeeded(
C);
195 void GlobalDCE::MarkUsedGlobalsAsNeeded(
Constant *
C) {
197 return GlobalIsNeeded(GV);
204 if (Op && SeenConstants.insert(Op))
205 MarkUsedGlobalsAsNeeded(Op);
215 bool GlobalDCE::RemoveUnusedGlobalValue(
GlobalValue &GV) {
static PassRegistry * getPassRegistry()
The main container class for the LLVM Intermediate Representation.
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const GlobalListType & getGlobalList() const
Get the Module's list of global variables (constant).
STATISTIC(NumAliases,"Number of global aliases removed")
const AliasListType & getAliasList() const
Get the Module's list of aliases (constant).
ID
LLVM Calling Convention Representation.
global_iterator global_begin()
INITIALIZE_PASS(GlobalDCE,"globaldce","Dead Global Elimination", false, false) ModulePass *llvm
ModulePass * createGlobalDCEPass()
alias_iterator alias_end()
LLVM Constant Representation.
iterator erase(iterator where)
global_iterator global_end()
const FunctionListType & getFunctionList() const
Get the Module's list of functions (constant).
alias_iterator alias_begin()
void initializeGlobalDCEPass(PassRegistry &)
bool hasPrefixData() const
Constant * getPrefixData() const
void removeDeadConstantUsers() const