popt386.pas 70 KB

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