popt386.pas 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  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 < ClassP6) 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 <= Class386)
  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 <= Class386) 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 <= Class386)
  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. (*
  585. If GetNextInstruction(hp1, hp2) And
  586. (Pai(hp2)^.typ = ait_labeled_instruction) And
  587. (TRegister(Pai386(p)^.op2) <> R_ESI)
  588. Then
  589. {change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  590. "test %reg1, %reg1; jxx"}
  591. Begin
  592. Pai386(hp1)^.op1 := Pai386(p)^.op1;
  593. Pai386(hp1)^.op2 := Pai386(p)^.op1;
  594. AsmL^.Remove(p);
  595. Dispose(p, done);
  596. p := hp1;
  597. continue
  598. End
  599. Else
  600. *)
  601. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  602. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  603. Begin
  604. Pai386(hp1)^.op1 := Pai386(p)^.op1;
  605. Pai386(hp1)^.op2 := Pai386(p)^.op1;
  606. End;
  607. End
  608. { Else
  609. If (Pai386(p^.next)^._operator
  610. In [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  611. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  612. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  613. End
  614. Else
  615. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  616. x >= RetOffset) as it doesn't do anything (it writes either to a
  617. parameter or to the temporary storage room for the function
  618. result)}
  619. If GetNextInstruction(p, hp1) And
  620. (Pai(hp1)^.typ = ait_instruction)
  621. Then
  622. If ((Pai386(hp1)^._operator = A_LEAVE) Or
  623. (Pai386(hp1)^._operator = A_RET)) And
  624. (Pai386(p)^.op2t = top_ref) And
  625. (TReference(Pai386(p)^.op2^).base = ProcInfo.FramePointer) And
  626. (TReference(Pai386(p)^.op2^).offset >= ProcInfo.RetOffset) And
  627. (TReference(Pai386(p)^.op2^).index = R_NO) And
  628. (Pai386(p)^.op1t = top_reg)
  629. Then
  630. Begin
  631. AsmL^.Remove(p);
  632. Dispose(p, done);
  633. p := hp1;
  634. End
  635. Else
  636. If (Pai386(p)^.op1t = top_reg) And
  637. (Pai386(p)^.op2t = top_ref) And
  638. (Pai386(p)^.Size = Pai386(hp1)^.Size) And
  639. (Pai386(hp1)^._operator = A_CMP) And
  640. (Pai386(hp1)^.op2t = top_ref) And
  641. RefsEqual(TReference(Pai386(p)^.op2^),
  642. TReference(Pai386(hp1)^.op2^))
  643. Then
  644. {change "mov reg, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  645. Begin
  646. Dispose(PReference(Pai386(hp1)^.op2));
  647. Pai386(hp1)^.opxt := Pai386(hp1)^.op1t + (top_reg shl 4);
  648. Pai386(hp1)^.op2 := Pai386(p)^.op1
  649. End;
  650. { Next instruction is also a MOV ? }
  651. If GetNextInstruction(p, hp1) And
  652. (pai(hp1)^.typ = ait_instruction) and
  653. (Pai386(hp1)^._operator = A_MOV)
  654. Then
  655. Begin
  656. If (Pai386(hp1)^.op1t = Pai386(p)^.op2t) and
  657. (Pai386(hp1)^.op2t = Pai386(p)^.op1t)
  658. Then
  659. {mov reg1, mem1 or mov mem1, reg1
  660. mov mem2, reg2 mov reg2, mem2}
  661. Begin
  662. If (Pai386(hp1)^.op2t = top_ref)
  663. Then
  664. TmpBool1 := RefsEqual(TReference(Pai386(hp1)^.op2^), TReference(Pai386(p)^.op1^))
  665. Else
  666. TmpBool1 := Pai386(hp1)^.op2 = Pai386(p)^.op1;
  667. If TmpBool1
  668. Then
  669. {mov reg1, mem1 or mov mem1, reg1
  670. mov mem2, reg1 mov reg2, mem1}
  671. Begin
  672. If (Pai386(hp1)^.op1t = top_ref)
  673. Then
  674. TmpBool1 := RefsEqual(TReference(Pai386(hp1)^.op1^),
  675. TReference(Pai386(p)^.op2^))
  676. Else TmpBool1 := (Pai386(hp1)^.op1 = Pai386(p)^.op2);
  677. If TmpBool1 Then
  678. { Removes the second statement from
  679. mov reg1, mem1
  680. mov mem1, reg1 }
  681. Begin
  682. AsmL^.remove(hp1);
  683. Dispose(hp1,done);
  684. End;
  685. End
  686. Else
  687. Begin
  688. If GetNextInstruction(hp1, hp2) And
  689. (Pai386(p)^.op1t = top_ref) And
  690. (Pai386(p)^.op2t = top_reg) And
  691. (Pai386(hp1)^.op1t = top_reg) And
  692. (Pai386(hp1)^.op1 = Pai386(p)^.op2) And
  693. (Pai386(hp1)^.op2t = top_ref) And
  694. (Pai(hp2)^.typ = ait_instruction) And
  695. (Pai386(hp2)^._operator = A_MOV) And
  696. (Pai386(hp2)^.op2t = top_reg) And
  697. (Pai386(hp2)^.op1t = top_ref) And
  698. RefsEqual(TReference(Pai386(hp2)^.op1^),
  699. TReference(Pai386(hp1)^.op2^))
  700. Then
  701. If (TRegister(Pai386(p)^.op2) = R_EDI)
  702. Then
  703. { mov mem1, reg1
  704. mov reg1, mem2
  705. mov mem2, reg2
  706. to:
  707. mov mem1, reg2
  708. mov reg2, mem2}
  709. Begin
  710. Pai386(p)^.op2 := Pai386(hp2)^.op2;
  711. Pai386(hp1)^.op1 := Pai386(hp2)^.op2;
  712. AsmL^.Remove(hp2);
  713. Dispose(hp2,Done);
  714. End
  715. Else
  716. { mov mem1, reg1
  717. mov reg1, mem2
  718. mov mem2, reg2
  719. to:
  720. mov mem1, reg1
  721. mov mem1, reg2
  722. mov reg1, mem2}
  723. Begin
  724. Pai386(hp1)^.opxt := top_ref + top_reg shl 4;
  725. Pai386(hp1)^.op1 := Pai386(hp1)^.op2; {move the treference}
  726. TReference(Pai386(hp1)^.op1^) := TReference(Pai386(p)^.op1^);
  727. If Assigned(TReference(Pai386(p)^.op1^).Symbol) Then
  728. Begin
  729. New(TReference(Pai386(hp1)^.op1^).Symbol);
  730. TReference(Pai386(hp1)^.op1^).Symbol^ :=
  731. TReference(Pai386(p)^.op1^).Symbol^;
  732. End;
  733. Pai386(hp1)^.op2 := Pai386(hp2)^.op2;
  734. Pai386(hp2)^.opxt := top_reg + top_ref shl 4;
  735. Pai386(hp2)^.op2 := Pai386(hp2)^.op1;
  736. Pai386(hp2)^.op1 := Pai386(p)^.op2;
  737. End;
  738. End;
  739. End
  740. Else
  741. (* {movl [mem1],reg1
  742. movl [mem1],reg2
  743. to:
  744. movl [mem1],reg1
  745. movl reg1,reg2 }
  746. If (Pai386(p)^.op1t = top_ref) and
  747. (Pai386(p)^.op2t = top_reg) and
  748. (Pai386(hp1)^.op1t = top_ref) and
  749. (Pai386(hp1)^.op2t = top_reg) and
  750. (Pai386(p)^.size = Pai386(hp1)^.size) and
  751. RefsEqual(TReference(Pai386(p)^.op1^),TReference(Pai386(hp1)^.op1^)) and
  752. (TRegister(Pai386(p)^.op2)<>TReference(Pai386(hp1)^.op1^).base) and
  753. (TRegister(Pai386(p)^.op2)<>TReference(Pai386(hp1)^.op1^).index) then
  754. Begin
  755. Dispose(PReference(Pai386(hp1)^.op1));
  756. Pai386(hp1)^.op1:=Pai386(p)^.op2;
  757. Pai386(hp1)^.opxt:=Top_reg+Top_reg shl 4;
  758. End
  759. Else*)
  760. { movl const1,[mem1]
  761. movl [mem1],reg1
  762. to:
  763. movl const1,reg1
  764. movl reg1,[mem1] }
  765. If (Pai386(p)^.op1t = top_const) and
  766. (Pai386(p)^.op2t = top_ref) and
  767. (Pai386(hp1)^.op1t = top_ref) and
  768. (Pai386(hp1)^.op2t = top_reg) and
  769. (Pai386(p)^.size = Pai386(hp1)^.size) and
  770. RefsEqual(TReference(Pai386(hp1)^.op1^),TReference(Pai386(p)^.op2^)) then
  771. Begin
  772. Pai386(hp1)^.op1:=Pai386(hp1)^.op2;
  773. Pai386(hp1)^.op2:=Pai386(p)^.op2;
  774. Pai386(hp1)^.opxt:=Top_reg+Top_ref shl 4;
  775. Pai386(p)^.op2:=Pai386(hp1)^.op1;
  776. Pai386(p)^.opxt:=Top_const+(top_reg shl 4);
  777. End
  778. End;
  779. {changes "mov $0, %reg" into "xor %reg, %reg"}
  780. If (Pai386(p)^.op1t = Top_Const) And
  781. (Pai386(p)^.op1 = Pointer(0)) And
  782. (Pai386(p)^.op2t = Top_Reg)
  783. Then
  784. Begin
  785. Pai386(p)^._operator := A_XOR;
  786. Pai386(p)^.opxt := Top_Reg+Top_reg shl 4;
  787. Pai386(p)^.op1 := Pai386(p)^.op2;
  788. End;
  789. End;
  790. A_MOVZX:
  791. Begin
  792. {removes superfluous And's after movzx's}
  793. If (Pai386(p)^.op2t = top_reg) And
  794. GetNextInstruction(p, hp1) And
  795. (Pai(hp1)^.typ = ait_instruction) And
  796. (Pai386(hp1)^._operator = A_AND) And
  797. (Pai386(hp1)^.op1t = top_const) And
  798. (Pai386(hp1)^.op2t = top_reg) And
  799. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  800. Then
  801. Case Pai386(p)^.Size Of
  802. S_BL, S_BW:
  803. If (Longint(Pai386(hp1)^.op1) = $ff)
  804. Then
  805. Begin
  806. AsmL^.Remove(hp1);
  807. Dispose(hp1, Done);
  808. End;
  809. S_WL:
  810. If (Longint(Pai386(hp1)^.op1) = $ffff)
  811. Then
  812. Begin
  813. AsmL^.Remove(hp1);
  814. Dispose(hp1, Done);
  815. End;
  816. End;
  817. {changes some movzx constructs to faster synonims (all examples
  818. are given with eax/ax, but are also valid for other registers)}
  819. If (Pai386(p)^.op2t = top_reg) Then
  820. If (Pai386(p)^.op1t = top_reg)
  821. Then
  822. Case Pai386(p)^.size of
  823. S_BW:
  824. Begin
  825. If (TRegister(Pai386(p)^.op1) = Reg16ToReg8(TRegister(Pai386(p)^.op2))) And
  826. Not(CS_LittleSize In aktglobalswitches)
  827. Then
  828. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  829. Begin
  830. Pai386(p)^._operator := A_AND;
  831. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  832. Longint(Pai386(p)^.op1) := $ff;
  833. Pai386(p)^.Size := S_W
  834. End
  835. Else
  836. If GetNextInstruction(p, hp1) And
  837. (Pai(hp1)^.typ = ait_instruction) And
  838. (Pai386(hp1)^._operator = A_AND) And
  839. (Pai386(hp1)^.op1t = top_const) And
  840. (Pai386(hp1)^.op2t = top_reg) And
  841. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  842. Then
  843. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  844. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  845. Begin
  846. Pai386(p)^._operator := A_MOV;
  847. Pai386(p)^.Size := S_W;
  848. Pai386(p)^.op1 := Pointer(Reg8ToReg16(TRegister(Pai386(p)^.op1)));
  849. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ff);
  850. End;
  851. End;
  852. S_BL:
  853. Begin
  854. If (TRegister(Pai386(p)^.op1) = Reg32ToReg8(TRegister(Pai386(p)^.op2))) And
  855. Not(CS_LittleSize in aktglobalswitches)
  856. Then
  857. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  858. Begin
  859. Pai386(p)^._operator := A_AND;
  860. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  861. Longint(Pai386(p)^.op1) := $ff;
  862. Pai386(p)^.Size := S_L;
  863. End
  864. Else
  865. If GetNextInstruction(p, hp1) And
  866. (Pai(hp1)^.typ = ait_instruction) And
  867. (Pai386(hp1)^._operator = A_AND) And
  868. (Pai386(hp1)^.op1t = top_const) And
  869. (Pai386(hp1)^.op2t = top_reg) And
  870. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  871. Then
  872. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  873. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  874. Begin
  875. Pai386(p)^._operator := A_MOV;
  876. Pai386(p)^.Size := S_L;
  877. Pai386(p)^.op1 := Pointer(Reg8ToReg32(TRegister(Pai386(p)^.op1)));
  878. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ff);
  879. End
  880. End;
  881. S_WL:
  882. Begin
  883. If (TRegister(Pai386(p)^.op1) = Reg32ToReg16(TRegister(Pai386(p)^.op2))) And
  884. Not(CS_LittleSize In aktglobalswitches)
  885. Then
  886. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  887. Begin
  888. Pai386(p)^._operator := A_AND;
  889. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  890. Longint(Pai386(p)^.op1) := $ffff;
  891. Pai386(p)^.Size := S_L
  892. End
  893. Else
  894. If GetNextInstruction(p, hp1) And
  895. (Pai(hp1)^.typ = ait_instruction) And
  896. (Pai386(hp1)^._operator = A_AND) And
  897. (Pai386(hp1)^.op1t = top_const) And
  898. (Pai386(hp1)^.op2t = top_reg) And
  899. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  900. Then
  901. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  902. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  903. Begin
  904. Pai386(p)^._operator := A_MOV;
  905. Pai386(p)^.Size := S_L;
  906. Pai386(p)^.op1 := Pointer(Reg16ToReg32(TRegister(Pai386(p)^.op1)));
  907. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ffff);
  908. End;
  909. End;
  910. End
  911. Else
  912. If (Pai386(p)^.op1t = top_ref) Then
  913. Begin
  914. If GetNextInstruction(p, hp1) And
  915. (Pai(hp1)^.typ = ait_instruction) And
  916. (Pai386(hp1)^._operator = A_AND) And
  917. (Pai386(hp1)^.op1t = Top_Const) And
  918. (Pai386(hp1)^.op2t = Top_Reg) And
  919. (Pai386(hp1)^.op2 = Pai386(p)^.op2) Then
  920. Begin
  921. Pai386(p)^._operator := A_MOV;
  922. Case Pai386(p)^.Size Of
  923. S_BL:
  924. Begin
  925. Pai386(p)^.Size := S_L;
  926. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  927. And $ff);
  928. End;
  929. S_WL:
  930. Begin
  931. Pai386(p)^.Size := S_L;
  932. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  933. And $ffff);
  934. End;
  935. S_BW:
  936. Begin
  937. Pai386(p)^.Size := S_W;
  938. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  939. And $ff);
  940. End;
  941. End;
  942. End;
  943. End;
  944. End;
  945. A_POP:
  946. Begin
  947. if (Pai386(p)^.op1t = top_reg) And
  948. GetNextInstruction(p, hp1) And
  949. (pai(hp1)^.typ=ait_instruction) and
  950. (Pai386(hp1)^._operator=A_PUSH) and
  951. (Pai386(hp1)^.op1t = top_reg) And
  952. (Pai386(hp1)^.op1=Pai386(p)^.op1) then
  953. If (Not(cs_regalloc in aktglobalswitches)) Then
  954. Begin
  955. hp2:=pai(hp1^.next);
  956. asml^.remove(p);
  957. asml^.remove(hp1);
  958. dispose(p,done);
  959. dispose(hp1,done);
  960. p:=hp2;
  961. continue
  962. End
  963. Else
  964. Begin
  965. Pai386(p)^._operator := A_MOV;
  966. Pai386(p)^.op2 := Pai386(p)^.op1;
  967. Pai386(p)^.opxt := top_ref + top_reg shl 4;
  968. New(TmpRef);
  969. TmpRef^.segment := R_DEFAULT_SEG;
  970. TmpRef^.base := R_ESP;
  971. TmpRef^.index := R_NO;
  972. TmpRef^.scalefactor := 1;
  973. TmpRef^.symbol := nil;
  974. TmpRef^.isintvalue := false;
  975. TmpRef^.offset := 0;
  976. Pai386(p)^.op1 := Pointer(TmpRef);
  977. hp1 := Pai(p^.next);
  978. AsmL^.Remove(hp1);
  979. Dispose(hp1, Done)
  980. End
  981. end;
  982. A_PUSH:
  983. Begin
  984. If (Pai386(p)^.size = S_W) And
  985. (Pai386(p)^.op1t = Top_Const) And
  986. GetNextInstruction(p, hp1) And
  987. (Pai(hp1)^.typ = ait_instruction) And
  988. (Pai386(hp1)^._operator = A_PUSH) And
  989. (Pai386(hp1)^.op1t = Top_Const) And
  990. (Pai386(hp1)^.size = S_W) Then
  991. Begin
  992. Pai386(p)^.Size := S_L;
  993. Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) shl 16 + Longint(Pai386(hp1)^.op1));
  994. AsmL^.Remove(hp1);
  995. Dispose(hp1, Done)
  996. End;
  997. End;
  998. A_SHL, A_SAL:
  999. Begin
  1000. If (Pai386(p)^.op1t = Top_Const) And
  1001. (Pai386(p)^.op2t = Top_Reg) And
  1002. (Pai386(p)^.Size = S_L) And
  1003. (Longint(Pai386(p)^.op1) <= 3)
  1004. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1005. Then
  1006. Begin
  1007. TmpBool1 := True; {should we check the next instruction?}
  1008. TmpBool2 := False; {have we found an add/sub which could be
  1009. integrated in the lea?}
  1010. New(TmpRef);
  1011. TmpRef^.segment := R_DEFAULT_SEG;
  1012. TmpRef^.base := R_NO;
  1013. TmpRef^.index := TRegister(Pai386(p)^.op2);
  1014. TmpRef^.scalefactor := PowerOf2(Longint(Pai386(p)^.op1));
  1015. TmpRef^.symbol := nil;
  1016. TmpRef^.isintvalue := false;
  1017. TmpRef^.offset := 0;
  1018. While TmpBool1 And
  1019. GetNextInstruction(p, hp1) And
  1020. (Pai(hp1)^.typ = ait_instruction) And
  1021. ((Pai386(hp1)^._operator = A_ADD) Or
  1022. (Pai386(hp1)^._operator = A_SUB)) And
  1023. (Pai386(hp1)^.op2t = Top_Reg) And
  1024. (Pai386(hp1)^.op2 = Pai386(p)^.op2) Do
  1025. Begin
  1026. TmpBool1 := False;
  1027. If (Pai386(hp1)^.op1t = Top_Const)
  1028. Then
  1029. Begin
  1030. TmpBool1 := True;
  1031. TmpBool2 := True;
  1032. If Pai386(hp1)^._operator = A_ADD
  1033. Then Inc(TmpRef^.offset, Longint(Pai386(hp1)^.op1))
  1034. Else Dec(TmpRef^.offset, Longint(Pai386(hp1)^.op1));
  1035. AsmL^.Remove(hp1);
  1036. Dispose(hp1, Done);
  1037. End
  1038. Else
  1039. If (Pai386(hp1)^.op1t = Top_Reg) And
  1040. (Pai386(hp1)^._operator = A_ADD) And
  1041. (TmpRef^.base = R_NO) Then
  1042. Begin
  1043. TmpBool1 := True;
  1044. TmpBool2 := True;
  1045. TmpRef^.base := TRegister(Pai386(hp1)^.op1);
  1046. AsmL^.Remove(hp1);
  1047. Dispose(hp1, Done);
  1048. End;
  1049. End;
  1050. If TmpBool2 Or
  1051. ((aktoptprocessor < ClassP6) And
  1052. (Longint(Pai386(p)^.op1) <= 3) And
  1053. Not(CS_LittleSize in aktglobalswitches))
  1054. Then
  1055. Begin
  1056. If Not(TmpBool2) And
  1057. (Longint(Pai386(p)^.op1) = 1)
  1058. Then
  1059. Begin
  1060. Dispose(TmpRef);
  1061. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.Size,
  1062. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)))
  1063. End
  1064. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  1065. TRegister(Pai386(p)^.op2)));
  1066. hp1^.fileinfo := p^.fileinfo;
  1067. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1068. Dispose(p, Done);
  1069. p := hp1;
  1070. End;
  1071. End
  1072. Else
  1073. If (aktoptprocessor < ClassP6) And
  1074. (Pai386(p)^.op1t = top_const) And
  1075. (Pai386(p)^.op2t = top_reg) Then
  1076. If (Longint(Pai386(p)^.op1) = 1)
  1077. Then
  1078. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1079. but faster on a 486, and pairable in both U and V pipes on the Pentium
  1080. (unlike shl, which is only pairable in the U pipe)}
  1081. Begin
  1082. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.Size,
  1083. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)));
  1084. hp1^.fileinfo := p^.fileinfo;
  1085. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1086. Dispose(p, done);
  1087. p := hp1;
  1088. End
  1089. Else If (Pai386(p)^.size = S_L) and
  1090. (Longint(Pai386(p)^.op1) <= 3) Then
  1091. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1092. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1093. Begin
  1094. New(TmpRef);
  1095. TmpRef^.segment := R_DEFAULT_SEG;
  1096. TmpRef^.base := R_NO;
  1097. TmpRef^.index := TRegister(Pai386(p)^.op2);
  1098. TmpRef^.scalefactor := PowerOf2(Longint(Pai386(p)^.op1));
  1099. TmpRef^.symbol := nil;
  1100. TmpRef^.isintvalue := false;
  1101. TmpRef^.offset := 0;
  1102. hp1 := new(Pai386,op_ref_reg(A_LEA,S_L,TmpRef, TRegister(Pai386(p)^.op2)));
  1103. hp1^.fileinfo := p^.fileinfo;
  1104. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1105. Dispose(p, done);
  1106. p := hp1;
  1107. End
  1108. End;
  1109. A_SAR, A_SHR:
  1110. {changes the code sequence
  1111. shr/sar const1, %reg
  1112. shl const2, %reg
  1113. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  1114. Begin
  1115. If GetNextInstruction(p, hp1) And
  1116. (pai(hp1)^.typ = ait_instruction) and
  1117. (Pai386(hp1)^._operator = A_SHL) and
  1118. (Pai386(p)^.op1t = top_const) and
  1119. (Pai386(hp1)^.op1t = top_const)
  1120. Then
  1121. If (Longint(Pai386(p)^.op1) > Longint(Pai386(hp1)^.op1)) And
  1122. (Pai386(p)^.op2t = Top_reg) And
  1123. Not(CS_LittleSize In aktglobalswitches) And
  1124. ((Pai386(p)^.Size = S_B) Or
  1125. (Pai386(p)^.Size = S_L))
  1126. Then
  1127. Begin
  1128. Dec(Longint(Pai386(p)^.op1), Longint(Pai386(hp1)^.op1));
  1129. Pai386(hp1)^._operator := A_And;
  1130. Pai386(hp1)^.op1 := Pointer(PowerOf2(Longint(Pai386(hp1)^.op1))-1);
  1131. If (Pai386(p)^.Size = S_L)
  1132. Then Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffffffff)
  1133. Else Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ff);
  1134. End
  1135. Else
  1136. If (Longint(Pai386(p)^.op1) < Longint(Pai386(hp1)^.op1)) And
  1137. (Pai386(p)^.op2t = Top_reg) And
  1138. Not(CS_LittleSize In aktglobalswitches) And
  1139. ((Pai386(p)^.Size = S_B) Or
  1140. (Pai386(p)^.Size = S_L))
  1141. Then
  1142. Begin
  1143. Dec(Longint(Pai386(hp1)^.op1), Longint(Pai386(p)^.op1));
  1144. Pai386(p)^._operator := A_And;
  1145. Pai386(p)^.op1 := Pointer(PowerOf2(Longint(Pai386(p)^.op1))-1);
  1146. If (Pai386(p)^.Size = S_L)
  1147. Then Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffffffff)
  1148. Else Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ff);
  1149. End
  1150. Else
  1151. Begin
  1152. Pai386(p)^._operator := A_And;
  1153. Pai386(p)^.op1 := Pointer(PowerOf2(Longint(Pai386(p)^.op1))-1);
  1154. Case Pai386(p)^.Size Of
  1155. S_B: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ff);
  1156. S_W: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffff);
  1157. S_L: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor
  1158. $ffffffff);
  1159. End;
  1160. AsmL^.remove(hp1);
  1161. dispose(hp1, done);
  1162. End;
  1163. End;
  1164. A_SUB:
  1165. {change "subl $2, %esp; pushw x" to "pushl x"}
  1166. Begin
  1167. If (Pai386(p)^.op1t = top_const) And
  1168. (Longint(Pai386(p)^.op1) = 2) And
  1169. (Pai386(p)^.op2t = top_reg) And
  1170. (TRegister(Pai386(p)^.op2) = R_ESP)
  1171. Then
  1172. Begin
  1173. hp1 := Pai(p^.next);
  1174. While Assigned(hp1) And
  1175. (Pai(hp1)^.typ In [ait_instruction]+SkipInstr) And
  1176. Not((Pai(hp1)^.typ = ait_instruction) And
  1177. ((Pai386(hp1)^._operator = A_PUSH) or
  1178. ((Pai386(hp1)^._operator = A_MOV) And
  1179. (Pai386(hp1)^.op2t = top_ref) And
  1180. (TReference(Pai386(hp1)^.op2^).base = R_ESP)))) do
  1181. hp1 := Pai(hp1^.next);
  1182. If Assigned(hp1) And
  1183. (Pai(hp1)^.typ = ait_instruction) And
  1184. (Pai386(hp1)^._operator = A_PUSH) And
  1185. (Pai386(hp1)^.Size = S_W)
  1186. Then
  1187. Begin
  1188. Pai386(hp1)^.size := S_L;
  1189. If (Pai386(hp1)^.op1t = top_reg) Then
  1190. Pai386(hp1)^.op1 := Pointer(Reg16ToReg32(TRegister(Pai386(hp1)^.op1)));
  1191. hp1 := Pai(p^.next);
  1192. AsmL^.Remove(p);
  1193. Dispose(p, Done);
  1194. p := hp1;
  1195. Continue
  1196. End
  1197. Else
  1198. If GetLastInstruction(p, hp1) And
  1199. (Pai(hp1)^.typ = ait_instruction) And
  1200. (Pai386(hp1)^._operator = A_SUB) And
  1201. (Pai386(hp1)^.op1t = top_const) And
  1202. (Pai386(hp1)^.op2t = top_reg) And
  1203. (TRegister(Pai386(hp1)^.Op2) = R_ESP)
  1204. Then
  1205. Begin
  1206. Inc(Longint(Pai386(p)^.op1), Longint(Pai386(hp1)^.op1));
  1207. AsmL^.Remove(hp1);
  1208. Dispose(hp1, Done);
  1209. End;
  1210. End;
  1211. End;
  1212. A_TEST, A_OR:
  1213. {removes the line marked with (x) from the sequence
  1214. And/or/xor/add/sub/... $x, %y
  1215. test/or %y, %y (x)
  1216. j(n)z _Label
  1217. as the first instruction already adjusts the ZF}
  1218. Begin
  1219. If (Pai386(p)^.op1 = Pai386(p)^.op2) And
  1220. GetLastInstruction(p, hp1) And
  1221. (pai(hp1)^.typ = ait_instruction) Then
  1222. Case Pai386(hp1)^._operator Of
  1223. A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_SHL, A_SHR:
  1224. Begin
  1225. If (Pai386(hp1)^.op2 = Pai386(p)^.op1) Then
  1226. Begin
  1227. hp1 := pai(p^.next);
  1228. asml^.remove(p);
  1229. dispose(p, done);
  1230. p := pai(hp1);
  1231. continue
  1232. End;
  1233. End;
  1234. A_DEC, A_INC, A_NEG:
  1235. Begin
  1236. If (Pai386(hp1)^.op1 = Pai386(p)^.op1) Then
  1237. Begin
  1238. hp1 := pai(p^.next);
  1239. asml^.remove(p);
  1240. dispose(p, done);
  1241. p := pai(hp1);
  1242. continue
  1243. End;
  1244. End
  1245. End;
  1246. End;
  1247. End;
  1248. End;
  1249. ait_label:
  1250. Begin
  1251. If Not(Pai_Label(p)^.l^.is_used)
  1252. Then
  1253. Begin
  1254. hp1 := Pai(p^.next);
  1255. AsmL^.Remove(p);
  1256. Dispose(p, Done);
  1257. p := hp1;
  1258. Continue
  1259. End;
  1260. End;
  1261. {$ifdef regalloc}
  1262. ait_regalloc: UsedRegs := UsedRegs + [PaiAlloc(p)^.Reg];
  1263. ait_regdealloc: UsedRegs := UsedRegs - [PaiAlloc(p)^.Reg];
  1264. {$endif regalloc}
  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. End;
  1308. End;
  1309. End
  1310. Else
  1311. If (Pai386(p)^.op1t = top_ref) And
  1312. (PReference(Pai386(p)^.op1)^.base <> TRegister(Pai386(p)^.op2)) And
  1313. (PReference(Pai386(p)^.op1)^.index <> TRegister(Pai386(p)^.op2)) And
  1314. Not(CS_LittleSize in aktglobalswitches) And
  1315. IsGP32Reg(TRegister(Pai386(p)^.op2)) And
  1316. (aktoptprocessor = ClassP5) And
  1317. (Pai386(p)^.Size = S_BL)
  1318. Then
  1319. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1320. Pentium and PentiumMMX}
  1321. Begin
  1322. hp1 := New(Pai386,op_reg_reg(A_XOR, S_L, TRegister(Pai386(p)^.op2),
  1323. TRegister(Pai386(p)^.op2)));
  1324. hp1^.fileinfo := p^.fileinfo;
  1325. Pai386(p)^._operator := A_MOV;
  1326. Pai386(p)^.size := S_B;
  1327. Pai386(p)^.op2 := Pointer(Reg32ToReg8(TRegister(Pai386(p)^.op2)));
  1328. InsertLLItem(AsmL,p^.previous, p, hp1);
  1329. End;
  1330. End;
  1331. End;
  1332. End;
  1333. End;
  1334. p := Pai(p^.next)
  1335. End;
  1336. End;
  1337. End.
  1338. {
  1339. $Log$
  1340. Revision 1.11 1998-08-28 10:57:02 peter
  1341. * removed warnings
  1342. Revision 1.10 1998/08/27 15:17:50 florian
  1343. * reinstated Jonas' bugfix
  1344. Revision 1.9 1998/08/25 16:58:59 pierre
  1345. * removed a line that add no sense and
  1346. introduce garbage in the asmlist
  1347. (uninitialized data !)
  1348. Revision 1.7 1998/08/19 16:07:53 jonas
  1349. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1350. Revision 1.6 1998/08/10 14:50:14 peter
  1351. + localswitches, moduleswitches, globalswitches splitting
  1352. Revision 1.5 1998/08/06 19:40:28 jonas
  1353. * removed $ before and after Log in comment
  1354. Revision 1.4 1998/08/05 16:27:17 jonas
  1355. * fstp/fld bugfix (fstt does not exist)
  1356. Revision 1.3 1998/08/05 16:00:15 florian
  1357. * some fixes for ansi strings
  1358. * log to Log changed
  1359. }