aoptcpu.pas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the RiscV64 optimizer object
  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. interface
  20. {$I fpcdefs.inc}
  21. {$define DEBUG_AOPTCPU}
  22. uses
  23. cpubase,
  24. globals, globtype,
  25. cgbase,
  26. aoptobj, aoptcpub, aopt,
  27. aasmtai, aasmcpu;
  28. type
  29. TCpuAsmOptimizer = class(TAsmOptimizer)
  30. function RegLoadedWithNewValue(reg: tregister; hp: tai): boolean; override;
  31. Function GetNextInstructionUsingReg(Current: tai; Out Next: tai; reg: TRegister): Boolean;
  32. { outputs a debug message into the assembler file }
  33. procedure DebugMsg(const s: string; p: tai);
  34. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  35. end;
  36. implementation
  37. uses
  38. cutils;
  39. {$ifdef DEBUG_AOPTCPU}
  40. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  41. begin
  42. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  43. end;
  44. {$else DEBUG_AOPTCPU}
  45. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  46. begin
  47. end;
  48. {$endif DEBUG_AOPTCPU}
  49. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  50. begin
  51. result:=
  52. (hp.typ=ait_instruction) and
  53. (taicpu(hp).ops>1) and
  54. (taicpu(hp).oper[0]^.typ=top_reg) and
  55. (taicpu(hp).oper[0]^.reg=reg) and
  56. (taicpu(hp).spilling_get_operation_type(0)=operand_write);
  57. end;
  58. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  59. begin
  60. Next:=Current;
  61. repeat
  62. Result:=GetNextInstruction(Next,Next);
  63. until not (Result) or
  64. not(cs_opt_level3 in current_settings.optimizerswitches) or
  65. (Next.typ<>ait_instruction) or
  66. RegInInstruction(reg,Next) or
  67. is_calljmp(taicpu(Next).opcode);
  68. end;
  69. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  70. var
  71. hp1: tai;
  72. begin
  73. result:=false;
  74. case p.typ of
  75. ait_instruction:
  76. begin
  77. case taicpu(p).opcode of
  78. A_ADDI:
  79. begin
  80. {
  81. Changes
  82. addi x, y, #
  83. addi z, x, #
  84. dealloc x
  85. To
  86. addi z, y, #+#
  87. }
  88. if (taicpu(p).ops=3) and
  89. (taicpu(p).oper[2]^.typ=top_const) and
  90. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  91. (hp1.typ=ait_instruction) and
  92. (taicpu(hp1).opcode=A_ADDI) and
  93. (taicpu(hp1).ops=3) and
  94. (taicpu(p).oper[2]^.typ=top_const) and
  95. is_imm12(taicpu(p).oper[2]^.val+taicpu(hp1).oper[2]^.val) and
  96. (not RegModifiedBetween(taicpu(p).oper[1]^.reg, p,hp1)) and
  97. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  98. begin
  99. taicpu(hp1).loadreg(1,taicpu(p).oper[1]^.reg);
  100. taicpu(hp1).loadconst(2, taicpu(p).oper[2]^.val+taicpu(hp1).oper[2]^.val);
  101. DebugMsg('Peephole AddiAddi2Addi performed', hp1);
  102. GetNextInstruction(p,hp1);
  103. AsmL.Remove(p);
  104. p.Free;
  105. p:=hp1;
  106. result:=true;
  107. end
  108. {
  109. Changes
  110. addi x, x, (ref)
  111. ld/sd y, 0(x)
  112. dealloc x
  113. To
  114. ld/sd y, 0(ref)(x)
  115. }
  116. else if (taicpu(p).ops=3) and
  117. (taicpu(p).oper[2]^.typ=top_ref) and
  118. (taicpu(p).oper[0]^.reg=taicpu(p).oper[1]^.reg) and
  119. GetNextInstructionUsingReg(p, hp1, taicpu(p).oper[0]^.reg) and
  120. (hp1.typ=ait_instruction) and
  121. (taicpu(hp1).opcode in [A_LB,A_LBU,A_LH,A_LHU,A_LW,A_LWU,A_LD,
  122. A_SB,A_SH,A_SW,A_SD]) and
  123. (taicpu(hp1).ops=2) and
  124. (taicpu(hp1).oper[1]^.typ=top_ref) and
  125. (taicpu(hp1).oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  126. (taicpu(hp1).oper[1]^.ref^.offset=0) and
  127. (not RegModifiedBetween(taicpu(p).oper[1]^.reg, p,hp1)) and
  128. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(hp1)) then
  129. begin
  130. taicpu(hp1).loadref(1,taicpu(p).oper[2]^.ref^);
  131. taicpu(hp1).oper[1]^.ref^.base:=taicpu(p).oper[0]^.reg;
  132. DebugMsg('Peephole AddiMem2Mem performed', hp1);
  133. GetNextInstruction(p,hp1);
  134. AsmL.Remove(p);
  135. p.Free;
  136. p:=hp1;
  137. result:=true;
  138. end;
  139. end;
  140. A_ANDI:
  141. begin
  142. end;
  143. end;
  144. end;
  145. end;
  146. end;
  147. begin
  148. casmoptimizer := TCpuAsmOptimizer;
  149. end.