rgobj.pas 81 KB

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