From patchwork Fri Jul 2 15:36:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrew MacLeod X-Patchwork-Id: 1500171 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=KHlOzTCF; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GGfNB2MpVz9sV8 for ; Sat, 3 Jul 2021 01:37:29 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 85FFF398381A for ; Fri, 2 Jul 2021 15:37:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 85FFF398381A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1625240244; bh=ZPmCE1GMhMY8QvuaUnPAOkifmOAeWXO8h1pZsiTH9Qc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=KHlOzTCFLY2CqZFHwkKRmKbnhHOz6n6qL/IJ3XYJAlhGhxRzYF7FxcKuknA18zew5 pX0SPMHe94NBE72Ok4YuVxve1qMBIuiQrpQ+wnrygtfDhsEqnfJHvbjCPVbD7ye9E/ w47GlWq8V5sztp/fGuOIQGPP4f4nAqI28lAUonoo= 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 ESMTP id C76413854807 for ; Fri, 2 Jul 2021 15:36:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C76413854807 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-259-EpaiJI6UMr27XAIwO4z45A-1; Fri, 02 Jul 2021 11:36:38 -0400 X-MC-Unique: EpaiJI6UMr27XAIwO4z45A-1 Received: by mail-qv1-f69.google.com with SMTP id d11-20020a0cf6cb0000b029028486d617faso6486794qvo.19 for ; Fri, 02 Jul 2021 08:36:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:cc:from:subject:message-id:date:user-agent :mime-version:content-language; bh=ZPmCE1GMhMY8QvuaUnPAOkifmOAeWXO8h1pZsiTH9Qc=; b=ikMuTSQkuzMTF4UvfN5OBm04eup9Y6118BCm4RkEG+CEVftkxaHJQEotDJoje0JeCX pnzfBFi8jOXPhszPArmrfqgHynIORLTB6M4nHxDiLPs3v9CTpYnLkTcbT1d/zQoI6vsR bKJZGls0SfAReM1TcA5ZFifnQuSPLCYnyTP6jUTF6LoAaEs6B6rWI6FuCa9sxDyoZrUz eh+bN9jpkK/CTEhvWOedMcaxXZp7oQJuR9ocARnvvYEJt0b+XbqlvnEpdoJMDl8T++ZW cl/ybdaOFhAF3Mr+Kgrp3djeZYApgz7oo5QGrAafVc99b/6cHy1UINvI6COf08w9ZMg0 EsJA== X-Gm-Message-State: AOAM532uaFFp4ZoEvbJrvL+nlXN5doRMrvGhttW7WBaHTuOaa9KSbdXT epQfRdArQCHmg4j0ZmSGzqaNFSlVzqtGoY3RXVFHg++dqUvLlSWpEt+V2AL+zDkeNb+4vHWSJ6Z +LsY7ZIf7A6yR7xuphA== X-Received: by 2002:ad4:4345:: with SMTP id q5mr107198qvs.27.1625240198382; Fri, 02 Jul 2021 08:36:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxNalXVh6k5TwAeawg4NY0u0Ntw0TY4j3u39ayxwW4qZXBlxpwxxH/FDEwKYSF3OF6Ebg0+yw== X-Received: by 2002:ad4:4345:: with SMTP id q5mr107185qvs.27.1625240198215; Fri, 02 Jul 2021 08:36:38 -0700 (PDT) Received: from [192.168.0.102] ([104.219.121.150]) by smtp.gmail.com with ESMTPSA id k12sm1302839qtx.58.2021.07.02.08.36.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 Jul 2021 08:36:25 -0700 (PDT) To: gcc-patches Subject: [COMMITTED] tree-optimization/101223 - Fix build_gt and build_lt for signed 1 bit values. Message-ID: <9bcaf476-156c-84b3-6dea-b4788e9db2cc@redhat.com> Date: Fri, 2 Jul 2021 11:36:14 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.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.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" These 2 routines were adding and subtracting 1 to a range bound, and checking for overflow. Signed 1 bit values have a range of [-1, 0]. Adding or subtracting 1 cannot be properly represented resulting in the overflow being set.  This caused us to set UNDEFINED when we shouldn't. This patch changes it such that signed values always have -1 added or subtracted as appropriate, allowing for proper representation and overflow setting. Bootstrapped on x86_64-pc-linux-gnu with no regressions.  Pushed. Andrew commit 84f7bab89279ca1234fef88929c74caeda8cb55e Author: Andrew MacLeod Date: Wed Jun 30 14:15:53 2021 -0400 Fix build_gt and build_lt for signed 1 bit values. Signed 1 bit values have a range of [-1, 0] but neither (0 - 1) nor (-1 + 1) can be represented. For signed values, add or subtract -1 as appropriate. PR tree-optimization/101223 gcc/ * range-op.cc (build_lt): Add -1 for signed values. (built_gt): Subtract -1 for signed values. gcc/testsuite/ * gcc.dg/pr101223.c: New. diff --git a/gcc/range-op.cc b/gcc/range-op.cc index 97b9843e095..f8e4c6d4e49 100644 --- a/gcc/range-op.cc +++ b/gcc/range-op.cc @@ -687,7 +687,14 @@ static void build_lt (irange &r, tree type, const wide_int &val) { wi::overflow_type ov; - wide_int lim = wi::sub (val, 1, TYPE_SIGN (type), &ov); + wide_int lim; + signop sgn = TYPE_SIGN (type); + + // Signed 1 bit cannot represent 1 for subtraction. + if (sgn == SIGNED) + lim = wi::add (val, -1, sgn, &ov); + else + lim = wi::sub (val, 1, sgn, &ov); // If val - 1 underflows, check if X < MIN, which is an empty range. if (ov) @@ -710,7 +717,14 @@ static void build_gt (irange &r, tree type, const wide_int &val) { wi::overflow_type ov; - wide_int lim = wi::add (val, 1, TYPE_SIGN (type), &ov); + wide_int lim; + signop sgn = TYPE_SIGN (type); + + // Signed 1 bit cannot represent 1 for addition. + if (sgn == SIGNED) + lim = wi::sub (val, -1, sgn, &ov); + else + lim = wi::add (val, 1, sgn, &ov); // If val + 1 overflows, check is for X > MAX, which is an empty range. if (ov) r.set_undefined (); diff --git a/gcc/testsuite/gcc.dg/pr101223.c b/gcc/testsuite/gcc.dg/pr101223.c new file mode 100644 index 00000000000..6d5a247fa6c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr101223.c @@ -0,0 +1,44 @@ +/* PR tree-optimization/101223 */ +/* { dg-do run } */ +/* { dg-options "-O2 " } */ + +struct { + int a : 1; +} b; +int c = 1, d; +int foo1() { + for (; d < 2; d++) { + int e = ~c, f = 0, g; + if (e) { + f = c; + g = b.a; + b.a = f; + if (b.a >= g) + __builtin_abort(); + } + c = f; + b.a = g; + } + return 0; +} + +int foo2() { + for (; d < 2; d++) { + int e = ~c, f = 0, g; + if (e) { + f = c; + g = b.a; + b.a = f; + if (g <= b.a) + __builtin_abort(); + } + c = f; + b.a = g; + } + return 0; +} +int main () +{ + return foo1() + foo2(); +} +