[llvm] r256274 - Merge duplicated code.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 22 11:38:08 PST 2015
Author: rafael
Date: Tue Dec 22 13:38:07 2015
New Revision: 256274
URL: http://llvm.org/viewvc/llvm-project?rev=256274&view=rev
Log:
Merge duplicated code.
The code for deleting dead global variables and functions was
duplicated.
This is in preparation for also deleting dead global aliases.
Modified:
llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
Modified: llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp?rev=256274&r1=256273&r2=256274&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp Tue Dec 22 13:38:07 2015
@@ -55,7 +55,6 @@ STATISTIC(NumSRA , "Number of aggr
STATISTIC(NumHeapSRA , "Number of heap objects SRA'd");
STATISTIC(NumSubstitute,"Number of globals with initializers stored into them");
STATISTIC(NumDeleted , "Number of globals deleted");
-STATISTIC(NumFnDeleted , "Number of functions deleted");
STATISTIC(NumGlobUses , "Number of global uses devirtualized");
STATISTIC(NumLocalized , "Number of globals localized");
STATISTIC(NumShrunkToBool , "Number of global vars shrunk to booleans");
@@ -83,6 +82,7 @@ namespace {
bool OptimizeFunctions(Module &M);
bool OptimizeGlobalVars(Module &M);
bool OptimizeGlobalAliases(Module &M);
+ bool deleteIfDead(GlobalValue &GV);
bool processGlobal(GlobalVariable *GV);
bool processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS);
bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn);
@@ -1679,20 +1679,37 @@ static bool TryToShrinkGlobalToBoolean(G
return true;
}
+bool GlobalOpt::deleteIfDead(GlobalValue &GV) {
+ GV.removeDeadConstantUsers();
+
+ if (!GV.isDiscardableIfUnused())
+ return false;
+
+ if (const Comdat *C = GV.getComdat())
+ if (!GV.hasLocalLinkage() && NotDiscardableComdats.count(C))
+ return false;
+
+ bool Dead;
+ if (auto *F = dyn_cast<Function>(&GV))
+ Dead = F->isDefTriviallyDead();
+ else
+ Dead = GV.use_empty();
+ if (!Dead)
+ return false;
+
+ DEBUG(dbgs() << "GLOBAL DEAD: " << GV << "\n");
+ GV.eraseFromParent();
+ ++NumDeleted;
+ return true;
+}
/// Analyze the specified global variable and optimize it if possible. If we
/// make a change, return true.
bool GlobalOpt::processGlobal(GlobalVariable *GV) {
- // Do more involved optimizations if the global is internal.
- GV->removeDeadConstantUsers();
-
- if (GV->use_empty()) {
- DEBUG(dbgs() << "GLOBAL DEAD: " << *GV << "\n");
- GV->eraseFromParent();
- ++NumDeleted;
+ if (deleteIfDead(*GV))
return true;
- }
+ // Do more involved optimizations if the global is internal.
if (!GV->hasLocalLinkage())
return false;
@@ -2023,13 +2040,8 @@ bool GlobalOpt::OptimizeFunctions(Module
if (!F->hasName() && !F->isDeclaration() && !F->hasLocalLinkage())
F->setLinkage(GlobalValue::InternalLinkage);
- const Comdat *C = F->getComdat();
- bool inComdat = C && NotDiscardableComdats.count(C);
- F->removeDeadConstantUsers();
- if ((!inComdat || F->hasLocalLinkage()) && F->isDefTriviallyDead()) {
- F->eraseFromParent();
+ if (deleteIfDead(*F)) {
Changed = true;
- ++NumFnDeleted;
continue;
}
if (!F->hasLocalLinkage())
@@ -2075,12 +2087,7 @@ bool GlobalOpt::OptimizeGlobalVars(Modul
GV->setInitializer(New);
}
- if (GV->isDiscardableIfUnused()) {
- if (const Comdat *C = GV->getComdat())
- if (NotDiscardableComdats.count(C) && !GV->hasLocalLinkage())
- continue;
- Changed |= processGlobal(GV);
- }
+ Changed |= processGlobal(GV);
}
return Changed;
}
More information about the llvm-commits
mailing list