From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf1-x432.google.com (mail-pf1-x432.google.com [IPv6:2607:f8b0:4864:20::432]) by sourceware.org (Postfix) with ESMTPS id 9EB4C3857C51 for ; Mon, 28 Nov 2022 19:15:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9EB4C3857C51 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pf1-x432.google.com with SMTP id q12so7371357pfn.10 for ; Mon, 28 Nov 2022 11:15:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:cc:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=uPTVzCF76B7VFuHYILbZ3i/w2WunofzkbLpaA89PLAc=; b=OyIDbX4caXT6zmrzpfFcx4phtHE7fggJR1aFy533DWdAitC5kVNlwWKZieI0p5cIN9 wohEpguqqRdhDtdrZpFM6JdG4lT+yqoY6Chd/ypK1YiGQjfVxn2lRdtAV2wa11MoajVz Wl2rjMGnOXRvlfeMHgLuEQ5o6QJPugvEmRNJ+G8+rL8s21rMD8/WXXWHavL1IXUKZ+BS XrWgI7xn83i758JC2m9PKCTxrQ4NG4kNcekHIqzroKM8UdYF1DYgpAoT+nw70RjN6hvr QjexeVfN1gWHJwmd1357xZyTszwFD/Z8V72a7FM1V2od3AMckUX+QhDRgDxeGK5owm4Z oT5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:cc:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uPTVzCF76B7VFuHYILbZ3i/w2WunofzkbLpaA89PLAc=; b=g8BzoG3d7dX9v+ePwCNrBCFjzZY9Zz8zBd9OIo6VFuaLCLdCrsMSUHPC0TO0gsdlz1 r17MgeCEQFk7z4H+ddrS0+/upffm+0RHkgG+luIFA7AmlbCU0RInUv75j5qG729Du2WT jPlDeEAaW5pgRoPhFgKm0spcctKtKvRQeHsk+gCo1RcXEY9tvvpCc6RYKKqLchYCHflW Joa4mEpNbpGoFxChUMzmMS+QZOHiWBvGrnbwBi4yHB7cAmtMRD5fTqcsgee2hkfydACm egQndF+QgfmpcPYVReuGfPgLDGTxy1PG1/FquqD0plrOAzRxDx2ZKgGlXHnGR/qPv3+6 ItHg== X-Gm-Message-State: ANoB5pn2da9+kwA46k8uHwoYwvDgMlBpE3tEqRrcizZlngzKuGyel1Ez XN/z4CpPsgh5NRDtTbC/QpEkvbaYFfM= X-Google-Smtp-Source: AA0mqf5xm56C2+rK1kdE6MIzOY7wYzEcO/GmGGuxNvxxwAeeplAeWrH6srFFbbqoobv6o3Q3VSj3BQ== X-Received: by 2002:a63:2160:0:b0:46f:f26e:e8ba with SMTP id s32-20020a632160000000b0046ff26ee8bamr31169046pgm.250.1669662903328; Mon, 28 Nov 2022 11:15:03 -0800 (PST) Received: from ?IPV6:2601:681:8600:13d0::f0a? ([2601:681:8600:13d0::f0a]) by smtp.gmail.com with ESMTPSA id k7-20020a170902ce0700b001897d30143asm4357866plg.289.2022.11.28.11.15.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 28 Nov 2022 11:15:02 -0800 (PST) Message-ID: Date: Mon, 28 Nov 2022 12:15:01 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0 Subject: =?UTF-8?B?UmU6IOWbnuWkje+8mltQSU5HXSBbUEFUQ0ggUkVTRU5EXSByaXNjdjog?= =?UTF-8?Q?improve_the_cost_model_for_loading_a_64bit_constant_in_rv32=2e?= Content-Language: en-US To: Sinan References: <6787a2d509d2b8ef27083d3b9806661eb8f56102.1668090837.git.sinan.lin@linux.alibaba.com> From: Jeff Law Cc: "gcc-patches@gcc.gnu.org" In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,BODY_8BITS,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,KAM_SHORT,NICE_REPLY_A,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 11/24/22 00:43, Sinan wrote: >> The motivation of this patch is to correct the wrong estimation of >>> the number of instructions needed for loading a 64bit constant in >>> rv32 in the current cost model(riscv_interger_cost). According to >>> the current implementation, if a constant requires more than 3 >>> instructions(riscv_const_insn and riscv_legitimate_constant_p), >>> then the constant will be put into constant pool when expanding >>> gimple to rtl(legitimate_constant_p hook and emit_move_insn). >>> So the inaccurate cost model leads to the suboptimal codegen >>> in rv32 and the wrong estimation part could be corrected through >>> this fix. >>> >>> e.g. the current codegen for loading 0x839290001 in rv32 >>> >>>    lui     a5,%hi(.LC0) >>>    lw      a0,%lo(.LC0)(a5) >>>    lw      a1,%lo(.LC0+4)(a5) >>> .LC0: >>>    .word   958988289 >>>    .word   8 >>> >>> output after this patch >>> >>>    li a0,958988288 >>>    addi a0,a0,1 >>>    li a1,8 >>> >>> gcc/ChangeLog: >>> >>>          * config/riscv/riscv.cc (riscv_build_integer): Handle the case of loading 64bit constant in rv32. >>> >>> gcc/testsuite/ChangeLog: >>> >>>          * gcc.target/riscv/rv32-load-64bit-constant.c: New test. >>> >>> Signed-off-by: Lin Sinan  >>> --- >>>   gcc/config/riscv/riscv.cc                     | 23 +++++++++++ >>>   .../riscv/rv32-load-64bit-constant.c          | 38 +++++++++++++++++++ >>>   2 files changed, 61 insertions(+) >>>   create mode 100644 gcc/testsuite/gcc.target/riscv/rv32-load-64bit-constant.c >>> >>> diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc >>> index 32f9ef9ade9..9dffabdc5e3 100644 >>> --- a/gcc/config/riscv/riscv.cc >>> +++ b/gcc/config/riscv/riscv.cc >>> @@ -618,6 +618,29 @@ riscv_build_integer (struct riscv_integer_op *codes, HOST_WIDE_INT value, >>>    } >>>       } >>> >>> +  if ((value > INT32_MAX || value < INT32_MIN) && !TARGET_64BIT) >> >> Nit.   It's common practice to have the TARGET test first in a series of >> tests.  It may also be advisable to break this into two lines. >> Something like this: >> >> >>   if ((!TARGET_64BIT) >>       || value > INT32_MAX || value < INT32_MIN) >> >> >> That's the style most GCC folks are more accustomed to reading. > > Thanks for the tips and I will change it then. > >>> +    { >>> +      unsigned HOST_WIDE_INT loval = sext_hwi (value, 32); >>> +      unsigned HOST_WIDE_INT hival = sext_hwi ((value - loval) >> 32, 32); >>> +      struct riscv_integer_op alt_codes[RISCV_MAX_INTEGER_OPS], >>> +       hicode[RISCV_MAX_INTEGER_OPS]; >>> +      int hi_cost, lo_cost; >>> + >>> +      hi_cost = riscv_build_integer_1 (hicode, hival, mode); >>> +      if (hi_cost < cost) >>> + { >>> +   lo_cost = riscv_build_integer_1 (alt_codes, loval, mode); >>> +   if (lo_cost + hi_cost < cost) >> >> Just so I'm sure.  "cost" here refers strictly to other synthesized >> forms? If so, then ISTM that we'd want to generate the new style when >> lo_cost + hi_cost < cost OR when lo_cost + hi_cost is less than loading >> the constant from memory -- which is almost certainly more than "3" >> since the sequence from memory will be at least 3 instructions, two of >> which will hit memory. >> >> >> Jeff >> > > Yes, almost right. The basic idea of this patch is to improve the cost > calculation for loading 64bit constant in rv32, instead of adding a new > way to load constant. > > gcc now loads 0x739290001LL in rv32gc with three instructions, >         li      a0,958988288 >         addi    a0,a0,1 >         li      a1,7 > However, when it loads 0x839290001LL, the output assembly becomes >         lui     a5,%hi(.LC0) >         lw      a0,%lo(.LC0)(a5) >         lw      a1,%lo(.LC0+4)(a5) >     .LC0: >         .word   958988289 >         .word   8 > The cost calculation is inaccurate in such cases, since loading these > two constants should have no difference in rv32 (just change `li a1,7` > to `li a1,8` to load the hi part). This patch will take these cases > into consideration. > I think I see better what's going on. This really isn't about the constant pool costing. It's about another way to break down the constant into components. riscv_build_integer_1, for the cases we're looking at breaks down the constant so that high + low will give the final result. It costs the high and low parts separately, then sums their cost + 1 for the addition step. Your patch adds another method that is specific to rv32 and takes advantage of register pairs. You break the constant down into 32bit high and low chunks, where each chunk will go into a different 32 bit register. You just then need to sum the cost of loading each chunk. For the constants in question, your new method will result in a smaller cost than the current method. That's really the point of riscv_build_integer -- find the sequence and cost of creation. We later use that information to determine if we should use that sequence or a constant pool. Palmer raised an issue on the tests with a request to not include the arch/abi specification. But I think you addressed that in a later comment. Specifically for rv64 we end up with another instruction, which would cause some constants to be considered cheaper as constant pool entries rather than inline sequences. Palmer is right in this seems like it ought to be generic, particularly breaking things down on word boundaries. But I don't think adding that infrastructure should hold this patch up. Reality is not much is happening with 32bit (or smaller) architectures and little is happening with 128bit integer types. So there's not much motivation to fix this stuff more generically right now. Jeff