rgobj.pas 71 KB

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