From patchwork Wed Nov 9 23:08:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Philipp Tomsich X-Patchwork-Id: 60315 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 1107E3896C3D for ; Wed, 9 Nov 2022 23:08:38 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x12f.google.com (mail-lf1-x12f.google.com [IPv6:2a00:1450:4864:20::12f]) by sourceware.org (Postfix) with ESMTPS id 4AB7A3893677 for ; Wed, 9 Nov 2022 23:08:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4AB7A3893677 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-x12f.google.com with SMTP id j4so28508294lfk.0 for ; Wed, 09 Nov 2022 15:08: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=OAXkJVMsDkHqTfPkK/xjUERE1ZiLOtG20PgJwI+LC4k=; b=FSLA2oU3mI+S5KnoGZSZ8GSfWFHYqHJD5wrJxVb09a78HgGXCcsXCUeggCD9Cm0lb4 nqd3AzeteS7qS227HJhfnpFoDgWneInH5po67khU8tLCa8cbOkILFDxs/ZOxBA4ibxp2 xu0D6ShOY5pHOLoxM+1ITfIcPpBkkMxXxrLZPeQ6KP8dQmI8e6PVwEnMdxf6fnwAtbys PtYuLv2FzFLup2h3hbtvWf8/y/k2fOuJIdfVwMEylBlMw466SRNGjCoXqa9FXZ4Qf+C2 42M6LET4A3ZkAkgv4d9zU8F2MHol4RVR/lX7DdEIOJtEIMDuINNHrZBiJE8I6N5+nuY8 4ntA== 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=OAXkJVMsDkHqTfPkK/xjUERE1ZiLOtG20PgJwI+LC4k=; b=NY/cjnf3Adyz3pOQJoyic9dv4ayq99woE6P07u2KKid9M2NCumg0OLrPEQJt+XHnIY O5iwEq8PXGu/BWCQcaAYb9Ds7Vu8IcwI0GDA7frG+O8hSq8d+AakqZO9m9nR3scY1rWh mpnw0mWmDywFtDYzi9K2I3KKNSMzc98v+QYQN6/AwjywYVN0OVfvDZpf7bkKBOxuocn5 ieHE9qHm6ZiFrtZqnncGg5ssagwT/cbmSWMwnNSD/lMjOWnK+qC0lT/JrA1G8rVMylZJ hqrm+wCkKTlI+uEVHkPzAJa/8PHNCLuNJNFTrRHj3I4bhWHytcI6+4MWQVAdeTOVNuxy 2pOg== X-Gm-Message-State: ACrzQf1ZVMS/VQpFgX65TxirqLQR2LbuiSgbOWqyxXSFomyil2wyhdJI utlgNITqB6rSF8yNxymQiioNeTgHRueQB716 X-Google-Smtp-Source: AMsMyM5GnxESa8O3NenYZO987ov6MtPzXPWwFpGiLzHvXKSLmHHc4skEZjDfwT/2XZW/Q0ZtWRiLvA== X-Received: by 2002:ac2:5f65:0:b0:4a2:6b44:d742 with SMTP id c5-20020ac25f65000000b004a26b44d742mr970519lfc.191.1668035299496; Wed, 09 Nov 2022 15:08:19 -0800 (PST) Received: from ubuntu-focal.. ([2a01:4f9:3a:1e26::2]) by smtp.gmail.com with ESMTPSA id a5-20020a19ca05000000b004a62ff61b3dsm2430149lfg.252.2022.11.09.15.08.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 15:08:19 -0800 (PST) From: Philipp Tomsich To: gcc-patches@gcc.gnu.org Cc: Christoph Muellner , Tamar Christina , Jiang-Ning Liu , Richard Biener , Jeff Law , Philipp Tomsich Subject: [PATCH] ifcombine: fold two bit tests with different polarity Date: Thu, 10 Nov 2022 00:08:15 +0100 Message-Id: <20221109230815.3240583-1-philipp.tomsich@vrull.eu> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 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 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Our ifcombine pass combines 2 single-bit tests into a single test of the form "(a & T) == T", requiring the same polarity (i.e., tests for bit set/cleared) for both bit-tests. However some applications test against two bits expecting one set and the other cleared. This adds support for the case "(a & T) == C" where T is a constant with 2 bits set and C has only one of those bits set. gcc/ChangeLog: * tree-ssa-ifcombine.cc (ifcombine_ifandif): Add support for combining two bit-tests that test for bits of different polarity. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/ssa-ifcombine-15.c: New test. Signed-off-by: Philipp Tomsich --- .../gcc.dg/tree-ssa/ssa-ifcombine-15.c | 14 +++++ gcc/tree-ssa-ifcombine.cc | 56 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c new file mode 100644 index 00000000000..081faa39628 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-ifcombine-15.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-ifcombine-details-blocks" } */ + +void sink(); + +void reversed(unsigned char *a) +{ + if (*a & 0x60) + if (!(*a & 0x02)) + 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 cd6331f84db..ea49cc2bff1 100644 --- a/gcc/tree-ssa-ifcombine.cc +++ b/gcc/tree-ssa-ifcombine.cc @@ -498,6 +498,62 @@ ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv, return true; } + /* See if we test polarity-reversed single bits of the same name in + both tests. In that case remove the outer test, merging both + else edges, and change the inner one to test for + name & (bit1 | bit2) == (bit2). */ + else if ((recognize_single_bit_test (inner_cond, &name1, &bit1, !inner_inv) + && recognize_single_bit_test (outer_cond, &name2, &bit2, outer_inv) + && name1 == name2) + || (recognize_single_bit_test (inner_cond, &name2, &bit2, inner_inv) + && recognize_single_bit_test (outer_cond, &name1, &bit1, !outer_inv) + && name1 == name2)) + { + tree t, t2, t3; + + /* Do it. */ + gsi = gsi_for_stmt (inner_cond); + t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1), + build_int_cst (TREE_TYPE (name1), 1), bit1); + t2 = fold_build2 (LSHIFT_EXPR, TREE_TYPE (name1), + build_int_cst (TREE_TYPE (name1), 1), bit2); + t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), t, t2); + t = force_gimple_operand_gsi (&gsi, t, true, NULL_TREE, + true, GSI_SAME_STMT); + t3 = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t); + t3 = force_gimple_operand_gsi (&gsi, t3, true, NULL_TREE, + true, GSI_SAME_STMT); + t = fold_build2 (result_inv ? NE_EXPR : EQ_EXPR, + boolean_type_node, t2, t3); + t = canonicalize_cond_expr_cond (t); + if (!t) + return false; + gimple_cond_set_condition_from_tree (inner_cond, t); + update_stmt (inner_cond); + + /* Leave CFG optimization to cfg_cleanup. */ + gimple_cond_set_condition_from_tree (outer_cond, + outer_inv ? boolean_false_node : boolean_true_node); + update_stmt (outer_cond); + + update_profile_after_ifcombine (inner_cond_bb, outer_cond_bb); + + if (dump_file) + { + fprintf (dump_file, "optimizing double bit test to "); + print_generic_expr (dump_file, name1); + fprintf (dump_file, " & T == C\nwith temporary T = (1 << "); + print_generic_expr (dump_file, bit1); + fprintf (dump_file, ") | (1 << "); + print_generic_expr (dump_file, bit2); + fprintf (dump_file, ")\nand temporary C = (1 << "); + print_generic_expr (dump_file, bit2); + fprintf (dump_file, ")\n"); + } + + return true; + } + /* See if we have two bit tests of the same name in both tests. In that case remove the outer test and change the inner one to test for name & (bits1 | bits2) != 0. */