llvm-project/clang/lib/Frontend
Oliver Stannard 3b598b9c86 Reland: Dead Virtual Function Elimination
Remove dead virtual functions from vtables with
replaceNonMetadataUsesWith, so that CGProfile metadata gets cleaned up
correctly.

Original commit message:

Currently, it is hard for the compiler to remove unused C++ virtual
functions, because they are all referenced from vtables, which are referenced
by constructors. This means that if the constructor is called from any live
code, then we keep every virtual function in the final link, even if there
are no call sites which can use it.

This patch allows unused virtual functions to be removed during LTO (and
regular compilation in limited circumstances) by using type metadata to match
virtual function call sites to the vtable slots they might load from. This
information can then be used in the global dead code elimination pass instead
of the references from vtables to virtual functions, to more accurately
determine which functions are reachable.

To make this transformation safe, I have changed clang's code-generation to
always load virtual function pointers using the llvm.type.checked.load
intrinsic, instead of regular load instructions. I originally tried writing
this using clang's existing code-generation, which uses the llvm.type.test
and llvm.assume intrinsics after doing a normal load. However, it is possible
for optimisations to obscure the relationship between the GEP, load and
llvm.type.test, causing GlobalDCE to fail to find virtual function call
sites.

The existing linkage and visibility types don't accurately describe the scope
in which a virtual call could be made which uses a given vtable. This is
wider than the visibility of the type itself, because a virtual function call
could be made using a more-visible base class. I've added a new
!vcall_visibility metadata type to represent this, described in
TypeMetadata.rst. The internalization pass and libLTO have been updated to
change this metadata when linking is performed.

This doesn't currently work with ThinLTO, because it needs to see every call
to llvm.type.checked.load in the linkage unit. It might be possible to
extend this optimisation to be able to use the ThinLTO summary, as was done
for devirtualization, but until then that combination is rejected in the
clang driver.

To test this, I've written a fuzzer which generates random C++ programs with
complex class inheritance graphs, and virtual functions called through object
and function pointers of different types. The programs are spread across
multiple translation units and DSOs to test the different visibility
restrictions.

I've also tried doing bootstrap builds of LLVM to test this. This isn't
ideal, because only classes in anonymous namespaces can be optimised with
-fvisibility=default, and some parts of LLVM (plugins and bugpoint) do not
work correctly with -fvisibility=hidden. However, there are only 12 test
failures when building with -fvisibility=hidden (and an unmodified compiler),
and this change does not cause any new failures for either value of
-fvisibility.

On the 7 C++ sub-benchmarks of SPEC2006, this gives a geomean code-size
reduction of ~6%, over a baseline compiled with "-O2 -flto
-fvisibility=hidden -fwhole-program-vtables". The best cases are reductions
of ~14% in 450.soplex and 483.xalancbmk, and there are no code size
increases.

I've also run this on a set of 8 mbed-os examples compiled for Armv7M, which
show a geomean size reduction of ~3%, again with no size increases.

I had hoped that this would have no effect on performance, which would allow
it to awlays be enabled (when using -fwhole-program-vtables). However, the
changes in clang to use the llvm.type.checked.load intrinsic are causing ~1%
performance regression in the C++ parts of SPEC2006. It should be possible to
recover some of this perf loss by teaching optimisations about the
llvm.type.checked.load intrinsic, which would make it worth turning this on
by default (though it's still dependent on -fwhole-program-vtables).

Differential revision: https://reviews.llvm.org/D63932

llvm-svn: 375094
2019-10-17 09:58:57 +00:00
..
Rewrite RewriteModernObjC - silence static analyzer getAs<> null dereference warnings. NFCI. 2019-10-16 10:50:06 +00:00
ASTConsumers.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
ASTMerge.cpp [ASTImporter] Mark erroneous nodes in shared st 2019-07-01 15:37:07 +00:00
ASTUnit.cpp [Support] Add overload writeFileAtomically(std::function Writer) 2019-09-13 20:08:27 +00:00
CMakeLists.txt Move LangStandard*, InputKind::Language to Basic 2019-08-05 13:59:26 +00:00
ChainedDiagnosticConsumer.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ChainedIncludesSource.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
CompilerInstance.cpp Replace platform-dependent `stat` with `llvm::sys::fs::status`. NFC intended. 2019-10-16 19:12:34 +00:00
CompilerInvocation.cpp Reland: Dead Virtual Function Elimination 2019-10-17 09:58:57 +00:00
CreateInvocationFromCommandLine.cpp ArrayRef'ized CompilerInvocation::CreateFromArgs 2019-08-27 22:13:31 +00:00
DependencyFile.cpp [clang][DependencyFileGenerator] Fix missing -MT option handling 2019-09-07 00:59:13 +00:00
DependencyGraph.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
DiagnosticRenderer.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
FrontendAction.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
FrontendActions.cpp [Concepts] Concept Specialization Expressions 2019-10-15 15:24:26 +00:00
FrontendOptions.cpp Move LangStandard*, InputKind::Language to Basic 2019-08-05 13:59:26 +00:00
FrontendTiming.cpp Fix file headers. NFC 2019-03-01 06:49:51 +00:00
HeaderIncludeGen.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
InitHeaderSearch.cpp Reland r371785: Add -Wpoison-system-directories warning 2019-09-13 18:00:51 +00:00
InitPreprocessor.cpp [clang-scan-deps] Support for clang --analyze in clang-scan-deps 2019-10-14 20:15:01 +00:00
InterfaceStubFunctionsConsumer.cpp [clang][IFS] Escape mangled names so MS ABI doesn't break YAML parsing. 2019-10-14 18:03:03 +00:00
LayoutOverrideSource.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
LogDiagnosticPrinter.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
ModuleDependencyCollector.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
MultiplexConsumer.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
PrecompiledPreamble.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
PrintPreprocessedOutput.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
SerializedDiagnosticPrinter.cpp [Clang] Migrate llvm::make_unique to std::make_unique 2019-08-14 23:04:18 +00:00
SerializedDiagnosticReader.cpp Bitstream reader: Fix undefined behavior seen after rL364464 2019-07-05 20:22:40 +00:00
TestModuleFileExtension.cpp [Bitcode] Move Bitstream to a separate library 2019-07-03 22:40:07 +00:00
TestModuleFileExtension.h [Bitcode] Move Bitstream to a separate library 2019-07-03 22:40:07 +00:00
TextDiagnostic.cpp [lldb] Print better diagnostics for user expressions and modules 2019-09-18 08:53:35 +00:00
TextDiagnosticBuffer.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
TextDiagnosticPrinter.cpp Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
VerifyDiagnosticConsumer.cpp Introduce FileEntryRef and use it when handling includes to report correct dependencies 2019-08-22 18:15:50 +00:00