aoptcpu.pas 50 KB

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