aoptcpu.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the Z80 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. {$i fpcdefs.inc}
  20. {$define DEBUG_AOPTCPU}
  21. Interface
  22. uses cpubase, cgbase, aasmtai, aopt,AoptObj, aoptcpub;
  23. Type
  24. TCpuAsmOptimizer = class(TAsmOptimizer)
  25. { outputs a debug message into the assembler file }
  26. procedure DebugMsg(const s: string; p: tai);
  27. Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
  28. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  29. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  30. { uses the same constructor as TAopObj }
  31. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  32. procedure PeepHoleOptPass2;override;
  33. End;
  34. Implementation
  35. uses
  36. cutils,
  37. verbose,
  38. cpuinfo,
  39. aasmbase,aasmcpu,aasmdata,
  40. globals,globtype,
  41. cgutils;
  42. type
  43. TAsmOpSet = set of TAsmOp;
  44. function CanBeCond(p : tai) : boolean;
  45. begin
  46. result:=(p.typ=ait_instruction) and (taicpu(p).condition=C_None);
  47. end;
  48. function RefsEqual(const r1, r2: treference): boolean;
  49. begin
  50. refsequal :=
  51. (r1.offset = r2.offset) and
  52. (r1.base = r2.base) and
  53. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  54. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  55. (r1.relsymbol = r2.relsymbol);
  56. end;
  57. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  58. begin
  59. result:=oper1.typ=oper2.typ;
  60. if result then
  61. case oper1.typ of
  62. top_const:
  63. Result:=oper1.val = oper2.val;
  64. top_reg:
  65. Result:=oper1.reg = oper2.reg;
  66. top_ref:
  67. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  68. else Result:=false;
  69. end
  70. end;
  71. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  72. begin
  73. result := (oper.typ = top_reg) and (oper.reg = reg);
  74. end;
  75. function MatchInstruction(const instr: tai; const op: TAsmOp): boolean;
  76. begin
  77. result :=
  78. (instr.typ = ait_instruction) and
  79. (taicpu(instr).opcode = op);
  80. end;
  81. function MatchInstruction(const instr: tai; const ops: TAsmOpSet): boolean;
  82. begin
  83. result :=
  84. (instr.typ = ait_instruction) and
  85. (taicpu(instr).opcode in ops);
  86. end;
  87. function MatchInstruction(const instr: tai; const ops: TAsmOpSet;opcount : byte): boolean;
  88. begin
  89. result :=
  90. (instr.typ = ait_instruction) and
  91. (taicpu(instr).opcode in ops) and
  92. (taicpu(instr).ops=opcount);
  93. end;
  94. function MatchOpType(const instr : tai;ot0,ot1 : toptype) : Boolean;
  95. begin
  96. Result:=(taicpu(instr).ops=2) and
  97. (taicpu(instr).oper[0]^.typ=ot0) and
  98. (taicpu(instr).oper[1]^.typ=ot1);
  99. end;
  100. {$ifdef DEBUG_AOPTCPU}
  101. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  102. begin
  103. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  104. end;
  105. {$else DEBUG_AOPTCPU}
  106. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  107. begin
  108. end;
  109. {$endif DEBUG_AOPTCPU}
  110. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  111. var Next: tai; reg: TRegister): Boolean;
  112. begin
  113. Next:=Current;
  114. repeat
  115. Result:=GetNextInstruction(Next,Next);
  116. until not(cs_opt_level3 in current_settings.optimizerswitches) or not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  117. (is_calljmp(taicpu(Next).opcode));
  118. end;
  119. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  120. var
  121. p: taicpu;
  122. begin
  123. if not assigned(hp) or
  124. (hp.typ <> ait_instruction) then
  125. begin
  126. Result := false;
  127. exit;
  128. end;
  129. internalerror(2017032606);
  130. //p := taicpu(hp);
  131. //Result := ((p.opcode in [A_LDI,A_MOV,A_LDS]) and (reg=p.oper[0]^.reg) and ((p.oper[1]^.typ<>top_reg) or (reg<>p.oper[0]^.reg))) or
  132. // ((p.opcode in [A_LD,A_LDD,A_LPM]) and (reg=p.oper[0]^.reg) and not(RegInRef(reg,p.oper[1]^.ref^))) or
  133. // ((p.opcode in [A_MOVW]) and ((reg=p.oper[0]^.reg) or (TRegister(ord(reg)+1)=p.oper[0]^.reg)) and not(reg=p.oper[1]^.reg) and not(TRegister(ord(reg)+1)=p.oper[1]^.reg)) or
  134. // ((p.opcode in [A_POP]) and (reg=p.oper[0]^.reg));
  135. end;
  136. function TCpuAsmOptimizer.InstructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  137. var
  138. p: taicpu;
  139. i: longint;
  140. begin
  141. Result := false;
  142. internalerror(2017032607);
  143. //if not (assigned(hp) and (hp.typ = ait_instruction)) then
  144. // exit;
  145. //p:=taicpu(hp);
  146. //
  147. //i:=0;
  148. //
  149. //{ we do not care about the stack pointer }
  150. //if p.opcode in [A_POP] then
  151. // exit;
  152. //
  153. //{ first operand only written?
  154. // then skip it }
  155. //if p.opcode in [A_MOV,A_LD,A_LDD,A_LDS,A_LPM,A_LDI,A_MOVW] then
  156. // i:=1;
  157. //
  158. //while(i<p.ops) do
  159. // begin
  160. // case p.oper[I]^.typ of
  161. // top_reg:
  162. // Result := (p.oper[I]^.reg = reg) or
  163. // { MOVW }
  164. // ((i=1) and (p.opcode=A_MOVW) and (getsupreg(p.oper[0]^.reg)+1=getsupreg(reg)));
  165. // top_ref:
  166. // Result :=
  167. // (p.oper[I]^.ref^.base = reg) or
  168. // (p.oper[I]^.ref^.index = reg);
  169. // end;
  170. // { Bailout if we found something }
  171. // if Result then
  172. // exit;
  173. // Inc(I);
  174. // end;
  175. end;
  176. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  177. var
  178. hp1,hp2,hp3,hp4,hp5: tai;
  179. alloc, dealloc: tai_regalloc;
  180. i: integer;
  181. l: TAsmLabel;
  182. //TmpUsedRegs : TAllUsedRegs;
  183. begin
  184. result := false;
  185. //case p.typ of
  186. // ait_instruction:
  187. // begin
  188. // end;
  189. //end;
  190. end;
  191. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  192. begin
  193. end;
  194. begin
  195. casmoptimizer:=TCpuAsmOptimizer;
  196. End.