From ac8be9e1f999ecf2cc298349420edc7f3f9c9fd6 Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Thu, 8 Nov 2018 14:08:35 -0800 Subject: [PATCH] Add a CC1 option to enable the Swift function-merging pass Patch by Linus Nilsson. --- include/clang/Driver/CC1Options.td | 2 ++ include/clang/Frontend/CodeGenOptions.def | 1 + lib/CodeGen/BackendUtil.cpp | 1 + lib/Driver/ToolChains/Clang.cpp | 8 ++++++++ lib/Frontend/CompilerInvocation.cpp | 1 + test/CodeGenCXX/merge-functions.cpp | 2 +- 6 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td index 67e7952742c..848d6dfc3dd 100644 --- a/include/clang/Driver/CC1Options.td +++ b/include/clang/Driver/CC1Options.td @@ -217,6 +217,8 @@ 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 fmerge_similar_functions : Flag<["-"], "fmerge-similar-functions">, + HelpText<"Permit merging of similar 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/include/clang/Frontend/CodeGenOptions.def b/include/clang/Frontend/CodeGenOptions.def index a7e71f7ac01..4be926c220c 100644 --- a/include/clang/Frontend/CodeGenOptions.def +++ b/include/clang/Frontend/CodeGenOptions.def @@ -119,6 +119,7 @@ CODEGENOPT(IncrementalLinkerCompatible, 1, 0) ///< Emit an object file which can ///< linker. CODEGENOPT(MergeAllConstants , 1, 1) ///< Merge identical constants. CODEGENOPT(MergeFunctions , 1, 0) ///< Set when -fmerge-functions is enabled. +CODEGENOPT(MergeSimilarFunctions , 1, 0) ///< Set when -fmerge-similar-functions is enabled. CODEGENOPT(MSVolatile , 1, 0) ///< Set when /volatile:ms 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 diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index 826ac267c68..5f50e265242 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -535,6 +535,7 @@ void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM, PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops; PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions; + PMBuilder.MergeSimilarFunctions = CodeGenOpts.MergeSimilarFunctions; PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO; PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO; PMBuilder.RerollLoops = CodeGenOpts.RerollLoops; diff --git a/lib/Driver/ToolChains/Clang.cpp b/lib/Driver/ToolChains/Clang.cpp index 36375347b92..e6496094d4e 100644 --- a/lib/Driver/ToolChains/Clang.cpp +++ b/lib/Driver/ToolChains/Clang.cpp @@ -1,3 +1,5 @@ + + //===--- LLVM.cpp - Clang+LLVM ToolChain Implementations --------*- C++ -*-===// // // The LLVM Compiler Infrastructure @@ -3407,6 +3409,12 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, options::OPT_fdelete_null_pointer_checks, false)) CmdArgs.push_back("-fno-delete-null-pointer-checks"); + if (Args.hasArg(options::OPT_fmerge_similar_functions)) + CmdArgs.push_back("-fmerge-similar-functions"); + + if (Args.hasArg(options::OPT_fmerge_functions)) + CmdArgs.push_back("-fmerge-functions"); + // LLVM Code Generator Options. if (Args.hasArg(options::OPT_frewrite_map_file) || diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 588a30d503e..1012af62978 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -743,6 +743,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, OPT_fno_unique_section_names, true); Opts.MergeFunctions = Args.hasArg(OPT_fmerge_functions); + Opts.MergeSimilarFunctions = Args.hasArg(OPT_fmerge_similar_functions); Opts.NoUseJumpTables = Args.hasArg(OPT_fno_jump_tables); diff --git a/test/CodeGenCXX/merge-functions.cpp b/test/CodeGenCXX/merge-functions.cpp index 2137f19c409..c3d0129f6c2 100644 --- a/test/CodeGenCXX/merge-functions.cpp +++ b/test/CodeGenCXX/merge-functions.cpp @@ -1,5 +1,5 @@ // REQUIRES: x86-registered-target -// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -O1 -fmerge-functions -emit-llvm -o - -x c++ < %s | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -O1 -fmerge-functions -fmerge-similar-functions -emit-llvm -o - -x c++ < %s | FileCheck %s // Basic functionality test. Function merging doesn't kick in on functions that // are too simple.