csopt386.pas 87 KB

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