aoptcpu.pas 50 KB

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