aoptcpu.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. {$ifdef EXTDEBUG}
  21. {$define DEBUG_AOPTCPU}
  22. {$endif EXTDEBUG}
  23. Interface
  24. uses
  25. cgbase, cpubase, aoptobj, aoptcpub, aopt, aasmtai;
  26. Type
  27. TCpuAsmOptimizer = class(TAsmOptimizer)
  28. function GetNextInstructionUsingReg(Current: tai;
  29. var Next: tai; reg: TRegister): Boolean;
  30. function TryRemoveMov(var p: tai; opcode: TAsmOp): boolean;
  31. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  32. function RegUsedAfterInstruction(reg: Tregister; p: tai;
  33. var AllUsedRegs: TAllUsedRegs): Boolean;
  34. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  35. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  36. procedure DebugMsg(const s : string;p : tai);
  37. End;
  38. Implementation
  39. uses
  40. {$ifdef DEBUG_AOPTCPU}
  41. cutils,
  42. {$endif DEBUG_AOPTCPU}
  43. globtype,globals,aasmcpu;
  44. function MatchInstruction(const instr: tai; const op: TAsmOp): boolean;
  45. begin
  46. result :=
  47. (instr.typ = ait_instruction) and
  48. (taicpu(instr).opcode = op);
  49. end;
  50. function MatchOperand(const oper: TOper; reg: TRegister): boolean;
  51. begin
  52. result:=(oper.typ=top_reg) and (oper.reg=reg);
  53. end;
  54. function IsSameReg(this,next: taicpu): boolean;
  55. begin
  56. result:=(next.ops=3) and
  57. (next.oper[2]^.typ=top_reg) and
  58. (next.oper[0]^.typ=top_reg) and
  59. (next.oper[2]^.reg=next.oper[0]^.reg) and
  60. (next.oper[2]^.reg=this.oper[2]^.reg);
  61. end;
  62. function TCpuAsmOptimizer.InstructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  63. var
  64. p: taicpu;
  65. i: longint;
  66. begin
  67. result:=false;
  68. if not (assigned(hp) and (hp.typ=ait_instruction)) then
  69. exit;
  70. p:=taicpu(hp);
  71. i:=0;
  72. while(i<p.ops) do
  73. begin
  74. case p.oper[I]^.typ of
  75. top_reg:
  76. result:=(p.oper[I]^.reg=reg) and (I<2);
  77. top_ref:
  78. result:=
  79. (p.oper[I]^.ref^.base=reg) or
  80. (p.oper[I]^.ref^.index=reg);
  81. else
  82. ;
  83. end;
  84. if result then exit; {Bailout if we found something}
  85. Inc(I);
  86. end;
  87. end;
  88. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  89. var
  90. p: taicpu;
  91. begin
  92. result:=false;
  93. if not ((assigned(hp)) and (hp.typ=ait_instruction)) then
  94. exit;
  95. p:=taicpu(hp);
  96. case p.opcode of
  97. { These instructions do not write into a register at all }
  98. A_NOP,
  99. A_FCMPs,A_FCMPd,A_FCMPq,A_CMP,
  100. A_BA,A_Bxx,A_FBA,A_FBxx,
  101. A_STB,A_STH,A_ST,A_STF,A_STDF,
  102. A_STX:
  103. exit;
  104. else
  105. ;
  106. end;
  107. result:=(p.ops>0) and (p.oper[p.ops-1]^.typ=top_reg) and
  108. (p.oper[p.ops-1]^.reg=reg);
  109. end;
  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. if result and (next.typ=ait_instruction) and is_calljmp(taicpu(next).opcode) then
  119. begin
  120. result:=false;
  121. next:=nil;
  122. end;
  123. end;
  124. function TCpuAsmOptimizer.RegUsedAfterInstruction(reg: Tregister; p: tai;
  125. var AllUsedRegs: TAllUsedRegs): Boolean;
  126. begin
  127. AllUsedRegs[getregtype(reg)].Update(tai(p.Next),true);
  128. RegUsedAfterInstruction :=
  129. AllUsedRegs[getregtype(reg)].IsUsed(reg) and
  130. not(regLoadedWithNewValue(reg,p)) and
  131. (
  132. not(GetNextInstruction(p,p)) or
  133. instructionLoadsFromReg(reg,p) or
  134. not(regLoadedWithNewValue(reg,p))
  135. );
  136. end;
  137. {$ifdef DEBUG_AOPTCPU}
  138. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  139. begin
  140. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  141. end;
  142. {$else DEBUG_AOPTCPU}
  143. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  144. begin
  145. end;
  146. {$endif DEBUG_AOPTCPU}
  147. function TCpuAsmOptimizer.TryRemoveMov(var p: tai; opcode: TAsmOp): boolean;
  148. var
  149. next,hp1: tai;
  150. alloc,dealloc: tai_regalloc;
  151. begin
  152. { Fold
  153. op ...,%reg1
  154. ...
  155. opcode %reg1,%reg2
  156. dealloc %reg1
  157. into
  158. op ...,%reg2
  159. opcode may be A_MOV, A_FMOVs, A_FMOVd, etc.
  160. }
  161. result:=false;
  162. if (taicpu(p).ops=3) and
  163. { don't mess with instructions using %g0 for destination }
  164. (taicpu(p).oper[2]^.reg<>NR_G0) and
  165. GetNextInstructionUsingReg(p,next,taicpu(p).oper[2]^.reg) and
  166. MatchInstruction(next,opcode) and
  167. MatchOperand(taicpu(next).oper[0]^,taicpu(p).oper[2]^.reg) and
  168. { the destination register of mov cannot be used between p and next }
  169. (not RegUsedBetween(taicpu(next).oper[1]^.reg,p,next)) and
  170. { This is necessary so 'mov %reg1,%y' is not folded. Compiler should
  171. probably generate A_WRY opcode for this, not A_MOV. }
  172. (getregtype(taicpu(next).oper[1]^.reg)<>R_SPECIALREGISTER) then
  173. begin
  174. dealloc:=FindRegDealloc(taicpu(p).oper[2]^.reg,tai(next.Next));
  175. if assigned(dealloc) then
  176. begin
  177. { taicpu(p).oper[2]^.reg is not used anymore, try to find its allocation
  178. and remove it if possible }
  179. GetLastInstruction(p,hp1);
  180. asml.Remove(dealloc);
  181. alloc:=FindRegAlloc(taicpu(p).oper[2]^.reg,tai(hp1.Next));
  182. if assigned(alloc) then
  183. begin
  184. asml.Remove(alloc);
  185. alloc.free;
  186. dealloc.free;
  187. end
  188. else
  189. asml.InsertAfter(dealloc,p);
  190. { try to move the allocation of the target register }
  191. GetLastInstruction(next,hp1);
  192. alloc:=FindRegAlloc(taicpu(next).oper[1]^.reg,tai(hp1.Next));
  193. if assigned(alloc) then
  194. begin
  195. asml.Remove(alloc);
  196. asml.InsertBefore(alloc,p);
  197. { adjust used regs }
  198. IncludeRegInUsedRegs(taicpu(next).oper[1]^.reg,UsedRegs);
  199. end;
  200. { finally get rid of the mov }
  201. taicpu(p).loadreg(2,taicpu(next).oper[1]^.reg);
  202. DebugMsg('Peephole OpMov2Op done',p);
  203. asml.remove(next);
  204. next.free;
  205. end;
  206. end;
  207. end;
  208. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  209. var
  210. next,next2: tai;
  211. begin
  212. result:=false;
  213. case p.typ of
  214. ait_instruction:
  215. begin
  216. case taicpu(p).opcode of
  217. A_SLL:
  218. begin
  219. { if this is sign/zero extension... }
  220. if (taicpu(p).oper[1]^.typ=top_const) and
  221. GetNextInstruction(p,next) and
  222. (MatchInstruction(next,A_SRL) or MatchInstruction(next,A_SRA)) and
  223. IsSameReg(taicpu(p),taicpu(next)) and
  224. (taicpu(next).oper[1]^.typ=top_const) and
  225. (taicpu(next).oper[1]^.val=taicpu(p).oper[1]^.val) and
  226. (taicpu(next).oper[1]^.val=16) and
  227. { ...followed by 16-bit store (possibly with PIC simplification, etc. in between) }
  228. GetNextInstructionUsingReg(next,next2,taicpu(p).oper[2]^.reg) and
  229. MatchInstruction(next2,A_STH) and
  230. (taicpu(next2).oper[0]^.typ=top_reg) and
  231. (taicpu(next2).oper[0]^.reg=taicpu(p).oper[2]^.reg) and
  232. { the initial register may not be reused }
  233. (not RegUsedBetween(taicpu(p).oper[0]^.reg,next,next2)) then
  234. begin
  235. TransferUsedRegs(TmpUsedRegs);
  236. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  237. UpdateUsedRegs(TmpUsedRegs, tai(next.next));
  238. if not RegUsedAfterInstruction(taicpu(p).oper[2]^.reg,next2,TmpUsedRegs) then
  239. begin
  240. taicpu(next2).loadreg(0,taicpu(p).oper[0]^.reg);
  241. DebugMsg('Peephole SLLSRxSTH2STH done',next2);
  242. asml.remove(p);
  243. asml.remove(next);
  244. p.free;
  245. next.free;
  246. p:=next2;
  247. end;
  248. end
  249. else
  250. TryRemoveMov(p,A_MOV);
  251. end;
  252. {$ifdef SPARC64}
  253. A_SLLX:
  254. begin
  255. { if this is sign/zero extension... }
  256. if (taicpu(p).oper[1]^.typ=top_const) and
  257. GetNextInstruction(p,next) and
  258. (MatchInstruction(next,A_SRLX) or MatchInstruction(next,A_SRAX)) and
  259. IsSameReg(taicpu(p),taicpu(next)) and
  260. (taicpu(next).oper[1]^.typ=top_const) and
  261. (taicpu(next).oper[1]^.val=taicpu(p).oper[1]^.val) and
  262. (taicpu(next).oper[1]^.val=32) and
  263. { ...followed by 32-bit store (possibly with PIC simplification, etc. in between) }
  264. GetNextInstructionUsingReg(next,next2,taicpu(p).oper[2]^.reg) and
  265. MatchInstruction(next2,A_ST) and
  266. (taicpu(next2).oper[0]^.typ=top_reg) and
  267. (taicpu(next2).oper[0]^.reg=taicpu(p).oper[2]^.reg) and
  268. { the initial register may not be reused }
  269. (not RegUsedBetween(taicpu(p).oper[0]^.reg,next,next2)) then
  270. begin
  271. TransferUsedRegs(TmpUsedRegs);
  272. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  273. UpdateUsedRegs(TmpUsedRegs, tai(next.next));
  274. if not RegUsedAfterInstruction(taicpu(p).oper[2]^.reg,next2,TmpUsedRegs) then
  275. begin
  276. taicpu(next2).loadreg(0,taicpu(p).oper[0]^.reg);
  277. DebugMsg('Peephole SLLXSRxXST2ST done',next2);
  278. asml.remove(p);
  279. asml.remove(next);
  280. p.free;
  281. next.free;
  282. p:=next2;
  283. end;
  284. end
  285. else
  286. TryRemoveMov(p,A_MOV);
  287. end;
  288. {$endif SPARC64}
  289. A_SRL:
  290. begin
  291. { happens with a_load_const_ref(...,0), where %g0 is used instead of 0 }
  292. { TODO: override a_load_reg_ref_unaligned and don't generate such shifts }
  293. if (taicpu(p).oper[2]^.typ=top_reg) and
  294. (taicpu(p).oper[2]^.reg=NR_G0) then
  295. begin
  296. next:=tai(p.next);
  297. asml.remove(p);
  298. p.free;
  299. p:=next;
  300. end
  301. { kill zero extension after right shift (e.g. happens with "high(dword)")}
  302. else if (taicpu(p).oper[1]^.typ=top_const) and
  303. (taicpu(p).oper[1]^.val>=16) and
  304. GetNextInstruction(p,next) and
  305. MatchInstruction(next,A_SLL) and
  306. GetNextInstruction(next,next2) and
  307. MatchInstruction(next2,A_SRL) and
  308. IsSameReg(taicpu(p),taicpu(next)) and
  309. IsSameReg(taicpu(p),taicpu(next2)) and
  310. (taicpu(next).oper[1]^.typ=top_const) and
  311. (taicpu(next2).oper[1]^.typ=top_const) and
  312. (taicpu(next).oper[1]^.val=taicpu(next2).oper[1]^.val) and
  313. (taicpu(next).oper[1]^.val=16) then
  314. begin
  315. asml.remove(next);
  316. asml.remove(next2);
  317. next.free;
  318. next2.free;
  319. end
  320. else
  321. TryRemoveMov(p,A_MOV);
  322. end;
  323. A_AND:
  324. begin
  325. { Remove sign extension after 'and' if bit 7 of const operand is clear }
  326. if (taicpu(p).oper[1]^.typ=top_const) and
  327. GetNextInstruction(p,next) and
  328. MatchInstruction(next,A_SLL) and
  329. GetNextInstruction(next,next2) and
  330. MatchInstruction(next2,A_SRA) and
  331. IsSameReg(taicpu(p),taicpu(next)) and
  332. IsSameReg(taicpu(p),taicpu(next2)) and
  333. (taicpu(next).oper[1]^.typ=top_const) and
  334. (taicpu(next2).oper[1]^.typ=top_const) and
  335. (taicpu(next).oper[1]^.val=taicpu(next2).oper[1]^.val) and
  336. ({(
  337. (taicpu(p).oper[2]^.val<=$7fff) and
  338. (taicpu(next).oper[2]^.val=16)
  339. ) or }(
  340. (taicpu(p).oper[1]^.val<=$7f) and
  341. (taicpu(next).oper[1]^.val=24)
  342. )) then
  343. begin
  344. asml.remove(next);
  345. asml.remove(next2);
  346. next.free;
  347. next2.free;
  348. end
  349. else if (taicpu(p).oper[1]^.typ=top_const) and
  350. (taicpu(p).oper[1]^.val=255) and
  351. GetNextInstruction(p,next) and
  352. MatchInstruction(next,A_STB) and
  353. (taicpu(next).oper[0]^.typ=top_reg) and
  354. (taicpu(next).oper[0]^.reg=taicpu(p).oper[2]^.reg) then
  355. begin
  356. TransferUsedRegs(TmpUsedRegs);
  357. UpdateUsedRegs(TmpUsedRegs, tai(p.next));
  358. if not RegUsedAfterInstruction(taicpu(p).oper[2]^.reg,next,TmpUsedRegs) then
  359. begin
  360. taicpu(next).loadreg(0,taicpu(p).oper[0]^.reg);
  361. asml.remove(p);
  362. p.free;
  363. p:=next;
  364. end;
  365. end
  366. else
  367. TryRemoveMov(p,A_MOV);
  368. end;
  369. A_ADD,A_ADDcc,A_ADDX,
  370. A_SUB,A_SUBcc,A_SUBX,
  371. A_SRA,A_SRAX,A_MULX,
  372. A_ANDcc,A_OR,A_ORcc,A_XOR,A_XORcc:
  373. TryRemoveMov(p,A_MOV);
  374. A_FADDs, A_FSUBs, A_FMULs, A_FDIVs,
  375. A_FABSs, A_FNEGs, A_FSQRTs,
  376. A_FDTOs, A_FITOs, A_FQTOs:
  377. TryRemoveMov(p,A_FMOVs);
  378. A_FADDd, A_FSUBd, A_FMULd, A_FDIVd,
  379. A_FABSd, A_FNEGd, A_FSQRTd,
  380. A_FSTOd, A_FITOd, A_FQTOd:
  381. TryRemoveMov(p,A_FMOVd);
  382. else
  383. ;
  384. end;
  385. end;
  386. else
  387. ;
  388. end;
  389. end;
  390. begin
  391. casmoptimizer:=TCpuAsmOptimizer;
  392. end.