rgobj.pas 90 KB

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