rgobj.pas 80 KB

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