popt386.pas 86 KB

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