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: 157257 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1755617vqx; Mon, 23 Oct 2023 23:45:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGBCr57fObc4VCgeyJpENAiVhmuIuTacTziL/wpuHDLimOLCrye0DhZZEDIfLhDPatF9Nfg X-Received: by 2002:a05:620a:284b:b0:777:5b4e:e52e with SMTP id h11-20020a05620a284b00b007775b4ee52emr11867201qkp.66.1698129947552; Mon, 23 Oct 2023 23:45:47 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698129947; cv=pass; d=google.com; s=arc-20160816; b=TnbiSLruTaa4YWwE/vXDjkURnKLrBp4/q0OjPluCkDh+cmqKfSpet/o1XwPvqhOBsg UmmYfutvLOv06PjiE2gRikeUIDrzB4vZzbsx/EoPSIEsMl9nvX4TWZnuRP5ooVMvx9aV utlAyUp5C/dcuW37PgxFDzrl4Mq9rxjxPJdRoK5gTLp//1ie2KmEZFOMrzmsTCt0C3fk N7/Ds1BmDggf5VRuVpVcU9v0umkqsoPXm4CYeCg2yNk47mWdxX1e5UJRMb4N23QlFkG2 B87dVZEJItC+9a4ZkkgcJfj4CtDuFzhFhrbSIuSHwvhOw/K+Sjv8XwWvbH840phGPcXL Qnxg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=0fwqV1piDOlYrE/FUZLeGSpXg2I6XqsZvBpQ/EXHFbI=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=iblF6wf1AEaBvoI/SsR3qW2M+YBUt0fYU4FD1Po1AFuDeQoJcpEPmOhfotyvaJCxry kVZxmrXDHA1noBiAFb4L1R8dxD8IRpME4idxznw2bdyZU7d9EWqsajhrRjRcJ6374CMc nPykqfICd8NDkkfl1wtAjB92A0PduLdlW0N6LWJ5wYJ63iAPjK+cYxddiPRHTY+ta6sG XZ5yVsHfr9gYc9M4S7CzDdGNny+qi5x0QbVSu5vwjnXsNB0qVPMM50nQ58NOyvBYI95i aTUFBFOHRy5OH9kYRSKvoP4pjrgYw5Q+L9n4MOx3lDVfhPMCZehKKPSW6BT821+oI6Tk 8Rmg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=cEW3vP9Y; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j6-20020a05620a288600b0077435da759esi6509143qkp.595.2023.10.23.23.45.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 23:45:47 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=cEW3vP9Y; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 51003385840F for ; Tue, 24 Oct 2023 06:45:47 +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-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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780618307984268482 X-GMAIL-MSGID: 1780618307984268482 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;