popt386.pas 90 KB

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