csopt386.pas 90 KB

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