From patchwork Sun Sep 2 14:27:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 965106 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-484946-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="SqmiuqJG"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="UVVKS+Tz"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 423FnS1KwNz9ryn for ; Mon, 3 Sep 2018 00:28:11 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; q=dns; s=default; b=dQ3ZJzl75ZK6AFtEM hFeun45J+j3bCK6p1blbbcflsI/xwa+k7V6WNoPi8X9oIIZX5Ndk27bLOyb5adqT E/BEsiu9CxVzxT0tMklqfCV6Y6KtzjlhyTJvCq37FGnwhx0jZYBUkpMMC7W2R2Zj VL2qAL7PLsrRpEIY7RMoalks24= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=default; bh=HBhzFxYMByxf7k8xcfjyG1b VryE=; b=SqmiuqJGfZz/LVKlgtjA2/+vvIwRzka9N9tk565ngPNDW3TdJufNpOx 44EOK6QGu4nWQNRU+1rcSm8Vzd6cpkPuboN9mEqc3xencKycZBzB7IgxXsPzOZev 5yN+qqtoClh41dtvlUhc05/j7l3ubexwAlisE6ElK6H09TLVFbB4= Received: (qmail 65350 invoked by alias); 2 Sep 2018 14:28:04 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 65341 invoked by uid 89); 2 Sep 2018 14:28:03 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.8 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=HX-Received:sk:z20-v6m, optimize, GET_CODE, get_code X-HELO: mail-pg1-f194.google.com Received: from mail-pg1-f194.google.com (HELO mail-pg1-f194.google.com) (209.85.215.194) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 02 Sep 2018 14:28:01 +0000 Received: by mail-pg1-f194.google.com with SMTP id 2-v6so6934271pgo.4 for ; Sun, 02 Sep 2018 07:28:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=HraxYxfIqOVmtDbOerniQRNuACWWcr2gl3Bv6Mafr70=; b=UVVKS+TzT1BJfSq5xN6m9Nfhv1FthoOYv+ImkEktTYbTDjPzQyEPW75xmen/RUFGcz 3PyDJNVyZiNYreOiHYhYHvvz14nFxmMUstJ6OQS40RFRo9T/62m314eDidOkACu8w0BM RLD4yQW0A3wP+nQOD1rumRqnRNJVpnlZATxqjOTowshYx6qjJZePIrIvCoV1svcIswEP QP6qCDwNu2v5kJY9YAgR5uu3ya0wiuf53/iL/g+92nFzOI1k4EIn4GwFmZYlaO1qqEhO x7tWrkHdacRbme7KNMcBiqgOedjm0jM+0hNbSvdU7XQLGQKvYtZSZLAH7X7eu3MsB1pB 7bJg== Received: from gnu-tools-1.localdomain (c-73-93-86-59.hsd1.ca.comcast.net. [73.93.86.59]) by smtp.gmail.com with ESMTPSA id z20-v6sm28030065pfd.99.2018.09.02.07.27.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Sun, 02 Sep 2018 07:27:59 -0700 (PDT) Received: by gnu-tools-1.localdomain (Postfix, from userid 1000) id 1E85A1C08B0; Sun, 2 Sep 2018 07:27:58 -0700 (PDT) Date: Sun, 2 Sep 2018 07:27:58 -0700 From: "H.J. Lu" To: Segher Boessenkool Cc: Jason Merrill , Uros Bizjak , GCC Patches , Cary Coutant Subject: [PATCH] x86: Replace hard frame pointer with stack pointer - UNITS_PER_WORD Message-ID: <20180902142758.GA7162@gmail.com> References: <20180901113835.GG24439@gate.crashing.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20180901113835.GG24439@gate.crashing.org> User-Agent: Mutt/1.10.1 (2018-07-13) X-IsSubscribed: yes On Sat, Sep 01, 2018 at 06:38:35AM -0500, Segher Boessenkool wrote: > > > > With -fno-omit-frame-pointer, arg pointer is eliminated with hard frame > > pointer. But > > > > commit cd557ff63f388ad27c376d0a225e74d3594a6f9d > > Author: hjl > > Date: Thu Aug 10 15:29:05 2017 +0000 > > > > i386: Don't use frame pointer without stack access > > > > When there is no stack access, there is no need to use frame pointer > > even if -fno-omit-frame-pointer is used and caller's frame pointer is > > unchanged. > > > > changed it in the last minute. It is too late to go back. When it is done, > > hard frame pointer must be replaced by stack pointer - UNITS_PER_WORD > > if it is ever used. > > So after that patch something uses the hard frame pointer, while it also > claims nothing uses the hard frame pointer? Sounds to me you should fix > the uses, and all will be fine. > > Here is the patch to replace hard frame pointer with stack pointer - UNITS_PER_WORD in x86 backend. OK for trunk? H.J. --- With commit cd557ff63f388ad27c376d0a225e74d3594a6f9d Author: hjl Date: Thu Aug 10 15:29:05 2017 +0000 i386: Don't use frame pointer without stack access When there is no stack access, there is no need to use frame pointer even if -fno-omit-frame-pointer is used and caller's frame pointer is unchanged. frame pointer may not be available even if -fno-omit-frame-pointer is used. When this happened, arg pointer in debug info may be eliminated by hard frame pointer. In this case, hard frame pointer should be replaced by stack pointer - UNITS_PER_WORD. This patch adds replace_fp_with_sp_in_debug_info to x86 machine_function, replaces arg pointer in debug info with stack pointer - UNITS_PER_WORD if it is eliminated by hard frame pointer and disallows hard frame pointer in debug info when frame pointer isn't available. gcc/ PR debug/86593 * dwarf2out.c (based_loc_descr): Disallow hard frame pointer when frame pointer isn't available. (compute_frame_pointer_to_fb_displacement): Likewise. * config/i386/i386.c (ix86_finalize_stack_frame_flags): Set replace_fp_with_sp_in_debug_info to true if frame pointer isn't used. (ix86_replace_fp_with_sp_1): New function. (ix86_replace_fp_with_sp): Likewise. (ix86_reorg): Call ix86_replace_fp_with_sp if needed. * config/i386/i386.h (machine_function): Add replace_fp_with_sp_in_debug_info. gcc/testsuite/ PR debug/86593 * g++.dg/pr86593-1.C: New test. * g++.dg/pr86593-2.C: Likewise. --- gcc/config/i386/i386.c | 103 +++++++++++++++++++++++++++++++ gcc/config/i386/i386.h | 4 ++ gcc/dwarf2out.c | 6 +- gcc/testsuite/g++.dg/pr86593-1.C | 11 ++++ gcc/testsuite/g++.dg/pr86593-2.C | 11 ++++ 5 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/pr86593-1.C create mode 100644 gcc/testsuite/g++.dg/pr86593-2.C diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index 8672a666024..8166ea06d42 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -13161,6 +13161,11 @@ ix86_finalize_stack_frame_flags (void) df_compute_regs_ever_live (true); df_analyze (); + /* Replace hard frame pointer with stack pointer in debug + info. */ + cfun->machine->replace_fp_with_sp_in_debug_info + = debug_info_level > DINFO_LEVEL_NONE; + if (flag_var_tracking) { /* Since frame pointer is no longer available, replace it with @@ -41944,6 +41949,99 @@ ix86_seh_fixup_eh_fallthru (void) } } +/* Help function for ix86_replace_fp_with_sp. */ + +static rtx +ix86_replace_fp_with_sp_1 (rtx x) +{ + if (!x) + return x; + + if (x == hard_frame_pointer_rtx) + return plus_constant (Pmode, stack_pointer_rtx, -UNITS_PER_WORD); + + const char *fmt = GET_RTX_FORMAT (GET_CODE (x)); + int i, j; + for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--) + { + if (fmt[i] == 'e') + XEXP (x, i) = ix86_replace_fp_with_sp_1 (XEXP (x, i)); + else if (fmt[i] == 'E') + for (j = XVECLEN (x, i) - 1; j >= 0; j--) + XVECEXP (x, i, j) = ix86_replace_fp_with_sp_1 (XVECEXP (x, i, j)); + } + + return x; +} + +/* Replace hard frame pointer in debug info with stack pointer + - UNITS_PER_WORD. */ + +static void +ix86_replace_fp_with_sp (void) +{ + if (flag_var_tracking) + { + rtx_insn *insn, *next; + rtx replace, note; + for (insn = get_insns (); insn; insn = next) + { + next = NEXT_INSN (insn); + if (NOTE_P (insn)) + { + if (NOTE_KIND (insn) == NOTE_INSN_VAR_LOCATION) + { + replace = PATTERN (insn); + if (reg_mentioned_p (arg_pointer_rtx, replace)) + { + replace = eliminate_regs (replace, VOIDmode, + NULL_RTX); + if (reg_mentioned_p (hard_frame_pointer_rtx, + replace)) + { + ix86_replace_fp_with_sp_1 (replace); + PATTERN (insn) = replace; + } + } + } + } + else if (CALL_P (insn)) + { + note = find_reg_note (insn, REG_CALL_ARG_LOCATION, + NULL_RTX); + if (note && reg_mentioned_p (arg_pointer_rtx, note)) + { + replace = eliminate_regs (XEXP (note, 0), VOIDmode, + NULL_RTX); + if (reg_mentioned_p (hard_frame_pointer_rtx, replace)) + { + ix86_replace_fp_with_sp_1 (replace); + remove_note (insn, note); + add_reg_note (insn, REG_CALL_ARG_LOCATION, + replace); + } + } + } + } + } + + for (tree arg = DECL_ARGUMENTS (current_function_decl); + arg; + arg = TREE_CHAIN (arg)) + { + rtx incoming = DECL_INCOMING_RTL (arg); + if (reg_mentioned_p (arg_pointer_rtx, incoming)) + { + incoming = eliminate_regs (incoming, VOIDmode, NULL_RTX); + if (reg_mentioned_p (hard_frame_pointer_rtx, incoming)) + { + ix86_replace_fp_with_sp_1 (incoming); + DECL_INCOMING_RTL (arg) = incoming; + } + } + } +} + /* Implement machine specific optimizations. We implement padding of returns for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window. */ static void @@ -41956,6 +42054,11 @@ ix86_reorg (void) if (TARGET_SEH && current_function_has_exception_handlers ()) ix86_seh_fixup_eh_fallthru (); + /* Replace hard frame pointer in debug info with stack pointer + - UNITS_PER_WORD after the variable tracking pass. */ + if (cfun->machine->replace_fp_with_sp_in_debug_info) + ix86_replace_fp_with_sp (); + if (optimize && optimize_function_for_speed_p (cfun)) { if (TARGET_PAD_SHORT_FUNCTION) diff --git a/gcc/config/i386/i386.h b/gcc/config/i386/i386.h index 2a46fccdec1..23cef7a9c61 100644 --- a/gcc/config/i386/i386.h +++ b/gcc/config/i386/i386.h @@ -2610,6 +2610,10 @@ struct GTY(()) machine_function { /* Nonzero if the function places outgoing arguments on stack. */ BOOL_BITFIELD outgoing_args_on_stack : 1; + /* If true, replace hard frame pointer with stack pointer in debug + info. */ + BOOL_BITFIELD replace_fp_with_sp_in_debug_info : 1; + /* The largest alignment, in bytes, of stack slot actually used. */ unsigned int max_used_stack_alignment; diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c index 77317ed2575..a879ac1f18c 100644 --- a/gcc/dwarf2out.c +++ b/gcc/dwarf2out.c @@ -14327,8 +14327,7 @@ based_loc_descr (rtx reg, poly_int64 offset, { elim = strip_offset_and_add (elim, &offset); gcc_assert ((SUPPORTS_STACK_ALIGNMENT - && (elim == hard_frame_pointer_rtx - || elim == stack_pointer_rtx)) + && elim == stack_pointer_rtx) || elim == (frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx)); @@ -20515,8 +20514,7 @@ compute_frame_pointer_to_fb_displacement (poly_int64 offset) frame_pointer_fb_offset, we won't need one either. */ frame_pointer_fb_offset_valid = ((SUPPORTS_STACK_ALIGNMENT - && (elim == hard_frame_pointer_rtx - || elim == stack_pointer_rtx)) + && elim == stack_pointer_rtx) || elim == (frame_pointer_needed ? hard_frame_pointer_rtx : stack_pointer_rtx)); diff --git a/gcc/testsuite/g++.dg/pr86593-1.C b/gcc/testsuite/g++.dg/pr86593-1.C new file mode 100644 index 00000000000..31dc4153cb9 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr86593-1.C @@ -0,0 +1,11 @@ +// { dg-options "-O -g -fno-omit-frame-pointer -fvar-tracking" } + +struct Foo +{ + int bar(int a, int b, int c, int i1, int i2, int i3, int d); +}; + +int Foo::bar(int a, int b, int c, int i1, int i2, int i3, int d) +{ + return 0; +} diff --git a/gcc/testsuite/g++.dg/pr86593-2.C b/gcc/testsuite/g++.dg/pr86593-2.C new file mode 100644 index 00000000000..65aeac98099 --- /dev/null +++ b/gcc/testsuite/g++.dg/pr86593-2.C @@ -0,0 +1,11 @@ +// { dg-options "-O -g -fno-omit-frame-pointer -fno-var-tracking" } + +struct Foo +{ + int bar(int a, int b, int c, int i1, int i2, int i3, int d); +}; + +int Foo::bar(int a, int b, int c, int i1, int i2, int i3, int d) +{ + return 0; +}