https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97417

--- Comment #47 from Levy <admin at levyhsu dot com> ---
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) 
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===================846930886===================OLD
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) + 800B]+0
S4 A32]))) "array_test.c":7:5 90 {extendsidi2}
     (nil))
Old Cost 20:



NEW===================846930886===================NEW
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) + 800B]+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===================1681692777===================OLD
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) + 804B]+0
S4 A32]))) "array_test.c":7:5 90 {extendsidi2}
     (nil))
Old Cost 20:



NEW===================1681692777===================NEW
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) + 804B]+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===================1714636915===================OLD
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) + 808B]+0
S4 A32]))) "array_test.c":8:5 90 {extendsidi2}
     (nil))
Old Cost 20:



NEW===================1714636915===================NEW
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) + 808B]+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===================1957747793===================OLD
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) + 812B]+0
S4 A32]))) "array_test.c":9:5 90 {extendsidi2}
     (expr_list:REG_DEAD (reg/f:DI 92)
        (nil)))
Old Cost 20:



NEW===================1957747793===================NEW
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) + 812B]+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 = old_cost > 0 && new_cost > old_cost;
  to:
int reject = old_cost > 0 && new_cost >= 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.

Reply via email to