rgobj.pas 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  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. // if reginfo[m].degree=0 then
  668. // internalerror(200312151);
  669. if reginfo[m].degree>0 then
  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. m,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. m:=simplifyworklist.get;
  702. {Push it on the selectstack.}
  703. selectstack.add(m);
  704. include(reginfo[m].flags,ri_selected);
  705. adj:=reginfo[m].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
  724. (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:=(t<first_imaginary) or
  737. (reginfo[t].degree<usable_registers_cnt) 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
  831. (t>=first_imaginary) and
  832. (reginfo[t].degree>0) then
  833. dec(reginfo[t].degree);
  834. end;
  835. end;
  836. if (reginfo[u].degree>=usable_registers_cnt) and
  837. freezeworklist.delete(u) then
  838. spillworklist.add(u);
  839. end;
  840. procedure trgobj.coalesce;
  841. var m:Tmoveins;
  842. x,y,u,v:Tsuperregister;
  843. begin
  844. m:=Tmoveins(worklist_moves.getfirst);
  845. x:=get_alias(m.x);
  846. y:=get_alias(m.y);
  847. if (y<first_imaginary) then
  848. begin
  849. u:=y;
  850. v:=x;
  851. end
  852. else
  853. begin
  854. u:=x;
  855. v:=y;
  856. end;
  857. if (u=v) then
  858. begin
  859. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  860. coalesced_moves.insert(m);
  861. add_worklist(u);
  862. end
  863. {Do u and v interfere? In that case the move is constrained. Two
  864. precoloured nodes interfere allways. If v is precoloured, by the above
  865. code u is precoloured, thus interference...}
  866. else if (v<first_imaginary) or ibitmap[u,v] then
  867. begin
  868. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  869. constrained_moves.insert(m);
  870. add_worklist(u);
  871. add_worklist(v);
  872. end
  873. {Next test: is it possible and a good idea to coalesce??}
  874. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  875. ((u>=first_imaginary) and conservative(u,v)) then
  876. begin
  877. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  878. coalesced_moves.insert(m);
  879. combine(u,v);
  880. add_worklist(u);
  881. end
  882. else
  883. begin
  884. m.moveset:=ms_active_moves;
  885. active_moves.insert(m);
  886. end;
  887. end;
  888. procedure trgobj.freeze_moves(u:Tsuperregister);
  889. var i:cardinal;
  890. m:Tlinkedlistitem;
  891. v,x,y:Tsuperregister;
  892. begin
  893. if reginfo[u].movelist<>nil then
  894. for i:=0 to reginfo[u].movelist^.count-1 do
  895. begin
  896. m:=reginfo[u].movelist^.data[i];
  897. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  898. begin
  899. x:=Tmoveins(m).x;
  900. y:=Tmoveins(m).y;
  901. if get_alias(y)=get_alias(u) then
  902. v:=get_alias(x)
  903. else
  904. v:=get_alias(y);
  905. {Move m from active_moves/worklist_moves to frozen_moves.}
  906. if Tmoveins(m).moveset=ms_active_moves then
  907. active_moves.remove(m)
  908. else
  909. worklist_moves.remove(m);
  910. Tmoveins(m).moveset:=ms_frozen_moves;
  911. frozen_moves.insert(m);
  912. if (v>=first_imaginary) and not(move_related(v)) and
  913. (reginfo[v].degree<usable_registers_cnt) then
  914. begin
  915. freezeworklist.delete(v);
  916. simplifyworklist.add(v);
  917. end;
  918. end;
  919. end;
  920. end;
  921. procedure trgobj.freeze;
  922. var n:Tsuperregister;
  923. begin
  924. { We need to take a random element out of the freezeworklist. We take
  925. the last element. Dirty code! }
  926. n:=freezeworklist.get;
  927. {Add it to the simplifyworklist.}
  928. simplifyworklist.add(n);
  929. freeze_moves(n);
  930. end;
  931. procedure trgobj.select_spill;
  932. var
  933. n : tsuperregister;
  934. adj : psuperregisterworklist;
  935. max,p,i:word;
  936. begin
  937. { We must look for the element with the most interferences in the
  938. spillworklist. This is required because those registers are creating
  939. the most conflicts and keeping them in a register will not reduce the
  940. complexity and even can cause the help registers for the spilling code
  941. to get too much conflicts with the result that the spilling code
  942. will never converge (PFV) }
  943. max:=0;
  944. p:=0;
  945. {Safe: This procedure is only called if length<>0}
  946. for i:=0 to spillworklist.length-1 do
  947. begin
  948. adj:=reginfo[spillworklist.buf[i]].adjlist;
  949. if assigned(adj) and (adj^.length>max) then
  950. begin
  951. p:=i;
  952. max:=adj^.length;
  953. end;
  954. end;
  955. n:=spillworklist.buf[p];
  956. spillworklist.deleteidx(p);
  957. simplifyworklist.add(n);
  958. freeze_moves(n);
  959. end;
  960. procedure trgobj.assign_colours;
  961. {Assign_colours assigns the actual colours to the registers.}
  962. var adj : Psuperregisterworklist;
  963. i,j,k : word;
  964. n,a,c : Tsuperregister;
  965. adj_colours,
  966. colourednodes : Tsuperregisterset;
  967. found : boolean;
  968. begin
  969. spillednodes.clear;
  970. {Reset colours}
  971. for n:=0 to maxreg-1 do
  972. reginfo[n].colour:=n;
  973. {Colour the cpu registers...}
  974. supregset_reset(colourednodes,false);
  975. for n:=0 to first_imaginary-1 do
  976. supregset_include(colourednodes,n);
  977. {Now colour the imaginary registers on the select-stack.}
  978. for i:=selectstack.length downto 1 do
  979. begin
  980. n:=selectstack.buf[i-1];
  981. {Create a list of colours that we cannot assign to n.}
  982. supregset_reset(adj_colours,false);
  983. adj:=reginfo[n].adjlist;
  984. if adj<>nil then
  985. for j:=0 to adj^.length-1 do
  986. begin
  987. a:=get_alias(adj^.buf[j]);
  988. if supregset_in(colourednodes,a) then
  989. supregset_include(adj_colours,reginfo[a].colour);
  990. end;
  991. supregset_include(adj_colours,RS_STACK_POINTER_REG);
  992. {Assume a spill by default...}
  993. found:=false;
  994. {Search for a colour not in this list.}
  995. for k:=0 to usable_registers_cnt-1 do
  996. begin
  997. c:=usable_registers[k];
  998. if not(supregset_in(adj_colours,c)) then
  999. begin
  1000. reginfo[n].colour:=c;
  1001. found:=true;
  1002. supregset_include(colourednodes,n);
  1003. include(used_in_proc,c);
  1004. break;
  1005. end;
  1006. end;
  1007. if not found then
  1008. spillednodes.add(n);
  1009. end;
  1010. {Finally colour the nodes that were coalesced.}
  1011. for i:=1 to coalescednodes.length do
  1012. begin
  1013. n:=coalescednodes.buf[i-1];
  1014. k:=get_alias(n);
  1015. reginfo[n].colour:=reginfo[k].colour;
  1016. if reginfo[k].colour<maxcpuregister then
  1017. include(used_in_proc,reginfo[k].colour);
  1018. end;
  1019. {$ifdef ra_debug}
  1020. if aktfilepos.line=51 then
  1021. begin
  1022. writeln('colourlist');
  1023. for i:=0 to maxreg-1 do
  1024. writeln(i:4,' ',reginfo[i].colour:4)
  1025. end;
  1026. {$endif ra_debug}
  1027. end;
  1028. procedure trgobj.colour_registers;
  1029. begin
  1030. repeat
  1031. if simplifyworklist.length<>0 then
  1032. simplify
  1033. else if not(worklist_moves.empty) then
  1034. coalesce
  1035. else if freezeworklist.length<>0 then
  1036. freeze
  1037. else if spillworklist.length<>0 then
  1038. select_spill;
  1039. until (simplifyworklist.length=0) and
  1040. worklist_moves.empty and
  1041. (freezeworklist.length=0) and
  1042. (spillworklist.length=0);
  1043. assign_colours;
  1044. end;
  1045. procedure trgobj.epilogue_colouring;
  1046. {
  1047. procedure move_to_worklist_moves(list:Tlinkedlist);
  1048. var p:Tlinkedlistitem;
  1049. begin
  1050. p:=list.first;
  1051. while p<>nil do
  1052. begin
  1053. Tmoveins(p).moveset:=ms_worklist_moves;
  1054. p:=p.next;
  1055. end;
  1056. worklist_moves.concatlist(list);
  1057. end;
  1058. }
  1059. var i:Tsuperregister;
  1060. begin
  1061. worklist_moves.clear;
  1062. {$ifdef Principle_wrong_by_definition}
  1063. {Move everything back to worklist_moves.}
  1064. move_to_worklist_moves(active_moves);
  1065. move_to_worklist_moves(frozen_moves);
  1066. move_to_worklist_moves(coalesced_moves);
  1067. move_to_worklist_moves(constrained_moves);
  1068. {$endif Principle_wrong_by_definition}
  1069. active_moves.destroy;
  1070. active_moves:=nil;
  1071. frozen_moves.destroy;
  1072. frozen_moves:=nil;
  1073. coalesced_moves.destroy;
  1074. coalesced_moves:=nil;
  1075. constrained_moves.destroy;
  1076. constrained_moves:=nil;
  1077. for i:=0 to maxreg-1 do
  1078. if reginfo[i].movelist<>nil then
  1079. begin
  1080. dispose(reginfo[i].movelist);
  1081. reginfo[i].movelist:=nil;
  1082. end;
  1083. end;
  1084. procedure trgobj.clear_interferences(u:Tsuperregister);
  1085. {Remove node u from the interference graph and remove all collected
  1086. move instructions it is associated with.}
  1087. var i : word;
  1088. v : Tsuperregister;
  1089. adj,adj2 : Psuperregisterworklist;
  1090. {$ifdef Principle_wrong_by_definition}
  1091. k,j,count : cardinal;
  1092. m,n : Tmoveins;
  1093. {$endif Principle_wrong_by_definition}
  1094. begin
  1095. adj:=reginfo[u].adjlist;
  1096. if adj<>nil then
  1097. begin
  1098. for i:=1 to adj^.length do
  1099. begin
  1100. v:=adj^.buf[i-1];
  1101. {Remove (u,v) and (v,u) from bitmap.}
  1102. ibitmap[u,v]:=false;
  1103. ibitmap[v,u]:=false;
  1104. {Remove (v,u) from adjacency list.}
  1105. adj2:=reginfo[v].adjlist;
  1106. if adj2<>nil then
  1107. begin
  1108. adj2^.delete(v);
  1109. if adj2^.length=0 then
  1110. begin
  1111. dispose(adj2,done);
  1112. reginfo[v].adjlist:=nil;
  1113. end;
  1114. end;
  1115. end;
  1116. {Remove ( u,* ) from adjacency list.}
  1117. dispose(adj,done);
  1118. reginfo[u].adjlist:=nil;
  1119. end;
  1120. {$ifdef Principle_wrong_by_definition}
  1121. {Now remove the moves.}
  1122. if movelist[u]<>nil then
  1123. begin
  1124. for j:=0 to movelist[u]^.count-1 do
  1125. begin
  1126. m:=Tmoveins(movelist[u]^.data[j]);
  1127. {Get the other register of the move instruction.}
  1128. v:=m.instruction.oper[0]^.reg.number shr 8;
  1129. if v=u then
  1130. v:=m.instruction.oper[1]^.reg.number shr 8;
  1131. repeat
  1132. repeat
  1133. if (u<>v) and (movelist[v]<>nil) then
  1134. begin
  1135. {Remove the move from it's movelist.}
  1136. count:=movelist[v]^.count-1;
  1137. for k:=0 to count do
  1138. if m=movelist[v]^.data[k] then
  1139. begin
  1140. if k<>count then
  1141. movelist[v]^.data[k]:=movelist[v]^.data[count];
  1142. dec(movelist[v]^.count);
  1143. if count=0 then
  1144. begin
  1145. dispose(movelist[v]);
  1146. movelist[v]:=nil;
  1147. end;
  1148. break;
  1149. end;
  1150. end;
  1151. {The complexity is enourmous: the register might have been
  1152. coalesced. In that case it's movelists have been added to
  1153. it's coalescing alias. (DM)}
  1154. v:=alias[v];
  1155. until v=0;
  1156. {And also register u might have been coalesced.}
  1157. u:=alias[u];
  1158. until u=0;
  1159. case m.moveset of
  1160. ms_coalesced_moves:
  1161. coalesced_moves.remove(m);
  1162. ms_constrained_moves:
  1163. constrained_moves.remove(m);
  1164. ms_frozen_moves:
  1165. frozen_moves.remove(m);
  1166. ms_worklist_moves:
  1167. worklist_moves.remove(m);
  1168. ms_active_moves:
  1169. active_moves.remove(m);
  1170. end;
  1171. end;
  1172. dispose(movelist[u]);
  1173. movelist[u]:=nil;
  1174. end;
  1175. {$endif Principle_wrong_by_definition}
  1176. end;
  1177. procedure trgobj.getregisterinline(list:Taasmoutput;
  1178. position:Tai;subreg:Tsubregister;var result:Tregister);
  1179. var p:Tsuperregister;
  1180. r:Tregister;
  1181. begin
  1182. p:=getnewreg(subreg);
  1183. live_registers.add(p);
  1184. r:=newreg(regtype,p,subreg);
  1185. if position=nil then
  1186. list.insert(Tai_regalloc.alloc(r))
  1187. else
  1188. list.insertafter(Tai_regalloc.alloc(r),position);
  1189. add_edges_used(p);
  1190. add_constraints(r);
  1191. result:=r;
  1192. end;
  1193. procedure trgobj.ungetregisterinline(list:Taasmoutput;
  1194. position:Tai;r:Tregister);
  1195. var supreg:Tsuperregister;
  1196. begin
  1197. supreg:=getsupreg(r);
  1198. live_registers.delete(supreg);
  1199. if position=nil then
  1200. list.insert(Tai_regalloc.dealloc(r))
  1201. else
  1202. list.insertafter(Tai_regalloc.dealloc(r),position);
  1203. end;
  1204. procedure trgobj.insert_regalloc_info(list:Taasmoutput;headertai:tai);
  1205. var
  1206. supreg : tsuperregister;
  1207. p : tai;
  1208. r : tregister;
  1209. begin
  1210. { Insert regallocs for all imaginary registers }
  1211. for supreg:=first_imaginary to maxreg-1 do
  1212. begin
  1213. r:=newreg(regtype,supreg,reginfo[supreg].subreg);
  1214. if assigned(reginfo[supreg].live_start) then
  1215. begin
  1216. {$ifdef EXTDEBUG}
  1217. if reginfo[supreg].live_start=reginfo[supreg].live_end then
  1218. Comment(V_Warning,'Register '+std_regname(r)+' is only used once');
  1219. {$endif EXTDEBUG}
  1220. list.insertbefore(Tai_regalloc.alloc(r),reginfo[supreg].live_start);
  1221. { Insert live end deallocation before reg allocations
  1222. to reduce conflicts }
  1223. p:=reginfo[supreg].live_end;
  1224. while assigned(p) and
  1225. assigned(p.previous) and
  1226. (tai(p.previous).typ=ait_regalloc) and
  1227. tai_regalloc(p.previous).allocation and
  1228. (tai_regalloc(p.previous).reg<>r) do
  1229. p:=tai(p.previous);
  1230. list.insertbefore(Tai_regalloc.dealloc(r),p);
  1231. end
  1232. {$ifdef EXTDEBUG}
  1233. else
  1234. Comment(V_Warning,'Register '+std_regname(r)+' not used');
  1235. {$endif EXTDEBUG}
  1236. end;
  1237. end;
  1238. procedure trgobj.generate_interference_graph(list:Taasmoutput;headertai:tai);
  1239. var
  1240. p : tai;
  1241. i : integer;
  1242. supreg : tsuperregister;
  1243. begin
  1244. { All allocations are available. Now we can generate the
  1245. interference graph. Walk through all instructions, we can
  1246. start with the headertai, because before the header tai is
  1247. only symbols. }
  1248. live_registers.clear;
  1249. p:=headertai;
  1250. while assigned(p) do
  1251. begin
  1252. case p.typ of
  1253. ait_regalloc:
  1254. begin
  1255. if (getregtype(Tai_regalloc(p).reg)=regtype) then
  1256. begin
  1257. supreg:=getsupreg(Tai_regalloc(p).reg);
  1258. if Tai_regalloc(p).allocation then
  1259. live_registers.add(supreg)
  1260. else
  1261. live_registers.delete(supreg);
  1262. add_edges_used(supreg);
  1263. add_constraints(Tai_regalloc(p).reg);
  1264. end;
  1265. end;
  1266. { ait_instruction:
  1267. begin
  1268. aktfilepos:=Taicpu_abstract(p).fileinfo;
  1269. for i:=0 to Taicpu_abstract(p).ops-1 do
  1270. with Taicpu_abstract(p).oper[i]^ do
  1271. begin
  1272. case typ of
  1273. top_reg :
  1274. begin
  1275. add_edges_used(getsupreg(reg));
  1276. add_constraints(reg);
  1277. end;
  1278. top_ref :
  1279. begin
  1280. add_edges_used(getsupreg(ref^.base));
  1281. add_constraints(ref^.base);
  1282. add_edges_used(getsupreg(ref^.index));
  1283. add_constraints(ref^.index);
  1284. end;
  1285. end;
  1286. end;
  1287. end; }
  1288. end;
  1289. p:=Tai(p.next);
  1290. end;
  1291. {$ifdef EXTDEBUG}
  1292. if live_registers.length>0 then
  1293. begin
  1294. for i:=0 to live_registers.length-1 do
  1295. begin
  1296. { Only report for imaginary registers }
  1297. if live_registers.buf[i]>=first_imaginary then
  1298. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,live_registers.buf[i],defaultsub))+' not released');
  1299. end;
  1300. end;
  1301. {$endif}
  1302. end;
  1303. function trgobj.spill_registers(list:Taasmoutput;headertai:tai):boolean;
  1304. {Returns true if any help registers have been used.}
  1305. var i : word;
  1306. t : tsuperregister;
  1307. p,q : Tai;
  1308. regs_to_spill_set:Tsuperregisterset;
  1309. spill_temps : ^Tspill_temp_list;
  1310. supreg : tsuperregister;
  1311. templist : taasmoutput;
  1312. begin
  1313. spill_registers:=false;
  1314. live_registers.clear;
  1315. {Precoloured nodes should have an infinite degree, which we can approach
  1316. by 255.}
  1317. for i:=0 to first_imaginary-1 do
  1318. reginfo[i].degree:=high(tsuperregister);
  1319. for i:=first_imaginary to maxreg-1 do
  1320. begin
  1321. reginfo[i].degree:=0;
  1322. reginfo[i].flags:=[];
  1323. end;
  1324. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1325. supregset_reset(regs_to_spill_set,false);
  1326. { Allocate temps and insert in front of the list }
  1327. templist:=taasmoutput.create;
  1328. {Safe: this procedure is only called if there are spilled nodes.}
  1329. for i:=0 to spillednodes.length-1 do
  1330. begin
  1331. t:=spillednodes.buf[i];
  1332. {Alternative representation.}
  1333. supregset_include(regs_to_spill_set,t);
  1334. {Clear all interferences of the spilled register.}
  1335. clear_interferences(t);
  1336. {Get a temp for the spilled register}
  1337. tg.gettemp(templist,4,tt_noreuse,spill_temps^[t]);
  1338. end;
  1339. list.insertlistafter(headertai,templist);
  1340. templist.free;
  1341. { Walk through all instructions, we can start with the headertai,
  1342. because before the header tai is only symbols }
  1343. p:=headertai;
  1344. while assigned(p) do
  1345. begin
  1346. case p.typ of
  1347. ait_regalloc:
  1348. begin
  1349. if (getregtype(Tai_regalloc(p).reg)=regtype) then
  1350. begin
  1351. {A register allocation of a spilled register can be removed.}
  1352. supreg:=getsupreg(Tai_regalloc(p).reg);
  1353. if supregset_in(regs_to_spill_set,supreg) then
  1354. begin
  1355. q:=Tai(p.next);
  1356. list.remove(p);
  1357. p.free;
  1358. p:=q;
  1359. continue;
  1360. end
  1361. else
  1362. if Tai_regalloc(p).allocation then
  1363. live_registers.add(supreg)
  1364. else
  1365. live_registers.delete(supreg);
  1366. end;
  1367. end;
  1368. ait_instruction:
  1369. begin
  1370. aktfilepos:=Taicpu_abstract(p).fileinfo;
  1371. if Taicpu_abstract(p).spill_registers(list,
  1372. regtype,
  1373. @getregisterinline,
  1374. @ungetregisterinline,
  1375. regs_to_spill_set,
  1376. live_registers,
  1377. spill_temps^) then
  1378. spill_registers:=true;
  1379. if Taicpu_abstract(p).is_reg_move then
  1380. add_move_instruction(Taicpu(p));
  1381. end;
  1382. end;
  1383. p:=Tai(p.next);
  1384. end;
  1385. aktfilepos:=current_procinfo.exitpos;
  1386. {Safe: this procedure is only called if there are spilled nodes.}
  1387. for i:=0 to spillednodes.length-1 do
  1388. tg.ungettemp(list,spill_temps^[spillednodes.buf[i]]);
  1389. freemem(spill_temps);
  1390. end;
  1391. procedure Trgobj.translate_registers(list:taasmoutput);
  1392. var hp,p,q:Tai;
  1393. i:shortint;
  1394. r:Preference;
  1395. {$ifdef arm}
  1396. so:pshifterop;
  1397. {$endif arm}
  1398. begin
  1399. { Leave when no imaginary registers are used }
  1400. if maxreg<=first_imaginary then
  1401. exit;
  1402. p:=Tai(list.first);
  1403. while assigned(p) do
  1404. begin
  1405. case p.typ of
  1406. ait_regalloc:
  1407. begin
  1408. if (getregtype(Tai_regalloc(p).reg)=regtype) then
  1409. setsupreg(Tai_regalloc(p).reg,reginfo[getsupreg(Tai_regalloc(p).reg)].colour);
  1410. {
  1411. Remove sequences of release and
  1412. allocation of the same register like:
  1413. # Register X released
  1414. # Register X allocated
  1415. }
  1416. if assigned(p.previous) and
  1417. (Tai(p.previous).typ=ait_regalloc) and
  1418. (Tai_regalloc(p.previous).reg=Tai_regalloc(p).reg) and
  1419. { allocation,deallocation or deallocation,allocation }
  1420. (Tai_regalloc(p.previous).allocation xor Tai_regalloc(p).allocation) then
  1421. begin
  1422. q:=Tai(p.next);
  1423. hp:=tai(p.previous);
  1424. list.remove(hp);
  1425. hp.free;
  1426. list.remove(p);
  1427. p.free;
  1428. p:=q;
  1429. continue;
  1430. end;
  1431. end;
  1432. ait_instruction:
  1433. begin
  1434. for i:=0 to Taicpu_abstract(p).ops-1 do
  1435. case Taicpu_abstract(p).oper[i]^.typ of
  1436. Top_reg:
  1437. if (getregtype(Taicpu_abstract(p).oper[i]^.reg)=regtype) then
  1438. setsupreg(Taicpu_abstract(p).oper[i]^.reg,reginfo[getsupreg(Taicpu_abstract(p).oper[i]^.reg)].colour);
  1439. Top_ref:
  1440. begin
  1441. if regtype=R_INTREGISTER then
  1442. begin
  1443. r:=Taicpu_abstract(p).oper[i]^.ref;
  1444. if r^.base<>NR_NO then
  1445. setsupreg(r^.base,reginfo[getsupreg(r^.base)].colour);
  1446. if r^.index<>NR_NO then
  1447. setsupreg(r^.index,reginfo[getsupreg(r^.index)].colour);
  1448. end;
  1449. end;
  1450. {$ifdef arm}
  1451. Top_shifterop:
  1452. begin
  1453. so:=Taicpu_abstract(p).oper[i]^.shifterop;
  1454. if so^.rs<>NR_NO then
  1455. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1456. end;
  1457. {$endif arm}
  1458. end;
  1459. { Maybe the operation can be removed when
  1460. it is a move and both arguments are the same }
  1461. if Taicpu_abstract(p).is_same_reg_move then
  1462. begin
  1463. q:=Tai(p.next);
  1464. list.remove(p);
  1465. p.free;
  1466. p:=q;
  1467. continue;
  1468. end;
  1469. end;
  1470. end;
  1471. p:=Tai(p.next);
  1472. end;
  1473. end;
  1474. end.
  1475. {
  1476. $Log$
  1477. Revision 1.109 2003-12-26 14:02:30 peter
  1478. * sparc updates
  1479. * use registertype in spill_register
  1480. Revision 1.108 2003/12/22 23:09:34 peter
  1481. * only report unreleased imaginary registers
  1482. Revision 1.107 2003/12/22 22:13:46 peter
  1483. * made decrease_degree working, but not really fixed
  1484. Revision 1.106 2003/12/18 17:06:21 florian
  1485. * arm compiler compilation fixed
  1486. Revision 1.105 2003/12/17 21:59:05 peter
  1487. * don't insert dealloc before alloc of the same register
  1488. Revision 1.104 2003/12/16 09:41:44 daniel
  1489. * Automatic conversion from integer constants to pointer constants is no
  1490. longer done except in Delphi mode
  1491. Revision 1.103 2003/12/15 21:25:49 peter
  1492. * reg allocations for imaginary register are now inserted just
  1493. before reg allocation
  1494. * tregister changed to enum to allow compile time check
  1495. * fixed several tregister-tsuperregister errors
  1496. Revision 1.102 2003/12/15 16:37:47 daniel
  1497. * More microoptimizations
  1498. Revision 1.101 2003/12/15 15:58:58 peter
  1499. * fix statedebug compile
  1500. Revision 1.100 2003/12/14 20:24:28 daniel
  1501. * Register allocator speed optimizations
  1502. - Worklist no longer a ringbuffer
  1503. - No find operations are left
  1504. - Simplify now done in constant time
  1505. - unusedregs is now a Tsuperregisterworklist
  1506. - Microoptimizations
  1507. Revision 1.99 2003/12/12 17:16:17 peter
  1508. * rg[tregistertype] added in tcg
  1509. Revision 1.98 2003/12/04 23:27:32 peter
  1510. * remove redundant calls to add_edge_used
  1511. Revision 1.97 2003/11/29 17:36:41 peter
  1512. * check for add_move_instruction
  1513. Revision 1.96 2003/11/24 15:17:37 florian
  1514. * changed some types to prevend range check errors
  1515. Revision 1.95 2003/11/10 19:05:50 peter
  1516. * fixed alias/colouring > 255
  1517. Revision 1.94 2003/11/07 15:58:32 florian
  1518. * Florian's culmutative nr. 1; contains:
  1519. - invalid calling conventions for a certain cpu are rejected
  1520. - arm softfloat calling conventions
  1521. - -Sp for cpu dependend code generation
  1522. - several arm fixes
  1523. - remaining code for value open array paras on heap
  1524. Revision 1.93 2003/10/30 16:22:40 peter
  1525. * call firstpass before allocation and codegeneration is started
  1526. * move leftover code from pass_2.generatecode() to psub
  1527. Revision 1.92 2003/10/29 21:29:14 jonas
  1528. * some ALLOWDUPREG improvements
  1529. Revision 1.91 2003/10/21 15:15:36 peter
  1530. * taicpu_abstract.oper[] changed to pointers
  1531. Revision 1.90 2003/10/19 12:36:36 florian
  1532. * improved speed; reduced memory usage of the interference bitmap
  1533. Revision 1.89 2003/10/19 01:34:30 florian
  1534. * some ppc stuff fixed
  1535. * memory leak fixed
  1536. Revision 1.88 2003/10/18 15:41:26 peter
  1537. * made worklists dynamic in size
  1538. Revision 1.87 2003/10/17 16:16:08 peter
  1539. * fixed last commit
  1540. Revision 1.86 2003/10/17 15:25:18 florian
  1541. * fixed more ppc stuff
  1542. Revision 1.85 2003/10/17 14:38:32 peter
  1543. * 64k registers supported
  1544. * fixed some memory leaks
  1545. Revision 1.84 2003/10/11 16:06:42 florian
  1546. * fixed some MMX<->SSE
  1547. * started to fix ppc, needs an overhaul
  1548. + stabs info improve for spilling, not sure if it works correctly/completly
  1549. - MMX_SUPPORT removed from Makefile.fpc
  1550. Revision 1.83 2003/10/10 17:48:14 peter
  1551. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  1552. * tregisteralloctor renamed to trgobj
  1553. * removed rgobj from a lot of units
  1554. * moved location_* and reference_* to cgobj
  1555. * first things for mmx register allocation
  1556. Revision 1.82 2003/10/09 21:31:37 daniel
  1557. * Register allocator splitted, ans abstract now
  1558. Revision 1.81 2003/10/01 20:34:49 peter
  1559. * procinfo unit contains tprocinfo
  1560. * cginfo renamed to cgbase
  1561. * moved cgmessage to verbose
  1562. * fixed ppc and sparc compiles
  1563. Revision 1.80 2003/09/30 19:54:42 peter
  1564. * reuse registers with the least conflicts
  1565. Revision 1.79 2003/09/29 20:58:56 peter
  1566. * optimized releasing of registers
  1567. Revision 1.78 2003/09/28 13:41:12 peter
  1568. * return reg 255 when allowdupreg is defined
  1569. Revision 1.77 2003/09/25 16:19:32 peter
  1570. * fix filepositions
  1571. * insert spill temp allocations at the start of the proc
  1572. Revision 1.76 2003/09/16 16:17:01 peter
  1573. * varspez in calls to push_addr_param
  1574. Revision 1.75 2003/09/12 19:07:42 daniel
  1575. * Fixed fast spilling functionality by re-adding the code that initializes
  1576. precoloured nodes to degree 255. I would like to play hangman on the one
  1577. who removed that code.
  1578. Revision 1.74 2003/09/11 11:54:59 florian
  1579. * improved arm code generation
  1580. * move some protected and private field around
  1581. * the temp. register for register parameters/arguments are now released
  1582. before the move to the parameter register is done. This improves
  1583. the code in a lot of cases.
  1584. Revision 1.73 2003/09/09 20:59:27 daniel
  1585. * Adding register allocation order
  1586. Revision 1.72 2003/09/09 15:55:44 peter
  1587. * use register with least interferences in spillregister
  1588. Revision 1.71 2003/09/07 22:09:35 peter
  1589. * preparations for different default calling conventions
  1590. * various RA fixes
  1591. Revision 1.70 2003/09/03 21:06:45 peter
  1592. * fixes for FPU register allocation
  1593. Revision 1.69 2003/09/03 15:55:01 peter
  1594. * NEWRA branch merged
  1595. Revision 1.68 2003/09/03 11:18:37 florian
  1596. * fixed arm concatcopy
  1597. + arm support in the common compiler sources added
  1598. * moved some generic cg code around
  1599. + tfputype added
  1600. * ...
  1601. Revision 1.67.2.5 2003/08/31 20:44:07 peter
  1602. * fixed getexplicitregisterint tregister value
  1603. Revision 1.67.2.4 2003/08/31 20:40:50 daniel
  1604. * Fixed add_edges_used
  1605. Revision 1.67.2.3 2003/08/29 17:28:59 peter
  1606. * next batch of updates
  1607. Revision 1.67.2.2 2003/08/28 18:35:08 peter
  1608. * tregister changed to cardinal
  1609. Revision 1.67.2.1 2003/08/27 19:55:54 peter
  1610. * first tregister patch
  1611. Revision 1.67 2003/08/23 10:46:21 daniel
  1612. * Register allocator bugfix for h2pas
  1613. Revision 1.66 2003/08/17 16:59:20 jonas
  1614. * fixed regvars so they work with newra (at least for ppc)
  1615. * fixed some volatile register bugs
  1616. + -dnotranslation option for -dnewra, which causes the registers not to
  1617. be translated from virtual to normal registers. Requires support in
  1618. the assembler writer as well, which is only implemented in aggas/
  1619. agppcgas currently
  1620. Revision 1.65 2003/08/17 14:32:48 daniel
  1621. * Precoloured nodes now have an infinite degree approached with 255,
  1622. like they should.
  1623. Revision 1.64 2003/08/17 08:48:02 daniel
  1624. * Another register allocator bug fixed.
  1625. * usable_registers_cnt set to 6 for i386
  1626. Revision 1.63 2003/08/09 18:56:54 daniel
  1627. * cs_regalloc renamed to cs_regvars to avoid confusion with register
  1628. allocator
  1629. * Some preventive changes to i386 spillinh code
  1630. Revision 1.62 2003/08/03 14:09:50 daniel
  1631. * Fixed a register allocator bug
  1632. * Figured out why -dnewra generates superfluous "mov reg1,reg2"
  1633. statements: changes in location_force. These moves are now no longer
  1634. constrained so they are optimized away.
  1635. Revision 1.61 2003/07/21 13:32:39 jonas
  1636. * add_edges_used() is now also called for registers allocated with
  1637. getexplicitregisterint()
  1638. * writing the intereference graph is now only done with -dradebug2 and
  1639. the created files are now called "igraph.<module_name>"
  1640. Revision 1.60 2003/07/06 15:31:21 daniel
  1641. * Fixed register allocator. *Lots* of fixes.
  1642. Revision 1.59 2003/07/06 15:00:47 jonas
  1643. * fixed my previous completely broken commit. It's not perfect though,
  1644. registers > last_int_supreg and < max_intreg may still be "translated"
  1645. Revision 1.58 2003/07/06 14:45:05 jonas
  1646. * support integer registers that are not managed by newra (ie. don't
  1647. translate register numbers that fall outside the range
  1648. first_int_supreg..last_int_supreg)
  1649. Revision 1.57 2003/07/02 22:18:04 peter
  1650. * paraloc splitted in callerparaloc,calleeparaloc
  1651. * sparc calling convention updates
  1652. Revision 1.56 2003/06/17 16:34:44 jonas
  1653. * lots of newra fixes (need getfuncretparaloc implementation for i386)!
  1654. * renamed all_intregisters to volatile_intregisters and made it
  1655. processor dependent
  1656. Revision 1.55 2003/06/14 14:53:50 jonas
  1657. * fixed newra cycle for x86
  1658. * added constants for indicating source and destination operands of the
  1659. "move reg,reg" instruction to aasmcpu (and use those in rgobj)
  1660. Revision 1.54 2003/06/13 21:19:31 peter
  1661. * current_procdef removed, use current_procinfo.procdef instead
  1662. Revision 1.53 2003/06/12 21:11:10 peter
  1663. * ungetregisterfpu gets size parameter
  1664. Revision 1.52 2003/06/12 16:43:07 peter
  1665. * newra compiles for sparc
  1666. Revision 1.51 2003/06/09 14:54:26 jonas
  1667. * (de)allocation of registers for parameters is now performed properly
  1668. (and checked on the ppc)
  1669. - removed obsolete allocation of all parameter registers at the start
  1670. of a procedure (and deallocation at the end)
  1671. Revision 1.50 2003/06/03 21:11:09 peter
  1672. * cg.a_load_* get a from and to size specifier
  1673. * makeregsize only accepts newregister
  1674. * i386 uses generic tcgnotnode,tcgunaryminus
  1675. Revision 1.49 2003/06/03 13:01:59 daniel
  1676. * Register allocator finished
  1677. Revision 1.48 2003/06/01 21:38:06 peter
  1678. * getregisterfpu size parameter added
  1679. * op_const_reg size parameter added
  1680. * sparc updates
  1681. Revision 1.47 2003/05/31 20:31:11 jonas
  1682. * set inital costs of assigning a variable to a register to 120 for
  1683. non-i386, because the used register must be store to memory at the
  1684. start and loaded again at the end
  1685. Revision 1.46 2003/05/30 18:55:21 jonas
  1686. * fixed several regvar related bugs for non-i386. make cycle with -Or now
  1687. works for ppc
  1688. Revision 1.45 2003/05/30 12:36:13 jonas
  1689. * use as little different registers on the ppc until newra is released,
  1690. since every used register must be saved
  1691. Revision 1.44 2003/05/17 13:30:08 jonas
  1692. * changed tt_persistant to tt_persistent :)
  1693. * tempcreatenode now doesn't accept a boolean anymore for persistent
  1694. temps, but a ttemptype, so you can also create ansistring temps etc
  1695. Revision 1.43 2003/05/16 14:33:31 peter
  1696. * regvar fixes
  1697. Revision 1.42 2003/04/26 20:03:49 daniel
  1698. * Bug fix in simplify
  1699. Revision 1.41 2003/04/25 20:59:35 peter
  1700. * removed funcretn,funcretsym, function result is now in varsym
  1701. and aliases for result and function name are added using absolutesym
  1702. * vs_hidden parameter for funcret passed in parameter
  1703. * vs_hidden fixes
  1704. * writenode changed to printnode and released from extdebug
  1705. * -vp option added to generate a tree.log with the nodetree
  1706. * nicer printnode for statements, callnode
  1707. Revision 1.40 2003/04/25 08:25:26 daniel
  1708. * Ifdefs around a lot of calls to cleartempgen
  1709. * Fixed registers that are allocated but not freed in several nodes
  1710. * Tweak to register allocator to cause less spills
  1711. * 8-bit registers now interfere with esi,edi and ebp
  1712. Compiler can now compile rtl successfully when using new register
  1713. allocator
  1714. Revision 1.39 2003/04/23 20:23:06 peter
  1715. * compile fix for no-newra
  1716. Revision 1.38 2003/04/23 14:42:07 daniel
  1717. * Further register allocator work. Compiler now smaller with new
  1718. allocator than without.
  1719. * Somebody forgot to adjust ppu version number
  1720. Revision 1.37 2003/04/22 23:50:23 peter
  1721. * firstpass uses expectloc
  1722. * checks if there are differences between the expectloc and
  1723. location.loc from secondpass in EXTDEBUG
  1724. Revision 1.36 2003/04/22 10:09:35 daniel
  1725. + Implemented the actual register allocator
  1726. + Scratch registers unavailable when new register allocator used
  1727. + maybe_save/maybe_restore unavailable when new register allocator used
  1728. Revision 1.35 2003/04/21 19:16:49 peter
  1729. * count address regs separate
  1730. Revision 1.34 2003/04/17 16:48:21 daniel
  1731. * Added some code to keep track of move instructions in register
  1732. allocator
  1733. Revision 1.33 2003/04/17 07:50:24 daniel
  1734. * Some work on interference graph construction
  1735. Revision 1.32 2003/03/28 19:16:57 peter
  1736. * generic constructor working for i386
  1737. * remove fixed self register
  1738. * esi added as address register for i386
  1739. Revision 1.31 2003/03/11 21:46:24 jonas
  1740. * lots of new regallocator fixes, both in generic and ppc-specific code
  1741. (ppc compiler still can't compile the linux system unit though)
  1742. Revision 1.30 2003/03/09 21:18:59 olle
  1743. + added cutils to the uses clause
  1744. Revision 1.29 2003/03/08 20:36:41 daniel
  1745. + Added newra version of Ti386shlshrnode
  1746. + Added interference graph construction code
  1747. Revision 1.28 2003/03/08 13:59:16 daniel
  1748. * Work to handle new register notation in ag386nsm
  1749. + Added newra version of Ti386moddivnode
  1750. Revision 1.27 2003/03/08 10:53:48 daniel
  1751. * Created newra version of secondmul in n386add.pas
  1752. Revision 1.26 2003/03/08 08:59:07 daniel
  1753. + $define newra will enable new register allocator
  1754. + getregisterint will return imaginary registers with $newra
  1755. + -sr switch added, will skip register allocation so you can see
  1756. the direct output of the code generator before register allocation
  1757. Revision 1.25 2003/02/26 20:50:45 daniel
  1758. * Fixed ungetreference
  1759. Revision 1.24 2003/02/19 22:39:56 daniel
  1760. * Fixed a few issues
  1761. Revision 1.23 2003/02/19 22:00:14 daniel
  1762. * Code generator converted to new register notation
  1763. - Horribily outdated todo.txt removed
  1764. Revision 1.22 2003/02/02 19:25:54 carl
  1765. * Several bugfixes for m68k target (register alloc., opcode emission)
  1766. + VIS target
  1767. + Generic add more complete (still not verified)
  1768. Revision 1.21 2003/01/08 18:43:57 daniel
  1769. * Tregister changed into a record
  1770. Revision 1.20 2002/10/05 12:43:28 carl
  1771. * fixes for Delphi 6 compilation
  1772. (warning : Some features do not work under Delphi)
  1773. Revision 1.19 2002/08/23 16:14:49 peter
  1774. * tempgen cleanup
  1775. * tt_noreuse temp type added that will be used in genentrycode
  1776. Revision 1.18 2002/08/17 22:09:47 florian
  1777. * result type handling in tcgcal.pass_2 overhauled
  1778. * better tnode.dowrite
  1779. * some ppc stuff fixed
  1780. Revision 1.17 2002/08/17 09:23:42 florian
  1781. * first part of procinfo rewrite
  1782. Revision 1.16 2002/08/06 20:55:23 florian
  1783. * first part of ppc calling conventions fix
  1784. Revision 1.15 2002/08/05 18:27:48 carl
  1785. + more more more documentation
  1786. + first version include/exclude (can't test though, not enough scratch for i386 :()...
  1787. Revision 1.14 2002/08/04 19:06:41 carl
  1788. + added generic exception support (still does not work!)
  1789. + more documentation
  1790. Revision 1.13 2002/07/07 09:52:32 florian
  1791. * powerpc target fixed, very simple units can be compiled
  1792. * some basic stuff for better callparanode handling, far from being finished
  1793. Revision 1.12 2002/07/01 18:46:26 peter
  1794. * internal linker
  1795. * reorganized aasm layer
  1796. Revision 1.11 2002/05/18 13:34:17 peter
  1797. * readded missing revisions
  1798. Revision 1.10 2002/05/16 19:46:44 carl
  1799. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1800. + try to fix temp allocation (still in ifdef)
  1801. + generic constructor calls
  1802. + start of tassembler / tmodulebase class cleanup
  1803. Revision 1.8 2002/04/21 15:23:03 carl
  1804. + makeregsize
  1805. + changeregsize is now a local routine
  1806. Revision 1.7 2002/04/20 21:32:25 carl
  1807. + generic FPC_CHECKPOINTER
  1808. + first parameter offset in stack now portable
  1809. * rename some constants
  1810. + move some cpu stuff to other units
  1811. - remove unused constents
  1812. * fix stacksize for some targets
  1813. * fix generic size problems which depend now on EXTEND_SIZE constant
  1814. Revision 1.6 2002/04/15 19:03:31 carl
  1815. + reg2str -> std_reg2str()
  1816. Revision 1.5 2002/04/06 18:13:01 jonas
  1817. * several powerpc-related additions and fixes
  1818. Revision 1.4 2002/04/04 19:06:04 peter
  1819. * removed unused units
  1820. * use tlocation.size in cg.a_*loc*() routines
  1821. Revision 1.3 2002/04/02 17:11:29 peter
  1822. * tlocation,treference update
  1823. * LOC_CONSTANT added for better constant handling
  1824. * secondadd splitted in multiple routines
  1825. * location_force_reg added for loading a location to a register
  1826. of a specified size
  1827. * secondassignment parses now first the right and then the left node
  1828. (this is compatible with Kylix). This saves a lot of push/pop especially
  1829. with string operations
  1830. * adapted some routines to use the new cg methods
  1831. Revision 1.2 2002/04/01 19:24:25 jonas
  1832. * fixed different parameter name in interface and implementation
  1833. declaration of a method (only 1.0.x detected this)
  1834. Revision 1.1 2002/03/31 20:26:36 jonas
  1835. + a_loadfpu_* and a_loadmm_* methods in tcg
  1836. * register allocation is now handled by a class and is mostly processor
  1837. independent (+rgobj.pas and i386/rgcpu.pas)
  1838. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  1839. * some small improvements and fixes to the optimizer
  1840. * some register allocation fixes
  1841. * some fpuvaroffset fixes in the unary minus node
  1842. * push/popusedregisters is now called rg.save/restoreusedregisters and
  1843. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  1844. also better optimizable)
  1845. * fixed and optimized register saving/restoring for new/dispose nodes
  1846. * LOC_FPU locations now also require their "register" field to be set to
  1847. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  1848. - list field removed of the tnode class because it's not used currently
  1849. and can cause hard-to-find bugs
  1850. }