Index: include/clang/Basic/CodeGenOptions.def =================================================================== --- include/clang/Basic/CodeGenOptions.def +++ include/clang/Basic/CodeGenOptions.def @@ -109,6 +109,7 @@ CODEGENOPT(InstrumentForProfiling , 1, 0) ///< Set when -pg is enabled. CODEGENOPT(CallFEntry , 1, 0) ///< Set when -mfentry is enabled. +CODEGENOPT(MNopMCount , 1, 0) ///< Set when -mnop-mcount 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 Index: include/clang/Basic/DiagnosticCommonKinds.td =================================================================== --- include/clang/Basic/DiagnosticCommonKinds.td +++ include/clang/Basic/DiagnosticCommonKinds.td @@ -270,6 +270,8 @@ "-mcmse is not supported for %0">; def err_opt_not_valid_with_opt : Error< "option '%0' cannot be specified with '%1'">; +def err_opt_not_valid_without_opt : Error< + "option '%0' cannot be specified without '%1'">; def err_opt_not_valid_on_target : Error< "option '%0' cannot be specified on this target">; Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -2389,6 +2389,8 @@ def mno_pie_copy_relocations : Flag<["-"], "mno-pie-copy-relocations">, Group; def mfentry : Flag<["-"], "mfentry">, HelpText<"Insert calls to fentry at function entry (x86/SystemZ only)">, Flags<[CC1Option]>, Group; +def mnop_mcount : Flag<["-"], "mnop-mcount">, HelpText<"Generate mcount/__fentry__ calls as nops. To activate they need to be patched in.">, + Flags<[CC1Option]>, Group; def mips16 : Flag<["-"], "mips16">, Group; def mno_mips16 : Flag<["-"], "mno-mips16">, Group; def mmicromips : Flag<["-"], "mmicromips">, Group; Index: lib/CodeGen/CodeGenFunction.cpp =================================================================== --- lib/CodeGen/CodeGenFunction.cpp +++ lib/CodeGen/CodeGenFunction.cpp @@ -889,6 +889,16 @@ Fn->addFnAttr("instrument-function-entry-inlined", getTarget().getMCountName()); } + if (CGM.getCodeGenOpts().MNopMCount) { + if (getContext().getTargetInfo().getTriple().getArch() != + llvm::Triple::systemz) + CGM.getDiags().Report(diag::err_opt_not_valid_on_target) + << "-mnop-mcount"; + if (!CGM.getCodeGenOpts().CallFEntry) + CGM.getDiags().Report(diag::err_opt_not_valid_without_opt) + << "-mnop-mcount" << "-mfentry"; + Fn->addFnAttr("mnop-mcount", "true"); + } } } Index: lib/Driver/ToolChains/Clang.cpp =================================================================== --- lib/Driver/ToolChains/Clang.cpp +++ lib/Driver/ToolChains/Clang.cpp @@ -4681,6 +4681,9 @@ if (TC.SupportsProfiling()) Args.AddLastArg(CmdArgs, options::OPT_mfentry); + if (TC.SupportsProfiling()) + Args.AddLastArg(CmdArgs, options::OPT_mnop_mcount); + if (Args.getLastArg(options::OPT_fapple_kext) || (Args.hasArg(options::OPT_mkernel) && types::isCXX(InputType))) CmdArgs.push_back("-fapple-kext"); Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -1092,6 +1092,7 @@ Opts.InstrumentForProfiling = Args.hasArg(OPT_pg); Opts.CallFEntry = Args.hasArg(OPT_mfentry); + Opts.MNopMCount = Args.hasArg(OPT_mnop_mcount); Opts.EmitOpenCLArgMetadata = Args.hasArg(OPT_cl_kernel_arg_info); if (const Arg *A = Args.getLastArg(OPT_fcf_protection_EQ)) { Index: test/CodeGen/mnop-mcount.c =================================================================== --- /dev/null +++ test/CodeGen/mnop-mcount.c @@ -0,0 +1,26 @@ +// RUN: %clang_cc1 -pg -mfentry -mnop-mcount -triple s390x-ibm-linux -emit-llvm \ +// RUN: -o - %s 2>&1 | FileCheck %s +// RUN: not %clang_cc1 -pg -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOMFENTRY %s +// RUN: %clang_cc1 -mfentry -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - \ +// RUN: %s 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: %clang_cc1 -mnop-mcount -triple s390x-ibm-linux -emit-llvm -o - %s \ +// RUN: 2>&1 | FileCheck -check-prefix=NOPG %s +// RUN: not %clang_cc1 -pg -mfentry -mnop-mcount -triple x86_64-linux-gnu \ +// RUN: -emit-llvm -o - %s 2>&1 | FileCheck -check-prefix=X86 %s + +int foo(void) { + return 0; +} + +int __attribute__((no_instrument_function)) no_instrument(void) { + return foo(); +} + +//CHECK: attributes #0 = { {{.*}}"mnop-mcount"="true"{{.*}} } +//CHECK: attributes #1 = { {{.*}} } +//CHECK-NOT: attributes #1 = { {{.*}}"mnop-mcount"="true"{{.*}} } +//NOMFENTRY: error: option '-mnop-mcount' cannot be specified without '-mfentry' +//NOPG-NOT: attributes #0 = { {{.*}}"mnop-mcount"{{.*}} } +//NOPG-NOT: attributes #1 = { {{.*}}"mnop-mcount"{{.*}} } +//X86: error: option '-mnop-mcount' cannot be specified on this target