From patchwork Tue Nov 8 18:05:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1701426 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=U2WquAdy; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N6GHJ0yq4z23lg for ; Wed, 9 Nov 2022 05:05:46 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BC5CA385841D for ; Tue, 8 Nov 2022 18:05:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BC5CA385841D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667930740; bh=EaBk+1O8aqEFQ0pRVleNJU4WGVitxWs8GjNevwXygQA=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=U2WquAdyHy3XDutaqmf0e5YZgUdVbDqdZ/6LzBOX5agUdsyoVjz9m0zlAfbLot6mZ xalFJT3CdJeJNxdqjWRS6rIeNbzp5YCVhtDpEI4dym/IjsQS6TD2PsQ1lUWMbDdffS /YjbP9Khsjbf0l64fCSlu6N6OOz8guO9kc9Snp7s= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0EBF83858C62 for ; Tue, 8 Nov 2022 18:05:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0EBF83858C62 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-99-CzAk3FnTPKuCTgm4_qMOjA-1; Tue, 08 Nov 2022 13:05:17 -0500 X-MC-Unique: CzAk3FnTPKuCTgm4_qMOjA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DB106381A727 for ; Tue, 8 Nov 2022 18:05:16 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.65]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 11E61C16922 for ; Tue, 8 Nov 2022 18:05:15 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/3] Compute a table of DWARF register sizes at compile In-Reply-To: References: X-From-Line: df0993a57a506629ba121656e5384c1500cb6338 Mon Sep 17 00:00:00 2001 Message-Id: Date: Tue, 08 Nov 2022 19:05:14 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-36.7 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Gcc-patches From: Florian Weimer Reply-To: Florian Weimer Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The sizes are compile-time constants. Create a vector with them, so that they can be inspected at compile time. * gcc/dwarf2cfi.cc (init_return_column_size): Remove. (init_one_dwarf_reg_size): Adjust. (generate_dwarf_reg_sizes): New function. Extracted from expand_builtin_init_dwarf_reg_sizes. (expand_builtin_init_dwarf_reg_sizes): Call generate_dwarf_reg_sizes. * gcc/target.def (init_dwarf_reg_sizes_extra): Adjust hook signature. * gcc/config/msp430/msp430.cc (msp430_init_dwarf_reg_sizes_extra): Adjust. * gcc/config/rs6000.cc (rs6000_init_dwarf_reg_sizes_extra): Likewise. * gcc/doc/tm.texi: Update. --- gcc/config/msp430/msp430.cc | 11 +---- gcc/config/rs6000/rs6000.cc | 14 +----- gcc/doc/tm.texi | 7 +-- gcc/dwarf2cfi.cc | 93 ++++++++++++++++++------------------- gcc/target.def | 8 ++-- 5 files changed, 58 insertions(+), 75 deletions(-) diff --git a/gcc/config/msp430/msp430.cc b/gcc/config/msp430/msp430.cc index 6c15780a2b6..dbea8d7f50f 100644 --- a/gcc/config/msp430/msp430.cc +++ b/gcc/config/msp430/msp430.cc @@ -3202,11 +3202,9 @@ msp430_expand_eh_return (rtx eh_handler) #undef TARGET_INIT_DWARF_REG_SIZES_EXTRA #define TARGET_INIT_DWARF_REG_SIZES_EXTRA msp430_init_dwarf_reg_sizes_extra void -msp430_init_dwarf_reg_sizes_extra (tree address) +msp430_init_dwarf_reg_sizes_extra (poly_uint16 *sizes) { int i; - rtx addr = expand_normal (address); - rtx mem = gen_rtx_MEM (BLKmode, addr); /* This needs to match msp430_unwind_word_mode (above). */ if (!msp430x) @@ -3218,12 +3216,7 @@ msp430_init_dwarf_reg_sizes_extra (tree address) unsigned int rnum = DWARF2_FRAME_REG_OUT (dnum, 1); if (rnum < DWARF_FRAME_REGISTERS) - { - HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (QImode); - - emit_move_insn (adjust_address (mem, QImode, offset), - gen_int_mode (4, QImode)); - } + sizes[rnum] = 4; } } diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc index a85d7630b41..fddb6a8a0f7 100644 --- a/gcc/config/rs6000/rs6000.cc +++ b/gcc/config/rs6000/rs6000.cc @@ -23783,27 +23783,17 @@ rs6000_initial_elimination_offset (int from, int to) /* Fill in sizes of registers used by unwinder. */ static void -rs6000_init_dwarf_reg_sizes_extra (tree address) +rs6000_init_dwarf_reg_sizes_extra (poly_uint16 *sizes) { if (TARGET_MACHO && ! TARGET_ALTIVEC) { int i; - machine_mode mode = TYPE_MODE (char_type_node); - rtx addr = expand_expr (address, NULL_RTX, VOIDmode, EXPAND_NORMAL); - rtx mem = gen_rtx_MEM (BLKmode, addr); - rtx value = gen_int_mode (16, mode); /* On Darwin, libgcc may be built to run on both G3 and G4/5. The unwinder still needs to know the size of Altivec registers. */ for (i = FIRST_ALTIVEC_REGNO; i < LAST_ALTIVEC_REGNO+1; i++) - { - int column = DWARF_REG_TO_UNWIND_COLUMN - (DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), true)); - HOST_WIDE_INT offset = column * GET_MODE_SIZE (mode); - - emit_move_insn (adjust_address (mem, mode, offset), value); - } + sizes[i] = 16; } } diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 8572313b308..09a3ab3e55c 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -9824,13 +9824,14 @@ used to return a smaller mode than the raw mode to prevent call clobbered parts of a register altering the frame register size @end deftypefn -@deftypefn {Target Hook} void TARGET_INIT_DWARF_REG_SIZES_EXTRA (tree @var{address}) +@deftypefn {Target Hook} void TARGET_INIT_DWARF_REG_SIZES_EXTRA (poly_uint16 *@var{sizes}) If some registers are represented in Dwarf-2 unwind information in multiple pieces, define this hook to fill in information about the sizes of those pieces in the table used by the unwinder at runtime. -It will be called by @code{expand_builtin_init_dwarf_reg_sizes} after +It will be called by @code{generate_dwarf_reg_sizes} after filling in a single size corresponding to each hard register; -@var{address} is the address of the table. +@var{sizes} is the address of the table. It will contain +@code{DWARF_FRAME_REGISTERS} elements when this hook is called. @end deftypefn @deftypefn {Target Hook} bool TARGET_ASM_TTYPE (rtx @var{sym}) diff --git a/gcc/dwarf2cfi.cc b/gcc/dwarf2cfi.cc index bef3165e691..b29173b2156 100644 --- a/gcc/dwarf2cfi.cc +++ b/gcc/dwarf2cfi.cc @@ -36,7 +36,7 @@ along with GCC; see the file COPYING3. If not see #include "except.h" /* expand_builtin_dwarf_sp_column */ #include "profile-count.h" /* For expr.h */ -#include "expr.h" /* init_return_column_size */ +#include "expr.h" /* expand_normal, emit_move_insn */ #include "output.h" /* asm_out_file */ #include "debug.h" /* dwarf2out_do_frame, dwarf2out_do_cfi_asm */ #include "flags.h" /* dwarf_debuginfo_p */ @@ -241,18 +241,6 @@ expand_builtin_dwarf_sp_column (void) return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1)); } -/* MEM is a memory reference for the register size table, each element of - which has mode MODE. Initialize column C as a return address column. */ - -static void -init_return_column_size (scalar_int_mode mode, rtx mem, unsigned int c) -{ - HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode); - HOST_WIDE_INT size = GET_MODE_SIZE (Pmode); - emit_move_insn (adjust_address (mem, mode, offset), - gen_int_mode (size, mode)); -} - /* Datastructure used by expand_builtin_init_dwarf_reg_sizes and init_one_dwarf_reg_size to communicate on what has been done by the latter. */ @@ -274,17 +262,14 @@ struct init_one_dwarf_reg_state use for the size entry to initialize, and INIT_STATE is the communication datastructure conveying what we're doing to our caller. */ -static -void init_one_dwarf_reg_size (int regno, machine_mode regmode, - rtx table, machine_mode slotmode, - init_one_dwarf_reg_state *init_state) +static void +init_one_dwarf_reg_size (int regno, machine_mode regmode, + poly_uint16 *table, + init_one_dwarf_reg_state *init_state) { const unsigned int dnum = DWARF_FRAME_REGNUM (regno); const unsigned int rnum = DWARF2_FRAME_REG_OUT (dnum, 1); const unsigned int dcol = DWARF_REG_TO_UNWIND_COLUMN (rnum); - - poly_int64 slotoffset = dcol * GET_MODE_SIZE (slotmode); - poly_int64 regsize = GET_MODE_SIZE (regmode); init_state->processed_regno[regno] = true; @@ -298,34 +283,22 @@ void init_one_dwarf_reg_size (int regno, machine_mode regmode, init_state->wrote_return_column = true; } - /* ??? When is this true? Should it be a test based on DCOL instead? */ - if (maybe_lt (slotoffset, 0)) - return; - - emit_move_insn (adjust_address (table, slotmode, slotoffset), - gen_int_mode (regsize, slotmode)); + table[dcol] = GET_MODE_SIZE (regmode); } -/* Generate code to initialize the dwarf register size table located - at the provided ADDRESS. */ +/* Fill SIZES with size information for each DWARF register. */ -void -expand_builtin_init_dwarf_reg_sizes (tree address) +static void +generate_dwarf_reg_sizes (poly_uint16 *sizes) { - unsigned int i; - scalar_int_mode mode = SCALAR_INT_TYPE_MODE (char_type_node); - rtx addr = expand_normal (address); - rtx mem = gen_rtx_MEM (BLKmode, addr); - - init_one_dwarf_reg_state init_state; + for (unsigned int i = 0; i < DWARF_FRAME_REGISTERS; i++) + sizes[i] = poly_uint16{}; + init_one_dwarf_reg_state init_state{}; memset ((char *)&init_state, 0, sizeof (init_state)); - for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) + for (unsigned int i = 0; i < FIRST_PSEUDO_REGISTER; i++) { - machine_mode save_mode; - rtx span; - /* No point in processing a register multiple times. This could happen with register spans, e.g. when a reg is first processed as a piece of a span, then as a register on its own later on. */ @@ -333,11 +306,11 @@ expand_builtin_init_dwarf_reg_sizes (tree address) if (init_state.processed_regno[i]) continue; - save_mode = targetm.dwarf_frame_reg_mode (i); - span = targetm.dwarf_register_span (gen_rtx_REG (save_mode, i)); + machine_mode save_mode = targetm.dwarf_frame_reg_mode (i); + rtx span = targetm.dwarf_register_span (gen_rtx_REG (save_mode, i)); if (!span) - init_one_dwarf_reg_size (i, save_mode, mem, mode, &init_state); + init_one_dwarf_reg_size (i, save_mode, sizes, &init_state); else { for (int si = 0; si < XVECLEN (span, 0); si++) @@ -345,19 +318,45 @@ expand_builtin_init_dwarf_reg_sizes (tree address) rtx reg = XVECEXP (span, 0, si); init_one_dwarf_reg_size - (REGNO (reg), GET_MODE (reg), mem, mode, &init_state); + (REGNO (reg), GET_MODE (reg), sizes, &init_state); } } } if (!init_state.wrote_return_column) - init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN); + sizes[DWARF_FRAME_RETURN_COLUMN] = GET_MODE_SIZE (Pmode); #ifdef DWARF_ALT_FRAME_RETURN_COLUMN - init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN); + sizes[DWARF_ALT_FRAME_RETURN_COLUMN] = GET_MODE_SIZE (Pmode); #endif - targetm.init_dwarf_reg_sizes_extra (address); + if (targetm.init_dwarf_reg_sizes_extra != nullptr) + targetm.init_dwarf_reg_sizes_extra (sizes); +} + +/* Generate code to initialize the dwarf register size table located + at the provided ADDRESS. */ + +void +expand_builtin_init_dwarf_reg_sizes (tree address) +{ + poly_uint16 *sizes = XALLOCAVEC (poly_uint16, DWARF_FRAME_REGISTERS); + generate_dwarf_reg_sizes (sizes); + + scalar_int_mode mode = SCALAR_INT_TYPE_MODE (char_type_node); + rtx addr = expand_normal (address); + rtx mem = gen_rtx_MEM (BLKmode, addr); + for (unsigned int i = 0; i < DWARF_FRAME_REGISTERS; ++i) + { + unsigned short value; + if (sizes[i].is_constant (&value) && value == 0) + /* No need to set the value to zero again. */ + continue; + + HOST_WIDE_INT offset = i * GET_MODE_SIZE (mode); + emit_move_insn (adjust_address (mem, mode, offset), + gen_int_mode (sizes[i], mode)); + } } diff --git a/gcc/target.def b/gcc/target.def index 25f94c19fa7..e2c080d42a6 100644 --- a/gcc/target.def +++ b/gcc/target.def @@ -4020,11 +4020,11 @@ DEFHOOK "If some registers are represented in Dwarf-2 unwind information in\n\ multiple pieces, define this hook to fill in information about the\n\ sizes of those pieces in the table used by the unwinder at runtime.\n\ -It will be called by @code{expand_builtin_init_dwarf_reg_sizes} after\n\ +It will be called by @code{generate_dwarf_reg_sizes} after\n\ filling in a single size corresponding to each hard register;\n\ -@var{address} is the address of the table.", - void, (tree address), - hook_void_tree) +@var{sizes} is the address of the table. It will contain\n\ +@code{DWARF_FRAME_REGISTERS} elements when this hook is called.", + void, (poly_uint16 *sizes), nullptr) /* Fetch the fixed register(s) which hold condition codes, for targets where it makes sense to look for duplicate assignments to From patchwork Tue Nov 8 18:05:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1701429 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=vSuo5bNL; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N6GJn3pS9z23lg for ; Wed, 9 Nov 2022 05:07:05 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82B6A38582AD for ; Tue, 8 Nov 2022 18:07:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 82B6A38582AD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667930823; bh=G+YCq2UwM0n2qWe9CJOsZX8aOPoR2yUyx/P1s3OJv3g=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=vSuo5bNLZ1xUIr8aPWRe+oaNYs7kM1lVW2qWzyedFwHSFPEnFHlkvq1mb9iZgA+J3 4j35VAc7S9AxK6kDExqRUZZzwksT0L6lo7k6sEVB2zmNZeiHT3aTE7jTiyhxn+lmCW Z5SEYs0+azDbahbBsrpKnSmsTxFEHQlT+IDLYC34= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id C4DB53858432 for ; Tue, 8 Nov 2022 18:05:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C4DB53858432 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-306-_kTpN6D1MsKVsSOyxPNwXg-1; Tue, 08 Nov 2022 13:05:37 -0500 X-MC-Unique: _kTpN6D1MsKVsSOyxPNwXg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 00BDF8027FD for ; Tue, 8 Nov 2022 18:05:33 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.65]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6D26C112C062 for ; Tue, 8 Nov 2022 18:05:32 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/3] Define __LIBGCC_DWARF_REG_SIZES_CONSTANT__ if DWARF register size is constant In-Reply-To: References: X-From-Line: f08400a5054aadb4fa6e2da62a2768700944b591 Mon Sep 17 00:00:00 2001 Message-Id: Date: Tue, 08 Nov 2022 19:05:30 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-36.1 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Gcc-patches From: Florian Weimer Reply-To: Florian Weimer Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" And use that to speed up the libgcc unwinder. * gcc/debug.h (dwarf_reg_sizes_constant): Declare. * gcc/dwarf2cfi.cc (dwarf_reg_sizes_constant): New function. * gcc/c-family/c-cppbuiltin.c (__LIBGCC_DWARF_REG_SIZES_CONSTANT__): Define if constant is known. libgcc/ * unwind-dw2.c (dwarf_reg_size): New function. (_Unwind_GetGR, _Unwind_SetGR, _Unwind_SetGRPtr) (_Unwind_SetSpColumn, uw_install_context_1): Use it. (uw_init_context_1): Do not initialize dwarf_reg_size_table if not in use. --- gcc/c-family/c-cppbuiltin.cc | 6 ++++++ gcc/debug.h | 2 ++ gcc/dwarf2cfi.cc | 23 ++++++++++++++++++++ libgcc/unwind-dw2.c | 41 +++++++++++++++++++++++++----------- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/gcc/c-family/c-cppbuiltin.cc b/gcc/c-family/c-cppbuiltin.cc index cdb658f6ac9..ab98bf3b059 100644 --- a/gcc/c-family/c-cppbuiltin.cc +++ b/gcc/c-family/c-cppbuiltin.cc @@ -1515,6 +1515,12 @@ c_cpp_builtins (cpp_reader *pfile) #endif builtin_define_with_int_value ("__LIBGCC_DWARF_FRAME_REGISTERS__", DWARF_FRAME_REGISTERS); + { + int value = dwarf_reg_sizes_constant (); + if (value > 0) + builtin_define_with_int_value ("__LIBGCC_DWARF_REG_SIZES_CONSTANT__", + value); + } #ifdef EH_RETURN_STACKADJ_RTX cpp_define (pfile, "__LIBGCC_EH_RETURN_STACKADJ_RTX__"); #endif diff --git a/gcc/debug.h b/gcc/debug.h index fe85115d5f3..6bcc8da1f76 100644 --- a/gcc/debug.h +++ b/gcc/debug.h @@ -245,6 +245,8 @@ extern const struct gcc_debug_hooks vmsdbg_debug_hooks; /* Dwarf2 frame information. */ +extern int dwarf_reg_sizes_constant (); + extern void dwarf2out_begin_prologue (unsigned int, unsigned int, const char *); extern void dwarf2out_vms_end_prologue (unsigned int, const char *); diff --git a/gcc/dwarf2cfi.cc b/gcc/dwarf2cfi.cc index b29173b2156..d45d20478b4 100644 --- a/gcc/dwarf2cfi.cc +++ b/gcc/dwarf2cfi.cc @@ -334,6 +334,29 @@ generate_dwarf_reg_sizes (poly_uint16 *sizes) targetm.init_dwarf_reg_sizes_extra (sizes); } +/* Return 0 if the DWARF register sizes are not constant, otherwise + return the size constant. */ + +int +dwarf_reg_sizes_constant () +{ + poly_uint16 *sizes = XALLOCAVEC (poly_uint16, DWARF_FRAME_REGISTERS); + generate_dwarf_reg_sizes (sizes); + + int result; + for (unsigned int i = 0; i < DWARF_FRAME_REGISTERS; i++) + { + unsigned short value; + if (!sizes[i].is_constant (&value)) + return 0; + if (i == 0) + result = value; + else if (result != value) + return 0; + } + return result; +} + /* Generate code to initialize the dwarf register size table located at the provided ADDRESS. */ diff --git a/libgcc/unwind-dw2.c b/libgcc/unwind-dw2.c index eaceace2029..c370121bb29 100644 --- a/libgcc/unwind-dw2.c +++ b/libgcc/unwind-dw2.c @@ -148,9 +148,25 @@ struct _Unwind_Context char by_value[__LIBGCC_DWARF_FRAME_REGISTERS__+1]; }; +#ifdef __LIBGCC_DWARF_REG_SIZES_CONSTANT__ +static inline unsigned char +dwarf_reg_size (int index __attribute__ ((__unused__))) +{ + return __LIBGCC_DWARF_REG_SIZES_CONSTANT__; +} +#else /* Byte size of every register managed by these routines. */ static unsigned char dwarf_reg_size_table[__LIBGCC_DWARF_FRAME_REGISTERS__+1]; + +static inline unsigned char +dwarf_reg_size (unsigned index) +{ + gcc_assert (index < sizeof (dwarf_reg_size_table)); + return dwarf_reg_size_table[index]; +} +#endif + /* Read unaligned data from the instruction buffer. */ @@ -232,8 +248,7 @@ _Unwind_GetGR (struct _Unwind_Context *context, int regno) #endif index = DWARF_REG_TO_UNWIND_COLUMN (regno); - gcc_assert (index < (int) sizeof(dwarf_reg_size_table)); - size = dwarf_reg_size_table[index]; + size = dwarf_reg_size (index); val = context->reg[index]; if (_Unwind_IsExtendedContext (context) && context->by_value[index]) @@ -280,8 +295,7 @@ _Unwind_SetGR (struct _Unwind_Context *context, int index, _Unwind_Word val) void *ptr; index = DWARF_REG_TO_UNWIND_COLUMN (index); - gcc_assert (index < (int) sizeof(dwarf_reg_size_table)); - size = dwarf_reg_size_table[index]; + size = dwarf_reg_size (index); if (_Unwind_IsExtendedContext (context) && context->by_value[index]) { @@ -329,9 +343,8 @@ _Unwind_SetGRValue (struct _Unwind_Context *context, int index, _Unwind_Word val) { index = DWARF_REG_TO_UNWIND_COLUMN (index); - gcc_assert (index < (int) sizeof(dwarf_reg_size_table)); /* Return column size may be smaller than _Unwind_Context_Reg_Val. */ - gcc_assert (dwarf_reg_size_table[index] <= sizeof (_Unwind_Context_Reg_Val)); + gcc_assert (dwarf_reg_size (index) <= sizeof (_Unwind_Context_Reg_Val)); context->by_value[index] = 1; context->reg[index] = _Unwind_Get_Unwind_Context_Reg_Val (val); @@ -1387,7 +1400,7 @@ static inline void _Unwind_SetSpColumn (struct _Unwind_Context *context, void *cfa, _Unwind_SpTmp *tmp_sp) { - int size = dwarf_reg_size_table[__builtin_dwarf_sp_column ()]; + int size = dwarf_reg_size (__builtin_dwarf_sp_column ()); if (size == sizeof(_Unwind_Ptr)) tmp_sp->ptr = (_Unwind_Ptr) cfa; @@ -1573,11 +1586,13 @@ uw_advance_context (struct _Unwind_Context *context, _Unwind_FrameState *fs) } \ while (0) +#ifndef __LIBGCC_DWARF_REG_SIZES_CONSTANT__ static inline void init_dwarf_reg_size_table (void) { __builtin_init_dwarf_reg_size_table (dwarf_reg_size_table); } +#endif static void __attribute__((noinline)) uw_init_context_1 (struct _Unwind_Context *context, @@ -1596,16 +1611,18 @@ uw_init_context_1 (struct _Unwind_Context *context, code = uw_frame_state_for (context, &fs); gcc_assert (code == _URC_NO_REASON); -#if __GTHREADS +#ifndef __LIBGCC_DWARF_REG_SIZES_CONSTANT__ +# if __GTHREADS { static __gthread_once_t once_regsizes = __GTHREAD_ONCE_INIT; if (__gthread_once (&once_regsizes, init_dwarf_reg_size_table) != 0 && dwarf_reg_size_table[0] == 0) init_dwarf_reg_size_table (); } -#else +# else if (dwarf_reg_size_table[0] == 0) init_dwarf_reg_size_table (); +# endif #endif /* Force the frame state to use the known cfa value. */ @@ -1682,20 +1699,20 @@ uw_install_context_1 (struct _Unwind_Context *current, { _Unwind_Word w; _Unwind_Ptr p; - if (dwarf_reg_size_table[i] == sizeof (_Unwind_Word)) + if (dwarf_reg_size (i) == sizeof (_Unwind_Word)) { w = (_Unwind_Internal_Ptr) t; memcpy (c, &w, sizeof (_Unwind_Word)); } else { - gcc_assert (dwarf_reg_size_table[i] == sizeof (_Unwind_Ptr)); + gcc_assert (dwarf_reg_size (i) == sizeof (_Unwind_Ptr)); p = (_Unwind_Internal_Ptr) t; memcpy (c, &p, sizeof (_Unwind_Ptr)); } } else if (t && c && t != c) - memcpy (c, t, dwarf_reg_size_table[i]); + memcpy (c, t, dwarf_reg_size (i)); } /* If the current frame doesn't have a saved stack pointer, then we From patchwork Tue Nov 8 18:05:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1701428 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=ctIOWg0P; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4N6GHv19kMz23lg for ; Wed, 9 Nov 2022 05:06:19 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 229433858036 for ; Tue, 8 Nov 2022 18:06:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 229433858036 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667930777; bh=+nQ38qfziHIBCI9NolDvvDqzaKYt+GKg4NLnuRsIeBQ=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=ctIOWg0P4JS2BVQqqua/dyQohSnE64FydrxGo/cusOnGRFS7Dz2RqG7YS5PA2IoHU FC6icOWqi1XIRTF2n8pwG1LJkBqwhTr9VyQmQVYi631QKGJLnPhl6VuIslFM61HbEI EaYfr/T6s+Vpr2hDkq/Hv7BzxGSnh/B4JBuEhd/0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 523DA385829D for ; Tue, 8 Nov 2022 18:05:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 523DA385829D Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-45-LqW8-55gNdGd0UtNXlxQeA-1; Tue, 08 Nov 2022 13:05:42 -0500 X-MC-Unique: LqW8-55gNdGd0UtNXlxQeA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 38CB7185A7AD for ; Tue, 8 Nov 2022 18:05:42 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.65]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6FCE62024CC1 for ; Tue, 8 Nov 2022 18:05:41 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [PATCH 3/3] libgcc: Specialize execute_cfa_program in DWARF unwinder for alignments In-Reply-To: References: X-From-Line: e5de1b7feecc0ce5bec77e6a21032ab1f6c0a315 Mon Sep 17 00:00:00 2001 Message-Id: Date: Tue, 08 Nov 2022 19:05:40 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-35.8 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Gcc-patches From: Florian Weimer Reply-To: Florian Weimer Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The parameters fs->data_align and fs->code_align always have fixed values for a particular target in GCC-generated code. Specialize execute_cfa_program for these values, to avoid multiplications. gcc/ * c-family/c-cppbuiltin.c (c_cpp_builtins): Define __LIBGCC_DWARF_CIE_DATA_ALIGNMENT__. libgcc/ * unwind-dw2-execute_cfa.h: New file. Extracted from the execute_cfa_program function in unwind-dw2.c. * unwind-dw2.c (execute_cfa_program_generic): New function. (execute_cfa_program_specialized): Likewise. (execute_cfa_program): Call execute_cfa_program_specialized or execute_cfa_program_generic, as appropriate. --- gcc/c-family/c-cppbuiltin.cc | 2 + libgcc/unwind-dw2-execute_cfa.h | 322 ++++++++++++++++++++++++++++++++ libgcc/unwind-dw2.c | 319 +++---------------------------- 3 files changed, 354 insertions(+), 289 deletions(-) create mode 100644 libgcc/unwind-dw2-execute_cfa.h diff --git a/gcc/c-family/c-cppbuiltin.cc b/gcc/c-family/c-cppbuiltin.cc index ab98bf3b059..c8c327b3b2e 100644 --- a/gcc/c-family/c-cppbuiltin.cc +++ b/gcc/c-family/c-cppbuiltin.cc @@ -1521,6 +1521,8 @@ c_cpp_builtins (cpp_reader *pfile) builtin_define_with_int_value ("__LIBGCC_DWARF_REG_SIZES_CONSTANT__", value); } + builtin_define_with_int_value ("__LIBGCC_DWARF_CIE_DATA_ALIGNMENT__", + DWARF_CIE_DATA_ALIGNMENT); #ifdef EH_RETURN_STACKADJ_RTX cpp_define (pfile, "__LIBGCC_EH_RETURN_STACKADJ_RTX__"); #endif diff --git a/libgcc/unwind-dw2-execute_cfa.h b/libgcc/unwind-dw2-execute_cfa.h new file mode 100644 index 00000000000..dd97b786668 --- /dev/null +++ b/libgcc/unwind-dw2-execute_cfa.h @@ -0,0 +1,322 @@ +/* DWARF2 exception handling CFA execution engine. + Copyright (C) 1997-2022 Free Software Foundation, Inc. + + This file is part of GCC. + + GCC is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3, or (at your option) + any later version. + + GCC is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + Under Section 7 of GPL version 3, you are granted additional + permissions described in the GCC Runtime Library Exception, version + 3.1, as published by the Free Software Foundation. + + You should have received a copy of the GNU General Public License and + a copy of the GCC Runtime Library Exception along with this program; + see the files COPYING3 and COPYING.RUNTIME respectively. If not, see + . */ + +/* This file is included from unwind-dw2.c to specialize the code for certain + values of DATA_ALIGN and CODE_ALIGN. These macros must be defined prior to + including this file. */ + +{ + struct frame_state_reg_info *unused_rs = NULL; + + /* Don't allow remember/restore between CIE and FDE programs. */ + fs->regs.prev = NULL; + + /* The comparison with the return address uses < rather than <= because + we are only interested in the effects of code before the call; for a + noreturn function, the return address may point to unrelated code with + a different stack configuration that we are not interested in. We + assume that the call itself is unwind info-neutral; if not, or if + there are delay instructions that adjust the stack, these must be + reflected at the point immediately before the call insn. + In signal frames, return address is after last completed instruction, + so we add 1 to return address to make the comparison <=. */ + while (insn_ptr < insn_end + && fs->pc < context->ra + _Unwind_IsSignalFrame (context)) + { + unsigned char insn = *insn_ptr++; + _uleb128_t reg, utmp; + _sleb128_t offset, stmp; + + if ((insn & 0xc0) == DW_CFA_advance_loc) + fs->pc += (insn & 0x3f) * CODE_ALIGN; + else if ((insn & 0xc0) == DW_CFA_offset) + { + reg = insn & 0x3f; + insn_ptr = read_uleb128 (insn_ptr, &utmp); + offset = (_Unwind_Sword) utmp * DATA_ALIGN; + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + { + fs->regs.how[reg] = REG_SAVED_OFFSET; + fs->regs.reg[reg].loc.offset = offset; + } + } + else if ((insn & 0xc0) == DW_CFA_restore) + { + reg = insn & 0x3f; + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + fs->regs.how[reg] = REG_UNSAVED; + } + else switch (insn) + { + case DW_CFA_set_loc: + { + _Unwind_Ptr pc; + + insn_ptr = read_encoded_value (context, fs->fde_encoding, + insn_ptr, &pc); + fs->pc = (void *) pc; + } + break; + + case DW_CFA_advance_loc1: + fs->pc += read_1u (insn_ptr) * CODE_ALIGN; + insn_ptr += 1; + break; + case DW_CFA_advance_loc2: + fs->pc += read_2u (insn_ptr) * CODE_ALIGN; + insn_ptr += 2; + break; + case DW_CFA_advance_loc4: + fs->pc += read_4u (insn_ptr) * CODE_ALIGN; + insn_ptr += 4; + break; + + case DW_CFA_offset_extended: + insn_ptr = read_uleb128 (insn_ptr, ®); + insn_ptr = read_uleb128 (insn_ptr, &utmp); + offset = (_Unwind_Sword) utmp * DATA_ALIGN; + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + { + fs->regs.how[reg] = REG_SAVED_OFFSET; + fs->regs.reg[reg].loc.offset = offset; + } + break; + + case DW_CFA_restore_extended: + insn_ptr = read_uleb128 (insn_ptr, ®); + /* FIXME, this is wrong; the CIE might have said that the + register was saved somewhere. */ + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + fs->regs.how[reg] = REG_UNSAVED; + break; + + case DW_CFA_same_value: + insn_ptr = read_uleb128 (insn_ptr, ®); + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + fs->regs.how[reg] = REG_UNSAVED; + break; + + case DW_CFA_undefined: + insn_ptr = read_uleb128 (insn_ptr, ®); + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + fs->regs.how[reg] = REG_UNDEFINED; + break; + + case DW_CFA_nop: + break; + + case DW_CFA_register: + { + _uleb128_t reg2; + insn_ptr = read_uleb128 (insn_ptr, ®); + insn_ptr = read_uleb128 (insn_ptr, ®2); + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + { + fs->regs.how[reg] = REG_SAVED_REG; + fs->regs.reg[reg].loc.reg = (_Unwind_Word)reg2; + } + } + break; + + case DW_CFA_remember_state: + { + struct frame_state_reg_info *new_rs; + if (unused_rs) + { + new_rs = unused_rs; + unused_rs = unused_rs->prev; + } + else + new_rs = alloca (sizeof (struct frame_state_reg_info)); + + *new_rs = fs->regs; + fs->regs.prev = new_rs; + } + break; + + case DW_CFA_restore_state: + { + struct frame_state_reg_info *old_rs = fs->regs.prev; + fs->regs = *old_rs; + old_rs->prev = unused_rs; + unused_rs = old_rs; + } + break; + + case DW_CFA_def_cfa: + insn_ptr = read_uleb128 (insn_ptr, &utmp); + fs->regs.cfa_reg = (_Unwind_Word)utmp; + insn_ptr = read_uleb128 (insn_ptr, &utmp); + fs->regs.cfa_offset = (_Unwind_Word)utmp; + fs->regs.cfa_how = CFA_REG_OFFSET; + break; + + case DW_CFA_def_cfa_register: + insn_ptr = read_uleb128 (insn_ptr, &utmp); + fs->regs.cfa_reg = (_Unwind_Word)utmp; + fs->regs.cfa_how = CFA_REG_OFFSET; + break; + + case DW_CFA_def_cfa_offset: + insn_ptr = read_uleb128 (insn_ptr, &utmp); + fs->regs.cfa_offset = utmp; + /* cfa_how deliberately not set. */ + break; + + case DW_CFA_def_cfa_expression: + fs->regs.cfa_exp = insn_ptr; + fs->regs.cfa_how = CFA_EXP; + insn_ptr = read_uleb128 (insn_ptr, &utmp); + insn_ptr += utmp; + break; + + case DW_CFA_expression: + insn_ptr = read_uleb128 (insn_ptr, ®); + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + { + fs->regs.how[reg] = REG_SAVED_EXP; + fs->regs.reg[reg].loc.exp = insn_ptr; + } + insn_ptr = read_uleb128 (insn_ptr, &utmp); + insn_ptr += utmp; + break; + + /* Dwarf3. */ + case DW_CFA_offset_extended_sf: + insn_ptr = read_uleb128 (insn_ptr, ®); + insn_ptr = read_sleb128 (insn_ptr, &stmp); + offset = stmp * DATA_ALIGN; + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + { + fs->regs.how[reg] = REG_SAVED_OFFSET; + fs->regs.reg[reg].loc.offset = offset; + } + break; + + case DW_CFA_def_cfa_sf: + insn_ptr = read_uleb128 (insn_ptr, &utmp); + fs->regs.cfa_reg = (_Unwind_Word)utmp; + insn_ptr = read_sleb128 (insn_ptr, &stmp); + fs->regs.cfa_offset = (_Unwind_Sword)stmp; + fs->regs.cfa_how = CFA_REG_OFFSET; + fs->regs.cfa_offset *= DATA_ALIGN; + break; + + case DW_CFA_def_cfa_offset_sf: + insn_ptr = read_sleb128 (insn_ptr, &stmp); + fs->regs.cfa_offset = (_Unwind_Sword)stmp; + fs->regs.cfa_offset *= DATA_ALIGN; + /* cfa_how deliberately not set. */ + break; + + case DW_CFA_val_offset: + insn_ptr = read_uleb128 (insn_ptr, ®); + insn_ptr = read_uleb128 (insn_ptr, &utmp); + offset = (_Unwind_Sword) utmp * DATA_ALIGN; + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + { + fs->regs.how[reg] = REG_SAVED_VAL_OFFSET; + fs->regs.reg[reg].loc.offset = offset; + } + break; + + case DW_CFA_val_offset_sf: + insn_ptr = read_uleb128 (insn_ptr, ®); + insn_ptr = read_sleb128 (insn_ptr, &stmp); + offset = stmp * DATA_ALIGN; + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + { + fs->regs.how[reg] = REG_SAVED_VAL_OFFSET; + fs->regs.reg[reg].loc.offset = offset; + } + break; + + case DW_CFA_val_expression: + insn_ptr = read_uleb128 (insn_ptr, ®); + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + { + fs->regs.how[reg] = REG_SAVED_VAL_EXP; + fs->regs.reg[reg].loc.exp = insn_ptr; + } + insn_ptr = read_uleb128 (insn_ptr, &utmp); + insn_ptr += utmp; + break; + + case DW_CFA_GNU_window_save: +#if defined (__aarch64__) && !defined (__ILP32__) + /* This CFA is multiplexed with Sparc. On AArch64 it's used to toggle + return address signing status. */ + reg = DWARF_REGNUM_AARCH64_RA_STATE; + gcc_assert (fs->regs.how[reg] == REG_UNSAVED); + fs->regs.reg[reg].loc.offset ^= 1; +#else + /* ??? Hardcoded for SPARC register window configuration. */ + if (__LIBGCC_DWARF_FRAME_REGISTERS__ >= 32) + for (reg = 16; reg < 32; ++reg) + { + fs->regs.how[reg] = REG_SAVED_OFFSET; + fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *); + } +#endif + break; + + case DW_CFA_GNU_args_size: + insn_ptr = read_uleb128 (insn_ptr, &utmp); + context->args_size = (_Unwind_Word)utmp; + break; + + case DW_CFA_GNU_negative_offset_extended: + /* Obsoleted by DW_CFA_offset_extended_sf, but used by + older PowerPC code. */ + insn_ptr = read_uleb128 (insn_ptr, ®); + insn_ptr = read_uleb128 (insn_ptr, &utmp); + offset = (_Unwind_Word) utmp * DATA_ALIGN; + reg = DWARF_REG_TO_UNWIND_COLUMN (reg); + if (UNWIND_COLUMN_IN_RANGE (reg)) + { + fs->regs.how[reg] = REG_SAVED_OFFSET; + fs->regs.reg[reg].loc.offset = -offset; + } + break; + + default: + gcc_unreachable (); + } + } +} + +#undef DATA_ALIGN +#undef CODE_ALIGN diff --git a/libgcc/unwind-dw2.c b/libgcc/unwind-dw2.c index c370121bb29..792338c1e38 100644 --- a/libgcc/unwind-dw2.c +++ b/libgcc/unwind-dw2.c @@ -960,302 +960,43 @@ execute_stack_op (const unsigned char *op_ptr, const unsigned char *op_end, instruction sequence to decode, current register information and CIE info, and the PC range to evaluate. */ +static void __attribute__ ((__noinline__)) +execute_cfa_program_generic (const unsigned char *insn_ptr, + const unsigned char *insn_end, + struct _Unwind_Context *context, + _Unwind_FrameState *fs) +{ +#define DATA_ALIGN fs->data_align +#define CODE_ALIGN fs->code_align +#include "unwind-dw2-execute_cfa.h" +} + +static inline void +execute_cfa_program_specialized (const unsigned char *insn_ptr, + const unsigned char *insn_end, + struct _Unwind_Context *context, + _Unwind_FrameState *fs) +{ +#define DATA_ALIGN __LIBGCC_DWARF_CIE_DATA_ALIGNMENT__ + /* GCC always uses 1 even on architectures with a fixed instruction + width. */ +#define CODE_ALIGN 1 +#include "unwind-dw2-execute_cfa.h" +} + static void execute_cfa_program (const unsigned char *insn_ptr, const unsigned char *insn_end, struct _Unwind_Context *context, _Unwind_FrameState *fs) { - struct frame_state_reg_info *unused_rs = NULL; - - /* Don't allow remember/restore between CIE and FDE programs. */ - fs->regs.prev = NULL; - - /* The comparison with the return address uses < rather than <= because - we are only interested in the effects of code before the call; for a - noreturn function, the return address may point to unrelated code with - a different stack configuration that we are not interested in. We - assume that the call itself is unwind info-neutral; if not, or if - there are delay instructions that adjust the stack, these must be - reflected at the point immediately before the call insn. - In signal frames, return address is after last completed instruction, - so we add 1 to return address to make the comparison <=. */ - while (insn_ptr < insn_end - && fs->pc < context->ra + _Unwind_IsSignalFrame (context)) - { - unsigned char insn = *insn_ptr++; - _uleb128_t reg, utmp; - _sleb128_t offset, stmp; - - if ((insn & 0xc0) == DW_CFA_advance_loc) - fs->pc += (insn & 0x3f) * fs->code_align; - else if ((insn & 0xc0) == DW_CFA_offset) - { - reg = insn & 0x3f; - insn_ptr = read_uleb128 (insn_ptr, &utmp); - offset = (_Unwind_Sword) utmp * fs->data_align; - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - { - fs->regs.how[reg] = REG_SAVED_OFFSET; - fs->regs.reg[reg].loc.offset = offset; - } - } - else if ((insn & 0xc0) == DW_CFA_restore) - { - reg = insn & 0x3f; - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - fs->regs.how[reg] = REG_UNSAVED; - } - else switch (insn) - { - case DW_CFA_set_loc: - { - _Unwind_Ptr pc; - - insn_ptr = read_encoded_value (context, fs->fde_encoding, - insn_ptr, &pc); - fs->pc = (void *) pc; - } - break; - - case DW_CFA_advance_loc1: - fs->pc += read_1u (insn_ptr) * fs->code_align; - insn_ptr += 1; - break; - case DW_CFA_advance_loc2: - fs->pc += read_2u (insn_ptr) * fs->code_align; - insn_ptr += 2; - break; - case DW_CFA_advance_loc4: - fs->pc += read_4u (insn_ptr) * fs->code_align; - insn_ptr += 4; - break; - - case DW_CFA_offset_extended: - insn_ptr = read_uleb128 (insn_ptr, ®); - insn_ptr = read_uleb128 (insn_ptr, &utmp); - offset = (_Unwind_Sword) utmp * fs->data_align; - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - { - fs->regs.how[reg] = REG_SAVED_OFFSET; - fs->regs.reg[reg].loc.offset = offset; - } - break; - - case DW_CFA_restore_extended: - insn_ptr = read_uleb128 (insn_ptr, ®); - /* FIXME, this is wrong; the CIE might have said that the - register was saved somewhere. */ - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - fs->regs.how[reg] = REG_UNSAVED; - break; - - case DW_CFA_same_value: - insn_ptr = read_uleb128 (insn_ptr, ®); - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - fs->regs.how[reg] = REG_UNSAVED; - break; - - case DW_CFA_undefined: - insn_ptr = read_uleb128 (insn_ptr, ®); - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - fs->regs.how[reg] = REG_UNDEFINED; - break; - - case DW_CFA_nop: - break; - - case DW_CFA_register: - { - _uleb128_t reg2; - insn_ptr = read_uleb128 (insn_ptr, ®); - insn_ptr = read_uleb128 (insn_ptr, ®2); - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - { - fs->regs.how[reg] = REG_SAVED_REG; - fs->regs.reg[reg].loc.reg = (_Unwind_Word)reg2; - } - } - break; - - case DW_CFA_remember_state: - { - struct frame_state_reg_info *new_rs; - if (unused_rs) - { - new_rs = unused_rs; - unused_rs = unused_rs->prev; - } - else - new_rs = alloca (sizeof (struct frame_state_reg_info)); - - *new_rs = fs->regs; - fs->regs.prev = new_rs; - } - break; - - case DW_CFA_restore_state: - { - struct frame_state_reg_info *old_rs = fs->regs.prev; - fs->regs = *old_rs; - old_rs->prev = unused_rs; - unused_rs = old_rs; - } - break; - - case DW_CFA_def_cfa: - insn_ptr = read_uleb128 (insn_ptr, &utmp); - fs->regs.cfa_reg = (_Unwind_Word)utmp; - insn_ptr = read_uleb128 (insn_ptr, &utmp); - fs->regs.cfa_offset = (_Unwind_Word)utmp; - fs->regs.cfa_how = CFA_REG_OFFSET; - break; - - case DW_CFA_def_cfa_register: - insn_ptr = read_uleb128 (insn_ptr, &utmp); - fs->regs.cfa_reg = (_Unwind_Word)utmp; - fs->regs.cfa_how = CFA_REG_OFFSET; - break; - - case DW_CFA_def_cfa_offset: - insn_ptr = read_uleb128 (insn_ptr, &utmp); - fs->regs.cfa_offset = utmp; - /* cfa_how deliberately not set. */ - break; - - case DW_CFA_def_cfa_expression: - fs->regs.cfa_exp = insn_ptr; - fs->regs.cfa_how = CFA_EXP; - insn_ptr = read_uleb128 (insn_ptr, &utmp); - insn_ptr += utmp; - break; - - case DW_CFA_expression: - insn_ptr = read_uleb128 (insn_ptr, ®); - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - { - fs->regs.how[reg] = REG_SAVED_EXP; - fs->regs.reg[reg].loc.exp = insn_ptr; - } - insn_ptr = read_uleb128 (insn_ptr, &utmp); - insn_ptr += utmp; - break; - - /* Dwarf3. */ - case DW_CFA_offset_extended_sf: - insn_ptr = read_uleb128 (insn_ptr, ®); - insn_ptr = read_sleb128 (insn_ptr, &stmp); - offset = stmp * fs->data_align; - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - { - fs->regs.how[reg] = REG_SAVED_OFFSET; - fs->regs.reg[reg].loc.offset = offset; - } - break; - - case DW_CFA_def_cfa_sf: - insn_ptr = read_uleb128 (insn_ptr, &utmp); - fs->regs.cfa_reg = (_Unwind_Word)utmp; - insn_ptr = read_sleb128 (insn_ptr, &stmp); - fs->regs.cfa_offset = (_Unwind_Sword)stmp; - fs->regs.cfa_how = CFA_REG_OFFSET; - fs->regs.cfa_offset *= fs->data_align; - break; - - case DW_CFA_def_cfa_offset_sf: - insn_ptr = read_sleb128 (insn_ptr, &stmp); - fs->regs.cfa_offset = (_Unwind_Sword)stmp; - fs->regs.cfa_offset *= fs->data_align; - /* cfa_how deliberately not set. */ - break; - - case DW_CFA_val_offset: - insn_ptr = read_uleb128 (insn_ptr, ®); - insn_ptr = read_uleb128 (insn_ptr, &utmp); - offset = (_Unwind_Sword) utmp * fs->data_align; - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - { - fs->regs.how[reg] = REG_SAVED_VAL_OFFSET; - fs->regs.reg[reg].loc.offset = offset; - } - break; - - case DW_CFA_val_offset_sf: - insn_ptr = read_uleb128 (insn_ptr, ®); - insn_ptr = read_sleb128 (insn_ptr, &stmp); - offset = stmp * fs->data_align; - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - { - fs->regs.how[reg] = REG_SAVED_VAL_OFFSET; - fs->regs.reg[reg].loc.offset = offset; - } - break; - - case DW_CFA_val_expression: - insn_ptr = read_uleb128 (insn_ptr, ®); - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - { - fs->regs.how[reg] = REG_SAVED_VAL_EXP; - fs->regs.reg[reg].loc.exp = insn_ptr; - } - insn_ptr = read_uleb128 (insn_ptr, &utmp); - insn_ptr += utmp; - break; - - case DW_CFA_GNU_window_save: -#if defined (__aarch64__) && !defined (__ILP32__) - /* This CFA is multiplexed with Sparc. On AArch64 it's used to toggle - return address signing status. */ - reg = DWARF_REGNUM_AARCH64_RA_STATE; - gcc_assert (fs->regs.how[reg] == REG_UNSAVED); - fs->regs.reg[reg].loc.offset ^= 1; -#else - /* ??? Hardcoded for SPARC register window configuration. */ - if (__LIBGCC_DWARF_FRAME_REGISTERS__ >= 32) - for (reg = 16; reg < 32; ++reg) - { - fs->regs.how[reg] = REG_SAVED_OFFSET; - fs->regs.reg[reg].loc.offset = (reg - 16) * sizeof (void *); - } -#endif - break; - - case DW_CFA_GNU_args_size: - insn_ptr = read_uleb128 (insn_ptr, &utmp); - context->args_size = (_Unwind_Word)utmp; - break; - - case DW_CFA_GNU_negative_offset_extended: - /* Obsoleted by DW_CFA_offset_extended_sf, but used by - older PowerPC code. */ - insn_ptr = read_uleb128 (insn_ptr, ®); - insn_ptr = read_uleb128 (insn_ptr, &utmp); - offset = (_Unwind_Word) utmp * fs->data_align; - reg = DWARF_REG_TO_UNWIND_COLUMN (reg); - if (UNWIND_COLUMN_IN_RANGE (reg)) - { - fs->regs.how[reg] = REG_SAVED_OFFSET; - fs->regs.reg[reg].loc.offset = -offset; - } - break; - - default: - gcc_unreachable (); - } - } + if (fs->data_align == __LIBGCC_DWARF_CIE_DATA_ALIGNMENT__ + && fs->code_align == 1) + execute_cfa_program_specialized (insn_ptr, insn_end, context, fs); + else + execute_cfa_program_generic (insn_ptr, insn_end, context, fs); } + /* Given the _Unwind_Context CONTEXT for a stack frame, look up the FDE for its caller and decode it into FS. This function also sets the