From patchwork Wed Jun 3 22:20:12 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeffrey Law X-Patchwork-Id: 136422 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A1C864BA2E2C for ; Wed, 3 Jun 2026 22:21:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A1C864BA2E2C Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=EtrECtML; dkim=pass (2048-bit key, unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=L7Rs6X5o X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by sourceware.org (Postfix) with ESMTPS id D56134BA2E08 for ; Wed, 3 Jun 2026 22:20:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D56134BA2E08 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=oss.qualcomm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oss.qualcomm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D56134BA2E08 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780525217; cv=none; b=Zqvie9izcTdUV7XzEnm+B/DHphZ5ZWE7y92YUu835sP0A2P5gPaR5//ZG3xrmf47N5Ll6U1GJYiUQX9rFaq+fxP22XDh4R/OqpqeEsXmydjQ1jt8f6ry9xZmytM/P4FPaT+DoU5ih/PLt7BnaSaoDn7HQ5vKA7RCsvi9kNIF/6A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1780525217; c=relaxed/simple; bh=zUSVaS71de/HNpt5HWTLkxxYdjRUxRYKQYL1ZJAI7Mc=; h=DKIM-Signature:DKIM-Signature:Message-ID:Date:MIME-Version:From: Subject:To; b=CwdK2v1SpUFqZWg8rGS5ldYMHIQxE74mNEaOmx4dntERQS4182TeeZ5YAuMXmCJavmCRi0NJzAvW7t3cXRJW4m2ZPFgu5FIkyOD7Ixxi8PrQC3SaKkDZJ9JZ/CkGv93Z2R/FD2E/lu234OZDrc74ZDSIsdYAdNzpDmQm9+gav0U= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=EtrECtML; dkim=pass (2048-bit key, unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=L7Rs6X5o DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D56134BA2E08 Received: from pps.filterd (m0279862.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 653Gu3JF3552309 for ; Wed, 3 Jun 2026 22:20:16 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-type:date:from:message-id:mime-version:subject:to; s= qcppdkim1; bh=fQna5430oFcH4Bn1V5JBsPv5xsB55l/933aed6Xa4DY=; b=Et rECtMLOT7oiWVsbgvhGHFWAnRZXonHVhj23voMwLSQ38sEk/Dc79UlI6xSin/Ftv MEjEIFOt3g9T57fQD39UVFGkfdXsKizdR+DlDEwnfTmnPYVucvjSpdRF7DAZ4gKU RXPJCMNRrsN1TNZv8rxtD/R/Vx3YCIkA9WmFrgn5iLj1Ys+fEM/prtml8Bpw9oWA 5JuTJA8a1LdDijIjs1O0U/vXjIR3WHzSMguD/EjAMJhaKeO/0eH4ZzBCsxQ+ngEi Mgw09yn73GLaIcwany0x7rsW24TYzX6FVZQBsQRK3AGVMHWjPmpGWhwiv+3r38zB HqobIOqlvtrJJLH0rkhQ== Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4ejj9mtxup-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Wed, 03 Jun 2026 22:20:15 +0000 (GMT) Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-304f23c55b2so245eec.0 for ; Wed, 03 Jun 2026 15:20:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1780525215; x=1781130015; darn=gcc.gnu.org; h=to:cc:subject:from:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=fQna5430oFcH4Bn1V5JBsPv5xsB55l/933aed6Xa4DY=; b=L7Rs6X5oE1ecBzo9wLKIQooHx+axIXpj3Ikzh2/Zhx/kIQspMXMOYNIvQXunoCZy+T 39o60n5I9akl7Wy8Bx0N8+IyXuZMV8aa42iQrp0StK8Xa9Y1ZlyOfFV2utm0wHyEW2qj C/iLUXyvA1nDra1d4NkPEwDcaVM4jc+3z9UFuoGhdZPnG18e+JMcpieaAfvH1VSEjb2n SVEAEfw1TzZjUwSC0Ad6Ero9uJNoq55GKMl+vzU8VHqilaIqx196QReafWRm1Y/JlLCX Ptix/Sr+6KcnoPTosRqxL00BQSLoOK1HOsZ2fk6XDivH49kTQefZENTjCf97ZKqTEOfo iLAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780525215; x=1781130015; h=to:cc:subject:from:content-language:user-agent:mime-version:date :message-id:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=fQna5430oFcH4Bn1V5JBsPv5xsB55l/933aed6Xa4DY=; b=Ka3r8Y1XNggORzutwUiVM18WCks/zKHLPGgM4sVwY6pIdjKHx0kr47DcRy9dJXoDK3 3VPa/0CQpGR/SFsHzXDm09HmZXf0but82cRCq39kcVUzRsHG/aGtE+TDdNPkSmcESIMU 2fLb579priwc/TDOofZSCrG7X0f3Ff0Lcj+YciCjdqnK0o1zOVU1ChNRJFOjqTI8wTdg FY/4eWC2MP5I9Lt13sGXLO92hR8fikJ21q5PXMsrbhWf5x0brPqEXtc5NVHTve5A3dX5 m5iXpPIhjVq90fVF01D9R3iJRm3mUBgqVF7j+YKq4uaKYn4PcfJZqgCKZLzZj63dFy9Y lbUg== X-Gm-Message-State: AOJu0YwEh0ji5KwFkGKlDTQpGG5SgvdwXiEq+TmA1aInkpfLRGKpP3fu 3GWj340ZJ7w6Opi3uclMPt1QwCLW9lFy+uyHmwvjeLNym+F9rseR7PdT2X+OIPmmGluS2lIoArK Cc8Pio/2ISawU4KL21jxpj5dZDuCakexZdmdiY94w8AwX4S0QPz/XpwEDZJRdd3XqOHrk X-Gm-Gg: Acq92OFo4RWij5PVc6ADuioI+OpJZLGjGOqx/Dv+PY8/8lnwrNfNHcHRXx5vQIu+xLZ B2qG8DIUjZCm3+Ha/osNGq54uOzu5C4G6ySm4UPVF0CO9sL4PxxDSnoYlGksyEspG2A8bCxkuaT K1lft2HqZm2xVA5//JNKjqlTRwfMPkzHZnm8UxxgMpNLdia8Nzb2VjaZhSfPhrXzvqYVWMKsFZH SX54bMqcUk32P9/UxnORnCyZvr1Ftd1jPAzC262iOoQjXmlBkZLKHER1Eb94Brf+4uM+GtLRSJG HPzWP0Z/9auRgRNxZOJ9xSo6q/WbGhYjF2f/T1rvoXqKLwYJ1wwfNeu9vhB0ZiXLIGlO+BkxHdJ Vc70zrtuHafw3cb67n4pRN16DkHvYxyW6DX80IrQNRbfYYOZVEbAWnNh5gO/A X-Received: by 2002:a05:7300:b58a:b0:304:29dd:70b6 with SMTP id 5a478bee46e88-3074fa89b88mr2602116eec.13.1780525214765; Wed, 03 Jun 2026 15:20:14 -0700 (PDT) X-Received: by 2002:a05:7300:b58a:b0:304:29dd:70b6 with SMTP id 5a478bee46e88-3074fa89b88mr2602106eec.13.1780525214089; Wed, 03 Jun 2026 15:20:14 -0700 (PDT) Received: from [172.31.0.17] ([136.38.201.137]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3074df191d0sm5168720eec.21.2026.06.03.15.20.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Jun 2026 15:20:13 -0700 (PDT) Message-ID: <98e13ce3-62e8-4123-b104-add995579105@oss.qualcomm.com> Date: Wed, 3 Jun 2026 16:20:12 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US From: Jeffrey Law Subject: [to-be-committed][PR target/125387][RISC-V] Properly cost mulh for RISC-V Cc: Raphael Zinsly To: 'GCC Patches' X-Proofpoint-GUID: gWEk6c2iKESzt83V7p6CxkyiVFrF2oWf X-Authority-Analysis: v=2.4 cv=XK0AjwhE c=1 sm=1 tr=0 ts=6a20a89f cx=c_pps a=PfFC4Oe2JQzmKTvty2cRDw==:117 a=asGLMfRmzhnGNxaIYohjRg==:17 a=FelO9ux0wxsA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=_K5XuSEh1TEqbUxoQ0s3:22 a=r77TgQKjGQsHNAKrUKIA:9 a=iYGPv-BpwjjskREpa7sA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=_ESKrnsbRfghtwzB_EMA:9 a=B2y7HmGcmWMA:10 a=6Ab_bkdmUrQuMsNx7PHu:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNjAzMDIxNyBTYWx0ZWRfX9zsQglhz0T/V NK+Q5CQtmQgGwUSn5SfybyOCA74zKLRlfsU9EZabdDnss4sPcdn6ZHdBSxvFdOrFc145wXSMHIE CGDWb2Uv/IDQtfFjwk/GFjcNcMwepJXnfqr0aRhdhxnY9UcAg0mZQiiSthi9fZubtYO4KsEVqHC hkc9Hr6gAhAqbycDaoXJp5wR0dW8efvfTGsaJMxPrwanQQFfvbE9tNxxXQiNuqS9/2WYBxcGcT8 Z+T867VqPUuXdBn6La58WjRM07TwJvgmCZtosG9lqTZ2ULHruhYPB2MREwXcOD3ZjG9x5vaTaFH jG6EHaf6lRbygkH85LFTxyWhKsBqPfBdEu6UH0NQEffhiHMFsNbvpSWMSyaeuYg7GXCTqsJbL+g PvbL7bHgGIBJNsGm/EYeqo/O26dIRLNlTV1ap6QFM7Rvh4dPxdb14FY/je2GFWWQCLwyzRZ178Y Z52d3hbj8FNdrYf9fgQ== X-Proofpoint-ORIG-GUID: gWEk6c2iKESzt83V7p6CxkyiVFrF2oWf X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.125,FMLib:17.12.100.49 definitions=2026-06-03_06,2026-05-28_03,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 adultscore=0 priorityscore=1501 bulkscore=0 suspectscore=0 phishscore=0 malwarescore=0 spamscore=0 lowpriorityscore=0 impostorscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2605210000 definitions=main-2606030217 X-Spam-Status: No, score=-8.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, LIKELY_SPAM_BODY, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_PBL, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLOCKED shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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~patchwork=sourceware.org@gcc.gnu.org This is a patch Raphael wrote a few years back, but which never got upstreamed.  I can vaguely recall evaluating the patch and concluding to drop it both internally and from the queue of things to upstream.  But the details of how I reached that conclusion are lost. The core problem is we didn't recognize the fairly complex widening multiply RTL, so it got costed by recursion which is painful on its own.  To add insult to injury the input operands are TI mode which, IIRC, also increased the cost of the mulh insn. The net was a totally bogus cost, it was totally off the charts which in turn discouraged using multiplies for division-by-constant operations as can be seen in the BZ. This patch from Raphael fixes recognition of the mulh RTL and uses the integer multiply cost out of the costing structure.  This results in sensible costs for the multiply and using a multiply-by-reciprocal instead of division in more cases. It's been tested on riscv32-elf and riscv64-elf without regressions.  I haven't bootstrapped it as I had my system disabled yesterday for "reasons".  I'll obviously wait for the pre-commit verdict. Jeff PR target/125387 gcc/ * config/riscv/riscv.cc (riscv_rtx_costs): Recognize and properly cost mulh instructions. gcc/testsuite * gcc.target/riscv/pr125387.c: New test. diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index a2730a90ca8b..99e1e4b3d8d2 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -4677,8 +4677,26 @@ riscv_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno ATTRIBUTE_UN return true; } gcc_fallthrough (); - case ASHIFTRT: case LSHIFTRT: + /* mulh[u] pattern */ + if (GET_CODE (x) == LSHIFTRT + && TARGET_MUL + && mode == GET_MODE_2XWIDER_MODE (word_mode).require () + && outer_code == TRUNCATE + && (GET_CODE (XEXP (x, 0)) == MULT) + && (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND + || GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND) + && (GET_CODE (XEXP (XEXP (x, 0), 1)) == ZERO_EXTEND + || GET_CODE (XEXP (XEXP (x, 0), 1)) == SIGN_EXTEND) + && (GET_CODE (XEXP (x, 1)) == CONST_INT + && INTVAL (XEXP (x, 1)) == BITS_PER_WORD)) + { + *total = COSTS_N_INSNS (tune_param->int_mul[mode == TImode]); + return true; + } + /* Fall through. */ + + case ASHIFTRT: *total = riscv_binary_cost (x, SINGLE_SHIFT_COST, CONSTANT_P (XEXP (x, 1)) ? 4 : 9); return false; diff --git a/gcc/testsuite/gcc.target/riscv/pr125387.c b/gcc/testsuite/gcc.target/riscv/pr125387.c new file mode 100644 index 000000000000..c381b7a28e21 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/pr125387.c @@ -0,0 +1,9 @@ +/* { dg-do compile { target rv64} } */ +/* { dg-additional-options "-mtune=tt-ascalon-d8 -march=rv64gc" } +/* { dg-skip-if "" { *-*-* } { "-Os" "-Oz" } } */ + +long foo1 (long x) { return x / 10000L; } +long foo2 (long x) { return x / 30; } + +/* { dg-final { scan-assembler-times "mulh" 2 } } */ +/* { dg-final { scan-assembler-not "\tdiv\t" } } */