Discussion:
Bad code generation on linux x86_64
(too old to reply)
C Western
2017-05-20 08:50:36 UTC
Permalink
Raw Message
The following revision seems to be generating bad code for me on
linux/x86_64:

r36200 | florian | 2017-05-13 10:58:25 +0100 (Sat, 13 May 2017) | 1 line
Changed paths:
M /trunk/compiler/i386/aoptcpu.pas
M /trunk/compiler/x86/aoptx86.pas

* moved InstructionLoadsFromReg and RegReadByInstruction from
TCpuAsmOptimizer (i386) to TX86AsmOptimizer

I am not sure how to generate a simple test case, but the code fragment
that fails is below. As far as I can tell, r14 is used, but never
loaded; I think r13 should have been used instead.

Colin

.section .text.n_logunit$_$tlogform_$__$$_formcreate$tobject
.balign 16,0x90
.globl LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT
.type LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT,@function
LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT:
.Lc1:
# Register rsp allocated
.Ll1:
# [LogUnit.pas]
# [95] begin
pushq %rbx
pushq %r12
pushq %r13
pushq %r14
pushq %r15
.Lc3:
# Register rdi,rsi,rax allocated
movq %rdi,%r15
# Var $self located in register rax
# Register rsi released
# Var Sender located in register rsi
# Register r15 allocated
# Register rax released
# Var $self located in register r15
# Register sil allocated
.Ll2:
# [96] DragAcceptFiles(Self, True);
movb $1,%sil
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call WCOMPAT_$$_DRAGACCEPTFILES$TCUSTOMFORM$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register rax allocated
.Ll3:
# [98] OnDropFiles := @FormDropFiles;
movq
LOGUNIT$_$TLOGFORM_$__$$_FORMDROPFILES$TOBJECT$***@GOTPCREL(%rip),%rax
movq %rax,1736(%r15)
# Register rax released
movq %r15,1744(%r15)
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
.Ll4:
# [100]
GlobalMixture.GetEnumNames(GlobalMixture.IndexOfSetting('PrintLevel'),OutputComboBox.Items);
call GLOBAL_$$_GLOBALMIXTURE$$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register r13 allocated
movq %rax,%r13
# Register rax released
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call GLOBAL_$$_GLOBALMIXTURE$$***@PLT
# Register rcx,rdx,rsi,r8,r9,r10,r11 released
# Register rdi allocated
movq %rax,%rdi
# Register rax released
# Register rsi allocated
leaq .Ld2(%rip),%rsi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call MNODE$_$TMNODE_$__$$_INDEXOFSETTING$ANSISTRING$$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register r12d allocated
movl %eax,%r12d
# Register eax released
# Register rax allocated
movq 2048(%r15),%rax
# Register rbx allocated
movq 1424(%rax),%rbx
# Register rax released
# Register rax,r14 allocated
# Register rax released
testq %r13,%r13
jne .Lj6
# Register rflags released
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call ***@PLT
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
.Lj6:
# Register rax allocated
movq (%r14),%r14
# Register r14 released
# Register r14 allocated
# Register rax released
# Register rdi allocated
movq %r14,%rdi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call ***@PLT
# Register rax,rcx,rdx,rsi,r8,r9,r10,r11 released
# Register rdi allocated
movq %r13,%rdi
# Register r13 released
# Register rdx allocated
movq %rbx,%rdx
# Register rbx released
# Register esi allocated
movl %r12d,%esi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call *744(%r14) <-------------------- Fail here
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
.Ll5:
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://
Florian Klämpfl
2017-05-21 07:45:05 UTC
Permalink
Raw Message
Can you please change line 26 in fpc compiler/x86/aoptx86.pas from
{ $define DEBUG_AOPTCPU}
to
{$define DEBUG_AOPTCPU}

and post the assembler output again?
Post by C Western
r36200 | florian | 2017-05-13 10:58:25 +0100 (Sat, 13 May 2017) | 1 line
M /trunk/compiler/i386/aoptcpu.pas
M /trunk/compiler/x86/aoptx86.pas
* moved InstructionLoadsFromReg and RegReadByInstruction from TCpuAsmOptimizer (i386) to
TX86AsmOptimizer
I am not sure how to generate a simple test case, but the code fragment that fails is below. As far
as I can tell, r14 is used, but never loaded; I think r13 should have been used instead.
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailm
C Western
2017-05-21 09:23:45 UTC
Permalink
Raw Message
Post by Florian Klämpfl
Can you please change line 26 in fpc compiler/x86/aoptx86.pas from
{ $define DEBUG_AOPTCPU}
to
{$define DEBUG_AOPTCPU}
and post the assembler output again?
See below. Note this is with r36200 - the current trunk is giving me
internal error.

Thank you for looking at this.

Colin

.section .text.n_logunit$_$tlogform_$__$$_formcreate$tobject
.balign 16,0x90
.globl LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT
.type LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT,@function
LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT:
.Lc1:
# Register rsp allocated
.Ll1:
# [LogUnit.pas]
# [95] begin
pushq %rbx
pushq %r12
pushq %r13
pushq %r14
pushq %r15
.Lc3:
# Register rdi,rsi,rax allocated
# PeepHole Optimization,MovMov2Mov 2
# PeepHole Optimization,MovMov2Mov 1
movq %rdi,%r15
# Var $self located in register rax
# Register rsi released
# Var Sender located in register rsi
# Register r15 allocated
# Register rax released
# Var $self located in register r15
# Register sil allocated
.Ll2:
# [96] DragAcceptFiles(Self, True);
movb $1,%sil
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call WCOMPAT_$$_DRAGACCEPTFILES$TCUSTOMFORM$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register rax allocated
.Ll3:
# [98] OnDropFiles := @FormDropFiles;
movq
LOGUNIT$_$TLOGFORM_$__$$_FORMDROPFILES$TOBJECT$***@GOTPCREL(%rip),%rax
movq %rax,1736(%r15)
# Register rax released
movq %r15,1744(%r15)
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
.Ll4:
# [100]
GlobalMixture.GetEnumNames(GlobalMixture.IndexOfSetting('PrintLevel'),OutputComboBox.Items);
call GLOBAL_$$_GLOBALMIXTURE$$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register r13 allocated
movq %rax,%r13
# Register rax released
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call GLOBAL_$$_GLOBALMIXTURE$$***@PLT
# Register rcx,rdx,rsi,r8,r9,r10,r11 released
# Register rdi allocated
movq %rax,%rdi
# Register rax released
# Register rsi allocated
leaq .Ld2(%rip),%rsi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call MNODE$_$TMNODE_$__$$_INDEXOFSETTING$ANSISTRING$$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register r12d allocated
movl %eax,%r12d
# Register eax released
# Register rax allocated
movq 2048(%r15),%rax
# Register rbx allocated
movq 1424(%rax),%rbx
# Register rax released
# Register rax allocated
# PeepHole Optimization,MovMov2Mov 1
# PeepHole Optimization,MovMov2Mov 2
# Register r14 allocated
# Register rax released
testq %r13,%r13
jne .Lj6
# Register rflags released
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call ***@PLT
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
.Lj6:
# Register rax allocated
# PeepHole Optimization,MovMov2Mov 3
movq (%r14),%r14
# Register r14 released
# Register r14 allocated
# Register rax released
# Register rdi allocated
movq %r14,%rdi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call ***@PLT
# Register rax,rcx,rdx,rsi,r8,r9,r10,r11 released
# Register rdi allocated
movq %r13,%rdi
# Register r13 released
# Register rdx allocated
movq %rbx,%rdx
# Register rbx released
# Register esi allocated
movl %r12d,%esi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call *744(%r14)
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
Post by Florian Klämpfl
Post by C Western
r36200 | florian | 2017-05-13 10:58:25 +0100 (Sat, 13 May 2017) | 1 line
M /trunk/compiler/i386/aoptcpu.pas
M /trunk/compiler/x86/aoptx86.pas
* moved InstructionLoadsFromReg and RegReadByInstruction from TCpuAsmOptimizer (i386) to
TX86AsmOptimizer
I am not sure how to generate a simple test case, but the code fragment that fails is below. As far
as I can tell, r14 is used, but never loaded; I think r13 should have been used instead.
_______________________________________________
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mail
C Western
2017-05-21 09:52:21 UTC
Permalink
Raw Message
Post by Florian Klämpfl
Can you please change line 26 in fpc compiler/x86/aoptx86.pas from
{ $define DEBUG_AOPTCPU}
to
{$define DEBUG_AOPTCPU}
and post the assembler output again?
If you are interested, the internal error with the above change and
r36278 is:

/home/me/fpc/trunk/fpcsrc/compiler/ppc1 -Ur -Ur -Xs -O2 -n -Fi../inc
-Fi../x86_64 -Fi../unix -Fix86_64 -FE.
-FU/home/me/fpc/trunk/fpcsrc/rtl/units/x86_64-linux -Cg -dx86_64
-dRELEASE -Fi../objpas/sysutils ../unix/sysutils.pp
sysutils.pp(1654,4) Fatal: Internal error 200905042
Fatal: Compilation aborted
Makefile:3783: recipe for target 'sysutils.ppu' failed
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-
Florian Klämpfl
2017-05-21 10:56:23 UTC
Permalink
Raw Message
Post by C Western
Post by Florian Klämpfl
Can you please change line 26 in fpc compiler/x86/aoptx86.pas from
{ $define DEBUG_AOPTCPU}
to
{$define DEBUG_AOPTCPU}
and post the assembler output again?
See below.
Thanks. Last request ;): please send me the same output but compile additionally with -Oonopeephole

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.f
C Western
2017-05-21 11:21:25 UTC
Permalink
Raw Message
Post by Florian Klämpfl
Post by C Western
Post by Florian Klämpfl
Can you please change line 26 in fpc compiler/x86/aoptx86.pas from
{ $define DEBUG_AOPTCPU}
to
{$define DEBUG_AOPTCPU}
and post the assembler output again?
See below.
Thanks. Last request ;): please send me the same output but compile additionally with -Oonopeephole
This code doesn't crash:

.section .text.n_logunit$_$tlogform_$__$$_formcreate$tobject
.balign 16,0x90
.globl LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT
.type LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT,@function
LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT:
.Lc1:
# Register rsp allocated
.Ll1:
# [LogUnit.pas]
# [95] begin
pushq %rbx
pushq %r12
pushq %r13
pushq %r14
pushq %r15
.Lc3:
# Register rdi,rsi,rax allocated
movq %rdi,%rax
# Register rdi released
# Var $self located in register rax
# Register rsi released
# Var Sender located in register rsi
# Register r15 allocated
.Ll2:
# [96] DragAcceptFiles(Self, True);
movq %rax,%r15
# Register rax released
# Var $self located in register r15
# Register rdi allocated
movq %r15,%rdi
# Register sil allocated
movb $1,%sil
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call WCOMPAT_$$_DRAGACCEPTFILES$TCUSTOMFORM$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register rax allocated
.Ll3:
# [98] OnDropFiles := @FormDropFiles;
movq
LOGUNIT$_$TLOGFORM_$__$$_FORMDROPFILES$TOBJECT$***@GOTPCREL(%rip),%rax
movq %rax,1736(%r15)
# Register rax released
movq %r15,1744(%r15)
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
.Ll4:
# [100]
GlobalMixture.GetEnumNames(GlobalMixture.IndexOfSetting('PrintLevel'),OutputComboBox.Items);
call GLOBAL_$$_GLOBALMIXTURE$$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register r13 allocated
movq %rax,%r13
# Register rax released
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call GLOBAL_$$_GLOBALMIXTURE$$***@PLT
# Register rcx,rdx,rsi,r8,r9,r10,r11 released
# Register rdi allocated
movq %rax,%rdi
# Register rax released
# Register rsi allocated
leaq .Ld2(%rip),%rsi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call MNODE$_$TMNODE_$__$$_INDEXOFSETTING$ANSISTRING$$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register r12d allocated
movl %eax,%r12d
# Register eax released
# Register rax allocated
movq 2048(%r15),%rax
# Register rbx allocated
movq 1424(%rax),%rbx
# Register rax released
# Register rax allocated
movq %r13,%rax
# Register r13 released
# Register r13 allocated
movq %rax,%r13
# Register r14 allocated
movq %rax,%r14
# Register rax released
testq %r14,%r14
je .Lj5
# Register rflags released
jmp .Lj6
.Lj5:
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call ***@PLT
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
.Lj6:
# Register rax allocated
movq (%r14),%rax
# Register r14 released
# Register r14 allocated
movq %rax,%r14
# Register rax released
# Register rdi allocated
movq %r14,%rdi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call ***@PLT
# Register rax,rcx,rdx,rsi,r8,r9,r10,r11 released
# Register rdi allocated
movq %r13,%rdi
# Register r13 released
# Register rdx allocated
movq %rbx,%rdx
# Register rbx released
# Register esi allocated
movl %r12d,%esi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call *744(%r14)
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
.Ll5:

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/list
Florian Klämpfl
2017-05-21 15:08:17 UTC
Permalink
Raw Message
Post by Florian Klämpfl
Post by C Western
Post by Florian Klämpfl
Can you please change line 26 in fpc compiler/x86/aoptx86.pas from
{ $define DEBUG_AOPTCPU}
to
{$define DEBUG_AOPTCPU}
and post the assembler output again?
See below.
Thanks. Last request ;): please send me the same output but compile additionally with -Oonopeephole
Can you please test without the -Oonopeephole and r36284?

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/
C Western
2017-05-21 17:20:22 UTC
Permalink
Raw Message
Post by Florian Klämpfl
Post by Florian Klämpfl
Post by C Western
Post by Florian Klämpfl
Can you please change line 26 in fpc compiler/x86/aoptx86.pas from
{ $define DEBUG_AOPTCPU}
to
{$define DEBUG_AOPTCPU}
and post the assembler output again?
See below.
Thanks. Last request ;): please send me the same output but compile additionally with -Oonopeephole
Can you please test without the -Oonopeephole and r36284?
This now works - excellent!

.section .text.n_logunit$_$tlogform_$__$$_formcreate$tobject
.balign 16,0x90
.globl LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT
.type LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT,@function
LOGUNIT$_$TLOGFORM_$__$$_FORMCREATE$TOBJECT:
.Lc1:
# Register rsp allocated
.Ll1:
# [LogUnit.pas]
# [95] begin
pushq %rbx
pushq %r12
pushq %r13
pushq %r14
pushq %r15
.Lc3:
# Register rdi,rsi,rax,r15,r15 allocated
movq %rdi,%r15
# Var $self located in register rax
# Register rsi released
# Var Sender located in register rsi
# Register rax released
# Var $self located in register r15
# Register sil allocated
.Ll2:
# [96] DragAcceptFiles(Self, True);
movb $1,%sil
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call WCOMPAT_$$_DRAGACCEPTFILES$TCUSTOMFORM$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register rax allocated
.Ll3:
# [98] OnDropFiles := @FormDropFiles;
movq
LOGUNIT$_$TLOGFORM_$__$$_FORMDROPFILES$TOBJECT$***@GOTPCREL(%rip),%rax
movq %rax,1736(%r15)
# Register rax released
movq %r15,1744(%r15)
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
.Ll4:
# [100]
GlobalMixture.GetEnumNames(GlobalMixture.IndexOfSetting('PrintLevel'),OutputComboBox.Items);
call GLOBAL_$$_GLOBALMIXTURE$$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register r13 allocated
movq %rax,%r13
# Register rax released
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call GLOBAL_$$_GLOBALMIXTURE$$***@PLT
# Register rcx,rdx,rsi,r8,r9,r10,r11 released
# Register rdi allocated
movq %rax,%rdi
# Register rax released
# Register rsi allocated
leaq .Ld2(%rip),%rsi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call MNODE$_$TMNODE_$__$$_INDEXOFSETTING$ANSISTRING$$***@PLT
# Register rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
# Register r12d allocated
movl %eax,%r12d
# Register eax released
# Register rax allocated
movq 2048(%r15),%rax
# Register rbx allocated
movq 1424(%rax),%rbx
# Register rax released
# Register rax,r14,r14 allocated
movq %r13,%r14
# Register rax released
testq %r13,%r13
jne .Lj6
# Register rflags released
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call ***@PLT
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 released
.Lj6:
# Register rax allocated
movq (%r14),%r14
# Register r14 released
# Register r14 allocated
# Register rax released
# Register rdi allocated
movq %r14,%rdi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call ***@PLT
# Register rax,rcx,rdx,rsi,r8,r9,r10,r11 released
# Register rdi allocated
movq %r13,%rdi
# Register r13 released
# Register rdx allocated
movq %rbx,%rdx
# Register rbx released
# Register esi allocated
movl %r12d,%esi
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
call *744(%r14)
# Register rax,rcx,rdx,rsi,rdi,r8,r9,r10,r11 allocated
.Ll5:

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://l
Florian Klämpfl
2017-05-21 20:25:28 UTC
Permalink
Raw Message
Post by C Western
Post by Florian Klämpfl
Post by Florian Klämpfl
Post by C Western
Post by Florian Klämpfl
Can you please change line 26 in fpc compiler/x86/aoptx86.pas from
{ $define DEBUG_AOPTCPU}
to
{$define DEBUG_AOPTCPU}
and post the assembler output again?
See below.
Thanks. Last request ;): please send me the same output but compile additionally with -Oonopeephole
Can you please test without the -Oonopeephole and r36284?
This now works - excellent!
Good. Thanks for the analysis.

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
http://lists

Loading...