From patchwork Thu Apr 4 20:10:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vladimir Makarov X-Patchwork-Id: 88052 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 C3A3E385DC3F for ; Thu, 4 Apr 2024 20:10:55 +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 333F13858C98 for ; Thu, 4 Apr 2024 20:10:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 333F13858C98 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 333F13858C98 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=1712261423; cv=none; b=KpNrPBD5m2BBxMGwKH3R9zFgmTNmab3l5ao4+cWGvkLOPoizVzkyKRMC6ttV8HYMAx3SH3eEZo+XwXDGKDXnnJ/IjSd1BVaiam0FunZ5fhZpXRQ23L/tjRmgv6sS2jEYrcx0yAm/q9L2XDF4Ic1PUzxfNgpIR/EaYZ883m8dqEw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712261423; c=relaxed/simple; bh=ghJ3a+eUN2/zEadg0E8kwUdlB6KwP/b6vmTp/dqEhQc=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=NEJiXoieDfMrFnJM0sO55nDJmjhdVuHC2CxoZoUhNmoRZkuaWZVlQumei6+iY4AQCrSyDf4VLpWPfLJupsZ3f4RNNBfW+obdDtsD+TQWer7KjEq82bHAfptC6A9Z5SRsJ7IxN42BxJAPMXfziYpeqYme4uwhvqtMIAO2hjnIT+I= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712261420; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=u1qmPeSqzop0coLdZ7yKRpVYQnJ9AxM5Nxy0KHy/Azo=; b=fo03tCsIDJcd0QcMldllzp/x9QG8+PNij965zkcsWkHO3z0wP/VPnH8WUQQ3llVB2MUqB5 gSdUTfxECRprFsE7eGBQDtIZtW2o4lx5FthrrF6EaReWvTDsEGw8oQjI3+Yyi9uZoJLZzC 4GamCG6RgSJGs1moDObRwuC917lrEtU= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-642-TLzZX1bOPXqUvSXOnvSJTA-1; Thu, 04 Apr 2024 16:10:17 -0400 X-MC-Unique: TLzZX1bOPXqUvSXOnvSJTA-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-4311e71a898so16491991cf.1 for ; Thu, 04 Apr 2024 13:10:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712261416; x=1712866216; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ts+hPidf4Ov1Vtbku+FVkoZSXhSM1IpOFvL//Xs/UEQ=; b=gwT4sc9UqZngeIhdD7kEIQplEA6odFNkdaDjhmDfNCDwAVCnaSdOw9/kzrFKggNI3l K4xYOMOjip9rhTEeNzCgWSSVi9aCFwtQhSeas0++674DGpm113dufY+3jO3d6RnnUcFP hr3Ql5yi+hoGRceibrK89Cc4sqAwtDZOaBelNnP7oTpGqHGafLuSVZ45CEqnCssNaqcl 2PMpPwEzbOgheDFUvtIisZ630b7r3ZKBYh94bxVC7Pt91G9lLOGkqAs7JRBUBfyNzNl7 vM6n+sy6RUcSDxWhTDrzKpgcSjXJxjK1qTBeDG8jFPnTkYnKCsJJDZs7DvmD80URVFhF r9uw== X-Gm-Message-State: AOJu0YwpHb5X7BFlIaLDbB/Eq27R5GrTjIewJupZxsdb7uWO04qCme+R SZBsrBfaklHVWmDZ6eVc+yrR2IqBBEBAeLheT3aMEw9cqC9Pjf+Jx/DfDlUyZWqh7ltPTsQ5yYn A1kvC/vKwpfFJ+wIx+5j+VJ53wSEBLrA6uSeaLLFX57BBAJ04dbORIiSrPkKOskevvjjNgcHlsx OBEsnbdYrEUv5DDaXh+vs76Up2ooN7TwMxw2BxJVY= X-Received: by 2002:a05:622a:1996:b0:432:bb20:a40b with SMTP id u22-20020a05622a199600b00432bb20a40bmr748528qtc.62.1712261416541; Thu, 04 Apr 2024 13:10:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGghZCvjmqKuzPF/qAyEVAQzbqKRl1c2esBFMoDVQb8lnbvnqcRAudbxW0/OJMN1HkAsL04hw== X-Received: by 2002:a05:622a:1996:b0:432:bb20:a40b with SMTP id u22-20020a05622a199600b00432bb20a40bmr748492qtc.62.1712261415670; Thu, 04 Apr 2024 13:10:15 -0700 (PDT) Received: from [192.168.1.88] (23-233-12-249.cpe.pppoe.ca. [23.233.12.249]) by smtp.gmail.com with ESMTPSA id ep12-20020a05622a548c00b004317c90d0d6sm33726qtb.65.2024.04.04.13.10.14 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Apr 2024 13:10:14 -0700 (PDT) Message-ID: Date: Thu, 4 Apr 2024 16:10:13 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: "gcc-patches@gcc.gnu.org" From: Vladimir Makarov Subject: [pushed][PR114415][scheduler]: Fixing wrong code generation X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, KAM_SHORT, LIKELY_SPAM_BODY, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, 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: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org The following patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114415 The patch was successfully tested and bootstrapped on x86_64, ppc64le, aarch64. commit fe305ca39930afc301cdd1f1143d540d1bfa2a48 Author: Vladimir N. Makarov Date: Thu Apr 4 16:04:04 2024 -0400 [PR114415][scheduler]: Fixing wrong code generation For the test case, the insn scheduler (working for live range shrinkage) moves insns modifying stack memory before an insn reserving the stack memory. Comments in the patch contains more details about the problem and its solution. gcc/ChangeLog: PR rtl-optimization/114415 * sched-deps.cc (add_insn_mem_dependence): Add memory check for mem argument. (sched_analyze_1): Treat stack pointer modification as memory read. (sched_analyze_2, sched_analyze_insn): Add memory guard for processing pending_read_mems. * sched-int.h (deps_desc): Add comment to pending_read_mems. gcc/testsuite/ChangeLog: PR rtl-optimization/114415 * gcc.target/i386/pr114415.c: New test. diff --git a/gcc/sched-deps.cc b/gcc/sched-deps.cc index 5034e664e5e..4c668245049 100644 --- a/gcc/sched-deps.cc +++ b/gcc/sched-deps.cc @@ -1735,7 +1735,7 @@ add_insn_mem_dependence (class deps_desc *deps, bool read_p, insn_node = alloc_INSN_LIST (insn, *insn_list); *insn_list = insn_node; - if (sched_deps_info->use_cselib) + if (sched_deps_info->use_cselib && MEM_P (mem)) { mem = shallow_copy_rtx (mem); XEXP (mem, 0) = cselib_subst_to_values_from_insn (XEXP (mem, 0), @@ -2458,6 +2458,25 @@ sched_analyze_1 (class deps_desc *deps, rtx x, rtx_insn *insn) FIRST_STACK_REG); } #endif + if (!deps->readonly && regno == STACK_POINTER_REGNUM) + { + /* Please see PR114115. We have insn modifying memory on the stack + and not addressed by stack pointer and we have insn reserving the + stack space. If we move the insn modifying memory before insn + reserving the stack space, we can change memory out of the red + zone. Even worse, some optimizations (e.g. peephole) can add + insns using temporary stack slots before insn reserving the stack + space but after the insn modifying memory. This will corrupt the + modified memory. Therefore we treat insn changing the stack as + reading unknown memory. This will create anti-dependence. We + don't need to treat the insn as writing memory because GCC by + itself does not generate code reading undefined stack memory. */ + if ((deps->pending_read_list_length + deps->pending_write_list_length) + >= param_max_pending_list_length + && !DEBUG_INSN_P (insn)) + flush_pending_lists (deps, insn, true, true); + add_insn_mem_dependence (deps, true, insn, dest); + } } else if (MEM_P (dest)) { @@ -2498,10 +2517,11 @@ sched_analyze_1 (class deps_desc *deps, rtx x, rtx_insn *insn) pending_mem = deps->pending_read_mems; while (pending) { - if (anti_dependence (pending_mem->element (), t) - && ! sched_insns_conditions_mutex_p (insn, pending->insn ())) - note_mem_dep (t, pending_mem->element (), pending->insn (), - DEP_ANTI); + rtx mem = pending_mem->element (); + if (REG_P (mem) + || (anti_dependence (mem, t) + && ! sched_insns_conditions_mutex_p (insn, pending->insn ()))) + note_mem_dep (t, mem, pending->insn (), DEP_ANTI); pending = pending->next (); pending_mem = pending_mem->next (); @@ -2637,12 +2657,10 @@ sched_analyze_2 (class deps_desc *deps, rtx x, rtx_insn *insn) pending_mem = deps->pending_read_mems; while (pending) { - if (read_dependence (pending_mem->element (), t) - && ! sched_insns_conditions_mutex_p (insn, - pending->insn ())) - note_mem_dep (t, pending_mem->element (), - pending->insn (), - DEP_ANTI); + rtx mem = pending_mem->element (); + if (MEM_P (mem) && read_dependence (mem, t) + && ! sched_insns_conditions_mutex_p (insn, pending->insn ())) + note_mem_dep (t, mem, pending->insn (), DEP_ANTI); pending = pending->next (); pending_mem = pending_mem->next (); @@ -3026,8 +3044,7 @@ sched_analyze_insn (class deps_desc *deps, rtx x, rtx_insn *insn) while (pending) { if (! sched_insns_conditions_mutex_p (insn, pending->insn ())) - add_dependence (insn, pending->insn (), - REG_DEP_OUTPUT); + add_dependence (insn, pending->insn (), REG_DEP_OUTPUT); pending = pending->next (); pending_mem = pending_mem->next (); } @@ -3036,10 +3053,10 @@ sched_analyze_insn (class deps_desc *deps, rtx x, rtx_insn *insn) pending_mem = deps->pending_read_mems; while (pending) { - if (MEM_VOLATILE_P (pending_mem->element ()) + rtx mem = pending_mem->element (); + if (MEM_P (mem) && MEM_VOLATILE_P (mem) && ! sched_insns_conditions_mutex_p (insn, pending->insn ())) - add_dependence (insn, pending->insn (), - REG_DEP_OUTPUT); + add_dependence (insn, pending->insn (), REG_DEP_OUTPUT); pending = pending->next (); pending_mem = pending_mem->next (); } diff --git a/gcc/sched-int.h b/gcc/sched-int.h index 4df092013e9..85b1ddb13ef 100644 --- a/gcc/sched-int.h +++ b/gcc/sched-int.h @@ -471,7 +471,9 @@ public: /* An INSN_LIST containing all insns with pending read operations. */ rtx_insn_list *pending_read_insns; - /* An EXPR_LIST containing all MEM rtx's which are pending reads. */ + /* An EXPR_LIST containing all MEM rtx's which are pending reads. The list + can contain stack pointer instead of memory. This is a special case (see + sched-deps.cc::sched_analyze_1). */ rtx_expr_list *pending_read_mems; /* An INSN_LIST containing all insns with pending write operations. */ diff --git a/gcc/testsuite/gcc.target/i386/pr114415.c b/gcc/testsuite/gcc.target/i386/pr114415.c new file mode 100644 index 00000000000..90c16a5128f --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr114415.c @@ -0,0 +1,47 @@ +/* { dg-do run { target int128 } } */ +/* { dg-options "-Oz -fno-dce -fno-forward-propagate -flive-range-shrinkage -fweb -Wno-psabi" } */ + +typedef unsigned V __attribute__((vector_size (64))); +typedef unsigned __int128 W __attribute__((vector_size (64))); +__int128 i; +V v; +W w; + +W +bar2 (V, W c) +{ + v |= (V){c[3]}; + i = 0; + return c; +} + +W +bar1 (W, W d) +{ + v[0] %= 2; + return d; +} + +W +bar0 (V a, W b) +{ + W t = bar2 ((V) { }, w); + (void)bar2 ((V){b[0] - a[0]}, (W){}); + b += bar1 (t, (W){1}); + a[5] &= __builtin_mul_overflow_p (a[0], i, 0u); + return (W) a + b; +} + +int +main () +{ + W x = bar0 ((V) { }, (W) { }); + if (x[0] != 1) + __builtin_abort(); + if (x[1]) + __builtin_abort(); + if (x[2]) + __builtin_abort(); + if (x[3]) + __builtin_abort(); +}