csopt386.pas 88 KB

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