popt386.pas 88 KB

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