From patchwork Thu Sep 8 21:16:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takayuki 'January June' Suwa X-Patchwork-Id: 1104 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp446546wrt; Thu, 8 Sep 2022 14:39:14 -0700 (PDT) X-Google-Smtp-Source: AA6agR4Eo/wXocl1OZUTmLYqV3akjgF4odTrg6foy7q8JkoU8ao/YJhPDxSsck8/vlEy1j1arBpW X-Received: by 2002:a05:6402:1f02:b0:445:f674:eac0 with SMTP id b2-20020a0564021f0200b00445f674eac0mr8591715edb.370.1662673154741; Thu, 08 Sep 2022 14:39:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662673154; cv=none; d=google.com; s=arc-20160816; b=hj4M1JwBmYnhZpcJy0vwlO+OrlXPNwcBNezqzjSDaKqYhtDi+F1hhfiRzNusAVbydJ lmtO5C3WRuEBrvuNjbo4szRWqFbvPO/pnwwiaZ8gIIaUh9o5SCvRve27KuKBYRwhDEkD uXbYfNQ5kdxoWSUz0a2x8tnsmD3m/rPEA1Q03q0vkeaO7RI4+KjpYwirt4vyQVWRB9S8 JN0cyjQewrXjO7tldSs2EQYcDjtkwKS2gs9PRbuM3h5x8Ye10DKjySbw9Qm5oBfKG9X4 /7zzT2CGZ9KAkt7MccqKmi9bbhM7U98SRhZuSJt9JS9/DuJs1SQrdIsSRL+gDYtEzfNO OAOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:subject:to:user-agent:mime-version:date :message-id:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=olwfJ4oBDnkT25iOLKfjhVtAF2Il+lbH1diAL/hkiBA=; b=GFwjAHhpN7Q3CZbIbQPYpto8gtDzvffXPuhBbhNn2o9YKKzn38aVMuhcSdFxl8msAo JDz9fDKXqHhLAzBq1/qcRMowARPqWd6IpSNMHB5gLp+76imkdGaUOrGuddaCT0ZqWTsv qBot0Y+pCxz8ZE+Q0NaEJQJas3/MrRXe/1AYTJzpMwe7z1jAAmGPDgiI+BaxFSmpKeCg hCHbmqVioWYp6BWtDw4lDQHrYCpo7pBZrLobhIyq2TWTW4CDUrJEOax0U5RQRksgcT40 oDXk1umDw5O8mKfGT3/+Uw55VO7PSjNj2dJCRYZjFe4pMFP2od1sS3iEzN4fGENIBNnm 8SIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=lSeLj55i; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id v6-20020a50d586000000b00448f0a4f4d9si15132683edi.61.2022.09.08.14.39.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Sep 2022 14:39:14 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=lSeLj55i; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9BCFF3858427 for ; Thu, 8 Sep 2022 21:39:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9BCFF3858427 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662673153; bh=olwfJ4oBDnkT25iOLKfjhVtAF2Il+lbH1diAL/hkiBA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=lSeLj55iydrSlYF+vihAQ+QbH5b+xlEbleRlwReJLMFXCkQP7ic8bZUmo7q7xIlCt gjA0yG5zSCHQIr5vaMXuEqCGtsf8ZGm0BY5YYX9ZMcswWS0toAhDrHOOYQy/HU/XzK eyq90mkgxOeMZJnQHgVCEMPv3JKpQHOWXo+/cri0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from nh605-vm7.bullet.mail.ssk.yahoo.co.jp (nh605-vm7.bullet.mail.ssk.yahoo.co.jp [182.22.90.80]) by sourceware.org (Postfix) with SMTP id BCE453858C2D for ; Thu, 8 Sep 2022 21:38:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BCE453858C2D Received: from [182.22.66.105] by nh605.bullet.mail.ssk.yahoo.co.jp with NNFMP; 08 Sep 2022 21:38:24 -0000 Received: from [182.22.91.131] by t603.bullet.mail.ssk.yahoo.co.jp with NNFMP; 08 Sep 2022 21:38:24 -0000 Received: from [127.0.0.1] by omp604.mail.ssk.yahoo.co.jp with NNFMP; 08 Sep 2022 21:38:24 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 452697.94362.bm@omp604.mail.ssk.yahoo.co.jp Received: (qmail 47081 invoked by alias); 8 Sep 2022 21:38:24 -0000 Received: from unknown (HELO ?192.168.150.80?) (183.77.162.93 with ) by smtp6009.mail.ssk.ynwp.yahoo.co.jp with SMTP; 8 Sep 2022 21:38:23 -0000 X-YMail-JAS: 4LJoiT0VM1nHDUZ9wsj8IZ6ly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I85X vBRo0ttxpX7SqaNif Message-ID: <58b7fe91-a678-0cfd-9697-aa565bda0d68@yahoo.co.jp> Date: Fri, 9 Sep 2022 06:16:31 +0900 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 To: GCC Patches Subject: [PATCH v4 1/2] xtensa: Eliminate unused stack frame allocation/freeing X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: Takayuki 'January June' Suwa via Gcc-patches From: Takayuki 'January June' Suwa Reply-To: Takayuki 'January June' Suwa Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743439166169415842?= X-GMAIL-MSGID: =?utf-8?q?1743439166169415842?= Changes from v3: (xtensa_expand_prologue): Changed to exclude debug insns from DF use chain analysis. --- In the example below, 'x' is once placed on the stack frame and then read into registers as the argument value of bar(): /* example */ struct foo { int a, b; }; extern struct foo bar(struct foo); struct foo test(void) { struct foo x = { 0, 1 }; return bar(x); } Thanks to the dead store elimination, the initialization of 'x' turns into merely loading the immediates to registers, but corresponding stack frame growth is not rolled back. As a result: ;; prereq: the CALL0 ABI ;; before test: addi sp, sp, -16 // unused stack frame allocation/freeing movi.n a2, 0 movi.n a3, 1 addi sp, sp, 16 // because no instructions that refer to j.l bar, a9 // the stack pointer between the two This patch eliminates such unused stack frame allocation/freeing: ;; after test: movi.n a2, 0 movi.n a3, 1 j.l bar, a9 gcc/ChangeLog: * config/xtensa/xtensa.cc (machine_function): New boolean member as a flag that controls whether to emit the insns for stack pointer adjustment inside of the pro/epilogue. (xtensa_emit_adjust_stack_ptr): New function to share the common codes and to emit insns if not inhibited. (xtensa_expand_epilogue): Change to use the function mentioned above when using the CALL0 ABI. (xtensa_expand_prologue): Ditto. And also change to set the inhibit flag used by xtensa_emit_adjust_stack_ptr() to true if the stack pointer is only used for its own adjustment. --- gcc/config/xtensa/xtensa.cc | 164 ++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 84 deletions(-) diff --git a/gcc/config/xtensa/xtensa.cc b/gcc/config/xtensa/xtensa.cc index 93ac6562b22..0f586b09dfb 100644 --- a/gcc/config/xtensa/xtensa.cc +++ b/gcc/config/xtensa/xtensa.cc @@ -102,6 +102,7 @@ struct GTY(()) machine_function int callee_save_size; bool frame_laid_out; bool epilogue_done; + bool inhibit_logues_a1_adjusts; }; /* Vector, indexed by hard register number, which contains 1 for a @@ -3048,7 +3049,7 @@ xtensa_output_literal (FILE *file, rtx x, machine_mode mode, int labelno) } static bool -xtensa_call_save_reg(int regno) +xtensa_call_save_reg (int regno) { if (TARGET_WINDOWED_ABI) return false; @@ -3084,7 +3085,7 @@ compute_frame_size (poly_int64 size) cfun->machine->callee_save_size = 0; for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno) { - if (xtensa_call_save_reg(regno)) + if (xtensa_call_save_reg (regno)) cfun->machine->callee_save_size += UNITS_PER_WORD; } @@ -3139,6 +3140,49 @@ xtensa_initial_elimination_offset (int from, int to ATTRIBUTE_UNUSED) return offset; } +#define ADJUST_SP_NONE 0x0 +#define ADJUST_SP_NEED_NOTE 0x1 +#define ADJUST_SP_FRAME_PTR 0x2 +static void +xtensa_emit_adjust_stack_ptr (HOST_WIDE_INT offset, int flags) +{ + rtx_insn *insn; + rtx ptr = (flags & ADJUST_SP_FRAME_PTR) ? hard_frame_pointer_rtx + : stack_pointer_rtx; + + if (cfun->machine->inhibit_logues_a1_adjusts) + return; + + if (xtensa_simm8 (offset) + || xtensa_simm8x256 (offset)) + insn = emit_insn (gen_addsi3 (stack_pointer_rtx, ptr, GEN_INT (offset))); + else + { + rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG); + + if (offset < 0) + { + emit_move_insn (tmp_reg, GEN_INT (-offset)); + insn = emit_insn (gen_subsi3 (stack_pointer_rtx, ptr, tmp_reg)); + } + else + { + emit_move_insn (tmp_reg, GEN_INT (offset)); + insn = emit_insn (gen_addsi3 (stack_pointer_rtx, ptr, tmp_reg)); + } + } + + if (flags & ADJUST_SP_NEED_NOTE) + { + rtx note_rtx = gen_rtx_SET (stack_pointer_rtx, + plus_constant (Pmode, stack_pointer_rtx, + offset)); + + RTX_FRAME_RELATED_P (insn) = 1; + add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx); + } +} + /* minimum frame = reg save area (4 words) plus static chain (1 word) and the total number of words must be a multiple of 128 bits. */ #define MIN_FRAME_SIZE (8 * UNITS_PER_WORD) @@ -3174,17 +3218,30 @@ xtensa_expand_prologue (void) int regno; HOST_WIDE_INT offset = 0; int callee_save_size = cfun->machine->callee_save_size; + df_ref ref; + bool stack_pointer_needed = frame_pointer_needed + || crtl->calls_eh_return; + + /* Check if the function body really needs the stack pointer. */ + if (!stack_pointer_needed) + for (ref = DF_REG_USE_CHAIN (A1_REG); + ref; ref = DF_REF_NEXT_REG (ref)) + if (DF_REF_CLASS (ref) == DF_REF_REGULAR + && NONJUMP_INSN_P (DF_REF_INSN (ref))) + stack_pointer_needed = true; + /* Check if callee-saved registers really need saving to the stack. */ + if (!stack_pointer_needed) + for (regno = 0; regno < FIRST_PSEUDO_REGISTER; ++regno) + if (xtensa_call_save_reg (regno)) + stack_pointer_needed = true; + + cfun->machine->inhibit_logues_a1_adjusts = !stack_pointer_needed; /* -128 is a limit of single addi instruction. */ if (IN_RANGE (total_size, 1, 128)) { - insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, - GEN_INT (-total_size))); - RTX_FRAME_RELATED_P (insn) = 1; - note_rtx = gen_rtx_SET (stack_pointer_rtx, - plus_constant (Pmode, stack_pointer_rtx, - -total_size)); - add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx); + xtensa_emit_adjust_stack_ptr (-total_size, + ADJUST_SP_NEED_NOTE); offset = total_size - UNITS_PER_WORD; } else if (callee_save_size) @@ -3194,33 +3251,14 @@ xtensa_expand_prologue (void) * move it to its final location. */ if (total_size > 1024) { - insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, - GEN_INT (-callee_save_size))); - RTX_FRAME_RELATED_P (insn) = 1; - note_rtx = gen_rtx_SET (stack_pointer_rtx, - plus_constant (Pmode, stack_pointer_rtx, - -callee_save_size)); - add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx); + xtensa_emit_adjust_stack_ptr (-callee_save_size, + ADJUST_SP_NEED_NOTE); offset = callee_save_size - UNITS_PER_WORD; } else { - if (xtensa_simm8x256 (-total_size)) - insn = emit_insn (gen_addsi3 (stack_pointer_rtx, - stack_pointer_rtx, - GEN_INT (-total_size))); - else - { - rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG); - emit_move_insn (tmp_reg, GEN_INT (total_size)); - insn = emit_insn (gen_subsi3 (stack_pointer_rtx, - stack_pointer_rtx, tmp_reg)); - } - RTX_FRAME_RELATED_P (insn) = 1; - note_rtx = gen_rtx_SET (stack_pointer_rtx, - plus_constant (Pmode, stack_pointer_rtx, - -total_size)); - add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx); + xtensa_emit_adjust_stack_ptr (-total_size, + ADJUST_SP_NEED_NOTE); offset = total_size - UNITS_PER_WORD; } } @@ -3242,27 +3280,8 @@ xtensa_expand_prologue (void) } if (total_size > 1024 || (!callee_save_size && total_size > 128)) - { - if (xtensa_simm8x256 (callee_save_size - total_size)) - insn = emit_insn (gen_addsi3 (stack_pointer_rtx, - stack_pointer_rtx, - GEN_INT (callee_save_size - - total_size))); - else - { - rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG); - emit_move_insn (tmp_reg, GEN_INT (total_size - - callee_save_size)); - insn = emit_insn (gen_subsi3 (stack_pointer_rtx, - stack_pointer_rtx, tmp_reg)); - } - RTX_FRAME_RELATED_P (insn) = 1; - note_rtx = gen_rtx_SET (stack_pointer_rtx, - plus_constant (Pmode, stack_pointer_rtx, - callee_save_size - - total_size)); - add_reg_note (insn, REG_FRAME_RELATED_EXPR, note_rtx); - } + xtensa_emit_adjust_stack_ptr (callee_save_size - total_size, + ADJUST_SP_NEED_NOTE); } if (frame_pointer_needed) @@ -3329,21 +3348,11 @@ xtensa_expand_epilogue (bool sibcall_p) if (cfun->machine->current_frame_size > (frame_pointer_needed ? 127 : 1024)) { - if (xtensa_simm8x256 (cfun->machine->current_frame_size - - cfun->machine->callee_save_size)) - emit_insn (gen_addsi3 (stack_pointer_rtx, frame_pointer_needed ? - hard_frame_pointer_rtx : stack_pointer_rtx, - GEN_INT (cfun->machine->current_frame_size - - cfun->machine->callee_save_size))); - else - { - rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG); - emit_move_insn (tmp_reg, GEN_INT (cfun->machine->current_frame_size - - cfun->machine->callee_save_size)); - emit_insn (gen_addsi3 (stack_pointer_rtx, frame_pointer_needed ? - hard_frame_pointer_rtx : stack_pointer_rtx, - tmp_reg)); - } + xtensa_emit_adjust_stack_ptr (cfun->machine->current_frame_size - + cfun->machine->callee_save_size, + frame_pointer_needed + ? ADJUST_SP_FRAME_PTR + : ADJUST_SP_NONE); offset = cfun->machine->callee_save_size - UNITS_PER_WORD; } else @@ -3384,24 +3393,11 @@ xtensa_expand_epilogue (bool sibcall_p) else offset = cfun->machine->callee_save_size; if (offset) - emit_insn (gen_addsi3 (stack_pointer_rtx, - stack_pointer_rtx, - GEN_INT (offset))); + xtensa_emit_adjust_stack_ptr (offset, ADJUST_SP_NONE); } else - { - if (xtensa_simm8x256 (cfun->machine->current_frame_size)) - emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, - GEN_INT (cfun->machine->current_frame_size))); - else - { - rtx tmp_reg = gen_rtx_REG (Pmode, A9_REG); - emit_move_insn (tmp_reg, - GEN_INT (cfun->machine->current_frame_size)); - emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, - tmp_reg)); - } - } + xtensa_emit_adjust_stack_ptr (cfun->machine->current_frame_size, + ADJUST_SP_NONE); } if (crtl->calls_eh_return)