aoptcpu.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. {
  2. Copyright (c) 1998-2004 by Jonas Maebe
  3. This unit calls the optimization procedures to optimize the assembler
  4. code for sparc
  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. cgbase, cpubase, aoptobj, aoptcpub, aopt, aasmtai, aasmcpu;
  24. Type
  25. TAsmOpSet = set of TAsmOp;
  26. TCpuAsmOptimizer = class(TAsmOptimizer)
  27. function RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean; override;
  28. function GetNextInstructionUsingReg(Current: tai;
  29. var Next: tai; reg: TRegister): Boolean;
  30. function TryRemoveMov(var p: tai; opcode: TAsmOp): boolean;
  31. function TryRemoveMovToRefIndex(var p: tai; next: taicpu): boolean;
  32. function TryRemoveMovBeforeStore(var p: tai; next: taicpu; const storeops: TAsmOpSet): boolean;
  33. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  34. procedure PeepHoleOptPass2; override;
  35. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  36. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  37. { outputs a debug message into the assembler file }
  38. procedure DebugMsg(const s: string; p: tai);
  39. End;
  40. Implementation
  41. uses
  42. cutils,globtype,globals,aasmbase,cpuinfo,verbose;
  43. function MatchInstruction(const instr: tai; const op: TAsmOp): boolean;
  44. begin
  45. result :=
  46. (instr.typ = ait_instruction) and
  47. (taicpu(instr).opcode = op);
  48. end;
  49. function MatchOperand(const oper: TOper; reg: TRegister): boolean;
  50. begin
  51. result:=(oper.typ=top_reg) and (oper.reg=reg);
  52. end;
  53. function IsSameReg(this,next: taicpu): boolean;
  54. begin
  55. result:=(next.oper[0]^.typ=top_reg) and
  56. (next.oper[1]^.typ=top_reg) and
  57. (next.oper[0]^.reg=next.oper[1]^.reg) and
  58. (next.oper[0]^.reg=this.oper[0]^.reg);
  59. end;
  60. function CanBeCMOV(p: tai; condreg: tregister): boolean;
  61. begin
  62. result:=assigned(p) and (p.typ=ait_instruction) and
  63. ((taicpu(p).opcode in [A_MOV_D,A_MOV_S]) or
  64. (
  65. { register with condition must not be overwritten }
  66. (taicpu(p).opcode=A_MOVE) and
  67. (taicpu(p).oper[0]^.reg<>condreg)
  68. ));
  69. end;
  70. procedure ChangeToCMOV(p: taicpu; cond: tasmcond; reg: tregister);
  71. begin
  72. case cond of
  73. C_COP1TRUE:
  74. case p.opcode of
  75. A_MOV_D: p.opcode:=A_MOVT_D;
  76. A_MOV_S: p.opcode:=A_MOVT_S;
  77. A_MOVE: p.opcode:=A_MOVT;
  78. else
  79. InternalError(2014061701);
  80. end;
  81. C_COP1FALSE:
  82. case p.opcode of
  83. A_MOV_D: p.opcode:=A_MOVF_D;
  84. A_MOV_S: p.opcode:=A_MOVF_S;
  85. A_MOVE: p.opcode:=A_MOVF;
  86. else
  87. InternalError(2014061702);
  88. end;
  89. C_EQ:
  90. case p.opcode of
  91. A_MOV_D: p.opcode:=A_MOVZ_D;
  92. A_MOV_S: p.opcode:=A_MOVZ_S;
  93. A_MOVE: p.opcode:=A_MOVZ;
  94. else
  95. InternalError(2014061703);
  96. end;
  97. C_NE:
  98. case p.opcode of
  99. A_MOV_D: p.opcode:=A_MOVN_D;
  100. A_MOV_S: p.opcode:=A_MOVN_S;
  101. A_MOVE: p.opcode:=A_MOVN;
  102. else
  103. InternalError(2014061704);
  104. end;
  105. else
  106. InternalError(2014061705);
  107. end;
  108. p.ops:=3;
  109. p.loadreg(2,reg);
  110. end;
  111. {$ifdef DEBUG_AOPTCPU}
  112. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  113. begin
  114. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  115. end;
  116. {$else DEBUG_AOPTCPU}
  117. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  118. begin
  119. end;
  120. {$endif DEBUG_AOPTCPU}
  121. function TCpuAsmOptimizer.InstructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  122. var
  123. p: taicpu;
  124. i: longint;
  125. begin
  126. result:=false;
  127. if not (assigned(hp) and (hp.typ=ait_instruction)) then
  128. exit;
  129. p:=taicpu(hp);
  130. i:=0;
  131. while(i<p.ops) do
  132. begin
  133. case p.oper[I]^.typ of
  134. top_reg:
  135. result:=(p.oper[I]^.reg=reg) and (p.spilling_get_operation_type(I)<>operand_write);
  136. top_ref:
  137. result:=
  138. (p.oper[I]^.ref^.base=reg) or
  139. (p.oper[I]^.ref^.index=reg);
  140. else
  141. ;
  142. end;
  143. if result then exit; {Bailout if we found something}
  144. Inc(I);
  145. end;
  146. end;
  147. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  148. var
  149. p: taicpu;
  150. begin
  151. p:=taicpu(hp);
  152. result:=false;
  153. if not ((assigned(hp)) and (hp.typ=ait_instruction)) then
  154. exit;
  155. case p.opcode of
  156. { These instructions do not write into a register at all }
  157. A_NOP,
  158. A_C_EQ_D,A_C_EQ_S,A_C_LE_D,A_C_LE_S,A_C_LT_D,A_C_LT_S,
  159. A_BA,A_BC,
  160. A_SB,A_SH,A_SW,A_SWL,A_SWR,A_SWC1,A_SDC1:
  161. exit;
  162. else
  163. ;
  164. end;
  165. result:=(p.ops>0) and (p.oper[0]^.typ=top_reg) and
  166. (p.oper[0]^.reg=reg);
  167. end;
  168. function TCpuAsmOptimizer.RegModifiedByInstruction(Reg: TRegister; p1: tai): boolean;
  169. var
  170. i : Longint;
  171. begin
  172. result:=false;
  173. for i:=0 to taicpu(p1).ops-1 do
  174. if (taicpu(p1).oper[i]^.typ=top_reg) and (taicpu(p1).oper[i]^.reg=Reg) and (taicpu(p1).spilling_get_operation_type(i) in [operand_write,operand_readwrite]) then
  175. begin
  176. result:=true;
  177. exit;
  178. end;
  179. end;
  180. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  181. var Next: tai; reg: TRegister): Boolean;
  182. begin
  183. Next:=Current;
  184. repeat
  185. Result:=GetNextInstruction(Next,Next);
  186. until {not(cs_opt_level3 in current_settings.optimizerswitches) or} not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  187. (is_calljmp(taicpu(Next).opcode));
  188. end;
  189. function TCpuAsmOptimizer.TryRemoveMov(var p: tai; opcode: TAsmOp): boolean;
  190. var
  191. next,hp1: tai;
  192. alloc,dealloc: tai_regalloc;
  193. begin
  194. { Fold
  195. op $reg1,...
  196. opcode $reg2,$reg1
  197. dealloc $reg1
  198. into
  199. op $reg2,...
  200. opcode may be A_MOVE, A_MOV_s, A_MOV_d, etc.
  201. }
  202. result:=false;
  203. if (taicpu(p).ops>0) and
  204. GetNextInstructionUsingReg(p,next,taicpu(p).oper[0]^.reg) and
  205. MatchInstruction(next,opcode) and
  206. MatchOperand(taicpu(next).oper[1]^,taicpu(p).oper[0]^.reg) and
  207. { the destination register of mov cannot be used between p and next }
  208. (not RegUsedBetween(taicpu(next).oper[0]^.reg,p,next)) then
  209. begin
  210. dealloc:=FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.Next));
  211. if assigned(dealloc) then
  212. begin
  213. { taicpu(p).oper[0]^.reg is not used anymore, try to find its allocation
  214. and remove it if possible }
  215. GetLastInstruction(p,hp1);
  216. asml.Remove(dealloc);
  217. alloc:=FindRegAlloc(taicpu(p).oper[0]^.reg,tai(hp1.Next));
  218. if assigned(alloc) then
  219. begin
  220. asml.Remove(alloc);
  221. alloc.free;
  222. dealloc.free;
  223. end
  224. else
  225. asml.InsertAfter(dealloc,p);
  226. { try to move the allocation of the target register }
  227. GetLastInstruction(next,hp1);
  228. alloc:=FindRegAlloc(taicpu(next).oper[0]^.reg,tai(hp1.Next));
  229. if assigned(alloc) then
  230. begin
  231. asml.Remove(alloc);
  232. asml.InsertBefore(alloc,p);
  233. { adjust used regs }
  234. IncludeRegInUsedRegs(taicpu(next).oper[0]^.reg,UsedRegs);
  235. end;
  236. { finally get rid of the mov }
  237. taicpu(p).loadreg(0,taicpu(next).oper[0]^.reg);
  238. DebugMsg('Peephole: Move removed 1',next);
  239. asml.remove(next);
  240. next.free;
  241. result:=true;
  242. end
  243. else // no dealloc found
  244. begin
  245. { try to optimize the typical call sequence
  246. lw $reg, (whatever)
  247. <alloc volatile registers (including $reg!!)>
  248. move $t9,$reg
  249. jalr $t9
  250. if $reg is nonvolatile, its value may be used after call
  251. and we cannot safely replace it with $t9 }
  252. if (opcode=A_MOVE) and
  253. (taicpu(next).oper[0]^.reg=NR_R25) and
  254. GetNextInstruction(next,hp1) and
  255. MatchInstruction(hp1,A_JALR) and
  256. MatchOperand(taicpu(hp1).oper[0]^,NR_R25) and
  257. assigned(FindRegAlloc(taicpu(p).oper[0]^.reg,tai(p.next))) then
  258. begin
  259. taicpu(p).loadreg(0,taicpu(next).oper[0]^.reg);
  260. DebugMsg('Peephole: Move removed 2',next);
  261. asml.remove(next);
  262. next.free;
  263. result:=true;
  264. end;
  265. end;
  266. end;
  267. end;
  268. function TCpuAsmOptimizer.TryRemoveMovBeforeStore(var p: tai; next: taicpu; const storeops: TAsmOpSet): boolean;
  269. begin
  270. result:=(next.opcode in storeops) and
  271. MatchOperand(next.oper[0]^,taicpu(p).oper[0]^.reg) and
  272. { Ry cannot be modified between move and store }
  273. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,next)) and
  274. Assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.next)));
  275. if result then
  276. begin
  277. next.loadreg(0,taicpu(p).oper[1]^.reg);
  278. DebugMsg('Peephole: Move removed 3',p);
  279. asml.remove(p);
  280. p.free;
  281. p:=next;
  282. end;
  283. end;
  284. function TCpuAsmOptimizer.TryRemoveMovToRefIndex(var p: tai; next: taicpu): boolean;
  285. begin
  286. result:=(next.ops>1) and
  287. (next.oper[1]^.typ=top_ref) and
  288. (next.oper[1]^.ref^.refaddr<>addr_full) and
  289. (next.oper[1]^.ref^.base=taicpu(p).oper[0]^.reg) and
  290. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,next)) and
  291. Assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.next)));
  292. if result then
  293. begin
  294. next.oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  295. DebugMsg('Peephole: Move removed 4',p);
  296. asml.remove(p);
  297. p.free;
  298. p:=next;
  299. end;
  300. end;
  301. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  302. var
  303. next,next2: tai;
  304. begin
  305. result:=false;
  306. case p.typ of
  307. ait_instruction:
  308. begin
  309. case taicpu(p).opcode of
  310. A_BC:
  311. begin
  312. { BEQ/BNE with same register are bogus, but can be generated for code like
  313. "if lo(qwordvar)=cardinal(qwordvar) ...",
  314. optimizations below can also yield them, e.g. if one register was initially R0. }
  315. if (taicpu(p).condition in [C_EQ,C_NE]) and
  316. (taicpu(p).oper[0]^.reg=taicpu(p).oper[1]^.reg) then
  317. begin
  318. if (taicpu(p).condition=C_NE) then
  319. begin
  320. if (taicpu(p).oper[2]^.typ = top_ref) and
  321. (taicpu(p).oper[2]^.ref^.symbol is TAsmLabel) then
  322. TAsmLabel(taicpu(p).oper[2]^.ref^.symbol).decrefs;
  323. RemoveDelaySlot(p);
  324. GetNextInstruction(p,next);
  325. end
  326. else
  327. begin
  328. next:=taicpu.op_sym(A_BA,taicpu(p).oper[2]^.ref^.symbol);
  329. taicpu(next).fileinfo:=taicpu(p).fileinfo;
  330. asml.insertbefore(next,p);
  331. end;
  332. asml.remove(p);
  333. p.Free;
  334. p:=next;
  335. result:=true;
  336. end;
  337. end;
  338. A_SEH:
  339. begin
  340. if GetNextInstructionUsingReg(p,next,taicpu(p).oper[0]^.reg) and
  341. MatchInstruction(next,A_SH) and
  342. MatchOperand(taicpu(next).oper[0]^,taicpu(p).oper[0]^.reg) and
  343. (not RegUsedBetween(taicpu(p).oper[1]^.reg,p,next)) and
  344. Assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.next))) then
  345. begin
  346. taicpu(next).loadreg(0,taicpu(p).oper[1]^.reg);
  347. asml.remove(p);
  348. p.free;
  349. p:=next;
  350. result:=true;
  351. end
  352. else
  353. result:=TryRemoveMov(p,A_MOVE);
  354. end;
  355. A_SEB:
  356. { TODO: can be handled similar to A_SEH, but it's almost never encountered }
  357. result:=TryRemoveMov(p,A_MOVE);
  358. A_SLL:
  359. begin
  360. { if this is a sign extension... }
  361. if (taicpu(p).oper[2]^.typ=top_const) and
  362. GetNextInstruction(p,next) and
  363. MatchInstruction(next,A_SRA) and
  364. IsSameReg(taicpu(p),taicpu(next)) and
  365. (taicpu(next).oper[2]^.typ=top_const) and
  366. (taicpu(next).oper[2]^.val=taicpu(p).oper[2]^.val) and
  367. (taicpu(next).oper[2]^.val=16) and
  368. { ...followed by 16-bit store (possibly with PIC simplification, etc. in between) }
  369. GetNextInstructionUsingReg(next,next2,taicpu(p).oper[0]^.reg) and
  370. MatchInstruction(next2,A_SH) and
  371. (taicpu(next2).oper[0]^.typ=top_reg) and
  372. (taicpu(next2).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  373. { the initial register may not be reused }
  374. (not RegUsedBetween(taicpu(p).oper[1]^.reg,next,next2)) then
  375. begin
  376. if Assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next2.next))) then
  377. begin
  378. taicpu(next2).loadreg(0,taicpu(p).oper[1]^.reg);
  379. asml.remove(p);
  380. asml.remove(next);
  381. p.free;
  382. next.free;
  383. p:=next2;
  384. result:=true;
  385. end;
  386. end
  387. else
  388. result:=TryRemoveMov(p,A_MOVE);
  389. end;
  390. A_SRL:
  391. begin
  392. { TODO: also kill sign-extensions that follow, both SLL+SRA and SEB/SEH versions }
  393. { Remove 'andi' in sequences
  394. srl Rx,Ry,16
  395. andi Rx,Rx,65535
  396. srl Rx,Ry,24
  397. andi Rx,Rx,255
  398. since 'srl' clears all relevant upper bits }
  399. if (taicpu(p).oper[2]^.typ=top_const) and
  400. GetNextInstruction(p,next) and
  401. MatchInstruction(next,A_ANDI) and
  402. IsSameReg(taicpu(p),taicpu(next)) and
  403. (taicpu(next).oper[2]^.typ=top_const) and
  404. ((
  405. (taicpu(p).oper[2]^.val>=16) and
  406. (taicpu(next).oper[2]^.val=65535)
  407. ) or (
  408. (taicpu(p).oper[2]^.val>=24) and
  409. (taicpu(next).oper[2]^.val=255)
  410. )) then
  411. begin
  412. asml.remove(next);
  413. next.free;
  414. result:=true;
  415. end
  416. else
  417. result:=TryRemoveMov(p,A_MOVE);
  418. end;
  419. A_ANDI:
  420. begin
  421. { Remove sign extension after 'andi' if bit 7/15 of const operand is clear }
  422. if (taicpu(p).oper[2]^.typ=top_const) and
  423. GetNextInstruction(p,next) and
  424. MatchInstruction(next,A_SLL) and
  425. GetNextInstruction(next,next2) and
  426. MatchInstruction(next2,A_SRA) and
  427. IsSameReg(taicpu(p),taicpu(next)) and
  428. IsSameReg(taicpu(p),taicpu(next2)) and
  429. (taicpu(next).oper[2]^.typ=top_const) and
  430. (taicpu(next2).oper[2]^.typ=top_const) and
  431. (taicpu(next).oper[2]^.val=taicpu(next2).oper[2]^.val) and
  432. ((
  433. (taicpu(p).oper[2]^.val<=$7fff) and
  434. (taicpu(next).oper[2]^.val=16)
  435. ) or (
  436. (taicpu(p).oper[2]^.val<=$7f) and
  437. (taicpu(next).oper[2]^.val=24)
  438. )) then
  439. begin
  440. asml.remove(next);
  441. asml.remove(next2);
  442. next.free;
  443. next2.free;
  444. result:=true;
  445. end
  446. { Remove zero extension if register is used only for byte/word memory store }
  447. else if (taicpu(p).oper[2]^.typ=top_const) and
  448. GetNextInstruction(p,next) and
  449. ((taicpu(p).oper[2]^.val=255) and MatchInstruction(next,A_SB)) or
  450. ((taicpu(p).oper[2]^.val=65535) and MatchInstruction(next,A_SH)) and
  451. (taicpu(next).oper[0]^.typ=top_reg) and
  452. (taicpu(next).oper[0]^.reg=taicpu(p).oper[0]^.reg) and
  453. assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.next))) then
  454. begin
  455. taicpu(next).loadreg(0,taicpu(p).oper[1]^.reg);
  456. asml.remove(p);
  457. p.free;
  458. p:=next;
  459. result:=true;
  460. end
  461. else
  462. result:=TryRemoveMov(p,A_MOVE);
  463. end;
  464. A_MOV_S:
  465. begin
  466. if GetNextInstructionUsingReg(p,next,taicpu(p).oper[0]^.reg) and
  467. (next.typ=ait_instruction) then
  468. begin
  469. if TryRemoveMovBeforeStore(p,taicpu(next),[A_SWC1]) then
  470. result:=true;
  471. end;
  472. end;
  473. A_MOV_D:
  474. begin
  475. if GetNextInstructionUsingReg(p,next,taicpu(p).oper[0]^.reg) and
  476. (next.typ=ait_instruction) then
  477. begin
  478. if TryRemoveMovBeforeStore(p,taicpu(next),[A_SDC1]) then
  479. result:=true;
  480. end;
  481. end;
  482. A_MOVE:
  483. begin
  484. if GetNextInstructionUsingReg(p,next,taicpu(p).oper[0]^.reg) and
  485. (next.typ=ait_instruction) and
  486. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,next)) then
  487. begin
  488. { MOVE Rx,Ry; store Rx,(ref); dealloc Rx ==> store Ry,(ref) }
  489. if TryRemoveMovBeforeStore(p,taicpu(next),[A_SB,A_SH,A_SW]) then
  490. result:=true
  491. else if TryRemoveMovToRefIndex(p,taicpu(next)) then
  492. result:=true
  493. { MOVE Rx,Ry; opcode Rx,Rx,any ==> opcode Rx,Ry,any
  494. MOVE Rx,Ry; opcode Rx,Rz,Rx ==> opcode Rx,Rz,Ry }
  495. else if (taicpu(next).opcode in [A_ADD,A_ADDU,A_ADDI,A_ADDIU,A_SUB,A_SUBU,A_AND,A_ANDI,A_SLLV,A_SRLV,A_SRAV]) and
  496. MatchOperand(taicpu(next).oper[0]^,taicpu(p).oper[0]^.reg) then
  497. begin
  498. if MatchOperand(taicpu(next).oper[1]^,taicpu(p).oper[0]^.reg) then
  499. begin
  500. taicpu(next).loadreg(1,taicpu(p).oper[1]^.reg);
  501. asml.remove(p);
  502. p.free;
  503. p:=next;
  504. result:=true;
  505. end
  506. { TODO: if Ry=NR_R0, this effectively changes instruction into MOVE,
  507. providing further optimization possibilities }
  508. else if MatchOperand(taicpu(next).oper[2]^,taicpu(p).oper[0]^.reg) then
  509. begin
  510. taicpu(next).loadreg(2,taicpu(p).oper[1]^.reg);
  511. asml.remove(p);
  512. p.free;
  513. p:=next;
  514. result:=true;
  515. end;
  516. end
  517. { MOVE Rx,Ry; opcode Rz,Rx,any; dealloc Rx ==> opcode Rz,Ry,any }
  518. else if (taicpu(next).opcode in [A_ADD,A_ADDU,A_ADDI,A_ADDIU,A_SUB,A_SUBU,A_SLT,A_SLTU,A_DIV,A_DIVU,
  519. A_SLL,A_SRL,A_SRA,A_SLLV,A_SRLV,A_SRAV,A_AND,A_ANDI,A_OR,A_ORI,A_XOR,A_XORI]) and
  520. Assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.next))) then
  521. begin
  522. if MatchOperand(taicpu(next).oper[1]^,taicpu(p).oper[0]^.reg) then
  523. begin
  524. taicpu(next).loadreg(1,taicpu(p).oper[1]^.reg);
  525. asml.remove(p);
  526. p.free;
  527. p:=next;
  528. result:=true;
  529. end
  530. else if MatchOperand(taicpu(next).oper[2]^,taicpu(p).oper[0]^.reg) then
  531. begin
  532. taicpu(next).loadreg(2,taicpu(p).oper[1]^.reg);
  533. asml.remove(p);
  534. p.free;
  535. p:=next;
  536. result:=true;
  537. end;
  538. end
  539. { MULT[U] and cond.branches must be handled separately due to different operand numbers }
  540. else if (taicpu(next).opcode in [A_MULT,A_MULTU,A_BC]) and
  541. Assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.next))) then
  542. begin
  543. if MatchOperand(taicpu(next).oper[0]^,taicpu(p).oper[0]^.reg) then
  544. begin
  545. taicpu(next).loadreg(0,taicpu(p).oper[1]^.reg);
  546. if MatchOperand(taicpu(next).oper[1]^,taicpu(p).oper[0]^.reg) then
  547. taicpu(next).loadreg(1,taicpu(p).oper[1]^.reg);
  548. asml.remove(p);
  549. p.free;
  550. p:=next;
  551. result:=true;
  552. end
  553. else if MatchOperand(taicpu(next).oper[1]^,taicpu(p).oper[0]^.reg) then
  554. begin
  555. taicpu(next).loadreg(1,taicpu(p).oper[1]^.reg);
  556. if MatchOperand(taicpu(next).oper[0]^,taicpu(p).oper[0]^.reg) then
  557. taicpu(next).loadreg(0,taicpu(p).oper[1]^.reg);
  558. asml.remove(p);
  559. p.free;
  560. p:=next;
  561. result:=true;
  562. end;
  563. end
  564. else if TryRemoveMov(p,A_MOVE) then
  565. begin
  566. { Ended up with move between same register? Suicide then. }
  567. if (taicpu(p).oper[0]^.reg=taicpu(p).oper[1]^.reg) then
  568. begin
  569. GetNextInstruction(p,next);
  570. asml.remove(p);
  571. p.free;
  572. p:=next;
  573. result:=true;
  574. end;
  575. end;
  576. end;
  577. end;
  578. A_ADDIU:
  579. begin
  580. { ADDIU Rx,Ry,const; load/store Rz,(Rx); dealloc Rx ==> load/store Rz,const(Ry)
  581. ADDIU Rx,Ry,%lo(sym); load/store Rz,(Rx); dealloc Rx ==> load/store Rz,%lo(sym)(Ry)
  582. ADDIU Rx,Ry,const; load Rx,(Rx) ==> load Rx,const(Ry)
  583. ADDIU Rx,Ry,%lo(sym); load Rx,(Rx) ==> load Rx,%lo(sym)(Ry) }
  584. if GetNextInstructionUsingReg(p,next,taicpu(p).oper[0]^.reg) and
  585. (next.typ=ait_instruction) and
  586. (taicpu(next).opcode in [A_LB,A_LBU,A_LH,A_LHU,A_LW,A_SB,A_SH,A_SW]) and
  587. (taicpu(p).oper[0]^.reg=taicpu(next).oper[1]^.ref^.base) and
  588. (taicpu(next).oper[1]^.ref^.offset=0) and
  589. (taicpu(next).oper[1]^.ref^.symbol=nil) and
  590. (
  591. Assigned(FindRegDealloc(taicpu(p).oper[0]^.reg,tai(next.next))) or
  592. (
  593. (taicpu(p).oper[0]^.reg=taicpu(next).oper[0]^.reg) and
  594. (taicpu(next).opcode in [A_LB,A_LBU,A_LH,A_LHU,A_LW])
  595. )
  596. ) and
  597. (not RegModifiedBetween(taicpu(p).oper[1]^.reg,p,next)) then
  598. begin
  599. case taicpu(p).oper[2]^.typ of
  600. top_const:
  601. taicpu(next).oper[1]^.ref^.offset:=taicpu(p).oper[2]^.val;
  602. top_ref:
  603. taicpu(next).oper[1]^.ref^:=taicpu(p).oper[2]^.ref^;
  604. else
  605. InternalError(2014100401);
  606. end;
  607. taicpu(next).oper[1]^.ref^.base:=taicpu(p).oper[1]^.reg;
  608. asml.remove(p);
  609. p.free;
  610. p:=next;
  611. result:=true;
  612. end
  613. else
  614. result:=TryRemoveMov(p,A_MOVE);
  615. end;
  616. A_ADD,A_ADDU,A_OR:
  617. begin
  618. if MatchOperand(taicpu(p).oper[1]^,NR_R0) then
  619. begin
  620. taicpu(p).freeop(1);
  621. taicpu(p).oper[1]:=taicpu(p).oper[2];
  622. taicpu(p).oper[2]:=nil;
  623. taicpu(p).ops:=2;
  624. taicpu(p).opercnt:=2;
  625. taicpu(p).opcode:=A_MOVE;
  626. result:=true;
  627. end
  628. else if MatchOperand(taicpu(p).oper[2]^,NR_R0) then
  629. begin
  630. taicpu(p).freeop(2);
  631. taicpu(p).ops:=2;
  632. taicpu(p).opercnt:=2;
  633. taicpu(p).opcode:=A_MOVE;
  634. result:=true;
  635. end
  636. else
  637. result:=TryRemoveMov(p,A_MOVE);
  638. end;
  639. A_LB,A_LBU,A_LH,A_LHU,A_LW,
  640. A_ADDI,
  641. A_SUB,A_SUBU,
  642. A_SRA,A_SRAV,
  643. A_SRLV,
  644. A_SLLV,
  645. A_MFLO,A_MFHI,
  646. A_AND,A_XOR,A_ORI,A_XORI:
  647. result:=TryRemoveMov(p,A_MOVE);
  648. A_LWC1,
  649. A_ADD_s, A_SUB_s, A_MUL_s, A_DIV_s,
  650. A_ABS_s, A_NEG_s, A_SQRT_s,
  651. A_CVT_s_w, A_CVT_s_l, A_CVT_s_d:
  652. result:=TryRemoveMov(p,A_MOV_s);
  653. A_LDC1,
  654. A_ADD_d, A_SUB_d, A_MUL_d, A_DIV_d,
  655. A_ABS_d, A_NEG_d, A_SQRT_d,
  656. A_CVT_d_w, A_CVT_d_l, A_CVT_d_s:
  657. result:=TryRemoveMov(p,A_MOV_d);
  658. else
  659. ;
  660. end;
  661. end;
  662. else
  663. ;
  664. end;
  665. end;
  666. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  667. var
  668. p: tai;
  669. l: longint;
  670. hp1,hp2,hp3: tai;
  671. condition: tasmcond;
  672. condreg: tregister;
  673. begin
  674. { Currently, everything below is mips4+ }
  675. if (current_settings.cputype<cpu_mips4) then
  676. exit;
  677. p:=BlockStart;
  678. ClearUsedRegs;
  679. while (p<>BlockEnd) Do
  680. begin
  681. UpdateUsedRegs(tai(p.next));
  682. case p.typ of
  683. ait_instruction:
  684. begin
  685. case taicpu(p).opcode of
  686. A_BC:
  687. begin
  688. condreg:=NR_NO;
  689. if (taicpu(p).condition in [C_COP1TRUE,C_COP1FALSE]) then
  690. { TODO: must be taken from "p" if/when codegen makes use of multiple %fcc }
  691. condreg:=NR_FCC0
  692. else if (taicpu(p).condition in [C_EQ,C_NE]) then
  693. begin
  694. if (taicpu(p).oper[0]^.reg=NR_R0) then
  695. condreg:=taicpu(p).oper[1]^.reg
  696. else if (taicpu(p).oper[1]^.reg=NR_R0) then
  697. condreg:=taicpu(p).oper[0]^.reg
  698. end;
  699. if (condreg<>NR_NO) then
  700. begin
  701. { check for
  702. bCC xxx
  703. <several movs>
  704. xxx:
  705. }
  706. l:=0;
  707. GetNextInstruction(p, hp1);
  708. while CanBeCMOV(hp1,condreg) do // CanBeCMOV returns False for nil or labels
  709. begin
  710. inc(l);
  711. GetNextInstruction(hp1,hp1);
  712. end;
  713. if assigned(hp1) then
  714. begin
  715. if FindLabel(tasmlabel(taicpu(p).oper[taicpu(p).ops-1]^.ref^.symbol),hp1) then
  716. begin
  717. if (l<=4) and (l>0) then
  718. begin
  719. condition:=inverse_cond(taicpu(p).condition);
  720. hp2:=p;
  721. GetNextInstruction(p,hp1);
  722. p:=hp1;
  723. repeat
  724. ChangeToCMOV(taicpu(hp1),condition,condreg);
  725. GetNextInstruction(hp1,hp1);
  726. until not CanBeCMOV(hp1,condreg);
  727. { wait with removing else GetNextInstruction could
  728. ignore the label if it was the only usage in the
  729. jump moved away }
  730. tasmlabel(taicpu(hp2).oper[taicpu(hp2).ops-1]^.ref^.symbol).decrefs;
  731. RemoveDelaySlot(hp2);
  732. asml.remove(hp2);
  733. hp2.free;
  734. continue;
  735. end;
  736. end
  737. else
  738. begin
  739. { check further for
  740. bCC xxx
  741. <several movs 1>
  742. b yyy
  743. xxx:
  744. <several movs 2>
  745. yyy:
  746. }
  747. { hp2 points to jmp yyy }
  748. hp2:=hp1;
  749. { skip hp1 to xxx }
  750. GetNextInstruction(hp1, hp1);
  751. if assigned(hp2) and
  752. assigned(hp1) and
  753. (l<=3) and
  754. (hp2.typ=ait_instruction) and
  755. (taicpu(hp2).opcode=A_BA) and
  756. { real label and jump, no further references to the
  757. label are allowed }
  758. (tasmlabel(taicpu(p).oper[taicpu(p).ops-1]^.ref^.symbol).getrefs<=2) and
  759. FindLabel(tasmlabel(taicpu(p).oper[taicpu(p).ops-1]^.ref^.symbol),hp1) then
  760. begin
  761. l:=0;
  762. { skip hp1 to <several moves 2> }
  763. GetNextInstruction(hp1, hp1);
  764. while CanBeCMOV(hp1,condreg) do
  765. begin
  766. inc(l);
  767. GetNextInstruction(hp1, hp1);
  768. end;
  769. { hp1 points to yyy: }
  770. if assigned(hp1) and (l<=3) and
  771. FindLabel(tasmlabel(taicpu(hp2).oper[taicpu(hp2).ops-1]^.ref^.symbol),hp1) then
  772. begin
  773. condition:=inverse_cond(taicpu(p).condition);
  774. GetNextInstruction(p,hp1);
  775. hp3:=p;
  776. p:=hp1;
  777. while CanBeCMOV(hp1,condreg) do
  778. begin
  779. ChangeToCMOV(taicpu(hp1),condition,condreg);
  780. GetNextInstruction(hp1,hp1);
  781. end;
  782. { hp2 is still at b yyy }
  783. GetNextInstruction(hp2,hp1);
  784. { hp2 is now at xxx: }
  785. condition:=inverse_cond(condition);
  786. GetNextInstruction(hp1,hp1);
  787. { hp1 is now at <several movs 2> }
  788. while CanBeCMOV(hp1,condreg) do
  789. begin
  790. ChangeToCMOV(taicpu(hp1),condition,condreg);
  791. GetNextInstruction(hp1,hp1);
  792. end;
  793. { remove bCC }
  794. tasmlabel(taicpu(hp3).oper[taicpu(hp3).ops-1]^.ref^.symbol).decrefs;
  795. RemoveDelaySlot(hp3);
  796. asml.remove(hp3);
  797. hp3.free;
  798. { remove jmp }
  799. tasmlabel(taicpu(hp2).oper[taicpu(hp2).ops-1]^.ref^.symbol).decrefs;
  800. RemoveDelaySlot(hp2);
  801. asml.remove(hp2);
  802. hp2.free;
  803. continue;
  804. end;
  805. end;
  806. end;
  807. end;
  808. end;
  809. end;
  810. else
  811. ;
  812. end;
  813. end;
  814. else
  815. ;
  816. end;
  817. UpdateUsedRegs(p);
  818. p:=tai(p.next);
  819. end;
  820. end;
  821. begin
  822. casmoptimizer:=TCpuAsmOptimizer;
  823. end.