rgobj.pas 70 KB

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