From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by sourceware.org (Postfix) with ESMTPS id CE7F7388981D for ; Wed, 9 Nov 2022 23:08:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CE7F7388981D 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-x129.google.com with SMTP id p8so28404055lfu.11 for ; Wed, 09 Nov 2022 15:08:07 -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=6skWvNOOS+0MrQuZ0SROYfTfNese/0mcifd9yrS03d0=; b=QjX64Rqm2lEYSkDMdSZ033CFUPoSLGQCk/sjMpiQrGyYIJwaGLn16i/nIqKQ+ndWoC NOrEvgVe6tam4HQtARWxA4Q0/UpquUwsGtCpU9orjpbIS3ME9BvSkygbjJ9l+kZerqjB rCu6narV05079GYfMCnbkQq2J4R7AXnuX/nW2fgsrhp+hOjwPhotM71alDdmrkKJq70d d3rMIo+LB7eJDODX+Khb3GpVIW6/LKhJODhy3twgU51pMMm3cdoX5zcdcEN/MiGV0cNG uTB8p1Iy7WmgQnJ4iVZFV6wa3GRVnbeu7cqlQ5LHMX0TCncozwsrtnw049nrGh4vDH4g d4tQ== 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=6skWvNOOS+0MrQuZ0SROYfTfNese/0mcifd9yrS03d0=; b=cTz849y3fzzIovHZlO2mh5PMW52xIM4JRtTkFHHUIZmDfhSV4Xz+kGQ1ToC1zQRmdw MlzBQLm+NQNHp+uRIAZy5zi1QHpwNn5uyPpqW1qZSlWzJOxyZ02AOTw7CaAmmvAw9eTH FRq2oCqSnvsn3SEtql6BrLrsd3sIT3x9h3wsW+RgkJXsid3Uh4H8EHsenDSgfw4mroqY 1OOIaN/Jv7cDcXswTzd/wu8cY0kX8Wp02wHzopkcMwLUftQU0EwAg3SlZxkVQ8g7++Vc Q2GuFQKeiEN5kBrNKqIzkp2bhlwjsc5MIZjIUqpodXdpOrKStSfwsf4oUg4aXtbUCZ1d z2kA== X-Gm-Message-State: ACrzQf3/9xs9CdyHy0QGl1TdQVZZZOo6kQpyctd/LWjhXUtDBCuR1Jf7 j11TbcdgGR/qJb2yJkjRZhvloqS4TQzXqRXE X-Google-Smtp-Source: AMsMyM6l5zWY40kwY/Avu8s3yNLIp+N3nSlh35Tjlc2aALR0szneKJdgln+rj6j1vGHtzgzKZlsoSg== X-Received: by 2002:ac2:5a0c:0:b0:4b1:21e2:9e22 with SMTP id q12-20020ac25a0c000000b004b121e29e22mr13538503lfn.427.1668035286040; Wed, 09 Nov 2022 15:08:06 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id 131-20020a2e0989000000b0026c5579c64csm2393416ljj.89.2022.11.09.15.08.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 15:08:05 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Cc: Richard Biener , Jiang-Ning Liu , Jeff Law , Tamar Christina , Christoph Muellner , Philipp Tomsich Subject: [PATCH] ifcombine: recognize single bit test of sign-bit Date: Thu, 10 Nov 2022 00:07:47 +0100 Message-Id: <20221109230747.3240551-1-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,JMQ_SPF_NEUTRAL,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 List-Id: Our ifcombine pass combines 2 single-bit tests into a single test of the form "(a & T) == T". However, detection of the bit-tests does not work for sign-bits (of any mode) as the RTL presented to the pass uses a cast to a signed-type and an order-operator. E.g., the test for 'a & 0x80' presents as: _1 = *a_5(D); _2 = (signed char) _1; if (_2 < 0) goto ; else goto ; This adds detection logic to recognize_single_bit_test() for this case and reports it as a single-bit test. gcc/ChangeLog: * tree-ssa-ifcombine.cc (recognize_single_bit_test): Add detection for tests against the sign-bit of the relevant type as a single-bit test. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/ssa-ifcombine-14.c: New test. Signed-off-by: Philipp Tomsich --- .../gcc.dg/tree-ssa/ssa-ifcombine-14.c | 14 +++++++++++++ gcc/tree-ssa-ifcombine.cc | 20 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-14.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-14.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-14.c new file mode 100644 index 00000000000..f9b1115a34a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-14.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-ifcombine-details-blocks" } */ + +void sink(); + +void same(unsigned char *a) +{ + if (*a & 0x80) + if (*a & 0x40) + g(); +} + +/* { dg-final { scan-tree-dump "optimizing double bit test" } } */ + diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc index 80c41c45489..cd6331f84db 100644 --- a/gcc/tree-ssa-ifcombine.cc +++ b/gcc/tree-ssa-ifcombine.cc @@ -225,6 +225,26 @@ recognize_single_bit_test (gcond *cond, tree *name, tree *bit, bool inv) { gimple *stmt; + /* Handle the test for a sign-bit: + unsigned charD.15 _1; + _2 = (signed char) _1; + if (_2 < 0) */ + if (TREE_CODE (gimple_cond_lhs (cond)) == SSA_NAME + && !TYPE_UNSIGNED (TREE_TYPE (gimple_cond_lhs (cond))) + && gimple_cond_code (cond) == (inv ? GE_EXPR : LT_EXPR) + && integer_zerop (gimple_cond_rhs (cond))) + { + tree type = TREE_TYPE (gimple_cond_lhs (cond)); + + stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond)); + if (!is_gimple_assign (stmt)) + return false; + *name = gimple_assign_rhs1 (stmt); + *bit = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1); + + return true; + } + /* Get at the definition of the result of the bit test. */ if (gimple_cond_code (cond) != (inv ? EQ_EXPR : NE_EXPR) || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME -- 2.34.1