From 22de16dc7582dac43429ce0dcb374604020c01f5 Mon Sep 17 00:00:00 2001 From: Nick Lewycky Date: Thu, 19 Jan 2012 00:34:10 +0000 Subject: [PATCH] Add a TargetOption for disabling tail calls. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148442 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Target/TargetOptions.h | 9 +++++-- lib/Target/X86/X86ISelLowering.cpp | 5 +++- test/CodeGen/X86/tailcall-disable.ll | 40 ++++++++++++++++++++++++++++ tools/llc/llc.cpp | 6 +++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 test/CodeGen/X86/tailcall-disable.ll diff --git a/include/llvm/Target/TargetOptions.h b/include/llvm/Target/TargetOptions.h index 3f42bbe16a0..852e67f5829 100644 --- a/include/llvm/Target/TargetOptions.h +++ b/include/llvm/Target/TargetOptions.h @@ -43,8 +43,9 @@ namespace llvm { NoNaNsFPMath(false), HonorSignDependentRoundingFPMathOption(false), UseSoftFloat(false), NoZerosInBSS(false), JITExceptionHandling(false), JITEmitDebugInfo(false), JITEmitDebugInfoToDisk(false), - GuaranteedTailCallOpt(false), StackAlignmentOverride(0), - RealignStack(true), DisableJumpTables(false), EnableFastISel(false), + GuaranteedTailCallOpt(false), DisableTailCalls(false), + StackAlignmentOverride(0), RealignStack(true), + DisableJumpTables(false), EnableFastISel(false), EnableSegmentedStacks(false), TrapFuncName(""), FloatABIType(FloatABI::Default) {} @@ -147,6 +148,10 @@ namespace llvm { /// as their parent function, etc.), using an alternate ABI if necessary. unsigned GuaranteedTailCallOpt : 1; + /// DisableTailCalls - This flag controls whether we will use tail calls. + /// Disabling them may be useful to maintain a correct call stack. + unsigned DisableTailCalls : 1; + /// StackAlignmentOverride - Override default stack alignment for target. unsigned StackAlignmentOverride; diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp index 91dc7b51d89..858ebc5de53 100644 --- a/lib/Target/X86/X86ISelLowering.cpp +++ b/lib/Target/X86/X86ISelLowering.cpp @@ -1732,7 +1732,7 @@ static bool IsTailCallConvention(CallingConv::ID CC) { } bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { - if (!CI->isTailCall()) + if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls) return false; CallSite CS(CI); @@ -2133,6 +2133,9 @@ X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee, bool IsStructRet = CallIsStructReturn(Outs); bool IsSibcall = false; + if (MF.getTarget().Options.DisableTailCalls) + isTailCall = false; + if (isTailCall) { // Check if it's really possible to do a tail call. isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, diff --git a/test/CodeGen/X86/tailcall-disable.ll b/test/CodeGen/X86/tailcall-disable.ll new file mode 100644 index 00000000000..a561b74609a --- /dev/null +++ b/test/CodeGen/X86/tailcall-disable.ll @@ -0,0 +1,40 @@ +; RUN: llc -disable-tail-calls < %s | FileCheck --check-prefix=CALL %s +; RUN: llc < %s | FileCheck --check-prefix=JMP %s + +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +define i32 @helper() nounwind { +entry: + ret i32 7 +} + +define i32 @test1() nounwind { +entry: + %call = tail call i32 @helper() + ret i32 %call +} + +;CALL: test1: +;CALL-NOT: ret +;CALL: callq helper +;CALL: ret + +;JMP: test1: +;JMP-NOT: ret +;JMP: jmp helper # TAILCALL + +define i32 @test2() nounwind { +entry: + %call = tail call i32 @test2() + ret i32 %call +} + +;CALL: test2: +;CALL-NOT: ret +;CALL: callq test2 +;CALL: ret + +;JMP: test2: +;JMP-NOT: ret +;JMP: jmp test2 # TAILCALL diff --git a/tools/llc/llc.cpp b/tools/llc/llc.cpp index 790f9e6027b..5fabdaf0691 100644 --- a/tools/llc/llc.cpp +++ b/tools/llc/llc.cpp @@ -237,6 +237,11 @@ EnableGuaranteedTailCallOpt("tailcallopt", cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."), cl::init(false)); +static cl::opt +DisableTailCalls("disable-tail-calls", + cl::desc("Never emit tail calls"), + cl::init(false)); + static cl::opt OverrideStackAlignment("stack-alignment", cl::desc("Override default stack alignment"), @@ -462,6 +467,7 @@ int main(int argc, char **argv) { Options.JITEmitDebugInfo = EmitJitDebugInfo; Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk; Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt; + Options.DisableTailCalls = DisableTailCalls; Options.StackAlignmentOverride = OverrideStackAlignment; Options.RealignStack = EnableRealignStack; Options.DisableJumpTables = DisableSwitchTables;