aoptcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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=A_MOVE) or (p.opcode=A_MOVEA) or (p.opcode=A_MVS) or
  132. (p.opcode=A_MVZ) or (p.opcode=A_MOVEQ) or (p.opcode=A_LEA)) and
  133. (p.oper[1]^.typ = top_reg) and
  134. (SuperRegistersEqual(p.oper[1]^.reg,reg)) and
  135. ((p.oper[0]^.typ = top_const) or
  136. ((p.oper[0]^.typ = top_reg) and
  137. not(SuperRegistersEqual(p.oper[0]^.reg,reg))) or
  138. ((p.oper[0]^.typ = top_ref) and
  139. not RegInRef(reg,p.oper[0]^.ref^)))) or
  140. ((p.opcode = A_FMOVE) and
  141. (p.oper[1]^.typ = top_reg) and
  142. (SuperRegistersEqual(p.oper[1]^.reg,reg)) and
  143. ((p.oper[0]^.typ = top_realconst) or
  144. ((p.oper[0]^.typ = top_reg) and
  145. not(SuperRegistersEqual(p.oper[0]^.reg,reg))))) or
  146. ((p.opcode = A_MOVEM) and
  147. (p.oper[1]^.typ = top_regset) and
  148. ((getsupreg(reg) in p.oper[1]^.dataregset) or
  149. (getsupreg(reg) in p.oper[1]^.addrregset))) or
  150. ((p.opcode = A_FMOVEM) and
  151. (p.oper[1]^.typ = top_regset) and
  152. (getsupreg(reg) in p.oper[1]^.fpuregset));
  153. end;
  154. {$ifdef DEBUG_AOPTCPU}
  155. procedure TCpuAsmOptimizer.DebugMsg(const s: string; p : tai);
  156. begin
  157. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  158. end;
  159. {$else DEBUG_AOPTCPU}
  160. procedure TCpuAsmOptimizer.DebugMsg(const s: string; p : tai);inline;
  161. begin
  162. end;
  163. {$endif DEBUG_AOPTCPU}
  164. function TCpuAsmOptimizer.TryToOptimizeMove(var p: tai): boolean;
  165. var
  166. next, next2: tai;
  167. opstr: string[15];
  168. begin
  169. result:=false;
  170. if GetNextInstruction(p,next) and
  171. (next.typ = ait_instruction) and
  172. (taicpu(next).opcode = taicpu(p).opcode) and
  173. (taicpu(p).opsize = taicpu(next).opsize) and
  174. (taicpu(p).oper[1]^.typ = top_reg) and
  175. MatchOperand(taicpu(p).oper[1]^,taicpu(next).oper[0]^) then
  176. begin
  177. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(next).oper[1]^)) and
  178. RegEndOfLife(taicpu(next).oper[0]^.reg, taicpu(next)) then
  179. begin
  180. opstr:=opname(p);
  181. case taicpu(p).oper[0]^.typ of
  182. top_reg:
  183. begin
  184. { move %reg0, %tmpreg; move %tmpreg, <ea> -> move %reg0, <ea> }
  185. taicpu(p).loadOper(1,taicpu(next).oper[1]^);
  186. asml.remove(next);
  187. next.free;
  188. result:=true;
  189. { also remove leftover move %reg0, %reg0, which can occur as the result
  190. of the previous optimization, if %reg0 and %tmpreg was different types
  191. (addr vs. data), so these moves were left in by the cg }
  192. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  193. begin
  194. DebugMsg('Optimizer: '+opstr+' + '+opstr+' removed',p);
  195. GetNextInstruction(p,next);
  196. asml.remove(p);
  197. p.free;
  198. p:=next;
  199. end
  200. else
  201. DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #1',p)
  202. end;
  203. top_const:
  204. begin
  205. // DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #2',p);
  206. end;
  207. top_ref:
  208. begin
  209. { move ref, %tmpreg; move %tmpreg, <ea> -> move ref, <ea> }
  210. { we only want to do this when <ea> is a reg or a simple reference }
  211. with taicpu(next).oper[1]^ do
  212. if (taicpu(next).opcode <> A_FMOVE) and
  213. ((typ = top_reg) or
  214. ((typ = top_ref) and
  215. ((ref^.index = NR_NO) or
  216. (ref^.base = NR_NO)) and
  217. (ref^.symbol = nil) and
  218. (ref^.offset = 0))) then
  219. begin
  220. DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #3',p);
  221. taicpu(p).loadOper(1,taicpu(next).oper[1]^);
  222. asml.remove(next);
  223. next.free;
  224. result:=true;
  225. end;
  226. end;
  227. else
  228. ;
  229. end;
  230. end;
  231. exit;
  232. end;
  233. if GetNextInstruction(p,next) and
  234. (next.typ = ait_instruction) and
  235. GetNextInstruction(next,next2) and
  236. (next2.typ = ait_instruction) and
  237. (taicpu(next).opcode <> taicpu(p).opcode) and
  238. (taicpu(next2).opcode = taicpu(p).opcode) and
  239. (taicpu(p).oper[0]^.typ = top_reg) and
  240. (taicpu(p).oper[1]^.typ = top_reg) and
  241. (getregtype(taicpu(p).oper[0]^.reg) = getregtype(taicpu(p).oper[1]^.reg)) and
  242. MatchOperand(taicpu(p).oper[1]^,taicpu(next2).oper[0]^) and
  243. MatchOperand(taicpu(next2).oper[1]^,taicpu(p).oper[0]^) and
  244. (taicpu(p).opsize = taicpu(next2).opsize) and
  245. ((taicpu(p).opcode = A_FMOVE) or
  246. (taicpu(p).opsize = taicpu(next).opsize)) then
  247. begin
  248. opstr:=opname(p);
  249. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(next2).oper[1]^)) and
  250. not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(next).oper[0]^)) and
  251. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(next2)) then
  252. begin
  253. { move %reg0, %tmpreg
  254. op ???, %tmpreg
  255. move %tmpreg, %reg0
  256. to:
  257. op ???, %reg0 }
  258. if MatchOperand(taicpu(p).oper[1]^,taicpu(next).oper[taicpu(next).ops-1]^) then
  259. begin
  260. {
  261. Disabled, because it breaks some tests... :( (KB)
  262. DebugMsg('Optimizer: '+opstr+' + OP + '+opstr+' to OP #1',next);
  263. taicpu(next).loadOper(taicpu(next).ops-1,taicpu(p).oper[0]^);
  264. asml.remove(p);
  265. asml.remove(next2);
  266. p.free;
  267. next2.free;
  268. result:=true;
  269. }
  270. end;
  271. end;
  272. end;
  273. end;
  274. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  275. var
  276. next: tai;
  277. tmpref: treference;
  278. begin
  279. result:=false;
  280. case p.typ of
  281. ait_instruction:
  282. begin
  283. //asml.insertbefore(tai_comment.Create(strpnew('pass1 called for instr')), p);
  284. case taicpu(p).opcode of
  285. A_MOVE:
  286. result:=TryToOptimizeMove(p);
  287. { LEA (Ax),Ax is a NOP if src and dest reg is equal, so remove it. }
  288. A_LEA:
  289. if not assigned(taicpu(p).oper[0]^.ref^.symbol) and
  290. (((taicpu(p).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  291. (taicpu(p).oper[0]^.ref^.index = NR_NO)) or
  292. ((taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  293. (taicpu(p).oper[0]^.ref^.base = NR_NO))) and
  294. (taicpu(p).oper[0]^.ref^.offset = 0) then
  295. begin
  296. DebugMsg('Optimizer: LEA 0(Ax),Ax removed',p);
  297. GetNextInstruction(p,next);
  298. asml.remove(p);
  299. p.free;
  300. p:=next;
  301. result:=true;
  302. end;
  303. { Address register sub/add can be replaced with ADDQ/SUBQ or LEA if the value is in the
  304. SmallInt range, which is shorter to encode and faster to execute on most 68k }
  305. A_SUB,A_SUBA,A_ADD,A_ADDA:
  306. if (taicpu(p).oper[1]^.typ = top_reg) and isaddressregister(taicpu(p).oper[1]^.reg) and
  307. (taicpu(p).oper[0]^.typ = top_const) then
  308. begin
  309. if isvalueforaddqsubq(taicpu(p).oper[0]^.val) then
  310. begin
  311. DebugMsg('Optimizer: SUB/ADD #val,Ax to SUBQ/ADDQ',p);
  312. taicpu(p).opsize:=S_L; // this is safe, because we're targetting an address reg
  313. if taicpu(p).opcode in [A_ADD,A_ADDA] then
  314. taicpu(p).opcode:=A_ADDQ
  315. else
  316. taicpu(p).opcode:=A_SUBQ;
  317. result:=true;
  318. end
  319. else
  320. if isvalue16bit(abs(taicpu(p).oper[0]^.val)) then
  321. begin
  322. DebugMsg('Optimizer: SUB/ADD #val,Ax to LEA val(Ax),Ax',p);
  323. if (taicpu(p).opcode=A_SUB) or (taicpu(p).opcode=A_SUBA) then
  324. reference_reset_base(tmpref,taicpu(p).oper[1]^.reg,-taicpu(p).oper[0]^.val,ctempposinvalid,0,[])
  325. else
  326. reference_reset_base(tmpref,taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.val,ctempposinvalid,0,[]);
  327. taicpu(p).opcode:=A_LEA;
  328. taicpu(p).loadref(0,tmpref);
  329. result:=true;
  330. end;
  331. end;
  332. { MOVEA #0,Ax to SUBA Ax,Ax, because it's shorter }
  333. A_MOVEA:
  334. if (taicpu(p).oper[0]^.typ = top_const) and
  335. (taicpu(p).oper[0]^.val = 0) then
  336. begin
  337. DebugMsg('Optimizer: MOVEA #0,Ax to SUBA Ax,Ax',p);
  338. taicpu(p).opcode:=A_SUBA;
  339. taicpu(p).opsize:=S_L; { otherwise it will be .W -> BOOM }
  340. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  341. result:=true;
  342. end;
  343. { CLR.L Dx on a 68000 is slower than MOVEQ #0,Dx }
  344. A_CLR:
  345. if (current_settings.cputype in [cpu_mc68000]) and
  346. (taicpu(p).oper[0]^.typ = top_reg) and
  347. (taicpu(p).opsize = S_L) and
  348. isintregister(taicpu(p).oper[0]^.reg) then
  349. begin
  350. //DebugMsg('Optimizer: CLR.L Dx to MOVEQ #0,Dx',p);
  351. taicpu(p).opcode:=A_MOVEQ;
  352. taicpu(p).loadoper(1,taicpu(p).oper[0]^);
  353. taicpu(p).loadconst(0,0);
  354. taicpu(p).ops:=2;
  355. result:=true;
  356. end;
  357. { CMP #0,<ea> equals to TST <ea>, just shorter and TST is more flexible anyway }
  358. A_CMP,A_CMPI:
  359. if (taicpu(p).oper[0]^.typ = top_const) and
  360. (taicpu(p).oper[0]^.val = 0) then
  361. begin
  362. DebugMsg('Optimizer: CMP #0 to TST',p);
  363. taicpu(p).opcode:=A_TST;
  364. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  365. taicpu(p).clearop(1);
  366. taicpu(p).ops:=1;
  367. result:=true;
  368. end;
  369. A_FCMP:
  370. if (taicpu(p).oper[0]^.typ = top_realconst) then
  371. begin
  372. if (taicpu(p).oper[0]^.val_real = 0.0) then
  373. begin
  374. DebugMsg('Optimizer: FCMP #0.0 to FTST',p);
  375. taicpu(p).opcode:=A_FTST;
  376. taicpu(p).opsize:=S_FX;
  377. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  378. taicpu(p).clearop(1);
  379. taicpu(p).ops:=1;
  380. result:=true;
  381. end
  382. else
  383. result:=result or MaybeRealConstOperSimplify(p);
  384. end;
  385. A_FMOVE,A_FSMOVE,A_FDMOVE,
  386. A_FADD,A_FSADD,A_FDADD,A_FSUB,A_FSSUB,A_FDSUB,
  387. A_FMUL,A_FSMUL,A_FDMUL,A_FDIV,A_FSDIV,A_FDDIV,
  388. A_FSGLMUL,A_FSGLDIV:
  389. begin
  390. if (taicpu(p).opcode = A_FMOVE) and TryToOptimizeMove(p) then
  391. begin
  392. result:=true;
  393. exit;
  394. end;
  395. result:=result or MaybeRealConstOperSimplify(p);
  396. end;
  397. else
  398. ;
  399. end;
  400. end;
  401. else
  402. ;
  403. end;
  404. end;
  405. begin
  406. casmoptimizer:=TCpuAsmOptimizer;
  407. end.