Message ID | 20231030020407.754075-1-vineetg@rivosinc.com |
---|---|
State | Accepted |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp1941711vqb; Sun, 29 Oct 2023 19:04:42 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqMrMzr4W08ro/BpQ8z+qfLfR9kdXHab5OnA3EKXT/u6rjk3RQKtEV1yNTnYabuFu5EQfk X-Received: by 2002:a05:622a:1b8e:b0:41e:1754:daa3 with SMTP id bp14-20020a05622a1b8e00b0041e1754daa3mr9256408qtb.29.1698631481909; Sun, 29 Oct 2023 19:04:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698631481; cv=pass; d=google.com; s=arc-20160816; b=w/i+/n9YBVNquqpimQ0WtLtTX2dy7BK1UbEElknaJ+qt79/gOLdrieuX36qZNSRv5J xhUzVp+L/eJh01ds02HIGvo97cqQItU33RhEUHMw3sJyh8vygS2UQ3u7gOQtoikf0B4B EpSWDDbuq7v2dxcHh7G+XQoy01H0HeCqjfKqt70rhIKBCV63AoKIssORgwdjg38uSpq7 Tk3Ku7ya3YH6eHq37PaP1flXRKMv34NqRQtxh58Z9hKcvWCu+lwqf1WlEXEhs7mqsV6B ldYDUBUn1TpFyXrKi/D8EGAG564Gs9OakRtaAvlLCQUg6SAc45oZZTv23a+lFNLk6HgU giVQ== 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:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=9SN4cvOo/FwAla/2CMUgRDr8gO1bDQ/erU+zew2lL2I=; fh=UBXKnmcw6mbBdZ+QEZmGZWgjpE4fFWhTObBxu7V8G/Q=; b=Lp7BTYWwIh59CkqK0gksxs9Nh3A/YyKSQLADq0n/UojDkCQ7gPh9jJTRzT9mvFyake AxamvG7ZpAeI16UkE/N+UzRINLxtnVXEOPSf3r0A1QYRLd1SuktZIO5NMdAGGzohfnT+ CpM1cK1RMjNbklM6t/sRAY1V1UnELbotPqhWpzE4bCYdBb7RFN3SbP6GQENlisokACTa a07JwX1Tfl9dbkCa9HLAlcWvOmKYjgx41ZzQwlGFIvajlxOHYKsvmF66gXDtFoqp7Ciu f9stibR+NX+c3hyRCK+wXR5fVnMFM5XJNPfN1WuP7tqj6pkQLvd8RF58gwyysfTqiw0E HeQQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=diliDWC8; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d15-20020a05622a15cf00b00412058a36c2si4806671qty.324.2023.10.29.19.04.41 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 19:04:41 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=diliDWC8; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 216D13861931 for <ouuuleilei@gmail.com>; Mon, 30 Oct 2023 02:04:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ot1-x335.google.com (mail-ot1-x335.google.com [IPv6:2607:f8b0:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id 2801F385697B for <gcc-patches@gcc.gnu.org>; Mon, 30 Oct 2023 02:04:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2801F385697B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 2801F385697B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::335 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698631452; cv=none; b=lpDo7eTbRnXzjZUYk8Zflr5ZnYPogKQywlT9KHBEX3u59TlE3tzrcY5Y48TqJWEGqWj0vinXQOfgeD5+GrITa47P7wtu4IG+4hHaW5d1HOryQOonTOhXdNyBnRrNYG/DKlb0UTIixrrkEWGfzJF/1l4vGASelPwol5nHAq8Ot2w= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698631452; c=relaxed/simple; bh=SwSYmaDOezEtuQtIS8+IHSEe/bJowLuLItLIBU/dHGk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=gx4NxdqBOaNhVSojGNcuQpZ3wOpiuSxujKsPxJ/+45mqRm4WS6c0uvzP9l/xjzQYaTfgG1i340wdRtwV6HfNgffXAhZXKxSFJahn7gorlP9r1t07dDSQ/jgV9GkkfjZfKbqCUmxcq9ptVvszKXwr41phW22Jg6SGST2yVRv30VQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-6ce2add34c9so2683883a34.1 for <gcc-patches@gcc.gnu.org>; Sun, 29 Oct 2023 19:04:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1698631450; x=1699236250; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=9SN4cvOo/FwAla/2CMUgRDr8gO1bDQ/erU+zew2lL2I=; b=diliDWC8AFsySZhPFYrpW6T5KqdSXBqP/hqB/OmyuaXbp1s1mP4i+YbST6KQRkCa7x /jpwoqqHomwefJCQ0DnTcW/8HVPmTeZnNYEhKIp0V5N62WXyJ6rt82KRKZREGkOn0tcc ApVwuj/VfYf0osk3qL1dypvO2EANOxnBGNiJR6N05uN44OHzYxCyVlz+afGvMbUHEGVI wIWKlBsRkorcLBVEAQvqolOcS55fwdLm0PIHjwDL5pgHNCpocPcVVP0ebAdurApZui+2 2UaFu6rgMJDibDBoumGgcpXmjFHmo3sj+hRmyyModxcMQnePqBsl6QiorEVVBAzHcrts k15g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698631450; x=1699236250; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9SN4cvOo/FwAla/2CMUgRDr8gO1bDQ/erU+zew2lL2I=; b=wsp2en0fBwDcZJaX7qaL/20JXaBSZfoOASOgApjkk4htXbanc4Y355JsMhNrEDgN21 WyAAoAHkDgNnrztGFlD3SznJwZk1bcUbe84hgxkzHJK16bJq9H/qJJQzWPyJV7phXc3N smp+7wKVn0GHXihwRzcnoHMy8QIquyzpdSm8iO0dTl0LpmDAKwCuCiGQqN7rpkBWLu52 gBNJJzmtoH/fBZzW1etD3raTaktM5gT2IHxzmjpkw+JS/eyW/0LTOjdHsYQtLOwylhS6 u2d8o5rEVPM2PKEWcrNpmIrxjAnDhQcWrTSM1aQ5VtyNABwISly97kE1sUsBhzpDJ7ml dP0g== X-Gm-Message-State: AOJu0YybBfqp2YFp2oYuJDeaUBYpRWOefvs5JtRmuz1lQptVDOGfBIhb msm4hZFYF3ESpIuTCSwMsmKfIVu3pYbfWhotbnt8vg== X-Received: by 2002:a05:6830:4486:b0:6c4:b326:8fdb with SMTP id r6-20020a056830448600b006c4b3268fdbmr11414994otv.8.1698631450209; Sun, 29 Oct 2023 19:04:10 -0700 (PDT) Received: from vineet-framework.ba.rivosinc.com (c-98-210-197-24.hsd1.ca.comcast.net. [98.210.197.24]) by smtp.gmail.com with ESMTPSA id w12-20020a4aa98c000000b0057b6d8e51ddsm1526593oom.40.2023.10.29.19.04.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Oct 2023 19:04:09 -0700 (PDT) From: Vineet Gupta <vineetg@rivosinc.com> To: gcc-patches@gcc.gnu.org Cc: gnu-toolchain@rivosinc.com, Jeff Law <jeffreyalaw@gmail.com>, Robin Dapp <rdapp.gcc@gmail.com>, Vineet Gupta <vineetg@rivosinc.com> Subject: [PATCH v2] RISC-V: elide unnecessary sign extend when expanding cmp_and_jump Date: Sun, 29 Oct 2023 19:04:07 -0700 Message-Id: <20231030020407.754075-1-vineetg@rivosinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1781144204790820680 X-GMAIL-MSGID: 1781144204790820680 |
Series |
[v2] RISC-V: elide unnecessary sign extend when expanding cmp_and_jump
|
|
Checks
Context | Check | Description |
---|---|---|
snail/gcc-patch-check | success | Github commit url |
Commit Message
Vineet Gupta
Oct. 30, 2023, 2:04 a.m. UTC
RV64 compare and branch instructions only support 64-bit operands.
At Expand time, the backend conservatively zero/sign extends
its operands even if not needed, such as incoming 32-bit function args
which ABI/ISA guarantee to be sign-extended already.
And subsequently REE fails to eliminate them as
"missing defintion(s)" or "multiple definition(s)
since function args don't have explicit definition.
So during expand riscv_extend_comparands (), if an operand is a
subreg-promoted SI with inner DI, which is representative of a function
arg, just peel away the subreg to expose the DI, eliding the sign
extension. As Jeff noted this routine is also used in if-conversion so
also helps there.
Note there's currently patches floating around to improve REE and also a
new pass to eliminate unneccesary extensions, but it is still beneficial
to not generate those extra extensions in first place. It is obviously
less work for post-reload passes such as REE, but even for earlier
passes, such as combine, having to deal with one less thing and ensuing
fewer combinations is a win too.
Way too many existing tests used to observe this issue.
e.g. gcc.c-torture/compile/20190827-1.c -O2 -march=rv64gc
It elimiates the SEXT.W
Tested with rv64gc with no regressions, I'm relying on PAtrick's
pre-commit CI to do the full testing.
gcc/ChangeLog:
* config/riscv/riscv.cc (riscv_sign_extend_if_not_subreg_prom): New.
* (riscv_extend_comparands): Call New function on operands.
Signed-off-by: Vineet Gupta <vineetg@rivosinc.com>
---
Changes since v1:
- Elide sign extension for 32-bit operarnds only
- Apply elison for both arguments
---
gcc/config/riscv/riscv.cc | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
Comments
On 10/29/23 19:04, Vineet Gupta wrote: > RV64 compare and branch instructions only support 64-bit operands. > At Expand time, the backend conservatively zero/sign extends > its operands even if not needed, such as incoming 32-bit function args > which ABI/ISA guarantee to be sign-extended already. > > And subsequently REE fails to eliminate them as > "missing defintion(s)" or "multiple definition(s) > since function args don't have explicit definition. > > So during expand riscv_extend_comparands (), if an operand is a > subreg-promoted SI with inner DI, which is representative of a function > arg, just peel away the subreg to expose the DI, eliding the sign > extension. As Jeff noted this routine is also used in if-conversion so > also helps there. > > Note there's currently patches floating around to improve REE and also a > new pass to eliminate unneccesary extensions, but it is still beneficial > to not generate those extra extensions in first place. It is obviously > less work for post-reload passes such as REE, but even for earlier > passes, such as combine, having to deal with one less thing and ensuing > fewer combinations is a win too. > > Way too many existing tests used to observe this issue. > e.g. gcc.c-torture/compile/20190827-1.c -O2 -march=rv64gc > It elimiates the SEXT.W > > Tested with rv64gc with no regressions, I'm relying on PAtrick's > pre-commit CI to do the full testing. > > gcc/ChangeLog: > * config/riscv/riscv.cc (riscv_sign_extend_if_not_subreg_prom): New. > * (riscv_extend_comparands): Call New function on operands. > > Signed-off-by: Vineet Gupta <vineetg@rivosinc.com> > --- > Changes since v1: > - Elide sign extension for 32-bit operarnds only > - Apply elison for both arguments > --- > gcc/config/riscv/riscv.cc | 23 +++++++++++++++++++++-- > 1 file changed, 21 insertions(+), 2 deletions(-) > > diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc > index f2dcb0db6fbd..3af834f92977 100644 > --- a/gcc/config/riscv/riscv.cc > +++ b/gcc/config/riscv/riscv.cc > @@ -3678,6 +3678,24 @@ riscv_zero_if_equal (rtx cmp0, rtx cmp1) > cmp0, cmp1, 0, 0, OPTAB_DIRECT); > } > > +/* Helper function for riscv_extend_comparands to Sign-extend the OP. > + However if the OP is SI subreg promoted with an inner DI, such as > + (subreg/s/v:SI (reg/v:DI 150 [ xx ]) 0) > + just peel off the SUBREG to get DI, avoiding extraneous extension. */ > + > +static void > +riscv_sign_extend_if_not_subreg_prom (rtx *op) > +{ > + if (GET_MODE(*op) == SImode Weird, this is flagged in pre-commit CI, but contrib scripts think this is ok contrib/gcc-changelog/git_check_commit.py Checking 3d9823e2fb1c1f99bb875bffd999ab8dafd53a50: OK > + && GET_CODE (*op) == SUBREG > + && SUBREG_PROMOTED_VAR_P (*op) > + && GET_MODE_SIZE (GET_MODE (XEXP (*op, 0))).to_constant () > + == GET_MODE_SIZE (word_mode)) > + *op = XEXP (*op, 0); > + else > + *op = gen_rtx_SIGN_EXTEND (word_mode, *op); > +} > + > /* Sign- or zero-extend OP0 and OP1 for integer comparisons. */ > > static void > @@ -3707,9 +3725,10 @@ riscv_extend_comparands (rtx_code code, rtx *op0, rtx *op1) > } > else > { > - *op0 = gen_rtx_SIGN_EXTEND (word_mode, *op0); > + riscv_sign_extend_if_not_subreg_prom(op0); > + > if (*op1 != const0_rtx) > - *op1 = gen_rtx_SIGN_EXTEND (word_mode, *op1); > + riscv_sign_extend_if_not_subreg_prom(op1); > } > } > }
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index f2dcb0db6fbd..3af834f92977 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -3678,6 +3678,24 @@ riscv_zero_if_equal (rtx cmp0, rtx cmp1) cmp0, cmp1, 0, 0, OPTAB_DIRECT); } +/* Helper function for riscv_extend_comparands to Sign-extend the OP. + However if the OP is SI subreg promoted with an inner DI, such as + (subreg/s/v:SI (reg/v:DI 150 [ xx ]) 0) + just peel off the SUBREG to get DI, avoiding extraneous extension. */ + +static void +riscv_sign_extend_if_not_subreg_prom (rtx *op) +{ + if (GET_MODE(*op) == SImode + && GET_CODE (*op) == SUBREG + && SUBREG_PROMOTED_VAR_P (*op) + && GET_MODE_SIZE (GET_MODE (XEXP (*op, 0))).to_constant () + == GET_MODE_SIZE (word_mode)) + *op = XEXP (*op, 0); + else + *op = gen_rtx_SIGN_EXTEND (word_mode, *op); +} + /* Sign- or zero-extend OP0 and OP1 for integer comparisons. */ static void @@ -3707,9 +3725,10 @@ riscv_extend_comparands (rtx_code code, rtx *op0, rtx *op1) } else { - *op0 = gen_rtx_SIGN_EXTEND (word_mode, *op0); + riscv_sign_extend_if_not_subreg_prom(op0); + if (*op1 != const0_rtx) - *op1 = gen_rtx_SIGN_EXTEND (word_mode, *op1); + riscv_sign_extend_if_not_subreg_prom(op1); } } }