popt386.pas 86 KB

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