From patchwork Mon Jan 22 06:12:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Monk Chiang X-Patchwork-Id: 1888989 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=sifive.com header.i=@sifive.com header.a=rsa-sha256 header.s=google header.b=PKFlFJNW; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TJKfB4sbrz1yPJ for ; Mon, 22 Jan 2024 17:13:21 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 23D96385842B for ; Mon, 22 Jan 2024 06:13:17 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by sourceware.org (Postfix) with ESMTPS id 4ED163858419 for ; Mon, 22 Jan 2024 06:12:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4ED163858419 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4ED163858419 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705903978; cv=none; b=H8WLjJmPb1MTwSB5TFEd2FzU+atr5inT/LTKmUFHl5UudEDW9aq0sSbX7VTeQ48OZP3eVEIIM6OXm97McNO3RBxD0DxUr7qSb3K4VY4Y+5OD8g1/IYtBqjZPWwh8Y/IaGbiSYEElAYG6SAHRphaRkIkB5GDYMUqUXxZlnJhFlP8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705903978; c=relaxed/simple; bh=RPNFhzmjDudgrTSaYoK5ordtRg+g28cX0MCDm995Sfc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=mdBPCOUJbas7GYF6GsfOAErVjQA5LqV+yX60pzENJkMxalBEu9FQbEO0Y3VZcGVySYXSk+engthOttiacCjjl2GLZZe/DY8lkX97g1wy4FPfsnY9v5rZnfMl2pnyqV1CMKC0iZ6JQMaxKUXqmNpAICodrF6/JpeskPQpgZTXzXU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-29080973530so888004a91.1 for ; Sun, 21 Jan 2024 22:12:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1705903975; x=1706508775; 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=TMGOl5k6LdB5y4IneSKb3TJVfnbt57pwb5tThRAzoxI=; b=PKFlFJNW+jzN2GpLOMW6El33+Pu3v6vWN9IS6PMD96i64WEYGhCnRWjTGTFHUs5pmS BQ/o68q7/Vf7N8tk8/pTVT6EMAc+ajwnNz/EBW1eAbO7b0Midk0nCBSvaYzxBkbgR/xT z19/SVcOZOBMufNfJcJpxJaMEBIJE62mCzHmIU6jvBtp3/gptiEV8yPDqL8Nr/UIsaka YWAUazSMmaYnazrT+ebWytl2xeqwtyDsUbXpDsWUro9Mt0FNffvVYHI5dvya3OSmVIyl yOL8yRpIaZJHchUpBedoFhn8ofSLoxFD5i9ncQT9DM5Mib9VQe+INu6ECxJkJKTRLAqw /mJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705903975; x=1706508775; 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=TMGOl5k6LdB5y4IneSKb3TJVfnbt57pwb5tThRAzoxI=; b=qsEf/o9R+32yd35psLAtgXbcTtWgZa5qzSImnfsCdAwduZAl/qCIbx1ovLzNwZDAz9 6xwPYr+XJDzJM1H2LXGR4WThors1NYYBKbtFrrTlIFibMrmB2n533XXagRpEtXei40MW 4oTBCJN1eTBvkiF4lM2tfl/QqwczAL6hAv94517LZv0yN/fyzKoiPhOdZ0wsRwVx9/yr 7AoYmUkFkNfjNVqEl31bxzUOJJCchC0V0GFdTrV3qya5eONMb+nmxB1e2hZYtnUvcW7i 0PXxu+aBRvb8S0zGKzAPQ4LhtYRiTVc09CPh1Ku3mBPV4HRE9FM4MoM/hfFJ4tii0sSE cLoQ== X-Gm-Message-State: AOJu0YxdpKuTCST68vm5PcRMNZu7TGIARQ8dn3K+G1QdvM7cDNJo8b5i 6ME6iQVTjmVhVDmHv/UuNtnTFRzhZidIu/cA4m7wMVjksng0iEuv6YXmUKd6GpT7mRM5bKCFe9r ju17Hyjkmhs1dROjp1O4dMZEJ8XeGESNSajOfASUSCG2rt6WudRJezi5OyB+hVyV9Ai+HvMDT6a 0yAb89Z0WtX0K9QCSH0zpAwstiPJG8Y5hoW1CoxNFiZw== X-Google-Smtp-Source: AGHT+IEHFUWjfoCdnrrkeRUoq8sRLkbDsbtbNXBusm/UqzVWbtPWM9tFHwSdVlRM4Cu2ijhhd7Mzhg== X-Received: by 2002:a17:90b:b12:b0:290:6c93:1b37 with SMTP id bf18-20020a17090b0b1200b002906c931b37mr747009pjb.12.1705903974801; Sun, 21 Jan 2024 22:12:54 -0800 (PST) Received: from hsinchu02.internal.sifive.com (59-124-168-89.hinet-ip.hinet.net. [59.124.168.89]) by smtp.gmail.com with ESMTPSA id u7-20020a17090ac88700b0028b89520c7asm8637865pjt.9.2024.01.21.22.12.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 22:12:54 -0800 (PST) From: Monk Chiang To: gcc-patches@gcc.gnu.org, rguenther@suse.de, jeffreyalaw@gmail.com, kito.cheng@sifive.com Cc: Monk Chiang Subject: [PATCH v2] RISC-V: Add split pattern to generate SFB instructions. [PR113095] Date: Mon, 22 Jan 2024 14:12:39 +0800 Message-Id: <20240122061239.83409-1-monk.chiang@sifive.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Since the match.pd transforms (zero_one == 0) ? y : z y, into ((typeof(y))zero_one * z) y. Add splitters to recongize this expression to generate SFB instructions. gcc/ChangeLog: PR target/113095 * config/riscv/sfb.md: New splitters to rewrite single bit sign extension as the condition to SFB instructions. gcc/testsuite/ChangeLog: * gcc.target/riscv/sfb.c: New test. * gcc.target/riscv/pr113095.c: New test. --- gcc/config/riscv/sfb.md | 32 +++++++++++++++++++++++ gcc/testsuite/gcc.target/riscv/pr113095.c | 20 ++++++++++++++ gcc/testsuite/gcc.target/riscv/sfb.c | 24 +++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 gcc/testsuite/gcc.target/riscv/pr113095.c create mode 100644 gcc/testsuite/gcc.target/riscv/sfb.c diff --git a/gcc/config/riscv/sfb.md b/gcc/config/riscv/sfb.md index 8ab747142c8..bfd229e3d09 100644 --- a/gcc/config/riscv/sfb.md +++ b/gcc/config/riscv/sfb.md @@ -35,3 +35,35 @@ [(set_attr "length" "8") (set_attr "type" "sfb_alu") (set_attr "mode" "")]) + +;; Combine creates this form ((typeof(y))zero_one * z) y +;; for SiFive short forward branches. + +(define_split + [(set (match_operand:X 0 "register_operand") + (and:X (sign_extract:X (match_operand:X 1 "register_operand") + (const_int 1) + (match_operand 2 "immediate_operand")) + (match_operand:X 3 "register_operand"))) + (clobber (match_operand:X 4 "register_operand"))] + "TARGET_SFB_ALU" + [(set (match_dup 4) (zero_extract:X (match_dup 1) (const_int 1) (match_dup 2))) + (set (match_dup 0) (if_then_else:X (ne (match_dup 4) (const_int 0)) + (match_dup 3) + (const_int 0)))]) + +(define_split + [(set (match_operand:X 0 "register_operand") + (and:X (sign_extract:X (match_operand:X 1 "register_operand") + (const_int 1) + (match_operand 2 "immediate_operand")) + (match_operand:X 3 "register_operand"))) + (clobber (match_operand:X 4 "register_operand"))] + "TARGET_SFB_ALU && (UINTVAL (operands[2]) < 11)" + [(set (match_dup 4) (and:X (match_dup 1) (match_dup 2))) + (set (match_dup 0) (if_then_else:X (ne (match_dup 4) (const_int 0)) + (match_dup 3) + (const_int 0)))] +{ + operands[2] = GEN_INT (1 << UINTVAL(operands[2])); +}) diff --git a/gcc/testsuite/gcc.target/riscv/pr113095.c b/gcc/testsuite/gcc.target/riscv/pr113095.c new file mode 100644 index 00000000000..50dd6be11c4 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/pr113095.c @@ -0,0 +1,20 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -march=rv32gc -mabi=ilp32d -mtune=sifive-7-series" } */ + +extern void abort (void); +extern void exit (int); + +unsigned short __attribute__ ((noinline, noclone)) +foo (unsigned short x) { + if (x == 1) + x ^= 0x4002; + + return x; +} + +int main () { + if (foo(1) != 0x4003) + abort (); + + exit(0); +} diff --git a/gcc/testsuite/gcc.target/riscv/sfb.c b/gcc/testsuite/gcc.target/riscv/sfb.c new file mode 100644 index 00000000000..22f164051f4 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/sfb.c @@ -0,0 +1,24 @@ +//* { dg-do compile } */ +/* { dg-options "-O2 -march=rv32gc -mabi=ilp32d -mtune=sifive-7-series" } */ + +int f1(unsigned int x, unsigned int y, unsigned int z) +{ + return ((x & 1) == 0) ? y : z ^ y; +} + +int f2(unsigned int x, unsigned int y, unsigned int z) +{ + return ((x & 1) != 0) ? z ^ y : y; +} + +int f3(unsigned int x, unsigned int y, unsigned int z) +{ + return ((x & 1) == 0) ? y : z | y; +} + +int f4(unsigned int x, unsigned int y, unsigned int z) +{ + return ((x & 1) != 0) ? z | y : y; +} +/* { dg-final { scan-assembler-times "bne" 4 } } */ +/* { dg-final { scan-assembler-times "movcc" 4 } } */