popt386.pas 80 KB

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