popt386.pas 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl and Jonas Maebe
  4. This unit contains the peephole optimizer.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit POpt386;
  19. Interface
  20. Uses Aasm;
  21. Procedure PeepHoleOptPass1(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  22. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  23. Implementation
  24. Uses
  25. globtype,systems,
  26. globals,verbose,hcodegen,
  27. i386base,i386asm,
  28. DAOpt386;
  29. Function RegUsedAfterInstruction(Reg: TRegister; p: Pai; Var UsedRegs: TRegSet): Boolean;
  30. Begin
  31. UpdateUsedRegs(UsedRegs, Pai(p^.Next));
  32. RegUsedAfterInstruction := Reg in UsedRegs
  33. End;
  34. Procedure PeepHoleOptPass1(Asml: PAasmOutput; BlockStart, BlockEnd: Pai);
  35. {First pass of peepholeoptimizations}
  36. Var
  37. l : longint;
  38. p ,hp1, hp2: pai;
  39. TmpBool1, TmpBool2: Boolean;
  40. TmpRef: PReference;
  41. UsedRegs, TmpUsedRegs: TRegSet;
  42. Procedure GetFinalDestination(hp: pai386);
  43. {traces sucessive jumps to their final destination and sets it, e.g.
  44. je l1 je l3
  45. <code> <code>
  46. l1: becomes l1:
  47. je l2 je l3
  48. <code> <code>
  49. l2: l2:
  50. jmp l3 jmp l3}
  51. Var p1: pai;
  52. Function SkipLabels(hp: Pai): Pai;
  53. {skips all labels and returns the next "real" instruction; it is
  54. assumed that hp is of the type ait_label}
  55. Begin
  56. While assigned(hp^.next) and
  57. (pai(hp^.next)^.typ In SkipInstr + [ait_label]) Do
  58. hp := pai(hp^.next);
  59. If assigned(hp^.next)
  60. Then SkipLabels := pai(hp^.next)
  61. Else SkipLabels := hp;
  62. End;
  63. Begin
  64. If (pasmlabel(hp^.oper[0].sym)^.labelnr >= LoLab) and
  65. (pasmlabel(hp^.oper[0].sym)^.labelnr <= HiLab) and {range check, a jump can go past an assembler block!}
  66. Assigned(LTable^[pasmlabel(hp^.oper[0].sym)^.labelnr-LoLab].PaiObj) Then
  67. Begin
  68. p1 := LTable^[pasmlabel(hp^.oper[0].sym)^.labelnr-LoLab].PaiObj; {the jump's destination}
  69. p1 := SkipLabels(p1);
  70. If (pai(p1)^.typ = ait_instruction) and
  71. (pai386(p1)^.is_jmp) and
  72. (pai386(p1)^.condition = hp^.condition) Then
  73. Begin
  74. GetFinalDestination(pai386(p1));
  75. Dec(pasmlabel(hp^.oper[0].sym)^.refs);
  76. hp^.oper[0].sym:=pai386(p1)^.oper[0].sym;
  77. inc(pasmlabel(hp^.oper[0].sym)^.refs);
  78. End;
  79. End;
  80. End;
  81. Begin
  82. P := BlockStart;
  83. UsedRegs := [];
  84. While (P <> BlockEnd) Do
  85. Begin
  86. UpDateUsedRegs(UsedRegs, Pai(p^.next));
  87. Case P^.Typ Of
  88. ait_instruction:
  89. Begin
  90. { Handle Jmp Optimizations }
  91. if Pai386(p)^.is_jmp then
  92. begin
  93. {the following if-block removes all code between a jmp and the next label,
  94. because it can never be executed}
  95. If (pai386(p)^.opcode = A_JMP) Then
  96. Begin
  97. hp1 := pai(p^.next);
  98. While GetNextInstruction(p, hp1) and
  99. ((hp1^.typ <> ait_label) or
  100. { skip unused labels, they're not referenced anywhere }
  101. Not(Pai_Label(hp1)^.l^.is_used)) Do
  102. If (hp1^.typ <> ait_label) Then
  103. Begin
  104. AsmL^.Remove(hp1);
  105. Dispose(hp1, done);
  106. End;
  107. End;
  108. If GetNextInstruction(p, hp1) then
  109. Begin
  110. If (pai(hp1)^.typ=ait_instruction) and
  111. (pai386(hp1)^.opcode=A_JMP) and
  112. GetNextInstruction(hp1, hp2) And
  113. FindLabel(PAsmLabel(pai386(p)^.oper[0].sym), hp2)
  114. Then
  115. Begin
  116. if pai386(p)^.opcode=A_Jcc then
  117. pai386(p)^.condition:=inverse_cond[pai386(p)^.condition]
  118. else
  119. begin
  120. If (LabDif <> 0) Then
  121. GetFinalDestination(pai386(p));
  122. p:=pai(p^.next);
  123. continue;
  124. end;
  125. Dec(pai_label(hp2)^.l^.refs);
  126. pai386(p)^.oper[0].sym:=pai386(hp1)^.oper[0].sym;
  127. Inc(pai386(p)^.oper[0].sym^.refs);
  128. asml^.remove(hp1);
  129. dispose(hp1,done);
  130. If (LabDif <> 0) Then
  131. GetFinalDestination(pai386(p));
  132. end
  133. else
  134. if FindLabel(pasmlabel(pai386(p)^.oper[0].sym), hp1) then
  135. Begin
  136. hp2:=pai(hp1^.next);
  137. asml^.remove(p);
  138. dispose(p,done);
  139. p:=hp2;
  140. continue;
  141. end
  142. Else
  143. If (LabDif <> 0) Then
  144. GetFinalDestination(pai386(p));
  145. end
  146. end
  147. else
  148. { All other optimizes }
  149. begin
  150. If (Pai386(p)^.oper[0].typ = top_ref) Then
  151. With Pai386(p)^.oper[0].ref^ Do
  152. Begin
  153. If (base = R_NO) And
  154. (index <> R_NO) And
  155. (scalefactor = 1)
  156. Then
  157. Begin
  158. base := index;
  159. index := R_NO
  160. End
  161. End;
  162. If (Pai386(p)^.oper[1].typ = top_ref) Then
  163. With Pai386(p)^.oper[1].ref^ Do
  164. Begin
  165. If (base = R_NO) And
  166. (index <> R_NO) And
  167. (scalefactor = 1) Then
  168. Begin
  169. base := index;
  170. index := R_NO
  171. End
  172. End;
  173. Case Pai386(p)^.opcode Of
  174. A_AND:
  175. Begin
  176. If (Pai386(p)^.oper[0].typ = top_const) And
  177. (Pai386(p)^.oper[1].typ = top_reg) And
  178. GetNextInstruction(p, hp1) And
  179. (Pai(hp1)^.typ = ait_instruction) And
  180. (Pai386(hp1)^.opcode = A_AND) And
  181. (Pai386(hp1)^.oper[0].typ = top_const) And
  182. (Pai386(hp1)^.oper[1].typ = top_reg) And
  183. (Pai386(hp1)^.oper[1].reg = Pai386(hp1)^.oper[1].reg)
  184. Then
  185. {change "and const1, reg; and const2, reg" to "and (const1 and const2), reg"}
  186. Begin
  187. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val And Pai386(hp1)^.oper[0].val);
  188. AsmL^.Remove(hp1);
  189. Dispose(hp1, Done)
  190. End
  191. Else
  192. {change "and x, reg; jxx" to "test x, reg", if reg is deallocated before the
  193. jump}
  194. If (Pai386(p)^.oper[1].typ = top_reg) And
  195. GetNextInstruction(p, hp1) And
  196. (hp1^.typ = ait_instruction) And
  197. (Pai386(hp1)^.is_jmp) and
  198. Not(Pai386(p)^.oper[1].reg in UsedRegs) Then
  199. Pai386(p)^.opcode := A_TEST;
  200. End;
  201. A_CMP:
  202. Begin
  203. If (Pai386(p)^.oper[0].typ = top_const) And
  204. (Pai386(p)^.oper[1].typ = top_reg) And
  205. (Pai386(p)^.oper[0].val = 0) Then
  206. {change "cmp $0, %reg" to "test %reg, %reg"}
  207. Begin
  208. Pai386(p)^.opcode := A_TEST;
  209. Pai386(p)^.loadreg(0,Pai386(p)^.oper[1].reg);
  210. End;
  211. End;
  212. A_FLD:
  213. Begin
  214. If (Pai386(p)^.oper[0].typ = top_ref) And
  215. GetNextInstruction(p, hp2) And
  216. (hp2^.typ = Ait_Instruction) And
  217. (Pai386(hp2)^.oper[0].typ = top_reg) And
  218. (Pai386(hp2)^.oper[1].typ = top_reg) And
  219. (Pai386(p)^.opsize in [S_FS, S_FL]) And
  220. (Pai386(hp2)^.oper[0].reg = R_ST) And
  221. (Pai386(hp2)^.oper[1].reg = R_ST1) Then
  222. If GetLastInstruction(p, hp1) And
  223. (hp1^.typ = Ait_Instruction) And
  224. ((Pai386(hp1)^.opcode = A_FLD) Or
  225. (Pai386(hp1)^.opcode = A_FST)) And
  226. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  227. (Pai386(hp1)^.oper[0].typ = top_ref) And
  228. RefsEqual(Pai386(p)^.oper[0].ref^, Pai386(hp1)^.oper[0].ref^) Then
  229. If ((Pai386(hp2)^.opcode = A_FMULP) Or
  230. (Pai386(hp2)^.opcode = A_FADDP)) Then
  231. { change to
  232. fld/fst mem1 (hp1) fld/fst mem1
  233. fld mem1 (p) fadd/
  234. faddp/ fmul st, st
  235. fmulp st, st1 (hp2) }
  236. Begin
  237. AsmL^.Remove(p);
  238. Dispose(p, Done);
  239. p := hp1;
  240. If (Pai386(hp2)^.opcode = A_FADDP) Then
  241. Pai386(hp2)^.opcode := A_FADD
  242. Else
  243. Pai386(hp2)^.opcode := A_FMUL;
  244. Pai386(hp2)^.oper[1].reg := R_ST;
  245. End
  246. Else
  247. { change to
  248. fld/fst mem1 (hp1) fld/fst mem1
  249. fld mem1 (p) fld st}
  250. Begin
  251. Pai386(p)^.changeopsize(S_FL);
  252. Pai386(p)^.loadreg(0,R_ST);
  253. End
  254. Else
  255. Begin
  256. Case Pai386(hp2)^.opcode Of
  257. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  258. { change to
  259. fld/fst mem1 (hp1) fld/fst mem1
  260. fld mem2 (p) fxxx mem2
  261. fxxxp st, st1 (hp2) }
  262. Begin
  263. Case Pai386(hp2)^.opcode Of
  264. A_FADDP: Pai386(p)^.opcode := A_FADD;
  265. A_FMULP: Pai386(p)^.opcode := A_FMUL;
  266. A_FSUBP: Pai386(p)^.opcode := A_FSUBR;
  267. A_FSUBRP: Pai386(p)^.opcode := A_FSUB;
  268. A_FDIVP: Pai386(p)^.opcode := A_FDIVR;
  269. A_FDIVRP: Pai386(p)^.opcode := A_FDIV;
  270. End;
  271. AsmL^.Remove(hp2);
  272. Dispose(hp2, Done)
  273. End
  274. End
  275. End
  276. End;
  277. A_FSTP,A_FISTP:
  278. Begin
  279. If (Pai386(p)^.oper[0].typ = top_ref) And
  280. GetNextInstruction(p, hp1) And
  281. (Pai(hp1)^.typ = ait_instruction) And
  282. (((Pai386(hp1)^.opcode = A_FLD) And
  283. (Pai386(p)^.opcode = A_FSTP)) Or
  284. ((Pai386(p)^.opcode = A_FISTP) And
  285. (Pai386(hp1)^.opcode = A_FILD))) And
  286. (Pai386(hp1)^.oper[0].typ = top_ref) And
  287. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  288. RefsEqual(Pai386(p)^.oper[0].ref^, Pai386(hp1)^.oper[0].ref^)
  289. Then
  290. Begin
  291. If GetNextInstruction(hp1, hp2) And
  292. (hp2^.typ = ait_instruction) And
  293. ((Pai386(hp2)^.opcode = A_LEAVE) Or
  294. (Pai386(hp2)^.opcode = A_RET)) And
  295. (Pai386(p)^.oper[0].ref^.Base = ProcInfo.FramePointer) And
  296. (Pai386(p)^.oper[0].ref^.Offset >= ProcInfo.RetOffset) And
  297. (Pai386(p)^.oper[0].ref^.Index = R_NO)
  298. Then
  299. Begin
  300. AsmL^.Remove(p);
  301. AsmL^.Remove(hp1);
  302. Dispose(p, Done);
  303. Dispose(hp1, Done);
  304. p := hp2;
  305. Continue
  306. End
  307. Else
  308. {fst can't store an extended value!}
  309. If (Pai386(p)^.opsize <> S_FX) And
  310. (Pai386(p)^.opsize <> S_IQ) Then
  311. Begin
  312. If (Pai386(p)^.opcode = A_FSTP) Then
  313. Pai386(p)^.opcode := A_FST
  314. Else Pai386(p)^.opcode := A_FIST;
  315. AsmL^.Remove(hp1);
  316. Dispose(hp1, done)
  317. End
  318. End;
  319. End;
  320. A_IMUL:
  321. {changes certain "imul const, %reg"'s to lea sequences}
  322. Begin
  323. If (Pai386(p)^.oper[0].typ = Top_Const) And
  324. (Pai386(p)^.oper[1].typ = Top_Reg) And
  325. (Pai386(p)^.opsize = S_L) Then
  326. If (Pai386(p)^.oper[0].val = 1) Then
  327. If (Pai386(p)^.oper[2].typ = Top_None) Then
  328. {remove "imul $1, reg"}
  329. Begin
  330. hp1 := Pai(p^.Next);
  331. AsmL^.Remove(p);
  332. Dispose(p, Done);
  333. p := hp1;
  334. Continue;
  335. End
  336. Else
  337. {change "imul $1, reg1, reg2" to "mov reg1, reg2"}
  338. Begin
  339. hp1 := New(Pai386, Op_Reg_Reg(A_MOV, S_L, Pai386(p)^.oper[1].reg,Pai386(p)^.oper[2].reg));
  340. hp1^.fileinfo := p^.fileinfo;
  341. InsertLLItem(AsmL, p^.previous, p^.next, hp1);
  342. Dispose(p, Done);
  343. p := hp1;
  344. End
  345. Else If
  346. ((Pai386(p)^.oper[2].typ = Top_Reg) or
  347. (Pai386(p)^.oper[2].typ = Top_None)) And
  348. (aktoptprocessor < ClassP6) And
  349. (Pai386(p)^.oper[0].val <= 12) And
  350. Not(CS_LittleSize in aktglobalswitches) And
  351. (Not(GetNextInstruction(p, hp1)) Or
  352. {GetNextInstruction(p, hp1) And}
  353. Not((Pai(hp1)^.typ = ait_instruction) And
  354. ((pai386(hp1)^.opcode=A_Jcc) and (pai386(hp1)^.condition in [C_O,C_NO]))))
  355. Then
  356. Begin
  357. New(TmpRef);
  358. Reset_reference(tmpref^);
  359. Case Pai386(p)^.oper[0].val Of
  360. 3: Begin
  361. {imul 3, reg1, reg2 to
  362. lea (reg1,reg1,2), reg2
  363. imul 3, reg1 to
  364. lea (reg1,reg1,2), reg1}
  365. TmpRef^.base := Pai386(p)^.oper[1].reg;
  366. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  367. TmpRef^.ScaleFactor := 2;
  368. If (Pai386(p)^.oper[2].typ = Top_None) Then
  369. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg))
  370. Else
  371. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  372. hp1^.fileinfo := p^.fileinfo;
  373. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  374. Dispose(p, Done);
  375. p := hp1;
  376. End;
  377. 5: Begin
  378. {imul 5, reg1, reg2 to
  379. lea (reg1,reg1,4), reg2
  380. imul 5, reg1 to
  381. lea (reg1,reg1,4), reg1}
  382. TmpRef^.base := Pai386(p)^.oper[1].reg;
  383. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  384. TmpRef^.ScaleFactor := 4;
  385. If (Pai386(p)^.oper[2].typ = Top_None) Then
  386. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg))
  387. Else
  388. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  389. hp1^.fileinfo:= p^.fileinfo;
  390. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  391. Dispose(p, Done);
  392. p := hp1;
  393. End;
  394. 6: Begin
  395. {imul 6, reg1, reg2 to
  396. lea (,reg1,2), reg2
  397. lea (reg2,reg1,4), reg2
  398. imul 6, reg1 to
  399. lea (reg1,reg1,2), reg1
  400. add reg1, reg1}
  401. If (aktoptprocessor <= Class386)
  402. Then
  403. Begin
  404. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  405. If (Pai386(p)^.oper[2].typ = Top_Reg)
  406. Then
  407. Begin
  408. TmpRef^.base := Pai386(p)^.oper[2].reg;
  409. TmpRef^.ScaleFactor := 4;
  410. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  411. End
  412. Else
  413. Begin
  414. Dispose(TmpRef);
  415. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  416. Pai386(p)^.oper[1].reg,Pai386(p)^.oper[1].reg));
  417. End;
  418. hp1^.fileinfo := p^.fileinfo;
  419. InsertLLItem(AsmL,p, p^.next, hp1);
  420. New(TmpRef);
  421. Reset_reference(tmpref^);
  422. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  423. TmpRef^.ScaleFactor := 2;
  424. If (Pai386(p)^.oper[2].typ = Top_Reg)
  425. Then
  426. Begin
  427. TmpRef^.base := R_NO;
  428. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  429. Pai386(p)^.oper[2].reg));
  430. End
  431. Else
  432. Begin
  433. TmpRef^.base := Pai386(p)^.oper[1].reg;
  434. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  435. End;
  436. hp1^.fileinfo := p^.fileinfo;
  437. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  438. Dispose(p, Done);
  439. p := Pai(hp1^.next);
  440. End
  441. Else Dispose(TmpRef);
  442. End;
  443. 9: Begin
  444. {imul 9, reg1, reg2 to
  445. lea (reg1,reg1,8), reg2
  446. imul 9, reg1 to
  447. lea (reg1,reg1,8), reg1}
  448. TmpRef^.base := Pai386(p)^.oper[1].reg;
  449. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  450. TmpRef^.ScaleFactor := 8;
  451. If (Pai386(p)^.oper[2].typ = Top_None) Then
  452. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg))
  453. Else
  454. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  455. hp1^.fileinfo := p^.fileinfo;
  456. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  457. Dispose(p, Done);
  458. p := hp1;
  459. End;
  460. 10: Begin
  461. {imul 10, reg1, reg2 to
  462. lea (reg1,reg1,4), reg2
  463. add reg2, reg2
  464. imul 10, reg1 to
  465. lea (reg1,reg1,4), reg1
  466. add reg1, reg1}
  467. If (aktoptprocessor <= Class386) Then
  468. Begin
  469. If (Pai386(p)^.oper[2].typ = Top_Reg) Then
  470. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  471. Pai386(p)^.oper[2].reg,Pai386(p)^.oper[2].reg))
  472. Else
  473. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  474. Pai386(p)^.oper[1].reg,Pai386(p)^.oper[1].reg));
  475. hp1^.fileinfo := p^.fileinfo;
  476. InsertLLItem(AsmL,p, p^.next, hp1);
  477. TmpRef^.base := Pai386(p)^.oper[1].reg;
  478. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  479. TmpRef^.ScaleFactor := 4;
  480. If (Pai386(p)^.oper[2].typ = Top_Reg)
  481. Then
  482. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg))
  483. Else
  484. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  485. hp1^.fileinfo := p^.fileinfo;
  486. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  487. Dispose(p, Done);
  488. p := Pai(hp1^.next);
  489. End
  490. Else Dispose(TmpRef);
  491. End;
  492. 12: Begin
  493. {imul 12, reg1, reg2 to
  494. lea (,reg1,4), reg2
  495. lea (,reg1,8) reg2
  496. imul 12, reg1 to
  497. lea (reg1,reg1,2), reg1
  498. lea (,reg1,4), reg1}
  499. If (aktoptprocessor <= Class386)
  500. Then
  501. Begin
  502. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  503. If (Pai386(p)^.oper[2].typ = Top_Reg) Then
  504. Begin
  505. TmpRef^.base := Pai386(p)^.oper[2].reg;
  506. TmpRef^.ScaleFactor := 8;
  507. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  508. End
  509. Else
  510. Begin
  511. TmpRef^.base := R_NO;
  512. TmpRef^.ScaleFactor := 4;
  513. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  514. End;
  515. hp1^.fileinfo := p^.fileinfo;
  516. InsertLLItem(AsmL,p, p^.next, hp1);
  517. New(TmpRef);
  518. Reset_reference(tmpref^);
  519. TmpRef^.Index := Pai386(p)^.oper[1].reg;
  520. If (Pai386(p)^.oper[2].typ = Top_Reg) Then
  521. Begin
  522. TmpRef^.base := R_NO;
  523. TmpRef^.ScaleFactor := 4;
  524. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[2].reg));
  525. End
  526. Else
  527. Begin
  528. TmpRef^.base := Pai386(p)^.oper[1].reg;
  529. TmpRef^.ScaleFactor := 2;
  530. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, Pai386(p)^.oper[1].reg));
  531. End;
  532. hp1^.fileinfo := p^.fileinfo;
  533. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  534. Dispose(p, Done);
  535. p := Pai(hp1^.next);
  536. End
  537. Else Dispose(TmpRef);
  538. End
  539. Else Dispose(TmpRef);
  540. End;
  541. End;
  542. End;
  543. A_LEA:
  544. Begin
  545. {removes seg register prefixes from LEA operations, as they
  546. don't do anything}
  547. Pai386(p)^.oper[0].ref^.Segment := R_NO;
  548. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  549. If (Pai386(p)^.oper[0].ref^.Base In [R_EAX..R_EDI]) And
  550. (Pai386(p)^.oper[0].ref^.Index = R_NO) And
  551. (Pai386(p)^.oper[0].ref^.Offset = 0) And
  552. (Not(Assigned(Pai386(p)^.oper[0].ref^.Symbol))) Then
  553. If (Pai386(p)^.oper[0].ref^.Base <> Pai386(p)^.oper[1].reg)
  554. Then
  555. Begin
  556. hp1 := New(Pai386, op_reg_reg(A_MOV, S_L,Pai386(p)^.oper[0].ref^.Base,
  557. Pai386(p)^.oper[1].reg));
  558. hp1^.fileinfo := p^.fileinfo;
  559. InsertLLItem(AsmL,p^.previous,p^.next, hp1);
  560. Dispose(p, Done);
  561. p := hp1;
  562. Continue;
  563. End
  564. Else
  565. Begin
  566. hp1 := Pai(p^.Next);
  567. AsmL^.Remove(p);
  568. Dispose(p, Done);
  569. p := hp1;
  570. Continue;
  571. End;
  572. End;
  573. A_MOV:
  574. Begin
  575. TmpUsedRegs := UsedRegs;
  576. If (Pai386(p)^.oper[1].typ = top_reg) And
  577. (Pai386(p)^.oper[1].reg In [R_EAX, R_EBX, R_EDX, R_EDI]) And
  578. GetNextInstruction(p, hp1) And
  579. (Pai(hp1)^.typ = ait_instruction) And
  580. (Pai386(hp1)^.opcode = A_MOV) And
  581. (Pai386(hp1)^.oper[0].typ = top_reg) And
  582. (Pai386(hp1)^.oper[0].reg = Pai386(p)^.oper[1].reg)
  583. Then
  584. {we have "mov x, %treg; mov %treg, y}
  585. If not(RegUsedAfterInstruction(Pai386(p)^.oper[1].reg, hp1, TmpUsedRegs)) then
  586. {we've got "mov x, %treg; mov %treg, y; with %treg is not used after }
  587. Case Pai386(p)^.oper[0].typ Of
  588. top_reg:
  589. Begin
  590. { change "mov %reg, %treg; mov %treg, y"
  591. to "mov %reg, y" }
  592. Pai386(hp1)^.LoadOper(0,Pai386(p)^.oper[0]);
  593. AsmL^.Remove(p);
  594. Dispose(p, Done);
  595. p := hp1;
  596. continue;
  597. End;
  598. top_ref:
  599. If (Pai386(hp1)^.oper[1].typ = top_reg) Then
  600. Begin
  601. { change "mov mem, %treg; mov %treg, %reg"
  602. to "mov mem, %reg" }
  603. Pai386(p)^.Loadoper(1,Pai386(hp1)^.oper[1]);
  604. AsmL^.Remove(hp1);
  605. Dispose(hp1, Done);
  606. continue;
  607. End;
  608. End
  609. Else
  610. {remove an instruction which never makes sense: we've got
  611. "mov mem, %reg1; mov %reg1, %edi" and then EDI isn't used anymore!}
  612. { Begin
  613. If (Pai386(hp1)^.oper[1].reg = R_EDI) And
  614. Not(GetNextInstruction(hp1, hp2) And
  615. (Pai(hp2)^.typ = ait_instruction) And
  616. (Pai386(hp2)^.oper[1].typ = top_reg) And
  617. (Pai386(hp2)^.oper[1] = Pointer(R_ESI))) Then
  618. Begin
  619. AsmL^.Remove(hp1);
  620. Dispose(hp1, Done);
  621. Continue;
  622. End
  623. End}
  624. Else
  625. {Change "mov %reg1, %reg2; xxx %reg2, ???" to
  626. "mov %reg1, %reg2; xxx %reg1, ???" to avoid a write/read
  627. penalty}
  628. If (Pai386(p)^.oper[0].typ = top_reg) And
  629. (Pai386(p)^.oper[1].typ = top_reg) And
  630. GetNextInstruction(p,hp1) And
  631. (Pai(hp1)^.typ = ait_instruction) And
  632. (Pai386(hp1)^.oper[0].typ = top_reg) And
  633. (Pai386(hp1)^.oper[0].reg = Pai386(p)^.oper[1].reg)
  634. Then
  635. {we have "mov %reg1, %reg2; XXX %reg2, ???"}
  636. Begin
  637. If ((Pai386(hp1)^.opcode = A_OR) Or
  638. (Pai386(hp1)^.opcode = A_TEST)) And
  639. (Pai386(hp1)^.oper[1].typ = top_reg) And
  640. (Pai386(hp1)^.oper[0].reg = Pai386(hp1)^.oper[1].reg)
  641. Then
  642. {we have "mov %reg1, %reg2; test/or %reg2, %reg2"}
  643. Begin
  644. TmpUsedRegs := UsedRegs;
  645. If GetNextInstruction(hp1, hp2) And
  646. (hp2^.typ = ait_instruction) And
  647. pai386(hp2)^.is_jmp and
  648. Not(RegUsedAfterInstruction(Pai386(hp1)^.oper[0].reg, hp1, TmpUsedRegs))
  649. Then
  650. {change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  651. "test %reg1, %reg1; jxx"}
  652. Begin
  653. Pai386(hp1)^.Loadoper(0,Pai386(p)^.oper[0]);
  654. Pai386(hp1)^.Loadoper(1,Pai386(p)^.oper[0]);
  655. AsmL^.Remove(p);
  656. Dispose(p, done);
  657. p := hp1;
  658. continue
  659. End
  660. Else
  661. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  662. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  663. Begin
  664. Pai386(hp1)^.Loadoper(0,Pai386(p)^.oper[0]);
  665. Pai386(hp1)^.Loadoper(1,Pai386(p)^.oper[0]);
  666. End;
  667. End
  668. { Else
  669. If (Pai386(p^.next)^.opcode
  670. In [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  671. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  672. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  673. End
  674. Else
  675. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  676. x >= RetOffset) as it doesn't do anything (it writes either to a
  677. parameter or to the temporary storage room for the function
  678. result)}
  679. If GetNextInstruction(p, hp1) And
  680. (Pai(hp1)^.typ = ait_instruction)
  681. Then
  682. If ((Pai386(hp1)^.opcode = A_LEAVE) Or
  683. (Pai386(hp1)^.opcode = A_RET)) And
  684. (Pai386(p)^.oper[1].typ = top_ref) And
  685. (Pai386(p)^.oper[1].ref^.base = ProcInfo.FramePointer) And
  686. (Pai386(p)^.oper[1].ref^.offset >= ProcInfo.RetOffset) And
  687. (Pai386(p)^.oper[1].ref^.index = R_NO) And
  688. (Pai386(p)^.oper[0].typ = top_reg)
  689. Then
  690. Begin
  691. AsmL^.Remove(p);
  692. Dispose(p, done);
  693. p := hp1;
  694. End
  695. Else
  696. If (Pai386(p)^.oper[0].typ = top_reg) And
  697. (Pai386(p)^.oper[1].typ = top_ref) And
  698. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) And
  699. (Pai386(hp1)^.opcode = A_CMP) And
  700. (Pai386(hp1)^.oper[1].typ = top_ref) And
  701. RefsEqual(Pai386(p)^.oper[1].ref^, Pai386(hp1)^.oper[1].ref^)
  702. Then
  703. {change "mov reg, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  704. Pai386(hp1)^.loadreg(1,Pai386(p)^.oper[0].reg);
  705. { Next instruction is also a MOV ? }
  706. If GetNextInstruction(p, hp1) And
  707. (pai(hp1)^.typ = ait_instruction) and
  708. (Pai386(hp1)^.opcode = A_MOV) and
  709. (Pai386(hp1)^.opsize = Pai386(p)^.opsize)
  710. Then
  711. Begin
  712. If (Pai386(hp1)^.oper[0].typ = Pai386(p)^.oper[1].typ) and
  713. (Pai386(hp1)^.oper[1].typ = Pai386(p)^.oper[0].typ)
  714. Then
  715. {mov reg1, mem1 or mov mem1, reg1
  716. mov mem2, reg2 mov reg2, mem2}
  717. Begin
  718. If OpsEqual(Pai386(hp1)^.oper[1],Pai386(p)^.oper[0]) Then
  719. {mov reg1, mem1 or mov mem1, reg1
  720. mov mem2, reg1 mov reg2, mem1}
  721. Begin
  722. If OpsEqual(Pai386(hp1)^.oper[0],Pai386(p)^.oper[1]) Then
  723. { Removes the second statement from
  724. mov reg1, mem1
  725. mov mem1, reg1 }
  726. Begin
  727. AsmL^.remove(hp1);
  728. Dispose(hp1,done);
  729. End
  730. Else
  731. Begin
  732. TmpUsedRegs := UsedRegs;
  733. UpdateUsedRegs(TmpUsedRegs, Pai(hp1^.next));
  734. If (Pai386(p)^.oper[0].typ = top_reg) And
  735. { mov reg1, mem1
  736. mov mem2, reg1 }
  737. GetNextInstruction(hp1, hp2) And
  738. (hp2^.typ = ait_instruction) And
  739. (Pai386(hp2)^.opcode = A_CMP) And
  740. (Pai386(hp2)^.opsize = Pai386(p)^.opsize) and
  741. (Pai386(hp2)^.oper[0].typ = TOp_Ref) And
  742. (Pai386(hp2)^.oper[1].typ = TOp_Reg) And
  743. RefsEqual(Pai386(hp2)^.oper[0].ref^, Pai386(p)^.oper[1].ref^) And
  744. (Pai386(hp2)^.oper[1].reg = Pai386(p)^.oper[0].reg) And
  745. Not(RegUsedAfterInstruction(Pai386(p)^.oper[0].reg, hp2, TmpUsedRegs)) Then
  746. { change to
  747. mov reg1, mem1 mov reg1, mem1
  748. mov mem2, reg1 cmp reg1, mem2
  749. cmp mem1, reg1 }
  750. Begin
  751. AsmL^.Remove(hp2);
  752. Dispose(hp2, Done);
  753. Pai386(hp1)^.opcode := A_CMP;
  754. Pai386(hp1)^.loadref(1,newreference(Pai386(hp1)^.oper[0].ref^));
  755. Pai386(hp1)^.loadreg(0,Pai386(p)^.oper[0].reg);
  756. End;
  757. End;
  758. End
  759. Else
  760. Begin
  761. If GetNextInstruction(hp1, hp2) And
  762. (Pai386(p)^.oper[0].typ = top_ref) And
  763. (Pai386(p)^.oper[1].typ = top_reg) And
  764. (Pai386(hp1)^.oper[0].typ = top_reg) And
  765. (Pai386(hp1)^.oper[0].reg = Pai386(p)^.oper[1].reg) And
  766. (Pai386(hp1)^.oper[1].typ = top_ref) And
  767. (Pai(hp2)^.typ = ait_instruction) And
  768. (Pai386(hp2)^.opcode = A_MOV) And
  769. (Pai386(hp2)^.opsize = Pai386(p)^.opsize) and
  770. (Pai386(hp2)^.oper[1].typ = top_reg) And
  771. (Pai386(hp2)^.oper[0].typ = top_ref) And
  772. RefsEqual(Pai386(hp2)^.oper[0].ref^, Pai386(hp1)^.oper[1].ref^)
  773. Then
  774. If (Pai386(p)^.oper[1].reg in [R_DI,R_EDI])
  775. Then
  776. { mov mem1, %edi
  777. mov %edi, mem2
  778. mov mem2, reg2
  779. to:
  780. mov mem1, reg2
  781. mov reg2, mem2}
  782. Begin
  783. Pai386(p)^.Loadoper(1,Pai386(hp2)^.oper[1]);
  784. Pai386(hp1)^.loadoper(0,Pai386(hp2)^.oper[1]);
  785. AsmL^.Remove(hp2);
  786. Dispose(hp2,Done);
  787. End
  788. Else
  789. { mov mem1, reg1 mov mem1, reg1
  790. mov reg1, mem2 mov reg1, mem2
  791. mov mem2, reg2 mov mem2, reg1
  792. to: to:
  793. mov mem1, reg1 mov mem1, reg1
  794. mov mem1, reg2 mov reg1, mem2
  795. mov reg1, mem2}
  796. Begin
  797. If (Pai386(p)^.oper[1].reg <> Pai386(hp2)^.oper[1].reg) Then
  798. Begin
  799. Pai386(hp1)^.LoadRef(0,newreference(Pai386(p)^.oper[0].ref^));
  800. Pai386(hp1)^.LoadReg(1,Pai386(hp2)^.oper[1].reg);
  801. End
  802. Else
  803. Begin
  804. AsmL^.Remove(hp1);
  805. Dispose(hp1, Done)
  806. End;
  807. Pai386(hp2)^.LoadRef(1,newreference(Pai386(hp2)^.oper[0].ref^));
  808. Pai386(hp2)^.LoadReg(0,Pai386(p)^.oper[1].reg);
  809. End;
  810. End;
  811. End
  812. Else
  813. (* {movl [mem1],reg1
  814. movl [mem1],reg2
  815. to:
  816. movl [mem1],reg1
  817. movl reg1,reg2 }
  818. If (Pai386(p)^.oper[0].typ = top_ref) and
  819. (Pai386(p)^.oper[1].typ = top_reg) and
  820. (Pai386(hp1)^.oper[0].typ = top_ref) and
  821. (Pai386(hp1)^.oper[1].typ = top_reg) and
  822. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) and
  823. RefsEqual(TReference(Pai386(p)^.oper[0]^),Pai386(hp1)^.oper[0]^.ref^) and
  824. (Pai386(p)^.oper[1].reg<>Pai386(hp1)^.oper[0]^.ref^.base) and
  825. (Pai386(p)^.oper[1].reg<>Pai386(hp1)^.oper[0]^.ref^.index) then
  826. Pai386(hp1)^.LoadReg(0,Pai386(p)^.oper[1].reg)
  827. Else*)
  828. { movl const1,[mem1]
  829. movl [mem1],reg1
  830. to:
  831. movl const1,reg1
  832. movl reg1,[mem1] }
  833. If (Pai386(p)^.oper[0].typ = top_const) and
  834. (Pai386(p)^.oper[1].typ = top_ref) and
  835. (Pai386(hp1)^.oper[0].typ = top_ref) and
  836. (Pai386(hp1)^.oper[1].typ = top_reg) and
  837. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) and
  838. RefsEqual(Pai386(hp1)^.oper[0].ref^,Pai386(p)^.oper[1].ref^) then
  839. Begin
  840. Pai386(hp1)^.LoadReg(0,Pai386(hp1)^.oper[1].reg);
  841. Pai386(hp1)^.LoadRef(1,newreference(Pai386(p)^.oper[1].ref^));
  842. Pai386(p)^.LoadReg(1,Pai386(hp1)^.oper[0].reg);
  843. End
  844. End;
  845. {changes "mov $0, %reg" into "xor %reg, %reg"}
  846. If (Pai386(p)^.oper[0].typ = Top_Const) And
  847. (Pai386(p)^.oper[0].val = 0) And
  848. (Pai386(p)^.oper[1].typ = Top_Reg)
  849. Then
  850. Begin
  851. Pai386(p)^.opcode := A_XOR;
  852. Pai386(p)^.LoadReg(0,Pai386(p)^.oper[1].reg);
  853. End;
  854. End;
  855. A_MOVZX:
  856. Begin
  857. {removes superfluous And's after movzx's}
  858. If (Pai386(p)^.oper[1].typ = top_reg) And
  859. GetNextInstruction(p, hp1) And
  860. (Pai(hp1)^.typ = ait_instruction) And
  861. (Pai386(hp1)^.opcode = A_AND) And
  862. (Pai386(hp1)^.oper[0].typ = top_const) And
  863. (Pai386(hp1)^.oper[1].typ = top_reg) And
  864. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg)
  865. Then
  866. Case Pai386(p)^.opsize Of
  867. S_BL, S_BW:
  868. If (Pai386(hp1)^.oper[0].val = $ff) Then
  869. Begin
  870. AsmL^.Remove(hp1);
  871. Dispose(hp1, Done);
  872. End;
  873. S_WL:
  874. If (Pai386(hp1)^.oper[0].val = $ffff) Then
  875. Begin
  876. AsmL^.Remove(hp1);
  877. Dispose(hp1, Done);
  878. End;
  879. End;
  880. {changes some movzx constructs to faster synonims (all examples
  881. are given with eax/ax, but are also valid for other registers)}
  882. If (Pai386(p)^.oper[1].typ = top_reg) Then
  883. If (Pai386(p)^.oper[0].typ = top_reg) Then
  884. Case Pai386(p)^.opsize of
  885. S_BW:
  886. Begin
  887. If (Pai386(p)^.oper[0].reg = Reg16ToReg8(Pai386(p)^.oper[1].reg)) And
  888. Not(CS_LittleSize In aktglobalswitches)
  889. Then
  890. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  891. Begin
  892. Pai386(p)^.opcode := A_AND;
  893. Pai386(p)^.changeopsize(S_W);
  894. Pai386(p)^.LoadConst(0,$ff);
  895. End
  896. Else
  897. If GetNextInstruction(p, hp1) And
  898. (Pai(hp1)^.typ = ait_instruction) And
  899. (Pai386(hp1)^.opcode = A_AND) And
  900. (Pai386(hp1)^.oper[0].typ = top_const) And
  901. (Pai386(hp1)^.oper[1].typ = top_reg) And
  902. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg)
  903. Then
  904. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  905. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  906. Begin
  907. Pai386(p)^.opcode := A_MOV;
  908. Pai386(p)^.changeopsize(S_W);
  909. Pai386(p)^.LoadReg(0,Reg8ToReg16(Pai386(p)^.oper[0].reg));
  910. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ff);
  911. End;
  912. End;
  913. S_BL:
  914. Begin
  915. If (Pai386(p)^.oper[0].reg = Reg32ToReg8(Pai386(p)^.oper[1].reg)) And
  916. Not(CS_LittleSize in aktglobalswitches)
  917. Then
  918. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  919. Begin
  920. Pai386(p)^.opcode := A_AND;
  921. Pai386(p)^.changeopsize(S_L);
  922. Pai386(p)^.loadconst(0,$ff)
  923. End
  924. Else
  925. If GetNextInstruction(p, hp1) And
  926. (Pai(hp1)^.typ = ait_instruction) And
  927. (Pai386(hp1)^.opcode = A_AND) And
  928. (Pai386(hp1)^.oper[0].typ = top_const) And
  929. (Pai386(hp1)^.oper[1].typ = top_reg) And
  930. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg)
  931. Then
  932. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  933. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  934. Begin
  935. Pai386(p)^.opcode := A_MOV;
  936. Pai386(p)^.changeopsize(S_L);
  937. Pai386(p)^.LoadReg(0,Reg8ToReg32(Pai386(p)^.oper[0].reg));
  938. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ff);
  939. End
  940. End;
  941. S_WL:
  942. Begin
  943. If (Pai386(p)^.oper[0].reg = Reg32ToReg16(Pai386(p)^.oper[1].reg)) And
  944. Not(CS_LittleSize In aktglobalswitches)
  945. Then
  946. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  947. Begin
  948. Pai386(p)^.opcode := A_AND;
  949. Pai386(p)^.changeopsize(S_L);
  950. Pai386(p)^.LoadConst(0,$ffff);
  951. End
  952. Else
  953. If GetNextInstruction(p, hp1) And
  954. (Pai(hp1)^.typ = ait_instruction) And
  955. (Pai386(hp1)^.opcode = A_AND) And
  956. (Pai386(hp1)^.oper[0].typ = top_const) And
  957. (Pai386(hp1)^.oper[1].typ = top_reg) And
  958. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg)
  959. Then
  960. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  961. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  962. Begin
  963. Pai386(p)^.opcode := A_MOV;
  964. Pai386(p)^.changeopsize(S_L);
  965. Pai386(p)^.LoadReg(0,Reg16ToReg32(Pai386(p)^.oper[0].reg));
  966. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ffff);
  967. End;
  968. End;
  969. End
  970. Else
  971. If (Pai386(p)^.oper[0].typ = top_ref) Then
  972. Begin
  973. If GetNextInstruction(p, hp1) And
  974. (Pai(hp1)^.typ = ait_instruction) And
  975. (Pai386(hp1)^.opcode = A_AND) And
  976. (Pai386(hp1)^.oper[0].typ = Top_Const) And
  977. (Pai386(hp1)^.oper[1].typ = Top_Reg) And
  978. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Then
  979. Begin
  980. Pai386(p)^.opcode := A_MOV;
  981. Case Pai386(p)^.opsize Of
  982. S_BL:
  983. Begin
  984. Pai386(p)^.changeopsize(S_L);
  985. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ff);
  986. End;
  987. S_WL:
  988. Begin
  989. Pai386(p)^.changeopsize(S_L);
  990. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ffff);
  991. End;
  992. S_BW:
  993. Begin
  994. Pai386(p)^.changeopsize(S_W);
  995. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val And $ff);
  996. End;
  997. End;
  998. End;
  999. End;
  1000. End;
  1001. A_POP:
  1002. Begin
  1003. if (Pai386(p)^.oper[0].typ = top_reg) And
  1004. GetNextInstruction(p, hp1) And
  1005. (pai(hp1)^.typ=ait_instruction) and
  1006. (Pai386(hp1)^.opcode=A_PUSH) and
  1007. (Pai386(hp1)^.oper[0].typ = top_reg) And
  1008. (Pai386(hp1)^.oper[0].reg=Pai386(p)^.oper[0].reg) then
  1009. If (Not(cs_regalloc in aktglobalswitches)) Then
  1010. Begin
  1011. hp2:=pai(hp1^.next);
  1012. asml^.remove(p);
  1013. asml^.remove(hp1);
  1014. dispose(p,done);
  1015. dispose(hp1,done);
  1016. p:=hp2;
  1017. continue
  1018. End
  1019. Else
  1020. Begin
  1021. { change it to a two op operation }
  1022. Pai386(p)^.oper[1].typ:=top_none;
  1023. Pai386(p)^.ops:=2;
  1024. Pai386(p)^.opcode := A_MOV;
  1025. Pai386(p)^.Loadoper(1,Pai386(p)^.oper[0]);
  1026. New(TmpRef);
  1027. Reset_reference(tmpref^);
  1028. TmpRef^.base := R_ESP;
  1029. Pai386(p)^.LoadRef(0,TmpRef);
  1030. hp1 := Pai(p^.next);
  1031. AsmL^.Remove(hp1);
  1032. Dispose(hp1, Done)
  1033. End
  1034. end;
  1035. A_PUSH:
  1036. Begin
  1037. If (Pai386(p)^.opsize = S_W) And
  1038. (Pai386(p)^.oper[0].typ = Top_Const) And
  1039. GetNextInstruction(p, hp1) And
  1040. (Pai(hp1)^.typ = ait_instruction) And
  1041. (Pai386(hp1)^.opcode = A_PUSH) And
  1042. (Pai386(hp1)^.oper[0].typ = Top_Const) And
  1043. (Pai386(hp1)^.opsize = S_W) Then
  1044. Begin
  1045. Pai386(p)^.changeopsize(S_L);
  1046. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val shl 16 + Pai386(hp1)^.oper[0].val);
  1047. AsmL^.Remove(hp1);
  1048. Dispose(hp1, Done)
  1049. End;
  1050. End;
  1051. A_SHL, A_SAL:
  1052. Begin
  1053. If (Pai386(p)^.oper[0].typ = Top_Const) And
  1054. (Pai386(p)^.oper[1].typ = Top_Reg) And
  1055. (Pai386(p)^.opsize = S_L) And
  1056. (Pai386(p)^.oper[0].val <= 3)
  1057. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1058. Then
  1059. Begin
  1060. TmpBool1 := True; {should we check the next instruction?}
  1061. TmpBool2 := False; {have we found an add/sub which could be
  1062. integrated in the lea?}
  1063. New(TmpRef);
  1064. Reset_reference(tmpref^);
  1065. TmpRef^.index := Pai386(p)^.oper[1].reg;
  1066. TmpRef^.scalefactor := 1 shl Pai386(p)^.oper[0].val;
  1067. While TmpBool1 And
  1068. GetNextInstruction(p, hp1) And
  1069. (Pai(hp1)^.typ = ait_instruction) And
  1070. ((Pai386(hp1)^.opcode = A_ADD) Or
  1071. (Pai386(hp1)^.opcode = A_SUB)) And
  1072. (Pai386(hp1)^.oper[1].typ = Top_Reg) And
  1073. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Do
  1074. Begin
  1075. TmpBool1 := False;
  1076. If (Pai386(hp1)^.oper[0].typ = Top_Const)
  1077. Then
  1078. Begin
  1079. TmpBool1 := True;
  1080. TmpBool2 := True;
  1081. If Pai386(hp1)^.opcode = A_ADD Then
  1082. Inc(TmpRef^.offset, Pai386(hp1)^.oper[0].val)
  1083. Else
  1084. Dec(TmpRef^.offset, Pai386(hp1)^.oper[0].val);
  1085. AsmL^.Remove(hp1);
  1086. Dispose(hp1, Done);
  1087. End
  1088. Else
  1089. If (Pai386(hp1)^.oper[0].typ = Top_Reg) And
  1090. (Pai386(hp1)^.opcode = A_ADD) And
  1091. (TmpRef^.base = R_NO) Then
  1092. Begin
  1093. TmpBool1 := True;
  1094. TmpBool2 := True;
  1095. TmpRef^.base := Pai386(hp1)^.oper[0].reg;
  1096. AsmL^.Remove(hp1);
  1097. Dispose(hp1, Done);
  1098. End;
  1099. End;
  1100. If TmpBool2 Or
  1101. ((aktoptprocessor < ClassP6) And
  1102. (Pai386(p)^.oper[0].val <= 3) And
  1103. Not(CS_LittleSize in aktglobalswitches))
  1104. Then
  1105. Begin
  1106. If Not(TmpBool2) And
  1107. (Pai386(p)^.oper[0].val = 1)
  1108. Then
  1109. Begin
  1110. Dispose(TmpRef);
  1111. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.opsize,
  1112. Pai386(p)^.oper[1].reg, Pai386(p)^.oper[1].reg))
  1113. End
  1114. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  1115. Pai386(p)^.oper[1].reg));
  1116. hp1^.fileinfo := p^.fileinfo;
  1117. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1118. Dispose(p, Done);
  1119. p := hp1;
  1120. End;
  1121. End
  1122. Else
  1123. If (aktoptprocessor < ClassP6) And
  1124. (Pai386(p)^.oper[0].typ = top_const) And
  1125. (Pai386(p)^.oper[1].typ = top_reg) Then
  1126. If (Pai386(p)^.oper[0].val = 1)
  1127. Then
  1128. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1129. but faster on a 486, and pairable in both U and V pipes on the Pentium
  1130. (unlike shl, which is only pairable in the U pipe)}
  1131. Begin
  1132. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.opsize,
  1133. Pai386(p)^.oper[1].reg, Pai386(p)^.oper[1].reg));
  1134. hp1^.fileinfo := p^.fileinfo;
  1135. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1136. Dispose(p, done);
  1137. p := hp1;
  1138. End
  1139. Else If (Pai386(p)^.opsize = S_L) and
  1140. (Pai386(p)^.oper[0].val<= 3) Then
  1141. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1142. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1143. Begin
  1144. New(TmpRef);
  1145. Reset_reference(tmpref^);
  1146. TmpRef^.index := Pai386(p)^.oper[1].reg;
  1147. TmpRef^.scalefactor := 1 shl Pai386(p)^.oper[0].val;
  1148. hp1 := new(Pai386,op_ref_reg(A_LEA,S_L,TmpRef, Pai386(p)^.oper[1].reg));
  1149. hp1^.fileinfo := p^.fileinfo;
  1150. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1151. Dispose(p, done);
  1152. p := hp1;
  1153. End
  1154. End;
  1155. A_SAR, A_SHR:
  1156. {changes the code sequence
  1157. shr/sar const1, x
  1158. shl const2, x
  1159. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  1160. Begin
  1161. If GetNextInstruction(p, hp1) And
  1162. (pai(hp1)^.typ = ait_instruction) and
  1163. (Pai386(hp1)^.opcode = A_SHL) and
  1164. (Pai386(p)^.oper[0].typ = top_const) and
  1165. (Pai386(hp1)^.oper[0].typ = top_const) and
  1166. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  1167. (Pai386(hp1)^.oper[1].typ = Pai386(p)^.oper[1].typ) And
  1168. OpsEqual(Pai386(hp1)^.oper[1], Pai386(p)^.oper[1])
  1169. Then
  1170. If (Pai386(p)^.oper[0].val > Pai386(hp1)^.oper[0].val) And
  1171. Not(CS_LittleSize In aktglobalswitches)
  1172. Then
  1173. { shr/sar const1, %reg
  1174. shl const2, %reg
  1175. with const1 > const2 }
  1176. Begin
  1177. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val-Pai386(hp1)^.oper[0].val);
  1178. Pai386(hp1)^.opcode := A_AND;
  1179. l := 1 shl (Pai386(hp1)^.oper[0].val-1);
  1180. Case Pai386(p)^.opsize Of
  1181. S_L: Pai386(hp1)^.LoadConst(0,l Xor longint(-1));
  1182. S_B: Pai386(hp1)^.LoadConst(0,l Xor $ff);
  1183. S_W: Pai386(hp1)^.LoadConst(0,l Xor $ffff);
  1184. End;
  1185. End
  1186. Else
  1187. If (Pai386(p)^.oper[0].val<Pai386(hp1)^.oper[0].val) And
  1188. Not(CS_LittleSize In aktglobalswitches)
  1189. Then
  1190. { shr/sar const1, %reg
  1191. shl const2, %reg
  1192. with const1 < const2 }
  1193. Begin
  1194. Pai386(hp1)^.LoadConst(0,Pai386(hp1)^.oper[0].val-Pai386(p)^.oper[0].val);
  1195. Pai386(p)^.opcode := A_AND;
  1196. l := 1 shl (Pai386(p)^.oper[0].val-1);
  1197. Case Pai386(p)^.opsize Of
  1198. S_L: Pai386(p)^.LoadConst(0,l Xor $ffffffff);
  1199. S_B: Pai386(p)^.LoadConst(0,l Xor $ff);
  1200. S_W: Pai386(p)^.LoadConst(0,l Xor $ffff);
  1201. End;
  1202. End
  1203. Else
  1204. { shr/sar const1, %reg
  1205. shl const2, %reg
  1206. with const1 = const2 }
  1207. Begin
  1208. Pai386(p)^.opcode := A_AND;
  1209. l := 1 shl (Pai386(p)^.oper[0].val-1);
  1210. Case Pai386(p)^.opsize Of
  1211. S_B: Pai386(p)^.LoadConst(0,l Xor $ff);
  1212. S_W: Pai386(p)^.LoadConst(0,l Xor $ffff);
  1213. S_L: Pai386(p)^.LoadConst(0,l Xor $ffffffff);
  1214. End;
  1215. AsmL^.remove(hp1);
  1216. dispose(hp1, done);
  1217. End;
  1218. End;
  1219. A_SETcc :
  1220. Begin
  1221. If (Pai386(p)^.oper[0].typ = top_ref) And
  1222. GetNextInstruction(p, hp1) And
  1223. GetNextInstruction(hp1, hp2) And
  1224. (hp2^.typ = ait_instruction) And
  1225. ((Pai386(hp2)^.opcode = A_LEAVE) or
  1226. (Pai386(hp2)^.opcode = A_RET)) And
  1227. (Pai386(p)^.oper[0].ref^.Base = ProcInfo.FramePointer) And
  1228. (Pai386(p)^.oper[0].ref^.Index = R_NO) And
  1229. (Pai386(p)^.oper[0].ref^.Offset >= ProcInfo.RetOffset) And
  1230. (hp1^.typ = ait_instruction) And
  1231. (Pai386(hp1)^.opcode = A_MOV) And
  1232. (Pai386(hp1)^.opsize = S_B) And
  1233. (Pai386(hp1)^.oper[0].typ = top_ref) And
  1234. RefsEqual(Pai386(hp1)^.oper[0].ref^, Pai386(p)^.oper[0].ref^) Then
  1235. Begin
  1236. Pai386(p)^.LoadReg(0,Pai386(hp1)^.oper[1].reg);
  1237. AsmL^.Remove(hp1);
  1238. Dispose(hp1, Done)
  1239. End
  1240. End;
  1241. A_SUB:
  1242. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1243. { * change "sub/add const1, reg" or "dec reg" followed by
  1244. "sub const2, reg" to one "sub ..., reg" }
  1245. Begin
  1246. If (Pai386(p)^.oper[0].typ = top_const) And
  1247. (Pai386(p)^.oper[1].typ = top_reg) Then
  1248. If (Pai386(p)^.oper[0].val = 2) And
  1249. (Pai386(p)^.oper[1].reg = R_ESP) Then
  1250. Begin
  1251. hp1 := Pai(p^.next);
  1252. While Assigned(hp1) And
  1253. (Pai(hp1)^.typ In [ait_instruction]+SkipInstr) And
  1254. Not((Pai(hp1)^.typ = ait_instruction) And
  1255. ((Pai386(hp1)^.opcode = A_CALL) or
  1256. (Pai386(hp1)^.opcode = A_PUSH) or
  1257. ((Pai386(hp1)^.opcode = A_MOV) And
  1258. (Pai386(hp1)^.oper[1].typ = top_ref) And
  1259. (Pai386(hp1)^.oper[1].ref^.base = R_ESP)))) do
  1260. hp1 := Pai(hp1^.next);
  1261. If Assigned(hp1) And
  1262. (Pai(hp1)^.typ = ait_instruction) And
  1263. (Pai386(hp1)^.opcode = A_PUSH) And
  1264. (Pai386(hp1)^.opsize = S_W)
  1265. Then
  1266. Begin
  1267. Pai386(hp1)^.changeopsize(S_L);
  1268. if Pai386(hp1)^.oper[0].typ=top_reg then
  1269. Pai386(hp1)^.LoadReg(0,Reg16ToReg32(Pai386(hp1)^.oper[0].reg));
  1270. hp1 := Pai(p^.next);
  1271. AsmL^.Remove(p);
  1272. Dispose(p, Done);
  1273. p := hp1;
  1274. Continue
  1275. End
  1276. Else
  1277. If GetLastInstruction(p, hp1) And
  1278. (Pai(hp1)^.typ = ait_instruction) And
  1279. (Pai386(hp1)^.opcode = A_SUB) And
  1280. (Pai386(hp1)^.oper[0].typ = top_const) And
  1281. (Pai386(hp1)^.oper[1].typ = top_reg) And
  1282. (Pai386(hp1)^.oper[1].reg = R_ESP)
  1283. Then
  1284. Begin
  1285. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val+Pai386(hp1)^.oper[0].val);
  1286. AsmL^.Remove(hp1);
  1287. Dispose(hp1, Done);
  1288. End;
  1289. End
  1290. Else
  1291. If GetLastInstruction(p, hp1) And
  1292. (hp1^.typ = ait_instruction) And
  1293. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) then
  1294. Case Pai386(hp1)^.opcode Of
  1295. A_DEC:
  1296. If (Pai386(hp1)^.oper[0].typ = top_reg) And
  1297. (Pai386(hp1)^.oper[0].reg = Pai386(p)^.oper[1].reg) Then
  1298. Begin
  1299. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val+1);
  1300. AsmL^.Remove(hp1);
  1301. Dispose(hp1, Done)
  1302. End;
  1303. A_SUB:
  1304. If (Pai386(hp1)^.oper[0].typ = top_const) And
  1305. (Pai386(hp1)^.oper[1].typ = top_reg) And
  1306. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Then
  1307. Begin
  1308. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val+Pai386(hp1)^.oper[0].val);
  1309. AsmL^.Remove(hp1);
  1310. Dispose(hp1, Done)
  1311. End;
  1312. A_ADD:
  1313. If (Pai386(hp1)^.oper[0].typ = top_const) And
  1314. (Pai386(hp1)^.oper[1].typ = top_reg) And
  1315. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Then
  1316. Begin
  1317. Pai386(p)^.LoadConst(0,Pai386(p)^.oper[0].val-Pai386(hp1)^.oper[0].val);
  1318. AsmL^.Remove(hp1);
  1319. Dispose(hp1, Done)
  1320. End;
  1321. End
  1322. End;
  1323. A_TEST, A_OR:
  1324. {removes the line marked with (x) from the sequence
  1325. And/or/xor/add/sub/... $x, %y
  1326. test/or %y, %y (x)
  1327. j(n)z _Label
  1328. as the first instruction already adjusts the ZF}
  1329. Begin
  1330. If OpsEqual(Pai386(p)^.oper[0],Pai386(p)^.oper[1]) And
  1331. GetLastInstruction(p, hp1) And
  1332. (pai(hp1)^.typ = ait_instruction) Then
  1333. Case Pai386(hp1)^.opcode Of
  1334. A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_SHL, A_SHR:
  1335. Begin
  1336. If OpsEqual(Pai386(hp1)^.oper[1],Pai386(p)^.oper[0]) Then
  1337. Begin
  1338. hp1 := pai(p^.next);
  1339. asml^.remove(p);
  1340. dispose(p, done);
  1341. p := pai(hp1);
  1342. continue
  1343. End;
  1344. End;
  1345. A_DEC, A_INC, A_NEG:
  1346. Begin
  1347. If OpsEqual(Pai386(hp1)^.oper[0],Pai386(p)^.oper[0]) Then
  1348. Begin
  1349. Case Pai386(hp1)^.opcode Of
  1350. A_DEC, A_INC:
  1351. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  1352. Begin
  1353. Case Pai386(hp1)^.opcode Of
  1354. A_DEC: Pai386(hp1)^.opcode := A_SUB;
  1355. A_INC: Pai386(hp1)^.opcode := A_ADD;
  1356. End;
  1357. Pai386(hp1)^.Loadoper(1,Pai386(hp1)^.oper[0]);
  1358. Pai386(hp1)^.LoadConst(0,1);
  1359. Pai386(hp1)^.ops:=2;
  1360. End
  1361. End;
  1362. hp1 := pai(p^.next);
  1363. asml^.remove(p);
  1364. dispose(p, done);
  1365. p := pai(hp1);
  1366. continue
  1367. End;
  1368. End
  1369. End;
  1370. End;
  1371. End;
  1372. end; { if is_jmp }
  1373. End;
  1374. { ait_label:
  1375. Begin
  1376. If Not(Pai_Label(p)^.l^.is_used)
  1377. Then
  1378. Begin
  1379. hp1 := Pai(p^.next);
  1380. AsmL^.Remove(p);
  1381. Dispose(p, Done);
  1382. p := hp1;
  1383. Continue
  1384. End;
  1385. End;}
  1386. End;
  1387. p:=pai(p^.next);
  1388. end;
  1389. end;
  1390. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  1391. var
  1392. p,hp1,hp2: pai;
  1393. Begin
  1394. P := BlockStart;
  1395. While (P <> BlockEnd) Do
  1396. Begin
  1397. Case P^.Typ Of
  1398. Ait_Instruction:
  1399. Begin
  1400. Case Pai386(p)^.opcode Of
  1401. A_CALL:
  1402. If (AktOptProcessor < ClassP6) And
  1403. GetNextInstruction(p, hp1) And
  1404. (hp1^.typ = ait_instruction) And
  1405. (pai386(hp1)^.opcode = A_JMP) Then
  1406. Begin
  1407. Inc(pai386(hp1)^.oper[0].sym^.refs);
  1408. hp2 := New(Pai386,op_sym(A_PUSH,S_L,pai386(hp1)^.oper[0].sym));
  1409. hp2^.fileinfo := p^.fileinfo;
  1410. InsertLLItem(AsmL, p^.previous, p, hp2);
  1411. Pai386(p)^.opcode := A_JMP;
  1412. AsmL^.Remove(hp1);
  1413. Dispose(hp1, Done)
  1414. End;
  1415. A_MOV:
  1416. Begin
  1417. If (Pai386(p)^.oper[0].typ = top_reg) And
  1418. (Pai386(p)^.oper[1].typ = top_reg) And
  1419. GetNextInstruction(p, hp1) And
  1420. (hp1^.typ = ait_Instruction) And
  1421. (Pai386(hp1)^.opcode = A_MOV) And
  1422. (Pai386(hp1)^.oper[0].typ = top_ref) And
  1423. (Pai386(hp1)^.oper[1].typ = top_reg) And
  1424. ((Pai386(hp1)^.oper[0].ref^.Base = Pai386(p)^.oper[1].reg) Or
  1425. (Pai386(hp1)^.oper[0].ref^.Index = Pai386(p)^.oper[1].reg)) And
  1426. (Pai386(hp1)^.oper[1].reg = Pai386(p)^.oper[1].reg) Then
  1427. {mov reg1, reg2
  1428. mov (reg2, ..), reg2 to mov (reg1, ..), reg2}
  1429. Begin
  1430. If (Pai386(hp1)^.oper[0].ref^.Base = Pai386(p)^.oper[1].reg) Then
  1431. Pai386(hp1)^.oper[0].ref^.Base := Pai386(p)^.oper[0].reg;
  1432. If (Pai386(hp1)^.oper[0].ref^.Index = Pai386(p)^.oper[1].reg) Then
  1433. Pai386(hp1)^.oper[0].ref^.Index := Pai386(p)^.oper[0].reg;
  1434. AsmL^.Remove(p);
  1435. Dispose(p, Done);
  1436. p := hp1;
  1437. Continue;
  1438. End;
  1439. End;
  1440. A_MOVZX:
  1441. Begin
  1442. If (Pai386(p)^.oper[1].typ = top_reg) Then
  1443. If (Pai386(p)^.oper[0].typ = top_reg)
  1444. Then
  1445. Case Pai386(p)^.opsize of
  1446. S_BL:
  1447. Begin
  1448. If IsGP32Reg(Pai386(p)^.oper[1].reg) And
  1449. Not(CS_LittleSize in aktglobalswitches) And
  1450. (aktoptprocessor = ClassP5)
  1451. Then
  1452. {Change "movzbl %reg1, %reg2" to
  1453. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1454. PentiumMMX}
  1455. Begin
  1456. hp1 := New(Pai386, op_reg_reg(A_XOR, S_L,
  1457. Pai386(p)^.oper[1].reg, Pai386(p)^.oper[1].reg));
  1458. hp1^.fileinfo := p^.fileinfo;
  1459. InsertLLItem(AsmL,p^.previous, p, hp1);
  1460. Pai386(p)^.opcode := A_MOV;
  1461. Pai386(p)^.changeopsize(S_B);
  1462. Pai386(p)^.LoadReg(1,Reg32ToReg8(Pai386(p)^.oper[1].reg));
  1463. End;
  1464. End;
  1465. End
  1466. Else
  1467. If (Pai386(p)^.oper[0].typ = top_ref) And
  1468. (Pai386(p)^.oper[0].ref^.base <> Pai386(p)^.oper[1].reg) And
  1469. (Pai386(p)^.oper[0].ref^.index <> Pai386(p)^.oper[1].reg) And
  1470. Not(CS_LittleSize in aktglobalswitches) And
  1471. IsGP32Reg(Pai386(p)^.oper[1].reg) And
  1472. (aktoptprocessor = ClassP5) And
  1473. (Pai386(p)^.opsize = S_BL)
  1474. Then
  1475. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1476. Pentium and PentiumMMX}
  1477. Begin
  1478. hp1 := New(Pai386,op_reg_reg(A_XOR, S_L, Pai386(p)^.oper[1].reg,
  1479. Pai386(p)^.oper[1].reg));
  1480. hp1^.fileinfo := p^.fileinfo;
  1481. Pai386(p)^.opcode := A_MOV;
  1482. Pai386(p)^.changeopsize(S_B);
  1483. Pai386(p)^.LoadReg(1,Reg32ToReg8(Pai386(p)^.oper[1].reg));
  1484. InsertLLItem(AsmL,p^.previous, p, hp1);
  1485. End;
  1486. End;
  1487. End;
  1488. End;
  1489. End;
  1490. p := Pai(p^.next)
  1491. End;
  1492. End;
  1493. End.
  1494. {
  1495. $Log$
  1496. Revision 1.54 1999-05-27 19:44:49 peter
  1497. * removed oldasm
  1498. * plabel -> pasmlabel
  1499. * -a switches to source writing automaticly
  1500. * assembler readers OOPed
  1501. * asmsymbol automaticly external
  1502. * jumptables and other label fixes for asm readers
  1503. Revision 1.53 1999/05/12 00:19:52 peter
  1504. * removed R_DEFAULT_SEG
  1505. * uniform float names
  1506. Revision 1.52 1999/05/05 16:19:04 jonas
  1507. + remove the segment prefixes from LEA instructions
  1508. Revision 1.51 1999/05/05 10:05:54 florian
  1509. * a delphi compiled compiler recompiles ppc
  1510. Revision 1.50 1999/05/02 21:33:55 florian
  1511. * several bugs regarding -Or fixed
  1512. Revision 1.49 1999/05/02 14:26:31 peter
  1513. * fixed dec -> sub $1 opt which didn't set ops=2
  1514. Revision 1.48 1999/05/01 13:24:34 peter
  1515. * merged nasm compiler
  1516. * old asm moved to oldasm/
  1517. Revision 1.5 1999/04/30 12:36:50 jonas
  1518. * fix from Brussels: call/jmp => push/jmp transformation didn't
  1519. count correctly the jmp references
  1520. Revision 1.4 1999/04/10 16:14:11 peter
  1521. * fixed optimizer
  1522. Revision 1.3 1999/04/09 08:33:18 peter
  1523. * fixed mov reg,treg;mov treg,x bug
  1524. Revision 1.2 1999/03/29 16:05:51 peter
  1525. * optimizer working for ag386bin
  1526. Revision 1.1 1999/03/26 00:01:15 peter
  1527. * first things for optimizer (compiles but cycle crashes)
  1528. Revision 1.39 1999/02/26 00:48:22 peter
  1529. * assembler writers fixed for ag386bin
  1530. Revision 1.38 1999/02/25 21:02:44 peter
  1531. * ag386bin updates
  1532. + coff writer
  1533. Revision 1.37 1999/02/22 02:15:30 peter
  1534. * updates for ag386bin
  1535. Revision 1.36 1999/01/04 22:04:15 jonas
  1536. + mov reg, mem1 to mov reg, mem1
  1537. mov mem2, reg cmp reg, mem2
  1538. cmp mem1, reg
  1539. # reg released
  1540. Revision 1.35 1999/01/04 12:58:55 jonas
  1541. * no fistp/fild optimization for S_IQ (fistq doesn't exist)
  1542. Revision 1.34 1998/12/29 18:48:17 jonas
  1543. + optimize pascal code surrounding assembler blocks
  1544. Revision 1.33 1998/12/23 15:16:21 jonas
  1545. * change "inc x/dec x; test x, x" to "add 1, x/sub 1,x" because inc and dec
  1546. don't affect the carry flag (test does). This *doesn't* fix the problem with
  1547. cardinal, that's a cg issue.
  1548. Revision 1.32 1998/12/16 12:09:29 jonas
  1549. * fixed fistp/fild optimization
  1550. Revision 1.31 1998/12/15 22:30:39 jonas
  1551. + change "sub/add const1, reg" or "dec reg" followed by "sub const2, reg" to one
  1552. "sub const3, reg"
  1553. * some small cleaning up
  1554. Revision 1.30 1998/12/15 15:43:20 jonas
  1555. * fixed bug in shr/shl optimization
  1556. Revision 1.29 1998/12/15 11:53:54 peter
  1557. * removed commentlevel
  1558. Revision 1.28 1998/12/14 22:01:45 jonas
  1559. - removed $ifdef ver0_99_11's
  1560. Revision 1.27 1998/12/11 00:03:35 peter
  1561. + globtype,tokens,version unit splitted from globals
  1562. Revision 1.26 1998/12/09 18:16:13 jonas
  1563. * corrected small syntax error in part between ifdef ver0_99_11
  1564. + added fistp/fild optimization between ifdef ver0_99_11
  1565. Revision 1.25 1998/12/02 16:23:29 jonas
  1566. * changed "if longintvar in set" to case or "if () or () .." statements
  1567. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  1568. Revision 1.24 1998/11/26 15:41:45 jonas
  1569. + change "setxx mem; movb mem, reg8" to "setxx reg8" if mem is a local
  1570. variable/parameter or function result (between $ifdef ver0_99_11)
  1571. Revision 1.23 1998/11/03 16:26:09 jonas
  1572. * "call x;jmp y" optimization not done anymore for P6 and equivalents
  1573. * made FPU optimizations simpler and more effective
  1574. Revision 1.22 1998/10/29 18:37:55 jonas
  1575. + change "call x; jmp y" to "push y; jmp x" (suggestion from Daniel)
  1576. Revision 1.19 1998/10/23 15:38:23 jonas
  1577. + some small FPU peephole optimizations (use value in FP regs instead of loading it
  1578. from memory if possible, mostly with var1+var1 and var1*var1)
  1579. Revision 1.18 1998/10/05 14:41:14 jonas
  1580. * fixed small memory leak
  1581. * fixed small inefficiency
  1582. * tested multiple line comments ability of my new MacCVS client :)
  1583. Revision 1.17 1998/10/02 17:29:56 jonas
  1584. + removal of "lea (reg), reg)", "imul $1, reg", change "mov reg1, reg2; mov (reg2), reg2" to "mov (reg1), reg2"
  1585. Revision 1.16 1998/10/01 20:19:57 jonas
  1586. * moved UpdateUsedRegs (+ bugfix) to daopt386
  1587. Revision 1.15 1998/09/30 12:18:29 peter
  1588. * fixed subl $2,esp;psuhw bug
  1589. Revision 1.14 1998/09/20 17:11:51 jonas
  1590. * released REGALLOC
  1591. Revision 1.13 1998/09/16 18:00:00 jonas
  1592. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  1593. Revision 1.12 1998/09/15 14:05:22 jonas
  1594. * fixed optimizer incompatibilities with freelabel code in psub
  1595. Revision 1.11 1998/08/28 10:57:02 peter
  1596. * removed warnings
  1597. Revision 1.10 1998/08/27 15:17:50 florian
  1598. * reinstated Jonas' bugfix
  1599. Revision 1.9 1998/08/25 16:58:59 pierre
  1600. * removed a line that add no sense and
  1601. introduce garbage in the asmlist
  1602. (uninitialized data !)
  1603. Revision 1.7 1998/08/19 16:07:53 jonas
  1604. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1605. Revision 1.6 1998/08/10 14:50:14 peter
  1606. + localswitches, moduleswitches, globalswitches splitting
  1607. Revision 1.5 1998/08/06 19:40:28 jonas
  1608. * removed $ before and after Log in comment
  1609. Revision 1.4 1998/08/05 16:27:17 jonas
  1610. * fstp/fld bugfix (fstt does not exist)
  1611. Revision 1.3 1998/08/05 16:00:15 florian
  1612. * some fixes for ansi strings
  1613. * log to Log changed
  1614. }