Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -686,6 +686,9 @@ def flto_EQ : Joined<["-"], "flto=">, Group; def flto : Flag<["-"], "flto">, Flags<[CC1Option]>, Group; def fno_lto : Flag<["-"], "fno-lto">, Group; +def fthinlto : Flag<["-"], "fthinlto">, Flags<[CC1Option]>, Group; +def fthinlto_be : Flag<["-"], "fthinlto-be">, Flags<[CC1Option]>, Group; +def fno_thinlto : Flag<["-"], "fno-thinlto">, Group; def fmacro_backtrace_limit_EQ : Joined<["-"], "fmacro-backtrace-limit=">, Group, Flags<[DriverOption, CoreOption]>; def fmerge_all_constants : Flag<["-"], "fmerge-all-constants">, Group; Index: include/clang/Frontend/CodeGenOptions.def =================================================================== --- include/clang/Frontend/CodeGenOptions.def +++ include/clang/Frontend/CodeGenOptions.def @@ -72,7 +72,9 @@ CODEGENOPT(InstrumentForProfiling , 1, 0) ///< Set when -pg is enabled. CODEGENOPT(LessPreciseFPMAD , 1, 0) ///< Enable less precise MAD instructions to ///< be generated. -CODEGENOPT(PrepareForLTO , 1, 0) ///< Set when -flto is enabled on the +CODEGENOPT(PrepareForLTO , 1, 0) ///< Set when -flto or -fthinlto is enabled + ///< on the compile step. +CODEGENOPT(EmitThinLTOIndex , 1, 0) ///< Set when -fthinlto is enabled on the ///< compile step. CODEGENOPT(MergeAllConstants , 1, 1) ///< Merge identical constants. CODEGENOPT(MergeFunctions , 1, 0) ///< Set when -fmerge-functions is enabled. Index: lib/CodeGen/BackendUtil.cpp =================================================================== --- lib/CodeGen/BackendUtil.cpp +++ lib/CodeGen/BackendUtil.cpp @@ -268,6 +268,7 @@ return; unsigned OptLevel = CodeGenOpts.OptimizationLevel; + CodeGenOptions::InliningMethod Inlining = CodeGenOpts.getInlining(); // Handle disabling of LLVM optimization, where we want to preserve the @@ -610,7 +611,8 @@ case Backend_EmitBC: getPerModulePasses()->add( - createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists)); + createBitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, + CodeGenOpts.EmitThinLTOIndex)); break; case Backend_EmitLL: Index: lib/Driver/Driver.cpp =================================================================== --- lib/Driver/Driver.cpp +++ lib/Driver/Driver.cpp @@ -1576,7 +1576,8 @@ } bool Driver::IsUsingLTO(const ArgList &Args) const { - return Args.hasFlag(options::OPT_flto, options::OPT_fno_lto, false); + return Args.hasFlag(options::OPT_flto, options::OPT_fno_lto, false) || + Args.hasFlag(options::OPT_fthinlto, options::OPT_fno_thinlto, false); } void Driver::BuildJobs(Compilation &C) const { Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -1664,6 +1664,9 @@ std::string CPU = getCPUName(Args, ToolChain.getTriple()); if (!CPU.empty()) CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=mcpu=") + CPU)); + + if (Args.hasArg(options::OPT_fthinlto)) + CmdArgs.push_back("-plugin-opt=thinlto"); } /// This is a helper function for validating the optional refinement step @@ -2818,6 +2821,8 @@ // preprocessing, precompiling or assembling. Args.ClaimAllArgs(options::OPT_flto); Args.ClaimAllArgs(options::OPT_fno_lto); + Args.ClaimAllArgs(options::OPT_fthinlto); + Args.ClaimAllArgs(options::OPT_fno_thinlto); } static void appendUserToPath(SmallVectorImpl &Result) { @@ -3236,6 +3241,8 @@ "Invalid action for clang tool."); if (JA.getType() == types::TY_LTO_IR || JA.getType() == types::TY_LTO_BC) { + // Enables PrepareForLTO, which we want for -fthinlto as well. + // ThinLTO also uses the TY_LTO_* types. CmdArgs.push_back("-flto"); } if (JA.getType() == types::TY_Nothing) { @@ -3268,6 +3275,10 @@ // the use-list order, since serialization to bitcode is part of the flow. if (JA.getType() == types::TY_LLVM_BC) CmdArgs.push_back("-emit-llvm-uselists"); + + if (Args.hasArg(options::OPT_fthinlto)) { + CmdArgs.push_back("-fthinlto"); + } } // We normally speed up the clang process a bit by skipping destructors at Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -518,6 +518,7 @@ Opts.MergeFunctions = Args.hasArg(OPT_fmerge_functions); Opts.PrepareForLTO = Args.hasArg(OPT_flto); + Opts.EmitThinLTOIndex = Args.hasArg(OPT_fthinlto); Opts.MSVolatile = Args.hasArg(OPT_fms_volatile); Index: test/Driver/thinlto.c =================================================================== --- /dev/null +++ test/Driver/thinlto.c @@ -0,0 +1,11 @@ +// -fthinlto causes a switch to llvm-bc object files. +// RUN: %clang -ccc-print-phases -c %s -fthinlto 2> %t.log +// RUN: grep '2: compiler, {1}, ir' %t.log +// RUN: grep '3: backend, {2}, lto-bc' %t.log + +// RUN: %clang -ccc-print-phases %s -fthinlto 2> %t.log +// RUN: grep '0: input, ".*thinlto.c", c' %t.log +// RUN: grep '1: preprocessor, {0}, cpp-output' %t.log +// RUN: grep '2: compiler, {1}, ir' %t.log +// RUN: grep '3: backend, {2}, lto-bc' %t.log +// RUN: grep '4: linker, {3}, image' %t.log Index: test/Misc/thinlto.c =================================================================== --- /dev/null +++ test/Misc/thinlto.c @@ -0,0 +1,22 @@ +// RUN: %clang_cc1 -fthinlto -emit-llvm-bc < %s | llvm-bcanalyzer -dump | FileCheck %s +// CHECK: