aoptcpu.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the LoongArch64 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, cutils,
  26. aoptobj, aoptcpub, aopt,
  27. aasmtai, aasmcpu;
  28. type
  29. TCpuAsmOptimizer = class(TAsmOptimizer)
  30. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  31. function RegLoadedWithNewValue(reg: tregister; hp: tai): boolean; override;
  32. function GetNextInstructionUsingReg(Current: tai; Out Next: tai; reg: TRegister): Boolean;
  33. function OptPass1Mov(var p: tai): Boolean;
  34. end;
  35. implementation
  36. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  37. var
  38. p: taicpu;
  39. begin
  40. result:=false;
  41. if not ((assigned(hp)) and (hp.typ = ait_instruction)) then
  42. exit;
  43. p := taicpu(hp);
  44. if p.ops=0 then
  45. exit;
  46. case p.oper[0]^.typ of
  47. top_reg:
  48. result:=(SuperRegistersEqual(p.oper[0]^.reg,reg)) and
  49. (p.spilling_get_operation_type(0)<>operand_read);
  50. else
  51. ;
  52. end;
  53. end;
  54. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai; out Next: tai; reg: TRegister): Boolean;
  55. begin
  56. Next:=Current;
  57. repeat
  58. Result:=GetNextInstruction(Next,Next);
  59. until not (Result) or
  60. not(cs_opt_level3 in current_settings.optimizerswitches) or
  61. (Next.typ<>ait_instruction) or
  62. RegInInstruction(reg,Next) or
  63. is_calljmp(taicpu(Next).opcode);
  64. end;
  65. function MatchInstruction(const instr: tai; const op: TAsmOp; const AConditions: TAsmConds = []): boolean;
  66. begin
  67. result :=
  68. (instr.typ = ait_instruction) and
  69. (taicpu(instr).opcode = op) and
  70. ((AConditions=[]) or (taicpu(instr).condition in AConditions));
  71. end;
  72. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  73. begin
  74. result := oper1.typ = oper2.typ;
  75. if result then
  76. case oper1.typ of
  77. top_const:
  78. Result:=oper1.val = oper2.val;
  79. top_reg:
  80. Result:=oper1.reg = oper2.reg;
  81. {top_ref:
  82. Result:=RefsEqual(oper1.ref^, oper2.ref^);}
  83. else Result:=false;
  84. end
  85. end;
  86. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  87. begin
  88. result := (oper.typ = top_reg) and (oper.reg = reg);
  89. end;
  90. function TCpuAsmOptimizer.OptPass1Mov(var p: tai): Boolean;
  91. var
  92. hp1: tai;
  93. alloc, dealloc: tai_regalloc;
  94. begin
  95. {
  96. change
  97. mov reg0,reg1
  98. mov reg1,reg0
  99. into
  100. mov reg0,reg1
  101. }
  102. Result := False;
  103. while GetNextInstruction(p, hp1) and
  104. MatchInstruction(hp1, A_MOVE, [taicpu(p).condition]) and
  105. MatchOperand(taicpu(p).oper[0]^, taicpu(hp1).oper[1]^) and
  106. MatchOperand(taicpu(p).oper[1]^, taicpu(hp1).oper[0]^) do
  107. begin
  108. asml.Remove(hp1);
  109. hp1.free;
  110. Result:=true;
  111. end;
  112. end;
  113. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  114. begin
  115. result := false;
  116. if p.typ=ait_instruction then
  117. begin
  118. case taicpu(p).opcode of
  119. A_MOVE:
  120. Result:=OptPass1Mov(p);
  121. else
  122. ;
  123. end;
  124. end;
  125. end;
  126. begin
  127. casmoptimizer := TCpuAsmOptimizer;
  128. end.