rgobj.pas 90 KB

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