popt386.pas 82 KB

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