From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id BD2E23857C74; Tue, 8 Dec 2020 09:22:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BD2E23857C74 From: "admin at levyhsu dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/97417] RISC-V Unnecessary andi instruction when loading volatile bool Date: Tue, 08 Dec 2020 09:22:19 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 10.2.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: admin at levyhsu dot com X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Dec 2020 09:22:21 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97417 --- Comment #47 from Levy --- where insns are merged: In combine.c (pass_combine) rest_of_handle_combine() calls: combine_instructions() calls: creat_log_links() creates links of insn (768&32/36/40/44)=20 for both patched and unpatched version with log_links() Then in combine_instructions(), for_each_log_link(), try_combine() calls combine_validate_cost() in combine_validate_cost(), for the patched version: OLD=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D846930886=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DOLD i2 & Cost 4: (insn 27 3 6 2 (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) "array_test.c":7:5 4 {adddi3} (expr_list:REG_DEAD (reg:DI 96) (nil))) i3 & Cost 16: (insn 6 27 8 2 (set (reg:DI 82 [ MEM[(int *)array_5(D) + 800B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 800 [0x320])) [1 MEM[(int *)array_5(D) + 800= B]+0 S4 A32]))) "array_test.c":7:5 90 {extendsidi2} (nil)) Old Cost 20: NEW=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D846930886=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DNEW New_Cost: 20 i0 & Cost 0: (nil) i1 & Cost 0: (nil) i2 & Cost 4: (insn 27 3 6 2 (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) "array_test.c":7:5 4 {adddi3} (expr_list:REG_DEAD (reg:DI 96) (nil))) i3 & Cost 16: (insn 6 27 8 2 (set (reg:DI 82 [ MEM[(int *)array_5(D) + 800B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 800 [0x320])) [1 MEM[(int *)array_5(D) + 800= B]+0 S4 A32]))) "array_test.c":7:5 90 {extendsidi2} (nil)) newpat: (set (reg:DI 82 [ MEM[(int *)array_5(D) + 800B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 800 [0x320])) [1 MEM[(int *)array_5(D) + 800B]+0= S4 A32]))) newi2pat: (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) newotherpat: (nil) GO!--------------------------------------------------- OLD=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D1681692777=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DOLD i2 & Cost 4: (insn 27 3 6 2 (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) "array_test.c":7:5 4 {adddi3} (nil)) i3 & Cost 16: (insn 8 6 10 2 (set (reg:DI 84 [ MEM[(int *)array_5(D) + 804B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 804 [0x324])) [1 MEM[(int *)array_5(D) + 804= B]+0 S4 A32]))) "array_test.c":7:5 90 {extendsidi2} (nil)) Old Cost 20: NEW=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D1681692777=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DNEW New_Cost: 20 i0 & Cost 0: (nil) i1 & Cost 0: (nil) i2 & Cost 4: (insn 27 3 6 2 (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) "array_test.c":7:5 4 {adddi3} (nil)) i3 & Cost 16: (insn 8 6 10 2 (set (reg:DI 84 [ MEM[(int *)array_5(D) + 804B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 804 [0x324])) [1 MEM[(int *)array_5(D) + 804= B]+0 S4 A32]))) "array_test.c":7:5 90 {extendsidi2} (nil)) newpat: (set (reg:DI 84 [ MEM[(int *)array_5(D) + 804B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 804 [0x324])) [1 MEM[(int *)array_5(D) + 804B]+0= S4 A32]))) newi2pat: (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) newotherpat: (nil) GO!--------------------------------------------------- OLD=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D1714636915=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DOLD i2 & Cost 4: (insn 27 3 6 2 (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) "array_test.c":7:5 4 {adddi3} (nil)) i3 & Cost 16: (insn 12 10 14 2 (set (reg:DI 87 [ MEM[(int *)array_5(D) + 808B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 808 [0x328])) [1 MEM[(int *)array_5(D) + 808= B]+0 S4 A32]))) "array_test.c":8:5 90 {extendsidi2} (nil)) Old Cost 20: NEW=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D1714636915=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DNEW New_Cost: 20 i0 & Cost 0: (nil) i1 & Cost 0: (nil) i2 & Cost 4: (insn 27 3 6 2 (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) "array_test.c":7:5 4 {adddi3} (nil)) i3 & Cost 16: (insn 12 10 14 2 (set (reg:DI 87 [ MEM[(int *)array_5(D) + 808B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 808 [0x328])) [1 MEM[(int *)array_5(D) + 808= B]+0 S4 A32]))) "array_test.c":8:5 90 {extendsidi2} (nil)) newpat: (set (reg:DI 87 [ MEM[(int *)array_5(D) + 808B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 808 [0x328])) [1 MEM[(int *)array_5(D) + 808B]+0= S4 A32]))) newi2pat: (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) newotherpat: (nil) GO!--------------------------------------------------- OLD=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D1957747793=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DOLD i2 & Cost 4: (insn 27 3 6 2 (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) "array_test.c":7:5 4 {adddi3} (nil)) i3 & Cost 16: (insn 16 14 18 2 (set (reg:DI 90 [ MEM[(int *)array_5(D) + 812B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 812 [0x32c])) [1 MEM[(int *)array_5(D) + 812= B]+0 S4 A32]))) "array_test.c":9:5 90 {extendsidi2} (expr_list:REG_DEAD (reg/f:DI 92) (nil))) Old Cost 20: NEW=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D1957747793=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3DNEW New_Cost: 16 i0 & Cost 0: (nil) i1 & Cost 0: (nil) i2 & Cost 4: (insn 27 3 6 2 (set (reg/f:DI 92) (plus:DI (reg:DI 96) (const_int 768 [0x300]))) "array_test.c":7:5 4 {adddi3} (nil)) i3 & Cost 16: (insn 16 14 18 2 (set (reg:DI 90 [ MEM[(int *)array_5(D) + 812B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 812 [0x32c])) [1 MEM[(int *)array_5(D) + 812= B]+0 S4 A32]))) "array_test.c":9:5 90 {extendsidi2} (expr_list:REG_DEAD (reg/f:DI 92) (nil))) newpat: (set (reg:DI 90 [ MEM[(int *)array_5(D) + 812B] ]) (sign_extend:DI (mem:SI (plus:DI (reg:DI 96) (const_int 812 [0x32c])) [1 MEM[(int *)array_5(D) + 812B]+0= S4 A32]))) newi2pat: (nil) newotherpat: (nil) GO!--------------------------------------------------- where the go stands for !reject (replacement happens) and newpat is the new pattern. And above merge is not observed in the unpatched version. One naive fix to verify this would be changing: int reject =3D old_cost > 0 && new_cost > old_cost; to: int reject =3D old_cost > 0 && new_cost >=3D old_cost; since both unmerged and merged results cost 20, but it would surely cause side effect. As for why the unpatched version's insns are missed here, I think it would = be better to look back at try_combine() or before.=