From patchwork Wed Nov 15 17:37:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vineet Gupta X-Patchwork-Id: 165525 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b909:0:b0:403:3b70:6f57 with SMTP id t9csp2699861vqg; Wed, 15 Nov 2023 09:37:39 -0800 (PST) X-Google-Smtp-Source: AGHT+IF8ecdmovguJQYnvHujYoU+BQTU5LbgDpCO+Yb9JMECBlVkGnTWaoHPo5aLOVWfMuTOg5u8 X-Received: by 2002:a05:6870:bac9:b0:1ea:f1a5:51f6 with SMTP id js9-20020a056870bac900b001eaf1a551f6mr14572012oab.0.1700069859074; Wed, 15 Nov 2023 09:37:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700069859; cv=pass; d=google.com; s=arc-20160816; b=AjpSoA0Cd9+gukfbj4aFJRPHdVXzGwgo/VM1XVuNn/SjaKw1+TJ0wWxzSt5Rf1bTDr zbTGpoyqBgkev/cqJvl/ZUfkNXqsm/L11C0D04joJndk1o5yUrgH0727qzE1wb07MqoL ddNeCifnvxRDVqtgKHIADYcB3oFrr4vLQBi/T3EnTHyKbeimktyfHMWPojHKN57b4C1x L9zFZOjMktvoGUijXb7bw/Pd7XIhgOTrlZzQDvbxvNRlmLpJneIk4Qk1xCnzmA2BHVhm tBfqzvYfeOAp+Rm6B1HbaIzu6Ov+XFYsraT1PpT5Bw7nWYVOr7YwEuKEtEk3VX9LbW7/ Fr2w== 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=z/FtLqD1lTWh3pGKU6Weqofyca6jyjVEc8/5aRSpXgk=; fh=TPHHJocGCFX/tLNOKR+63EdS5sD0G3BmrcPZBQRysJs=; b=ayCp4tcvOo5jmcOkX6EH9gUjePoKFhkPSxiRDckd5th/tOS/vU9Tru+bglHGWhG9S9 agnePBBFC95k9pqQUtuqOR9xP7tvqpcDWzXnT5hD3oIDvoA+HgSvLwCRqceac2zK/VGs xip1718l6UNkgH4eN/6oMjozkQt9QkubrSI3+cIg9qhkdRCMtZlNaUAin453AXWTvDaL 9Meg+51w5Ldo888iNLrYKOqOn0/OAs/ZsTGKDw8gKONyh0sVh6mBT9fSBfisbyYXbjVl ez3/mU2LqAWErDZxM5MUvLiM/Tk85wy5q8nmHhqzNejcNvW3Ao6QIBaRbQ9BTXB+25uc lcpg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hlkS4vDQ; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y11-20020a05622a004b00b0041cba84cc02si9170563qtw.436.2023.11.15.09.37.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 09:37:39 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=hlkS4vDQ; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 AD48C385829E for ; Wed, 15 Nov 2023 17:37:38 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id D0B483858421 for ; Wed, 15 Nov 2023 17:37:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D0B483858421 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 D0B483858421 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700069836; cv=none; b=oFeUdlv3WdGWxufKT7+3x21UZGEyEAFPLmz7TfvP6atfybD+DAljlJsrc5dt1UhdHRg03PeihGTTsv7axlbKwTG+A6X7GIpU/7dw70oBOmujfgUsYFIlGOvLjJ5f9KdKlXKD5v+qWtt7LAzCnId1ZPr1nmfytI0i5ZUVWWiem24= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700069836; c=relaxed/simple; bh=VvULXvRSMpi3yMYsw7rnTmjoVlCnCkJd495XSGixhls=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=STvYPxi88mdtkRgYbv/mpAysNAkhbTcpwm99gPyKFofXNTXXJra1xS/UFDrzY2ZoKjXuqrDZldtgSHG+K9HD4WJVIFrNMTq+s76T3/Y0iiBzRt8JX4S/421E5Rhq5ZQPVxJ10NmphCIBHhouzCsAXI7TQBinswwjUvi/DUVdmds= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-6bd32d1a040so7311579b3a.3 for ; Wed, 15 Nov 2023 09:37:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1700069832; x=1700674632; 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=z/FtLqD1lTWh3pGKU6Weqofyca6jyjVEc8/5aRSpXgk=; b=hlkS4vDQ72KHpmCiFYbzInT0Ih3OHPThVTyxu7bmXzL6IjTrH/mj4rmXWOK1zoQR4u qlsRsTcYRvLzOvFB2FgasRvMR5MQB2JNq9wDd/CGeKhhrMa2MeziwVeaVx25BSKir6/J poeavRnFq2Jcy/DzqZ8/vRELZx2YePqIV1EgvCCLJG2ygJOQDZggwmVLML3xg9aNV6aD B7tq+1aA/P/BaCkt4leQUc/nUxGR6U21t9Ca/L9xPTjZMkk1liMI5dnfMOMFQM6WacVJ WF9g/sFpxXMjtHOTBmhl+IjgJjrpZKKkYMgKoEYidn44/nX08rgmtKPI37urplkLdePA IN9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700069832; x=1700674632; 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=z/FtLqD1lTWh3pGKU6Weqofyca6jyjVEc8/5aRSpXgk=; b=V+7TWHmdiCeUha2rUqaNQYr6cI5zwHcoD+Ecr2tE7mdxttyp3R1u9WVJ/wbW6lmHL1 Ffvb6aj+xOu7U1hwdPW6gAPp8g0to+uRwQOY5TOnXHJN1ZPjMlX9bX6FhvrmDKKtY0sJ N/GY6Cy3CDxYeDsIXqWfOD8KsZzfoL6/zV3dtFTOpfBZio7ZNmqDTJOCUoaJESQQukt8 RLPuyixK3laY13lANaqOfejWWXg6FqMRcL1KiAfKdsI8tfflZBKcBb6pnn3sF6A4K3GG P4/F4ATMnwn+ELpKiVk/EEZYyUBH/9myi/HAPu628vgCK2rbJiPeMkIMmdSwrfQ2QaE7 EcPA== X-Gm-Message-State: AOJu0Yyv5Fo60bTSGEBdRuHRUJyJtTKrXFKfgIPRQQWa6QlpYHylNwKU +icT77/7+LAiXyRwH3M8b17yhNeulzJWfKxOMP6o2g== X-Received: by 2002:a05:6a20:e113:b0:187:7761:6155 with SMTP id kr19-20020a056a20e11300b0018777616155mr2335064pzb.55.1700069832468; Wed, 15 Nov 2023 09:37:12 -0800 (PST) Received: from vineet-framework.. (c-73-170-212-163.hsd1.ca.comcast.net. [73.170.212.163]) by smtp.gmail.com with ESMTPSA id g2-20020a63fa42000000b005af08f65227sm1438405pgk.80.2023.11.15.09.37.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Nov 2023 09:37:12 -0800 (PST) From: Vineet Gupta To: gcc-patches@gcc.gnu.org Cc: Jeff Law , Robin Dapp , gnu-toolchain@rivosinc.com, Vineet Gupta , Patrick O'Neill Subject: [Committed] RISC-V: elide unnecessary sign extend when expanding cmp_and_jump Date: Wed, 15 Nov 2023 09:37:06 -0800 Message-Id: <20231115173706.2159712-1-vineetg@rivosinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782652452728067715 X-GMAIL-MSGID: 1782652452728067715 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 function args which ABI/ISA guarantee to be sign-extended already (this is true for SI, HI, QI operands) 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 potentially can also help 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 gcc/ChangeLog: * config/riscv/riscv.cc (riscv_sign_extend_if_not_subreg_prom): New. * (riscv_extend_comparands): Call New function on operands. Tested-by: Patrick O'Neill # pre-commit-CI #676 Signed-off-by: Vineet Gupta --- 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 e919850fc6cb..e466d4f168af 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -3695,6 +3695,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) 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_CODE (*op) == SUBREG + && SUBREG_PROMOTED_VAR_P (*op) + && SUBREG_PROMOTED_SIGNED_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 @@ -3724,9 +3742,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); } } }