csopt386.pas 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. development team
  4. This unit contains the common subexpression elimination procedure.
  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 CSOpt386;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses aasmbase,aasmtai,aasmcpu, cpuinfo, cpubase, optbase, cgbase;
  22. function CSE(asml: TAAsmoutput; first, last: tai; pass: longint): boolean;
  23. function doReplaceReg(hp: taicpu; newReg, orgReg: tsuperregister): boolean;
  24. function changeOp(var o: toper; newReg, orgReg: tsuperregister): boolean;
  25. function storeBack(start, current: tai; orgReg, newReg: tsuperregister): boolean;
  26. function NoHardCodedRegs(p: taicpu; orgReg, newReg: tsuperregister): boolean;
  27. function RegSizesOK(oldReg,newReg: tsuperregister; p: taicpu): boolean;
  28. implementation
  29. uses
  30. {$ifdef csdebug}
  31. cutils,
  32. {$else}
  33. {$ifdef replaceregdebug}cutils,{$endif}
  34. {$endif}
  35. globtype, verbose, procinfo, globals, daopt386, rgobj, rropt386,cgutils;
  36. {
  37. function TaiInSequence(P: tai; Const Seq: TContent): Boolean;
  38. var P1: tai;
  39. Counter: Byte;
  40. TmpResult: Boolean;
  41. begin
  42. TmpResult := False;
  43. P1 := Seq.StartMod;
  44. Counter := 1;
  45. while not(TmpResult) and
  46. (Counter <= Seq.NrofMods) do
  47. begin
  48. if (P = P1) then TmpResult := True;
  49. inc(Counter);
  50. p1 := tai(p1.Next);
  51. end;
  52. TaiInSequence := TmpResult;
  53. end;
  54. }
  55. function modifiesConflictingMemLocation(p1: tai; supreg: tsuperregister; c: tregContent;
  56. var regsStillValid: tregset; onlymem: boolean; var invalsmemwrite: boolean): boolean;
  57. var
  58. p, hp: taicpu;
  59. tmpRef: treference;
  60. r,regCounter: tsuperregister;
  61. opCount: byte;
  62. dummy: boolean;
  63. begin
  64. modifiesConflictingMemLocation := false;
  65. invalsmemwrite := false;
  66. if p1.typ <> ait_instruction then
  67. exit;
  68. p := taicpu(p1);
  69. case p.opcode of
  70. A_MOV,A_MOVSX,A_MOVZX:
  71. if p.oper[1]^.typ = top_ref then
  72. for regCounter := RS_EAX to RS_EDI do
  73. begin
  74. if p.oper[0]^.typ<>top_reg then
  75. break;
  76. if writeToMemDestroysContents(getsupreg(p.oper[0]^.reg),p.oper[1]^.ref^,
  77. regCounter,topsize2tcgsize[p.opsize],c[regCounter],dummy) then
  78. begin
  79. exclude(regsStillValid,regCounter);
  80. modifiesConflictingMemLocation := not(supreg in regsStillValid);
  81. end;
  82. if (regcounter = supreg) then
  83. invalsmemwrite := invalsmemwrite or dummy;
  84. end
  85. else
  86. { if is_reg_var[getsupreg(p.oper[1]^.reg)] then }
  87. if not onlymem then
  88. for regCounter := RS_EAX to RS_EDI do
  89. begin
  90. if writeDestroysContents(p.oper[1]^,regCounter,topsize2tcgsize[p.opsize],c[regCounter],dummy) then
  91. begin
  92. exclude(regsStillValid,regCounter);
  93. modifiesConflictingMemLocation := not(supreg in regsStillValid);
  94. end
  95. end;
  96. A_DIV, A_IDIV, A_MUL, A_IMUL:
  97. begin
  98. if not onlymem then
  99. if (p.ops = 1) then
  100. begin
  101. for regCounter := RS_EAX to RS_EDI do
  102. begin
  103. if writeToRegDestroysContents(RS_EDX,regCounter,c[regCounter]) then
  104. begin
  105. exclude(regsStillValid,RS_EDX);
  106. modifiesConflictingMemLocation := not(supreg in regsStillValid);
  107. end;
  108. if writeToRegDestroysContents(RS_EAX,regCounter,c[regCounter]) then
  109. begin
  110. exclude(regsStillValid,RS_EAX);
  111. modifiesConflictingMemLocation := not(supreg in regsStillValid);
  112. end;
  113. end
  114. end
  115. else
  116. { only possible for imul }
  117. { last operand is always destination }
  118. for regCounter := RS_EAX to RS_EDI do
  119. begin
  120. if writeDestroysContents(p.oper[p.ops-1]^,regCounter,topsize2tcgsize[p.opsize],c[regCounter],dummy) then
  121. begin
  122. exclude(regsStillValid,regCounter);
  123. modifiesConflictingMemLocation := not(supreg in regsStillValid);
  124. end
  125. end
  126. end;
  127. else
  128. for opCount := 1 to maxinschanges do
  129. case InsProp[p.opcode].Ch[opCount] of
  130. Ch_MOp1,CH_WOp1,CH_RWOp1:
  131. if not(onlymem) or
  132. (p.oper[0]^.typ = top_ref) then
  133. { or ((p.oper[0]^.typ = top_reg) and }
  134. { is_reg_var[getsupreg(p.oper[0]^.reg)]) then }
  135. for regCounter := RS_EAX to RS_EDI do
  136. begin
  137. if writeDestroysContents(p.oper[0]^,regCounter,topsize2tcgsize[p.opsize],c[regCounter],dummy) then
  138. begin
  139. exclude(regsStillValid,regCounter);
  140. modifiesConflictingMemLocation := not(supreg in regsStillValid);
  141. end;
  142. if (regcounter = supreg) then
  143. invalsmemwrite := invalsmemwrite or dummy;
  144. end;
  145. Ch_MOp2,CH_WOp2,CH_RWOp2:
  146. if not(onlymem) or
  147. (p.oper[1]^.typ = top_ref) then
  148. { or ((p.oper[1]^.typ = top_reg) and }
  149. { is_reg_var[getsupreg(p.oper[1]^.reg)]) then }
  150. for regCounter := RS_EAX to RS_EDI do
  151. begin
  152. if writeDestroysContents(p.oper[1]^,regCounter,topsize2tcgsize[p.opsize],c[regCounter],dummy) then
  153. begin
  154. exclude(regsStillValid,regCounter);
  155. modifiesConflictingMemLocation := not(supreg in regsStillValid);
  156. end;
  157. if (regcounter = supreg) then
  158. invalsmemwrite := invalsmemwrite or dummy;
  159. end;
  160. Ch_MOp3,CH_WOp3,CH_RWOp3:
  161. if not(onlymem) or
  162. (p.oper[2]^.typ = top_ref) then
  163. { or ((p.oper[2]^.typ = top_reg) and }
  164. { is_reg_var[getsupreg(p.oper[2]^.reg)]) then }
  165. for regCounter := RS_EAX to RS_EDI do
  166. begin
  167. if writeDestroysContents(p.oper[2]^,regCounter,topsize2tcgsize[p.opsize],c[regCounter],dummy) then
  168. begin
  169. exclude(regsStillValid,regCounter);
  170. modifiesConflictingMemLocation := not(supreg in regsStillValid);
  171. end;
  172. if (regcounter = supreg) then
  173. invalsmemwrite := invalsmemwrite or dummy;
  174. end;
  175. Ch_WMemEDI:
  176. begin
  177. fillchar(tmpref,sizeof(tmpref),0);
  178. tmpRef.base := NR_EDI;
  179. tmpRef.index := NR_EDI;
  180. for regCounter := RS_EAX to RS_EDI do
  181. if writeToMemDestroysContents(RS_INVALID,tmpRef,regCounter,OS_32,c[regCounter],dummy) then
  182. begin
  183. exclude(regsStillValid,regCounter);
  184. modifiesConflictingMemLocation := not(supreg in regsStillValid);
  185. end;
  186. if (regcounter = supreg) then
  187. invalsmemwrite := invalsmemwrite or dummy;
  188. end;
  189. end;
  190. end;
  191. end;
  192. function isSimpleMemLoc(const ref: treference): boolean;
  193. begin
  194. { isSimpleMemLoc :=
  195. (ref.index = RS_NO) and
  196. not(ref.base in (rg.usableregsint+[RS_EDI]));}
  197. isSimpleMemLoc :=
  198. (ref.index = NR_NO) and
  199. ((ref.base = NR_NO) or
  200. not(getsupreg(ref.base) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]));
  201. end;
  202. {checks whether the current instruction sequence (starting with p) and the
  203. one between StartMod and EndMod of Reg are the same. if so, the number of
  204. instructions that match is stored in Found and true is returned, otherwise
  205. Found holds the number of instructions between StartMod and EndMod and false
  206. is returned}
  207. function CheckSequence(p: tai; var prev: tai; supreg: tsuperregister; var Found: Longint;
  208. var reginfo: toptreginfo; findPrevSeqs: boolean): Boolean;
  209. var
  210. regsNotRead, regsStillValid : tregset;
  211. checkingPrevSequences,
  212. passedFlagsModifyingInstr,
  213. invalsmemwrite : boolean;
  214. function getPrevSequence(p: tai; supreg: tsuperregister; currentPrev: tai; var newPrev: tai): tsuperregister;
  215. const
  216. current_reg: tsuperregister = RS_INVALID;
  217. function stillValid(p: tai): boolean;
  218. var
  219. hp: tai;
  220. begin
  221. { only regvars are still used at jump instructions }
  222. if (cs_regvars in aktglobalswitches) and
  223. (p.typ = ait_instruction) and
  224. taicpu(p).is_jmp then
  225. regsstillvalid := regsstillvalid - ptaiprop(p.optinfo)^.usedregs;
  226. stillValid :=
  227. (p.typ = ait_instruction) and
  228. (taicpu(p).opcode <> a_jmp) and
  229. (ptaiprop(p.optinfo)^.regs[supreg].wstate =
  230. ptaiprop(currentPrev.optinfo)^.regs[supreg].wstate) and
  231. { in case destroyreg is called with doIncState = false }
  232. (ptaiprop(p.optinfo)^.regs[supreg].typ =
  233. ptaiprop(currentPrev.optinfo)^.regs[supreg].typ) and
  234. (supreg in (regsNotRead * regsStillValid));
  235. { stop if the register was still used right before a (conditional) }
  236. { jump, since in that case its current contents could still be }
  237. { used in the other path of the jump) }
  238. if (p.typ = ait_instruction) and
  239. (taicpu(p).is_jmp) and
  240. getlastinstruction(p,hp) then
  241. stillValid := stillValid and
  242. not(supreg in ptaiprop(hp.optinfo)^.usedregs);
  243. passedFlagsModifyingInstr := passedFlagsModifyingInstr or
  244. instrWritesFlags(currentPrev);
  245. end;
  246. function findChangedRegister(p: tai): tsuperregister;
  247. var
  248. regCounter, loopstart: tsuperregister;
  249. begin
  250. if (current_reg <> RS_INVALID) then
  251. loopstart := succ(current_reg)
  252. else
  253. loopstart := RS_EAX;
  254. for regCounter := loopstart to RS_EDI do
  255. with ptaiprop(p.optinfo)^.regs[regCounter] do
  256. if ((startmod <>
  257. ptaiprop(currentPrev.optinfo)^.regs[regCounter].startmod) or
  258. (nrofMods <>
  259. ptaiprop(currentPrev.optinfo)^.regs[regCounter].nrofMods)) and
  260. (ptaiprop(p.optinfo)^.regs[regCounter].typ in [con_ref,con_noRemoveRef]) then
  261. begin
  262. findChangedRegister := regCounter;
  263. current_reg := regCounter;
  264. exit;
  265. end;
  266. current_reg := RS_INVALID;
  267. findChangedRegister := RS_INVALID;
  268. end;
  269. var
  270. hp, prevFound: tai;
  271. tmpResult, regCounter: tsuperregister;
  272. invalsmemwrite: boolean;
  273. begin
  274. if (current_reg <> RS_EDI) and
  275. (current_reg <> RS_INVALID) then
  276. begin
  277. tmpResult := findChangedRegister(currentPrev);
  278. if tmpResult <> RS_INVALID then
  279. begin
  280. getPrevSequence := tmpResult;
  281. exit;
  282. end;
  283. end;
  284. getPrevSequence := RS_INVALID;
  285. passedFlagsModifyingInstr := passedFlagsModifyingInstr or
  286. instrWritesFlags(currentPrev);
  287. if (cs_regvars in aktglobalswitches) and
  288. (currentprev.typ = ait_instruction) and
  289. taicpu(currentprev).is_jmp then
  290. regsstillvalid := regsstillvalid - ptaiprop(currentprev.optinfo)^.usedregs;
  291. if not getLastInstruction(currentPrev,hp) then
  292. exit;
  293. prevFound := currentPrev;
  294. tmpResult := RS_INVALID;
  295. while (tmpResult = RS_INVALID) and
  296. stillValid(hp) and
  297. (ptaiprop(prevFound.optinfo)^.canBeRemoved or
  298. not(modifiesConflictingMemLocation(prevFound,supreg,
  299. ptaiprop(p.optinfo)^.regs,regsStillValid,false, invalsmemwrite))) do
  300. begin
  301. { only update the regsread for the instructions we already passed }
  302. if not(ptaiprop(prevFound.optinfo)^.canBeRemoved) then
  303. for regCounter := RS_EAX to RS_EDI do
  304. if regReadByInstruction(regCounter,prevFound) then
  305. exclude(regsNotRead,regCounter);
  306. { in case getPreviousInstruction fails and sets hp to nil in the }
  307. { next iteration }
  308. prevFound := hp;
  309. if not(ptaiprop(hp.optinfo)^.canBeRemoved) then
  310. tmpResult := findChangedRegister(hp);
  311. if not getLastInstruction(hp,hp) then
  312. break;
  313. end;
  314. getPrevSequence := tmpResult;
  315. if tmpResult <> RS_INVALID then
  316. newPrev := prevFound;
  317. end;
  318. function getNextRegToTest(var prev: tai; currentReg: tsuperregister): tsuperregister;
  319. begin
  320. if not checkingPrevSequences then
  321. begin
  322. if (currentreg = RS_INVALID) then
  323. currentreg := RS_EAX
  324. else
  325. inc(currentreg);
  326. while (currentReg <= RS_EDI) and
  327. not(ptaiprop(prev.optinfo)^.regs[currentReg].typ in [con_ref,con_noRemoveRef]) do
  328. inc(currentReg);
  329. if currentReg > RS_EDI then
  330. begin
  331. if (taicpu(p).oper[0]^.typ <> top_ref) or
  332. isSimpleMemLoc(taicpu(p).oper[0]^.ref^) then
  333. begin
  334. checkingPrevSequences := true;
  335. end
  336. else
  337. getNextRegToTest := RS_INVALID;
  338. end
  339. else
  340. getNextRegToTest := currentReg;
  341. end;
  342. if checkingPrevSequences then
  343. if findPrevSeqs then
  344. getNextRegToTest :=
  345. getPrevSequence(p,supreg,prev,prev)
  346. else
  347. getNextRegToTest := RS_INVALID;
  348. end;
  349. function changedreginvalidatedbetween(const oldreginfo: toptreginfo; var newreginfo: toptreginfo; startp,endp,current: tai): boolean;
  350. var
  351. orgdiffregs,diffregs: tregset;
  352. runner: tai;
  353. invalsmemwrite: boolean;
  354. begin
  355. diffregs := newreginfo.newregsencountered - oldreginfo.newregsencountered;
  356. orgdiffregs := diffregs;
  357. if diffregs <> [] then
  358. begin
  359. runner := startp;
  360. repeat
  361. modifiesConflictingMemLocation(runner,RS_EAX { dummy },ptaiprop(current.optinfo)^.regs,diffregs,true,invalsmemwrite);
  362. if orgdiffregs <> diffregs then
  363. begin
  364. changedreginvalidatedbetween := true;
  365. newreginfo := oldreginfo;
  366. exit;
  367. end;
  368. getnextinstruction(runner,runner);
  369. until (runner = endp);
  370. end;
  371. changedreginvalidatedbetween := false;
  372. end;
  373. var
  374. prevreginfo: toptreginfo;
  375. hp2, hp3{, EndMod},highPrev, orgPrev: tai;
  376. {Cnt,} OldNrofMods: Longint;
  377. startRegInfo, OrgRegInfo, HighRegInfo: toptreginfo;
  378. regModified, lastregloadremoved: array[RS_EAX..RS_ESP] of boolean;
  379. HighFound, OrgRegFound: longint;
  380. regcounter, regCounter2, tmpreg, base, index: tsuperregister;
  381. OrgRegResult: Boolean;
  382. TmpResult, flagResultsNeeded, stopchecking: Boolean;
  383. begin {CheckSequence}
  384. TmpResult := False;
  385. FillChar(OrgRegInfo, Sizeof(OrgRegInfo), 0);
  386. FillChar(startRegInfo, sizeof(startRegInfo), 0);
  387. OrgRegFound := 0;
  388. HighFound := 0;
  389. OrgRegResult := False;
  390. with startRegInfo do
  391. begin
  392. newRegsEncountered := [RS_EBP, RS_ESP];
  393. fillword(new2oldreg,sizeof(new2oldreg),RS_INVALID);
  394. new2OldReg[RS_EBP] := RS_EBP;
  395. new2OldReg[RS_ESP] := RS_ESP;
  396. oldRegsEncountered := newRegsEncountered;
  397. end;
  398. checkingPrevSequences := false;
  399. passedFlagsModifyingInstr := false;
  400. flagResultsNeeded := false;
  401. regsNotRead := [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESP,RS_EBP,RS_EDI,RS_ESI];
  402. regsStillValid := regsNotRead;
  403. GetLastInstruction(p, prev);
  404. tmpreg:=RS_INVALID;
  405. regCounter := getNextRegToTest(prev,tmpreg);
  406. while (regcounter <> RS_INVALID) do
  407. begin
  408. fillchar(regModified,sizeof(regModified),0);
  409. fillchar(lastregloadremoved,sizeof(lastregloadremoved),0);
  410. reginfo := startRegInfo;
  411. Found := 0;
  412. hp2 := ptaiprop(prev.optinfo)^.Regs[regcounter].StartMod;
  413. if (prev <> ptaiprop(prev.optinfo)^.Regs[regcounter].StartMod) then
  414. OldNrofMods := ptaiprop(prev.optinfo)^.Regs[regcounter].NrofMods
  415. else
  416. OldNrofMods := 1;
  417. hp3 := p;
  418. if checkingprevsequences then
  419. prevreginfo := reginfo;
  420. stopchecking := false;
  421. while (Found <> OldNrofMods) and
  422. { old new }
  423. InstructionsEquivalent(hp2, hp3, reginfo) and
  424. (not(checkingprevsequences) or
  425. not(changedreginvalidatedbetween(prevreginfo,reginfo,prev,p,hp3))) do
  426. begin
  427. if checkingprevsequences then
  428. begin
  429. prevreginfo := reginfo;
  430. end;
  431. if (hp3.typ = ait_instruction) and
  432. ((taicpu(hp3).opcode = A_MOV) or
  433. (taicpu(hp3).opcode = A_MOVZX) or
  434. (taicpu(hp3).opcode = A_LEA) or
  435. (taicpu(hp3).opcode = A_MOVSX)) and
  436. (taicpu(hp3).oper[1]^.typ = top_reg) and
  437. not(regInOp(getsupreg(taicpu(hp3).oper[1]^.reg),taicpu(hp3).oper[0]^)) then
  438. begin
  439. tmpreg := getsupreg(taicpu(hp3).oper[1]^.reg);
  440. lastregloadremoved[tmpreg] := ptaiprop(hp2.optinfo)^.canberemoved;
  441. reginfo.lastReload[tmpreg] := hp3;
  442. case taicpu(hp3).oper[0]^.typ of
  443. top_ref:
  444. begin
  445. base := getsupreg(taicpu(hp3).oper[0]^.ref^.base);
  446. index := getsupreg(taicpu(hp3).oper[0]^.ref^.index);
  447. if (found <> 0) and
  448. ((taicpu(hp3).oper[0]^.ref^.base = NR_NO) or
  449. regModified[base] or
  450. (base = getsupreg(current_procinfo.framepointer))) and
  451. ((taicpu(hp3).oper[0]^.ref^.index = NR_NO) or
  452. regModified[index]) and
  453. not(regInRef(tmpReg,taicpu(hp3).oper[0]^.ref^)) then
  454. begin
  455. with ptaiprop(hp3.optinfo)^.regs[tmpreg] do
  456. if nrofMods > (oldNrofMods - found) then
  457. oldNrofMods := found + nrofMods;
  458. { next is safe because instructions are equivalent }
  459. with ptaiprop(hp2.optinfo)^.regs[getsupreg(taicpu(hp2).oper[1]^.reg)] do
  460. if nrofMods > (oldNrofMods - found) then
  461. oldNrofMods := found + nrofMods;
  462. end;
  463. end;
  464. top_reg:
  465. if regModified[getsupreg(taicpu(hp3).oper[0]^.reg)] then
  466. begin
  467. with ptaiprop(hp3.optinfo)^.regs[tmpreg] do
  468. if nrofMods > (oldNrofMods - found) then
  469. oldNrofMods := found + nrofMods;
  470. with ptaiprop(hp2.optinfo)^.regs[getsupreg(taicpu(hp2).oper[1]^.reg)] do
  471. if nrofMods > (oldNrofMods - found) then
  472. oldNrofMods := found + nrofMods;
  473. end;
  474. end;
  475. end;
  476. for regCounter2 := RS_EAX to RS_EDI do
  477. regModified[regCounter2] := regModified[regCounter2] or
  478. regModifiedByInstruction(regCounter2,hp3);
  479. if flagResultsNeeded then
  480. flagResultsNeeded := not instrReadsFlags(hp3);
  481. if not flagResultsNeeded then
  482. flagResultsNeeded := ptaiprop(hp3.optinfo)^.FlagsUsed;
  483. GetNextInstruction(hp2, hp2);
  484. GetNextInstruction(hp3, hp3);
  485. inc(Found);
  486. end;
  487. for regCounter2 := RS_EAX to RS_EDI do
  488. if (reginfo.new2OldReg[regCounter2] <> RS_INVALID) and
  489. (regCounter2 in ptaiprop(hp3.optinfo)^.usedRegs) and
  490. not regLoadedWithNewValue(regCounter2,false,hp3) and
  491. lastregloadremoved[regcounter2] then
  492. found := 0;
  493. if checkingPrevSequences then
  494. begin
  495. for regCounter2 := RS_EAX to RS_EDI do
  496. if (reginfo.new2OldReg[regCounter2] <> RS_INVALID) and
  497. (reginfo.new2OldReg[regCounter2] <> regCounter2) and
  498. (not(regCounter2 in (regsNotRead * regsStillValid)) or
  499. not(reginfo.new2OldReg[regCounter2] in regsStillValid)) then
  500. begin
  501. found := 0;
  502. break;
  503. end;
  504. if passedFlagsModifyingInstr and flagResultsNeeded then
  505. found := 0;
  506. end;
  507. TmpResult := true;
  508. if (found <> OldNrofMods) then
  509. TmpResult := false
  510. else if assigned(hp3) then
  511. for regcounter2 := RS_EAX to RS_EDI do
  512. if (regcounter2 in reginfo.regsLoadedforRef) and
  513. regModified[regcounter2] and
  514. (regcounter2 in ptaiprop(hp3.optinfo)^.usedRegs) and
  515. not regLoadedWithNewValue(regcounter2,false,hp3) then
  516. begin
  517. TmpResult := False;
  518. if (found > 0) then
  519. {this is correct because we only need to turn off the CanBeRemoved flag
  520. when an instruction has already been processed by CheckSequence
  521. (otherwise CanBeRemoved can't be true and thus can't have to be turned off).
  522. if it has already been processed by CheckSequence and flagged to be
  523. removed, it means that it has been checked against a previous sequence
  524. and that it was equal (otherwise CheckSequence would have returned false
  525. and the instruction wouldn't have been removed). if this "if found > 0"
  526. check is left out, incorrect optimizations are performed.}
  527. Found := ptaiprop(tai(p).optinfo)^.Regs[supreg].NrofMods;
  528. break;
  529. end;
  530. if TmpResult and
  531. (Found > HighFound) then
  532. begin
  533. highPrev := prev;
  534. HighFound := Found;
  535. HighRegInfo := reginfo;
  536. end;
  537. if (regcounter = supreg) then
  538. begin
  539. orgPrev := prev;
  540. OrgRegFound := Found;
  541. OrgRegResult := TmpResult;
  542. OrgRegInfo := reginfo
  543. end;
  544. regCounter := getNextRegToTest(prev,regCounter);
  545. end;
  546. if (HighFound > 0) and
  547. (not(OrgRegResult) Or
  548. (HighFound > OrgRegFound))
  549. then
  550. begin
  551. {$ifndef fpc}
  552. TmpResult := True;
  553. {$else fpc}
  554. CheckSequence := True;
  555. {$endif fpc}
  556. prev := highPrev;
  557. reginfo := HighRegInfo;
  558. Found := HighFound
  559. end
  560. else
  561. begin
  562. {$ifndef fpc}
  563. TmpResult := OrgRegResult;
  564. {$else fpc}
  565. CheckSequence := OrgRegResult;
  566. {$endif fpc}
  567. prev := orgPrev;
  568. Found := OrgRegFound;
  569. reginfo := OrgRegInfo;
  570. end;
  571. {$ifndef fpc}
  572. CheckSequence := TmpResult;
  573. {$endif fpc}
  574. end; {CheckSequence}
  575. procedure SetAlignReg(p: tai);
  576. Const alignSearch = 12;
  577. var regsUsable: TRegSet;
  578. prevInstrCount, nextInstrCount: Longint;
  579. prevState, nextWState,nextRState: Array[RS_EAX..RS_EDI] of byte;
  580. regCounter, lastRemoved: tsuperregister;
  581. prev, next: tai;
  582. {$ifdef alignregdebug}
  583. temp: tai;
  584. {$endif alignregdebug}
  585. begin
  586. regsUsable := [RS_EAX,RS_ECX,RS_EDX,RS_EBX,{R_ESP,RS_EBP,}RS_ESI,RS_EDI];
  587. for regCounter := RS_EAX to RS_EDI do
  588. begin
  589. prevState[regCounter] := ptaiprop(p.optinfo)^.Regs[regCounter].wState;
  590. nextWState[regCounter] := ptaiprop(p.optinfo)^.Regs[regCounter].wState;
  591. nextRState[regCounter] := ptaiprop(p.optinfo)^.Regs[regCounter].rState;
  592. end;
  593. getLastInstruction(p,prev);
  594. getNextInstruction(p,next);
  595. lastRemoved := getsupreg(tai_align(p).reg);
  596. nextInstrCount := 0;
  597. prevInstrCount := 0;
  598. while ((assigned(prev) and
  599. assigned(prev.optinfo) and
  600. (prevInstrCount < alignSearch)) or
  601. (assigned(next) and
  602. assigned(next.optinfo) and
  603. (nextInstrCount < alignSearch))) and
  604. (regsUsable <> []) do
  605. begin
  606. {$ifdef alignregdebug}
  607. if assigned(prev) then
  608. begin
  609. temp := tai_comment.Create(strpnew('got here'));
  610. temp.next := prev.next;
  611. temp.previous := prev;
  612. prev.next := temp;
  613. if assigned(temp.next) then
  614. temp.next.previous := temp;
  615. end;
  616. {$endif alignregdebug}
  617. if assigned(prev) and assigned(prev.optinfo) and
  618. (prevInstrCount < alignSearch) then
  619. begin
  620. if (prev.typ = ait_instruction) and
  621. (insProp[taicpu(prev).opcode].ch[1] <> Ch_ALL) and
  622. (taicpu(prev).opcode <> A_JMP) then
  623. begin
  624. inc(prevInstrCount);
  625. for regCounter := RS_EAX to RS_EDI do
  626. begin
  627. if (regCounter in regsUsable) and
  628. (ptaiprop(prev.optinfo)^.Regs[regCounter].wState <>
  629. prevState[regCounter]) then
  630. begin
  631. lastRemoved := regCounter;
  632. exclude(regsUsable,regCounter);
  633. {$ifdef alignregdebug}
  634. temp := tai_comment.Create(strpnew(
  635. std_regname(newreg(R_INTREGISTER,regCounter,R_SUBWHOLE))+' removed')));
  636. temp.next := prev.next;
  637. temp.previous := prev;
  638. prev.next := temp;
  639. if assigned(temp.next) then
  640. temp.next.previous := temp;
  641. if regsUsable = [] then
  642. begin
  643. temp := tai_comment.Create(strpnew(
  644. 'regsUsable empty here')));
  645. temp.next := prev.next;
  646. temp.previous := prev;
  647. prev.next := temp;
  648. if assigned(temp.next) then
  649. temp.next.previous := temp;
  650. end;
  651. {$endif alignregdebug}
  652. end;
  653. prevState[regCounter] :=
  654. ptaiprop(prev.optinfo)^.Regs[regCounter].wState;
  655. end;
  656. getLastInstruction(prev,prev);
  657. end
  658. else
  659. if GetLastInstruction(prev,prev) and
  660. assigned(prev.optinfo) then
  661. for regCounter := RS_EAX to RS_EDI do
  662. prevState[regCounter] :=
  663. ptaiprop(prev.optinfo)^.Regs[regCounter].wState
  664. end;
  665. if assigned(next) and assigned(next.optinfo) and
  666. (nextInstrCount < alignSearch) then
  667. begin
  668. if (next.typ = ait_instruction) and
  669. (insProp[taicpu(next).opcode].ch[1] <> Ch_ALL) and
  670. (taicpu(next).opcode <> A_JMP) then
  671. begin
  672. inc(nextInstrCount);
  673. for regCounter := RS_EAX to RS_EDI do
  674. begin
  675. if (regCounter in regsUsable) and
  676. ((ptaiprop(next.optinfo)^.Regs[regCounter].wState <>
  677. nextWState[regCounter]) or
  678. (ptaiprop(next.optinfo)^.Regs[regCounter].rState <>
  679. nextRState[regCounter])) then
  680. begin
  681. lastRemoved := regCounter;
  682. exclude(regsUsable,regCounter);
  683. {$ifdef alignregdebug}
  684. temp := tai_comment.Create(strpnew(
  685. std_regname(newreg(R_INTREGISTER,regCounter,R_SUBWHOLE))+' removed')));
  686. temp.next := next.next;
  687. temp.previous := next;
  688. next.next := temp;
  689. if assigned(temp.next) then
  690. temp.next.previous := temp;
  691. if regsUsable = [] then
  692. begin
  693. temp := tai_comment.Create(strpnew(
  694. 'regsUsable empty here')));
  695. temp.next := next.next;
  696. temp.previous := next;
  697. next.next := temp;
  698. if assigned(temp.next) then
  699. temp.next.previous := temp;
  700. end;
  701. {$endif alignregdebug}
  702. end;
  703. nextWState[regCounter] :=
  704. ptaiprop(next.optinfo)^.Regs[regCounter].wState;
  705. nextRState[regCounter] :=
  706. ptaiprop(next.optinfo)^.Regs[regCounter].rState;
  707. end
  708. end
  709. else
  710. for regCounter := RS_EAX to RS_EDI do
  711. begin
  712. nextWState[regCounter] :=
  713. ptaiprop(next.optinfo)^.Regs[regCounter].wState;
  714. nextRState[regCounter] :=
  715. ptaiprop(next.optinfo)^.Regs[regCounter].rState;
  716. end;
  717. getNextInstruction(next,next);
  718. end;
  719. end;
  720. if regsUsable <> [] then
  721. for regCounter := RS_EAX to RS_EDI do
  722. if regCounter in regsUsable then
  723. begin
  724. lastRemoved := regCounter;
  725. break
  726. end;
  727. {$ifdef alignregdebug}
  728. next := tai_comment.Create(strpnew(std_regname(newreg(R_INTREGISTER,lastremoved,R_SUBWHOLE))+
  729. ' chosen as alignment register')));
  730. next.next := p.next;
  731. next.previous := p;
  732. p.next := next;
  733. if assigned(next.next) then
  734. next.next.previous := next;
  735. {$endif alignregdebug}
  736. tai_align(p).reg := newreg(R_INTREGISTER,lastRemoved,R_SUBWHOLE);
  737. end;
  738. procedure clearmemwrites(p: tai; supreg: tsuperregister);
  739. var
  740. beginmemwrite: tai;
  741. begin
  742. beginmemwrite := ptaiprop(p.optinfo)^.regs[supreg].memwrite;
  743. repeat
  744. ptaiprop(p.optinfo)^.regs[supreg].memwrite := nil;
  745. until not getnextinstruction(p,p) or
  746. (ptaiprop(p.optinfo)^.regs[supreg].memwrite <> beginmemwrite);
  747. end;
  748. procedure ClearRegContentsFrom(supreg: tsuperregister; p, endP: tai);
  749. { first clears the contents of reg from p till endP. then the contents are }
  750. { cleared until the first instruction that changes reg }
  751. var
  752. {$ifdef replaceregdebug}
  753. hp: tai;
  754. l: longint;
  755. {$endif replaceregdebug}
  756. regcounter: tsuperregister;
  757. oldStartmod: tai;
  758. begin
  759. {$ifdef replaceregdebug}
  760. l := random(1000);
  761. hp := tai_comment.Create(strpnew(
  762. 'cleared '+std_regname(newreg(R_INTREGISTER,supreg,R_SUBWHOLE))+' from here... '+tostr(l)));
  763. hp.next := p;
  764. hp.previous := p.previous;
  765. p.previous := hp;
  766. if assigned(hp.previous) then
  767. hp.previous.next := hp;
  768. {$endif replaceregdebug}
  769. ptaiprop(p.optinfo)^.Regs[supreg].typ := con_unknown;
  770. while (p <> endP) do
  771. begin
  772. for regcounter := RS_EAX to RS_EDI do
  773. if (regcounter <> supreg) and
  774. assigned(ptaiprop(p.optinfo)^.regs[supreg].memwrite) and
  775. reginref(regcounter,ptaiprop(p.optinfo)^.regs[supreg].memwrite.oper[1]^.ref^) then
  776. clearmemwrites(p,regcounter);
  777. with ptaiprop(p.optinfo)^.Regs[supreg] do
  778. begin
  779. typ := con_unknown;
  780. memwrite := nil;
  781. end;
  782. getNextInstruction(p,p);
  783. end;
  784. oldStartmod := ptaiprop(p.optinfo)^.Regs[supreg].startmod;
  785. repeat
  786. with ptaiprop(p.optinfo)^.Regs[supreg] do
  787. begin
  788. typ := con_unknown;
  789. memwrite := nil;
  790. end;
  791. until not getNextInstruction(p,p) or
  792. (ptaiprop(p.optinfo)^.Regs[supreg].startmod <> oldStartmod);
  793. {$ifdef replaceregdebug}
  794. if assigned(p) then
  795. begin
  796. hp := tai_comment.Create(strpnew(
  797. 'cleared '+std_regname(newreg(R_INTREGISTER,supreg,R_SUBWHOLE))+' till here... '+tostr(l)));
  798. hp.next := p;
  799. hp.previous := p.previous;
  800. p.previous := hp;
  801. if assigned(hp.previous) then
  802. hp.previous.next := hp;
  803. end;
  804. {$endif replaceregdebug}
  805. end;
  806. procedure RestoreRegContentsTo(supreg: tsuperregister; const c: TContent; p, endP: tai);
  807. var
  808. {$ifdef replaceregdebug}
  809. l: longint;
  810. {$endif replaceregdebug}
  811. hp: tai;
  812. dummyregs: tregset;
  813. tmpState, newrstate: byte;
  814. prevcontenttyp: byte;
  815. memconflict: boolean;
  816. invalsmemwrite: boolean;
  817. begin
  818. {$ifdef replaceregdebug}
  819. l := random(1000);
  820. hp := tai_comment.Create(strpnew(
  821. 'restored '+std_regname(newreg(R_INTREGISTER,supreg,R_SUBWHOLE))+' with data from here... '+tostr(l)));
  822. hp.next := p;
  823. hp.previous := p.previous;
  824. p.previous := hp;
  825. if assigned(hp.previous) then
  826. hp.previous.next := hp;
  827. {$endif replaceregdebug}
  828. { ptaiprop(p.optinfo)^.Regs[reg] := c;}
  829. newrstate := c.rstate;
  830. incstate(newrstate,$7f);
  831. while (p <> endP) do
  832. begin
  833. if not(ptaiprop(p.optinfo)^.canberemoved) and
  834. regreadbyinstruction(supreg,p) then
  835. incstate(newrstate,1);
  836. ptaiprop(p.optinfo)^.Regs[supreg] := c;
  837. ptaiprop(p.optinfo)^.Regs[supreg].rstate := newrstate;
  838. getNextInstruction(p,p);
  839. end;
  840. tmpState := ptaiprop(p.optinfo)^.Regs[supreg].wState;
  841. if (newrstate = ptaiprop(p.optinfo)^.Regs[supreg].rState) then
  842. begin
  843. incstate(ptaiprop(p.optinfo)^.regs[supreg].rstate,63);
  844. if getnextinstruction(p,hp) and
  845. (ptaiprop(hp.optinfo)^.regs[supreg].rstate = ptaiprop(p.optinfo)^.regs[supreg].rstate) then
  846. internalerror(2004122710);
  847. end;
  848. dummyregs := [supreg];
  849. repeat
  850. newrstate := ptaiprop(p.optinfo)^.Regs[supreg].rState;
  851. prevcontenttyp := ptaiprop(p.optinfo)^.Regs[supreg].typ;
  852. // is this a write to memory that destroys the contents we are restoring?
  853. memconflict := modifiesConflictingMemLocation(p,supreg,ptaiprop(p.optinfo)^.regs,dummyregs,true,invalsmemwrite);
  854. if not memconflict and not invalsmemwrite then
  855. begin
  856. ptaiprop(p.optinfo)^.Regs[supreg] := c;
  857. ptaiprop(p.optinfo)^.Regs[supreg].rstate := newrstate;
  858. end;
  859. until invalsmemwrite or
  860. memconflict or
  861. not getNextInstruction(p,p) or
  862. (ptaiprop(p.optinfo)^.Regs[supreg].wState <> tmpState) or
  863. (p.typ = ait_label) or
  864. ((prevcontenttyp <> con_invalid) and
  865. (ptaiprop(p.optinfo)^.Regs[supreg].typ = con_invalid));
  866. if assigned(p) and
  867. (p.typ <> ait_marker) then
  868. if ((p.typ = ait_label) or
  869. memconflict or
  870. invalsmemwrite) then
  871. clearRegContentsFrom(supreg,p,p)
  872. else if (ptaiprop(p.optinfo)^.Regs[supreg].rstate = newrstate) then
  873. incstate(ptaiprop(p.optinfo)^.Regs[supreg].rstate,20);
  874. {$ifdef replaceregdebug}
  875. if assigned(p) then
  876. begin
  877. hp := tai_comment.Create(strpnew(
  878. 'restored '+std_regname(newreg(R_INTREGISTER,supreg,R_SUBWHOLE))+' till here... '+tostr(l)));
  879. hp.next := p;
  880. hp.previous := p.previous;
  881. p.previous := hp;
  882. if assigned(hp.previous) then
  883. hp.previous.next := hp;
  884. end;
  885. {$endif replaceregdebug}
  886. end;
  887. function NoHardCodedRegs(p: taicpu; orgReg, newReg: tsuperregister): boolean;
  888. var
  889. chCount: byte;
  890. begin
  891. case p.opcode of
  892. A_IMUL: noHardCodedRegs := p.ops <> 1;
  893. A_SHL,A_SHR,A_SHLD,A_SHRD: noHardCodedRegs :=
  894. (p.oper[0]^.typ <> top_reg) or
  895. ((orgReg <> RS_ECX) and (newReg <> RS_ECX));
  896. else
  897. begin
  898. NoHardCodedRegs := true;
  899. with InsProp[p.opcode] do
  900. for chCount := 1 to maxinschanges do
  901. if Ch[chCount] in ([Ch_REAX..Ch_MEDI,Ch_WMemEDI,Ch_All]-[Ch_RESP,Ch_WESP,Ch_RWESP]) then
  902. begin
  903. NoHardCodedRegs := false;
  904. break
  905. end;
  906. end;
  907. end;
  908. end;
  909. function ChangeReg(var Reg: TRegister; newReg, orgReg: tsuperregister): boolean;
  910. begin
  911. changereg := false;
  912. if (reg <> NR_NO) and
  913. (getregtype(reg) = R_INTREGISTER) and
  914. (getsupreg(reg) = newreg) then
  915. begin
  916. changereg := true;
  917. setsupreg(reg,orgreg);
  918. end;
  919. end;
  920. function changeOp(var o: toper; newReg, orgReg: tsuperregister): boolean;
  921. var
  922. tmpresult: boolean;
  923. begin
  924. changeOp := false;
  925. case o.typ of
  926. top_reg: changeOp := changeReg(o.reg,newReg,orgReg);
  927. top_ref:
  928. begin
  929. tmpresult := changeReg(o.ref^.base,newReg,orgReg);
  930. changeop := changeReg(o.ref^.index,newReg,orgReg) or tmpresult;
  931. end;
  932. end;
  933. end;
  934. procedure updateStates(orgReg,newReg: tsuperregister; hp: tai; writeStateToo: boolean);
  935. var
  936. prev: tai;
  937. newOrgRegRState, newOrgRegWState: byte;
  938. begin
  939. if getLastInstruction(hp,prev) then
  940. with ptaiprop(prev.optinfo)^ do
  941. begin
  942. {$ifopt r+}
  943. {$define rangeon}
  944. {$r-}
  945. {$endif}
  946. newOrgRegRState := regs[orgReg].rState +
  947. ptaiprop(hp.optinfo)^.regs[newReg].rState - regs[newReg].rstate;
  948. if writeStateToo then
  949. newOrgRegWState := regs[orgReg].wState +
  950. ptaiprop(hp.optinfo)^.regs[newReg].wState - regs[newReg].wstate;
  951. {$ifdef rangeon}
  952. {$undef rangeon}
  953. {$r+}
  954. {$endif}
  955. end
  956. else
  957. with ptaiprop(hp.optinfo)^.regs[newReg] do
  958. begin
  959. newOrgRegRState := rState;
  960. if writeStateToo then
  961. newOrgRegWState := wState;
  962. end;
  963. with ptaiprop(hp.optinfo)^.regs[orgReg] do
  964. begin
  965. rState := newOrgRegRState;
  966. if writeStateToo then
  967. wState := newOrgRegwState;
  968. end;
  969. end;
  970. function doReplaceReg(hp: taicpu; newReg, orgReg: tsuperregister): boolean;
  971. var
  972. opCount: longint;
  973. tmpResult: boolean;
  974. begin
  975. tmpresult := false;
  976. for opCount := 0 to hp.ops-1 do
  977. tmpResult :=
  978. changeOp(hp.oper[opCount]^,newReg,orgReg) or tmpResult;
  979. doReplaceReg := tmpResult;
  980. end;
  981. function RegSizesOK(oldReg,newReg: tsuperregister; p: taicpu): boolean;
  982. { oldreg and newreg must be 32bit components }
  983. var
  984. opCount: longint;
  985. tmpreg: tsuperregister;
  986. begin
  987. RegSizesOK := true;
  988. { if only one of them is a general purpose register ... }
  989. if (IsGP32reg(oldReg) xor IsGP32Reg(newReg)) then
  990. begin
  991. for opCount := 0 to p.ops-1 do
  992. if (p.oper[opCount]^.typ = top_reg) and
  993. (getsubreg(p.oper[opCount]^.reg) in [R_SUBL,R_SUBH]) then
  994. begin
  995. tmpreg := getsupreg(p.oper[opCount]^.reg);
  996. if (tmpreg = oldreg) or
  997. (tmpreg = newreg) then
  998. begin
  999. RegSizesOK := false;
  1000. break
  1001. end
  1002. end;
  1003. end;
  1004. end;
  1005. function doReplaceReadReg(p: taicpu; newReg,orgReg: tsuperregister): boolean;
  1006. var
  1007. opCount: byte;
  1008. begin
  1009. doReplaceReadReg := false;
  1010. { handle special case }
  1011. case p.opcode of
  1012. A_IMUL:
  1013. begin
  1014. case p.ops of
  1015. 1: internalerror(1301001);
  1016. 2,3:
  1017. begin
  1018. if changeOp(p.oper[0]^,newReg,orgReg) then
  1019. begin
  1020. { updateStates(orgReg,newReg,p,false);}
  1021. doReplaceReadReg := true;
  1022. end;
  1023. if p.ops = 3 then
  1024. if changeOp(p.oper[1]^,newReg,orgReg) then
  1025. begin
  1026. { updateStates(orgReg,newReg,p,false);}
  1027. doReplaceReadReg := true;
  1028. end;
  1029. end;
  1030. end;
  1031. end;
  1032. A_DIV,A_IDIV,A_MUL: internalerror(1301002);
  1033. else
  1034. begin
  1035. for opCount := 0 to p.ops-1 do
  1036. if p.oper[opCount]^.typ = top_ref then
  1037. if changeOp(p.oper[opCount]^,newReg,orgReg) then
  1038. begin
  1039. { updateStates(orgReg,newReg,p,false);}
  1040. doReplaceReadReg := true;
  1041. end;
  1042. for opCount := 1 to maxinschanges do
  1043. case InsProp[p.opcode].Ch[opCount] of
  1044. Ch_ROp1:
  1045. if p.oper[0]^.typ = top_reg then
  1046. if changeReg(p.oper[0]^.reg,newReg,orgReg) then
  1047. begin
  1048. { updateStates(orgReg,newReg,p,false);}
  1049. doReplaceReadReg := true;
  1050. end;
  1051. Ch_ROp2:
  1052. if p.oper[1]^.typ = top_reg then
  1053. if changeReg(p.oper[1]^.reg,newReg,orgReg) then
  1054. begin
  1055. { updateStates(orgReg,newReg,p,false);}
  1056. doReplaceReadReg := true;
  1057. end;
  1058. Ch_ROp3:
  1059. if p.oper[2]^.typ = top_reg then
  1060. if changeReg(p.oper[2]^.reg,newReg,orgReg) then
  1061. begin
  1062. { updateStates(orgReg,newReg,p,false);}
  1063. doReplaceReadReg := true;
  1064. end;
  1065. end;
  1066. end;
  1067. end;
  1068. end;
  1069. procedure updateState(supreg: tsuperregister; p: tai);
  1070. { this procedure updates the read and write states of the instructions }
  1071. { coming after p. It's called when the read/write state of p has been }
  1072. { changed and this change has to be propagated to the following }
  1073. { instructions as well }
  1074. var
  1075. newRState, newWState: byte;
  1076. prevRState, prevWState: byte;
  1077. doRState, doWState: boolean;
  1078. begin
  1079. { get the new read/write states from p }
  1080. with ptaiprop(p.optinfo)^.regs[supreg] do
  1081. begin
  1082. newRState := rState;
  1083. newWState := wState;
  1084. end;
  1085. if not GetNextInstruction(p,p) then
  1086. exit;
  1087. { get the old read/write states from the next instruction, to know }
  1088. { when we can stop updating }
  1089. with ptaiprop(p.optinfo)^.regs[supreg] do
  1090. begin
  1091. prevRState := rState;
  1092. prevWState := wState;
  1093. end;
  1094. { adjust the states if this next instruction reads/writes the register }
  1095. if regReadByInstruction(supreg,p) then
  1096. incState(newRState,1);
  1097. if regModifiedByInstruction(supreg,p) then
  1098. incState(newWState,1);
  1099. { do we still have to update the read and/or write states? }
  1100. doRState := true;
  1101. doWState := true;
  1102. repeat
  1103. { update the states }
  1104. with ptaiprop(p.optinfo)^.regs[supreg] do
  1105. begin
  1106. if doRState then
  1107. rState := newRState;
  1108. if doWState then
  1109. wState := newWState;
  1110. end;
  1111. if not getNextInstruction(p,p) then
  1112. break;
  1113. with ptaiprop(p.optinfo)^.regs[supreg] do
  1114. begin
  1115. { stop updating the read state if it changes }
  1116. doRState :=
  1117. doRState and (rState = prevRState);
  1118. { if, by accident, this changed state is the same as the one }
  1119. { we've been using, change it to a value that's definitely }
  1120. { different from the previous and next state }
  1121. if not doRState and
  1122. (rState = newRState) then
  1123. begin
  1124. incState(newRState,1);
  1125. prevRState := rState;
  1126. doRState := true;
  1127. end;
  1128. { ditto for the write state }
  1129. doWState :=
  1130. doWState and (WState = prevWState);
  1131. if not doWState and
  1132. (wState = newWState) then
  1133. begin
  1134. incState(newWState,1);
  1135. prevWState := wState;
  1136. doWState := true;
  1137. end;
  1138. end;
  1139. { stop when we don't have to update either state anymore }
  1140. until not(doRState or doWState);
  1141. end;
  1142. function storeBack(start, current: tai; orgReg, newReg: tsuperregister): boolean;
  1143. { returns true if p1 contains an instruction that stores the contents }
  1144. { of newReg back to orgReg }
  1145. begin
  1146. storeback := false;
  1147. if (current.typ = ait_instruction) and
  1148. (taicpu(current).opcode = A_MOV) and
  1149. (taicpu(current).oper[0]^.typ = top_reg) and
  1150. (getsupreg(taicpu(current).oper[0]^.reg) = newReg) and
  1151. (taicpu(current).oper[1]^.typ = top_reg) and
  1152. (getsupreg(taicpu(current).oper[1]^.reg) = orgReg) then
  1153. case taicpu(current).opsize of
  1154. S_B:
  1155. storeback := true;
  1156. S_W:
  1157. storeback := taicpu(start).opsize <> S_B;
  1158. S_L:
  1159. storeback := taicpu(start).opsize = S_L;
  1160. else
  1161. internalerror(2003121501);
  1162. end;
  1163. end;
  1164. function canreplacereg(orgsupreg, newsupreg: tsuperregister; p: tai;
  1165. orgRegCanBeModified: boolean; var resnewregmodified, resorgregread, resremovelast: boolean; var returnendp: tai): boolean;
  1166. var
  1167. endP, hp: tai;
  1168. removeLast, sequenceEnd, tmpResult, newRegModified, orgRegRead: boolean;
  1169. begin
  1170. canreplacereg := false;
  1171. tmpResult := true;
  1172. sequenceEnd := false;
  1173. newRegModified := false;
  1174. orgRegRead := false;
  1175. removeLast := false;
  1176. endP := p;
  1177. while tmpResult and not sequenceEnd do
  1178. begin
  1179. tmpResult :=
  1180. getNextInstruction(endP,endP) and
  1181. (endp.typ = ait_instruction) and
  1182. not(taicpu(endp).is_jmp);
  1183. if tmpresult and not assigned(endp.optinfo) then
  1184. begin
  1185. { hp := tai_comment.Create(strpnew('next no optinfo'));
  1186. hp.next := endp;
  1187. hp.previous := endp.previous;
  1188. endp.previous := hp;
  1189. if assigned(hp.previous) then
  1190. hp.previous.next := hp;}
  1191. exit;
  1192. end;
  1193. if tmpResult and
  1194. { don't take into account instructions that will be removed }
  1195. not (ptaiprop(endp.optinfo)^.canBeRemoved) then
  1196. begin
  1197. { if the newsupreg gets stored back to the oldReg, we can change }
  1198. { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
  1199. { %oldReg" to "<operations on %oldReg>" }
  1200. removeLast := storeBack(p,endP, orgsupreg, newsupreg);
  1201. sequenceEnd :=
  1202. { no support for (i)div, mul and imul with hardcoded operands }
  1203. noHardCodedRegs(taicpu(endP),orgsupreg,newsupreg) and
  1204. { if newsupreg gets loaded with a new value, we can stop }
  1205. { replacing newsupreg with oldReg here (possibly keeping }
  1206. { the original contents of oldReg so we still know them }
  1207. { afterwards) }
  1208. (RegLoadedWithNewValue(newsupreg,true,taicpu(endP)) or
  1209. { we can also stop if we reached the end of the use of }
  1210. { newReg's current contents }
  1211. (GetNextInstruction(endp,hp) and
  1212. FindRegDealloc(newsupreg,hp)));
  1213. { to be able to remove the first and last instruction of }
  1214. { movl %reg1, %reg2 }
  1215. { <operations on %reg2> (replacing reg2 with reg1 here) }
  1216. { movl %reg2, %reg1 }
  1217. { %reg2 must not be use afterwards (it can be as the }
  1218. { result of a peepholeoptimization) }
  1219. removeLast := removeLast and sequenceEnd;
  1220. newRegModified :=
  1221. newRegModified or
  1222. (not(regLoadedWithNewValue(newsupreg,true,taicpu(endP))) and
  1223. RegModifiedByInstruction(newsupreg,endP));
  1224. orgRegRead := newRegModified and RegReadByInstruction(orgsupreg,endP);
  1225. sequenceEnd := SequenceEnd and
  1226. (removeLast or
  1227. { since newsupreg will be replaced by orgsupreg, we can't allow that newsupreg }
  1228. { gets modified if orgsupreg is still read afterwards (since after }
  1229. { replacing, this would mean that orgsupreg first gets modified and then }
  1230. { gets read in the assumption it still contains the unmodified value) }
  1231. not(newRegModified and orgRegRead)) (* and
  1232. { since newsupreg will be replaced by orgsupreg, we can't allow that newsupreg }
  1233. { gets modified if orgRegCanBeModified = false }
  1234. { this now gets checked after the loop (JM) }
  1235. (orgRegCanBeModified or not(newRegModified)) *);
  1236. tmpResult :=
  1237. not(removeLast) and
  1238. not(newRegModified and orgRegRead) and
  1239. (* (orgRegCanBeModified or not(newRegModified)) and *)
  1240. (* already checked at the top
  1241. (endp.typ = ait_instruction) and *)
  1242. NoHardCodedRegs(taicpu(endP),orgsupreg,newsupreg) and
  1243. RegSizesOk(orgsupreg,newsupreg,taicpu(endP)) and
  1244. not RegModifiedByInstruction(orgsupreg,endP);
  1245. end;
  1246. end;
  1247. canreplacereg := sequenceEnd and
  1248. (removeLast or
  1249. (orgRegCanBeModified or not(newRegModified))) and
  1250. (not(assigned(endp)) or
  1251. not(endp.typ = ait_instruction) or
  1252. (noHardCodedRegs(taicpu(endP),orgsupreg,newsupreg) and
  1253. RegSizesOk(orgsupreg,newsupreg,taicpu(endP)) and
  1254. not(newRegModified and
  1255. (orgsupreg in ptaiprop(endp.optinfo)^.usedRegs) and
  1256. not(RegLoadedWithNewValue(orgsupreg,true,taicpu(endP))))));
  1257. if canreplacereg then
  1258. begin
  1259. resnewregmodified := newregmodified;
  1260. resorgregread := orgregread;
  1261. resremovelast := removelast;
  1262. returnendp := endp;
  1263. end;
  1264. end;
  1265. function ReplaceReg(asml: TAAsmOutput; orgsupreg, newsupreg: tsuperregister; p,
  1266. seqstart: tai; const c: TContent; orgRegCanBeModified: Boolean;
  1267. var returnEndP: tai): Boolean;
  1268. { Tries to replace orgsupreg with newsupreg in all instructions coming after p }
  1269. { until orgsupreg gets loaded with a new value. Returns true if successful, }
  1270. { false otherwise. if successful, the contents of newsupreg are set to c, }
  1271. { which should hold the contents of newsupreg before the current sequence }
  1272. { started }
  1273. { if the function returns true, returnEndP holds the last instruction }
  1274. { where newsupreg was replaced by orgsupreg }
  1275. var
  1276. endP, hp: tai;
  1277. removeLast, sequenceEnd, newRegModified, orgRegRead,
  1278. stateChanged, readStateChanged: Boolean;
  1279. begin
  1280. replacereg := false;
  1281. if canreplacereg(orgsupreg,newsupreg,p,orgregcanbemodified,newregmodified, orgregread, removelast,endp) then
  1282. begin
  1283. {$ifdef replaceregdebug}
  1284. hp := tai_comment.Create(strpnew(
  1285. 'replacing '+std_regname(newreg(R_INTREGISTER,newsupreg,R_SUBWHOLE))+' with '+std_regname(newreg(R_INTREGISTER,orgsupreg,R_SUBWHOLE))+
  1286. ' from here...'));
  1287. hp.next := p;
  1288. hp.previous := p.previous;
  1289. p.previous := hp;
  1290. if assigned(hp.previous) then
  1291. hp.previous.next := hp;
  1292. hp := tai_comment.Create(strpnew(
  1293. 'replaced '+std_regname(newreg(R_INTREGISTER,newsupreg,R_SUBWHOLE))+' with '+std_regname(newreg(R_INTREGISTER,orgsupreg,R_SUBWHOLE))+
  1294. ' till here'));
  1295. hp.next := endp.next;
  1296. hp.previous := endp;
  1297. endp.next := hp;
  1298. if assigned(hp.next) then
  1299. hp.next.previous := hp;
  1300. {$endif replaceregdebug}
  1301. replaceReg := true;
  1302. returnEndP := endP;
  1303. getNextInstruction(p,hp);
  1304. stateChanged := false;
  1305. while hp <> endP do
  1306. begin
  1307. if {not(ptaiprop(hp.optinfo)^.canBeRemoved) and }
  1308. (hp.typ = ait_instruction) then
  1309. stateChanged :=
  1310. doReplaceReg(taicpu(hp),newsupreg,orgsupreg) or stateChanged;
  1311. if stateChanged then
  1312. updateStates(orgsupreg,newsupreg,hp,true);
  1313. getNextInstruction(hp,hp)
  1314. end;
  1315. if assigned(endp) and (endp.typ = ait_instruction) then
  1316. readStateChanged :=
  1317. doReplaceReadReg(taicpu(endP),newsupreg,orgsupreg);
  1318. if stateChanged or readStateChanged then
  1319. updateStates(orgsupreg,newsupreg,endP,stateChanged);
  1320. if stateChanged or readStateChanged then
  1321. updateState(orgsupreg,endP);
  1322. { We replaced newreg with oldreg between p and endp, so restore the contents }
  1323. { of newreg there with its contents from before the sequence. }
  1324. if removeLast or
  1325. RegLoadedWithNewValue(newsupreg,true,endP) then
  1326. GetLastInstruction(endP,hp)
  1327. else hp := endP;
  1328. RestoreRegContentsTo(newsupreg,c,seqstart,hp);
  1329. { Ot is possible that the new register was modified (e.g. an add/sub), so if }
  1330. { it was replaced by oldreg in that instruction, oldreg's contents have been }
  1331. { changed. To take this into account, we simply set the contents of orgsupreg }
  1332. { to "unknown" after this sequence }
  1333. if newRegModified then
  1334. ClearRegContentsFrom(orgsupreg,p,hp);
  1335. if removeLast then
  1336. ptaiprop(endp.optinfo)^.canBeRemoved := true;
  1337. allocRegBetween(asml,newreg(R_INTREGISTER,orgsupreg,R_SUBWHOLE),p,endP,ptaiprop(p.optinfo)^.usedregs);
  1338. end
  1339. {$ifdef replaceregdebug}
  1340. else
  1341. begin
  1342. hp := tai_comment.Create(strpnew(
  1343. 'replacing '+std_regname(newreg(R_INTREGISTER,newsupreg,R_SUBWHOLE))+' with '+std_regname(newreg(R_INTREGISTER,orgsupreg,R_SUBWHOLE))+
  1344. ' from here...'));
  1345. hp.previous := p.previous;
  1346. hp.next := p;
  1347. p.previous := hp;
  1348. if assigned(hp.previous) then
  1349. hp.previous.next := hp;
  1350. hp := tai_comment.Create(strpnew(
  1351. 'replacing '+std_regname(newreg(R_INTREGISTER,newsupreg,R_SUBWHOLE))+' with '+std_regname(newreg(R_INTREGISTER,orgsupreg,R_SUBWHOLE))+
  1352. ' failed here'));
  1353. hp.next := endp.next;
  1354. hp.previous := endp;
  1355. endp.next := hp;
  1356. if assigned(hp.next) then
  1357. hp.next.previous := hp;
  1358. end;
  1359. {$endif replaceregdebug}
  1360. end;
  1361. function FindRegWithConst(p: tai; size: topsize; l: aint; var Res: TRegister): Boolean;
  1362. {Finds a register which contains the constant l}
  1363. var
  1364. Counter: tsuperregister;
  1365. {$ifdef testing}
  1366. hp: tai;
  1367. {$endif testing}
  1368. begin
  1369. Result:=false;
  1370. Counter := RS_EAX;
  1371. repeat
  1372. {$ifdef testing}
  1373. if (ptaiprop(p.optinfo)^.regs[counter].typ in [con_const,con_noRemoveConst]) then
  1374. begin
  1375. hp := tai_comment.Create(strpnew(
  1376. 'checking const load of '+tostr(l)+' here...'));
  1377. hp.next := ptaiprop(p.optinfo)^.Regs[Counter].StartMod;
  1378. hp.previous := ptaiprop(p.optinfo)^.Regs[Counter].StartMod^.previous;
  1379. ptaiprop(p.optinfo)^.Regs[Counter].StartMod^.previous := hp;
  1380. if assigned(hp.previous) then
  1381. hp.previous.next := hp;
  1382. end;
  1383. {$endif testing}
  1384. if (ptaiprop(p.optinfo)^.regs[counter].typ in [con_const,con_noRemoveConst]) and
  1385. (taicpu(ptaiprop(p.optinfo)^.Regs[Counter].StartMod).opsize = size) and
  1386. (taicpu(ptaiprop(p.optinfo)^.Regs[Counter].StartMod).oper[0]^.typ = top_const) and
  1387. (taicpu(ptaiprop(p.optinfo)^.Regs[Counter].StartMod).oper[0]^.val = l) then
  1388. begin
  1389. res:=taicpu(ptaiprop(p.optinfo)^.Regs[Counter].StartMod).oper[1]^.reg;
  1390. result:=true;
  1391. exit;
  1392. end;
  1393. inc(counter);
  1394. until (Counter > RS_EDI);
  1395. end;
  1396. procedure removePrevNotUsedLoad(p: tai; supreg: tsuperregister; check: boolean);
  1397. { if check = true, it means the procedure has to check whether it isn't }
  1398. { possible that the contents are still used after p (used when removing }
  1399. { instructions because of a "call"), otherwise this is not necessary }
  1400. { (e.g. when you have a "mov 8(%ebp),%eax", you can be sure the previous }
  1401. { value of %eax isn't used anymore later on) }
  1402. var
  1403. hp1, next, beforestartmod: tai;
  1404. begin
  1405. if getLastInstruction(p,hp1) then
  1406. with ptaiprop(hp1.optinfo)^.regs[supreg] do
  1407. if (typ in [con_ref,con_invalid,con_const]) and
  1408. (nrofMods = 1) and
  1409. (rState = ptaiprop(startmod.optinfo)^.regs[supreg].rState) and
  1410. (not(check) or
  1411. (not(regInInstruction(supreg,p)) and
  1412. (not(supreg in ptaiprop(hp1.optinfo)^.usedRegs) or
  1413. findRegDealloc(supreg,p)))) then
  1414. begin
  1415. ptaiprop(startMod.optinfo)^.canBeRemoved := true;
  1416. getnextinstruction(p,next);
  1417. { give the register that was modified by this instruction again }
  1418. { the contents it had before this instruction }
  1419. if getlastinstruction(startmod,beforestartmod) then
  1420. RestoreRegContentsTo(supreg,ptaiprop(beforestartmod.optinfo)^.regs[supreg],
  1421. startmod,p)
  1422. else
  1423. ClearRegContentsFrom(supreg,startmod,hp1);
  1424. end;
  1425. end;
  1426. {$ifdef notused}
  1427. function is_mov_for_div(p: taicpu): boolean;
  1428. begin
  1429. is_mov_for_div :=
  1430. (p.opcode = A_MOV) and
  1431. (p.oper[0]^.typ = top_const) and
  1432. (p.oper[1]^.typ = top_reg) and
  1433. (p.oper[1]^.reg = RS_EDX) and
  1434. getNextInstruction(p,p) and
  1435. (p.typ = ait_instruction) and
  1436. ((p.opcode = A_DIV) or
  1437. (p.opcode = A_IDIV));
  1438. end;
  1439. {$endif notused}
  1440. function memtoreg(t: taicpu; const ref: treference; var startp: tai): tregister;
  1441. var
  1442. hp: tai;
  1443. p: ptaiprop;
  1444. regcounter: tsuperregister;
  1445. optimizable: boolean;
  1446. begin
  1447. if not getlastinstruction(t,hp) or
  1448. not issimplememloc(ref) then
  1449. begin
  1450. memtoreg := NR_NO;
  1451. exit;
  1452. end;
  1453. p := ptaiprop(hp.optinfo);
  1454. optimizable := false;
  1455. for regcounter := RS_EAX to RS_EDI do
  1456. begin
  1457. if (assigned(p^.regs[regcounter].memwrite) and
  1458. refsequal(ref,p^.regs[regcounter].memwrite.oper[1]^.ref^)) then
  1459. begin
  1460. optimizable := true;
  1461. hp := p^.regs[regcounter].memwrite;
  1462. end
  1463. else if ((p^.regs[regcounter].typ in [CON_REF,CON_NOREMOVEREF]) and
  1464. (p^.regs[regcounter].nrofmods = 1) and
  1465. ((taicpu(p^.regs[regcounter].startmod).opcode = A_MOV) or
  1466. (taicpu(p^.regs[regcounter].startmod).opcode = A_MOVZX) or
  1467. (taicpu(p^.regs[regcounter].startmod).opcode = A_MOVSX)) and
  1468. (taicpu(p^.regs[regcounter].startmod).oper[0]^.typ = top_ref) and
  1469. refsequal(ref,taicpu(p^.regs[regcounter].startmod).oper[0]^.ref^)) then
  1470. begin
  1471. optimizable := true;
  1472. hp := p^.regs[regcounter].startmod;
  1473. end;
  1474. if optimizable then
  1475. if ((t.opsize <> S_B) or
  1476. not(regcounter in [RS_ESI,RS_EDI])) and
  1477. sizescompatible(taicpu(hp).opsize,t.opsize) then
  1478. begin
  1479. case t.opsize of
  1480. S_B:
  1481. begin
  1482. memtoreg := newreg(R_INTREGISTER,regcounter,R_SUBL)
  1483. end;
  1484. S_W,S_BW:
  1485. begin
  1486. memtoreg := newreg(R_INTREGISTER,regcounter,R_SUBW);
  1487. if (t.opsize = S_BW) then
  1488. begin
  1489. t.opcode := A_MOV;
  1490. t.opsize := S_W;
  1491. end;
  1492. end;
  1493. S_L,S_BL,S_WL:
  1494. begin
  1495. memtoreg := newreg(R_INTREGISTER,regcounter,R_SUBWHOLE);
  1496. if (t.opsize <> S_L) then
  1497. begin
  1498. t.opcode := A_MOV;
  1499. t.opsize := S_L;
  1500. end;
  1501. end;
  1502. end;
  1503. startp := hp;
  1504. exit;
  1505. end;
  1506. end;
  1507. memtoreg := NR_NO;
  1508. end;
  1509. procedure removeLocalStores(const t1: tai);
  1510. {var
  1511. p: tai;
  1512. regcount: tregister; }
  1513. begin
  1514. {
  1515. for regcount := LoGPReg to HiGPReg do
  1516. if assigned(pTaiProp(t1.optinfo)^.regs[regcount].memwrite) and
  1517. (taicpu(pTaiProp(t1.optinfo)^.regs[regcount].memwrite).oper[1]^.ref^.base
  1518. = current_procinfo.framepointer) then
  1519. begin
  1520. pTaiProp(pTaiProp(t1.optinfo)^.regs[regcount].memwrite.optinfo)^.canberemoved := true;
  1521. clearmemwrites(pTaiProp(t1.optinfo)^.regs[regcount].memwrite,regcount);
  1522. end;
  1523. }
  1524. end;
  1525. procedure loadcseregs(asml: taasmoutput; const reginfo: toptreginfo; curseqend, prevseqstart, curseqstart, curprev: tai; cnt: longint);
  1526. var
  1527. regsloaded: tregset;
  1528. regloads, reguses: array[RS_EAX..RS_EDI] of tai;
  1529. regcounter, substreg: tsuperregister;
  1530. hp, hp2: tai;
  1531. insertpos, prevseq_next: tai;
  1532. i: longint;
  1533. opc: tasmop;
  1534. begin
  1535. regsloaded := [];
  1536. fillchar(regloads,sizeof(regloads),0);
  1537. fillchar(reguses,sizeof(reguses),0);
  1538. getnextinstruction(prevseqstart,prevseq_next);
  1539. for regcounter := RS_EAX To RS_EDI do
  1540. if (reginfo.new2oldreg[regcounter] <> RS_INVALID) Then
  1541. begin
  1542. include(regsloaded,regcounter);
  1543. if assigned(ptaiprop(prevseqstart.optinfo)^.Regs[reginfo.new2oldreg[regcounter]].StartMod) then
  1544. AllocRegBetween(asml,newreg(R_INTREGISTER,reginfo.new2oldreg[regcounter],R_SUBWHOLE),
  1545. ptaiprop(prevseqstart.optinfo)^.Regs[reginfo.new2oldreg[regcounter]].StartMod,curseqstart,
  1546. ptaiprop(ptaiprop(prevseqstart.optinfo)^.Regs[reginfo.new2oldreg[regcounter]].StartMod.optinfo)^.usedregs)
  1547. else
  1548. AllocRegBetween(asml,newreg(R_INTREGISTER,reginfo.new2oldreg[regcounter],R_SUBWHOLE),
  1549. prevseqstart,curseqstart,ptaiprop(prevseqstart.optinfo)^.usedregs);
  1550. if curprev <> prevseqstart then
  1551. begin
  1552. if assigned(reginfo.lastReload[regCounter]) then
  1553. getLastInstruction(reginfo.lastReload[regCounter],hp)
  1554. else if assigned(reginfo.lastReload[reginfo.new2oldreg[regCounter]]) then
  1555. getLastInstruction(reginfo.lastReload[reginfo.new2OldReg[regCounter]],hp)
  1556. else
  1557. hp := curprev;
  1558. clearRegContentsFrom(regCounter,prevSeq_next,hp);
  1559. getnextInstruction(hp,hp);
  1560. allocRegBetween(asml,newreg(R_INTREGISTER,regCounter,R_SUBWHOLE),prevseqstart,hp,
  1561. ptaiprop(prevseqstart.optinfo)^.usedregs);
  1562. end;
  1563. if not(regcounter in reginfo.RegsLoadedforRef) and
  1564. {old reg new reg}
  1565. (reginfo.new2oldreg[regcounter] <> regcounter) then
  1566. begin
  1567. getLastInstruction(curseqend,hp);
  1568. if (curprev <> prevseqstart) or
  1569. {not(regCounter in rg.usableregsint + [RS_EDI,RS_ESI]) or}
  1570. not(regCounter in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_EDI,RS_ESI]) or
  1571. not ReplaceReg(asml,reginfo.new2oldreg[regcounter],
  1572. regCounter,hp,curseqstart,
  1573. ptaiprop(prevseqstart.optinfo)^.Regs[regCounter],true,hp2) then
  1574. begin
  1575. opc := A_MOV;
  1576. insertpos := prevseq_next;
  1577. if assigned(reguses[regcounter]) then
  1578. if assigned(regloads[reginfo.new2oldreg[regcounter]]) then
  1579. opc := A_XCHG
  1580. else
  1581. insertpos := tai(reguses[regcounter].next)
  1582. else
  1583. if assigned(regloads[reginfo.new2oldreg[regcounter]]) then
  1584. insertpos := regloads[reginfo.new2oldreg[regcounter]];
  1585. hp := Tai_Marker.Create(NoPropInfoStart);
  1586. InsertLLItem(asml, insertpos.previous,insertpos, hp);
  1587. hp2 := taicpu.Op_Reg_Reg(opc, S_L,
  1588. {old reg new reg}
  1589. newreg(R_INTREGISTER,reginfo.new2oldreg[regcounter],R_SUBWHOLE), newreg(R_INTREGISTER,regcounter,R_SUBWHOLE));
  1590. regloads[regcounter] := hp2;
  1591. reguses[reginfo.new2oldreg[regcounter]] := hp2;
  1592. new(ptaiprop(hp2.optinfo));
  1593. ptaiprop(hp2.optinfo)^ := ptaiprop(insertpos.optinfo)^;
  1594. ptaiprop(hp2.optinfo)^.canBeRemoved := false;
  1595. InsertLLItem(asml, insertpos.previous, insertpos, hp2);
  1596. hp := Tai_Marker.Create(NoPropInfoEnd);
  1597. InsertLLItem(asml, insertpos.previous, insertpos, hp);
  1598. { adjusts states in previous instruction so that it will }
  1599. { definitely be different from the previous or next state }
  1600. incstate(ptaiprop(hp2.optinfo)^.
  1601. regs[reginfo.new2oldreg[regcounter]].rstate,20);
  1602. incstate(ptaiprop(hp2.optinfo)^.
  1603. regs[regCounter].wstate,20);
  1604. updateState(reginfo.new2oldreg[regcounter],hp2);
  1605. updateState(regcounter,hp2);
  1606. end
  1607. else
  1608. begin
  1609. // replace the new register with the old register in the
  1610. // sequence itself as well so later comparisons get the
  1611. // correct knowledge about which registers are used
  1612. hp2 := curseqstart;
  1613. // curseqend = instruction following last instruction of this
  1614. // sequence
  1615. while hp2 <> curseqend do
  1616. begin
  1617. doreplacereg(taicpu(hp2),regcounter,reginfo.new2oldreg[regcounter]);
  1618. getnextinstruction(hp2,hp2);
  1619. end;
  1620. end;
  1621. end
  1622. else
  1623. { imagine the following code: }
  1624. { normal wrong optimized }
  1625. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  1626. { movl (%eax), %eax movl (%eax), %eax }
  1627. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  1628. { jne l1 jne l1 }
  1629. { movl 8(%ebp), %eax }
  1630. { movl (%eax), %edi movl %eax, %edi }
  1631. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  1632. { movl 8(%ebp), %eax }
  1633. { pushl 70(%eax) pushl 70(%eax) }
  1634. { }
  1635. { The error is that at the moment that the last instruction is executed, }
  1636. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  1637. { registers that are completely removed from a sequence (= registers in }
  1638. { RegLoadedforRef), have to be changed to their contents from before the }
  1639. { sequence. }
  1640. { if regcounter in reginfo.RegsLoadedforRef then }
  1641. begin
  1642. hp := curseqstart;
  1643. { cnt still holds the number of instructions }
  1644. { of the sequence, so go to the end of it }
  1645. for i := 1 to pred(cnt) do
  1646. getNextInstruction(hp,hp);
  1647. { curprev = instruction prior to start of sequence }
  1648. restoreRegContentsTo(regCounter,
  1649. ptaiprop(curprev.optinfo)^.Regs[regcounter],
  1650. curseqstart,hp);
  1651. end;
  1652. end;
  1653. end;
  1654. procedure replaceoperandwithreg(asml: taasmoutput; p: tai; opnr: byte; reg: tregister);
  1655. var
  1656. hp: tai;
  1657. begin
  1658. { new instruction -> it's info block is not in the big one allocated at the start }
  1659. hp := Tai_Marker.Create(NoPropInfoStart);
  1660. InsertLLItem(asml, p.previous,p, hp);
  1661. { duplicate the original instruction and replace it's designated operant with the register }
  1662. hp := tai(p.getcopy);
  1663. taicpu(hp).loadreg(opnr,reg);
  1664. { add optimizer state info }
  1665. new(ptaiprop(hp.optinfo));
  1666. ptaiprop(hp.optinfo)^ := ptaiprop(p.optinfo)^;
  1667. { new instruction can not be removed }
  1668. ptaiprop(hp.optinfo)^.canBeRemoved := false;
  1669. { but the old one can }
  1670. ptaiprop(p.optinfo)^.canBeRemoved := true;
  1671. { insert end marker }
  1672. InsertLLItem(asml, p.previous, p, hp);
  1673. hp := Tai_Marker.Create(NoPropInfoEnd);
  1674. InsertLLItem(asml, p.previous, p, hp);
  1675. end;
  1676. procedure doCSE(asml: TAAsmOutput; First, Last: tai; findPrevSeqs, doSubOpts: boolean);
  1677. {marks the instructions that can be removed by RemoveInstructs. They're not
  1678. removed immediately because sometimes an instruction needs to be checked in
  1679. two different sequences}
  1680. var cnt, cnt2, {cnt3,} orgNrofMods: longint;
  1681. p, hp1, hp2, prevSeq: tai;
  1682. hp3, hp4: tai;
  1683. hp5 : tai;
  1684. reginfo: toptreginfo;
  1685. memreg: tregister;
  1686. regcounter: tsuperregister;
  1687. begin
  1688. p := First;
  1689. SkipHead(p);
  1690. while (p <> Last) do
  1691. begin
  1692. case p.typ of
  1693. ait_align:
  1694. if not(tai_align(p).use_op) then
  1695. SetAlignReg(p);
  1696. ait_instruction:
  1697. begin
  1698. case taicpu(p).opcode of
  1699. {
  1700. Does not work anymore with register calling because the registers are
  1701. released before the call
  1702. A_CALL:
  1703. for regCounter := RS_EAX to RS_EBX do
  1704. removePrevNotUsedLoad(p,regCounter,true);
  1705. }
  1706. A_CLD: if GetLastInstruction(p, hp1) and
  1707. (ptaiprop(hp1.optinfo)^.DirFlag = F_NotSet) then
  1708. ptaiprop(tai(p).optinfo)^.CanBeRemoved := True;
  1709. A_LEA, A_MOV, A_MOVZX, A_MOVSX:
  1710. begin
  1711. hp2 := p;
  1712. case taicpu(p).oper[0]^.typ of
  1713. top_ref, top_reg:
  1714. if (taicpu(p).oper[1]^.typ = top_reg) then
  1715. begin
  1716. With ptaiprop(p.optinfo)^.Regs[getsupreg(taicpu(p).oper[1]^.reg)] do
  1717. begin
  1718. if (startmod = p) then
  1719. orgNrofMods := nrofMods
  1720. else
  1721. orgNrofMods := 0;
  1722. if (p = StartMod) and
  1723. GetLastInstruction (p, hp1) and
  1724. not(hp1.typ in [ait_marker,ait_label]) then
  1725. {so we don't try to check a sequence when p is the first instruction of the block}
  1726. begin
  1727. {$ifdef csdebug}
  1728. hp5 := tai_comment.Create(strpnew(
  1729. 'cse checking '+std_regname(taicpu(p).oper[1]^.reg)));
  1730. insertLLItem(asml,p,p.next,hp5);
  1731. {$endif csdebug}
  1732. if CheckSequence(p,prevSeq,getsupreg(taicpu(p).oper[1]^.reg), Cnt, reginfo, findPrevSeqs) and
  1733. (Cnt > 0) then
  1734. begin
  1735. (*
  1736. hp1 := nil;
  1737. { although it's perfectly ok to remove an instruction which doesn't contain }
  1738. { the register that we've just checked (CheckSequence takes care of that), }
  1739. { the sequence containing this other register should also be completely }
  1740. { checked and removed, otherwise we may get situations like this: }
  1741. { }
  1742. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  1743. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  1744. { movl 8(%edx), %edx movl 8(%edx), %edx }
  1745. { movl (%eax), eax movl (%eax), eax }
  1746. { cmpl %eax, %edx cmpl %eax, %edx }
  1747. { jnz l123 getting converted to jnz l123 }
  1748. { movl 12(%ebp), %edx movl 4(%eax), eax }
  1749. { movl 16(%ebp), %eax }
  1750. { movl 8(%edx), %edx }
  1751. { movl 4(%eax), eax }
  1752. *)
  1753. { not anymore: if the start of a new sequence is found while checking (e.g. }
  1754. { above that of eax while checking edx, this new sequence is automatically }
  1755. { also checked }
  1756. Cnt2 := 1;
  1757. while Cnt2 <= Cnt do
  1758. begin
  1759. {$ifndef noremove}
  1760. ptaiprop(p.optinfo)^.CanBeRemoved := True
  1761. {$endif noremove}
  1762. ; inc(Cnt2);
  1763. GetNextInstruction(p, p);
  1764. end;
  1765. {hp4 is used to get the contents of the registers before the sequence}
  1766. GetLastInstruction(hp2, hp4);
  1767. {$IfDef CSDebug}
  1768. for regcounter := RS_EAX To RS_EDI do
  1769. if (regcounter in reginfo.RegsLoadedforRef) then
  1770. begin
  1771. hp5 := tai_comment.Create(strpnew('New: '+std_regname(newreg(R_INTREGISTER,regcounter,R_SUBNONE))+', Old: '+
  1772. std_regname(newreg(R_INTREGISTER,reginfo.new2oldreg[regcounter],R_SUBNONE))));
  1773. InsertLLItem(asml, tai(hp2.previous), hp2, hp5);
  1774. end;
  1775. {$EndIf CSDebug}
  1776. { if some registers were different in the old and the new sequence, move }
  1777. { the contents of those old registers to the new ones }
  1778. loadcseregs(asml,reginfo,p,prevseq,hp2,hp4,cnt);
  1779. continue;
  1780. end
  1781. end;
  1782. end;
  1783. { try to replace the new reg with the old reg }
  1784. if not(ptaiprop(p.optinfo)^.canBeRemoved) then
  1785. if (taicpu(p).oper[0]^.typ = top_reg) and
  1786. (taicpu(p).oper[1]^.typ = top_reg) and
  1787. { only remove if we're not storing something in a regvar }
  1788. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) and
  1789. { (taicpu(p).oper[1]^.reg in (rg.usableregsint+[RS_EDI])) and}
  1790. (taicpu(p).opcode = A_MOV) and
  1791. getLastInstruction(p,hp4) and
  1792. { we only have to start replacing from the instruction after the mov, }
  1793. { but replacereg only starts with getnextinstruction(p,p) }
  1794. replaceReg(asml,getsupreg(taicpu(p).oper[0]^.reg),
  1795. getsupreg(taicpu(p).oper[1]^.reg),p,p,
  1796. ptaiprop(hp4.optinfo)^.regs[getsupreg(taicpu(p).oper[1]^.reg)],false,hp1) then
  1797. begin
  1798. ptaiprop(p.optinfo)^.canBeRemoved := true;
  1799. { this is just a regular move that was here, so the source register should be }
  1800. { allocated already at this point -> only allocate from here onwards }
  1801. if not(getsupreg(taicpu(p).oper[0]^.reg) in pTaiProp(p.optinfo)^.usedregs) then
  1802. internalerror(2004101011);
  1803. allocRegBetween(asml,taicpu(p).oper[0]^.reg,
  1804. p,hp1,pTaiProp(p.optinfo)^.usedregs)
  1805. end
  1806. else
  1807. begin
  1808. if (taicpu(p).oper[1]^.typ = top_reg) and
  1809. not regInOp(getsupreg(taicpu(p).oper[1]^.reg),taicpu(p).oper[0]^) then
  1810. removePrevNotUsedLoad(p,getsupreg(taicpu(p).oper[1]^.reg),false);
  1811. if doSubOpts and
  1812. (taicpu(p).opcode <> A_LEA) and
  1813. (taicpu(p).oper[0]^.typ = top_ref) then
  1814. begin
  1815. memreg :=
  1816. memtoreg(taicpu(p),
  1817. taicpu(p).oper[0]^.ref^,hp5);
  1818. if memreg <> NR_NO then
  1819. if (taicpu(p).opcode = A_MOV) and
  1820. (taicpu(p).oper[1]^.typ = top_reg) and
  1821. (taicpu(p).oper[1]^.reg = memreg) then
  1822. begin
  1823. pTaiProp(p.optinfo)^.canberemoved := true;
  1824. allocregbetween(asml,memreg,hp5,p,ptaiprop(hp5.optinfo)^.usedregs);
  1825. end
  1826. else
  1827. begin
  1828. replaceoperandwithreg(asml,p,0,memreg);
  1829. allocregbetween(asml,memreg,hp5,p,ptaiprop(hp5.optinfo)^.usedregs);
  1830. regcounter := getsupreg(memreg);
  1831. incstate(pTaiProp(p.optinfo)^.regs[regcounter].rstate,1);
  1832. updatestate(regcounter,p);
  1833. end;
  1834. end;
  1835. end;
  1836. { at first, only try optimizations of large blocks, because doing }
  1837. { doing smaller ones may prevent bigger ones from completing in }
  1838. { in the next pass }
  1839. if not doSubOpts and (orgNrofMods <> 0) then
  1840. begin
  1841. p := hp2;
  1842. for cnt := 1 to pred(orgNrofMods) do
  1843. getNextInstruction(p,p);
  1844. end;
  1845. end;
  1846. top_Const:
  1847. begin
  1848. case taicpu(p).oper[1]^.typ of
  1849. Top_Reg:
  1850. begin
  1851. regCounter := getsupreg(taicpu(p).oper[1]^.reg);
  1852. if GetLastInstruction(p, hp1) then
  1853. With ptaiprop(hp1.optinfo)^.Regs[regCounter] do
  1854. if (typ in [con_const,con_noRemoveConst]) and
  1855. (taicpu(startMod).opsize >= taicpu(p).opsize) and
  1856. opsequal(taicpu(StartMod).oper[0]^,taicpu(p).oper[0]^) then
  1857. begin
  1858. ptaiprop(p.optinfo)^.CanBeRemoved := True;
  1859. allocRegBetween(asml,taicpu(p).oper[1]^.reg,startmod,p,
  1860. ptaiprop(startmod.optinfo)^.usedregs);
  1861. end
  1862. else
  1863. removePrevNotUsedLoad(p,getsupreg(taicpu(p).oper[1]^.reg),false);
  1864. end;
  1865. Top_Ref:
  1866. if (taicpu(p).oper[0]^.typ = top_const) and
  1867. getLastInstruction(p,hp1) and
  1868. findRegWithConst(hp1,taicpu(p).opsize,taicpu(p).oper[0]^.val,memreg) then
  1869. begin
  1870. taicpu(p).loadreg(0,memreg);
  1871. allocRegBetween(asml,memreg,
  1872. ptaiprop(hp1.optinfo)^.regs[getsupreg(memreg)].startMod,p,
  1873. ptaiprop(ptaiprop(hp1.optinfo)^.regs[getsupreg(memreg)].startMod.optinfo)^.usedregs);
  1874. end;
  1875. end;
  1876. end;
  1877. end;
  1878. end;
  1879. A_LEAVE:
  1880. begin
  1881. if getlastinstruction(p,hp1) then
  1882. removeLocalStores(hp1);
  1883. end;
  1884. A_STD: if GetLastInstruction(p, hp1) and
  1885. (ptaiprop(hp1.optinfo)^.DirFlag = F_Set) then
  1886. ptaiprop(tai(p).optinfo)^.CanBeRemoved := True;
  1887. else
  1888. begin
  1889. for cnt := 1 to maxinschanges do
  1890. begin
  1891. case InsProp[taicpu(p).opcode].Ch[cnt] of
  1892. Ch_ROp1:
  1893. if (taicpu(p).oper[0]^.typ = top_ref) and
  1894. ((taicpu(p).opcode < A_F2XM1) or
  1895. ((taicpu(p).opcode > A_IN) and
  1896. (taicpu(p).opcode < A_OUT)) or
  1897. (taicpu(p).opcode = A_PUSH) or
  1898. ((taicpu(p).opcode >= A_RCL) and
  1899. (taicpu(p).opcode <= A_XOR))) then
  1900. begin
  1901. memreg :=
  1902. memtoreg(taicpu(p),
  1903. taicpu(p).oper[0]^.ref^,hp5);
  1904. if memreg <> NR_NO then
  1905. begin
  1906. replaceoperandwithreg(asml,p,0,memreg);
  1907. allocregbetween(asml,memreg,hp5,p,ptaiprop(hp5.optinfo)^.usedregs);
  1908. regcounter := getsupreg(memreg);
  1909. incstate(pTaiProp(p.optinfo)^.regs[regcounter].rstate,1);
  1910. updatestate(regcounter,p);
  1911. end;
  1912. end;
  1913. Ch_MOp1:
  1914. if not(CS_LittleSize in aktglobalswitches) and
  1915. (taicpu(p).oper[0]^.typ = top_ref) then
  1916. begin
  1917. memreg :=
  1918. memtoreg(taicpu(p),
  1919. taicpu(p).oper[0]^.ref^,hp5);
  1920. if (memreg <> NR_NO) and
  1921. (not getNextInstruction(p,hp1) or
  1922. (RegLoadedWithNewValue(getsupreg(memreg),false,hp1) or
  1923. FindRegDealloc(getsupreg(memreg),hp1))) then
  1924. begin
  1925. hp1 := Tai_Marker.Create(NoPropInfoEnd);
  1926. insertllitem(asml,p,p.next,hp1);
  1927. hp1 := taicpu.op_reg_ref(A_MOV,reg2opsize(memreg),
  1928. memreg,taicpu(p).oper[0]^.ref^);
  1929. new(ptaiprop(hp1.optinfo));
  1930. pTaiProp(hp1.optinfo)^ := pTaiProp(p.optinfo)^;
  1931. insertllitem(asml,p,p.next,hp1);
  1932. regcounter := getsupreg(memreg);
  1933. incstate(pTaiProp(hp1.optinfo)^.regs[regcounter].rstate,1);
  1934. updatestate(regcounter,hp1);
  1935. hp1 := Tai_Marker.Create(NoPropInfoStart);
  1936. insertllitem(asml,p,p.next,hp1);
  1937. replaceoperandwithreg(asml,p,0,memreg);
  1938. allocregbetween(asml,memreg,hp5,
  1939. tai(p.next.next),ptaiprop(hp5.optinfo)^.usedregs);
  1940. ClearRegContentsFrom(regcounter,hp5,p);
  1941. end;
  1942. end;
  1943. Ch_ROp2:
  1944. if ((taicpu(p).opcode = A_CMP) or
  1945. (taicpu(p).opcode = A_TEST)) and
  1946. (taicpu(p).oper[1]^.typ = top_ref) then
  1947. begin
  1948. memreg :=
  1949. memtoreg(taicpu(p),
  1950. taicpu(p).oper[1]^.ref^,hp5);
  1951. if memreg <> NR_NO then
  1952. begin
  1953. replaceoperandwithreg(asml,p,1,memreg);
  1954. allocregbetween(asml,memreg,hp5,p,ptaiprop(hp5.optinfo)^.usedregs);
  1955. regcounter := getsupreg(memreg);
  1956. incstate(pTaiProp(p.optinfo)^.regs[regcounter].rstate,1);
  1957. updatestate(regcounter,p);
  1958. end;
  1959. end;
  1960. Ch_MOp2:
  1961. if not(cs_littlesize in aktglobalswitches) and
  1962. (taicpu(p).oper[1]^.typ = top_ref) and
  1963. ((taicpu(p).opcode < A_BT) or
  1964. ((taicpu(p).opcode > A_IN) and
  1965. (taicpu(p).opcode < A_OUT)) or
  1966. (taicpu(p).opcode = A_PUSH) or
  1967. ((taicpu(p).opcode >= A_RCL) and
  1968. (taicpu(p).opcode <= A_XOR))) then
  1969. begin
  1970. memreg :=
  1971. memtoreg(taicpu(p),
  1972. taicpu(p).oper[1]^.ref^,hp5);
  1973. if (memreg <> NR_NO) and
  1974. (not getNextInstruction(p,hp1) or
  1975. (RegLoadedWithNewValue(getsupreg(memreg),false,hp1) or
  1976. FindRegDealloc(getsupreg(memreg),hp1))) then
  1977. begin
  1978. hp1 := Tai_Marker.Create(NoPropInfoEnd);
  1979. insertllitem(asml,p,p.next,hp1);
  1980. hp1 := taicpu.op_reg_ref(A_MOV,reg2opsize(memreg),
  1981. memreg,taicpu(p).oper[1]^.ref^);
  1982. new(ptaiprop(hp1.optinfo));
  1983. pTaiProp(hp1.optinfo)^ := pTaiProp(p.optinfo)^;
  1984. insertllitem(asml,p,p.next,hp1);
  1985. regcounter := getsupreg(memreg);
  1986. incstate(pTaiProp(hp1.optinfo)^.regs[regcounter].rstate,1);
  1987. updatestate(regcounter,hp1);
  1988. hp1 := Tai_Marker.Create(NoPropInfoStart);
  1989. insertllitem(asml,p,p.next,hp1);
  1990. replaceoperandwithreg(asml,p,1,memreg);
  1991. allocregbetween(asml,memreg,hp5,
  1992. tai(p.next.next),ptaiprop(hp5.optinfo)^.usedregs);
  1993. ClearRegContentsFrom(regcounter,hp5,p);
  1994. end;
  1995. end;
  1996. end;
  1997. end;
  1998. end;
  1999. end
  2000. end;
  2001. end;
  2002. GetNextInstruction(p, p);
  2003. end;
  2004. end;
  2005. function removeInstructs(asml: TAAsmoutput; first, last: tai): boolean;
  2006. { Removes the marked instructions and disposes the PTaiProps of the other }
  2007. { instructions }
  2008. var
  2009. p, hp1: tai;
  2010. nopropinfolevel: longint;
  2011. begin
  2012. removeInstructs := false;
  2013. p := First;
  2014. nopropinfolevel := 0;
  2015. while (p <> Last) do
  2016. begin
  2017. if (p.typ = ait_marker) and
  2018. (Tai_marker(p).kind = noPropInfoStart) then
  2019. begin
  2020. hp1 := tai(p.next);
  2021. asml.remove(p);
  2022. p.free;
  2023. nopropinfolevel := 1;
  2024. while (nopropinfolevel <> 0) do
  2025. begin
  2026. p := tai(hp1.next);
  2027. {$ifndef noinstremove}
  2028. { allocregbetween can insert new ait_regalloc objects }
  2029. { without optinfo }
  2030. if (hp1.typ = ait_marker) then
  2031. begin
  2032. case Tai_marker(hp1).kind of
  2033. { they can be nested! }
  2034. noPropInfoStart: inc(nopropinfolevel);
  2035. noPropInfoEnd: dec(nopropinfolevel);
  2036. else
  2037. begin
  2038. hp1 := p;
  2039. continue;
  2040. end;
  2041. end;
  2042. asml.remove(hp1);
  2043. hp1.free;
  2044. end
  2045. else if assigned(hp1.optinfo) then
  2046. if ptaiprop(hp1.optinfo)^.canBeRemoved then
  2047. begin
  2048. dispose(ptaiprop(hp1.optinfo));
  2049. hp1.optinfo := nil;
  2050. asml.remove(hp1);
  2051. hp1.free;
  2052. end
  2053. else
  2054. {$endif noinstremove}
  2055. begin
  2056. dispose(ptaiprop(hp1.optinfo));
  2057. hp1.optinfo := nil;
  2058. end;
  2059. hp1 := p;
  2060. end;
  2061. end
  2062. else
  2063. {$ifndef noinstremove}
  2064. if assigned(p.optinfo) and
  2065. ptaiprop(p.optinfo)^.canBeRemoved then
  2066. begin
  2067. hp1 := tai(p.next);
  2068. asml.Remove(p);
  2069. p.free;
  2070. p := hp1;
  2071. removeInstructs := true;
  2072. end
  2073. else
  2074. {$endif noinstremove}
  2075. begin
  2076. p.optinfo := nil;
  2077. p := tai(p.next);;
  2078. end;
  2079. end;
  2080. end;
  2081. function CSE(asml: TAAsmOutput; First, Last: tai; pass: longint): boolean;
  2082. begin
  2083. doCSE(asml, First, Last, not(cs_slowoptimize in aktglobalswitches) or (pass >= 2),
  2084. not(cs_slowoptimize in aktglobalswitches) or (pass >= 1));
  2085. { register renaming }
  2086. if not(cs_slowoptimize in aktglobalswitches) or (pass > 0) then
  2087. doRenaming(asml, first, last);
  2088. cse := removeInstructs(asml, first, last);
  2089. end;
  2090. end.