rgobj.pas 71 KB

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