From patchwork Tue Oct 24 06:45:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1854153 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=cEW3vP9Y; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4SF2cx3Rk3z23jn for ; Tue, 24 Oct 2023 17:45:37 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 54037385783F for ; Tue, 24 Oct 2023 06:45:35 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id ACBAC3858D39 for ; Tue, 24 Oct 2023 06:45:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ACBAC3858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ACBAC3858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::436 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698129925; cv=none; b=BKpUqDRAG+bY2HTyfQiO9OizUIfuAs/saxjZP7xO6EFNoEXPVcfpVcYx/m3FQiBERdRVq4Wp0DVQG3tLC04v7osqnrgaL97fZEx1xQPVulWC62mNljobcHfclXtVBWRmU+I+Vz1V0YewfmbwVkD1hxmvogNfaEaDynSpQO9VaX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698129925; c=relaxed/simple; bh=NwDNgyikXfxqe9Xj5HyzR0RarSKu3OJoTI85hwU3ZWk=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=gGXcKuMfYEHb+cOaJmTL2ry3w+z05fnCT9O6+Xuoc4xcm+17F2VEWYnDIWjAh172uL4ijJ7O4bVXelocU847/b5k1eTKhzfcOChaFpJppb330LRTxYMuvX8LUt+N7L90pqCIX9cOFTu0A6KBfNp5sRGV7wrda6mFySm7Y18v/z0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-694ed847889so3428182b3a.2 for ; Mon, 23 Oct 2023 23:45:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698129922; x=1698734722; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=0fwqV1piDOlYrE/FUZLeGSpXg2I6XqsZvBpQ/EXHFbI=; b=cEW3vP9YCm+4vVKaaFBgE4hudFTHR0JreQUdz7Tu9klMiIn2Jz8h2Q6ZpNOa/olv9N 6JRfTRzcasHLVloV1U13Wo90JV3ZGFIg3sE+7j8WB0RR4QsQ/7SRq7ml09q6kiOaNn55 a4EqWE0F3E9nRZyGA0HwYBV2nIZ8GHa4ZhorksqpRL6yMA6MSExeTzmLhL/ADFGDtzu3 H0W9yeT+2hEMChi5Jr2gVMQxMLl3vj8TFuXqaMVfJtICE22hAug09dVuQkNTX9wdkQWt suLhkgHCpXhG619nt5T/Az2I3j2fLCUSpKfy3eaGtAORBrXxTLdh0GdI49JqhsUBBOZF HVXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698129922; x=1698734722; 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=0fwqV1piDOlYrE/FUZLeGSpXg2I6XqsZvBpQ/EXHFbI=; b=Lf2cENlOO/keFTiKPWJ59AykEIVHf6GmNr9MynWxtN2AJolZ9iUr8vL7Nh3cdEvl1M Rc5is+OQqQ7Yy/juZAHKL0enjFf5B4a3X98UsovWsTrnq4joOGgKshGXn8vE6Yo0/DM9 OVW3EvsFvJVRN1Q4IzKGJAzYPD+IeC2GrTwSBrO8m2MW3S73qv9VYNY5bWvRnRLi1BnN oZiT1PHgfjYcV0XcOubza0jCaqfu4SPIsbpCab9hE1tjwe1DLht532B1cgSYLBHR8WFd L347rdmdFNfpXHCPKorkpxbUo/DjMtUCMd/fz5L/J+9uSzxWJ0INFL1XAE0aN2GfwIgy gPOw== X-Gm-Message-State: AOJu0Yz/5KfXQjBRpjpg0x5LVVvxOecbaE0A3dx3xMcAetevFseXo6Ez 8YurjAnNgXXsW9KrGy2fhbDU17Am/il6iw== X-Google-Smtp-Source: AGHT+IHxEzgNWCUNr3FTOsw7hn9net15xMR4Etq7U85VudhTVm3IAk5R38mHGTaUSbfBXpQmdw8MUw== X-Received: by 2002:a05:6a20:160e:b0:172:6771:d766 with SMTP id l14-20020a056a20160e00b001726771d766mr2052069pzj.51.1698129922355; Mon, 23 Oct 2023 23:45:22 -0700 (PDT) Received: from xeond.wrightpinski.org ([98.97.114.173]) by smtp.gmail.com with ESMTPSA id oe17-20020a17090b395100b0027cf4c554dasm8450070pjb.11.2023.10.23.23.45.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 23:45:21 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCHv2] Improve factor_out_conditional_operation for conversions and constants Date: Tue, 24 Oct 2023 06:45:58 +0000 Message-Id: <20231024064557.213050-1-pinskia@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-10.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SCC_5_SHORT_WORD_LINES, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org In the case of a NOP conversion (precisions of the 2 types are equal), factoring out the conversion can be done even if int_fits_type_p returns false and even when the conversion is defined by a statement inside the conditional. Since it is a NOP conversion there is no zero/sign extending happening which is why it is ok to be done here; we were trying to prevent an extra sign/zero extend from being moved away from definition which no-op conversions are not. Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/104376 PR tree-optimization/101541 * tree-ssa-phiopt.cc (factor_out_conditional_operation): Allow nop conversions even if it is defined by a statement inside the conditional. gcc/testsuite/ChangeLog: PR tree-optimization/101541 * gcc.dg/tree-ssa/phi-opt-39.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c | 43 ++++++++++++++++++++++ gcc/tree-ssa-phiopt.cc | 16 ++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c new file mode 100644 index 00000000000..6b6006a96db --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-39.c @@ -0,0 +1,43 @@ +/* { dg-options "-O2 -fdump-tree-phiopt" } */ + +unsigned f0(int A) +{ +// A == 0? A : -A same as -A + if (A == 0) return A; + return -A; +} + +unsigned f1(int A) +{ +// A != 0? A : -A same as A + if (A != 0) return A; + return -A; +} +unsigned f2(int A) +{ +// A >= 0? A : -A same as abs (A) + if (A >= 0) return A; + return -A; +} +unsigned f3(int A) +{ +// A > 0? A : -A same as abs (A) + if (A > 0) return A; + return -A; +} +unsigned f4(int A) +{ +// A <= 0? A : -A same as -abs (A) + if (A <= 0) return A; + return -A; +} +unsigned f5(int A) +{ +// A < 0? A : -A same as -abs (A) + if (A < 0) return A; + return -A; +} + +/* f4 and f5 are not allowed to be optimized in early phi-opt. */ +/* { dg-final { scan-tree-dump-times "if" 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-not "if" "phiopt2" } } */ diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 312a6f9082b..bb55a4fba33 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -310,7 +310,9 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, return NULL; /* If arg1 is an INTEGER_CST, fold it to new type. */ if (INTEGRAL_TYPE_P (TREE_TYPE (new_arg0)) - && int_fits_type_p (arg1, TREE_TYPE (new_arg0))) + && (int_fits_type_p (arg1, TREE_TYPE (new_arg0)) + || (TYPE_PRECISION (TREE_TYPE (new_arg0)) + == TYPE_PRECISION (TREE_TYPE (arg1))))) { if (gimple_assign_cast_p (arg0_def_stmt)) { @@ -322,8 +324,12 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, if arg0_def_stmt is the only non-debug stmt in its basic block, because then it is possible this could enable further optimizations (minmax replacement - etc.). See PR71016. */ - if (new_arg0 != gimple_cond_lhs (cond_stmt) + etc.). See PR71016. + Note no-op conversions don't have this issue as + it will not generate any zero/sign extend in that case. */ + if ((TYPE_PRECISION (TREE_TYPE (new_arg0)) + != TYPE_PRECISION (TREE_TYPE (arg1))) + && new_arg0 != gimple_cond_lhs (cond_stmt) && new_arg0 != gimple_cond_rhs (cond_stmt) && gimple_bb (arg0_def_stmt) == e0->src) { @@ -354,6 +360,10 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, return NULL; } new_arg1 = fold_convert (TREE_TYPE (new_arg0), arg1); + + /* Drop the overlow that fold_convert might add. */ + if (TREE_OVERFLOW (new_arg1)) + new_arg1 = drop_tree_overflow (new_arg1); } else return NULL;