GlobalOpt is already dead-code-eliminating global definitions. With
this change it also takes care of declarations.
Hopefully this should make it now a strict superset of GlobalDCE.
This is important for LTO/ThinLTO as we don't want the linker to see
"undefined reference" when it processes the input files: it could
prevent proper internalization (or even load an extra file from a
static archive, changing the behavior of the program!).
llvm-svn: 281653
SmallSet<const Comdat *, 8> &NotDiscardableComdats) {
GV.removeDeadConstantUsers();
- if (!GV.isDiscardableIfUnused())
+ if (!GV.isDiscardableIfUnused() && !GV.isDeclaration())
return false;
if (const Comdat *C = GV.getComdat())
bool Dead;
if (auto *F = dyn_cast<Function>(&GV))
- Dead = F->isDefTriviallyDead();
+ Dead = (F->isDeclaration() && F->use_empty()) || F->isDefTriviallyDead();
else
Dead = GV.use_empty();
if (!Dead)
--- /dev/null
+; RUN: opt < %s -globalopt -S | FileCheck %s
+
+; CHECK-NOT: aa
+; CHECK-NOT: bb
+
+declare void @aa()
+@bb = external global i8