aoptcpu.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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 TryToRemoveTST(var p: tai): boolean;
  30. function TryToOptimizeMove(var p: tai): boolean;
  31. function MaybeRealConstOperSimplify(var p: tai): boolean;
  32. function OptPass1LEA(var p: tai): Boolean;
  33. function OptPass1MOVEM(var p: tai): Boolean;
  34. function OptPass1Bitwise(var p: tai): Boolean;
  35. { outputs a debug message into the assembler file }
  36. procedure DebugMsg(const s: string; p: tai);
  37. End;
  38. Implementation
  39. uses
  40. cutils, aasmcpu, cgutils, globtype, globals, verbose, cpuinfo, itcpugas, procinfo, cpupi,
  41. aoptutils;
  42. { Range check must be disabled explicitly as conversions between signed and unsigned
  43. 32-bit values are done without explicit typecasts }
  44. {$R-}
  45. function opname(var p: tai): string;
  46. begin
  47. result:=upcase(gas_op2str[taicpu(p).opcode]);
  48. end;
  49. function RefsEqual(const r1, r2: treference): boolean;
  50. begin
  51. RefsEqual :=
  52. (r1.offset = r2.offset) and
  53. (r1.base = r2.base) and
  54. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  55. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  56. (r1.relsymbol = r2.relsymbol) and
  57. (r1.volatility=[]) and
  58. (r2.volatility=[]);
  59. end;
  60. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean;
  61. begin
  62. result := oper1.typ = oper2.typ;
  63. if result then
  64. case oper1.typ of
  65. top_const:
  66. Result:=oper1.val = oper2.val;
  67. top_reg:
  68. Result:=oper1.reg = oper2.reg;
  69. top_ref:
  70. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  71. else
  72. internalerror(2016112401);
  73. end
  74. end;
  75. function MatchInstruction(const instr: tai; const op: TAsmOp; const opsize: topsizes): boolean;
  76. begin
  77. result :=
  78. (instr.typ = ait_instruction) and
  79. (taicpu(instr).opcode = op) and
  80. ((opsize = []) or (taicpu(instr).opsize in opsize));
  81. end;
  82. function MatchInstruction(const instr : tai;const ops : array of TAsmOp;
  83. const opsize : topsizes) : boolean;
  84. var
  85. op : TAsmOp;
  86. begin
  87. result:=false;
  88. for op in ops do
  89. begin
  90. if (instr.typ = ait_instruction) and
  91. (taicpu(instr).opcode = op) and
  92. ((opsize = []) or (taicpu(instr).opsize in opsize)) then
  93. begin
  94. result:=true;
  95. exit;
  96. end;
  97. end;
  98. end;
  99. function TCpuAsmOptimizer.MaybeRealConstOperSimplify(var p: tai): boolean;
  100. var
  101. tmpint64: int64;
  102. tmpsingle: single;
  103. begin
  104. result:=false;
  105. if (taicpu(p).oper[0]^.typ = top_realconst) then
  106. begin
  107. { if we work with actual integers, turn the operand into one }
  108. if frac(taicpu(p).oper[0]^.val_real) = 0 then
  109. begin
  110. tmpint64:=trunc(taicpu(p).oper[0]^.val_real);
  111. if (high(shortint) >= tmpint64) and (low(shortint) <= tmpint64) then
  112. begin
  113. taicpu(p).opsize := S_B;
  114. taicpu(p).oper[0]^.typ:=top_const;
  115. end
  116. else
  117. if (high(smallint) >= tmpint64) and (low(smallint) <= tmpint64) then
  118. begin
  119. taicpu(p).opsize := S_W;
  120. taicpu(p).oper[0]^.typ:=top_const;
  121. end
  122. else
  123. if (high(longint) >= tmpint64) and (low(longint) <= tmpint64) then
  124. begin
  125. taicpu(p).opsize := S_L;
  126. taicpu(p).oper[0]^.typ:=top_const;
  127. end;
  128. if (taicpu(p).oper[0]^.typ) = top_const then
  129. begin
  130. DebugMsg('Optimizer: FPU real const to integer',p);
  131. taicpu(p).oper[0]^.val:=tmpint64;
  132. result:=true;
  133. end;
  134. end
  135. else
  136. begin
  137. tmpsingle:=taicpu(p).oper[0]^.val_real;
  138. if (taicpu(p).opsize = S_FD) and
  139. ((taicpu(p).oper[0]^.val_real - tmpsingle) = 0.0) then
  140. begin
  141. DebugMsg('Optimizer: FPU real const to lesser precision',p);
  142. taicpu(p).opsize:=S_FS;
  143. result:=true;
  144. end;
  145. end;
  146. end;
  147. end;
  148. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  149. var
  150. p: taicpu;
  151. begin
  152. if not assigned(hp) or
  153. (hp.typ <> ait_instruction) then
  154. begin
  155. Result := false;
  156. exit;
  157. end;
  158. p := taicpu(hp);
  159. Result :=
  160. (((p.opcode=A_MOVE) or (p.opcode=A_MOVEA) or (p.opcode=A_MVS) or
  161. (p.opcode=A_MVZ) or (p.opcode=A_MOVEQ) or (p.opcode=A_LEA)) and
  162. (p.oper[1]^.typ = top_reg) and
  163. (SuperRegistersEqual(p.oper[1]^.reg,reg)) and
  164. ((p.oper[0]^.typ = top_const) or
  165. ((p.oper[0]^.typ = top_reg) and
  166. not(SuperRegistersEqual(p.oper[0]^.reg,reg))) or
  167. ((p.oper[0]^.typ = top_ref) and
  168. not RegInRef(reg,p.oper[0]^.ref^)))) or
  169. ((p.opcode = A_FMOVE) and
  170. (p.oper[1]^.typ = top_reg) and
  171. (SuperRegistersEqual(p.oper[1]^.reg,reg)) and
  172. ((p.oper[0]^.typ = top_realconst) or
  173. ((p.oper[0]^.typ = top_reg) and
  174. not(SuperRegistersEqual(p.oper[0]^.reg,reg))))) or
  175. ((p.opcode = A_MOVEM) and
  176. (p.oper[1]^.typ = top_regset) and
  177. ((getsupreg(reg) in p.oper[1]^.dataregset) or
  178. (getsupreg(reg) in p.oper[1]^.addrregset))) or
  179. ((p.opcode = A_FMOVEM) and
  180. (p.oper[1]^.typ = top_regset) and
  181. (getsupreg(reg) in p.oper[1]^.fpuregset));
  182. end;
  183. {$ifdef DEBUG_AOPTCPU}
  184. procedure TCpuAsmOptimizer.DebugMsg(const s: string; p : tai);
  185. begin
  186. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  187. end;
  188. {$else DEBUG_AOPTCPU}
  189. procedure TCpuAsmOptimizer.DebugMsg(const s: string; p : tai);inline;
  190. begin
  191. end;
  192. {$endif DEBUG_AOPTCPU}
  193. function TCpuAsmOptimizer.TryToRemoveTST(var p: tai): boolean;
  194. var
  195. next, next2: tai;
  196. opstr: string[15];
  197. begin
  198. result:=false;
  199. if not((taicpu(p).oper[1]^.typ=top_reg) and isaddressregister(taicpu(p).oper[1]^.reg)) and
  200. GetNextInstruction(p,next) and
  201. MatchInstruction(next,A_TST,[taicpu(p).opsize]) and
  202. MatchOperand(taicpu(p).oper[1]^,taicpu(next).oper[0]^) and
  203. GetNextInstruction(next,next2) and
  204. MatchInstruction(next2,[A_BXX,A_SXX],[S_NO]) and
  205. (taicpu(next2).condition in [C_NE,C_EQ,C_PL,C_MI]) then
  206. begin
  207. opstr:=opname(p);
  208. DebugMsg('Optimizer: '+opstr+', TST, Jxx/Sxx to '+opstr+', Jxx/Sxx',p);
  209. RemoveInstruction(next);
  210. result:=true;
  211. end;
  212. end;
  213. function TCpuAsmOptimizer.TryToOptimizeMove(var p: tai): boolean;
  214. var
  215. next, next2: tai;
  216. opstr: string[15];
  217. begin
  218. result:=false;
  219. if (taicpu(p).opcode=A_MOVE) then
  220. begin
  221. result:=TryToRemoveTST(p);
  222. if result then
  223. exit;
  224. end;
  225. if GetNextInstruction(p,next) and
  226. (next.typ = ait_instruction) and
  227. (taicpu(next).opcode = taicpu(p).opcode) and
  228. (taicpu(p).opsize = taicpu(next).opsize) and
  229. (taicpu(p).oper[1]^.typ = top_reg) and
  230. MatchOperand(taicpu(p).oper[1]^,taicpu(next).oper[0]^) then
  231. begin
  232. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(next).oper[1]^)) and
  233. RegEndOfLife(taicpu(next).oper[0]^.reg, taicpu(next)) then
  234. begin
  235. opstr:=opname(p);
  236. case taicpu(p).oper[0]^.typ of
  237. top_reg:
  238. { do not optimize away FPU to INT to FPU reg moves. These are used for
  239. to-single-rounding on FPUs which have no FSMOVE/FDMOVE. (KB) }
  240. if not ((taicpu(p).opcode = A_FMOVE) and
  241. (getregtype(taicpu(p).oper[0]^.reg) <> getregtype(taicpu(p).oper[1]^.reg))) then
  242. begin
  243. { move %reg0, %tmpreg; move %tmpreg, <ea> -> move %reg0, <ea> }
  244. taicpu(p).loadOper(1,taicpu(next).oper[1]^);
  245. UpdateUsedRegs(p);
  246. RemoveInstruction(next);
  247. result:=true;
  248. { also remove leftover move %reg0, %reg0, which can occur as the result
  249. of the previous optimization, if %reg0 and %tmpreg was different types
  250. (addr vs. data), so these moves were left in by the cg }
  251. if MatchOperand(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  252. begin
  253. DebugMsg('Optimizer: '+opstr+' + '+opstr+' removed',p);
  254. RemoveCurrentP(p);
  255. end
  256. else
  257. DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #1',p)
  258. end;
  259. top_const:
  260. begin
  261. // DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #2',p);
  262. end;
  263. top_ref:
  264. begin
  265. { move ref, %tmpreg; move %tmpreg, <ea> -> move ref, <ea> }
  266. { we only want to do this when <ea> is a reg or a simple reference }
  267. with taicpu(next).oper[1]^ do
  268. if (taicpu(next).opcode <> A_FMOVE) and
  269. ((typ = top_reg) or
  270. ((typ = top_ref) and
  271. ((ref^.index = NR_NO) or
  272. (ref^.base = NR_NO)) and
  273. (ref^.symbol = nil) and
  274. (ref^.offset = 0))) then
  275. begin
  276. DebugMsg('Optimizer: '+opstr+' + '+opstr+' to '+opstr+' #3',p);
  277. taicpu(p).loadOper(1,taicpu(next).oper[1]^);
  278. UpdateUsedRegs(p);
  279. RemoveInstruction(next);
  280. result:=true;
  281. end;
  282. end;
  283. else
  284. ;
  285. end;
  286. end;
  287. exit;
  288. end;
  289. if GetNextInstruction(p,next) and
  290. (next.typ = ait_instruction) and
  291. GetNextInstruction(next,next2) and
  292. (next2.typ = ait_instruction) and
  293. (taicpu(next).opcode <> taicpu(p).opcode) and
  294. (taicpu(next2).opcode = taicpu(p).opcode) and
  295. (taicpu(p).oper[0]^.typ = top_reg) and
  296. (taicpu(p).oper[1]^.typ = top_reg) and
  297. (getregtype(taicpu(p).oper[0]^.reg) = getregtype(taicpu(p).oper[1]^.reg)) and
  298. MatchOperand(taicpu(p).oper[1]^,taicpu(next2).oper[0]^) and
  299. MatchOperand(taicpu(next2).oper[1]^,taicpu(p).oper[0]^) and
  300. (taicpu(p).opsize = taicpu(next2).opsize) and
  301. ((taicpu(p).opcode = A_FMOVE) or
  302. (taicpu(p).opsize = taicpu(next).opsize)) then
  303. begin
  304. opstr:=opname(p);
  305. if not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(next2).oper[1]^)) and
  306. not(RegInOp(taicpu(p).oper[1]^.reg,taicpu(next).oper[0]^)) and
  307. RegEndOfLife(taicpu(p).oper[0]^.reg, taicpu(next2)) then
  308. begin
  309. { move %reg0, %tmpreg
  310. op ???, %tmpreg
  311. move %tmpreg, %reg0
  312. to:
  313. op ???, %reg0 }
  314. if MatchOperand(taicpu(p).oper[1]^,taicpu(next).oper[taicpu(next).ops-1]^) then
  315. begin
  316. {
  317. Disabled, because it breaks some tests... :( (KB)
  318. DebugMsg('Optimizer: '+opstr+' + OP + '+opstr+' to OP #1',next);
  319. taicpu(next).loadOper(taicpu(next).ops-1,taicpu(p).oper[0]^);
  320. asml.remove(p);
  321. asml.remove(next2);
  322. p.free;
  323. next2.free;
  324. result:=true;
  325. }
  326. end;
  327. end;
  328. end;
  329. end;
  330. function TCpuAsmOptimizer.OptPass1LEA(var p: tai): Boolean;
  331. var
  332. next: tai;
  333. begin
  334. Result:=false;
  335. { LEA (Ax),Ax is a NOP if src and dest reg is equal, so remove it. }
  336. if not assigned(taicpu(p).oper[0]^.ref^.symbol) and
  337. (((taicpu(p).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) and
  338. (taicpu(p).oper[0]^.ref^.index = NR_NO)) or
  339. ((taicpu(p).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) and
  340. (taicpu(p).oper[0]^.ref^.base = NR_NO))) and
  341. (taicpu(p).oper[0]^.ref^.offset = 0) then
  342. begin
  343. DebugMsg('Optimizer: LEA 0(Ax),Ax removed',p);
  344. result:=RemoveCurrentP(p);
  345. if result then
  346. exit;
  347. end;
  348. if (taicpu(p).oper[1]^.reg=NR_A7) and
  349. (taicpu(p).oper[0]^.ref^.base=NR_A7) and
  350. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  351. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  352. (taicpu(p).oper[0]^.ref^.direction=dir_none) and
  353. GetNextInstruction(p,next) and
  354. MatchInstruction(next,A_MOVEM,[S_L]) and
  355. MatchOpType(taicpu(next),top_regset,top_ref) and
  356. ((taicpu(p).oper[0]^.ref^.offset=-(PopCnt(Byte(taicpu(next).oper[0]^.dataregset))+PopCnt(Byte(taicpu(next).oper[0]^.addrregset)))*4)) and
  357. (taicpu(next).oper[1]^.ref^.base=NR_A7) and
  358. (taicpu(next).oper[1]^.ref^.index=NR_NO) and
  359. (taicpu(next).oper[1]^.ref^.symbol=nil) and
  360. (taicpu(next).oper[1]^.ref^.direction=dir_none) and
  361. not (current_settings.cputype in cpu_coldfire) then
  362. begin
  363. DebugMsg('Optimizer: LEA, MOVE(M) to MOVE(M) predecremented',p);
  364. taicpu(next).oper[1]^.ref^.direction:=dir_dec;
  365. RemoveCurrentP(p,next);
  366. result:=true;
  367. exit;
  368. end;
  369. end;
  370. function TCpuAsmOptimizer.OptPass1MOVEM(var p: tai): Boolean;
  371. var
  372. next: tai;
  373. begin
  374. Result:=false;
  375. if MatchOpType(taicpu(p),top_ref,top_regset) and
  376. (taicpu(p).oper[0]^.ref^.base=NR_A7) and
  377. (taicpu(p).oper[0]^.ref^.index=NR_NO) and
  378. (taicpu(p).oper[0]^.ref^.symbol=nil) and
  379. (taicpu(p).oper[0]^.ref^.direction=dir_none) and
  380. GetNextInstruction(p,next) and
  381. MatchInstruction(next,A_LEA,[S_NO]) and
  382. (taicpu(next).oper[1]^.reg=NR_A7) and
  383. (taicpu(next).oper[0]^.ref^.base=NR_A7) and
  384. (taicpu(next).oper[0]^.ref^.index=NR_NO) and
  385. (taicpu(next).oper[0]^.ref^.symbol=nil) and
  386. (taicpu(next).oper[0]^.ref^.direction=dir_none) and
  387. ((taicpu(next).oper[0]^.ref^.offset=(PopCnt(Byte(taicpu(p).oper[1]^.dataregset))+PopCnt(Byte(taicpu(p).oper[1]^.addrregset)))*4)) and
  388. not (current_settings.cputype in cpu_coldfire) then
  389. begin
  390. DebugMsg('Optimizer: MOVE(M), LEA to MOVE(M) postincremented',p);
  391. taicpu(p).oper[0]^.ref^.direction:=dir_inc;
  392. RemoveInstruction(next);
  393. result:=true;
  394. exit;
  395. end;
  396. end;
  397. function TCpuAsmOptimizer.OptPass1Bitwise(var p: tai): Boolean;
  398. begin
  399. Result:=TryToRemoveTST(p);
  400. end;
  401. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  402. var
  403. next: tai;
  404. tmpref: treference;
  405. begin
  406. result:=false;
  407. case p.typ of
  408. ait_instruction:
  409. begin
  410. //asml.insertbefore(tai_comment.Create(strpnew('pass1 called for instr')), p);
  411. case taicpu(p).opcode of
  412. A_MOVE:
  413. result:=TryToOptimizeMove(p);
  414. A_MOVEM:
  415. result:=OptPass1MOVEM(p);
  416. A_LEA:
  417. result:=OptPass1LEA(p);
  418. { Bitwise operations }
  419. A_AND,A_OR,A_EOR:
  420. result:=OptPass1Bitwise(p);
  421. { Address register sub/add can be replaced with ADDQ/SUBQ or LEA if the value is in the
  422. SmallInt range, which is shorter to encode and faster to execute on most 68k }
  423. A_SUB,A_SUBA,A_ADD,A_ADDA:
  424. if (taicpu(p).oper[1]^.typ = top_reg) and isaddressregister(taicpu(p).oper[1]^.reg) and
  425. (taicpu(p).oper[0]^.typ = top_const) then
  426. begin
  427. if isvalueforaddqsubq(taicpu(p).oper[0]^.val) then
  428. begin
  429. DebugMsg('Optimizer: SUB/ADD #val,Ax to SUBQ/ADDQ',p);
  430. taicpu(p).opsize:=S_L; // this is safe, because we're targetting an address reg
  431. if taicpu(p).opcode in [A_ADD,A_ADDA] then
  432. taicpu(p).opcode:=A_ADDQ
  433. else
  434. taicpu(p).opcode:=A_SUBQ;
  435. result:=true;
  436. end
  437. else
  438. if isvalue16bit(abs(taicpu(p).oper[0]^.val)) then
  439. begin
  440. DebugMsg('Optimizer: SUB/ADD #val,Ax to LEA val(Ax),Ax',p);
  441. if (taicpu(p).opcode=A_SUB) or (taicpu(p).opcode=A_SUBA) then
  442. reference_reset_base(tmpref,taicpu(p).oper[1]^.reg,-taicpu(p).oper[0]^.val,ctempposinvalid,0,[])
  443. else
  444. reference_reset_base(tmpref,taicpu(p).oper[1]^.reg,taicpu(p).oper[0]^.val,ctempposinvalid,0,[]);
  445. taicpu(p).opcode:=A_LEA;
  446. taicpu(p).opsize:=S_NO;
  447. taicpu(p).loadref(0,tmpref);
  448. result:=true;
  449. end;
  450. end
  451. else
  452. result:=TryToRemoveTST(p);
  453. A_SUBQ,A_ADDQ:
  454. result:=TryToRemoveTST(p);
  455. { MOVEA #0,Ax to SUBA Ax,Ax, because it's shorter }
  456. A_MOVEA:
  457. if (taicpu(p).oper[0]^.typ = top_const) and
  458. (taicpu(p).oper[0]^.val = 0) then
  459. begin
  460. DebugMsg('Optimizer: MOVEA #0,Ax to SUBA Ax,Ax',p);
  461. taicpu(p).opcode:=A_SUBA;
  462. taicpu(p).opsize:=S_L; { otherwise it will be .W -> BOOM }
  463. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  464. result:=true;
  465. end;
  466. { CLR.L Dx on a 68000 is slower than MOVEQ #0,Dx }
  467. A_CLR:
  468. if (current_settings.cputype in [cpu_mc68000]) and
  469. (taicpu(p).oper[0]^.typ = top_reg) and
  470. (taicpu(p).opsize = S_L) and
  471. isintregister(taicpu(p).oper[0]^.reg) then
  472. begin
  473. //DebugMsg('Optimizer: CLR.L Dx to MOVEQ #0,Dx',p);
  474. taicpu(p).opcode:=A_MOVEQ;
  475. taicpu(p).loadoper(1,taicpu(p).oper[0]^);
  476. taicpu(p).loadconst(0,0);
  477. taicpu(p).ops:=2;
  478. result:=true;
  479. end;
  480. A_JSR:
  481. begin
  482. if (cs_opt_level4 in current_settings.optimizerswitches) and
  483. GetNextInstruction(p,next) and
  484. MatchInstruction(next,A_RTS,[S_NO]) and
  485. { play safe: if any parameter is pushed on the stack, we cannot to this optimization
  486. as the bottom stack element might be a parameter and not the return address as it is expected
  487. after a call (which we simulate by a jmp)
  488. Actually, as in this case the stack pointer is no used as a frame pointer and
  489. there will be more instructions to restore the stack frame before jsr, so this
  490. is unlikedly to happen }
  491. (current_procinfo.maxpushedparasize=0) then
  492. begin
  493. DebugMsg('Optimizer: JSR, RTS to JMP',p);
  494. taicpu(p).opcode:=A_JMP;
  495. RemoveInstruction(next);
  496. result:=true;
  497. end;
  498. end;
  499. { CMP #0,<ea> equals to TST <ea>, just shorter and TST is more flexible anyway }
  500. A_CMP,A_CMPI:
  501. if (taicpu(p).oper[0]^.typ = top_const) and
  502. (taicpu(p).oper[0]^.val = 0) then
  503. begin
  504. DebugMsg('Optimizer: CMP #0 to TST',p);
  505. taicpu(p).opcode:=A_TST;
  506. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  507. taicpu(p).clearop(1);
  508. taicpu(p).ops:=1;
  509. result:=true;
  510. end;
  511. A_FCMP:
  512. if (taicpu(p).oper[0]^.typ = top_realconst) then
  513. begin
  514. if (taicpu(p).oper[0]^.val_real = 0.0) then
  515. begin
  516. DebugMsg('Optimizer: FCMP #0.0 to FTST',p);
  517. taicpu(p).opcode:=A_FTST;
  518. taicpu(p).opsize:=S_FX;
  519. taicpu(p).loadoper(0,taicpu(p).oper[1]^);
  520. taicpu(p).clearop(1);
  521. taicpu(p).ops:=1;
  522. result:=true;
  523. end
  524. else
  525. result:=result or MaybeRealConstOperSimplify(p);
  526. end;
  527. A_FMOVE,A_FSMOVE,A_FDMOVE,
  528. A_FADD,A_FSADD,A_FDADD,A_FSUB,A_FSSUB,A_FDSUB,
  529. A_FMUL,A_FSMUL,A_FDMUL,A_FDIV,A_FSDIV,A_FDDIV,
  530. A_FSGLMUL,A_FSGLDIV:
  531. begin
  532. if (taicpu(p).opcode = A_FMOVE) and TryToOptimizeMove(p) then
  533. begin
  534. result:=true;
  535. exit;
  536. end;
  537. result:=result or MaybeRealConstOperSimplify(p);
  538. end;
  539. else
  540. ;
  541. end;
  542. end;
  543. else
  544. ;
  545. end;
  546. end;
  547. begin
  548. casmoptimizer:=TCpuAsmOptimizer;
  549. end.