From patchwork Thu Nov 4 17:27:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 47063 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EE0773857C69 for ; Thu, 4 Nov 2021 17:28:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EE0773857C69 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1636046889; bh=b+ZZd4IXLxzE37NlcNCIgD6ZnKQlhJ4HhF8hzLpzdXI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=wuFSdRdcepjxaKtnH4h0kUo0drGvK3bl5AkiX43plehp1JVCXQVyTie26ofJkjg5W 6LO40GEcVcpqXTjjPUpkT7hrUlxqFWpugQkY5kmYrr2dAVNkqzKrl+p8IIKFpNvzql ppAIVTXtycbRWdt9ZeoH63cxNDic+dVz8GBTIS4M= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by sourceware.org (Postfix) with ESMTPS id 8E2B73857C6D for ; Thu, 4 Nov 2021 17:27:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8E2B73857C6D Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-297-d3fhBMXPNL-WMFC2wxuiUQ-1; Thu, 04 Nov 2021 13:27:28 -0400 X-MC-Unique: d3fhBMXPNL-WMFC2wxuiUQ-1 Received: by mail-qt1-f200.google.com with SMTP id e8-20020ac84e48000000b002acae45e8e8so2890797qtw.7 for ; Thu, 04 Nov 2021 10:27:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:to:cc:from:subject:message-id:date:user-agent :mime-version:content-language; bh=b+ZZd4IXLxzE37NlcNCIgD6ZnKQlhJ4HhF8hzLpzdXI=; b=Ff800yCb2jMWC4Haj81C2mX7QHx+1zxZp7eNIbzqbSmdvjCw6+w44nD3CUetjphY1H Vzu+3VpO9+4r7HJY06ZByQvdr8Xhz9OeeIg6479kgg7FLUuWEuYIweIuZJ38VP/QmHue KNc6mudmO47tJi7/3kGC27+VBWAhx3pz+GBoCIIHG/14PQURjDX8ZRo2Y6HqYwVHkAAv BPt5P7/eDJSvoijWhPKSALWv5D/iJlxAgu6a3+/o1lUIw83p5ZvjALBLIaPX9N4ahVPM SxVBE1ItZvBVqEVTpgV+byjYFrS7oKYMg1Y9TMvT7FvMd9w+1luQfMdBCIG7xG1dk1fk D7Rw== X-Gm-Message-State: AOAM530VKW+W/nLn0Q3Us0nkjCBTQ5n9wgECMUcZKU541bOFTmjA7Pgh urbje+Iy6SGOAGU8QLkCUAlG04SsWBYYgS0BHruN7T0E30RI1mhEo4Ak203wkPIEC0czfguJxqS 7m8b2WLTX/Zm51JIv2w== X-Received: by 2002:a05:622a:148d:: with SMTP id t13mr53956147qtx.393.1636046848126; Thu, 04 Nov 2021 10:27:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYhw5DSjqDQfBp5EfHYLY93ERew1/34FBNtbKEMxfFjVC0CBqfjAR/ZnilC05TNBQ2Pq0RGg== X-Received: by 2002:a05:622a:148d:: with SMTP id t13mr53956117qtx.393.1636046847842; Thu, 04 Nov 2021 10:27:27 -0700 (PDT) Received: from ?IPv6:2607:fea8:a262:5f00::e8d1? ([2607:fea8:a262:5f00::e8d1]) by smtp.gmail.com with ESMTPSA id t4sm4212129qkp.42.2021.11.04.10.27.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Nov 2021 10:27:27 -0700 (PDT) To: gcc-patches Subject: [COMMITTED] PR tree-optimization/103079 - Treat undefined operands as varying in GORI. Message-ID: <6633332e-d379-99fd-f98b-83b9b52bd003@redhat.com> Date: Thu, 4 Nov 2021 13:27:25 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-CA X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, BODY_8BITS, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew MacLeod via Gcc-patches From: Andrew MacLeod Reply-To: Andrew MacLeod Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Outgoing range calculations were assuming an undefined operand produces an undefined result.  This is not true, as in the testcxase:   [0, 0]  = UNDEFINED | b_9     tells us that b_9 is [0,0] and the rest of the time, we only know its varying.    Returning UNDEFINED for b_9 is clearly wrong. This patch turns any UNDEFINED values for op1 and op2 into VARYING, and then invokes the calcualtion, so instead, we';ll know see:  [0, 0] = VARYING | b_9   which the solvers will correctly pick the right range. Furthermore, we were always returning UNDEFINED when the LHS is undefined... ie. UNDEFINED = 1 | b_9   was returning UNDEFINED for b_9...  even tho the LHS is undefined, it seems to be asking for trouble to continue calculating an outgoing range for b_9, so instead we just stop looking now. Bootstrapped on x86_64-pc-linux-gnu with no regressions.  Pushed. Andrew commit 004afb984beb6efbe25f44a5857b1c27ebc2ec82 Author: Andrew MacLeod Date: Thu Nov 4 11:07:28 2021 -0400 Treat undefined operands as varying in GORI. If the LHS is UNDEFINED simply stop calculating. Treat op1 and op2 as VARYING if they are UNDEFINED. PR tree-optimization/103079 gcc/ * gimple-range-gori.cc (gimple_range_calc_op1): Treat undefined as varying. (gimple_range_calc_op2): Ditto. gcc/testsuite/ * gcc.dg/pr103079.c: New. diff --git a/gcc/gimple-range-gori.cc b/gcc/gimple-range-gori.cc index 4e45c593871..2e58c23216b 100644 --- a/gcc/gimple-range-gori.cc +++ b/gcc/gimple-range-gori.cc @@ -37,16 +37,13 @@ bool gimple_range_calc_op1 (irange &r, const gimple *stmt, const irange &lhs_range) { gcc_checking_assert (gimple_num_ops (stmt) < 3); - - // An empty range is viral. - tree type = TREE_TYPE (gimple_range_operand1 (stmt)); + // Give up on empty ranges. if (lhs_range.undefined_p ()) - { - r.set_undefined (); - return true; - } + return false; + // Unary operations require the type of the first operand in the // second range position. + tree type = TREE_TYPE (gimple_range_operand1 (stmt)); int_range<2> type_range (type); return gimple_range_handler (stmt)->op1_range (r, type, lhs_range, type_range); @@ -61,15 +58,23 @@ bool gimple_range_calc_op1 (irange &r, const gimple *stmt, const irange &lhs_range, const irange &op2_range) { + // Give up on empty ranges. + if (lhs_range.undefined_p ()) + return false; + // Unary operation are allowed to pass a range in for second operand // as there are often additional restrictions beyond the type which // can be imposed. See operator_cast::op1_range(). tree type = TREE_TYPE (gimple_range_operand1 (stmt)); - // An empty range is viral. - if (op2_range.undefined_p () || lhs_range.undefined_p ()) + // If op2 is undefined, solve as if it is varying. + if (op2_range.undefined_p ()) { - r.set_undefined (); - return true; + // This is sometimes invoked on single operand stmts. + if (gimple_num_ops (stmt) < 3) + return false; + int_range<2> trange (TREE_TYPE (gimple_range_operand2 (stmt))); + return gimple_range_handler (stmt)->op1_range (r, type, lhs_range, + trange); } return gimple_range_handler (stmt)->op1_range (r, type, lhs_range, op2_range); @@ -84,12 +89,17 @@ bool gimple_range_calc_op2 (irange &r, const gimple *stmt, const irange &lhs_range, const irange &op1_range) { + // Give up on empty ranges. + if (lhs_range.undefined_p ()) + return false; + tree type = TREE_TYPE (gimple_range_operand2 (stmt)); - // An empty range is viral. - if (op1_range.undefined_p () || lhs_range.undefined_p ()) + // If op1 is undefined, solve as if it is varying. + if (op1_range.undefined_p ()) { - r.set_undefined (); - return true; + int_range<2> trange (TREE_TYPE (gimple_range_operand1 (stmt))); + return gimple_range_handler (stmt)->op2_range (r, type, lhs_range, + trange); } return gimple_range_handler (stmt)->op2_range (r, type, lhs_range, op1_range); diff --git a/gcc/testsuite/gcc.dg/pr103079.c b/gcc/testsuite/gcc.dg/pr103079.c new file mode 100644 index 00000000000..7f6632fc669 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr103079.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-Os -fdump-tree-vrp2" } */ + +int a, b = -2; +int main() { + int d = 0; + int t; + if (b) + goto t1; + if (t) { +t1: + if (!a) + d = b; + while (d > -1) + ; + } + return 0; +} +/* { dg-final { scan-tree-dump "PHI" "vrp2" } } */ +