From patchwork Sat Apr 6 16:58:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1920431 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=apEq7GUU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VBhQn10w7z1yYt for ; Sun, 7 Apr 2024 02:59:11 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 124DA3858433 for ; Sat, 6 Apr 2024 16:59:09 +0000 (GMT) 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 [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4F1773858D33 for ; Sat, 6 Apr 2024 16:58:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4F1773858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4F1773858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712422734; cv=none; b=YAbJa8tBJSjWXe9rQ8iwtHgBOEXOLIb7QAgCQaQ09mqzjohCqCRPg+0JUqrhdWm3k1jROOCVITLu6NMgOle3KhoC/G/GiLY2YoTrsrVameoxrvf7Vj6ZlVeeFV2vZGr/wEIiM8iWYdO6T6c08HM6hfrt3RAOFGwjomIQGl5fF88= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712422734; c=relaxed/simple; bh=HKMoT6UjLbxu5nTHg1b9brjT3+iF6hryuMT2nCLusSE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=J2iyP1TIG5QFTDnB0GOQzN+Xvd8l3uUJcSOGko3P7ozAcw+dVy+slU5oJ+qk3ZKMyGTpK9o5Hv//HB2x9ygmyqX485K6z/5UMYxF+FDiFDGoregMwhNsg0yNs8+ypvIbeAwvgdpYQYXBmTZ5YnK64A/hI2abx7epmOJ+OMePDzA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712422731; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=bokTCuVWCCpi3stH8Rt2Bys8OiLH+/tBjlqh+IIIoZE=; b=apEq7GUUluhKrw+WAnGvlr1gltgc4SD67MYa6zFdOXRBeJp16eyU1kxUN5/SwmHTp+Yam9 ctmGtb2dvL1fxUi4YbwacZoh3/3+5kkGRHjCSrq6aZjbfJHKuPh5x69NW9bG+DX6lK6oJ6 4XrIUYO0lAK2r+ucTVCg9Zazh3te8OM= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-610-8fECx6RPPTqw9vjK1mzjgw-1; Sat, 06 Apr 2024 12:58:48 -0400 X-MC-Unique: 8fECx6RPPTqw9vjK1mzjgw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3A492285F9A3; Sat, 6 Apr 2024 16:58:48 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.14]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D361917A91; Sat, 6 Apr 2024 16:58:47 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 436Gwgrb2576701 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 6 Apr 2024 18:58:42 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 436GwfcS2576700; Sat, 6 Apr 2024 18:58:41 +0200 Date: Sat, 6 Apr 2024 18:58:41 +0200 From: Jakub Jelinek To: Andreas Krebbel Cc: gcc-patches@gcc.gnu.org, Ilya Leoshkevich Subject: [PATCH] s390: Fix s390_const_int_pool_entry_p and movdi peephole2 [PR114605] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The following testcase is miscompiled, because we have initially a movti which loads the 0x3f8000003f800000ULL TImode constant from constant pool. Later on we split it into a pair of DImode loads. Now, for the first load (why just that?, though not stage4 material) we trigger the peephole2 which uses s390_const_int_pool_entry_p. That function doesn't check at all the constant pool mode though, sees the constant pool at that address has a CONST_INT value and just assumes that is the value to return, which is especially wrong for big-endian, if it is a DImode load from offset 0, it should be loading 0 rather than 0x3f8000003f800000ULL. The following patch adds checks if we are extracing a MODE_INT mode, if the constant pool has MODE_INT mode as well, punts if constant pool has smaller mode size than the extraction one (then it would be UB), if it has the same mode as before keeps using what it did before, if constant pool has a larger mode than the one being extracted, uses simplify_subreg. I'd have used avoid_constant_pool_reference instead which can handle also offsets into the constant pool constants, but it can't handle UNSPEC_LTREF. Another thing is that once that is fixed, we ICE when we extract constant like 0, ior insn predicate require non-0 constant. So, the patch also fixes the peephole2 so that if either 32-bit half is zero, it uses a mere load of the constant into register rather than a pair of such load and ior. Bootstrapped/regtested on s390x-linux, ok for trunk? 2024-04-06 Jakub Jelinek PR target/114605 * config/s390/s390.cc (s390_const_int_pool_entry_p): Punt if mem doesn't have MODE_INT mode, or pool constant doesn't have MODE_INT mode, or if pool constant mode is smaller than mem mode. If mem mode is different from pool constant mode, try to simplify subreg. If that doesn't work, punt, if it does, use the simplified constant instead of the constant pool constant. * config/s390/s390.md (movdi from const pool peephole): If either low or high 32-bit part is zero, just emit move insn instead of move + ior. * gcc.dg/pr114605.c: New test. Jakub --- gcc/config/s390/s390.cc.jj 2024-03-14 14:07:34.088426911 +0100 +++ gcc/config/s390/s390.cc 2024-04-05 15:58:57.757057420 +0200 @@ -9984,7 +9984,7 @@ s390_const_int_pool_entry_p (rtx mem, HO - (mem (unspec [(symbol_ref) (reg)] UNSPEC_LTREF)). - (mem (symbol_ref)). */ - if (!MEM_P (mem)) + if (!MEM_P (mem) || GET_MODE_CLASS (GET_MODE (mem)) != MODE_INT) return false; rtx addr = XEXP (mem, 0); @@ -9998,9 +9998,19 @@ s390_const_int_pool_entry_p (rtx mem, HO return false; rtx val_rtx = get_pool_constant (sym); - if (!CONST_INT_P (val_rtx)) + machine_mode mode = get_pool_mode (sym); + if (!CONST_INT_P (val_rtx) + || GET_MODE_CLASS (mode) != MODE_INT + || GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (mem))) return false; + if (mode != GET_MODE (mem)) + { + val_rtx = simplify_subreg (GET_MODE (mem), val_rtx, mode, 0); + if (val_rtx == NULL_RTX || !CONST_INT_P (val_rtx)) + return false; + } + if (val != nullptr) *val = INTVAL (val_rtx); return true; --- gcc/config/s390/s390.md.jj 2024-01-03 11:51:54.638410489 +0100 +++ gcc/config/s390/s390.md 2024-04-05 16:17:17.322234553 +0200 @@ -2152,6 +2152,16 @@ (define_peephole2 gcc_assert (ok); operands[2] = GEN_INT (val & 0xFFFFFFFF00000000ULL); operands[3] = GEN_INT (val & 0x00000000FFFFFFFFULL); + if (operands[2] == const0_rtx) + { + emit_move_insn (operands[0], operands[3]); + DONE; + } + else if (operands[3] == const0_rtx) + { + emit_move_insn (operands[0], operands[2]); + DONE; + } }) ; --- gcc/testsuite/gcc.dg/pr114605.c.jj 2024-04-05 16:25:34.678505438 +0200 +++ gcc/testsuite/gcc.dg/pr114605.c 2024-04-05 16:25:10.388834268 +0200 @@ -0,0 +1,37 @@ +/* PR target/114605 */ +/* { dg-do run } */ +/* { dg-options "-O0" } */ + +typedef struct { const float *a; int b, c; float *d; } S; + +__attribute__((noipa)) void +bar (void) +{ +} + +__attribute__((noinline, optimize (2))) static void +foo (S *e) +{ + const float *f; + float *g; + float h[4] = { 0.0, 0.0, 1.0, 1.0 }; + if (!e->b) + f = h; + else + f = e->a; + g = &e->d[0]; + __builtin_memcpy (g, f, sizeof (float) * 4); + bar (); + if (!e->b) + if (g[0] != 0.0 || g[1] != 0.0 || g[2] != 1.0 || g[3] != 1.0) + __builtin_abort (); +} + +int +main () +{ + float d[4]; + S e = { .d = d }; + foo (&e); + return 0; +}