csopt386.pas 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  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. newOrgRegRState := byte(longint(regs[orgReg].rState) +
  943. longint(ptaiprop(hp.optinfo)^.regs[newReg].rState) - regs[newReg].rstate);
  944. if writeStateToo then
  945. newOrgRegWState := byte(longint(regs[orgReg].wState) +
  946. longint(ptaiprop(hp.optinfo)^.regs[newReg].wState) - regs[newReg].wstate);
  947. end
  948. else
  949. with ptaiprop(hp.optinfo)^.regs[newReg] do
  950. begin
  951. newOrgRegRState := rState;
  952. if writeStateToo then
  953. newOrgRegWState := wState;
  954. end;
  955. with ptaiprop(hp.optinfo)^.regs[orgReg] do
  956. begin
  957. rState := newOrgRegRState;
  958. if writeStateToo then
  959. wState := newOrgRegwState;
  960. end;
  961. end;
  962. function doReplaceReg(hp: taicpu; newReg, orgReg: tsuperregister): boolean;
  963. var
  964. opCount: longint;
  965. tmpResult: boolean;
  966. begin
  967. tmpresult := false;
  968. for opCount := 0 to hp.ops-1 do
  969. tmpResult :=
  970. changeOp(hp.oper[opCount]^,newReg,orgReg) or tmpResult;
  971. doReplaceReg := tmpResult;
  972. end;
  973. function RegSizesOK(oldReg,newReg: tsuperregister; p: taicpu): boolean;
  974. { oldreg and newreg must be 32bit components }
  975. var
  976. opCount: longint;
  977. tmpreg: tsuperregister;
  978. begin
  979. RegSizesOK := true;
  980. { if only one of them is a general purpose register ... }
  981. if (IsGP32reg(oldReg) xor IsGP32Reg(newReg)) then
  982. begin
  983. for opCount := 0 to p.ops-1 do
  984. if (p.oper[opCount]^.typ = top_reg) and
  985. (getsubreg(p.oper[opCount]^.reg) in [R_SUBL,R_SUBH]) then
  986. begin
  987. tmpreg := getsupreg(p.oper[opCount]^.reg);
  988. if (tmpreg = oldreg) or
  989. (tmpreg = newreg) then
  990. begin
  991. RegSizesOK := false;
  992. break
  993. end
  994. end;
  995. end;
  996. end;
  997. function doReplaceReadReg(p: taicpu; newReg,orgReg: tsuperregister): boolean;
  998. var
  999. opCount: byte;
  1000. begin
  1001. doReplaceReadReg := false;
  1002. { handle special case }
  1003. case p.opcode of
  1004. A_IMUL:
  1005. begin
  1006. case p.ops of
  1007. 1: internalerror(1301001);
  1008. 2,3:
  1009. begin
  1010. if changeOp(p.oper[0]^,newReg,orgReg) then
  1011. begin
  1012. { updateStates(orgReg,newReg,p,false);}
  1013. doReplaceReadReg := true;
  1014. end;
  1015. if p.ops = 3 then
  1016. if changeOp(p.oper[1]^,newReg,orgReg) then
  1017. begin
  1018. { updateStates(orgReg,newReg,p,false);}
  1019. doReplaceReadReg := true;
  1020. end;
  1021. end;
  1022. end;
  1023. end;
  1024. A_DIV,A_IDIV,A_MUL: internalerror(1301002);
  1025. else
  1026. begin
  1027. for opCount := 0 to p.ops-1 do
  1028. if p.oper[opCount]^.typ = top_ref then
  1029. if changeOp(p.oper[opCount]^,newReg,orgReg) then
  1030. begin
  1031. { updateStates(orgReg,newReg,p,false);}
  1032. doReplaceReadReg := true;
  1033. end;
  1034. for opCount := 1 to maxinschanges do
  1035. case InsProp[p.opcode].Ch[opCount] of
  1036. Ch_ROp1:
  1037. if p.oper[0]^.typ = top_reg then
  1038. if changeReg(p.oper[0]^.reg,newReg,orgReg) then
  1039. begin
  1040. { updateStates(orgReg,newReg,p,false);}
  1041. doReplaceReadReg := true;
  1042. end;
  1043. Ch_ROp2:
  1044. if p.oper[1]^.typ = top_reg then
  1045. if changeReg(p.oper[1]^.reg,newReg,orgReg) then
  1046. begin
  1047. { updateStates(orgReg,newReg,p,false);}
  1048. doReplaceReadReg := true;
  1049. end;
  1050. Ch_ROp3:
  1051. if p.oper[2]^.typ = top_reg then
  1052. if changeReg(p.oper[2]^.reg,newReg,orgReg) then
  1053. begin
  1054. { updateStates(orgReg,newReg,p,false);}
  1055. doReplaceReadReg := true;
  1056. end;
  1057. end;
  1058. end;
  1059. end;
  1060. end;
  1061. procedure updateState(supreg: tsuperregister; p: tai);
  1062. { this procedure updates the read and write states of the instructions }
  1063. { coming after p. It's called when the read/write state of p has been }
  1064. { changed and this change has to be propagated to the following }
  1065. { instructions as well }
  1066. var
  1067. newRState, newWState: byte;
  1068. prevRState, prevWState: byte;
  1069. doRState, doWState: boolean;
  1070. begin
  1071. { get the new read/write states from p }
  1072. with ptaiprop(p.optinfo)^.regs[supreg] do
  1073. begin
  1074. newRState := rState;
  1075. newWState := wState;
  1076. end;
  1077. if not GetNextInstruction(p,p) then
  1078. exit;
  1079. { get the old read/write states from the next instruction, to know }
  1080. { when we can stop updating }
  1081. with ptaiprop(p.optinfo)^.regs[supreg] do
  1082. begin
  1083. prevRState := rState;
  1084. prevWState := wState;
  1085. end;
  1086. { adjust the states if this next instruction reads/writes the register }
  1087. if regReadByInstruction(supreg,p) then
  1088. incState(newRState,1);
  1089. if regModifiedByInstruction(supreg,p) then
  1090. incState(newWState,1);
  1091. { do we still have to update the read and/or write states? }
  1092. doRState := true;
  1093. doWState := true;
  1094. repeat
  1095. { update the states }
  1096. with ptaiprop(p.optinfo)^.regs[supreg] do
  1097. begin
  1098. if doRState then
  1099. rState := newRState;
  1100. if doWState then
  1101. wState := newWState;
  1102. end;
  1103. if not getNextInstruction(p,p) then
  1104. break;
  1105. with ptaiprop(p.optinfo)^.regs[supreg] do
  1106. begin
  1107. { stop updating the read state if it changes }
  1108. doRState :=
  1109. doRState and (rState = prevRState);
  1110. { if, by accident, this changed state is the same as the one }
  1111. { we've been using, change it to a value that's definitely }
  1112. { different from the previous and next state }
  1113. if not doRState and
  1114. (rState = newRState) then
  1115. begin
  1116. incState(newRState,1);
  1117. prevRState := rState;
  1118. doRState := true;
  1119. end;
  1120. { ditto for the write state }
  1121. doWState :=
  1122. doWState and (WState = prevWState);
  1123. if not doWState and
  1124. (wState = newWState) then
  1125. begin
  1126. incState(newWState,1);
  1127. prevWState := wState;
  1128. doWState := true;
  1129. end;
  1130. end;
  1131. { stop when we don't have to update either state anymore }
  1132. until not(doRState or doWState);
  1133. end;
  1134. function storeBack(start, current: tai; orgReg, newReg: tsuperregister): boolean;
  1135. { returns true if p1 contains an instruction that stores the contents }
  1136. { of newReg back to orgReg }
  1137. begin
  1138. storeback := false;
  1139. if (current.typ = ait_instruction) and
  1140. (taicpu(current).opcode = A_MOV) and
  1141. (taicpu(current).oper[0]^.typ = top_reg) and
  1142. (getsupreg(taicpu(current).oper[0]^.reg) = newReg) and
  1143. (taicpu(current).oper[1]^.typ = top_reg) and
  1144. (getsupreg(taicpu(current).oper[1]^.reg) = orgReg) then
  1145. case taicpu(current).opsize of
  1146. S_B:
  1147. storeback := true;
  1148. S_W:
  1149. storeback := taicpu(start).opsize <> S_B;
  1150. S_L:
  1151. storeback := taicpu(start).opsize = S_L;
  1152. else
  1153. internalerror(2003121501);
  1154. end;
  1155. end;
  1156. function canreplacereg(orgsupreg, newsupreg: tsuperregister; p: tai;
  1157. orgRegCanBeModified: boolean; var resnewregmodified, resorgregread, resremovelast: boolean; var returnendp: tai): boolean;
  1158. var
  1159. endP, hp: tai;
  1160. removeLast, sequenceEnd, tmpResult, newRegModified, orgRegRead: boolean;
  1161. begin
  1162. canreplacereg := false;
  1163. tmpResult := true;
  1164. sequenceEnd := false;
  1165. newRegModified := false;
  1166. orgRegRead := false;
  1167. removeLast := false;
  1168. endP := p;
  1169. while tmpResult and not sequenceEnd do
  1170. begin
  1171. tmpResult :=
  1172. getNextInstruction(endP,endP) and
  1173. (endp.typ = ait_instruction) and
  1174. not(taicpu(endp).is_jmp);
  1175. if tmpresult and not assigned(endp.optinfo) then
  1176. begin
  1177. { hp := tai_comment.Create(strpnew('next no optinfo'));
  1178. hp.next := endp;
  1179. hp.previous := endp.previous;
  1180. endp.previous := hp;
  1181. if assigned(hp.previous) then
  1182. hp.previous.next := hp;}
  1183. exit;
  1184. end;
  1185. if tmpResult and
  1186. { don't take into account instructions that will be removed }
  1187. not (ptaiprop(endp.optinfo)^.canBeRemoved) then
  1188. begin
  1189. { if the newsupreg gets stored back to the oldReg, we can change }
  1190. { "mov %oldReg,%newReg; <operations on %newReg>; mov %newReg, }
  1191. { %oldReg" to "<operations on %oldReg>" }
  1192. removeLast := storeBack(p,endP, orgsupreg, newsupreg);
  1193. sequenceEnd :=
  1194. { no support for (i)div, mul and imul with hardcoded operands }
  1195. noHardCodedRegs(taicpu(endP),orgsupreg,newsupreg) and
  1196. { if newsupreg gets loaded with a new value, we can stop }
  1197. { replacing newsupreg with oldReg here (possibly keeping }
  1198. { the original contents of oldReg so we still know them }
  1199. { afterwards) }
  1200. (RegLoadedWithNewValue(newsupreg,true,taicpu(endP)) or
  1201. { we can also stop if we reached the end of the use of }
  1202. { newReg's current contents }
  1203. (GetNextInstruction(endp,hp) and
  1204. FindRegDealloc(newsupreg,hp)));
  1205. { to be able to remove the first and last instruction of }
  1206. { movl %reg1, %reg2 }
  1207. { <operations on %reg2> (replacing reg2 with reg1 here) }
  1208. { movl %reg2, %reg1 }
  1209. { %reg2 must not be use afterwards (it can be as the }
  1210. { result of a peepholeoptimization) }
  1211. removeLast := removeLast and sequenceEnd;
  1212. newRegModified :=
  1213. newRegModified or
  1214. (not(regLoadedWithNewValue(newsupreg,true,taicpu(endP))) and
  1215. RegModifiedByInstruction(newsupreg,endP));
  1216. orgRegRead := newRegModified and RegReadByInstruction(orgsupreg,endP);
  1217. sequenceEnd := SequenceEnd and
  1218. (removeLast or
  1219. { since newsupreg will be replaced by orgsupreg, we can't allow that newsupreg }
  1220. { gets modified if orgsupreg is still read afterwards (since after }
  1221. { replacing, this would mean that orgsupreg first gets modified and then }
  1222. { gets read in the assumption it still contains the unmodified value) }
  1223. not(newRegModified and orgRegRead)) (* and
  1224. { since newsupreg will be replaced by orgsupreg, we can't allow that newsupreg }
  1225. { gets modified if orgRegCanBeModified = false }
  1226. { this now gets checked after the loop (JM) }
  1227. (orgRegCanBeModified or not(newRegModified)) *);
  1228. tmpResult :=
  1229. not(removeLast) and
  1230. not(newRegModified and orgRegRead) and
  1231. (* (orgRegCanBeModified or not(newRegModified)) and *)
  1232. (* already checked at the top
  1233. (endp.typ = ait_instruction) and *)
  1234. NoHardCodedRegs(taicpu(endP),orgsupreg,newsupreg) and
  1235. RegSizesOk(orgsupreg,newsupreg,taicpu(endP)) and
  1236. not RegModifiedByInstruction(orgsupreg,endP);
  1237. end;
  1238. end;
  1239. canreplacereg := sequenceEnd and
  1240. (removeLast or
  1241. (orgRegCanBeModified or not(newRegModified))) and
  1242. (not(assigned(endp)) or
  1243. not(endp.typ = ait_instruction) or
  1244. (noHardCodedRegs(taicpu(endP),orgsupreg,newsupreg) and
  1245. RegSizesOk(orgsupreg,newsupreg,taicpu(endP)) and
  1246. not(newRegModified and
  1247. (orgsupreg in ptaiprop(endp.optinfo)^.usedRegs) and
  1248. not(RegLoadedWithNewValue(orgsupreg,true,taicpu(endP))))));
  1249. if canreplacereg then
  1250. begin
  1251. resnewregmodified := newregmodified;
  1252. resorgregread := orgregread;
  1253. resremovelast := removelast;
  1254. returnendp := endp;
  1255. end;
  1256. end;
  1257. function ReplaceReg(asml: TAAsmOutput; orgsupreg, newsupreg: tsuperregister; p,
  1258. seqstart: tai; const c: TContent; orgRegCanBeModified: Boolean;
  1259. var returnEndP: tai): Boolean;
  1260. { Tries to replace orgsupreg with newsupreg in all instructions coming after p }
  1261. { until orgsupreg gets loaded with a new value. Returns true if successful, }
  1262. { false otherwise. if successful, the contents of newsupreg are set to c, }
  1263. { which should hold the contents of newsupreg before the current sequence }
  1264. { started }
  1265. { if the function returns true, returnEndP holds the last instruction }
  1266. { where newsupreg was replaced by orgsupreg }
  1267. var
  1268. endP, hp: tai;
  1269. removeLast, sequenceEnd, newRegModified, orgRegRead,
  1270. stateChanged, readStateChanged: Boolean;
  1271. begin
  1272. replacereg := false;
  1273. if canreplacereg(orgsupreg,newsupreg,p,orgregcanbemodified,newregmodified, orgregread, removelast,endp) then
  1274. begin
  1275. {$ifdef replaceregdebug}
  1276. hp := tai_comment.Create(strpnew(
  1277. 'replacing '+std_regname(newreg(R_INTREGISTER,newsupreg,R_SUBWHOLE))+' with '+std_regname(newreg(R_INTREGISTER,orgsupreg,R_SUBWHOLE))+
  1278. ' from here...'));
  1279. hp.next := p;
  1280. hp.previous := p.previous;
  1281. p.previous := hp;
  1282. if assigned(hp.previous) then
  1283. hp.previous.next := hp;
  1284. hp := tai_comment.Create(strpnew(
  1285. 'replaced '+std_regname(newreg(R_INTREGISTER,newsupreg,R_SUBWHOLE))+' with '+std_regname(newreg(R_INTREGISTER,orgsupreg,R_SUBWHOLE))+
  1286. ' till here'));
  1287. hp.next := endp.next;
  1288. hp.previous := endp;
  1289. endp.next := hp;
  1290. if assigned(hp.next) then
  1291. hp.next.previous := hp;
  1292. {$endif replaceregdebug}
  1293. replaceReg := true;
  1294. returnEndP := endP;
  1295. getNextInstruction(p,hp);
  1296. stateChanged := false;
  1297. while hp <> endP do
  1298. begin
  1299. if {not(ptaiprop(hp.optinfo)^.canBeRemoved) and }
  1300. (hp.typ = ait_instruction) then
  1301. stateChanged :=
  1302. doReplaceReg(taicpu(hp),newsupreg,orgsupreg) or stateChanged;
  1303. if stateChanged then
  1304. updateStates(orgsupreg,newsupreg,hp,true);
  1305. getNextInstruction(hp,hp)
  1306. end;
  1307. if assigned(endp) and (endp.typ = ait_instruction) then
  1308. readStateChanged :=
  1309. doReplaceReadReg(taicpu(endP),newsupreg,orgsupreg);
  1310. if stateChanged or readStateChanged then
  1311. updateStates(orgsupreg,newsupreg,endP,stateChanged);
  1312. if stateChanged or readStateChanged then
  1313. updateState(orgsupreg,endP);
  1314. { We replaced newreg with oldreg between p and endp, so restore the contents }
  1315. { of newreg there with its contents from before the sequence. }
  1316. if removeLast or
  1317. RegLoadedWithNewValue(newsupreg,true,endP) then
  1318. GetLastInstruction(endP,hp)
  1319. else hp := endP;
  1320. RestoreRegContentsTo(newsupreg,c,seqstart,hp);
  1321. { Ot is possible that the new register was modified (e.g. an add/sub), so if }
  1322. { it was replaced by oldreg in that instruction, oldreg's contents have been }
  1323. { changed. To take this into account, we simply set the contents of orgsupreg }
  1324. { to "unknown" after this sequence }
  1325. if newRegModified then
  1326. ClearRegContentsFrom(orgsupreg,p,hp);
  1327. if removeLast then
  1328. ptaiprop(endp.optinfo)^.canBeRemoved := true;
  1329. allocRegBetween(asml,newreg(R_INTREGISTER,orgsupreg,R_SUBWHOLE),p,endP,ptaiprop(p.optinfo)^.usedregs);
  1330. end
  1331. {$ifdef replaceregdebug}
  1332. else
  1333. begin
  1334. hp := tai_comment.Create(strpnew(
  1335. 'replacing '+std_regname(newreg(R_INTREGISTER,newsupreg,R_SUBWHOLE))+' with '+std_regname(newreg(R_INTREGISTER,orgsupreg,R_SUBWHOLE))+
  1336. ' from here...'));
  1337. hp.previous := p.previous;
  1338. hp.next := p;
  1339. p.previous := hp;
  1340. if assigned(hp.previous) then
  1341. hp.previous.next := hp;
  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. ' failed here'));
  1345. hp.next := endp.next;
  1346. hp.previous := endp;
  1347. endp.next := hp;
  1348. if assigned(hp.next) then
  1349. hp.next.previous := hp;
  1350. end;
  1351. {$endif replaceregdebug}
  1352. end;
  1353. function FindRegWithConst(p: tai; size: topsize; l: aint; var Res: TRegister): Boolean;
  1354. {Finds a register which contains the constant l}
  1355. var
  1356. Counter: tsuperregister;
  1357. {$ifdef testing}
  1358. hp: tai;
  1359. {$endif testing}
  1360. begin
  1361. Result:=false;
  1362. Counter := RS_EAX;
  1363. repeat
  1364. {$ifdef testing}
  1365. if (ptaiprop(p.optinfo)^.regs[counter].typ in [con_const,con_noRemoveConst]) then
  1366. begin
  1367. hp := tai_comment.Create(strpnew(
  1368. 'checking const load of '+tostr(l)+' here...'));
  1369. hp.next := ptaiprop(p.optinfo)^.Regs[Counter].StartMod;
  1370. hp.previous := ptaiprop(p.optinfo)^.Regs[Counter].StartMod^.previous;
  1371. ptaiprop(p.optinfo)^.Regs[Counter].StartMod^.previous := hp;
  1372. if assigned(hp.previous) then
  1373. hp.previous.next := hp;
  1374. end;
  1375. {$endif testing}
  1376. if (ptaiprop(p.optinfo)^.regs[counter].typ in [con_const,con_noRemoveConst]) and
  1377. (taicpu(ptaiprop(p.optinfo)^.Regs[Counter].StartMod).opsize = size) and
  1378. (taicpu(ptaiprop(p.optinfo)^.Regs[Counter].StartMod).oper[0]^.typ = top_const) and
  1379. (taicpu(ptaiprop(p.optinfo)^.Regs[Counter].StartMod).oper[0]^.val = l) then
  1380. begin
  1381. res:=taicpu(ptaiprop(p.optinfo)^.Regs[Counter].StartMod).oper[1]^.reg;
  1382. result:=true;
  1383. exit;
  1384. end;
  1385. inc(counter);
  1386. until (Counter > RS_EDI);
  1387. end;
  1388. procedure removePrevNotUsedLoad(p: tai; supreg: tsuperregister; check: boolean);
  1389. { if check = true, it means the procedure has to check whether it isn't }
  1390. { possible that the contents are still used after p (used when removing }
  1391. { instructions because of a "call"), otherwise this is not necessary }
  1392. { (e.g. when you have a "mov 8(%ebp),%eax", you can be sure the previous }
  1393. { value of %eax isn't used anymore later on) }
  1394. var
  1395. hp1, next, beforestartmod: tai;
  1396. begin
  1397. if getLastInstruction(p,hp1) then
  1398. with ptaiprop(hp1.optinfo)^.regs[supreg] do
  1399. if (typ in [con_ref,con_invalid,con_const]) and
  1400. (nrofMods = 1) and
  1401. (rState = ptaiprop(startmod.optinfo)^.regs[supreg].rState) and
  1402. (not(check) or
  1403. (not(regInInstruction(supreg,p)) and
  1404. (not(supreg in ptaiprop(hp1.optinfo)^.usedRegs) or
  1405. findRegDealloc(supreg,p)))) then
  1406. begin
  1407. ptaiprop(startMod.optinfo)^.canBeRemoved := true;
  1408. getnextinstruction(p,next);
  1409. { give the register that was modified by this instruction again }
  1410. { the contents it had before this instruction }
  1411. if getlastinstruction(startmod,beforestartmod) then
  1412. RestoreRegContentsTo(supreg,ptaiprop(beforestartmod.optinfo)^.regs[supreg],
  1413. startmod,p)
  1414. else
  1415. ClearRegContentsFrom(supreg,startmod,hp1);
  1416. end;
  1417. end;
  1418. {$ifdef notused}
  1419. function is_mov_for_div(p: taicpu): boolean;
  1420. begin
  1421. is_mov_for_div :=
  1422. (p.opcode = A_MOV) and
  1423. (p.oper[0]^.typ = top_const) and
  1424. (p.oper[1]^.typ = top_reg) and
  1425. (p.oper[1]^.reg = RS_EDX) and
  1426. getNextInstruction(p,p) and
  1427. (p.typ = ait_instruction) and
  1428. ((p.opcode = A_DIV) or
  1429. (p.opcode = A_IDIV));
  1430. end;
  1431. {$endif notused}
  1432. function memtoreg(t: taicpu; const ref: treference; var startp: tai): tregister;
  1433. var
  1434. hp: tai;
  1435. p: ptaiprop;
  1436. regcounter: tsuperregister;
  1437. optimizable: boolean;
  1438. begin
  1439. if not getlastinstruction(t,hp) or
  1440. not issimplememloc(ref) then
  1441. begin
  1442. memtoreg := NR_NO;
  1443. exit;
  1444. end;
  1445. p := ptaiprop(hp.optinfo);
  1446. optimizable := false;
  1447. for regcounter := RS_EAX to RS_EDI do
  1448. begin
  1449. if (assigned(p^.regs[regcounter].memwrite) and
  1450. refsequal(ref,p^.regs[regcounter].memwrite.oper[1]^.ref^)) then
  1451. begin
  1452. optimizable := true;
  1453. hp := p^.regs[regcounter].memwrite;
  1454. end
  1455. else if ((p^.regs[regcounter].typ in [CON_REF,CON_NOREMOVEREF]) and
  1456. (p^.regs[regcounter].nrofmods = 1) and
  1457. ((taicpu(p^.regs[regcounter].startmod).opcode = A_MOV) or
  1458. (taicpu(p^.regs[regcounter].startmod).opcode = A_MOVZX) or
  1459. (taicpu(p^.regs[regcounter].startmod).opcode = A_MOVSX)) and
  1460. (taicpu(p^.regs[regcounter].startmod).oper[0]^.typ = top_ref) and
  1461. refsequal(ref,taicpu(p^.regs[regcounter].startmod).oper[0]^.ref^)) then
  1462. begin
  1463. optimizable := true;
  1464. hp := p^.regs[regcounter].startmod;
  1465. end;
  1466. if optimizable then
  1467. if ((t.opsize <> S_B) or
  1468. not(regcounter in [RS_ESI,RS_EDI])) and
  1469. sizescompatible(taicpu(hp).opsize,t.opsize) then
  1470. begin
  1471. case t.opsize of
  1472. S_B:
  1473. begin
  1474. memtoreg := newreg(R_INTREGISTER,regcounter,R_SUBL)
  1475. end;
  1476. S_W,S_BW:
  1477. begin
  1478. memtoreg := newreg(R_INTREGISTER,regcounter,R_SUBW);
  1479. if (t.opsize = S_BW) then
  1480. begin
  1481. t.opcode := A_MOV;
  1482. t.opsize := S_W;
  1483. end;
  1484. end;
  1485. S_L,S_BL,S_WL:
  1486. begin
  1487. memtoreg := newreg(R_INTREGISTER,regcounter,R_SUBWHOLE);
  1488. if (t.opsize <> S_L) then
  1489. begin
  1490. t.opcode := A_MOV;
  1491. t.opsize := S_L;
  1492. end;
  1493. end;
  1494. end;
  1495. startp := hp;
  1496. exit;
  1497. end;
  1498. end;
  1499. memtoreg := NR_NO;
  1500. end;
  1501. procedure removeLocalStores(const t1: tai);
  1502. {var
  1503. p: tai;
  1504. regcount: tregister; }
  1505. begin
  1506. {
  1507. for regcount := LoGPReg to HiGPReg do
  1508. if assigned(pTaiProp(t1.optinfo)^.regs[regcount].memwrite) and
  1509. (taicpu(pTaiProp(t1.optinfo)^.regs[regcount].memwrite).oper[1]^.ref^.base
  1510. = current_procinfo.framepointer) then
  1511. begin
  1512. pTaiProp(pTaiProp(t1.optinfo)^.regs[regcount].memwrite.optinfo)^.canberemoved := true;
  1513. clearmemwrites(pTaiProp(t1.optinfo)^.regs[regcount].memwrite,regcount);
  1514. end;
  1515. }
  1516. end;
  1517. procedure loadcseregs(asml: taasmoutput; const reginfo: toptreginfo; curseqend, prevseqstart, curseqstart, curprev: tai; cnt: longint);
  1518. var
  1519. regsloaded: tregset;
  1520. regloads, reguses: array[RS_EAX..RS_EDI] of tai;
  1521. regcounter, substreg: tsuperregister;
  1522. hp, hp2: tai;
  1523. insertpos, prevseq_next: tai;
  1524. i: longint;
  1525. opc: tasmop;
  1526. begin
  1527. regsloaded := [];
  1528. fillchar(regloads,sizeof(regloads),0);
  1529. fillchar(reguses,sizeof(reguses),0);
  1530. getnextinstruction(prevseqstart,prevseq_next);
  1531. for regcounter := RS_EAX To RS_EDI do
  1532. if (reginfo.new2oldreg[regcounter] <> RS_INVALID) Then
  1533. begin
  1534. include(regsloaded,regcounter);
  1535. if assigned(ptaiprop(prevseqstart.optinfo)^.Regs[reginfo.new2oldreg[regcounter]].StartMod) then
  1536. AllocRegBetween(asml,newreg(R_INTREGISTER,reginfo.new2oldreg[regcounter],R_SUBWHOLE),
  1537. ptaiprop(prevseqstart.optinfo)^.Regs[reginfo.new2oldreg[regcounter]].StartMod,curseqstart,
  1538. ptaiprop(ptaiprop(prevseqstart.optinfo)^.Regs[reginfo.new2oldreg[regcounter]].StartMod.optinfo)^.usedregs)
  1539. else
  1540. AllocRegBetween(asml,newreg(R_INTREGISTER,reginfo.new2oldreg[regcounter],R_SUBWHOLE),
  1541. prevseqstart,curseqstart,ptaiprop(prevseqstart.optinfo)^.usedregs);
  1542. if curprev <> prevseqstart then
  1543. begin
  1544. if assigned(reginfo.lastReload[regCounter]) then
  1545. getLastInstruction(reginfo.lastReload[regCounter],hp)
  1546. else if assigned(reginfo.lastReload[reginfo.new2oldreg[regCounter]]) then
  1547. getLastInstruction(reginfo.lastReload[reginfo.new2OldReg[regCounter]],hp)
  1548. else
  1549. hp := curprev;
  1550. clearRegContentsFrom(regCounter,prevSeq_next,hp);
  1551. getnextInstruction(hp,hp);
  1552. allocRegBetween(asml,newreg(R_INTREGISTER,regCounter,R_SUBWHOLE),prevseqstart,hp,
  1553. ptaiprop(prevseqstart.optinfo)^.usedregs);
  1554. end;
  1555. if not(regcounter in reginfo.RegsLoadedforRef) and
  1556. {old reg new reg}
  1557. (reginfo.new2oldreg[regcounter] <> regcounter) then
  1558. begin
  1559. getLastInstruction(curseqend,hp);
  1560. if (curprev <> prevseqstart) or
  1561. {not(regCounter in rg.usableregsint + [RS_EDI,RS_ESI]) or}
  1562. not(regCounter in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_EDI,RS_ESI]) or
  1563. not ReplaceReg(asml,reginfo.new2oldreg[regcounter],
  1564. regCounter,hp,curseqstart,
  1565. ptaiprop(prevseqstart.optinfo)^.Regs[regCounter],true,hp2) then
  1566. begin
  1567. opc := A_MOV;
  1568. insertpos := prevseq_next;
  1569. if assigned(reguses[regcounter]) then
  1570. if assigned(regloads[reginfo.new2oldreg[regcounter]]) then
  1571. opc := A_XCHG
  1572. else
  1573. insertpos := tai(reguses[regcounter].next)
  1574. else
  1575. if assigned(regloads[reginfo.new2oldreg[regcounter]]) then
  1576. insertpos := regloads[reginfo.new2oldreg[regcounter]];
  1577. hp := Tai_Marker.Create(NoPropInfoStart);
  1578. InsertLLItem(asml, insertpos.previous,insertpos, hp);
  1579. hp2 := taicpu.Op_Reg_Reg(opc, S_L,
  1580. {old reg new reg}
  1581. newreg(R_INTREGISTER,reginfo.new2oldreg[regcounter],R_SUBWHOLE), newreg(R_INTREGISTER,regcounter,R_SUBWHOLE));
  1582. regloads[regcounter] := hp2;
  1583. reguses[reginfo.new2oldreg[regcounter]] := hp2;
  1584. new(ptaiprop(hp2.optinfo));
  1585. ptaiprop(hp2.optinfo)^ := ptaiprop(insertpos.optinfo)^;
  1586. ptaiprop(hp2.optinfo)^.canBeRemoved := false;
  1587. InsertLLItem(asml, insertpos.previous, insertpos, hp2);
  1588. hp := Tai_Marker.Create(NoPropInfoEnd);
  1589. InsertLLItem(asml, insertpos.previous, insertpos, hp);
  1590. { adjusts states in previous instruction so that it will }
  1591. { definitely be different from the previous or next state }
  1592. incstate(ptaiprop(hp2.optinfo)^.
  1593. regs[reginfo.new2oldreg[regcounter]].rstate,20);
  1594. incstate(ptaiprop(hp2.optinfo)^.
  1595. regs[regCounter].wstate,20);
  1596. updateState(reginfo.new2oldreg[regcounter],hp2);
  1597. updateState(regcounter,hp2);
  1598. end
  1599. else
  1600. begin
  1601. // replace the new register with the old register in the
  1602. // sequence itself as well so later comparisons get the
  1603. // correct knowledge about which registers are used
  1604. hp2 := curseqstart;
  1605. // curseqend = instruction following last instruction of this
  1606. // sequence
  1607. while hp2 <> curseqend do
  1608. begin
  1609. doreplacereg(taicpu(hp2),regcounter,reginfo.new2oldreg[regcounter]);
  1610. getnextinstruction(hp2,hp2);
  1611. end;
  1612. end;
  1613. end
  1614. else
  1615. { imagine the following code: }
  1616. { normal wrong optimized }
  1617. { movl 8(%ebp), %eax movl 8(%ebp), %eax }
  1618. { movl (%eax), %eax movl (%eax), %eax }
  1619. { cmpl 8(%ebp), %eax cmpl 8(%ebp), %eax }
  1620. { jne l1 jne l1 }
  1621. { movl 8(%ebp), %eax }
  1622. { movl (%eax), %edi movl %eax, %edi }
  1623. { movl %edi, -4(%ebp) movl %edi, -4(%ebp) }
  1624. { movl 8(%ebp), %eax }
  1625. { pushl 70(%eax) pushl 70(%eax) }
  1626. { }
  1627. { The error is that at the moment that the last instruction is executed, }
  1628. { %eax doesn't contain 8(%ebp) anymore. Solution: the contents of }
  1629. { registers that are completely removed from a sequence (= registers in }
  1630. { RegLoadedforRef), have to be changed to their contents from before the }
  1631. { sequence. }
  1632. { if regcounter in reginfo.RegsLoadedforRef then }
  1633. begin
  1634. hp := curseqstart;
  1635. { cnt still holds the number of instructions }
  1636. { of the sequence, so go to the end of it }
  1637. for i := 1 to pred(cnt) do
  1638. getNextInstruction(hp,hp);
  1639. { curprev = instruction prior to start of sequence }
  1640. restoreRegContentsTo(regCounter,
  1641. ptaiprop(curprev.optinfo)^.Regs[regcounter],
  1642. curseqstart,hp);
  1643. end;
  1644. end;
  1645. end;
  1646. procedure replaceoperandwithreg(asml: taasmoutput; p: tai; opnr: byte; reg: tregister);
  1647. var
  1648. hp: tai;
  1649. begin
  1650. { new instruction -> it's info block is not in the big one allocated at the start }
  1651. hp := Tai_Marker.Create(NoPropInfoStart);
  1652. InsertLLItem(asml, p.previous,p, hp);
  1653. { duplicate the original instruction and replace it's designated operant with the register }
  1654. hp := tai(p.getcopy);
  1655. taicpu(hp).loadreg(opnr,reg);
  1656. { add optimizer state info }
  1657. new(ptaiprop(hp.optinfo));
  1658. ptaiprop(hp.optinfo)^ := ptaiprop(p.optinfo)^;
  1659. { new instruction can not be removed }
  1660. ptaiprop(hp.optinfo)^.canBeRemoved := false;
  1661. { but the old one can }
  1662. ptaiprop(p.optinfo)^.canBeRemoved := true;
  1663. { insert end marker }
  1664. InsertLLItem(asml, p.previous, p, hp);
  1665. hp := Tai_Marker.Create(NoPropInfoEnd);
  1666. InsertLLItem(asml, p.previous, p, hp);
  1667. end;
  1668. procedure doCSE(asml: TAAsmOutput; First, Last: tai; findPrevSeqs, doSubOpts: boolean);
  1669. {marks the instructions that can be removed by RemoveInstructs. They're not
  1670. removed immediately because sometimes an instruction needs to be checked in
  1671. two different sequences}
  1672. var cnt, cnt2, {cnt3,} orgNrofMods: longint;
  1673. p, hp1, hp2, prevSeq: tai;
  1674. hp3, hp4: tai;
  1675. hp5 : tai;
  1676. reginfo: toptreginfo;
  1677. memreg: tregister;
  1678. regcounter: tsuperregister;
  1679. begin
  1680. p := First;
  1681. SkipHead(p);
  1682. while (p <> Last) do
  1683. begin
  1684. case p.typ of
  1685. ait_align:
  1686. if not(tai_align(p).use_op) then
  1687. SetAlignReg(p);
  1688. ait_instruction:
  1689. begin
  1690. case taicpu(p).opcode of
  1691. {
  1692. Does not work anymore with register calling because the registers are
  1693. released before the call
  1694. A_CALL:
  1695. for regCounter := RS_EAX to RS_EBX do
  1696. removePrevNotUsedLoad(p,regCounter,true);
  1697. }
  1698. A_CLD: if GetLastInstruction(p, hp1) and
  1699. (ptaiprop(hp1.optinfo)^.DirFlag = F_NotSet) then
  1700. ptaiprop(tai(p).optinfo)^.CanBeRemoved := True;
  1701. A_LEA, A_MOV, A_MOVZX, A_MOVSX:
  1702. begin
  1703. hp2 := p;
  1704. case taicpu(p).oper[0]^.typ of
  1705. top_ref, top_reg:
  1706. if (taicpu(p).oper[1]^.typ = top_reg) then
  1707. begin
  1708. With ptaiprop(p.optinfo)^.Regs[getsupreg(taicpu(p).oper[1]^.reg)] do
  1709. begin
  1710. if (startmod = p) then
  1711. orgNrofMods := nrofMods
  1712. else
  1713. orgNrofMods := 0;
  1714. if (p = StartMod) and
  1715. GetLastInstruction (p, hp1) and
  1716. not(hp1.typ in [ait_marker,ait_label]) then
  1717. {so we don't try to check a sequence when p is the first instruction of the block}
  1718. begin
  1719. {$ifdef csdebug}
  1720. hp5 := tai_comment.Create(strpnew(
  1721. 'cse checking '+std_regname(taicpu(p).oper[1]^.reg)));
  1722. insertLLItem(asml,p,p.next,hp5);
  1723. {$endif csdebug}
  1724. if CheckSequence(p,prevSeq,getsupreg(taicpu(p).oper[1]^.reg), Cnt, reginfo, findPrevSeqs) and
  1725. (Cnt > 0) then
  1726. begin
  1727. (*
  1728. hp1 := nil;
  1729. { although it's perfectly ok to remove an instruction which doesn't contain }
  1730. { the register that we've just checked (CheckSequence takes care of that), }
  1731. { the sequence containing this other register should also be completely }
  1732. { checked and removed, otherwise we may get situations like this: }
  1733. { }
  1734. { movl 12(%ebp), %edx movl 12(%ebp), %edx }
  1735. { movl 16(%ebp), %eax movl 16(%ebp), %eax }
  1736. { movl 8(%edx), %edx movl 8(%edx), %edx }
  1737. { movl (%eax), eax movl (%eax), eax }
  1738. { cmpl %eax, %edx cmpl %eax, %edx }
  1739. { jnz l123 getting converted to jnz l123 }
  1740. { movl 12(%ebp), %edx movl 4(%eax), eax }
  1741. { movl 16(%ebp), %eax }
  1742. { movl 8(%edx), %edx }
  1743. { movl 4(%eax), eax }
  1744. *)
  1745. { not anymore: if the start of a new sequence is found while checking (e.g. }
  1746. { above that of eax while checking edx, this new sequence is automatically }
  1747. { also checked }
  1748. Cnt2 := 1;
  1749. while Cnt2 <= Cnt do
  1750. begin
  1751. {$ifndef noremove}
  1752. ptaiprop(p.optinfo)^.CanBeRemoved := True
  1753. {$endif noremove}
  1754. ; inc(Cnt2);
  1755. GetNextInstruction(p, p);
  1756. end;
  1757. {hp4 is used to get the contents of the registers before the sequence}
  1758. GetLastInstruction(hp2, hp4);
  1759. {$IfDef CSDebug}
  1760. for regcounter := RS_EAX To RS_EDI do
  1761. if (regcounter in reginfo.RegsLoadedforRef) then
  1762. begin
  1763. hp5 := tai_comment.Create(strpnew('New: '+std_regname(newreg(R_INTREGISTER,regcounter,R_SUBNONE))+', Old: '+
  1764. std_regname(newreg(R_INTREGISTER,reginfo.new2oldreg[regcounter],R_SUBNONE))));
  1765. InsertLLItem(asml, tai(hp2.previous), hp2, hp5);
  1766. end;
  1767. {$EndIf CSDebug}
  1768. { if some registers were different in the old and the new sequence, move }
  1769. { the contents of those old registers to the new ones }
  1770. loadcseregs(asml,reginfo,p,prevseq,hp2,hp4,cnt);
  1771. continue;
  1772. end
  1773. end;
  1774. end;
  1775. { try to replace the new reg with the old reg }
  1776. if not(ptaiprop(p.optinfo)^.canBeRemoved) then
  1777. if (taicpu(p).oper[0]^.typ = top_reg) and
  1778. (taicpu(p).oper[1]^.typ = top_reg) and
  1779. { only remove if we're not storing something in a regvar }
  1780. (getsupreg(taicpu(p).oper[1]^.reg) in [RS_EAX,RS_EBX,RS_ECX,RS_EDX,RS_ESI,RS_EDI]) and
  1781. { (taicpu(p).oper[1]^.reg in (rg.usableregsint+[RS_EDI])) and}
  1782. (taicpu(p).opcode = A_MOV) and
  1783. getLastInstruction(p,hp4) and
  1784. { we only have to start replacing from the instruction after the mov, }
  1785. { but replacereg only starts with getnextinstruction(p,p) }
  1786. replaceReg(asml,getsupreg(taicpu(p).oper[0]^.reg),
  1787. getsupreg(taicpu(p).oper[1]^.reg),p,p,
  1788. ptaiprop(hp4.optinfo)^.regs[getsupreg(taicpu(p).oper[1]^.reg)],false,hp1) then
  1789. begin
  1790. ptaiprop(p.optinfo)^.canBeRemoved := true;
  1791. { this is just a regular move that was here, so the source register should be }
  1792. { allocated already at this point -> only allocate from here onwards }
  1793. if not(getsupreg(taicpu(p).oper[0]^.reg) in pTaiProp(p.optinfo)^.usedregs) then
  1794. internalerror(2004101011);
  1795. allocRegBetween(asml,taicpu(p).oper[0]^.reg,
  1796. p,hp1,pTaiProp(p.optinfo)^.usedregs)
  1797. end
  1798. else
  1799. begin
  1800. if (taicpu(p).oper[1]^.typ = top_reg) and
  1801. not regInOp(getsupreg(taicpu(p).oper[1]^.reg),taicpu(p).oper[0]^) then
  1802. removePrevNotUsedLoad(p,getsupreg(taicpu(p).oper[1]^.reg),false);
  1803. if doSubOpts and
  1804. (taicpu(p).opcode <> A_LEA) and
  1805. (taicpu(p).oper[0]^.typ = top_ref) then
  1806. begin
  1807. memreg :=
  1808. memtoreg(taicpu(p),
  1809. taicpu(p).oper[0]^.ref^,hp5);
  1810. if memreg <> NR_NO then
  1811. if (taicpu(p).opcode = A_MOV) and
  1812. (taicpu(p).oper[1]^.typ = top_reg) and
  1813. (taicpu(p).oper[1]^.reg = memreg) then
  1814. begin
  1815. pTaiProp(p.optinfo)^.canberemoved := true;
  1816. allocregbetween(asml,memreg,hp5,p,ptaiprop(hp5.optinfo)^.usedregs);
  1817. end
  1818. else
  1819. begin
  1820. replaceoperandwithreg(asml,p,0,memreg);
  1821. allocregbetween(asml,memreg,hp5,p,ptaiprop(hp5.optinfo)^.usedregs);
  1822. regcounter := getsupreg(memreg);
  1823. incstate(pTaiProp(p.optinfo)^.regs[regcounter].rstate,1);
  1824. updatestate(regcounter,p);
  1825. end;
  1826. end;
  1827. end;
  1828. { at first, only try optimizations of large blocks, because doing }
  1829. { doing smaller ones may prevent bigger ones from completing in }
  1830. { in the next pass }
  1831. if not doSubOpts and (orgNrofMods <> 0) then
  1832. begin
  1833. p := hp2;
  1834. for cnt := 1 to pred(orgNrofMods) do
  1835. getNextInstruction(p,p);
  1836. end;
  1837. end;
  1838. top_Const:
  1839. begin
  1840. case taicpu(p).oper[1]^.typ of
  1841. Top_Reg:
  1842. begin
  1843. regCounter := getsupreg(taicpu(p).oper[1]^.reg);
  1844. if GetLastInstruction(p, hp1) then
  1845. With ptaiprop(hp1.optinfo)^.Regs[regCounter] do
  1846. if (typ in [con_const,con_noRemoveConst]) and
  1847. (taicpu(startMod).opsize >= taicpu(p).opsize) and
  1848. opsequal(taicpu(StartMod).oper[0]^,taicpu(p).oper[0]^) then
  1849. begin
  1850. ptaiprop(p.optinfo)^.CanBeRemoved := True;
  1851. allocRegBetween(asml,taicpu(p).oper[1]^.reg,startmod,p,
  1852. ptaiprop(startmod.optinfo)^.usedregs);
  1853. end
  1854. else
  1855. removePrevNotUsedLoad(p,getsupreg(taicpu(p).oper[1]^.reg),false);
  1856. end;
  1857. Top_Ref:
  1858. if (taicpu(p).oper[0]^.typ = top_const) and
  1859. getLastInstruction(p,hp1) and
  1860. findRegWithConst(hp1,taicpu(p).opsize,taicpu(p).oper[0]^.val,memreg) then
  1861. begin
  1862. taicpu(p).loadreg(0,memreg);
  1863. allocRegBetween(asml,memreg,
  1864. ptaiprop(hp1.optinfo)^.regs[getsupreg(memreg)].startMod,p,
  1865. ptaiprop(ptaiprop(hp1.optinfo)^.regs[getsupreg(memreg)].startMod.optinfo)^.usedregs);
  1866. end;
  1867. end;
  1868. end;
  1869. end;
  1870. end;
  1871. A_LEAVE:
  1872. begin
  1873. if getlastinstruction(p,hp1) then
  1874. removeLocalStores(hp1);
  1875. end;
  1876. A_STD: if GetLastInstruction(p, hp1) and
  1877. (ptaiprop(hp1.optinfo)^.DirFlag = F_Set) then
  1878. ptaiprop(tai(p).optinfo)^.CanBeRemoved := True;
  1879. else
  1880. begin
  1881. for cnt := 1 to maxinschanges do
  1882. begin
  1883. case InsProp[taicpu(p).opcode].Ch[cnt] of
  1884. Ch_ROp1:
  1885. if (taicpu(p).oper[0]^.typ = top_ref) and
  1886. ((taicpu(p).opcode < A_F2XM1) or
  1887. ((taicpu(p).opcode > A_IN) and
  1888. (taicpu(p).opcode < A_OUT)) or
  1889. (taicpu(p).opcode = A_PUSH) or
  1890. ((taicpu(p).opcode >= A_RCL) and
  1891. (taicpu(p).opcode <= A_XOR))) then
  1892. begin
  1893. memreg :=
  1894. memtoreg(taicpu(p),
  1895. taicpu(p).oper[0]^.ref^,hp5);
  1896. if memreg <> NR_NO then
  1897. begin
  1898. replaceoperandwithreg(asml,p,0,memreg);
  1899. allocregbetween(asml,memreg,hp5,p,ptaiprop(hp5.optinfo)^.usedregs);
  1900. regcounter := getsupreg(memreg);
  1901. incstate(pTaiProp(p.optinfo)^.regs[regcounter].rstate,1);
  1902. updatestate(regcounter,p);
  1903. end;
  1904. end;
  1905. Ch_MOp1:
  1906. if not(CS_LittleSize in aktglobalswitches) and
  1907. (taicpu(p).oper[0]^.typ = top_ref) then
  1908. begin
  1909. memreg :=
  1910. memtoreg(taicpu(p),
  1911. taicpu(p).oper[0]^.ref^,hp5);
  1912. if (memreg <> NR_NO) and
  1913. (not getNextInstruction(p,hp1) or
  1914. (RegLoadedWithNewValue(getsupreg(memreg),false,hp1) or
  1915. FindRegDealloc(getsupreg(memreg),hp1))) then
  1916. begin
  1917. hp1 := Tai_Marker.Create(NoPropInfoEnd);
  1918. insertllitem(asml,p,p.next,hp1);
  1919. hp1 := taicpu.op_reg_ref(A_MOV,reg2opsize(memreg),
  1920. memreg,taicpu(p).oper[0]^.ref^);
  1921. new(ptaiprop(hp1.optinfo));
  1922. pTaiProp(hp1.optinfo)^ := pTaiProp(p.optinfo)^;
  1923. insertllitem(asml,p,p.next,hp1);
  1924. regcounter := getsupreg(memreg);
  1925. incstate(pTaiProp(hp1.optinfo)^.regs[regcounter].rstate,1);
  1926. updatestate(regcounter,hp1);
  1927. hp1 := Tai_Marker.Create(NoPropInfoStart);
  1928. insertllitem(asml,p,p.next,hp1);
  1929. replaceoperandwithreg(asml,p,0,memreg);
  1930. allocregbetween(asml,memreg,hp5,
  1931. tai(p.next.next),ptaiprop(hp5.optinfo)^.usedregs);
  1932. ClearRegContentsFrom(regcounter,hp5,p);
  1933. end;
  1934. end;
  1935. Ch_ROp2:
  1936. if ((taicpu(p).opcode = A_CMP) or
  1937. (taicpu(p).opcode = A_TEST)) and
  1938. (taicpu(p).oper[1]^.typ = top_ref) then
  1939. begin
  1940. memreg :=
  1941. memtoreg(taicpu(p),
  1942. taicpu(p).oper[1]^.ref^,hp5);
  1943. if memreg <> NR_NO then
  1944. begin
  1945. replaceoperandwithreg(asml,p,1,memreg);
  1946. allocregbetween(asml,memreg,hp5,p,ptaiprop(hp5.optinfo)^.usedregs);
  1947. regcounter := getsupreg(memreg);
  1948. incstate(pTaiProp(p.optinfo)^.regs[regcounter].rstate,1);
  1949. updatestate(regcounter,p);
  1950. end;
  1951. end;
  1952. Ch_MOp2:
  1953. if not(cs_littlesize in aktglobalswitches) and
  1954. (taicpu(p).oper[1]^.typ = top_ref) and
  1955. ((taicpu(p).opcode < A_BT) or
  1956. ((taicpu(p).opcode > A_IN) and
  1957. (taicpu(p).opcode < A_OUT)) or
  1958. (taicpu(p).opcode = A_PUSH) or
  1959. ((taicpu(p).opcode >= A_RCL) and
  1960. (taicpu(p).opcode <= A_XOR))) then
  1961. begin
  1962. memreg :=
  1963. memtoreg(taicpu(p),
  1964. taicpu(p).oper[1]^.ref^,hp5);
  1965. if (memreg <> NR_NO) and
  1966. (not getNextInstruction(p,hp1) or
  1967. (RegLoadedWithNewValue(getsupreg(memreg),false,hp1) or
  1968. FindRegDealloc(getsupreg(memreg),hp1))) then
  1969. begin
  1970. hp1 := Tai_Marker.Create(NoPropInfoEnd);
  1971. insertllitem(asml,p,p.next,hp1);
  1972. hp1 := taicpu.op_reg_ref(A_MOV,reg2opsize(memreg),
  1973. memreg,taicpu(p).oper[1]^.ref^);
  1974. new(ptaiprop(hp1.optinfo));
  1975. pTaiProp(hp1.optinfo)^ := pTaiProp(p.optinfo)^;
  1976. insertllitem(asml,p,p.next,hp1);
  1977. regcounter := getsupreg(memreg);
  1978. incstate(pTaiProp(hp1.optinfo)^.regs[regcounter].rstate,1);
  1979. updatestate(regcounter,hp1);
  1980. hp1 := Tai_Marker.Create(NoPropInfoStart);
  1981. insertllitem(asml,p,p.next,hp1);
  1982. replaceoperandwithreg(asml,p,1,memreg);
  1983. allocregbetween(asml,memreg,hp5,
  1984. tai(p.next.next),ptaiprop(hp5.optinfo)^.usedregs);
  1985. ClearRegContentsFrom(regcounter,hp5,p);
  1986. end;
  1987. end;
  1988. end;
  1989. end;
  1990. end;
  1991. end
  1992. end;
  1993. end;
  1994. GetNextInstruction(p, p);
  1995. end;
  1996. end;
  1997. function removeInstructs(asml: TAAsmoutput; first, last: tai): boolean;
  1998. { Removes the marked instructions and disposes the PTaiProps of the other }
  1999. { instructions }
  2000. var
  2001. p, hp1: tai;
  2002. nopropinfolevel: longint;
  2003. begin
  2004. removeInstructs := false;
  2005. p := First;
  2006. nopropinfolevel := 0;
  2007. while (p <> Last) do
  2008. begin
  2009. if (p.typ = ait_marker) and
  2010. (Tai_marker(p).kind = noPropInfoStart) then
  2011. begin
  2012. hp1 := tai(p.next);
  2013. asml.remove(p);
  2014. p.free;
  2015. nopropinfolevel := 1;
  2016. while (nopropinfolevel <> 0) do
  2017. begin
  2018. p := tai(hp1.next);
  2019. {$ifndef noinstremove}
  2020. { allocregbetween can insert new ait_regalloc objects }
  2021. { without optinfo }
  2022. if (hp1.typ = ait_marker) then
  2023. begin
  2024. case Tai_marker(hp1).kind of
  2025. { they can be nested! }
  2026. noPropInfoStart: inc(nopropinfolevel);
  2027. noPropInfoEnd: dec(nopropinfolevel);
  2028. else
  2029. begin
  2030. hp1 := p;
  2031. continue;
  2032. end;
  2033. end;
  2034. asml.remove(hp1);
  2035. hp1.free;
  2036. end
  2037. else if assigned(hp1.optinfo) then
  2038. if ptaiprop(hp1.optinfo)^.canBeRemoved then
  2039. begin
  2040. dispose(ptaiprop(hp1.optinfo));
  2041. hp1.optinfo := nil;
  2042. asml.remove(hp1);
  2043. hp1.free;
  2044. end
  2045. else
  2046. {$endif noinstremove}
  2047. begin
  2048. dispose(ptaiprop(hp1.optinfo));
  2049. hp1.optinfo := nil;
  2050. end;
  2051. hp1 := p;
  2052. end;
  2053. end
  2054. else
  2055. {$ifndef noinstremove}
  2056. if assigned(p.optinfo) and
  2057. ptaiprop(p.optinfo)^.canBeRemoved then
  2058. begin
  2059. hp1 := tai(p.next);
  2060. asml.Remove(p);
  2061. p.free;
  2062. p := hp1;
  2063. removeInstructs := true;
  2064. end
  2065. else
  2066. {$endif noinstremove}
  2067. begin
  2068. p.optinfo := nil;
  2069. p := tai(p.next);;
  2070. end;
  2071. end;
  2072. end;
  2073. function CSE(asml: TAAsmOutput; First, Last: tai; pass: longint): boolean;
  2074. begin
  2075. doCSE(asml, First, Last, not(cs_slowoptimize in aktglobalswitches) or (pass >= 2),
  2076. not(cs_slowoptimize in aktglobalswitches) or (pass >= 1));
  2077. { register renaming }
  2078. if not(cs_slowoptimize in aktglobalswitches) or (pass > 0) then
  2079. doRenaming(asml, first, last);
  2080. cse := removeInstructs(asml, first, last);
  2081. end;
  2082. end.