rgobj.pas 84 KB

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