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: 1854587 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=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SFKYV0Dcgz23jn for ; Wed, 25 Oct 2023 04:58:38 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 13A783857342 for ; Tue, 24 Oct 2023 17:58:36 +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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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"); }