From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 177F63857419; Mon, 9 May 2022 10:17:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 177F63857419 From: "christophm30 at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug rtl-optimization/105477] RISC-V: Regression: Useless moves in conditional select return Date: Mon, 09 May 2022 10:17:27 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: rtl-optimization X-Bugzilla-Version: 12.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: christophm30 at gmail dot com X-Bugzilla-Status: UNCONFIRMED 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: Mon, 09 May 2022 10:17:27 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105477 --- Comment #2 from Christoph M=C3=BCllner = --- I've analysed this issue a bit more and want to share my observations. I mention commit 3a7ba8fd here again as trigger of this issue, but not as the underlying issue (which I did not fully understand so far). When looking into the dump files, the input to the sink2 pass (output of forwprop4) is: long int test (long int a, long int b, long int c) {=20 long int iftmp.0_1; [local count: 1073741824]: if (c_2(D) =3D=3D 0) goto ; [50.00%]=20 else=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 goto ; [50.00%]=20 [local count: 536870912]: [local count: 1073741824]: # iftmp.0_1 =3D PHI return iftmp.0_1; } Before commit 3a7ba8fd this did not get further changed up to the expand pa= ss. Since commit 3a7ba8fd the sink2 pass transforms into the following: long int test (long int a, long int b, long int c) { long int iftmp.0_1; [local count: 1073741824]: if (c_2(D) =3D=3D 0) goto ; [50.00%] else goto ; [50.00%] [local count: 536870912]: goto ; [100.00%] [local count: 536870912]: [local count: 1073741824]: # iftmp.0_1 =3D PHI return iftmp.0_1; } This has an impact on * the expansion pass (for obvious reasons) * the output of the combiner pass * the result of the reload pass Let's start with the behaviour before change 3a7ba8fd. The expander generated the following output: (insn 2 7 3 2 (set (reg/v:DI 73 [ a ]) (reg:DI 10 a0 [ a ])) "pr105477.c":2:1 -1 (nil)) (insn 3 2 4 2 (set (reg/v:DI 74 [ b ]) (reg:DI 11 a1 [ b ])) "pr105477.c":2:1 -1 (nil)) (insn 4 3 5 2 (set (reg/v:DI 75 [ c ]) (reg:DI 12 a2 [ c ])) "pr105477.c":2:1 -1 (nil)) (jump_insn 9 5 10 2 (set (pc) (if_then_else (eq (reg/v:DI 75 [ c ]) (const_int 0 [0])) (label_ref 11) (pc))) "pr105477.c":3:17 -1 (int_list:REG_BR_PROB 536870916 (nil)) -> 11) (insn 6 10 11 4 (set (reg/v:DI 73 [ a ]) (reg/v:DI 74 [ b ])) "pr105477.c":3:17 -1 (nil)) (code_label 11 6 12 5 2 (nil) [1 uses]) (insn 13 12 17 5 (set (reg:DI 72 [ ]) (reg/v:DI 73 [ a ])) "pr105477.c":3:17 -1 (nil)) (insn 17 13 18 5 (set (reg/i:DI 10 a0) (reg:DI 72 [ ])) "pr105477.c":4:1 -1 (nil)) (insn 18 17 0 5 (use (reg/i:DI 10 a0)) "pr105477.c":4:1 -1 (nil)) The combiner then converted to: (insn 30 7 2 2 (set (reg:DI 77) (reg:DI 10 a0 [ a ])) "pr105477.c":2:1 -1 (expr_list:REG_DEAD (reg:DI 10 a0 [ a ]) (nil))) (insn 2 30 31 2 (set (reg/v:DI 73 [ a ]) (reg:DI 77)) "pr105477.c":2:1 135 {*movdi_64bit} (expr_list:REG_DEAD (reg:DI 77) (nil))) (insn 31 2 3 2 (set (reg:DI 78) (reg:DI 11 a1 [ b ])) "pr105477.c":2:1 -1 (expr_list:REG_DEAD (reg:DI 11 a1 [ b ]) (nil))) (insn 3 31 32 2 (set (reg/v:DI 74 [ b ]) (reg:DI 78)) "pr105477.c":2:1 135 {*movdi_64bit} (expr_list:REG_DEAD (reg:DI 78) (nil))) (insn 32 3 4 2 (set (reg:DI 79) (reg:DI 12 a2 [ c ])) "pr105477.c":2:1 -1 (expr_list:REG_DEAD (reg:DI 12 a2 [ c ]) (nil))) (jump_insn 9 5 10 2 (set (pc) (if_then_else (eq (reg:DI 79) (const_int 0 [0])) (label_ref:DI 11) (pc))) "pr105477.c":3:17 182 {*branchdi} (expr_list:REG_DEAD (reg:DI 79) (int_list:REG_BR_PROB 536870916 (nil))) -> 11) (insn 6 10 11 3 (set (reg/v:DI 73 [ a ]) (reg/v:DI 74 [ b ])) "pr105477.c":3:17 135 {*movdi_64bit} (expr_list:REG_DEAD (reg/v:DI 74 [ b ]) (nil))) (code_label 11 6 12 4 2 (nil) [1 uses]) (insn 17 12 18 4 (set (reg/i:DI 10 a0) (reg/v:DI 73 [ a ])) "pr105477.c":4:1 135 {*movdi_64bit} (expr_list:REG_DEAD (reg/v:DI 73 [ a ]) (nil))) (insn 18 17 0 4 (use (reg/i:DI 10 a0)) "pr105477.c":4:1 -1 (nil)) This could be simplified by the reload pass to: (jump_insn 9 5 10 2 (set (pc) (if_then_else (eq (reg:DI 12 a2 [79]) (const_int 0 [0])) (label_ref:DI 11) (pc))) "pr105477.c":3:17 182 {*branchdi} (int_list:REG_BR_PROB 536870916 (nil)) -> 11) (insn 6 10 11 3 (set (reg/v:DI 10 a0 [orig:73 a ] [73]) (reg/v:DI 11 a1 [orig:74 b ] [74])) "pr105477.c":3:17 135 {*movdi_64bit} (nil)) (code_label 11 6 12 4 2 (nil) [1 uses]) (insn 18 12 36 4 (use (reg/i:DI 10 a0)) "pr105477.c":4:1 -1 (nil)) The resulting assembly did not contain the two useless move instructions. Since commit 3a7ba8fd the expanded code looks like this: (insn 2 7 3 2 (set (reg/v:DI 73 [ a ]) (reg:DI 10 a0 [ a ])) "pr105477.c":2:1 -1 (nil)) (insn 3 2 4 2 (set (reg/v:DI 74 [ b ]) (reg:DI 11 a1 [ b ])) "pr105477.c":2:1 -1 (nil)) (insn 4 3 5 2 (set (reg/v:DI 75 [ c ]) (reg:DI 12 a2 [ c ])) "pr105477.c":2:1 -1 (nil)) (jump_insn 9 5 10 2 (set (pc) (if_then_else (ne (reg/v:DI 75 [ c ]) (const_int 0 [0])) (label_ref 11) (pc))) "pr105477.c":3:17 -1 (int_list:REG_BR_PROB 536870916 (nil)) -> 11) (insn 6 10 11 4 (set (reg/v:DI 74 [ b ]) (reg/v:DI 73 [ a ])) "pr105477.c":3:17 -1 (nil)) (code_label 11 6 12 5 2 (nil) [1 uses]) (insn 13 12 17 5 (set (reg:DI 72 [ ]) (reg/v:DI 74 [ b ])) "pr105477.c":3:17 -1 (nil)) (insn 17 13 18 5 (set (reg/i:DI 10 a0) (reg:DI 72 [ ])) "pr105477.c":4:1 -1 (nil)) (insn 18 17 0 5 (use (reg/i:DI 10 a0)) "pr105477.c":4:1 -1 (nil)) The code remains like this and gets changed by the combiner later on to: (insn 24 7 2 2 (set (reg:DI 77) (reg:DI 10 a0 [ a ])) "pr105477.c":2:1 -1 (expr_list:REG_DEAD (reg:DI 10 a0 [ a ]) (nil))) (insn 2 24 25 2 (set (reg/v:DI 73 [ a ]) (reg:DI 77)) "pr105477.c":2:1 135 {*movdi_64bit} (expr_list:REG_DEAD (reg:DI 77) (nil))) (insn 25 2 3 2 (set (reg:DI 78) (reg:DI 11 a1 [ b ])) "pr105477.c":2:1 -1 (expr_list:REG_DEAD (reg:DI 11 a1 [ b ]) (nil))) (insn 3 25 26 2 (set (reg/v:DI 74 [ b ]) (reg:DI 78)) "pr105477.c":2:1 135 {*movdi_64bit} (expr_list:REG_DEAD (reg:DI 78) (nil))) (insn 26 3 4 2 (set (reg:DI 79) (reg:DI 12 a2 [ c ])) "pr105477.c":2:1 -1 (expr_list:REG_DEAD (reg:DI 12 a2 [ c ]) (nil))) (jump_insn 9 5 10 2 (set (pc) (if_then_else (ne (reg:DI 79) (const_int 0 [0])) (label_ref 11) (pc))) "pr105477.c":3:17 182 {*branchdi} (expr_list:REG_DEAD (reg:DI 79) (int_list:REG_BR_PROB 536870916 (nil))) -> 11) (insn 6 10 11 3 (set (reg/v:DI 74 [ b ]) (reg/v:DI 73 [ a ])) "pr105477.c":3:17 135 {*movdi_64bit} (expr_list:REG_DEAD (reg/v:DI 73 [ a ]) (nil))) (code_label 11 6 12 4 2 (nil) [1 uses]) (insn 17 12 18 4 (set (reg/i:DI 10 a0) (reg/v:DI 74 [ b ])) "pr105477.c":4:1 135 {*movdi_64bit} (expr_list:REG_DEAD (reg/v:DI 74 [ b ]) (nil))) (insn 18 17 0 4 (use (reg/i:DI 10 a0)) "pr105477.c":4:1 -1 (nil)) The reload pass can then simplify as to the following: (insn 2 5 3 2 (set (reg/v:DI 15 a5 [orig:73 a ] [73]) (reg:DI 10 a0 [77])) "pr105477.c":2:1 135 {*movdi_64bit} (nil)) (insn 3 2 9 2 (set (reg/v:DI 10 a0 [orig:74 b ] [74]) (reg:DI 11 a1 [78])) "pr105477.c":2:1 135 {*movdi_64bit} (nil)) (jump_insn 9 3 10 2 (set (pc) (if_then_else (ne (reg:DI 12 a2 [79]) (const_int 0 [0])) (label_ref 11) (pc))) "pr105477.c":3:17 182 {*branchdi} (int_list:REG_BR_PROB 536870916 (nil)) -> 11) (insn 6 10 11 3 (set (reg/v:DI 10 a0 [orig:74 b ] [74]) (reg/v:DI 15 a5 [orig:73 a ] [73])) "pr105477.c":3:17 135 {*movdi_64bit} (nil)) (code_label 11 6 12 4 2 (nil) [1 uses]) (insn 18 12 30 4 (use (reg/i:DI 10 a0)) "pr105477.c":4:1 -1 (nil)) The remaining two set statements above will become move instructions.=