From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by sourceware.org (Postfix) with ESMTPS id 3B1523898537 for ; Wed, 19 May 2021 19:05:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3B1523898537 Received: by mail-ot1-x32c.google.com with SMTP id 36-20020a9d0ba70000b02902e0a0a8fe36so12677989oth.8 for ; Wed, 19 May 2021 12:05:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SZ5e92Ahmm0Wv1bovbGyKEHiQTzLkciVCClLEzCziWM=; b=ZvoSa/dAaoeUNzLu3aXbgC7l9MrAyZKm8xK+XBN1S1IFzwAClrq7plgpVfjc9COoKv 8XwWdFDcWMPN9goSEIb5S5PRxm96nNFh4WiCr6hP/eVKDgugzztCuZUhnhX45OBo89FT Iq3ePdRBK92yjIqT/1AMHKq/oRWBclKwwAAL+oREJerph1se/BJkZCg7tR/idwdLCED7 UmLBi0e/dHYyy8Z9QxpEE8i9111m+uSOP/5nlYuxxgZvuhgdJfAICE3SaW0V90pn3VT9 BKzxtx+pMKHU40VwNTiy8j3IZ5eISj4PNxnMUCU/zcDXwtQPPB3Kb3T3gPPzDYf05vn4 Uwrg== X-Gm-Message-State: AOAM530AldWEk8Eq4Mn6NfMRRR0Hep83YnDsxfPIZEBApuLHo+qB1zBi sd/nH3UrDu0ASxZmHIHPW8ho5BXn35cJs22BACY= X-Google-Smtp-Source: ABdhPJwo0gP/owUbPEKA+7LDAwL15KFDpUpOjm/8JdMwBEUOpsQrZB/CH0clrvAhdUtcl3ek7Pu1hTh5ZykABRxTd2g= X-Received: by 2002:a9d:67cf:: with SMTP id c15mr764739otn.285.1621451102496; Wed, 19 May 2021 12:05:02 -0700 (PDT) MIME-Version: 1.0 References: <20210518191646.3071005-1-hjl.tools@gmail.com> <20210518191646.3071005-13-hjl.tools@gmail.com> In-Reply-To: From: "H.J. Lu" Date: Wed, 19 May 2021 12:04:26 -0700 Message-ID: Subject: Re: [PATCH v4 12/12] constructor: Check if it is faster to load constant from memory To: Bernd Edlinger Cc: Richard Biener , GCC Patches , Richard Sandiford , Uros Bizjak Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3033.9 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.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Wed, 19 May 2021 19:05:05 -0000 On Wed, May 19, 2021 at 6:27 AM Bernd Edlinger wrote: > > On 5/19/21 3:22 PM, H.J. Lu wrote: > > On Wed, May 19, 2021 at 2:33 AM Richard Biener > > wrote: > >> > >> On Tue, May 18, 2021 at 9:16 PM H.J. Lu wrote: > >>> > >>> When expanding a constant constructor, don't call expand_constructor if > >>> it is more efficient to load the data from the memory via move by pieces. > >>> > >>> gcc/ > >>> > >>> PR middle-end/90773 > >>> * expr.c (expand_expr_real_1): Don't call expand_constructor if > >>> it is more efficient to load the data from the memory. > >>> > >>> gcc/testsuite/ > >>> > >>> PR middle-end/90773 > >>> * gcc.target/i386/pr90773-24.c: New test. > >>> * gcc.target/i386/pr90773-25.c: Likewise. > >>> --- > >>> gcc/expr.c | 10 ++++++++++ > >>> gcc/testsuite/gcc.target/i386/pr90773-24.c | 22 ++++++++++++++++++++++ > >>> gcc/testsuite/gcc.target/i386/pr90773-25.c | 20 ++++++++++++++++++++ > >>> 3 files changed, 52 insertions(+) > >>> create mode 100644 gcc/testsuite/gcc.target/i386/pr90773-24.c > >>> create mode 100644 gcc/testsuite/gcc.target/i386/pr90773-25.c > >>> > >>> diff --git a/gcc/expr.c b/gcc/expr.c > >>> index d09ee42e262..80e01ea1cbe 100644 > >>> --- a/gcc/expr.c > >>> +++ b/gcc/expr.c > >>> @@ -10886,6 +10886,16 @@ expand_expr_real_1 (tree exp, rtx target, machine_mode tmode, > >>> unsigned HOST_WIDE_INT ix; > >>> tree field, value; > >>> > >>> + /* Check if it is more efficient to load the data from > >>> + the memory directly. FIXME: How many stores do we > >>> + need here if not moved by pieces? */ > >>> + unsigned HOST_WIDE_INT bytes > >>> + = tree_to_uhwi (TYPE_SIZE_UNIT (type)); > >> > >> that's prone to fail - it could be a VLA. > > > > What do you mean by fail? Is it ICE or missed optimization? > > Do you have a testcase? > > > > I think for a VLA the TYPE_SIZE_UNIT may be unknown (NULL), or something like "x". > > for instance something like > > int test (int x) > { > int vla[x]; > > vla[x-1] = 0; > return vla[x-1]; > } My patch changes the CONSTRUCTOR code path. I couldn't find a CONSTRUCTOR testcase with VLA. > > Bernd. > > >> > >>> + if ((bytes / UNITS_PER_WORD) > 2 > >>> + && MOVE_MAX_PIECES > UNITS_PER_WORD > >>> + && can_move_by_pieces (bytes, TYPE_ALIGN (type))) > >>> + goto normal_inner_ref; > >>> + > >> > >> It looks like you're concerned about aggregate copies but this also handles > >> non-aggregates (which on GIMPLE might already be optimized of course). > > > > Here I check if we copy more than 2 words and we can move more than > > a word in a single instruction. > > > >> Also you say "if it's cheaper" but I see no cost considerations. How do > >> we generally handle immed const vs. load from constant pool costs? > > > > This trades 2 (update to 8) stores with one load plus one store. Is there > > a way to check which one is faster? > > > >>> FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix, > >>> field, value) > >>> if (tree_int_cst_equal (field, index)) > >>> diff --git a/gcc/testsuite/gcc.target/i386/pr90773-24.c b/gcc/testsuite/gcc.target/i386/pr90773-24.c > >>> new file mode 100644 > >>> index 00000000000..4a4b62533dc > >>> --- /dev/null > >>> +++ b/gcc/testsuite/gcc.target/i386/pr90773-24.c > >>> @@ -0,0 +1,22 @@ > >>> +/* { dg-do compile } */ > >>> +/* { dg-options "-O2 -march=x86-64" } */ > >>> + > >>> +struct S > >>> +{ > >>> + long long s1 __attribute__ ((aligned (8))); > >>> + unsigned s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14; > >>> +}; > >>> + > >>> +const struct S array[] = { > >>> + { 0, 60, 640, 2112543726, 39682, 48, 16, 33, 10, 96, 2, 0, 0, 4 } > >>> +}; > >>> + > >>> +void > >>> +foo (struct S *x) > >>> +{ > >>> + x[0] = array[0]; > >>> +} > >>> +/* { dg-final { scan-assembler-times "movups\[\\t \]%xmm\[0-9\]+, \\(%\[\^,\]+\\)" 1 } } */ > >>> +/* { dg-final { scan-assembler-times "movups\[\\t \]%xmm\[0-9\]+, 16\\(%\[\^,\]+\\)" 1 } } */ > >>> +/* { dg-final { scan-assembler-times "movups\[\\t \]%xmm\[0-9\]+, 32\\(%\[\^,\]+\\)" 1 } } */ > >>> +/* { dg-final { scan-assembler-times "movups\[\\t \]%xmm\[0-9\]+, 48\\(%\[\^,\]+\\)" 1 } } */ > >>> diff --git a/gcc/testsuite/gcc.target/i386/pr90773-25.c b/gcc/testsuite/gcc.target/i386/pr90773-25.c > >>> new file mode 100644 > >>> index 00000000000..2520b670989 > >>> --- /dev/null > >>> +++ b/gcc/testsuite/gcc.target/i386/pr90773-25.c > >>> @@ -0,0 +1,20 @@ > >>> +/* { dg-do compile } */ > >>> +/* { dg-options "-O2 -march=skylake" } */ > >>> + > >>> +struct S > >>> +{ > >>> + long long s1 __attribute__ ((aligned (8))); > >>> + unsigned s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14; > >>> +}; > >>> + > >>> +const struct S array[] = { > >>> + { 0, 60, 640, 2112543726, 39682, 48, 16, 33, 10, 96, 2, 0, 0, 4 } > >>> +}; > >>> + > >>> +void > >>> +foo (struct S *x) > >>> +{ > >>> + x[0] = array[0]; > >>> +} > >>> +/* { dg-final { scan-assembler-times "vmovdqu\[\\t \]%ymm\[0-9\]+, \\(%\[\^,\]+\\)" 1 } } */ > >>> +/* { dg-final { scan-assembler-times "vmovdqu\[\\t \]%ymm\[0-9\]+, 32\\(%\[\^,\]+\\)" 1 } } */ > >>> -- > >>> 2.31.1 > >>> > > > > > > -- H.J.