From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout-p-201.mailbox.org (mout-p-201.mailbox.org [80.241.56.171]) by sourceware.org (Postfix) with ESMTPS id EE4FB3858D20 for ; Mon, 26 Jun 2023 02:12:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE4FB3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gdcproject.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gdcproject.org Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-201.mailbox.org (Postfix) with ESMTPS id 4QqBF36pYRz9sWJ; Mon, 26 Jun 2023 04:12:23 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gdcproject.org; s=MBO0001; t=1687745543; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=zN8Ayfsr6/Jy5YGPxPLZdD8F3p/y67gMNId8lw5u3gQ=; b=QHUXjknp0fci2HH4BQja8mFz8Hn5ji+FZ+bJKHzBNiUgX5/EWVhQn22goZ39d8U4evRaI2 mMhOHN/qC+9XkmKQyTHRiDC7UHllY3IqwxQ15HuJztnDCJ0OGaD/+WGN5RUwB2cI+lkaqv liGmEgiYzEfvzKyNVZCYEAGMyIYTlnPZoxBqBxqBi4Kzt203Q3A1RjtXSOYOUBAkheJil5 MkiVGAHSRIyXPx4S+vGRnlqkFszPtonPMHIZaLqUP+h7o11KwItxt6Icl+GdKA09lbX8u4 ksJypMTYXNb9b9JMxUlrblQwekvq8IIzJXN8+AzG0TIMoZgVDLJ+gLpDN+CzSQ== From: Iain Buclaw To: gcc-patches@gcc.gnu.org Cc: Iain Buclaw Subject: [committed] d: Suboptimal codegen for __builtin_expect(cond, false) Date: Mon, 26 Jun 2023 04:12:22 +0200 Message-Id: <20230626021222.419821-1-ibuclaw@gdcproject.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4QqBF36pYRz9sWJ 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_LOW,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 List-Id: Hi, Since PR96435, both boolean objects and expressions have been evaluated in the following way by the D front-end. (*(ubyte*)&obj_or_expr) & 1 It has been noted that sometimes this can cause the back-end to optimize in non-obvious ways - in particular with __builtin_expect. This @safe feature is now restricted to just when reading the value of a bool field that comes from a union. Bootstrapped and regression tested on x86_64-linux-gnu/-m32, committed to mainline, and backported to releases/gcc-13 through to gcc-10. Regards, Iain. --- PR d/110359 gcc/d/ChangeLog: * d-convert.cc (convert_for_rvalue): Only apply the @safe boolean conversion to boolean fields of a union. (convert_for_condition): Call convert_for_rvalue in the default case. gcc/testsuite/ChangeLog: * gdc.dg/pr110359.d: New test. --- gcc/d/d-convert.cc | 31 +++++++++++++++++++------------ gcc/testsuite/gdc.dg/pr110359.d | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/gdc.dg/pr110359.d diff --git a/gcc/d/d-convert.cc b/gcc/d/d-convert.cc index cdbd69cf012..2b9d8e78fb6 100644 --- a/gcc/d/d-convert.cc +++ b/gcc/d/d-convert.cc @@ -619,7 +619,7 @@ convert_expr (tree exp, Type *etype, Type *totype) return result ? result : convert (build_ctype (totype), exp); } -/* Return a TREE represenwation of EXPR, whose type has been converted from +/* Return a TREE representation of EXPR, whose type has been converted from * ETYPE to TOTYPE, and is being used in an rvalue context. */ tree @@ -634,20 +634,27 @@ convert_for_rvalue (tree expr, Type *etype, Type *totype) { /* If casting from bool, the result is either 0 or 1, any other value violates @safe code, so enforce that it is never invalid. */ - if (CONSTANT_CLASS_P (expr)) - result = d_truthvalue_conversion (expr); - else + for (tree ref = expr; TREE_CODE (ref) == COMPONENT_REF; + ref = TREE_OPERAND (ref, 0)) { - /* Reinterpret the boolean as an integer and test the first bit. - The generated code should end up being equivalent to: + /* If the expression is a field that's part of a union, reinterpret + the boolean as an integer and test the first bit. The generated + code should end up being equivalent to: *cast(ubyte *)&expr & 1; */ - machine_mode bool_mode = TYPE_MODE (TREE_TYPE (expr)); - tree mtype = lang_hooks.types.type_for_mode (bool_mode, 1); - result = fold_build2 (BIT_AND_EXPR, mtype, - build_vconvert (mtype, expr), - build_one_cst (mtype)); + if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == UNION_TYPE) + { + machine_mode bool_mode = TYPE_MODE (TREE_TYPE (expr)); + tree mtype = lang_hooks.types.type_for_mode (bool_mode, 1); + result = fold_build2 (BIT_AND_EXPR, mtype, + build_vconvert (mtype, expr), + build_one_cst (mtype)); + break; + } } + if (result == NULL_TREE) + result = d_truthvalue_conversion (expr); + result = convert (build_ctype (tbtype), result); } @@ -844,7 +851,7 @@ convert_for_condition (tree expr, Type *type) break; default: - result = expr; + result = convert_for_rvalue (expr, type, type); break; } diff --git a/gcc/testsuite/gdc.dg/pr110359.d b/gcc/testsuite/gdc.dg/pr110359.d new file mode 100644 index 00000000000..bf69201d9a5 --- /dev/null +++ b/gcc/testsuite/gdc.dg/pr110359.d @@ -0,0 +1,22 @@ +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110359 +// { dg-do compile } +// { dg-options "-fdump-tree-original" } +double pow(in double x, in ulong p) +{ + import gcc.builtins : __builtin_expect; + if (__builtin_expect(p == 0, false)) + return 1; + if (__builtin_expect(p == 1, false)) + return x; + + double s = x; + double v = 1; + for (ulong i = p; i > 1; i >>= 1) + { + v = (i & 0x1) ? s * v : v; + s = s * s; + } + return v * s; +} +// { dg-final { scan-tree-dump "if \\(__builtin_expect \\(p == 0, 0\\) != 0\\)" "original" } } +// { dg-final { scan-tree-dump "if \\(__builtin_expect \\(p == 1, 0\\) != 0\\)" "original" } } -- 2.39.2