From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 110674 invoked by alias); 16 Apr 2019 19:05:21 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 110666 invoked by uid 89); 16 Apr 2019 19:05:20 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.1 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=bright, decisions X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 16 Apr 2019 19:05:19 +0000 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6A7383199394; Tue, 16 Apr 2019 19:05:08 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.40.205.45]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EC2F419C67; Tue, 16 Apr 2019 19:05:07 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id x3GJ55q8003460; Tue, 16 Apr 2019 21:05:05 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id x3GJ5168003459; Tue, 16 Apr 2019 21:05:01 +0200 Date: Tue, 16 Apr 2019 19:08:00 -0000 From: Jakub Jelinek To: Eric Botcazou , Richard Biener , Jeff Law Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix __builtin_*mul*_overflow* expansion (PR middle-end/90095, take 2) Message-ID: <20190416190501.GM21066@tucnak> Reply-To: Jakub Jelinek References: <20190416075713.GZ21066@tucnak> <1688157.qcEDEAZ5tB@polaris> <20190416130210.GF21066@tucnak> <9046197.GW4lqKc94u@polaris> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9046197.GW4lqKc94u@polaris> User-Agent: Mutt/1.11.3 (2019-02-01) X-IsSubscribed: yes X-SW-Source: 2019-04/txt/msg00666.txt.bz2 On Tue, Apr 16, 2019 at 06:21:25PM +0200, Eric Botcazou wrote: > > The runtime check assures that at runtime, the upper 32 bits of pseudo 104 > > must be always 0 (in this case, in some other case could be sign bit > > copies). > > OK, as Richard pointed out, that's not sufficient if we allow... > > > The question is if it would be valid say for forward propagation to first > > propagate (or combine) the pseudo 97 into the (subreg/s/v:SI (reg:DI 104) > > 0), then hoisting it before the jump_insn 16, have the subreg optimized > > away and miscompile later on. > > ...this to happen. So we could clear SUBREG_PROMOTED_VAR_P as soon as the > SUBREG is rewritten, but this looks quite fragile. The safest route is > probably not to use SUBREG_PROMOTED_VAR_P in this conditional context. > > > That means either that the hoisting pass is buggy, or that SUBREG_PROMOTED_* > > is only safe at the function boundary (function arguments and return value) > > and not elsewhere. > > I think that Richard's characterization is correct: > > "Note that likely SUBREG_PROMOTED_VAR_P wasn't designed to communicate > zero-extend info (can't you use a REG_EQUIV note somehow?) but it has > to be information that is valid everywhere in the function unless > data dependences force its motion (thus a conditional doesn't do)." > > i.e. this also works for a local variable that is always accessed with the > SUBREG_PROMOTED_VAR_P semantics. Ok, here is a patch that just removes all of that SUBREG_PROMOTED_SET then, as even for the opN_small_p we can't actually guarantee that for the whole function, only for where the pseudo with the SSA_NAME for which we get the range appears. On the bright side, the generated code at least for the particular testcase has somewhat different RA decisions, but isn't significantly worse. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2019-04-16 Jakub Jelinek PR middle-end/90095 * internal-fn.c (expand_mul_overflow): Don't set SUBREG_PROMOTED_VAR_P on lowpart SUBREGs. * gcc.dg/pr90095-1.c: New test. * gcc.dg/pr90095-2.c: New test. --- gcc/internal-fn.c.jj 2019-04-15 19:45:22.384444646 +0200 +++ gcc/internal-fn.c 2019-04-16 15:18:56.614708804 +0200 @@ -1753,22 +1753,9 @@ expand_mul_overflow (location_t loc, tre /* If both op0 and op1 are sign (!uns) or zero (uns) extended from hmode to mode, the multiplication will never overflow. We can do just one hmode x hmode => mode widening multiplication. */ - rtx lopart0s = lopart0, lopart1s = lopart1; - if (GET_CODE (lopart0) == SUBREG) - { - lopart0s = shallow_copy_rtx (lopart0); - SUBREG_PROMOTED_VAR_P (lopart0s) = 1; - SUBREG_PROMOTED_SET (lopart0s, uns ? SRP_UNSIGNED : SRP_SIGNED); - } - if (GET_CODE (lopart1) == SUBREG) - { - lopart1s = shallow_copy_rtx (lopart1); - SUBREG_PROMOTED_VAR_P (lopart1s) = 1; - SUBREG_PROMOTED_SET (lopart1s, uns ? SRP_UNSIGNED : SRP_SIGNED); - } tree halfstype = build_nonstandard_integer_type (hprec, uns); - ops.op0 = make_tree (halfstype, lopart0s); - ops.op1 = make_tree (halfstype, lopart1s); + ops.op0 = make_tree (halfstype, lopart0); + ops.op1 = make_tree (halfstype, lopart1); ops.code = WIDEN_MULT_EXPR; ops.type = type; rtx thisres --- gcc/testsuite/gcc.dg/pr90095-1.c.jj 2019-04-16 13:45:22.614772955 +0200 +++ gcc/testsuite/gcc.dg/pr90095-1.c 2019-04-16 13:45:22.614772955 +0200 @@ -0,0 +1,18 @@ +/* PR middle-end/90095 */ +/* { dg-do run } */ +/* { dg-options "-Os -fno-tree-bit-ccp" } */ + +unsigned long long a; +unsigned int b; + +int +main () +{ + unsigned int c = 255, d = c |= b; + if (__CHAR_BIT__ != 8 || __SIZEOF_INT__ != 4 || __SIZEOF_LONG_LONG__ != 8) + return 0; + d = __builtin_mul_overflow (-(unsigned long long) d, (unsigned char) - c, &a); + if (d != 0) + __builtin_abort (); + return 0; +} --- gcc/testsuite/gcc.dg/pr90095-2.c.jj 2019-04-16 15:20:14.728414325 +0200 +++ gcc/testsuite/gcc.dg/pr90095-2.c 2019-04-16 15:20:29.597167928 +0200 @@ -0,0 +1,5 @@ +/* PR middle-end/90095 */ +/* { dg-do run } */ +/* { dg-options "-Os -fno-tree-bit-ccp -fno-split-wide-types" } */ + +#include "pr90095-1.c" Jakub