aoptcpu.pas 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. begin
  140. Result := false;
  141. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  142. exit;
  143. p:=taicpu(hp);
  144. case p.opcode of
  145. A_LD,A_BIT,A_SET,A_RES:
  146. begin
  147. if p.ops<>2 then
  148. internalerror(2020051102);
  149. result:=((p.oper[0]^.typ=top_ref) and RegInRef(reg,p.oper[0]^.ref^)) or
  150. RegInOp(reg,p.oper[1]^);
  151. end;
  152. A_PUSH,A_INC,A_DEC,A_RLC,A_RRC,A_SLA,A_SRA,A_SRL:
  153. begin
  154. if p.ops<>1 then
  155. internalerror(2020051103);
  156. result:=RegInOp(reg,p.oper[0]^);
  157. end;
  158. A_POP:
  159. result:=(reg=NR_SP);
  160. A_EX,A_ADD,A_SUB,A_AND,A_OR,A_XOR,A_CP:
  161. begin
  162. if p.ops<>2 then
  163. internalerror(2020051104);
  164. result:=RegInOp(reg,p.oper[0]^) or
  165. RegInOp(reg,p.oper[1]^);
  166. end;
  167. A_EXX:
  168. result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_DE) or SuperRegistersEqual(reg,NR_HL) or
  169. SuperRegistersEqual(reg,NR_BC_) or SuperRegistersEqual(reg,NR_DE_) or SuperRegistersEqual(reg,NR_HL_);
  170. A_LDI,A_LDIR,A_LDD,A_LDDR:
  171. result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_DE) or SuperRegistersEqual(reg,NR_HL);
  172. A_CPI,A_CPIR,A_CPD,A_CPDR:
  173. result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_HL) or RegistersInterfere(reg,NR_A);
  174. A_ADC,A_SBC:
  175. begin
  176. if p.ops<>2 then
  177. internalerror(2020051105);
  178. result:=RegInOp(reg,p.oper[0]^) or
  179. RegInOp(reg,p.oper[1]^) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  180. end;
  181. A_DAA:
  182. result:=RegistersInterfere(reg,NR_A) or (reg=NR_CARRYFLAG) or (reg=NR_HALFCARRYFLAG) or (reg=NR_ADDSUBTRACTFLAG) or (reg=NR_DEFAULTFLAGS);
  183. A_CPL,A_NEG,A_RLCA,A_RRCA:
  184. result:=RegistersInterfere(reg,NR_A);
  185. A_CCF:
  186. result:=(reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  187. A_SCF,A_NOP,A_HALT,A_DI,A_EI,A_IM:
  188. result:=false;
  189. A_RLA,A_RRA:
  190. result:=RegistersInterfere(reg,NR_A) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  191. A_RL,A_RR:
  192. begin
  193. if p.ops<>1 then
  194. internalerror(2020051106);
  195. result:=RegInOp(reg,p.oper[0]^) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  196. end;
  197. A_RLD,A_RRD:
  198. result:=RegistersInterfere(reg,NR_A) or RegistersInterfere(reg,NR_HL);
  199. A_JP,A_JR:
  200. begin
  201. if p.ops<>1 then
  202. internalerror(2020051107);
  203. if RegInOp(reg,p.oper[0]^) then
  204. result:=true
  205. else
  206. case p.condition of
  207. C_None:
  208. result:=false;
  209. C_NZ,C_Z:
  210. result:=(reg=NR_ZEROFLAG) or (reg=NR_DEFAULTFLAGS);
  211. C_NC,C_C:
  212. result:=(reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  213. C_PO,C_PE:
  214. result:=(reg=NR_PARITYOVERFLOWFLAG) or (reg=NR_DEFAULTFLAGS);
  215. C_P,C_M:
  216. result:=(reg=NR_SIGNFLAG) or (reg=NR_DEFAULTFLAGS);
  217. end;
  218. end;
  219. A_DJNZ:
  220. result:=RegistersInterfere(reg,NR_B);
  221. A_CALL,A_RET,A_RETI,A_RETN,A_RST:
  222. result:=true;
  223. A_IN:
  224. begin
  225. if p.ops<>2 then
  226. internalerror(2020051109);
  227. result:=(p.oper[1]^.typ=top_ref) and (p.oper[1]^.ref^.base=NR_C) and RegistersInterfere(reg,NR_BC);
  228. end;
  229. A_OUT:
  230. begin
  231. if p.ops<>2 then
  232. internalerror(2020051110);
  233. result:=RegInOp(reg,p.oper[1]^) or (p.oper[0]^.typ=top_ref) and (p.oper[0]^.ref^.base=NR_C) and RegistersInterfere(reg,NR_BC);
  234. end;
  235. A_INI,A_INIR,A_IND,A_INDR,A_OUTI,A_OTIR,A_OUTD,A_OTDR:
  236. result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_HL);
  237. else
  238. internalerror(2020051101);
  239. end;
  240. end;
  241. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  242. var
  243. hp1,hp2,hp3,hp4,hp5: tai;
  244. alloc, dealloc: tai_regalloc;
  245. i: integer;
  246. l: TAsmLabel;
  247. //TmpUsedRegs : TAllUsedRegs;
  248. begin
  249. result := false;
  250. //case p.typ of
  251. // ait_instruction:
  252. // begin
  253. // end;
  254. //end;
  255. end;
  256. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  257. begin
  258. end;
  259. begin
  260. casmoptimizer:=TCpuAsmOptimizer;
  261. End.