popt386.pas 114 KB

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