rgobj.pas 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729
  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. {$ifdef arm}
  1624. so: pshifterop;
  1625. {$endif arm}
  1626. begin
  1627. { All allocations are available. Now we can generate the
  1628. interference graph. Walk through all instructions, we can
  1629. start with the headertai, because before the header tai is
  1630. only symbols. }
  1631. live_registers.clear;
  1632. p:=headertai;
  1633. while assigned(p) do
  1634. begin
  1635. prefetch(pointer(p.next)^);
  1636. case p.typ of
  1637. ait_instruction:
  1638. with Taicpu(p) do
  1639. begin
  1640. current_filepos:=fileinfo;
  1641. {For speed reasons, get_alias isn't used here, instead,
  1642. assign_colours will also set the colour of coalesced nodes.
  1643. If there are registers with colour=0, then the coalescednodes
  1644. list probably doesn't contain these registers, causing
  1645. assign_colours not to do this properly.}
  1646. for i:=0 to ops-1 do
  1647. with oper[i]^ do
  1648. case typ of
  1649. top_reg:
  1650. if (getregtype(reg)=regtype) then
  1651. begin
  1652. u:=getsupreg(reg);
  1653. {$ifdef EXTDEBUG}
  1654. if (u>=maxreginfo) then
  1655. internalerror(2018111701);
  1656. {$endif}
  1657. RecordUse(reginfo[u]);
  1658. end;
  1659. top_ref:
  1660. begin
  1661. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1662. with ref^ do
  1663. begin
  1664. if (base<>NR_NO) and
  1665. (getregtype(base)=regtype) then
  1666. begin
  1667. u:=getsupreg(base);
  1668. {$ifdef EXTDEBUG}
  1669. if (u>=maxreginfo) then
  1670. internalerror(2018111702);
  1671. {$endif}
  1672. RecordUse(reginfo[u]);
  1673. end;
  1674. if (index<>NR_NO) and
  1675. (getregtype(index)=regtype) then
  1676. begin
  1677. u:=getsupreg(index);
  1678. {$ifdef EXTDEBUG}
  1679. if (u>=maxreginfo) then
  1680. internalerror(2018111703);
  1681. {$endif}
  1682. RecordUse(reginfo[u]);
  1683. end;
  1684. {$if defined(x86)}
  1685. if (segment<>NR_NO) and
  1686. (getregtype(segment)=regtype) then
  1687. begin
  1688. u:=getsupreg(segment);
  1689. {$ifdef EXTDEBUG}
  1690. if (u>=maxreginfo) then
  1691. internalerror(2018111704);
  1692. {$endif}
  1693. RecordUse(reginfo[u]);
  1694. end;
  1695. {$endif defined(x86)}
  1696. end;
  1697. end;
  1698. {$ifdef arm}
  1699. Top_shifterop:
  1700. begin
  1701. if regtype=R_INTREGISTER then
  1702. begin
  1703. so:=shifterop;
  1704. if (so^.rs<>NR_NO) and
  1705. (getregtype(so^.rs)=regtype) then
  1706. RecordUse(reginfo[getsupreg(so^.rs)]);
  1707. end;
  1708. end;
  1709. {$endif arm}
  1710. end;
  1711. end;
  1712. ait_regalloc:
  1713. with Tai_regalloc(p) do
  1714. begin
  1715. if (getregtype(reg)=regtype) then
  1716. begin
  1717. supreg:=getsupreg(reg);
  1718. case ratype of
  1719. ra_alloc :
  1720. begin
  1721. live_registers.add(supreg);
  1722. {$ifdef DEBUG_REGISTERLIFE}
  1723. write(live_registers.length,' ');
  1724. for i:=0 to live_registers.length-1 do
  1725. write(std_regname(newreg(regtype,live_registers.buf^[i],defaultsub)),' ');
  1726. writeln;
  1727. {$endif DEBUG_REGISTERLIFE}
  1728. add_edges_used(supreg);
  1729. end;
  1730. ra_dealloc :
  1731. begin
  1732. live_registers.delete(supreg);
  1733. {$ifdef DEBUG_REGISTERLIFE}
  1734. write(live_registers.length,' ');
  1735. for i:=0 to live_registers.length-1 do
  1736. write(std_regname(newreg(regtype,live_registers.buf^[i],defaultsub)),' ');
  1737. writeln;
  1738. {$endif DEBUG_REGISTERLIFE}
  1739. add_edges_used(supreg);
  1740. end;
  1741. ra_markused :
  1742. if (supreg<first_imaginary) then
  1743. begin
  1744. include(used_in_proc,supreg);
  1745. has_usedmarks:=true;
  1746. end;
  1747. end;
  1748. { constraints needs always to be updated }
  1749. add_constraints(reg);
  1750. end;
  1751. end;
  1752. end;
  1753. add_cpu_interferences(p);
  1754. p:=Tai(p.next);
  1755. end;
  1756. {$ifdef EXTDEBUG}
  1757. if live_registers.length>0 then
  1758. begin
  1759. for i:=0 to live_registers.length-1 do
  1760. begin
  1761. { Only report for imaginary registers }
  1762. if live_registers.buf^[i]>=first_imaginary then
  1763. Comment(V_Warning,'Register '+std_regname(newreg(regtype,live_registers.buf^[i],defaultsub))+' not released');
  1764. end;
  1765. end;
  1766. {$endif}
  1767. end;
  1768. procedure trgobj.translate_register(var reg : tregister);
  1769. begin
  1770. if (getregtype(reg)=regtype) then
  1771. setsupreg(reg,reginfo[getsupreg(reg)].colour)
  1772. else
  1773. internalerror(200602021);
  1774. end;
  1775. procedure Trgobj.translate_registers(list:TAsmList);
  1776. var
  1777. hp,p,q:Tai;
  1778. i:shortint;
  1779. u:longint;
  1780. {$ifdef arm}
  1781. so:pshifterop;
  1782. {$endif arm}
  1783. begin
  1784. { Leave when no imaginary registers are used }
  1785. if maxreg<=first_imaginary then
  1786. exit;
  1787. p:=Tai(list.first);
  1788. while assigned(p) do
  1789. begin
  1790. prefetch(pointer(p.next)^);
  1791. case p.typ of
  1792. ait_regalloc:
  1793. with Tai_regalloc(p) do
  1794. begin
  1795. if (getregtype(reg)=regtype) then
  1796. begin
  1797. { Only alloc/dealloc is needed for the optimizer, remove
  1798. other regalloc }
  1799. if not(ratype in [ra_alloc,ra_dealloc]) then
  1800. begin
  1801. q:=Tai(next);
  1802. list.remove(p);
  1803. p.free;
  1804. p:=q;
  1805. continue;
  1806. end
  1807. else
  1808. begin
  1809. u:=reginfo[getsupreg(reg)].colour;
  1810. include(used_in_proc,u);
  1811. {$ifdef EXTDEBUG}
  1812. if u>=maxreginfo then
  1813. internalerror(2015040501);
  1814. {$endif}
  1815. setsupreg(reg,u);
  1816. {
  1817. Remove sequences of release and
  1818. allocation of the same register like. Other combinations
  1819. of release/allocate need to stay in the list.
  1820. # Register X released
  1821. # Register X allocated
  1822. }
  1823. if assigned(previous) and
  1824. (ratype=ra_alloc) and
  1825. (Tai(previous).typ=ait_regalloc) and
  1826. (Tai_regalloc(previous).reg=reg) and
  1827. (Tai_regalloc(previous).ratype=ra_dealloc) then
  1828. begin
  1829. q:=Tai(next);
  1830. hp:=tai(previous);
  1831. list.remove(hp);
  1832. hp.free;
  1833. list.remove(p);
  1834. p.free;
  1835. p:=q;
  1836. continue;
  1837. end;
  1838. end;
  1839. end;
  1840. end;
  1841. ait_varloc:
  1842. begin
  1843. if (getregtype(tai_varloc(p).newlocation)=regtype) then
  1844. begin
  1845. if (cs_asm_source in current_settings.globalswitches) then
  1846. begin
  1847. setsupreg(tai_varloc(p).newlocation,reginfo[getsupreg(tai_varloc(p).newlocation)].colour);
  1848. if tai_varloc(p).newlocationhi<>NR_NO then
  1849. begin
  1850. setsupreg(tai_varloc(p).newlocationhi,reginfo[getsupreg(tai_varloc(p).newlocationhi)].colour);
  1851. hp:=Tai_comment.Create(strpnew('Var '+tai_varloc(p).varsym.realname+' located in register '+
  1852. std_regname(tai_varloc(p).newlocationhi)+':'+std_regname(tai_varloc(p).newlocation)));
  1853. end
  1854. else
  1855. hp:=Tai_comment.Create(strpnew('Var '+tai_varloc(p).varsym.realname+' located in register '+
  1856. std_regname(tai_varloc(p).newlocation)));
  1857. list.insertafter(hp,p);
  1858. end;
  1859. q:=tai(p.next);
  1860. list.remove(p);
  1861. p.free;
  1862. p:=q;
  1863. continue;
  1864. end;
  1865. end;
  1866. ait_instruction:
  1867. with Taicpu(p) do
  1868. begin
  1869. current_filepos:=fileinfo;
  1870. {For speed reasons, get_alias isn't used here, instead,
  1871. assign_colours will also set the colour of coalesced nodes.
  1872. If there are registers with colour=0, then the coalescednodes
  1873. list probably doesn't contain these registers, causing
  1874. assign_colours not to do this properly.}
  1875. for i:=0 to ops-1 do
  1876. with oper[i]^ do
  1877. case typ of
  1878. Top_reg:
  1879. if (getregtype(reg)=regtype) then
  1880. begin
  1881. u:=getsupreg(reg);
  1882. {$ifdef EXTDEBUG}
  1883. if (u>=maxreginfo) then
  1884. internalerror(2012101903);
  1885. {$endif}
  1886. setsupreg(reg,reginfo[u].colour);
  1887. end;
  1888. Top_ref:
  1889. begin
  1890. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1891. with ref^ do
  1892. begin
  1893. if (base<>NR_NO) and
  1894. (getregtype(base)=regtype) then
  1895. begin
  1896. u:=getsupreg(base);
  1897. {$ifdef EXTDEBUG}
  1898. if (u>=maxreginfo) then
  1899. internalerror(2012101904);
  1900. {$endif}
  1901. setsupreg(base,reginfo[u].colour);
  1902. end;
  1903. if (index<>NR_NO) and
  1904. (getregtype(index)=regtype) then
  1905. begin
  1906. u:=getsupreg(index);
  1907. {$ifdef EXTDEBUG}
  1908. if (u>=maxreginfo) then
  1909. internalerror(2012101905);
  1910. {$endif}
  1911. setsupreg(index,reginfo[u].colour);
  1912. end;
  1913. {$if defined(x86)}
  1914. if (segment<>NR_NO) and
  1915. (getregtype(segment)=regtype) then
  1916. begin
  1917. u:=getsupreg(segment);
  1918. {$ifdef EXTDEBUG}
  1919. if (u>=maxreginfo) then
  1920. internalerror(2013052401);
  1921. {$endif}
  1922. setsupreg(segment,reginfo[u].colour);
  1923. end;
  1924. {$endif defined(x86)}
  1925. end;
  1926. end;
  1927. {$ifdef arm}
  1928. Top_shifterop:
  1929. begin
  1930. if regtype=R_INTREGISTER then
  1931. begin
  1932. so:=shifterop;
  1933. if (so^.rs<>NR_NO) and
  1934. (getregtype(so^.rs)=regtype) then
  1935. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1936. end;
  1937. end;
  1938. {$endif arm}
  1939. end;
  1940. { Maybe the operation can be removed when
  1941. it is a move and both arguments are the same }
  1942. if is_same_reg_move(regtype) then
  1943. begin
  1944. q:=Tai(p.next);
  1945. list.remove(p);
  1946. p.free;
  1947. p:=q;
  1948. continue;
  1949. end;
  1950. end;
  1951. end;
  1952. p:=Tai(p.next);
  1953. end;
  1954. current_filepos:=current_procinfo.exitpos;
  1955. end;
  1956. function trgobj.spill_registers(list:TAsmList;headertai:tai):boolean;
  1957. { Returns true if any help registers have been used }
  1958. var
  1959. i : cardinal;
  1960. t : tsuperregister;
  1961. p,q : Tai;
  1962. regs_to_spill_set:Tsuperregisterset;
  1963. spill_temps : ^Tspill_temp_list;
  1964. supreg,x,y : tsuperregister;
  1965. templist : TAsmList;
  1966. j : Longint;
  1967. getnewspillloc : Boolean;
  1968. begin
  1969. spill_registers:=false;
  1970. live_registers.clear;
  1971. { spilling should start with the node with the highest number of interferences, so we can coalesce as
  1972. much as possible spilled nodes (coalesce in case of spilled node means they share the same memory location) }
  1973. sort_spillednodes;
  1974. for i:=first_imaginary to maxreg-1 do
  1975. exclude(reginfo[i].flags,ri_selected);
  1976. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1977. supregset_reset(regs_to_spill_set,false,$ffff);
  1978. {$ifdef DEBUG_SPILLCOALESCE}
  1979. writeln('trgobj.spill_registers: Got maxreg ',maxreg);
  1980. writeln('trgobj.spill_registers: Spilling ',spillednodes.length,' nodes');
  1981. {$endif DEBUG_SPILLCOALESCE}
  1982. { after each round of spilling, more registers could be used due to allocations for spilling }
  1983. if Length(spillinfo)<maxreg then
  1984. begin
  1985. j:=Length(spillinfo);
  1986. SetLength(spillinfo,maxreg);
  1987. fillchar(spillinfo[j],sizeof(spillinfo[0])*(Length(spillinfo)-j),0);
  1988. end;
  1989. { Allocate temps and insert in front of the list }
  1990. templist:=TAsmList.create;
  1991. { Safe: this procedure is only called if there are spilled nodes. }
  1992. with spillednodes do
  1993. { the node with the highest interferences is the last one }
  1994. for i:=length-1 downto 0 do
  1995. begin
  1996. t:=buf^[i];
  1997. {$ifdef DEBUG_SPILLCOALESCE}
  1998. writeln('trgobj.spill_registers: Spilling ',t);
  1999. {$endif DEBUG_SPILLCOALESCE}
  2000. spillinfo[t].interferences:=Tinterferencebitmap.create;
  2001. { copy interferences }
  2002. for j:=0 to maxreg-1 do
  2003. spillinfo[t].interferences[0,j]:=ibitmap[t,j];
  2004. { Alternative representation. }
  2005. supregset_include(regs_to_spill_set,t);
  2006. { Clear all interferences of the spilled register. }
  2007. clear_interferences(t);
  2008. getnewspillloc:=true;
  2009. { check if we can "coalesce" spilled nodes. To do so, it is required that they do not
  2010. interfere but are connected by a move instruction
  2011. doing so might save some mem->mem moves }
  2012. if (cs_opt_level3 in current_settings.optimizerswitches) and assigned(reginfo[t].movelist) then
  2013. for j:=0 to reginfo[t].movelist^.header.count-1 do
  2014. begin
  2015. x:=Tmoveins(reginfo[t].movelist^.data[j]).x;
  2016. y:=Tmoveins(reginfo[t].movelist^.data[j]).y;
  2017. if (x=t) and
  2018. (spillinfo[get_alias(y)].spilled) and
  2019. not(spillinfo[get_alias(y)].interferences[0,t]) then
  2020. begin
  2021. spill_temps^[t]:=spillinfo[get_alias(y)].spilllocation;
  2022. {$ifdef DEBUG_SPILLCOALESCE}
  2023. writeln('trgobj.spill_registers: Spill coalesce ',t,' to ',y);
  2024. {$endif DEBUG_SPILLCOALESCE}
  2025. getnewspillloc:=false;
  2026. break;
  2027. end
  2028. else if (y=t) and
  2029. (spillinfo[get_alias(x)].spilled) and
  2030. not(spillinfo[get_alias(x)].interferences[0,t]) then
  2031. begin
  2032. {$ifdef DEBUG_SPILLCOALESCE}
  2033. writeln('trgobj.spill_registers: Spill coalesce ',t,' to ',x);
  2034. {$endif DEBUG_SPILLCOALESCE}
  2035. spill_temps^[t]:=spillinfo[get_alias(x)].spilllocation;
  2036. getnewspillloc:=false;
  2037. break;
  2038. end;
  2039. end;
  2040. if getnewspillloc then
  2041. get_spill_temp(templist,spill_temps,t);
  2042. {$ifdef DEBUG_SPILLCOALESCE}
  2043. writeln('trgobj.spill_registers: Spill temp: ',getsupreg(spill_temps^[t].base),'+',spill_temps^[t].offset);
  2044. {$endif DEBUG_SPILLCOALESCE}
  2045. { set spilled only as soon as a temp is assigned, else a mov iregX,iregX results in a spill coalesce with itself }
  2046. spillinfo[t].spilled:=true;
  2047. spillinfo[t].spilllocation:=spill_temps^[t];
  2048. end;
  2049. list.insertlistafter(headertai,templist);
  2050. templist.free;
  2051. { Walk through all instructions, we can start with the headertai,
  2052. because before the header tai is only symbols }
  2053. p:=headertai;
  2054. while assigned(p) do
  2055. begin
  2056. case p.typ of
  2057. ait_regalloc:
  2058. with Tai_regalloc(p) do
  2059. begin
  2060. if (getregtype(reg)=regtype) then
  2061. begin
  2062. {A register allocation of a spilled register can be removed.}
  2063. supreg:=getsupreg(reg);
  2064. if supregset_in(regs_to_spill_set,supreg) then
  2065. begin
  2066. q:=Tai(p.next);
  2067. list.remove(p);
  2068. p.free;
  2069. p:=q;
  2070. continue;
  2071. end
  2072. else
  2073. begin
  2074. case ratype of
  2075. ra_alloc :
  2076. live_registers.add(supreg);
  2077. ra_dealloc :
  2078. live_registers.delete(supreg);
  2079. end;
  2080. end;
  2081. end;
  2082. end;
  2083. {$ifdef llvm}
  2084. ait_llvmins,
  2085. {$endif llvm}
  2086. ait_instruction:
  2087. with tai_cpu_abstract_sym(p) do
  2088. begin
  2089. // writeln(gas_op2str[tai_cpu_abstract_sym(p).opcode]);
  2090. current_filepos:=fileinfo;
  2091. if instr_spill_register(list,tai_cpu_abstract_sym(p),regs_to_spill_set,spill_temps^) then
  2092. spill_registers:=true;
  2093. end;
  2094. end;
  2095. p:=Tai(p.next);
  2096. end;
  2097. current_filepos:=current_procinfo.exitpos;
  2098. {Safe: this procedure is only called if there are spilled nodes.}
  2099. with spillednodes do
  2100. for i:=0 to length-1 do
  2101. tg.ungettemp(list,spill_temps^[buf^[i]]);
  2102. freemem(spill_temps);
  2103. end;
  2104. function trgobj.do_spill_replace(list:TAsmList;instr:tai_cpu_abstract_sym;orgreg:tsuperregister;const spilltemp:treference):boolean;
  2105. begin
  2106. result:=false;
  2107. end;
  2108. procedure trgobj.do_spill_read(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister;orgsupreg:tsuperregister);
  2109. var
  2110. ins:tai_cpu_abstract_sym;
  2111. begin
  2112. ins:=spilling_create_load(spilltemp,tempreg);
  2113. add_cpu_interferences(ins);
  2114. list.insertafter(ins,pos);
  2115. {$ifdef DEBUG_SPILLING}
  2116. list.Insertbefore(tai_comment.Create(strpnew('Spilling: Spill Read')),ins);
  2117. {$endif}
  2118. end;
  2119. procedure Trgobj.do_spill_written(list:TAsmList;pos:tai;const spilltemp:treference;tempreg:tregister;orgsupreg:tsuperregister);
  2120. var
  2121. ins:tai_cpu_abstract_sym;
  2122. begin
  2123. ins:=spilling_create_store(tempreg,spilltemp);
  2124. add_cpu_interferences(ins);
  2125. list.insertafter(ins,pos);
  2126. {$ifdef DEBUG_SPILLING}
  2127. list.Insertbefore(tai_comment.Create(strpnew('Spilling: Spill Write')),ins);
  2128. {$endif}
  2129. end;
  2130. function trgobj.get_spill_subreg(r : tregister) : tsubregister;
  2131. begin
  2132. result:=defaultsub;
  2133. end;
  2134. function trgobj.addreginfo(var regs: tspillregsinfo; const r: tsuperregisterset; reg: tregister; operation: topertype): boolean;
  2135. var
  2136. i, tmpindex: longint;
  2137. supreg: tsuperregister;
  2138. begin
  2139. result:=false;
  2140. tmpindex := regs.reginfocount;
  2141. supreg := get_alias(getsupreg(reg));
  2142. { did we already encounter this register? }
  2143. for i := 0 to pred(regs.reginfocount) do
  2144. if (regs.reginfo[i].orgreg = supreg) then
  2145. begin
  2146. tmpindex := i;
  2147. break;
  2148. end;
  2149. if tmpindex > high(regs.reginfo) then
  2150. internalerror(2003120301);
  2151. regs.reginfo[tmpindex].orgreg := supreg;
  2152. include(regs.reginfo[tmpindex].spillregconstraints,get_spill_subreg(reg));
  2153. if supregset_in(r,supreg) then
  2154. begin
  2155. { add/update info on this register }
  2156. regs.reginfo[tmpindex].mustbespilled := true;
  2157. case operation of
  2158. operand_read:
  2159. regs.reginfo[tmpindex].regread := true;
  2160. operand_write:
  2161. regs.reginfo[tmpindex].regwritten := true;
  2162. operand_readwrite:
  2163. begin
  2164. regs.reginfo[tmpindex].regread := true;
  2165. regs.reginfo[tmpindex].regwritten := true;
  2166. end;
  2167. end;
  2168. result:=true;
  2169. end;
  2170. inc(regs.reginfocount,ord(regs.reginfocount=tmpindex));
  2171. end;
  2172. function trgobj.instr_get_oper_spilling_info(var regs: tspillregsinfo; const r: tsuperregisterset; instr: tai_cpu_abstract_sym; opidx: longint): boolean;
  2173. begin
  2174. result:=false;
  2175. with instr.oper[opidx]^ do
  2176. begin
  2177. case typ of
  2178. top_reg:
  2179. begin
  2180. if (getregtype(reg) = regtype) then
  2181. result:=addreginfo(regs,r,reg,instr.spilling_get_operation_type(opidx));
  2182. end;
  2183. top_ref:
  2184. begin
  2185. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  2186. with ref^ do
  2187. begin
  2188. if (base <> NR_NO) and
  2189. (getregtype(base)=regtype) then
  2190. result:=addreginfo(regs,r,base,instr.spilling_get_operation_type_ref(opidx,base));
  2191. if (index <> NR_NO) and
  2192. (getregtype(index)=regtype) then
  2193. result:=addreginfo(regs,r,index,instr.spilling_get_operation_type_ref(opidx,index)) or result;
  2194. {$if defined(x86)}
  2195. if (segment <> NR_NO) and
  2196. (getregtype(segment)=regtype) then
  2197. result:=addreginfo(regs,r,segment,instr.spilling_get_operation_type_ref(opidx,segment)) or result;
  2198. {$endif defined(x86)}
  2199. end;
  2200. end;
  2201. {$ifdef ARM}
  2202. top_shifterop:
  2203. begin
  2204. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  2205. if shifterop^.rs<>NR_NO then
  2206. result:=addreginfo(regs,r,shifterop^.rs,operand_read);
  2207. end;
  2208. {$endif ARM}
  2209. end;
  2210. end;
  2211. end;
  2212. procedure trgobj.try_replace_reg(const regs: tspillregsinfo; var reg: tregister; useloadreg: boolean);
  2213. var
  2214. i: longint;
  2215. supreg: tsuperregister;
  2216. begin
  2217. supreg:=get_alias(getsupreg(reg));
  2218. for i:=0 to pred(regs.reginfocount) do
  2219. if (regs.reginfo[i].mustbespilled) and
  2220. (regs.reginfo[i].orgreg=supreg) then
  2221. begin
  2222. { Only replace supreg }
  2223. if useloadreg then
  2224. setsupreg(reg, getsupreg(regs.reginfo[i].loadreg))
  2225. else
  2226. setsupreg(reg, getsupreg(regs.reginfo[i].storereg));
  2227. break;
  2228. end;
  2229. end;
  2230. procedure trgobj.substitute_spilled_registers(const regs: tspillregsinfo; instr: tai_cpu_abstract_sym; opidx: longint);
  2231. begin
  2232. with instr.oper[opidx]^ do
  2233. case typ of
  2234. top_reg:
  2235. begin
  2236. if (getregtype(reg) = regtype) then
  2237. try_replace_reg(regs, reg, not ssa_safe or
  2238. (instr.spilling_get_operation_type(opidx)=operand_read));
  2239. end;
  2240. top_ref:
  2241. begin
  2242. if regtype in [R_INTREGISTER, R_ADDRESSREGISTER] then
  2243. begin
  2244. if (ref^.base <> NR_NO) and
  2245. (getregtype(ref^.base)=regtype) then
  2246. try_replace_reg(regs, ref^.base,
  2247. not ssa_safe or (instr.spilling_get_operation_type_ref(opidx, ref^.base)=operand_read));
  2248. if (ref^.index <> NR_NO) and
  2249. (getregtype(ref^.index)=regtype) then
  2250. try_replace_reg(regs, ref^.index,
  2251. not ssa_safe or (instr.spilling_get_operation_type_ref(opidx, ref^.index)=operand_read));
  2252. {$if defined(x86)}
  2253. if (ref^.segment <> NR_NO) and
  2254. (getregtype(ref^.segment)=regtype) then
  2255. try_replace_reg(regs, ref^.segment, true { always read-only });
  2256. {$endif defined(x86)}
  2257. end;
  2258. end;
  2259. {$ifdef ARM}
  2260. top_shifterop:
  2261. begin
  2262. if regtype in [R_INTREGISTER, R_ADDRESSREGISTER] then
  2263. try_replace_reg(regs, shifterop^.rs, true { always read-only });
  2264. end;
  2265. {$endif ARM}
  2266. end;
  2267. end;
  2268. function trgobj.instr_spill_register(list:TAsmList;
  2269. instr:tai_cpu_abstract_sym;
  2270. const r:Tsuperregisterset;
  2271. const spilltemplist:Tspill_temp_list): boolean;
  2272. var
  2273. counter: longint;
  2274. regs: tspillregsinfo;
  2275. spilled: boolean;
  2276. var
  2277. loadpos,
  2278. storepos : tai;
  2279. oldlive_registers : tsuperregisterworklist;
  2280. begin
  2281. result := false;
  2282. fillchar(regs,sizeof(regs),0);
  2283. for counter := low(regs.reginfo) to high(regs.reginfo) do
  2284. begin
  2285. regs.reginfo[counter].orgreg := RS_INVALID;
  2286. regs.reginfo[counter].loadreg := NR_INVALID;
  2287. regs.reginfo[counter].storereg := NR_INVALID;
  2288. end;
  2289. spilled := false;
  2290. { check whether and if so which and how (read/written) this instructions contains
  2291. registers that must be spilled }
  2292. for counter := 0 to instr.ops-1 do
  2293. spilled:=instr_get_oper_spilling_info(regs,r,instr,counter) or spilled;
  2294. { if no spilling for this instruction we can leave }
  2295. if not spilled then
  2296. exit;
  2297. {$if defined(x86) or defined(mips) or defined(sparcgen) or defined(arm) or defined(m68k)}
  2298. { Try replacing the register with the spilltemp. This is useful only
  2299. for the i386,x86_64 that support memory locations for several instructions
  2300. For non-x86 it is nevertheless possible to replace moves to/from the register
  2301. with loads/stores to spilltemp (Sergei) }
  2302. for counter := 0 to pred(regs.reginfocount) do
  2303. with regs.reginfo[counter] do
  2304. begin
  2305. if mustbespilled then
  2306. begin
  2307. if do_spill_replace(list,instr,orgreg,spilltemplist[orgreg]) then
  2308. mustbespilled:=false;
  2309. end;
  2310. end;
  2311. {$endif defined(x86) or defined(mips) or defined(sparcgen) or defined(arm) or defined(m68k)}
  2312. {
  2313. There are registers that need are spilled. We generate the
  2314. following code for it. The used positions where code need
  2315. to be inserted are marked using #. Note that code is always inserted
  2316. before the positions using pos.previous. This way the position is always
  2317. the same since pos doesn't change, but pos.previous is modified everytime
  2318. new code is inserted.
  2319. [
  2320. - reg_allocs load spills
  2321. - load spills
  2322. ]
  2323. [#loadpos
  2324. - reg_deallocs
  2325. - reg_allocs
  2326. ]
  2327. [
  2328. - reg_deallocs for load-only spills
  2329. - reg_allocs for store-only spills
  2330. ]
  2331. [#instr
  2332. - original instruction
  2333. ]
  2334. [
  2335. - store spills
  2336. - reg_deallocs store spills
  2337. ]
  2338. [#storepos
  2339. ]
  2340. }
  2341. result := true;
  2342. oldlive_registers.copyfrom(live_registers);
  2343. { Process all tai_regallocs belonging to this instruction, ignore explicit
  2344. inserted regallocs. These can happend for example in i386:
  2345. mov ref,ireg26
  2346. <regdealloc ireg26, instr=taicpu of lea>
  2347. <regalloc edi, insrt=nil>
  2348. lea [ireg26+ireg17],edi
  2349. All released registers are also added to the live_registers because
  2350. they can't be used during the spilling }
  2351. loadpos:=tai(instr.previous);
  2352. while assigned(loadpos) and
  2353. (loadpos.typ=ait_regalloc) and
  2354. ((tai_regalloc(loadpos).instr=nil) or
  2355. (tai_regalloc(loadpos).instr=instr)) do
  2356. begin
  2357. { Only add deallocs belonging to the instruction. Explicit inserted deallocs
  2358. belong to the previous instruction and not the current instruction }
  2359. if (tai_regalloc(loadpos).instr=instr) and
  2360. (tai_regalloc(loadpos).ratype=ra_dealloc) then
  2361. live_registers.add(getsupreg(tai_regalloc(loadpos).reg));
  2362. loadpos:=tai(loadpos.previous);
  2363. end;
  2364. loadpos:=tai(loadpos.next);
  2365. { Load the spilled registers }
  2366. for counter := 0 to pred(regs.reginfocount) do
  2367. with regs.reginfo[counter] do
  2368. begin
  2369. if mustbespilled and regread then
  2370. begin
  2371. loadreg:=getregisterinline(list,regs.reginfo[counter].spillregconstraints);
  2372. do_spill_read(list,tai(loadpos.previous),spilltemplist[orgreg],loadreg,orgreg);
  2373. end;
  2374. end;
  2375. { Release temp registers of read-only registers, and add reference of the instruction
  2376. to the reginfo }
  2377. for counter := 0 to pred(regs.reginfocount) do
  2378. with regs.reginfo[counter] do
  2379. begin
  2380. if mustbespilled and regread and
  2381. (ssa_safe or
  2382. not regwritten) then
  2383. begin
  2384. { The original instruction will be the next that uses this register
  2385. set weigth of the newly allocated register higher than the old one,
  2386. so it will selected for spilling with a lower priority than
  2387. the original one, this prevents an endless spilling loop if orgreg
  2388. is short living, see e.g. tw25164.pp }
  2389. add_reg_instruction(instr,loadreg,reginfo[orgreg].weight+1);
  2390. ungetregisterinline(list,loadreg);
  2391. end;
  2392. end;
  2393. { Allocate temp registers of write-only registers, and add reference of the instruction
  2394. to the reginfo }
  2395. for counter := 0 to pred(regs.reginfocount) do
  2396. with regs.reginfo[counter] do
  2397. begin
  2398. if mustbespilled and regwritten then
  2399. begin
  2400. { When the register is also loaded there is already a register assigned }
  2401. if (not regread) or
  2402. ssa_safe then
  2403. begin
  2404. storereg:=getregisterinline(list,regs.reginfo[counter].spillregconstraints);
  2405. { we also use loadreg for store replacements in case we
  2406. don't have ensure ssa -> initialise loadreg even if
  2407. there are no reads }
  2408. if not regread then
  2409. loadreg:=storereg;
  2410. end
  2411. else
  2412. storereg:=loadreg;
  2413. { The original instruction will be the next that uses this register, this
  2414. also needs to be done for read-write registers,
  2415. set weigth of the newly allocated register higher than the old one,
  2416. so it will selected for spilling with a lower priority than
  2417. the original one, this prevents an endless spilling loop if orgreg
  2418. is short living, see e.g. tw25164.pp }
  2419. add_reg_instruction(instr,storereg,reginfo[orgreg].weight+1);
  2420. end;
  2421. end;
  2422. { store the spilled registers }
  2423. if not assigned(instr.next) then
  2424. list.concat(tai_marker.Create(mark_Position));
  2425. storepos:=tai(instr.next);
  2426. for counter := 0 to pred(regs.reginfocount) do
  2427. with regs.reginfo[counter] do
  2428. begin
  2429. if mustbespilled and regwritten then
  2430. begin
  2431. do_spill_written(list,tai(storepos.previous),spilltemplist[orgreg],storereg,orgreg);
  2432. ungetregisterinline(list,storereg);
  2433. end;
  2434. end;
  2435. { now all spilling code is generated we can restore the live registers. This
  2436. must be done after the store because the store can need an extra register
  2437. that also needs to conflict with the registers of the instruction }
  2438. live_registers.done;
  2439. live_registers:=oldlive_registers;
  2440. { substitute registers }
  2441. for counter:=0 to instr.ops-1 do
  2442. substitute_spilled_registers(regs,instr,counter);
  2443. { We have modified the instruction; perhaps the new instruction has
  2444. certain constraints regarding which imaginary registers interfere
  2445. with certain physical registers. }
  2446. add_cpu_interferences(instr);
  2447. end;
  2448. end.