rgobj.pas 96 KB

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