aoptcpu.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. for l := 0 to taicpu(p).ops-1 Do
  363. if (taicpu(p).oper[l]^.typ = top_ref) then
  364. With taicpu(p).oper[l]^.ref^ Do
  365. begin
  366. if (base = NR_NO) and
  367. (index <> NR_NO) and
  368. (scalefactor in [0,1]) then
  369. begin
  370. base := index;
  371. index := NR_NO
  372. end
  373. end;
  374. case taicpu(p).opcode Of
  375. A_AND:
  376. if OptPass1And(p) then
  377. continue;
  378. A_CMP:
  379. begin
  380. { cmp register,$8000 neg register
  381. je target --> jo target
  382. .... only if register is deallocated before jump.}
  383. case Taicpu(p).opsize of
  384. S_B: v:=$80;
  385. S_W: v:=$8000;
  386. S_L: v:=aint($80000000);
  387. else
  388. internalerror(2013112905);
  389. end;
  390. if (taicpu(p).oper[0]^.typ=Top_const) and
  391. (taicpu(p).oper[0]^.val=v) and
  392. (Taicpu(p).oper[1]^.typ=top_reg) and
  393. GetNextInstruction(p, hp1) and
  394. (hp1.typ=ait_instruction) and
  395. (taicpu(hp1).opcode=A_Jcc) and
  396. (Taicpu(hp1).condition in [C_E,C_NE]) and
  397. not(RegInUsedRegs(Taicpu(p).oper[1]^.reg, UsedRegs)) then
  398. begin
  399. Taicpu(p).opcode:=A_NEG;
  400. Taicpu(p).loadoper(0,Taicpu(p).oper[1]^);
  401. Taicpu(p).clearop(1);
  402. Taicpu(p).ops:=1;
  403. if Taicpu(hp1).condition=C_E then
  404. Taicpu(hp1).condition:=C_O
  405. else
  406. Taicpu(hp1).condition:=C_NO;
  407. continue;
  408. end;
  409. {
  410. @@2: @@2:
  411. .... ....
  412. cmp operand1,0
  413. jle/jbe @@1
  414. dec operand1 --> sub operand1,1
  415. jmp @@2 jge/jae @@2
  416. @@1: @@1:
  417. ... ....}
  418. if (taicpu(p).oper[0]^.typ = top_const) and
  419. (taicpu(p).oper[1]^.typ in [top_reg,top_ref]) and
  420. (taicpu(p).oper[0]^.val = 0) and
  421. GetNextInstruction(p, hp1) and
  422. (hp1.typ = ait_instruction) and
  423. (taicpu(hp1).is_jmp) and
  424. (taicpu(hp1).opcode=A_Jcc) and
  425. (taicpu(hp1).condition in [C_LE,C_BE]) and
  426. GetNextInstruction(hp1,hp2) and
  427. (hp2.typ = ait_instruction) and
  428. (taicpu(hp2).opcode = A_DEC) and
  429. OpsEqual(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  430. GetNextInstruction(hp2, hp3) and
  431. (hp3.typ = ait_instruction) and
  432. (taicpu(hp3).is_jmp) and
  433. (taicpu(hp3).opcode = A_JMP) and
  434. GetNextInstruction(hp3, hp4) and
  435. FindLabel(tasmlabel(taicpu(hp1).oper[0]^.ref^.symbol),hp4) then
  436. begin
  437. taicpu(hp2).Opcode := A_SUB;
  438. taicpu(hp2).loadoper(1,taicpu(hp2).oper[0]^);
  439. taicpu(hp2).loadConst(0,1);
  440. taicpu(hp2).ops:=2;
  441. taicpu(hp3).Opcode := A_Jcc;
  442. case taicpu(hp1).condition of
  443. C_LE: taicpu(hp3).condition := C_GE;
  444. C_BE: taicpu(hp3).condition := C_AE;
  445. end;
  446. asml.remove(p);
  447. asml.remove(hp1);
  448. p.free;
  449. hp1.free;
  450. p := hp2;
  451. continue;
  452. end
  453. end;
  454. A_FLD:
  455. begin
  456. if (taicpu(p).oper[0]^.typ = top_reg) and
  457. GetNextInstruction(p, hp1) and
  458. (hp1.typ = Ait_Instruction) and
  459. (taicpu(hp1).oper[0]^.typ = top_reg) and
  460. (taicpu(hp1).oper[1]^.typ = top_reg) and
  461. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  462. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  463. { change to
  464. fld reg fxxx reg,st
  465. fxxxp st, st1 (hp1)
  466. Remark: non commutative operations must be reversed!
  467. }
  468. begin
  469. case taicpu(hp1).opcode Of
  470. A_FMULP,A_FADDP,
  471. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  472. begin
  473. case taicpu(hp1).opcode Of
  474. A_FADDP: taicpu(hp1).opcode := A_FADD;
  475. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  476. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  477. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  478. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  479. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  480. end;
  481. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  482. taicpu(hp1).oper[1]^.reg := NR_ST;
  483. asml.remove(p);
  484. p.free;
  485. p := hp1;
  486. continue;
  487. end;
  488. end;
  489. end
  490. else
  491. if (taicpu(p).oper[0]^.typ = top_ref) and
  492. GetNextInstruction(p, hp2) and
  493. (hp2.typ = Ait_Instruction) and
  494. (taicpu(hp2).ops = 2) and
  495. (taicpu(hp2).oper[0]^.typ = top_reg) and
  496. (taicpu(hp2).oper[1]^.typ = top_reg) and
  497. (taicpu(p).opsize in [S_FS, S_FL]) and
  498. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  499. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  500. if GetLastInstruction(p, hp1) and
  501. (hp1.typ = Ait_Instruction) and
  502. ((taicpu(hp1).opcode = A_FLD) or
  503. (taicpu(hp1).opcode = A_FST)) and
  504. (taicpu(hp1).opsize = taicpu(p).opsize) and
  505. (taicpu(hp1).oper[0]^.typ = top_ref) and
  506. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  507. if ((taicpu(hp2).opcode = A_FMULP) or
  508. (taicpu(hp2).opcode = A_FADDP)) then
  509. { change to
  510. fld/fst mem1 (hp1) fld/fst mem1
  511. fld mem1 (p) fadd/
  512. faddp/ fmul st, st
  513. fmulp st, st1 (hp2) }
  514. begin
  515. asml.remove(p);
  516. p.free;
  517. p := hp1;
  518. if (taicpu(hp2).opcode = A_FADDP) then
  519. taicpu(hp2).opcode := A_FADD
  520. else
  521. taicpu(hp2).opcode := A_FMUL;
  522. taicpu(hp2).oper[1]^.reg := NR_ST;
  523. end
  524. else
  525. { change to
  526. fld/fst mem1 (hp1) fld/fst mem1
  527. fld mem1 (p) fld st}
  528. begin
  529. taicpu(p).changeopsize(S_FL);
  530. taicpu(p).loadreg(0,NR_ST);
  531. end
  532. else
  533. begin
  534. case taicpu(hp2).opcode Of
  535. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  536. { change to
  537. fld/fst mem1 (hp1) fld/fst mem1
  538. fld mem2 (p) fxxx mem2
  539. fxxxp st, st1 (hp2) }
  540. begin
  541. case taicpu(hp2).opcode Of
  542. A_FADDP: taicpu(p).opcode := A_FADD;
  543. A_FMULP: taicpu(p).opcode := A_FMUL;
  544. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  545. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  546. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  547. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  548. end;
  549. asml.remove(hp2);
  550. hp2.free;
  551. end
  552. end
  553. end
  554. end;
  555. A_FSTP,A_FISTP:
  556. if doFpuLoadStoreOpt(p) then
  557. continue;
  558. A_LEA:
  559. begin
  560. if OptPass1LEA(p) then
  561. continue;
  562. end;
  563. A_MOV:
  564. begin
  565. If OptPass1MOV(p) then
  566. Continue;
  567. end;
  568. A_MOVSX,
  569. A_MOVZX :
  570. begin
  571. If OptPass1Movx(p) then
  572. Continue
  573. end;
  574. (* should not be generated anymore by the current code generator
  575. A_POP:
  576. begin
  577. if target_info.system=system_i386_go32v2 then
  578. begin
  579. { Transform a series of pop/pop/pop/push/push/push to }
  580. { 'movl x(%esp),%reg' for go32v2 (not for the rest, }
  581. { because I'm not sure whether they can cope with }
  582. { 'movl x(%esp),%reg' with x > 0, I believe we had }
  583. { such a problem when using esp as frame pointer (JM) }
  584. if (taicpu(p).oper[0]^.typ = top_reg) then
  585. begin
  586. hp1 := p;
  587. hp2 := p;
  588. l := 0;
  589. while getNextInstruction(hp1,hp1) and
  590. (hp1.typ = ait_instruction) and
  591. (taicpu(hp1).opcode = A_POP) and
  592. (taicpu(hp1).oper[0]^.typ = top_reg) do
  593. begin
  594. hp2 := hp1;
  595. inc(l,4);
  596. end;
  597. getLastInstruction(p,hp3);
  598. l1 := 0;
  599. while (hp2 <> hp3) and
  600. assigned(hp1) and
  601. (hp1.typ = ait_instruction) and
  602. (taicpu(hp1).opcode = A_PUSH) and
  603. (taicpu(hp1).oper[0]^.typ = top_reg) and
  604. (taicpu(hp1).oper[0]^.reg.enum = taicpu(hp2).oper[0]^.reg.enum) do
  605. begin
  606. { change it to a two op operation }
  607. taicpu(hp2).oper[1]^.typ:=top_none;
  608. taicpu(hp2).ops:=2;
  609. taicpu(hp2).opcode := A_MOV;
  610. taicpu(hp2).loadoper(1,taicpu(hp1).oper[0]^);
  611. reference_reset(tmpref);
  612. tmpRef.base.enum:=R_INTREGISTER;
  613. tmpRef.base.number:=NR_STACK_POINTER_REG;
  614. convert_register_to_enum(tmpref.base);
  615. tmpRef.offset := l;
  616. taicpu(hp2).loadRef(0,tmpRef);
  617. hp4 := hp1;
  618. getNextInstruction(hp1,hp1);
  619. asml.remove(hp4);
  620. hp4.free;
  621. getLastInstruction(hp2,hp2);
  622. dec(l,4);
  623. inc(l1);
  624. end;
  625. if l <> -4 then
  626. begin
  627. inc(l,4);
  628. for l1 := l1 downto 1 do
  629. begin
  630. getNextInstruction(hp2,hp2);
  631. dec(taicpu(hp2).oper[0]^.ref^.offset,l);
  632. end
  633. end
  634. end
  635. end
  636. else
  637. begin
  638. if (taicpu(p).oper[0]^.typ = top_reg) and
  639. GetNextInstruction(p, hp1) and
  640. (tai(hp1).typ=ait_instruction) and
  641. (taicpu(hp1).opcode=A_PUSH) and
  642. (taicpu(hp1).oper[0]^.typ = top_reg) and
  643. (taicpu(hp1).oper[0]^.reg.enum=taicpu(p).oper[0]^.reg.enum) then
  644. begin
  645. { change it to a two op operation }
  646. taicpu(p).oper[1]^.typ:=top_none;
  647. taicpu(p).ops:=2;
  648. taicpu(p).opcode := A_MOV;
  649. taicpu(p).loadoper(1,taicpu(p).oper[0]^);
  650. reference_reset(tmpref);
  651. TmpRef.base.enum := R_ESP;
  652. taicpu(p).loadRef(0,TmpRef);
  653. asml.remove(hp1);
  654. hp1.free;
  655. end;
  656. end;
  657. end;
  658. *)
  659. A_PUSH:
  660. begin
  661. if (taicpu(p).opsize = S_W) and
  662. (taicpu(p).oper[0]^.typ = Top_Const) and
  663. GetNextInstruction(p, hp1) and
  664. (tai(hp1).typ = ait_instruction) and
  665. (taicpu(hp1).opcode = A_PUSH) and
  666. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  667. (taicpu(hp1).opsize = S_W) then
  668. begin
  669. taicpu(p).changeopsize(S_L);
  670. taicpu(p).loadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  671. asml.remove(hp1);
  672. hp1.free;
  673. end;
  674. end;
  675. A_SHL, A_SAL:
  676. if OptPass1SHLSAL(p) then
  677. Continue;
  678. A_SUB:
  679. if OptPass1Sub(p) then
  680. continue;
  681. A_VMOVAPS,
  682. A_VMOVAPD:
  683. if OptPass1VMOVAP(p) then
  684. continue;
  685. A_VDIVSD,
  686. A_VDIVSS,
  687. A_VSUBSD,
  688. A_VSUBSS,
  689. A_VMULSD,
  690. A_VMULSS,
  691. A_VADDSD,
  692. A_VADDSS,
  693. A_VANDPD,
  694. A_VANDPS,
  695. A_VORPD,
  696. A_VORPS,
  697. A_VXORPD,
  698. A_VXORPS:
  699. if OptPass1VOP(p) then
  700. continue;
  701. A_MULSD,
  702. A_MULSS,
  703. A_ADDSD,
  704. A_ADDSS:
  705. if OptPass1OP(p) then
  706. continue;
  707. A_MOVAPD,
  708. A_MOVAPS:
  709. if OptPass1MOVAP(p) then
  710. continue;
  711. A_VMOVSD,
  712. A_VMOVSS,
  713. A_MOVSD,
  714. A_MOVSS:
  715. if OptPass1MOVXX(p) then
  716. continue;
  717. A_SETcc:
  718. if OptPass1SETcc(p) then
  719. continue;
  720. end;
  721. end; { if is_jmp }
  722. end;
  723. end;
  724. updateUsedRegs(UsedRegs,p);
  725. p:=tai(p.next);
  726. end;
  727. end;
  728. procedure TCPUAsmOptimizer.PeepHoleOptPass2;
  729. var
  730. p : tai;
  731. begin
  732. p := BlockStart;
  733. ClearUsedRegs;
  734. while (p <> BlockEnd) Do
  735. begin
  736. UpdateUsedRegs(UsedRegs, tai(p.next));
  737. case p.Typ Of
  738. Ait_Instruction:
  739. begin
  740. if InsContainsSegRef(taicpu(p)) then
  741. begin
  742. p := tai(p.next);
  743. continue;
  744. end;
  745. case taicpu(p).opcode Of
  746. A_Jcc:
  747. if OptPass2Jcc(p) then
  748. continue;
  749. A_FSTP,A_FISTP:
  750. if DoFpuLoadStoreOpt(p) then
  751. continue;
  752. A_IMUL:
  753. if OptPass2Imul(p) then
  754. continue;
  755. A_JMP:
  756. if OptPass2Jmp(p) then
  757. continue;
  758. A_MOV:
  759. if OptPass2MOV(p) then
  760. continue;
  761. end;
  762. end;
  763. end;
  764. p := tai(p.next)
  765. end;
  766. end;
  767. procedure TCPUAsmOptimizer.PostPeepHoleOpts;
  768. var
  769. p,hp1: tai;
  770. begin
  771. p := BlockStart;
  772. ClearUsedRegs;
  773. while (p <> BlockEnd) Do
  774. begin
  775. UpdateUsedRegs(UsedRegs, tai(p.next));
  776. case p.Typ Of
  777. Ait_Instruction:
  778. begin
  779. if InsContainsSegRef(taicpu(p)) then
  780. begin
  781. p := tai(p.next);
  782. continue;
  783. end;
  784. case taicpu(p).opcode Of
  785. A_CALL:
  786. if PostPeepHoleOptCall(p) then
  787. Continue;
  788. A_LEA:
  789. if PostPeepholeOptLea(p) then
  790. Continue;
  791. A_CMP:
  792. if PostPeepholeOptCmp(p) then
  793. Continue;
  794. A_MOV:
  795. if PostPeepholeOptMov(p) then
  796. Continue;
  797. A_MOVZX:
  798. { if register vars are on, it's possible there is code like }
  799. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  800. { so we can't safely replace the movzx then with xor/mov, }
  801. { since that would change the flags (JM) }
  802. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  803. begin
  804. if (taicpu(p).oper[1]^.typ = top_reg) then
  805. if (taicpu(p).oper[0]^.typ = top_reg)
  806. then
  807. case taicpu(p).opsize of
  808. S_BL:
  809. begin
  810. if IsGP32Reg(taicpu(p).oper[1]^.reg) and
  811. not(cs_opt_size in current_settings.optimizerswitches) and
  812. (current_settings.optimizecputype = cpu_Pentium) then
  813. {Change "movzbl %reg1, %reg2" to
  814. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  815. PentiumMMX}
  816. begin
  817. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  818. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  819. InsertLLItem(p.previous, p, hp1);
  820. taicpu(p).opcode := A_MOV;
  821. taicpu(p).changeopsize(S_B);
  822. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  823. end;
  824. end;
  825. end
  826. else if (taicpu(p).oper[0]^.typ = top_ref) and
  827. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  828. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  829. not(cs_opt_size in current_settings.optimizerswitches) and
  830. IsGP32Reg(taicpu(p).oper[1]^.reg) and
  831. (current_settings.optimizecputype = cpu_Pentium) and
  832. (taicpu(p).opsize = S_BL) then
  833. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  834. Pentium and PentiumMMX}
  835. begin
  836. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  837. taicpu(p).oper[1]^.reg);
  838. taicpu(p).opcode := A_MOV;
  839. taicpu(p).changeopsize(S_B);
  840. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  841. InsertLLItem(p.previous, p, hp1);
  842. end;
  843. end;
  844. A_TEST, A_OR:
  845. if PostPeepholeOptTestOr(p) then
  846. Continue;
  847. end;
  848. end;
  849. end;
  850. p := tai(p.next)
  851. end;
  852. OptReferences;
  853. end;
  854. Procedure TCpuAsmOptimizer.Optimize;
  855. Var
  856. HP: Tai;
  857. pass: longint;
  858. slowopt, changed, lastLoop: boolean;
  859. Begin
  860. slowopt := (cs_opt_level3 in current_settings.optimizerswitches);
  861. pass := 0;
  862. changed := false;
  863. repeat
  864. lastLoop :=
  865. not(slowopt) or
  866. (not changed and (pass > 2)) or
  867. { prevent endless loops }
  868. (pass = 4);
  869. changed := false;
  870. { Setup labeltable, always necessary }
  871. blockstart := tai(asml.first);
  872. pass_1;
  873. { Blockend now either contains an ait_marker with Kind = mark_AsmBlockStart, }
  874. { or nil }
  875. While Assigned(BlockStart) Do
  876. Begin
  877. if (cs_opt_peephole in current_settings.optimizerswitches) then
  878. begin
  879. if (pass = 0) then
  880. PrePeepHoleOpts;
  881. { Peephole optimizations }
  882. PeepHoleOptPass1;
  883. { Only perform them twice in the first pass }
  884. if pass = 0 then
  885. PeepHoleOptPass1;
  886. end;
  887. { More peephole optimizations }
  888. if (cs_opt_peephole in current_settings.optimizerswitches) then
  889. begin
  890. PeepHoleOptPass2;
  891. if lastLoop then
  892. PostPeepHoleOpts;
  893. end;
  894. { Continue where we left off, BlockEnd is either the start of an }
  895. { assembler block or nil }
  896. BlockStart := BlockEnd;
  897. While Assigned(BlockStart) And
  898. (BlockStart.typ = ait_Marker) And
  899. (Tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  900. Begin
  901. { We stopped at an assembler block, so skip it }
  902. Repeat
  903. BlockStart := Tai(BlockStart.Next);
  904. Until (BlockStart.Typ = Ait_Marker) And
  905. (Tai_Marker(Blockstart).Kind = mark_AsmBlockEnd);
  906. { Blockstart now contains a Tai_marker(mark_AsmBlockEnd) }
  907. If GetNextInstruction(BlockStart, HP) And
  908. ((HP.typ <> ait_Marker) Or
  909. (Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
  910. { There is no assembler block anymore after the current one, so }
  911. { optimize the next block of "normal" instructions }
  912. pass_1
  913. { Otherwise, skip the next assembler block }
  914. else
  915. blockStart := hp;
  916. End;
  917. End;
  918. inc(pass);
  919. until lastLoop;
  920. dfa.free;
  921. End;
  922. begin
  923. casmoptimizer:=TCpuAsmOptimizer;
  924. end.