csopt386.pas 87 KB

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