From patchwork Mon Oct 16 00:01:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 153078 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp3152006vqb; Sun, 15 Oct 2023 17:02:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqNX8H7qVew/dydMisVEPLRKRHyYia8r0xO9Sv/jtgaoEC/caFGcjby5p8YZTYnkrM25L4 X-Received: by 2002:ac8:7c52:0:b0:405:396e:f059 with SMTP id o18-20020ac87c52000000b00405396ef059mr45096662qtv.33.1697414530935; Sun, 15 Oct 2023 17:02:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697414530; cv=pass; d=google.com; s=arc-20160816; b=IgbuNNlQf1l8aVgnKi4GODk6g0QUVG5orGV/dilmvkwe6RvmXdEJbSgWGw3Or5qXeH aHkrn5H65Y6tn3jXiN+ebX28JGWiOvyqxRr/dh4iDRVu96PzHJkSYBnsu07RUUSUe7jP rA0JSFlv0pKxrC4hBNdRqYgMqXWdeotTmilDpFM1a87dJtDUT5cZHVH0Qn4jNhNJa4fP kBwmlTj3B/cs/qWpAGad+rWVVs5hNXnC0vUVSk6WtwpZ/8AiMLLK4O58+i+rI/he8xpx YUk1JJtMXRi9ZcyNHcjaLwiMA4vvm1CtQDB0Bbe5fJfJPJ8ca2HszBmlPyyGS5fVi1ba +WyQ== 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=W4X3ssKpxc6a5NKBK1focb67vQ+rn7f1FXhntsNGtIo=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=zOjp3ktiaqsiVmuf3bWXDmQOzG8LM2+x66LbeEix1pGAxcIYWRzk2oudPTDzw+q/FH mF1tULS0l9y+Iuxwd9nP/VJ7CjemKxQmVp+q32c+2aV8XI18x6wgTFkqBsG/Lw3fOTCl /9eVVLsjCI28fu6vjrWzhQHukUvvv8k4MXdnI1PwonshlT+RWq9dZsQEquKoCjQWYpj7 3Mm8mI5d+MwenK7V/uPIbqk39oMI3e8yKBKWY8/gA4nCAZ7d7eNUVHHsoqMn/819N+Vy yNV/Qj5R6TVemvhseEPjrKHUzVIFpRoqEc5EqB9eviox6982SZE+pnffC1SVRcIA6WK7 1osg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=MDcT8m2A; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id a14-20020a05622a064e00b0041984e22bd6si4580480qtb.202.2023.10.15.17.02.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 17:02:10 -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=MDcT8m2A; 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 9CD363858284 for ; Mon, 16 Oct 2023 00:02:10 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by sourceware.org (Postfix) with ESMTPS id A0A183858D32 for ; Mon, 16 Oct 2023 00:01:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A0A183858D32 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 A0A183858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697414499; cv=none; b=E4q/fQcjiZk+HmAixv0hneA5V0lWBBHdsWdLhp9lglokhW0vjrTw9qfkizpPDiPDx4NCfIUozIK78t479oeOEkyjXTmeb+5nNlFzGMCrmBeWCsYkW0+rL84MzUbL9WkOL8A/OcFZMYWANGh0gLaiTl9qSxI+F9+GRETMrzpK+1Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697414499; c=relaxed/simple; bh=B/BKPAkoDh342KvsaNveMM8I3AYqdD/M74418sLywUU=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=BX4YTMjUOuLta63a1jMSs0omMRu2qN3hBU9RL4Z+xBSNd6KmjFKPu9OZAFvhgbtK/S8y2bY6eOah0FLoXNYViKiUglMYQdu28LLTam1n9Qyz2TPY2yefgEl/EWJoZ5jP+++Kqyke5oYIJIjFbs2DFBz3VyVRky6OaFe4lNMq+kE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x52e.google.com with SMTP id 41be03b00d2f7-5859d13f73dso2306109a12.1 for ; Sun, 15 Oct 2023 17:01:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697414470; x=1698019270; 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=W4X3ssKpxc6a5NKBK1focb67vQ+rn7f1FXhntsNGtIo=; b=MDcT8m2AXKrpT50YKN6dm5kLtNLbQZit5wLhhJ7KmtsOKFGLEKvy9y5GkhSOMccYSI /0JkhjObHMSCCmjV+H5u/jrzu5RwbwmFa8Ml2kp6xJWW7wCQLHzqOc7tjyHJep02vMOS WcMAv4lP+Kwous9TuYUrUv+MvX3PmUYIaKOBhqVrZQFjGEQTx4DxLAc+vFzuLAAxfHGG nzhZRoPqpBo9sSx+2EIPCFnvtJSAaeepcPKmcVIMq/9nJreIHnGidZBvRTCYxvX7tAfZ K/0Pb5MCMVudHmrbA2gFtPOQH5beVMaoZwsdzF711ZX7m2gAQztu4dUeRvbvK3OMbPkM Xd6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697414470; x=1698019270; 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=W4X3ssKpxc6a5NKBK1focb67vQ+rn7f1FXhntsNGtIo=; b=SF8U3DkV1HbxoxolvOKVc0KeTLl81iKDz2b8Dm8QZG/NL1QIFgrRTMb3WqXGUXOUdi p7K3jCCzLR9iAzfA82UOv1J2uz17cpS37G4IRW47uZ1wYpX5N0Fy9ED6Gn0fjGCV93Bc t0gG2VeG95wecSPnsJ24sHkczZUX/GZRHFv2t3lAb6b0JzqpKbnR0Iq1VV0EN3x0/H/D AWIwda/Ydp3DSh8HKxqPVT7sU7ba5NXQTGrkrSFNarq4CHOtVVxPkH1N29j6rKWcki3Z wF1jkJtoViugbUaJDDII+YbZhL+/qjiZXnueLi7gADTaxn9siGzZNLn6iECmzzH3OXy1 K1qg== X-Gm-Message-State: AOJu0YwCzszkKv80chAoI6sRPGPTxq3AJJYlYPDdDH0R9deb+hO0k01P umwE8WA7TSJZYVILCmem2CgpGsWoYvhABA== X-Received: by 2002:a17:902:aa48:b0:1c0:bcbc:d5d with SMTP id c8-20020a170902aa4800b001c0bcbc0d5dmr26384375plr.61.1697414469783; Sun, 15 Oct 2023 17:01:09 -0700 (PDT) Received: from xeond.wrightpinski.org ([98.97.114.173]) by smtp.gmail.com with ESMTPSA id e1-20020a170902744100b001bde65894c8sm6702879plt.268.2023.10.15.17.01.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 17:01:09 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH] Improve factor_out_conditional_operation for conversions and constants Date: Mon, 16 Oct 2023 00:01:38 +0000 Message-Id: <20231016000138.2235395-1-pinskia@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-10.7 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, 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: 1779868139077437069 X-GMAIL-MSGID: 1779868139077437069 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. OK? 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-38.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c | 44 ++++++++++++++++++++++ gcc/tree-ssa-phiopt.cc | 8 +++- 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c new file mode 100644 index 00000000000..ca04d1619e6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-38.c @@ -0,0 +1,44 @@ +/* { 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..0ab8fad5898 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)) { @@ -323,7 +325,9 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, 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) + 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) {