aoptcpu.pas 16 KB

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