popt386.pas 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Jonas Maebe
  3. This unit contains the peephole optimizer.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit popt386;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses Aasmbase,aasmtai,aasmcpu,verbose;
  21. procedure PrePeepHoleOpts(asml: taasmoutput; BlockStart, BlockEnd: tai);
  22. procedure PeepHoleOptPass1(asml: taasmoutput; BlockStart, BlockEnd: tai);
  23. procedure PeepHoleOptPass2(asml: taasmoutput; BlockStart, BlockEnd: tai);
  24. procedure PostPeepHoleOpts(asml: taasmoutput; BlockStart, BlockEnd: tai);
  25. implementation
  26. uses
  27. globtype,systems,
  28. globals,cgbase,procinfo,
  29. symsym,
  30. {$ifdef finaldestdebug}
  31. cobjects,
  32. {$endif finaldestdebug}
  33. cpuinfo,cpubase,cgutils,daopt386;
  34. function RegUsedAfterInstruction(reg: Tregister; p: tai; var UsedRegs: TRegSet): Boolean;
  35. var
  36. supreg: tsuperregister;
  37. begin
  38. supreg := getsupreg(reg);
  39. UpdateUsedRegs(UsedRegs, tai(p.Next));
  40. RegUsedAfterInstruction :=
  41. (supreg in UsedRegs) and
  42. (not(getNextInstruction(p,p)) or
  43. not(regLoadedWithNewValue(supreg,false,p)));
  44. end;
  45. function doFpuLoadStoreOpt(asmL: TAAsmoutput; var p: tai): boolean;
  46. { returns true if a "continue" should be done after this optimization }
  47. var hp1, hp2: tai;
  48. begin
  49. doFpuLoadStoreOpt := false;
  50. if (taicpu(p).oper[0]^.typ = top_ref) and
  51. getNextInstruction(p, hp1) and
  52. (hp1.typ = ait_instruction) and
  53. (((taicpu(hp1).opcode = A_FLD) and
  54. (taicpu(p).opcode = A_FSTP)) or
  55. ((taicpu(p).opcode = A_FISTP) and
  56. (taicpu(hp1).opcode = A_FILD))) and
  57. (taicpu(hp1).oper[0]^.typ = top_ref) and
  58. (taicpu(hp1).opsize = taicpu(p).opsize) and
  59. refsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  60. begin
  61. { replacing fstp f;fld f by fst f is only valid for extended because of rounding }
  62. if (taicpu(p).opsize=S_FX) and
  63. getNextInstruction(hp1, hp2) and
  64. (hp2.typ = ait_instruction) and
  65. ((taicpu(hp2).opcode = A_LEAVE) or
  66. (taicpu(hp2).opcode = A_RET)) and
  67. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  68. not(assigned(current_procinfo.procdef.funcretsym) and
  69. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  70. (taicpu(p).oper[0]^.ref^.index = NR_NO) then
  71. begin
  72. asml.remove(p);
  73. asml.remove(hp1);
  74. p.free;
  75. hp1.free;
  76. p := hp2;
  77. removeLastDeallocForFuncRes(asmL, p);
  78. doFPULoadStoreOpt := true;
  79. end
  80. { can't be done because the store operation rounds
  81. else
  82. { fst can't store an extended value! }
  83. if (taicpu(p).opsize <> S_FX) and
  84. (taicpu(p).opsize <> S_IQ) then
  85. begin
  86. if (taicpu(p).opcode = A_FSTP) then
  87. taicpu(p).opcode := A_FST
  88. else taicpu(p).opcode := A_FIST;
  89. asml.remove(hp1);
  90. hp1.free;
  91. end
  92. }
  93. end;
  94. end;
  95. procedure PrePeepHoleOpts(asml: taasmoutput; BlockStart, BlockEnd: tai);
  96. var
  97. p,hp1: tai;
  98. l: aint;
  99. tmpRef: treference;
  100. begin
  101. p := BlockStart;
  102. while (p <> BlockEnd) Do
  103. begin
  104. case p.Typ Of
  105. Ait_Instruction:
  106. begin
  107. case taicpu(p).opcode Of
  108. A_IMUL:
  109. {changes certain "imul const, %reg"'s to lea sequences}
  110. begin
  111. if (taicpu(p).oper[0]^.typ = Top_Const) and
  112. (taicpu(p).oper[1]^.typ = Top_Reg) and
  113. (taicpu(p).opsize = S_L) then
  114. if (taicpu(p).oper[0]^.val = 1) then
  115. if (taicpu(p).ops = 2) then
  116. {remove "imul $1, reg"}
  117. begin
  118. hp1 := tai(p.Next);
  119. asml.remove(p);
  120. p.free;
  121. p := hp1;
  122. continue;
  123. end
  124. else
  125. {change "imul $1, reg1, reg2" to "mov reg1, reg2"}
  126. begin
  127. hp1 := taicpu.Op_Reg_Reg(A_MOV, S_L, taicpu(p).oper[1]^.reg,taicpu(p).oper[2]^.reg);
  128. InsertLLItem(asml, p.previous, p.next, hp1);
  129. p.free;
  130. p := hp1;
  131. end
  132. else if
  133. ((taicpu(p).ops <= 2) or
  134. (taicpu(p).oper[2]^.typ = Top_Reg)) and
  135. (aktoptprocessor < ClassPentium2) and
  136. (taicpu(p).oper[0]^.val <= 12) and
  137. not(CS_LittleSize in aktglobalswitches) and
  138. (not(GetNextInstruction(p, hp1)) or
  139. {GetNextInstruction(p, hp1) and}
  140. not((tai(hp1).typ = ait_instruction) and
  141. ((taicpu(hp1).opcode=A_Jcc) and
  142. (taicpu(hp1).condition in [C_O,C_NO])))) then
  143. begin
  144. reference_reset(tmpref);
  145. case taicpu(p).oper[0]^.val Of
  146. 3: begin
  147. {imul 3, reg1, reg2 to
  148. lea (reg1,reg1,2), reg2
  149. imul 3, reg1 to
  150. lea (reg1,reg1,2), reg1}
  151. TmpRef.base := taicpu(p).oper[1]^.reg;
  152. TmpRef.index := taicpu(p).oper[1]^.reg;
  153. TmpRef.ScaleFactor := 2;
  154. if (taicpu(p).ops = 2) then
  155. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg)
  156. else
  157. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  158. InsertLLItem(asml,p.previous, p.next, hp1);
  159. p.free;
  160. p := hp1;
  161. end;
  162. 5: begin
  163. {imul 5, reg1, reg2 to
  164. lea (reg1,reg1,4), reg2
  165. imul 5, reg1 to
  166. lea (reg1,reg1,4), reg1}
  167. TmpRef.base := taicpu(p).oper[1]^.reg;
  168. TmpRef.index := taicpu(p).oper[1]^.reg;
  169. TmpRef.ScaleFactor := 4;
  170. if (taicpu(p).ops = 2) then
  171. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg)
  172. else
  173. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  174. InsertLLItem(asml,p.previous, p.next, hp1);
  175. p.free;
  176. p := hp1;
  177. end;
  178. 6: begin
  179. {imul 6, reg1, reg2 to
  180. lea (,reg1,2), reg2
  181. lea (reg2,reg1,4), reg2
  182. imul 6, reg1 to
  183. lea (reg1,reg1,2), reg1
  184. add reg1, reg1}
  185. if (aktoptprocessor <= Class386) then
  186. begin
  187. TmpRef.index := taicpu(p).oper[1]^.reg;
  188. if (taicpu(p).ops = 3) then
  189. begin
  190. TmpRef.base := taicpu(p).oper[2]^.reg;
  191. TmpRef.ScaleFactor := 4;
  192. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  193. end
  194. else
  195. begin
  196. hp1 := taicpu.op_reg_reg(A_ADD, S_L,
  197. taicpu(p).oper[1]^.reg,taicpu(p).oper[1]^.reg);
  198. end;
  199. InsertLLItem(asml,p, p.next, hp1);
  200. reference_reset(tmpref);
  201. TmpRef.index := taicpu(p).oper[1]^.reg;
  202. TmpRef.ScaleFactor := 2;
  203. if (taicpu(p).ops = 3) then
  204. begin
  205. TmpRef.base := NR_NO;
  206. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef,
  207. taicpu(p).oper[2]^.reg);
  208. end
  209. else
  210. begin
  211. TmpRef.base := taicpu(p).oper[1]^.reg;
  212. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  213. end;
  214. InsertLLItem(asml,p.previous, p.next, hp1);
  215. p.free;
  216. p := tai(hp1.next);
  217. end
  218. end;
  219. 9: begin
  220. {imul 9, reg1, reg2 to
  221. lea (reg1,reg1,8), reg2
  222. imul 9, reg1 to
  223. lea (reg1,reg1,8), reg1}
  224. TmpRef.base := taicpu(p).oper[1]^.reg;
  225. TmpRef.index := taicpu(p).oper[1]^.reg;
  226. TmpRef.ScaleFactor := 8;
  227. if (taicpu(p).ops = 2) then
  228. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg)
  229. else
  230. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  231. InsertLLItem(asml,p.previous, p.next, hp1);
  232. p.free;
  233. p := hp1;
  234. end;
  235. 10: begin
  236. {imul 10, reg1, reg2 to
  237. lea (reg1,reg1,4), reg2
  238. add reg2, reg2
  239. imul 10, reg1 to
  240. lea (reg1,reg1,4), reg1
  241. add reg1, reg1}
  242. if (aktoptprocessor <= Class386) then
  243. begin
  244. if (taicpu(p).ops = 3) then
  245. hp1 := taicpu.op_reg_reg(A_ADD, S_L,
  246. taicpu(p).oper[2]^.reg,taicpu(p).oper[2]^.reg)
  247. else
  248. hp1 := taicpu.op_reg_reg(A_ADD, S_L,
  249. taicpu(p).oper[1]^.reg,taicpu(p).oper[1]^.reg);
  250. InsertLLItem(asml,p, p.next, hp1);
  251. TmpRef.base := taicpu(p).oper[1]^.reg;
  252. TmpRef.index := taicpu(p).oper[1]^.reg;
  253. TmpRef.ScaleFactor := 4;
  254. if (taicpu(p).ops = 3) then
  255. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg)
  256. else
  257. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  258. InsertLLItem(asml,p.previous, p.next, hp1);
  259. p.free;
  260. p := tai(hp1.next);
  261. end
  262. end;
  263. 12: begin
  264. {imul 12, reg1, reg2 to
  265. lea (,reg1,4), reg2
  266. lea (,reg1,8) reg2
  267. imul 12, reg1 to
  268. lea (reg1,reg1,2), reg1
  269. lea (,reg1,4), reg1}
  270. if (aktoptprocessor <= Class386)
  271. then
  272. begin
  273. TmpRef.index := taicpu(p).oper[1]^.reg;
  274. if (taicpu(p).ops = 3) then
  275. begin
  276. TmpRef.base := taicpu(p).oper[2]^.reg;
  277. TmpRef.ScaleFactor := 8;
  278. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  279. end
  280. else
  281. begin
  282. TmpRef.base := NR_NO;
  283. TmpRef.ScaleFactor := 4;
  284. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  285. end;
  286. InsertLLItem(asml,p, p.next, hp1);
  287. reference_reset(tmpref);
  288. TmpRef.index := taicpu(p).oper[1]^.reg;
  289. if (taicpu(p).ops = 3) then
  290. begin
  291. TmpRef.base := NR_NO;
  292. TmpRef.ScaleFactor := 4;
  293. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[2]^.reg);
  294. end
  295. else
  296. begin
  297. TmpRef.base := taicpu(p).oper[1]^.reg;
  298. TmpRef.ScaleFactor := 2;
  299. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef, taicpu(p).oper[1]^.reg);
  300. end;
  301. InsertLLItem(asml,p.previous, p.next, hp1);
  302. p.free;
  303. p := tai(hp1.next);
  304. end
  305. end
  306. end;
  307. end;
  308. end;
  309. A_SAR, A_SHR:
  310. {changes the code sequence
  311. shr/sar const1, x
  312. shl const2, x
  313. to either "sar/and", "shl/and" or just "and" depending on const1 and const2}
  314. begin
  315. if GetNextInstruction(p, hp1) and
  316. (tai(hp1).typ = ait_instruction) and
  317. (taicpu(hp1).opcode = A_SHL) and
  318. (taicpu(p).oper[0]^.typ = top_const) and
  319. (taicpu(hp1).oper[0]^.typ = top_const) and
  320. (taicpu(hp1).opsize = taicpu(p).opsize) and
  321. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[1]^.typ) and
  322. OpsEqual(taicpu(hp1).oper[1]^, taicpu(p).oper[1]^) then
  323. if (taicpu(p).oper[0]^.val > taicpu(hp1).oper[0]^.val) and
  324. not(CS_LittleSize in aktglobalswitches) then
  325. { shr/sar const1, %reg
  326. shl const2, %reg
  327. with const1 > const2 }
  328. begin
  329. taicpu(p).LoadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  330. taicpu(hp1).opcode := A_AND;
  331. l := (1 shl (taicpu(hp1).oper[0]^.val)) - 1;
  332. case taicpu(p).opsize Of
  333. S_L: taicpu(hp1).LoadConst(0,l Xor aint($ffffffff));
  334. S_B: taicpu(hp1).LoadConst(0,l Xor $ff);
  335. S_W: taicpu(hp1).LoadConst(0,l Xor $ffff);
  336. end;
  337. end
  338. else if (taicpu(p).oper[0]^.val<taicpu(hp1).oper[0]^.val) and
  339. not(CS_LittleSize in aktglobalswitches) then
  340. { shr/sar const1, %reg
  341. shl const2, %reg
  342. with const1 < const2 }
  343. begin
  344. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val-taicpu(p).oper[0]^.val);
  345. taicpu(p).opcode := A_AND;
  346. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  347. case taicpu(p).opsize Of
  348. S_L: taicpu(p).LoadConst(0,l Xor aint($ffffffff));
  349. S_B: taicpu(p).LoadConst(0,l Xor $ff);
  350. S_W: taicpu(p).LoadConst(0,l Xor $ffff);
  351. end;
  352. end
  353. else
  354. { shr/sar const1, %reg
  355. shl const2, %reg
  356. with const1 = const2 }
  357. if (taicpu(p).oper[0]^.val = taicpu(hp1).oper[0]^.val) then
  358. begin
  359. taicpu(p).opcode := A_AND;
  360. l := (1 shl (taicpu(p).oper[0]^.val))-1;
  361. case taicpu(p).opsize Of
  362. S_B: taicpu(p).LoadConst(0,l Xor $ff);
  363. S_W: taicpu(p).LoadConst(0,l Xor $ffff);
  364. S_L: taicpu(p).LoadConst(0,l Xor aint($ffffffff));
  365. end;
  366. asml.remove(hp1);
  367. hp1.free;
  368. end;
  369. end;
  370. A_XOR:
  371. if (taicpu(p).oper[0]^.typ = top_reg) and
  372. (taicpu(p).oper[1]^.typ = top_reg) and
  373. (taicpu(p).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  374. { temporarily change this to 'mov reg,0' to make it easier }
  375. { for the CSE. Will be changed back in pass 2 }
  376. begin
  377. taicpu(p).opcode := A_MOV;
  378. taicpu(p).loadconst(0,0);
  379. end;
  380. end;
  381. end;
  382. end;
  383. p := tai(p.next)
  384. end;
  385. end;
  386. procedure PeepHoleOptPass1(Asml: taasmoutput; BlockStart, BlockEnd: tai);
  387. {First pass of peepholeoptimizations}
  388. var
  389. l : longint;
  390. p,hp1,hp2 : tai;
  391. hp3,hp4: tai;
  392. TmpRef: TReference;
  393. UsedRegs, TmpUsedRegs: TRegSet;
  394. TmpBool1, TmpBool2: Boolean;
  395. function SkipLabels(hp: tai; var hp2: tai): boolean;
  396. {skips all labels and returns the next "real" instruction}
  397. begin
  398. while assigned(hp.next) and
  399. (tai(hp.next).typ in SkipInstr + [ait_label,ait_align]) Do
  400. hp := tai(hp.next);
  401. if assigned(hp.next) then
  402. begin
  403. SkipLabels := True;
  404. hp2 := tai(hp.next)
  405. end
  406. else
  407. begin
  408. hp2 := hp;
  409. SkipLabels := False
  410. end;
  411. end;
  412. function GetFinalDestination(asml: taasmoutput; hp: taicpu; level: longint): boolean;
  413. {traces sucessive jumps to their final destination and sets it, e.g.
  414. je l1 je l3
  415. <code> <code>
  416. l1: becomes l1:
  417. je l2 je l3
  418. <code> <code>
  419. l2: l2:
  420. jmp l3 jmp l3
  421. the level parameter denotes how deeep we have already followed the jump,
  422. to avoid endless loops with constructs such as "l5: ; jmp l5" }
  423. var p1, p2: tai;
  424. l: tasmlabel;
  425. function FindAnyLabel(hp: tai; var l: tasmlabel): Boolean;
  426. begin
  427. FindAnyLabel := false;
  428. while assigned(hp.next) and
  429. (tai(hp.next).typ in (SkipInstr+[ait_align])) Do
  430. hp := tai(hp.next);
  431. if assigned(hp.next) and
  432. (tai(hp.next).typ = ait_label) then
  433. begin
  434. FindAnyLabel := true;
  435. l := tai_label(hp.next).l;
  436. end
  437. end;
  438. begin
  439. GetfinalDestination := false;
  440. if level > 20 then
  441. exit;
  442. p1 := dfa.getlabelwithsym(tasmlabel(hp.oper[0]^.ref^.symbol));
  443. if assigned(p1) then
  444. begin
  445. SkipLabels(p1,p1);
  446. if (tai(p1).typ = ait_instruction) and
  447. (taicpu(p1).is_jmp) then
  448. if { the next instruction after the label where the jump hp arrives}
  449. { is unconditional or of the same type as hp, so continue }
  450. (taicpu(p1).condition in [C_None,hp.condition]) or
  451. { the next instruction after the label where the jump hp arrives}
  452. { is the opposite of hp (so this one is never taken), but after }
  453. { that one there is a branch that will be taken, so perform a }
  454. { little hack: set p1 equal to this instruction (that's what the}
  455. { last SkipLabels is for, only works with short bool evaluation)}
  456. ((taicpu(p1).condition = inverse_cond(hp.condition)) and
  457. SkipLabels(p1,p2) and
  458. (p2.typ = ait_instruction) and
  459. (taicpu(p2).is_jmp) and
  460. (taicpu(p2).condition in [C_None,hp.condition]) and
  461. SkipLabels(p1,p1)) then
  462. begin
  463. { quick check for loops of the form "l5: ; jmp l5 }
  464. if (tasmlabel(taicpu(p1).oper[0]^.ref^.symbol).labelnr =
  465. tasmlabel(hp.oper[0]^.ref^.symbol).labelnr) then
  466. exit;
  467. if not GetFinalDestination(asml, taicpu(p1),succ(level)) then
  468. exit;
  469. tasmlabel(hp.oper[0]^.ref^.symbol).decrefs;
  470. hp.oper[0]^.ref^.symbol:=taicpu(p1).oper[0]^.ref^.symbol;
  471. tasmlabel(hp.oper[0]^.ref^.symbol).increfs;
  472. end
  473. else
  474. if (taicpu(p1).condition = inverse_cond(hp.condition)) then
  475. if not FindAnyLabel(p1,l) then
  476. begin
  477. {$ifdef finaldestdebug}
  478. insertllitem(asml,p1,p1.next,tai_comment.Create(
  479. strpnew('previous label inserted'))));
  480. {$endif finaldestdebug}
  481. objectlibrary.getlabel(l);
  482. insertllitem(asml,p1,p1.next,tai_label.Create(l));
  483. tasmlabel(taicpu(hp).oper[0]^.ref^.symbol).decrefs;
  484. hp.oper[0]^.ref^.symbol := l;
  485. l.increfs;
  486. { this won't work, since the new label isn't in the labeltable }
  487. { so it will fail the rangecheck. Labeltable should become a }
  488. { hashtable to support this: }
  489. { GetFinalDestination(asml, hp); }
  490. end
  491. else
  492. begin
  493. {$ifdef finaldestdebug}
  494. insertllitem(asml,p1,p1.next,tai_comment.Create(
  495. strpnew('next label reused'))));
  496. {$endif finaldestdebug}
  497. l.increfs;
  498. hp.oper[0]^.ref^.symbol := l;
  499. if not GetFinalDestination(asml, hp,succ(level)) then
  500. exit;
  501. end;
  502. end;
  503. GetFinalDestination := true;
  504. end;
  505. function DoSubAddOpt(var p: tai): Boolean;
  506. begin
  507. DoSubAddOpt := False;
  508. if GetLastInstruction(p, hp1) and
  509. (hp1.typ = ait_instruction) and
  510. (taicpu(hp1).opsize = taicpu(p).opsize) then
  511. case taicpu(hp1).opcode Of
  512. A_DEC:
  513. if (taicpu(hp1).oper[0]^.typ = top_reg) and
  514. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  515. begin
  516. taicpu(p).LoadConst(0,taicpu(p).oper[0]^.val+1);
  517. asml.remove(hp1);
  518. hp1.free;
  519. end;
  520. A_SUB:
  521. if (taicpu(hp1).oper[0]^.typ = top_const) and
  522. (taicpu(hp1).oper[1]^.typ = top_reg) and
  523. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  524. begin
  525. taicpu(p).LoadConst(0,taicpu(p).oper[0]^.val+taicpu(hp1).oper[0]^.val);
  526. asml.remove(hp1);
  527. hp1.free;
  528. end;
  529. A_ADD:
  530. if (taicpu(hp1).oper[0]^.typ = top_const) and
  531. (taicpu(hp1).oper[1]^.typ = top_reg) and
  532. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  533. begin
  534. taicpu(p).LoadConst(0,taicpu(p).oper[0]^.val-taicpu(hp1).oper[0]^.val);
  535. asml.remove(hp1);
  536. hp1.free;
  537. if (taicpu(p).oper[0]^.val = 0) then
  538. begin
  539. hp1 := tai(p.next);
  540. asml.remove(p);
  541. p.free;
  542. if not GetLastInstruction(hp1, p) then
  543. p := hp1;
  544. DoSubAddOpt := True;
  545. end
  546. end;
  547. end;
  548. end;
  549. begin
  550. p := BlockStart;
  551. UsedRegs := [];
  552. while (p <> BlockEnd) Do
  553. begin
  554. UpDateUsedRegs(UsedRegs, tai(p.next));
  555. case p.Typ Of
  556. ait_instruction:
  557. begin
  558. { Handle Jmp Optimizations }
  559. if taicpu(p).is_jmp then
  560. begin
  561. {the following if-block removes all code between a jmp and the next label,
  562. because it can never be executed}
  563. if (taicpu(p).opcode = A_JMP) then
  564. begin
  565. while GetNextInstruction(p, hp1) and
  566. (hp1.typ <> ait_label) do
  567. if not(hp1.typ in ([ait_label,ait_align]+skipinstr)) then
  568. begin
  569. asml.remove(hp1);
  570. hp1.free;
  571. end
  572. else break;
  573. end;
  574. { remove jumps to a label coming right after them }
  575. if GetNextInstruction(p, hp1) then
  576. begin
  577. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp1) and
  578. {$warning FIXME removing the first instruction fails}
  579. (p<>blockstart) then
  580. begin
  581. hp2:=tai(hp1.next);
  582. asml.remove(p);
  583. p.free;
  584. p:=hp2;
  585. continue;
  586. end
  587. else
  588. begin
  589. if hp1.typ = ait_label then
  590. SkipLabels(hp1,hp1);
  591. if (tai(hp1).typ=ait_instruction) and
  592. (taicpu(hp1).opcode=A_JMP) and
  593. GetNextInstruction(hp1, hp2) and
  594. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol), hp2) then
  595. begin
  596. if taicpu(p).opcode=A_Jcc then
  597. begin
  598. taicpu(p).condition:=inverse_cond(taicpu(p).condition);
  599. tai_label(hp2).l.decrefs;
  600. taicpu(p).oper[0]^.ref^.symbol:=taicpu(hp1).oper[0]^.ref^.symbol;
  601. taicpu(p).oper[0]^.ref^.symbol.increfs;
  602. asml.remove(hp1);
  603. hp1.free;
  604. GetFinalDestination(asml, taicpu(p),0);
  605. end
  606. else
  607. begin
  608. GetFinalDestination(asml, taicpu(p),0);
  609. p:=tai(p.next);
  610. continue;
  611. end;
  612. end
  613. else
  614. GetFinalDestination(asml, taicpu(p),0);
  615. end;
  616. end;
  617. end
  618. else
  619. { All other optimizes }
  620. begin
  621. for l := 0 to taicpu(p).ops-1 Do
  622. if (taicpu(p).oper[l]^.typ = top_ref) then
  623. With taicpu(p).oper[l]^.ref^ Do
  624. begin
  625. if (base = NR_NO) and
  626. (index <> NR_NO) and
  627. (scalefactor in [0,1]) then
  628. begin
  629. base := index;
  630. index := NR_NO
  631. end
  632. end;
  633. case taicpu(p).opcode Of
  634. A_AND:
  635. begin
  636. if (taicpu(p).oper[0]^.typ = top_const) and
  637. (taicpu(p).oper[1]^.typ = top_reg) and
  638. GetNextInstruction(p, hp1) and
  639. (tai(hp1).typ = ait_instruction) and
  640. (taicpu(hp1).opcode = A_AND) and
  641. (taicpu(hp1).oper[0]^.typ = top_const) and
  642. (taicpu(hp1).oper[1]^.typ = top_reg) and
  643. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) then
  644. {change "and const1, reg; and const2, reg" to "and (const1 and const2), reg"}
  645. begin
  646. taicpu(p).LoadConst(0,taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  647. asml.remove(hp1);
  648. hp1.free;
  649. end
  650. else
  651. {change "and x, reg; jxx" to "test x, reg", if reg is deallocated before the
  652. jump, but only if it's a conditional jump (PFV) }
  653. if (taicpu(p).oper[1]^.typ = top_reg) and
  654. GetNextInstruction(p, hp1) and
  655. (hp1.typ = ait_instruction) and
  656. (taicpu(hp1).is_jmp) and
  657. (taicpu(hp1).opcode<>A_JMP) and
  658. not(getsupreg(taicpu(p).oper[1]^.reg) in UsedRegs) then
  659. taicpu(p).opcode := A_TEST;
  660. end;
  661. A_CMP:
  662. begin
  663. if (taicpu(p).oper[0]^.typ = top_const) and
  664. (taicpu(p).oper[1]^.typ in [top_reg,top_ref]) and
  665. (taicpu(p).oper[0]^.val = 0) and
  666. GetNextInstruction(p, hp1) and
  667. (hp1.typ = ait_instruction) and
  668. (taicpu(hp1).is_jmp) and
  669. (taicpu(hp1).opcode=A_Jcc) and
  670. (taicpu(hp1).condition in [C_LE,C_BE]) and
  671. GetNextInstruction(hp1,hp2) and
  672. (hp2.typ = ait_instruction) and
  673. (taicpu(hp2).opcode = A_DEC) and
  674. OpsEqual(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  675. GetNextInstruction(hp2, hp3) and
  676. (hp3.typ = ait_instruction) and
  677. (taicpu(hp3).is_jmp) and
  678. (taicpu(hp3).opcode = A_JMP) and
  679. GetNextInstruction(hp3, hp4) and
  680. FindLabel(tasmlabel(taicpu(hp1).oper[0]^.ref^.symbol),hp4) then
  681. begin
  682. taicpu(hp2).Opcode := A_SUB;
  683. taicpu(hp2).Loadoper(1,taicpu(hp2).oper[0]^);
  684. taicpu(hp2).LoadConst(0,1);
  685. taicpu(hp2).ops:=2;
  686. taicpu(hp3).Opcode := A_Jcc;
  687. case taicpu(hp1).condition of
  688. C_LE: taicpu(hp3).condition := C_GE;
  689. C_BE: taicpu(hp3).condition := C_AE;
  690. end;
  691. asml.remove(p);
  692. asml.remove(hp1);
  693. p.free;
  694. hp1.free;
  695. p := hp2;
  696. continue;
  697. end
  698. end;
  699. A_FLD:
  700. begin
  701. if (taicpu(p).oper[0]^.typ = top_reg) and
  702. GetNextInstruction(p, hp1) and
  703. (hp1.typ = Ait_Instruction) and
  704. (taicpu(hp1).oper[0]^.typ = top_reg) and
  705. (taicpu(hp1).oper[1]^.typ = top_reg) and
  706. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  707. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  708. { change to
  709. fld reg fxxx reg,st
  710. fxxxp st, st1 (hp1)
  711. Remark: non commutative operations must be reversed!
  712. }
  713. begin
  714. case taicpu(hp1).opcode Of
  715. A_FMULP,A_FADDP,
  716. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  717. begin
  718. case taicpu(hp1).opcode Of
  719. A_FADDP: taicpu(hp1).opcode := A_FADD;
  720. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  721. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  722. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  723. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  724. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  725. end;
  726. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  727. taicpu(hp1).oper[1]^.reg := NR_ST;
  728. asml.remove(p);
  729. p.free;
  730. p := hp1;
  731. continue;
  732. end;
  733. end;
  734. end
  735. else
  736. if (taicpu(p).oper[0]^.typ = top_ref) and
  737. GetNextInstruction(p, hp2) and
  738. (hp2.typ = Ait_Instruction) and
  739. (taicpu(hp2).ops = 2) and
  740. (taicpu(hp2).oper[0]^.typ = top_reg) and
  741. (taicpu(hp2).oper[1]^.typ = top_reg) and
  742. (taicpu(p).opsize in [S_FS, S_FL]) and
  743. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  744. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  745. if GetLastInstruction(p, hp1) and
  746. (hp1.typ = Ait_Instruction) and
  747. ((taicpu(hp1).opcode = A_FLD) or
  748. (taicpu(hp1).opcode = A_FST)) and
  749. (taicpu(hp1).opsize = taicpu(p).opsize) and
  750. (taicpu(hp1).oper[0]^.typ = top_ref) and
  751. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  752. if ((taicpu(hp2).opcode = A_FMULP) or
  753. (taicpu(hp2).opcode = A_FADDP)) then
  754. { change to
  755. fld/fst mem1 (hp1) fld/fst mem1
  756. fld mem1 (p) fadd/
  757. faddp/ fmul st, st
  758. fmulp st, st1 (hp2) }
  759. begin
  760. asml.remove(p);
  761. p.free;
  762. p := hp1;
  763. if (taicpu(hp2).opcode = A_FADDP) then
  764. taicpu(hp2).opcode := A_FADD
  765. else
  766. taicpu(hp2).opcode := A_FMUL;
  767. taicpu(hp2).oper[1]^.reg := NR_ST;
  768. end
  769. else
  770. { change to
  771. fld/fst mem1 (hp1) fld/fst mem1
  772. fld mem1 (p) fld st}
  773. begin
  774. taicpu(p).changeopsize(S_FL);
  775. taicpu(p).loadreg(0,NR_ST);
  776. end
  777. else
  778. begin
  779. case taicpu(hp2).opcode Of
  780. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  781. { change to
  782. fld/fst mem1 (hp1) fld/fst mem1
  783. fld mem2 (p) fxxx mem2
  784. fxxxp st, st1 (hp2) }
  785. begin
  786. case taicpu(hp2).opcode Of
  787. A_FADDP: taicpu(p).opcode := A_FADD;
  788. A_FMULP: taicpu(p).opcode := A_FMUL;
  789. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  790. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  791. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  792. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  793. end;
  794. asml.remove(hp2);
  795. hp2.free;
  796. end
  797. end
  798. end
  799. end;
  800. A_FSTP,A_FISTP:
  801. if doFpuLoadStoreOpt(asmL,p) then
  802. continue;
  803. A_LEA:
  804. begin
  805. {removes seg register prefixes from LEA operations, as they
  806. don't do anything}
  807. taicpu(p).oper[0]^.ref^.Segment := NR_NO;
  808. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  809. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  810. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX..RS_ESP]) and
  811. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  812. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  813. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  814. (taicpu(p).oper[0]^.ref^.offset = 0) then
  815. begin
  816. hp1 := taicpu.op_reg_reg(A_MOV, S_L,taicpu(p).oper[0]^.ref^.base,
  817. taicpu(p).oper[1]^.reg);
  818. InsertLLItem(asml,p.previous,p.next, hp1);
  819. p.free;
  820. p := hp1;
  821. continue;
  822. end
  823. else if (taicpu(p).oper[0]^.ref^.offset = 0) then
  824. begin
  825. hp1 := tai(p.Next);
  826. asml.remove(p);
  827. p.free;
  828. p := hp1;
  829. continue;
  830. end
  831. else
  832. with taicpu(p).oper[0]^.ref^ do
  833. if (base = taicpu(p).oper[1]^.reg) then
  834. begin
  835. l := offset;
  836. if (l=1) then
  837. begin
  838. taicpu(p).opcode := A_INC;
  839. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  840. taicpu(p).ops := 1
  841. end
  842. else if (l=-1) then
  843. begin
  844. taicpu(p).opcode := A_DEC;
  845. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  846. taicpu(p).ops := 1;
  847. end
  848. else
  849. begin
  850. taicpu(p).opcode := A_ADD;
  851. taicpu(p).loadconst(0,l);
  852. end;
  853. end;
  854. end;
  855. A_MOV:
  856. begin
  857. TmpUsedRegs := UsedRegs;
  858. if (taicpu(p).oper[1]^.typ = top_reg) and
  859. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX, RS_ESI, RS_EDI]) and
  860. GetNextInstruction(p, hp1) and
  861. (tai(hp1).typ = ait_instruction) and
  862. (taicpu(hp1).opcode = A_MOV) and
  863. (taicpu(hp1).oper[0]^.typ = top_reg) and
  864. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  865. begin
  866. {we have "mov x, %treg; mov %treg, y}
  867. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  868. {we've got "mov x, %treg; mov %treg, y; with %treg is not used after }
  869. case taicpu(p).oper[0]^.typ Of
  870. top_reg:
  871. begin
  872. { change "mov %reg, %treg; mov %treg, y"
  873. to "mov %reg, y" }
  874. taicpu(p).LoadOper(1,taicpu(hp1).oper[1]^);
  875. asml.remove(hp1);
  876. hp1.free;
  877. continue;
  878. end;
  879. top_ref:
  880. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  881. begin
  882. { change "mov mem, %treg; mov %treg, %reg"
  883. to "mov mem, %reg" }
  884. taicpu(p).Loadoper(1,taicpu(hp1).oper[1]^);
  885. asml.remove(hp1);
  886. hp1.free;
  887. continue;
  888. end;
  889. end
  890. end
  891. else
  892. {Change "mov %reg1, %reg2; xxx %reg2, ???" to
  893. "mov %reg1, %reg2; xxx %reg1, ???" to avoid a write/read
  894. penalty}
  895. if (taicpu(p).oper[0]^.typ = top_reg) and
  896. (taicpu(p).oper[1]^.typ = top_reg) and
  897. GetNextInstruction(p,hp1) and
  898. (tai(hp1).typ = ait_instruction) and
  899. (taicpu(hp1).ops >= 1) and
  900. (taicpu(hp1).oper[0]^.typ = top_reg) and
  901. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  902. {we have "mov %reg1, %reg2; XXX %reg2, ???"}
  903. begin
  904. if ((taicpu(hp1).opcode = A_OR) or
  905. (taicpu(hp1).opcode = A_TEST)) and
  906. (taicpu(hp1).oper[1]^.typ = top_reg) and
  907. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) then
  908. {we have "mov %reg1, %reg2; test/or %reg2, %reg2"}
  909. begin
  910. TmpUsedRegs := UsedRegs;
  911. { reg1 will be used after the first instruction, }
  912. { so update the allocation info }
  913. allocRegBetween(asmL,taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  914. if GetNextInstruction(hp1, hp2) and
  915. (hp2.typ = ait_instruction) and
  916. taicpu(hp2).is_jmp and
  917. not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, hp1, TmpUsedRegs)) then
  918. { change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  919. "test %reg1, %reg1; jxx" }
  920. begin
  921. taicpu(hp1).Loadoper(0,taicpu(p).oper[0]^);
  922. taicpu(hp1).Loadoper(1,taicpu(p).oper[0]^);
  923. asml.remove(p);
  924. p.free;
  925. p := hp1;
  926. continue
  927. end
  928. else
  929. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  930. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  931. begin
  932. taicpu(hp1).Loadoper(0,taicpu(p).oper[0]^);
  933. taicpu(hp1).Loadoper(1,taicpu(p).oper[0]^);
  934. end;
  935. end
  936. { else
  937. if (taicpu(p.next)^.opcode
  938. in [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  939. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  940. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  941. end
  942. else
  943. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  944. x >= RetOffset) as it doesn't do anything (it writes either to a
  945. parameter or to the temporary storage room for the function
  946. result)}
  947. if GetNextInstruction(p, hp1) and
  948. (tai(hp1).typ = ait_instruction) then
  949. if ((taicpu(hp1).opcode = A_LEAVE) or
  950. (taicpu(hp1).opcode = A_RET)) and
  951. (taicpu(p).oper[1]^.typ = top_ref) and
  952. (taicpu(p).oper[1]^.ref^.base = current_procinfo.FramePointer) and
  953. not(assigned(current_procinfo.procdef.funcretsym) and
  954. (taicpu(p).oper[1]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  955. (taicpu(p).oper[1]^.ref^.index = NR_NO) and
  956. (taicpu(p).oper[0]^.typ = top_reg) then
  957. begin
  958. asml.remove(p);
  959. p.free;
  960. p := hp1;
  961. RemoveLastDeallocForFuncRes(asmL,p);
  962. end
  963. else
  964. if (taicpu(p).oper[0]^.typ = top_reg) and
  965. (taicpu(p).oper[1]^.typ = top_ref) and
  966. (taicpu(p).opsize = taicpu(hp1).opsize) and
  967. (taicpu(hp1).opcode = A_CMP) and
  968. (taicpu(hp1).oper[1]^.typ = top_ref) and
  969. RefsEqual(taicpu(p).oper[1]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  970. {change "mov reg1, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  971. begin
  972. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  973. allocRegBetween(asmL,taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  974. end;
  975. { Next instruction is also a MOV ? }
  976. if GetNextInstruction(p, hp1) and
  977. (tai(hp1).typ = ait_instruction) and
  978. (taicpu(hp1).opcode = A_MOV) and
  979. (taicpu(hp1).opsize = taicpu(p).opsize) then
  980. begin
  981. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  982. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  983. {mov reg1, mem1 or mov mem1, reg1
  984. mov mem2, reg2 mov reg2, mem2}
  985. begin
  986. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  987. {mov reg1, mem1 or mov mem1, reg1
  988. mov mem2, reg1 mov reg2, mem1}
  989. begin
  990. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  991. { Removes the second statement from
  992. mov reg1, mem1/reg2
  993. mov mem1/reg2, reg1 }
  994. begin
  995. if (taicpu(p).oper[0]^.typ = top_reg) then
  996. AllocRegBetween(asmL,taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  997. asml.remove(hp1);
  998. hp1.free;
  999. end
  1000. else
  1001. begin
  1002. TmpUsedRegs := UsedRegs;
  1003. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1004. if (taicpu(p).oper[1]^.typ = top_ref) and
  1005. { mov reg1, mem1
  1006. mov mem2, reg1 }
  1007. (taicpu(hp1).oper[0]^.ref^.refaddr = addr_no) and
  1008. GetNextInstruction(hp1, hp2) and
  1009. (hp2.typ = ait_instruction) and
  1010. (taicpu(hp2).opcode = A_CMP) and
  1011. (taicpu(hp2).opsize = taicpu(p).opsize) and
  1012. (taicpu(hp2).oper[0]^.typ = TOp_Ref) and
  1013. (taicpu(hp2).oper[1]^.typ = TOp_Reg) and
  1014. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(p).oper[1]^.ref^) and
  1015. (taicpu(hp2).oper[1]^.reg= taicpu(p).oper[0]^.reg) and
  1016. not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs)) then
  1017. { change to
  1018. mov reg1, mem1 mov reg1, mem1
  1019. mov mem2, reg1 cmp reg1, mem2
  1020. cmp mem1, reg1 }
  1021. begin
  1022. asml.remove(hp2);
  1023. hp2.free;
  1024. taicpu(hp1).opcode := A_CMP;
  1025. taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
  1026. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  1027. end;
  1028. end;
  1029. end
  1030. else
  1031. begin
  1032. tmpUsedRegs := UsedRegs;
  1033. if GetNextInstruction(hp1, hp2) and
  1034. (taicpu(p).oper[0]^.typ = top_ref) and
  1035. (taicpu(p).oper[1]^.typ = top_reg) and
  1036. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1037. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  1038. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1039. (tai(hp2).typ = ait_instruction) and
  1040. (taicpu(hp2).opcode = A_MOV) and
  1041. (taicpu(hp2).opsize = taicpu(p).opsize) and
  1042. (taicpu(hp2).oper[1]^.typ = top_reg) and
  1043. (taicpu(hp2).oper[0]^.typ = top_ref) and
  1044. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  1045. if not regInRef(getsupreg(taicpu(hp2).oper[1]^.reg),taicpu(hp2).oper[0]^.ref^) and
  1046. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,tmpUsedRegs)) then
  1047. { mov mem1, %reg1
  1048. mov %reg1, mem2
  1049. mov mem2, reg2
  1050. to:
  1051. mov mem1, reg2
  1052. mov reg2, mem2}
  1053. begin
  1054. AllocRegBetween(asmL,taicpu(hp2).oper[1]^.reg,p,hp2,usedregs);
  1055. taicpu(p).Loadoper(1,taicpu(hp2).oper[1]^);
  1056. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  1057. asml.remove(hp2);
  1058. hp2.free;
  1059. end
  1060. else
  1061. if (taicpu(p).oper[1]^.reg <> taicpu(hp2).oper[1]^.reg) and
  1062. not(RegInRef(getsupreg(taicpu(p).oper[1]^.reg),taicpu(p).oper[0]^.ref^)) and
  1063. not(RegInRef(getsupreg(taicpu(hp2).oper[1]^.reg),taicpu(hp2).oper[0]^.ref^)) then
  1064. { mov mem1, reg1 mov mem1, reg1
  1065. mov reg1, mem2 mov reg1, mem2
  1066. mov mem2, reg2 mov mem2, reg1
  1067. to: to:
  1068. mov mem1, reg1 mov mem1, reg1
  1069. mov mem1, reg2 mov reg1, mem2
  1070. mov reg1, mem2
  1071. or (if mem1 depends on reg1
  1072. and/or if mem2 depends on reg2)
  1073. to:
  1074. mov mem1, reg1
  1075. mov reg1, mem2
  1076. mov reg1, reg2
  1077. }
  1078. begin
  1079. taicpu(hp1).LoadRef(0,taicpu(p).oper[0]^.ref^);
  1080. taicpu(hp1).LoadReg(1,taicpu(hp2).oper[1]^.reg);
  1081. taicpu(hp2).LoadRef(1,taicpu(hp2).oper[0]^.ref^);
  1082. taicpu(hp2).LoadReg(0,taicpu(p).oper[1]^.reg);
  1083. allocRegBetween(asmL,taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1084. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  1085. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1086. allocRegBetween(asmL,taicpu(p).oper[0]^.ref^.base,p,hp2,usedregs);
  1087. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and
  1088. (getsupreg(taicpu(p).oper[0]^.ref^.index) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1089. allocRegBetween(asmL,taicpu(p).oper[0]^.ref^.index,p,hp2,usedregs);
  1090. end
  1091. else
  1092. if (taicpu(hp1).Oper[0]^.reg <> taicpu(hp2).Oper[1]^.reg) then
  1093. begin
  1094. taicpu(hp2).LoadReg(0,taicpu(hp1).Oper[0]^.reg);
  1095. allocRegBetween(asmL,taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1096. end
  1097. else
  1098. begin
  1099. asml.remove(hp2);
  1100. hp2.free;
  1101. end
  1102. end
  1103. end
  1104. else
  1105. (* {movl [mem1],reg1
  1106. movl [mem1],reg2
  1107. to:
  1108. movl [mem1],reg1
  1109. movl reg1,reg2 }
  1110. if (taicpu(p).oper[0]^.typ = top_ref) and
  1111. (taicpu(p).oper[1]^.typ = top_reg) and
  1112. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1113. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1114. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1115. RefsEqual(TReference(taicpu(p).oper[0]^^),taicpu(hp1).oper[0]^^.ref^) and
  1116. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.base) and
  1117. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.index) then
  1118. taicpu(hp1).LoadReg(0,taicpu(p).oper[1]^.reg)
  1119. else*)
  1120. { movl const1,[mem1]
  1121. movl [mem1],reg1
  1122. to:
  1123. movl const1,reg1
  1124. movl reg1,[mem1] }
  1125. if (taicpu(p).oper[0]^.typ = top_const) and
  1126. (taicpu(p).oper[1]^.typ = top_ref) and
  1127. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1128. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1129. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1130. RefsEqual(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.ref^) then
  1131. begin
  1132. allocregbetween(asml,taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1133. taicpu(hp1).LoadReg(0,taicpu(hp1).oper[1]^.reg);
  1134. taicpu(hp1).LoadRef(1,taicpu(p).oper[1]^.ref^);
  1135. taicpu(p).LoadReg(1,taicpu(hp1).oper[0]^.reg);
  1136. end
  1137. end;
  1138. end;
  1139. A_MOVZX:
  1140. begin
  1141. {removes superfluous And's after movzx's}
  1142. if (taicpu(p).oper[1]^.typ = top_reg) and
  1143. GetNextInstruction(p, hp1) and
  1144. (tai(hp1).typ = ait_instruction) and
  1145. (taicpu(hp1).opcode = A_AND) and
  1146. (taicpu(hp1).oper[0]^.typ = top_const) and
  1147. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1148. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1149. case taicpu(p).opsize Of
  1150. S_BL, S_BW:
  1151. if (taicpu(hp1).oper[0]^.val = $ff) then
  1152. begin
  1153. asml.remove(hp1);
  1154. hp1.free;
  1155. end;
  1156. S_WL:
  1157. if (taicpu(hp1).oper[0]^.val = $ffff) then
  1158. begin
  1159. asml.remove(hp1);
  1160. hp1.free;
  1161. end;
  1162. end;
  1163. {changes some movzx constructs to faster synonims (all examples
  1164. are given with eax/ax, but are also valid for other registers)}
  1165. if (taicpu(p).oper[1]^.typ = top_reg) then
  1166. if (taicpu(p).oper[0]^.typ = top_reg) then
  1167. case taicpu(p).opsize of
  1168. S_BW:
  1169. begin
  1170. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  1171. not(CS_LittleSize in aktglobalswitches) then
  1172. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  1173. begin
  1174. taicpu(p).opcode := A_AND;
  1175. taicpu(p).changeopsize(S_W);
  1176. taicpu(p).LoadConst(0,$ff);
  1177. end
  1178. else if GetNextInstruction(p, hp1) and
  1179. (tai(hp1).typ = ait_instruction) and
  1180. (taicpu(hp1).opcode = A_AND) and
  1181. (taicpu(hp1).oper[0]^.typ = top_const) and
  1182. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1183. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1184. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  1185. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  1186. begin
  1187. taicpu(p).opcode := A_MOV;
  1188. taicpu(p).changeopsize(S_W);
  1189. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  1190. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1191. end;
  1192. end;
  1193. S_BL:
  1194. begin
  1195. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  1196. not(CS_LittleSize in aktglobalswitches) then
  1197. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  1198. begin
  1199. taicpu(p).opcode := A_AND;
  1200. taicpu(p).changeopsize(S_L);
  1201. taicpu(p).loadconst(0,$ff)
  1202. end
  1203. else if GetNextInstruction(p, hp1) and
  1204. (tai(hp1).typ = ait_instruction) and
  1205. (taicpu(hp1).opcode = A_AND) and
  1206. (taicpu(hp1).oper[0]^.typ = top_const) and
  1207. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1208. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1209. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  1210. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  1211. begin
  1212. taicpu(p).opcode := A_MOV;
  1213. taicpu(p).changeopsize(S_L);
  1214. setsubreg(taicpu(p).oper[0]^.reg,R_SUBWHOLE);
  1215. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1216. end
  1217. end;
  1218. S_WL:
  1219. begin
  1220. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  1221. not(CS_LittleSize in aktglobalswitches) then
  1222. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  1223. begin
  1224. taicpu(p).opcode := A_AND;
  1225. taicpu(p).changeopsize(S_L);
  1226. taicpu(p).LoadConst(0,$ffff);
  1227. end
  1228. else if GetNextInstruction(p, hp1) and
  1229. (tai(hp1).typ = ait_instruction) and
  1230. (taicpu(hp1).opcode = A_AND) and
  1231. (taicpu(hp1).oper[0]^.typ = top_const) and
  1232. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1233. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1234. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  1235. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  1236. begin
  1237. taicpu(p).opcode := A_MOV;
  1238. taicpu(p).changeopsize(S_L);
  1239. setsubreg(taicpu(p).oper[0]^.reg,R_SUBWHOLE);
  1240. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  1241. end;
  1242. end;
  1243. end
  1244. else if (taicpu(p).oper[0]^.typ = top_ref) then
  1245. begin
  1246. if GetNextInstruction(p, hp1) and
  1247. (tai(hp1).typ = ait_instruction) and
  1248. (taicpu(hp1).opcode = A_AND) and
  1249. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  1250. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  1251. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1252. begin
  1253. taicpu(p).opcode := A_MOV;
  1254. case taicpu(p).opsize Of
  1255. S_BL:
  1256. begin
  1257. taicpu(p).changeopsize(S_L);
  1258. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1259. end;
  1260. S_WL:
  1261. begin
  1262. taicpu(p).changeopsize(S_L);
  1263. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  1264. end;
  1265. S_BW:
  1266. begin
  1267. taicpu(p).changeopsize(S_W);
  1268. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1269. end;
  1270. end;
  1271. end;
  1272. end;
  1273. end;
  1274. (* should not be generated anymore by the current code generator
  1275. A_POP:
  1276. begin
  1277. if target_info.system=system_i386_go32v2 then
  1278. begin
  1279. { Transform a series of pop/pop/pop/push/push/push to }
  1280. { 'movl x(%esp),%reg' for go32v2 (not for the rest, }
  1281. { because I'm not sure whether they can cope with }
  1282. { 'movl x(%esp),%reg' with x > 0, I believe we had }
  1283. { such a problem when using esp as frame pointer (JM) }
  1284. if (taicpu(p).oper[0]^.typ = top_reg) then
  1285. begin
  1286. hp1 := p;
  1287. hp2 := p;
  1288. l := 0;
  1289. while getNextInstruction(hp1,hp1) and
  1290. (hp1.typ = ait_instruction) and
  1291. (taicpu(hp1).opcode = A_POP) and
  1292. (taicpu(hp1).oper[0]^.typ = top_reg) do
  1293. begin
  1294. hp2 := hp1;
  1295. inc(l,4);
  1296. end;
  1297. getLastInstruction(p,hp3);
  1298. l1 := 0;
  1299. while (hp2 <> hp3) and
  1300. assigned(hp1) and
  1301. (hp1.typ = ait_instruction) and
  1302. (taicpu(hp1).opcode = A_PUSH) and
  1303. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1304. (taicpu(hp1).oper[0]^.reg.enum = taicpu(hp2).oper[0]^.reg.enum) do
  1305. begin
  1306. { change it to a two op operation }
  1307. taicpu(hp2).oper[1]^.typ:=top_none;
  1308. taicpu(hp2).ops:=2;
  1309. taicpu(hp2).opcode := A_MOV;
  1310. taicpu(hp2).Loadoper(1,taicpu(hp1).oper[0]^);
  1311. reference_reset(tmpref);
  1312. tmpRef.base.enum:=R_INTREGISTER;
  1313. tmpRef.base.number:=NR_STACK_POINTER_REG;
  1314. convert_register_to_enum(tmpref.base);
  1315. tmpRef.offset := l;
  1316. taicpu(hp2).loadRef(0,tmpRef);
  1317. hp4 := hp1;
  1318. getNextInstruction(hp1,hp1);
  1319. asml.remove(hp4);
  1320. hp4.free;
  1321. getLastInstruction(hp2,hp2);
  1322. dec(l,4);
  1323. inc(l1);
  1324. end;
  1325. if l <> -4 then
  1326. begin
  1327. inc(l,4);
  1328. for l1 := l1 downto 1 do
  1329. begin
  1330. getNextInstruction(hp2,hp2);
  1331. dec(taicpu(hp2).oper[0]^.ref^.offset,l);
  1332. end
  1333. end
  1334. end
  1335. end
  1336. else
  1337. begin
  1338. if (taicpu(p).oper[0]^.typ = top_reg) and
  1339. GetNextInstruction(p, hp1) and
  1340. (tai(hp1).typ=ait_instruction) and
  1341. (taicpu(hp1).opcode=A_PUSH) and
  1342. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1343. (taicpu(hp1).oper[0]^.reg.enum=taicpu(p).oper[0]^.reg.enum) then
  1344. begin
  1345. { change it to a two op operation }
  1346. taicpu(p).oper[1]^.typ:=top_none;
  1347. taicpu(p).ops:=2;
  1348. taicpu(p).opcode := A_MOV;
  1349. taicpu(p).Loadoper(1,taicpu(p).oper[0]^);
  1350. reference_reset(tmpref);
  1351. TmpRef.base.enum := R_ESP;
  1352. taicpu(p).LoadRef(0,TmpRef);
  1353. asml.remove(hp1);
  1354. hp1.free;
  1355. end;
  1356. end;
  1357. end;
  1358. *)
  1359. A_PUSH:
  1360. begin
  1361. if (taicpu(p).opsize = S_W) and
  1362. (taicpu(p).oper[0]^.typ = Top_Const) and
  1363. GetNextInstruction(p, hp1) and
  1364. (tai(hp1).typ = ait_instruction) and
  1365. (taicpu(hp1).opcode = A_PUSH) and
  1366. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  1367. (taicpu(hp1).opsize = S_W) then
  1368. begin
  1369. taicpu(p).changeopsize(S_L);
  1370. taicpu(p).LoadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  1371. asml.remove(hp1);
  1372. hp1.free;
  1373. end;
  1374. end;
  1375. A_SHL, A_SAL:
  1376. begin
  1377. if (taicpu(p).oper[0]^.typ = Top_Const) and
  1378. (taicpu(p).oper[1]^.typ = Top_Reg) and
  1379. (taicpu(p).opsize = S_L) and
  1380. (taicpu(p).oper[0]^.val <= 3) then
  1381. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1382. begin
  1383. TmpBool1 := True; {should we check the next instruction?}
  1384. TmpBool2 := False; {have we found an add/sub which could be
  1385. integrated in the lea?}
  1386. reference_reset(tmpref);
  1387. TmpRef.index := taicpu(p).oper[1]^.reg;
  1388. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  1389. while TmpBool1 and
  1390. GetNextInstruction(p, hp1) and
  1391. (tai(hp1).typ = ait_instruction) and
  1392. ((((taicpu(hp1).opcode = A_ADD) or
  1393. (taicpu(hp1).opcode = A_SUB)) and
  1394. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  1395. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  1396. (((taicpu(hp1).opcode = A_INC) or
  1397. (taicpu(hp1).opcode = A_DEC)) and
  1398. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  1399. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg))) Do
  1400. begin
  1401. TmpBool1 := False;
  1402. if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  1403. begin
  1404. TmpBool1 := True;
  1405. TmpBool2 := True;
  1406. case taicpu(hp1).opcode of
  1407. A_ADD:
  1408. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  1409. A_SUB:
  1410. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  1411. end;
  1412. asml.remove(hp1);
  1413. hp1.free;
  1414. end
  1415. else
  1416. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  1417. (((taicpu(hp1).opcode = A_ADD) and
  1418. (TmpRef.base = NR_NO)) or
  1419. (taicpu(hp1).opcode = A_INC) or
  1420. (taicpu(hp1).opcode = A_DEC)) then
  1421. begin
  1422. TmpBool1 := True;
  1423. TmpBool2 := True;
  1424. case taicpu(hp1).opcode of
  1425. A_ADD:
  1426. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  1427. A_INC:
  1428. inc(TmpRef.offset);
  1429. A_DEC:
  1430. dec(TmpRef.offset);
  1431. end;
  1432. asml.remove(hp1);
  1433. hp1.free;
  1434. end;
  1435. end;
  1436. if TmpBool2 or
  1437. ((aktoptprocessor < ClassPentium2) and
  1438. (taicpu(p).oper[0]^.val <= 3) and
  1439. not(CS_LittleSize in aktglobalswitches)) then
  1440. begin
  1441. if not(TmpBool2) and
  1442. (taicpu(p).oper[0]^.val = 1) then
  1443. begin
  1444. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  1445. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  1446. end
  1447. else
  1448. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef,
  1449. taicpu(p).oper[1]^.reg);
  1450. InsertLLItem(asml,p.previous, p.next, hp1);
  1451. p.free;
  1452. p := hp1;
  1453. end;
  1454. end
  1455. else
  1456. if (aktoptprocessor < ClassPentium2) and
  1457. (taicpu(p).oper[0]^.typ = top_const) and
  1458. (taicpu(p).oper[1]^.typ = top_reg) then
  1459. if (taicpu(p).oper[0]^.val = 1) then
  1460. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1461. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  1462. (unlike shl, which is only Tairable in the U pipe)}
  1463. begin
  1464. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  1465. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  1466. InsertLLItem(asml,p.previous, p.next, hp1);
  1467. p.free;
  1468. p := hp1;
  1469. end
  1470. else if (taicpu(p).opsize = S_L) and
  1471. (taicpu(p).oper[0]^.val<= 3) then
  1472. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1473. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1474. begin
  1475. reference_reset(tmpref);
  1476. TmpRef.index := taicpu(p).oper[1]^.reg;
  1477. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  1478. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  1479. InsertLLItem(asml,p.previous, p.next, hp1);
  1480. p.free;
  1481. p := hp1;
  1482. end
  1483. end;
  1484. A_SETcc :
  1485. { changes
  1486. setcc (funcres) setcc reg
  1487. movb (funcres), reg to leave/ret
  1488. leave/ret }
  1489. begin
  1490. if (taicpu(p).oper[0]^.typ = top_ref) and
  1491. GetNextInstruction(p, hp1) and
  1492. GetNextInstruction(hp1, hp2) and
  1493. (hp2.typ = ait_instruction) and
  1494. ((taicpu(hp2).opcode = A_LEAVE) or
  1495. (taicpu(hp2).opcode = A_RET)) and
  1496. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  1497. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  1498. not(assigned(current_procinfo.procdef.funcretsym) and
  1499. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  1500. (hp1.typ = ait_instruction) and
  1501. (taicpu(hp1).opcode = A_MOV) and
  1502. (taicpu(hp1).opsize = S_B) and
  1503. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1504. RefsEqual(taicpu(hp1).oper[0]^.ref^, taicpu(p).oper[0]^.ref^) then
  1505. begin
  1506. taicpu(p).LoadReg(0,taicpu(hp1).oper[1]^.reg);
  1507. asml.remove(hp1);
  1508. hp1.free;
  1509. end
  1510. end;
  1511. A_SUB:
  1512. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1513. { * change "sub/add const1, reg" or "dec reg" followed by
  1514. "sub const2, reg" to one "sub ..., reg" }
  1515. begin
  1516. if (taicpu(p).oper[0]^.typ = top_const) and
  1517. (taicpu(p).oper[1]^.typ = top_reg) then
  1518. if (taicpu(p).oper[0]^.val = 2) and
  1519. (taicpu(p).oper[1]^.reg = NR_ESP) and
  1520. { Don't do the sub/push optimization if the sub }
  1521. { comes from setting up the stack frame (JM) }
  1522. (not getLastInstruction(p,hp1) or
  1523. (hp1.typ <> ait_instruction) or
  1524. (taicpu(hp1).opcode <> A_MOV) or
  1525. (taicpu(hp1).oper[0]^.typ <> top_reg) or
  1526. (taicpu(hp1).oper[0]^.reg <> NR_ESP) or
  1527. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  1528. (taicpu(hp1).oper[1]^.reg <> NR_EBP)) then
  1529. begin
  1530. hp1 := tai(p.next);
  1531. while Assigned(hp1) and
  1532. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  1533. not regReadByInstruction(RS_ESP,hp1) and
  1534. not regModifiedByInstruction(RS_ESP,hp1) do
  1535. hp1 := tai(hp1.next);
  1536. if Assigned(hp1) and
  1537. (tai(hp1).typ = ait_instruction) and
  1538. (taicpu(hp1).opcode = A_PUSH) and
  1539. (taicpu(hp1).opsize = S_W) then
  1540. begin
  1541. taicpu(hp1).changeopsize(S_L);
  1542. if taicpu(hp1).oper[0]^.typ=top_reg then
  1543. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  1544. hp1 := tai(p.next);
  1545. asml.remove(p);
  1546. p.free;
  1547. p := hp1;
  1548. continue
  1549. end;
  1550. if DoSubAddOpt(p) then
  1551. continue;
  1552. end
  1553. else if DoSubAddOpt(p) then
  1554. continue
  1555. end;
  1556. end;
  1557. end; { if is_jmp }
  1558. end;
  1559. end;
  1560. updateUsedRegs(UsedRegs,p);
  1561. p:=tai(p.next);
  1562. end;
  1563. end;
  1564. function isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  1565. begin
  1566. isFoldableArithOp := False;
  1567. case hp1.opcode of
  1568. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  1569. isFoldableArithOp :=
  1570. ((taicpu(hp1).oper[0]^.typ = top_const) or
  1571. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  1572. (taicpu(hp1).oper[0]^.reg <> reg))) and
  1573. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1574. (taicpu(hp1).oper[1]^.reg = reg);
  1575. A_INC,A_DEC:
  1576. isFoldableArithOp :=
  1577. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1578. (taicpu(hp1).oper[0]^.reg = reg);
  1579. end;
  1580. end;
  1581. procedure PeepHoleOptPass2(asml: taasmoutput; BlockStart, BlockEnd: tai);
  1582. {$ifdef USECMOV}
  1583. function CanBeCMOV(p : tai) : boolean;
  1584. begin
  1585. CanBeCMOV:=assigned(p) and (p.typ=ait_instruction) and
  1586. (taicpu(p).opcode=A_MOV) and
  1587. (taicpu(p).opsize in [S_L,S_W]) and
  1588. ((taicpu(p).oper[0]^.typ = top_reg) or
  1589. ((taicpu(p).oper[0]^.typ = top_ref) and
  1590. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))) and
  1591. (taicpu(p).oper[1]^.typ in [top_reg]);
  1592. end;
  1593. {$endif USECMOV}
  1594. var
  1595. p,hp1,hp2: tai;
  1596. {$ifdef USECMOV}
  1597. l : longint;
  1598. condition : tasmcond;
  1599. hp3: tai;
  1600. {$endif USECMOV}
  1601. UsedRegs, TmpUsedRegs: TRegSet;
  1602. begin
  1603. p := BlockStart;
  1604. UsedRegs := [];
  1605. while (p <> BlockEnd) Do
  1606. begin
  1607. UpdateUsedRegs(UsedRegs, tai(p.next));
  1608. case p.Typ Of
  1609. Ait_Instruction:
  1610. begin
  1611. case taicpu(p).opcode Of
  1612. {$ifdef USECMOV}
  1613. A_Jcc:
  1614. if (aktspecificoptprocessor>=ClassPentium2) then
  1615. begin
  1616. { check for
  1617. jCC xxx
  1618. <several movs>
  1619. xxx:
  1620. }
  1621. l:=0;
  1622. GetNextInstruction(p, hp1);
  1623. while assigned(hp1) and
  1624. CanBeCMOV(hp1) and
  1625. { stop on labels }
  1626. not(hp1.typ=ait_label) do
  1627. begin
  1628. inc(l);
  1629. GetNextInstruction(hp1,hp1);
  1630. end;
  1631. if assigned(hp1) then
  1632. begin
  1633. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1634. begin
  1635. if (l<=4) and (l>0) then
  1636. begin
  1637. condition:=inverse_cond(taicpu(p).condition);
  1638. hp2:=p;
  1639. GetNextInstruction(p,hp1);
  1640. p:=hp1;
  1641. repeat
  1642. taicpu(hp1).opcode:=A_CMOVcc;
  1643. taicpu(hp1).condition:=condition;
  1644. GetNextInstruction(hp1,hp1);
  1645. until not(assigned(hp1)) or
  1646. not(CanBeCMOV(hp1));
  1647. { wait with removing else GetNextInstruction could
  1648. ignore the label if it was the only usage in the
  1649. jump moved away }
  1650. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1651. asml.remove(hp2);
  1652. hp2.free;
  1653. continue;
  1654. end;
  1655. end
  1656. else
  1657. begin
  1658. { check further for
  1659. jCC xxx
  1660. <several movs 1>
  1661. jmp yyy
  1662. xxx:
  1663. <several movs 2>
  1664. yyy:
  1665. }
  1666. { hp2 points to jmp yyy }
  1667. hp2:=hp1;
  1668. { skip hp1 to xxx }
  1669. GetNextInstruction(hp1, hp1);
  1670. if assigned(hp2) and
  1671. assigned(hp1) and
  1672. (l<=3) and
  1673. (hp2.typ=ait_instruction) and
  1674. (taicpu(hp2).is_jmp) and
  1675. (taicpu(hp2).condition=C_None) and
  1676. { real label and jump, no further references to the
  1677. label are allowed }
  1678. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  1679. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1680. begin
  1681. l:=0;
  1682. { skip hp1 to <several moves 2> }
  1683. GetNextInstruction(hp1, hp1);
  1684. while assigned(hp1) and
  1685. CanBeCMOV(hp1) do
  1686. begin
  1687. inc(l);
  1688. GetNextInstruction(hp1, hp1);
  1689. end;
  1690. { hp1 points to yyy: }
  1691. if assigned(hp1) and
  1692. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  1693. begin
  1694. condition:=inverse_cond(taicpu(p).condition);
  1695. GetNextInstruction(p,hp1);
  1696. hp3:=p;
  1697. p:=hp1;
  1698. repeat
  1699. taicpu(hp1).opcode:=A_CMOVcc;
  1700. taicpu(hp1).condition:=condition;
  1701. GetNextInstruction(hp1,hp1);
  1702. until not(assigned(hp1)) or
  1703. not(CanBeCMOV(hp1));
  1704. { hp2 is still at jmp yyy }
  1705. GetNextInstruction(hp2,hp1);
  1706. { hp2 is now at xxx: }
  1707. condition:=inverse_cond(condition);
  1708. GetNextInstruction(hp1,hp1);
  1709. { hp1 is now at <several movs 2> }
  1710. repeat
  1711. taicpu(hp1).opcode:=A_CMOVcc;
  1712. taicpu(hp1).condition:=condition;
  1713. GetNextInstruction(hp1,hp1);
  1714. until not(assigned(hp1)) or
  1715. not(CanBeCMOV(hp1));
  1716. {
  1717. asml.remove(hp1.next)
  1718. hp1.next.free;
  1719. asml.remove(hp1);
  1720. hp1.free;
  1721. }
  1722. { remove jCC }
  1723. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  1724. asml.remove(hp3);
  1725. hp3.free;
  1726. { remove jmp }
  1727. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1728. asml.remove(hp2);
  1729. hp2.free;
  1730. continue;
  1731. end;
  1732. end;
  1733. end;
  1734. end;
  1735. end;
  1736. {$endif USECMOV}
  1737. A_FSTP,A_FISTP:
  1738. if doFpuLoadStoreOpt(asmL,p) then
  1739. continue;
  1740. A_IMUL:
  1741. begin
  1742. if (taicpu(p).ops >= 2) and
  1743. ((taicpu(p).oper[0]^.typ = top_const) or
  1744. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  1745. (taicpu(p).oper[1]^.typ = top_reg) and
  1746. ((taicpu(p).ops = 2) or
  1747. ((taicpu(p).oper[2]^.typ = top_reg) and
  1748. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  1749. getLastInstruction(p,hp1) and
  1750. (hp1.typ = ait_instruction) and
  1751. (taicpu(hp1).opcode = A_MOV) and
  1752. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1753. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1754. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1755. { change "mov reg1,reg2; imul y,reg2" to "imul y,reg1,reg2" }
  1756. begin
  1757. taicpu(p).ops := 3;
  1758. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  1759. taicpu(p).loadreg(2,taicpu(hp1).oper[1]^.reg);
  1760. asml.remove(hp1);
  1761. hp1.free;
  1762. end;
  1763. end;
  1764. A_MOV:
  1765. begin
  1766. if (taicpu(p).oper[0]^.typ = top_reg) and
  1767. (taicpu(p).oper[1]^.typ = top_reg) and
  1768. GetNextInstruction(p, hp1) and
  1769. (hp1.typ = ait_Instruction) and
  1770. ((taicpu(hp1).opcode = A_MOV) or
  1771. (taicpu(hp1).opcode = A_MOVZX) or
  1772. (taicpu(hp1).opcode = A_MOVSX)) and
  1773. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1774. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1775. ((taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) or
  1776. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg)) and
  1777. (getsupreg(taicpu(hp1).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) then
  1778. {mov reg1, reg2
  1779. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  1780. begin
  1781. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) then
  1782. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[0]^.reg;
  1783. if (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) then
  1784. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  1785. asml.remove(p);
  1786. p.free;
  1787. p := hp1;
  1788. continue;
  1789. end
  1790. else if (taicpu(p).oper[0]^.typ = top_ref) and
  1791. GetNextInstruction(p,hp1) and
  1792. (hp1.typ = ait_instruction) and
  1793. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  1794. GetNextInstruction(hp1,hp2) and
  1795. (hp2.typ = ait_instruction) and
  1796. (taicpu(hp2).opcode = A_MOV) and
  1797. (taicpu(hp2).oper[0]^.typ = top_reg) and
  1798. (taicpu(hp2).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  1799. (taicpu(hp2).oper[1]^.typ = top_ref) then
  1800. begin
  1801. TmpUsedRegs := UsedRegs;
  1802. UpdateUsedRegs(TmpUsedRegs,tai(hp1.next));
  1803. if (RefsEqual(taicpu(hp2).oper[1]^.ref^, taicpu(p).oper[0]^.ref^) and
  1804. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,
  1805. hp2, TmpUsedRegs))) then
  1806. { change mov (ref), reg }
  1807. { add/sub/or/... reg2/$const, reg }
  1808. { mov reg, (ref) }
  1809. { # release reg }
  1810. { to add/sub/or/... reg2/$const, (ref) }
  1811. begin
  1812. case taicpu(hp1).opcode of
  1813. A_INC,A_DEC:
  1814. taicpu(hp1).LoadRef(0,taicpu(p).oper[0]^.ref^)
  1815. else
  1816. taicpu(hp1).LoadRef(1,taicpu(p).oper[0]^.ref^);
  1817. end;
  1818. asml.remove(p);
  1819. asml.remove(hp2);
  1820. p.free;
  1821. hp2.free;
  1822. p := hp1
  1823. end;
  1824. end
  1825. end;
  1826. end;
  1827. end;
  1828. end;
  1829. p := tai(p.next)
  1830. end;
  1831. end;
  1832. procedure PostPeepHoleOpts(asml: taasmoutput; BlockStart, BlockEnd: tai);
  1833. var
  1834. p,hp1,hp2: tai;
  1835. begin
  1836. p := BlockStart;
  1837. while (p <> BlockEnd) Do
  1838. begin
  1839. case p.Typ Of
  1840. Ait_Instruction:
  1841. begin
  1842. case taicpu(p).opcode Of
  1843. A_CALL:
  1844. if (AktOptProcessor < ClassPentium2) and
  1845. GetNextInstruction(p, hp1) and
  1846. (hp1.typ = ait_instruction) and
  1847. (taicpu(hp1).opcode = A_JMP) and
  1848. ((taicpu(hp1).oper[0]^.typ=top_ref) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full)) then
  1849. begin
  1850. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  1851. InsertLLItem(asml, p.previous, p, hp2);
  1852. taicpu(p).opcode := A_JMP;
  1853. taicpu(p).is_jmp := true;
  1854. asml.remove(hp1);
  1855. hp1.free;
  1856. end;
  1857. A_CMP:
  1858. begin
  1859. if (taicpu(p).oper[0]^.typ = top_const) and
  1860. (taicpu(p).oper[0]^.val = 0) and
  1861. (taicpu(p).oper[1]^.typ = top_reg) then
  1862. {change "cmp $0, %reg" to "test %reg, %reg"}
  1863. begin
  1864. taicpu(p).opcode := A_TEST;
  1865. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  1866. continue;
  1867. end;
  1868. end;
  1869. (*
  1870. Optimization is not safe; xor clears the carry flag.
  1871. See test/tgadint64 in the test suite.
  1872. A_MOV:
  1873. if (taicpu(p).oper[0]^.typ = Top_Const) and
  1874. (taicpu(p).oper[0]^.val = 0) and
  1875. (taicpu(p).oper[1]^.typ = Top_Reg) then
  1876. { change "mov $0, %reg" into "xor %reg, %reg" }
  1877. begin
  1878. taicpu(p).opcode := A_XOR;
  1879. taicpu(p).LoadReg(0,taicpu(p).oper[1]^.reg);
  1880. end;
  1881. *)
  1882. A_MOVZX:
  1883. { if register vars are on, it's possible there is code like }
  1884. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  1885. { so we can't safely replace the movzx then with xor/mov, }
  1886. { since that would change the flags (JM) }
  1887. if not(cs_regvars in aktglobalswitches) then
  1888. begin
  1889. if (taicpu(p).oper[1]^.typ = top_reg) then
  1890. if (taicpu(p).oper[0]^.typ = top_reg)
  1891. then
  1892. case taicpu(p).opsize of
  1893. S_BL:
  1894. begin
  1895. if IsGP32Reg(getsupreg(taicpu(p).oper[1]^.reg)) and
  1896. not(CS_LittleSize in aktglobalswitches) and
  1897. (aktoptprocessor = ClassPentium) then
  1898. {Change "movzbl %reg1, %reg2" to
  1899. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1900. PentiumMMX}
  1901. begin
  1902. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  1903. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  1904. InsertLLItem(asml,p.previous, p, hp1);
  1905. taicpu(p).opcode := A_MOV;
  1906. taicpu(p).changeopsize(S_B);
  1907. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  1908. end;
  1909. end;
  1910. end
  1911. else if (taicpu(p).oper[0]^.typ = top_ref) and
  1912. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  1913. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  1914. not(CS_LittleSize in aktglobalswitches) and
  1915. IsGP32Reg(getsupreg(taicpu(p).oper[1]^.reg)) and
  1916. (aktoptprocessor = ClassPentium) and
  1917. (taicpu(p).opsize = S_BL) then
  1918. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1919. Pentium and PentiumMMX}
  1920. begin
  1921. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  1922. taicpu(p).oper[1]^.reg);
  1923. taicpu(p).opcode := A_MOV;
  1924. taicpu(p).changeopsize(S_B);
  1925. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  1926. InsertLLItem(asml,p.previous, p, hp1);
  1927. end;
  1928. end;
  1929. A_TEST, A_OR:
  1930. {removes the line marked with (x) from the sequence
  1931. and/or/xor/add/sub/... $x, %y
  1932. test/or %y, %y (x)
  1933. j(n)z _Label
  1934. as the first instruction already adjusts the ZF}
  1935. begin
  1936. if OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  1937. if GetLastInstruction(p, hp1) and
  1938. (tai(hp1).typ = ait_instruction) then
  1939. case taicpu(hp1).opcode Of
  1940. A_ADD, A_SUB, A_OR, A_XOR, A_AND{, A_SHL, A_SHR}:
  1941. begin
  1942. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  1943. begin
  1944. hp1 := tai(p.next);
  1945. asml.remove(p);
  1946. p.free;
  1947. p := tai(hp1);
  1948. continue
  1949. end;
  1950. end;
  1951. A_DEC, A_INC, A_NEG:
  1952. begin
  1953. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[0]^) then
  1954. begin
  1955. case taicpu(hp1).opcode Of
  1956. A_DEC, A_INC:
  1957. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  1958. begin
  1959. case taicpu(hp1).opcode Of
  1960. A_DEC: taicpu(hp1).opcode := A_SUB;
  1961. A_INC: taicpu(hp1).opcode := A_ADD;
  1962. end;
  1963. taicpu(hp1).Loadoper(1,taicpu(hp1).oper[0]^);
  1964. taicpu(hp1).LoadConst(0,1);
  1965. taicpu(hp1).ops:=2;
  1966. end
  1967. end;
  1968. hp1 := tai(p.next);
  1969. asml.remove(p);
  1970. p.free;
  1971. p := tai(hp1);
  1972. continue
  1973. end;
  1974. end
  1975. end
  1976. end;
  1977. end;
  1978. end;
  1979. end;
  1980. p := tai(p.next)
  1981. end;
  1982. end;
  1983. end.