popt386.pas 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725
  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. hp1 := New(Pai386, Op_Reg_Reg(A_MOV, S_L, TRegister(TwoWords(Pai386(p)^.op2).Word1),
  355. TRegister(TwoWords(Pai386(p)^.op2).Word2)));
  356. hp1^.fileinfo := p^.fileinfo;
  357. InsertLLItem(AsmL, p^.previous, p^.next, hp1);
  358. Dispose(p, Done);
  359. p := hp1;
  360. End
  361. Else If
  362. ((Pai386(p)^.op3t = Top_Reg) or
  363. (Pai386(p)^.op3t = Top_None)) And
  364. (aktoptprocessor < ClassP6) And
  365. (Longint(Pai386(p)^.op1) <= 12) And
  366. Not(CS_LittleSize in aktglobalswitches) And
  367. (Not(GetNextInstruction(p, hp1)) Or
  368. {GetNextInstruction(p, hp1) And}
  369. Not((Pai(hp1)^.typ = ait_labeled_instruction) And
  370. ((pai386_labeled(hp1)^.opcode = A_JO) or
  371. (pai386_labeled(hp1)^.opcode = A_JNO))))
  372. Then
  373. Begin
  374. New(TmpRef);
  375. Reset_reference(TmpRef^);
  376. Case Longint(Pai386(p)^.op1) Of
  377. 3: Begin
  378. {imul 3, reg1, reg2 to
  379. lea (reg1,reg1,2), reg2
  380. imul 3, reg1 to
  381. lea (reg1,reg1,2), reg1}
  382. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).Word1);
  383. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  384. TmpRef^.ScaleFactor := 2;
  385. If (Pai386(p)^.op3t = Top_None)
  386. Then hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, TRegister(Pai386(p)^.op2)))
  387. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  388. TRegister(twowords(Pai386(p)^.op2).word2)));
  389. hp1^.fileinfo := p^.fileinfo;
  390. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  391. Dispose(p, Done);
  392. p := hp1;
  393. End;
  394. 5: Begin
  395. {imul 5, reg1, reg2 to
  396. lea (reg1,reg1,4), reg2
  397. imul 5, reg1 to
  398. lea (reg1,reg1,4), reg1}
  399. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).Word1);
  400. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  401. TmpRef^.ScaleFactor := 4;
  402. If (Pai386(p)^.op3t = Top_None)
  403. Then hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, TRegister(Pai386(p)^.op2)))
  404. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  405. TRegister(twowords(Pai386(p)^.op2).word2)));
  406. hp1^.fileinfo:= p^.fileinfo;
  407. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  408. Dispose(p, Done);
  409. p := hp1;
  410. End;
  411. 6: Begin
  412. {imul 6, reg1, reg2 to
  413. lea (,reg1,2), reg2
  414. lea (reg2,reg1,4), reg2
  415. imul 6, reg1 to
  416. lea (reg1,reg1,2), reg1
  417. add reg1, reg1}
  418. If (aktoptprocessor <= Class386)
  419. Then
  420. Begin
  421. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  422. If (Pai386(p)^.op3t = Top_Reg)
  423. Then
  424. Begin
  425. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).word2);
  426. TmpRef^.ScaleFactor := 4;
  427. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  428. TRegister(twowords(Pai386(p)^.op2).word2)));
  429. End
  430. Else
  431. Begin
  432. Dispose(TmpRef);
  433. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  434. TRegister(Pai386(p)^.op2),TRegister(Pai386(p)^.op2)));
  435. End;
  436. hp1^.fileinfo := p^.fileinfo;
  437. InsertLLItem(AsmL,p, p^.next, hp1);
  438. New(TmpRef);
  439. Reset_reference(TmpRef^);
  440. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  441. TmpRef^.ScaleFactor := 2;
  442. If (Pai386(p)^.op3t = Top_Reg)
  443. Then
  444. Begin
  445. TmpRef^.base := R_NO;
  446. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  447. TRegister(twowords(Pai386(p)^.op2).word2)));
  448. End
  449. Else
  450. Begin
  451. TmpRef^.base := TRegister(Pai386(p)^.op2);
  452. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, TRegister(Pai386(p)^.op2)));
  453. End;
  454. hp1^.fileinfo := p^.fileinfo;
  455. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  456. Dispose(p, Done);
  457. p := Pai(hp1^.next);
  458. End
  459. Else Dispose(TmpRef);
  460. End;
  461. 9: Begin
  462. {imul 9, reg1, reg2 to
  463. lea (reg1,reg1,8), reg2
  464. imul 9, reg1 to
  465. lea (reg1,reg1,8), reg1}
  466. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).Word1);
  467. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  468. TmpRef^.ScaleFactor := 8;
  469. If (Pai386(p)^.op3t = Top_None)
  470. Then hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef, TRegister(Pai386(p)^.op2)))
  471. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  472. TRegister(twowords(Pai386(p)^.op2).word2)));
  473. hp1^.fileinfo := p^.fileinfo;
  474. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  475. Dispose(p, Done);
  476. p := hp1;
  477. End;
  478. 10: Begin
  479. {imul 10, reg1, reg2 to
  480. lea (reg1,reg1,4), reg2
  481. add reg2, reg2
  482. imul 10, reg1 to
  483. lea (reg1,reg1,4), reg1
  484. add reg1, reg1}
  485. If (aktoptprocessor <= Class386) Then
  486. Begin
  487. If (Pai386(p)^.op3t = Top_Reg)
  488. Then
  489. hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  490. Tregister(twowords(Pai386(p)^.op2).word2),
  491. Tregister(twowords(Pai386(p)^.op2).word2)))
  492. Else hp1 := New(Pai386, op_reg_reg(A_ADD, S_L,
  493. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)));
  494. hp1^.fileinfo := p^.fileinfo;
  495. InsertLLItem(AsmL,p, p^.next, hp1);
  496. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).Word1);
  497. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  498. TmpRef^.ScaleFactor := 4;
  499. If (Pai386(p)^.op3t = Top_Reg)
  500. Then
  501. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  502. TRegister(twowords(Pai386(p)^.op2).word2)))
  503. Else
  504. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  505. TRegister(Pai386(p)^.op2)));
  506. hp1^.fileinfo := p^.fileinfo;
  507. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  508. Dispose(p, Done);
  509. p := Pai(hp1^.next);
  510. End
  511. Else Dispose(TmpRef);
  512. End;
  513. 12: Begin
  514. {imul 12, reg1, reg2 to
  515. lea (,reg1,4), reg2
  516. lea (,reg1,8) reg2
  517. imul 12, reg1 to
  518. lea (reg1,reg1,2), reg1
  519. lea (,reg1,4), reg1}
  520. If (aktoptprocessor <= Class386)
  521. Then
  522. Begin
  523. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  524. If (Pai386(p)^.op3t = Top_Reg)
  525. Then
  526. Begin
  527. TmpRef^.base := TRegister(twowords(Pai386(p)^.op2).word2);
  528. TmpRef^.ScaleFactor := 8;
  529. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  530. TRegister(twowords(Pai386(p)^.op2).word2)));
  531. End
  532. Else
  533. Begin
  534. TmpRef^.base := R_NO;
  535. TmpRef^.ScaleFactor := 4;
  536. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  537. TRegister(Pai386(p)^.op2)));
  538. End;
  539. hp1^.fileinfo := p^.fileinfo;
  540. InsertLLItem(AsmL,p, p^.next, hp1);
  541. New(TmpRef);
  542. Reset_reference(TmpRef^);
  543. TmpRef^.Index := TRegister(twowords(Pai386(p)^.op2).Word1);
  544. If (Pai386(p)^.op3t = Top_Reg)
  545. Then
  546. Begin
  547. TmpRef^.base := R_NO;
  548. TmpRef^.ScaleFactor := 4;
  549. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  550. TRegister(twowords(Pai386(p)^.op2).word2)));
  551. End
  552. Else
  553. Begin
  554. TmpRef^.base := TRegister(Pai386(p)^.op2);
  555. TmpRef^.ScaleFactor := 2;
  556. hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  557. TRegister(Pai386(p)^.op2)));
  558. End;
  559. hp1^.fileinfo := p^.fileinfo;
  560. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  561. Dispose(p, Done);
  562. p := Pai(hp1^.next);
  563. End
  564. Else Dispose(TmpRef);
  565. End
  566. Else Dispose(TmpRef);
  567. End;
  568. End;
  569. End;
  570. A_LEA:
  571. Begin
  572. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  573. If (PReference(Pai386(p)^.op1)^.Base In [R_EAX..R_EDI]) And
  574. (PReference(Pai386(p)^.op1)^.Index = R_NO) And
  575. (PReference(Pai386(p)^.op1)^.Offset = 0) And
  576. (Not(Assigned(PReference(Pai386(p)^.op1)^.Symbol))) Then
  577. If (PReference(Pai386(p)^.op1)^.Base <> TRegister(Pai386(p)^.op2))
  578. Then
  579. Begin
  580. hp1 := New(Pai386, op_reg_reg(A_MOV, S_L,PReference(Pai386(p)^.op1)^.Base,
  581. TRegister(Pai386(p)^.op2)));
  582. hp1^.fileinfo := p^.fileinfo;
  583. InsertLLItem(AsmL,p^.previous,p^.next, hp1);
  584. Dispose(p, Done);
  585. p := hp1;
  586. Continue;
  587. End
  588. Else
  589. Begin
  590. hp1 := Pai(p^.Next);
  591. AsmL^.Remove(p);
  592. Dispose(p, Done);
  593. p := hp1;
  594. Continue;
  595. End;
  596. End;
  597. A_MOV:
  598. Begin
  599. TmpUsedRegs := UsedRegs;
  600. If (Pai386(p)^.op2t = top_reg) And
  601. (TRegister(Pai386(p)^.op2) In [R_EAX, R_EBX, R_EDX, R_EDI]) And
  602. GetNextInstruction(p, hp1) And
  603. (Pai(hp1)^.typ = ait_instruction) And
  604. (Pai386(hp1)^.opcode = A_MOV) And
  605. (Pai386(hp1)^.op1t = top_reg) And
  606. (Pai386(hp1)^.op1 = Pai386(p)^.op2)
  607. Then
  608. {we have "mov x, %treg; mov %treg, y }
  609. (* If (GetNextInstruction(hp1, hp2) And
  610. (RegUsedAfterInstruction(TRegister(Pai386(hp1)^.op2), hp1, TmpUsedRegs) or
  611. {now TmpUsedRegs contains the regalloc data after hp1}
  612. (RegInInstruction(TRegister(Pai386(hp1)^.op2), hp2))) And
  613. Not(TRegister(Pai386(hp1)^.op1) in TmpUsedRegs)) *)
  614. If not(RegUsedAfterInstruction(TRegister(Pai386(p)^.op2), hp1, TmpUsedRegs))
  615. Then
  616. {we've got "mov x, %treg; mov %treg, y; where %treg is not used after that }
  617. Case Pai386(p)^.op1t Of
  618. top_reg:
  619. {change "mov %reg, %treg; mov %treg, y"
  620. to "mov %reg, y"}
  621. Begin
  622. Pai386(hp1)^.op1 := Pai386(p)^.op1;
  623. AsmL^.Remove(p);
  624. Dispose(p, Done);
  625. p := hp1;
  626. continue;
  627. End;
  628. top_ref:
  629. If (Pai386(hp1)^.op2t = top_reg)
  630. Then
  631. {change "mov mem, %treg; mov %treg, %reg"
  632. to "mov mem, %reg"}
  633. Begin
  634. Pai386(p)^.op2 := Pai386(hp1)^.op2;
  635. AsmL^.Remove(hp1);
  636. Dispose(hp1, Done);
  637. continue;
  638. End;
  639. End
  640. Else
  641. {remove an instruction which never makes sense: we've got
  642. "mov mem, %reg1; mov %reg1, %edi" and then EDI isn't used anymore!}
  643. { Begin
  644. If (TRegister(Pai386(hp1)^.op2) = R_EDI) And
  645. Not(GetNextInstruction(hp1, hp2) And
  646. (Pai(hp2)^.typ = ait_instruction) And
  647. (Pai386(hp2)^.op2t = top_reg) And
  648. (Pai386(hp2)^.op2 = Pointer(R_ESI))) Then
  649. Begin
  650. AsmL^.Remove(hp1);
  651. Dispose(hp1, Done);
  652. Continue;
  653. End
  654. End}
  655. Else
  656. {Change "mov %reg1, %reg2; xxx %reg2, ???" to
  657. "mov %reg1, %reg2; xxx %reg1, ???" to avoid a write/read
  658. penalty}
  659. If (Pai386(p)^.op1t = top_reg) And
  660. (Pai386(p)^.op2t = top_reg) And
  661. GetNextInstruction(p,hp1) And
  662. (Pai(hp1)^.typ = ait_instruction) And
  663. (Pai386(hp1)^.op1t = top_reg) And
  664. (Pai386(hp1)^.op1 = Pai386(p)^.op2)
  665. Then
  666. {we have "mov %reg1, %reg2; XXX %reg2, ???"}
  667. Begin
  668. If ((Pai386(hp1)^.opcode = A_OR) Or
  669. (Pai386(hp1)^.opcode = A_TEST)) And
  670. (Pai386(hp1)^.op2t = top_reg) And
  671. (Pai386(hp1)^.op1 = Pai386(hp1)^.op2)
  672. Then
  673. {we have "mov %reg1, %reg2; test/or %reg2, %reg2"}
  674. Begin
  675. TmpUsedRegs := UsedRegs;
  676. If GetNextInstruction(hp1, hp2) And
  677. (hp2^.typ = ait_labeled_instruction) And
  678. Not(RegUsedAfterInstruction(TRegister(Pai386(hp1)^.op1), hp1, TmpUsedRegs))
  679. Then
  680. {change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  681. "test %reg1, %reg1; jxx"}
  682. Begin
  683. Pai386(hp1)^.op1 := Pai386(p)^.op1;
  684. Pai386(hp1)^.op2 := Pai386(p)^.op1;
  685. AsmL^.Remove(p);
  686. Dispose(p, done);
  687. p := hp1;
  688. continue
  689. End
  690. Else
  691. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  692. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  693. Begin
  694. Pai386(hp1)^.op1 := Pai386(p)^.op1;
  695. Pai386(hp1)^.op2 := Pai386(p)^.op1;
  696. End;
  697. End
  698. { Else
  699. If (Pai386(p^.next)^.opcode
  700. In [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  701. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  702. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  703. End
  704. Else
  705. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  706. x >= RetOffset) as it doesn't do anything (it writes either to a
  707. parameter or to the temporary storage room for the function
  708. result)}
  709. If GetNextInstruction(p, hp1) And
  710. (Pai(hp1)^.typ = ait_instruction)
  711. Then
  712. If ((Pai386(hp1)^.opcode = A_LEAVE) Or
  713. (Pai386(hp1)^.opcode = A_RET)) And
  714. (Pai386(p)^.op2t = top_ref) And
  715. (TReference(Pai386(p)^.op2^).base = ProcInfo.FramePointer) And
  716. (TReference(Pai386(p)^.op2^).offset >= ProcInfo.RetOffset) And
  717. (TReference(Pai386(p)^.op2^).index = R_NO) And
  718. (Pai386(p)^.op1t = top_reg)
  719. Then
  720. Begin
  721. AsmL^.Remove(p);
  722. Dispose(p, done);
  723. p := hp1;
  724. End
  725. Else
  726. If (Pai386(p)^.op1t = top_reg) And
  727. (Pai386(p)^.op2t = top_ref) And
  728. (Pai386(hp1)^.opcode = A_CMP) And
  729. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  730. (Pai386(hp1)^.op2t = top_ref) And
  731. RefsEqual(TReference(Pai386(p)^.op2^),
  732. TReference(Pai386(hp1)^.op2^))
  733. Then
  734. {change "mov reg, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  735. Begin
  736. Dispose(PReference(Pai386(hp1)^.op2));
  737. Pai386(hp1)^.opxt := Pai386(hp1)^.op1t + (top_reg shl 4);
  738. Pai386(hp1)^.op2 := Pai386(p)^.op1
  739. End;
  740. { Next instruction is also a MOV ? }
  741. If GetNextInstruction(p, hp1) And
  742. (pai(hp1)^.typ = ait_instruction) and
  743. (Pai386(hp1)^.opcode = A_MOV) and
  744. (Pai386(hp1)^.opsize = Pai386(p)^.opsize)
  745. Then
  746. Begin
  747. If (Pai386(hp1)^.op1t = Pai386(p)^.op2t) and
  748. (Pai386(hp1)^.op2t = Pai386(p)^.op1t)
  749. Then
  750. {mov reg1, mem1 or mov mem1, reg1
  751. mov mem2, reg2 mov reg2, mem2}
  752. Begin
  753. If (Pai386(hp1)^.op2t = top_ref)
  754. Then
  755. TmpBool1 := RefsEqual(TReference(Pai386(hp1)^.op2^), TReference(Pai386(p)^.op1^))
  756. Else
  757. TmpBool1 := Pai386(hp1)^.op2 = Pai386(p)^.op1;
  758. If TmpBool1 Then
  759. {mov reg1, mem1 or mov mem1, reg1
  760. mov mem2, reg1 mov reg2, mem1}
  761. Begin
  762. If (Pai386(hp1)^.op1t = top_ref) Then
  763. TmpBool1 := RefsEqual(TReference(Pai386(hp1)^.op1^),
  764. TReference(Pai386(p)^.op2^))
  765. Else TmpBool1 := (Pai386(hp1)^.op1 = Pai386(p)^.op2);
  766. If TmpBool1 Then
  767. { Removes the second statement from
  768. mov reg1, mem1
  769. mov mem1, reg1 }
  770. Begin
  771. AsmL^.remove(hp1);
  772. Dispose(hp1,done);
  773. End
  774. Else
  775. Begin
  776. TmpUsedRegs := UsedRegs;
  777. UpdateUsedRegs(TmpUsedRegs, Pai(hp1^.next));
  778. If (Pai386(p)^.op1t = top_reg) And
  779. { mov reg1, mem1
  780. mov mem2, reg1 }
  781. GetNextInstruction(hp1, hp2) And
  782. (hp2^.typ = ait_instruction) And
  783. (Pai386(hp2)^.opcode = A_CMP) And
  784. (Pai386(hp2)^.opsize = Pai386(p)^.opsize) and
  785. (Pai386(hp2)^.Op1t = TOp_Ref) And
  786. (Pai386(hp2)^.Op2t = TOp_Reg) And
  787. RefsEqual(TReference(Pai386(hp2)^.Op1^),
  788. TReference(Pai386(p)^.Op2^)) And
  789. (Pai386(hp2)^.Op2 = Pai386(p)^.Op1) And
  790. Not(RegUsedAfterInstruction(TRegister(Pai386(p)^.Op1),
  791. hp2, TmpUsedRegs)) Then
  792. { change to
  793. mov reg1, mem1 mov reg1, mem1
  794. mov mem2, reg1 cmp reg1, mem2
  795. cmp mem1, reg1 }
  796. Begin
  797. AsmL^.Remove(hp2);
  798. Dispose(hp2, Done);
  799. Pai386(hp1)^.opcode := A_CMP;
  800. Pai386(hp1)^.Opxt := top_reg + top_ref shl 4;
  801. Pai386(hp1)^.Op2 := Pai386(hp1)^.Op1;
  802. Pai386(hp1)^.Op1 := Pai386(p)^.Op1
  803. End;
  804. End;
  805. End
  806. Else
  807. Begin
  808. If GetNextInstruction(hp1, hp2) And
  809. (Pai386(p)^.op1t = top_ref) And
  810. (Pai386(p)^.op2t = top_reg) And
  811. (Pai386(hp1)^.op1t = top_reg) And
  812. (Pai386(hp1)^.op1 = Pai386(p)^.op2) And
  813. (Pai386(hp1)^.op2t = top_ref) And
  814. (Pai(hp2)^.typ = ait_instruction) And
  815. (Pai386(hp2)^.opcode = A_MOV) And
  816. (Pai386(hp2)^.opsize = Pai386(p)^.opsize) and
  817. (Pai386(hp2)^.op2t = top_reg) And
  818. (Pai386(hp2)^.op1t = top_ref) And
  819. RefsEqual(TReference(Pai386(hp2)^.op1^),
  820. TReference(Pai386(hp1)^.op2^))
  821. Then
  822. If (TRegister(Pai386(p)^.op2) in [R_DI,R_EDI])
  823. Then
  824. { mov mem1, %edi
  825. mov %edi, mem2
  826. mov mem2, reg2
  827. to:
  828. mov mem1, reg2
  829. mov reg2, mem2}
  830. Begin
  831. Pai386(p)^.op2 := Pai386(hp2)^.op2;
  832. Pai386(hp1)^.op1 := Pai386(hp2)^.op2;
  833. AsmL^.Remove(hp2);
  834. Dispose(hp2,Done);
  835. End
  836. Else
  837. { mov mem1, reg1 mov mem1, reg1
  838. mov reg1, mem2 mov reg1, mem2
  839. mov mem2, reg2 mov mem2, reg1
  840. to: to:
  841. mov mem1, reg1 mov mem1, reg1
  842. mov mem1, reg2 mov reg1, mem2
  843. mov reg1, mem2}
  844. Begin
  845. If (Pai386(p)^.op2 <> Pai386(hp2)^.op2) Then
  846. Begin
  847. Pai386(hp1)^.opxt := top_ref + top_reg shl 4;
  848. Pai386(hp1)^.op1 := Pai386(hp1)^.op2; {move the treference}
  849. TReference(Pai386(hp1)^.op1^) := TReference(Pai386(p)^.op1^);
  850. Pai386(hp1)^.op2 := Pai386(hp2)^.op2;
  851. End
  852. Else
  853. Begin
  854. AsmL^.Remove(hp1);
  855. Dispose(hp1, Done)
  856. End;
  857. Pai386(hp2)^.opxt := top_reg + top_ref shl 4;
  858. Pai386(hp2)^.op2 := Pai386(hp2)^.op1;
  859. Pai386(hp2)^.op1 := Pai386(p)^.op2;
  860. End;
  861. End;
  862. End
  863. Else
  864. (* {movl [mem1],reg1
  865. movl [mem1],reg2
  866. to:
  867. movl [mem1],reg1
  868. movl reg1,reg2 }
  869. If (Pai386(p)^.op1t = top_ref) and
  870. (Pai386(p)^.op2t = top_reg) and
  871. (Pai386(hp1)^.op1t = top_ref) and
  872. (Pai386(hp1)^.op2t = top_reg) and
  873. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) and
  874. RefsEqual(TReference(Pai386(p)^.op1^),TReference(Pai386(hp1)^.op1^)) and
  875. (TRegister(Pai386(p)^.op2)<>TReference(Pai386(hp1)^.op1^).base) and
  876. (TRegister(Pai386(p)^.op2)<>TReference(Pai386(hp1)^.op1^).index) then
  877. Begin
  878. Dispose(PReference(Pai386(hp1)^.op1));
  879. Pai386(hp1)^.op1:=Pai386(p)^.op2;
  880. Pai386(hp1)^.opxt:=Top_reg+Top_reg shl 4;
  881. End
  882. Else*)
  883. { movl const1,[mem1]
  884. movl [mem1],reg1
  885. to:
  886. movl const1,reg1
  887. movl reg1,[mem1] }
  888. If (Pai386(p)^.op1t = top_const) and
  889. (Pai386(p)^.op2t = top_ref) and
  890. (Pai386(hp1)^.op1t = top_ref) and
  891. (Pai386(hp1)^.op2t = top_reg) and
  892. (Pai386(p)^.opsize = Pai386(hp1)^.opsize) and
  893. RefsEqual(TReference(Pai386(hp1)^.op1^),TReference(Pai386(p)^.op2^)) then
  894. Begin
  895. Pai386(hp1)^.op1:=Pai386(hp1)^.op2;
  896. Pai386(hp1)^.op2:=Pai386(p)^.op2;
  897. Pai386(hp1)^.opxt:=Top_reg+Top_ref shl 4;
  898. Pai386(p)^.op2:=Pai386(hp1)^.op1;
  899. Pai386(p)^.opxt:=Top_const+(top_reg shl 4);
  900. End
  901. End;
  902. {changes "mov $0, %reg" into "xor %reg, %reg"}
  903. If (Pai386(p)^.op1t = Top_Const) And
  904. (Pai386(p)^.op1 = Pointer(0)) And
  905. (Pai386(p)^.op2t = Top_Reg)
  906. Then
  907. Begin
  908. Pai386(p)^.opcode := A_XOR;
  909. Pai386(p)^.opxt := Top_Reg+Top_reg shl 4;
  910. Pai386(p)^.op1 := Pai386(p)^.op2;
  911. End;
  912. End;
  913. A_MOVZX:
  914. Begin
  915. {removes superfluous And's after movzx's}
  916. If (Pai386(p)^.op2t = top_reg) And
  917. GetNextInstruction(p, hp1) And
  918. (Pai(hp1)^.typ = ait_instruction) And
  919. (Pai386(hp1)^.opcode = A_AND) And
  920. (Pai386(hp1)^.op1t = top_const) And
  921. (Pai386(hp1)^.op2t = top_reg) And
  922. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  923. Then
  924. Case Pai386(p)^.opsize Of
  925. S_BL, S_BW:
  926. If (Longint(Pai386(hp1)^.op1) = $ff)
  927. Then
  928. Begin
  929. AsmL^.Remove(hp1);
  930. Dispose(hp1, Done);
  931. End;
  932. S_WL:
  933. If (Longint(Pai386(hp1)^.op1) = $ffff)
  934. Then
  935. Begin
  936. AsmL^.Remove(hp1);
  937. Dispose(hp1, Done);
  938. End;
  939. End;
  940. {changes some movzx constructs to faster synonims (all examples
  941. are given with eax/ax, but are also valid for other registers)}
  942. If (Pai386(p)^.op2t = top_reg) Then
  943. If (Pai386(p)^.op1t = top_reg)
  944. Then
  945. Case Pai386(p)^.opsize of
  946. S_BW:
  947. Begin
  948. If (TRegister(Pai386(p)^.op1) = Reg16ToReg8(TRegister(Pai386(p)^.op2))) And
  949. Not(CS_LittleSize In aktglobalswitches)
  950. Then
  951. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  952. Begin
  953. Pai386(p)^.opcode := A_AND;
  954. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  955. Longint(Pai386(p)^.op1) := $ff;
  956. Pai386(p)^.opsize := S_W
  957. End
  958. Else
  959. If GetNextInstruction(p, hp1) And
  960. (Pai(hp1)^.typ = ait_instruction) And
  961. (Pai386(hp1)^.opcode = A_AND) And
  962. (Pai386(hp1)^.op1t = top_const) And
  963. (Pai386(hp1)^.op2t = top_reg) And
  964. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  965. Then
  966. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  967. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  968. Begin
  969. Pai386(p)^.opcode := A_MOV;
  970. Pai386(p)^.opsize := S_W;
  971. Pai386(p)^.op1 := Pointer(Reg8ToReg16(TRegister(Pai386(p)^.op1)));
  972. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ff);
  973. End;
  974. End;
  975. S_BL:
  976. Begin
  977. If (TRegister(Pai386(p)^.op1) = Reg32ToReg8(TRegister(Pai386(p)^.op2))) And
  978. Not(CS_LittleSize in aktglobalswitches)
  979. Then
  980. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  981. Begin
  982. Pai386(p)^.opcode := A_AND;
  983. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  984. Longint(Pai386(p)^.op1) := $ff;
  985. Pai386(p)^.opsize := S_L;
  986. End
  987. Else
  988. If GetNextInstruction(p, hp1) And
  989. (Pai(hp1)^.typ = ait_instruction) And
  990. (Pai386(hp1)^.opcode = A_AND) And
  991. (Pai386(hp1)^.op1t = top_const) And
  992. (Pai386(hp1)^.op2t = top_reg) And
  993. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  994. Then
  995. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  996. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  997. Begin
  998. Pai386(p)^.opcode := A_MOV;
  999. Pai386(p)^.opsize := S_L;
  1000. Pai386(p)^.op1 := Pointer(Reg8ToReg32(TRegister(Pai386(p)^.op1)));
  1001. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ff);
  1002. End
  1003. End;
  1004. S_WL:
  1005. Begin
  1006. If (TRegister(Pai386(p)^.op1) = Reg32ToReg16(TRegister(Pai386(p)^.op2))) And
  1007. Not(CS_LittleSize In aktglobalswitches)
  1008. Then
  1009. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  1010. Begin
  1011. Pai386(p)^.opcode := A_AND;
  1012. Pai386(p)^.opxt := top_const+Top_reg shl 4;
  1013. Longint(Pai386(p)^.op1) := $ffff;
  1014. Pai386(p)^.opsize := S_L
  1015. End
  1016. Else
  1017. If GetNextInstruction(p, hp1) And
  1018. (Pai(hp1)^.typ = ait_instruction) And
  1019. (Pai386(hp1)^.opcode = A_AND) And
  1020. (Pai386(hp1)^.op1t = top_const) And
  1021. (Pai386(hp1)^.op2t = top_reg) And
  1022. (Pai386(hp1)^.op2 = Pai386(p)^.op2)
  1023. Then
  1024. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  1025. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  1026. Begin
  1027. Pai386(p)^.opcode := A_MOV;
  1028. Pai386(p)^.opsize := S_L;
  1029. Pai386(p)^.op1 := Pointer(Reg16ToReg32(TRegister(Pai386(p)^.op1)));
  1030. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) And $ffff);
  1031. End;
  1032. End;
  1033. End
  1034. Else
  1035. If (Pai386(p)^.op1t = top_ref) Then
  1036. Begin
  1037. If GetNextInstruction(p, hp1) And
  1038. (Pai(hp1)^.typ = ait_instruction) And
  1039. (Pai386(hp1)^.opcode = A_AND) And
  1040. (Pai386(hp1)^.op1t = Top_Const) And
  1041. (Pai386(hp1)^.op2t = Top_Reg) And
  1042. (Pai386(hp1)^.op2 = Pai386(p)^.op2) Then
  1043. Begin
  1044. Pai386(p)^.opcode := A_MOV;
  1045. Case Pai386(p)^.opsize Of
  1046. S_BL:
  1047. Begin
  1048. Pai386(p)^.opsize := S_L;
  1049. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  1050. And $ff);
  1051. End;
  1052. S_WL:
  1053. Begin
  1054. Pai386(p)^.opsize := S_L;
  1055. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  1056. And $ffff);
  1057. End;
  1058. S_BW:
  1059. Begin
  1060. Pai386(p)^.opsize := S_W;
  1061. Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1)
  1062. And $ff);
  1063. End;
  1064. End;
  1065. End;
  1066. End;
  1067. End;
  1068. A_POP:
  1069. Begin
  1070. if (Pai386(p)^.op1t = top_reg) And
  1071. GetNextInstruction(p, hp1) And
  1072. (pai(hp1)^.typ=ait_instruction) and
  1073. (Pai386(hp1)^.opcode=A_PUSH) and
  1074. (Pai386(hp1)^.op1t = top_reg) And
  1075. (Pai386(hp1)^.op1=Pai386(p)^.op1) then
  1076. If (Not(cs_regalloc in aktglobalswitches)) Then
  1077. Begin
  1078. hp2:=pai(hp1^.next);
  1079. asml^.remove(p);
  1080. asml^.remove(hp1);
  1081. dispose(p,done);
  1082. dispose(hp1,done);
  1083. p:=hp2;
  1084. continue
  1085. End
  1086. Else
  1087. Begin
  1088. Pai386(p)^.opcode := A_MOV;
  1089. Pai386(p)^.op2 := Pai386(p)^.op1;
  1090. Pai386(p)^.opxt := top_ref + top_reg shl 4;
  1091. New(TmpRef);
  1092. Reset_reference(TmpRef^);
  1093. TmpRef^.base := R_ESP;
  1094. Pai386(p)^.op1 := Pointer(TmpRef);
  1095. hp1 := Pai(p^.next);
  1096. AsmL^.Remove(hp1);
  1097. Dispose(hp1, Done)
  1098. End
  1099. end;
  1100. A_PUSH:
  1101. Begin
  1102. If (Pai386(p)^.opsize = S_W) And
  1103. (Pai386(p)^.op1t = Top_Const) And
  1104. GetNextInstruction(p, hp1) And
  1105. (Pai(hp1)^.typ = ait_instruction) And
  1106. (Pai386(hp1)^.opcode = A_PUSH) And
  1107. (Pai386(hp1)^.op1t = Top_Const) And
  1108. (Pai386(hp1)^.opsize = S_W) Then
  1109. Begin
  1110. Pai386(p)^.opsize := S_L;
  1111. Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) shl 16 + Longint(Pai386(hp1)^.op1));
  1112. AsmL^.Remove(hp1);
  1113. Dispose(hp1, Done)
  1114. End;
  1115. End;
  1116. A_SHL, A_SAL:
  1117. Begin
  1118. If (Pai386(p)^.op1t = Top_Const) And
  1119. (Pai386(p)^.op2t = Top_Reg) And
  1120. (Pai386(p)^.opsize = S_L) And
  1121. (Longint(Pai386(p)^.op1) <= 3)
  1122. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1123. Then
  1124. Begin
  1125. TmpBool1 := True; {should we check the next instruction?}
  1126. TmpBool2 := False; {have we found an add/sub which could be
  1127. integrated in the lea?}
  1128. New(TmpRef);
  1129. Reset_reference(TmpRef^);
  1130. TmpRef^.index := TRegister(Pai386(p)^.op2);
  1131. TmpRef^.scalefactor := 1 shl Longint(Pai386(p)^.op1);
  1132. While TmpBool1 And
  1133. GetNextInstruction(p, hp1) And
  1134. (Pai(hp1)^.typ = ait_instruction) And
  1135. ((Pai386(hp1)^.opcode = A_ADD) Or
  1136. (Pai386(hp1)^.opcode = A_SUB)) And
  1137. (Pai386(hp1)^.op2t = Top_Reg) And
  1138. (Pai386(hp1)^.op2 = Pai386(p)^.op2) Do
  1139. Begin
  1140. TmpBool1 := False;
  1141. If (Pai386(hp1)^.op1t = Top_Const)
  1142. Then
  1143. Begin
  1144. TmpBool1 := True;
  1145. TmpBool2 := True;
  1146. If Pai386(hp1)^.opcode = A_ADD
  1147. Then Inc(TmpRef^.offset, Longint(Pai386(hp1)^.op1))
  1148. Else Dec(TmpRef^.offset, Longint(Pai386(hp1)^.op1));
  1149. AsmL^.Remove(hp1);
  1150. Dispose(hp1, Done);
  1151. End
  1152. Else
  1153. If (Pai386(hp1)^.op1t = Top_Reg) And
  1154. (Pai386(hp1)^.opcode = A_ADD) And
  1155. (TmpRef^.base = R_NO) Then
  1156. Begin
  1157. TmpBool1 := True;
  1158. TmpBool2 := True;
  1159. TmpRef^.base := TRegister(Pai386(hp1)^.op1);
  1160. AsmL^.Remove(hp1);
  1161. Dispose(hp1, Done);
  1162. End;
  1163. End;
  1164. If TmpBool2 Or
  1165. ((aktoptprocessor < ClassP6) And
  1166. (Longint(Pai386(p)^.op1) <= 3) And
  1167. Not(CS_LittleSize in aktglobalswitches))
  1168. Then
  1169. Begin
  1170. If Not(TmpBool2) And
  1171. (Longint(Pai386(p)^.op1) = 1)
  1172. Then
  1173. Begin
  1174. Dispose(TmpRef);
  1175. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.opsize,
  1176. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)))
  1177. End
  1178. Else hp1 := New(Pai386, op_ref_reg(A_LEA, S_L, TmpRef,
  1179. TRegister(Pai386(p)^.op2)));
  1180. hp1^.fileinfo := p^.fileinfo;
  1181. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1182. Dispose(p, Done);
  1183. p := hp1;
  1184. End;
  1185. End
  1186. Else
  1187. If (aktoptprocessor < ClassP6) And
  1188. (Pai386(p)^.op1t = top_const) And
  1189. (Pai386(p)^.op2t = top_reg) Then
  1190. If (Longint(Pai386(p)^.op1) = 1)
  1191. Then
  1192. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1193. but faster on a 486, and pairable in both U and V pipes on the Pentium
  1194. (unlike shl, which is only pairable in the U pipe)}
  1195. Begin
  1196. hp1 := new(Pai386,op_reg_reg(A_ADD,Pai386(p)^.opsize,
  1197. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)));
  1198. hp1^.fileinfo := p^.fileinfo;
  1199. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1200. Dispose(p, done);
  1201. p := hp1;
  1202. End
  1203. Else If (Pai386(p)^.opsize = S_L) and
  1204. (Longint(Pai386(p)^.op1) <= 3) Then
  1205. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1206. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1207. Begin
  1208. New(TmpRef);
  1209. Reset_reference(TmpRef^);
  1210. TmpRef^.index := TRegister(Pai386(p)^.op2);
  1211. TmpRef^.scalefactor := 1 shl Longint(Pai386(p)^.op1);
  1212. hp1 := new(Pai386,op_ref_reg(A_LEA,S_L,TmpRef, TRegister(Pai386(p)^.op2)));
  1213. hp1^.fileinfo := p^.fileinfo;
  1214. InsertLLItem(AsmL,p^.previous, p^.next, hp1);
  1215. Dispose(p, done);
  1216. p := hp1;
  1217. End
  1218. End;
  1219. A_SAR, A_SHR:
  1220. {changes the code sequence
  1221. shr/sar const1, x
  1222. shl const2, x
  1223. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  1224. Begin
  1225. If GetNextInstruction(p, hp1) And
  1226. (pai(hp1)^.typ = ait_instruction) and
  1227. (Pai386(hp1)^.opcode = A_SHL) and
  1228. (Pai386(p)^.op1t = top_const) and
  1229. (Pai386(hp1)^.op1t = top_const) and
  1230. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) And
  1231. (Pai386(hp1)^.op2t = Pai386(p)^.op2t) And
  1232. OpsEqual(Pai386(hp1)^.op2t, Pai386(hp1)^.Op2, Pai386(p)^.Op2)
  1233. Then
  1234. If (Longint(Pai386(p)^.op1) > Longint(Pai386(hp1)^.op1)) And
  1235. Not(CS_LittleSize In aktglobalswitches)
  1236. Then
  1237. { shr/sar const1, %reg
  1238. shl const2, %reg
  1239. with const1 > const2 }
  1240. Begin
  1241. Dec(Longint(Pai386(p)^.op1), Longint(Pai386(hp1)^.op1));
  1242. Pai386(hp1)^.opcode := A_And;
  1243. Pai386(hp1)^.op1 := Pointer(1 shl Longint(Pai386(hp1)^.op1)-1);
  1244. Case Pai386(p)^.opsize Of
  1245. S_L: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffffffff);
  1246. S_B: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ff);
  1247. S_W: Pai386(hp1)^.op1 := Pointer(Longint(Pai386(hp1)^.op1) Xor $ffff);
  1248. End;
  1249. End
  1250. Else
  1251. If (Longint(Pai386(p)^.op1) < Longint(Pai386(hp1)^.op1)) And
  1252. Not(CS_LittleSize In aktglobalswitches)
  1253. Then
  1254. { shr/sar const1, %reg
  1255. shl const2, %reg
  1256. with const1 < const2 }
  1257. Begin
  1258. Dec(Longint(Pai386(hp1)^.op1), Longint(Pai386(p)^.op1));
  1259. Pai386(p)^.opcode := A_And;
  1260. Pai386(p)^.op1 := Pointer(1 shl Longint(Pai386(p)^.op1)-1);
  1261. Case Pai386(p)^.opsize Of
  1262. S_L: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ffffffff);
  1263. S_B: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ff);
  1264. S_W: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ffff);
  1265. End;
  1266. End
  1267. Else
  1268. { shr/sar const1, %reg
  1269. shl const2, %reg
  1270. with const1 = const2 }
  1271. Begin
  1272. Pai386(p)^.opcode := A_And;
  1273. Pai386(p)^.op1 := Pointer(1 shl Longint(Pai386(p)^.op1)-1);
  1274. Case Pai386(p)^.opsize Of
  1275. S_B: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ff);
  1276. S_W: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ffff);
  1277. S_L: Pai386(p)^.op1 := Pointer(Longint(Pai386(p)^.op1) Xor $ffffffff);
  1278. End;
  1279. AsmL^.remove(hp1);
  1280. dispose(hp1, done);
  1281. End;
  1282. End;
  1283. A_SETE..A_SETGE,A_SETC,A_SETNC,A_SETA..A_SETBE,A_SETO..A_SETNLE:
  1284. Begin
  1285. If (Pai386(p)^.Op1t = top_ref) And
  1286. GetNextInstruction(p, hp1) And
  1287. GetNextInstruction(hp1, hp2) And
  1288. (hp2^.typ = ait_instruction) And
  1289. ((Pai386(hp2)^.opcode = A_LEAVE) or
  1290. (Pai386(hp2)^.opcode = A_RET)) And
  1291. (TReference(Pai386(p)^.Op1^).Base = ProcInfo.FramePointer) And
  1292. (TReference(Pai386(p)^.Op1^).Index = R_NO) And
  1293. (TReference(Pai386(p)^.Op1^).Offset >= ProcInfo.RetOffset) And
  1294. (hp1^.typ = ait_instruction) And
  1295. (Pai386(hp1)^.opcode = A_MOV) And
  1296. (Pai386(hp1)^.opsize = S_B) And
  1297. (Pai386(hp1)^.Op1t = top_ref) And
  1298. RefsEqual(TReference(Pai386(hp1)^.Op1^), TReference(Pai386(p)^.Op1^)) Then
  1299. Begin
  1300. Dispose(PReference(Pai386(p)^.Op1));
  1301. Pai386(p)^.Op1 := Pai386(hp1)^.Op2;
  1302. Pai386(p)^.Opxt := top_reg;
  1303. AsmL^.Remove(hp1);
  1304. Dispose(hp1, Done)
  1305. End
  1306. End;
  1307. A_SUB:
  1308. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1309. { * change "sub/add const1, reg" or "dec reg" followed by
  1310. "sub const2, reg" to one "sub ..., reg" }
  1311. Begin
  1312. If (Pai386(p)^.op1t = top_const) And
  1313. (Pai386(p)^.op2t = top_reg) Then
  1314. If (Longint(Pai386(p)^.op1) = 2) And
  1315. (TRegister(Pai386(p)^.op2) = R_ESP) Then
  1316. Begin
  1317. hp1 := Pai(p^.next);
  1318. While Assigned(hp1) And
  1319. (Pai(hp1)^.typ In [ait_instruction]+SkipInstr) And
  1320. Not((Pai(hp1)^.typ = ait_instruction) And
  1321. ((Pai386(hp1)^.opcode = A_CALL) or
  1322. (Pai386(hp1)^.opcode = A_PUSH) or
  1323. ((Pai386(hp1)^.opcode = A_MOV) And
  1324. (Pai386(hp1)^.op2t = top_ref) And
  1325. (TReference(Pai386(hp1)^.op2^).base = R_ESP)))) do
  1326. hp1 := Pai(hp1^.next);
  1327. If Assigned(hp1) And
  1328. (Pai(hp1)^.typ = ait_instruction) And
  1329. (Pai386(hp1)^.opcode = A_PUSH) And
  1330. (Pai386(hp1)^.opsize = S_W)
  1331. Then
  1332. Begin
  1333. Pai386(hp1)^.opsize := S_L;
  1334. If (Pai386(hp1)^.op1t = top_reg) Then
  1335. Pai386(hp1)^.op1 := Pointer(Reg16ToReg32(TRegister(Pai386(hp1)^.op1)));
  1336. hp1 := Pai(p^.next);
  1337. AsmL^.Remove(p);
  1338. Dispose(p, Done);
  1339. p := hp1;
  1340. Continue
  1341. End
  1342. Else
  1343. If GetLastInstruction(p, hp1) And
  1344. (Pai(hp1)^.typ = ait_instruction) And
  1345. (Pai386(hp1)^.opcode = A_SUB) And
  1346. (Pai386(hp1)^.op1t = top_const) And
  1347. (Pai386(hp1)^.op2t = top_reg) And
  1348. (TRegister(Pai386(hp1)^.Op2) = R_ESP)
  1349. Then
  1350. Begin
  1351. Inc(Longint(Pai386(p)^.op1), Longint(Pai386(hp1)^.op1));
  1352. AsmL^.Remove(hp1);
  1353. Dispose(hp1, Done);
  1354. End;
  1355. End
  1356. Else
  1357. If GetLastInstruction(p, hp1) And
  1358. (hp1^.typ = ait_instruction) And
  1359. (Pai386(hp1)^.opsize = Pai386(p)^.opsize) then
  1360. Case Pai386(hp1)^.opcode Of
  1361. A_DEC:
  1362. If (Pai386(hp1)^.Op1t = top_reg) And
  1363. (Pai386(hp1)^.Op1 = Pai386(p)^.op2) Then
  1364. Begin
  1365. Inc(Longint(Pai386(p)^.Op1));
  1366. AsmL^.Remove(hp1);
  1367. Dispose(hp1, Done)
  1368. End;
  1369. A_SUB:
  1370. If (Pai386(hp1)^.Op1t = top_const) And
  1371. (Pai386(hp1)^.Op2t = top_reg) And
  1372. (Pai386(hp1)^.Op2 = Pai386(p)^.Op2) Then
  1373. Begin
  1374. Inc(Longint(Pai386(p)^.Op1), Longint(Pai386(hp1)^.Op1));
  1375. AsmL^.Remove(hp1);
  1376. Dispose(hp1, Done)
  1377. End;
  1378. A_ADD:
  1379. If (Pai386(hp1)^.Op1t = top_const) And
  1380. (Pai386(hp1)^.Op2t = top_reg) And
  1381. (Pai386(hp1)^.Op2 = Pai386(p)^.Op2) Then
  1382. Begin
  1383. Dec(Longint(Pai386(p)^.Op1), Longint(Pai386(hp1)^.Op1));
  1384. AsmL^.Remove(hp1);
  1385. Dispose(hp1, Done)
  1386. End;
  1387. End
  1388. End;
  1389. A_TEST, A_OR:
  1390. {removes the line marked with (x) from the sequence
  1391. And/or/xor/add/sub/... $x, %y
  1392. test/or %y, %y (x)
  1393. j(n)z _Label
  1394. as the first instruction already adjusts the ZF}
  1395. Begin
  1396. If (Pai386(p)^.op1 = Pai386(p)^.op2) And
  1397. GetLastInstruction(p, hp1) And
  1398. (pai(hp1)^.typ = ait_instruction) Then
  1399. Case Pai386(hp1)^.opcode Of
  1400. A_ADD, A_SUB, A_OR, A_XOR, A_AND, A_SHL, A_SHR:
  1401. Begin
  1402. If (Pai386(hp1)^.op2 = Pai386(p)^.op1) and
  1403. (Pai386(hp1)^.op1t = pai386(p)^.op1t) Then
  1404. Begin
  1405. hp1 := pai(p^.next);
  1406. asml^.remove(p);
  1407. dispose(p, done);
  1408. p := pai(hp1);
  1409. continue
  1410. End;
  1411. End;
  1412. A_DEC, A_INC, A_NEG:
  1413. Begin
  1414. If (Pai386(hp1)^.op2 = Pai386(p)^.op1) and
  1415. (Pai386(hp1)^.op1t = pai386(p)^.op1t) Then
  1416. Begin
  1417. Case Pai386(hp1)^.opcode Of
  1418. A_DEC, A_INC:
  1419. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  1420. Begin
  1421. Pai386(hp1)^.opxt := Pai386(hp1)^.opxt shl 4 + top_const;
  1422. Pai386(hp1)^.Op2 := Pai386(hp1)^.Op1;
  1423. Pai386(hp1)^.Op1 := Pointer(1);
  1424. Case Pai386(hp1)^.opcode Of
  1425. A_DEC: Pai386(hp1)^.opcode := A_SUB;
  1426. A_INC: Pai386(hp1)^.opcode := A_ADD;
  1427. End
  1428. End
  1429. End;
  1430. hp1 := pai(p^.next);
  1431. asml^.remove(p);
  1432. dispose(p, done);
  1433. p := pai(hp1);
  1434. continue
  1435. End;
  1436. End
  1437. End;
  1438. End;
  1439. End;
  1440. End;
  1441. { ait_label:
  1442. Begin
  1443. If Not(Pai_Label(p)^.l^.is_used)
  1444. Then
  1445. Begin
  1446. hp1 := Pai(p^.next);
  1447. AsmL^.Remove(p);
  1448. Dispose(p, Done);
  1449. p := hp1;
  1450. Continue
  1451. End;
  1452. End;}
  1453. End;
  1454. p:=pai(p^.next);
  1455. end;
  1456. end;
  1457. Procedure PeepHoleOptPass2(AsmL: PAasmOutput; BlockStart, BlockEnd: Pai);
  1458. var
  1459. p,hp1,hp2: pai;
  1460. Begin
  1461. P := BlockStart;
  1462. While (P <> BlockEnd) Do
  1463. Begin
  1464. Case P^.Typ Of
  1465. Ait_Instruction:
  1466. Begin
  1467. Case Pai386(p)^.opcode Of
  1468. A_CALL:
  1469. If (AktOptProcessor < ClassP6) And
  1470. GetNextInstruction(p, hp1) And
  1471. (hp1^.typ = ait_labeled_instruction) And
  1472. (pai386_labeled(hp1)^.opcode = A_JMP) Then
  1473. Begin
  1474. hp2 := New(Pai386,op_sym(A_PUSH,S_L,NewAsmSymbol(Lab2Str(pai386_labeled(hp1)^.lab))));
  1475. hp2^.fileinfo := p^.fileinfo;
  1476. InsertLLItem(AsmL, p^.previous, p, hp2);
  1477. Pai386(p)^.opcode := A_JMP;
  1478. AsmL^.Remove(hp1);
  1479. Dispose(hp1, Done)
  1480. End;
  1481. A_MOV:
  1482. Begin
  1483. If (Pai386(p)^.op1t = top_reg) And
  1484. (Pai386(p)^.op2t = top_reg) And
  1485. GetNextInstruction(p, hp1) And
  1486. (hp1^.typ = ait_Instruction) And
  1487. (Pai386(hp1)^.opcode = A_MOV) And
  1488. (Pai386(hp1)^.op1t = top_ref) And
  1489. (Pai386(hp1)^.op2t = top_reg) And
  1490. ((TReference(Pai386(hp1)^.op1^).Base = TRegister(Pai386(p)^.op2)) Or
  1491. (TReference(Pai386(hp1)^.op1^).Index = TRegister(Pai386(p)^.op2))) And
  1492. (TRegister(Pai386(hp1)^.op2) = TRegister(Pai386(p)^.op2)) Then
  1493. {mov reg1, reg2
  1494. mov (reg2, ..), reg2 to mov (reg1, ..), reg2}
  1495. Begin
  1496. If (TReference(Pai386(hp1)^.op1^).Base = TRegister(Pai386(p)^.op2)) Then
  1497. TReference(Pai386(hp1)^.op1^).Base := TRegister(Pai386(p)^.op1);
  1498. If (TReference(Pai386(hp1)^.op1^).Index = TRegister(Pai386(p)^.op2)) Then
  1499. TReference(Pai386(hp1)^.op1^).Index := TRegister(Pai386(p)^.op1);
  1500. AsmL^.Remove(p);
  1501. Dispose(p, Done);
  1502. p := hp1;
  1503. Continue;
  1504. End;
  1505. End;
  1506. A_MOVZX:
  1507. Begin
  1508. If (Pai386(p)^.op2t = top_reg) Then
  1509. If (Pai386(p)^.op1t = top_reg)
  1510. Then
  1511. Case Pai386(p)^.opsize of
  1512. S_BL:
  1513. Begin
  1514. If IsGP32Reg(TRegister(Pai386(p)^.op2)) And
  1515. Not(CS_LittleSize in aktglobalswitches) And
  1516. (aktoptprocessor = ClassP5)
  1517. Then
  1518. {Change "movzbl %reg1, %reg2" to
  1519. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1520. PentiumMMX}
  1521. Begin
  1522. hp1 := New(Pai386, op_reg_reg(A_XOR, S_L,
  1523. TRegister(Pai386(p)^.op2), TRegister(Pai386(p)^.op2)));
  1524. hp1^.fileinfo := p^.fileinfo;
  1525. InsertLLItem(AsmL,p^.previous, p, hp1);
  1526. Pai386(p)^.opcode := A_MOV;
  1527. Pai386(p)^.opsize := S_B;
  1528. Pai386(p)^.op2 :=
  1529. Pointer(Reg32ToReg8(TRegister(Pai386(p)^.op2)));
  1530. { Jonas
  1531. InsertLLItem(AsmL,p, p^.next, hp2);
  1532. I think you forgot to delete this line PM
  1533. Indeed, I had forgotten that one (JM) }
  1534. End;
  1535. End;
  1536. End
  1537. Else
  1538. If (Pai386(p)^.op1t = top_ref) And
  1539. (PReference(Pai386(p)^.op1)^.base <> TRegister(Pai386(p)^.op2)) And
  1540. (PReference(Pai386(p)^.op1)^.index <> TRegister(Pai386(p)^.op2)) And
  1541. Not(CS_LittleSize in aktglobalswitches) And
  1542. IsGP32Reg(TRegister(Pai386(p)^.op2)) And
  1543. (aktoptprocessor = ClassP5) And
  1544. (Pai386(p)^.opsize = S_BL)
  1545. Then
  1546. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1547. Pentium and PentiumMMX}
  1548. Begin
  1549. hp1 := New(Pai386,op_reg_reg(A_XOR, S_L, TRegister(Pai386(p)^.op2),
  1550. TRegister(Pai386(p)^.op2)));
  1551. hp1^.fileinfo := p^.fileinfo;
  1552. Pai386(p)^.opcode := A_MOV;
  1553. Pai386(p)^.opsize := S_B;
  1554. Pai386(p)^.op2 := Pointer(Reg32ToReg8(TRegister(Pai386(p)^.op2)));
  1555. InsertLLItem(AsmL,p^.previous, p, hp1);
  1556. End;
  1557. End;
  1558. End;
  1559. End;
  1560. End;
  1561. p := Pai(p^.next)
  1562. End;
  1563. End;
  1564. End.
  1565. {
  1566. $Log$
  1567. Revision 1.42 1999-04-10 16:15:04 peter
  1568. * fixed browcol
  1569. + -ar to show regalloc info in .s file
  1570. Revision 1.41 1999/04/09 08:33:05 peter
  1571. * fixed mov reg,treg;mov treg,x bug
  1572. Revision 1.40 1999/03/29 16:02:50 peter
  1573. * added type check for or/test x,x
  1574. * added size check for mov,mov,mov optimizes
  1575. Revision 1.39 1999/02/26 00:48:22 peter
  1576. * assembler writers fixed for ag386bin
  1577. Revision 1.38 1999/02/25 21:02:44 peter
  1578. * ag386bin updates
  1579. + coff writer
  1580. Revision 1.37 1999/02/22 02:15:30 peter
  1581. * updates for ag386bin
  1582. Revision 1.36 1999/01/04 22:04:15 jonas
  1583. + mov reg, mem1 to mov reg, mem1
  1584. mov mem2, reg cmp reg, mem2
  1585. cmp mem1, reg
  1586. # reg released
  1587. Revision 1.35 1999/01/04 12:58:55 jonas
  1588. * no fistp/fild optimization for S_IQ (fistq doesn't exist)
  1589. Revision 1.34 1998/12/29 18:48:17 jonas
  1590. + optimize pascal code surrounding assembler blocks
  1591. Revision 1.33 1998/12/23 15:16:21 jonas
  1592. * change "inc x/dec x; test x, x" to "add 1, x/sub 1,x" because inc and dec
  1593. don't affect the carry flag (test does). This *doesn't* fix the problem with
  1594. cardinal, that's a cg issue.
  1595. Revision 1.32 1998/12/16 12:09:29 jonas
  1596. * fixed fistp/fild optimization
  1597. Revision 1.31 1998/12/15 22:30:39 jonas
  1598. + change "sub/add const1, reg" or "dec reg" followed by "sub const2, reg" to one
  1599. "sub const3, reg"
  1600. * some small cleaning up
  1601. Revision 1.30 1998/12/15 15:43:20 jonas
  1602. * fixed bug in shr/shl optimization
  1603. Revision 1.29 1998/12/15 11:53:54 peter
  1604. * removed commentlevel
  1605. Revision 1.28 1998/12/14 22:01:45 jonas
  1606. - removed $ifdef ver0_99_11's
  1607. Revision 1.27 1998/12/11 00:03:35 peter
  1608. + globtype,tokens,version unit splitted from globals
  1609. Revision 1.26 1998/12/09 18:16:13 jonas
  1610. * corrected small syntax error in part between ifdef ver0_99_11
  1611. + added fistp/fild optimization between ifdef ver0_99_11
  1612. Revision 1.25 1998/12/02 16:23:29 jonas
  1613. * changed "if longintvar in set" to case or "if () or () .." statements
  1614. * tree.pas: changed inlinenumber (and associated constructor/vars) to a byte
  1615. Revision 1.24 1998/11/26 15:41:45 jonas
  1616. + change "setxx mem; movb mem, reg8" to "setxx reg8" if mem is a local
  1617. variable/parameter or function result (between $ifdef ver0_99_11)
  1618. Revision 1.23 1998/11/03 16:26:09 jonas
  1619. * "call x;jmp y" optimization not done anymore for P6 and equivalents
  1620. * made FPU optimizations simpler and more effective
  1621. Revision 1.22 1998/10/29 18:37:55 jonas
  1622. + change "call x; jmp y" to "push y; jmp x" (suggestion from Daniel)
  1623. Revision 1.19 1998/10/23 15:38:23 jonas
  1624. + some small FPU peephole optimizations (use value in FP regs instead of loading it
  1625. from memory if possible, mostly with var1+var1 and var1*var1)
  1626. Revision 1.18 1998/10/05 14:41:14 jonas
  1627. * fixed small memory leak
  1628. * fixed small inefficiency
  1629. * tested multiple line comments ability of my new MacCVS client :)
  1630. Revision 1.17 1998/10/02 17:29:56 jonas
  1631. + removal of "lea (reg), reg)", "imul $1, reg", change "mov reg1, reg2; mov (reg2), reg2" to "mov (reg1), reg2"
  1632. Revision 1.16 1998/10/01 20:19:57 jonas
  1633. * moved UpdateUsedRegs (+ bugfix) to daopt386
  1634. Revision 1.15 1998/09/30 12:18:29 peter
  1635. * fixed subl $2,esp;psuhw bug
  1636. Revision 1.14 1998/09/20 17:11:51 jonas
  1637. * released REGALLOC
  1638. Revision 1.13 1998/09/16 18:00:00 jonas
  1639. * optimizer now completely dependant on GetNext/GetLast instruction, works again with -dRegAlloc
  1640. Revision 1.12 1998/09/15 14:05:22 jonas
  1641. * fixed optimizer incompatibilities with freelabel code in psub
  1642. Revision 1.11 1998/08/28 10:57:02 peter
  1643. * removed warnings
  1644. Revision 1.10 1998/08/27 15:17:50 florian
  1645. * reinstated Jonas' bugfix
  1646. Revision 1.9 1998/08/25 16:58:59 pierre
  1647. * removed a line that add no sense and
  1648. introduce garbage in the asmlist
  1649. (uninitialized data !)
  1650. Revision 1.7 1998/08/19 16:07:53 jonas
  1651. * changed optimizer switches + cleanup of DestroyRefs in daopt386.pas
  1652. Revision 1.6 1998/08/10 14:50:14 peter
  1653. + localswitches, moduleswitches, globalswitches splitting
  1654. Revision 1.5 1998/08/06 19:40:28 jonas
  1655. * removed $ before and after Log in comment
  1656. Revision 1.4 1998/08/05 16:27:17 jonas
  1657. * fstp/fld bugfix (fstt does not exist)
  1658. Revision 1.3 1998/08/05 16:00:15 florian
  1659. * some fixes for ansi strings
  1660. * log to Log changed
  1661. }