From patchwork Thu Nov 23 05:47:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Feng Wang X-Patchwork-Id: 168719 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp245479vqx; Wed, 22 Nov 2023 22:07:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IH2MPVZyL2QlsXojyEagwr0iRiRQmkT6oZps+ylqu+14k3oP1fxpyPJ/+43GeyoFZKQQewc X-Received: by 2002:a0c:f88c:0:b0:67a:8df:d444 with SMTP id u12-20020a0cf88c000000b0067a08dfd444mr358968qvn.7.1700719624118; Wed, 22 Nov 2023 22:07:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700719624; cv=pass; d=google.com; s=arc-20160816; b=gPnk3VHh4o+C6pye8u30Z2gW4hbXZVWVX3WwRaZuYY0LuEORcfCpAWEjctO7NrOIy7 KknUBeoMRDi5LiKH8vSoJRZ8cjT4MndEqwKrHN77EBQHYCGqmNTQNHPgbmXDfbH4hdsL P+JFdFpVuZrJOqZY1LC+Af8VaEnVAvcR1DIH6WvoQGCQy52+lOxWedhYSJWGaVgChHh3 Fx7mHYnUaD06cHBzqPLx7Mp0sp3PJtGEgBujIcL0mSfI7XCuOZ/fHtPvd7TvH+Cx+Iom rukZnWGiJLYetFSOK8b2cboIjxIX8M/WsXKdvKEpxQ33X0BpfzFtukfNWMWewxIxTtju W8Hg== 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:message-id:date:subject:cc:to :from:arc-filter:dmarc-filter:delivered-to; bh=xodQYXKORJR9dmfRr1wXyboOPoB/9wez0lcQfeDygNM=; fh=BmNy8UxvGkxau2yr1HBiUMVbmODcbPrkvQ7Pba0vQOs=; b=FpZwoX3pxkdjAzvAVohsSHyIo7kc6u+VIlzBIPBJ8VnlB3XcgMHTY5WmkIYLqDpkcH I0sNyrnig+EdPnus+T0yHGNFINbIkXbsinA7LQLVilh2A2ES0+pGBWRoenKzDBOBatx6 uXVzQ6H1UV1yIJDimC3EVhfqjJzup/DtlQhs/Qr9jF9/2BUTIg8BT28mX1B/Ym/JzhTM ddOlWox5thimLqPjDzwxjPtixClphQcFDsWgV9FbQvcHwg2pcvB0AkpFKTdgpvnAvQGv 6SUimPj0RGO6RhAv9UCgLJePkIRY873yJu8dr8s2UPdGOHF49Eqo5MFO+eIrVcl/B+CS KlsQ== ARC-Authentication-Results: i=2; mx.google.com; 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" Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id f12-20020a05620a15ac00b0077d6d6f433esi489809qkk.485.2023.11.22.22.07.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 22:07:04 -0800 (PST) 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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0A1EE3883011 for ; Thu, 23 Nov 2023 05:49:18 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from azure-sdnproxy.icoremail.net (azure-sdnproxy.icoremail.net [52.237.72.81]) by sourceware.org (Postfix) with ESMTP id A405638582A4 for ; Thu, 23 Nov 2023 05:48:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A405638582A4 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=eswincomputing.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=eswincomputing.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A405638582A4 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=52.237.72.81 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700718531; cv=none; b=HtKCPVYIym6K07qTHl0bKkeDhPVA/+ADfWbmzrU88xzr6KDFCO5jp+/eBc6xO/1DNFl8LLcoOp+Fzb197JPiKSQlN/voPwwJxyoXxyFMjAGjLC9IKsTAldqi0y4tKbTm+O5WEmIVTmxgwAk6d9tFBlOQniCCTLlym3tumk/qHcw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700718531; c=relaxed/simple; bh=D//WiRA1dh4v9SMKFn2SZ1rT88B8FIHyIIKJADBOYGk=; h=From:To:Subject:Date:Message-Id; b=ggRpR9tmDibrM7gofvQAk9ue85UT+Zw1Ojzw7JKVXO515MnETKfiTPSH2dwrBdw1vtY/d3y/sx3gBRGSp5Cx4J+y46617RKM9ETf+5feqY/3HgomBmaUeEIh2HAMFMtaZiwXZIR330r6mkMV8j6NgfILLXbVj4at0AjA72d+3g4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost.localdomain (unknown [10.12.130.31]) by app2 (Coremail) with SMTP id TQJkCgD334SH515ljxoBAA--.13137S4; Thu, 23 Nov 2023 13:47:51 +0800 (CST) From: Feng Wang To: gcc-patches@gcc.gnu.org Cc: kito.cheng@gmail.com, jeffreyalaw@gmail.com, Feng Wang Subject: [PATCH] gimple-vr-values:Add constraint for gimple-cond optimization Date: Thu, 23 Nov 2023 05:47:58 +0000 Message-Id: <20231123054758.28829-1-wangfeng@eswincomputing.com> X-Mailer: git-send-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U5WlkUUUUU X-CM-SenderInfo: pzdqwwxhqjqvxvzl0uprps33xlqjhudrp/ X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, 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 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: 1783333780362756689 X-GMAIL-MSGID: 1783333780362756689 This patch add another condition for gimple-cond optimization. Refer to the following test case. int foo1 (int data, int res) { res = data & 0xf; res |= res << 4; if (res < 0x22) return 0x22; return res; } with the compilation flag "-march=rv64gc_zba_zbb -mabi=lp64d -O2", before this patch the compilation result is foo1: andi a0,a0,15 slliw a5,a0,4 addw a3,a5,a0 li a4,33 add a0,a5,a0 bleu a3,a4,.L5 ret .L5: li a0,34 ret after this patch the compilation result is foo1: andi a0,a0,15 slliw a5,a0,4 add a5,a5,a0 li a0,34 max a0,a5,a0 ret The reason is in the pass_early_vrp, the arg0 of gimple_cond is replaced,but the PHI node still use the arg0. The some of evrp pass logs are as follows gimple_assign gimple_assign gimple_cond goto ; [INV] else goto ; [INV] : // predicted unlikely by early return (on trees) predictor. : # gimple_phi <_2, 34(3), res_5(2)> The arg0 of gimple_cond is replaced by _9,but the gimple_phi still uses res_5,it will cause optimization fail of PHI node to MAX_EXPR. So the next_use_is_phi is added to control the replacement. gcc/ChangeLog: * vr-values.cc (next_use_is_phi): (simplify_using_ranges::simplify_casted_compare): add new function next_use_is_phi to control the replacement. gcc/testsuite/ChangeLog: * gcc.target/riscv/zbb-min-max-04.c: New test. --- gcc/testsuite/gcc.target/riscv/zbb-min-max-04.c | 14 ++++++++++++++ gcc/vr-values.cc | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/riscv/zbb-min-max-04.c diff --git a/gcc/testsuite/gcc.target/riscv/zbb-min-max-04.c b/gcc/testsuite/gcc.target/riscv/zbb-min-max-04.c new file mode 100644 index 00000000000..8c3e87a35e0 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/zbb-min-max-04.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc_zba_zbb -mabi=lp64d" } */ +/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" } } */ + +int foo1 (int data, int res) +{ + res = data & 0xf; + res |= res << 4; + if (res < 0x22) + return 0x22; + return res; +} + +/* { dg-final { scan-assembler-times "max\t" 1 } } */ \ No newline at end of file diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc index ecb294131b0..1f7a727c638 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -1263,6 +1263,18 @@ simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tr return happened; } +/* Return true if the next use of SSA_NAME is PHI node */ +bool +next_use_is_phi (tree arg) +{ + use_operand_p imm = &(SSA_NAME_IMM_USE_NODE (arg)); + use_operand_p next = imm->next; + if (next && next->loc.stmt + && (gimple_code (next->loc.stmt) == GIMPLE_PHI)) + return true; + return false; +} + /* Simplify OP0 code OP1 when OP1 is a constant and OP0 was a SSA_NAME defined by a type conversion. Replacing OP0 with RHS of the type conversion. Doing so makes the conversion dead which helps subsequent passes. */ @@ -1305,7 +1317,8 @@ simplify_using_ranges::simplify_casted_compare (tree_code &, tree &op0, tree &op if (TREE_CODE (innerop) == SSA_NAME && !POINTER_TYPE_P (TREE_TYPE (innerop)) && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (innerop) - && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0))) + && desired_pro_or_demotion_p (TREE_TYPE (innerop), TREE_TYPE (op0)) + && !next_use_is_phi (op0)) { value_range vr;