From patchwork Wed Feb 12 12:14:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uros Bizjak X-Patchwork-Id: 106372 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 A12F53858C31 for ; Wed, 12 Feb 2025 12:15:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A12F53858C31 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=Nb1YV9BM X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by sourceware.org (Postfix) with ESMTPS id 9498C3858C53 for ; Wed, 12 Feb 2025 12:14:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9498C3858C53 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9498C3858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::12e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1739362484; cv=none; b=V/Y+uVvTIVj8S5UrARpTjfMvqrvJIqILv9n7XIr9DtcLFdnK0Wfn1jpHR4JBoN5aZ2/s+Ah0ToUrfrG/woj+SW06wR2vpUuDIhyQOJeItc2cs2mxDHG/2+ZuYaIVKu1Fw8NtQxSx0gG1OYeaBiLV+H33hOI61sQBHvuONwj2uVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1739362484; c=relaxed/simple; bh=URCV3ioz0xRJ3kgSXmqL5s6wfIiQ+q90sYtSlOSo9VY=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=Ya5MytZ39G73hjhIkPxOqZAlFhRzl4Wm65K8QegbwSuqVZO3l7NmpRVWTtGITc5KF52QSfb/s6LvxugW4MjCnBxqCs3WxHJUuxjnN0prK+MhtFuJpc0n4qEN+tXtkKjk3cTuVmSS5sFEYtzBcPx2CVcw6cOE1XJKPmqXqhVrRPQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9498C3858C53 Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-545039b6a67so4107496e87.0 for ; Wed, 12 Feb 2025 04:14:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1739362482; x=1739967282; darn=gcc.gnu.org; h=cc:to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=IhVBNEVZDoIXE3TKi8QjYxvD5w+FxSxuSrHzmaMBz7o=; b=Nb1YV9BMs6BKefdcx3SCkFJZs1wgCNug6ZJnjeaaUlg5lmbZmqz1knxT0ojSSB2+a4 WGyM5md5Ucs0pQlj03JTXhQb5lwFTx840WXP8as1nKc64mk/+7d4Qw0VdwIOQ9vIL8w4 eu6e3HxLuGOPVShm9rw4LNZF4NAMCUFEI4J5PUKcZYfJowFKrgkELyeHVOQKh9Pex/Fk zoujbPcQZ/nCsNT1Jw6wWc3ik2H1sGWJBUOeLC+h7DknQJXUWDG5OWRFPvmvvrf5nKAU 1Gc8xQ1Mn+AoYcMvoM+4EbuvnPo+8Ha+n3WTpsy3PyQtqPNY6IugpCQa5xeHGABrOTqw F+cQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739362482; x=1739967282; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=IhVBNEVZDoIXE3TKi8QjYxvD5w+FxSxuSrHzmaMBz7o=; b=MWz2OWVRjYo9DG9I6TujbwGnLlmAy6/T9Qc9EZBZAExm1s5+ikbymchIGKYse4H4mw IwhnYt5IntJ3ixu7F/UvUbOz1zsKhNCjxZCs3g2IT0EeeJiNCwWxwR7XEfFE6butnj6f uMQyIGUclS11hZNFhu/t2/95u5xJnZl7NgJ4Yewe8+VMwOKmOIK9PIFMK5UlOxcPNh+3 DUIDHagwsbWWDjWGeNU+81n1Xfro1g9B0EAd7Z0i0nM4noC94jCGUnO8ovL1YAjK0QRh nS2JQ93dD0U8gFHhLsKxdyA+YyOHiHB/cjvqP95bnK/0WX/4zRZ+Z4V/GJ3brz2Imkb3 EYVg== X-Gm-Message-State: AOJu0Yx75b4GtHz370n2s6m4v1ByPWGHH2ej0pX0uqvV5tVpdql10lOw fsbt8c8sedL18iAolfWxMjK1BbPfHAr6QllmVYn2jpi24yJpcL5v153NZZaJlRPJwszInXXZjQn f0/Nht1FIs9soyEYScqOd9ST9spKtgdbo0LI= X-Gm-Gg: ASbGnctZHxYkSwDKX/SOxsoLfSTCfTu1Cz5HkF2Bd50hZ+ttXimWBLmAB24WI4j1M28 LqNBcM+e48H7PvKPgd2e2mty61BP+zGuN5qNRHrmUOcZwR/2+v+jF8IX3J4LRINtLy6kZH9aO X-Google-Smtp-Source: AGHT+IE02LI/nNaYNrQ2gRGb5Up7LEHXZqjjT/RKqCcmo3mppggn/0HXPtwU4N0Odu1x4E7k0uBEujAcji9Jt9iXmfM= X-Received: by 2002:ac2:4e91:0:b0:53e:fa8b:822e with SMTP id 2adb3069b0e04-545181893d7mr704464e87.37.1739362481678; Wed, 12 Feb 2025 04:14:41 -0800 (PST) MIME-Version: 1.0 From: Uros Bizjak Date: Wed, 12 Feb 2025 13:14:28 +0100 X-Gm-Features: AWEUYZktRxv0N2huat3vdQAQsgNzOsOlg70bRktlXQ0XCVoGoLs5Rj-V3sIa-bA Message-ID: Subject: [PATCH] combine: Discard REG_UNUSED note in i2 when register is also referenced in i3 [PR118739] To: "gcc-patches@gcc.gnu.org" Cc: Steven Bosscher , Richard Biener , Jakub Jelinek X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, 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 combine pass is trying to combine: Trying 16, 22, 21 -> 23: 16: r104:QI=flags:CCNO>0 22: {r120:QI=r104:QI^0x1;clobber flags:CC;} REG_UNUSED flags:CC 21: r119:QI=flags:CCNO<=0 REG_DEAD flags:CCNO 23: {r110:QI=r119:QI|r120:QI;clobber flags:CC;} REG_DEAD r120:QI REG_DEAD r119:QI REG_UNUSED flags:CC and creates the following two insn sequence: modifying insn i2 22: r104:QI=flags:CCNO>0 REG_DEAD flags:CC deferring rescan insn with uid = 22. modifying insn i3 23: r110:QI=flags:CCNO<=0 REG_DEAD flags:CC deferring rescan insn with uid = 23. where the REG_DEAD note in i2 is not correct, because the flags register is still referenced in i3. In try_combine() megafunction, we have this part: --cut here-- /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */ if (i3notes) distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL, elim_i2, elim_i1, elim_i0); if (i2notes) distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL, elim_i2, elim_i1, elim_i0); if (i1notes) distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL, elim_i2, local_elim_i1, local_elim_i0); if (i0notes) distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL, elim_i2, elim_i1, local_elim_i0); if (midnotes) distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL, elim_i2, elim_i1, elim_i0); --cut here-- where the compiler distributes REG_UNUSED note from i2: 22: {r120:QI=r104:QI^0x1;clobber flags:CC;} REG_UNUSED flags:CC via distribute_notes() using the following: --cut here-- /* Otherwise, if this register is now referenced in i2 then the register used to be modified in one of the original insns. If it was i3 (say, in an unused parallel), it's now completely gone, so the note can be discarded. But if it was modified in i2, i1 or i0 and we still reference it in i2, then we're referencing the previous value, and since the register was modified and REG_UNUSED, we know that the previous value is now dead. So, if we only reference the register in i2, we change the note to REG_DEAD, to reflect the previous value. However, if we're also setting or clobbering the register as scratch, we know (because the register was not referenced in i3) that it's unused, just as it was unused before, and we place the note in i2. */ if (from_insn != i3 && i2 && INSN_P (i2) && reg_referenced_p (XEXP (note, 0), PATTERN (i2))) { if (!reg_set_p (XEXP (note, 0), PATTERN (i2))) PUT_REG_NOTE_KIND (note, REG_DEAD); if (! (REG_P (XEXP (note, 0)) ? find_regno_note (i2, REG_NOTE_KIND (note), REGNO (XEXP (note, 0))) : find_reg_note (i2, REG_NOTE_KIND (note), XEXP (note, 0)))) place = i2; } --cut here-- However, the flags register is not UNUSED (or DEAD), because it is used in i3. The proposed solution is to remove the REG_UNUSED note from i2 when the register is also mentioned in i3. Bootstrapped and regression tested on x86_64-linux-gnu {,-m32}. OK for master and eventual backports? Uros. diff --git a/gcc/combine.cc b/gcc/combine.cc index 3beeb514b81..0589ddbaca7 100644 --- a/gcc/combine.cc +++ b/gcc/combine.cc @@ -14557,9 +14557,12 @@ distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2, we're also setting or clobbering the register as scratch, we know (because the register was not referenced in i3) that it's unused, just as it was - unused before, and we place the note in i2. */ + unused before, and we place the note in i2. If this + register is still referenced in i3, the note can be + discarded. */ if (from_insn != i3 && i2 && INSN_P (i2) - && reg_referenced_p (XEXP (note, 0), PATTERN (i2))) + && reg_referenced_p (XEXP (note, 0), PATTERN (i2)) + && !reg_referenced_p (XEXP (note, 0), PATTERN (i3))) { if (!reg_set_p (XEXP (note, 0), PATTERN (i2))) PUT_REG_NOTE_KIND (note, REG_DEAD); diff --git a/gcc/testsuite/gcc.target/i386/pr118739.c b/gcc/testsuite/gcc.target/i386/pr118739.c new file mode 100644 index 00000000000..89bed546363 --- /dev/null +++ b/gcc/testsuite/gcc.target/i386/pr118739.c @@ -0,0 +1,50 @@ +/* PR rtl-optimization/118739 */ +/* { dg-do run } */ +/* { dg-options "-O3 -fno-tree-forwprop -fno-tree-vrp" } */ + +volatile int a; +int b, c, d = 1, e, f, g; + +int h (void) +{ + int i = 1; + + j: + for (b = 1; b; b--) + { + asm ("#"); + + g = 0; + + for (; g <= 1; g++) + { + int k = f = 0; + + for (; f <= 1; f++) + k = (1 == i) >= k || ((d = 0) >= a) + k; + } + } + + for (; i < 3; i++) + { + if (!c) + return g; + + if (e) + goto j; + + asm ("#"); + } + + return 0; +} + +int main() +{ + h(); + + if (d != 1) + __builtin_abort(); + + return 0; +}