popt386.pas 88 KB

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