Message ID | 20211111141020.2738001-2-philipp.tomsich@vrull.eu |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> 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 160BF385AC2E for <patchwork@sourceware.org>; Thu, 11 Nov 2021 14:13:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by sourceware.org (Postfix) with ESMTPS id 37C853857823 for <gcc-patches@gcc.gnu.org>; Thu, 11 Nov 2021 14:10:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 37C853857823 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-x22d.google.com with SMTP id h11so12237098ljk.1 for <gcc-patches@gcc.gnu.org>; Thu, 11 Nov 2021 06:10:26 -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=2Z4aYaIgpj6TniDsVoyYPXaMCCvyBY06ipmVaZ2BHeY=; b=pdcjl3Ij/hU6t6l6Gfqh9YiDqWcvGWKhIp1NnPENHzurqkJ3TvR8ysZKI+LGQADTd8 DW7rQ3r4lRvmrgYNu92Fw1IB53F/JyUKaDe3Vj8Ihy05bOFrxKsYbbY+C3Xcf+GACOOw Kqfqy2uCl1WMDXtNlPumU5HKGtEb2Kby+dKURrpfFODtjzDoIvwTw8HCyyyA4KGfbuqJ 22eXgybO9lj9ssicQWnpC+t0KwyQR0b5x16bWGUIhFKPouwZaNDVUtUROQus3hESZ+2z S2VeSHrvfYUDHr7RMjUE3t7v+coDMQMJ1+tomDUa61NMVzyeJr1bR61eG5l/7sHCG6ur bNyg== 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=2Z4aYaIgpj6TniDsVoyYPXaMCCvyBY06ipmVaZ2BHeY=; b=AB/cG/8GvC9ligwc3N2D+z46eQnaEc5LqktY+XCLIyGud+ce3L6R254Fn8jMWb+fjI 8r+v91a70xLMklzc4KgeBGesR5KG42MDhaou3NHVJWSRYKRoG/vOLdSfNWc2nzOCo7/B sE6EgfxW3sC9N8gXi+1uARM2gJBFCOOPN/uqJHXDseOL+4izwk2Smn8ITyNyxTG+bez3 g4tYphugPKN4TX9jY6IVZ5izoUE81Su7VWO32tMPDq4bg2JhU++Mg62JPn//R+aV5EXQ XP7OnSq98SFBxA4M8gCzr8FUFO8nktJ41Qwj4jFkm/iARNkkXxW3SIHGjVsRreSruPZz 3cMA== X-Gm-Message-State: AOAM5311doDrG9L3JfW9kg6bw+FZAEYegGdBR4lXiL0bwzTOcY3bnlmk kMyoQ9fb0ZrdhbTX7N8mC1Tb/mDPXCXi+bwG X-Google-Smtp-Source: ABdhPJxZTYQPHCKzwCrov1CTPjZgj6SvtyUct9Zcec42dOt/xzjtFKZWixE+ivMQTyxLM/epBIw0rA== X-Received: by 2002:a2e:9b41:: with SMTP id o1mr7223770ljj.499.1636639824804; Thu, 11 Nov 2021 06:10:24 -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.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Nov 2021 06:10:24 -0800 (PST) From: Philipp Tomsich <philipp.tomsich@vrull.eu> To: gcc-patches@gcc.gnu.org Subject: [PATCH v1 1/8] bswap: synthesize HImode bswap from SImode or DImode Date: Thu, 11 Nov 2021 15:10:13 +0100 Message-Id: <20211111141020.2738001-2-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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.5 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Cc: wilson@tuliptree.org, kito.cheng@gmail.com, Philipp Tomsich <philipp.tomsich@vrull.eu> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
Improvements to bitmanip-1.0 (Zb[abcs]) support
|
|
Commit Message
Philipp Tomsich
Nov. 11, 2021, 2:10 p.m. UTC
The RISC-V Zbb extension adds an XLEN (i.e. SImode for rv32, DImode
for rv64) bswap instruction (rev8). While, with the current master,
SImode is synthesized correctly from DImode, HImode is not.
This change adds an appropriate expansion for a HImode bswap, if a
wider bswap is available.
Without this change, the following rv64gc_zbb code is generated for
__builtin_bswap16():
slliw a5,a0,8
zext.h a0,a0
srliw a0,a0,8
or a0,a5,a0
sext.h a0,a0 // this is a 16bit sign-extension following
// the byteswap (e.g. on a 'short' function
// return).
After this change, a bswap (rev8) is used and any extensions are
combined into the shift-right:
rev8 a0,a0
srai a0,a0,48 // the sign-extension is combined into the
// shift; a srli is emitted otherwise...
gcc/ChangeLog:
* optabs.c (expand_unop): support expanding a HImode bswap
using SImode or DImode, followed by a shift.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/zbb-bswap.c: New test.
Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu>
---
gcc/optabs.c | 6 ++++++
gcc/testsuite/gcc.target/riscv/zbb-bswap.c | 22 ++++++++++++++++++++++
2 files changed, 28 insertions(+)
create mode 100644 gcc/testsuite/gcc.target/riscv/zbb-bswap.c
Comments
Hi Philipp: I would suggest add define_expand pattern for bswaphi2 rather than changing expand_unop with following reasons: - There is a comment above this change, and it also tried widen_bswap after this if-block, so I think this patch is kind of violating this comment. /* HImode is special because in this mode BSWAP is equivalent to ROTATE or ROTATERT. First try these directly; if this fails, then try the obvious pair of shifts with allowed widening, as this will probably be always more efficient than the other fallback methods. */ - This change doesn't improve the code gen without bswapsi2 or bswapdi2, (e.g. rv64gc result same code) and this also might also affect other targets, but we didn't have evidence it will always get better results, so I guess at least we should add a target hook for this. - ...I didn't have permission to approve this change since it's not part of RISC-V back-end :p On Thu, Nov 11, 2021 at 10:10 PM Philipp Tomsich <philipp.tomsich@vrull.eu> wrote: > > The RISC-V Zbb extension adds an XLEN (i.e. SImode for rv32, DImode > for rv64) bswap instruction (rev8). While, with the current master, > SImode is synthesized correctly from DImode, HImode is not. > > This change adds an appropriate expansion for a HImode bswap, if a > wider bswap is available. > > Without this change, the following rv64gc_zbb code is generated for > __builtin_bswap16(): > slliw a5,a0,8 > zext.h a0,a0 > srliw a0,a0,8 > or a0,a5,a0 > sext.h a0,a0 // this is a 16bit sign-extension following > // the byteswap (e.g. on a 'short' function > // return). > > After this change, a bswap (rev8) is used and any extensions are > combined into the shift-right: > rev8 a0,a0 > srai a0,a0,48 // the sign-extension is combined into the > // shift; a srli is emitted otherwise... > > gcc/ChangeLog: > > * optabs.c (expand_unop): support expanding a HImode bswap > using SImode or DImode, followed by a shift. > > gcc/testsuite/ChangeLog: > > * gcc.target/riscv/zbb-bswap.c: New test. > > Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu> > --- > > gcc/optabs.c | 6 ++++++ > gcc/testsuite/gcc.target/riscv/zbb-bswap.c | 22 ++++++++++++++++++++++ > 2 files changed, 28 insertions(+) > create mode 100644 gcc/testsuite/gcc.target/riscv/zbb-bswap.c > > diff --git a/gcc/optabs.c b/gcc/optabs.c > index 019bbb62882..7a3ffbe4525 100644 > --- a/gcc/optabs.c > +++ b/gcc/optabs.c > @@ -3307,6 +3307,12 @@ expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target, > return temp; > } > > + /* If we are missing a HImode BSWAP, but have one for SImode or > + DImode, use a BSWAP followed by a SHIFT. */ > + temp = widen_bswap (as_a <scalar_int_mode> (mode), op0, target); > + if (temp) > + return temp; > + > last = get_last_insn (); > > temp1 = expand_binop (mode, ashl_optab, op0, > diff --git a/gcc/testsuite/gcc.target/riscv/zbb-bswap.c b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c > new file mode 100644 > index 00000000000..6ee27d9f47a > --- /dev/null > +++ b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c > @@ -0,0 +1,22 @@ > +/* { dg-do compile } */ > +/* { dg-options "-march=rv64gc_zbb -mabi=lp64 -O2" } */ > + > +unsigned long > +func64 (unsigned long i) > +{ > + return __builtin_bswap64(i); > +} > + > +unsigned int > +func32 (unsigned int i) > +{ > + return __builtin_bswap32(i); > +} > + > +unsigned short > +func16 (unsigned short i) > +{ > + return __builtin_bswap16(i); > +} > + > +/* { dg-final { scan-assembler-times "rev8" 3 } } */ > -- > 2.32.0 >
On Thu, Nov 11, 2021 at 3:13 PM Philipp Tomsich <philipp.tomsich@vrull.eu> wrote: > > The RISC-V Zbb extension adds an XLEN (i.e. SImode for rv32, DImode > for rv64) bswap instruction (rev8). While, with the current master, > SImode is synthesized correctly from DImode, HImode is not. > > This change adds an appropriate expansion for a HImode bswap, if a > wider bswap is available. > > Without this change, the following rv64gc_zbb code is generated for > __builtin_bswap16(): > slliw a5,a0,8 > zext.h a0,a0 > srliw a0,a0,8 > or a0,a5,a0 > sext.h a0,a0 // this is a 16bit sign-extension following > // the byteswap (e.g. on a 'short' function > // return). > > After this change, a bswap (rev8) is used and any extensions are > combined into the shift-right: > rev8 a0,a0 > srai a0,a0,48 // the sign-extension is combined into the > // shift; a srli is emitted otherwise... > > gcc/ChangeLog: > > * optabs.c (expand_unop): support expanding a HImode bswap > using SImode or DImode, followed by a shift. > > gcc/testsuite/ChangeLog: > > * gcc.target/riscv/zbb-bswap.c: New test. > > Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu> > --- > > gcc/optabs.c | 6 ++++++ > gcc/testsuite/gcc.target/riscv/zbb-bswap.c | 22 ++++++++++++++++++++++ > 2 files changed, 28 insertions(+) > create mode 100644 gcc/testsuite/gcc.target/riscv/zbb-bswap.c > > diff --git a/gcc/optabs.c b/gcc/optabs.c > index 019bbb62882..7a3ffbe4525 100644 > --- a/gcc/optabs.c > +++ b/gcc/optabs.c > @@ -3307,6 +3307,12 @@ expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target, > return temp; > } > > + /* If we are missing a HImode BSWAP, but have one for SImode or > + DImode, use a BSWAP followed by a SHIFT. */ > + temp = widen_bswap (as_a <scalar_int_mode> (mode), op0, target); > + if (temp) > + return temp; > + I think it would be more natural to temporarily terminate the HImode case here and re-open it inside the following if (is_a <scalar_int_mode> (mode, &int_mode)) { temp = widen_bswap (int_mode, op0, target); if (temp) return temp; here to handle the ashl/lshr/ior fallback. Richard. > last = get_last_insn (); > > temp1 = expand_binop (mode, ashl_optab, op0, > diff --git a/gcc/testsuite/gcc.target/riscv/zbb-bswap.c b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c > new file mode 100644 > index 00000000000..6ee27d9f47a > --- /dev/null > +++ b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c > @@ -0,0 +1,22 @@ > +/* { dg-do compile } */ > +/* { dg-options "-march=rv64gc_zbb -mabi=lp64 -O2" } */ > + > +unsigned long > +func64 (unsigned long i) > +{ > + return __builtin_bswap64(i); > +} > + > +unsigned int > +func32 (unsigned int i) > +{ > + return __builtin_bswap32(i); > +} > + > +unsigned short > +func16 (unsigned short i) > +{ > + return __builtin_bswap16(i); > +} > + > +/* { dg-final { scan-assembler-times "rev8" 3 } } */ > -- > 2.32.0 >
On Fri, Nov 19, 2021 at 11:20 AM Richard Biener <richard.guenther@gmail.com> wrote: > > On Thu, Nov 11, 2021 at 3:13 PM Philipp Tomsich > <philipp.tomsich@vrull.eu> wrote: > > > > The RISC-V Zbb extension adds an XLEN (i.e. SImode for rv32, DImode > > for rv64) bswap instruction (rev8). While, with the current master, > > SImode is synthesized correctly from DImode, HImode is not. > > > > This change adds an appropriate expansion for a HImode bswap, if a > > wider bswap is available. > > > > Without this change, the following rv64gc_zbb code is generated for > > __builtin_bswap16(): > > slliw a5,a0,8 > > zext.h a0,a0 > > srliw a0,a0,8 > > or a0,a5,a0 > > sext.h a0,a0 // this is a 16bit sign-extension following > > // the byteswap (e.g. on a 'short' function > > // return). > > > > After this change, a bswap (rev8) is used and any extensions are > > combined into the shift-right: > > rev8 a0,a0 > > srai a0,a0,48 // the sign-extension is combined into the > > // shift; a srli is emitted otherwise... > > > > gcc/ChangeLog: > > > > * optabs.c (expand_unop): support expanding a HImode bswap > > using SImode or DImode, followed by a shift. > > > > gcc/testsuite/ChangeLog: > > > > * gcc.target/riscv/zbb-bswap.c: New test. > > > > Signed-off-by: Philipp Tomsich <philipp.tomsich@vrull.eu> > > --- > > > > gcc/optabs.c | 6 ++++++ > > gcc/testsuite/gcc.target/riscv/zbb-bswap.c | 22 ++++++++++++++++++++++ > > 2 files changed, 28 insertions(+) > > create mode 100644 gcc/testsuite/gcc.target/riscv/zbb-bswap.c > > > > diff --git a/gcc/optabs.c b/gcc/optabs.c > > index 019bbb62882..7a3ffbe4525 100644 > > --- a/gcc/optabs.c > > +++ b/gcc/optabs.c > > @@ -3307,6 +3307,12 @@ expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target, > > return temp; > > } > > > > + /* If we are missing a HImode BSWAP, but have one for SImode or > > + DImode, use a BSWAP followed by a SHIFT. */ > > + temp = widen_bswap (as_a <scalar_int_mode> (mode), op0, target); > > + if (temp) > > + return temp; > > + > > I think it would be more natural to temporarily terminate the HImode case here > and re-open it inside the following > > if (is_a <scalar_int_mode> (mode, &int_mode)) > { > temp = widen_bswap (int_mode, op0, target); > if (temp) > return temp; > > here to handle the ashl/lshr/ior fallback. But as Kito says, code generation for more targets would need to be looked at. Richard. > Richard. > > > last = get_last_insn (); > > > > temp1 = expand_binop (mode, ashl_optab, op0, > > diff --git a/gcc/testsuite/gcc.target/riscv/zbb-bswap.c b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c > > new file mode 100644 > > index 00000000000..6ee27d9f47a > > --- /dev/null > > +++ b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c > > @@ -0,0 +1,22 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-march=rv64gc_zbb -mabi=lp64 -O2" } */ > > + > > +unsigned long > > +func64 (unsigned long i) > > +{ > > + return __builtin_bswap64(i); > > +} > > + > > +unsigned int > > +func32 (unsigned int i) > > +{ > > + return __builtin_bswap32(i); > > +} > > + > > +unsigned short > > +func16 (unsigned short i) > > +{ > > + return __builtin_bswap16(i); > > +} > > + > > +/* { dg-final { scan-assembler-times "rev8" 3 } } */ > > -- > > 2.32.0 > >
diff --git a/gcc/optabs.c b/gcc/optabs.c index 019bbb62882..7a3ffbe4525 100644 --- a/gcc/optabs.c +++ b/gcc/optabs.c @@ -3307,6 +3307,12 @@ expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target, return temp; } + /* If we are missing a HImode BSWAP, but have one for SImode or + DImode, use a BSWAP followed by a SHIFT. */ + temp = widen_bswap (as_a <scalar_int_mode> (mode), op0, target); + if (temp) + return temp; + last = get_last_insn (); temp1 = expand_binop (mode, ashl_optab, op0, diff --git a/gcc/testsuite/gcc.target/riscv/zbb-bswap.c b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c new file mode 100644 index 00000000000..6ee27d9f47a --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zbb-bswap.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zbb -mabi=lp64 -O2" } */ + +unsigned long +func64 (unsigned long i) +{ + return __builtin_bswap64(i); +} + +unsigned int +func32 (unsigned int i) +{ + return __builtin_bswap32(i); +} + +unsigned short +func16 (unsigned short i) +{ + return __builtin_bswap16(i); +} + +/* { dg-final { scan-assembler-times "rev8" 3 } } */