popt386.pas 87 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: pai386_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. ((pai386_labeled(p1)^.opcode = A_JMP) or
  70. (pai386_labeled(p1)^.opcode = hp^.opcode))
  71. Then
  72. Begin
  73. GetFinalDestination(pai386_labeled(p1));
  74. Dec(hp^.lab^.refcount);
  75. If (hp^.lab^.refcount = 0) Then
  76. hp^.lab^.is_used := False;
  77. hp^.lab := pai386_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 (pai386_labeled(p)^.opcode = 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. (pai386_labeled(hp1)^.opcode=A_JMP) and
  110. GetNextInstruction(hp1, hp2) And
  111. FindLabel(pai386_labeled(p)^.lab, hp2)
  112. Then
  113. Begin
  114. Case pai386_labeled(p)^.opcode Of
  115. A_JE : pai386_labeled(p)^.opcode:=A_JNE;
  116. A_JNE : pai386_labeled(p)^.opcode:=A_JE;
  117. A_JL : pai386_labeled(p)^.opcode:=A_JGE;
  118. A_JG : pai386_labeled(p)^.opcode:=A_JLE;
  119. A_JLE : pai386_labeled(p)^.opcode:=A_JG;
  120. A_JGE : pai386_labeled(p)^.opcode:=A_JL;
  121. A_JNZ : pai386_labeled(p)^.opcode:=A_JZ;
  122. A_JNO : pai386_labeled(p)^.opcode:=A_JO;
  123. A_JZ : pai386_labeled(p)^.opcode:=A_JNZ;
  124. A_JS : pai386_labeled(p)^.opcode:=A_JNS;
  125. A_JNS : pai386_labeled(p)^.opcode:=A_JS;
  126. A_JO : pai386_labeled(p)^.opcode:=A_JNO;
  127. A_JC : pai386_labeled(p)^.opcode:=A_JNC;
  128. A_JNC : pai386_labeled(p)^.opcode:=A_JC;
  129. A_JA : pai386_labeled(p)^.opcode:=A_JBE;
  130. A_JAE : pai386_labeled(p)^.opcode:=A_JB;
  131. A_JB : pai386_labeled(p)^.opcode:=A_JAE;
  132. A_JBE : pai386_labeled(p)^.opcode:=A_JA;
  133. Else
  134. begin
  135. If (LabDif <> 0) Then GetFinalDestination(pai386_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. pai386_labeled(p)^.lab:=pai386_labeled(hp1)^.lab;
  144. Inc(pai386_labeled(p)^.lab^.refcount);
  145. asml^.remove(hp1);
  146. dispose(hp1,done);
  147. If (LabDif <> 0) Then GetFinalDestination(pai386_labeled(p));
  148. end
  149. else
  150. if FindLabel(pai386_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(pai386_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)^.opcode 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)^.opcode = 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)^.opcode := 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)^.opcode := 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)^.opsize 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)^.opcode = A_FLD) Or
  239. (Pai386(hp1)^.opcode = A_FST)) And
  240. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  241. (Pai386(hp1)^.op1t = top_ref) And
  242. RefsEqual(TReference(Pai386(p)^.Op1^), TReference(Pai386(hp1)^.Op1^)) Then
  243. If ((Pai386(hp2)^.opcode = A_FMULP) Or
  244. (Pai386(hp2)^.opcode = 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)^.opcode = A_FADDP)
  255. Then Pai386(hp2)^.opcode := A_FADD
  256. Else Pai386(hp2)^.opcode := 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)^.opsize := 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)^.opcode 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)^.opcode Of
  279. A_FADDP: Pai386(p)^.opcode := A_FADD;
  280. A_FMULP: Pai386(p)^.opcode := A_FMUL;
  281. A_FSUBP: Pai386(p)^.opcode := A_FSUBR;
  282. A_FSUBRP: Pai386(p)^.opcode := A_FSUB;
  283. A_FDIVP: Pai386(p)^.opcode := A_FDIVR;
  284. A_FDIVRP: Pai386(p)^.opcode := 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)^.opcode = A_FLD) And
  298. (Pai386(p)^.opcode = A_FSTP)) Or
  299. ((Pai386(p)^.opcode = A_FISTP) And
  300. (Pai386(hp1)^.opcode = A_FILD))) And
  301. (Pai386(hp1)^.op1t = top_ref) And
  302. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) 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)^.opcode = A_LEAVE) Or
  309. (Pai386(hp2)^.opcode = 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)^.opsize <> S_FX) And
  325. (Pai386(p)^.opsize <> S_IQ) Then
  326. Begin
  327. If (Pai386(p)^.opcode = A_FSTP) Then
  328. Pai386(p)^.opcode := A_FST
  329. Else Pai386(p)^.opcode := 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)^.opsize = 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. ((pai386_labeled(hp1)^.opcode = A_JO) or
  371. (pai386_labeled(hp1)^.opcode = A_JNO))))
  372. Then
  373. Begin
  374. New(TmpRef);
  375. TmpRef^.segment := R_DEFAULT_SEG;
  376. TmpRef^.symbol := nil;
  377. TmpRef^.is_immediate := 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^.is_immediate := 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^.is_immediate := 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)^.opcode = 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)^.opcode = A_OR) Or
  679. (Pai386(hp1)^.opcode = 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)^.opcode
  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)^.opcode = A_LEAVE) Or
  723. (Pai386(hp1)^.opcode = 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)^.opsize = Pai386(hp1)^.opsize) And
  739. (Pai386(hp1)^.opcode = 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)^.opcode = 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)^.opcode = 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)^.opcode := 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)^.opcode = 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. Pai386(hp1)^.op1 := Pai386(hp1)^.op2; {move the treference}
  856. TReference(Pai386(hp1)^.op1^) := TReference(Pai386(p)^.op1^);
  857. Pai386(hp1)^.op2 := Pai386(hp2)^.op2;
  858. End
  859. Else
  860. Begin
  861. AsmL^.Remove(hp1);
  862. Dispose(hp1, Done)
  863. End;
  864. Pai386(hp2)^.opxt := top_reg + top_ref shl 4;
  865. Pai386(hp2)^.op2 := Pai386(hp2)^.op1;
  866. Pai386(hp2)^.op1 := Pai386(p)^.op2;
  867. End;
  868. End;
  869. End
  870. Else
  871. (* {movl [mem1],reg1
  872. movl [mem1],reg2
  873. to:
  874. movl [mem1],reg1
  875. movl reg1,reg2 }
  876. If (Pai386(p)^.op1t = top_ref) and
  877. (Pai386(p)^.op2t = top_reg) and
  878. (Pai386(hp1)^.op1t = top_ref) and
  879. (Pai386(hp1)^.op2t = top_reg) and
  880. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) and
  881. RefsEqual(TReference(Pai386(p)^.op1^),TReference(Pai386(hp1)^.op1^)) and
  882. (TRegister(Pai386(p)^.op2)<>TReference(Pai386(hp1)^.op1^).base) and
  883. (TRegister(Pai386(p)^.op2)<>TReference(Pai386(hp1)^.op1^).index) then
  884. Begin
  885. Dispose(PReference(Pai386(hp1)^.op1));
  886. Pai386(hp1)^.op1:=Pai386(p)^.op2;
  887. Pai386(hp1)^.opxt:=Top_reg+Top_reg shl 4;
  888. End
  889. Else*)
  890. { movl const1,[mem1]
  891. movl [mem1],reg1
  892. to:
  893. movl const1,reg1
  894. movl reg1,[mem1] }
  895. If (Pai386(p)^.op1t = top_const) and
  896. (Pai386(p)^.op2t = top_ref) and
  897. (Pai386(hp1)^.op1t = top_ref) and
  898. (Pai386(hp1)^.op2t = top_reg) and
  899. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) and
  900. RefsEqual(TReference(Pai386(hp1)^.op1^),TReference(Pai386(p)^.op2^)) then
  901. Begin
  902. Pai386(hp1)^.op1:=Pai386(hp1)^.op2;
  903. Pai386(hp1)^.op2:=Pai386(p)^.op2;
  904. Pai386(hp1)^.opxt:=Top_reg+Top_ref shl 4;
  905. Pai386(p)^.op2:=Pai386(hp1)^.op1;
  906. Pai386(p)^.opxt:=Top_const+(top_reg shl 4);
  907. End
  908. End;
  909. {changes "mov $0, %reg" into "xor %reg, %reg"}
  910. If (Pai386(p)^.op1t = Top_Const) And
  911. (Pai386(p)^.op1 = Pointer(0)) And
  912. (Pai386(p)^.op2t = Top_Reg)
  913. Then
  914. Begin
  915. Pai386(p)^.opcode := A_XOR;
  916. Pai386(p)^.opxt := Top_Reg+Top_reg shl 4;
  917. Pai386(p)^.op1 := Pai386(p)^.op2;
  918. End;
  919. End;
  920. A_MOVZX:
  921. Begin
  922. {removes superfluous And's after movzx's}
  923. If (Pai386(p)^.op2t = top_reg) And
  924. GetNextInstruction(p, hp1) And
  925. (Pai(hp1)^.typ = ait_instruction) And
  926. (Pai386(hp1)^.opcode = A_AND) And
  927. (Pai386(hp1)^.op1t = top_const) And
  928. (Pai386(hp1)^.op2t = top_reg) And
  929. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  930. Then
  931. Case Pai386(p)^.opsize Of
  932. S_BL, S_BW:
  933. If (Longint(Pai386(hp1)^.op1) = $ff)
  934. Then
  935. Begin
  936. AsmL^.Remove(hp1);
  937. Dispose(hp1, Done);
  938. End;
  939. S_WL:
  940. If (Longint(Pai386(hp1)^.op1) = $ffff)
  941. Then
  942. Begin
  943. AsmL^.Remove(hp1);
  944. Dispose(hp1, Done);
  945. End;
  946. End;
  947. {changes some movzx constructs to faster synonims (all examples
  948. are given with eax/ax, but are also valid for other registers)}
  949. If (Pai386(p)^.op2t = top_reg) Then
  950. If (Pai386(p)^.op1t = top_reg)
  951. Then
  952. Case Pai386(p)^.opsize of
  953. S_BW:
  954. Begin
  955. If (TRegister(Pai386(p)^.op1) = Reg16ToReg8(TRegister(Pai386(p)^.op2))) And
  956. Not(CS_LittleSize In aktglobalswitches)
  957. Then
  958. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  959. Begin
  960. Pai386(p)^.opcode := A_AND;
  961. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  962. Longint(Pai386(p)^.op1) := $ff;
  963. Pai386(p)^.opsize := S_W
  964. End
  965. Else
  966. If GetNextInstruction(p, hp1) And
  967. (Pai(hp1)^.typ = ait_instruction) And
  968. (Pai386(hp1)^.opcode = A_AND) And
  969. (Pai386(hp1)^.op1t = top_const) And
  970. (Pai386(hp1)^.op2t = top_reg) And
  971. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  972. Then
  973. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  974. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  975. Begin
  976. Pai386(p)^.opcode := A_MOV;
  977. Pai386(p)^.opsize := S_W;
  978. Pai386(p)^.op1 := Pointer(Reg8ToReg16(TRegister(Pai386(p)^.op1)));
  979. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ff);
  980. End;
  981. End;
  982. S_BL:
  983. Begin
  984. If (TRegister(Pai386(p)^.op1) = Reg32ToReg8(TRegister(Pai386(p)^.op2))) And
  985. Not(CS_LittleSize in aktglobalswitches)
  986. Then
  987. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  988. Begin
  989. Pai386(p)^.opcode := A_AND;
  990. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  991. Longint(Pai386(p)^.op1) := $ff;
  992. Pai386(p)^.opsize := S_L;
  993. End
  994. Else
  995. If GetNextInstruction(p, hp1) And
  996. (Pai(hp1)^.typ = ait_instruction) And
  997. (Pai386(hp1)^.opcode = A_AND) And
  998. (Pai386(hp1)^.op1t = top_const) And
  999. (Pai386(hp1)^.op2t = top_reg) And
  1000. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  1001. Then
  1002. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  1003. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  1004. Begin
  1005. Pai386(p)^.opcode := A_MOV;
  1006. Pai386(p)^.opsize := S_L;
  1007. Pai386(p)^.op1 := Pointer(Reg8ToReg32(TRegister(Pai386(p)^.op1)));
  1008. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ff);
  1009. End
  1010. End;
  1011. S_WL:
  1012. Begin
  1013. If (TRegister(Pai386(p)^.op1) = Reg32ToReg16(TRegister(Pai386(p)^.op2))) And
  1014. Not(CS_LittleSize In aktglobalswitches)
  1015. Then
  1016. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  1017. Begin
  1018. Pai386(p)^.opcode := A_AND;
  1019. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  1020. Longint(Pai386(p)^.op1) := $ffff;
  1021. Pai386(p)^.opsize := S_L
  1022. End
  1023. Else
  1024. If GetNextInstruction(p, hp1) And
  1025. (Pai(hp1)^.typ = ait_instruction) And
  1026. (Pai386(hp1)^.opcode = A_AND) And
  1027. (Pai386(hp1)^.op1t = top_const) And
  1028. (Pai386(hp1)^.op2t = top_reg) And
  1029. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  1030. Then
  1031. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  1032. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  1033. Begin
  1034. Pai386(p)^.opcode := A_MOV;
  1035. Pai386(p)^.opsize := S_L;
  1036. Pai386(p)^.op1 := Pointer(Reg16ToReg32(TRegister(Pai386(p)^.op1)));
  1037. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ffff);
  1038. End;
  1039. End;
  1040. End
  1041. Else
  1042. If (Pai386(p)^.op1t = top_ref) Then
  1043. Begin
  1044. If GetNextInstruction(p, hp1) And
  1045. (Pai(hp1)^.typ = ait_instruction) And
  1046. (Pai386(hp1)^.opcode = A_AND) And
  1047. (Pai386(hp1)^.op1t = Top_Const) And
  1048. (Pai386(hp1)^.op2t = Top_Reg) And
  1049. (Pai386(hp1)^.op2 = Pai386(p)^.op2) Then
  1050. Begin
  1051. Pai386(p)^.opcode := A_MOV;
  1052. Case Pai386(p)^.opsize Of
  1053. S_BL:
  1054. Begin
  1055. Pai386(p)^.opsize := S_L;
  1056. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  1057. And $ff);
  1058. End;
  1059. S_WL:
  1060. Begin
  1061. Pai386(p)^.opsize := S_L;
  1062. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  1063. And $ffff);
  1064. End;
  1065. S_BW:
  1066. Begin
  1067. Pai386(p)^.opsize := S_W;
  1068. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  1069. And $ff);
  1070. End;
  1071. End;
  1072. End;
  1073. End;
  1074. End;
  1075. A_POP:
  1076. Begin
  1077. if (Pai386(p)^.op1t = top_reg) And
  1078. GetNextInstruction(p, hp1) And
  1079. (pai(hp1)^.typ=ait_instruction) and
  1080. (Pai386(hp1)^.opcode=A_PUSH) and
  1081. (Pai386(hp1)^.op1t = top_reg) And
  1082. (Pai386(hp1)^.op1=Pai386(p)^.op1) then
  1083. If (Not(cs_regalloc in aktglobalswitches)) Then
  1084. Begin
  1085. hp2:=pai(hp1^.next);
  1086. asml^.remove(p);
  1087. asml^.remove(hp1);
  1088. dispose(p,done);
  1089. dispose(hp1,done);
  1090. p:=hp2;
  1091. continue
  1092. End
  1093. Else
  1094. Begin
  1095. Pai386(p)^.opcode := A_MOV;
  1096. Pai386(p)^.op2 := Pai386(p)^.op1;
  1097. Pai386(p)^.opxt := top_ref + top_reg shl 4;
  1098. New(TmpRef);
  1099. TmpRef^.segment := R_DEFAULT_SEG;
  1100. TmpRef^.base := R_ESP;
  1101. TmpRef^.index := R_NO;
  1102. TmpRef^.scalefactor := 1;
  1103. TmpRef^.symbol := nil;
  1104. TmpRef^.is_immediate := false;
  1105. TmpRef^.offset := 0;
  1106. Pai386(p)^.op1 := Pointer(TmpRef);
  1107. hp1 := Pai(p^.next);
  1108. AsmL^.Remove(hp1);
  1109. Dispose(hp1, Done)
  1110. End
  1111. end;
  1112. A_PUSH:
  1113. Begin
  1114. If (Pai386(p)^.opsize = S_W) And
  1115. (Pai386(p)^.op1t = Top_Const) And
  1116. GetNextInstruction(p, hp1) And
  1117. (Pai(hp1)^.typ = ait_instruction) And
  1118. (Pai386(hp1)^.opcode = A_PUSH) And
  1119. (Pai386(hp1)^.op1t = Top_Const) And
  1120. (Pai386(hp1)^.opsize = S_W) Then
  1121. Begin
  1122. Pai386(p)^.opsize := S_L;
  1123. Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) shl 16 + Longint(Pai386(hp1)^.op1));
  1124. AsmL^.Remove(hp1);
  1125. Dispose(hp1, Done)
  1126. End;
  1127. End;
  1128. A_SHL, A_SAL:
  1129. Begin
  1130. If (Pai386(p)^.op1t = Top_Const) And
  1131. (Pai386(p)^.op2t = Top_Reg) And
  1132. (Pai386(p)^.opsize = S_L) And
  1133. (Longint(Pai386(p)^.op1) <= 3)
  1134. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1135. Then
  1136. Begin
  1137. TmpBool1 := True; {should we check the next instruction?}
  1138. TmpBool2 := False; {have we found an add/sub which could be
  1139. integrated in the lea?}
  1140. New(TmpRef);
  1141. TmpRef^.segment := R_DEFAULT_SEG;
  1142. TmpRef^.base := R_NO;
  1143. TmpRef^.index := TRegister(Pai386(p)^.op2);
  1144. TmpRef^.scalefactor := 1 shl Longint(Pai386(p)^.op1);
  1145. TmpRef^.symbol := nil;
  1146. TmpRef^.is_immediate := false;
  1147. TmpRef^.offset := 0;
  1148. While TmpBool1 And
  1149. GetNextInstruction(p, hp1) And
  1150. (Pai(hp1)^.typ = ait_instruction) And
  1151. ((Pai386(hp1)^.opcode = A_ADD) Or
  1152. (Pai386(hp1)^.opcode = A_SUB)) And
  1153. (Pai386(hp1)^.op2t = Top_Reg) And
  1154. (Pai386(hp1)^.op2 = Pai386(p)^.op2) Do
  1155. Begin
  1156. TmpBool1 := False;
  1157. If (Pai386(hp1)^.op1t = Top_Const)
  1158. Then
  1159. Begin
  1160. TmpBool1 := True;
  1161. TmpBool2 := True;
  1162. If Pai386(hp1)^.opcode = A_ADD
  1163. Then Inc(TmpRef^.offset, Longint(Pai386(hp1)^.op1))
  1164. Else Dec(TmpRef^.offset, Longint(Pai386(hp1)^.op1));
  1165. AsmL^.Remove(hp1);
  1166. Dispose(hp1, Done);
  1167. End
  1168. Else
  1169. If (Pai386(hp1)^.op1t = Top_Reg) And
  1170. (Pai386(hp1)^.opcode = A_ADD) And
  1171. (TmpRef^.base = R_NO) Then
  1172. Begin
  1173. TmpBool1 := True;
  1174. TmpBool2 := True;
  1175. TmpRef^.base := TRegister(Pai386(hp1)^.op1);
  1176. AsmL^.Remove(hp1);
  1177. Dispose(hp1, Done);
  1178. End;
  1179. End;
  1180. If TmpBool2 Or
  1181. ((aktoptprocessor < ClassP6) And
  1182. (Longint(Pai386(p)^.op1) <= 3) And
  1183. Not(CS_LittleSize in aktglobalswitches))
  1184. Then
  1185. Begin
  1186. If Not(TmpBool2) And
  1187. (Longint(Pai386(p)^.op1) = 1)
  1188. Then
  1189. Begin
  1190. Dispose(TmpRef);
  1191. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.opsize,
  1192. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)))
  1193. End
  1194. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  1195. TRegister(Pai386(p)^.op2)));
  1196. hp1^.fileinfo := p^.fileinfo;
  1197. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1198. Dispose(p, Done);
  1199. p := hp1;
  1200. End;
  1201. End
  1202. Else
  1203. If (aktoptprocessor < ClassP6) And
  1204. (Pai386(p)^.op1t = top_const) And
  1205. (Pai386(p)^.op2t = top_reg) Then
  1206. If (Longint(Pai386(p)^.op1) = 1)
  1207. Then
  1208. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1209. but faster on a 486, and pairable in both U and V pipes on the Pentium
  1210. (unlike shl, which is only pairable in the U pipe)}
  1211. Begin
  1212. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.opsize,
  1213. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)));
  1214. hp1^.fileinfo := p^.fileinfo;
  1215. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1216. Dispose(p, done);
  1217. p := hp1;
  1218. End
  1219. Else If (Pai386(p)^.opsize = S_L) and
  1220. (Longint(Pai386(p)^.op1) <= 3) Then
  1221. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1222. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1223. Begin
  1224. New(TmpRef);
  1225. TmpRef^.segment := R_DEFAULT_SEG;
  1226. TmpRef^.base := R_NO;
  1227. TmpRef^.index := TRegister(Pai386(p)^.op2);
  1228. TmpRef^.scalefactor := 1 shl Longint(Pai386(p)^.op1);
  1229. TmpRef^.symbol := nil;
  1230. TmpRef^.is_immediate := false;
  1231. TmpRef^.offset := 0;
  1232. hp1 := new(Pai386,op_ref_reg(A_LEA,S_L,TmpRef, TRegister(Pai386(p)^.op2)));
  1233. hp1^.fileinfo := p^.fileinfo;
  1234. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1235. Dispose(p, done);
  1236. p := hp1;
  1237. End
  1238. End;
  1239. A_SAR, A_SHR:
  1240. {changes the code sequence
  1241. shr/sar const1, x
  1242. shl const2, x
  1243. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  1244. Begin
  1245. If GetNextInstruction(p, hp1) And
  1246. (pai(hp1)^.typ = ait_instruction) and
  1247. (Pai386(hp1)^.opcode = A_SHL) and
  1248. (Pai386(p)^.op1t = top_const) and
  1249. (Pai386(hp1)^.op1t = top_const) and
  1250. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  1251. (Pai386(hp1)^.op2t = Pai386(p)^.op2t) And
  1252. OpsEqual(Pai386(hp1)^.op2t, Pai386(hp1)^.Op2, Pai386(p)^.Op2)
  1253. Then
  1254. If (Longint(Pai386(p)^.op1) > Longint(Pai386(hp1)^.op1)) And
  1255. Not(CS_LittleSize In aktglobalswitches)
  1256. Then
  1257. { shr/sar const1, %reg
  1258. shl const2, %reg
  1259. with const1 > const2 }
  1260. Begin
  1261. Dec(Longint(Pai386(p)^.op1), Longint(Pai386(hp1)^.op1));
  1262. Pai386(hp1)^.opcode := A_And;
  1263. Pai386(hp1)^.op1 := Pointer(1 shl Longint(Pai386(hp1)^.op1)-1);
  1264. Case Pai386(p)^.opsize Of
  1265. S_L: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffffffff);
  1266. S_B: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ff);
  1267. S_W: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffff);
  1268. End;
  1269. End
  1270. Else
  1271. If (Longint(Pai386(p)^.op1) < Longint(Pai386(hp1)^.op1)) And
  1272. Not(CS_LittleSize In aktglobalswitches)
  1273. Then
  1274. { shr/sar const1, %reg
  1275. shl const2, %reg
  1276. with const1 < const2 }
  1277. Begin
  1278. Dec(Longint(Pai386(hp1)^.op1), Longint(Pai386(p)^.op1));
  1279. Pai386(p)^.opcode := A_And;
  1280. Pai386(p)^.op1 := Pointer(1 shl Longint(Pai386(p)^.op1)-1);
  1281. Case Pai386(p)^.opsize Of
  1282. S_L: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ffffffff);
  1283. S_B: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ff);
  1284. S_W: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ffff);
  1285. End;
  1286. End
  1287. Else
  1288. { shr/sar const1, %reg
  1289. shl const2, %reg
  1290. with const1 = const2 }
  1291. Begin
  1292. Pai386(p)^.opcode := A_And;
  1293. Pai386(p)^.op1 := Pointer(1 shl Longint(Pai386(p)^.op1)-1);
  1294. Case Pai386(p)^.opsize Of
  1295. S_B: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ff);
  1296. S_W: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ffff);
  1297. S_L: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ffffffff);
  1298. End;
  1299. AsmL^.remove(hp1);
  1300. dispose(hp1, done);
  1301. End;
  1302. End;
  1303. A_SETE..A_SETGE,A_SETC,A_SETNC,A_SETA..A_SETBE,A_SETO..A_SETNLE:
  1304. Begin
  1305. If (Pai386(p)^.Op1t = top_ref) And
  1306. GetNextInstruction(p, hp1) And
  1307. GetNextInstruction(hp1, hp2) And
  1308. (hp2^.typ = ait_instruction) And
  1309. ((Pai386(hp2)^.opcode = A_LEAVE) or
  1310. (Pai386(hp2)^.opcode = A_RET)) And
  1311. (TReference(Pai386(p)^.Op1^).Base = ProcInfo.FramePointer) And
  1312. (TReference(Pai386(p)^.Op1^).Index = R_NO) And
  1313. (TReference(Pai386(p)^.Op1^).Offset >= ProcInfo.RetOffset) And
  1314. (hp1^.typ = ait_instruction) And
  1315. (Pai386(hp1)^.opcode = A_MOV) And
  1316. (Pai386(hp1)^.opsize = S_B) And
  1317. (Pai386(hp1)^.Op1t = top_ref) And
  1318. RefsEqual(TReference(Pai386(hp1)^.Op1^), TReference(Pai386(p)^.Op1^)) Then
  1319. Begin
  1320. Dispose(PReference(Pai386(p)^.Op1));
  1321. Pai386(p)^.Op1 := Pai386(hp1)^.Op2;
  1322. Pai386(p)^.Opxt := top_reg;
  1323. AsmL^.Remove(hp1);
  1324. Dispose(hp1, Done)
  1325. End
  1326. End;
  1327. A_SUB:
  1328. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1329. { * change "sub/add const1, reg" or "dec reg" followed by
  1330. "sub const2, reg" to one "sub ..., reg" }
  1331. Begin
  1332. If (Pai386(p)^.op1t = top_const) And
  1333. (Pai386(p)^.op2t = top_reg) Then
  1334. If (Longint(Pai386(p)^.op1) = 2) And
  1335. (TRegister(Pai386(p)^.op2) = R_ESP) Then
  1336. Begin
  1337. hp1 := Pai(p^.next);
  1338. While Assigned(hp1) And
  1339. (Pai(hp1)^.typ In [ait_instruction]+SkipInstr) And
  1340. Not((Pai(hp1)^.typ = ait_instruction) And
  1341. ((Pai386(hp1)^.opcode = A_CALL) or
  1342. (Pai386(hp1)^.opcode = A_PUSH) or
  1343. ((Pai386(hp1)^.opcode = A_MOV) And
  1344. (Pai386(hp1)^.op2t = top_ref) And
  1345. (TReference(Pai386(hp1)^.op2^).base = R_ESP)))) do
  1346. hp1 := Pai(hp1^.next);
  1347. If Assigned(hp1) And
  1348. (Pai(hp1)^.typ = ait_instruction) And
  1349. (Pai386(hp1)^.opcode = A_PUSH) And
  1350. (Pai386(hp1)^.opsize = S_W)
  1351. Then
  1352. Begin
  1353. Pai386(hp1)^.opsize := S_L;
  1354. If (Pai386(hp1)^.op1t = top_reg) Then
  1355. Pai386(hp1)^.op1 := Pointer(Reg16ToReg32(TRegister(Pai386(hp1)^.op1)));
  1356. hp1 := Pai(p^.next);
  1357. AsmL^.Remove(p);
  1358. Dispose(p, Done);
  1359. p := hp1;
  1360. Continue
  1361. End
  1362. Else
  1363. If GetLastInstruction(p, hp1) And
  1364. (Pai(hp1)^.typ = ait_instruction) And
  1365. (Pai386(hp1)^.opcode = A_SUB) And
  1366. (Pai386(hp1)^.op1t = top_const) And
  1367. (Pai386(hp1)^.op2t = top_reg) And
  1368. (TRegister(Pai386(hp1)^.Op2) = R_ESP)
  1369. Then
  1370. Begin
  1371. Inc(Longint(Pai386(p)^.op1), Longint(Pai386(hp1)^.op1));
  1372. AsmL^.Remove(hp1);
  1373. Dispose(hp1, Done);
  1374. End;
  1375. End
  1376. Else
  1377. If GetLastInstruction(p, hp1) And
  1378. (hp1^.typ = ait_instruction) And
  1379. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) then
  1380. Case Pai386(hp1)^.opcode Of
  1381. A_DEC:
  1382. If (Pai386(hp1)^.Op1t = top_reg) And
  1383. (Pai386(hp1)^.Op1 = Pai386(p)^.op2) Then
  1384. Begin
  1385. Inc(Longint(Pai386(p)^.Op1));
  1386. AsmL^.Remove(hp1);
  1387. Dispose(hp1, Done)
  1388. End;
  1389. A_SUB:
  1390. If (Pai386(hp1)^.Op1t = top_const) And
  1391. (Pai386(hp1)^.Op2t = top_reg) And
  1392. (Pai386(hp1)^.Op2 = Pai386(p)^.Op2) Then
  1393. Begin
  1394. Inc(Longint(Pai386(p)^.Op1), Longint(Pai386(hp1)^.Op1));
  1395. AsmL^.Remove(hp1);
  1396. Dispose(hp1, Done)
  1397. End;
  1398. A_ADD:
  1399. If (Pai386(hp1)^.Op1t = top_const) And
  1400. (Pai386(hp1)^.Op2t = top_reg) And
  1401. (Pai386(hp1)^.Op2 = Pai386(p)^.Op2) Then
  1402. Begin
  1403. Dec(Longint(Pai386(p)^.Op1), Longint(Pai386(hp1)^.Op1));
  1404. AsmL^.Remove(hp1);
  1405. Dispose(hp1, Done)
  1406. End;
  1407. End
  1408. End;
  1409. A_TEST, A_OR:
  1410. {removes the line marked with (x) from the sequence
  1411. And/or/xor/add/sub/... $x, %y
  1412. test/or %y, %y (x)
  1413. j(n)z _Label
  1414. as the first instruction already adjusts the ZF}
  1415. Begin
  1416. If (Pai386(p)^.op1 = Pai386(p)^.op2) And
  1417. GetLastInstruction(p, hp1) And
  1418. (pai(hp1)^.typ = ait_instruction) Then
  1419. Case Pai386(hp1)^.opcode Of
  1420. A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_SHL, A_SHR:
  1421. Begin
  1422. If (Pai386(hp1)^.op2 = Pai386(p)^.op1) Then
  1423. Begin
  1424. hp1 := pai(p^.next);
  1425. asml^.remove(p);
  1426. dispose(p, done);
  1427. p := pai(hp1);
  1428. continue
  1429. End;
  1430. End;
  1431. A_DEC, A_INC, A_NEG:
  1432. Begin
  1433. If (Pai386(hp1)^.op1 = Pai386(p)^.op1) Then
  1434. Begin
  1435. Case Pai386(hp1)^.opcode Of
  1436. A_DEC, A_INC:
  1437. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  1438. Begin
  1439. Pai386(hp1)^.opxt := Pai386(hp1)^.opxt shl 4 + top_const;
  1440. Pai386(hp1)^.Op2 := Pai386(hp1)^.Op1;
  1441. Pai386(hp1)^.Op1 := Pointer(1);
  1442. Case Pai386(hp1)^.opcode Of
  1443. A_DEC: Pai386(hp1)^.opcode := A_SUB;
  1444. A_INC: Pai386(hp1)^.opcode := A_ADD;
  1445. End
  1446. End
  1447. End;
  1448. hp1 := pai(p^.next);
  1449. asml^.remove(p);
  1450. dispose(p, done);
  1451. p := pai(hp1);
  1452. continue
  1453. End;
  1454. End
  1455. End;
  1456. End;
  1457. End;
  1458. End;
  1459. { ait_label:
  1460. Begin
  1461. If Not(Pai_Label(p)^.l^.is_used)
  1462. Then
  1463. Begin
  1464. hp1 := Pai(p^.next);
  1465. AsmL^.Remove(p);
  1466. Dispose(p, Done);
  1467. p := hp1;
  1468. Continue
  1469. End;
  1470. End;}
  1471. End;
  1472. p:=pai(p^.next);
  1473. end;
  1474. end;
  1475. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  1476. var
  1477. p,hp1,hp2: pai;
  1478. Begin
  1479. P := BlockStart;
  1480. While (P <> BlockEnd) Do
  1481. Begin
  1482. Case P^.Typ Of
  1483. Ait_Instruction:
  1484. Begin
  1485. Case Pai386(p)^.opcode Of
  1486. A_CALL:
  1487. If (AktOptProcessor < ClassP6) And
  1488. GetNextInstruction(p, hp1) And
  1489. (hp1^.typ = ait_labeled_instruction) And
  1490. (pai386_labeled(hp1)^.opcode = A_JMP) Then
  1491. Begin
  1492. hp2 := New(Pai386,op_sym(A_PUSH,S_L,NewAsmSymbol(Lab2Str(pai386_labeled(hp1)^.lab))));
  1493. hp2^.fileinfo := p^.fileinfo;
  1494. InsertLLItem(AsmL, p^.previous, p, hp2);
  1495. Pai386(p)^.opcode := A_JMP;
  1496. AsmL^.Remove(hp1);
  1497. Dispose(hp1, Done)
  1498. End;
  1499. A_MOV:
  1500. Begin
  1501. If (Pai386(p)^.op1t = top_reg) And
  1502. (Pai386(p)^.op2t = top_reg) And
  1503. GetNextInstruction(p, hp1) And
  1504. (hp1^.typ = ait_Instruction) And
  1505. (Pai386(hp1)^.opcode = A_MOV) And
  1506. (Pai386(hp1)^.op1t = top_ref) And
  1507. (Pai386(hp1)^.op2t = top_reg) And
  1508. ((TReference(Pai386(hp1)^.op1^).Base = TRegister(Pai386(p)^.op2)) Or
  1509. (TReference(Pai386(hp1)^.op1^).Index = TRegister(Pai386(p)^.op2))) And
  1510. (TRegister(Pai386(hp1)^.op2) = TRegister(Pai386(p)^.op2)) Then
  1511. {mov reg1, reg2
  1512. mov (reg2, ..), reg2 to mov (reg1, ..), reg2}
  1513. Begin
  1514. If (TReference(Pai386(hp1)^.op1^).Base = TRegister(Pai386(p)^.op2)) Then
  1515. TReference(Pai386(hp1)^.op1^).Base := TRegister(Pai386(p)^.op1);
  1516. If (TReference(Pai386(hp1)^.op1^).Index = TRegister(Pai386(p)^.op2)) Then
  1517. TReference(Pai386(hp1)^.op1^).Index := TRegister(Pai386(p)^.op1);
  1518. AsmL^.Remove(p);
  1519. Dispose(p, Done);
  1520. p := hp1;
  1521. Continue;
  1522. End;
  1523. End;
  1524. A_MOVZX:
  1525. Begin
  1526. If (Pai386(p)^.op2t = top_reg) Then
  1527. If (Pai386(p)^.op1t = top_reg)
  1528. Then
  1529. Case Pai386(p)^.opsize of
  1530. S_BL:
  1531. Begin
  1532. If IsGP32Reg(TRegister(Pai386(p)^.op2)) And
  1533. Not(CS_LittleSize in aktglobalswitches) And
  1534. (aktoptprocessor = ClassP5)
  1535. Then
  1536. {Change "movzbl %reg1, %reg2" to
  1537. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1538. PentiumMMX}
  1539. Begin
  1540. hp1 := New(Pai386, op_reg_reg(A_XOR, S_L,
  1541. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)));
  1542. hp1^.fileinfo := p^.fileinfo;
  1543. InsertLLItem(AsmL,p^.previous, p, hp1);
  1544. Pai386(p)^.opcode := A_MOV;
  1545. Pai386(p)^.opsize := S_B;
  1546. Pai386(p)^.op2 :=
  1547. Pointer(Reg32ToReg8(TRegister(Pai386(p)^.op2)));
  1548. { Jonas
  1549. InsertLLItem(AsmL,p, p^.next, hp2);
  1550. I think you forgot to delete this line PM
  1551. Indeed, I had forgotten that one (JM) }
  1552. End;
  1553. End;
  1554. End
  1555. Else
  1556. If (Pai386(p)^.op1t = top_ref) And
  1557. (PReference(Pai386(p)^.op1)^.base <> TRegister(Pai386(p)^.op2)) And
  1558. (PReference(Pai386(p)^.op1)^.index <> TRegister(Pai386(p)^.op2)) And
  1559. Not(CS_LittleSize in aktglobalswitches) And
  1560. IsGP32Reg(TRegister(Pai386(p)^.op2)) And
  1561. (aktoptprocessor = ClassP5) And
  1562. (Pai386(p)^.opsize = S_BL)
  1563. Then
  1564. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1565. Pentium and PentiumMMX}
  1566. Begin
  1567. hp1 := New(Pai386,op_reg_reg(A_XOR, S_L, TRegister(Pai386(p)^.op2),
  1568. TRegister(Pai386(p)^.op2)));
  1569. hp1^.fileinfo := p^.fileinfo;
  1570. Pai386(p)^.opcode := A_MOV;
  1571. Pai386(p)^.opsize := S_B;
  1572. Pai386(p)^.op2 := Pointer(Reg32ToReg8(TRegister(Pai386(p)^.op2)));
  1573. InsertLLItem(AsmL,p^.previous, p, hp1);
  1574. End;
  1575. End;
  1576. End;
  1577. End;
  1578. End;
  1579. p := Pai(p^.next)
  1580. End;
  1581. End;
  1582. End.
  1583. {
  1584. $Log$
  1585. Revision 1.39 1999-02-26 00:48:22 peter
  1586. * assembler writers fixed for ag386bin
  1587. Revision 1.38 1999/02/25 21:02:44 peter
  1588. * ag386bin updates
  1589. + coff writer
  1590. Revision 1.37 1999/02/22 02:15:30 peter
  1591. * updates for ag386bin
  1592. Revision 1.36 1999/01/04 22:04:15 jonas
  1593. + mov reg, mem1 to mov reg, mem1
  1594. mov mem2, reg cmp reg, mem2
  1595. cmp mem1, reg
  1596. # reg released
  1597. Revision 1.35 1999/01/04 12:58:55 jonas
  1598. * no fistp/fild optimization for S_IQ (fistq doesn't exist)
  1599. Revision 1.34 1998/12/29 18:48:17 jonas
  1600. + optimize pascal code surrounding assembler blocks
  1601. Revision 1.33 1998/12/23 15:16:21 jonas
  1602. * change "inc x/dec x; test x, x" to "add 1, x/sub 1,x" because inc and dec
  1603. don't affect the carry flag (test does). This *doesn't* fix the problem with
  1604. cardinal, that's a cg issue.
  1605. Revision 1.32 1998/12/16 12:09:29 jonas
  1606. * fixed fistp/fild optimization
  1607. Revision 1.31 1998/12/15 22:30:39 jonas
  1608. + change "sub/add const1, reg" or "dec reg" followed by "sub const2, reg" to one
  1609. "sub const3, reg"
  1610. * some small cleaning up
  1611. Revision 1.30 1998/12/15 15:43:20 jonas
  1612. * fixed bug in shr/shl optimization
  1613. Revision 1.29 1998/12/15 11:53:54 peter
  1614. * removed commentlevel
  1615. Revision 1.28 1998/12/14 22:01:45 jonas
  1616. - removed $ifdef ver0_99_11's
  1617. Revision 1.27 1998/12/11 00:03:35 peter
  1618. + globtype,tokens,version unit splitted from globals
  1619. Revision 1.26 1998/12/09 18:16:13 jonas
  1620. * corrected small syntax error in part between ifdef ver0_99_11
  1621. + added fistp/fild optimization between ifdef ver0_99_11
  1622. Revision 1.25 1998/12/02 16:23:29 jonas
  1623. * changed "if longintvar in set" to case or "if () or () .." statements
  1624. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  1625. Revision 1.24 1998/11/26 15:41:45 jonas
  1626. + change "setxx mem; movb mem, reg8" to "setxx reg8" if mem is a local
  1627. variable/parameter or function result (between $ifdef ver0_99_11)
  1628. Revision 1.23 1998/11/03 16:26:09 jonas
  1629. * "call x;jmp y" optimization not done anymore for P6 and equivalents
  1630. * made FPU optimizations simpler and more effective
  1631. Revision 1.22 1998/10/29 18:37:55 jonas
  1632. + change "call x; jmp y" to "push y; jmp x" (suggestion from Daniel)
  1633. Revision 1.19 1998/10/23 15:38:23 jonas
  1634. + some small FPU peephole optimizations (use value in FP regs instead of loading it
  1635. from memory if possible, mostly with var1+var1 and var1*var1)
  1636. Revision 1.18 1998/10/05 14:41:14 jonas
  1637. * fixed small memory leak
  1638. * fixed small inefficiency
  1639. * tested multiple line comments ability of my new MacCVS client :)
  1640. Revision 1.17 1998/10/02 17:29:56 jonas
  1641. + removal of "lea (reg), reg)", "imul $1, reg", change "mov reg1, reg2; mov (reg2), reg2" to "mov (reg1), reg2"
  1642. Revision 1.16 1998/10/01 20:19:57 jonas
  1643. * moved UpdateUsedRegs (+ bugfix) to daopt386
  1644. Revision 1.15 1998/09/30 12:18:29 peter
  1645. * fixed subl $2,esp;psuhw bug
  1646. Revision 1.14 1998/09/20 17:11:51 jonas
  1647. * released REGALLOC
  1648. Revision 1.13 1998/09/16 18:00:00 jonas
  1649. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  1650. Revision 1.12 1998/09/15 14:05:22 jonas
  1651. * fixed optimizer incompatibilities with freelabel code in psub
  1652. Revision 1.11 1998/08/28 10:57:02 peter
  1653. * removed warnings
  1654. Revision 1.10 1998/08/27 15:17:50 florian
  1655. * reinstated Jonas' bugfix
  1656. Revision 1.9 1998/08/25 16:58:59 pierre
  1657. * removed a line that add no sense and
  1658. introduce garbage in the asmlist
  1659. (uninitialized data !)
  1660. Revision 1.7 1998/08/19 16:07:53 jonas
  1661. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1662. Revision 1.6 1998/08/10 14:50:14 peter
  1663. + localswitches, moduleswitches, globalswitches splitting
  1664. Revision 1.5 1998/08/06 19:40:28 jonas
  1665. * removed $ before and after Log in comment
  1666. Revision 1.4 1998/08/05 16:27:17 jonas
  1667. * fstp/fld bugfix (fstt does not exist)
  1668. Revision 1.3 1998/08/05 16:00:15 florian
  1669. * some fixes for ansi strings
  1670. * log to Log changed
  1671. }