From patchwork Thu Oct 26 14:00:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Vladimir Makarov X-Patchwork-Id: 158557 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp695754vqb; Thu, 26 Oct 2023 07:00:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFcGPDm7544L9gMQoiw423RRprMGqjOpoWmNrCq9emKw+T5lUD8OMmGCqfH8Z2dlDhOkgGw X-Received: by 2002:a05:622a:6:b0:41c:e88c:8e5c with SMTP id x6-20020a05622a000600b0041ce88c8e5cmr17509815qtw.63.1698328849050; Thu, 26 Oct 2023 07:00:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698328849; cv=pass; d=google.com; s=arc-20160816; b=nuWpoMjk7LaqRqVpfcuZ2ntQz5rBHs2kNHLFT2v6YzJg6pnzqwfR29bxPrXh1Zr9u2 imFOTMnSq+KVbKQ8ZA59/Pk03ME472CeX5/ezcOwu3++f95Ayetqh9s3RvNpLWHSQ5TT 1M+pgEw6Wt8MTC8d3sqt4nHXidiwLwj6KDtAzWfoYOBMFBriMqODb8L7+TaBKM9qSXq1 s1umVhga4VXTjP5EJug/t+bkSslMRcOkN+9J1YvFHirc4r2KvnUOTEusl0UyAGoK5W4d 2MdzEh0ylbkp/9+Klps8URtsX2O2pqRGR//e456HhX7sbLH93KBgwIjrEAZqHS6HpeJf pKDA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:from:to :content-language:user-agent:mime-version:date:message-id :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=2BrOECqNd+4Mxzl9ZLKEY86fOdQGNox1xI67fGEOPVE=; fh=XNn3asQvIblazGK92GBt13dVv+YmGV3pBS0JC29ZQco=; b=OScRuZ22/3BvXVzINz2RweJE1IM8P1iq1K8ksexoNRdf10DjqnZA18UnOb8p4/RaK9 eoz46AKK97m7ebNgjnET1WPob8UhCde1pfcuZQ32T4GjfoJX5aXYdsKOX5L2al1dnkBD qIDdOSHX27mqfZE5GYAhd8cTVV3+D8ZLLe9lYWt66f5bfNUkOfwfQge2BDxdaVE4771b JOlZ13q5l+RCduNz8IoKFhpGKSehnE9rfNun9CY/0iGaxVJmoynRKTXCdCrCq6AezjVK Xgvptz83qCZSVz9RtvJXec4Yz/131nMr52nqnhEVu/LtXylEhGUNLDNF5B9dz5UAGDYI LDAw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=K0nSEYqu; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id o9-20020ac85a49000000b0041b66272051si9269536qta.471.2023.10.26.07.00.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Oct 2023 07:00:49 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=K0nSEYqu; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8EF4C385C303 for ; Thu, 26 Oct 2023 14:00:48 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by sourceware.org (Postfix) with ESMTPS id D89EB3858D37 for ; Thu, 26 Oct 2023 14:00:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D89EB3858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D89EB3858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::72d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698328824; cv=none; b=C0u+JGM7OLOY3gurEaN5TCt/kDhAIGeiSjHzrrbYXZolWqsrpHCPrBIPgskH2o9CE3UiLsdxzx+MgdN3L7/EMYaOclJigBBf1nly05bE1DVxX1gbVm0wzXzyUz2lY+JijsICsDtxK0ja2xl+Y/t5ik7o5djaMr9WBISDcoV8oZE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698328824; c=relaxed/simple; bh=IBCcpafJiBgcl/bGAYtK7wRCAZVaoM0MGz5mg4en9Qc=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=O8UyozhvX/CuZoo2DcZHpXmzTHH3bHmdg2uJvJ17TZV1ggbyIYz2aPmWJoUKA8mF0pNYUfly7wrfACBFQAMvY3jbFyFGSxroLX0krRXCVrO36JNZuXCnoaPFoEn0Rtmx5ioZE2ilMHCAuhvF35J5dOmo9GHIbxgvxXqIwPLogS8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qk1-x72d.google.com with SMTP id af79cd13be357-7789923612dso74395385a.0 for ; Thu, 26 Oct 2023 07:00:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698328821; x=1698933621; darn=gcc.gnu.org; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=2BrOECqNd+4Mxzl9ZLKEY86fOdQGNox1xI67fGEOPVE=; b=K0nSEYquqwCxHPBtoKbhP76EiuutIWdhoMtEt3Rh5DLY3rOKGPMK/4k40Kmt1kf5Xo Pj/bzGFaht0LUGIS0uHo3eJ3ENcuGUCgFSRPb0v+PLxim7nXzT3FXd9wV2iePJaIiJ01 LzRfgrI35ytum2VV2nIzZcSudLKD49fY7eR0SYvCNcxHsFAdvUcvEwHP4Sz1kZHxj/eu 5RY0L+1Sd1VcmjuJ9mRsAdPtgh/NsGI3UqLar3yFsJZww6/IKFhjwANeCJcdDQHGOrYK b3KAzfaT++LswakFR522DoDxc98PlBYn/N5liyGZFpJuGGlAp0Lf+mogp0OdLRny8JIL EcLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698328821; x=1698933621; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2BrOECqNd+4Mxzl9ZLKEY86fOdQGNox1xI67fGEOPVE=; b=FmHK8/TkgDjytlXk1Gm8tHESKcgIVdgieoIRkGtfbiPATRTFEKAvfjv24YoWNSPyVl KwwWqeTLziFRsPGSBdAOg4Fl2ZCT/JF9IDxGUA5a92IegpAOHunlJ3IMUT7jR7qvkmQP pl4VQmWA7HWtlogSJR3sGCpvQb1dWxaV0v+03LLULERMrcMupDY9jsYReYlKJn3j1IyK BzeHvoT3+OMedRYvW4wMmM/pWDFezzgqI2CGd8L7PxzqBbX/MZO4hmstnDDWbkk/GRqN 9u0bSuyV9Uk/WsOX+urftg1C2p69w9MaGmCWImLGKWXEUZhgj4WQxGR6jgrrq2vbH3fD kCZg== X-Gm-Message-State: AOJu0Yxa8z6lTq8X6P2k3uRJHibvVQbcAoVQaE2ZAYWtyvpTaioCMJeC tXrR2jeq4wo5Pg2R1QOxvvAgFZ5iMQk= X-Received: by 2002:a05:620a:2947:b0:779:f0be:8d7e with SMTP id n7-20020a05620a294700b00779f0be8d7emr7909633qkp.27.1698328821103; Thu, 26 Oct 2023 07:00:21 -0700 (PDT) Received: from [192.168.1.88] (23-233-12-249.cpe.pppoe.ca. [23.233.12.249]) by smtp.gmail.com with ESMTPSA id z12-20020ae9e60c000000b0077772296f9dsm4948767qkf.126.2023.10.26.07.00.19 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 26 Oct 2023 07:00:20 -0700 (PDT) Message-ID: Date: Thu, 26 Oct 2023 10:00:18 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Content-Language: en-US To: "gcc-patches@gcc.gnu.org" From: Vladimir Makarov Subject: [pushed] [RA]: Modify cost calculation for dealing with pseudo equivalences X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780826871291690419 X-GMAIL-MSGID: 1780826871291690419 This is the second attempt to improve RA cost calculation for pseudos with equivalences.  The patch explanation is in the log message. The patch was successfully bootstrapped and tested on x86-64, aarch64, and ppc64le.  The patch was also benchmarked on x86-64 spec2017.  specfp2017 performance did not changed, specint2017 improved by 0.3%. commit f55cdce3f8dd8503e080e35be59c5f5390f6d95e Author: Vladimir N. Makarov Date: Thu Oct 26 09:50:40 2023 -0400 [RA]: Modfify cost calculation for dealing with equivalences RISCV target developers reported that pseudos with equivalence used in a loop can be spilled. Simple changes of heuristics of cost calculation of pseudos with equivalence or even ignoring equivalences resulted in numerous testsuite failures on different targets or worse spec2017 performance. This patch implements more sophisticated cost calculations of pseudos with equivalences. The patch does not change RA behaviour for targets still using the old reload pass instead of LRA. The patch solves the reported problem and improves x86-64 specint2017 a bit (specfp2017 performance stays the same). The patch takes into account how the equivalence will be used: will it be integrated into the user insns or require an input reload insn. It requires additional pass over insns. To compensate RA slow down, the patch removes a pass over insns in the reload pass used by IRA before. This also decouples IRA from reload more and will help to remove the reload pass in the future if it ever happens. gcc/ChangeLog: * dwarf2out.cc (reg_loc_descriptor): Use lra_eliminate_regs when LRA is used. * ira-costs.cc: Include regset.h. (equiv_can_be_consumed_p, get_equiv_regno, calculate_equiv_gains): New functions. (find_costs_and_classes): Call calculate_equiv_gains and redefine mem_cost of pseudos with equivs when LRA is used. * var-tracking.cc: Include ira.h and lra.h. (vt_initialize): Use lra_eliminate_regs when LRA is used. diff --git a/gcc/dwarf2out.cc b/gcc/dwarf2out.cc index 0ea73bf782e..1e0cec66c5e 100644 --- a/gcc/dwarf2out.cc +++ b/gcc/dwarf2out.cc @@ -14311,7 +14311,9 @@ reg_loc_descriptor (rtx rtl, enum var_init_status initialized) argument pointer and soft frame pointer rtx's. Use DW_OP_fbreg offset DW_OP_stack_value in this case. */ if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx) - && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl) + && (ira_use_lra_p + ? lra_eliminate_regs (rtl, VOIDmode, NULL_RTX) + : eliminate_regs (rtl, VOIDmode, NULL_RTX)) != rtl) { dw_loc_descr_ref result = NULL; diff --git a/gcc/ira-costs.cc b/gcc/ira-costs.cc index d9e700e8947..a59d45a6e24 100644 --- a/gcc/ira-costs.cc +++ b/gcc/ira-costs.cc @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see #include "tm_p.h" #include "insn-config.h" #include "regs.h" +#include "regset.h" #include "ira.h" #include "ira-int.h" #include "addresses.h" @@ -1757,6 +1758,145 @@ process_bb_node_for_costs (ira_loop_tree_node_t loop_tree_node) process_bb_for_costs (bb); } +/* Check that reg REGNO can be changed by TO in INSN. Return true in case the + result insn would be valid one. */ +static bool +equiv_can_be_consumed_p (int regno, rtx to, rtx_insn *insn) +{ + validate_replace_src_group (regno_reg_rtx[regno], to, insn); + bool res = verify_changes (0); + cancel_changes (0); + return res; +} + +/* Return true if X contains a pseudo with equivalence. In this case also + return the pseudo through parameter REG. If the pseudo is a part of subreg, + return the subreg through parameter SUBREG. */ + +static bool +get_equiv_regno (rtx x, int ®no, rtx &subreg) +{ + subreg = NULL_RTX; + if (GET_CODE (x) == SUBREG) + { + subreg = x; + x = SUBREG_REG (x); + } + if (REG_P (x) + && (ira_reg_equiv[REGNO (x)].memory != NULL + || ira_reg_equiv[REGNO (x)].constant != NULL)) + { + regno = REGNO (x); + return true; + } + RTX_CODE code = GET_CODE (x); + const char *fmt = GET_RTX_FORMAT (code); + + for (int i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) + if (fmt[i] == 'e') + { + if (get_equiv_regno (XEXP (x, i), regno, subreg)) + return true; + } + else if (fmt[i] == 'E') + { + for (int j = 0; j < XVECLEN (x, i); j++) + if (get_equiv_regno (XVECEXP (x, i, j), regno, subreg)) + return true; + } + return false; +} + +/* A pass through the current function insns. Calculate costs of using + equivalences for pseudos and store them in regno_equiv_gains. */ + +static void +calculate_equiv_gains (void) +{ + basic_block bb; + int regno, freq, cost; + rtx subreg; + rtx_insn *insn; + machine_mode mode; + enum reg_class rclass; + bitmap_head equiv_pseudos; + + ira_assert (allocno_p); + bitmap_initialize (&equiv_pseudos, ®_obstack); + for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--) + if (ira_reg_equiv[regno].init_insns != NULL + && (ira_reg_equiv[regno].memory != NULL + || (ira_reg_equiv[regno].constant != NULL + /* Ignore complicated constants which probably will be placed + in memory: */ + && GET_CODE (ira_reg_equiv[regno].constant) != CONST_DOUBLE + && GET_CODE (ira_reg_equiv[regno].constant) != CONST_VECTOR + && GET_CODE (ira_reg_equiv[regno].constant) != LABEL_REF))) + { + rtx_insn_list *x; + for (x = ira_reg_equiv[regno].init_insns; x != NULL; x = x->next ()) + { + insn = x->insn (); + rtx set = single_set (insn); + + if (set == NULL_RTX || SET_DEST (set) != regno_reg_rtx[regno]) + break; + bb = BLOCK_FOR_INSN (insn); + ira_curr_regno_allocno_map + = ira_bb_nodes[bb->index].parent->regno_allocno_map; + mode = PSEUDO_REGNO_MODE (regno); + rclass = pref[COST_INDEX (regno)]; + ira_init_register_move_cost_if_necessary (mode); + if (ira_reg_equiv[regno].memory != NULL) + cost = ira_memory_move_cost[mode][rclass][1]; + else + cost = ira_register_move_cost[mode][rclass][rclass]; + freq = REG_FREQ_FROM_BB (bb); + regno_equiv_gains[regno] += cost * freq; + } + if (x != NULL) + /* We found complicated equiv or reverse equiv mem=reg. Ignore + them. */ + regno_equiv_gains[regno] = 0; + else + bitmap_set_bit (&equiv_pseudos, regno); + } + + FOR_EACH_BB_FN (bb, cfun) + { + freq = REG_FREQ_FROM_BB (bb); + ira_curr_regno_allocno_map + = ira_bb_nodes[bb->index].parent->regno_allocno_map; + FOR_BB_INSNS (bb, insn) + { + if (!INSN_P (insn) || !get_equiv_regno (PATTERN (insn), regno, subreg) + || !bitmap_bit_p (&equiv_pseudos, regno)) + continue; + rtx subst = ira_reg_equiv[regno].memory; + + if (subst == NULL) + subst = ira_reg_equiv[regno].constant; + ira_assert (subst != NULL); + mode = PSEUDO_REGNO_MODE (regno); + ira_init_register_move_cost_if_necessary (mode); + bool consumed_p = equiv_can_be_consumed_p (regno, subst, insn); + + rclass = pref[COST_INDEX (regno)]; + if (MEM_P (subst) + /* If it is a change of constant into double for example, the + result constant probably will be placed in memory. */ + || (subreg != NULL_RTX && !INTEGRAL_MODE_P (GET_MODE (subreg)))) + cost = ira_memory_move_cost[mode][rclass][1] + (consumed_p ? 0 : 1); + else if (consumed_p) + continue; + else + cost = ira_register_move_cost[mode][rclass][rclass]; + regno_equiv_gains[regno] -= cost * freq; + } + } + bitmap_clear (&equiv_pseudos); +} + /* Find costs of register classes and memory for allocnos or pseudos and their best costs. Set up preferred, alternative and allocno classes for pseudos. */ @@ -1855,6 +1995,12 @@ find_costs_and_classes (FILE *dump_file) if (pass == 0) pref = pref_buffer; + if (ira_use_lra_p && allocno_p && pass == 1) + /* It is a pass through all insns. So do it once and only for RA (not + for insn scheduler) when we already found preferable pseudo register + classes on the previous pass. */ + calculate_equiv_gains (); + /* Now for each allocno look at how desirable each class is and find which class is preferred. */ for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--) @@ -1947,6 +2093,17 @@ find_costs_and_classes (FILE *dump_file) } if (i >= first_moveable_pseudo && i < last_moveable_pseudo) i_mem_cost = 0; + else if (ira_use_lra_p) + { + if (equiv_savings > 0) + { + i_mem_cost = 0; + if (ira_dump_file != NULL && internal_flag_ira_verbose > 5) + fprintf (ira_dump_file, + " Use MEM for r%d as the equiv savings is %d\n", + i, equiv_savings); + } + } else if (equiv_savings < 0) i_mem_cost = -equiv_savings; else if (equiv_savings > 0) @@ -2395,7 +2552,10 @@ ira_costs (void) total_allocno_costs = (struct costs *) ira_allocate (max_struct_costs_size * ira_allocnos_num); initiate_regno_cost_classes (); - calculate_elim_costs_all_insns (); + if (!ira_use_lra_p) + /* Process equivs in reload to update costs through hook + ira_adjust_equiv_reg_cost. */ + calculate_elim_costs_all_insns (); find_costs_and_classes (ira_dump_file); setup_allocno_class_and_costs (); finish_regno_cost_classes (); @@ -2520,13 +2680,14 @@ ira_tune_allocno_costs (void) } } -/* Add COST to the estimated gain for eliminating REGNO with its - equivalence. If COST is zero, record that no such elimination is - possible. */ +/* A hook from the reload pass. Add COST to the estimated gain for eliminating + REGNO with its equivalence. If COST is zero, record that no such + elimination is possible. */ void ira_adjust_equiv_reg_cost (unsigned regno, int cost) { + ira_assert (!ira_use_lra_p); if (cost == 0) regno_equiv_gains[regno] = 0; else diff --git a/gcc/var-tracking.cc b/gcc/var-tracking.cc index d8dafa5481a..4a81ccbe486 100644 --- a/gcc/var-tracking.cc +++ b/gcc/var-tracking.cc @@ -107,6 +107,8 @@ #include "cfgrtl.h" #include "cfganal.h" #include "reload.h" +#include "ira.h" +#include "lra.h" #include "calls.h" #include "tree-dfa.h" #include "tree-ssa.h" @@ -10131,7 +10133,9 @@ vt_initialize (void) #else reg = arg_pointer_rtx; #endif - elim = eliminate_regs (reg, VOIDmode, NULL_RTX); + elim = (ira_use_lra_p + ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX) + : eliminate_regs (reg, VOIDmode, NULL_RTX)); if (elim != reg) { if (GET_CODE (elim) == PLUS) @@ -10151,7 +10155,9 @@ vt_initialize (void) reg = arg_pointer_rtx; fp_cfa_offset = ARG_POINTER_CFA_OFFSET (current_function_decl); #endif - elim = eliminate_regs (reg, VOIDmode, NULL_RTX); + elim = (ira_use_lra_p + ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX) + : eliminate_regs (reg, VOIDmode, NULL_RTX)); if (elim != reg) { if (GET_CODE (elim) == PLUS) @@ -10183,7 +10189,9 @@ vt_initialize (void) #else reg = arg_pointer_rtx; #endif - elim = eliminate_regs (reg, VOIDmode, NULL_RTX); + elim = (ira_use_lra_p + ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX) + : eliminate_regs (reg, VOIDmode, NULL_RTX)); if (elim != reg) { if (GET_CODE (elim) == PLUS)