rgobj.pas 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the base class for the register allocator
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {$i fpcdefs.inc}
  18. { Allow duplicate allocations, can be used to get the .s file written }
  19. { $define ALLOWDUPREG}
  20. unit rgobj;
  21. interface
  22. uses
  23. cutils, cpubase,
  24. aasmbase,aasmtai,aasmdata,aasmcpu,
  25. cclasses,globtype,cgbase,cgutils,
  26. cpuinfo
  27. ;
  28. type
  29. {
  30. The interference bitmap contains of 2 layers:
  31. layer 1 - 256*256 blocks with pointers to layer 2 blocks
  32. layer 2 - blocks of 32*256 (32 bytes = 256 bits)
  33. }
  34. Tinterferencebitmap2 = array[byte] of set of byte;
  35. Pinterferencebitmap2 = ^Tinterferencebitmap2;
  36. Tinterferencebitmap1 = array[byte] of Pinterferencebitmap2;
  37. pinterferencebitmap1 = ^tinterferencebitmap1;
  38. Tinterferencebitmap=class
  39. private
  40. maxx1,
  41. maxy1 : byte;
  42. fbitmap : pinterferencebitmap1;
  43. function getbitmap(x,y:tsuperregister):boolean;
  44. procedure setbitmap(x,y:tsuperregister;b:boolean);
  45. public
  46. constructor create;
  47. destructor destroy;override;
  48. property bitmap[x,y:tsuperregister]:boolean read getbitmap write setbitmap;default;
  49. end;
  50. Tmovelistheader=record
  51. count,
  52. maxcount,
  53. sorted_until : cardinal;
  54. end;
  55. Tmovelist=record
  56. header : Tmovelistheader;
  57. data : array[tsuperregister] of Tlinkedlistitem;
  58. end;
  59. Pmovelist=^Tmovelist;
  60. {In the register allocator we keep track of move instructions.
  61. These instructions are moved between five linked lists. There
  62. is also a linked list per register to keep track about the moves
  63. it is associated with. Because we need to determine quickly in
  64. which of the five lists it is we add anu enumeradtion to each
  65. move instruction.}
  66. Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
  67. ms_worklist_moves,ms_active_moves);
  68. Tmoveins=class(Tlinkedlistitem)
  69. moveset:Tmoveset;
  70. x,y:Tsuperregister;
  71. end;
  72. Treginfoflag=(ri_coalesced,ri_selected);
  73. Treginfoflagset=set of Treginfoflag;
  74. Treginfo=record
  75. live_start,
  76. live_end : Tai;
  77. subreg : tsubregister;
  78. alias : Tsuperregister;
  79. { The register allocator assigns each register a colour }
  80. colour : Tsuperregister;
  81. movelist : Pmovelist;
  82. adjlist : Psuperregisterworklist;
  83. degree : TSuperregister;
  84. flags : Treginfoflagset;
  85. end;
  86. Preginfo=^TReginfo;
  87. tspillreginfo = record
  88. spillreg : tregister;
  89. orgreg : tsuperregister;
  90. tempreg : tregister;
  91. regread,regwritten, mustbespilled: boolean;
  92. end;
  93. tspillregsinfo = array[0..3] of tspillreginfo;
  94. Tspill_temp_list=array[tsuperregister] of Treference;
  95. {#------------------------------------------------------------------
  96. This class implements the default register allocator. It is used by the
  97. code generator to allocate and free registers which might be valid
  98. across nodes. It also contains utility routines related to registers.
  99. Some of the methods in this class should be overriden
  100. by cpu-specific implementations.
  101. --------------------------------------------------------------------}
  102. trgobj=class
  103. preserved_by_proc : tcpuregisterset;
  104. used_in_proc : tcpuregisterset;
  105. constructor create(Aregtype:Tregistertype;
  106. Adefaultsub:Tsubregister;
  107. const Ausable:array of tsuperregister;
  108. Afirst_imaginary:Tsuperregister;
  109. Apreserved_by_proc:Tcpuregisterset);
  110. destructor destroy;override;
  111. {# Allocate a register. An internalerror will be generated if there is
  112. no more free registers which can be allocated.}
  113. function getregister(list:TAsmList;subreg:Tsubregister):Tregister;virtual;
  114. {# Get the register specified.}
  115. procedure getcpuregister(list:TAsmList;r:Tregister);virtual;
  116. procedure ungetcpuregister(list:TAsmList;r:Tregister);virtual;
  117. {# Get multiple registers specified.}
  118. procedure alloccpuregisters(list:TAsmList;const r:Tcpuregisterset);virtual;
  119. {# Free multiple registers specified.}
  120. procedure dealloccpuregisters(list:TAsmList;const r:Tcpuregisterset);virtual;
  121. function uses_registers:boolean;virtual;
  122. procedure add_reg_instruction(instr:Tai;r:tregister);
  123. procedure add_move_instruction(instr:Taicpu);
  124. {# Do the register allocation.}
  125. procedure do_register_allocation(list:TAsmList;headertai:tai);virtual;
  126. { Adds an interference edge.
  127. don't move this to the protected section, the arm cg requires to access this (FK) }
  128. procedure add_edge(u,v:Tsuperregister);
  129. { translates a single given imaginary register to it's real register }
  130. procedure translate_register(var reg : tregister);
  131. protected
  132. regtype : Tregistertype;
  133. { default subregister used }
  134. defaultsub : tsubregister;
  135. live_registers:Tsuperregisterworklist;
  136. { can be overriden to add cpu specific interferences }
  137. procedure add_cpu_interferences(p : tai);virtual;
  138. procedure add_constraints(reg:Tregister);virtual;
  139. function get_alias(n:Tsuperregister):Tsuperregister;
  140. function getregisterinline(list:TAsmList;subreg:Tsubregister):Tregister;
  141. procedure ungetregisterinline(list:TAsmList;r:Tregister);
  142. function get_spill_subreg(r : tregister) : tsubregister;virtual;
  143. function do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;virtual;
  144. procedure do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);virtual;
  145. procedure do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);virtual;
  146. function instr_spill_register(list:TAsmList;
  147. instr:taicpu;
  148. const r:Tsuperregisterset;
  149. const spilltemplist:Tspill_temp_list): boolean;virtual;
  150. private
  151. int_live_range_direction: TRADirection;
  152. {# First imaginary register.}
  153. first_imaginary : Tsuperregister;
  154. {# Highest register allocated until now.}
  155. reginfo : PReginfo;
  156. maxreginfo,
  157. maxreginfoinc,
  158. maxreg : Tsuperregister;
  159. usable_registers_cnt : word;
  160. usable_registers : array[0..maxcpuregister-1] of tsuperregister;
  161. ibitmap : Tinterferencebitmap;
  162. spillednodes,
  163. simplifyworklist,
  164. freezeworklist,
  165. spillworklist,
  166. coalescednodes,
  167. selectstack : tsuperregisterworklist;
  168. worklist_moves,
  169. active_moves,
  170. frozen_moves,
  171. coalesced_moves,
  172. constrained_moves : Tlinkedlist;
  173. extended_backwards,
  174. backwards_was_first : tsuperregisterset;
  175. {$ifdef EXTDEBUG}
  176. procedure writegraph(loopidx:longint);
  177. {$endif EXTDEBUG}
  178. {# Disposes of the reginfo array.}
  179. procedure dispose_reginfo;
  180. {# Prepare the register colouring.}
  181. procedure prepare_colouring;
  182. {# Clean up after register colouring.}
  183. procedure epilogue_colouring;
  184. {# Colour the registers; that is do the register allocation.}
  185. procedure colour_registers;
  186. procedure insert_regalloc_info(list:TAsmList;u:tsuperregister);
  187. procedure insert_regalloc_info_all(list:TAsmList);
  188. procedure generate_interference_graph(list:TAsmList;headertai:tai);
  189. { translates the registers in the given assembler list }
  190. procedure translate_registers(list:TAsmList);
  191. function spill_registers(list:TAsmList;headertai:tai):boolean;virtual;
  192. function getnewreg(subreg:tsubregister):tsuperregister;
  193. procedure add_edges_used(u:Tsuperregister);
  194. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  195. function move_related(n:Tsuperregister):boolean;
  196. procedure make_work_list;
  197. procedure sort_simplify_worklist;
  198. procedure enable_moves(n:Tsuperregister);
  199. procedure decrement_degree(m:Tsuperregister);
  200. procedure simplify;
  201. procedure add_worklist(u:Tsuperregister);
  202. function adjacent_ok(u,v:Tsuperregister):boolean;
  203. function conservative(u,v:Tsuperregister):boolean;
  204. procedure combine(u,v:Tsuperregister);
  205. procedure coalesce;
  206. procedure freeze_moves(u:Tsuperregister);
  207. procedure freeze;
  208. procedure select_spill;
  209. procedure assign_colours;
  210. procedure clear_interferences(u:Tsuperregister);
  211. procedure set_live_range_direction(dir: TRADirection);
  212. public
  213. property live_range_direction: TRADirection read int_live_range_direction write set_live_range_direction;
  214. end;
  215. const
  216. first_reg = 0;
  217. last_reg = high(tsuperregister)-1;
  218. maxspillingcounter = 20;
  219. implementation
  220. uses
  221. systems,fmodule,globals,
  222. verbose,tgobj,procinfo;
  223. procedure sort_movelist(ml:Pmovelist);
  224. {Ok, sorting pointers is silly, but it does the job to make Trgobj.combine
  225. faster.}
  226. var h,i,p:word;
  227. t:Tlinkedlistitem;
  228. begin
  229. with ml^ do
  230. begin
  231. if header.count<2 then
  232. exit;
  233. p:=1;
  234. while 2*p<header.count do
  235. p:=2*p;
  236. while p<>0 do
  237. begin
  238. for h:=p to header.count-1 do
  239. begin
  240. i:=h;
  241. t:=data[i];
  242. repeat
  243. if ptruint(data[i-p])<=ptruint(t) then
  244. break;
  245. data[i]:=data[i-p];
  246. dec(i,p);
  247. until i<p;
  248. data[i]:=t;
  249. end;
  250. p:=p shr 1;
  251. end;
  252. header.sorted_until:=header.count-1;
  253. end;
  254. end;
  255. {******************************************************************************
  256. tinterferencebitmap
  257. ******************************************************************************}
  258. constructor tinterferencebitmap.create;
  259. begin
  260. inherited create;
  261. maxx1:=1;
  262. getmem(fbitmap,sizeof(tinterferencebitmap1)*2);
  263. fillchar(fbitmap^,sizeof(tinterferencebitmap1)*2,0);
  264. end;
  265. destructor tinterferencebitmap.destroy;
  266. var i,j:byte;
  267. begin
  268. for i:=0 to maxx1 do
  269. for j:=0 to maxy1 do
  270. if assigned(fbitmap[i,j]) then
  271. dispose(fbitmap[i,j]);
  272. freemem(fbitmap);
  273. end;
  274. function tinterferencebitmap.getbitmap(x,y:tsuperregister):boolean;
  275. var
  276. page : pinterferencebitmap2;
  277. begin
  278. result:=false;
  279. if (x shr 8>maxx1) then
  280. exit;
  281. page:=fbitmap[x shr 8,y shr 8];
  282. result:=assigned(page) and
  283. ((x and $ff) in page^[y and $ff]);
  284. end;
  285. procedure tinterferencebitmap.setbitmap(x,y:tsuperregister;b:boolean);
  286. var
  287. x1,y1 : byte;
  288. begin
  289. x1:=x shr 8;
  290. y1:=y shr 8;
  291. if x1>maxx1 then
  292. begin
  293. reallocmem(fbitmap,sizeof(tinterferencebitmap1)*(x1+1));
  294. fillchar(fbitmap[maxx1+1],sizeof(tinterferencebitmap1)*(x1-maxx1),0);
  295. maxx1:=x1;
  296. end;
  297. if not assigned(fbitmap[x1,y1]) then
  298. begin
  299. if y1>maxy1 then
  300. maxy1:=y1;
  301. new(fbitmap[x1,y1]);
  302. fillchar(fbitmap[x1,y1]^,sizeof(tinterferencebitmap2),0);
  303. end;
  304. if b then
  305. include(fbitmap[x1,y1]^[y and $ff],(x and $ff))
  306. else
  307. exclude(fbitmap[x1,y1]^[y and $ff],(x and $ff));
  308. end;
  309. {******************************************************************************
  310. trgobj
  311. ******************************************************************************}
  312. constructor trgobj.create(Aregtype:Tregistertype;
  313. Adefaultsub:Tsubregister;
  314. const Ausable:array of tsuperregister;
  315. Afirst_imaginary:Tsuperregister;
  316. Apreserved_by_proc:Tcpuregisterset);
  317. var
  318. i : Tsuperregister;
  319. begin
  320. { empty super register sets can cause very strange problems }
  321. if high(Ausable)=-1 then
  322. internalerror(200210181);
  323. live_range_direction:=rad_forward;
  324. supregset_reset(extended_backwards,false,high(tsuperregister));
  325. supregset_reset(backwards_was_first,false,high(tsuperregister));
  326. first_imaginary:=Afirst_imaginary;
  327. maxreg:=Afirst_imaginary;
  328. regtype:=Aregtype;
  329. defaultsub:=Adefaultsub;
  330. preserved_by_proc:=Apreserved_by_proc;
  331. used_in_proc:=[];
  332. live_registers.init;
  333. { Get reginfo for CPU registers }
  334. maxreginfo:=first_imaginary;
  335. maxreginfoinc:=16;
  336. worklist_moves:=Tlinkedlist.create;
  337. reginfo:=allocmem(first_imaginary*sizeof(treginfo));
  338. for i:=0 to first_imaginary-1 do
  339. begin
  340. reginfo[i].degree:=high(tsuperregister);
  341. reginfo[i].alias:=RS_INVALID;
  342. end;
  343. { Usable registers }
  344. fillchar(usable_registers,sizeof(usable_registers),0);
  345. for i:=low(Ausable) to high(Ausable) do
  346. usable_registers[i]:=Ausable[i];
  347. usable_registers_cnt:=high(Ausable)+1;
  348. { Initialize Worklists }
  349. spillednodes.init;
  350. simplifyworklist.init;
  351. freezeworklist.init;
  352. spillworklist.init;
  353. coalescednodes.init;
  354. selectstack.init;
  355. end;
  356. destructor trgobj.destroy;
  357. begin
  358. spillednodes.done;
  359. simplifyworklist.done;
  360. freezeworklist.done;
  361. spillworklist.done;
  362. coalescednodes.done;
  363. selectstack.done;
  364. live_registers.done;
  365. worklist_moves.free;
  366. dispose_reginfo;
  367. end;
  368. procedure Trgobj.dispose_reginfo;
  369. var i:Tsuperregister;
  370. begin
  371. if reginfo<>nil then
  372. begin
  373. for i:=0 to maxreg-1 do
  374. with reginfo[i] do
  375. begin
  376. if adjlist<>nil then
  377. dispose(adjlist,done);
  378. if movelist<>nil then
  379. dispose(movelist);
  380. end;
  381. freemem(reginfo);
  382. reginfo:=nil;
  383. end;
  384. end;
  385. function trgobj.getnewreg(subreg:tsubregister):tsuperregister;
  386. var
  387. oldmaxreginfo : tsuperregister;
  388. begin
  389. result:=maxreg;
  390. inc(maxreg);
  391. if maxreg>=last_reg then
  392. Message(parser_f_too_complex_proc);
  393. if maxreg>=maxreginfo then
  394. begin
  395. oldmaxreginfo:=maxreginfo;
  396. { Prevent overflow }
  397. if maxreginfoinc>last_reg-maxreginfo then
  398. maxreginfo:=last_reg
  399. else
  400. begin
  401. inc(maxreginfo,maxreginfoinc);
  402. if maxreginfoinc<256 then
  403. maxreginfoinc:=maxreginfoinc*2;
  404. end;
  405. reallocmem(reginfo,maxreginfo*sizeof(treginfo));
  406. { Do we really need it to clear it ? At least for 1.0.x (PFV) }
  407. fillchar(reginfo[oldmaxreginfo],(maxreginfo-oldmaxreginfo)*sizeof(treginfo),0);
  408. end;
  409. reginfo[result].subreg:=subreg;
  410. end;
  411. function trgobj.getregister(list:TAsmList;subreg:Tsubregister):Tregister;
  412. begin
  413. {$ifdef EXTDEBUG}
  414. if reginfo=nil then
  415. InternalError(2004020901);
  416. {$endif EXTDEBUG}
  417. if defaultsub=R_SUBNONE then
  418. result:=newreg(regtype,getnewreg(R_SUBNONE),R_SUBNONE)
  419. else
  420. result:=newreg(regtype,getnewreg(subreg),subreg);
  421. end;
  422. function trgobj.uses_registers:boolean;
  423. begin
  424. result:=(maxreg>first_imaginary);
  425. end;
  426. procedure trgobj.ungetcpuregister(list:TAsmList;r:Tregister);
  427. begin
  428. if (getsupreg(r)>=first_imaginary) then
  429. InternalError(2004020901);
  430. list.concat(Tai_regalloc.dealloc(r,nil));
  431. end;
  432. procedure trgobj.getcpuregister(list:TAsmList;r:Tregister);
  433. var
  434. supreg:Tsuperregister;
  435. begin
  436. supreg:=getsupreg(r);
  437. if supreg>=first_imaginary then
  438. internalerror(2003121503);
  439. include(used_in_proc,supreg);
  440. list.concat(Tai_regalloc.alloc(r,nil));
  441. end;
  442. procedure trgobj.alloccpuregisters(list:TAsmList;const r:Tcpuregisterset);
  443. var i:Tsuperregister;
  444. begin
  445. for i:=0 to first_imaginary-1 do
  446. if i in r then
  447. getcpuregister(list,newreg(regtype,i,defaultsub));
  448. end;
  449. procedure trgobj.dealloccpuregisters(list:TAsmList;const r:Tcpuregisterset);
  450. var i:Tsuperregister;
  451. begin
  452. for i:=0 to first_imaginary-1 do
  453. if i in r then
  454. ungetcpuregister(list,newreg(regtype,i,defaultsub));
  455. end;
  456. procedure trgobj.do_register_allocation(list:TAsmList;headertai:tai);
  457. var
  458. spillingcounter:byte;
  459. endspill:boolean;
  460. begin
  461. { Insert regalloc info for imaginary registers }
  462. insert_regalloc_info_all(list);
  463. ibitmap:=tinterferencebitmap.create;
  464. generate_interference_graph(list,headertai);
  465. { Don't do the real allocation when -sr is passed }
  466. if (cs_no_regalloc in current_settings.globalswitches) then
  467. exit;
  468. {Do register allocation.}
  469. spillingcounter:=0;
  470. repeat
  471. prepare_colouring;
  472. colour_registers;
  473. epilogue_colouring;
  474. endspill:=true;
  475. if spillednodes.length<>0 then
  476. begin
  477. inc(spillingcounter);
  478. if spillingcounter>maxspillingcounter then
  479. begin
  480. {$ifdef EXTDEBUG}
  481. { Only exit here so the .s file is still generated. Assembling
  482. the file will still trigger an error }
  483. exit;
  484. {$else}
  485. internalerror(200309041);
  486. {$endif}
  487. end;
  488. endspill:=not spill_registers(list,headertai);
  489. end;
  490. until endspill;
  491. ibitmap.free;
  492. translate_registers(list);
  493. { we need the translation table for debugging info and verbose assembler output (FK)
  494. dispose_reginfo;
  495. }
  496. end;
  497. procedure trgobj.add_constraints(reg:Tregister);
  498. begin
  499. end;
  500. procedure trgobj.add_edge(u,v:Tsuperregister);
  501. {This procedure will add an edge to the virtual interference graph.}
  502. procedure addadj(u,v:Tsuperregister);
  503. begin
  504. with reginfo[u] do
  505. begin
  506. if adjlist=nil then
  507. new(adjlist,init);
  508. adjlist^.add(v);
  509. end;
  510. end;
  511. begin
  512. if (u<>v) and not(ibitmap[v,u]) then
  513. begin
  514. ibitmap[v,u]:=true;
  515. ibitmap[u,v]:=true;
  516. {Precoloured nodes are not stored in the interference graph.}
  517. if (u>=first_imaginary) then
  518. addadj(u,v);
  519. if (v>=first_imaginary) then
  520. addadj(v,u);
  521. end;
  522. end;
  523. procedure trgobj.add_edges_used(u:Tsuperregister);
  524. var i:word;
  525. begin
  526. with live_registers do
  527. if length>0 then
  528. for i:=0 to length-1 do
  529. add_edge(u,get_alias(buf^[i]));
  530. end;
  531. {$ifdef EXTDEBUG}
  532. procedure trgobj.writegraph(loopidx:longint);
  533. {This procedure writes out the current interference graph in the
  534. register allocator.}
  535. var f:text;
  536. i,j:Tsuperregister;
  537. begin
  538. assign(f,'igraph'+tostr(loopidx));
  539. rewrite(f);
  540. writeln(f,'Interference graph');
  541. writeln(f);
  542. write(f,' ');
  543. for i:=0 to 15 do
  544. for j:=0 to 15 do
  545. write(f,hexstr(i,1));
  546. writeln(f);
  547. write(f,' ');
  548. for i:=0 to 15 do
  549. write(f,'0123456789ABCDEF');
  550. writeln(f);
  551. for i:=0 to maxreg-1 do
  552. begin
  553. write(f,hexstr(i,2):4);
  554. for j:=0 to maxreg-1 do
  555. if ibitmap[i,j] then
  556. write(f,'*')
  557. else
  558. write(f,'-');
  559. writeln(f);
  560. end;
  561. close(f);
  562. end;
  563. {$endif EXTDEBUG}
  564. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  565. begin
  566. with reginfo[u] do
  567. begin
  568. if movelist=nil then
  569. begin
  570. getmem(movelist,sizeof(tmovelistheader)+60*sizeof(pointer));
  571. movelist^.header.maxcount:=60;
  572. movelist^.header.count:=0;
  573. movelist^.header.sorted_until:=0;
  574. end
  575. else
  576. begin
  577. if movelist^.header.count>=movelist^.header.maxcount then
  578. begin
  579. movelist^.header.maxcount:=movelist^.header.maxcount*2;
  580. reallocmem(movelist,sizeof(tmovelistheader)+movelist^.header.maxcount*sizeof(pointer));
  581. end;
  582. end;
  583. movelist^.data[movelist^.header.count]:=data;
  584. inc(movelist^.header.count);
  585. end;
  586. end;
  587. procedure trgobj.set_live_range_direction(dir: TRADirection);
  588. begin
  589. if (dir in [rad_backwards,rad_backwards_reinit]) then
  590. begin
  591. if (dir=rad_backwards_reinit) then
  592. supregset_reset(extended_backwards,false,high(tsuperregister));
  593. int_live_range_direction:=rad_backwards;
  594. { new registers may be allocated }
  595. supregset_reset(backwards_was_first,false,high(tsuperregister));
  596. end
  597. else
  598. int_live_range_direction:=rad_forward;
  599. end;
  600. procedure trgobj.add_reg_instruction(instr:Tai;r:tregister);
  601. var
  602. supreg : tsuperregister;
  603. begin
  604. supreg:=getsupreg(r);
  605. {$ifdef extdebug}
  606. if not (cs_no_regalloc in current_settings.globalswitches) and
  607. (supreg>=maxreginfo) then
  608. internalerror(200411061);
  609. {$endif extdebug}
  610. if supreg>=first_imaginary then
  611. with reginfo[supreg] do
  612. begin
  613. if (live_range_direction=rad_forward) then
  614. begin
  615. if not assigned(live_start) then
  616. live_start:=instr;
  617. live_end:=instr;
  618. end
  619. else
  620. begin
  621. if not supregset_in(extended_backwards,supreg) then
  622. begin
  623. supregset_include(extended_backwards,supreg);
  624. live_start := instr;
  625. if not assigned(live_end) then
  626. begin
  627. supregset_include(backwards_was_first,supreg);
  628. live_end := instr;
  629. end;
  630. end
  631. else
  632. begin
  633. if supregset_in(backwards_was_first,supreg) then
  634. live_end := instr;
  635. end
  636. end
  637. end;
  638. end;
  639. procedure trgobj.add_move_instruction(instr:Taicpu);
  640. {This procedure notifies a certain as a move instruction so the
  641. register allocator can try to eliminate it.}
  642. var i:Tmoveins;
  643. ssupreg,dsupreg:Tsuperregister;
  644. begin
  645. {$ifdef extdebug}
  646. if (instr.oper[O_MOV_SOURCE]^.typ<>top_reg) or
  647. (instr.oper[O_MOV_DEST]^.typ<>top_reg) then
  648. internalerror(200311291);
  649. {$endif}
  650. i:=Tmoveins.create;
  651. i.moveset:=ms_worklist_moves;
  652. worklist_moves.insert(i);
  653. ssupreg:=getsupreg(instr.oper[O_MOV_SOURCE]^.reg);
  654. add_to_movelist(ssupreg,i);
  655. dsupreg:=getsupreg(instr.oper[O_MOV_DEST]^.reg);
  656. if ssupreg<>dsupreg then
  657. {Avoid adding the same move instruction twice to a single register.}
  658. add_to_movelist(dsupreg,i);
  659. i.x:=ssupreg;
  660. i.y:=dsupreg;
  661. end;
  662. function trgobj.move_related(n:Tsuperregister):boolean;
  663. var i:cardinal;
  664. begin
  665. move_related:=false;
  666. with reginfo[n] do
  667. if movelist<>nil then
  668. with movelist^ do
  669. for i:=0 to header.count-1 do
  670. if Tmoveins(data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  671. begin
  672. move_related:=true;
  673. break;
  674. end;
  675. end;
  676. procedure Trgobj.sort_simplify_worklist;
  677. {Sorts the simplifyworklist by the number of interferences the
  678. registers in it cause. This allows simplify to execute in
  679. constant time.}
  680. var p,h,i,leni,lent:word;
  681. t:Tsuperregister;
  682. adji,adjt:Psuperregisterworklist;
  683. begin
  684. with simplifyworklist do
  685. begin
  686. if length<2 then
  687. exit;
  688. p:=1;
  689. while 2*p<length do
  690. p:=2*p;
  691. while p<>0 do
  692. begin
  693. for h:=p to length-1 do
  694. begin
  695. i:=h;
  696. t:=buf^[i];
  697. adjt:=reginfo[buf^[i]].adjlist;
  698. lent:=0;
  699. if adjt<>nil then
  700. lent:=adjt^.length;
  701. repeat
  702. adji:=reginfo[buf^[i-p]].adjlist;
  703. leni:=0;
  704. if adji<>nil then
  705. leni:=adji^.length;
  706. if leni<=lent then
  707. break;
  708. buf^[i]:=buf^[i-p];
  709. dec(i,p)
  710. until i<p;
  711. buf^[i]:=t;
  712. end;
  713. p:=p shr 1;
  714. end;
  715. end;
  716. end;
  717. procedure trgobj.make_work_list;
  718. var n:Tsuperregister;
  719. begin
  720. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  721. assign it to any of the registers, thus it is significant.}
  722. for n:=first_imaginary to maxreg-1 do
  723. with reginfo[n] do
  724. begin
  725. if adjlist=nil then
  726. degree:=0
  727. else
  728. degree:=adjlist^.length;
  729. if degree>=usable_registers_cnt then
  730. spillworklist.add(n)
  731. else if move_related(n) then
  732. freezeworklist.add(n)
  733. else
  734. simplifyworklist.add(n);
  735. end;
  736. sort_simplify_worklist;
  737. end;
  738. procedure trgobj.prepare_colouring;
  739. begin
  740. make_work_list;
  741. active_moves:=Tlinkedlist.create;
  742. frozen_moves:=Tlinkedlist.create;
  743. coalesced_moves:=Tlinkedlist.create;
  744. constrained_moves:=Tlinkedlist.create;
  745. selectstack.clear;
  746. end;
  747. procedure trgobj.enable_moves(n:Tsuperregister);
  748. var m:Tlinkedlistitem;
  749. i:cardinal;
  750. begin
  751. with reginfo[n] do
  752. if movelist<>nil then
  753. for i:=0 to movelist^.header.count-1 do
  754. begin
  755. m:=movelist^.data[i];
  756. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  757. if Tmoveins(m).moveset=ms_active_moves then
  758. begin
  759. {Move m from the set active_moves to the set worklist_moves.}
  760. active_moves.remove(m);
  761. Tmoveins(m).moveset:=ms_worklist_moves;
  762. worklist_moves.concat(m);
  763. end;
  764. end;
  765. end;
  766. procedure Trgobj.decrement_degree(m:Tsuperregister);
  767. var adj : Psuperregisterworklist;
  768. n : tsuperregister;
  769. d,i : word;
  770. begin
  771. with reginfo[m] do
  772. begin
  773. d:=degree;
  774. if d=0 then
  775. internalerror(200312151);
  776. dec(degree);
  777. if d=usable_registers_cnt then
  778. begin
  779. {Enable moves for m.}
  780. enable_moves(m);
  781. {Enable moves for adjacent.}
  782. adj:=adjlist;
  783. if adj<>nil then
  784. for i:=1 to adj^.length do
  785. begin
  786. n:=adj^.buf^[i-1];
  787. if reginfo[n].flags*[ri_selected,ri_coalesced]<>[] then
  788. enable_moves(n);
  789. end;
  790. {Remove the node from the spillworklist.}
  791. if not spillworklist.delete(m) then
  792. internalerror(200310145);
  793. if move_related(m) then
  794. freezeworklist.add(m)
  795. else
  796. simplifyworklist.add(m);
  797. end;
  798. end;
  799. end;
  800. procedure trgobj.simplify;
  801. var adj : Psuperregisterworklist;
  802. m,n : Tsuperregister;
  803. i : word;
  804. begin
  805. {We take the element with the least interferences out of the
  806. simplifyworklist. Since the simplifyworklist is now sorted, we
  807. no longer need to search, but we can simply take the first element.}
  808. m:=simplifyworklist.get;
  809. {Push it on the selectstack.}
  810. selectstack.add(m);
  811. with reginfo[m] do
  812. begin
  813. include(flags,ri_selected);
  814. adj:=adjlist;
  815. end;
  816. if adj<>nil then
  817. for i:=1 to adj^.length do
  818. begin
  819. n:=adj^.buf^[i-1];
  820. if (n>=first_imaginary) and
  821. (reginfo[n].flags*[ri_selected,ri_coalesced]=[]) then
  822. decrement_degree(n);
  823. end;
  824. end;
  825. function trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  826. begin
  827. while ri_coalesced in reginfo[n].flags do
  828. n:=reginfo[n].alias;
  829. get_alias:=n;
  830. end;
  831. procedure trgobj.add_worklist(u:Tsuperregister);
  832. begin
  833. if (u>=first_imaginary) and
  834. (not move_related(u)) and
  835. (reginfo[u].degree<usable_registers_cnt) then
  836. begin
  837. if not freezeworklist.delete(u) then
  838. internalerror(200308161); {must be found}
  839. simplifyworklist.add(u);
  840. end;
  841. end;
  842. function trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  843. {Check wether u and v should be coalesced. u is precoloured.}
  844. function ok(t,r:Tsuperregister):boolean;
  845. begin
  846. ok:=(t<first_imaginary) or
  847. (reginfo[t].degree<usable_registers_cnt) or
  848. ibitmap[r,t];
  849. end;
  850. var adj : Psuperregisterworklist;
  851. i : word;
  852. n : tsuperregister;
  853. begin
  854. with reginfo[v] do
  855. begin
  856. adjacent_ok:=true;
  857. adj:=adjlist;
  858. if adj<>nil then
  859. for i:=1 to adj^.length do
  860. begin
  861. n:=adj^.buf^[i-1];
  862. if (flags*[ri_coalesced,ri_selected]=[]) and not ok(n,u) then
  863. begin
  864. adjacent_ok:=false;
  865. break;
  866. end;
  867. end;
  868. end;
  869. end;
  870. function trgobj.conservative(u,v:Tsuperregister):boolean;
  871. var adj : Psuperregisterworklist;
  872. done : Tsuperregisterset; {To prevent that we count nodes twice.}
  873. i,k:word;
  874. n : tsuperregister;
  875. begin
  876. k:=0;
  877. supregset_reset(done,false,maxreg);
  878. with reginfo[u] do
  879. begin
  880. adj:=adjlist;
  881. if adj<>nil then
  882. for i:=1 to adj^.length do
  883. begin
  884. n:=adj^.buf^[i-1];
  885. if flags*[ri_coalesced,ri_selected]=[] then
  886. begin
  887. supregset_include(done,n);
  888. if reginfo[n].degree>=usable_registers_cnt then
  889. inc(k);
  890. end;
  891. end;
  892. end;
  893. adj:=reginfo[v].adjlist;
  894. if adj<>nil then
  895. for i:=1 to adj^.length do
  896. begin
  897. n:=adj^.buf^[i-1];
  898. if not supregset_in(done,n) and
  899. (reginfo[n].degree>=usable_registers_cnt) and
  900. (reginfo[u].flags*[ri_coalesced,ri_selected]=[]) then
  901. inc(k);
  902. end;
  903. conservative:=(k<usable_registers_cnt);
  904. end;
  905. procedure trgobj.combine(u,v:Tsuperregister);
  906. var adj : Psuperregisterworklist;
  907. i,n,p,q:cardinal;
  908. t : tsuperregister;
  909. searched:Tlinkedlistitem;
  910. label l1;
  911. begin
  912. if not freezeworklist.delete(v) then
  913. spillworklist.delete(v);
  914. coalescednodes.add(v);
  915. include(reginfo[v].flags,ri_coalesced);
  916. reginfo[v].alias:=u;
  917. {Combine both movelists. Since the movelists are sets, only add
  918. elements that are not already present. The movelists cannot be
  919. empty by definition; nodes are only coalesced if there is a move
  920. between them. To prevent quadratic time blowup (movelists of
  921. especially machine registers can get very large because of moves
  922. generated during calls) we need to go into disgusting complexity.
  923. (See webtbs/tw2242 for an example that stresses this.)
  924. We want to sort the movelist to be able to search logarithmically.
  925. Unfortunately, sorting the movelist every time before searching
  926. is counter-productive, since the movelist usually grows with a few
  927. items at a time. Therefore, we split the movelist into a sorted
  928. and an unsorted part and search through both. If the unsorted part
  929. becomes too large, we sort.}
  930. if assigned(reginfo[u].movelist) then
  931. begin
  932. {We have to weigh the cost of sorting the list against searching
  933. the cost of the unsorted part. I use factor of 8 here; if the
  934. number of items is less than 8 times the numer of unsorted items,
  935. we'll sort the list.}
  936. with reginfo[u].movelist^ do
  937. if header.count<8*(header.count-header.sorted_until) then
  938. sort_movelist(reginfo[u].movelist);
  939. if assigned(reginfo[v].movelist) then
  940. begin
  941. for n:=0 to reginfo[v].movelist^.header.count-1 do
  942. begin
  943. {Binary search the sorted part of the list.}
  944. searched:=reginfo[v].movelist^.data[n];
  945. p:=0;
  946. q:=reginfo[u].movelist^.header.sorted_until;
  947. i:=0;
  948. if q<>0 then
  949. repeat
  950. i:=(p+q) shr 1;
  951. if ptruint(searched)>ptruint(reginfo[u].movelist^.data[i]) then
  952. p:=i+1
  953. else
  954. q:=i;
  955. until p=q;
  956. with reginfo[u].movelist^ do
  957. if searched<>data[i] then
  958. begin
  959. {Linear search the unsorted part of the list.}
  960. for i:=header.sorted_until+1 to header.count-1 do
  961. if searched=data[i] then
  962. goto l1;
  963. {Not found -> add}
  964. add_to_movelist(u,searched);
  965. l1:
  966. end;
  967. end;
  968. end;
  969. end;
  970. enable_moves(v);
  971. adj:=reginfo[v].adjlist;
  972. if adj<>nil then
  973. for i:=1 to adj^.length do
  974. begin
  975. t:=adj^.buf^[i-1];
  976. with reginfo[t] do
  977. if not(ri_coalesced in flags) then
  978. begin
  979. {t has a connection to v. Since we are adding v to u, we
  980. need to connect t to u. However, beware if t was already
  981. connected to u...}
  982. if (ibitmap[t,u]) and not (ri_selected in flags) then
  983. {... because in that case, we are actually removing an edge
  984. and the degree of t decreases.}
  985. decrement_degree(t)
  986. else
  987. begin
  988. add_edge(t,u);
  989. {We have added an edge to t and u. So their degree increases.
  990. However, v is added to u. That means its neighbours will
  991. no longer point to v, but to u instead. Therefore, only the
  992. degree of u increases.}
  993. if (u>=first_imaginary) and not (ri_selected in flags) then
  994. inc(reginfo[u].degree);
  995. end;
  996. end;
  997. end;
  998. if (reginfo[u].degree>=usable_registers_cnt) and freezeworklist.delete(u) then
  999. spillworklist.add(u);
  1000. end;
  1001. procedure trgobj.coalesce;
  1002. var m:Tmoveins;
  1003. x,y,u,v:Tsuperregister;
  1004. begin
  1005. m:=Tmoveins(worklist_moves.getfirst);
  1006. x:=get_alias(m.x);
  1007. y:=get_alias(m.y);
  1008. if (y<first_imaginary) then
  1009. begin
  1010. u:=y;
  1011. v:=x;
  1012. end
  1013. else
  1014. begin
  1015. u:=x;
  1016. v:=y;
  1017. end;
  1018. if (u=v) then
  1019. begin
  1020. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1021. coalesced_moves.insert(m);
  1022. add_worklist(u);
  1023. end
  1024. {Do u and v interfere? In that case the move is constrained. Two
  1025. precoloured nodes interfere allways. If v is precoloured, by the above
  1026. code u is precoloured, thus interference...}
  1027. else if (v<first_imaginary) or ibitmap[u,v] then
  1028. begin
  1029. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1030. constrained_moves.insert(m);
  1031. add_worklist(u);
  1032. add_worklist(v);
  1033. end
  1034. {Next test: is it possible and a good idea to coalesce??}
  1035. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  1036. ((u>=first_imaginary) and conservative(u,v)) then
  1037. begin
  1038. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1039. coalesced_moves.insert(m);
  1040. combine(u,v);
  1041. add_worklist(u);
  1042. end
  1043. else
  1044. begin
  1045. m.moveset:=ms_active_moves;
  1046. active_moves.insert(m);
  1047. end;
  1048. end;
  1049. procedure trgobj.freeze_moves(u:Tsuperregister);
  1050. var i:cardinal;
  1051. m:Tlinkedlistitem;
  1052. v,x,y:Tsuperregister;
  1053. begin
  1054. if reginfo[u].movelist<>nil then
  1055. for i:=0 to reginfo[u].movelist^.header.count-1 do
  1056. begin
  1057. m:=reginfo[u].movelist^.data[i];
  1058. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1059. begin
  1060. x:=Tmoveins(m).x;
  1061. y:=Tmoveins(m).y;
  1062. if get_alias(y)=get_alias(u) then
  1063. v:=get_alias(x)
  1064. else
  1065. v:=get_alias(y);
  1066. {Move m from active_moves/worklist_moves to frozen_moves.}
  1067. if Tmoveins(m).moveset=ms_active_moves then
  1068. active_moves.remove(m)
  1069. else
  1070. worklist_moves.remove(m);
  1071. Tmoveins(m).moveset:=ms_frozen_moves;
  1072. frozen_moves.insert(m);
  1073. if (v>=first_imaginary) and not(move_related(v)) and
  1074. (reginfo[v].degree<usable_registers_cnt) then
  1075. begin
  1076. freezeworklist.delete(v);
  1077. simplifyworklist.add(v);
  1078. end;
  1079. end;
  1080. end;
  1081. end;
  1082. procedure trgobj.freeze;
  1083. var n:Tsuperregister;
  1084. begin
  1085. { We need to take a random element out of the freezeworklist. We take
  1086. the last element. Dirty code! }
  1087. n:=freezeworklist.get;
  1088. {Add it to the simplifyworklist.}
  1089. simplifyworklist.add(n);
  1090. freeze_moves(n);
  1091. end;
  1092. procedure trgobj.select_spill;
  1093. var
  1094. n : tsuperregister;
  1095. adj : psuperregisterworklist;
  1096. max,p,i:word;
  1097. begin
  1098. { We must look for the element with the most interferences in the
  1099. spillworklist. This is required because those registers are creating
  1100. the most conflicts and keeping them in a register will not reduce the
  1101. complexity and even can cause the help registers for the spilling code
  1102. to get too much conflicts with the result that the spilling code
  1103. will never converge (PFV) }
  1104. max:=0;
  1105. p:=0;
  1106. with spillworklist do
  1107. begin
  1108. {Safe: This procedure is only called if length<>0}
  1109. for i:=0 to length-1 do
  1110. begin
  1111. adj:=reginfo[buf^[i]].adjlist;
  1112. if assigned(adj) and (adj^.length>max) then
  1113. begin
  1114. p:=i;
  1115. max:=adj^.length;
  1116. end;
  1117. end;
  1118. n:=buf^[p];
  1119. deleteidx(p);
  1120. end;
  1121. simplifyworklist.add(n);
  1122. freeze_moves(n);
  1123. end;
  1124. procedure trgobj.assign_colours;
  1125. {Assign_colours assigns the actual colours to the registers.}
  1126. var adj : Psuperregisterworklist;
  1127. i,j,k : word;
  1128. n,a,c : Tsuperregister;
  1129. colourednodes : Tsuperregisterset;
  1130. adj_colours:set of 0..255;
  1131. found : boolean;
  1132. begin
  1133. spillednodes.clear;
  1134. {Reset colours}
  1135. for n:=0 to maxreg-1 do
  1136. reginfo[n].colour:=n;
  1137. {Colour the cpu registers...}
  1138. supregset_reset(colourednodes,false,maxreg);
  1139. for n:=0 to first_imaginary-1 do
  1140. supregset_include(colourednodes,n);
  1141. {Now colour the imaginary registers on the select-stack.}
  1142. for i:=selectstack.length downto 1 do
  1143. begin
  1144. n:=selectstack.buf^[i-1];
  1145. {Create a list of colours that we cannot assign to n.}
  1146. adj_colours:=[];
  1147. adj:=reginfo[n].adjlist;
  1148. if adj<>nil then
  1149. for j:=0 to adj^.length-1 do
  1150. begin
  1151. a:=get_alias(adj^.buf^[j]);
  1152. if supregset_in(colourednodes,a) and (reginfo[a].colour<=255) then
  1153. include(adj_colours,reginfo[a].colour);
  1154. end;
  1155. if regtype=R_INTREGISTER then
  1156. include(adj_colours,RS_STACK_POINTER_REG);
  1157. {Assume a spill by default...}
  1158. found:=false;
  1159. {Search for a colour not in this list.}
  1160. for k:=0 to usable_registers_cnt-1 do
  1161. begin
  1162. c:=usable_registers[k];
  1163. if not(c in adj_colours) then
  1164. begin
  1165. reginfo[n].colour:=c;
  1166. found:=true;
  1167. supregset_include(colourednodes,n);
  1168. include(used_in_proc,c);
  1169. break;
  1170. end;
  1171. end;
  1172. if not found then
  1173. spillednodes.add(n);
  1174. end;
  1175. {Finally colour the nodes that were coalesced.}
  1176. for i:=1 to coalescednodes.length do
  1177. begin
  1178. n:=coalescednodes.buf^[i-1];
  1179. k:=get_alias(n);
  1180. reginfo[n].colour:=reginfo[k].colour;
  1181. if reginfo[k].colour<maxcpuregister then
  1182. include(used_in_proc,reginfo[k].colour);
  1183. end;
  1184. end;
  1185. procedure trgobj.colour_registers;
  1186. begin
  1187. repeat
  1188. if simplifyworklist.length<>0 then
  1189. simplify
  1190. else if not(worklist_moves.empty) then
  1191. coalesce
  1192. else if freezeworklist.length<>0 then
  1193. freeze
  1194. else if spillworklist.length<>0 then
  1195. select_spill;
  1196. until (simplifyworklist.length=0) and
  1197. worklist_moves.empty and
  1198. (freezeworklist.length=0) and
  1199. (spillworklist.length=0);
  1200. assign_colours;
  1201. end;
  1202. procedure trgobj.epilogue_colouring;
  1203. var
  1204. i : Tsuperregister;
  1205. begin
  1206. worklist_moves.clear;
  1207. active_moves.destroy;
  1208. active_moves:=nil;
  1209. frozen_moves.destroy;
  1210. frozen_moves:=nil;
  1211. coalesced_moves.destroy;
  1212. coalesced_moves:=nil;
  1213. constrained_moves.destroy;
  1214. constrained_moves:=nil;
  1215. for i:=0 to maxreg-1 do
  1216. with reginfo[i] do
  1217. if movelist<>nil then
  1218. begin
  1219. dispose(movelist);
  1220. movelist:=nil;
  1221. end;
  1222. end;
  1223. procedure trgobj.clear_interferences(u:Tsuperregister);
  1224. {Remove node u from the interference graph and remove all collected
  1225. move instructions it is associated with.}
  1226. var i : word;
  1227. v : Tsuperregister;
  1228. adj,adj2 : Psuperregisterworklist;
  1229. begin
  1230. adj:=reginfo[u].adjlist;
  1231. if adj<>nil then
  1232. begin
  1233. for i:=1 to adj^.length do
  1234. begin
  1235. v:=adj^.buf^[i-1];
  1236. {Remove (u,v) and (v,u) from bitmap.}
  1237. ibitmap[u,v]:=false;
  1238. ibitmap[v,u]:=false;
  1239. {Remove (v,u) from adjacency list.}
  1240. adj2:=reginfo[v].adjlist;
  1241. if adj2<>nil then
  1242. begin
  1243. adj2^.delete(u);
  1244. if adj2^.length=0 then
  1245. begin
  1246. dispose(adj2,done);
  1247. reginfo[v].adjlist:=nil;
  1248. end;
  1249. end;
  1250. end;
  1251. {Remove ( u,* ) from adjacency list.}
  1252. dispose(adj,done);
  1253. reginfo[u].adjlist:=nil;
  1254. end;
  1255. end;
  1256. function trgobj.getregisterinline(list:TAsmList;subreg:Tsubregister):Tregister;
  1257. var
  1258. p : Tsuperregister;
  1259. begin
  1260. p:=getnewreg(subreg);
  1261. live_registers.add(p);
  1262. result:=newreg(regtype,p,subreg);
  1263. add_edges_used(p);
  1264. add_constraints(result);
  1265. end;
  1266. procedure trgobj.ungetregisterinline(list:TAsmList;r:Tregister);
  1267. var
  1268. supreg:Tsuperregister;
  1269. begin
  1270. supreg:=getsupreg(r);
  1271. live_registers.delete(supreg);
  1272. insert_regalloc_info(list,supreg);
  1273. end;
  1274. procedure trgobj.insert_regalloc_info(list:TAsmList;u:tsuperregister);
  1275. var
  1276. p : tai;
  1277. r : tregister;
  1278. palloc,
  1279. pdealloc : tai_regalloc;
  1280. begin
  1281. { Insert regallocs for all imaginary registers }
  1282. with reginfo[u] do
  1283. begin
  1284. r:=newreg(regtype,u,subreg);
  1285. if assigned(live_start) then
  1286. begin
  1287. { Generate regalloc and bind it to an instruction, this
  1288. is needed to find all live registers belonging to an
  1289. instruction during the spilling }
  1290. if live_start.typ=ait_instruction then
  1291. palloc:=tai_regalloc.alloc(r,live_start)
  1292. else
  1293. palloc:=tai_regalloc.alloc(r,nil);
  1294. if live_end.typ=ait_instruction then
  1295. pdealloc:=tai_regalloc.dealloc(r,live_end)
  1296. else
  1297. pdealloc:=tai_regalloc.dealloc(r,nil);
  1298. { Insert live start allocation before the instruction/reg_a_sync }
  1299. list.insertbefore(palloc,live_start);
  1300. { Insert live end deallocation before reg allocations
  1301. to reduce conflicts }
  1302. p:=live_end;
  1303. while assigned(p) and
  1304. assigned(p.previous) and
  1305. (tai(p.previous).typ=ait_regalloc) and
  1306. (tai_regalloc(p.previous).ratype=ra_alloc) and
  1307. (tai_regalloc(p.previous).reg<>r) do
  1308. p:=tai(p.previous);
  1309. { , but add release after a reg_a_sync }
  1310. if assigned(p) and
  1311. (p.typ=ait_regalloc) and
  1312. (tai_regalloc(p).ratype=ra_sync) then
  1313. p:=tai(p.next);
  1314. if assigned(p) then
  1315. list.insertbefore(pdealloc,p)
  1316. else
  1317. list.concat(pdealloc);
  1318. end;
  1319. end;
  1320. end;
  1321. procedure trgobj.insert_regalloc_info_all(list:TAsmList);
  1322. var
  1323. supreg : tsuperregister;
  1324. begin
  1325. { Insert regallocs for all imaginary registers }
  1326. for supreg:=first_imaginary to maxreg-1 do
  1327. insert_regalloc_info(list,supreg);
  1328. end;
  1329. procedure trgobj.add_cpu_interferences(p : tai);
  1330. begin
  1331. end;
  1332. procedure trgobj.generate_interference_graph(list:TAsmList;headertai:tai);
  1333. var
  1334. p : tai;
  1335. {$ifdef EXTDEBUG}
  1336. i : integer;
  1337. {$endif EXTDEBUG}
  1338. supreg : tsuperregister;
  1339. begin
  1340. { All allocations are available. Now we can generate the
  1341. interference graph. Walk through all instructions, we can
  1342. start with the headertai, because before the header tai is
  1343. only symbols. }
  1344. live_registers.clear;
  1345. p:=headertai;
  1346. while assigned(p) do
  1347. begin
  1348. if p.typ=ait_regalloc then
  1349. with Tai_regalloc(p) do
  1350. begin
  1351. if (getregtype(reg)=regtype) then
  1352. begin
  1353. supreg:=getsupreg(reg);
  1354. case ratype of
  1355. ra_alloc :
  1356. begin
  1357. live_registers.add(supreg);
  1358. add_edges_used(supreg);
  1359. end;
  1360. ra_dealloc :
  1361. begin
  1362. live_registers.delete(supreg);
  1363. add_edges_used(supreg);
  1364. end;
  1365. end;
  1366. { constraints needs always to be updated }
  1367. add_constraints(reg);
  1368. end;
  1369. end;
  1370. add_cpu_interferences(p);
  1371. p:=Tai(p.next);
  1372. end;
  1373. {$ifdef EXTDEBUG}
  1374. if live_registers.length>0 then
  1375. begin
  1376. for i:=0 to live_registers.length-1 do
  1377. begin
  1378. { Only report for imaginary registers }
  1379. if live_registers.buf^[i]>=first_imaginary then
  1380. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,live_registers.buf^[i],defaultsub))+' not released');
  1381. end;
  1382. end;
  1383. {$endif}
  1384. end;
  1385. procedure trgobj.translate_register(var reg : tregister);
  1386. begin
  1387. if (getregtype(reg)=regtype) then
  1388. setsupreg(reg,reginfo[getsupreg(reg)].colour)
  1389. else
  1390. internalerror(200602021);
  1391. end;
  1392. procedure Trgobj.translate_registers(list:TAsmList);
  1393. var
  1394. hp,p,q:Tai;
  1395. i:shortint;
  1396. {$ifdef arm}
  1397. so:pshifterop;
  1398. {$endif arm}
  1399. begin
  1400. { Leave when no imaginary registers are used }
  1401. if maxreg<=first_imaginary then
  1402. exit;
  1403. p:=Tai(list.first);
  1404. while assigned(p) do
  1405. begin
  1406. case p.typ of
  1407. ait_regalloc:
  1408. with Tai_regalloc(p) do
  1409. begin
  1410. if (getregtype(reg)=regtype) then
  1411. begin
  1412. { Only alloc/dealloc is needed for the optimizer, remove
  1413. other regalloc }
  1414. if not(ratype in [ra_alloc,ra_dealloc]) then
  1415. begin
  1416. q:=Tai(next);
  1417. list.remove(p);
  1418. p.free;
  1419. p:=q;
  1420. continue;
  1421. end
  1422. else
  1423. begin
  1424. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1425. {
  1426. Remove sequences of release and
  1427. allocation of the same register like. Other combinations
  1428. of release/allocate need to stay in the list.
  1429. # Register X released
  1430. # Register X allocated
  1431. }
  1432. if assigned(previous) and
  1433. (ratype=ra_alloc) and
  1434. (Tai(previous).typ=ait_regalloc) and
  1435. (Tai_regalloc(previous).reg=reg) and
  1436. (Tai_regalloc(previous).ratype=ra_dealloc) then
  1437. begin
  1438. q:=Tai(next);
  1439. hp:=tai(previous);
  1440. list.remove(hp);
  1441. hp.free;
  1442. list.remove(p);
  1443. p.free;
  1444. p:=q;
  1445. continue;
  1446. end;
  1447. end;
  1448. end;
  1449. end;
  1450. ait_instruction:
  1451. with Taicpu(p) do
  1452. begin
  1453. current_filepos:=fileinfo;
  1454. for i:=0 to ops-1 do
  1455. with oper[i]^ do
  1456. case typ of
  1457. Top_reg:
  1458. if (getregtype(reg)=regtype) then
  1459. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1460. Top_ref:
  1461. begin
  1462. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1463. with ref^ do
  1464. begin
  1465. if base<>NR_NO then
  1466. setsupreg(base,reginfo[getsupreg(base)].colour);
  1467. if index<>NR_NO then
  1468. setsupreg(index,reginfo[getsupreg(index)].colour);
  1469. end;
  1470. end;
  1471. {$ifdef arm}
  1472. Top_shifterop:
  1473. begin
  1474. if regtype=R_INTREGISTER then
  1475. begin
  1476. so:=shifterop;
  1477. if so^.rs<>NR_NO then
  1478. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1479. end;
  1480. end;
  1481. {$endif arm}
  1482. end;
  1483. { Maybe the operation can be removed when
  1484. it is a move and both arguments are the same }
  1485. if is_same_reg_move(regtype) then
  1486. begin
  1487. q:=Tai(p.next);
  1488. list.remove(p);
  1489. p.free;
  1490. p:=q;
  1491. continue;
  1492. end;
  1493. end;
  1494. end;
  1495. p:=Tai(p.next);
  1496. end;
  1497. current_filepos:=current_procinfo.exitpos;
  1498. end;
  1499. function trgobj.spill_registers(list:TAsmList;headertai:tai):boolean;
  1500. { Returns true if any help registers have been used }
  1501. var
  1502. i : word;
  1503. t : tsuperregister;
  1504. p,q : Tai;
  1505. regs_to_spill_set:Tsuperregisterset;
  1506. spill_temps : ^Tspill_temp_list;
  1507. supreg : tsuperregister;
  1508. templist : TAsmList;
  1509. begin
  1510. spill_registers:=false;
  1511. live_registers.clear;
  1512. for i:=first_imaginary to maxreg-1 do
  1513. exclude(reginfo[i].flags,ri_selected);
  1514. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1515. supregset_reset(regs_to_spill_set,false,$ffff);
  1516. { Allocate temps and insert in front of the list }
  1517. templist:=TAsmList.create;
  1518. {Safe: this procedure is only called if there are spilled nodes.}
  1519. with spillednodes do
  1520. for i:=0 to length-1 do
  1521. begin
  1522. t:=buf^[i];
  1523. {Alternative representation.}
  1524. supregset_include(regs_to_spill_set,t);
  1525. {Clear all interferences of the spilled register.}
  1526. clear_interferences(t);
  1527. {Get a temp for the spilled register, the size must at least equal a complete register,
  1528. take also care of the fact that subreg can be larger than a single register like doubles
  1529. that occupy 2 registers }
  1530. tg.gettemp(templist,
  1531. max(tcgsize2size[reg_cgsize(newreg(regtype,t,R_SUBWHOLE))],
  1532. tcgsize2size[reg_cgsize(newreg(regtype,t,reginfo[t].subreg))]),
  1533. tt_noreuse,spill_temps^[t]);
  1534. end;
  1535. list.insertlistafter(headertai,templist);
  1536. templist.free;
  1537. { Walk through all instructions, we can start with the headertai,
  1538. because before the header tai is only symbols }
  1539. p:=headertai;
  1540. while assigned(p) do
  1541. begin
  1542. case p.typ of
  1543. ait_regalloc:
  1544. with Tai_regalloc(p) do
  1545. begin
  1546. if (getregtype(reg)=regtype) then
  1547. begin
  1548. {A register allocation of a spilled register can be removed.}
  1549. supreg:=getsupreg(reg);
  1550. if supregset_in(regs_to_spill_set,supreg) then
  1551. begin
  1552. q:=Tai(p.next);
  1553. list.remove(p);
  1554. p.free;
  1555. p:=q;
  1556. continue;
  1557. end
  1558. else
  1559. begin
  1560. case ratype of
  1561. ra_alloc :
  1562. live_registers.add(supreg);
  1563. ra_dealloc :
  1564. live_registers.delete(supreg);
  1565. end;
  1566. end;
  1567. end;
  1568. end;
  1569. ait_instruction:
  1570. with Taicpu(p) do
  1571. begin
  1572. current_filepos:=fileinfo;
  1573. if instr_spill_register(list,taicpu(p),regs_to_spill_set,spill_temps^) then
  1574. spill_registers:=true;
  1575. end;
  1576. end;
  1577. p:=Tai(p.next);
  1578. end;
  1579. current_filepos:=current_procinfo.exitpos;
  1580. {Safe: this procedure is only called if there are spilled nodes.}
  1581. with spillednodes do
  1582. for i:=0 to length-1 do
  1583. tg.ungettemp(list,spill_temps^[buf^[i]]);
  1584. freemem(spill_temps);
  1585. end;
  1586. function trgobj.do_spill_replace(list:TAsmList;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;
  1587. begin
  1588. result:=false;
  1589. end;
  1590. procedure Trgobj.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  1591. var ins:Taicpu;
  1592. begin
  1593. ins:=spilling_create_load(spilltemp,tempreg);
  1594. add_cpu_interferences(ins);
  1595. list.insertafter(ins,pos);
  1596. end;
  1597. procedure Trgobj.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister);
  1598. var ins:Taicpu;
  1599. begin
  1600. ins:=spilling_create_store(tempreg,spilltemp);
  1601. add_cpu_interferences(ins);
  1602. list.insertafter(ins,pos);
  1603. end;
  1604. function trgobj.get_spill_subreg(r : tregister) : tsubregister;
  1605. begin
  1606. result:=defaultsub;
  1607. end;
  1608. function trgobj.instr_spill_register(list:TAsmList;
  1609. instr:taicpu;
  1610. const r:Tsuperregisterset;
  1611. const spilltemplist:Tspill_temp_list): boolean;
  1612. var
  1613. counter, regindex: longint;
  1614. regs: tspillregsinfo;
  1615. spilled: boolean;
  1616. procedure addreginfo(reg: tregister; operation: topertype);
  1617. var
  1618. i, tmpindex: longint;
  1619. supreg : tsuperregister;
  1620. begin
  1621. tmpindex := regindex;
  1622. supreg:=get_alias(getsupreg(reg));
  1623. { did we already encounter this register? }
  1624. for i := 0 to pred(regindex) do
  1625. if (regs[i].orgreg = supreg) then
  1626. begin
  1627. tmpindex := i;
  1628. break;
  1629. end;
  1630. if tmpindex > high(regs) then
  1631. internalerror(2003120301);
  1632. regs[tmpindex].orgreg := supreg;
  1633. regs[tmpindex].spillreg:=reg;
  1634. if supregset_in(r,supreg) then
  1635. begin
  1636. { add/update info on this register }
  1637. regs[tmpindex].mustbespilled := true;
  1638. case operation of
  1639. operand_read:
  1640. regs[tmpindex].regread := true;
  1641. operand_write:
  1642. regs[tmpindex].regwritten := true;
  1643. operand_readwrite:
  1644. begin
  1645. regs[tmpindex].regread := true;
  1646. regs[tmpindex].regwritten := true;
  1647. end;
  1648. end;
  1649. spilled := true;
  1650. end;
  1651. inc(regindex,ord(regindex=tmpindex));
  1652. end;
  1653. procedure tryreplacereg(var reg: tregister);
  1654. var
  1655. i: longint;
  1656. supreg: tsuperregister;
  1657. begin
  1658. supreg:=get_alias(getsupreg(reg));
  1659. for i:=0 to pred(regindex) do
  1660. if (regs[i].mustbespilled) and
  1661. (regs[i].orgreg=supreg) then
  1662. begin
  1663. { Only replace supreg }
  1664. setsupreg(reg,getsupreg(regs[i].tempreg));
  1665. break;
  1666. end;
  1667. end;
  1668. var
  1669. loadpos,
  1670. storepos : tai;
  1671. oldlive_registers : tsuperregisterworklist;
  1672. begin
  1673. result := false;
  1674. fillchar(regs,sizeof(regs),0);
  1675. for counter := low(regs) to high(regs) do
  1676. regs[counter].orgreg := RS_INVALID;
  1677. spilled := false;
  1678. regindex := 0;
  1679. { check whether and if so which and how (read/written) this instructions contains
  1680. registers that must be spilled }
  1681. for counter := 0 to instr.ops-1 do
  1682. with instr.oper[counter]^ do
  1683. begin
  1684. case typ of
  1685. top_reg:
  1686. begin
  1687. if (getregtype(reg) = regtype) then
  1688. addreginfo(reg,instr.spilling_get_operation_type(counter));
  1689. end;
  1690. top_ref:
  1691. begin
  1692. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1693. with ref^ do
  1694. begin
  1695. if (base <> NR_NO) then
  1696. addreginfo(base,instr.spilling_get_operation_type_ref(counter,base));
  1697. if (index <> NR_NO) then
  1698. addreginfo(index,instr.spilling_get_operation_type_ref(counter,index));
  1699. end;
  1700. end;
  1701. {$ifdef ARM}
  1702. top_shifterop:
  1703. begin
  1704. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1705. if shifterop^.rs<>NR_NO then
  1706. addreginfo(shifterop^.rs,operand_read);
  1707. end;
  1708. {$endif ARM}
  1709. end;
  1710. end;
  1711. { if no spilling for this instruction we can leave }
  1712. if not spilled then
  1713. exit;
  1714. {$ifdef x86}
  1715. { Try replacing the register with the spilltemp. This is usefull only
  1716. for the i386,x86_64 that support memory locations for several instructions }
  1717. for counter := 0 to pred(regindex) do
  1718. with regs[counter] do
  1719. begin
  1720. if mustbespilled then
  1721. begin
  1722. if do_spill_replace(list,instr,orgreg,spilltemplist[orgreg]) then
  1723. mustbespilled:=false;
  1724. end;
  1725. end;
  1726. {$endif x86}
  1727. {
  1728. There are registers that need are spilled. We generate the
  1729. following code for it. The used positions where code need
  1730. to be inserted are marked using #. Note that code is always inserted
  1731. before the positions using pos.previous. This way the position is always
  1732. the same since pos doesn't change, but pos.previous is modified everytime
  1733. new code is inserted.
  1734. [
  1735. - reg_allocs load spills
  1736. - load spills
  1737. ]
  1738. [#loadpos
  1739. - reg_deallocs
  1740. - reg_allocs
  1741. ]
  1742. [
  1743. - reg_deallocs for load-only spills
  1744. - reg_allocs for store-only spills
  1745. ]
  1746. [#instr
  1747. - original instruction
  1748. ]
  1749. [
  1750. - store spills
  1751. - reg_deallocs store spills
  1752. ]
  1753. [#storepos
  1754. ]
  1755. }
  1756. result := true;
  1757. oldlive_registers.copyfrom(live_registers);
  1758. { Process all tai_regallocs belonging to this instruction, ignore explicit
  1759. inserted regallocs. These can happend for example in i386:
  1760. mov ref,ireg26
  1761. <regdealloc ireg26, instr=taicpu of lea>
  1762. <regalloc edi, insrt=nil>
  1763. lea [ireg26+ireg17],edi
  1764. All released registers are also added to the live_registers because
  1765. they can't be used during the spilling }
  1766. loadpos:=tai(instr.previous);
  1767. while assigned(loadpos) and
  1768. (loadpos.typ=ait_regalloc) and
  1769. ((tai_regalloc(loadpos).instr=nil) or
  1770. (tai_regalloc(loadpos).instr=instr)) do
  1771. begin
  1772. { Only add deallocs belonging to the instruction. Explicit inserted deallocs
  1773. belong to the previous instruction and not the current instruction }
  1774. if (tai_regalloc(loadpos).instr=instr) and
  1775. (tai_regalloc(loadpos).ratype=ra_dealloc) then
  1776. live_registers.add(getsupreg(tai_regalloc(loadpos).reg));
  1777. loadpos:=tai(loadpos.previous);
  1778. end;
  1779. loadpos:=tai(loadpos.next);
  1780. { Load the spilled registers }
  1781. for counter := 0 to pred(regindex) do
  1782. with regs[counter] do
  1783. begin
  1784. if mustbespilled and regread then
  1785. begin
  1786. tempreg:=getregisterinline(list,get_spill_subreg(regs[counter].spillreg));
  1787. do_spill_read(list,tai(loadpos.previous),spilltemplist[orgreg],tempreg);
  1788. end;
  1789. end;
  1790. { Release temp registers of read-only registers, and add reference of the instruction
  1791. to the reginfo }
  1792. for counter := 0 to pred(regindex) do
  1793. with regs[counter] do
  1794. begin
  1795. if mustbespilled and regread and (not regwritten) then
  1796. begin
  1797. { The original instruction will be the next that uses this register }
  1798. add_reg_instruction(instr,tempreg);
  1799. ungetregisterinline(list,tempreg);
  1800. end;
  1801. end;
  1802. { Allocate temp registers of write-only registers, and add reference of the instruction
  1803. to the reginfo }
  1804. for counter := 0 to pred(regindex) do
  1805. with regs[counter] do
  1806. begin
  1807. if mustbespilled and regwritten then
  1808. begin
  1809. { When the register is also loaded there is already a register assigned }
  1810. if (not regread) then
  1811. tempreg:=getregisterinline(list,get_spill_subreg(regs[counter].spillreg));
  1812. { The original instruction will be the next that uses this register, this
  1813. also needs to be done for read-write registers }
  1814. add_reg_instruction(instr,tempreg);
  1815. end;
  1816. end;
  1817. { store the spilled registers }
  1818. storepos:=tai(instr.next);
  1819. for counter := 0 to pred(regindex) do
  1820. with regs[counter] do
  1821. begin
  1822. if mustbespilled and regwritten then
  1823. begin
  1824. do_spill_written(list,tai(storepos.previous),spilltemplist[orgreg],tempreg);
  1825. ungetregisterinline(list,tempreg);
  1826. end;
  1827. end;
  1828. { now all spilling code is generated we can restore the live registers. This
  1829. must be done after the store because the store can need an extra register
  1830. that also needs to conflict with the registers of the instruction }
  1831. live_registers.done;
  1832. live_registers:=oldlive_registers;
  1833. { substitute registers }
  1834. for counter:=0 to instr.ops-1 do
  1835. with instr.oper[counter]^ do
  1836. case typ of
  1837. top_reg:
  1838. begin
  1839. if (getregtype(reg) = regtype) then
  1840. tryreplacereg(reg);
  1841. end;
  1842. top_ref:
  1843. begin
  1844. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1845. begin
  1846. tryreplacereg(ref^.base);
  1847. tryreplacereg(ref^.index);
  1848. end;
  1849. end;
  1850. {$ifdef ARM}
  1851. top_shifterop:
  1852. begin
  1853. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1854. tryreplacereg(shifterop^.rs);
  1855. end;
  1856. {$endif ARM}
  1857. end;
  1858. {We have modified the instruction; perhaps the new instruction has
  1859. certain constraints regarding which imaginary registers interfere
  1860. with certain physical registers.}
  1861. add_cpu_interferences(instr);
  1862. end;
  1863. end.