popt386.pas 71 KB

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