aoptcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. cgbase;
  25. Type
  26. TCpuAsmOptimizer = class(TAsmOptimizer)
  27. function RegLoadedWithNewValue(reg: tregister; hp: tai): boolean; override;
  28. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  29. function TryToOptimizeMove(var p: tai): boolean;
  30. function MaybeRealConstOperSimplify(var p: tai): boolean;
  31. { outputs a debug message into the assembler file }
  32. procedure DebugMsg(const s: string; p: tai);
  33. End;
  34. Implementation
  35. uses
  36. cutils, aasmcpu, cgutils, globals, verbose, cpuinfo, itcpugas;
  37. { Range check must be disabled explicitly as conversions between signed and unsigned
  38. 32-bit values are done without explicit typecasts }
  39. {$R-}
  40. function opname(var p: tai): string;
  41. begin
  42. result:=upcase(gas_op2str[taicpu(p).opcode]);
  43. end;
  44. function RefsEqual(const r1, r2: treference): boolean;
  45. begin
  46. RefsEqual :=
  47. (r1.offset = r2.offset) and
  48. (r1.base = r2.base) and
  49. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  50. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  51. (r1.relsymbol = r2.relsymbol) and
  52. (r1.volatility=[]) and
  53. (r2.volatility=[]);
  54. end;
  55. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  56. begin
  57. result := oper1.typ = oper2.typ;
  58. if result then
  59. case oper1.typ of
  60. top_const:
  61. Result:=oper1.val = oper2.val;
  62. top_reg:
  63. Result:=oper1.reg = oper2.reg;
  64. top_ref:
  65. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  66. else
  67. internalerror(2016112401);
  68. end
  69. end;
  70. function TCpuAsmOptimizer.MaybeRealConstOperSimplify(var p: tai): boolean;
  71. var
  72. tmpint64: int64;
  73. tmpsingle: single;
  74. begin
  75. result:=false;
  76. if (taicpu(p).oper[0]^.typ = top_realconst) then
  77. begin
  78. { if we work with actual integers, turn the operand into one }
  79. if frac(taicpu(p).oper[0]^.val_real) = 0 then
  80. begin
  81. tmpint64:=trunc(taicpu(p).oper[0]^.val_real);
  82. if (high(shortint) >= tmpint64) and (low(shortint) <= tmpint64) then
  83. begin
  84. taicpu(p).opsize := S_B;
  85. taicpu(p).oper[0]^.typ:=top_const;
  86. end
  87. else
  88. if (high(smallint) >= tmpint64) and (low(smallint) <= tmpint64) then
  89. begin
  90. taicpu(p).opsize := S_W;
  91. taicpu(p).oper[0]^.typ:=top_const;
  92. end
  93. else
  94. if (high(longint) >= tmpint64) and (low(longint) <= tmpint64) then
  95. begin
  96. taicpu(p).opsize := S_L;
  97. taicpu(p).oper[0]^.typ:=top_const;
  98. end;
  99. if (taicpu(p).oper[0]^.typ) = top_const then
  100. begin
  101. DebugMsg('Optimizer: FPU real const to integer',p);
  102. taicpu(p).oper[0]^.val:=tmpint64;
  103. result:=true;
  104. end;
  105. end
  106. else
  107. begin
  108. tmpsingle:=taicpu(p).oper[0]^.val_real;
  109. if (taicpu(p).opsize = S_FD) and
  110. ((taicpu(p).oper[0]^.val_real - tmpsingle) = 0.0) then
  111. begin
  112. DebugMsg('Optimizer: FPU real const to lesser precision',p);
  113. taicpu(p).opsize:=S_FS;
  114. result:=true;
  115. end;
  116. end;
  117. end;
  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. p := taicpu(hp);
  130. Result :=
  131. ((p.opcode in [A_MOVE,A_MOVEA,A_MVS,A_MVZ,A_MOVEQ,A_LEA]) and
  132. (p.oper[1]^.typ = top_reg) and
  133. (SuperRegistersEqual(p.oper[1]^.reg,reg)) and
  134. ((p.oper[0]^.typ = top_const) or
  135. ((p.oper[0]^.typ = top_reg) and
  136. not(SuperRegistersEqual(p.oper[0]^.reg,reg))) or
  137. ((p.oper[0]^.typ = top_ref) and
  138. not RegInRef(reg,p.oper[0]^.ref^)))) or
  139. ((p.opcode = A_FMOVE) and
  140. (p.oper[1]^.typ = top_reg) and
  141. (SuperRegistersEqual(p.oper[1]^.reg,reg)) and
  142. ((p.oper[0]^.typ = top_realconst) or
  143. ((p.oper[0]^.typ = top_reg) and
  144. not(SuperRegistersEqual(p.oper[0]^.reg,reg))))) or
  145. ((p.opcode = A_MOVEM) and
  146. (p.oper[1]^.typ = top_regset) and
  147. ((getsupreg(reg) in p.oper[1]^.dataregset) or
  148. (getsupreg(reg) in p.oper[1]^.addrregset))) or
  149. ((p.opcode = A_FMOVEM) and
  150. (p.oper[1]^.typ = top_regset) and
  151. (getsupreg(reg) in p.oper[1]^.fpuregset));
  152. end;
  153. {$ifdef DEBUG_AOPTCPU}
  154. procedure TCpuAsmOptimizer.DebugMsg(const s: string; p : tai);
  155. begin
  156. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  157. end;
  158. {$else DEBUG_AOPTCPU}
  159. procedure TCpuAsmOptimizer.DebugMsg(const s: string; p : tai);inline;
  160. begin
  161. end;
  162. {$endif DEBUG_AOPTCPU}
  163. function TCpuAsmOptimizer.TryToOptimizeMove(var p: tai): boolean;
  164. var
  165. next, next2: tai;
  166. opstr: string[15];
  167. begin
  168. result:=false;
  169. if GetNextInstruction(p,next) and
  170. (next.typ = ait_instruction) and
  171. (taicpu(next).opcode = taicpu(p).opcode) and
  172. (taicpu(p).opsize = taicpu(next).opsize) and
  173. (taicpu(p).oper[1]^.typ = top_reg) and
  174. MatchOperand(taicpu(p).oper[1]^,taicpu(next).oper[0]^) then
  175. begin
  176. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(next).oper[1]^)) and
  177. RegEndOfLife(taicpu(next).oper[0]^.reg, taicpu(next)) then
  178. begin
  179. opstr:=opname(p);
  180. case taicpu(p).oper[0]^.typ of
  181. top_reg:
  182. begin
  183. { move %reg0, %tmpreg; move %tmpreg, <ea> -> move %reg0, <ea> }
  184. taicpu(p).loadOper(1,taicpu(next).oper[1]^);
  185. asml.remove(next);
  186. next.free;
  187. result:=true;
  188. { also remove leftover move %reg0, %reg0, which can occur as the result
  189. of the previous optimization, if %reg0 and %tmpreg was different types
  190. (addr vs. data), so these moves were left in by the cg }
  191. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  192. begin
  193. DebugMsg('Optimizer: '+opstr+' + '+opstr+' removed',p);
  194. GetNextInstruction(p,next);
  195. asml.remove(p);
  196. p.free;
  197. p:=next;
  198. end
  199. else
  200. DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #1',p)
  201. end;
  202. top_const:
  203. begin
  204. // DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #2',p);
  205. end;
  206. top_ref:
  207. begin
  208. { move ref, %tmpreg; move %tmpreg, <ea> -> move ref, <ea> }
  209. { we only want to do this when <ea> is a reg or a simple reference }
  210. with taicpu(next).oper[1]^ do
  211. if (taicpu(next).opcode <> A_FMOVE) and
  212. ((typ = top_reg) or
  213. ((typ = top_ref) and
  214. ((ref^.index = NR_NO) or
  215. (ref^.base = NR_NO)) and
  216. (ref^.symbol = nil) and
  217. (ref^.offset = 0))) then
  218. begin
  219. DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #3',p);
  220. taicpu(p).loadOper(1,taicpu(next).oper[1]^);
  221. asml.remove(next);
  222. next.free;
  223. result:=true;
  224. end;
  225. end;
  226. end;
  227. end;
  228. exit;
  229. end;
  230. if GetNextInstruction(p,next) and
  231. (next.typ = ait_instruction) and
  232. GetNextInstruction(next,next2) and
  233. (next2.typ = ait_instruction) and
  234. (taicpu(next).opcode <> taicpu(p).opcode) and
  235. (taicpu(next2).opcode = taicpu(p).opcode) and
  236. (taicpu(p).oper[0]^.typ = top_reg) and
  237. (taicpu(p).oper[1]^.typ = top_reg) and
  238. (getregtype(taicpu(p).oper[0]^.reg) = getregtype(taicpu(p).oper[1]^.reg)) and
  239. MatchOperand(taicpu(p).oper[1]^,taicpu(next2).oper[0]^) and
  240. MatchOperand(taicpu(next2).oper[1]^,taicpu(p).oper[0]^) and
  241. (taicpu(p).opsize = taicpu(next2).opsize) and
  242. ((taicpu(p).opcode = A_FMOVE) or
  243. (taicpu(p).opsize = taicpu(next).opsize)) then
  244. begin
  245. opstr:=opname(p);
  246. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(next2).oper[1]^)) and
  247. not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(next).oper[0]^)) and
  248. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(next2)) then
  249. begin
  250. { move %reg0, %tmpreg
  251. op ???, %tmpreg
  252. move %tmpreg, %reg0
  253. to:
  254. op ???, %reg0 }
  255. if MatchOperand(taicpu(p).oper[1]^,taicpu(next).oper[taicpu(next).ops-1]^) then
  256. begin
  257. {
  258. Disabled, because it breaks some tests... :( (KB)
  259. DebugMsg('Optimizer: '+opstr+' + OP + '+opstr+' to OP #1',next);
  260. taicpu(next).loadOper(taicpu(next).ops-1,taicpu(p).oper[0]^);
  261. asml.remove(p);
  262. asml.remove(next2);
  263. p.free;
  264. next2.free;
  265. result:=true;
  266. }
  267. end;
  268. end;
  269. end;
  270. end;
  271. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  272. var
  273. next: tai;
  274. tmpref: treference;
  275. begin
  276. result:=false;
  277. case p.typ of
  278. ait_instruction:
  279. begin
  280. //asml.insertbefore(tai_comment.Create(strpnew('pass1 called for instr')), p);
  281. case taicpu(p).opcode of
  282. A_MOVE:
  283. result:=TryToOptimizeMove(p);
  284. { LEA (Ax),Ax is a NOP if src and dest reg is equal, so remove it. }
  285. A_LEA:
  286. if not assigned(taicpu(p).oper[0]^.ref^.symbol) and
  287. (((taicpu(p).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  288. (taicpu(p).oper[0]^.ref^.index = NR_NO)) or
  289. ((taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  290. (taicpu(p).oper[0]^.ref^.base = NR_NO))) and
  291. (taicpu(p).oper[0]^.ref^.offset = 0) then
  292. begin
  293. DebugMsg('Optimizer: LEA 0(Ax),Ax removed',p);
  294. GetNextInstruction(p,next);
  295. asml.remove(p);
  296. p.free;
  297. p:=next;
  298. result:=true;
  299. end;
  300. { Address register sub/add can be replaced with ADDQ/SUBQ or LEA if the value is in the
  301. SmallInt range, which is shorter to encode and faster to execute on most 68k }
  302. A_SUB,A_SUBA,A_ADD,A_ADDA:
  303. if (taicpu(p).oper[1]^.typ = top_reg) and isaddressregister(taicpu(p).oper[1]^.reg) and
  304. (taicpu(p).oper[0]^.typ = top_const) then
  305. begin
  306. if isvalueforaddqsubq(taicpu(p).oper[0]^.val) then
  307. begin
  308. DebugMsg('Optimizer: SUB/ADD #val,Ax to SUBQ/ADDQ',p);
  309. taicpu(p).opsize:=S_L; // this is safe, because we're targetting an address reg
  310. if taicpu(p).opcode in [A_ADD,A_ADDA] then
  311. taicpu(p).opcode:=A_ADDQ
  312. else
  313. taicpu(p).opcode:=A_SUBQ;
  314. result:=true;
  315. end
  316. else
  317. if isvalue16bit(abs(taicpu(p).oper[0]^.val)) then
  318. begin
  319. DebugMsg('Optimizer: SUB/ADD #val,Ax to LEA val(Ax),Ax',p);
  320. if taicpu(p).opcode in [A_SUB,A_SUBA] then
  321. reference_reset_base(tmpref,taicpu(p).oper[1]^.reg,-taicpu(p).oper[0]^.val,ctempposinvalid,0,[])
  322. else
  323. reference_reset_base(tmpref,taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.val,ctempposinvalid,0,[]);
  324. taicpu(p).opcode:=A_LEA;
  325. taicpu(p).loadref(0,tmpref);
  326. result:=true;
  327. end;
  328. end;
  329. { MOVEA #0,Ax to SUBA Ax,Ax, because it's shorter }
  330. A_MOVEA:
  331. if (taicpu(p).oper[0]^.typ = top_const) and
  332. (taicpu(p).oper[0]^.val = 0) then
  333. begin
  334. DebugMsg('Optimizer: MOVEA #0,Ax to SUBA Ax,Ax',p);
  335. taicpu(p).opcode:=A_SUBA;
  336. taicpu(p).opsize:=S_L; { otherwise it will be .W -> BOOM }
  337. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  338. result:=true;
  339. end;
  340. { CLR.L Dx on a 68000 is slower than MOVEQ #0,Dx }
  341. A_CLR:
  342. if (current_settings.cputype in [cpu_mc68000]) and
  343. (taicpu(p).oper[0]^.typ = top_reg) and
  344. (taicpu(p).opsize = S_L) and
  345. isintregister(taicpu(p).oper[0]^.reg) then
  346. begin
  347. //DebugMsg('Optimizer: CLR.L Dx to MOVEQ #0,Dx',p);
  348. taicpu(p).opcode:=A_MOVEQ;
  349. taicpu(p).loadoper(1,taicpu(p).oper[0]^);
  350. taicpu(p).loadconst(0,0);
  351. taicpu(p).ops:=2;
  352. result:=true;
  353. end;
  354. { CMP #0,<ea> equals to TST <ea>, just shorter and TST is more flexible anyway }
  355. A_CMP,A_CMPI:
  356. if (taicpu(p).oper[0]^.typ = top_const) and
  357. (taicpu(p).oper[0]^.val = 0) then
  358. begin
  359. DebugMsg('Optimizer: CMP #0 to TST',p);
  360. taicpu(p).opcode:=A_TST;
  361. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  362. taicpu(p).clearop(1);
  363. taicpu(p).ops:=1;
  364. result:=true;
  365. end;
  366. A_FCMP:
  367. if (taicpu(p).oper[0]^.typ = top_realconst) then
  368. begin
  369. if (taicpu(p).oper[0]^.val_real = 0.0) then
  370. begin
  371. DebugMsg('Optimizer: FCMP #0.0 to FTST',p);
  372. taicpu(p).opcode:=A_FTST;
  373. taicpu(p).opsize:=S_FX;
  374. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  375. taicpu(p).clearop(1);
  376. taicpu(p).ops:=1;
  377. result:=true;
  378. end
  379. else
  380. result:=result or MaybeRealConstOperSimplify(p);
  381. end;
  382. A_FMOVE,A_FSMOVE,A_FDMOVE,
  383. A_FADD,A_FSADD,A_FDADD,A_FSUB,A_FSSUB,A_FDSUB,
  384. A_FMUL,A_FSMUL,A_FDMUL,A_FDIV,A_FSDIV,A_FDDIV,
  385. A_FSGLMUL,A_FSGLDIV:
  386. begin
  387. if (taicpu(p).opcode = A_FMOVE) and TryToOptimizeMove(p) then
  388. begin
  389. result:=true;
  390. exit;
  391. end;
  392. result:=result or MaybeRealConstOperSimplify(p);
  393. end;
  394. end;
  395. end;
  396. end;
  397. end;
  398. begin
  399. casmoptimizer:=TCpuAsmOptimizer;
  400. end.