csopt386.pas 88 KB

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