rgobj.pas 72 KB

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