popt386.pas 85 KB

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