rgobj.pas 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the base class for the register allocator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. { Allow duplicate allocations, can be used to get the .s file written }
  20. { $define ALLOWDUPREG}
  21. {#******************************************************************************
  22. @abstract(Abstract register allocator unit)
  23. Register allocator introduction.
  24. Free Pascal uses a Chaitin style register allocator. We use a variant similair
  25. to the one described in the book "Modern compiler implementation in C" by
  26. Andrew W. Appel., published by Cambridge University Press.
  27. The register allocator that is described by Appel uses a much improved way
  28. of register coalescing, called "iterated register coalescing". Instead
  29. of doing coalescing as a prepass to the register allocation, the coalescing
  30. is done inside the register allocator. This has the advantage that the
  31. register allocator can coalesce very aggresively without introducing spills.
  32. Reading this book is recommended for a complete understanding. Here is a small
  33. introduction.
  34. The code generator thinks it has an infinite amount of registers. Our processor
  35. has a limited amount of registers. Therefore we must reduce the amount of
  36. registers until there are less enough to fit into the processors registers.
  37. Registers can interfere or not interfere. If two imaginary registers interfere
  38. they cannot be placed into the same psysical register. Reduction of registers
  39. is done by:
  40. - "coalescing" Two registers that do not interfere are combined
  41. into one register.
  42. - "spilling" A register is changed into a memory location and the generated
  43. code is modified to use the memory location instead of the register.
  44. Register allocation is a graph colouring problem. Each register is a colour, and
  45. if two registers interfere there is a connection between them in the graph.
  46. In addition to the imaginary registers in the code generator, the psysical
  47. CPU registers are also present in this graph. This allows us to make
  48. interferences between imaginary registers and cpu registers. This is very
  49. usefull for describing architectural constraints, like for example that
  50. the div instruction modifies edx, so variables that are in use at that time
  51. cannot be stored into edx. This can be modelled by making edx interfere
  52. with those variables.
  53. Graph colouring is an NP complete problem. Therefore we use an approximation
  54. that pushes registers to colour on to a stack. This is done in the "simplify"
  55. procedure.
  56. The register allocator first checks which registers are a candidate for
  57. coalescing.
  58. *******************************************************************************}
  59. unit rgobj;
  60. interface
  61. uses
  62. cutils, cpubase,
  63. aasmbase,aasmtai,aasmcpu,
  64. cclasses,globtype,cgbase,node,
  65. {$ifdef delphi}
  66. dmisc,
  67. {$endif}
  68. cpuinfo
  69. ;
  70. type
  71. {
  72. regvarother_longintarray = array[tregisterindex] of longint;
  73. regvarother_booleanarray = array[tregisterindex] of boolean;
  74. regvarint_longintarray = array[first_int_supreg..last_int_supreg] of longint;
  75. regvarint_ptreearray = array[first_int_supreg..last_int_supreg] of tnode;
  76. }
  77. {
  78. The interference bitmap contains of 2 layers:
  79. layer 1 - 256*256 blocks with pointers to layer 2 blocks
  80. layer 2 - blocks of 32*256 (32 bytes = 256 bits)
  81. }
  82. Tinterferencebitmap2 = array[byte] of set of byte;
  83. Pinterferencebitmap2 = ^Tinterferencebitmap2;
  84. Tinterferencebitmap1 = array[byte] of Pinterferencebitmap2;
  85. pinterferencebitmap1 = ^tinterferencebitmap1;
  86. Tinterferencebitmap=class
  87. private
  88. maxx1,
  89. maxy1 : byte;
  90. fbitmap : pinterferencebitmap1;
  91. function getbitmap(x,y:tsuperregister):boolean;
  92. procedure setbitmap(x,y:tsuperregister;b:boolean);
  93. public
  94. constructor create;
  95. destructor destroy;override;
  96. property bitmap[x,y:tsuperregister]:boolean read getbitmap write setbitmap;default;
  97. end;
  98. Tmovelistheader=record
  99. count,
  100. maxcount,
  101. sorted_until : cardinal;
  102. end;
  103. Tmovelist=record
  104. header : Tmovelistheader;
  105. data : array[tsuperregister] of Tlinkedlistitem;
  106. end;
  107. Pmovelist=^Tmovelist;
  108. {In the register allocator we keep track of move instructions.
  109. These instructions are moved between five linked lists. There
  110. is also a linked list per register to keep track about the moves
  111. it is associated with. Because we need to determine quickly in
  112. which of the five lists it is we add anu enumeradtion to each
  113. move instruction.}
  114. Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
  115. ms_worklist_moves,ms_active_moves);
  116. Tmoveins=class(Tlinkedlistitem)
  117. moveset:Tmoveset;
  118. x,y:Tsuperregister;
  119. end;
  120. Treginfoflag=(ri_coalesced,ri_selected);
  121. Treginfoflagset=set of Treginfoflag;
  122. Treginfo=record
  123. live_start,
  124. live_end : Tai;
  125. subreg : tsubregister;
  126. alias : Tsuperregister;
  127. { The register allocator assigns each register a colour }
  128. colour : Tsuperregister;
  129. movelist : Pmovelist;
  130. adjlist : Psuperregisterworklist;
  131. degree : TSuperregister;
  132. flags : Treginfoflagset;
  133. end;
  134. Preginfo=^TReginfo;
  135. tspillreginfo = record
  136. orgreg : tsuperregister;
  137. tempreg : tregister;
  138. regread,regwritten, mustbespilled: boolean;
  139. end;
  140. tspillregsinfo = array[0..2] of tspillreginfo;
  141. {#------------------------------------------------------------------
  142. This class implements the default register allocator. It is used by the
  143. code generator to allocate and free registers which might be valid
  144. across nodes. It also contains utility routines related to registers.
  145. Some of the methods in this class should be overriden
  146. by cpu-specific implementations.
  147. --------------------------------------------------------------------}
  148. trgobj=class
  149. preserved_by_proc : tcpuregisterset;
  150. used_in_proc : tcpuregisterset;
  151. // is_reg_var : Tsuperregisterset; {old regvars}
  152. // reg_var_loaded:Tsuperregisterset; {old regvars}
  153. constructor create(Aregtype:Tregistertype;
  154. Adefaultsub:Tsubregister;
  155. const Ausable:array of tsuperregister;
  156. Afirst_imaginary:Tsuperregister;
  157. Apreserved_by_proc:Tcpuregisterset);
  158. destructor destroy;override;
  159. {# Allocate a register. An internalerror will be generated if there is
  160. no more free registers which can be allocated.}
  161. function getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;virtual;
  162. {# Get the register specified.}
  163. procedure getexplicitregister(list:Taasmoutput;r:Tregister);virtual;
  164. {# Get multiple registers specified.}
  165. procedure allocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);virtual;
  166. {# Free multiple registers specified.}
  167. procedure deallocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);virtual;
  168. function uses_registers:boolean;virtual;
  169. {# Deallocate any kind of register }
  170. procedure ungetregister(list:Taasmoutput;r:Tregister);virtual;
  171. procedure add_reg_instruction(instr:Tai;r:tregister);
  172. procedure add_move_instruction(instr:Taicpu);
  173. {# Do the register allocation.}
  174. procedure do_register_allocation(list:Taasmoutput;headertai:tai);virtual;
  175. { Adds an interference edge.
  176. don't move this to the protected section, the arm cg requires to access this (FK) }
  177. procedure add_edge(u,v:Tsuperregister);
  178. protected
  179. regtype : Tregistertype;
  180. { default subregister used }
  181. defaultsub : tsubregister;
  182. live_registers:Tsuperregisterworklist;
  183. { can be overriden to add cpu specific interferences }
  184. procedure add_cpu_interferences(p : tai);virtual;
  185. function get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister):Tai;
  186. procedure forward_allocation(pfrom,pto:Tai);
  187. procedure getregisterinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
  188. procedure ungetregisterinline(list:Taasmoutput;position:Tai;r:Tregister);
  189. procedure add_constraints(reg:Tregister);virtual;
  190. procedure do_spill_read(list:Taasmoutput;instr:Taicpu_abstract;
  191. pos:Tai;regidx:word;
  192. const spilltemplist:Tspill_temp_list;
  193. const regs:Tspillregsinfo);virtual;
  194. procedure do_spill_written(list:Taasmoutput;instr:Taicpu_abstract;
  195. pos:Tai;regidx:word;
  196. const spilltemplist:Tspill_temp_list;
  197. const regs:Tspillregsinfo);virtual;
  198. procedure do_spill_readwritten(list:Taasmoutput;instr:Taicpu_abstract;
  199. pos:Tai;regidx:word;
  200. const spilltemplist:Tspill_temp_list;
  201. const regs:Tspillregsinfo);virtual;
  202. function instr_spill_register(list:Taasmoutput;
  203. instr:taicpu_abstract;
  204. const r:Tsuperregisterset;
  205. const spilltemplist:Tspill_temp_list): boolean;virtual;
  206. private
  207. {# First imaginary register.}
  208. first_imaginary : Tsuperregister;
  209. {# Highest register allocated until now.}
  210. reginfo : PReginfo;
  211. maxreginfo,
  212. maxreginfoinc,
  213. maxreg : Tsuperregister;
  214. usable_registers_cnt : word;
  215. usable_registers : array[0..maxcpuregister-1] of tsuperregister;
  216. ibitmap : Tinterferencebitmap;
  217. spillednodes,
  218. simplifyworklist,
  219. freezeworklist,
  220. spillworklist,
  221. coalescednodes,
  222. selectstack : tsuperregisterworklist;
  223. worklist_moves,
  224. active_moves,
  225. frozen_moves,
  226. coalesced_moves,
  227. constrained_moves : Tlinkedlist;
  228. {$ifdef EXTDEBUG}
  229. procedure writegraph(loopidx:longint);
  230. {$endif EXTDEBUG}
  231. {# Disposes of the reginfo array.}
  232. procedure dispose_reginfo;
  233. {# Prepare the register colouring.}
  234. procedure prepare_colouring;
  235. {# Clean up after register colouring.}
  236. procedure epilogue_colouring;
  237. {# Colour the registers; that is do the register allocation.}
  238. procedure colour_registers;
  239. {# Spills certain registers in the specified assembler list.}
  240. procedure insert_regalloc_info(list:Taasmoutput;headertai:tai);
  241. procedure generate_interference_graph(list:Taasmoutput;headertai:tai);
  242. procedure translate_registers(list:Taasmoutput);
  243. function spill_registers(list:Taasmoutput;headertai:tai):boolean;virtual;
  244. function getnewreg(subreg:tsubregister):tsuperregister;
  245. procedure add_edges_used(u:Tsuperregister);
  246. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  247. function move_related(n:Tsuperregister):boolean;
  248. procedure make_work_list;
  249. procedure sort_simplify_worklist;
  250. procedure enable_moves(n:Tsuperregister);
  251. procedure decrement_degree(m:Tsuperregister);
  252. procedure simplify;
  253. function get_alias(n:Tsuperregister):Tsuperregister;
  254. procedure add_worklist(u:Tsuperregister);
  255. function adjacent_ok(u,v:Tsuperregister):boolean;
  256. function conservative(u,v:Tsuperregister):boolean;
  257. procedure combine(u,v:Tsuperregister);
  258. procedure coalesce;
  259. procedure freeze_moves(u:Tsuperregister);
  260. procedure freeze;
  261. procedure select_spill;
  262. procedure assign_colours;
  263. procedure clear_interferences(u:Tsuperregister);
  264. end;
  265. const
  266. first_reg = 0;
  267. last_reg = high(tsuperregister)-1;
  268. maxspillingcounter = 20;
  269. implementation
  270. uses
  271. systems,
  272. globals,verbose,tgobj,procinfo;
  273. procedure sort_movelist(ml:Pmovelist);
  274. {Ok, sorting pointers is silly, but it does the job to make Trgobj.combine
  275. faster.}
  276. var h,i,p:word;
  277. t:Tlinkedlistitem;
  278. begin
  279. with ml^ do
  280. begin
  281. if header.count<2 then
  282. exit;
  283. p:=1;
  284. while 2*p<header.count do
  285. p:=2*p;
  286. while p<>0 do
  287. begin
  288. for h:=p to header.count-1 do
  289. begin
  290. i:=h;
  291. t:=data[i];
  292. repeat
  293. if ptrint(data[i-p])<=ptrint(t) then
  294. break;
  295. data[i]:=data[i-p];
  296. dec(i,p);
  297. until i<p;
  298. data[i]:=t;
  299. end;
  300. p:=p shr 1;
  301. end;
  302. header.sorted_until:=header.count-1;
  303. end;
  304. end;
  305. {******************************************************************************
  306. tinterferencebitmap
  307. ******************************************************************************}
  308. constructor tinterferencebitmap.create;
  309. begin
  310. inherited create;
  311. maxx1:=1;
  312. getmem(fbitmap,sizeof(tinterferencebitmap1)*2);
  313. fillchar(fbitmap^,sizeof(tinterferencebitmap1)*2,0);
  314. end;
  315. destructor tinterferencebitmap.destroy;
  316. var i,j:byte;
  317. begin
  318. for i:=0 to maxx1 do
  319. for j:=0 to maxy1 do
  320. if assigned(fbitmap[i,j]) then
  321. dispose(fbitmap[i,j]);
  322. freemem(fbitmap);
  323. end;
  324. function tinterferencebitmap.getbitmap(x,y:tsuperregister):boolean;
  325. var
  326. page : pinterferencebitmap2;
  327. begin
  328. result:=false;
  329. if (x shr 8>maxx1) then
  330. exit;
  331. page:=fbitmap[x shr 8,y shr 8];
  332. result:=assigned(page) and
  333. ((x and $ff) in page^[y and $ff]);
  334. end;
  335. procedure tinterferencebitmap.setbitmap(x,y:tsuperregister;b:boolean);
  336. var
  337. x1,y1 : byte;
  338. begin
  339. x1:=x shr 8;
  340. y1:=y shr 8;
  341. if x1>maxx1 then
  342. begin
  343. reallocmem(fbitmap,sizeof(tinterferencebitmap1)*(x1+1));
  344. fillchar(fbitmap[maxx1+1],sizeof(tinterferencebitmap1)*(x1-maxx1),0);
  345. maxx1:=x1;
  346. end;
  347. if not assigned(fbitmap[x1,y1]) then
  348. begin
  349. if y1>maxy1 then
  350. maxy1:=y1;
  351. new(fbitmap[x1,y1]);
  352. fillchar(fbitmap[x1,y1]^,sizeof(tinterferencebitmap2),0);
  353. end;
  354. if b then
  355. include(fbitmap[x1,y1]^[y and $ff],(x and $ff))
  356. else
  357. exclude(fbitmap[x1,y1]^[y and $ff],(x and $ff));
  358. end;
  359. {******************************************************************************
  360. trgobj
  361. ******************************************************************************}
  362. constructor trgobj.create(Aregtype:Tregistertype;
  363. Adefaultsub:Tsubregister;
  364. const Ausable:array of tsuperregister;
  365. Afirst_imaginary:Tsuperregister;
  366. Apreserved_by_proc:Tcpuregisterset);
  367. var
  368. i : Tsuperregister;
  369. begin
  370. { empty super register sets can cause very strange problems }
  371. if high(Ausable)=0 then
  372. internalerror(200210181);
  373. first_imaginary:=Afirst_imaginary;
  374. maxreg:=Afirst_imaginary;
  375. regtype:=Aregtype;
  376. defaultsub:=Adefaultsub;
  377. preserved_by_proc:=Apreserved_by_proc;
  378. used_in_proc:=[];
  379. live_registers.init;
  380. { Get reginfo for CPU registers }
  381. maxreginfo:=first_imaginary;
  382. maxreginfoinc:=16;
  383. worklist_moves:=Tlinkedlist.create;
  384. reginfo:=allocmem(first_imaginary*sizeof(treginfo));
  385. for i:=0 to first_imaginary-1 do
  386. begin
  387. reginfo[i].degree:=high(tsuperregister);
  388. reginfo[i].alias:=RS_INVALID;
  389. end;
  390. { Usable registers }
  391. fillchar(usable_registers,sizeof(usable_registers),0);
  392. for i:=low(Ausable) to high(Ausable) do
  393. usable_registers[i]:=Ausable[i];
  394. usable_registers_cnt:=high(Ausable)+1;
  395. { Initialize Worklists }
  396. spillednodes.init;
  397. simplifyworklist.init;
  398. freezeworklist.init;
  399. spillworklist.init;
  400. coalescednodes.init;
  401. selectstack.init;
  402. end;
  403. destructor trgobj.destroy;
  404. begin
  405. spillednodes.done;
  406. simplifyworklist.done;
  407. freezeworklist.done;
  408. spillworklist.done;
  409. coalescednodes.done;
  410. selectstack.done;
  411. live_registers.done;
  412. worklist_moves.free;
  413. dispose_reginfo;
  414. end;
  415. procedure Trgobj.dispose_reginfo;
  416. var i:Tsuperregister;
  417. begin
  418. if reginfo<>nil then
  419. begin
  420. for i:=0 to maxreg-1 do
  421. with reginfo[i] do
  422. begin
  423. if adjlist<>nil then
  424. dispose(adjlist,done);
  425. if movelist<>nil then
  426. dispose(movelist);
  427. end;
  428. freemem(reginfo);
  429. reginfo:=nil;
  430. end;
  431. end;
  432. function trgobj.getnewreg(subreg:tsubregister):tsuperregister;
  433. var
  434. oldmaxreginfo : tsuperregister;
  435. begin
  436. result:=maxreg;
  437. inc(maxreg);
  438. if maxreg>=last_reg then
  439. internalerror(200310146);
  440. if maxreg>=maxreginfo then
  441. begin
  442. oldmaxreginfo:=maxreginfo;
  443. inc(maxreginfo,maxreginfoinc);
  444. if maxreginfoinc<256 then
  445. maxreginfoinc:=maxreginfoinc*2;
  446. reallocmem(reginfo,maxreginfo*sizeof(treginfo));
  447. { Do we really need it to clear it ? At least for 1.0.x (PFV) }
  448. fillchar(reginfo[oldmaxreginfo],(maxreginfo-oldmaxreginfo)*sizeof(treginfo),0);
  449. end;
  450. reginfo[result].subreg:=subreg;
  451. end;
  452. function trgobj.getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;
  453. begin
  454. {$ifdef EXTDEBUG}
  455. if reginfo=nil then
  456. InternalError(2004020901);
  457. {$endif EXTDEBUG}
  458. if defaultsub=R_SUBNONE then
  459. result:=newreg(regtype,getnewreg(R_SUBNONE),R_SUBNONE)
  460. else
  461. result:=newreg(regtype,getnewreg(subreg),subreg);
  462. end;
  463. function trgobj.uses_registers:boolean;
  464. begin
  465. result:=(maxreg>first_imaginary);
  466. end;
  467. procedure trgobj.ungetregister(list:Taasmoutput;r:Tregister);
  468. begin
  469. {$ifdef EXTDEBUG}
  470. if (reginfo=nil) and (getsupreg(r)>=first_imaginary) then
  471. InternalError(2004020901);
  472. {$endif EXTDEBUG}
  473. { Only explicit allocs insert regalloc info }
  474. if getsupreg(r)<first_imaginary then
  475. list.concat(Tai_regalloc.dealloc(r));
  476. end;
  477. procedure trgobj.getexplicitregister(list:Taasmoutput;r:Tregister);
  478. var
  479. supreg:Tsuperregister;
  480. begin
  481. supreg:=getsupreg(r);
  482. if supreg>=first_imaginary then
  483. internalerror(2003121503);
  484. include(used_in_proc,supreg);
  485. list.concat(Tai_regalloc.alloc(r));
  486. end;
  487. procedure trgobj.allocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  488. var i:Tsuperregister;
  489. begin
  490. for i:=0 to first_imaginary-1 do
  491. if i in r then
  492. getexplicitregister(list,newreg(regtype,i,defaultsub));
  493. end;
  494. procedure trgobj.deallocexplicitregisters(list:Taasmoutput;r:Tcpuregisterset);
  495. var i:Tsuperregister;
  496. begin
  497. for i:=0 to first_imaginary-1 do
  498. if i in r then
  499. ungetregister(list,newreg(regtype,i,defaultsub));
  500. end;
  501. procedure trgobj.do_register_allocation(list:Taasmoutput;headertai:tai);
  502. var
  503. spillingcounter:byte;
  504. endspill:boolean;
  505. i:Tsuperregister;
  506. begin
  507. { Insert regalloc info for imaginary registers }
  508. insert_regalloc_info(list,headertai);
  509. ibitmap:=tinterferencebitmap.create;
  510. generate_interference_graph(list,headertai);
  511. { Don't do the real allocation when -sr is passed }
  512. if (cs_no_regalloc in aktglobalswitches) then
  513. exit;
  514. {Do register allocation.}
  515. spillingcounter:=0;
  516. repeat
  517. prepare_colouring;
  518. colour_registers;
  519. epilogue_colouring;
  520. endspill:=true;
  521. if spillednodes.length<>0 then
  522. begin
  523. inc(spillingcounter);
  524. if spillingcounter>maxspillingcounter then
  525. internalerror(200309041);
  526. endspill:=not spill_registers(list,headertai);
  527. end;
  528. until endspill;
  529. ibitmap.free;
  530. translate_registers(list);
  531. dispose_reginfo;
  532. end;
  533. procedure trgobj.add_constraints(reg:Tregister);
  534. begin
  535. end;
  536. procedure trgobj.add_edge(u,v:Tsuperregister);
  537. {This procedure will add an edge to the virtual interference graph.}
  538. procedure addadj(u,v:Tsuperregister);
  539. begin
  540. with reginfo[u] do
  541. begin
  542. if adjlist=nil then
  543. new(adjlist,init);
  544. adjlist^.add(v);
  545. end;
  546. end;
  547. begin
  548. if (u<>v) and not(ibitmap[v,u]) then
  549. begin
  550. ibitmap[v,u]:=true;
  551. ibitmap[u,v]:=true;
  552. {Precoloured nodes are not stored in the interference graph.}
  553. if (u>=first_imaginary) then
  554. addadj(u,v);
  555. if (v>=first_imaginary) then
  556. addadj(v,u);
  557. end;
  558. end;
  559. procedure trgobj.add_edges_used(u:Tsuperregister);
  560. var i:word;
  561. begin
  562. with live_registers do
  563. if length>0 then
  564. for i:=0 to length-1 do
  565. add_edge(u,buf^[i]);
  566. end;
  567. {$ifdef EXTDEBUG}
  568. procedure trgobj.writegraph(loopidx:longint);
  569. {This procedure writes out the current interference graph in the
  570. register allocator.}
  571. var f:text;
  572. i,j:Tsuperregister;
  573. begin
  574. assign(f,'igraph'+tostr(loopidx));
  575. rewrite(f);
  576. writeln(f,'Interference graph');
  577. writeln(f);
  578. write(f,' ');
  579. for i:=0 to 15 do
  580. for j:=0 to 15 do
  581. write(f,hexstr(i,1));
  582. writeln(f);
  583. write(f,' ');
  584. for i:=0 to 15 do
  585. write(f,'0123456789ABCDEF');
  586. writeln(f);
  587. for i:=0 to maxreg-1 do
  588. begin
  589. write(f,hexstr(i,2):4);
  590. for j:=0 to maxreg-1 do
  591. if ibitmap[i,j] then
  592. write(f,'*')
  593. else
  594. write(f,'-');
  595. writeln(f);
  596. end;
  597. close(f);
  598. end;
  599. {$endif EXTDEBUG}
  600. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  601. begin
  602. with reginfo[u] do
  603. begin
  604. if movelist=nil then
  605. begin
  606. getmem(movelist,sizeof(tmovelistheader)+60*sizeof(pointer));
  607. movelist^.header.maxcount:=60;
  608. movelist^.header.count:=0;
  609. movelist^.header.sorted_until:=0;
  610. end
  611. else
  612. begin
  613. if movelist^.header.count>=movelist^.header.maxcount then
  614. begin
  615. movelist^.header.maxcount:=movelist^.header.maxcount*2;
  616. reallocmem(movelist,sizeof(tmovelistheader)+movelist^.header.maxcount*sizeof(pointer));
  617. end;
  618. end;
  619. movelist^.data[movelist^.header.count]:=data;
  620. inc(movelist^.header.count);
  621. end;
  622. end;
  623. procedure trgobj.add_reg_instruction(instr:Tai;r:tregister);
  624. var
  625. supreg : tsuperregister;
  626. begin
  627. supreg:=getsupreg(r);
  628. if supreg>=first_imaginary then
  629. with reginfo[supreg] do
  630. begin
  631. if not assigned(live_start) then
  632. live_start:=instr;
  633. live_end:=instr;
  634. end;
  635. end;
  636. procedure trgobj.add_move_instruction(instr:Taicpu);
  637. {This procedure notifies a certain as a move instruction so the
  638. register allocator can try to eliminate it.}
  639. var i:Tmoveins;
  640. ssupreg,dsupreg:Tsuperregister;
  641. begin
  642. {$ifdef extdebug}
  643. if (instr.oper[O_MOV_SOURCE]^.typ<>top_reg) or
  644. (instr.oper[O_MOV_DEST]^.typ<>top_reg) then
  645. internalerror(200311291);
  646. {$endif}
  647. i:=Tmoveins.create;
  648. i.moveset:=ms_worklist_moves;
  649. worklist_moves.insert(i);
  650. ssupreg:=getsupreg(instr.oper[O_MOV_SOURCE]^.reg);
  651. add_to_movelist(ssupreg,i);
  652. dsupreg:=getsupreg(instr.oper[O_MOV_DEST]^.reg);
  653. if ssupreg<>dsupreg then
  654. {Avoid adding the same move instruction twice to a single register.}
  655. add_to_movelist(dsupreg,i);
  656. i.x:=ssupreg;
  657. i.y:=dsupreg;
  658. end;
  659. function trgobj.move_related(n:Tsuperregister):boolean;
  660. var i:cardinal;
  661. begin
  662. move_related:=false;
  663. with reginfo[n] do
  664. if movelist<>nil then
  665. with movelist^ do
  666. for i:=0 to header.count-1 do
  667. if Tmoveins(data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  668. begin
  669. move_related:=true;
  670. break;
  671. end;
  672. end;
  673. procedure Trgobj.sort_simplify_worklist;
  674. {Sorts the simplifyworklist by the number of interferences the
  675. registers in it cause. This allows simplify to execute in
  676. constant time.}
  677. var p,h,i,leni,lent:word;
  678. t:Tsuperregister;
  679. adji,adjt:Psuperregisterworklist;
  680. begin
  681. with simplifyworklist do
  682. begin
  683. if length<2 then
  684. exit;
  685. p:=1;
  686. while 2*p<length do
  687. p:=2*p;
  688. while p<>0 do
  689. begin
  690. for h:=p to length-1 do
  691. begin
  692. i:=h;
  693. t:=buf^[i];
  694. adjt:=reginfo[buf^[i]].adjlist;
  695. lent:=0;
  696. if adjt<>nil then
  697. lent:=adjt^.length;
  698. repeat
  699. adji:=reginfo[buf^[i-p]].adjlist;
  700. leni:=0;
  701. if adji<>nil then
  702. leni:=adji^.length;
  703. if leni<=lent then
  704. break;
  705. buf^[i]:=buf^[i-p];
  706. dec(i,p)
  707. until i<p;
  708. buf^[i]:=t;
  709. end;
  710. p:=p shr 1;
  711. end;
  712. end;
  713. end;
  714. procedure trgobj.make_work_list;
  715. var n:Tsuperregister;
  716. begin
  717. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  718. assign it to any of the registers, thus it is significant.}
  719. for n:=first_imaginary to maxreg-1 do
  720. with reginfo[n] do
  721. begin
  722. if adjlist=nil then
  723. degree:=0
  724. else
  725. degree:=adjlist^.length;
  726. if degree>=usable_registers_cnt then
  727. spillworklist.add(n)
  728. else if move_related(n) then
  729. freezeworklist.add(n)
  730. else
  731. simplifyworklist.add(n);
  732. end;
  733. sort_simplify_worklist;
  734. end;
  735. procedure trgobj.prepare_colouring;
  736. var i:word;
  737. begin
  738. make_work_list;
  739. active_moves:=Tlinkedlist.create;
  740. frozen_moves:=Tlinkedlist.create;
  741. coalesced_moves:=Tlinkedlist.create;
  742. constrained_moves:=Tlinkedlist.create;
  743. selectstack.clear;
  744. end;
  745. procedure trgobj.enable_moves(n:Tsuperregister);
  746. var m:Tlinkedlistitem;
  747. i:cardinal;
  748. begin
  749. with reginfo[n] do
  750. if movelist<>nil then
  751. for i:=0 to movelist^.header.count-1 do
  752. begin
  753. m:=movelist^.data[i];
  754. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  755. if Tmoveins(m).moveset=ms_active_moves then
  756. begin
  757. {Move m from the set active_moves to the set worklist_moves.}
  758. active_moves.remove(m);
  759. Tmoveins(m).moveset:=ms_worklist_moves;
  760. worklist_moves.concat(m);
  761. end;
  762. end;
  763. end;
  764. procedure Trgobj.decrement_degree(m:Tsuperregister);
  765. var adj : Psuperregisterworklist;
  766. n : tsuperregister;
  767. d,i : word;
  768. begin
  769. with reginfo[m] do
  770. begin
  771. d:=degree;
  772. if d=0 then
  773. internalerror(200312151);
  774. dec(degree);
  775. if d=usable_registers_cnt then
  776. begin
  777. {Enable moves for m.}
  778. enable_moves(m);
  779. {Enable moves for adjacent.}
  780. adj:=adjlist;
  781. if adj<>nil then
  782. for i:=1 to adj^.length do
  783. begin
  784. n:=adj^.buf^[i-1];
  785. if reginfo[n].flags*[ri_selected,ri_coalesced]<>[] then
  786. enable_moves(n);
  787. end;
  788. {Remove the node from the spillworklist.}
  789. if not spillworklist.delete(m) then
  790. internalerror(200310145);
  791. if move_related(m) then
  792. freezeworklist.add(m)
  793. else
  794. simplifyworklist.add(m);
  795. end;
  796. end;
  797. end;
  798. procedure trgobj.simplify;
  799. var adj : Psuperregisterworklist;
  800. m,n : Tsuperregister;
  801. i : word;
  802. begin
  803. {We take the element with the least interferences out of the
  804. simplifyworklist. Since the simplifyworklist is now sorted, we
  805. no longer need to search, but we can simply take the first element.}
  806. m:=simplifyworklist.get;
  807. {Push it on the selectstack.}
  808. selectstack.add(m);
  809. with reginfo[m] do
  810. begin
  811. include(flags,ri_selected);
  812. adj:=adjlist;
  813. end;
  814. if adj<>nil then
  815. for i:=1 to adj^.length do
  816. begin
  817. n:=adj^.buf^[i-1];
  818. if (n>=first_imaginary) and
  819. (reginfo[n].flags*[ri_selected,ri_coalesced]=[]) then
  820. decrement_degree(n);
  821. end;
  822. end;
  823. function trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  824. begin
  825. while ri_coalesced in reginfo[n].flags do
  826. n:=reginfo[n].alias;
  827. get_alias:=n;
  828. end;
  829. procedure trgobj.add_worklist(u:Tsuperregister);
  830. begin
  831. if (u>=first_imaginary) and
  832. (not move_related(u)) and
  833. (reginfo[u].degree<usable_registers_cnt) then
  834. begin
  835. if not freezeworklist.delete(u) then
  836. internalerror(200308161); {must be found}
  837. simplifyworklist.add(u);
  838. end;
  839. end;
  840. function trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  841. {Check wether u and v should be coalesced. u is precoloured.}
  842. function ok(t,r:Tsuperregister):boolean;
  843. begin
  844. ok:=(t<first_imaginary) or
  845. (reginfo[t].degree<usable_registers_cnt) or
  846. ibitmap[r,t];
  847. end;
  848. var adj : Psuperregisterworklist;
  849. i : word;
  850. n : tsuperregister;
  851. begin
  852. with reginfo[v] do
  853. begin
  854. adjacent_ok:=true;
  855. adj:=adjlist;
  856. if adj<>nil then
  857. for i:=1 to adj^.length do
  858. begin
  859. n:=adj^.buf^[i-1];
  860. if (flags*[ri_coalesced,ri_selected]=[]) and not ok(n,u) then
  861. begin
  862. adjacent_ok:=false;
  863. break;
  864. end;
  865. end;
  866. end;
  867. end;
  868. function trgobj.conservative(u,v:Tsuperregister):boolean;
  869. var adj : Psuperregisterworklist;
  870. done : Tsuperregisterset; {To prevent that we count nodes twice.}
  871. i,k:word;
  872. n : tsuperregister;
  873. begin
  874. k:=0;
  875. supregset_reset(done,false);
  876. with reginfo[u] do
  877. begin
  878. adj:=adjlist;
  879. if adj<>nil then
  880. for i:=1 to adj^.length do
  881. begin
  882. n:=adj^.buf^[i-1];
  883. if flags*[ri_coalesced,ri_selected]=[] then
  884. begin
  885. supregset_include(done,n);
  886. if reginfo[n].degree>=usable_registers_cnt then
  887. inc(k);
  888. end;
  889. end;
  890. end;
  891. adj:=reginfo[v].adjlist;
  892. if adj<>nil then
  893. for i:=1 to adj^.length do
  894. begin
  895. n:=adj^.buf^[i-1];
  896. if not supregset_in(done,n) and
  897. (reginfo[n].degree>=usable_registers_cnt) and
  898. (reginfo[u].flags*[ri_coalesced,ri_selected]=[]) then
  899. inc(k);
  900. end;
  901. conservative:=(k<usable_registers_cnt);
  902. end;
  903. procedure trgobj.combine(u,v:Tsuperregister);
  904. var adj : Psuperregisterworklist;
  905. i,n,p,q:cardinal;
  906. t : tsuperregister;
  907. searched:Tlinkedlistitem;
  908. label l1;
  909. begin
  910. if not freezeworklist.delete(v) then
  911. spillworklist.delete(v);
  912. coalescednodes.add(v);
  913. include(reginfo[v].flags,ri_coalesced);
  914. reginfo[v].alias:=u;
  915. {Combine both movelists. Since the movelists are sets, only add
  916. elements that are not already present. The movelists cannot be
  917. empty by definition; nodes are only coalesced if there is a move
  918. between them. To prevent quadratic time blowup (movelists of
  919. especially machine registers can get very large because of moves
  920. generated during calls) we need to go into disgusting complexity.
  921. (See webtbs/tw2242 for an example that stresses this.)
  922. We want to sort the movelist to be able to search logarithmically.
  923. Unfortunately, sorting the movelist every time before searching
  924. is counter-productive, since the movelist usually grows with a few
  925. items at a time. Therefore, we split the movelist into a sorted
  926. and an unsorted part and search through both. If the unsorted part
  927. becomes too large, we sort.}
  928. if assigned(reginfo[u].movelist) then
  929. begin
  930. {We have to weigh the cost of sorting the list against searching
  931. the cost of the unsorted part. I use factor of 8 here; if the
  932. number of items is less than 8 times the numer of unsorted items,
  933. we'll sort the list.}
  934. with reginfo[u].movelist^ do
  935. if header.count<8*(header.count-header.sorted_until) then
  936. sort_movelist(reginfo[u].movelist);
  937. if assigned(reginfo[v].movelist) then
  938. begin
  939. for n:=0 to reginfo[v].movelist^.header.count-1 do
  940. begin
  941. {Binary search the sorted part of the list.}
  942. searched:=reginfo[v].movelist^.data[n];
  943. p:=0;
  944. q:=reginfo[u].movelist^.header.sorted_until;
  945. i:=0;
  946. if q<>0 then
  947. repeat
  948. i:=(p+q) shr 1;
  949. if ptrint(searched)>ptrint(reginfo[u].movelist^.data[i]) then
  950. p:=i+1
  951. else
  952. q:=i;
  953. until p=q;
  954. with reginfo[u].movelist^ do
  955. if searched<>data[i] then
  956. begin
  957. {Linear search the unsorted part of the list.}
  958. for i:=header.sorted_until+1 to header.count-1 do
  959. if searched=data[i] then
  960. goto l1;
  961. {Not found -> add}
  962. add_to_movelist(u,searched);
  963. l1:
  964. end;
  965. end;
  966. end;
  967. end;
  968. enable_moves(v);
  969. adj:=reginfo[v].adjlist;
  970. if adj<>nil then
  971. for i:=1 to adj^.length do
  972. begin
  973. t:=adj^.buf^[i-1];
  974. with reginfo[t] do
  975. if not(ri_coalesced in flags) then
  976. begin
  977. {t has a connection to v. Since we are adding v to u, we
  978. need to connect t to u. However, beware if t was already
  979. connected to u...}
  980. if (ibitmap[t,u]) and not (ri_selected in flags) then
  981. {... because in that case, we are actually removing an edge
  982. and the degree of t decreases.}
  983. decrement_degree(t)
  984. else
  985. begin
  986. add_edge(t,u);
  987. {We have added an edge to t and u. So their degree increases.
  988. However, v is added to u. That means its neighbours will
  989. no longer point to v, but to u instead. Therefore, only the
  990. degree of u increases.}
  991. if (u>=first_imaginary) and not (ri_selected in flags) then
  992. inc(reginfo[u].degree);
  993. end;
  994. end;
  995. end;
  996. if (reginfo[u].degree>=usable_registers_cnt) and freezeworklist.delete(u) then
  997. spillworklist.add(u);
  998. end;
  999. procedure trgobj.coalesce;
  1000. var m:Tmoveins;
  1001. x,y,u,v:Tsuperregister;
  1002. begin
  1003. m:=Tmoveins(worklist_moves.getfirst);
  1004. x:=get_alias(m.x);
  1005. y:=get_alias(m.y);
  1006. if (y<first_imaginary) then
  1007. begin
  1008. u:=y;
  1009. v:=x;
  1010. end
  1011. else
  1012. begin
  1013. u:=x;
  1014. v:=y;
  1015. end;
  1016. if (u=v) then
  1017. begin
  1018. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  1019. coalesced_moves.insert(m);
  1020. add_worklist(u);
  1021. end
  1022. {Do u and v interfere? In that case the move is constrained. Two
  1023. precoloured nodes interfere allways. If v is precoloured, by the above
  1024. code u is precoloured, thus interference...}
  1025. else if (v<first_imaginary) or ibitmap[u,v] then
  1026. begin
  1027. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  1028. constrained_moves.insert(m);
  1029. add_worklist(u);
  1030. add_worklist(v);
  1031. end
  1032. {Next test: is it possible and a good idea to coalesce??}
  1033. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  1034. ((u>=first_imaginary) and conservative(u,v)) then
  1035. begin
  1036. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1037. coalesced_moves.insert(m);
  1038. combine(u,v);
  1039. add_worklist(u);
  1040. end
  1041. else
  1042. begin
  1043. m.moveset:=ms_active_moves;
  1044. active_moves.insert(m);
  1045. end;
  1046. end;
  1047. procedure trgobj.freeze_moves(u:Tsuperregister);
  1048. var i:cardinal;
  1049. m:Tlinkedlistitem;
  1050. v,x,y:Tsuperregister;
  1051. begin
  1052. if reginfo[u].movelist<>nil then
  1053. for i:=0 to reginfo[u].movelist^.header.count-1 do
  1054. begin
  1055. m:=reginfo[u].movelist^.data[i];
  1056. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1057. begin
  1058. x:=Tmoveins(m).x;
  1059. y:=Tmoveins(m).y;
  1060. if get_alias(y)=get_alias(u) then
  1061. v:=get_alias(x)
  1062. else
  1063. v:=get_alias(y);
  1064. {Move m from active_moves/worklist_moves to frozen_moves.}
  1065. if Tmoveins(m).moveset=ms_active_moves then
  1066. active_moves.remove(m)
  1067. else
  1068. worklist_moves.remove(m);
  1069. Tmoveins(m).moveset:=ms_frozen_moves;
  1070. frozen_moves.insert(m);
  1071. if (v>=first_imaginary) and not(move_related(v)) and
  1072. (reginfo[v].degree<usable_registers_cnt) then
  1073. begin
  1074. freezeworklist.delete(v);
  1075. simplifyworklist.add(v);
  1076. end;
  1077. end;
  1078. end;
  1079. end;
  1080. procedure trgobj.freeze;
  1081. var n:Tsuperregister;
  1082. begin
  1083. { We need to take a random element out of the freezeworklist. We take
  1084. the last element. Dirty code! }
  1085. n:=freezeworklist.get;
  1086. {Add it to the simplifyworklist.}
  1087. simplifyworklist.add(n);
  1088. freeze_moves(n);
  1089. end;
  1090. procedure trgobj.select_spill;
  1091. var
  1092. n : tsuperregister;
  1093. adj : psuperregisterworklist;
  1094. max,p,i:word;
  1095. begin
  1096. { We must look for the element with the most interferences in the
  1097. spillworklist. This is required because those registers are creating
  1098. the most conflicts and keeping them in a register will not reduce the
  1099. complexity and even can cause the help registers for the spilling code
  1100. to get too much conflicts with the result that the spilling code
  1101. will never converge (PFV) }
  1102. max:=0;
  1103. p:=0;
  1104. with spillworklist do
  1105. begin
  1106. {Safe: This procedure is only called if length<>0}
  1107. for i:=0 to length-1 do
  1108. begin
  1109. adj:=reginfo[buf^[i]].adjlist;
  1110. if assigned(adj) and (adj^.length>max) then
  1111. begin
  1112. p:=i;
  1113. max:=adj^.length;
  1114. end;
  1115. end;
  1116. n:=buf^[p];
  1117. deleteidx(p);
  1118. end;
  1119. simplifyworklist.add(n);
  1120. freeze_moves(n);
  1121. end;
  1122. procedure trgobj.assign_colours;
  1123. {Assign_colours assigns the actual colours to the registers.}
  1124. var adj : Psuperregisterworklist;
  1125. i,j,k : word;
  1126. n,a,c : Tsuperregister;
  1127. adj_colours,
  1128. colourednodes : Tsuperregisterset;
  1129. found : boolean;
  1130. begin
  1131. spillednodes.clear;
  1132. {Reset colours}
  1133. for n:=0 to maxreg-1 do
  1134. reginfo[n].colour:=n;
  1135. {Colour the cpu registers...}
  1136. supregset_reset(colourednodes,false);
  1137. for n:=0 to first_imaginary-1 do
  1138. supregset_include(colourednodes,n);
  1139. {Now colour the imaginary registers on the select-stack.}
  1140. for i:=selectstack.length downto 1 do
  1141. begin
  1142. n:=selectstack.buf^[i-1];
  1143. {Create a list of colours that we cannot assign to n.}
  1144. supregset_reset(adj_colours,false);
  1145. adj:=reginfo[n].adjlist;
  1146. if adj<>nil then
  1147. for j:=0 to adj^.length-1 do
  1148. begin
  1149. a:=get_alias(adj^.buf^[j]);
  1150. if supregset_in(colourednodes,a) then
  1151. supregset_include(adj_colours,reginfo[a].colour);
  1152. end;
  1153. supregset_include(adj_colours,RS_STACK_POINTER_REG);
  1154. {Assume a spill by default...}
  1155. found:=false;
  1156. {Search for a colour not in this list.}
  1157. for k:=0 to usable_registers_cnt-1 do
  1158. begin
  1159. c:=usable_registers[k];
  1160. if not(supregset_in(adj_colours,c)) then
  1161. begin
  1162. reginfo[n].colour:=c;
  1163. found:=true;
  1164. supregset_include(colourednodes,n);
  1165. include(used_in_proc,c);
  1166. break;
  1167. end;
  1168. end;
  1169. if not found then
  1170. spillednodes.add(n);
  1171. end;
  1172. {Finally colour the nodes that were coalesced.}
  1173. for i:=1 to coalescednodes.length do
  1174. begin
  1175. n:=coalescednodes.buf^[i-1];
  1176. k:=get_alias(n);
  1177. reginfo[n].colour:=reginfo[k].colour;
  1178. if reginfo[k].colour<maxcpuregister then
  1179. include(used_in_proc,reginfo[k].colour);
  1180. end;
  1181. {$ifdef ra_debug}
  1182. if aktfilepos.line=179 then
  1183. begin
  1184. writeln('colourlist');
  1185. for i:=0 to maxreg-1 do
  1186. writeln(i:4,' ',reginfo[i].colour:4)
  1187. end;
  1188. {$endif ra_debug}
  1189. end;
  1190. procedure trgobj.colour_registers;
  1191. begin
  1192. repeat
  1193. if simplifyworklist.length<>0 then
  1194. simplify
  1195. else if not(worklist_moves.empty) then
  1196. coalesce
  1197. else if freezeworklist.length<>0 then
  1198. freeze
  1199. else if spillworklist.length<>0 then
  1200. select_spill;
  1201. until (simplifyworklist.length=0) and
  1202. worklist_moves.empty and
  1203. (freezeworklist.length=0) and
  1204. (spillworklist.length=0);
  1205. assign_colours;
  1206. end;
  1207. procedure trgobj.epilogue_colouring;
  1208. var
  1209. i : Tsuperregister;
  1210. begin
  1211. worklist_moves.clear;
  1212. active_moves.destroy;
  1213. active_moves:=nil;
  1214. frozen_moves.destroy;
  1215. frozen_moves:=nil;
  1216. coalesced_moves.destroy;
  1217. coalesced_moves:=nil;
  1218. constrained_moves.destroy;
  1219. constrained_moves:=nil;
  1220. for i:=0 to maxreg-1 do
  1221. with reginfo[i] do
  1222. if movelist<>nil then
  1223. begin
  1224. dispose(movelist);
  1225. movelist:=nil;
  1226. end;
  1227. end;
  1228. procedure trgobj.clear_interferences(u:Tsuperregister);
  1229. {Remove node u from the interference graph and remove all collected
  1230. move instructions it is associated with.}
  1231. var i : word;
  1232. v : Tsuperregister;
  1233. adj,adj2 : Psuperregisterworklist;
  1234. begin
  1235. adj:=reginfo[u].adjlist;
  1236. if adj<>nil then
  1237. begin
  1238. for i:=1 to adj^.length do
  1239. begin
  1240. v:=adj^.buf^[i-1];
  1241. {Remove (u,v) and (v,u) from bitmap.}
  1242. ibitmap[u,v]:=false;
  1243. ibitmap[v,u]:=false;
  1244. {Remove (v,u) from adjacency list.}
  1245. adj2:=reginfo[v].adjlist;
  1246. if adj2<>nil then
  1247. begin
  1248. adj2^.delete(u);
  1249. if adj2^.length=0 then
  1250. begin
  1251. dispose(adj2,done);
  1252. reginfo[v].adjlist:=nil;
  1253. end;
  1254. end;
  1255. end;
  1256. {Remove ( u,* ) from adjacency list.}
  1257. dispose(adj,done);
  1258. reginfo[u].adjlist:=nil;
  1259. end;
  1260. end;
  1261. procedure trgobj.getregisterinline(list:Taasmoutput;
  1262. position:Tai;subreg:Tsubregister;var result:Tregister);
  1263. var p:Tsuperregister;
  1264. r:Tregister;
  1265. begin
  1266. p:=getnewreg(subreg);
  1267. live_registers.add(p);
  1268. r:=newreg(regtype,p,subreg);
  1269. if position=nil then
  1270. list.insert(Tai_regalloc.alloc(r))
  1271. else
  1272. list.insertafter(Tai_regalloc.alloc(r),position);
  1273. add_edges_used(p);
  1274. add_constraints(r);
  1275. result:=r;
  1276. end;
  1277. procedure trgobj.ungetregisterinline(list:Taasmoutput;
  1278. position:Tai;r:Tregister);
  1279. var supreg:Tsuperregister;
  1280. begin
  1281. supreg:=getsupreg(r);
  1282. live_registers.delete(supreg);
  1283. if position=nil then
  1284. list.insert(Tai_regalloc.dealloc(r))
  1285. else
  1286. list.insertafter(Tai_regalloc.dealloc(r),position);
  1287. end;
  1288. procedure trgobj.insert_regalloc_info(list:Taasmoutput;headertai:tai);
  1289. var
  1290. supreg : tsuperregister;
  1291. p : tai;
  1292. r : tregister;
  1293. begin
  1294. { Insert regallocs for all imaginary registers }
  1295. for supreg:=first_imaginary to maxreg-1 do
  1296. with reginfo[supreg] do
  1297. begin
  1298. r:=newreg(regtype,supreg,subreg);
  1299. if assigned(live_start) then
  1300. begin
  1301. {$ifdef EXTDEBUG}
  1302. if live_start=live_end then
  1303. Comment(V_Warning,'Register '+std_regname(r)+' is only used once');
  1304. {$endif EXTDEBUG}
  1305. list.insertbefore(Tai_regalloc.alloc(r),live_start);
  1306. { Insert live end deallocation before reg allocations
  1307. to reduce conflicts }
  1308. p:=live_end;
  1309. while assigned(p) and
  1310. assigned(p.previous) and
  1311. (tai(p.previous).typ=ait_regalloc) and
  1312. tai_regalloc(p.previous).allocation and
  1313. (tai_regalloc(p.previous).reg<>r) do
  1314. p:=tai(p.previous);
  1315. list.insertbefore(Tai_regalloc.dealloc(r),p);
  1316. end
  1317. {$ifdef EXTDEBUG}
  1318. else
  1319. Comment(V_Warning,'Register '+std_regname(r)+' not used');
  1320. {$endif EXTDEBUG}
  1321. end;
  1322. end;
  1323. procedure trgobj.add_cpu_interferences(p : tai);
  1324. begin
  1325. end;
  1326. procedure trgobj.generate_interference_graph(list:Taasmoutput;headertai:tai);
  1327. var
  1328. p : tai;
  1329. i : integer;
  1330. supreg : tsuperregister;
  1331. begin
  1332. { All allocations are available. Now we can generate the
  1333. interference graph. Walk through all instructions, we can
  1334. start with the headertai, because before the header tai is
  1335. only symbols. }
  1336. live_registers.clear;
  1337. p:=headertai;
  1338. while assigned(p) do
  1339. begin
  1340. if p.typ=ait_regalloc then
  1341. with Tai_regalloc(p) do
  1342. begin
  1343. if (getregtype(reg)=regtype) then
  1344. begin
  1345. supreg:=getsupreg(reg);
  1346. if allocation then
  1347. live_registers.add(supreg)
  1348. else
  1349. live_registers.delete(supreg);
  1350. add_edges_used(supreg);
  1351. add_constraints(reg);
  1352. end;
  1353. end;
  1354. add_cpu_interferences(p);
  1355. p:=Tai(p.next);
  1356. end;
  1357. {$ifdef EXTDEBUG}
  1358. if live_registers.length>0 then
  1359. begin
  1360. for i:=0 to live_registers.length-1 do
  1361. begin
  1362. { Only report for imaginary registers }
  1363. if live_registers.buf^[i]>=first_imaginary then
  1364. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,live_registers.buf^[i],defaultsub))+' not released');
  1365. end;
  1366. end;
  1367. {$endif}
  1368. end;
  1369. procedure Trgobj.translate_registers(list:taasmoutput);
  1370. var
  1371. hp,p,q:Tai;
  1372. i:shortint;
  1373. {$ifdef arm}
  1374. so:pshifterop;
  1375. {$endif arm}
  1376. begin
  1377. { Leave when no imaginary registers are used }
  1378. if maxreg<=first_imaginary then
  1379. exit;
  1380. p:=Tai(list.first);
  1381. while assigned(p) do
  1382. begin
  1383. case p.typ of
  1384. ait_regalloc:
  1385. with Tai_regalloc(p) do
  1386. begin
  1387. if (getregtype(reg)=regtype) then
  1388. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1389. {
  1390. Remove sequences of release and
  1391. allocation of the same register like:
  1392. # Register X released
  1393. # Register X allocated
  1394. }
  1395. if assigned(previous) and
  1396. (Tai(previous).typ=ait_regalloc) and
  1397. (Tai_regalloc(previous).reg=reg) and
  1398. { allocation,deallocation or deallocation,allocation }
  1399. (Tai_regalloc(previous).allocation xor allocation) then
  1400. begin
  1401. q:=Tai(next);
  1402. hp:=tai(previous);
  1403. list.remove(hp);
  1404. hp.free;
  1405. list.remove(p);
  1406. p.free;
  1407. p:=q;
  1408. continue;
  1409. end;
  1410. end;
  1411. ait_instruction:
  1412. with Taicpu_abstract(p) do
  1413. begin
  1414. for i:=0 to ops-1 do
  1415. with oper[i]^ do
  1416. case typ of
  1417. Top_reg:
  1418. if (getregtype(reg)=regtype) then
  1419. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1420. Top_ref:
  1421. begin
  1422. if regtype=R_INTREGISTER then
  1423. with ref^ do
  1424. begin
  1425. if base<>NR_NO then
  1426. setsupreg(base,reginfo[getsupreg(base)].colour);
  1427. if index<>NR_NO then
  1428. setsupreg(index,reginfo[getsupreg(index)].colour);
  1429. end;
  1430. end;
  1431. {$ifdef arm}
  1432. Top_shifterop:
  1433. begin
  1434. so:=shifterop;
  1435. if so^.rs<>NR_NO then
  1436. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1437. end;
  1438. {$endif arm}
  1439. end;
  1440. { Maybe the operation can be removed when
  1441. it is a move and both arguments are the same }
  1442. if is_same_reg_move(regtype) then
  1443. begin
  1444. q:=Tai(p.next);
  1445. list.remove(p);
  1446. p.free;
  1447. p:=q;
  1448. continue;
  1449. end;
  1450. end;
  1451. end;
  1452. p:=Tai(p.next);
  1453. end;
  1454. end;
  1455. function trgobj.get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister):Tai;
  1456. var
  1457. back : Tsuperregisterworklist;
  1458. supreg : tsuperregister;
  1459. begin
  1460. back.copyfrom(live_registers);
  1461. result:=p;
  1462. while (p<>nil) and (p.typ=ait_regalloc) do
  1463. begin
  1464. supreg:=getsupreg(Tai_regalloc(p).reg);
  1465. {Rewind the register allocation.}
  1466. if Tai_regalloc(p).allocation then
  1467. live_registers.delete(supreg)
  1468. else
  1469. begin
  1470. live_registers.add(supreg);
  1471. if supreg=huntfor1 then
  1472. begin
  1473. get_insert_pos:=Tai(p.previous);
  1474. back.done;
  1475. back.copyfrom(live_registers);
  1476. end;
  1477. if supreg=huntfor2 then
  1478. begin
  1479. get_insert_pos:=Tai(p.previous);
  1480. back.done;
  1481. back.copyfrom(live_registers);
  1482. end;
  1483. if supreg=huntfor3 then
  1484. begin
  1485. get_insert_pos:=Tai(p.previous);
  1486. back.done;
  1487. back.copyfrom(live_registers);
  1488. end;
  1489. end;
  1490. p:=Tai(p.previous);
  1491. end;
  1492. live_registers.done;
  1493. live_registers:=back;
  1494. end;
  1495. procedure trgobj.forward_allocation(pfrom,pto:Tai);
  1496. var
  1497. p : tai;
  1498. begin
  1499. {Forward the register allocation again.}
  1500. p:=pfrom;
  1501. while (p<>pto) do
  1502. begin
  1503. if p.typ<>ait_regalloc then
  1504. internalerror(200305311);
  1505. if Tai_regalloc(p).allocation then
  1506. live_registers.add(getsupreg(Tai_regalloc(p).reg))
  1507. else
  1508. live_registers.delete(getsupreg(Tai_regalloc(p).reg));
  1509. p:=Tai(p.next);
  1510. end;
  1511. end;
  1512. function trgobj.spill_registers(list:Taasmoutput;headertai:tai):boolean;
  1513. { Returns true if any help registers have been used }
  1514. var
  1515. i : word;
  1516. t : tsuperregister;
  1517. p,q : Tai;
  1518. regs_to_spill_set:Tsuperregisterset;
  1519. spill_temps : ^Tspill_temp_list;
  1520. supreg : tsuperregister;
  1521. templist : taasmoutput;
  1522. begin
  1523. spill_registers:=false;
  1524. live_registers.clear;
  1525. for i:=first_imaginary to maxreg-1 do
  1526. exclude(reginfo[i].flags,ri_selected);
  1527. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1528. supregset_reset(regs_to_spill_set,false);
  1529. { Allocate temps and insert in front of the list }
  1530. templist:=taasmoutput.create;
  1531. {Safe: this procedure is only called if there are spilled nodes.}
  1532. with spillednodes do
  1533. for i:=0 to length-1 do
  1534. begin
  1535. t:=buf^[i];
  1536. {Alternative representation.}
  1537. supregset_include(regs_to_spill_set,t);
  1538. {Clear all interferences of the spilled register.}
  1539. clear_interferences(t);
  1540. {Get a temp for the spilled register}
  1541. tg.gettemp(templist,4,tt_noreuse,spill_temps^[t]);
  1542. end;
  1543. list.insertlistafter(headertai,templist);
  1544. templist.free;
  1545. { Walk through all instructions, we can start with the headertai,
  1546. because before the header tai is only symbols }
  1547. p:=headertai;
  1548. while assigned(p) do
  1549. begin
  1550. case p.typ of
  1551. ait_regalloc:
  1552. with Tai_regalloc(p) do
  1553. begin
  1554. if (getregtype(reg)=regtype) then
  1555. begin
  1556. {A register allocation of a spilled register can be removed.}
  1557. supreg:=getsupreg(reg);
  1558. if supregset_in(regs_to_spill_set,supreg) then
  1559. begin
  1560. q:=Tai(p.next);
  1561. list.remove(p);
  1562. p.free;
  1563. p:=q;
  1564. continue;
  1565. end
  1566. else
  1567. if allocation then
  1568. live_registers.add(supreg)
  1569. else
  1570. live_registers.delete(supreg);
  1571. end;
  1572. end;
  1573. ait_instruction:
  1574. with Taicpu_abstract(p) do
  1575. begin
  1576. aktfilepos:=fileinfo;
  1577. if instr_spill_register(list,Taicpu_abstract(p),regs_to_spill_set,spill_temps^) then
  1578. spill_registers:=true;
  1579. end;
  1580. end;
  1581. p:=Tai(p.next);
  1582. end;
  1583. aktfilepos:=current_procinfo.exitpos;
  1584. {Safe: this procedure is only called if there are spilled nodes.}
  1585. with spillednodes do
  1586. for i:=0 to length-1 do
  1587. tg.ungettemp(list,spill_temps^[buf^[i]]);
  1588. freemem(spill_temps);
  1589. end;
  1590. procedure Trgobj.do_spill_read(list:Taasmoutput;instr:Taicpu_abstract;
  1591. pos:Tai;regidx:word;
  1592. const spilltemplist:Tspill_temp_list;
  1593. const regs:Tspillregsinfo);
  1594. var helpins:Tai;
  1595. begin
  1596. with regs[regidx] do
  1597. begin
  1598. helpins:=instr.spilling_create_load(spilltemplist[orgreg],tempreg);
  1599. if pos=nil then
  1600. list.insertafter(helpins,list.first)
  1601. else
  1602. list.insertafter(helpins,pos.next);
  1603. ungetregisterinline(list,instr,tempreg);
  1604. forward_allocation(tai(helpins.next),instr);
  1605. end;
  1606. end;
  1607. procedure Trgobj.do_spill_written(list:Taasmoutput;instr:Taicpu_abstract;
  1608. pos:Tai;regidx:word;
  1609. const spilltemplist:Tspill_temp_list;
  1610. const regs:Tspillregsinfo);
  1611. var helpins:Tai;
  1612. begin
  1613. with regs[regidx] do
  1614. begin
  1615. helpins:=instr.spilling_create_store(tempreg,spilltemplist[orgreg]);
  1616. list.insertafter(helpins,instr);
  1617. ungetregisterinline(list,helpins,tempreg);
  1618. end;
  1619. end;
  1620. procedure Trgobj.do_spill_readwritten(list:Taasmoutput;instr:Taicpu_abstract;
  1621. pos:Tai;regidx:word;
  1622. const spilltemplist:Tspill_temp_list;
  1623. const regs:Tspillregsinfo);
  1624. var helpins1,helpins2:Tai;
  1625. begin
  1626. with regs[regidx] do
  1627. begin
  1628. helpins1:=instr.spilling_create_load(spilltemplist[orgreg],tempreg);
  1629. if pos=nil then
  1630. list.insertafter(helpins1,list.first)
  1631. else
  1632. list.insertafter(helpins1,pos.next);
  1633. helpins2:=instr.spilling_create_store(tempreg,spilltemplist[orgreg]);
  1634. list.insertafter(helpins2,instr);
  1635. ungetregisterinline(list,helpins2,tempreg);
  1636. forward_allocation(tai(helpins1.next),instr);
  1637. end;
  1638. end;
  1639. function trgobj.instr_spill_register(list:Taasmoutput;
  1640. instr:taicpu_abstract;
  1641. const r:Tsuperregisterset;
  1642. const spilltemplist:Tspill_temp_list): boolean;
  1643. var
  1644. counter, regindex: longint;
  1645. pos: tai;
  1646. regs: tspillregsinfo;
  1647. spilled: boolean;
  1648. procedure addreginfo(reg: tsuperregister; operation: topertype);
  1649. var
  1650. i, tmpindex: longint;
  1651. begin
  1652. tmpindex := regindex;
  1653. // did we already encounter this register?
  1654. for i := 0 to pred(regindex) do
  1655. if (regs[i].orgreg = reg) then
  1656. begin
  1657. tmpindex := i;
  1658. break;
  1659. end;
  1660. if tmpindex > high(regs) then
  1661. internalerror(2003120301);
  1662. regs[tmpindex].orgreg := reg;
  1663. if supregset_in(r,reg) then
  1664. begin
  1665. // add/update info on this register
  1666. regs[tmpindex].mustbespilled := true;
  1667. case operation of
  1668. operand_read:
  1669. regs[tmpindex].regread := true;
  1670. operand_write:
  1671. regs[tmpindex].regwritten := true;
  1672. operand_readwrite:
  1673. begin
  1674. regs[tmpindex].regread := true;
  1675. regs[tmpindex].regwritten := true;
  1676. end;
  1677. end;
  1678. spilled := true;
  1679. end;
  1680. inc(regindex,ord(regindex=tmpindex));
  1681. end;
  1682. procedure tryreplacereg(var reg: tregister);
  1683. var
  1684. i: longint;
  1685. supreg: tsuperregister;
  1686. begin
  1687. if (getregtype(reg) = R_INTREGISTER) then
  1688. begin
  1689. supreg := getsupreg(reg);
  1690. for i := 0 to pred(regindex) do
  1691. if (regs[i].mustbespilled) and
  1692. (regs[i].orgreg = supreg) then
  1693. begin
  1694. reg := regs[i].tempreg;
  1695. break;
  1696. end;
  1697. end;
  1698. end;
  1699. begin
  1700. result := false;
  1701. fillchar(regs,sizeof(regs),0);
  1702. for counter := low(regs) to high(regs) do
  1703. regs[counter].orgreg := RS_INVALID;
  1704. spilled := false;
  1705. regindex := 0;
  1706. { check whether and if so which and how (read/written) this instructions contains
  1707. registers that must be spilled }
  1708. for counter := 0 to instr.ops-1 do
  1709. with instr.oper[counter]^ do
  1710. begin
  1711. case typ of
  1712. top_reg:
  1713. begin
  1714. if (getregtype(reg) = regtype) then
  1715. addreginfo(getsupreg(reg),instr.spilling_get_operation_type(counter));
  1716. end;
  1717. top_ref:
  1718. begin
  1719. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1720. with ref^ do
  1721. begin
  1722. if (base <> NR_NO) then
  1723. addreginfo(getsupreg(base),operand_read);
  1724. if (index <> NR_NO) then
  1725. addreginfo(getsupreg(index),operand_read);
  1726. end;
  1727. end;
  1728. {$ifdef ARM}
  1729. top_shifterop:
  1730. begin
  1731. if shifterop^.rs<>NR_NO then
  1732. addreginfo(getsupreg(shifterop^.rs),operand_read);
  1733. end;
  1734. {$endif ARM}
  1735. end;
  1736. end;
  1737. { if no spilling for this instruction we can leave }
  1738. if not spilled then
  1739. exit;
  1740. { generate the spilling code }
  1741. result := true;
  1742. for counter := 0 to pred(regindex) do
  1743. with regs[counter] do
  1744. begin
  1745. if mustbespilled then
  1746. begin
  1747. pos:=get_insert_pos(Tai(instr.previous),regs[0].orgreg,regs[1].orgreg,regs[2].orgreg);
  1748. getregisterinline(list,pos,defaultsub,tempreg);
  1749. if regread then
  1750. if regwritten then
  1751. do_spill_readwritten(list,instr,pos,counter,spilltemplist,regs)
  1752. else
  1753. do_spill_read(list,instr,pos,counter,spilltemplist,regs)
  1754. else
  1755. do_spill_written(list,instr,pos,counter,spilltemplist,regs)
  1756. end;
  1757. end;
  1758. { substitute registers }
  1759. for counter := 0 to instr.ops-1 do
  1760. with instr.oper[counter]^ do
  1761. begin
  1762. case typ of
  1763. top_reg:
  1764. begin
  1765. tryreplacereg(reg);
  1766. end;
  1767. top_ref:
  1768. begin
  1769. tryreplacereg(ref^.base);
  1770. tryreplacereg(ref^.index);
  1771. end;
  1772. {$ifdef ARM}
  1773. top_shifterop:
  1774. begin
  1775. tryreplacereg(shifterop^.rs);
  1776. end;
  1777. {$endif ARM}
  1778. end;
  1779. end;
  1780. end;
  1781. end.
  1782. {
  1783. $Log$
  1784. Revision 1.124 2004-03-14 22:50:04 peter
  1785. * rewrote add_to_movelist, it now uses a field to store the number
  1786. of allocated entries. Also made it using less hardcoded values
  1787. Revision 1.123 2004/03/14 20:06:17 peter
  1788. * check if movelist is valid
  1789. Revision 1.122 2004/02/12 15:54:03 peter
  1790. * make extcycle is working again
  1791. Revision 1.121 2004/02/09 20:12:23 olle
  1792. + check that register allocation is not made at the wrong moment
  1793. Revision 1.120 2004/02/08 23:10:21 jonas
  1794. * taicpu.is_same_reg_move() now gets a regtype parameter so it only
  1795. removes moves of that particular register type. This is necessary so
  1796. we don't remove the live_start instruction of a register before it
  1797. has been processed
  1798. Revision 1.119 2004/02/08 14:26:28 daniel
  1799. * Register allocator speed boost
  1800. Revision 1.118 2004/02/07 23:28:34 daniel
  1801. * Take advantage of our new with statement optimization
  1802. Revision 1.117 2004/02/06 13:34:46 daniel
  1803. * Some changes to better accomodate very large movelists
  1804. * movelist resizing now exponential (avoids heap fragmentation, saves
  1805. 300 kb memory in make cycle)
  1806. * Trgobj.combine hand-optimized (still too slow)
  1807. Revision 1.116 2004/01/28 22:16:31 peter
  1808. * more record alignment fixes
  1809. Revision 1.115 2004/01/26 17:40:11 florian
  1810. * made DoSpill* overrideable
  1811. + add_cpu_interferences added
  1812. Revision 1.114 2004/01/26 16:12:28 daniel
  1813. * reginfo now also only allocated during register allocation
  1814. * third round of gdb cleanups: kick out most of concatstabto
  1815. Revision 1.112 2004/01/12 16:37:59 peter
  1816. * moved spilling code from taicpu to rg
  1817. Revision 1.109 2003/12/26 14:02:30 peter
  1818. * sparc updates
  1819. * use registertype in spill_register
  1820. Revision 1.108 2003/12/22 23:09:34 peter
  1821. * only report unreleased imaginary registers
  1822. Revision 1.107 2003/12/22 22:13:46 peter
  1823. * made decrease_degree working, but not really fixed
  1824. Revision 1.106 2003/12/18 17:06:21 florian
  1825. * arm compiler compilation fixed
  1826. Revision 1.105 2003/12/17 21:59:05 peter
  1827. * don't insert dealloc before alloc of the same register
  1828. Revision 1.104 2003/12/16 09:41:44 daniel
  1829. * Automatic conversion from integer constants to pointer constants is no
  1830. longer done except in Delphi mode
  1831. Revision 1.103 2003/12/15 21:25:49 peter
  1832. * reg allocations for imaginary register are now inserted just
  1833. before reg allocation
  1834. * tregister changed to enum to allow compile time check
  1835. * fixed several tregister-tsuperregister errors
  1836. Revision 1.102 2003/12/15 16:37:47 daniel
  1837. * More microoptimizations
  1838. Revision 1.101 2003/12/15 15:58:58 peter
  1839. * fix statedebug compile
  1840. Revision 1.100 2003/12/14 20:24:28 daniel
  1841. * Register allocator speed optimizations
  1842. - Worklist no longer a ringbuffer
  1843. - No find operations are left
  1844. - Simplify now done in constant time
  1845. - unusedregs is now a Tsuperregisterworklist
  1846. - Microoptimizations
  1847. Revision 1.99 2003/12/12 17:16:17 peter
  1848. * rg[tregistertype] added in tcg
  1849. Revision 1.98 2003/12/04 23:27:32 peter
  1850. * remove redundant calls to add_edge_used
  1851. Revision 1.97 2003/11/29 17:36:41 peter
  1852. * check for add_move_instruction
  1853. Revision 1.96 2003/11/24 15:17:37 florian
  1854. * changed some types to prevend range check errors
  1855. Revision 1.95 2003/11/10 19:05:50 peter
  1856. * fixed alias/colouring > 255
  1857. Revision 1.94 2003/11/07 15:58:32 florian
  1858. * Florian's culmutative nr. 1; contains:
  1859. - invalid calling conventions for a certain cpu are rejected
  1860. - arm softfloat calling conventions
  1861. - -Sp for cpu dependend code generation
  1862. - several arm fixes
  1863. - remaining code for value open array paras on heap
  1864. Revision 1.93 2003/10/30 16:22:40 peter
  1865. * call firstpass before allocation and codegeneration is started
  1866. * move leftover code from pass_2.generatecode() to psub
  1867. Revision 1.92 2003/10/29 21:29:14 jonas
  1868. * some ALLOWDUPREG improvements
  1869. Revision 1.91 2003/10/21 15:15:36 peter
  1870. * taicpu_abstract.oper[] changed to pointers
  1871. Revision 1.90 2003/10/19 12:36:36 florian
  1872. * improved speed; reduced memory usage of the interference bitmap
  1873. Revision 1.89 2003/10/19 01:34:30 florian
  1874. * some ppc stuff fixed
  1875. * memory leak fixed
  1876. Revision 1.88 2003/10/18 15:41:26 peter
  1877. * made worklists dynamic in size
  1878. Revision 1.87 2003/10/17 16:16:08 peter
  1879. * fixed last commit
  1880. Revision 1.86 2003/10/17 15:25:18 florian
  1881. * fixed more ppc stuff
  1882. Revision 1.85 2003/10/17 14:38:32 peter
  1883. * 64k registers supported
  1884. * fixed some memory leaks
  1885. Revision 1.84 2003/10/11 16:06:42 florian
  1886. * fixed some MMX<->SSE
  1887. * started to fix ppc, needs an overhaul
  1888. + stabs info improve for spilling, not sure if it works correctly/completly
  1889. - MMX_SUPPORT removed from Makefile.fpc
  1890. Revision 1.83 2003/10/10 17:48:14 peter
  1891. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1892. * tregisteralloctor renamed to trgobj
  1893. * removed rgobj from a lot of units
  1894. * moved location_* and reference_* to cgobj
  1895. * first things for mmx register allocation
  1896. Revision 1.82 2003/10/09 21:31:37 daniel
  1897. * Register allocator splitted, ans abstract now
  1898. Revision 1.81 2003/10/01 20:34:49 peter
  1899. * procinfo unit contains tprocinfo
  1900. * cginfo renamed to cgbase
  1901. * moved cgmessage to verbose
  1902. * fixed ppc and sparc compiles
  1903. Revision 1.80 2003/09/30 19:54:42 peter
  1904. * reuse registers with the least conflicts
  1905. Revision 1.79 2003/09/29 20:58:56 peter
  1906. * optimized releasing of registers
  1907. Revision 1.78 2003/09/28 13:41:12 peter
  1908. * return reg 255 when allowdupreg is defined
  1909. Revision 1.77 2003/09/25 16:19:32 peter
  1910. * fix filepositions
  1911. * insert spill temp allocations at the start of the proc
  1912. Revision 1.76 2003/09/16 16:17:01 peter
  1913. * varspez in calls to push_addr_param
  1914. Revision 1.75 2003/09/12 19:07:42 daniel
  1915. * Fixed fast spilling functionality by re-adding the code that initializes
  1916. precoloured nodes to degree 255. I would like to play hangman on the one
  1917. who removed that code.
  1918. Revision 1.74 2003/09/11 11:54:59 florian
  1919. * improved arm code generation
  1920. * move some protected and private field around
  1921. * the temp. register for register parameters/arguments are now released
  1922. before the move to the parameter register is done. This improves
  1923. the code in a lot of cases.
  1924. Revision 1.73 2003/09/09 20:59:27 daniel
  1925. * Adding register allocation order
  1926. Revision 1.72 2003/09/09 15:55:44 peter
  1927. * use register with least interferences in spillregister
  1928. Revision 1.71 2003/09/07 22:09:35 peter
  1929. * preparations for different default calling conventions
  1930. * various RA fixes
  1931. Revision 1.70 2003/09/03 21:06:45 peter
  1932. * fixes for FPU register allocation
  1933. Revision 1.69 2003/09/03 15:55:01 peter
  1934. * NEWRA branch merged
  1935. Revision 1.68 2003/09/03 11:18:37 florian
  1936. * fixed arm concatcopy
  1937. + arm support in the common compiler sources added
  1938. * moved some generic cg code around
  1939. + tfputype added
  1940. * ...
  1941. Revision 1.67.2.5 2003/08/31 20:44:07 peter
  1942. * fixed getexplicitregisterint tregister value
  1943. Revision 1.67.2.4 2003/08/31 20:40:50 daniel
  1944. * Fixed add_edges_used
  1945. Revision 1.67.2.3 2003/08/29 17:28:59 peter
  1946. * next batch of updates
  1947. Revision 1.67.2.2 2003/08/28 18:35:08 peter
  1948. * tregister changed to cardinal
  1949. Revision 1.67.2.1 2003/08/27 19:55:54 peter
  1950. * first tregister patch
  1951. Revision 1.67 2003/08/23 10:46:21 daniel
  1952. * Register allocator bugfix for h2pas
  1953. Revision 1.66 2003/08/17 16:59:20 jonas
  1954. * fixed regvars so they work with newra (at least for ppc)
  1955. * fixed some volatile register bugs
  1956. + -dnotranslation option for -dnewra, which causes the registers not to
  1957. be translated from virtual to normal registers. Requires support in
  1958. the assembler writer as well, which is only implemented in aggas/
  1959. agppcgas currently
  1960. Revision 1.65 2003/08/17 14:32:48 daniel
  1961. * Precoloured nodes now have an infinite degree approached with 255,
  1962. like they should.
  1963. Revision 1.64 2003/08/17 08:48:02 daniel
  1964. * Another register allocator bug fixed.
  1965. * usable_registers_cnt set to 6 for i386
  1966. Revision 1.63 2003/08/09 18:56:54 daniel
  1967. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1968. allocator
  1969. * Some preventive changes to i386 spillinh code
  1970. Revision 1.62 2003/08/03 14:09:50 daniel
  1971. * Fixed a register allocator bug
  1972. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1973. statements: changes in location_force. These moves are now no longer
  1974. constrained so they are optimized away.
  1975. Revision 1.61 2003/07/21 13:32:39 jonas
  1976. * add_edges_used() is now also called for registers allocated with
  1977. getexplicitregisterint()
  1978. * writing the intereference graph is now only done with -dradebug2 and
  1979. the created files are now called "igraph.<module_name>"
  1980. Revision 1.60 2003/07/06 15:31:21 daniel
  1981. * Fixed register allocator. *Lots* of fixes.
  1982. Revision 1.59 2003/07/06 15:00:47 jonas
  1983. * fixed my previous completely broken commit. It's not perfect though,
  1984. registers > last_int_supreg and < max_intreg may still be "translated"
  1985. Revision 1.58 2003/07/06 14:45:05 jonas
  1986. * support integer registers that are not managed by newra (ie. don't
  1987. translate register numbers that fall outside the range
  1988. first_int_supreg..last_int_supreg)
  1989. Revision 1.57 2003/07/02 22:18:04 peter
  1990. * paraloc splitted in callerparaloc,calleeparaloc
  1991. * sparc calling convention updates
  1992. Revision 1.56 2003/06/17 16:34:44 jonas
  1993. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1994. * renamed all_intregisters to volatile_intregisters and made it
  1995. processor dependent
  1996. Revision 1.55 2003/06/14 14:53:50 jonas
  1997. * fixed newra cycle for x86
  1998. * added constants for indicating source and destination operands of the
  1999. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  2000. Revision 1.54 2003/06/13 21:19:31 peter
  2001. * current_procdef removed, use current_procinfo.procdef instead
  2002. Revision 1.53 2003/06/12 21:11:10 peter
  2003. * ungetregisterfpu gets size parameter
  2004. Revision 1.52 2003/06/12 16:43:07 peter
  2005. * newra compiles for sparc
  2006. Revision 1.51 2003/06/09 14:54:26 jonas
  2007. * (de)allocation of registers for parameters is now performed properly
  2008. (and checked on the ppc)
  2009. - removed obsolete allocation of all parameter registers at the start
  2010. of a procedure (and deallocation at the end)
  2011. Revision 1.50 2003/06/03 21:11:09 peter
  2012. * cg.a_load_* get a from and to size specifier
  2013. * makeregsize only accepts newregister
  2014. * i386 uses generic tcgnotnode,tcgunaryminus
  2015. Revision 1.49 2003/06/03 13:01:59 daniel
  2016. * Register allocator finished
  2017. Revision 1.48 2003/06/01 21:38:06 peter
  2018. * getregisterfpu size parameter added
  2019. * op_const_reg size parameter added
  2020. * sparc updates
  2021. Revision 1.47 2003/05/31 20:31:11 jonas
  2022. * set inital costs of assigning a variable to a register to 120 for
  2023. non-i386, because the used register must be store to memory at the
  2024. start and loaded again at the end
  2025. Revision 1.46 2003/05/30 18:55:21 jonas
  2026. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  2027. works for ppc
  2028. Revision 1.45 2003/05/30 12:36:13 jonas
  2029. * use as little different registers on the ppc until newra is released,
  2030. since every used register must be saved
  2031. Revision 1.44 2003/05/17 13:30:08 jonas
  2032. * changed tt_persistant to tt_persistent :)
  2033. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2034. temps, but a ttemptype, so you can also create ansistring temps etc
  2035. Revision 1.43 2003/05/16 14:33:31 peter
  2036. * regvar fixes
  2037. Revision 1.42 2003/04/26 20:03:49 daniel
  2038. * Bug fix in simplify
  2039. Revision 1.41 2003/04/25 20:59:35 peter
  2040. * removed funcretn,funcretsym, function result is now in varsym
  2041. and aliases for result and function name are added using absolutesym
  2042. * vs_hidden parameter for funcret passed in parameter
  2043. * vs_hidden fixes
  2044. * writenode changed to printnode and released from extdebug
  2045. * -vp option added to generate a tree.log with the nodetree
  2046. * nicer printnode for statements, callnode
  2047. Revision 1.40 2003/04/25 08:25:26 daniel
  2048. * Ifdefs around a lot of calls to cleartempgen
  2049. * Fixed registers that are allocated but not freed in several nodes
  2050. * Tweak to register allocator to cause less spills
  2051. * 8-bit registers now interfere with esi,edi and ebp
  2052. Compiler can now compile rtl successfully when using new register
  2053. allocator
  2054. Revision 1.39 2003/04/23 20:23:06 peter
  2055. * compile fix for no-newra
  2056. Revision 1.38 2003/04/23 14:42:07 daniel
  2057. * Further register allocator work. Compiler now smaller with new
  2058. allocator than without.
  2059. * Somebody forgot to adjust ppu version number
  2060. Revision 1.37 2003/04/22 23:50:23 peter
  2061. * firstpass uses expectloc
  2062. * checks if there are differences between the expectloc and
  2063. location.loc from secondpass in EXTDEBUG
  2064. Revision 1.36 2003/04/22 10:09:35 daniel
  2065. + Implemented the actual register allocator
  2066. + Scratch registers unavailable when new register allocator used
  2067. + maybe_save/maybe_restore unavailable when new register allocator used
  2068. Revision 1.35 2003/04/21 19:16:49 peter
  2069. * count address regs separate
  2070. Revision 1.34 2003/04/17 16:48:21 daniel
  2071. * Added some code to keep track of move instructions in register
  2072. allocator
  2073. Revision 1.33 2003/04/17 07:50:24 daniel
  2074. * Some work on interference graph construction
  2075. Revision 1.32 2003/03/28 19:16:57 peter
  2076. * generic constructor working for i386
  2077. * remove fixed self register
  2078. * esi added as address register for i386
  2079. Revision 1.31 2003/03/11 21:46:24 jonas
  2080. * lots of new regallocator fixes, both in generic and ppc-specific code
  2081. (ppc compiler still can't compile the linux system unit though)
  2082. Revision 1.30 2003/03/09 21:18:59 olle
  2083. + added cutils to the uses clause
  2084. Revision 1.29 2003/03/08 20:36:41 daniel
  2085. + Added newra version of Ti386shlshrnode
  2086. + Added interference graph construction code
  2087. Revision 1.28 2003/03/08 13:59:16 daniel
  2088. * Work to handle new register notation in ag386nsm
  2089. + Added newra version of Ti386moddivnode
  2090. Revision 1.27 2003/03/08 10:53:48 daniel
  2091. * Created newra version of secondmul in n386add.pas
  2092. Revision 1.26 2003/03/08 08:59:07 daniel
  2093. + $define newra will enable new register allocator
  2094. + getregisterint will return imaginary registers with $newra
  2095. + -sr switch added, will skip register allocation so you can see
  2096. the direct output of the code generator before register allocation
  2097. Revision 1.25 2003/02/26 20:50:45 daniel
  2098. * Fixed ungetreference
  2099. Revision 1.24 2003/02/19 22:39:56 daniel
  2100. * Fixed a few issues
  2101. Revision 1.23 2003/02/19 22:00:14 daniel
  2102. * Code generator converted to new register notation
  2103. - Horribily outdated todo.txt removed
  2104. Revision 1.22 2003/02/02 19:25:54 carl
  2105. * Several bugfixes for m68k target (register alloc., opcode emission)
  2106. + VIS target
  2107. + Generic add more complete (still not verified)
  2108. Revision 1.21 2003/01/08 18:43:57 daniel
  2109. * Tregister changed into a record
  2110. Revision 1.20 2002/10/05 12:43:28 carl
  2111. * fixes for Delphi 6 compilation
  2112. (warning : Some features do not work under Delphi)
  2113. Revision 1.19 2002/08/23 16:14:49 peter
  2114. * tempgen cleanup
  2115. * tt_noreuse temp type added that will be used in genentrycode
  2116. Revision 1.18 2002/08/17 22:09:47 florian
  2117. * result type handling in tcgcal.pass_2 overhauled
  2118. * better tnode.dowrite
  2119. * some ppc stuff fixed
  2120. Revision 1.17 2002/08/17 09:23:42 florian
  2121. * first part of procinfo rewrite
  2122. Revision 1.16 2002/08/06 20:55:23 florian
  2123. * first part of ppc calling conventions fix
  2124. Revision 1.15 2002/08/05 18:27:48 carl
  2125. + more more more documentation
  2126. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2127. Revision 1.14 2002/08/04 19:06:41 carl
  2128. + added generic exception support (still does not work!)
  2129. + more documentation
  2130. Revision 1.13 2002/07/07 09:52:32 florian
  2131. * powerpc target fixed, very simple units can be compiled
  2132. * some basic stuff for better callparanode handling, far from being finished
  2133. Revision 1.12 2002/07/01 18:46:26 peter
  2134. * internal linker
  2135. * reorganized aasm layer
  2136. Revision 1.11 2002/05/18 13:34:17 peter
  2137. * readded missing revisions
  2138. Revision 1.10 2002/05/16 19:46:44 carl
  2139. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2140. + try to fix temp allocation (still in ifdef)
  2141. + generic constructor calls
  2142. + start of tassembler / tmodulebase class cleanup
  2143. Revision 1.8 2002/04/21 15:23:03 carl
  2144. + makeregsize
  2145. + changeregsize is now a local routine
  2146. Revision 1.7 2002/04/20 21:32:25 carl
  2147. + generic FPC_CHECKPOINTER
  2148. + first parameter offset in stack now portable
  2149. * rename some constants
  2150. + move some cpu stuff to other units
  2151. - remove unused constents
  2152. * fix stacksize for some targets
  2153. * fix generic size problems which depend now on EXTEND_SIZE constant
  2154. Revision 1.6 2002/04/15 19:03:31 carl
  2155. + reg2str -> std_reg2str()
  2156. Revision 1.5 2002/04/06 18:13:01 jonas
  2157. * several powerpc-related additions and fixes
  2158. Revision 1.4 2002/04/04 19:06:04 peter
  2159. * removed unused units
  2160. * use tlocation.size in cg.a_*loc*() routines
  2161. Revision 1.3 2002/04/02 17:11:29 peter
  2162. * tlocation,treference update
  2163. * LOC_CONSTANT added for better constant handling
  2164. * secondadd splitted in multiple routines
  2165. * location_force_reg added for loading a location to a register
  2166. of a specified size
  2167. * secondassignment parses now first the right and then the left node
  2168. (this is compatible with Kylix). This saves a lot of push/pop especially
  2169. with string operations
  2170. * adapted some routines to use the new cg methods
  2171. Revision 1.2 2002/04/01 19:24:25 jonas
  2172. * fixed different parameter name in interface and implementation
  2173. declaration of a method (only 1.0.x detected this)
  2174. Revision 1.1 2002/03/31 20:26:36 jonas
  2175. + a_loadfpu_* and a_loadmm_* methods in tcg
  2176. * register allocation is now handled by a class and is mostly processor
  2177. independent (+rgobj.pas and i386/rgcpu.pas)
  2178. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2179. * some small improvements and fixes to the optimizer
  2180. * some register allocation fixes
  2181. * some fpuvaroffset fixes in the unary minus node
  2182. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2183. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2184. also better optimizable)
  2185. * fixed and optimized register saving/restoring for new/dispose nodes
  2186. * LOC_FPU locations now also require their "register" field to be set to
  2187. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2188. - list field removed of the tnode class because it's not used currently
  2189. and can cause hard-to-find bugs
  2190. }