popt386.pas 107 KB

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