aoptcpu.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. {
  2. Copyright (c) 1998-2014 by the Free Pascal development team
  3. This unit calls the optimization procedures to optimize the assembler
  4. code for m68k
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit aoptcpu;
  19. {$i fpcdefs.inc}
  20. {$define DEBUG_AOPTCPU}
  21. Interface
  22. uses
  23. cpubase, aoptobj, aoptcpub, aopt, aasmtai;
  24. Type
  25. TCpuAsmOptimizer = class(TAsmOptimizer)
  26. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  27. { outputs a debug message into the assembler file }
  28. procedure DebugMsg(const s: string; p: tai);
  29. End;
  30. Implementation
  31. uses
  32. cutils, aasmcpu, cgutils;
  33. {$ifdef DEBUG_AOPTCPU}
  34. procedure TCpuAsmOptimizer.DebugMsg(const s: string; p : tai);
  35. begin
  36. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  37. end;
  38. {$else DEBUG_AOPTCPU}
  39. procedure TCpuAsmOptimizer.DebugMsg(const s: string; p : tai);inline;
  40. begin
  41. end;
  42. {$endif DEBUG_AOPTCPU}
  43. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  44. var
  45. next: tai;
  46. tmpref: treference;
  47. begin
  48. result:=false;
  49. case p.typ of
  50. ait_instruction:
  51. begin
  52. //asml.insertbefore(tai_comment.Create(strpnew('pass1 called for instr')), p);
  53. case taicpu(p).opcode of
  54. { LEA (Ax),Ax is a NOP if src and dest reg is equal, so remove it. }
  55. A_LEA:
  56. if GetNextInstruction(p,next) and not assigned(taicpu(p).oper[0]^.ref^.symbol) and
  57. (((taicpu(p).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  58. (taicpu(p).oper[0]^.ref^.index = NR_NO)) or
  59. ((taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  60. (taicpu(p).oper[0]^.ref^.base = NR_NO))) and
  61. (taicpu(p).oper[0]^.ref^.offset = 0) then
  62. begin
  63. DebugMsg('Optimizer: LEA 0(Ax),Ax removed',p);
  64. asml.remove(p);
  65. p.free;
  66. p:=next;
  67. result:=true;
  68. end;
  69. { Address register sub/add can be replaced with ADDQ/SUBQ or LEA if the value is in the
  70. SmallInt range, which is shorter to encode and faster to execute on most 68k }
  71. A_SUB,A_SUBA,A_ADD,A_ADDA:
  72. if (taicpu(p).oper[1]^.typ = top_reg) and isaddressregister(taicpu(p).oper[1]^.reg) and
  73. (taicpu(p).oper[0]^.typ = top_const) then
  74. begin
  75. if isvalueforaddqsubq(taicpu(p).oper[0]^.val) then
  76. begin
  77. DebugMsg('Optimizer: SUB/ADD #val,Ax to SUBQ/ADDQ',p);
  78. taicpu(p).opsize:=S_L; // this is safe, because we're targetting an address reg
  79. if taicpu(p).opcode in [A_ADD,A_ADDA] then
  80. taicpu(p).opcode:=A_ADDQ
  81. else
  82. taicpu(p).opcode:=A_SUBQ;
  83. result:=true;
  84. end
  85. else
  86. if isvalue16bit(abs(taicpu(p).oper[0]^.val)) then
  87. begin
  88. DebugMsg('Optimizer: SUB/ADD #val,Ax to LEA val(Ax),Ax',p);
  89. if taicpu(p).opcode in [A_SUB,A_SUBA] then
  90. reference_reset_base(tmpref,taicpu(p).oper[1]^.reg,-taicpu(p).oper[0]^.val,0)
  91. else
  92. reference_reset_base(tmpref,taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.val,0);
  93. taicpu(p).opcode:=A_LEA;
  94. taicpu(p).loadref(0,tmpref);
  95. result:=true;
  96. end;
  97. end;
  98. { MOVEA #0,Ax to SUBA Ax,Ax, because it's shorter }
  99. A_MOVEA:
  100. if (taicpu(p).oper[0]^.typ = top_const) and
  101. (taicpu(p).oper[0]^.val = 0) then
  102. begin
  103. DebugMsg('Optimizer: MOVEA #0,Ax to SUBA Ax,Ax',p);
  104. taicpu(p).opcode:=A_SUBA;
  105. taicpu(p).opsize:=S_L; { otherwise it will be .W -> BOOM }
  106. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  107. result:=true;
  108. end;
  109. { CMP #0,<ea> equals to TST <ea>, just shorter and TST is more flexible anyway }
  110. A_CMP:
  111. if (taicpu(p).oper[0]^.typ = top_const) and
  112. (taicpu(p).oper[0]^.val = 0) then
  113. begin
  114. DebugMsg('Optimizer: CMP #0 to TST',p);
  115. taicpu(p).opcode:=A_TST;
  116. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  117. taicpu(p).clearop(1);
  118. taicpu(p).ops:=1;
  119. result:=true;
  120. end;
  121. end;
  122. end;
  123. end;
  124. end;
  125. begin
  126. casmoptimizer:=TCpuAsmOptimizer;
  127. end.