From 124c64152d256c1e62a1f13103c2bf5cafb33155 Mon Sep 17 00:00:00 2001 From: florian Date: Sun, 12 Jan 2020 09:20:01 +0000 Subject: [PATCH] * patch by J. Gareth Moreton: EAX -> EDX:EAX sign extension shortcuts, and MOVSX shortcuts for AX register, part 2 of #36551 git-svn-id: trunk@43918 - --- compiler/i386/aoptcpu.pas | 2 ++ compiler/x86/aoptx86.pas | 45 +++++++++++++++++++++++++++++++++++++ compiler/x86_64/aoptcpu.pas | 2 ++ 3 files changed, 49 insertions(+) diff --git a/compiler/i386/aoptcpu.pas b/compiler/i386/aoptcpu.pas index 700f08dcff..6080e2eaa2 100644 --- a/compiler/i386/aoptcpu.pas +++ b/compiler/i386/aoptcpu.pas @@ -324,6 +324,8 @@ unit aoptcpu; end; A_TEST, A_OR: Result:=PostPeepholeOptTestOr(p); + A_MOVSX: + Result:=PostPeepholeOptMOVSX(p); else ; end; diff --git a/compiler/x86/aoptx86.pas b/compiler/x86/aoptx86.pas index 73949b044a..c7cb2d9a1d 100644 --- a/compiler/x86/aoptx86.pas +++ b/compiler/x86/aoptx86.pas @@ -88,6 +88,7 @@ unit aoptx86; function PostPeepholeOptMovzx(var p : tai) : Boolean; function PostPeepholeOptXor(var p : tai) : Boolean; {$endif} + function PostPeepholeOptMOVSX(var p : tai) : boolean; function PostPeepholeOptCmp(var p : tai) : Boolean; function PostPeepholeOptTestOr(var p : tai) : Boolean; function PostPeepholeOptCall(var p : tai) : Boolean; @@ -5033,6 +5034,50 @@ unit aoptx86; end; + function TX86AsmOptimizer.PostPeepholeOptMOVSX(var p : tai) : boolean; + begin + Result := False; + if not MatchOpType(taicpu(p), top_reg, top_reg) then + Exit; + + { Convert: + movswl %ax,%eax -> cwtl + movslq %eax,%rax -> cdqe + + NOTE: Don't convert movswl %al,%ax to cbw, because cbw and cwde + refer to the same opcode and depends only on the assembler's + current operand-size attribute. [Kit] + } + with taicpu(p) do + case opsize of + S_WL: + if (oper[0]^.reg = NR_AX) and (oper[1]^.reg = NR_EAX) then + begin + DebugMsg(SPeepholeOptimization + 'Converted movswl %ax,%eax to cwtl', p); + opcode := A_CWDE; + clearop(0); + clearop(1); + ops := 0; + Result := True; + end; +{$ifdef x86_64} + S_LQ: + if (oper[0]^.reg = NR_EAX) and (oper[1]^.reg = NR_RAX) then + begin + DebugMsg(SPeepholeOptimization + 'Converted movslq %eax,%rax to cltq', p); + opcode := A_CDQE; + clearop(0); + clearop(1); + ops := 0; + Result := True; + end; +{$endif x86_64} + else + ; + end; + end; + + function TX86AsmOptimizer.PostPeepholeOptCmp(var p : tai) : Boolean; begin Result:=false; diff --git a/compiler/x86_64/aoptcpu.pas b/compiler/x86_64/aoptcpu.pas index d9bb1d6a58..76cd4d42aa 100644 --- a/compiler/x86_64/aoptcpu.pas +++ b/compiler/x86_64/aoptcpu.pas @@ -174,6 +174,8 @@ uses case taicpu(p).opcode of A_MOV: Result:=PostPeepholeOptMov(p); + A_MOVSX: + Result:=PostPeepholeOptMOVSX(p); A_MOVZX: Result:=PostPeepholeOptMovzx(p); A_CMP: