This patch adds support for the -fno-optimize-sibling-calls flag. The
driver lowers -fno-optimize-sibling-calls to -mdisable-tail-calls and cc1
will ask llvm to not do any tail/sibling call optimization in the codegen
layer. Thus far this is only implemented in LLVM's x86 backend.
The goal is to produce better ASAN reports by preserving correct call
stacks, though I don't enable it by default with -faddress-sanitizer. Maybe
the ASAN folks will want to add that after this lands, I leave it to them.
Please review!
Nick
Index: test/Driver/no-sibling-calls.c
===================================================================
--- test/Driver/no-sibling-calls.c (revision 0)
+++ test/Driver/no-sibling-calls.c (revision 0)
@@ -0,0 +1,8 @@
+// RUN: %clang -### %s -fno-optimize-sibling-calls 2> %t
+// RUN: FileCheck --check-prefix=CHECK-NOSC < %t %s
+// CHECK-NOSC: "-mdisable-tail-calls"
+
+// RUN: %clang -### -foptimize-sibling-calls %s 2> %t
+// RUN: FileCheck --check-prefix=CHECK-OSC < %t %s
+// CHECK-OSC-NOT: "-mdisable-tail-calls"
+
Index: include/clang/Frontend/CodeGenOptions.h
===================================================================
--- include/clang/Frontend/CodeGenOptions.h (revision 148442)
+++ include/clang/Frontend/CodeGenOptions.h (working copy)
@@ -50,6 +50,7 @@
/// internal state before optimizations are
/// done.
unsigned DisableRedZone : 1; /// Set when -mno-red-zone is enabled.
+ unsigned DisableTailCalls : 1; /// Do not emit tail calls.
unsigned EmitDeclMetadata : 1; /// Emit special metadata indicating what
/// Decl* various IR entities came from. Only
/// useful when running CodeGen as a
Index: include/clang/Driver/CC1Options.td
===================================================================
--- include/clang/Driver/CC1Options.td (revision 148442)
+++ include/clang/Driver/CC1Options.td (working copy)
@@ -166,6 +166,8 @@
HelpText<"Enable additional debug output">;
def mdisable_fp_elim : Flag<"-mdisable-fp-elim">,
HelpText<"Disable frame pointer elimination optimization">;
+def mdisable_tail_calls : Flag<"-mdisable-tail-calls">,
+ HelpText<"Disable tail call optimization, keeping the call stack accurate">;
def menable_no_infinities : Flag<"-menable-no-infs">,
HelpText<"Allow optimization to assume there are no infinities.">;
def menable_no_nans : Flag<"-menable-no-nans">,
Index: include/clang/Driver/Options.td
===================================================================
--- include/clang/Driver/Options.td (revision 148442)
+++ include/clang/Driver/Options.td (working copy)
@@ -475,6 +475,8 @@
def fobjc : Flag<"-fobjc">, Group<f_Group>;
def fomit_frame_pointer : Flag<"-fomit-frame-pointer">, Group<f_Group>;
def fopenmp : Flag<"-fopenmp">, Group<f_Group>;
+def fno_optimize_sibling_calls : Flag<"-fno-optimize-sibling-calls">, Group<f_Group>;
+def foptimize_sibling_calls : Flag<"-foptimize-sibling-calls">, Group<f_Group>;
def force__cpusubtype__ALL : Flag<"-force_cpusubtype_ALL">;
def force__flat__namespace : Flag<"-force_flat_namespace">;
def force__load : Separate<"-force_load">;
Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp (revision 148442)
+++ lib/Frontend/CompilerInvocation.cpp (working copy)
@@ -146,6 +146,8 @@
Res.push_back("-disable-llvm-optzns");
if (Opts.DisableRedZone)
Res.push_back("-disable-red-zone");
+ if (Opts.DisableTailCalls)
+ Res.push_back("-mdisable-tail-calls");
if (!Opts.DebugCompilationDir.empty()) {
Res.push_back("-fdebug-compilation-dir");
Res.push_back(Opts.DebugCompilationDir);
@@ -1100,6 +1102,7 @@
Opts.CodeModel = Args.getLastArgValue(OPT_mcode_model);
Opts.DebugPass = Args.getLastArgValue(OPT_mdebug_pass);
Opts.DisableFPElim = Args.hasArg(OPT_mdisable_fp_elim);
+ Opts.DisableTailCalls = Args.hasArg(OPT_mdisable_tail_calls);
Opts.FloatABI = Args.getLastArgValue(OPT_mfloat_abi);
Opts.HiddenWeakVTables = Args.hasArg(OPT_fhidden_weak_vtables);
Opts.LessPreciseFPMAD = Args.hasArg(OPT_cl_mad_enable);
Index: lib/Driver/Tools.cpp
===================================================================
--- lib/Driver/Tools.cpp (revision 148442)
+++ lib/Driver/Tools.cpp (working copy)
@@ -1440,6 +1440,9 @@
options::OPT_fno_strict_aliasing,
getToolChain().IsStrictAliasingDefault()))
CmdArgs.push_back("-relaxed-aliasing");
+ if (!Args.hasFlag(options::OPT_foptimize_sibling_calls,
+ options::OPT_fno_optimize_sibling_calls))
+ CmdArgs.push_back("-mdisable-tail-calls");
// Handle various floating point optimization flags, mapping them to the
// appropriate LLVM code generation flags. The pattern for all of these is to
Index: lib/CodeGen/BackendUtil.cpp
===================================================================
--- lib/CodeGen/BackendUtil.cpp (revision 148442)
+++ lib/CodeGen/BackendUtil.cpp (working copy)
@@ -326,6 +326,7 @@
Options.UseSoftFloat = CodeGenOpts.SoftFloat;
Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
Options.RealignStack = CodeGenOpts.StackRealignment;
+ Options.DisableTailCalls = CodeGenOpts.DisableTailCalls;
TargetMachine *TM = TheTarget->createTargetMachine(Triple, TargetOpts.CPU,
FeaturesStr, Options,
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits