Browse Source

* x86: Improved TrySwapMovCmp to insert before
the FLAGS register is allocated.

J. Gareth "Curious Kit" Moreton 3 years ago
parent
commit
1fd2d3a83d
1 changed files with 9 additions and 4 deletions
  1. 9 4
      compiler/x86/aoptx86.pas

+ 9 - 4
compiler/x86/aoptx86.pas

@@ -7920,11 +7920,16 @@ unit aoptx86;
       { The instruction can be safely moved }
       asml.Remove(hp1);
 
-      { Try to insert after the last instructions where the FLAGS register is not yet in use }
-      if not GetLastInstruction(p, hp2) then
-        asml.InsertBefore(hp1, p)
+      { Try to insert before the FLAGS register is allocated, so "mov $0,%reg"
+        can be optimised into "xor %reg,%reg" later }
+      if SetAndTest(FindRegAllocBackward(NR_DEFAULTFLAGS, tai(p.Previous)), hp2) then
+        asml.InsertBefore(hp1, hp2)
       else
-        asml.InsertAfter(hp1, hp2);
+        { Note, if p.Previous is nil (even if it should logically never be the
+          case), FindRegAllocBackward immediately exits with False and so we
+          safely land here (we can't just pass p because FindRegAllocBackward
+          immediately exits on an instruction). [Kit] }
+        asml.InsertBefore(hp1, p);
 
       DebugMsg(SPeepholeOptimization + 'Swapped ' + debug_op2str(taicpu(p).opcode) + ' and ' + debug_op2str(taicpu(hp1).opcode) + ' instructions to improve optimisation potential', hp1);