From patchwork Thu Nov 10 21:36:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 18332 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp387689wru; Thu, 10 Nov 2022 13:36:48 -0800 (PST) X-Google-Smtp-Source: AMsMyM5MBJHEoDbJ+VFpWs10Pijf/sUclMS3E4tkBNYhnK3Uf34oz/Rf4IB7BhY2lqcjTt/x6nUs X-Received: by 2002:a17:906:69d2:b0:7a4:c236:906 with SMTP id g18-20020a17090669d200b007a4c2360906mr3892713ejs.318.1668116208876; Thu, 10 Nov 2022 13:36:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668116208; cv=none; d=google.com; s=arc-20160816; b=aVJqaKORmckt35wrPKBATop+se69AJE9QXKV7AxqwE5eej2ggEeluZc2l3GKzIfqyt SoClsPv1nQXOwnZb23iuXrXn63igDs+nsm9PRd7qwY0w2HJynfmLLlgtlWOfxD+6KLmy l6tbFFwJxcmQbGwsE3c3Vb3GJ996E+ctsnZV0Z06Q3F85MxSzpYpQGdGVibQ8XoDxM6J dpTkmUfuBvzoPLG3xlmr8QyB+LOj3hvEL8kDJNgXxPAXM0xykbthD5CqC3seYTHPY69Z ltxYBsujuZ7bY0ehigzL6ah2sAit3NyUmwEnJqjIih5sfrlCRrbdekTVSydmWZVETC46 7RZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender: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 :dmarc-filter:delivered-to; bh=wsCU+eymHeDV2I0z4xwEPXgzfP0P7x0SX9yTYkBGAz8=; b=ne/x+fL99Bw5sMSYsuDlb6M5IE08RQgYf7JWJktf4J9l+ffPMQ8H8GPF2VVrWfaxir 1nfJJ8/QlAalpPBbR6EpkoKp0KPfOhV8hoJPL4NGaR1JT7U6TxqnbdCvdjV6sG5RVF5j p7Ji8E6U2kz50mdQRYx9vpHmy3dFUmTEAHgH2XRiD8VETSTeDzcrTwaN2jbVi7U+0lMo fyNvJUZ5w43wu1fiSQpiKQajGbuuV3URKwaKzV2RxcqvFb8P2BFgQ1nzVzxb+jQWhrI6 iVY7hq/C0nEB8duKs6Ox8p080807iWAwFeXj4T0jNwkr68ftwLK9Vm3iIoXvwx31yU3w Xuag== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@vrull.eu header.s=google header.b=nbiMDHp7; 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 sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id u1-20020a1709064ac100b0078db3762061si275835ejt.439.2022.11.10.13.36.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 13:36:48 -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=@vrull.eu header.s=google header.b=nbiMDHp7; 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 A4F35384F031 for ; Thu, 10 Nov 2022 21:36:45 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id 4FDE03858D28 for ; Thu, 10 Nov 2022 21:36:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4FDE03858D28 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=vrull.eu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=vrull.eu Received: by mail-lj1-x22f.google.com with SMTP id k19so2446292lji.2 for ; Thu, 10 Nov 2022 13:36:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull.eu; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=wsCU+eymHeDV2I0z4xwEPXgzfP0P7x0SX9yTYkBGAz8=; b=nbiMDHp7twq7c1NepcZzbli1mmGg0Jxz2AbQQKm7Ika/3vXsKJsLB4dwhrHf4evSyI EyhHzsJguMxj1tAKLzn7fm1+JjW1R57SClX0pORmeMRcPPHGNa6LgTky6gy7G36LZqsm kkBft6F6CkDdZDrtvdD7TmujzB2AXsz5t7U9E70kZHVV9ilNq3KV6/0D8QXgZ5h8s6Rk YjMOMfesz0d9ifRk1xUr2AvkPplPPMX0FVJrhfFhEkKTIVfDeA1lFokXeBLUMRmPuIRY w4ByNPqVK/o63WJkVBsZJVlDtdRArd9aPUQhMao+aCJu+Tr+zB2s9PZd3jnZEa1cK4Nw crBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=wsCU+eymHeDV2I0z4xwEPXgzfP0P7x0SX9yTYkBGAz8=; b=S+gF4M0Uw2MZmUs4IeOhdRsPaRBkdZVE4DyNH/QXK37knyy8lYrlUEOWvi8vY2ObCi LxRx7UuTaMw792gkHwGnIr0Z0yrN1IplJJxxEgORnbovRhSGOVBdhNUOeZm3i2ZmTuvr /oIuIJ827CjtfHcFNbluXeMmtLvbJZ6iEAngqRgumqQTUETAlx55ilpQNsAGbRGRUiS3 dQcxRrUBrfCP+gLoejRdyQSxpxb2z4Ow7mDp6W9VulRMNwF4yimO6G4zsUuCYoMiS2uf 28yvGYv1B4G7fB8rX43jB1KgC591DzxzASDQA5O/ogZnHHtvca082q+HIQ4wLdjzqEL3 eDdA== X-Gm-Message-State: ACrzQf1wLoq6IMnUC/0okxfn2VVyKyDKzzzYkU/LvsY80RDV7ksRhiqu 5FecpNr1R/10msckxvoj826NE6nultruXBT8 X-Received: by 2002:a2e:ab0c:0:b0:277:2b10:bf69 with SMTP id ce12-20020a2eab0c000000b002772b10bf69mr9994400ljb.392.1668116179531; Thu, 10 Nov 2022 13:36:19 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id 19-20020a2eb953000000b002770f7d8dcasm84801ljs.134.2022.11.10.13.36.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 13:36:19 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Cc: Jeff Law , Palmer Dabbelt , Christoph Muellner , Kito Cheng , Vineet Gupta , Philipp Tomsich Subject: [PATCH] RISC-V: Optimize masking with two clear bits not a SMALL_OPERAND Date: Thu, 10 Nov 2022 22:36:17 +0100 Message-Id: <20221110213617.3592572-1-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_SHORT, 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 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: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749146621877097898?= X-GMAIL-MSGID: =?utf-8?q?1749146621877097898?= Add a split for cases where we can use two bclri (or one bclri and an andi) to clear two bits. gcc/ChangeLog: * config/riscv/bitmanip.md (*bclri_nottwobits): New pattern. (*bclridisi_nottwobits): New pattern, handling the sign-bit. * config/riscv/predicates.md (const_nottwobits_operand): New predicate. gcc/testsuite/ChangeLog: * gcc.target/riscv/zbs-bclri.c: New test. Signed-off-by: Philipp Tomsich --- gcc/config/riscv/bitmanip.md | 38 ++++++++++++++++++++++ gcc/config/riscv/predicates.md | 5 +++ gcc/testsuite/gcc.target/riscv/zbs-bclri.c | 12 +++++++ 3 files changed, 55 insertions(+) create mode 100644 gcc/testsuite/gcc.target/riscv/zbs-bclri.c diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index 7fa8461bb71..f1d8f24c2d3 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -560,6 +560,44 @@ "bclri\t%0,%1,%T2" [(set_attr "type" "bitmanip")]) +;; In case we have "val & ~IMM" where ~IMM has 2 bits set. +(define_insn_and_split "*bclri_nottwobits" + [(set (match_operand:X 0 "register_operand" "=r") + (and:X (match_operand:X 1 "register_operand" "r") + (match_operand:X 2 "const_nottwobits_operand" "i")))] + "TARGET_ZBS && !paradoxical_subreg_p (operands[1])" + "#" + "&& reload_completed" + [(set (match_dup 0) (and:X (match_dup 1) (match_dup 3))) + (set (match_dup 0) (and:X (match_dup 0) (match_dup 4)))] +{ + unsigned HOST_WIDE_INT bits = ~UINTVAL (operands[2]); + unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (bits); + + operands[3] = GEN_INT (~bits | topbit); + operands[4] = GEN_INT (~topbit); +}) + +;; In case of a paradoxical subreg, the sign bit and the high bits are +;; not allowed to be changed +(define_insn_and_split "*bclridisi_nottwobits" + [(set (match_operand:DI 0 "register_operand" "=r") + (and:DI (match_operand:DI 1 "register_operand" "r") + (match_operand:DI 2 "const_nottwobits_operand" "i")))] + "TARGET_64BIT && TARGET_ZBS + && clz_hwi (~UINTVAL (operands[2])) > 33" + "#" + "&& reload_completed" + [(set (match_dup 0) (and:DI (match_dup 1) (match_dup 3))) + (set (match_dup 0) (and:DI (match_dup 0) (match_dup 4)))] +{ + unsigned HOST_WIDE_INT bits = ~UINTVAL (operands[2]); + unsigned HOST_WIDE_INT topbit = HOST_WIDE_INT_1U << floor_log2 (bits); + + operands[3] = GEN_INT (~bits | topbit); + operands[4] = GEN_INT (~topbit); +}) + (define_insn "*binv" [(set (match_operand:X 0 "register_operand" "=r") (xor:X (ashift:X (const_int 1) diff --git a/gcc/config/riscv/predicates.md b/gcc/config/riscv/predicates.md index 6de9b39e39b..b368c11c930 100644 --- a/gcc/config/riscv/predicates.md +++ b/gcc/config/riscv/predicates.md @@ -304,6 +304,11 @@ (match_test "ctz_hwi (INTVAL (op)) > 0") (match_test "SMALL_OPERAND (INTVAL (op) >> ctz_hwi (INTVAL (op)))"))) +;; A CONST_INT operand that has exactly two bits cleared. +(define_predicate "const_nottwobits_operand" + (and (match_code "const_int") + (match_test "popcount_hwi (~UINTVAL (op)) == 2"))) + ;; A CONST_INT operand that fits into the unsigned half of a ;; signed-immediate after the top bit has been cleared. (define_predicate "uimm_extra_bit_operand" diff --git a/gcc/testsuite/gcc.target/riscv/zbs-bclri.c b/gcc/testsuite/gcc.target/riscv/zbs-bclri.c new file mode 100644 index 00000000000..12e2063436c --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zbs-bclri.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zbs -mabi=lp64" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */ + +/* bclri + bclri */ +long long f5 (long long a) +{ + return a & ~0x11000; +} + +/* { dg-final { scan-assembler-times "bclri\t" 2 } } */ +