rgobj.pas 80 KB

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