popt386.pas 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  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. { when free'ing hp1, the ref. isn't decresed, so we don't
  602. increase it (FK)
  603. taicpu(p).oper[0]^.ref^.symbol.increfs;
  604. }
  605. asml.remove(hp1);
  606. hp1.free;
  607. GetFinalDestination(asml, taicpu(p),0);
  608. end
  609. else
  610. begin
  611. GetFinalDestination(asml, taicpu(p),0);
  612. p:=tai(p.next);
  613. continue;
  614. end;
  615. end
  616. else
  617. GetFinalDestination(asml, taicpu(p),0);
  618. end;
  619. end;
  620. end
  621. else
  622. { All other optimizes }
  623. begin
  624. for l := 0 to taicpu(p).ops-1 Do
  625. if (taicpu(p).oper[l]^.typ = top_ref) then
  626. With taicpu(p).oper[l]^.ref^ Do
  627. begin
  628. if (base = NR_NO) and
  629. (index <> NR_NO) and
  630. (scalefactor in [0,1]) then
  631. begin
  632. base := index;
  633. index := NR_NO
  634. end
  635. end;
  636. case taicpu(p).opcode Of
  637. A_AND:
  638. begin
  639. if (taicpu(p).oper[0]^.typ = top_const) and
  640. (taicpu(p).oper[1]^.typ = top_reg) and
  641. GetNextInstruction(p, hp1) and
  642. (tai(hp1).typ = ait_instruction) and
  643. (taicpu(hp1).opcode = A_AND) and
  644. (taicpu(hp1).oper[0]^.typ = top_const) and
  645. (taicpu(hp1).oper[1]^.typ = top_reg) and
  646. (taicpu(p).oper[1]^.reg = taicpu(hp1).oper[1]^.reg) then
  647. {change "and const1, reg; and const2, reg" to "and (const1 and const2), reg"}
  648. begin
  649. taicpu(p).LoadConst(0,taicpu(p).oper[0]^.val and taicpu(hp1).oper[0]^.val);
  650. asml.remove(hp1);
  651. hp1.free;
  652. end
  653. else
  654. {change "and x, reg; jxx" to "test x, reg", if reg is deallocated before the
  655. jump, but only if it's a conditional jump (PFV) }
  656. if (taicpu(p).oper[1]^.typ = top_reg) and
  657. GetNextInstruction(p, hp1) and
  658. (hp1.typ = ait_instruction) and
  659. (taicpu(hp1).is_jmp) and
  660. (taicpu(hp1).opcode<>A_JMP) and
  661. not(getsupreg(taicpu(p).oper[1]^.reg) in UsedRegs) then
  662. taicpu(p).opcode := A_TEST;
  663. end;
  664. A_CMP:
  665. begin
  666. if (taicpu(p).oper[0]^.typ = top_const) and
  667. (taicpu(p).oper[1]^.typ in [top_reg,top_ref]) and
  668. (taicpu(p).oper[0]^.val = 0) and
  669. GetNextInstruction(p, hp1) and
  670. (hp1.typ = ait_instruction) and
  671. (taicpu(hp1).is_jmp) and
  672. (taicpu(hp1).opcode=A_Jcc) and
  673. (taicpu(hp1).condition in [C_LE,C_BE]) and
  674. GetNextInstruction(hp1,hp2) and
  675. (hp2.typ = ait_instruction) and
  676. (taicpu(hp2).opcode = A_DEC) and
  677. OpsEqual(taicpu(hp2).oper[0]^,taicpu(p).oper[1]^) and
  678. GetNextInstruction(hp2, hp3) and
  679. (hp3.typ = ait_instruction) and
  680. (taicpu(hp3).is_jmp) and
  681. (taicpu(hp3).opcode = A_JMP) and
  682. GetNextInstruction(hp3, hp4) and
  683. FindLabel(tasmlabel(taicpu(hp1).oper[0]^.ref^.symbol),hp4) then
  684. begin
  685. taicpu(hp2).Opcode := A_SUB;
  686. taicpu(hp2).Loadoper(1,taicpu(hp2).oper[0]^);
  687. taicpu(hp2).LoadConst(0,1);
  688. taicpu(hp2).ops:=2;
  689. taicpu(hp3).Opcode := A_Jcc;
  690. case taicpu(hp1).condition of
  691. C_LE: taicpu(hp3).condition := C_GE;
  692. C_BE: taicpu(hp3).condition := C_AE;
  693. end;
  694. asml.remove(p);
  695. asml.remove(hp1);
  696. p.free;
  697. hp1.free;
  698. p := hp2;
  699. continue;
  700. end
  701. end;
  702. A_FLD:
  703. begin
  704. if (taicpu(p).oper[0]^.typ = top_reg) and
  705. GetNextInstruction(p, hp1) and
  706. (hp1.typ = Ait_Instruction) and
  707. (taicpu(hp1).oper[0]^.typ = top_reg) and
  708. (taicpu(hp1).oper[1]^.typ = top_reg) and
  709. (taicpu(hp1).oper[0]^.reg = NR_ST) and
  710. (taicpu(hp1).oper[1]^.reg = NR_ST1) then
  711. { change to
  712. fld reg fxxx reg,st
  713. fxxxp st, st1 (hp1)
  714. Remark: non commutative operations must be reversed!
  715. }
  716. begin
  717. case taicpu(hp1).opcode Of
  718. A_FMULP,A_FADDP,
  719. A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  720. begin
  721. case taicpu(hp1).opcode Of
  722. A_FADDP: taicpu(hp1).opcode := A_FADD;
  723. A_FMULP: taicpu(hp1).opcode := A_FMUL;
  724. A_FSUBP: taicpu(hp1).opcode := A_FSUBR;
  725. A_FSUBRP: taicpu(hp1).opcode := A_FSUB;
  726. A_FDIVP: taicpu(hp1).opcode := A_FDIVR;
  727. A_FDIVRP: taicpu(hp1).opcode := A_FDIV;
  728. end;
  729. taicpu(hp1).oper[0]^.reg := taicpu(p).oper[0]^.reg;
  730. taicpu(hp1).oper[1]^.reg := NR_ST;
  731. asml.remove(p);
  732. p.free;
  733. p := hp1;
  734. continue;
  735. end;
  736. end;
  737. end
  738. else
  739. if (taicpu(p).oper[0]^.typ = top_ref) and
  740. GetNextInstruction(p, hp2) and
  741. (hp2.typ = Ait_Instruction) and
  742. (taicpu(hp2).ops = 2) and
  743. (taicpu(hp2).oper[0]^.typ = top_reg) and
  744. (taicpu(hp2).oper[1]^.typ = top_reg) and
  745. (taicpu(p).opsize in [S_FS, S_FL]) and
  746. (taicpu(hp2).oper[0]^.reg = NR_ST) and
  747. (taicpu(hp2).oper[1]^.reg = NR_ST1) then
  748. if GetLastInstruction(p, hp1) and
  749. (hp1.typ = Ait_Instruction) and
  750. ((taicpu(hp1).opcode = A_FLD) or
  751. (taicpu(hp1).opcode = A_FST)) and
  752. (taicpu(hp1).opsize = taicpu(p).opsize) and
  753. (taicpu(hp1).oper[0]^.typ = top_ref) and
  754. RefsEqual(taicpu(p).oper[0]^.ref^, taicpu(hp1).oper[0]^.ref^) then
  755. if ((taicpu(hp2).opcode = A_FMULP) or
  756. (taicpu(hp2).opcode = A_FADDP)) then
  757. { change to
  758. fld/fst mem1 (hp1) fld/fst mem1
  759. fld mem1 (p) fadd/
  760. faddp/ fmul st, st
  761. fmulp st, st1 (hp2) }
  762. begin
  763. asml.remove(p);
  764. p.free;
  765. p := hp1;
  766. if (taicpu(hp2).opcode = A_FADDP) then
  767. taicpu(hp2).opcode := A_FADD
  768. else
  769. taicpu(hp2).opcode := A_FMUL;
  770. taicpu(hp2).oper[1]^.reg := NR_ST;
  771. end
  772. else
  773. { change to
  774. fld/fst mem1 (hp1) fld/fst mem1
  775. fld mem1 (p) fld st}
  776. begin
  777. taicpu(p).changeopsize(S_FL);
  778. taicpu(p).loadreg(0,NR_ST);
  779. end
  780. else
  781. begin
  782. case taicpu(hp2).opcode Of
  783. A_FMULP,A_FADDP,A_FSUBP,A_FDIVP,A_FSUBRP,A_FDIVRP:
  784. { change to
  785. fld/fst mem1 (hp1) fld/fst mem1
  786. fld mem2 (p) fxxx mem2
  787. fxxxp st, st1 (hp2) }
  788. begin
  789. case taicpu(hp2).opcode Of
  790. A_FADDP: taicpu(p).opcode := A_FADD;
  791. A_FMULP: taicpu(p).opcode := A_FMUL;
  792. A_FSUBP: taicpu(p).opcode := A_FSUBR;
  793. A_FSUBRP: taicpu(p).opcode := A_FSUB;
  794. A_FDIVP: taicpu(p).opcode := A_FDIVR;
  795. A_FDIVRP: taicpu(p).opcode := A_FDIV;
  796. end;
  797. asml.remove(hp2);
  798. hp2.free;
  799. end
  800. end
  801. end
  802. end;
  803. A_FSTP,A_FISTP:
  804. if doFpuLoadStoreOpt(asmL,p) then
  805. continue;
  806. A_LEA:
  807. begin
  808. {removes seg register prefixes from LEA operations, as they
  809. don't do anything}
  810. taicpu(p).oper[0]^.ref^.Segment := NR_NO;
  811. {changes "lea (%reg1), %reg2" into "mov %reg1, %reg2"}
  812. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  813. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX..RS_ESP]) and
  814. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  815. (not(Assigned(taicpu(p).oper[0]^.ref^.Symbol))) then
  816. if (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  817. (taicpu(p).oper[0]^.ref^.offset = 0) then
  818. begin
  819. hp1 := taicpu.op_reg_reg(A_MOV, S_L,taicpu(p).oper[0]^.ref^.base,
  820. taicpu(p).oper[1]^.reg);
  821. InsertLLItem(asml,p.previous,p.next, hp1);
  822. p.free;
  823. p := hp1;
  824. continue;
  825. end
  826. else if (taicpu(p).oper[0]^.ref^.offset = 0) then
  827. begin
  828. hp1 := tai(p.Next);
  829. asml.remove(p);
  830. p.free;
  831. p := hp1;
  832. continue;
  833. end
  834. else
  835. with taicpu(p).oper[0]^.ref^ do
  836. if (base = taicpu(p).oper[1]^.reg) then
  837. begin
  838. l := offset;
  839. if (l=1) then
  840. begin
  841. taicpu(p).opcode := A_INC;
  842. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  843. taicpu(p).ops := 1
  844. end
  845. else if (l=-1) then
  846. begin
  847. taicpu(p).opcode := A_DEC;
  848. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  849. taicpu(p).ops := 1;
  850. end
  851. else
  852. begin
  853. taicpu(p).opcode := A_ADD;
  854. taicpu(p).loadconst(0,l);
  855. end;
  856. end;
  857. end;
  858. A_MOV:
  859. begin
  860. TmpUsedRegs := UsedRegs;
  861. if (taicpu(p).oper[1]^.typ = top_reg) and
  862. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_EAX, RS_EBX, RS_ECX, RS_EDX, RS_ESI, RS_EDI]) and
  863. GetNextInstruction(p, hp1) and
  864. (tai(hp1).typ = ait_instruction) and
  865. (taicpu(hp1).opcode = A_MOV) and
  866. (taicpu(hp1).oper[0]^.typ = top_reg) and
  867. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  868. begin
  869. {we have "mov x, %treg; mov %treg, y}
  870. if not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg, hp1, TmpUsedRegs)) then
  871. {we've got "mov x, %treg; mov %treg, y; with %treg is not used after }
  872. case taicpu(p).oper[0]^.typ Of
  873. top_reg:
  874. begin
  875. { change "mov %reg, %treg; mov %treg, y"
  876. to "mov %reg, y" }
  877. taicpu(p).LoadOper(1,taicpu(hp1).oper[1]^);
  878. asml.remove(hp1);
  879. hp1.free;
  880. continue;
  881. end;
  882. top_ref:
  883. if (taicpu(hp1).oper[1]^.typ = top_reg) then
  884. begin
  885. { change "mov mem, %treg; mov %treg, %reg"
  886. to "mov mem, %reg" }
  887. taicpu(p).Loadoper(1,taicpu(hp1).oper[1]^);
  888. asml.remove(hp1);
  889. hp1.free;
  890. continue;
  891. end;
  892. end
  893. end
  894. else
  895. {Change "mov %reg1, %reg2; xxx %reg2, ???" to
  896. "mov %reg1, %reg2; xxx %reg1, ???" to avoid a write/read
  897. penalty}
  898. if (taicpu(p).oper[0]^.typ = top_reg) and
  899. (taicpu(p).oper[1]^.typ = top_reg) and
  900. GetNextInstruction(p,hp1) and
  901. (tai(hp1).typ = ait_instruction) and
  902. (taicpu(hp1).ops >= 1) and
  903. (taicpu(hp1).oper[0]^.typ = top_reg) and
  904. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) then
  905. {we have "mov %reg1, %reg2; XXX %reg2, ???"}
  906. begin
  907. if ((taicpu(hp1).opcode = A_OR) or
  908. (taicpu(hp1).opcode = A_TEST)) and
  909. (taicpu(hp1).oper[1]^.typ = top_reg) and
  910. (taicpu(hp1).oper[0]^.reg = taicpu(hp1).oper[1]^.reg) then
  911. {we have "mov %reg1, %reg2; test/or %reg2, %reg2"}
  912. begin
  913. TmpUsedRegs := UsedRegs;
  914. { reg1 will be used after the first instruction, }
  915. { so update the allocation info }
  916. allocRegBetween(asmL,taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  917. if GetNextInstruction(hp1, hp2) and
  918. (hp2.typ = ait_instruction) and
  919. taicpu(hp2).is_jmp and
  920. not(RegUsedAfterInstruction(taicpu(hp1).oper[0]^.reg, hp1, TmpUsedRegs)) then
  921. { change "mov %reg1, %reg2; test/or %reg2, %reg2; jxx" to
  922. "test %reg1, %reg1; jxx" }
  923. begin
  924. taicpu(hp1).Loadoper(0,taicpu(p).oper[0]^);
  925. taicpu(hp1).Loadoper(1,taicpu(p).oper[0]^);
  926. asml.remove(p);
  927. p.free;
  928. p := hp1;
  929. continue
  930. end
  931. else
  932. {change "mov %reg1, %reg2; test/or %reg2, %reg2" to
  933. "mov %reg1, %reg2; test/or %reg1, %reg1"}
  934. begin
  935. taicpu(hp1).Loadoper(0,taicpu(p).oper[0]^);
  936. taicpu(hp1).Loadoper(1,taicpu(p).oper[0]^);
  937. end;
  938. end
  939. { else
  940. if (taicpu(p.next)^.opcode
  941. in [A_PUSH, A_OR, A_XOR, A_AND, A_TEST])}
  942. {change "mov %reg1, %reg2; push/or/xor/... %reg2, ???" to
  943. "mov %reg1, %reg2; push/or/xor/... %reg1, ???"}
  944. end
  945. else
  946. {leave out the mov from "mov reg, x(%frame_pointer); leave/ret" (with
  947. x >= RetOffset) as it doesn't do anything (it writes either to a
  948. parameter or to the temporary storage room for the function
  949. result)}
  950. if GetNextInstruction(p, hp1) and
  951. (tai(hp1).typ = ait_instruction) then
  952. if ((taicpu(hp1).opcode = A_LEAVE) or
  953. (taicpu(hp1).opcode = A_RET)) and
  954. (taicpu(p).oper[1]^.typ = top_ref) and
  955. (taicpu(p).oper[1]^.ref^.base = current_procinfo.FramePointer) and
  956. not(assigned(current_procinfo.procdef.funcretsym) and
  957. (taicpu(p).oper[1]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  958. (taicpu(p).oper[1]^.ref^.index = NR_NO) and
  959. (taicpu(p).oper[0]^.typ = top_reg) then
  960. begin
  961. asml.remove(p);
  962. p.free;
  963. p := hp1;
  964. RemoveLastDeallocForFuncRes(asmL,p);
  965. end
  966. else
  967. if (taicpu(p).oper[0]^.typ = top_reg) and
  968. (taicpu(p).oper[1]^.typ = top_ref) and
  969. (taicpu(p).opsize = taicpu(hp1).opsize) and
  970. (taicpu(hp1).opcode = A_CMP) and
  971. (taicpu(hp1).oper[1]^.typ = top_ref) and
  972. RefsEqual(taicpu(p).oper[1]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  973. {change "mov reg1, mem1; cmp x, mem1" to "mov reg, mem1; cmp x, reg1"}
  974. begin
  975. taicpu(hp1).loadreg(1,taicpu(p).oper[0]^.reg);
  976. allocRegBetween(asmL,taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  977. end;
  978. { Next instruction is also a MOV ? }
  979. if GetNextInstruction(p, hp1) and
  980. (tai(hp1).typ = ait_instruction) and
  981. (taicpu(hp1).opcode = A_MOV) and
  982. (taicpu(hp1).opsize = taicpu(p).opsize) then
  983. begin
  984. if (taicpu(hp1).oper[0]^.typ = taicpu(p).oper[1]^.typ) and
  985. (taicpu(hp1).oper[1]^.typ = taicpu(p).oper[0]^.typ) then
  986. {mov reg1, mem1 or mov mem1, reg1
  987. mov mem2, reg2 mov reg2, mem2}
  988. begin
  989. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) then
  990. {mov reg1, mem1 or mov mem1, reg1
  991. mov mem2, reg1 mov reg2, mem1}
  992. begin
  993. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[1]^) then
  994. { Removes the second statement from
  995. mov reg1, mem1/reg2
  996. mov mem1/reg2, reg1 }
  997. begin
  998. if (taicpu(p).oper[0]^.typ = top_reg) then
  999. AllocRegBetween(asmL,taicpu(p).oper[0]^.reg,p,hp1,usedregs);
  1000. asml.remove(hp1);
  1001. hp1.free;
  1002. end
  1003. else
  1004. begin
  1005. TmpUsedRegs := UsedRegs;
  1006. UpdateUsedRegs(TmpUsedRegs, tai(hp1.next));
  1007. if (taicpu(p).oper[1]^.typ = top_ref) and
  1008. { mov reg1, mem1
  1009. mov mem2, reg1 }
  1010. (taicpu(hp1).oper[0]^.ref^.refaddr = addr_no) and
  1011. GetNextInstruction(hp1, hp2) and
  1012. (hp2.typ = ait_instruction) and
  1013. (taicpu(hp2).opcode = A_CMP) and
  1014. (taicpu(hp2).opsize = taicpu(p).opsize) and
  1015. (taicpu(hp2).oper[0]^.typ = TOp_Ref) and
  1016. (taicpu(hp2).oper[1]^.typ = TOp_Reg) and
  1017. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(p).oper[1]^.ref^) and
  1018. (taicpu(hp2).oper[1]^.reg= taicpu(p).oper[0]^.reg) and
  1019. not(RegUsedAfterInstruction(taicpu(p).oper[0]^.reg, hp2, TmpUsedRegs)) then
  1020. { change to
  1021. mov reg1, mem1 mov reg1, mem1
  1022. mov mem2, reg1 cmp reg1, mem2
  1023. cmp mem1, reg1 }
  1024. begin
  1025. asml.remove(hp2);
  1026. hp2.free;
  1027. taicpu(hp1).opcode := A_CMP;
  1028. taicpu(hp1).loadref(1,taicpu(hp1).oper[0]^.ref^);
  1029. taicpu(hp1).loadreg(0,taicpu(p).oper[0]^.reg);
  1030. end;
  1031. end;
  1032. end
  1033. else
  1034. begin
  1035. tmpUsedRegs := UsedRegs;
  1036. if GetNextInstruction(hp1, hp2) and
  1037. (taicpu(p).oper[0]^.typ = top_ref) and
  1038. (taicpu(p).oper[1]^.typ = top_reg) and
  1039. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1040. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  1041. (taicpu(hp1).oper[1]^.typ = top_ref) and
  1042. (tai(hp2).typ = ait_instruction) and
  1043. (taicpu(hp2).opcode = A_MOV) and
  1044. (taicpu(hp2).opsize = taicpu(p).opsize) and
  1045. (taicpu(hp2).oper[1]^.typ = top_reg) and
  1046. (taicpu(hp2).oper[0]^.typ = top_ref) and
  1047. RefsEqual(taicpu(hp2).oper[0]^.ref^, taicpu(hp1).oper[1]^.ref^) then
  1048. if not regInRef(getsupreg(taicpu(hp2).oper[1]^.reg),taicpu(hp2).oper[0]^.ref^) and
  1049. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,hp1,tmpUsedRegs)) then
  1050. { mov mem1, %reg1
  1051. mov %reg1, mem2
  1052. mov mem2, reg2
  1053. to:
  1054. mov mem1, reg2
  1055. mov reg2, mem2}
  1056. begin
  1057. AllocRegBetween(asmL,taicpu(hp2).oper[1]^.reg,p,hp2,usedregs);
  1058. taicpu(p).Loadoper(1,taicpu(hp2).oper[1]^);
  1059. taicpu(hp1).loadoper(0,taicpu(hp2).oper[1]^);
  1060. asml.remove(hp2);
  1061. hp2.free;
  1062. end
  1063. else
  1064. if (taicpu(p).oper[1]^.reg <> taicpu(hp2).oper[1]^.reg) and
  1065. not(RegInRef(getsupreg(taicpu(p).oper[1]^.reg),taicpu(p).oper[0]^.ref^)) and
  1066. not(RegInRef(getsupreg(taicpu(hp2).oper[1]^.reg),taicpu(hp2).oper[0]^.ref^)) then
  1067. { mov mem1, reg1 mov mem1, reg1
  1068. mov reg1, mem2 mov reg1, mem2
  1069. mov mem2, reg2 mov mem2, reg1
  1070. to: to:
  1071. mov mem1, reg1 mov mem1, reg1
  1072. mov mem1, reg2 mov reg1, mem2
  1073. mov reg1, mem2
  1074. or (if mem1 depends on reg1
  1075. and/or if mem2 depends on reg2)
  1076. to:
  1077. mov mem1, reg1
  1078. mov reg1, mem2
  1079. mov reg1, reg2
  1080. }
  1081. begin
  1082. taicpu(hp1).LoadRef(0,taicpu(p).oper[0]^.ref^);
  1083. taicpu(hp1).LoadReg(1,taicpu(hp2).oper[1]^.reg);
  1084. taicpu(hp2).LoadRef(1,taicpu(hp2).oper[0]^.ref^);
  1085. taicpu(hp2).LoadReg(0,taicpu(p).oper[1]^.reg);
  1086. allocRegBetween(asmL,taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1087. if (taicpu(p).oper[0]^.ref^.base <> NR_NO) and
  1088. (getsupreg(taicpu(p).oper[0]^.ref^.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1089. allocRegBetween(asmL,taicpu(p).oper[0]^.ref^.base,p,hp2,usedregs);
  1090. if (taicpu(p).oper[0]^.ref^.index <> NR_NO) and
  1091. (getsupreg(taicpu(p).oper[0]^.ref^.index) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) then
  1092. allocRegBetween(asmL,taicpu(p).oper[0]^.ref^.index,p,hp2,usedregs);
  1093. end
  1094. else
  1095. if (taicpu(hp1).Oper[0]^.reg <> taicpu(hp2).Oper[1]^.reg) then
  1096. begin
  1097. taicpu(hp2).LoadReg(0,taicpu(hp1).Oper[0]^.reg);
  1098. allocRegBetween(asmL,taicpu(p).oper[1]^.reg,p,hp2,usedregs);
  1099. end
  1100. else
  1101. begin
  1102. asml.remove(hp2);
  1103. hp2.free;
  1104. end
  1105. end
  1106. end
  1107. else
  1108. (* {movl [mem1],reg1
  1109. movl [mem1],reg2
  1110. to:
  1111. movl [mem1],reg1
  1112. movl reg1,reg2 }
  1113. if (taicpu(p).oper[0]^.typ = top_ref) and
  1114. (taicpu(p).oper[1]^.typ = top_reg) and
  1115. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1116. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1117. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1118. RefsEqual(TReference(taicpu(p).oper[0]^^),taicpu(hp1).oper[0]^^.ref^) and
  1119. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.base) and
  1120. (taicpu(p).oper[1]^.reg<>taicpu(hp1).oper[0]^^.ref^.index) then
  1121. taicpu(hp1).LoadReg(0,taicpu(p).oper[1]^.reg)
  1122. else*)
  1123. { movl const1,[mem1]
  1124. movl [mem1],reg1
  1125. to:
  1126. movl const1,reg1
  1127. movl reg1,[mem1] }
  1128. if (taicpu(p).oper[0]^.typ = top_const) and
  1129. (taicpu(p).oper[1]^.typ = top_ref) and
  1130. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1131. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1132. (taicpu(p).opsize = taicpu(hp1).opsize) and
  1133. RefsEqual(taicpu(hp1).oper[0]^.ref^,taicpu(p).oper[1]^.ref^) and
  1134. not(reginref(getsupreg(taicpu(hp1).oper[1]^.reg),taicpu(hp1).oper[0]^.ref^)) then
  1135. begin
  1136. allocregbetween(asml,taicpu(hp1).oper[1]^.reg,p,hp1,usedregs);
  1137. taicpu(hp1).LoadReg(0,taicpu(hp1).oper[1]^.reg);
  1138. taicpu(hp1).LoadRef(1,taicpu(p).oper[1]^.ref^);
  1139. taicpu(p).LoadReg(1,taicpu(hp1).oper[0]^.reg);
  1140. end
  1141. end;
  1142. end;
  1143. A_MOVZX:
  1144. begin
  1145. {removes superfluous And's after movzx's}
  1146. if (taicpu(p).oper[1]^.typ = top_reg) and
  1147. GetNextInstruction(p, hp1) and
  1148. (tai(hp1).typ = ait_instruction) and
  1149. (taicpu(hp1).opcode = A_AND) and
  1150. (taicpu(hp1).oper[0]^.typ = top_const) and
  1151. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1152. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1153. case taicpu(p).opsize Of
  1154. S_BL, S_BW:
  1155. if (taicpu(hp1).oper[0]^.val = $ff) then
  1156. begin
  1157. asml.remove(hp1);
  1158. hp1.free;
  1159. end;
  1160. S_WL:
  1161. if (taicpu(hp1).oper[0]^.val = $ffff) then
  1162. begin
  1163. asml.remove(hp1);
  1164. hp1.free;
  1165. end;
  1166. end;
  1167. {changes some movzx constructs to faster synonims (all examples
  1168. are given with eax/ax, but are also valid for other registers)}
  1169. if (taicpu(p).oper[1]^.typ = top_reg) then
  1170. if (taicpu(p).oper[0]^.typ = top_reg) then
  1171. case taicpu(p).opsize of
  1172. S_BW:
  1173. begin
  1174. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  1175. not(CS_LittleSize in aktglobalswitches) then
  1176. {Change "movzbw %al, %ax" to "andw $0x0ffh, %ax"}
  1177. begin
  1178. taicpu(p).opcode := A_AND;
  1179. taicpu(p).changeopsize(S_W);
  1180. taicpu(p).LoadConst(0,$ff);
  1181. end
  1182. else if GetNextInstruction(p, hp1) and
  1183. (tai(hp1).typ = ait_instruction) and
  1184. (taicpu(hp1).opcode = A_AND) and
  1185. (taicpu(hp1).oper[0]^.typ = top_const) and
  1186. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1187. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1188. {Change "movzbw %reg1, %reg2; andw $const, %reg2"
  1189. to "movw %reg1, reg2; andw $(const1 and $ff), %reg2"}
  1190. begin
  1191. taicpu(p).opcode := A_MOV;
  1192. taicpu(p).changeopsize(S_W);
  1193. setsubreg(taicpu(p).oper[0]^.reg,R_SUBW);
  1194. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1195. end;
  1196. end;
  1197. S_BL:
  1198. begin
  1199. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  1200. not(CS_LittleSize in aktglobalswitches) then
  1201. {Change "movzbl %al, %eax" to "andl $0x0ffh, %eax"}
  1202. begin
  1203. taicpu(p).opcode := A_AND;
  1204. taicpu(p).changeopsize(S_L);
  1205. taicpu(p).loadconst(0,$ff)
  1206. end
  1207. else if GetNextInstruction(p, hp1) and
  1208. (tai(hp1).typ = ait_instruction) and
  1209. (taicpu(hp1).opcode = A_AND) and
  1210. (taicpu(hp1).oper[0]^.typ = top_const) and
  1211. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1212. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1213. {Change "movzbl %reg1, %reg2; andl $const, %reg2"
  1214. to "movl %reg1, reg2; andl $(const1 and $ff), %reg2"}
  1215. begin
  1216. taicpu(p).opcode := A_MOV;
  1217. taicpu(p).changeopsize(S_L);
  1218. setsubreg(taicpu(p).oper[0]^.reg,R_SUBWHOLE);
  1219. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1220. end
  1221. end;
  1222. S_WL:
  1223. begin
  1224. if (getsupreg(taicpu(p).oper[0]^.reg)=getsupreg(taicpu(p).oper[1]^.reg)) and
  1225. not(CS_LittleSize in aktglobalswitches) then
  1226. {Change "movzwl %ax, %eax" to "andl $0x0ffffh, %eax"}
  1227. begin
  1228. taicpu(p).opcode := A_AND;
  1229. taicpu(p).changeopsize(S_L);
  1230. taicpu(p).LoadConst(0,$ffff);
  1231. end
  1232. else if GetNextInstruction(p, hp1) and
  1233. (tai(hp1).typ = ait_instruction) and
  1234. (taicpu(hp1).opcode = A_AND) and
  1235. (taicpu(hp1).oper[0]^.typ = top_const) and
  1236. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1237. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1238. {Change "movzwl %reg1, %reg2; andl $const, %reg2"
  1239. to "movl %reg1, reg2; andl $(const1 and $ffff), %reg2"}
  1240. begin
  1241. taicpu(p).opcode := A_MOV;
  1242. taicpu(p).changeopsize(S_L);
  1243. setsubreg(taicpu(p).oper[0]^.reg,R_SUBWHOLE);
  1244. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  1245. end;
  1246. end;
  1247. end
  1248. else if (taicpu(p).oper[0]^.typ = top_ref) then
  1249. begin
  1250. if GetNextInstruction(p, hp1) and
  1251. (tai(hp1).typ = ait_instruction) and
  1252. (taicpu(hp1).opcode = A_AND) and
  1253. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  1254. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  1255. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1256. begin
  1257. taicpu(p).opcode := A_MOV;
  1258. case taicpu(p).opsize Of
  1259. S_BL:
  1260. begin
  1261. taicpu(p).changeopsize(S_L);
  1262. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1263. end;
  1264. S_WL:
  1265. begin
  1266. taicpu(p).changeopsize(S_L);
  1267. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ffff);
  1268. end;
  1269. S_BW:
  1270. begin
  1271. taicpu(p).changeopsize(S_W);
  1272. taicpu(hp1).LoadConst(0,taicpu(hp1).oper[0]^.val and $ff);
  1273. end;
  1274. end;
  1275. end;
  1276. end;
  1277. end;
  1278. (* should not be generated anymore by the current code generator
  1279. A_POP:
  1280. begin
  1281. if target_info.system=system_i386_go32v2 then
  1282. begin
  1283. { Transform a series of pop/pop/pop/push/push/push to }
  1284. { 'movl x(%esp),%reg' for go32v2 (not for the rest, }
  1285. { because I'm not sure whether they can cope with }
  1286. { 'movl x(%esp),%reg' with x > 0, I believe we had }
  1287. { such a problem when using esp as frame pointer (JM) }
  1288. if (taicpu(p).oper[0]^.typ = top_reg) then
  1289. begin
  1290. hp1 := p;
  1291. hp2 := p;
  1292. l := 0;
  1293. while getNextInstruction(hp1,hp1) and
  1294. (hp1.typ = ait_instruction) and
  1295. (taicpu(hp1).opcode = A_POP) and
  1296. (taicpu(hp1).oper[0]^.typ = top_reg) do
  1297. begin
  1298. hp2 := hp1;
  1299. inc(l,4);
  1300. end;
  1301. getLastInstruction(p,hp3);
  1302. l1 := 0;
  1303. while (hp2 <> hp3) and
  1304. assigned(hp1) and
  1305. (hp1.typ = ait_instruction) and
  1306. (taicpu(hp1).opcode = A_PUSH) and
  1307. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1308. (taicpu(hp1).oper[0]^.reg.enum = taicpu(hp2).oper[0]^.reg.enum) do
  1309. begin
  1310. { change it to a two op operation }
  1311. taicpu(hp2).oper[1]^.typ:=top_none;
  1312. taicpu(hp2).ops:=2;
  1313. taicpu(hp2).opcode := A_MOV;
  1314. taicpu(hp2).Loadoper(1,taicpu(hp1).oper[0]^);
  1315. reference_reset(tmpref);
  1316. tmpRef.base.enum:=R_INTREGISTER;
  1317. tmpRef.base.number:=NR_STACK_POINTER_REG;
  1318. convert_register_to_enum(tmpref.base);
  1319. tmpRef.offset := l;
  1320. taicpu(hp2).loadRef(0,tmpRef);
  1321. hp4 := hp1;
  1322. getNextInstruction(hp1,hp1);
  1323. asml.remove(hp4);
  1324. hp4.free;
  1325. getLastInstruction(hp2,hp2);
  1326. dec(l,4);
  1327. inc(l1);
  1328. end;
  1329. if l <> -4 then
  1330. begin
  1331. inc(l,4);
  1332. for l1 := l1 downto 1 do
  1333. begin
  1334. getNextInstruction(hp2,hp2);
  1335. dec(taicpu(hp2).oper[0]^.ref^.offset,l);
  1336. end
  1337. end
  1338. end
  1339. end
  1340. else
  1341. begin
  1342. if (taicpu(p).oper[0]^.typ = top_reg) and
  1343. GetNextInstruction(p, hp1) and
  1344. (tai(hp1).typ=ait_instruction) and
  1345. (taicpu(hp1).opcode=A_PUSH) and
  1346. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1347. (taicpu(hp1).oper[0]^.reg.enum=taicpu(p).oper[0]^.reg.enum) then
  1348. begin
  1349. { change it to a two op operation }
  1350. taicpu(p).oper[1]^.typ:=top_none;
  1351. taicpu(p).ops:=2;
  1352. taicpu(p).opcode := A_MOV;
  1353. taicpu(p).Loadoper(1,taicpu(p).oper[0]^);
  1354. reference_reset(tmpref);
  1355. TmpRef.base.enum := R_ESP;
  1356. taicpu(p).LoadRef(0,TmpRef);
  1357. asml.remove(hp1);
  1358. hp1.free;
  1359. end;
  1360. end;
  1361. end;
  1362. *)
  1363. A_PUSH:
  1364. begin
  1365. if (taicpu(p).opsize = S_W) and
  1366. (taicpu(p).oper[0]^.typ = Top_Const) and
  1367. GetNextInstruction(p, hp1) and
  1368. (tai(hp1).typ = ait_instruction) and
  1369. (taicpu(hp1).opcode = A_PUSH) and
  1370. (taicpu(hp1).oper[0]^.typ = Top_Const) and
  1371. (taicpu(hp1).opsize = S_W) then
  1372. begin
  1373. taicpu(p).changeopsize(S_L);
  1374. taicpu(p).LoadConst(0,taicpu(p).oper[0]^.val shl 16 + word(taicpu(hp1).oper[0]^.val));
  1375. asml.remove(hp1);
  1376. hp1.free;
  1377. end;
  1378. end;
  1379. A_SHL, A_SAL:
  1380. begin
  1381. if (taicpu(p).oper[0]^.typ = Top_Const) and
  1382. (taicpu(p).oper[1]^.typ = Top_Reg) and
  1383. (taicpu(p).opsize = S_L) and
  1384. (taicpu(p).oper[0]^.val <= 3) then
  1385. {Changes "shl const, %reg32; add const/reg, %reg32" to one lea statement}
  1386. begin
  1387. TmpBool1 := True; {should we check the next instruction?}
  1388. TmpBool2 := False; {have we found an add/sub which could be
  1389. integrated in the lea?}
  1390. reference_reset(tmpref);
  1391. TmpRef.index := taicpu(p).oper[1]^.reg;
  1392. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  1393. while TmpBool1 and
  1394. GetNextInstruction(p, hp1) and
  1395. (tai(hp1).typ = ait_instruction) and
  1396. ((((taicpu(hp1).opcode = A_ADD) or
  1397. (taicpu(hp1).opcode = A_SUB)) and
  1398. (taicpu(hp1).oper[1]^.typ = Top_Reg) and
  1399. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg)) or
  1400. (((taicpu(hp1).opcode = A_INC) or
  1401. (taicpu(hp1).opcode = A_DEC)) and
  1402. (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  1403. (taicpu(hp1).oper[0]^.reg = taicpu(p).oper[1]^.reg))) Do
  1404. begin
  1405. TmpBool1 := False;
  1406. if (taicpu(hp1).oper[0]^.typ = Top_Const) then
  1407. begin
  1408. TmpBool1 := True;
  1409. TmpBool2 := True;
  1410. case taicpu(hp1).opcode of
  1411. A_ADD:
  1412. inc(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  1413. A_SUB:
  1414. dec(TmpRef.offset, longint(taicpu(hp1).oper[0]^.val));
  1415. end;
  1416. asml.remove(hp1);
  1417. hp1.free;
  1418. end
  1419. else
  1420. if (taicpu(hp1).oper[0]^.typ = Top_Reg) and
  1421. (((taicpu(hp1).opcode = A_ADD) and
  1422. (TmpRef.base = NR_NO)) or
  1423. (taicpu(hp1).opcode = A_INC) or
  1424. (taicpu(hp1).opcode = A_DEC)) then
  1425. begin
  1426. TmpBool1 := True;
  1427. TmpBool2 := True;
  1428. case taicpu(hp1).opcode of
  1429. A_ADD:
  1430. TmpRef.base := taicpu(hp1).oper[0]^.reg;
  1431. A_INC:
  1432. inc(TmpRef.offset);
  1433. A_DEC:
  1434. dec(TmpRef.offset);
  1435. end;
  1436. asml.remove(hp1);
  1437. hp1.free;
  1438. end;
  1439. end;
  1440. if TmpBool2 or
  1441. ((aktoptprocessor < ClassPentium2) and
  1442. (taicpu(p).oper[0]^.val <= 3) and
  1443. not(CS_LittleSize in aktglobalswitches)) then
  1444. begin
  1445. if not(TmpBool2) and
  1446. (taicpu(p).oper[0]^.val = 1) then
  1447. begin
  1448. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  1449. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg)
  1450. end
  1451. else
  1452. hp1 := taicpu.op_ref_reg(A_LEA, S_L, TmpRef,
  1453. taicpu(p).oper[1]^.reg);
  1454. InsertLLItem(asml,p.previous, p.next, hp1);
  1455. p.free;
  1456. p := hp1;
  1457. end;
  1458. end
  1459. else
  1460. if (aktoptprocessor < ClassPentium2) and
  1461. (taicpu(p).oper[0]^.typ = top_const) and
  1462. (taicpu(p).oper[1]^.typ = top_reg) then
  1463. if (taicpu(p).oper[0]^.val = 1) then
  1464. {changes "shl $1, %reg" to "add %reg, %reg", which is the same on a 386,
  1465. but faster on a 486, and Tairable in both U and V pipes on the Pentium
  1466. (unlike shl, which is only Tairable in the U pipe)}
  1467. begin
  1468. hp1 := taicpu.Op_reg_reg(A_ADD,taicpu(p).opsize,
  1469. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  1470. InsertLLItem(asml,p.previous, p.next, hp1);
  1471. p.free;
  1472. p := hp1;
  1473. end
  1474. else if (taicpu(p).opsize = S_L) and
  1475. (taicpu(p).oper[0]^.val<= 3) then
  1476. {changes "shl $2, %reg" to "lea (,%reg,4), %reg"
  1477. "shl $3, %reg" to "lea (,%reg,8), %reg}
  1478. begin
  1479. reference_reset(tmpref);
  1480. TmpRef.index := taicpu(p).oper[1]^.reg;
  1481. TmpRef.scalefactor := 1 shl taicpu(p).oper[0]^.val;
  1482. hp1 := taicpu.Op_ref_reg(A_LEA,S_L,TmpRef, taicpu(p).oper[1]^.reg);
  1483. InsertLLItem(asml,p.previous, p.next, hp1);
  1484. p.free;
  1485. p := hp1;
  1486. end
  1487. end;
  1488. A_SETcc :
  1489. { changes
  1490. setcc (funcres) setcc reg
  1491. movb (funcres), reg to leave/ret
  1492. leave/ret }
  1493. begin
  1494. if (taicpu(p).oper[0]^.typ = top_ref) and
  1495. GetNextInstruction(p, hp1) and
  1496. GetNextInstruction(hp1, hp2) and
  1497. (hp2.typ = ait_instruction) and
  1498. ((taicpu(hp2).opcode = A_LEAVE) or
  1499. (taicpu(hp2).opcode = A_RET)) and
  1500. (taicpu(p).oper[0]^.ref^.base = current_procinfo.FramePointer) and
  1501. (taicpu(p).oper[0]^.ref^.index = NR_NO) and
  1502. not(assigned(current_procinfo.procdef.funcretsym) and
  1503. (taicpu(p).oper[0]^.ref^.offset < tabstractnormalvarsym(current_procinfo.procdef.funcretsym).localloc.reference.offset)) and
  1504. (hp1.typ = ait_instruction) and
  1505. (taicpu(hp1).opcode = A_MOV) and
  1506. (taicpu(hp1).opsize = S_B) and
  1507. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1508. RefsEqual(taicpu(hp1).oper[0]^.ref^, taicpu(p).oper[0]^.ref^) then
  1509. begin
  1510. taicpu(p).LoadReg(0,taicpu(hp1).oper[1]^.reg);
  1511. asml.remove(hp1);
  1512. hp1.free;
  1513. end
  1514. end;
  1515. A_SUB:
  1516. { * change "subl $2, %esp; pushw x" to "pushl x"}
  1517. { * change "sub/add const1, reg" or "dec reg" followed by
  1518. "sub const2, reg" to one "sub ..., reg" }
  1519. begin
  1520. if (taicpu(p).oper[0]^.typ = top_const) and
  1521. (taicpu(p).oper[1]^.typ = top_reg) then
  1522. if (taicpu(p).oper[0]^.val = 2) and
  1523. (taicpu(p).oper[1]^.reg = NR_ESP) and
  1524. { Don't do the sub/push optimization if the sub }
  1525. { comes from setting up the stack frame (JM) }
  1526. (not getLastInstruction(p,hp1) or
  1527. (hp1.typ <> ait_instruction) or
  1528. (taicpu(hp1).opcode <> A_MOV) or
  1529. (taicpu(hp1).oper[0]^.typ <> top_reg) or
  1530. (taicpu(hp1).oper[0]^.reg <> NR_ESP) or
  1531. (taicpu(hp1).oper[1]^.typ <> top_reg) or
  1532. (taicpu(hp1).oper[1]^.reg <> NR_EBP)) then
  1533. begin
  1534. hp1 := tai(p.next);
  1535. while Assigned(hp1) and
  1536. (tai(hp1).typ in [ait_instruction]+SkipInstr) and
  1537. not regReadByInstruction(RS_ESP,hp1) and
  1538. not regModifiedByInstruction(RS_ESP,hp1) do
  1539. hp1 := tai(hp1.next);
  1540. if Assigned(hp1) and
  1541. (tai(hp1).typ = ait_instruction) and
  1542. (taicpu(hp1).opcode = A_PUSH) and
  1543. (taicpu(hp1).opsize = S_W) then
  1544. begin
  1545. taicpu(hp1).changeopsize(S_L);
  1546. if taicpu(hp1).oper[0]^.typ=top_reg then
  1547. setsubreg(taicpu(hp1).oper[0]^.reg,R_SUBWHOLE);
  1548. hp1 := tai(p.next);
  1549. asml.remove(p);
  1550. p.free;
  1551. p := hp1;
  1552. continue
  1553. end;
  1554. if DoSubAddOpt(p) then
  1555. continue;
  1556. end
  1557. else if DoSubAddOpt(p) then
  1558. continue
  1559. end;
  1560. end;
  1561. end; { if is_jmp }
  1562. end;
  1563. end;
  1564. updateUsedRegs(UsedRegs,p);
  1565. p:=tai(p.next);
  1566. end;
  1567. end;
  1568. function isFoldableArithOp(hp1: taicpu; reg: tregister): boolean;
  1569. begin
  1570. isFoldableArithOp := False;
  1571. case hp1.opcode of
  1572. A_ADD,A_SUB,A_OR,A_XOR,A_AND,A_SHL,A_SHR,A_SAR:
  1573. isFoldableArithOp :=
  1574. ((taicpu(hp1).oper[0]^.typ = top_const) or
  1575. ((taicpu(hp1).oper[0]^.typ = top_reg) and
  1576. (taicpu(hp1).oper[0]^.reg <> reg))) and
  1577. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1578. (taicpu(hp1).oper[1]^.reg = reg);
  1579. A_INC,A_DEC:
  1580. isFoldableArithOp :=
  1581. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1582. (taicpu(hp1).oper[0]^.reg = reg);
  1583. end;
  1584. end;
  1585. procedure PeepHoleOptPass2(asml: taasmoutput; BlockStart, BlockEnd: tai);
  1586. {$ifdef USECMOV}
  1587. function CanBeCMOV(p : tai) : boolean;
  1588. begin
  1589. CanBeCMOV:=assigned(p) and (p.typ=ait_instruction) and
  1590. (taicpu(p).opcode=A_MOV) and
  1591. (taicpu(p).opsize in [S_L,S_W]) and
  1592. ((taicpu(p).oper[0]^.typ = top_reg)
  1593. { we can't use cmov ref,reg because
  1594. ref could be nil and cmov still throws an exception
  1595. if ref=nil but the mov isn't done (FK)
  1596. or ((taicpu(p).oper[0]^.typ = top_ref) and
  1597. (taicpu(p).oper[0]^.ref^.refaddr = addr_no))
  1598. }
  1599. ) and
  1600. (taicpu(p).oper[1]^.typ in [top_reg]);
  1601. end;
  1602. {$endif USECMOV}
  1603. var
  1604. p,hp1,hp2: tai;
  1605. {$ifdef USECMOV}
  1606. l : longint;
  1607. condition : tasmcond;
  1608. hp3: tai;
  1609. {$endif USECMOV}
  1610. UsedRegs, TmpUsedRegs: TRegSet;
  1611. begin
  1612. p := BlockStart;
  1613. UsedRegs := [];
  1614. while (p <> BlockEnd) Do
  1615. begin
  1616. UpdateUsedRegs(UsedRegs, tai(p.next));
  1617. case p.Typ Of
  1618. Ait_Instruction:
  1619. begin
  1620. case taicpu(p).opcode Of
  1621. {$ifdef USECMOV}
  1622. A_Jcc:
  1623. if (aktspecificoptprocessor>=ClassPentium2) then
  1624. begin
  1625. { check for
  1626. jCC xxx
  1627. <several movs>
  1628. xxx:
  1629. }
  1630. l:=0;
  1631. GetNextInstruction(p, hp1);
  1632. while assigned(hp1) and
  1633. CanBeCMOV(hp1) and
  1634. { stop on labels }
  1635. not(hp1.typ=ait_label) do
  1636. begin
  1637. inc(l);
  1638. GetNextInstruction(hp1,hp1);
  1639. end;
  1640. if assigned(hp1) then
  1641. begin
  1642. if FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1643. begin
  1644. if (l<=4) and (l>0) then
  1645. begin
  1646. condition:=inverse_cond(taicpu(p).condition);
  1647. hp2:=p;
  1648. GetNextInstruction(p,hp1);
  1649. p:=hp1;
  1650. repeat
  1651. taicpu(hp1).opcode:=A_CMOVcc;
  1652. taicpu(hp1).condition:=condition;
  1653. GetNextInstruction(hp1,hp1);
  1654. until not(assigned(hp1)) or
  1655. not(CanBeCMOV(hp1));
  1656. { wait with removing else GetNextInstruction could
  1657. ignore the label if it was the only usage in the
  1658. jump moved away }
  1659. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1660. asml.remove(hp2);
  1661. hp2.free;
  1662. continue;
  1663. end;
  1664. end
  1665. else
  1666. begin
  1667. { check further for
  1668. jCC xxx
  1669. <several movs 1>
  1670. jmp yyy
  1671. xxx:
  1672. <several movs 2>
  1673. yyy:
  1674. }
  1675. { hp2 points to jmp yyy }
  1676. hp2:=hp1;
  1677. { skip hp1 to xxx }
  1678. GetNextInstruction(hp1, hp1);
  1679. if assigned(hp2) and
  1680. assigned(hp1) and
  1681. (l<=3) and
  1682. (hp2.typ=ait_instruction) and
  1683. (taicpu(hp2).is_jmp) and
  1684. (taicpu(hp2).condition=C_None) and
  1685. { real label and jump, no further references to the
  1686. label are allowed }
  1687. (tasmlabel(taicpu(p).oper[0]^.ref^.symbol).getrefs=2) and
  1688. FindLabel(tasmlabel(taicpu(p).oper[0]^.ref^.symbol),hp1) then
  1689. begin
  1690. l:=0;
  1691. { skip hp1 to <several moves 2> }
  1692. GetNextInstruction(hp1, hp1);
  1693. while assigned(hp1) and
  1694. CanBeCMOV(hp1) do
  1695. begin
  1696. inc(l);
  1697. GetNextInstruction(hp1, hp1);
  1698. end;
  1699. { hp1 points to yyy: }
  1700. if assigned(hp1) and
  1701. FindLabel(tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol),hp1) then
  1702. begin
  1703. condition:=inverse_cond(taicpu(p).condition);
  1704. GetNextInstruction(p,hp1);
  1705. hp3:=p;
  1706. p:=hp1;
  1707. repeat
  1708. taicpu(hp1).opcode:=A_CMOVcc;
  1709. taicpu(hp1).condition:=condition;
  1710. GetNextInstruction(hp1,hp1);
  1711. until not(assigned(hp1)) or
  1712. not(CanBeCMOV(hp1));
  1713. { hp2 is still at jmp yyy }
  1714. GetNextInstruction(hp2,hp1);
  1715. { hp2 is now at xxx: }
  1716. condition:=inverse_cond(condition);
  1717. GetNextInstruction(hp1,hp1);
  1718. { hp1 is now at <several movs 2> }
  1719. repeat
  1720. taicpu(hp1).opcode:=A_CMOVcc;
  1721. taicpu(hp1).condition:=condition;
  1722. GetNextInstruction(hp1,hp1);
  1723. until not(assigned(hp1)) or
  1724. not(CanBeCMOV(hp1));
  1725. {
  1726. asml.remove(hp1.next)
  1727. hp1.next.free;
  1728. asml.remove(hp1);
  1729. hp1.free;
  1730. }
  1731. { remove jCC }
  1732. tasmlabel(taicpu(hp3).oper[0]^.ref^.symbol).decrefs;
  1733. asml.remove(hp3);
  1734. hp3.free;
  1735. { remove jmp }
  1736. tasmlabel(taicpu(hp2).oper[0]^.ref^.symbol).decrefs;
  1737. asml.remove(hp2);
  1738. hp2.free;
  1739. continue;
  1740. end;
  1741. end;
  1742. end;
  1743. end;
  1744. end;
  1745. {$endif USECMOV}
  1746. A_FSTP,A_FISTP:
  1747. if doFpuLoadStoreOpt(asmL,p) then
  1748. continue;
  1749. A_IMUL:
  1750. begin
  1751. if (taicpu(p).ops >= 2) and
  1752. ((taicpu(p).oper[0]^.typ = top_const) or
  1753. ((taicpu(p).oper[0]^.typ = top_ref) and (taicpu(p).oper[0]^.ref^.refaddr=addr_full))) and
  1754. (taicpu(p).oper[1]^.typ = top_reg) and
  1755. ((taicpu(p).ops = 2) or
  1756. ((taicpu(p).oper[2]^.typ = top_reg) and
  1757. (taicpu(p).oper[2]^.reg = taicpu(p).oper[1]^.reg))) and
  1758. getLastInstruction(p,hp1) and
  1759. (hp1.typ = ait_instruction) and
  1760. (taicpu(hp1).opcode = A_MOV) and
  1761. (taicpu(hp1).oper[0]^.typ = top_reg) and
  1762. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1763. (taicpu(hp1).oper[1]^.reg = taicpu(p).oper[1]^.reg) then
  1764. { change "mov reg1,reg2; imul y,reg2" to "imul y,reg1,reg2" }
  1765. begin
  1766. taicpu(p).ops := 3;
  1767. taicpu(p).loadreg(1,taicpu(hp1).oper[0]^.reg);
  1768. taicpu(p).loadreg(2,taicpu(hp1).oper[1]^.reg);
  1769. asml.remove(hp1);
  1770. hp1.free;
  1771. end;
  1772. end;
  1773. A_MOV:
  1774. begin
  1775. if (taicpu(p).oper[0]^.typ = top_reg) and
  1776. (taicpu(p).oper[1]^.typ = top_reg) and
  1777. GetNextInstruction(p, hp1) and
  1778. (hp1.typ = ait_Instruction) and
  1779. ((taicpu(hp1).opcode = A_MOV) or
  1780. (taicpu(hp1).opcode = A_MOVZX) or
  1781. (taicpu(hp1).opcode = A_MOVSX)) and
  1782. (taicpu(hp1).oper[0]^.typ = top_ref) and
  1783. (taicpu(hp1).oper[1]^.typ = top_reg) and
  1784. ((taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) or
  1785. (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg)) and
  1786. (getsupreg(taicpu(hp1).oper[1]^.reg) = getsupreg(taicpu(p).oper[1]^.reg)) then
  1787. {mov reg1, reg2
  1788. mov/zx/sx (reg2, ..), reg2 to mov/zx/sx (reg1, ..), reg2}
  1789. begin
  1790. if (taicpu(hp1).oper[0]^.ref^.base = taicpu(p).oper[1]^.reg) then
  1791. taicpu(hp1).oper[0]^.ref^.base := taicpu(p).oper[0]^.reg;
  1792. if (taicpu(hp1).oper[0]^.ref^.index = taicpu(p).oper[1]^.reg) then
  1793. taicpu(hp1).oper[0]^.ref^.index := taicpu(p).oper[0]^.reg;
  1794. asml.remove(p);
  1795. p.free;
  1796. p := hp1;
  1797. continue;
  1798. end
  1799. else if (taicpu(p).oper[0]^.typ = top_ref) and
  1800. GetNextInstruction(p,hp1) and
  1801. (hp1.typ = ait_instruction) and
  1802. IsFoldableArithOp(taicpu(hp1),taicpu(p).oper[1]^.reg) and
  1803. GetNextInstruction(hp1,hp2) and
  1804. (hp2.typ = ait_instruction) and
  1805. (taicpu(hp2).opcode = A_MOV) and
  1806. (taicpu(hp2).oper[0]^.typ = top_reg) and
  1807. (taicpu(hp2).oper[0]^.reg = taicpu(p).oper[1]^.reg) and
  1808. (taicpu(hp2).oper[1]^.typ = top_ref) then
  1809. begin
  1810. TmpUsedRegs := UsedRegs;
  1811. UpdateUsedRegs(TmpUsedRegs,tai(hp1.next));
  1812. if (RefsEqual(taicpu(hp2).oper[1]^.ref^, taicpu(p).oper[0]^.ref^) and
  1813. not(RegUsedAfterInstruction(taicpu(p).oper[1]^.reg,
  1814. hp2, TmpUsedRegs))) then
  1815. { change mov (ref), reg }
  1816. { add/sub/or/... reg2/$const, reg }
  1817. { mov reg, (ref) }
  1818. { # release reg }
  1819. { to add/sub/or/... reg2/$const, (ref) }
  1820. begin
  1821. case taicpu(hp1).opcode of
  1822. A_INC,A_DEC:
  1823. taicpu(hp1).LoadRef(0,taicpu(p).oper[0]^.ref^)
  1824. else
  1825. taicpu(hp1).LoadRef(1,taicpu(p).oper[0]^.ref^);
  1826. end;
  1827. asml.remove(p);
  1828. asml.remove(hp2);
  1829. p.free;
  1830. hp2.free;
  1831. p := hp1
  1832. end;
  1833. end
  1834. end;
  1835. end;
  1836. end;
  1837. end;
  1838. p := tai(p.next)
  1839. end;
  1840. end;
  1841. procedure PostPeepHoleOpts(asml: taasmoutput; BlockStart, BlockEnd: tai);
  1842. var
  1843. p,hp1,hp2: tai;
  1844. begin
  1845. p := BlockStart;
  1846. while (p <> BlockEnd) Do
  1847. begin
  1848. case p.Typ Of
  1849. Ait_Instruction:
  1850. begin
  1851. case taicpu(p).opcode Of
  1852. A_CALL:
  1853. if (AktOptProcessor < ClassPentium2) and
  1854. GetNextInstruction(p, hp1) and
  1855. (hp1.typ = ait_instruction) and
  1856. (taicpu(hp1).opcode = A_JMP) and
  1857. ((taicpu(hp1).oper[0]^.typ=top_ref) and (taicpu(hp1).oper[0]^.ref^.refaddr=addr_full)) then
  1858. begin
  1859. hp2 := taicpu.Op_sym(A_PUSH,S_L,taicpu(hp1).oper[0]^.ref^.symbol);
  1860. InsertLLItem(asml, p.previous, p, hp2);
  1861. taicpu(p).opcode := A_JMP;
  1862. taicpu(p).is_jmp := true;
  1863. asml.remove(hp1);
  1864. hp1.free;
  1865. end;
  1866. A_CMP:
  1867. begin
  1868. if (taicpu(p).oper[0]^.typ = top_const) and
  1869. (taicpu(p).oper[0]^.val = 0) and
  1870. (taicpu(p).oper[1]^.typ = top_reg) then
  1871. {change "cmp $0, %reg" to "test %reg, %reg"}
  1872. begin
  1873. taicpu(p).opcode := A_TEST;
  1874. taicpu(p).loadreg(0,taicpu(p).oper[1]^.reg);
  1875. continue;
  1876. end;
  1877. end;
  1878. (*
  1879. Optimization is not safe; xor clears the carry flag.
  1880. See test/tgadint64 in the test suite.
  1881. A_MOV:
  1882. if (taicpu(p).oper[0]^.typ = Top_Const) and
  1883. (taicpu(p).oper[0]^.val = 0) and
  1884. (taicpu(p).oper[1]^.typ = Top_Reg) then
  1885. { change "mov $0, %reg" into "xor %reg, %reg" }
  1886. begin
  1887. taicpu(p).opcode := A_XOR;
  1888. taicpu(p).LoadReg(0,taicpu(p).oper[1]^.reg);
  1889. end;
  1890. *)
  1891. A_MOVZX:
  1892. { if register vars are on, it's possible there is code like }
  1893. { "cmpl $3,%eax; movzbl 8(%ebp),%ebx; je .Lxxx" }
  1894. { so we can't safely replace the movzx then with xor/mov, }
  1895. { since that would change the flags (JM) }
  1896. if not(cs_regvars in aktglobalswitches) then
  1897. begin
  1898. if (taicpu(p).oper[1]^.typ = top_reg) then
  1899. if (taicpu(p).oper[0]^.typ = top_reg)
  1900. then
  1901. case taicpu(p).opsize of
  1902. S_BL:
  1903. begin
  1904. if IsGP32Reg(getsupreg(taicpu(p).oper[1]^.reg)) and
  1905. not(CS_LittleSize in aktglobalswitches) and
  1906. (aktoptprocessor = ClassPentium) then
  1907. {Change "movzbl %reg1, %reg2" to
  1908. "xorl %reg2, %reg2; movb %reg1, %reg2" for Pentium and
  1909. PentiumMMX}
  1910. begin
  1911. hp1 := taicpu.op_reg_reg(A_XOR, S_L,
  1912. taicpu(p).oper[1]^.reg, taicpu(p).oper[1]^.reg);
  1913. InsertLLItem(asml,p.previous, p, hp1);
  1914. taicpu(p).opcode := A_MOV;
  1915. taicpu(p).changeopsize(S_B);
  1916. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  1917. end;
  1918. end;
  1919. end
  1920. else if (taicpu(p).oper[0]^.typ = top_ref) and
  1921. (taicpu(p).oper[0]^.ref^.base <> taicpu(p).oper[1]^.reg) and
  1922. (taicpu(p).oper[0]^.ref^.index <> taicpu(p).oper[1]^.reg) and
  1923. not(CS_LittleSize in aktglobalswitches) and
  1924. IsGP32Reg(getsupreg(taicpu(p).oper[1]^.reg)) and
  1925. (aktoptprocessor = ClassPentium) and
  1926. (taicpu(p).opsize = S_BL) then
  1927. {changes "movzbl mem, %reg" to "xorl %reg, %reg; movb mem, %reg8" for
  1928. Pentium and PentiumMMX}
  1929. begin
  1930. hp1 := taicpu.Op_reg_reg(A_XOR, S_L, taicpu(p).oper[1]^.reg,
  1931. taicpu(p).oper[1]^.reg);
  1932. taicpu(p).opcode := A_MOV;
  1933. taicpu(p).changeopsize(S_B);
  1934. setsubreg(taicpu(p).oper[1]^.reg,R_SUBL);
  1935. InsertLLItem(asml,p.previous, p, hp1);
  1936. end;
  1937. end;
  1938. A_TEST, A_OR:
  1939. {removes the line marked with (x) from the sequence
  1940. and/or/xor/add/sub/... $x, %y
  1941. test/or %y, %y (x)
  1942. j(n)z _Label
  1943. as the first instruction already adjusts the ZF}
  1944. begin
  1945. if OpsEqual(taicpu(p).oper[0]^,taicpu(p).oper[1]^) then
  1946. if GetLastInstruction(p, hp1) and
  1947. (tai(hp1).typ = ait_instruction) and
  1948. GetNextInstruction(hp1,hp2) and
  1949. (hp2.typ = ait_instruction) and
  1950. ((taicpu(hp2).opcode = A_SETcc) or
  1951. (taicpu(hp2).opcode = A_Jcc)) then
  1952. case taicpu(hp1).opcode Of
  1953. A_ADD, A_SUB, A_OR, A_XOR, A_AND{, A_SHL, A_SHR}:
  1954. begin
  1955. if OpsEqual(taicpu(hp1).oper[1]^,taicpu(p).oper[0]^) and
  1956. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  1957. { and in case of carry for A(E)/B(E)/C/NC }
  1958. ((taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) or
  1959. ((taicpu(hp1).opcode <> A_ADD) and
  1960. (taicpu(hp1).opcode <> A_SUB))) then
  1961. begin
  1962. hp1 := tai(p.next);
  1963. asml.remove(p);
  1964. p.free;
  1965. p := tai(hp1);
  1966. continue
  1967. end;
  1968. end;
  1969. A_DEC, A_INC, A_NEG:
  1970. begin
  1971. if OpsEqual(taicpu(hp1).oper[0]^,taicpu(p).oper[0]^) and
  1972. { does not work in case of overflow for G(E)/L(E)/C_O/C_NO }
  1973. { and in case of carry for A(E)/B(E)/C/NC }
  1974. (taicpu(hp2).condition in [C_Z,C_NZ,C_E,C_NE]) then
  1975. begin
  1976. case taicpu(hp1).opcode Of
  1977. A_DEC, A_INC:
  1978. {replace inc/dec with add/sub 1, because inc/dec doesn't set the carry flag}
  1979. begin
  1980. case taicpu(hp1).opcode Of
  1981. A_DEC: taicpu(hp1).opcode := A_SUB;
  1982. A_INC: taicpu(hp1).opcode := A_ADD;
  1983. end;
  1984. taicpu(hp1).Loadoper(1,taicpu(hp1).oper[0]^);
  1985. taicpu(hp1).LoadConst(0,1);
  1986. taicpu(hp1).ops:=2;
  1987. end
  1988. end;
  1989. hp1 := tai(p.next);
  1990. asml.remove(p);
  1991. p.free;
  1992. p := tai(hp1);
  1993. continue
  1994. end;
  1995. end
  1996. end
  1997. end;
  1998. end;
  1999. end;
  2000. end;
  2001. p := tai(p.next)
  2002. end;
  2003. end;
  2004. end.