From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 66862385828D for ; Tue, 17 Oct 2023 08:36:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 66862385828D 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 66862385828D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697531800; cv=none; b=WpeRZ7eCwfr2nAy9nRNL4ndmjvLEETgU6/QKaN/eIA+VW2y0xGGGCRFvHjPWdU9j3or30DIm9ZXvOHh76usawh646/YjK+oz+8YgvWEmmHrAKF+V5Xv9XJ4QZzhYB3mScGIXoDgnFvmOew/ONtDbIRUVLNbJheCM/cGfqriygpE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697531800; c=relaxed/simple; bh=1lF5iF1Y57Qz3Yan+gV5zEUU5S0eQHfUTEQ5LlNEqy0=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=n1dvISfoQzDHkXCVnw6Pc3SK1LWkce+u3IAi5lQ/60homN8YzTx0pIVUl1vQZIIHX2Q/V927VQpy9tBBOJPNskn+9l16ANiDPWCKa4m22ioLd2V6i4hS+pB3srE6Z/gzjV42gx0fF62v+xZvRDm8QmlORMaPZGVAku7EK9b/QfQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-507a29c7eefso3601024e87.1 for ; Tue, 17 Oct 2023 01:36:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697531796; x=1698136596; darn=gcc.gnu.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Nx9t8uCtD3Zb85M3OlOKna/4VdYh7pstpqtNAnr/9os=; b=X8yy4jIOCdHMZf1hnXBurFv9hUGDyARdbSBzlscevy2t60G0AkqI+5VzmMMYMOM40t DN8dHJIfH8i4ncnkI4qmz6E1oYLy8/acuUe6/qXzo0+NO2ri7AopiztQH94/lJqME/8w xK8EYhCYDM+kKuBrsa52QsTAdDd47ZVUdWfVFqHh61Y4CtJHFTOhCKdMxvRKWOHV9Lja 3H9qCYQ8TM0lIJWxb0Wj8SlXBIRyXNKPiETFsoqZOuwRaNKdv1o28X8eBoSYO0aJAkZT Lo/y7nTbs6hD5eof5kSMwVZl9DY5OptzMESOzq2t9TRIO7ejERjTCzt+b9S+W879zXUK aEHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697531796; x=1698136596; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Nx9t8uCtD3Zb85M3OlOKna/4VdYh7pstpqtNAnr/9os=; b=CKe1AnppVsGx54NYQS+Mu3r9k30nOdLdyIpvZg8ID77n0sY1Z21jl4DLQil3QqDQEY aHC8NmRrnCqMYGdJf+88APnaobfkm7+pQaR0eiFRMeCW4Onb1IcL3ipCgAy08lApGbsQ VQ8crydUt52ahiaj3VtoUAalxvFH4dEGrRhHwFQEXXcmFgWPOY8VG7HyURjqUyMwSM0n 99n356hUih7cFEuD53Y/CLHj9rM6MSsYub9pyHR41Gc7F1EVzhpF1HAPdpcdyvdrrexY eeGeGbtVjNHyUagTDKvQeU8Ne8xx75jvO7gBcuF3/p+LQ2KU5ucEI4rCBHT85aBBUtYa fmow== X-Gm-Message-State: AOJu0YxzgxONVV+a/TUC2qQIyW5DtWRQIV5uAlZa25APZ0JIe/dRP2yi 2P8dwk/WruCi9i6aTR6hDp4XwcmtAp2M3hPpvk20rwrd X-Google-Smtp-Source: AGHT+IHbx/DRF9BC6A3LSMPExoizM4ANc2PXIjObNjkLhe6s8iBAlne83f8iJEU/GcZ1W/yl4XdHEftcleiMg2ybQwc= X-Received: by 2002:a05:6512:2019:b0:503:c51:74e1 with SMTP id a25-20020a056512201900b005030c5174e1mr1240050lfb.15.1697531795369; Tue, 17 Oct 2023 01:36:35 -0700 (PDT) MIME-Version: 1.0 References: <79f04438-7473-2b01-d26a-9357ad9318af@linux.ibm.com> In-Reply-To: <79f04438-7473-2b01-d26a-9357ad9318af@linux.ibm.com> From: Richard Biener Date: Tue, 17 Oct 2023 10:33:38 +0200 Message-ID: Subject: Re: [PATCH v8] tree-ssa-sink: Improve code sinking pass To: Ajit Agarwal Cc: gcc-patches , Jeff Law , Segher Boessenkool , Peter Bergner Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.1 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 List-Id: On Thu, Oct 12, 2023 at 10:42=E2=80=AFAM Ajit Agarwal wrote: > > This patch improves code sinking pass to sink statements before call to r= educe > register pressure. > Review comments are incorporated. Synced and modified with latest trunk s= ources. > > For example : > > void bar(); > int j; > void foo(int a, int b, int c, int d, int e, int f) > { > int l; > l =3D a + b + c + d +e + f; > if (a !=3D 5) > { > bar(); > j =3D l; > } > } > > Code Sinking does the following: > > void bar(); > int j; > void foo(int a, int b, int c, int d, int e, int f) > { > int l; > > if (a !=3D 5) > { > l =3D a + b + c + d +e + f; > bar(); > j =3D l; > } > } > > Bootstrapped regtested on powerpc64-linux-gnu. > > Thanks & Regards > Ajit > > tree-ssa-sink: Improve code sinking pass > > Currently, code sinking will sink code after function calls. This increa= ses > register pressure for callee-saved registers. The following patch improv= es > code sinking by placing the sunk code before calls in the use block or in > the immediate dominator of the use blocks. The patch no longer does what the description above says. More comments below. > 2023-10-12 Ajit Kumar Agarwal > > gcc/ChangeLog: > > PR tree-optimization/81953 > * tree-ssa-sink.cc (statement_sink_location): Move statements bef= ore > calls. > (select_best_block): Add heuristics to select the best blocks in = the > immediate post dominator. > > gcc/testsuite/ChangeLog: > > PR tree-optimization/81953 > * gcc.dg/tree-ssa/ssa-sink-20.c: New test. > * gcc.dg/tree-ssa/ssa-sink-21.c: New test. > --- > gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c | 15 ++++++++ > gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-22.c | 19 ++++++++++ > gcc/tree-ssa-sink.cc | 39 ++++++++++++--------- > 3 files changed, 56 insertions(+), 17 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-22.c > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c b/gcc/testsuite/= gcc.dg/tree-ssa/ssa-sink-21.c > new file mode 100644 > index 00000000000..d3b79ca5803 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c > @@ -0,0 +1,15 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-sink-stats" } */ > +void bar(); > +int j; > +void foo(int a, int b, int c, int d, int e, int f) > +{ > + int l; > + l =3D a + b + c + d +e + f; > + if (a !=3D 5) > + { > + bar(); > + j =3D l; > + } > +} > +/* { dg-final { scan-tree-dump {l_12\s+=3D\s+_4\s+\+\s+f_11\(D\);\n\s+ba= r\s+\(\)} sink1 } } */ > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-22.c b/gcc/testsuite/= gcc.dg/tree-ssa/ssa-sink-22.c > new file mode 100644 > index 00000000000..84e7938c54f > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-22.c > @@ -0,0 +1,19 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-sink-stats" } */ > +void bar(); > +int j, x; > +void foo(int a, int b, int c, int d, int e, int f) > +{ > + int l; > + l =3D a + b + c + d +e + f; > + if (a !=3D 5) > + { > + bar(); > + if (b !=3D 3) > + x =3D 3; > + else > + x =3D 5; > + j =3D l; > + } > +} > +/* { dg-final { scan-tree-dump {l_13\s+=3D\s+_4\s+\+\s+f_12\(D\);\n\s+ba= r\s+\(\)} sink1 } } */ > diff --git a/gcc/tree-ssa-sink.cc b/gcc/tree-ssa-sink.cc > index a360c5cdd6e..95298bc8402 100644 > --- a/gcc/tree-ssa-sink.cc > +++ b/gcc/tree-ssa-sink.cc > @@ -174,7 +174,8 @@ nearest_common_dominator_of_uses (def_operand_p def_p= , bool *debug_stmts) > > /* Given EARLY_BB and LATE_BB, two blocks in a path through the dominato= r > tree, return the best basic block between them (inclusive) to place > - statements. > + statements. The best basic block should be an immediate dominator of > + best basic block if the use stmt is after the call. > > We want the most control dependent block in the shallowest loop nest. > > @@ -196,6 +197,16 @@ select_best_block (basic_block early_bb, > basic_block best_bb =3D late_bb; > basic_block temp_bb =3D late_bb; > int threshold; > + /* Get the sinking threshold. If the statement to be moved has memory > + operands, then increase the threshold by 7% as those are even more > + profitable to avoid, clamping at 100%. */ > + threshold =3D param_sink_frequency_threshold; > + if (gimple_vuse (stmt) || gimple_vdef (stmt)) > + { > + threshold +=3D 7; > + if (threshold > 100) > + threshold =3D 100; > + } > > while (temp_bb !=3D early_bb) > { > @@ -204,6 +215,14 @@ select_best_block (basic_block early_bb, > if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb)) > best_bb =3D temp_bb; > > + /* if we have temp_bb post dominated by use block block then immed= iate > + * dominator would be our best block. */ > + if (!gimple_vuse (stmt) > + && bb_loop_depth (temp_bb) =3D=3D bb_loop_depth (early_bb) > + && !(temp_bb->count * 100 >=3D early_bb->count * threshold) > + && dominated_by_p (CDI_DOMINATORS, late_bb, temp_bb)) this isn't a post-dominance check, in fact this always returns true. This also overrides the best found loop depth which probably means finding both inside the same loop doesn't work. What's the intent of the change? > + best_bb =3D temp_bb; > + > /* Walk up the dominator tree, hopefully we'll find a shallower > loop nest. */ > temp_bb =3D get_immediate_dominator (CDI_DOMINATORS, temp_bb); > @@ -233,17 +252,6 @@ select_best_block (basic_block early_bb, > && !dominated_by_p (CDI_DOMINATORS, best_bb->loop_father->latch, b= est_bb)) > return early_bb; > > - /* Get the sinking threshold. If the statement to be moved has memory > - operands, then increase the threshold by 7% as those are even more > - profitable to avoid, clamping at 100%. */ > - threshold =3D param_sink_frequency_threshold; > - if (gimple_vuse (stmt) || gimple_vdef (stmt)) > - { > - threshold +=3D 7; > - if (threshold > 100) > - threshold =3D 100; > - } > - > /* If BEST_BB is at the same nesting level, then require it to have > significantly lower execution frequency to avoid gratuitous movemen= t. */ > if (bb_loop_depth (best_bb) =3D=3D bb_loop_depth (early_bb) > @@ -430,6 +438,7 @@ statement_sink_location (gimple *stmt, basic_block fr= ombb, > continue; > break; > } > + > use =3D USE_STMT (one_use); > > if (gimple_code (use) !=3D GIMPLE_PHI) > @@ -439,10 +448,7 @@ statement_sink_location (gimple *stmt, basic_block f= rombb, > if (sinkbb =3D=3D frombb) > return false; > > - if (sinkbb =3D=3D gimple_bb (use)) > - *togsi =3D gsi_for_stmt (use); > - else > - *togsi =3D gsi_after_labels (sinkbb); > + *togsi =3D gsi_after_labels (sinkbb); > > return true; > } > @@ -825,7 +831,6 @@ pass_sink_code::execute (function *fun) > mark_dfs_back_edges (fun); > memset (&sink_stats, 0, sizeof (sink_stats)); > calculate_dominance_info (CDI_DOMINATORS); > - > virtual_operand_live vop_live; > > int *rpo =3D XNEWVEC (int, n_basic_blocks_for_fn (cfun)); > -- > 2.39.3 >