popt386.pas 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl and Jonas Maebe
  4. This unit contains the peephole optimizer.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit POpt386;
  19. Interface
  20. Uses Aasm;
  21. Procedure PeepHoleOptPass1(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  22. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  23. Implementation
  24. Uses
  25. globtype,systems,
  26. globals,verbose,hcodegen,
  27. i386base,i386asm,
  28. DAOpt386;
  29. Function RegUsedAfterInstruction(Reg: TRegister; p: Pai; Var UsedRegs: TRegSet): Boolean;
  30. Begin
  31. UpdateUsedRegs(UsedRegs, Pai(p^.Next));
  32. RegUsedAfterInstruction := Reg in UsedRegs
  33. End;
  34. Procedure PeepHoleOptPass1(Asml: PAasmOutput; BlockStart, BlockEnd: Pai);
  35. {First pass of peepholeoptimizations}
  36. Var
  37. l : longint;
  38. p ,hp1, hp2: pai;
  39. TmpBool1, TmpBool2: Boolean;
  40. TmpRef: PReference;
  41. UsedRegs, TmpUsedRegs: TRegSet;
  42. Procedure GetFinalDestination(hp: pai386);
  43. {traces sucessive jumps to their final destination and sets it, e.g.
  44. je l1 je l3
  45. <code> <code>
  46. l1: becomes l1:
  47. je l2 je l3
  48. <code> <code>
  49. l2: l2:
  50. jmp l3 jmp l3}
  51. Var p1: pai;
  52. Function SkipLabels(hp: Pai): Pai;
  53. {skips all labels and returns the next "real" instruction; it is
  54. assumed that hp is of the type ait_label}
  55. Begin
  56. While assigned(hp^.next) and
  57. (pai(hp^.next)^.typ In SkipInstr + [ait_label]) Do
  58. hp := pai(hp^.next);
  59. If assigned(hp^.next)
  60. Then SkipLabels := pai(hp^.next)
  61. Else SkipLabels := hp;
  62. End;
  63. Begin
  64. If (pasmlabel(hp^.oper[0].sym)^.labelnr >= LoLab) and
  65. (pasmlabel(hp^.oper[0].sym)^.labelnr <= HiLab) and {range check, a jump can go past an assembler block!}
  66. Assigned(LTable^[pasmlabel(hp^.oper[0].sym)^.labelnr-LoLab].PaiObj) Then
  67. Begin
  68. p1 := LTable^[pasmlabel(hp^.oper[0].sym)^.labelnr-LoLab].PaiObj; {the jump's destination}
  69. p1 := SkipLabels(p1);
  70. If (pai(p1)^.typ = ait_instruction) and
  71. (pai386(p1)^.is_jmp) and
  72. (pai386(p1)^.condition = hp^.condition) Then
  73. Begin
  74. GetFinalDestination(pai386(p1));
  75. Dec(pasmlabel(hp^.oper[0].sym)^.refs);
  76. hp^.oper[0].sym:=pai386(p1)^.oper[0].sym;
  77. inc(pasmlabel(hp^.oper[0].sym)^.refs);
  78. End;
  79. End;
  80. End;
  81. Begin
  82. P := BlockStart;
  83. UsedRegs := [];
  84. While (P <> BlockEnd) Do
  85. Begin
  86. UpDateUsedRegs(UsedRegs, Pai(p^.next));
  87. Case P^.Typ Of
  88. ait_instruction:
  89. Begin
  90. { Handle Jmp Optimizations }
  91. if Pai386(p)^.is_jmp then
  92. begin
  93. {the following if-block removes all code between a jmp and the next label,
  94. because it can never be executed}
  95. If (pai386(p)^.opcode = A_JMP) Then
  96. Begin
  97. hp1 := pai(p^.next);
  98. While GetNextInstruction(p, hp1) and
  99. ((hp1^.typ <> ait_label) or
  100. { skip unused labels, they're not referenced anywhere }
  101. Not(Pai_Label(hp1)^.l^.is_used)) Do
  102. If (hp1^.typ <> ait_label) Then
  103. Begin
  104. AsmL^.Remove(hp1);
  105. Dispose(hp1, done);
  106. End;
  107. End;
  108. If GetNextInstruction(p, hp1) then
  109. Begin
  110. If (pai(hp1)^.typ=ait_instruction) and
  111. (pai386(hp1)^.opcode=A_JMP) and
  112. GetNextInstruction(hp1, hp2) And
  113. FindLabel(PAsmLabel(pai386(p)^.oper[0].sym), hp2)
  114. Then
  115. Begin
  116. if pai386(p)^.opcode=A_Jcc then
  117. pai386(p)^.condition:=inverse_cond[pai386(p)^.condition]
  118. else
  119. begin
  120. If (LabDif <> 0) Then
  121. GetFinalDestination(pai386(p));
  122. p:=pai(p^.next);
  123. continue;
  124. end;
  125. Dec(pai_label(hp2)^.l^.refs);
  126. pai386(p)^.oper[0].sym:=pai386(hp1)^.oper[0].sym;
  127. Inc(pai386(p)^.oper[0].sym^.refs);
  128. asml^.remove(hp1);
  129. dispose(hp1,done);
  130. If (LabDif <> 0) Then
  131. GetFinalDestination(pai386(p));
  132. end
  133. else
  134. if FindLabel(pasmlabel(pai386(p)^.oper[0].sym), hp1) then
  135. Begin
  136. hp2:=pai(hp1^.next);
  137. asml^.remove(p);
  138. dispose(p,done);
  139. p:=hp2;
  140. continue;
  141. end
  142. Else
  143. If (LabDif <> 0) Then
  144. GetFinalDestination(pai386(p));
  145. end
  146. end
  147. else
  148. { All other optimizes }
  149. begin
  150. For l := 0 to 2 Do
  151. If (Pai386(p)^.oper[l].typ = top_ref) Then
  152. With Pai386(p)^.oper[l].ref^ Do
  153. Begin
  154. If (base = R_NO) And
  155. (index <> R_NO) And
  156. (scalefactor in [0,1])
  157. Then
  158. Begin
  159. base := index;
  160. index := R_NO
  161. End
  162. End;
  163. Case Pai386(p)^.opcode Of
  164. A_AND:
  165. Begin
  166. If (Pai386(p)^.oper[0].typ = top_const) And
  167. (Pai386(p)^.oper[1].typ = top_reg) And
  168. GetNextInstruction(p, hp1) And
  169. (Pai(hp1)^.typ = ait_instruction) And
  170. (Pai386(hp1)^.opcode = A_AND) And
  171. (Pai386(hp1)^.oper[0].typ = top_const) And
  172. (Pai386(hp1)^.oper[1].typ = top_reg) And
  173. (Pai386(hp1)^.oper[1].reg = Pai386(hp1)^.oper[1].reg)
  174. Then
  175. {change "and const1, reg; and const2, reg" to "and (const1 and const2), reg"}
  176. Begin
  177. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val And Pai386(hp1)^.oper[0].val);
  178. AsmL^.Remove(hp1);
  179. Dispose(hp1, Done)
  180. End
  181. Else
  182. {change "and x, reg; jxx" to "test x, reg", if reg is deallocated before the
  183. jump}
  184. If (Pai386(p)^.oper[1].typ = top_reg) And
  185. GetNextInstruction(p, hp1) And
  186. (hp1^.typ = ait_instruction) And
  187. (Pai386(hp1)^.is_jmp) and
  188. Not(Pai386(p)^.oper[1].reg in UsedRegs) Then
  189. Pai386(p)^.opcode := A_TEST;
  190. End;
  191. A_CMP:
  192. Begin
  193. If (Pai386(p)^.oper[0].typ = top_const) And
  194. (Pai386(p)^.oper[1].typ = top_reg) And
  195. (Pai386(p)^.oper[0].val = 0) Then
  196. {change "cmp $0, %reg" to "test %reg, %reg"}
  197. Begin
  198. Pai386(p)^.opcode := A_TEST;
  199. Pai386(p)^.loadreg(0,Pai386(p)^.oper[1].reg);
  200. End;
  201. End;
  202. A_FLD:
  203. Begin
  204. If (Pai386(p)^.oper[0].typ = top_ref) And
  205. GetNextInstruction(p, hp2) And
  206. (hp2^.typ = Ait_Instruction) And
  207. (Pai386(hp2)^.oper[0].typ = top_reg) And
  208. (Pai386(hp2)^.oper[1].typ = top_reg) And
  209. (Pai386(p)^.opsize in [S_FS, S_FL]) And
  210. (Pai386(hp2)^.oper[0].reg = R_ST) And
  211. (Pai386(hp2)^.oper[1].reg = R_ST1) Then
  212. If GetLastInstruction(p, hp1) And
  213. (hp1^.typ = Ait_Instruction) And
  214. ((Pai386(hp1)^.opcode = A_FLD) Or
  215. (Pai386(hp1)^.opcode = A_FST)) And
  216. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  217. (Pai386(hp1)^.oper[0].typ = top_ref) And
  218. RefsEqual(Pai386(p)^.oper[0].ref^, Pai386(hp1)^.oper[0].ref^) Then
  219. If ((Pai386(hp2)^.opcode = A_FMULP) Or
  220. (Pai386(hp2)^.opcode = A_FADDP)) Then
  221. { change to
  222. fld/fst mem1 (hp1) fld/fst mem1
  223. fld mem1 (p) fadd/
  224. faddp/ fmul st, st
  225. fmulp st, st1 (hp2) }
  226. Begin
  227. AsmL^.Remove(p);
  228. Dispose(p, Done);
  229. p := hp1;
  230. If (Pai386(hp2)^.opcode = A_FADDP) Then
  231. Pai386(hp2)^.opcode := A_FADD
  232. Else
  233. Pai386(hp2)^.opcode := A_FMUL;
  234. Pai386(hp2)^.oper[1].reg := R_ST;
  235. End
  236. Else
  237. { change to
  238. fld/fst mem1 (hp1) fld/fst mem1
  239. fld mem1 (p) fld st}
  240. Begin
  241. Pai386(p)^.changeopsize(S_FL);
  242. Pai386(p)^.loadreg(0,R_ST);
  243. End
  244. Else
  245. Begin
  246. Case Pai386(hp2)^.opcode Of
  247. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  248. { change to
  249. fld/fst mem1 (hp1) fld/fst mem1
  250. fld mem2 (p) fxxx mem2
  251. fxxxp st, st1 (hp2) }
  252. Begin
  253. Case Pai386(hp2)^.opcode Of
  254. A_FADDP: Pai386(p)^.opcode := A_FADD;
  255. A_FMULP: Pai386(p)^.opcode := A_FMUL;
  256. A_FSUBP: Pai386(p)^.opcode := A_FSUBR;
  257. A_FSUBRP: Pai386(p)^.opcode := A_FSUB;
  258. A_FDIVP: Pai386(p)^.opcode := A_FDIVR;
  259. A_FDIVRP: Pai386(p)^.opcode := A_FDIV;
  260. End;
  261. AsmL^.Remove(hp2);
  262. Dispose(hp2, Done)
  263. End
  264. End
  265. End
  266. End;
  267. A_FSTP,A_FISTP:
  268. Begin
  269. If (Pai386(p)^.oper[0].typ = top_ref) And
  270. GetNextInstruction(p, hp1) And
  271. (Pai(hp1)^.typ = ait_instruction) And
  272. (((Pai386(hp1)^.opcode = A_FLD) And
  273. (Pai386(p)^.opcode = A_FSTP)) Or
  274. ((Pai386(p)^.opcode = A_FISTP) And
  275. (Pai386(hp1)^.opcode = A_FILD))) And
  276. (Pai386(hp1)^.oper[0].typ = top_ref) And
  277. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  278. RefsEqual(Pai386(p)^.oper[0].ref^, Pai386(hp1)^.oper[0].ref^)
  279. Then
  280. Begin
  281. If GetNextInstruction(hp1, hp2) And
  282. (hp2^.typ = ait_instruction) And
  283. ((Pai386(hp2)^.opcode = A_LEAVE) Or
  284. (Pai386(hp2)^.opcode = A_RET)) And
  285. (Pai386(p)^.oper[0].ref^.Base = ProcInfo.FramePointer) And
  286. (Pai386(p)^.oper[0].ref^.Offset >= ProcInfo.RetOffset) And
  287. (Pai386(p)^.oper[0].ref^.Index = R_NO)
  288. Then
  289. Begin
  290. AsmL^.Remove(p);
  291. AsmL^.Remove(hp1);
  292. Dispose(p, Done);
  293. Dispose(hp1, Done);
  294. p := hp2;
  295. Continue
  296. End
  297. Else
  298. {fst can't store an extended value!}
  299. If (Pai386(p)^.opsize <> S_FX) And
  300. (Pai386(p)^.opsize <> S_IQ) Then
  301. Begin
  302. If (Pai386(p)^.opcode = A_FSTP) Then
  303. Pai386(p)^.opcode := A_FST
  304. Else Pai386(p)^.opcode := A_FIST;
  305. AsmL^.Remove(hp1);
  306. Dispose(hp1, done)
  307. End
  308. End;
  309. End;
  310. A_IMUL:
  311. {changes certain "imul const, %reg"'s to lea sequences}
  312. Begin
  313. If (Pai386(p)^.oper[0].typ = Top_Const) And
  314. (Pai386(p)^.oper[1].typ = Top_Reg) And
  315. (Pai386(p)^.opsize = S_L) Then
  316. If (Pai386(p)^.oper[0].val = 1) Then
  317. If (Pai386(p)^.oper[2].typ = Top_None) Then
  318. {remove "imul $1, reg"}
  319. Begin
  320. hp1 := Pai(p^.Next);
  321. AsmL^.Remove(p);
  322. Dispose(p, Done);
  323. p := hp1;
  324. Continue;
  325. End
  326. Else
  327. {change "imul $1, reg1, reg2" to "mov reg1, reg2"}
  328. Begin
  329. hp1 := New(Pai386, Op_Reg_Reg(A_MOV, S_L, Pai386(p)^.oper[1].reg,Pai386(p)^.oper[2].reg));
  330. hp1^.fileinfo := p^.fileinfo;
  331. InsertLLItem(AsmL, p^.previous, p^.next, hp1);
  332. Dispose(p, Done);
  333. p := hp1;
  334. End
  335. Else If
  336. ((Pai386(p)^.oper[2].typ = Top_Reg) or
  337. (Pai386(p)^.oper[2].typ = Top_None)) And
  338. (aktoptprocessor < ClassP6) And
  339. (Pai386(p)^.oper[0].val <= 12) And
  340. Not(CS_LittleSize in aktglobalswitches) And
  341. (Not(GetNextInstruction(p, hp1)) Or
  342. {GetNextInstruction(p, hp1) And}
  343. Not((Pai(hp1)^.typ = ait_instruction) And
  344. ((pai386(hp1)^.opcode=A_Jcc) and (pai386(hp1)^.condition in [C_O,C_NO]))))
  345. Then
  346. Begin
  347. New(TmpRef);
  348. Reset_reference(tmpref^);
  349. Case Pai386(p)^.oper[0].val Of
  350. 3: Begin
  351. {imul 3, reg1, reg2 to
  352. lea (reg1,reg1,2), reg2
  353. imul 3, reg1 to
  354. lea (reg1,reg1,2), reg1}
  355. TmpRef^.base := Pai386(p)^.oper[1].reg;
  356. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  357. TmpRef^.ScaleFactor := 2;
  358. If (Pai386(p)^.oper[2].typ = Top_None) Then
  359. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg))
  360. Else
  361. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  362. hp1^.fileinfo := p^.fileinfo;
  363. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  364. Dispose(p, Done);
  365. p := hp1;
  366. End;
  367. 5: Begin
  368. {imul 5, reg1, reg2 to
  369. lea (reg1,reg1,4), reg2
  370. imul 5, reg1 to
  371. lea (reg1,reg1,4), reg1}
  372. TmpRef^.base := Pai386(p)^.oper[1].reg;
  373. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  374. TmpRef^.ScaleFactor := 4;
  375. If (Pai386(p)^.oper[2].typ = Top_None) Then
  376. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg))
  377. Else
  378. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  379. hp1^.fileinfo:= p^.fileinfo;
  380. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  381. Dispose(p, Done);
  382. p := hp1;
  383. End;
  384. 6: Begin
  385. {imul 6, reg1, reg2 to
  386. lea (,reg1,2), reg2
  387. lea (reg2,reg1,4), reg2
  388. imul 6, reg1 to
  389. lea (reg1,reg1,2), reg1
  390. add reg1, reg1}
  391. If (aktoptprocessor <= Class386)
  392. Then
  393. Begin
  394. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  395. If (Pai386(p)^.oper[2].typ = Top_Reg)
  396. Then
  397. Begin
  398. TmpRef^.base := Pai386(p)^.oper[2].reg;
  399. TmpRef^.ScaleFactor := 4;
  400. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  401. End
  402. Else
  403. Begin
  404. Dispose(TmpRef);
  405. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  406. Pai386(p)^.oper[1].reg,Pai386(p)^.oper[1].reg));
  407. End;
  408. hp1^.fileinfo := p^.fileinfo;
  409. InsertLLItem(AsmL,p, p^.next, hp1);
  410. New(TmpRef);
  411. Reset_reference(tmpref^);
  412. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  413. TmpRef^.ScaleFactor := 2;
  414. If (Pai386(p)^.oper[2].typ = Top_Reg)
  415. Then
  416. Begin
  417. TmpRef^.base := R_NO;
  418. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  419. Pai386(p)^.oper[2].reg));
  420. End
  421. Else
  422. Begin
  423. TmpRef^.base := Pai386(p)^.oper[1].reg;
  424. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  425. End;
  426. hp1^.fileinfo := p^.fileinfo;
  427. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  428. Dispose(p, Done);
  429. p := Pai(hp1^.next);
  430. End
  431. Else Dispose(TmpRef);
  432. End;
  433. 9: Begin
  434. {imul 9, reg1, reg2 to
  435. lea (reg1,reg1,8), reg2
  436. imul 9, reg1 to
  437. lea (reg1,reg1,8), reg1}
  438. TmpRef^.base := Pai386(p)^.oper[1].reg;
  439. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  440. TmpRef^.ScaleFactor := 8;
  441. If (Pai386(p)^.oper[2].typ = Top_None) Then
  442. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg))
  443. Else
  444. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  445. hp1^.fileinfo := p^.fileinfo;
  446. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  447. Dispose(p, Done);
  448. p := hp1;
  449. End;
  450. 10: Begin
  451. {imul 10, reg1, reg2 to
  452. lea (reg1,reg1,4), reg2
  453. add reg2, reg2
  454. imul 10, reg1 to
  455. lea (reg1,reg1,4), reg1
  456. add reg1, reg1}
  457. If (aktoptprocessor <= Class386) Then
  458. Begin
  459. If (Pai386(p)^.oper[2].typ = Top_Reg) Then
  460. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  461. Pai386(p)^.oper[2].reg,Pai386(p)^.oper[2].reg))
  462. Else
  463. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  464. Pai386(p)^.oper[1].reg,Pai386(p)^.oper[1].reg));
  465. hp1^.fileinfo := p^.fileinfo;
  466. InsertLLItem(AsmL,p, p^.next, hp1);
  467. TmpRef^.base := Pai386(p)^.oper[1].reg;
  468. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  469. TmpRef^.ScaleFactor := 4;
  470. If (Pai386(p)^.oper[2].typ = Top_Reg)
  471. Then
  472. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg))
  473. Else
  474. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  475. hp1^.fileinfo := p^.fileinfo;
  476. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  477. Dispose(p, Done);
  478. p := Pai(hp1^.next);
  479. End
  480. Else Dispose(TmpRef);
  481. End;
  482. 12: Begin
  483. {imul 12, reg1, reg2 to
  484. lea (,reg1,4), reg2
  485. lea (,reg1,8) reg2
  486. imul 12, reg1 to
  487. lea (reg1,reg1,2), reg1
  488. lea (,reg1,4), reg1}
  489. If (aktoptprocessor <= Class386)
  490. Then
  491. Begin
  492. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  493. If (Pai386(p)^.oper[2].typ = Top_Reg) Then
  494. Begin
  495. TmpRef^.base := Pai386(p)^.oper[2].reg;
  496. TmpRef^.ScaleFactor := 8;
  497. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  498. End
  499. Else
  500. Begin
  501. TmpRef^.base := R_NO;
  502. TmpRef^.ScaleFactor := 4;
  503. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  504. End;
  505. hp1^.fileinfo := p^.fileinfo;
  506. InsertLLItem(AsmL,p, p^.next, hp1);
  507. New(TmpRef);
  508. Reset_reference(tmpref^);
  509. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  510. If (Pai386(p)^.oper[2].typ = Top_Reg) Then
  511. Begin
  512. TmpRef^.base := R_NO;
  513. TmpRef^.ScaleFactor := 4;
  514. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  515. End
  516. Else
  517. Begin
  518. TmpRef^.base := Pai386(p)^.oper[1].reg;
  519. TmpRef^.ScaleFactor := 2;
  520. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  521. End;
  522. hp1^.fileinfo := p^.fileinfo;
  523. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  524. Dispose(p, Done);
  525. p := Pai(hp1^.next);
  526. End
  527. Else Dispose(TmpRef);
  528. End
  529. Else Dispose(TmpRef);
  530. End;
  531. End;
  532. End;
  533. A_LEA:
  534. Begin
  535. {removes seg register prefixes from LEA operations, as they
  536. don't do anything}
  537. Pai386(p)^.oper[0].ref^.Segment := R_NO;
  538. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  539. If (Pai386(p)^.oper[0].ref^.Base In [R_EAX..R_EDI]) And
  540. (Pai386(p)^.oper[0].ref^.Index = R_NO) And
  541. (Pai386(p)^.oper[0].ref^.Offset = 0) And
  542. (Not(Assigned(Pai386(p)^.oper[0].ref^.Symbol))) Then
  543. If (Pai386(p)^.oper[0].ref^.Base <> Pai386(p)^.oper[1].reg)
  544. Then
  545. Begin
  546. hp1 := New(Pai386, op_reg_reg(A_MOV, S_L,Pai386(p)^.oper[0].ref^.Base,
  547. Pai386(p)^.oper[1].reg));
  548. hp1^.fileinfo := p^.fileinfo;
  549. InsertLLItem(AsmL,p^.previous,p^.next, hp1);
  550. Dispose(p, Done);
  551. p := hp1;
  552. Continue;
  553. End
  554. Else
  555. Begin
  556. hp1 := Pai(p^.Next);
  557. AsmL^.Remove(p);
  558. Dispose(p, Done);
  559. p := hp1;
  560. Continue;
  561. End;
  562. End;
  563. A_MOV:
  564. Begin
  565. TmpUsedRegs := UsedRegs;
  566. If (Pai386(p)^.oper[1].typ = top_reg) And
  567. (Pai386(p)^.oper[1].reg In [R_EAX, R_EBX, R_EDX, R_EDI]) And
  568. GetNextInstruction(p, hp1) And
  569. (Pai(hp1)^.typ = ait_instruction) And
  570. (Pai386(hp1)^.opcode = A_MOV) And
  571. (Pai386(hp1)^.oper[0].typ = top_reg) And
  572. (Pai386(hp1)^.oper[0].reg = Pai386(p)^.oper[1].reg)
  573. Then
  574. {we have "mov x, %treg; mov %treg, y}
  575. If not(RegUsedAfterInstruction(Pai386(p)^.oper[1].reg, hp1, TmpUsedRegs)) then
  576. {we've got "mov x, %treg; mov %treg, y; with %treg is not used after }
  577. Case Pai386(p)^.oper[0].typ Of
  578. top_reg:
  579. Begin
  580. { change "mov %reg, %treg; mov %treg, y"
  581. to "mov %reg, y" }
  582. Pai386(hp1)^.LoadOper(0,Pai386(p)^.oper[0]);
  583. AsmL^.Remove(p);
  584. Dispose(p, Done);
  585. p := hp1;
  586. continue;
  587. End;
  588. top_ref:
  589. If (Pai386(hp1)^.oper[1].typ = top_reg) Then
  590. Begin
  591. { change "mov mem, %treg; mov %treg, %reg"
  592. to "mov mem, %reg" }
  593. Pai386(p)^.Loadoper(1,Pai386(hp1)^.oper[1]);
  594. AsmL^.Remove(hp1);
  595. Dispose(hp1, Done);
  596. continue;
  597. End;
  598. End
  599. Else
  600. {remove an instruction which never makes sense: we've got
  601. "mov mem, %reg1; mov %reg1, %edi" and then EDI isn't used anymore!}
  602. { Begin
  603. If (Pai386(hp1)^.oper[1].reg = R_EDI) And
  604. Not(GetNextInstruction(hp1, hp2) And
  605. (Pai(hp2)^.typ = ait_instruction) And
  606. (Pai386(hp2)^.oper[1].typ = top_reg) And
  607. (Pai386(hp2)^.oper[1] = Pointer(R_ESI))) Then
  608. Begin
  609. AsmL^.Remove(hp1);
  610. Dispose(hp1, Done);
  611. Continue;
  612. End
  613. End}
  614. Else
  615. {Change "mov %reg1, %reg2; xxx %reg2, ???" to
  616. "mov %reg1, %reg2; xxx %reg1, ???" to avoid a write/read
  617. penalty}
  618. If (Pai386(p)^.oper[0].typ = top_reg) And
  619. (Pai386(p)^.oper[1].typ = top_reg) And
  620. GetNextInstruction(p,hp1) And
  621. (Pai(hp1)^.typ = ait_instruction) And
  622. (Pai386(hp1)^.oper[0].typ = top_reg) And
  623. (Pai386(hp1)^.oper[0].reg = Pai386(p)^.oper[1].reg)
  624. Then
  625. {we have "mov %reg1, %reg2; XXX %reg2, ???"}
  626. Begin
  627. If ((Pai386(hp1)^.opcode = A_OR) Or
  628. (Pai386(hp1)^.opcode = A_TEST)) And
  629. (Pai386(hp1)^.oper[1].typ = top_reg) And
  630. (Pai386(hp1)^.oper[0].reg = Pai386(hp1)^.oper[1].reg)
  631. Then
  632. {we have "mov %reg1, %reg2; test/or %reg2, %reg2"}
  633. Begin
  634. TmpUsedRegs := UsedRegs;
  635. If GetNextInstruction(hp1, hp2) And
  636. (hp2^.typ = ait_instruction) And
  637. pai386(hp2)^.is_jmp and
  638. Not(RegUsedAfterInstruction(Pai386(hp1)^.oper[0].reg, hp1, TmpUsedRegs))
  639. Then
  640. {change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  641. "test %reg1, %reg1; jxx"}
  642. Begin
  643. Pai386(hp1)^.Loadoper(0,Pai386(p)^.oper[0]);
  644. Pai386(hp1)^.Loadoper(1,Pai386(p)^.oper[0]);
  645. AsmL^.Remove(p);
  646. Dispose(p, done);
  647. p := hp1;
  648. continue
  649. End
  650. Else
  651. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  652. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  653. Begin
  654. Pai386(hp1)^.Loadoper(0,Pai386(p)^.oper[0]);
  655. Pai386(hp1)^.Loadoper(1,Pai386(p)^.oper[0]);
  656. End;
  657. End
  658. { Else
  659. If (Pai386(p^.next)^.opcode
  660. In [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  661. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  662. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  663. End
  664. Else
  665. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  666. x >= RetOffset) as it doesn't do anything (it writes either to a
  667. parameter or to the temporary storage room for the function
  668. result)}
  669. If GetNextInstruction(p, hp1) And
  670. (Pai(hp1)^.typ = ait_instruction)
  671. Then
  672. If ((Pai386(hp1)^.opcode = A_LEAVE) Or
  673. (Pai386(hp1)^.opcode = A_RET)) And
  674. (Pai386(p)^.oper[1].typ = top_ref) And
  675. (Pai386(p)^.oper[1].ref^.base = ProcInfo.FramePointer) And
  676. (Pai386(p)^.oper[1].ref^.offset >= ProcInfo.RetOffset) And
  677. (Pai386(p)^.oper[1].ref^.index = R_NO) And
  678. (Pai386(p)^.oper[0].typ = top_reg)
  679. Then
  680. Begin
  681. AsmL^.Remove(p);
  682. Dispose(p, done);
  683. p := hp1;
  684. End
  685. Else
  686. If (Pai386(p)^.oper[0].typ = top_reg) And
  687. (Pai386(p)^.oper[1].typ = top_ref) And
  688. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) And
  689. (Pai386(hp1)^.opcode = A_CMP) And
  690. (Pai386(hp1)^.oper[1].typ = top_ref) And
  691. RefsEqual(Pai386(p)^.oper[1].ref^, Pai386(hp1)^.oper[1].ref^)
  692. Then
  693. {change "mov reg, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  694. Pai386(hp1)^.loadreg(1,Pai386(p)^.oper[0].reg);
  695. { Next instruction is also a MOV ? }
  696. If GetNextInstruction(p, hp1) And
  697. (pai(hp1)^.typ = ait_instruction) and
  698. (Pai386(hp1)^.opcode = A_MOV) and
  699. (Pai386(hp1)^.opsize = Pai386(p)^.opsize)
  700. Then
  701. Begin
  702. If (Pai386(hp1)^.oper[0].typ = Pai386(p)^.oper[1].typ) and
  703. (Pai386(hp1)^.oper[1].typ = Pai386(p)^.oper[0].typ)
  704. Then
  705. {mov reg1, mem1 or mov mem1, reg1
  706. mov mem2, reg2 mov reg2, mem2}
  707. Begin
  708. If OpsEqual(Pai386(hp1)^.oper[1],Pai386(p)^.oper[0]) Then
  709. {mov reg1, mem1 or mov mem1, reg1
  710. mov mem2, reg1 mov reg2, mem1}
  711. Begin
  712. If OpsEqual(Pai386(hp1)^.oper[0],Pai386(p)^.oper[1]) Then
  713. { Removes the second statement from
  714. mov reg1, mem1
  715. mov mem1, reg1 }
  716. Begin
  717. AsmL^.remove(hp1);
  718. Dispose(hp1,done);
  719. End
  720. Else
  721. Begin
  722. TmpUsedRegs := UsedRegs;
  723. UpdateUsedRegs(TmpUsedRegs, Pai(hp1^.next));
  724. If (Pai386(p)^.oper[0].typ = top_reg) And
  725. { mov reg1, mem1
  726. mov mem2, reg1 }
  727. GetNextInstruction(hp1, hp2) And
  728. (hp2^.typ = ait_instruction) And
  729. (Pai386(hp2)^.opcode = A_CMP) And
  730. (Pai386(hp2)^.opsize = Pai386(p)^.opsize) and
  731. (Pai386(hp2)^.oper[0].typ = TOp_Ref) And
  732. (Pai386(hp2)^.oper[1].typ = TOp_Reg) And
  733. RefsEqual(Pai386(hp2)^.oper[0].ref^, Pai386(p)^.oper[1].ref^) And
  734. (Pai386(hp2)^.oper[1].reg = Pai386(p)^.oper[0].reg) And
  735. Not(RegUsedAfterInstruction(Pai386(p)^.oper[0].reg, hp2, TmpUsedRegs)) Then
  736. { change to
  737. mov reg1, mem1 mov reg1, mem1
  738. mov mem2, reg1 cmp reg1, mem2
  739. cmp mem1, reg1 }
  740. Begin
  741. AsmL^.Remove(hp2);
  742. Dispose(hp2, Done);
  743. Pai386(hp1)^.opcode := A_CMP;
  744. Pai386(hp1)^.loadref(1,newreference(Pai386(hp1)^.oper[0].ref^));
  745. Pai386(hp1)^.loadreg(0,Pai386(p)^.oper[0].reg);
  746. End;
  747. End;
  748. End
  749. Else
  750. Begin
  751. If GetNextInstruction(hp1, hp2) And
  752. (Pai386(p)^.oper[0].typ = top_ref) And
  753. (Pai386(p)^.oper[1].typ = top_reg) And
  754. (Pai386(hp1)^.oper[0].typ = top_reg) And
  755. (Pai386(hp1)^.oper[0].reg = Pai386(p)^.oper[1].reg) And
  756. (Pai386(hp1)^.oper[1].typ = top_ref) And
  757. (Pai(hp2)^.typ = ait_instruction) And
  758. (Pai386(hp2)^.opcode = A_MOV) And
  759. (Pai386(hp2)^.opsize = Pai386(p)^.opsize) and
  760. (Pai386(hp2)^.oper[1].typ = top_reg) And
  761. (Pai386(hp2)^.oper[0].typ = top_ref) And
  762. RefsEqual(Pai386(hp2)^.oper[0].ref^, Pai386(hp1)^.oper[1].ref^)
  763. Then
  764. If (Pai386(p)^.oper[1].reg in [R_DI,R_EDI])
  765. Then
  766. { mov mem1, %edi
  767. mov %edi, mem2
  768. mov mem2, reg2
  769. to:
  770. mov mem1, reg2
  771. mov reg2, mem2}
  772. Begin
  773. Pai386(p)^.Loadoper(1,Pai386(hp2)^.oper[1]);
  774. Pai386(hp1)^.loadoper(0,Pai386(hp2)^.oper[1]);
  775. AsmL^.Remove(hp2);
  776. Dispose(hp2,Done);
  777. End
  778. Else
  779. If (Pai386(p)^.oper[1].reg <> Pai386(hp2)^.oper[1].reg) And
  780. not(RegInRef(Pai386(p)^.oper[1].reg,Pai386(p)^.oper[0].ref^)) And
  781. not(RegInRef(Pai386(hp2)^.oper[1].reg,Pai386(hp2)^.oper[0].ref^))
  782. Then
  783. { mov mem1, reg1 mov mem1, reg1
  784. mov reg1, mem2 mov reg1, mem2
  785. mov mem2, reg2 mov mem2, reg1
  786. to: to:
  787. mov mem1, reg1 mov mem1, reg1
  788. mov mem1, reg2 mov reg1, mem2
  789. mov reg1, mem2
  790. or (if mem1 depends on reg1
  791. and/or if mem2 depends on reg2)
  792. to:
  793. mov mem1, reg1
  794. mov reg1, mem2
  795. mov reg1, reg2
  796. }
  797. Begin
  798. Pai386(hp1)^.LoadRef(0,newreference(Pai386(p)^.oper[0].ref^));
  799. Pai386(hp1)^.LoadReg(1,Pai386(hp2)^.oper[1].reg);
  800. Pai386(hp2)^.LoadRef(1,newreference(Pai386(hp2)^.oper[0].ref^));
  801. Pai386(hp2)^.LoadReg(0,Pai386(p)^.oper[1].reg);
  802. End
  803. Else
  804. If (Pai386(hp1)^.Oper[0].reg <> Pai386(hp2)^.Oper[1].reg) Then
  805. Pai386(hp2)^.LoadReg(0,Pai386(hp1)^.Oper[0].reg)
  806. Else
  807. Begin
  808. AsmL^.Remove(hp2);
  809. Dispose(hp2, Done);
  810. End
  811. End;
  812. End
  813. Else
  814. (* {movl [mem1],reg1
  815. movl [mem1],reg2
  816. to:
  817. movl [mem1],reg1
  818. movl reg1,reg2 }
  819. If (Pai386(p)^.oper[0].typ = top_ref) and
  820. (Pai386(p)^.oper[1].typ = top_reg) and
  821. (Pai386(hp1)^.oper[0].typ = top_ref) and
  822. (Pai386(hp1)^.oper[1].typ = top_reg) and
  823. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) and
  824. RefsEqual(TReference(Pai386(p)^.oper[0]^),Pai386(hp1)^.oper[0]^.ref^) and
  825. (Pai386(p)^.oper[1].reg<>Pai386(hp1)^.oper[0]^.ref^.base) and
  826. (Pai386(p)^.oper[1].reg<>Pai386(hp1)^.oper[0]^.ref^.index) then
  827. Pai386(hp1)^.LoadReg(0,Pai386(p)^.oper[1].reg)
  828. Else*)
  829. { movl const1,[mem1]
  830. movl [mem1],reg1
  831. to:
  832. movl const1,reg1
  833. movl reg1,[mem1] }
  834. If (Pai386(p)^.oper[0].typ = top_const) and
  835. (Pai386(p)^.oper[1].typ = top_ref) and
  836. (Pai386(hp1)^.oper[0].typ = top_ref) and
  837. (Pai386(hp1)^.oper[1].typ = top_reg) and
  838. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) and
  839. RefsEqual(Pai386(hp1)^.oper[0].ref^,Pai386(p)^.oper[1].ref^) then
  840. Begin
  841. Pai386(hp1)^.LoadReg(0,Pai386(hp1)^.oper[1].reg);
  842. Pai386(hp1)^.LoadRef(1,newreference(Pai386(p)^.oper[1].ref^));
  843. Pai386(p)^.LoadReg(1,Pai386(hp1)^.oper[0].reg);
  844. End
  845. End;
  846. {changes "mov $0, %reg" into "xor %reg, %reg"}
  847. If (Pai386(p)^.oper[0].typ = Top_Const) And
  848. (Pai386(p)^.oper[0].val = 0) And
  849. (Pai386(p)^.oper[1].typ = Top_Reg)
  850. Then
  851. Begin
  852. Pai386(p)^.opcode := A_XOR;
  853. Pai386(p)^.LoadReg(0,Pai386(p)^.oper[1].reg);
  854. End;
  855. End;
  856. A_MOVZX:
  857. Begin
  858. {removes superfluous And's after movzx's}
  859. If (Pai386(p)^.oper[1].typ = top_reg) And
  860. GetNextInstruction(p, hp1) And
  861. (Pai(hp1)^.typ = ait_instruction) And
  862. (Pai386(hp1)^.opcode = A_AND) And
  863. (Pai386(hp1)^.oper[0].typ = top_const) And
  864. (Pai386(hp1)^.oper[1].typ = top_reg) And
  865. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg)
  866. Then
  867. Case Pai386(p)^.opsize Of
  868. S_BL, S_BW:
  869. If (Pai386(hp1)^.oper[0].val = $ff) Then
  870. Begin
  871. AsmL^.Remove(hp1);
  872. Dispose(hp1, Done);
  873. End;
  874. S_WL:
  875. If (Pai386(hp1)^.oper[0].val = $ffff) Then
  876. Begin
  877. AsmL^.Remove(hp1);
  878. Dispose(hp1, Done);
  879. End;
  880. End;
  881. {changes some movzx constructs to faster synonims (all examples
  882. are given with eax/ax, but are also valid for other registers)}
  883. If (Pai386(p)^.oper[1].typ = top_reg) Then
  884. If (Pai386(p)^.oper[0].typ = top_reg) Then
  885. Case Pai386(p)^.opsize of
  886. S_BW:
  887. Begin
  888. If (Pai386(p)^.oper[0].reg = Reg16ToReg8(Pai386(p)^.oper[1].reg)) And
  889. Not(CS_LittleSize In aktglobalswitches)
  890. Then
  891. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  892. Begin
  893. Pai386(p)^.opcode := A_AND;
  894. Pai386(p)^.changeopsize(S_W);
  895. Pai386(p)^.LoadConst(0,$ff);
  896. End
  897. Else
  898. If GetNextInstruction(p, hp1) And
  899. (Pai(hp1)^.typ = ait_instruction) And
  900. (Pai386(hp1)^.opcode = A_AND) And
  901. (Pai386(hp1)^.oper[0].typ = top_const) And
  902. (Pai386(hp1)^.oper[1].typ = top_reg) And
  903. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg)
  904. Then
  905. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  906. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  907. Begin
  908. Pai386(p)^.opcode := A_MOV;
  909. Pai386(p)^.changeopsize(S_W);
  910. Pai386(p)^.LoadReg(0,Reg8ToReg16(Pai386(p)^.oper[0].reg));
  911. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ff);
  912. End;
  913. End;
  914. S_BL:
  915. Begin
  916. If (Pai386(p)^.oper[0].reg = Reg32ToReg8(Pai386(p)^.oper[1].reg)) And
  917. Not(CS_LittleSize in aktglobalswitches)
  918. Then
  919. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  920. Begin
  921. Pai386(p)^.opcode := A_AND;
  922. Pai386(p)^.changeopsize(S_L);
  923. Pai386(p)^.loadconst(0,$ff)
  924. End
  925. Else
  926. If GetNextInstruction(p, hp1) And
  927. (Pai(hp1)^.typ = ait_instruction) And
  928. (Pai386(hp1)^.opcode = A_AND) And
  929. (Pai386(hp1)^.oper[0].typ = top_const) And
  930. (Pai386(hp1)^.oper[1].typ = top_reg) And
  931. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg)
  932. Then
  933. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  934. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  935. Begin
  936. Pai386(p)^.opcode := A_MOV;
  937. Pai386(p)^.changeopsize(S_L);
  938. Pai386(p)^.LoadReg(0,Reg8ToReg32(Pai386(p)^.oper[0].reg));
  939. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ff);
  940. End
  941. End;
  942. S_WL:
  943. Begin
  944. If (Pai386(p)^.oper[0].reg = Reg32ToReg16(Pai386(p)^.oper[1].reg)) And
  945. Not(CS_LittleSize In aktglobalswitches)
  946. Then
  947. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  948. Begin
  949. Pai386(p)^.opcode := A_AND;
  950. Pai386(p)^.changeopsize(S_L);
  951. Pai386(p)^.LoadConst(0,$ffff);
  952. End
  953. Else
  954. If GetNextInstruction(p, hp1) And
  955. (Pai(hp1)^.typ = ait_instruction) And
  956. (Pai386(hp1)^.opcode = A_AND) And
  957. (Pai386(hp1)^.oper[0].typ = top_const) And
  958. (Pai386(hp1)^.oper[1].typ = top_reg) And
  959. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg)
  960. Then
  961. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  962. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  963. Begin
  964. Pai386(p)^.opcode := A_MOV;
  965. Pai386(p)^.changeopsize(S_L);
  966. Pai386(p)^.LoadReg(0,Reg16ToReg32(Pai386(p)^.oper[0].reg));
  967. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ffff);
  968. End;
  969. End;
  970. End
  971. Else
  972. If (Pai386(p)^.oper[0].typ = top_ref) Then
  973. Begin
  974. If GetNextInstruction(p, hp1) And
  975. (Pai(hp1)^.typ = ait_instruction) And
  976. (Pai386(hp1)^.opcode = A_AND) And
  977. (Pai386(hp1)^.oper[0].typ = Top_Const) And
  978. (Pai386(hp1)^.oper[1].typ = Top_Reg) And
  979. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Then
  980. Begin
  981. Pai386(p)^.opcode := A_MOV;
  982. Case Pai386(p)^.opsize Of
  983. S_BL:
  984. Begin
  985. Pai386(p)^.changeopsize(S_L);
  986. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ff);
  987. End;
  988. S_WL:
  989. Begin
  990. Pai386(p)^.changeopsize(S_L);
  991. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ffff);
  992. End;
  993. S_BW:
  994. Begin
  995. Pai386(p)^.changeopsize(S_W);
  996. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ff);
  997. End;
  998. End;
  999. End;
  1000. End;
  1001. End;
  1002. A_POP:
  1003. Begin
  1004. if (Pai386(p)^.oper[0].typ = top_reg) And
  1005. GetNextInstruction(p, hp1) And
  1006. (pai(hp1)^.typ=ait_instruction) and
  1007. (Pai386(hp1)^.opcode=A_PUSH) and
  1008. (Pai386(hp1)^.oper[0].typ = top_reg) And
  1009. (Pai386(hp1)^.oper[0].reg=Pai386(p)^.oper[0].reg) then
  1010. { This can't be done, because the register which is popped
  1011. can still be used after the push (PFV)
  1012. If (Not(cs_regalloc in aktglobalswitches)) Then
  1013. Begin
  1014. hp2:=pai(hp1^.next);
  1015. asml^.remove(p);
  1016. asml^.remove(hp1);
  1017. dispose(p,done);
  1018. dispose(hp1,done);
  1019. p:=hp2;
  1020. continue
  1021. End
  1022. Else }
  1023. Begin
  1024. { change it to a two op operation }
  1025. Pai386(p)^.oper[1].typ:=top_none;
  1026. Pai386(p)^.ops:=2;
  1027. Pai386(p)^.opcode := A_MOV;
  1028. Pai386(p)^.Loadoper(1,Pai386(p)^.oper[0]);
  1029. New(TmpRef);
  1030. Reset_reference(tmpref^);
  1031. TmpRef^.base := R_ESP;
  1032. Pai386(p)^.LoadRef(0,TmpRef);
  1033. hp1 := Pai(p^.next);
  1034. AsmL^.Remove(hp1);
  1035. Dispose(hp1, Done)
  1036. End;
  1037. end;
  1038. A_PUSH:
  1039. Begin
  1040. If (Pai386(p)^.opsize = S_W) And
  1041. (Pai386(p)^.oper[0].typ = Top_Const) And
  1042. GetNextInstruction(p, hp1) And
  1043. (Pai(hp1)^.typ = ait_instruction) And
  1044. (Pai386(hp1)^.opcode = A_PUSH) And
  1045. (Pai386(hp1)^.oper[0].typ = Top_Const) And
  1046. (Pai386(hp1)^.opsize = S_W) Then
  1047. Begin
  1048. Pai386(p)^.changeopsize(S_L);
  1049. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val shl 16 + Pai386(hp1)^.oper[0].val);
  1050. AsmL^.Remove(hp1);
  1051. Dispose(hp1, Done)
  1052. End;
  1053. End;
  1054. A_SHL, A_SAL:
  1055. Begin
  1056. If (Pai386(p)^.oper[0].typ = Top_Const) And
  1057. (Pai386(p)^.oper[1].typ = Top_Reg) And
  1058. (Pai386(p)^.opsize = S_L) And
  1059. (Pai386(p)^.oper[0].val <= 3)
  1060. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1061. Then
  1062. Begin
  1063. TmpBool1 := True; {should we check the next instruction?}
  1064. TmpBool2 := False; {have we found an add/sub which could be
  1065. integrated in the lea?}
  1066. New(TmpRef);
  1067. Reset_reference(tmpref^);
  1068. TmpRef^.index := Pai386(p)^.oper[1].reg;
  1069. TmpRef^.scalefactor := 1 shl Pai386(p)^.oper[0].val;
  1070. While TmpBool1 And
  1071. GetNextInstruction(p, hp1) And
  1072. (Pai(hp1)^.typ = ait_instruction) And
  1073. ((Pai386(hp1)^.opcode = A_ADD) Or
  1074. (Pai386(hp1)^.opcode = A_SUB)) And
  1075. (Pai386(hp1)^.oper[1].typ = Top_Reg) And
  1076. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Do
  1077. Begin
  1078. TmpBool1 := False;
  1079. If (Pai386(hp1)^.oper[0].typ = Top_Const)
  1080. Then
  1081. Begin
  1082. TmpBool1 := True;
  1083. TmpBool2 := True;
  1084. If Pai386(hp1)^.opcode = A_ADD Then
  1085. Inc(TmpRef^.offset, Pai386(hp1)^.oper[0].val)
  1086. Else
  1087. Dec(TmpRef^.offset, Pai386(hp1)^.oper[0].val);
  1088. AsmL^.Remove(hp1);
  1089. Dispose(hp1, Done);
  1090. End
  1091. Else
  1092. If (Pai386(hp1)^.oper[0].typ = Top_Reg) And
  1093. (Pai386(hp1)^.opcode = A_ADD) And
  1094. (TmpRef^.base = R_NO) Then
  1095. Begin
  1096. TmpBool1 := True;
  1097. TmpBool2 := True;
  1098. TmpRef^.base := Pai386(hp1)^.oper[0].reg;
  1099. AsmL^.Remove(hp1);
  1100. Dispose(hp1, Done);
  1101. End;
  1102. End;
  1103. If TmpBool2 Or
  1104. ((aktoptprocessor < ClassP6) And
  1105. (Pai386(p)^.oper[0].val <= 3) And
  1106. Not(CS_LittleSize in aktglobalswitches))
  1107. Then
  1108. Begin
  1109. If Not(TmpBool2) And
  1110. (Pai386(p)^.oper[0].val = 1)
  1111. Then
  1112. Begin
  1113. Dispose(TmpRef);
  1114. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.opsize,
  1115. Pai386(p)^.oper[1].reg, Pai386(p)^.oper[1].reg))
  1116. End
  1117. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  1118. Pai386(p)^.oper[1].reg));
  1119. hp1^.fileinfo := p^.fileinfo;
  1120. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1121. Dispose(p, Done);
  1122. p := hp1;
  1123. End;
  1124. End
  1125. Else
  1126. If (aktoptprocessor < ClassP6) And
  1127. (Pai386(p)^.oper[0].typ = top_const) And
  1128. (Pai386(p)^.oper[1].typ = top_reg) Then
  1129. If (Pai386(p)^.oper[0].val = 1)
  1130. Then
  1131. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1132. but faster on a 486, and pairable in both U and V pipes on the Pentium
  1133. (unlike shl, which is only pairable in the U pipe)}
  1134. Begin
  1135. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.opsize,
  1136. Pai386(p)^.oper[1].reg, Pai386(p)^.oper[1].reg));
  1137. hp1^.fileinfo := p^.fileinfo;
  1138. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1139. Dispose(p, done);
  1140. p := hp1;
  1141. End
  1142. Else If (Pai386(p)^.opsize = S_L) and
  1143. (Pai386(p)^.oper[0].val<= 3) Then
  1144. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1145. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1146. Begin
  1147. New(TmpRef);
  1148. Reset_reference(tmpref^);
  1149. TmpRef^.index := Pai386(p)^.oper[1].reg;
  1150. TmpRef^.scalefactor := 1 shl Pai386(p)^.oper[0].val;
  1151. hp1 := new(Pai386,op_ref_reg(A_LEA,S_L,TmpRef, Pai386(p)^.oper[1].reg));
  1152. hp1^.fileinfo := p^.fileinfo;
  1153. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1154. Dispose(p, done);
  1155. p := hp1;
  1156. End
  1157. End;
  1158. A_SAR, A_SHR:
  1159. {changes the code sequence
  1160. shr/sar const1, x
  1161. shl const2, x
  1162. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  1163. Begin
  1164. If GetNextInstruction(p, hp1) And
  1165. (pai(hp1)^.typ = ait_instruction) and
  1166. (Pai386(hp1)^.opcode = A_SHL) and
  1167. (Pai386(p)^.oper[0].typ = top_const) and
  1168. (Pai386(hp1)^.oper[0].typ = top_const) and
  1169. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  1170. (Pai386(hp1)^.oper[1].typ = Pai386(p)^.oper[1].typ) And
  1171. OpsEqual(Pai386(hp1)^.oper[1], Pai386(p)^.oper[1])
  1172. Then
  1173. If (Pai386(p)^.oper[0].val > Pai386(hp1)^.oper[0].val) And
  1174. Not(CS_LittleSize In aktglobalswitches)
  1175. Then
  1176. { shr/sar const1, %reg
  1177. shl const2, %reg
  1178. with const1 > const2 }
  1179. Begin
  1180. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val-Pai386(hp1)^.oper[0].val);
  1181. Pai386(hp1)^.opcode := A_AND;
  1182. l := 1 shl (Pai386(hp1)^.oper[0].val-1);
  1183. Case Pai386(p)^.opsize Of
  1184. S_L: Pai386(hp1)^.LoadConst(0,l Xor longint(-1));
  1185. S_B: Pai386(hp1)^.LoadConst(0,l Xor $ff);
  1186. S_W: Pai386(hp1)^.LoadConst(0,l Xor $ffff);
  1187. End;
  1188. End
  1189. Else
  1190. If (Pai386(p)^.oper[0].val<Pai386(hp1)^.oper[0].val) And
  1191. Not(CS_LittleSize In aktglobalswitches)
  1192. Then
  1193. { shr/sar const1, %reg
  1194. shl const2, %reg
  1195. with const1 < const2 }
  1196. Begin
  1197. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val-Pai386(p)^.oper[0].val);
  1198. Pai386(p)^.opcode := A_AND;
  1199. l := 1 shl (Pai386(p)^.oper[0].val-1);
  1200. Case Pai386(p)^.opsize Of
  1201. S_L: Pai386(p)^.LoadConst(0,l Xor $ffffffff);
  1202. S_B: Pai386(p)^.LoadConst(0,l Xor $ff);
  1203. S_W: Pai386(p)^.LoadConst(0,l Xor $ffff);
  1204. End;
  1205. End
  1206. Else
  1207. { shr/sar const1, %reg
  1208. shl const2, %reg
  1209. with const1 = const2 }
  1210. Begin
  1211. Pai386(p)^.opcode := A_AND;
  1212. l := 1 shl (Pai386(p)^.oper[0].val-1);
  1213. Case Pai386(p)^.opsize Of
  1214. S_B: Pai386(p)^.LoadConst(0,l Xor $ff);
  1215. S_W: Pai386(p)^.LoadConst(0,l Xor $ffff);
  1216. S_L: Pai386(p)^.LoadConst(0,l Xor $ffffffff);
  1217. End;
  1218. AsmL^.remove(hp1);
  1219. dispose(hp1, done);
  1220. End;
  1221. End;
  1222. A_SETcc :
  1223. Begin
  1224. If (Pai386(p)^.oper[0].typ = top_ref) And
  1225. GetNextInstruction(p, hp1) And
  1226. GetNextInstruction(hp1, hp2) And
  1227. (hp2^.typ = ait_instruction) And
  1228. ((Pai386(hp2)^.opcode = A_LEAVE) or
  1229. (Pai386(hp2)^.opcode = A_RET)) And
  1230. (Pai386(p)^.oper[0].ref^.Base = ProcInfo.FramePointer) And
  1231. (Pai386(p)^.oper[0].ref^.Index = R_NO) And
  1232. (Pai386(p)^.oper[0].ref^.Offset >= ProcInfo.RetOffset) And
  1233. (hp1^.typ = ait_instruction) And
  1234. (Pai386(hp1)^.opcode = A_MOV) And
  1235. (Pai386(hp1)^.opsize = S_B) And
  1236. (Pai386(hp1)^.oper[0].typ = top_ref) And
  1237. RefsEqual(Pai386(hp1)^.oper[0].ref^, Pai386(p)^.oper[0].ref^) Then
  1238. Begin
  1239. Pai386(p)^.LoadReg(0,Pai386(hp1)^.oper[1].reg);
  1240. AsmL^.Remove(hp1);
  1241. Dispose(hp1, Done)
  1242. End
  1243. End;
  1244. A_SUB:
  1245. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1246. { * change "sub/add const1, reg" or "dec reg" followed by
  1247. "sub const2, reg" to one "sub ..., reg" }
  1248. Begin
  1249. If (Pai386(p)^.oper[0].typ = top_const) And
  1250. (Pai386(p)^.oper[1].typ = top_reg) Then
  1251. If (Pai386(p)^.oper[0].val = 2) And
  1252. (Pai386(p)^.oper[1].reg = R_ESP) Then
  1253. Begin
  1254. hp1 := Pai(p^.next);
  1255. While Assigned(hp1) And
  1256. (Pai(hp1)^.typ In [ait_instruction]+SkipInstr) And
  1257. Not((Pai(hp1)^.typ = ait_instruction) And
  1258. ((Pai386(hp1)^.opcode = A_CALL) or
  1259. (Pai386(hp1)^.opcode = A_PUSH) or
  1260. ((Pai386(hp1)^.opcode = A_MOV) And
  1261. (Pai386(hp1)^.oper[1].typ = top_ref) And
  1262. (Pai386(hp1)^.oper[1].ref^.base = R_ESP)))) do
  1263. hp1 := Pai(hp1^.next);
  1264. If Assigned(hp1) And
  1265. (Pai(hp1)^.typ = ait_instruction) And
  1266. (Pai386(hp1)^.opcode = A_PUSH) And
  1267. (Pai386(hp1)^.opsize = S_W)
  1268. Then
  1269. Begin
  1270. Pai386(hp1)^.changeopsize(S_L);
  1271. if Pai386(hp1)^.oper[0].typ=top_reg then
  1272. Pai386(hp1)^.LoadReg(0,Reg16ToReg32(Pai386(hp1)^.oper[0].reg));
  1273. hp1 := Pai(p^.next);
  1274. AsmL^.Remove(p);
  1275. Dispose(p, Done);
  1276. p := hp1;
  1277. Continue
  1278. End
  1279. Else
  1280. If GetLastInstruction(p, hp1) And
  1281. (Pai(hp1)^.typ = ait_instruction) And
  1282. (Pai386(hp1)^.opcode = A_SUB) And
  1283. (Pai386(hp1)^.oper[0].typ = top_const) And
  1284. (Pai386(hp1)^.oper[1].typ = top_reg) And
  1285. (Pai386(hp1)^.oper[1].reg = R_ESP)
  1286. Then
  1287. Begin
  1288. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val+Pai386(hp1)^.oper[0].val);
  1289. AsmL^.Remove(hp1);
  1290. Dispose(hp1, Done);
  1291. End;
  1292. End
  1293. Else
  1294. If GetLastInstruction(p, hp1) And
  1295. (hp1^.typ = ait_instruction) And
  1296. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) then
  1297. Case Pai386(hp1)^.opcode Of
  1298. A_DEC:
  1299. If (Pai386(hp1)^.oper[0].typ = top_reg) And
  1300. (Pai386(hp1)^.oper[0].reg = Pai386(p)^.oper[1].reg) Then
  1301. Begin
  1302. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val+1);
  1303. AsmL^.Remove(hp1);
  1304. Dispose(hp1, Done)
  1305. End;
  1306. A_SUB:
  1307. If (Pai386(hp1)^.oper[0].typ = top_const) And
  1308. (Pai386(hp1)^.oper[1].typ = top_reg) And
  1309. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Then
  1310. Begin
  1311. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val+Pai386(hp1)^.oper[0].val);
  1312. AsmL^.Remove(hp1);
  1313. Dispose(hp1, Done)
  1314. End;
  1315. A_ADD:
  1316. If (Pai386(hp1)^.oper[0].typ = top_const) And
  1317. (Pai386(hp1)^.oper[1].typ = top_reg) And
  1318. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Then
  1319. Begin
  1320. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val-Pai386(hp1)^.oper[0].val);
  1321. AsmL^.Remove(hp1);
  1322. Dispose(hp1, Done)
  1323. End;
  1324. End
  1325. End;
  1326. A_TEST, A_OR:
  1327. {removes the line marked with (x) from the sequence
  1328. And/or/xor/add/sub/... $x, %y
  1329. test/or %y, %y (x)
  1330. j(n)z _Label
  1331. as the first instruction already adjusts the ZF}
  1332. Begin
  1333. If OpsEqual(Pai386(p)^.oper[0],Pai386(p)^.oper[1]) And
  1334. GetLastInstruction(p, hp1) And
  1335. (pai(hp1)^.typ = ait_instruction) Then
  1336. Case Pai386(hp1)^.opcode Of
  1337. A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_SHL, A_SHR:
  1338. Begin
  1339. If OpsEqual(Pai386(hp1)^.oper[1],Pai386(p)^.oper[0]) Then
  1340. Begin
  1341. hp1 := pai(p^.next);
  1342. asml^.remove(p);
  1343. dispose(p, done);
  1344. p := pai(hp1);
  1345. continue
  1346. End;
  1347. End;
  1348. A_DEC, A_INC, A_NEG:
  1349. Begin
  1350. If OpsEqual(Pai386(hp1)^.oper[0],Pai386(p)^.oper[0]) Then
  1351. Begin
  1352. Case Pai386(hp1)^.opcode Of
  1353. A_DEC, A_INC:
  1354. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  1355. Begin
  1356. Case Pai386(hp1)^.opcode Of
  1357. A_DEC: Pai386(hp1)^.opcode := A_SUB;
  1358. A_INC: Pai386(hp1)^.opcode := A_ADD;
  1359. End;
  1360. Pai386(hp1)^.Loadoper(1,Pai386(hp1)^.oper[0]);
  1361. Pai386(hp1)^.LoadConst(0,1);
  1362. Pai386(hp1)^.ops:=2;
  1363. End
  1364. End;
  1365. hp1 := pai(p^.next);
  1366. asml^.remove(p);
  1367. dispose(p, done);
  1368. p := pai(hp1);
  1369. continue
  1370. End;
  1371. End
  1372. End;
  1373. End;
  1374. End;
  1375. end; { if is_jmp }
  1376. End;
  1377. { ait_label:
  1378. Begin
  1379. If Not(Pai_Label(p)^.l^.is_used)
  1380. Then
  1381. Begin
  1382. hp1 := Pai(p^.next);
  1383. AsmL^.Remove(p);
  1384. Dispose(p, Done);
  1385. p := hp1;
  1386. Continue
  1387. End;
  1388. End;}
  1389. End;
  1390. p:=pai(p^.next);
  1391. end;
  1392. end;
  1393. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  1394. var
  1395. p,hp1,hp2: pai;
  1396. Begin
  1397. P := BlockStart;
  1398. While (P <> BlockEnd) Do
  1399. Begin
  1400. Case P^.Typ Of
  1401. Ait_Instruction:
  1402. Begin
  1403. Case Pai386(p)^.opcode Of
  1404. A_CALL:
  1405. If (AktOptProcessor < ClassP6) And
  1406. GetNextInstruction(p, hp1) And
  1407. (hp1^.typ = ait_instruction) And
  1408. (pai386(hp1)^.opcode = A_JMP) Then
  1409. Begin
  1410. Inc(pai386(hp1)^.oper[0].sym^.refs);
  1411. hp2 := New(Pai386,op_sym(A_PUSH,S_L,pai386(hp1)^.oper[0].sym));
  1412. hp2^.fileinfo := p^.fileinfo;
  1413. InsertLLItem(AsmL, p^.previous, p, hp2);
  1414. Pai386(p)^.opcode := A_JMP;
  1415. AsmL^.Remove(hp1);
  1416. Dispose(hp1, Done)
  1417. End;
  1418. A_MOV:
  1419. Begin
  1420. If (Pai386(p)^.oper[0].typ = top_reg) And
  1421. (Pai386(p)^.oper[1].typ = top_reg) And
  1422. GetNextInstruction(p, hp1) And
  1423. (hp1^.typ = ait_Instruction) And
  1424. ((Pai386(hp1)^.opcode = A_MOV) or
  1425. (Pai386(hp1)^.opcode = A_MOVZX) or
  1426. (Pai386(hp1)^.opcode = A_MOVSX)) And
  1427. (Pai386(hp1)^.oper[0].typ = top_ref) And
  1428. (Pai386(hp1)^.oper[1].typ = top_reg) And
  1429. ((Pai386(hp1)^.oper[0].ref^.Base = Pai386(p)^.oper[1].reg) Or
  1430. (Pai386(hp1)^.oper[0].ref^.Index = Pai386(p)^.oper[1].reg)) And
  1431. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Then
  1432. {mov reg1, reg2
  1433. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  1434. Begin
  1435. If (Pai386(hp1)^.oper[0].ref^.Base = Pai386(p)^.oper[1].reg) Then
  1436. Pai386(hp1)^.oper[0].ref^.Base := Pai386(p)^.oper[0].reg;
  1437. If (Pai386(hp1)^.oper[0].ref^.Index = Pai386(p)^.oper[1].reg) Then
  1438. Pai386(hp1)^.oper[0].ref^.Index := Pai386(p)^.oper[0].reg;
  1439. AsmL^.Remove(p);
  1440. Dispose(p, Done);
  1441. p := hp1;
  1442. Continue;
  1443. End;
  1444. End;
  1445. A_MOVZX:
  1446. Begin
  1447. If (Pai386(p)^.oper[1].typ = top_reg) Then
  1448. If (Pai386(p)^.oper[0].typ = top_reg)
  1449. Then
  1450. Case Pai386(p)^.opsize of
  1451. S_BL:
  1452. Begin
  1453. If IsGP32Reg(Pai386(p)^.oper[1].reg) And
  1454. Not(CS_LittleSize in aktglobalswitches) And
  1455. (aktoptprocessor = ClassP5)
  1456. Then
  1457. {Change "movzbl %reg1, %reg2" to
  1458. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1459. PentiumMMX}
  1460. Begin
  1461. hp1 := New(Pai386, op_reg_reg(A_XOR, S_L,
  1462. Pai386(p)^.oper[1].reg, Pai386(p)^.oper[1].reg));
  1463. hp1^.fileinfo := p^.fileinfo;
  1464. InsertLLItem(AsmL,p^.previous, p, hp1);
  1465. Pai386(p)^.opcode := A_MOV;
  1466. Pai386(p)^.changeopsize(S_B);
  1467. Pai386(p)^.LoadReg(1,Reg32ToReg8(Pai386(p)^.oper[1].reg));
  1468. End;
  1469. End;
  1470. End
  1471. Else
  1472. If (Pai386(p)^.oper[0].typ = top_ref) And
  1473. (Pai386(p)^.oper[0].ref^.base <> Pai386(p)^.oper[1].reg) And
  1474. (Pai386(p)^.oper[0].ref^.index <> Pai386(p)^.oper[1].reg) And
  1475. Not(CS_LittleSize in aktglobalswitches) And
  1476. IsGP32Reg(Pai386(p)^.oper[1].reg) And
  1477. (aktoptprocessor = ClassP5) And
  1478. (Pai386(p)^.opsize = S_BL)
  1479. Then
  1480. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1481. Pentium and PentiumMMX}
  1482. Begin
  1483. hp1 := New(Pai386,op_reg_reg(A_XOR, S_L, Pai386(p)^.oper[1].reg,
  1484. Pai386(p)^.oper[1].reg));
  1485. hp1^.fileinfo := p^.fileinfo;
  1486. Pai386(p)^.opcode := A_MOV;
  1487. Pai386(p)^.changeopsize(S_B);
  1488. Pai386(p)^.LoadReg(1,Reg32ToReg8(Pai386(p)^.oper[1].reg));
  1489. InsertLLItem(AsmL,p^.previous, p, hp1);
  1490. End;
  1491. End;
  1492. End;
  1493. End;
  1494. End;
  1495. p := Pai(p^.next)
  1496. End;
  1497. End;
  1498. End.
  1499. {
  1500. $Log$
  1501. Revision 1.58 1999-07-30 18:17:55 jonas
  1502. * fix so (,reg) gets optimized to (reg)
  1503. Revision 1.57 1999/07/01 18:12:16 jonas
  1504. * enabled "mov reg1,reg2;mov (reg2,..), reg2" also if the second mov is
  1505. a movzx or movsx
  1506. Revision 1.56 1999/06/23 12:33:52 jonas
  1507. * merged
  1508. Revision 1.54.2.2 1999/06/23 11:55:08 jonas
  1509. * fixed bug in "mov mem1,reg1;mov reg1,mem2;mov mem2,reg2" optimization
  1510. Revision 1.55 1999/06/18 09:55:31 peter
  1511. * merged
  1512. Revision 1.54.2.1 1999/06/18 09:52:40 peter
  1513. * pop;push -> mov (esp),reg always instead of being removed
  1514. Revision 1.54 1999/05/27 19:44:49 peter
  1515. * removed oldasm
  1516. * plabel -> pasmlabel
  1517. * -a switches to source writing automaticly
  1518. * assembler readers OOPed
  1519. * asmsymbol automaticly external
  1520. * jumptables and other label fixes for asm readers
  1521. Revision 1.53 1999/05/12 00:19:52 peter
  1522. * removed R_DEFAULT_SEG
  1523. * uniform float names
  1524. Revision 1.52 1999/05/05 16:19:04 jonas
  1525. + remove the segment prefixes from LEA instructions
  1526. Revision 1.51 1999/05/05 10:05:54 florian
  1527. * a delphi compiled compiler recompiles ppc
  1528. Revision 1.50 1999/05/02 21:33:55 florian
  1529. * several bugs regarding -Or fixed
  1530. Revision 1.49 1999/05/02 14:26:31 peter
  1531. * fixed dec -> sub $1 opt which didn't set ops=2
  1532. Revision 1.48 1999/05/01 13:24:34 peter
  1533. * merged nasm compiler
  1534. * old asm moved to oldasm/
  1535. Revision 1.5 1999/04/30 12:36:50 jonas
  1536. * fix from Brussels: call/jmp => push/jmp transformation didn't
  1537. count correctly the jmp references
  1538. Revision 1.4 1999/04/10 16:14:11 peter
  1539. * fixed optimizer
  1540. Revision 1.3 1999/04/09 08:33:18 peter
  1541. * fixed mov reg,treg;mov treg,x bug
  1542. Revision 1.2 1999/03/29 16:05:51 peter
  1543. * optimizer working for ag386bin
  1544. Revision 1.1 1999/03/26 00:01:15 peter
  1545. * first things for optimizer (compiles but cycle crashes)
  1546. Revision 1.39 1999/02/26 00:48:22 peter
  1547. * assembler writers fixed for ag386bin
  1548. Revision 1.38 1999/02/25 21:02:44 peter
  1549. * ag386bin updates
  1550. + coff writer
  1551. Revision 1.37 1999/02/22 02:15:30 peter
  1552. * updates for ag386bin
  1553. Revision 1.36 1999/01/04 22:04:15 jonas
  1554. + mov reg, mem1 to mov reg, mem1
  1555. mov mem2, reg cmp reg, mem2
  1556. cmp mem1, reg
  1557. # reg released
  1558. Revision 1.35 1999/01/04 12:58:55 jonas
  1559. * no fistp/fild optimization for S_IQ (fistq doesn't exist)
  1560. Revision 1.34 1998/12/29 18:48:17 jonas
  1561. + optimize pascal code surrounding assembler blocks
  1562. Revision 1.33 1998/12/23 15:16:21 jonas
  1563. * change "inc x/dec x; test x, x" to "add 1, x/sub 1,x" because inc and dec
  1564. don't affect the carry flag (test does). This *doesn't* fix the problem with
  1565. cardinal, that's a cg issue.
  1566. Revision 1.32 1998/12/16 12:09:29 jonas
  1567. * fixed fistp/fild optimization
  1568. Revision 1.31 1998/12/15 22:30:39 jonas
  1569. + change "sub/add const1, reg" or "dec reg" followed by "sub const2, reg" to one
  1570. "sub const3, reg"
  1571. * some small cleaning up
  1572. Revision 1.30 1998/12/15 15:43:20 jonas
  1573. * fixed bug in shr/shl optimization
  1574. Revision 1.29 1998/12/15 11:53:54 peter
  1575. * removed commentlevel
  1576. Revision 1.28 1998/12/14 22:01:45 jonas
  1577. - removed $ifdef ver0_99_11's
  1578. Revision 1.27 1998/12/11 00:03:35 peter
  1579. + globtype,tokens,version unit splitted from globals
  1580. Revision 1.26 1998/12/09 18:16:13 jonas
  1581. * corrected small syntax error in part between ifdef ver0_99_11
  1582. + added fistp/fild optimization between ifdef ver0_99_11
  1583. Revision 1.25 1998/12/02 16:23:29 jonas
  1584. * changed "if longintvar in set" to case or "if () or () .." statements
  1585. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  1586. Revision 1.24 1998/11/26 15:41:45 jonas
  1587. + change "setxx mem; movb mem, reg8" to "setxx reg8" if mem is a local
  1588. variable/parameter or function result (between $ifdef ver0_99_11)
  1589. Revision 1.23 1998/11/03 16:26:09 jonas
  1590. * "call x;jmp y" optimization not done anymore for P6 and equivalents
  1591. * made FPU optimizations simpler and more effective
  1592. Revision 1.22 1998/10/29 18:37:55 jonas
  1593. + change "call x; jmp y" to "push y; jmp x" (suggestion from Daniel)
  1594. Revision 1.19 1998/10/23 15:38:23 jonas
  1595. + some small FPU peephole optimizations (use value in FP regs instead of loading it
  1596. from memory if possible, mostly with var1+var1 and var1*var1)
  1597. Revision 1.18 1998/10/05 14:41:14 jonas
  1598. * fixed small memory leak
  1599. * fixed small inefficiency
  1600. * tested multiple line comments ability of my new MacCVS client :)
  1601. Revision 1.17 1998/10/02 17:29:56 jonas
  1602. + removal of "lea (reg), reg)", "imul $1, reg", change "mov reg1, reg2; mov (reg2), reg2" to "mov (reg1), reg2"
  1603. Revision 1.16 1998/10/01 20:19:57 jonas
  1604. * moved UpdateUsedRegs (+ bugfix) to daopt386
  1605. Revision 1.15 1998/09/30 12:18:29 peter
  1606. * fixed subl $2,esp;psuhw bug
  1607. Revision 1.14 1998/09/20 17:11:51 jonas
  1608. * released REGALLOC
  1609. Revision 1.13 1998/09/16 18:00:00 jonas
  1610. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  1611. Revision 1.12 1998/09/15 14:05:22 jonas
  1612. * fixed optimizer incompatibilities with freelabel code in psub
  1613. Revision 1.11 1998/08/28 10:57:02 peter
  1614. * removed warnings
  1615. Revision 1.10 1998/08/27 15:17:50 florian
  1616. * reinstated Jonas' bugfix
  1617. Revision 1.9 1998/08/25 16:58:59 pierre
  1618. * removed a line that add no sense and
  1619. introduce garbage in the asmlist
  1620. (uninitialized data !)
  1621. Revision 1.7 1998/08/19 16:07:53 jonas
  1622. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1623. Revision 1.6 1998/08/10 14:50:14 peter
  1624. + localswitches, moduleswitches, globalswitches splitting
  1625. Revision 1.5 1998/08/06 19:40:28 jonas
  1626. * removed $ before and after Log in comment
  1627. Revision 1.4 1998/08/05 16:27:17 jonas
  1628. * fstp/fld bugfix (fstt does not exist)
  1629. Revision 1.3 1998/08/05 16:00:15 florian
  1630. * some fixes for ansi strings
  1631. * log to Log changed
  1632. }