rgobj.pas 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561
  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. { deallocation,allocation }
  1399. { note: do not remove allocation,deallocation, those }
  1400. { do have a real meaning }
  1401. (not(Tai_regalloc(previous).allocation) and allocation) then
  1402. begin
  1403. q:=Tai(next);
  1404. hp:=tai(previous);
  1405. list.remove(hp);
  1406. hp.free;
  1407. list.remove(p);
  1408. p.free;
  1409. p:=q;
  1410. continue;
  1411. end;
  1412. end;
  1413. ait_instruction:
  1414. with Taicpu_abstract(p) do
  1415. begin
  1416. for i:=0 to ops-1 do
  1417. with oper[i]^ do
  1418. case typ of
  1419. Top_reg:
  1420. if (getregtype(reg)=regtype) then
  1421. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1422. Top_ref:
  1423. begin
  1424. if regtype=R_INTREGISTER then
  1425. with ref^ do
  1426. begin
  1427. if base<>NR_NO then
  1428. setsupreg(base,reginfo[getsupreg(base)].colour);
  1429. if index<>NR_NO then
  1430. setsupreg(index,reginfo[getsupreg(index)].colour);
  1431. end;
  1432. end;
  1433. {$ifdef arm}
  1434. Top_shifterop:
  1435. begin
  1436. so:=shifterop;
  1437. if so^.rs<>NR_NO then
  1438. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1439. end;
  1440. {$endif arm}
  1441. end;
  1442. { Maybe the operation can be removed when
  1443. it is a move and both arguments are the same }
  1444. if is_same_reg_move(regtype) then
  1445. begin
  1446. q:=Tai(p.next);
  1447. list.remove(p);
  1448. p.free;
  1449. p:=q;
  1450. continue;
  1451. end;
  1452. end;
  1453. end;
  1454. p:=Tai(p.next);
  1455. end;
  1456. end;
  1457. function trgobj.get_insert_pos(p:Tai;huntfor1,huntfor2,huntfor3:Tsuperregister):Tai;
  1458. var
  1459. back : Tsuperregisterworklist;
  1460. supreg : tsuperregister;
  1461. begin
  1462. back.copyfrom(live_registers);
  1463. result:=p;
  1464. while (p<>nil) and (p.typ=ait_regalloc) do
  1465. begin
  1466. supreg:=getsupreg(Tai_regalloc(p).reg);
  1467. {Rewind the register allocation.}
  1468. if Tai_regalloc(p).allocation then
  1469. live_registers.delete(supreg)
  1470. else
  1471. begin
  1472. live_registers.add(supreg);
  1473. if supreg=huntfor1 then
  1474. begin
  1475. get_insert_pos:=Tai(p.previous);
  1476. back.done;
  1477. back.copyfrom(live_registers);
  1478. end;
  1479. if supreg=huntfor2 then
  1480. begin
  1481. get_insert_pos:=Tai(p.previous);
  1482. back.done;
  1483. back.copyfrom(live_registers);
  1484. end;
  1485. if supreg=huntfor3 then
  1486. begin
  1487. get_insert_pos:=Tai(p.previous);
  1488. back.done;
  1489. back.copyfrom(live_registers);
  1490. end;
  1491. end;
  1492. p:=Tai(p.previous);
  1493. end;
  1494. live_registers.done;
  1495. live_registers:=back;
  1496. end;
  1497. procedure trgobj.forward_allocation(pfrom,pto:Tai);
  1498. var
  1499. p : tai;
  1500. begin
  1501. {Forward the register allocation again.}
  1502. p:=pfrom;
  1503. while (p<>pto) do
  1504. begin
  1505. if p.typ<>ait_regalloc then
  1506. internalerror(200305311);
  1507. if Tai_regalloc(p).allocation then
  1508. live_registers.add(getsupreg(Tai_regalloc(p).reg))
  1509. else
  1510. live_registers.delete(getsupreg(Tai_regalloc(p).reg));
  1511. p:=Tai(p.next);
  1512. end;
  1513. end;
  1514. function trgobj.spill_registers(list:Taasmoutput;headertai:tai):boolean;
  1515. { Returns true if any help registers have been used }
  1516. var
  1517. i : word;
  1518. t : tsuperregister;
  1519. p,q : Tai;
  1520. regs_to_spill_set:Tsuperregisterset;
  1521. spill_temps : ^Tspill_temp_list;
  1522. supreg : tsuperregister;
  1523. templist : taasmoutput;
  1524. begin
  1525. spill_registers:=false;
  1526. live_registers.clear;
  1527. for i:=first_imaginary to maxreg-1 do
  1528. exclude(reginfo[i].flags,ri_selected);
  1529. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1530. supregset_reset(regs_to_spill_set,false);
  1531. { Allocate temps and insert in front of the list }
  1532. templist:=taasmoutput.create;
  1533. {Safe: this procedure is only called if there are spilled nodes.}
  1534. with spillednodes do
  1535. for i:=0 to length-1 do
  1536. begin
  1537. t:=buf^[i];
  1538. {Alternative representation.}
  1539. supregset_include(regs_to_spill_set,t);
  1540. {Clear all interferences of the spilled register.}
  1541. clear_interferences(t);
  1542. {Get a temp for the spilled register}
  1543. tg.gettemp(templist,4,tt_noreuse,spill_temps^[t]);
  1544. end;
  1545. list.insertlistafter(headertai,templist);
  1546. templist.free;
  1547. { Walk through all instructions, we can start with the headertai,
  1548. because before the header tai is only symbols }
  1549. p:=headertai;
  1550. while assigned(p) do
  1551. begin
  1552. case p.typ of
  1553. ait_regalloc:
  1554. with Tai_regalloc(p) do
  1555. begin
  1556. if (getregtype(reg)=regtype) then
  1557. begin
  1558. {A register allocation of a spilled register can be removed.}
  1559. supreg:=getsupreg(reg);
  1560. if supregset_in(regs_to_spill_set,supreg) then
  1561. begin
  1562. q:=Tai(p.next);
  1563. list.remove(p);
  1564. p.free;
  1565. p:=q;
  1566. continue;
  1567. end
  1568. else
  1569. if allocation then
  1570. live_registers.add(supreg)
  1571. else
  1572. live_registers.delete(supreg);
  1573. end;
  1574. end;
  1575. ait_instruction:
  1576. with Taicpu_abstract(p) do
  1577. begin
  1578. aktfilepos:=fileinfo;
  1579. if instr_spill_register(list,Taicpu_abstract(p),regs_to_spill_set,spill_temps^) then
  1580. spill_registers:=true;
  1581. end;
  1582. end;
  1583. p:=Tai(p.next);
  1584. end;
  1585. aktfilepos:=current_procinfo.exitpos;
  1586. {Safe: this procedure is only called if there are spilled nodes.}
  1587. with spillednodes do
  1588. for i:=0 to length-1 do
  1589. tg.ungettemp(list,spill_temps^[buf^[i]]);
  1590. freemem(spill_temps);
  1591. end;
  1592. procedure Trgobj.do_spill_read(list:Taasmoutput;instr:Taicpu_abstract;
  1593. pos:Tai;regidx:word;
  1594. const spilltemplist:Tspill_temp_list;
  1595. const regs:Tspillregsinfo);
  1596. var helpins:Tai;
  1597. begin
  1598. with regs[regidx] do
  1599. begin
  1600. helpins:=instr.spilling_create_load(spilltemplist[orgreg],tempreg);
  1601. if pos=nil then
  1602. list.insertafter(helpins,list.first)
  1603. else
  1604. list.insertafter(helpins,pos.next);
  1605. ungetregisterinline(list,instr,tempreg);
  1606. forward_allocation(tai(helpins.next),instr);
  1607. end;
  1608. end;
  1609. procedure Trgobj.do_spill_written(list:Taasmoutput;instr:Taicpu_abstract;
  1610. pos:Tai;regidx:word;
  1611. const spilltemplist:Tspill_temp_list;
  1612. const regs:Tspillregsinfo);
  1613. var helpins:Tai;
  1614. begin
  1615. with regs[regidx] do
  1616. begin
  1617. helpins:=instr.spilling_create_store(tempreg,spilltemplist[orgreg]);
  1618. list.insertafter(helpins,instr);
  1619. ungetregisterinline(list,helpins,tempreg);
  1620. end;
  1621. end;
  1622. procedure Trgobj.do_spill_readwritten(list:Taasmoutput;instr:Taicpu_abstract;
  1623. pos:Tai;regidx:word;
  1624. const spilltemplist:Tspill_temp_list;
  1625. const regs:Tspillregsinfo);
  1626. var helpins1,helpins2:Tai;
  1627. begin
  1628. with regs[regidx] do
  1629. begin
  1630. helpins1:=instr.spilling_create_load(spilltemplist[orgreg],tempreg);
  1631. if pos=nil then
  1632. list.insertafter(helpins1,list.first)
  1633. else
  1634. list.insertafter(helpins1,pos.next);
  1635. helpins2:=instr.spilling_create_store(tempreg,spilltemplist[orgreg]);
  1636. list.insertafter(helpins2,instr);
  1637. ungetregisterinline(list,helpins2,tempreg);
  1638. forward_allocation(tai(helpins1.next),instr);
  1639. end;
  1640. end;
  1641. function trgobj.instr_spill_register(list:Taasmoutput;
  1642. instr:taicpu_abstract;
  1643. const r:Tsuperregisterset;
  1644. const spilltemplist:Tspill_temp_list): boolean;
  1645. var
  1646. counter, regindex: longint;
  1647. pos: tai;
  1648. regs: tspillregsinfo;
  1649. spilled: boolean;
  1650. procedure addreginfo(reg: tsuperregister; operation: topertype);
  1651. var
  1652. i, tmpindex: longint;
  1653. begin
  1654. tmpindex := regindex;
  1655. // did we already encounter this register?
  1656. for i := 0 to pred(regindex) do
  1657. if (regs[i].orgreg = reg) then
  1658. begin
  1659. tmpindex := i;
  1660. break;
  1661. end;
  1662. if tmpindex > high(regs) then
  1663. internalerror(2003120301);
  1664. regs[tmpindex].orgreg := reg;
  1665. if supregset_in(r,reg) then
  1666. begin
  1667. // add/update info on this register
  1668. regs[tmpindex].mustbespilled := true;
  1669. case operation of
  1670. operand_read:
  1671. regs[tmpindex].regread := true;
  1672. operand_write:
  1673. regs[tmpindex].regwritten := true;
  1674. operand_readwrite:
  1675. begin
  1676. regs[tmpindex].regread := true;
  1677. regs[tmpindex].regwritten := true;
  1678. end;
  1679. end;
  1680. spilled := true;
  1681. end;
  1682. inc(regindex,ord(regindex=tmpindex));
  1683. end;
  1684. procedure tryreplacereg(var reg: tregister);
  1685. var
  1686. i: longint;
  1687. supreg: tsuperregister;
  1688. begin
  1689. if (getregtype(reg) = R_INTREGISTER) then
  1690. begin
  1691. supreg := getsupreg(reg);
  1692. for i := 0 to pred(regindex) do
  1693. if (regs[i].mustbespilled) and
  1694. (regs[i].orgreg = supreg) then
  1695. begin
  1696. reg := regs[i].tempreg;
  1697. break;
  1698. end;
  1699. end;
  1700. end;
  1701. begin
  1702. result := false;
  1703. fillchar(regs,sizeof(regs),0);
  1704. for counter := low(regs) to high(regs) do
  1705. regs[counter].orgreg := RS_INVALID;
  1706. spilled := false;
  1707. regindex := 0;
  1708. { check whether and if so which and how (read/written) this instructions contains
  1709. registers that must be spilled }
  1710. for counter := 0 to instr.ops-1 do
  1711. with instr.oper[counter]^ do
  1712. begin
  1713. case typ of
  1714. top_reg:
  1715. begin
  1716. if (getregtype(reg) = regtype) then
  1717. addreginfo(getsupreg(reg),instr.spilling_get_operation_type(counter));
  1718. end;
  1719. top_ref:
  1720. begin
  1721. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1722. with ref^ do
  1723. begin
  1724. if (base <> NR_NO) then
  1725. addreginfo(getsupreg(base),operand_read);
  1726. if (index <> NR_NO) then
  1727. addreginfo(getsupreg(index),operand_read);
  1728. end;
  1729. end;
  1730. {$ifdef ARM}
  1731. top_shifterop:
  1732. begin
  1733. if shifterop^.rs<>NR_NO then
  1734. addreginfo(getsupreg(shifterop^.rs),operand_read);
  1735. end;
  1736. {$endif ARM}
  1737. end;
  1738. end;
  1739. { if no spilling for this instruction we can leave }
  1740. if not spilled then
  1741. exit;
  1742. { generate the spilling code }
  1743. result := true;
  1744. for counter := 0 to pred(regindex) do
  1745. with regs[counter] do
  1746. begin
  1747. if mustbespilled then
  1748. begin
  1749. pos:=get_insert_pos(Tai(instr.previous),regs[0].orgreg,regs[1].orgreg,regs[2].orgreg);
  1750. getregisterinline(list,pos,defaultsub,tempreg);
  1751. if regread then
  1752. if regwritten then
  1753. do_spill_readwritten(list,instr,pos,counter,spilltemplist,regs)
  1754. else
  1755. do_spill_read(list,instr,pos,counter,spilltemplist,regs)
  1756. else
  1757. do_spill_written(list,instr,pos,counter,spilltemplist,regs)
  1758. end;
  1759. end;
  1760. { substitute registers }
  1761. for counter := 0 to instr.ops-1 do
  1762. with instr.oper[counter]^ do
  1763. begin
  1764. case typ of
  1765. top_reg:
  1766. begin
  1767. tryreplacereg(reg);
  1768. end;
  1769. top_ref:
  1770. begin
  1771. tryreplacereg(ref^.base);
  1772. tryreplacereg(ref^.index);
  1773. end;
  1774. {$ifdef ARM}
  1775. top_shifterop:
  1776. begin
  1777. tryreplacereg(shifterop^.rs);
  1778. end;
  1779. {$endif ARM}
  1780. end;
  1781. end;
  1782. end;
  1783. end.
  1784. {
  1785. $Log$
  1786. Revision 1.125 2004-04-26 19:57:50 jonas
  1787. * do not remove "allocation,deallocation" pairs, as those are important
  1788. for the optimizer
  1789. Revision 1.124 2004/03/14 22:50:04 peter
  1790. * rewrote add_to_movelist, it now uses a field to store the number
  1791. of allocated entries. Also made it using less hardcoded values
  1792. Revision 1.123 2004/03/14 20:06:17 peter
  1793. * check if movelist is valid
  1794. Revision 1.122 2004/02/12 15:54:03 peter
  1795. * make extcycle is working again
  1796. Revision 1.121 2004/02/09 20:12:23 olle
  1797. + check that register allocation is not made at the wrong moment
  1798. Revision 1.120 2004/02/08 23:10:21 jonas
  1799. * taicpu.is_same_reg_move() now gets a regtype parameter so it only
  1800. removes moves of that particular register type. This is necessary so
  1801. we don't remove the live_start instruction of a register before it
  1802. has been processed
  1803. Revision 1.119 2004/02/08 14:26:28 daniel
  1804. * Register allocator speed boost
  1805. Revision 1.118 2004/02/07 23:28:34 daniel
  1806. * Take advantage of our new with statement optimization
  1807. Revision 1.117 2004/02/06 13:34:46 daniel
  1808. * Some changes to better accomodate very large movelists
  1809. * movelist resizing now exponential (avoids heap fragmentation, saves
  1810. 300 kb memory in make cycle)
  1811. * Trgobj.combine hand-optimized (still too slow)
  1812. Revision 1.116 2004/01/28 22:16:31 peter
  1813. * more record alignment fixes
  1814. Revision 1.115 2004/01/26 17:40:11 florian
  1815. * made DoSpill* overrideable
  1816. + add_cpu_interferences added
  1817. Revision 1.114 2004/01/26 16:12:28 daniel
  1818. * reginfo now also only allocated during register allocation
  1819. * third round of gdb cleanups: kick out most of concatstabto
  1820. Revision 1.112 2004/01/12 16:37:59 peter
  1821. * moved spilling code from taicpu to rg
  1822. Revision 1.109 2003/12/26 14:02:30 peter
  1823. * sparc updates
  1824. * use registertype in spill_register
  1825. Revision 1.108 2003/12/22 23:09:34 peter
  1826. * only report unreleased imaginary registers
  1827. Revision 1.107 2003/12/22 22:13:46 peter
  1828. * made decrease_degree working, but not really fixed
  1829. Revision 1.106 2003/12/18 17:06:21 florian
  1830. * arm compiler compilation fixed
  1831. Revision 1.105 2003/12/17 21:59:05 peter
  1832. * don't insert dealloc before alloc of the same register
  1833. Revision 1.104 2003/12/16 09:41:44 daniel
  1834. * Automatic conversion from integer constants to pointer constants is no
  1835. longer done except in Delphi mode
  1836. Revision 1.103 2003/12/15 21:25:49 peter
  1837. * reg allocations for imaginary register are now inserted just
  1838. before reg allocation
  1839. * tregister changed to enum to allow compile time check
  1840. * fixed several tregister-tsuperregister errors
  1841. Revision 1.102 2003/12/15 16:37:47 daniel
  1842. * More microoptimizations
  1843. Revision 1.101 2003/12/15 15:58:58 peter
  1844. * fix statedebug compile
  1845. Revision 1.100 2003/12/14 20:24:28 daniel
  1846. * Register allocator speed optimizations
  1847. - Worklist no longer a ringbuffer
  1848. - No find operations are left
  1849. - Simplify now done in constant time
  1850. - unusedregs is now a Tsuperregisterworklist
  1851. - Microoptimizations
  1852. Revision 1.99 2003/12/12 17:16:17 peter
  1853. * rg[tregistertype] added in tcg
  1854. Revision 1.98 2003/12/04 23:27:32 peter
  1855. * remove redundant calls to add_edge_used
  1856. Revision 1.97 2003/11/29 17:36:41 peter
  1857. * check for add_move_instruction
  1858. Revision 1.96 2003/11/24 15:17:37 florian
  1859. * changed some types to prevend range check errors
  1860. Revision 1.95 2003/11/10 19:05:50 peter
  1861. * fixed alias/colouring > 255
  1862. Revision 1.94 2003/11/07 15:58:32 florian
  1863. * Florian's culmutative nr. 1; contains:
  1864. - invalid calling conventions for a certain cpu are rejected
  1865. - arm softfloat calling conventions
  1866. - -Sp for cpu dependend code generation
  1867. - several arm fixes
  1868. - remaining code for value open array paras on heap
  1869. Revision 1.93 2003/10/30 16:22:40 peter
  1870. * call firstpass before allocation and codegeneration is started
  1871. * move leftover code from pass_2.generatecode() to psub
  1872. Revision 1.92 2003/10/29 21:29:14 jonas
  1873. * some ALLOWDUPREG improvements
  1874. Revision 1.91 2003/10/21 15:15:36 peter
  1875. * taicpu_abstract.oper[] changed to pointers
  1876. Revision 1.90 2003/10/19 12:36:36 florian
  1877. * improved speed; reduced memory usage of the interference bitmap
  1878. Revision 1.89 2003/10/19 01:34:30 florian
  1879. * some ppc stuff fixed
  1880. * memory leak fixed
  1881. Revision 1.88 2003/10/18 15:41:26 peter
  1882. * made worklists dynamic in size
  1883. Revision 1.87 2003/10/17 16:16:08 peter
  1884. * fixed last commit
  1885. Revision 1.86 2003/10/17 15:25:18 florian
  1886. * fixed more ppc stuff
  1887. Revision 1.85 2003/10/17 14:38:32 peter
  1888. * 64k registers supported
  1889. * fixed some memory leaks
  1890. Revision 1.84 2003/10/11 16:06:42 florian
  1891. * fixed some MMX<->SSE
  1892. * started to fix ppc, needs an overhaul
  1893. + stabs info improve for spilling, not sure if it works correctly/completly
  1894. - MMX_SUPPORT removed from Makefile.fpc
  1895. Revision 1.83 2003/10/10 17:48:14 peter
  1896. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1897. * tregisteralloctor renamed to trgobj
  1898. * removed rgobj from a lot of units
  1899. * moved location_* and reference_* to cgobj
  1900. * first things for mmx register allocation
  1901. Revision 1.82 2003/10/09 21:31:37 daniel
  1902. * Register allocator splitted, ans abstract now
  1903. Revision 1.81 2003/10/01 20:34:49 peter
  1904. * procinfo unit contains tprocinfo
  1905. * cginfo renamed to cgbase
  1906. * moved cgmessage to verbose
  1907. * fixed ppc and sparc compiles
  1908. Revision 1.80 2003/09/30 19:54:42 peter
  1909. * reuse registers with the least conflicts
  1910. Revision 1.79 2003/09/29 20:58:56 peter
  1911. * optimized releasing of registers
  1912. Revision 1.78 2003/09/28 13:41:12 peter
  1913. * return reg 255 when allowdupreg is defined
  1914. Revision 1.77 2003/09/25 16:19:32 peter
  1915. * fix filepositions
  1916. * insert spill temp allocations at the start of the proc
  1917. Revision 1.76 2003/09/16 16:17:01 peter
  1918. * varspez in calls to push_addr_param
  1919. Revision 1.75 2003/09/12 19:07:42 daniel
  1920. * Fixed fast spilling functionality by re-adding the code that initializes
  1921. precoloured nodes to degree 255. I would like to play hangman on the one
  1922. who removed that code.
  1923. Revision 1.74 2003/09/11 11:54:59 florian
  1924. * improved arm code generation
  1925. * move some protected and private field around
  1926. * the temp. register for register parameters/arguments are now released
  1927. before the move to the parameter register is done. This improves
  1928. the code in a lot of cases.
  1929. Revision 1.73 2003/09/09 20:59:27 daniel
  1930. * Adding register allocation order
  1931. Revision 1.72 2003/09/09 15:55:44 peter
  1932. * use register with least interferences in spillregister
  1933. Revision 1.71 2003/09/07 22:09:35 peter
  1934. * preparations for different default calling conventions
  1935. * various RA fixes
  1936. Revision 1.70 2003/09/03 21:06:45 peter
  1937. * fixes for FPU register allocation
  1938. Revision 1.69 2003/09/03 15:55:01 peter
  1939. * NEWRA branch merged
  1940. Revision 1.68 2003/09/03 11:18:37 florian
  1941. * fixed arm concatcopy
  1942. + arm support in the common compiler sources added
  1943. * moved some generic cg code around
  1944. + tfputype added
  1945. * ...
  1946. Revision 1.67.2.5 2003/08/31 20:44:07 peter
  1947. * fixed getexplicitregisterint tregister value
  1948. Revision 1.67.2.4 2003/08/31 20:40:50 daniel
  1949. * Fixed add_edges_used
  1950. Revision 1.67.2.3 2003/08/29 17:28:59 peter
  1951. * next batch of updates
  1952. Revision 1.67.2.2 2003/08/28 18:35:08 peter
  1953. * tregister changed to cardinal
  1954. Revision 1.67.2.1 2003/08/27 19:55:54 peter
  1955. * first tregister patch
  1956. Revision 1.67 2003/08/23 10:46:21 daniel
  1957. * Register allocator bugfix for h2pas
  1958. Revision 1.66 2003/08/17 16:59:20 jonas
  1959. * fixed regvars so they work with newra (at least for ppc)
  1960. * fixed some volatile register bugs
  1961. + -dnotranslation option for -dnewra, which causes the registers not to
  1962. be translated from virtual to normal registers. Requires support in
  1963. the assembler writer as well, which is only implemented in aggas/
  1964. agppcgas currently
  1965. Revision 1.65 2003/08/17 14:32:48 daniel
  1966. * Precoloured nodes now have an infinite degree approached with 255,
  1967. like they should.
  1968. Revision 1.64 2003/08/17 08:48:02 daniel
  1969. * Another register allocator bug fixed.
  1970. * usable_registers_cnt set to 6 for i386
  1971. Revision 1.63 2003/08/09 18:56:54 daniel
  1972. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1973. allocator
  1974. * Some preventive changes to i386 spillinh code
  1975. Revision 1.62 2003/08/03 14:09:50 daniel
  1976. * Fixed a register allocator bug
  1977. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1978. statements: changes in location_force. These moves are now no longer
  1979. constrained so they are optimized away.
  1980. Revision 1.61 2003/07/21 13:32:39 jonas
  1981. * add_edges_used() is now also called for registers allocated with
  1982. getexplicitregisterint()
  1983. * writing the intereference graph is now only done with -dradebug2 and
  1984. the created files are now called "igraph.<module_name>"
  1985. Revision 1.60 2003/07/06 15:31:21 daniel
  1986. * Fixed register allocator. *Lots* of fixes.
  1987. Revision 1.59 2003/07/06 15:00:47 jonas
  1988. * fixed my previous completely broken commit. It's not perfect though,
  1989. registers > last_int_supreg and < max_intreg may still be "translated"
  1990. Revision 1.58 2003/07/06 14:45:05 jonas
  1991. * support integer registers that are not managed by newra (ie. don't
  1992. translate register numbers that fall outside the range
  1993. first_int_supreg..last_int_supreg)
  1994. Revision 1.57 2003/07/02 22:18:04 peter
  1995. * paraloc splitted in callerparaloc,calleeparaloc
  1996. * sparc calling convention updates
  1997. Revision 1.56 2003/06/17 16:34:44 jonas
  1998. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1999. * renamed all_intregisters to volatile_intregisters and made it
  2000. processor dependent
  2001. Revision 1.55 2003/06/14 14:53:50 jonas
  2002. * fixed newra cycle for x86
  2003. * added constants for indicating source and destination operands of the
  2004. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  2005. Revision 1.54 2003/06/13 21:19:31 peter
  2006. * current_procdef removed, use current_procinfo.procdef instead
  2007. Revision 1.53 2003/06/12 21:11:10 peter
  2008. * ungetregisterfpu gets size parameter
  2009. Revision 1.52 2003/06/12 16:43:07 peter
  2010. * newra compiles for sparc
  2011. Revision 1.51 2003/06/09 14:54:26 jonas
  2012. * (de)allocation of registers for parameters is now performed properly
  2013. (and checked on the ppc)
  2014. - removed obsolete allocation of all parameter registers at the start
  2015. of a procedure (and deallocation at the end)
  2016. Revision 1.50 2003/06/03 21:11:09 peter
  2017. * cg.a_load_* get a from and to size specifier
  2018. * makeregsize only accepts newregister
  2019. * i386 uses generic tcgnotnode,tcgunaryminus
  2020. Revision 1.49 2003/06/03 13:01:59 daniel
  2021. * Register allocator finished
  2022. Revision 1.48 2003/06/01 21:38:06 peter
  2023. * getregisterfpu size parameter added
  2024. * op_const_reg size parameter added
  2025. * sparc updates
  2026. Revision 1.47 2003/05/31 20:31:11 jonas
  2027. * set inital costs of assigning a variable to a register to 120 for
  2028. non-i386, because the used register must be store to memory at the
  2029. start and loaded again at the end
  2030. Revision 1.46 2003/05/30 18:55:21 jonas
  2031. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  2032. works for ppc
  2033. Revision 1.45 2003/05/30 12:36:13 jonas
  2034. * use as little different registers on the ppc until newra is released,
  2035. since every used register must be saved
  2036. Revision 1.44 2003/05/17 13:30:08 jonas
  2037. * changed tt_persistant to tt_persistent :)
  2038. * tempcreatenode now doesn't accept a boolean anymore for persistent
  2039. temps, but a ttemptype, so you can also create ansistring temps etc
  2040. Revision 1.43 2003/05/16 14:33:31 peter
  2041. * regvar fixes
  2042. Revision 1.42 2003/04/26 20:03:49 daniel
  2043. * Bug fix in simplify
  2044. Revision 1.41 2003/04/25 20:59:35 peter
  2045. * removed funcretn,funcretsym, function result is now in varsym
  2046. and aliases for result and function name are added using absolutesym
  2047. * vs_hidden parameter for funcret passed in parameter
  2048. * vs_hidden fixes
  2049. * writenode changed to printnode and released from extdebug
  2050. * -vp option added to generate a tree.log with the nodetree
  2051. * nicer printnode for statements, callnode
  2052. Revision 1.40 2003/04/25 08:25:26 daniel
  2053. * Ifdefs around a lot of calls to cleartempgen
  2054. * Fixed registers that are allocated but not freed in several nodes
  2055. * Tweak to register allocator to cause less spills
  2056. * 8-bit registers now interfere with esi,edi and ebp
  2057. Compiler can now compile rtl successfully when using new register
  2058. allocator
  2059. Revision 1.39 2003/04/23 20:23:06 peter
  2060. * compile fix for no-newra
  2061. Revision 1.38 2003/04/23 14:42:07 daniel
  2062. * Further register allocator work. Compiler now smaller with new
  2063. allocator than without.
  2064. * Somebody forgot to adjust ppu version number
  2065. Revision 1.37 2003/04/22 23:50:23 peter
  2066. * firstpass uses expectloc
  2067. * checks if there are differences between the expectloc and
  2068. location.loc from secondpass in EXTDEBUG
  2069. Revision 1.36 2003/04/22 10:09:35 daniel
  2070. + Implemented the actual register allocator
  2071. + Scratch registers unavailable when new register allocator used
  2072. + maybe_save/maybe_restore unavailable when new register allocator used
  2073. Revision 1.35 2003/04/21 19:16:49 peter
  2074. * count address regs separate
  2075. Revision 1.34 2003/04/17 16:48:21 daniel
  2076. * Added some code to keep track of move instructions in register
  2077. allocator
  2078. Revision 1.33 2003/04/17 07:50:24 daniel
  2079. * Some work on interference graph construction
  2080. Revision 1.32 2003/03/28 19:16:57 peter
  2081. * generic constructor working for i386
  2082. * remove fixed self register
  2083. * esi added as address register for i386
  2084. Revision 1.31 2003/03/11 21:46:24 jonas
  2085. * lots of new regallocator fixes, both in generic and ppc-specific code
  2086. (ppc compiler still can't compile the linux system unit though)
  2087. Revision 1.30 2003/03/09 21:18:59 olle
  2088. + added cutils to the uses clause
  2089. Revision 1.29 2003/03/08 20:36:41 daniel
  2090. + Added newra version of Ti386shlshrnode
  2091. + Added interference graph construction code
  2092. Revision 1.28 2003/03/08 13:59:16 daniel
  2093. * Work to handle new register notation in ag386nsm
  2094. + Added newra version of Ti386moddivnode
  2095. Revision 1.27 2003/03/08 10:53:48 daniel
  2096. * Created newra version of secondmul in n386add.pas
  2097. Revision 1.26 2003/03/08 08:59:07 daniel
  2098. + $define newra will enable new register allocator
  2099. + getregisterint will return imaginary registers with $newra
  2100. + -sr switch added, will skip register allocation so you can see
  2101. the direct output of the code generator before register allocation
  2102. Revision 1.25 2003/02/26 20:50:45 daniel
  2103. * Fixed ungetreference
  2104. Revision 1.24 2003/02/19 22:39:56 daniel
  2105. * Fixed a few issues
  2106. Revision 1.23 2003/02/19 22:00:14 daniel
  2107. * Code generator converted to new register notation
  2108. - Horribily outdated todo.txt removed
  2109. Revision 1.22 2003/02/02 19:25:54 carl
  2110. * Several bugfixes for m68k target (register alloc., opcode emission)
  2111. + VIS target
  2112. + Generic add more complete (still not verified)
  2113. Revision 1.21 2003/01/08 18:43:57 daniel
  2114. * Tregister changed into a record
  2115. Revision 1.20 2002/10/05 12:43:28 carl
  2116. * fixes for Delphi 6 compilation
  2117. (warning : Some features do not work under Delphi)
  2118. Revision 1.19 2002/08/23 16:14:49 peter
  2119. * tempgen cleanup
  2120. * tt_noreuse temp type added that will be used in genentrycode
  2121. Revision 1.18 2002/08/17 22:09:47 florian
  2122. * result type handling in tcgcal.pass_2 overhauled
  2123. * better tnode.dowrite
  2124. * some ppc stuff fixed
  2125. Revision 1.17 2002/08/17 09:23:42 florian
  2126. * first part of procinfo rewrite
  2127. Revision 1.16 2002/08/06 20:55:23 florian
  2128. * first part of ppc calling conventions fix
  2129. Revision 1.15 2002/08/05 18:27:48 carl
  2130. + more more more documentation
  2131. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  2132. Revision 1.14 2002/08/04 19:06:41 carl
  2133. + added generic exception support (still does not work!)
  2134. + more documentation
  2135. Revision 1.13 2002/07/07 09:52:32 florian
  2136. * powerpc target fixed, very simple units can be compiled
  2137. * some basic stuff for better callparanode handling, far from being finished
  2138. Revision 1.12 2002/07/01 18:46:26 peter
  2139. * internal linker
  2140. * reorganized aasm layer
  2141. Revision 1.11 2002/05/18 13:34:17 peter
  2142. * readded missing revisions
  2143. Revision 1.10 2002/05/16 19:46:44 carl
  2144. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2145. + try to fix temp allocation (still in ifdef)
  2146. + generic constructor calls
  2147. + start of tassembler / tmodulebase class cleanup
  2148. Revision 1.8 2002/04/21 15:23:03 carl
  2149. + makeregsize
  2150. + changeregsize is now a local routine
  2151. Revision 1.7 2002/04/20 21:32:25 carl
  2152. + generic FPC_CHECKPOINTER
  2153. + first parameter offset in stack now portable
  2154. * rename some constants
  2155. + move some cpu stuff to other units
  2156. - remove unused constents
  2157. * fix stacksize for some targets
  2158. * fix generic size problems which depend now on EXTEND_SIZE constant
  2159. Revision 1.6 2002/04/15 19:03:31 carl
  2160. + reg2str -> std_reg2str()
  2161. Revision 1.5 2002/04/06 18:13:01 jonas
  2162. * several powerpc-related additions and fixes
  2163. Revision 1.4 2002/04/04 19:06:04 peter
  2164. * removed unused units
  2165. * use tlocation.size in cg.a_*loc*() routines
  2166. Revision 1.3 2002/04/02 17:11:29 peter
  2167. * tlocation,treference update
  2168. * LOC_CONSTANT added for better constant handling
  2169. * secondadd splitted in multiple routines
  2170. * location_force_reg added for loading a location to a register
  2171. of a specified size
  2172. * secondassignment parses now first the right and then the left node
  2173. (this is compatible with Kylix). This saves a lot of push/pop especially
  2174. with string operations
  2175. * adapted some routines to use the new cg methods
  2176. Revision 1.2 2002/04/01 19:24:25 jonas
  2177. * fixed different parameter name in interface and implementation
  2178. declaration of a method (only 1.0.x detected this)
  2179. Revision 1.1 2002/03/31 20:26:36 jonas
  2180. + a_loadfpu_* and a_loadmm_* methods in tcg
  2181. * register allocation is now handled by a class and is mostly processor
  2182. independent (+rgobj.pas and i386/rgcpu.pas)
  2183. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  2184. * some small improvements and fixes to the optimizer
  2185. * some register allocation fixes
  2186. * some fpuvaroffset fixes in the unary minus node
  2187. * push/popusedregisters is now called rg.save/restoreusedregisters and
  2188. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  2189. also better optimizable)
  2190. * fixed and optimized register saving/restoring for new/dispose nodes
  2191. * LOC_FPU locations now also require their "register" field to be set to
  2192. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  2193. - list field removed of the tnode class because it's not used currently
  2194. and can cause hard-to-find bugs
  2195. }