From patchwork Thu Nov 11 14:10:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 47472 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9B600385AC1E for ; Thu, 11 Nov 2021 14:16:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x131.google.com (mail-lf1-x131.google.com [IPv6:2a00:1450:4864:20::131]) by sourceware.org (Postfix) with ESMTPS id 44C653857C47 for ; Thu, 11 Nov 2021 14:10:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 44C653857C47 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-lf1-x131.google.com with SMTP id l22so14470135lfg.7 for ; Thu, 11 Nov 2021 06:10:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vrull-eu.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+2URXdDrLiNkTCN+zizlI9W83wt65buglYHNyqTIYhk=; b=nus9c5YioWG33AXgTLVtHRP+UOuxadfxWLg+7aeEHULWmxitE9x4XktY/hb0m5QG53 PKwROUoEs1ivHwntZpx+JEs0+Lh7CVYKfMHmVn9kqgQurPhaK51GaL4+hZYvY0DeudR/ jvSzJAlGsttmaftykNHYDObS8foM8OYIeSLwd87/bp8EanIVmBF+0yb3jrdbLzX/LFD4 zLrFK8oEiqOhL4Z9B6Wkb6XbPMRm1mM4UiDTy74NUoFBUeM4dbKOCtmzYi3NFrUfHqi6 8GcBp9wtwPpdID202rNbFLSHCtfzBYq6UiTFnBchb2s52S7QImFZlXyEgIsAXFuZs0wH l75A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+2URXdDrLiNkTCN+zizlI9W83wt65buglYHNyqTIYhk=; b=LVmtfegKE7g6dEO1v4dHGGKObM2B0Jt5S+ugdWa2hkBbsHg318tYw1TPjc1+ubx6fb gd+PBLN1aA5uUGrPq/abq70ucef+s+t4uOd0Jgeqq+DgbnWRYaxs8UOTcFs+eVn8nnSC wDmuXhZji+82PJygB3OWxwIj75qARIEd9ZyNmyEcWm4Ewq/fpskVhQ4Fq7DmTTY5uwVs KEDtN1JKBOYTaOHBhEvxDPQtm52Qpx9GfFFpIB7gdY/nZh2pIjy0O7isS0B/XjGifCLt /RL2kGfl/JrkmBSGC6J0/gxojU4YWrFG+E49MkCI6qboo//405y6qsAd5ng4xcsiBBzW xJsw== X-Gm-Message-State: AOAM532ge94zWFJUwk+j5KRWBMT4pHdAurwzzmBYgEaHKkj2dvapoE4h 4jnhQECv85QejDMAA52TQ8wBw2IJsV4Os2EH X-Google-Smtp-Source: ABdhPJwLEnZMDlu0A4TuZJ5HHNCo711nk1BulAPBZ+J7fCBLs12SCYBWgis1xPb11nvDdV2Dvitr7w== X-Received: by 2002:a19:f242:: with SMTP id d2mr6883041lfk.516.1636639828764; Thu, 11 Nov 2021 06:10:28 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id a23sm274427ljh.140.2021.11.11.06.10.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 06:10:28 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Subject: [PATCH v1 6/8] RISC-V: bitmanip: add splitter to use bexti for "(a & (1 << BIT_NO)) ? 0 : -1" Date: Thu, 11 Nov 2021 15:10:18 +0100 Message-Id: <20211111141020.2738001-7-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211111141020.2738001-1-philipp.tomsich@vrull.eu> References: <20211111141020.2738001-1-philipp.tomsich@vrull.eu> MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Cc: wilson@tuliptree.org, kito.cheng@gmail.com, Philipp Tomsich Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Consider creating a polarity-reversed mask from a set-bit (i.e., if the bit is set, produce all-ones; otherwise: all-zeros). Using Zbb, this can be expressed as bexti, followed by an addi of minus-one. To enable the combiner to discover this opportunity, we need to split the canonical expression for "(a & (1 << BIT_NO)) ? 0 : -1" into a form combinable into bexti. Consider the function: long f(long a) { return (a & (1 << BIT_NO)) ? 0 : -1; } This produces the following sequence prior to this change: andi a0,a0,16 seqz a0,a0 neg a0,a0 ret Following this change, it results in: bexti a0,a0,4 addi a0,a0,-1 ret gcc/ChangeLog: * config/riscv/bitmanip.md: Add a splitter to generate polarity-reversed masks from a set bit using bexti + addi. gcc/testsuite/ChangeLog: * gcc.target/riscv/zbs-bexti.c: New test. Signed-off-by: Philipp Tomsich --- gcc/config/riscv/bitmanip.md | 13 +++++++++++++ gcc/testsuite/gcc.target/riscv/zbs-bexti.c | 14 ++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 gcc/testsuite/gcc.target/riscv/zbs-bexti.c diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md index 178d1ca0e4b..9e10280e306 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -367,3 +367,16 @@ (define_insn "*bexti" "TARGET_ZBS" "bexti\t%0,%1,%2" [(set_attr "type" "bitmanip")]) + +;; We can create a polarity-reversed mask (i.e. bit N -> { set = 0, clear = -1 }) +;; using a bext(i) followed by an addi instruction. +;; This splits the canonical representation of "(a & (1 << BIT_NO)) ? 0 : -1". +(define_split + [(set (match_operand:GPR 0 "register_operand") + (neg:GPR (eq:GPR (zero_extract:GPR (match_operand:GPR 1 "register_operand") + (const_int 1) + (match_operand 2)) + (const_int 0))))] + "TARGET_ZBB" + [(set (match_dup 0) (zero_extract:GPR (match_dup 1) (const_int 1) (match_dup 2))) + (set (match_dup 0) (plus:GPR (match_dup 0) (const_int -1)))]) diff --git a/gcc/testsuite/gcc.target/riscv/zbs-bexti.c b/gcc/testsuite/gcc.target/riscv/zbs-bexti.c new file mode 100644 index 00000000000..d02c3f7a98d --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zbs-bexti.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zbs -mabi=lp64 -O2" } */ + +/* bexti */ +#define BIT_NO 27 + +long +foo0 (long a) +{ + return (a & (1 << BIT_NO)) ? 0 : -1; +} + +/* { dg-final { scan-assembler "bexti" } } */ +/* { dg-final { scan-assembler "addi" } } */