From patchwork Tue Oct 24 17:58:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Sandiford X-Patchwork-Id: 157653 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2112051vqx; Tue, 24 Oct 2023 10:59:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHCyuR/iNraovSKks+t0+RQJqZW50aFu9dh2JEROd752SspPvu/JPKDMWDtO1U9PdHBRz75 X-Received: by 2002:a05:622a:1a16:b0:418:f70:df1 with SMTP id f22-20020a05622a1a1600b004180f700df1mr14693017qtb.42.1698170340659; Tue, 24 Oct 2023 10:59:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698170340; cv=pass; d=google.com; s=arc-20160816; b=ua5+6yc48xv1QW305+8qq+Zw2KqjRRYzAZpj9R2BLLj2X9L9VAWDhDLfQPZtBDvRH7 7S9udQPL3kWmZ6EvSKjrZlXETPut2eR1rqRHexydNLHPf8A0SBrRMVzVtNtN7Ew0Yz9S 1uhW+fGBgu8Frkyo8fKKqMH8AQ9mU1+Tu3zeBWf/b9gfNZFQB2uDX8ib6hWlWkTX7M6w KiEd8wQiSY24CZvzu74/aUywgXChykzFSDZxhCOPv/d07zbNvI8x7imy7E0aF/TU3ZLR OumshrdpsGQ1ecR/dAatfS95FrQ/9uVM3TCJxv52PoTMJalALm0Z0m6BLBk7rIb3x27T 1IGA== 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:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:arc-filter:dmarc-filter:delivered-to; bh=GY8Y+5II9vLrxqQSZ18mqPzGfn1ry6LGIA+M0r0qPs4=; fh=WSfZ840Wi6BqtTz8VZjapJ1yCCJ9QRCfxSVxwOS9rLc=; b=GEsW4eEcP9nXef5oWDIIeuCAOK303OMPIfXIJ5byhasfRpbBQHdMw6aQvyPlQzxp7B ok1sE63EAm7N64A3e7UP7H6GnHpV7v/38qrbhNii46EB9xUDSCFhkBahlIPlxB7pCHf/ ZeOv6BefpIu/pKSR4ugkxhICoEglN8Jm2khlP/fjfA9UgdUvI2h28+oFPLOT+jPtvUMy 6Ds+7v3KKTsRgs1F37De0UuJBt0Otl6xmBDRBFqU9Bc/qwg0SBBM/QOUn+Jn3inxb1we 56HFnMuKgRe50lhlAoQmGFT7OWgNB06a9uluHwaDml0vLbrY+MfPdgl2CSckqPA09ASw kJDQ== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1); 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id n15-20020a05622a040f00b00419930eb097si7703690qtx.215.2023.10.24.10.59.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 10:59:00 -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; arc=pass (i=1); 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E92FD385F015 for ; Tue, 24 Oct 2023 17:58:48 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id 43C5D3858CDB for ; Tue, 24 Oct 2023 17:58:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 43C5D3858CDB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 43C5D3858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698170304; cv=none; b=O1Ia2Jk2sEeQNlnSCmwA/i7LOoDD5wI/fJEFy89WAUOcV864xMMZHTv88QvvfSkFSn4dL6HknFc+eBuLY5F1k1JX7baBzLWTeg3ZjvgniseZ5uXe7L8eLxY1ohtqn3TX/L1ZiE3gyHU31g9S4YVt/zUVY/Wto7t1H4KJgcd1dfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698170304; c=relaxed/simple; bh=DMObM9pB/ZwIzZRoa2+OAGN/UxM0pXwKzG1QLyXzhbw=; h=From:To:Subject:Date:Message-Id:MIME-Version; b=fkqNbjUEHNbpE1Q+Q9V+mF91UDKfZ/JLco5FWE0lcGRBVkrD5IHU/kCFKKB2MvMYwdkoJImoljNg5onmAlSjlC0quabjEaYFSBM5hwDHBo3DcEIWWYXHUpKtVzOZE8FmA4rJiJimXBc/wW8kCChOY+MzW/n77G9/5wXZKoFHPUg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 0D6C62F4; Tue, 24 Oct 2023 10:59:04 -0700 (PDT) Received: from e121540-lin.manchester.arm.com (e121540-lin.manchester.arm.com [10.32.110.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5112B3F64C; Tue, 24 Oct 2023 10:58:22 -0700 (PDT) From: Richard Sandiford To: jlaw@ventanamicro.com, gcc-patches@gcc.gnu.org Cc: Richard Sandiford Subject: [PATCH 1/3] rtl-ssa: Use frequency-weighted insn costs Date: Tue, 24 Oct 2023 18:58:03 +0100 Message-Id: <20231024175805.3359331-2-richard.sandiford@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20231024175805.3359331-1-richard.sandiford@arm.com> References: <20231024175805.3359331-1-richard.sandiford@arm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-23.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, 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.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: 1780660663289485010 X-GMAIL-MSGID: 1780660663289485010 rtl_ssa::changes_are_worthwhile used the standard approach of summing up the individual costs of the old and new sequences to see which one is better overall. But when optimising for speed and changing instructions in multiple blocks, it seems better to weight the cost of each instruction by its execution frequency. (We already do something similar for SLP layouts.) gcc/ * rtl-ssa/changes.cc: Include sreal.h. (rtl_ssa::changes_are_worthwhile): When optimizing for speed, scale the cost of each instruction by its execution frequency. --- gcc/rtl-ssa/changes.cc | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/gcc/rtl-ssa/changes.cc b/gcc/rtl-ssa/changes.cc index 3e14069421c..aab532b9f26 100644 --- a/gcc/rtl-ssa/changes.cc +++ b/gcc/rtl-ssa/changes.cc @@ -34,6 +34,7 @@ #include "emit-rtl.h" #include "cfghooks.h" #include "cfgrtl.h" +#include "sreal.h" using namespace rtl_ssa; @@ -171,18 +172,33 @@ rtl_ssa::changes_are_worthwhile (array_slice changes, { unsigned int old_cost = 0; unsigned int new_cost = 0; + sreal weighted_old_cost = 0; + sreal weighted_new_cost = 0; + auto entry_count = ENTRY_BLOCK_PTR_FOR_FN (cfun)->count; for (insn_change *change : changes) { old_cost += change->old_cost (); + basic_block cfg_bb = change->bb ()->cfg_bb (); + bool for_speed = optimize_bb_for_speed_p (cfg_bb); + if (for_speed) + weighted_old_cost += (cfg_bb->count.to_sreal_scale (entry_count) + * change->old_cost ()); if (!change->is_deletion ()) { - basic_block cfg_bb = change->bb ()->cfg_bb (); - change->new_cost = insn_cost (change->rtl (), - optimize_bb_for_speed_p (cfg_bb)); + change->new_cost = insn_cost (change->rtl (), for_speed); new_cost += change->new_cost; + if (for_speed) + weighted_new_cost += (cfg_bb->count.to_sreal_scale (entry_count) + * change->new_cost); } } - bool ok_p = (strict_p ? new_cost < old_cost : new_cost <= old_cost); + bool ok_p; + if (weighted_new_cost != weighted_old_cost) + ok_p = weighted_new_cost < weighted_old_cost; + else if (strict_p) + ok_p = new_cost < old_cost; + else + ok_p = new_cost <= old_cost; if (dump_file && (dump_flags & TDF_DETAILS)) { fprintf (dump_file, "original cost"); @@ -192,6 +208,8 @@ rtl_ssa::changes_are_worthwhile (array_slice changes, fprintf (dump_file, " %c %d", sep, change->old_cost ()); sep = '+'; } + if (weighted_old_cost != 0) + fprintf (dump_file, " (weighted: %f)", weighted_old_cost.to_double ()); fprintf (dump_file, ", replacement cost"); sep = '='; for (const insn_change *change : changes) @@ -200,6 +218,8 @@ rtl_ssa::changes_are_worthwhile (array_slice changes, fprintf (dump_file, " %c %d", sep, change->new_cost); sep = '+'; } + if (weighted_new_cost != 0) + fprintf (dump_file, " (weighted: %f)", weighted_new_cost.to_double ()); fprintf (dump_file, "; %s\n", ok_p ? "keeping replacement" : "rejecting replacement"); }