rgobj.pas 78 KB

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