From f04f237e0cf5a5069ac55da432446a0d01feaaf5 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Fri, 24 Oct 2014 00:49:29 +0000 Subject: [PATCH] Add a new -fmerge-functions -cc1 flag that enables function merging. llvm-svn: 220543 --- clang/include/clang/Driver/CC1Options.td | 2 ++ clang/include/clang/Frontend/CodeGenOptions.def | 1 + clang/lib/CodeGen/BackendUtil.cpp | 1 + clang/lib/Frontend/CompilerInvocation.cpp | 1 + clang/test/CodeGenCXX/merge-functions.cpp | 14 ++++++++++++++ 5 files changed, 19 insertions(+) create mode 100644 clang/test/CodeGenCXX/merge-functions.cpp diff --git a/clang/include/clang/Driver/CC1Options.td b/clang/include/clang/Driver/CC1Options.td index 8d477cc..689b384 100644 --- a/clang/include/clang/Driver/CC1Options.td +++ b/clang/include/clang/Driver/CC1Options.td @@ -169,6 +169,8 @@ def no_implicit_float : Flag<["-"], "no-implicit-float">, HelpText<"Don't generate implicit floating point instructions">; def fdump_vtable_layouts : Flag<["-"], "fdump-vtable-layouts">, HelpText<"Dump the layouts of all vtables that will be emitted in a translation unit">; +def fmerge_functions : Flag<["-"], "fmerge-functions">, + HelpText<"Permit merging of identical functions when optimizing.">; def femit_coverage_notes : Flag<["-"], "femit-coverage-notes">, HelpText<"Emit a gcov coverage notes file when compiling.">; def femit_coverage_data: Flag<["-"], "femit-coverage-data">, diff --git a/clang/include/clang/Frontend/CodeGenOptions.def b/clang/include/clang/Frontend/CodeGenOptions.def index 06f4c1a..8a9ddd4 100644 --- a/clang/include/clang/Frontend/CodeGenOptions.def +++ b/clang/include/clang/Frontend/CodeGenOptions.def @@ -67,6 +67,7 @@ CODEGENOPT(InstrumentForProfiling , 1, 0) ///< Set when -pg is enabled. CODEGENOPT(LessPreciseFPMAD , 1, 0) ///< Enable less precise MAD instructions to ///< be generated. CODEGENOPT(MergeAllConstants , 1, 1) ///< Merge identical constants. +CODEGENOPT(MergeFunctions , 1, 0) ///< Set when -fmerge-functions is enabled. CODEGENOPT(NoCommon , 1, 0) ///< Set when -fno-common or C++ is enabled. CODEGENOPT(NoDwarfDirectoryAsm , 1, 0) ///< Set when -fno-dwarf-directory-asm is ///< enabled. diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp index 4f1f52f..ecabb08 100644 --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/clang/lib/CodeGen/BackendUtil.cpp @@ -247,6 +247,7 @@ void EmitAssemblyHelper::CreatePasses() { PMBuilder.DisableTailCalls = CodeGenOpts.DisableTailCalls; PMBuilder.DisableUnitAtATime = !CodeGenOpts.UnitAtATime; PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops; + PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions; PMBuilder.RerollLoops = CodeGenOpts.RerollLoops; PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible, diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 4563d2a..327857a 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -452,6 +452,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, OPT_fno_function_sections, false); Opts.DataSections = Args.hasFlag(OPT_fdata_sections, OPT_fno_data_sections, false); + Opts.MergeFunctions = Args.hasArg(OPT_fmerge_functions); Opts.VectorizeBB = Args.hasArg(OPT_vectorize_slp_aggressive); Opts.VectorizeLoop = Args.hasArg(OPT_vectorize_loops); diff --git a/clang/test/CodeGenCXX/merge-functions.cpp b/clang/test/CodeGenCXX/merge-functions.cpp new file mode 100644 index 0000000..2137f19 --- /dev/null +++ b/clang/test/CodeGenCXX/merge-functions.cpp @@ -0,0 +1,14 @@ +// REQUIRES: x86-registered-target +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -O1 -fmerge-functions -emit-llvm -o - -x c++ < %s | FileCheck %s + +// Basic functionality test. Function merging doesn't kick in on functions that +// are too simple. + +struct A { + virtual int f(int x, int *p) { return x ? *p : 1; } + virtual int g(int x, int *p) { return x ? *p : 1; } +} a; + +// CHECK: define {{.*}} @_ZN1A1gEiPi +// CHECK-NEXT: tail call i32 @_ZN1A1fEiPi +// CHECK-NEXT: ret -- 2.7.4