rgobj.pas 68 KB

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