popt386.pas 82 KB

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