From patchwork Thu Apr 8 09:24:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1463699 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@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=FvxbwzzK; 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 4FGG6s0XPCz9sTD for ; Thu, 8 Apr 2021 19:24:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 470B33854833; Thu, 8 Apr 2021 09:24:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 470B33854833 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1617873857; bh=Htex1Jq8KdKxhjlBEuDQyvTMXClhHP7gJUE8kuZqK04=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=FvxbwzzKj3j9dmEu420KRf6ZiXJs2n8x3uyVVo9eIG8kYBG0ame0a4PawaqWuQwTi cUVhoBEluGDxG3/5pT2YwHHmo0STmzYpCfLh8gIRFsrtm/s7A5wtuStQekw2WlYhys Vu7mcmYhD++noeSaXyim4uUnc7LdFC0TBMtUiMiw= 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 [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id E4D443857C48 for ; Thu, 8 Apr 2021 09:24:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org E4D443857C48 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-528-uN8mbzh-NZ6bB-Ke0c2A3w-1; Thu, 08 Apr 2021 05:24:11 -0400 X-MC-Unique: uN8mbzh-NZ6bB-Ke0c2A3w-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E9E161020C22; Thu, 8 Apr 2021 09:24:09 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-35.ams2.redhat.com [10.36.112.35]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 83BC86268E; Thu, 8 Apr 2021 09:24:09 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.16.1/8.16.1) with ESMTPS id 1389O6uH3827567 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 8 Apr 2021 11:24:06 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.16.1/8.16.1/Submit) id 1389O4k33827566; Thu, 8 Apr 2021 11:24:04 +0200 Date: Thu, 8 Apr 2021 11:24:04 +0200 To: Segher Boessenkool Subject: [PATCH] combine: Fix up expand_compound_operation [PR99905] Message-ID: <20210408092404.GW1179226@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-5.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi! The following testcase is miscompiled on x86_64-linux. expand_compound_operation is called on (zero_extract:DI (mem/c:TI (reg/f:DI 16 argp) [3 i+0 S16 A128]) (const_int 16 [0x10]) (const_int 63 [0x3f])) so mode is DImode, inner_mode is TImode, pos 63, len 16 and modewidth 64. A couple of lines above the problematic spot we have: if (modewidth >= pos + len) { tem = gen_lowpart (mode, XEXP (x, 0)); where the code uses gen_lowpart and then shift left/right to extract it in mode. But the guarding condition is false - 64 >= 63 + 16 and so we enter the next condition, where the code shifts XEXP (x, 0) right by pos and then adds AND. It does so incorrectly though. Given the modewidth < pos + len, inner_mode must be necessarily larger than mode and XEXP (x, 0) has the innermode, but it was calling simplify_shift_const with mode rather than inner_mode, which meant inconsistent arguments to simplify_shift_const and in this case made a DImode MEM shift out of it. The following patch fixes it, by doing the shift in inner_mode properly and then after the shift doing the lowpart subreg and masking already in mode. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2021-04-08 Jakub Jelinek PR rtl-optimization/99905 * combine.c (expand_compound_operation): If pos + len > modewidth, perform the right shift by pos in inner_mode and then convert to mode, instead of trying to simplify a shift of rtx with inner_mode by pos as if it was a shift in mode. * gcc.target/i386/pr99905.c: New test. Jakub --- gcc/combine.c.jj 2021-01-04 10:25:39.127230495 +0100 +++ gcc/combine.c 2021-04-07 11:33:13.442626682 +0200 @@ -7409,11 +7409,15 @@ expand_compound_operation (rtx x) mode, tem, modewidth - len); } else if (unsignedp && len < HOST_BITS_PER_WIDE_INT) - tem = simplify_and_const_int (NULL_RTX, mode, - simplify_shift_const (NULL_RTX, LSHIFTRT, - mode, XEXP (x, 0), - pos), - (HOST_WIDE_INT_1U << len) - 1); + { + tem = simplify_shift_const (NULL_RTX, LSHIFTRT, inner_mode, + XEXP (x, 0), pos); + tem = gen_lowpart (mode, tem); + if (!tem || GET_CODE (tem) == CLOBBER) + return x; + tem = simplify_and_const_int (NULL_RTX, mode, tem, + (HOST_WIDE_INT_1U << len) - 1); + } else /* Any other cases we can't handle. */ return x; --- gcc/testsuite/gcc.target/i386/pr99905.c.jj 2021-04-07 11:42:40.954292535 +0200 +++ gcc/testsuite/gcc.target/i386/pr99905.c 2021-04-07 11:42:06.659675315 +0200 @@ -0,0 +1,33 @@ +/* PR rtl-optimization/99905 */ +/* { dg-do run { target int128 } } */ +/* { dg-options "-Os -mno-mmx -mno-sse" } */ + +typedef unsigned char U; +typedef unsigned char __attribute__((__vector_size__ (8))) A; +typedef unsigned char __attribute__((__vector_size__ (16))) B; +typedef unsigned char __attribute__((__vector_size__ (32))) C; +typedef unsigned int __attribute__((__vector_size__ (8))) D; +typedef unsigned long long __attribute__((__vector_size__ (8))) E; +typedef unsigned __int128 I; +typedef unsigned long long L; + +D gv; +I gi; + +L __attribute__((__noipa__)) +foo (int ua, int ub, int uc, int ud, E ue, I i) +{ + D d = (U) __builtin_bswap16 (i >> 63) + gv; + B y = ((union { C a; B b[2];}) (C){ }).b[0] + (B) gi; + A z = ((union { B a; A b[2];}) y).b[0] + (A) d; + return (L)z; +} + +int +main () +{ + L x = foo (0, 0, 0, 0, (E) { }, (I) 0x100 << 63); + if (x != 0x100000001) + __builtin_abort (); + return 0; +}