https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119457
Bug ID: 119457
Summary: gcobol: big codegen for simple STRING plus malloc/free
(and missing optimization)
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: cobol
Assignee: unassigned at gcc dot gnu.org
Reporter: simonsobisch at gnu dot org
Target Milestone: ---
Sample program:
~~~
PROGRAM-ID. STRMOV.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 dat pic x.
PROCEDURE DIVISION.
STRING 'a' INTO dat
STOP RUN.
~~~
This program has a single byte data field and overwrites its first byte by a
literal.
While this assignment can easily be handled as in internal MOVE (because it
overwrites all storage and as it is a single-byte item that's even an easily
inlined assignment), it currently does a lot of stuff (presumably preparation
for the complex STRING verb which can do a lot - just not doing it in that
case) before calling into libgcobol, surrounded by allocating a temporary item
using malloc/free.
To reproduce:
~~~
gcobol -g STRMOV.cob
gdb -batch -ex "set disassemble-next-line on" -ex "b STRMOV.cob:6" -ex run
a.out
~~~
Result:
~~~
Breakpoint 1, strmov () at STRMOV.cob:6
6 string 'a' into dat
=> 0x00000000004016e2 <strmov+723>: bf 10 00 00 00 mov $0x10,%edi
0x00000000004016e7 <strmov+728>: e8 f4 fa ff ff callq 0x4011e0
<malloc@plt>
0x00000000004016ec <strmov+733>: 48 89 05 1d 11 20 00 mov
%rax,0x20111d(%rip) # 0x602810
0x00000000004016f3 <strmov+740>: 48 8b 05 16 11 20 00 mov
0x201116(%rip),%rax # 0x602810
0x00000000004016fa <strmov+747>: ba 00 00 00 00 mov $0x0,%edx
0x00000000004016ff <strmov+752>: 48 01 d0 add %rdx,%rax
0x0000000000401702 <strmov+755>: 48 c7 00 01 00 00 00 movq
$0x1,(%rax)
0x0000000000401709 <strmov+762>: 48 8b 05 00 11 20 00 mov
0x201100(%rip),%rax # 0x602810
0x0000000000401710 <strmov+769>: ba 08 00 00 00 mov $0x8,%edx
0x0000000000401715 <strmov+774>: 48 01 d0 add %rdx,%rax
0x0000000000401718 <strmov+777>: 48 c7 00 01 00 00 00 movq
$0x1,(%rax)
0x000000000040171f <strmov+784>: 48 8b 05 9a 10 20 00 mov
0x20109a(%rip),%rax # 0x6027c0 <__gg__treeplet_1f>
0x0000000000401726 <strmov+791>: ba 00 00 00 00 mov $0x0,%edx
0x000000000040172b <strmov+796>: 48 01 d0 add %rdx,%rax
0x000000000040172e <strmov+799>: 48 c7 00 a0 21 60 00 movq
$0x6021a0,(%rax)
0x0000000000401735 <strmov+806>: 48 c7 85 20 ff ff ff 00 00 00 00
movq $0x0,-0xe0(%rbp)
0x0000000000401740 <strmov+817>: 48 8b 05 81 10 20 00 mov
0x201081(%rip),%rax # 0x6027c8 <__gg__treeplet_1o>
0x0000000000401747 <strmov+824>: ba 00 00 00 00 mov $0x0,%edx
0x000000000040174c <strmov+829>: 48 01 c2 add %rax,%rdx
0x000000000040174f <strmov+832>: 48 8b 85 20 ff ff ff mov
-0xe0(%rbp),%rax
0x0000000000401756 <strmov+839>: 48 89 02 mov %rax,(%rdx)
0x0000000000401759 <strmov+842>: 48 8b 05 30 10 20 00 mov
0x201030(%rip),%rax # 0x602790 <__gg__treeplet_1s>
0x0000000000401760 <strmov+849>: ba 00 00 00 00 mov $0x0,%edx
0x0000000000401765 <strmov+854>: 48 01 d0 add %rdx,%rax
0x0000000000401768 <strmov+857>: 48 c7 00 01 00 00 00 movq
$0x1,(%rax)
0x000000000040176f <strmov+864>: 48 8b 05 4a 10 20 00 mov
0x20104a(%rip),%rax # 0x6027c0 <__gg__treeplet_1f>
0x0000000000401776 <strmov+871>: ba 08 00 00 00 mov $0x8,%edx
0x000000000040177b <strmov+876>: 48 01 d0 add %rdx,%rax
0x000000000040177e <strmov+879>: 48 c7 00 00 00 00 00 movq
$0x0,(%rax)
0x0000000000401785 <strmov+886>: 48 8b 05 3c 10 20 00 mov
0x20103c(%rip),%rax # 0x6027c8 <__gg__treeplet_1o>
0x000000000040178c <strmov+893>: ba 08 00 00 00 mov $0x8,%edx
0x0000000000401791 <strmov+898>: 48 01 d0 add %rdx,%rax
0x0000000000401794 <strmov+901>: 48 c7 00 00 00 00 00 movq
$0x0,(%rax)
0x000000000040179b <strmov+908>: 48 8b 05 ee 0f 20 00 mov
0x200fee(%rip),%rax # 0x602790 <__gg__treeplet_1s>
0x00000000004017a2 <strmov+915>: ba 08 00 00 00 mov $0x8,%edx
0x00000000004017a7 <strmov+920>: 48 01 d0 add %rdx,%rax
0x00000000004017aa <strmov+923>: 48 c7 00 00 00 00 00 movq
$0x0,(%rax)
0x00000000004017b1 <strmov+930>: 48 8b 05 08 10 20 00 mov
0x201008(%rip),%rax # 0x6027c0 <__gg__treeplet_1f>
0x00000000004017b8 <strmov+937>: ba 10 00 00 00 mov $0x10,%edx
0x00000000004017bd <strmov+942>: 48 01 d0 add %rdx,%rax
0x00000000004017c0 <strmov+945>: 48 c7 00 00 00 00 00 movq
$0x0,(%rax)
0x00000000004017c7 <strmov+952>: 48 8b 05 fa 0f 20 00 mov
0x200ffa(%rip),%rax # 0x6027c8 <__gg__treeplet_1o>
0x00000000004017ce <strmov+959>: ba 10 00 00 00 mov $0x10,%edx
0x00000000004017d3 <strmov+964>: 48 01 d0 add %rdx,%rax
0x00000000004017d6 <strmov+967>: 48 c7 00 00 00 00 00 movq
$0x0,(%rax)
0x00000000004017dd <strmov+974>: 48 8b 05 ac 0f 20 00 mov
0x200fac(%rip),%rax # 0x602790 <__gg__treeplet_1s>
0x00000000004017e4 <strmov+981>: ba 10 00 00 00 mov $0x10,%edx
0x00000000004017e9 <strmov+986>: 48 01 d0 add %rdx,%rax
0x00000000004017ec <strmov+989>: 48 c7 00 00 00 00 00 movq
$0x0,(%rax)
0x00000000004017f3 <strmov+996>: 48 8b 05 c6 0f 20 00 mov
0x200fc6(%rip),%rax # 0x6027c0 <__gg__treeplet_1f>
0x00000000004017fa <strmov+1003>: ba 18 00 00 00 mov $0x18,%edx
0x00000000004017ff <strmov+1008>: 48 01 d0 add %rdx,%rax
0x0000000000401802 <strmov+1011>: 48 c7 00 20 21 60 00 movq
$0x602120,(%rax)
0x0000000000401809 <strmov+1018>: 48 c7 85 18 ff ff ff 00 00 00 00
movq $0x0,-0xe8(%rbp)
0x0000000000401814 <strmov+1029>: 48 8b 05 ad 0f 20 00 mov
0x200fad(%rip),%rax # 0x6027c8 <__gg__treeplet_1o>
0x000000000040181b <strmov+1036>: ba 18 00 00 00 mov $0x18,%edx
0x0000000000401820 <strmov+1041>: 48 01 c2 add %rax,%rdx
0x0000000000401823 <strmov+1044>: 48 8b 85 18 ff ff ff mov
-0xe8(%rbp),%rax
0x000000000040182a <strmov+1051>: 48 89 02 mov %rax,(%rdx)
0x000000000040182d <strmov+1054>: 48 8b 05 5c 0f 20 00 mov
0x200f5c(%rip),%rax # 0x602790 <__gg__treeplet_1s>
0x0000000000401834 <strmov+1061>: ba 18 00 00 00 mov $0x18,%edx
0x0000000000401839 <strmov+1066>: 48 01 c2 add %rax,%rdx
0x000000000040183c <strmov+1069>: 48 8b 05 e5 08 20 00 mov
0x2008e5(%rip),%rax # 0x602128 <_literal_a_1+8>
0x0000000000401843 <strmov+1076>: 48 89 02 mov %rax,(%rdx)
0x0000000000401846 <strmov+1079>: 48 8b 05 c3 0f 20 00 mov
0x200fc3(%rip),%rax # 0x602810
0x000000000040184d <strmov+1086>: 48 89 c7 mov %rax,%rdi
0x0000000000401850 <strmov+1089>: e8 bb f8 ff ff callq 0x401110
<__gg__string@plt>
0x0000000000401855 <strmov+1094>: 89 85 14 ff ff ff mov
%eax,-0xec(%rbp)
0x000000000040185b <strmov+1100>: 48 8b 05 ae 0f 20 00 mov
0x200fae(%rip),%rax # 0x602810
0x0000000000401862 <strmov+1107>: 48 89 c7 mov %rax,%rdi
0x0000000000401865 <strmov+1110>: e8 46 f9 ff ff callq 0x4011b0
<free@plt>
~~~
That shows:
* malloc/free - this temporary data is possibly better done on the stack, at
least for small items, because commonly there are a lot of those statements
* a lot of settings to some __gg__treeplet_.... (whatever that is) together
with addition (this _may_ be necessary for much more complex STRING variants -
I don't know)
* missing optimization: as the sending items (here only one) are of same size
as the receiving item, this can be internally handled as MOVE (also could if
the sending item(s) would be bigger, because MOVE truncates alphanumeric items)