aoptcpu.pas 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Jonas Maebe
  3. This unit contains the peephole optimizer for i386
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aoptcpu;
  18. {$i fpcdefs.inc}
  19. { $define DEBUG_AOPTCPU}
  20. Interface
  21. uses
  22. cgbase,
  23. cpubase, aopt, aoptx86,
  24. Aasmbase,aasmtai,aasmdata;
  25. Type
  26. TCpuAsmOptimizer = class(TX86AsmOptimizer)
  27. procedure Optimize; override;
  28. procedure PrePeepHoleOpts; override;
  29. procedure PeepHoleOptPass1; override;
  30. procedure PeepHoleOptPass2; override;
  31. procedure PostPeepHoleOpts; override;
  32. function DoFpuLoadStoreOpt(var p : tai) : boolean;
  33. end;
  34. Var
  35. AsmOptimizer : TCpuAsmOptimizer;
  36. Implementation
  37. uses
  38. verbose,globtype,globals,
  39. cpuinfo,
  40. aasmcpu,
  41. aoptutils,
  42. procinfo,
  43. cgutils,
  44. { units we should get rid off: }
  45. symsym,symconst;
  46. function TCPUAsmoptimizer.DoFpuLoadStoreOpt(var p: tai): boolean;
  47. { returns true if a "continue" should be done after this optimization }
  48. var hp1, hp2: tai;
  49. begin
  50. DoFpuLoadStoreOpt := false;
  51. if (taicpu(p).oper[0]^.typ = top_ref) and
  52. getNextInstruction(p, hp1) and
  53. (hp1.typ = ait_instruction) and
  54. (((taicpu(hp1).opcode = A_FLD) and
  55. (taicpu(p).opcode = A_FSTP)) or
  56. ((taicpu(p).opcode = A_FISTP) and
  57. (taicpu(hp1).opcode = A_FILD))) and
  58. (taicpu(hp1).oper[0]^.typ = top_ref) and
  59. (taicpu(hp1).opsize = taicpu(p).opsize) and
  60. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  61. begin
  62. { replacing fstp f;fld f by fst f is only valid for extended because of rounding }
  63. if (taicpu(p).opsize=S_FX) and
  64. getNextInstruction(hp1, hp2) and
  65. (hp2.typ = ait_instruction) and
  66. IsExitCode(hp2) and
  67. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  68. not(assigned(current_procinfo.procdef.funcretsym) and
  69. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  70. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  71. begin
  72. asml.remove(p);
  73. asml.remove(hp1);
  74. p.free;
  75. hp1.free;
  76. p := hp2;
  77. removeLastDeallocForFuncRes(p);
  78. doFPULoadStoreOpt := true;
  79. end
  80. (* can't be done because the store operation rounds
  81. else
  82. { fst can't store an extended value! }
  83. if (taicpu(p).opsize <> S_FX) and
  84. (taicpu(p).opsize <> S_IQ) then
  85. begin
  86. if (taicpu(p).opcode = A_FSTP) then
  87. taicpu(p).opcode := A_FST
  88. else taicpu(p).opcode := A_FIST;
  89. asml.remove(hp1);
  90. hp1.free;
  91. end
  92. *)
  93. end;
  94. end;
  95. { converts a TChange variable to a TRegister }
  96. function tch2reg(ch: tinschange): tsuperregister;
  97. const
  98. ch2reg: array[CH_REAX..CH_REDI] of tsuperregister = (RS_EAX,RS_ECX,RS_EDX,RS_EBX,RS_ESP,RS_EBP,RS_ESI,RS_EDI);
  99. begin
  100. if (ch <= CH_REDI) then
  101. tch2reg := ch2reg[ch]
  102. else if (ch <= CH_WEDI) then
  103. tch2reg := ch2reg[tinschange(ord(ch) - ord(CH_REDI))]
  104. else if (ch <= CH_RWEDI) then
  105. tch2reg := ch2reg[tinschange(ord(ch) - ord(CH_WEDI))]
  106. else if (ch <= CH_MEDI) then
  107. tch2reg := ch2reg[tinschange(ord(ch) - ord(CH_RWEDI))]
  108. else
  109. InternalError(2016041901)
  110. end;
  111. { Checks if the register is a 32 bit general purpose register }
  112. function isgp32reg(reg: TRegister): boolean;
  113. begin
  114. {$push}{$warnings off}
  115. isgp32reg:=(getregtype(reg)=R_INTREGISTER) and (getsupreg(reg)>=RS_EAX) and (getsupreg(reg)<=RS_EBX);
  116. {$pop}
  117. end;
  118. { returns true if p contains a memory operand with a segment set }
  119. function InsContainsSegRef(p: taicpu): boolean;
  120. var
  121. i: longint;
  122. begin
  123. result:=true;
  124. for i:=0 to p.opercnt-1 do
  125. if (p.oper[i]^.typ=top_ref) and
  126. (p.oper[i]^.ref^.segment<>NR_NO) then
  127. exit;
  128. result:=false;
  129. end;
  130. procedure TCPUAsmOptimizer.PrePeepHoleOpts;
  131. var
  132. p: tai;
  133. begin
  134. p := BlockStart;
  135. while (p <> BlockEnd) Do
  136. begin
  137. case p.Typ Of
  138. Ait_Instruction:
  139. begin
  140. if InsContainsSegRef(taicpu(p)) then
  141. begin
  142. p := tai(p.next);
  143. continue;
  144. end;
  145. case taicpu(p).opcode Of
  146. A_IMUL:
  147. if PrePeepholeOptIMUL(p) then
  148. Continue;
  149. A_SAR,A_SHR:
  150. if PrePeepholeOptSxx(p) then
  151. continue;
  152. A_XOR:
  153. if (taicpu(p).oper[0]^.typ = top_reg) and
  154. (taicpu(p).oper[1]^.typ = top_reg) and
  155. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  156. { temporarily change this to 'mov reg,0' to make it easier }
  157. { for the CSE. Will be changed back in pass 2 }
  158. begin
  159. taicpu(p).opcode := A_MOV;
  160. taicpu(p).loadConst(0,0);
  161. end;
  162. end;
  163. end;
  164. end;
  165. p := tai(p.next)
  166. end;
  167. end;
  168. { First pass of peephole optimizations }
  169. procedure TCPUAsmOPtimizer.PeepHoleOptPass1;
  170. function WriteOk : Boolean;
  171. begin
  172. writeln('Ok');
  173. Result:=True;
  174. end;
  175. var
  176. l : longint;
  177. p,hp1,hp2 : tai;
  178. hp3,hp4: tai;
  179. v:aint;
  180. function GetFinalDestination(asml: TAsmList; hp: taicpu; level: longint): boolean;
  181. {traces sucessive jumps to their final destination and sets it, e.g.
  182. je l1 je l3
  183. <code> <code>
  184. l1: becomes l1:
  185. je l2 je l3
  186. <code> <code>
  187. l2: l2:
  188. jmp l3 jmp l3
  189. the level parameter denotes how deeep we have already followed the jump,
  190. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  191. var p1, p2: tai;
  192. l: tasmlabel;
  193. function FindAnyLabel(hp: tai; var l: tasmlabel): Boolean;
  194. begin
  195. FindAnyLabel := false;
  196. while assigned(hp.next) and
  197. (tai(hp.next).typ in (SkipInstr+[ait_align])) Do
  198. hp := tai(hp.next);
  199. if assigned(hp.next) and
  200. (tai(hp.next).typ = ait_label) then
  201. begin
  202. FindAnyLabel := true;
  203. l := tai_label(hp.next).labsym;
  204. end
  205. end;
  206. begin
  207. GetfinalDestination := false;
  208. if level > 20 then
  209. exit;
  210. p1 := getlabelwithsym(tasmlabel(hp.oper[0]^.ref^.symbol));
  211. if assigned(p1) then
  212. begin
  213. SkipLabels(p1,p1);
  214. if (tai(p1).typ = ait_instruction) and
  215. (taicpu(p1).is_jmp) then
  216. if { the next instruction after the label where the jump hp arrives}
  217. { is unconditional or of the same type as hp, so continue }
  218. (taicpu(p1).condition in [C_None,hp.condition]) or
  219. { the next instruction after the label where the jump hp arrives}
  220. { is the opposite of hp (so this one is never taken), but after }
  221. { that one there is a branch that will be taken, so perform a }
  222. { little hack: set p1 equal to this instruction (that's what the}
  223. { last SkipLabels is for, only works with short bool evaluation)}
  224. ((taicpu(p1).condition = inverse_cond(hp.condition)) and
  225. SkipLabels(p1,p2) and
  226. (p2.typ = ait_instruction) and
  227. (taicpu(p2).is_jmp) and
  228. (taicpu(p2).condition in [C_None,hp.condition]) and
  229. SkipLabels(p1,p1)) then
  230. begin
  231. { quick check for loops of the form "l5: ; jmp l5 }
  232. if (tasmlabel(taicpu(p1).oper[0]^.ref^.symbol).labelnr =
  233. tasmlabel(hp.oper[0]^.ref^.symbol).labelnr) then
  234. exit;
  235. if not GetFinalDestination(asml, taicpu(p1),succ(level)) then
  236. exit;
  237. tasmlabel(hp.oper[0]^.ref^.symbol).decrefs;
  238. hp.oper[0]^.ref^.symbol:=taicpu(p1).oper[0]^.ref^.symbol;
  239. tasmlabel(hp.oper[0]^.ref^.symbol).increfs;
  240. end
  241. else
  242. if (taicpu(p1).condition = inverse_cond(hp.condition)) then
  243. if not FindAnyLabel(p1,l) then
  244. begin
  245. {$ifdef finaldestdebug}
  246. insertllitem(asml,p1,p1.next,tai_comment.Create(
  247. strpnew('previous label inserted'))));
  248. {$endif finaldestdebug}
  249. current_asmdata.getjumplabel(l);
  250. insertllitem(p1,p1.next,tai_label.Create(l));
  251. tasmlabel(taicpu(hp).oper[0]^.ref^.symbol).decrefs;
  252. hp.oper[0]^.ref^.symbol := l;
  253. l.increfs;
  254. { this won't work, since the new label isn't in the labeltable }
  255. { so it will fail the rangecheck. Labeltable should become a }
  256. { hashtable to support this: }
  257. { GetFinalDestination(asml, hp); }
  258. end
  259. else
  260. begin
  261. {$ifdef finaldestdebug}
  262. insertllitem(asml,p1,p1.next,tai_comment.Create(
  263. strpnew('next label reused'))));
  264. {$endif finaldestdebug}
  265. l.increfs;
  266. hp.oper[0]^.ref^.symbol := l;
  267. if not GetFinalDestination(asml, hp,succ(level)) then
  268. exit;
  269. end;
  270. end;
  271. GetFinalDestination := true;
  272. end;
  273. begin
  274. p := BlockStart;
  275. ClearUsedRegs;
  276. while (p <> BlockEnd) Do
  277. begin
  278. UpDateUsedRegs(UsedRegs, tai(p.next));
  279. case p.Typ Of
  280. ait_instruction:
  281. begin
  282. current_filepos:=taicpu(p).fileinfo;
  283. if InsContainsSegRef(taicpu(p)) then
  284. begin
  285. p := tai(p.next);
  286. continue;
  287. end;
  288. { Handle Jmp Optimizations }
  289. if taicpu(p).is_jmp then
  290. begin
  291. { the following if-block removes all code between a jmp and the next label,
  292. because it can never be executed }
  293. if (taicpu(p).opcode = A_JMP) then
  294. begin
  295. hp2:=p;
  296. while GetNextInstruction(hp2, hp1) and
  297. (hp1.typ <> ait_label) do
  298. if not(hp1.typ in ([ait_label]+skipinstr)) then
  299. begin
  300. { don't kill start/end of assembler block,
  301. no-line-info-start/end etc }
  302. if not(hp1.typ in [ait_align,ait_marker]) then
  303. begin
  304. asml.remove(hp1);
  305. hp1.free;
  306. end
  307. else
  308. hp2:=hp1;
  309. end
  310. else break;
  311. end;
  312. { remove jumps to a label coming right after them }
  313. if GetNextInstruction(p, hp1) then
  314. begin
  315. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp1) and
  316. { TODO: FIXME removing the first instruction fails}
  317. (p<>blockstart) then
  318. begin
  319. hp2:=tai(hp1.next);
  320. asml.remove(p);
  321. p.free;
  322. p:=hp2;
  323. continue;
  324. end
  325. else
  326. begin
  327. if hp1.typ = ait_label then
  328. SkipLabels(hp1,hp1);
  329. if (tai(hp1).typ=ait_instruction) and
  330. (taicpu(hp1).opcode=A_JMP) and
  331. GetNextInstruction(hp1, hp2) and
  332. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp2) then
  333. begin
  334. if taicpu(p).opcode=A_Jcc then
  335. begin
  336. taicpu(p).condition:=inverse_cond(taicpu(p).condition);
  337. tai_label(hp2).labsym.decrefs;
  338. taicpu(p).oper[0]^.ref^.symbol:=taicpu(hp1).oper[0]^.ref^.symbol;
  339. { when free'ing hp1, the ref. isn't decresed, so we don't
  340. increase it (FK)
  341. taicpu(p).oper[0]^.ref^.symbol.increfs;
  342. }
  343. asml.remove(hp1);
  344. hp1.free;
  345. GetFinalDestination(asml, taicpu(p),0);
  346. end
  347. else
  348. begin
  349. GetFinalDestination(asml, taicpu(p),0);
  350. p:=tai(p.next);
  351. continue;
  352. end;
  353. end
  354. else
  355. GetFinalDestination(asml, taicpu(p),0);
  356. end;
  357. end;
  358. end
  359. else
  360. { All other optimizes }
  361. begin
  362. case taicpu(p).opcode Of
  363. A_AND:
  364. if OptPass1And(p) then
  365. continue;
  366. A_CMP:
  367. begin
  368. { cmp register,$8000 neg register
  369. je target --> jo target
  370. .... only if register is deallocated before jump.}
  371. case Taicpu(p).opsize of
  372. S_B: v:=$80;
  373. S_W: v:=$8000;
  374. S_L: v:=aint($80000000);
  375. else
  376. internalerror(2013112905);
  377. end;
  378. if (taicpu(p).oper[0]^.typ=Top_const) and
  379. (taicpu(p).oper[0]^.val=v) and
  380. (Taicpu(p).oper[1]^.typ=top_reg) and
  381. GetNextInstruction(p, hp1) and
  382. (hp1.typ=ait_instruction) and
  383. (taicpu(hp1).opcode=A_Jcc) and
  384. (Taicpu(hp1).condition in [C_E,C_NE]) and
  385. not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, UsedRegs)) then
  386. begin
  387. Taicpu(p).opcode:=A_NEG;
  388. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  389. Taicpu(p).clearop(1);
  390. Taicpu(p).ops:=1;
  391. if Taicpu(hp1).condition=C_E then
  392. Taicpu(hp1).condition:=C_O
  393. else
  394. Taicpu(hp1).condition:=C_NO;
  395. continue;
  396. end;
  397. {
  398. @@2: @@2:
  399. .... ....
  400. cmp operand1,0
  401. jle/jbe @@1
  402. dec operand1 --> sub operand1,1
  403. jmp @@2 jge/jae @@2
  404. @@1: @@1:
  405. ... ....}
  406. if (taicpu(p).oper[0]^.typ = top_const) and
  407. (taicpu(p).oper[1]^.typ in [top_reg,top_ref]) and
  408. (taicpu(p).oper[0]^.val = 0) and
  409. GetNextInstruction(p, hp1) and
  410. (hp1.typ = ait_instruction) and
  411. (taicpu(hp1).is_jmp) and
  412. (taicpu(hp1).opcode=A_Jcc) and
  413. (taicpu(hp1).condition in [C_LE,C_BE]) and
  414. GetNextInstruction(hp1,hp2) and
  415. (hp2.typ = ait_instruction) and
  416. (taicpu(hp2).opcode = A_DEC) and
  417. OpsEqual(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  418. GetNextInstruction(hp2, hp3) and
  419. (hp3.typ = ait_instruction) and
  420. (taicpu(hp3).is_jmp) and
  421. (taicpu(hp3).opcode = A_JMP) and
  422. GetNextInstruction(hp3, hp4) and
  423. FindLabel(tasmlabel(taicpu(hp1).oper[0]^.ref^.symbol),hp4) then
  424. begin
  425. taicpu(hp2).Opcode := A_SUB;
  426. taicpu(hp2).loadoper(1,taicpu(hp2).oper[0]^);
  427. taicpu(hp2).loadConst(0,1);
  428. taicpu(hp2).ops:=2;
  429. taicpu(hp3).Opcode := A_Jcc;
  430. case taicpu(hp1).condition of
  431. C_LE: taicpu(hp3).condition := C_GE;
  432. C_BE: taicpu(hp3).condition := C_AE;
  433. end;
  434. asml.remove(p);
  435. asml.remove(hp1);
  436. p.free;
  437. hp1.free;
  438. p := hp2;
  439. continue;
  440. end
  441. end;
  442. A_FLD:
  443. begin
  444. if (taicpu(p).oper[0]^.typ = top_reg) and
  445. GetNextInstruction(p, hp1) and
  446. (hp1.typ = Ait_Instruction) and
  447. (taicpu(hp1).oper[0]^.typ = top_reg) and
  448. (taicpu(hp1).oper[1]^.typ = top_reg) and
  449. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  450. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  451. { change to
  452. fld reg fxxx reg,st
  453. fxxxp st, st1 (hp1)
  454. Remark: non commutative operations must be reversed!
  455. }
  456. begin
  457. case taicpu(hp1).opcode Of
  458. A_FMULP,A_FADDP,
  459. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  460. begin
  461. case taicpu(hp1).opcode Of
  462. A_FADDP: taicpu(hp1).opcode := A_FADD;
  463. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  464. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  465. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  466. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  467. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  468. end;
  469. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  470. taicpu(hp1).oper[1]^.reg := NR_ST;
  471. asml.remove(p);
  472. p.free;
  473. p := hp1;
  474. continue;
  475. end;
  476. end;
  477. end
  478. else
  479. if (taicpu(p).oper[0]^.typ = top_ref) and
  480. GetNextInstruction(p, hp2) and
  481. (hp2.typ = Ait_Instruction) and
  482. (taicpu(hp2).ops = 2) and
  483. (taicpu(hp2).oper[0]^.typ = top_reg) and
  484. (taicpu(hp2).oper[1]^.typ = top_reg) and
  485. (taicpu(p).opsize in [S_FS, S_FL]) and
  486. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  487. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  488. if GetLastInstruction(p, hp1) and
  489. (hp1.typ = Ait_Instruction) and
  490. ((taicpu(hp1).opcode = A_FLD) or
  491. (taicpu(hp1).opcode = A_FST)) and
  492. (taicpu(hp1).opsize = taicpu(p).opsize) and
  493. (taicpu(hp1).oper[0]^.typ = top_ref) and
  494. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  495. if ((taicpu(hp2).opcode = A_FMULP) or
  496. (taicpu(hp2).opcode = A_FADDP)) then
  497. { change to
  498. fld/fst mem1 (hp1) fld/fst mem1
  499. fld mem1 (p) fadd/
  500. faddp/ fmul st, st
  501. fmulp st, st1 (hp2) }
  502. begin
  503. asml.remove(p);
  504. p.free;
  505. p := hp1;
  506. if (taicpu(hp2).opcode = A_FADDP) then
  507. taicpu(hp2).opcode := A_FADD
  508. else
  509. taicpu(hp2).opcode := A_FMUL;
  510. taicpu(hp2).oper[1]^.reg := NR_ST;
  511. end
  512. else
  513. { change to
  514. fld/fst mem1 (hp1) fld/fst mem1
  515. fld mem1 (p) fld st}
  516. begin
  517. taicpu(p).changeopsize(S_FL);
  518. taicpu(p).loadreg(0,NR_ST);
  519. end
  520. else
  521. begin
  522. case taicpu(hp2).opcode Of
  523. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  524. { change to
  525. fld/fst mem1 (hp1) fld/fst mem1
  526. fld mem2 (p) fxxx mem2
  527. fxxxp st, st1 (hp2) }
  528. begin
  529. case taicpu(hp2).opcode Of
  530. A_FADDP: taicpu(p).opcode := A_FADD;
  531. A_FMULP: taicpu(p).opcode := A_FMUL;
  532. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  533. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  534. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  535. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  536. end;
  537. asml.remove(hp2);
  538. hp2.free;
  539. end
  540. end
  541. end
  542. end;
  543. A_FSTP,A_FISTP:
  544. if doFpuLoadStoreOpt(p) then
  545. continue;
  546. A_LEA:
  547. begin
  548. if OptPass1LEA(p) then
  549. continue;
  550. end;
  551. A_MOV:
  552. begin
  553. If OptPass1MOV(p) then
  554. Continue;
  555. end;
  556. A_MOVSX,
  557. A_MOVZX :
  558. begin
  559. If OptPass1Movx(p) then
  560. Continue
  561. end;
  562. (* should not be generated anymore by the current code generator
  563. A_POP:
  564. begin
  565. if target_info.system=system_i386_go32v2 then
  566. begin
  567. { Transform a series of pop/pop/pop/push/push/push to }
  568. { 'movl x(%esp),%reg' for go32v2 (not for the rest, }
  569. { because I'm not sure whether they can cope with }
  570. { 'movl x(%esp),%reg' with x > 0, I believe we had }
  571. { such a problem when using esp as frame pointer (JM) }
  572. if (taicpu(p).oper[0]^.typ = top_reg) then
  573. begin
  574. hp1 := p;
  575. hp2 := p;
  576. l := 0;
  577. while getNextInstruction(hp1,hp1) and
  578. (hp1.typ = ait_instruction) and
  579. (taicpu(hp1).opcode = A_POP) and
  580. (taicpu(hp1).oper[0]^.typ = top_reg) do
  581. begin
  582. hp2 := hp1;
  583. inc(l,4);
  584. end;
  585. getLastInstruction(p,hp3);
  586. l1 := 0;
  587. while (hp2 <> hp3) and
  588. assigned(hp1) and
  589. (hp1.typ = ait_instruction) and
  590. (taicpu(hp1).opcode = A_PUSH) and
  591. (taicpu(hp1).oper[0]^.typ = top_reg) and
  592. (taicpu(hp1).oper[0]^.reg.enum = taicpu(hp2).oper[0]^.reg.enum) do
  593. begin
  594. { change it to a two op operation }
  595. taicpu(hp2).oper[1]^.typ:=top_none;
  596. taicpu(hp2).ops:=2;
  597. taicpu(hp2).opcode := A_MOV;
  598. taicpu(hp2).loadoper(1,taicpu(hp1).oper[0]^);
  599. reference_reset(tmpref);
  600. tmpRef.base.enum:=R_INTREGISTER;
  601. tmpRef.base.number:=NR_STACK_POINTER_REG;
  602. convert_register_to_enum(tmpref.base);
  603. tmpRef.offset := l;
  604. taicpu(hp2).loadRef(0,tmpRef);
  605. hp4 := hp1;
  606. getNextInstruction(hp1,hp1);
  607. asml.remove(hp4);
  608. hp4.free;
  609. getLastInstruction(hp2,hp2);
  610. dec(l,4);
  611. inc(l1);
  612. end;
  613. if l <> -4 then
  614. begin
  615. inc(l,4);
  616. for l1 := l1 downto 1 do
  617. begin
  618. getNextInstruction(hp2,hp2);
  619. dec(taicpu(hp2).oper[0]^.ref^.offset,l);
  620. end
  621. end
  622. end
  623. end
  624. else
  625. begin
  626. if (taicpu(p).oper[0]^.typ = top_reg) and
  627. GetNextInstruction(p, hp1) and
  628. (tai(hp1).typ=ait_instruction) and
  629. (taicpu(hp1).opcode=A_PUSH) and
  630. (taicpu(hp1).oper[0]^.typ = top_reg) and
  631. (taicpu(hp1).oper[0]^.reg.enum=taicpu(p).oper[0]^.reg.enum) then
  632. begin
  633. { change it to a two op operation }
  634. taicpu(p).oper[1]^.typ:=top_none;
  635. taicpu(p).ops:=2;
  636. taicpu(p).opcode := A_MOV;
  637. taicpu(p).loadoper(1,taicpu(p).oper[0]^);
  638. reference_reset(tmpref);
  639. TmpRef.base.enum := R_ESP;
  640. taicpu(p).loadRef(0,TmpRef);
  641. asml.remove(hp1);
  642. hp1.free;
  643. end;
  644. end;
  645. end;
  646. *)
  647. A_PUSH:
  648. begin
  649. if (taicpu(p).opsize = S_W) and
  650. (taicpu(p).oper[0]^.typ = Top_Const) and
  651. GetNextInstruction(p, hp1) and
  652. (tai(hp1).typ = ait_instruction) and
  653. (taicpu(hp1).opcode = A_PUSH) and
  654. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  655. (taicpu(hp1).opsize = S_W) then
  656. begin
  657. taicpu(p).changeopsize(S_L);
  658. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  659. asml.remove(hp1);
  660. hp1.free;
  661. end;
  662. end;
  663. A_SHL, A_SAL:
  664. if OptPass1SHLSAL(p) then
  665. Continue;
  666. A_SUB:
  667. if OptPass1Sub(p) then
  668. continue;
  669. A_VMOVAPS,
  670. A_VMOVAPD:
  671. if OptPass1VMOVAP(p) then
  672. continue;
  673. A_VDIVSD,
  674. A_VDIVSS,
  675. A_VSUBSD,
  676. A_VSUBSS,
  677. A_VMULSD,
  678. A_VMULSS,
  679. A_VADDSD,
  680. A_VADDSS,
  681. A_VANDPD,
  682. A_VANDPS,
  683. A_VORPD,
  684. A_VORPS,
  685. A_VXORPD,
  686. A_VXORPS:
  687. if OptPass1VOP(p) then
  688. continue;
  689. A_MULSD,
  690. A_MULSS,
  691. A_ADDSD,
  692. A_ADDSS:
  693. if OptPass1OP(p) then
  694. continue;
  695. A_MOVAPD,
  696. A_MOVAPS:
  697. if OptPass1MOVAP(p) then
  698. continue;
  699. A_VMOVSD,
  700. A_VMOVSS,
  701. A_MOVSD,
  702. A_MOVSS:
  703. if OptPass1MOVXX(p) then
  704. continue;
  705. A_SETcc:
  706. if OptPass1SETcc(p) then
  707. continue;
  708. end;
  709. end; { if is_jmp }
  710. end;
  711. end;
  712. updateUsedRegs(UsedRegs,p);
  713. p:=tai(p.next);
  714. end;
  715. end;
  716. procedure TCPUAsmOptimizer.PeepHoleOptPass2;
  717. var
  718. p : tai;
  719. begin
  720. p := BlockStart;
  721. ClearUsedRegs;
  722. while (p <> BlockEnd) Do
  723. begin
  724. UpdateUsedRegs(UsedRegs, tai(p.next));
  725. case p.Typ Of
  726. Ait_Instruction:
  727. begin
  728. if InsContainsSegRef(taicpu(p)) then
  729. begin
  730. p := tai(p.next);
  731. continue;
  732. end;
  733. case taicpu(p).opcode Of
  734. A_Jcc:
  735. if OptPass2Jcc(p) then
  736. continue;
  737. A_FSTP,A_FISTP:
  738. if DoFpuLoadStoreOpt(p) then
  739. continue;
  740. A_IMUL:
  741. if OptPass2Imul(p) then
  742. continue;
  743. A_JMP:
  744. if OptPass2Jmp(p) then
  745. continue;
  746. A_MOV:
  747. if OptPass2MOV(p) then
  748. continue;
  749. end;
  750. end;
  751. end;
  752. p := tai(p.next)
  753. end;
  754. end;
  755. procedure TCPUAsmOptimizer.PostPeepHoleOpts;
  756. var
  757. p,hp1: tai;
  758. begin
  759. p := BlockStart;
  760. ClearUsedRegs;
  761. while (p <> BlockEnd) Do
  762. begin
  763. UpdateUsedRegs(UsedRegs, tai(p.next));
  764. case p.Typ Of
  765. Ait_Instruction:
  766. begin
  767. if InsContainsSegRef(taicpu(p)) then
  768. begin
  769. p := tai(p.next);
  770. continue;
  771. end;
  772. case taicpu(p).opcode Of
  773. A_CALL:
  774. if PostPeepHoleOptCall(p) then
  775. Continue;
  776. A_LEA:
  777. if PostPeepholeOptLea(p) then
  778. Continue;
  779. A_CMP:
  780. if PostPeepholeOptCmp(p) then
  781. Continue;
  782. A_MOV:
  783. if PostPeepholeOptMov(p) then
  784. Continue;
  785. A_MOVZX:
  786. { if register vars are on, it's possible there is code like }
  787. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  788. { so we can't safely replace the movzx then with xor/mov, }
  789. { since that would change the flags (JM) }
  790. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  791. begin
  792. if (taicpu(p).oper[1]^.typ = top_reg) then
  793. if (taicpu(p).oper[0]^.typ = top_reg)
  794. then
  795. case taicpu(p).opsize of
  796. S_BL:
  797. begin
  798. if IsGP32Reg(taicpu(p).oper[1]^.reg) and
  799. not(cs_opt_size in current_settings.optimizerswitches) and
  800. (current_settings.optimizecputype = cpu_Pentium) then
  801. {Change "movzbl %reg1, %reg2" to
  802. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  803. PentiumMMX}
  804. begin
  805. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  806. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  807. InsertLLItem(p.previous, p, hp1);
  808. taicpu(p).opcode := A_MOV;
  809. taicpu(p).changeopsize(S_B);
  810. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  811. end;
  812. end;
  813. end
  814. else if (taicpu(p).oper[0]^.typ = top_ref) and
  815. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  816. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  817. not(cs_opt_size in current_settings.optimizerswitches) and
  818. IsGP32Reg(taicpu(p).oper[1]^.reg) and
  819. (current_settings.optimizecputype = cpu_Pentium) and
  820. (taicpu(p).opsize = S_BL) then
  821. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  822. Pentium and PentiumMMX}
  823. begin
  824. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  825. taicpu(p).oper[1]^.reg);
  826. taicpu(p).opcode := A_MOV;
  827. taicpu(p).changeopsize(S_B);
  828. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  829. InsertLLItem(p.previous, p, hp1);
  830. end;
  831. end;
  832. A_TEST, A_OR:
  833. if PostPeepholeOptTestOr(p) then
  834. Continue;
  835. end;
  836. end;
  837. end;
  838. p := tai(p.next)
  839. end;
  840. OptReferences;
  841. end;
  842. Procedure TCpuAsmOptimizer.Optimize;
  843. Var
  844. HP: Tai;
  845. pass: longint;
  846. slowopt, changed, lastLoop: boolean;
  847. Begin
  848. slowopt := (cs_opt_level3 in current_settings.optimizerswitches);
  849. pass := 0;
  850. changed := false;
  851. repeat
  852. lastLoop :=
  853. not(slowopt) or
  854. (not changed and (pass > 2)) or
  855. { prevent endless loops }
  856. (pass = 4);
  857. changed := false;
  858. { Setup labeltable, always necessary }
  859. blockstart := tai(asml.first);
  860. pass_1;
  861. { Blockend now either contains an ait_marker with Kind = mark_AsmBlockStart, }
  862. { or nil }
  863. While Assigned(BlockStart) Do
  864. Begin
  865. if (cs_opt_peephole in current_settings.optimizerswitches) then
  866. begin
  867. if (pass = 0) then
  868. PrePeepHoleOpts;
  869. { Peephole optimizations }
  870. PeepHoleOptPass1;
  871. { Only perform them twice in the first pass }
  872. if pass = 0 then
  873. PeepHoleOptPass1;
  874. end;
  875. { More peephole optimizations }
  876. if (cs_opt_peephole in current_settings.optimizerswitches) then
  877. begin
  878. PeepHoleOptPass2;
  879. if lastLoop then
  880. PostPeepHoleOpts;
  881. end;
  882. { Continue where we left off, BlockEnd is either the start of an }
  883. { assembler block or nil }
  884. BlockStart := BlockEnd;
  885. While Assigned(BlockStart) And
  886. (BlockStart.typ = ait_Marker) And
  887. (Tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  888. Begin
  889. { We stopped at an assembler block, so skip it }
  890. Repeat
  891. BlockStart := Tai(BlockStart.Next);
  892. Until (BlockStart.Typ = Ait_Marker) And
  893. (Tai_Marker(Blockstart).Kind = mark_AsmBlockEnd);
  894. { Blockstart now contains a Tai_marker(mark_AsmBlockEnd) }
  895. If GetNextInstruction(BlockStart, HP) And
  896. ((HP.typ <> ait_Marker) Or
  897. (Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
  898. { There is no assembler block anymore after the current one, so }
  899. { optimize the next block of "normal" instructions }
  900. pass_1
  901. { Otherwise, skip the next assembler block }
  902. else
  903. blockStart := hp;
  904. End;
  905. End;
  906. inc(pass);
  907. until lastLoop;
  908. dfa.free;
  909. End;
  910. begin
  911. casmoptimizer:=TCpuAsmOptimizer;
  912. end.