From patchwork Wed Oct 25 03:50:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 157811 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp2350767vqx; Tue, 24 Oct 2023 20:51:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEazgtLNl0ocbvEJ4RhOMXd/TYkJO2Agu2XCbThb3NT/UFZuBcE3lNynb0F6TNuYtttiC9t X-Received: by 2002:a05:6214:d68:b0:659:e547:ca72 with SMTP id 8-20020a0562140d6800b00659e547ca72mr16584396qvs.40.1698205877063; Tue, 24 Oct 2023 20:51:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698205877; cv=pass; d=google.com; s=arc-20160816; b=fxAX6cUiQH0iiCcX5UlMdcY7bq9SjqOorpo9Pb9ixMaZEbcM+v+ZjUPQ4Nk7wNvmuw Caru5IdZkbYOfLmSGn/2439xICcmSGIOIaEWaWiQUEdB4bc7k+c4i+voZcU5c3l7Qt0E Ga+Ap4mGF8Qt/J4T6TII5tshKvw6pgRes4gr4C4laSBWmnvEtNshDxziDfEqKm0AM9C5 G6rVwJYTIeDKAtf1WXaIjNW9UF3pKi1PaYX0IU2Mo1rD44eChALepyfaLL743FfvXyN9 ArKLyeqR6LjBJYLOCF4u/v+ZbhpV7KGLIBZkwLf26j558bs+ExkmGGkrAgZ36bcjwosI oaiw== 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=hYYSNnTLRxcOYOuz+gxRpz4tfTauCxzZZrP3Vv20gH4=; fh=6xfd3IYC8Zt5CA4TsXZHhPmGMhO9cjo3oiHaQc5bSUI=; b=WVMBvBZiyxXWjXKrjZNYY7FenSNgI+pD9KQXzApVGKGI5HR5phffJdDlwiwSxKoxrs khyNKfE3p1LAWYf2XsEZmtnJf6lzgqbmcsnGorY5AWqTasdjn18Rdf4hlXAthWuLz4Y1 pG4mirkFa2WLN3hVEHupLxFfoslOtR1/VzV3kILtVtAL2du+JUQeCBA/mIALjNv30WZe 5wc09rFMCOrVIByOH9YDcAqDhm+4XSCQsgAGJMN7xyoXlGf19vHgbaB5jPFFcKgvHbQZ 2XV1G5sVyYwwhBEqeq3iqhgCTrg9bC5ERtnrM7SMo1qA9+NzUx4eJKBQ9SMTZe3K5wJA TJyQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="cIjF9/Vj"; 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 cp10-20020ad44aea000000b006498a534c79si8146392qvb.57.2023.10.24.20.51.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 20:51:17 -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="cIjF9/Vj"; 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 D29363858402 for ; Wed, 25 Oct 2023 03:51:16 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oi1-x231.google.com (mail-oi1-x231.google.com [IPv6:2607:f8b0:4864:20::231]) by sourceware.org (Postfix) with ESMTPS id CBEBD3858D28 for ; Wed, 25 Oct 2023 03:50:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CBEBD3858D28 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 CBEBD3858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::231 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698205854; cv=none; b=ZshV5cPCOFrb0JJgnc94Q2y6npSVZNlaDLJ+g60dHdSpn2mM6chgRq2uCMaUNOGxlmMMpPolESluKApDLP6JW4dZLGKbq/0hrPdJvrgH1CZA3KF5fyM2YjE+o+xnhj49WIPQsponqmcZiRTeUYsdVOMPgX9tsGgijUJ33V7/eaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698205854; c=relaxed/simple; bh=x2MBMtQ4tStegcZ1ZT920O0te9H7qn9qVsW7dVORv7A=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=PTdvrlb/Ybp+ZCiV4HJZX8xAoKbIninVaoWdepO+48z8YWskUzkjj9oo6M9Yll7Kwl13sIjlieAlprCqdKsR74aoHTme6Rm7rvj98Pf1tE8q2Cn+PVpC2JYXG3D3plyILVj4rFXYoq93sKFuRCLyHLHYS5V0bCDKYFVUbZ7VC4w= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3b3ec45d6e9so3276165b6e.0 for ; Tue, 24 Oct 2023 20:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698205852; x=1698810652; 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=hYYSNnTLRxcOYOuz+gxRpz4tfTauCxzZZrP3Vv20gH4=; b=cIjF9/VjXwORStMqVrPijj4e4GGoowA/kAUZwgGsYklZJtevIBaHZd+PE4TFyy1/Df xP5Br0Iq+iVQ3J2r4gyMUi2eHxpMK1ynTkTEHBVtXEv/K8JtQrvS4KywotFaoVQSCR7M ISFymT1Zo/DkIqx99TQtGpIWZpNoYwaa4TJ/RC3Fc46MxU3SRqWi5WguQB2CKO37V/dN SmfkcxyJnqi0u/VLkc36i1d3zslH4Nis72eYACgloIm4drSsIfnUzYcYksBS9hFR2Qji vfoGnUChE0e1D998HnLff8qcmzOtvXrhzvnIo3nJgy0DNeUcKjT2XbSu8rlHCFjbmiP3 +SrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698205852; x=1698810652; 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=hYYSNnTLRxcOYOuz+gxRpz4tfTauCxzZZrP3Vv20gH4=; b=ohwzRZvS0awDdtz5C6DYxJNsNMsY8hBw0+FiQF/no6kvDcqBcB8GwCMmVySmFOKjo9 rO6JqCmETh+itpZ+0jC9DoQ2zQ4cR3wbzJHHFKtXFPi8g9ou0kt+BmppScgn7pj/8kyN xw1Vk/a38YTdkRXpH6emWh4kMF2R8AXelhy12u0I4mzM09qECnxgaI69DsocdrVC/+R3 3x08+XiEePbTuwTZ7oEkgyOMVfUKS1XznVw/zxTpvH2YLH7rMYafxLHGZQ2sm9xieCyN J7oZl32cyKXOoEo+8U0R7739W9tqNGBvWABla6LTPmrlOAs3wwM4uYjjGUAD/lLQ8h44 5ItQ== X-Gm-Message-State: AOJu0Yy1buejfI+wAKFMOfP4QMVWvYm8Z4Fz4ZBs+bhoOsfBKXCeSV4S fTq9Kaqbuw6fuokXBXltNbXovdd2MYjH1g== X-Received: by 2002:a05:6808:189d:b0:3ad:f536:2f26 with SMTP id bi29-20020a056808189d00b003adf5362f26mr19357740oib.18.1698205851729; Tue, 24 Oct 2023 20:50:51 -0700 (PDT) Received: from xeond2.wrightpinski.org ([98.97.114.173]) by smtp.gmail.com with ESMTPSA id l63-20020a638842000000b005a9b20408a7sm7973213pgd.23.2023.10.24.20.50.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 20:50:51 -0700 (PDT) From: Andrew Pinski To: gcc-patches@gcc.gnu.org Cc: Andrew Pinski Subject: [PATCH] Improve tree_expr_nonnegative_p by using the ranger [PR111959] Date: Tue, 24 Oct 2023 20:50:46 -0700 Message-Id: <20231025035046.3918756-1-pinskia@gmail.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Spam-Status: No, score=-10.8 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: 1780697925524491169 X-GMAIL-MSGID: 1780697925524491169 I noticed we were missing optimizing `a / (1 << b)` when we know that a is nonnegative but only due to ranger information. This adds the use of the global ranger to tree_single_nonnegative_warnv_p for SSA_NAME. I didn't extend tree_single_nonnegative_warnv_p to use the ranger for floating point nor to use the local ranger since I am not 100% sure it is safe where all of the uses tree_expr_nonnegative_p would be safe. Note pr80776-1.c testcase fails again due to vrp's bad handling of setting global ranges from __builtin_unreachable. It just happened to be optimized before due to global ranges not being used as much. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/111959 gcc/ChangeLog: * fold-const.cc (tree_single_nonnegative_warnv_p): Use the global range to see if the SSA_NAME was nonnegative. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/forwprop-42.c: New test. * gcc.dg/pr80776-1.c: xfail and update comment. --- gcc/fold-const.cc | 36 +++++++++++++++------ gcc/testsuite/gcc.dg/pr80776-1.c | 8 ++--- gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c | 15 +++++++++ 3 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 40767736389..2a2a90230f5 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -15047,15 +15047,33 @@ tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth) return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2)); case SSA_NAME: - /* Limit the depth of recursion to avoid quadratic behavior. - This is expected to catch almost all occurrences in practice. - If this code misses important cases that unbounded recursion - would not, passes that need this information could be revised - to provide it through dataflow propagation. */ - return (!name_registered_for_update_p (t) - && depth < param_max_ssa_name_query_depth - && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t), - strict_overflow_p, depth)); + { + /* For integral types, querry the global range if possible. */ + if (INTEGRAL_TYPE_P (TREE_TYPE (t))) + { + value_range vr; + if (get_global_range_query ()->range_of_expr (vr, t) + && !vr.varying_p () && !vr.undefined_p ()) + { + /* If the range is nonnegative, return true. */ + if (vr.nonnegative_p ()) + return true; + + /* If the range is non-positive, then return false. */ + if (vr.nonpositive_p ()) + return false; + } + } + /* Limit the depth of recursion to avoid quadratic behavior. + This is expected to catch almost all occurrences in practice. + If this code misses important cases that unbounded recursion + would not, passes that need this information could be revised + to provide it through dataflow propagation. */ + return (!name_registered_for_update_p (t) + && depth < param_max_ssa_name_query_depth + && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t), + strict_overflow_p, depth)); + } default: return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t)); diff --git a/gcc/testsuite/gcc.dg/pr80776-1.c b/gcc/testsuite/gcc.dg/pr80776-1.c index b9bce62d982..f3d47aeda36 100644 --- a/gcc/testsuite/gcc.dg/pr80776-1.c +++ b/gcc/testsuite/gcc.dg/pr80776-1.c @@ -18,14 +18,14 @@ Foo (void) if (! (0 <= i && i <= 999999)) __builtin_unreachable (); - /* Legacy evrp sets the range of i to [0, MAX] *before* the first conditional, + /* vrp1 sets the range of i to [0, MAX] *before* the first conditional, and to [0,999999] *before* the second conditional. This is because both - evrp and VRP use trickery to set global ranges when this particular use of + vrp use trickery to set global ranges when this particular use of a __builtin_unreachable is in play (see uses of assert_unreachable_fallthru_edge_p). - Setting these ranges at the definition site, causes VRP to remove the + Setting these ranges at the definition site, causes other passes to remove the unreachable code altogether, leaving the following sprintf unguarded. This causes the bogus warning below. */ - sprintf (number, "%d", i); /* { dg-bogus "writing" "" } */ + sprintf (number, "%d", i); /* { dg-bogus "writing" "" { xfail *-*-* } } */ } diff --git a/gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c new file mode 100644 index 00000000000..4e5421ed4d4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/forwprop-42.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optimization/111959 */ + +int divbypow2(int a, int b) +{ + if (a & ~0xff) __builtin_unreachable(); + return a / (1<> " 1 "optimized" } } */ +