From patchwork Thu Feb 9 21:49:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladimir Makarov X-Patchwork-Id: 1740151 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=) Received: from sourceware.org (server2.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 4PCVs71JWKz23qt for ; Fri, 10 Feb 2023 08:50:03 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 57BC23858C74 for ; Thu, 9 Feb 2023 21:50:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 57BC23858C74 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675979400; bh=bBtU00F6JKr8JaN1YxSLcoav0dMEyCXNS5tacqK4gPw=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=t6ur+oxphdQHGXmCQhj31/qDbqYCVu4kCzPgV7DefT74Dzy6hkUh4vQCb2qVYbxRz pkika+UMqQQeR3vHi+aTudEvm9liU93f7XxFrOrR8ep7eR6L/CAFG7BclzN12/+5Cq IOWlINc1NaxJT23lOmxmdLAfZSKLxjY+igy2aV7I= 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 C17C73858C50 for ; Thu, 9 Feb 2023 21:49:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C17C73858C50 Received: from mail-il1-f198.google.com (mail-il1-f198.google.com [209.85.166.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-311-dwa3yq6LN72iSqdueRXddQ-1; Thu, 09 Feb 2023 16:49:38 -0500 X-MC-Unique: dwa3yq6LN72iSqdueRXddQ-1 Received: by mail-il1-f198.google.com with SMTP id t6-20020a056e02010600b0031417634f4bso8558ilm.18 for ; Thu, 09 Feb 2023 13:49:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:from:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=G0FJBc4tdfbInNQR83Qv2FALmC5u8jpLth0r3IkhDlU=; b=Ly3wz9zhcEVAO4pDjOUBf7U55HsAjS4K2j8xr3IvGezcW338fRm/cJh9rq84dHRyEK hB7yzFPhlXxc0DB/opTG4FPGCbsJ0xqiPfOm/ir2LeLWaDlNx1pDiIm6L+1EKnBAi55c CzhFtqMzxdr25AW+r7OukhZksxRDB4oH2slb3AzUXXIOWsaO33O4uZD8+S8NqMMVKDsA L6B0ljIAMq4Dav1TVXif+zL1rLcroK1N0KrAGLs0QXdT8f3wkecdXYN+DN4j//qtfne7 kNR1SYvYfn8qYZQ/m10hiZ1wa2nMF90hVQUWZCofpV3qOsrgEndd65MzggRj6b600Qa/ LjrQ== X-Gm-Message-State: AO0yUKU1S0m05jYy+aGHMojsZ9OKyfok/ookT8eryPrWIoi2H+ocPYfl TEuiqBAN6nUvKEE8bc4n/RkCAHs6g4xv9xfxKcQYYUFlxCwuP5ZHiOGoiXJe9EG1/SiQ1ecqmDU qNz4nOPqh+PKuEVQr3CbKV3kKAKPTtc8hQKW3ptWIDyeXnzXHGDuyZnmmemFdQjzLnOlAAAQQd/ k= X-Received: by 2002:a05:6e02:216c:b0:313:ee3f:2b2b with SMTP id s12-20020a056e02216c00b00313ee3f2b2bmr10254303ilv.8.1675979376489; Thu, 09 Feb 2023 13:49:36 -0800 (PST) X-Google-Smtp-Source: AK7set+TvkVi2/zR06iqEO8aude71T8ISLufZNCYpfDFha1a1sVm9BjA+RvmWLxM8q9L2OpUnoP4Fw== X-Received: by 2002:a05:6e02:216c:b0:313:ee3f:2b2b with SMTP id s12-20020a056e02216c00b00313ee3f2b2bmr10254285ilv.8.1675979376189; Thu, 09 Feb 2023 13:49:36 -0800 (PST) Received: from [192.168.1.104] (23-233-12-120.cpe.pppoe.ca. [23.233.12.120]) by smtp.gmail.com with ESMTPSA id k11-20020a928e4b000000b0030ef5881a51sm773864ilh.34.2023.02.09.13.49.34 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 09 Feb 2023 13:49:35 -0800 (PST) Message-ID: Date: Thu, 9 Feb 2023 16:49:34 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.0 Subject: [pushed] [PR103541] RA: Implement reuse of equivalent memory for caller saves optimization (version 2) To: "gcc-patches@gcc.gnu.org" X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: Vladimir Makarov via Gcc-patches From: Vladimir Makarov Reply-To: Vladimir Makarov Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This is another try to solve https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103541 The patch was successfully bootstrapped (--enable-languages=all) and tested on x86, x86-64, aarch64 commit 1ad898d18904ac68432ba9b8ffa2b083d007cc2d Author: Vladimir N. Makarov Date: Thu Feb 9 15:18:48 2023 -0500 RA: Implement reuse of equivalent memory for caller saves optimization (2nd version) The test pr103541.c shows opportunity to reuse memory with constant address for caller saves optimization for constant or pure function call. The patch implements the memory reuse. PR rtl-optimization/103541 PR rtl-optimization/108711 gcc/ChangeLog: * ira.h (struct ira_reg_equiv_s): Add new field caller_save_p. * ira.cc (validate_equiv_mem): Check memref address variance. (no_equiv): Clear caller_save_p flag. (update_equiv_regs): Define caller save equivalence for valid_combine. (setup_reg_equiv): Clear defined_p flag for caller save equivalence. * lra-constraints.cc (lra_copy_reg_equiv): Add new arg call_save_p. Use caller save equivalence depending on the arg. (split_reg): Adjust the call. gcc/testsuite/ChangeLog: * gcc.target/i386/pr103541.c: New. * g++.target/i386/pr108711.C: New. diff --git a/gcc/ira.cc b/gcc/ira.cc index 66df03e8a59..6143db06c52 100644 --- a/gcc/ira.cc +++ b/gcc/ira.cc @@ -3070,6 +3070,8 @@ validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED, info->equiv_mem_modified = true; } +static int equiv_init_varies_p (rtx x); + enum valid_equiv { valid_none, valid_combine, valid_reload }; /* Verify that no store between START and the death of REG invalidates @@ -3113,7 +3115,8 @@ validate_equiv_mem (rtx_insn *start, rtx reg, rtx memref) been changed and all hell breaks loose. */ ret = valid_combine; if (!MEM_READONLY_P (memref) - && !RTL_CONST_OR_PURE_CALL_P (insn)) + && (!RTL_CONST_OR_PURE_CALL_P (insn) + || equiv_init_varies_p (XEXP (memref, 0)))) return valid_none; } @@ -3414,6 +3417,7 @@ no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED, if (reg_equiv[regno].is_arg_equivalence) return; ira_reg_equiv[regno].defined_p = false; + ira_reg_equiv[regno].caller_save_p = false; ira_reg_equiv[regno].init_insns = NULL; for (; list; list = list->next ()) { @@ -3766,7 +3770,18 @@ update_equiv_regs (void) { replacement = copy_rtx (SET_SRC (set)); if (validity == valid_reload) - note = set_unique_reg_note (insn, REG_EQUIV, replacement); + { + note = set_unique_reg_note (insn, REG_EQUIV, replacement); + } + else + { + /* We still can use this equivalence for caller save + optimization in LRA. Mark this. */ + ira_reg_equiv[regno].caller_save_p = true; + ira_reg_equiv[regno].init_insns + = gen_rtx_INSN_LIST (VOIDmode, insn, + ira_reg_equiv[regno].init_insns); + } } } @@ -4156,7 +4171,7 @@ setup_reg_equiv (void) legitimate, we ignore such REG_EQUIV notes. */ if (memory_operand (x, VOIDmode)) { - ira_reg_equiv[i].defined_p = true; + ira_reg_equiv[i].defined_p = !ira_reg_equiv[i].caller_save_p; ira_reg_equiv[i].memory = x; continue; } diff --git a/gcc/ira.h b/gcc/ira.h index 58b50dbe8a2..3d35025a46e 100644 --- a/gcc/ira.h +++ b/gcc/ira.h @@ -175,8 +175,11 @@ extern struct target_ira *this_target_ira; /* Major structure describing equivalence info for a pseudo. */ struct ira_reg_equiv_s { - /* True if we can use this equivalence. */ + /* True if we can use this as a general equivalence. */ bool defined_p; + /* True if we can use this equivalence only for caller save/restore + location. */ + bool caller_save_p; /* True if the usage of the equivalence is profitable. */ bool profitable_p; /* Equiv. memory, constant, invariant, and initializing insns of diff --git a/gcc/lra-constraints.cc b/gcc/lra-constraints.cc index 7bffbc07ee2..dd4f68bbfc0 100644 --- a/gcc/lra-constraints.cc +++ b/gcc/lra-constraints.cc @@ -5771,14 +5771,17 @@ choose_split_class (enum reg_class allocno_class, return best_cl; } -/* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO. - It only makes sense to call this function if NEW_REGNO is always - equal to ORIGINAL_REGNO. */ +/* Copy any equivalence information from ORIGINAL_REGNO to NEW_REGNO. It only + makes sense to call this function if NEW_REGNO is always equal to + ORIGINAL_REGNO. Set up defined_p flag when caller_save_p flag is set up and + CALL_SAVE_P is true. */ static void -lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno) +lra_copy_reg_equiv (unsigned int new_regno, unsigned int original_regno, + bool call_save_p) { - if (!ira_reg_equiv[original_regno].defined_p) + if (!ira_reg_equiv[original_regno].defined_p + && !(call_save_p && ira_reg_equiv[original_regno].caller_save_p)) return; ira_expand_reg_equiv (); @@ -5958,7 +5961,7 @@ split_reg (bool before_p, int original_regno, rtx_insn *insn, rematerializing the original value instead of spilling to the stack. */ if (!HARD_REGISTER_NUM_P (original_regno) && mode == PSEUDO_REGNO_MODE (original_regno)) - lra_copy_reg_equiv (new_regno, original_regno); + lra_copy_reg_equiv (new_regno, original_regno, call_save_p); lra_reg_info[new_regno].restore_rtx = regno_reg_rtx[original_regno]; bitmap_set_bit (&lra_split_regs, new_regno); if (to != NULL) diff --git a/gcc/testsuite/g++.target/i386/pr108711.C b/gcc/testsuite/g++.target/i386/pr108711.C new file mode 100644 index 00000000000..0a223e005e5 --- /dev/null +++ b/gcc/testsuite/g++.target/i386/pr108711.C @@ -0,0 +1,20 @@ +/* PR target/108711.C */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fno-exceptions" } */ +struct Expression_list { + Expression_list *copy(); +} vals_; +struct Parser_expression { + Parser_expression(); +}; +struct Composite_literal_expression : Parser_expression { + Composite_literal_expression(bool has_keys, Expression_list *, + bool all_are_names) + : has_keys_(has_keys), all_are_names_(all_are_names) {} + void do_copy(); + bool has_keys_; + bool all_are_names_; +}; +void Composite_literal_expression::do_copy() { + new Composite_literal_expression(has_keys_, vals_.copy(), all_are_names_); +} diff --git a/gcc/testsuite/gcc.target/i386/pr103541.c b/gcc/testsuite/gcc.target/i386/pr103541.c new file mode 100644 index 00000000000..72b257d42ee --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr103541.c @@ -0,0 +1,14 @@ +/* PR rtl-optimization/103541 */ +/* { dg-do compile { target x86_64-*-* } } */ +/* { dg-options "-O2" } */ + +float a; +__attribute__((const)) float foo (float); + +float +test() +{ + return a + foo(a) + a; +} + +/* { dg-final { scan-assembler-not "\\\(%rsp\\\)" } } */