aoptcpu.pas 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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,ait_align]+skipinstr)) then
  500. begin
  501. { don't kill start/end of assembler block,
  502. no-line-info-start/end etc }
  503. if hp1.typ<>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_SETcc :
  880. { changes
  881. setcc (funcres) setcc reg
  882. movb (funcres), reg to leave/ret
  883. leave/ret }
  884. begin
  885. if (taicpu(p).oper[0]^.typ = top_ref) and
  886. GetNextInstruction(p, hp1) and
  887. GetNextInstruction(hp1, hp2) and
  888. IsExitCode(hp2) and
  889. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  890. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  891. not(assigned(current_procinfo.procdef.funcretsym) and
  892. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  893. (hp1.typ = ait_instruction) and
  894. (taicpu(hp1).opcode = A_MOV) and
  895. (taicpu(hp1).opsize = S_B) and
  896. (taicpu(hp1).oper[0]^.typ = top_ref) and
  897. RefsEqual(taicpu(hp1).oper[0]^.ref^, taicpu(p).oper[0]^.ref^) then
  898. begin
  899. taicpu(p).loadReg(0,taicpu(hp1).oper[1]^.reg);
  900. DebugMsg('Peephole optimizer SetccMovbLeaveRet2SetccLeaveRet',p);
  901. asml.remove(hp1);
  902. hp1.free;
  903. end
  904. end;
  905. A_SUB:
  906. if OptPass1Sub(p) then
  907. continue;
  908. A_VMOVAPS,
  909. A_VMOVAPD:
  910. if OptPass1VMOVAP(p) then
  911. continue;
  912. A_VDIVSD,
  913. A_VDIVSS,
  914. A_VSUBSD,
  915. A_VSUBSS,
  916. A_VMULSD,
  917. A_VMULSS,
  918. A_VADDSD,
  919. A_VADDSS,
  920. A_VANDPD,
  921. A_VANDPS,
  922. A_VORPD,
  923. A_VORPS,
  924. A_VXORPD,
  925. A_VXORPS:
  926. if OptPass1VOP(p) then
  927. continue;
  928. A_MULSD,
  929. A_MULSS,
  930. A_ADDSD,
  931. A_ADDSS:
  932. if OptPass1OP(p) then
  933. continue;
  934. A_MOVAPD,
  935. A_MOVAPS:
  936. if OptPass1MOVAP(p) then
  937. continue;
  938. A_VMOVSD,
  939. A_VMOVSS,
  940. A_MOVSD,
  941. A_MOVSS:
  942. if OptPass1MOVXX(p) then
  943. continue;
  944. end;
  945. end; { if is_jmp }
  946. end;
  947. end;
  948. updateUsedRegs(UsedRegs,p);
  949. p:=tai(p.next);
  950. end;
  951. end;
  952. procedure TCPUAsmOptimizer.PeepHoleOptPass2;
  953. var
  954. p : tai;
  955. begin
  956. p := BlockStart;
  957. ClearUsedRegs;
  958. while (p <> BlockEnd) Do
  959. begin
  960. UpdateUsedRegs(UsedRegs, tai(p.next));
  961. case p.Typ Of
  962. Ait_Instruction:
  963. begin
  964. if InsContainsSegRef(taicpu(p)) then
  965. begin
  966. p := tai(p.next);
  967. continue;
  968. end;
  969. case taicpu(p).opcode Of
  970. A_Jcc:
  971. if OptPass2Jcc(p) then
  972. continue;
  973. A_FSTP,A_FISTP:
  974. if DoFpuLoadStoreOpt(p) then
  975. continue;
  976. A_IMUL:
  977. if OptPass2Imul(p) then
  978. continue;
  979. A_JMP:
  980. if OptPass2Jmp(p) then
  981. continue;
  982. A_MOV:
  983. if OptPass2MOV(p) then
  984. continue;
  985. end;
  986. end;
  987. end;
  988. p := tai(p.next)
  989. end;
  990. end;
  991. procedure TCPUAsmOptimizer.PostPeepHoleOpts;
  992. var
  993. p,hp1,hp2: tai;
  994. begin
  995. p := BlockStart;
  996. ClearUsedRegs;
  997. while (p <> BlockEnd) Do
  998. begin
  999. UpdateUsedRegs(UsedRegs, tai(p.next));
  1000. case p.Typ Of
  1001. Ait_Instruction:
  1002. begin
  1003. if InsContainsSegRef(taicpu(p)) then
  1004. begin
  1005. p := tai(p.next);
  1006. continue;
  1007. end;
  1008. case taicpu(p).opcode Of
  1009. A_CALL:
  1010. if PostPeepHoleOptCall(p) then
  1011. Continue;
  1012. A_LEA:
  1013. if PostPeepholeOptLea(p) then
  1014. Continue;
  1015. A_CMP:
  1016. if PostPeepholeOptCmp(p) then
  1017. Continue;
  1018. A_MOV:
  1019. if PostPeepholeOptMov(p) then
  1020. Continue;
  1021. A_MOVZX:
  1022. { if register vars are on, it's possible there is code like }
  1023. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  1024. { so we can't safely replace the movzx then with xor/mov, }
  1025. { since that would change the flags (JM) }
  1026. if not(cs_opt_regvar in current_settings.optimizerswitches) then
  1027. begin
  1028. if (taicpu(p).oper[1]^.typ = top_reg) then
  1029. if (taicpu(p).oper[0]^.typ = top_reg)
  1030. then
  1031. case taicpu(p).opsize of
  1032. S_BL:
  1033. begin
  1034. if IsGP32Reg(taicpu(p).oper[1]^.reg) and
  1035. not(cs_opt_size in current_settings.optimizerswitches) and
  1036. (current_settings.optimizecputype = cpu_Pentium) then
  1037. {Change "movzbl %reg1, %reg2" to
  1038. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1039. PentiumMMX}
  1040. begin
  1041. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  1042. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  1043. InsertLLItem(p.previous, p, hp1);
  1044. taicpu(p).opcode := A_MOV;
  1045. taicpu(p).changeopsize(S_B);
  1046. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  1047. end;
  1048. end;
  1049. end
  1050. else if (taicpu(p).oper[0]^.typ = top_ref) and
  1051. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  1052. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  1053. not(cs_opt_size in current_settings.optimizerswitches) and
  1054. IsGP32Reg(taicpu(p).oper[1]^.reg) and
  1055. (current_settings.optimizecputype = cpu_Pentium) and
  1056. (taicpu(p).opsize = S_BL) then
  1057. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1058. Pentium and PentiumMMX}
  1059. begin
  1060. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  1061. taicpu(p).oper[1]^.reg);
  1062. taicpu(p).opcode := A_MOV;
  1063. taicpu(p).changeopsize(S_B);
  1064. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  1065. InsertLLItem(p.previous, p, hp1);
  1066. end;
  1067. end;
  1068. A_TEST, A_OR:
  1069. if PostPeepholeOptTestOr(p) then
  1070. Continue;
  1071. end;
  1072. end;
  1073. end;
  1074. p := tai(p.next)
  1075. end;
  1076. OptReferences;
  1077. end;
  1078. Procedure TCpuAsmOptimizer.Optimize;
  1079. Var
  1080. HP: Tai;
  1081. pass: longint;
  1082. slowopt, changed, lastLoop: boolean;
  1083. Begin
  1084. slowopt := (cs_opt_level3 in current_settings.optimizerswitches);
  1085. pass := 0;
  1086. changed := false;
  1087. repeat
  1088. lastLoop :=
  1089. not(slowopt) or
  1090. (not changed and (pass > 2)) or
  1091. { prevent endless loops }
  1092. (pass = 4);
  1093. changed := false;
  1094. { Setup labeltable, always necessary }
  1095. blockstart := tai(asml.first);
  1096. pass_1;
  1097. { Blockend now either contains an ait_marker with Kind = mark_AsmBlockStart, }
  1098. { or nil }
  1099. While Assigned(BlockStart) Do
  1100. Begin
  1101. if (cs_opt_peephole in current_settings.optimizerswitches) then
  1102. begin
  1103. if (pass = 0) then
  1104. PrePeepHoleOpts;
  1105. { Peephole optimizations }
  1106. PeepHoleOptPass1;
  1107. { Only perform them twice in the first pass }
  1108. if pass = 0 then
  1109. PeepHoleOptPass1;
  1110. end;
  1111. { More peephole optimizations }
  1112. if (cs_opt_peephole in current_settings.optimizerswitches) then
  1113. begin
  1114. PeepHoleOptPass2;
  1115. if lastLoop then
  1116. PostPeepHoleOpts;
  1117. end;
  1118. { Continue where we left off, BlockEnd is either the start of an }
  1119. { assembler block or nil }
  1120. BlockStart := BlockEnd;
  1121. While Assigned(BlockStart) And
  1122. (BlockStart.typ = ait_Marker) And
  1123. (Tai_Marker(BlockStart).Kind = mark_AsmBlockStart) Do
  1124. Begin
  1125. { We stopped at an assembler block, so skip it }
  1126. Repeat
  1127. BlockStart := Tai(BlockStart.Next);
  1128. Until (BlockStart.Typ = Ait_Marker) And
  1129. (Tai_Marker(Blockstart).Kind = mark_AsmBlockEnd);
  1130. { Blockstart now contains a Tai_marker(mark_AsmBlockEnd) }
  1131. If GetNextInstruction(BlockStart, HP) And
  1132. ((HP.typ <> ait_Marker) Or
  1133. (Tai_Marker(HP).Kind <> mark_AsmBlockStart)) Then
  1134. { There is no assembler block anymore after the current one, so }
  1135. { optimize the next block of "normal" instructions }
  1136. pass_1
  1137. { Otherwise, skip the next assembler block }
  1138. else
  1139. blockStart := hp;
  1140. End;
  1141. End;
  1142. inc(pass);
  1143. until lastLoop;
  1144. dfa.free;
  1145. End;
  1146. begin
  1147. casmoptimizer:=TCpuAsmOptimizer;
  1148. end.