aoptcpu.pas 16 KB

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