View Issue Details

IDProjectCategoryView StatusLast Update
0039271FPCCompilerpublic2021-07-20 11:52
ReporterJ. Gareth Moreton Assigned To 
PrioritylowSeveritytweakReproducibilityN/A
Status newResolutionopen 
Platformi386 and x86_64OSMicrosoft Windows 
Product Version3.3.1 
Summary0039271: [Patch] x86: SETcc/TEST/SETcc -> SETcc/SETcc optimisation
DescriptionThis patch extends the SETcc/TEST/Jcc optimisation to also work with SETcc instead of just Jcc.
Steps To ReproduceApply patch and confirm correct compilation.
Additional InformationThis particular optimisation only appears a few times in the RTL, but the addition to the compiler source is very cheap.

In Sysutils, before:

    ...
    btl %eax,TC_$SYSTEM_$$_ALLOWDIRECTORYSEPARATORS(%rip)
    setcb %al
    testb %al,%al
# Peephole Optimization: J(c)Mov1JmpMov0 -> Set(~c) (partial)
    setneb %al
    ret
.Lj1274:
    ...

After:

    ...
    btl %eax,TC_$SYSTEM_$$_ALLOWDIRECTORYSEPARATORS(%rip)
# Peephole Optimization: J(c)Mov1JmpMov0 -> Set(~c) (partial)
# Peephole Optimization: SETcc/TEST/SETcc -> SETcc
    setcb %al
    ret
.Lj1274:
    ...
Tagscompiler, i386, optimization, patch, x86, x86_64
Fixed in Revision
FPCOldBugId
FPCTarget-
Attached Files

Activities

J. Gareth Moreton

2021-07-20 11:51

developer  

setcc-test-setcc.patch (1,963 bytes)   
Index: compiler/x86/aoptx86.pas
===================================================================
--- compiler/x86/aoptx86.pas	(revision 49621)
+++ compiler/x86/aoptx86.pas	(working copy)
@@ -7186,7 +7186,7 @@
               (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
               (taicpu(p).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) and
               GetNextInstruction(hp1, hp2) and
-              MatchInstruction(hp2, A_Jcc, []) then
+              MatchInstruction(hp2, A_Jcc, A_SETcc, []) then
               { Change from:             To:
 
                 set(C) %reg              j(~C) label
@@ -7197,6 +7197,8 @@
                 set(C) %reg              j(C)  label
                 test   %reg,%reg/cmp $0,%reg
                 jne    label
+
+                (Also do something similar with sete/setne instead of je/jne)
               }
               begin
                 { Before we do anything else, we need to check the instructions
@@ -7422,10 +7424,16 @@
                 if not TmpUsedRegs[getregtype(taicpu(p).oper[0]^.reg)].IsUsed(taicpu(p).oper[0]^.reg) then
                   begin
                     RemoveCurrentp(p, hp2);
-                    DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> Jcc',p);
+                    if taicpu(hp2).opcode = A_SETcc then
+                      DebugMsg(SPeepholeOptimization + 'SETcc/TEST/SETcc -> SETcc',p)
+                    else
+                      DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> Jcc',p);
                   end
                 else
-                  DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> SETcc/Jcc',p);
+                  if taicpu(hp2).opcode = A_SETcc then
+                    DebugMsg(SPeepholeOptimization + 'SETcc/TEST/SETcc -> SETcc/SETcc',p)
+                  else
+                    DebugMsg(SPeepholeOptimization + 'SETcc/TEST/Jcc -> SETcc/Jcc',p);
 
                 Result := True;
               end
setcc-test-setcc.patch (1,963 bytes)   

J. Gareth Moreton

2021-07-20 11:52

developer   ~0131954

Note, it's only turned into SETcc/SETcc if the first register is different to the one in the second SETcc and isn't discarded after the TEST instruction.

Issue History

Date Modified Username Field Change
2021-07-20 11:51 J. Gareth Moreton New Issue
2021-07-20 11:51 J. Gareth Moreton File Added: setcc-test-setcc.patch
2021-07-20 11:51 J. Gareth Moreton Tag Attached: patch
2021-07-20 11:51 J. Gareth Moreton Tag Attached: compiler
2021-07-20 11:51 J. Gareth Moreton Tag Attached: optimization
2021-07-20 11:51 J. Gareth Moreton Tag Attached: i386
2021-07-20 11:51 J. Gareth Moreton Tag Attached: x86_64
2021-07-20 11:51 J. Gareth Moreton Tag Attached: x86
2021-07-20 11:51 J. Gareth Moreton Priority normal => low
2021-07-20 11:51 J. Gareth Moreton Severity minor => tweak
2021-07-20 11:51 J. Gareth Moreton FPCTarget => -
2021-07-20 11:52 J. Gareth Moreton Note Added: 0131954