rgobj.pas 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the base class for the register allocator
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {$i fpcdefs.inc}
  18. { Allow duplicate allocations, can be used to get the .s file written }
  19. { $define ALLOWDUPREG}
  20. unit rgobj;
  21. interface
  22. uses
  23. cutils, cpubase,
  24. aasmbase,aasmtai,aasmcpu,
  25. cclasses,globtype,cgbase,cgutils,
  26. cpuinfo
  27. ;
  28. type
  29. {
  30. The interference bitmap contains of 2 layers:
  31. layer 1 - 256*256 blocks with pointers to layer 2 blocks
  32. layer 2 - blocks of 32*256 (32 bytes = 256 bits)
  33. }
  34. Tinterferencebitmap2 = array[byte] of set of byte;
  35. Pinterferencebitmap2 = ^Tinterferencebitmap2;
  36. Tinterferencebitmap1 = array[byte] of Pinterferencebitmap2;
  37. pinterferencebitmap1 = ^tinterferencebitmap1;
  38. Tinterferencebitmap=class
  39. private
  40. maxx1,
  41. maxy1 : byte;
  42. fbitmap : pinterferencebitmap1;
  43. function getbitmap(x,y:tsuperregister):boolean;
  44. procedure setbitmap(x,y:tsuperregister;b:boolean);
  45. public
  46. constructor create;
  47. destructor destroy;override;
  48. property bitmap[x,y:tsuperregister]:boolean read getbitmap write setbitmap;default;
  49. end;
  50. Tmovelistheader=record
  51. count,
  52. maxcount,
  53. sorted_until : cardinal;
  54. end;
  55. Tmovelist=record
  56. header : Tmovelistheader;
  57. data : array[tsuperregister] of Tlinkedlistitem;
  58. end;
  59. Pmovelist=^Tmovelist;
  60. {In the register allocator we keep track of move instructions.
  61. These instructions are moved between five linked lists. There
  62. is also a linked list per register to keep track about the moves
  63. it is associated with. Because we need to determine quickly in
  64. which of the five lists it is we add anu enumeradtion to each
  65. move instruction.}
  66. Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
  67. ms_worklist_moves,ms_active_moves);
  68. Tmoveins=class(Tlinkedlistitem)
  69. moveset:Tmoveset;
  70. x,y:Tsuperregister;
  71. end;
  72. Treginfoflag=(ri_coalesced,ri_selected);
  73. Treginfoflagset=set of Treginfoflag;
  74. Treginfo=record
  75. live_start,
  76. live_end : Tai;
  77. subreg : tsubregister;
  78. alias : Tsuperregister;
  79. { The register allocator assigns each register a colour }
  80. colour : Tsuperregister;
  81. movelist : Pmovelist;
  82. adjlist : Psuperregisterworklist;
  83. degree : TSuperregister;
  84. flags : Treginfoflagset;
  85. end;
  86. Preginfo=^TReginfo;
  87. tspillreginfo = record
  88. spillreg : tregister;
  89. orgreg : tsuperregister;
  90. tempreg : tregister;
  91. regread,regwritten, mustbespilled: boolean;
  92. end;
  93. tspillregsinfo = array[0..2] of tspillreginfo;
  94. {#------------------------------------------------------------------
  95. This class implements the default register allocator. It is used by the
  96. code generator to allocate and free registers which might be valid
  97. across nodes. It also contains utility routines related to registers.
  98. Some of the methods in this class should be overriden
  99. by cpu-specific implementations.
  100. --------------------------------------------------------------------}
  101. trgobj=class
  102. extend_live_range_backwards: boolean;
  103. preserved_by_proc : tcpuregisterset;
  104. used_in_proc : tcpuregisterset;
  105. constructor create(Aregtype:Tregistertype;
  106. Adefaultsub:Tsubregister;
  107. const Ausable:array of tsuperregister;
  108. Afirst_imaginary:Tsuperregister;
  109. Apreserved_by_proc:Tcpuregisterset);
  110. destructor destroy;override;
  111. {# Allocate a register. An internalerror will be generated if there is
  112. no more free registers which can be allocated.}
  113. function getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;virtual;
  114. {# Get the register specified.}
  115. procedure getcpuregister(list:Taasmoutput;r:Tregister);virtual;
  116. procedure ungetcpuregister(list:Taasmoutput;r:Tregister);virtual;
  117. {# Get multiple registers specified.}
  118. procedure alloccpuregisters(list:Taasmoutput;const r:Tcpuregisterset);virtual;
  119. {# Free multiple registers specified.}
  120. procedure dealloccpuregisters(list:Taasmoutput;const r:Tcpuregisterset);virtual;
  121. function uses_registers:boolean;virtual;
  122. procedure add_reg_instruction(instr:Tai;r:tregister);
  123. procedure add_move_instruction(instr:Taicpu);
  124. {# Do the register allocation.}
  125. procedure do_register_allocation(list:Taasmoutput;headertai:tai);virtual;
  126. { Adds an interference edge.
  127. don't move this to the protected section, the arm cg requires to access this (FK) }
  128. procedure add_edge(u,v:Tsuperregister);
  129. protected
  130. regtype : Tregistertype;
  131. { default subregister used }
  132. defaultsub : tsubregister;
  133. live_registers:Tsuperregisterworklist;
  134. { can be overriden to add cpu specific interferences }
  135. procedure add_cpu_interferences(p : tai);virtual;
  136. procedure add_constraints(reg:Tregister);virtual;
  137. function getregisterinline(list:Taasmoutput;subreg:Tsubregister):Tregister;
  138. procedure ungetregisterinline(list:Taasmoutput;r:Tregister);
  139. function get_spill_subreg(r : tregister) : tsubregister;virtual;
  140. function do_spill_replace(list:Taasmoutput;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;virtual;
  141. procedure do_spill_read(list:Taasmoutput;pos:tai;const spilltemp:treference;tempreg:tregister);virtual;
  142. procedure do_spill_written(list:Taasmoutput;pos:tai;const spilltemp:treference;tempreg:tregister);virtual;
  143. function instr_spill_register(list:Taasmoutput;
  144. instr:taicpu;
  145. const r:Tsuperregisterset;
  146. const spilltemplist:Tspill_temp_list): boolean;virtual;
  147. private
  148. {# First imaginary register.}
  149. first_imaginary : Tsuperregister;
  150. {# Highest register allocated until now.}
  151. reginfo : PReginfo;
  152. maxreginfo,
  153. maxreginfoinc,
  154. maxreg : Tsuperregister;
  155. usable_registers_cnt : word;
  156. usable_registers : array[0..maxcpuregister-1] of tsuperregister;
  157. ibitmap : Tinterferencebitmap;
  158. spillednodes,
  159. simplifyworklist,
  160. freezeworklist,
  161. spillworklist,
  162. coalescednodes,
  163. selectstack : tsuperregisterworklist;
  164. worklist_moves,
  165. active_moves,
  166. frozen_moves,
  167. coalesced_moves,
  168. constrained_moves : Tlinkedlist;
  169. {$ifdef EXTDEBUG}
  170. procedure writegraph(loopidx:longint);
  171. {$endif EXTDEBUG}
  172. {# Disposes of the reginfo array.}
  173. procedure dispose_reginfo;
  174. {# Prepare the register colouring.}
  175. procedure prepare_colouring;
  176. {# Clean up after register colouring.}
  177. procedure epilogue_colouring;
  178. {# Colour the registers; that is do the register allocation.}
  179. procedure colour_registers;
  180. procedure insert_regalloc_info(list:Taasmoutput;u:tsuperregister);
  181. procedure insert_regalloc_info_all(list:Taasmoutput);
  182. procedure generate_interference_graph(list:Taasmoutput;headertai:tai);
  183. procedure translate_registers(list:Taasmoutput);
  184. function spill_registers(list:Taasmoutput;headertai:tai):boolean;virtual;
  185. function getnewreg(subreg:tsubregister):tsuperregister;
  186. procedure add_edges_used(u:Tsuperregister);
  187. procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  188. function move_related(n:Tsuperregister):boolean;
  189. procedure make_work_list;
  190. procedure sort_simplify_worklist;
  191. procedure enable_moves(n:Tsuperregister);
  192. procedure decrement_degree(m:Tsuperregister);
  193. procedure simplify;
  194. function get_alias(n:Tsuperregister):Tsuperregister;
  195. procedure add_worklist(u:Tsuperregister);
  196. function adjacent_ok(u,v:Tsuperregister):boolean;
  197. function conservative(u,v:Tsuperregister):boolean;
  198. procedure combine(u,v:Tsuperregister);
  199. procedure coalesce;
  200. procedure freeze_moves(u:Tsuperregister);
  201. procedure freeze;
  202. procedure select_spill;
  203. procedure assign_colours;
  204. procedure clear_interferences(u:Tsuperregister);
  205. end;
  206. const
  207. first_reg = 0;
  208. last_reg = high(tsuperregister)-1;
  209. maxspillingcounter = 20;
  210. implementation
  211. uses
  212. systems,
  213. globals,verbose,tgobj,procinfo;
  214. procedure sort_movelist(ml:Pmovelist);
  215. {Ok, sorting pointers is silly, but it does the job to make Trgobj.combine
  216. faster.}
  217. var h,i,p:word;
  218. t:Tlinkedlistitem;
  219. begin
  220. with ml^ do
  221. begin
  222. if header.count<2 then
  223. exit;
  224. p:=1;
  225. while 2*p<header.count do
  226. p:=2*p;
  227. while p<>0 do
  228. begin
  229. for h:=p to header.count-1 do
  230. begin
  231. i:=h;
  232. t:=data[i];
  233. repeat
  234. if ptrint(data[i-p])<=ptrint(t) then
  235. break;
  236. data[i]:=data[i-p];
  237. dec(i,p);
  238. until i<p;
  239. data[i]:=t;
  240. end;
  241. p:=p shr 1;
  242. end;
  243. header.sorted_until:=header.count-1;
  244. end;
  245. end;
  246. {******************************************************************************
  247. tinterferencebitmap
  248. ******************************************************************************}
  249. constructor tinterferencebitmap.create;
  250. begin
  251. inherited create;
  252. maxx1:=1;
  253. getmem(fbitmap,sizeof(tinterferencebitmap1)*2);
  254. fillchar(fbitmap^,sizeof(tinterferencebitmap1)*2,0);
  255. end;
  256. destructor tinterferencebitmap.destroy;
  257. var i,j:byte;
  258. begin
  259. for i:=0 to maxx1 do
  260. for j:=0 to maxy1 do
  261. if assigned(fbitmap[i,j]) then
  262. dispose(fbitmap[i,j]);
  263. freemem(fbitmap);
  264. end;
  265. function tinterferencebitmap.getbitmap(x,y:tsuperregister):boolean;
  266. var
  267. page : pinterferencebitmap2;
  268. begin
  269. result:=false;
  270. if (x shr 8>maxx1) then
  271. exit;
  272. page:=fbitmap[x shr 8,y shr 8];
  273. result:=assigned(page) and
  274. ((x and $ff) in page^[y and $ff]);
  275. end;
  276. procedure tinterferencebitmap.setbitmap(x,y:tsuperregister;b:boolean);
  277. var
  278. x1,y1 : byte;
  279. begin
  280. x1:=x shr 8;
  281. y1:=y shr 8;
  282. if x1>maxx1 then
  283. begin
  284. reallocmem(fbitmap,sizeof(tinterferencebitmap1)*(x1+1));
  285. fillchar(fbitmap[maxx1+1],sizeof(tinterferencebitmap1)*(x1-maxx1),0);
  286. maxx1:=x1;
  287. end;
  288. if not assigned(fbitmap[x1,y1]) then
  289. begin
  290. if y1>maxy1 then
  291. maxy1:=y1;
  292. new(fbitmap[x1,y1]);
  293. fillchar(fbitmap[x1,y1]^,sizeof(tinterferencebitmap2),0);
  294. end;
  295. if b then
  296. include(fbitmap[x1,y1]^[y and $ff],(x and $ff))
  297. else
  298. exclude(fbitmap[x1,y1]^[y and $ff],(x and $ff));
  299. end;
  300. {******************************************************************************
  301. trgobj
  302. ******************************************************************************}
  303. constructor trgobj.create(Aregtype:Tregistertype;
  304. Adefaultsub:Tsubregister;
  305. const Ausable:array of tsuperregister;
  306. Afirst_imaginary:Tsuperregister;
  307. Apreserved_by_proc:Tcpuregisterset);
  308. var
  309. i : Tsuperregister;
  310. begin
  311. { empty super register sets can cause very strange problems }
  312. if high(Ausable)=0 then
  313. internalerror(200210181);
  314. extend_live_range_backwards := false;
  315. first_imaginary:=Afirst_imaginary;
  316. maxreg:=Afirst_imaginary;
  317. regtype:=Aregtype;
  318. defaultsub:=Adefaultsub;
  319. preserved_by_proc:=Apreserved_by_proc;
  320. used_in_proc:=[];
  321. live_registers.init;
  322. { Get reginfo for CPU registers }
  323. maxreginfo:=first_imaginary;
  324. maxreginfoinc:=16;
  325. worklist_moves:=Tlinkedlist.create;
  326. reginfo:=allocmem(first_imaginary*sizeof(treginfo));
  327. for i:=0 to first_imaginary-1 do
  328. begin
  329. reginfo[i].degree:=high(tsuperregister);
  330. reginfo[i].alias:=RS_INVALID;
  331. end;
  332. { Usable registers }
  333. fillchar(usable_registers,sizeof(usable_registers),0);
  334. for i:=low(Ausable) to high(Ausable) do
  335. usable_registers[i]:=Ausable[i];
  336. usable_registers_cnt:=high(Ausable)+1;
  337. { Initialize Worklists }
  338. spillednodes.init;
  339. simplifyworklist.init;
  340. freezeworklist.init;
  341. spillworklist.init;
  342. coalescednodes.init;
  343. selectstack.init;
  344. end;
  345. destructor trgobj.destroy;
  346. begin
  347. spillednodes.done;
  348. simplifyworklist.done;
  349. freezeworklist.done;
  350. spillworklist.done;
  351. coalescednodes.done;
  352. selectstack.done;
  353. live_registers.done;
  354. worklist_moves.free;
  355. dispose_reginfo;
  356. end;
  357. procedure Trgobj.dispose_reginfo;
  358. var i:Tsuperregister;
  359. begin
  360. if reginfo<>nil then
  361. begin
  362. for i:=0 to maxreg-1 do
  363. with reginfo[i] do
  364. begin
  365. if adjlist<>nil then
  366. dispose(adjlist,done);
  367. if movelist<>nil then
  368. dispose(movelist);
  369. end;
  370. freemem(reginfo);
  371. reginfo:=nil;
  372. end;
  373. end;
  374. function trgobj.getnewreg(subreg:tsubregister):tsuperregister;
  375. var
  376. oldmaxreginfo : tsuperregister;
  377. begin
  378. result:=maxreg;
  379. inc(maxreg);
  380. if maxreg>=last_reg then
  381. Message(parser_f_too_complex_proc);
  382. if maxreg>=maxreginfo then
  383. begin
  384. oldmaxreginfo:=maxreginfo;
  385. { Prevent overflow }
  386. if maxreginfoinc>last_reg-maxreginfo then
  387. maxreginfo:=last_reg
  388. else
  389. begin
  390. inc(maxreginfo,maxreginfoinc);
  391. if maxreginfoinc<256 then
  392. maxreginfoinc:=maxreginfoinc*2;
  393. end;
  394. reallocmem(reginfo,maxreginfo*sizeof(treginfo));
  395. { Do we really need it to clear it ? At least for 1.0.x (PFV) }
  396. fillchar(reginfo[oldmaxreginfo],(maxreginfo-oldmaxreginfo)*sizeof(treginfo),0);
  397. end;
  398. reginfo[result].subreg:=subreg;
  399. end;
  400. function trgobj.getregister(list:Taasmoutput;subreg:Tsubregister):Tregister;
  401. begin
  402. {$ifdef EXTDEBUG}
  403. if reginfo=nil then
  404. InternalError(2004020901);
  405. {$endif EXTDEBUG}
  406. if defaultsub=R_SUBNONE then
  407. result:=newreg(regtype,getnewreg(R_SUBNONE),R_SUBNONE)
  408. else
  409. result:=newreg(regtype,getnewreg(subreg),subreg);
  410. end;
  411. function trgobj.uses_registers:boolean;
  412. begin
  413. result:=(maxreg>first_imaginary);
  414. end;
  415. procedure trgobj.ungetcpuregister(list:Taasmoutput;r:Tregister);
  416. begin
  417. if (getsupreg(r)>=first_imaginary) then
  418. InternalError(2004020901);
  419. list.concat(Tai_regalloc.dealloc(r,nil));
  420. end;
  421. procedure trgobj.getcpuregister(list:Taasmoutput;r:Tregister);
  422. var
  423. supreg:Tsuperregister;
  424. begin
  425. supreg:=getsupreg(r);
  426. if supreg>=first_imaginary then
  427. internalerror(2003121503);
  428. include(used_in_proc,supreg);
  429. list.concat(Tai_regalloc.alloc(r,nil));
  430. end;
  431. procedure trgobj.alloccpuregisters(list:Taasmoutput;const r:Tcpuregisterset);
  432. var i:Tsuperregister;
  433. begin
  434. for i:=0 to first_imaginary-1 do
  435. if i in r then
  436. getcpuregister(list,newreg(regtype,i,defaultsub));
  437. end;
  438. procedure trgobj.dealloccpuregisters(list:Taasmoutput;const r:Tcpuregisterset);
  439. var i:Tsuperregister;
  440. begin
  441. for i:=0 to first_imaginary-1 do
  442. if i in r then
  443. ungetcpuregister(list,newreg(regtype,i,defaultsub));
  444. end;
  445. procedure trgobj.do_register_allocation(list:Taasmoutput;headertai:tai);
  446. var
  447. spillingcounter:byte;
  448. endspill:boolean;
  449. begin
  450. { Insert regalloc info for imaginary registers }
  451. insert_regalloc_info_all(list);
  452. ibitmap:=tinterferencebitmap.create;
  453. generate_interference_graph(list,headertai);
  454. { Don't do the real allocation when -sr is passed }
  455. if (cs_no_regalloc in aktglobalswitches) then
  456. exit;
  457. {Do register allocation.}
  458. spillingcounter:=0;
  459. repeat
  460. prepare_colouring;
  461. colour_registers;
  462. epilogue_colouring;
  463. endspill:=true;
  464. if spillednodes.length<>0 then
  465. begin
  466. inc(spillingcounter);
  467. if spillingcounter>maxspillingcounter then
  468. begin
  469. {$ifdef EXTDEBUG}
  470. { Only exit here so the .s file is still generated. Assembling
  471. the file will still trigger an error }
  472. exit;
  473. {$else}
  474. internalerror(200309041);
  475. {$endif}
  476. end;
  477. endspill:=not spill_registers(list,headertai);
  478. end;
  479. until endspill;
  480. ibitmap.free;
  481. translate_registers(list);
  482. dispose_reginfo;
  483. end;
  484. procedure trgobj.add_constraints(reg:Tregister);
  485. begin
  486. end;
  487. procedure trgobj.add_edge(u,v:Tsuperregister);
  488. {This procedure will add an edge to the virtual interference graph.}
  489. procedure addadj(u,v:Tsuperregister);
  490. begin
  491. with reginfo[u] do
  492. begin
  493. if adjlist=nil then
  494. new(adjlist,init);
  495. adjlist^.add(v);
  496. end;
  497. end;
  498. begin
  499. if (u<>v) and not(ibitmap[v,u]) then
  500. begin
  501. ibitmap[v,u]:=true;
  502. ibitmap[u,v]:=true;
  503. {Precoloured nodes are not stored in the interference graph.}
  504. if (u>=first_imaginary) then
  505. addadj(u,v);
  506. if (v>=first_imaginary) then
  507. addadj(v,u);
  508. end;
  509. end;
  510. procedure trgobj.add_edges_used(u:Tsuperregister);
  511. var i:word;
  512. begin
  513. with live_registers do
  514. if length>0 then
  515. for i:=0 to length-1 do
  516. add_edge(u,get_alias(buf^[i]));
  517. end;
  518. {$ifdef EXTDEBUG}
  519. procedure trgobj.writegraph(loopidx:longint);
  520. {This procedure writes out the current interference graph in the
  521. register allocator.}
  522. var f:text;
  523. i,j:Tsuperregister;
  524. begin
  525. assign(f,'igraph'+tostr(loopidx));
  526. rewrite(f);
  527. writeln(f,'Interference graph');
  528. writeln(f);
  529. write(f,' ');
  530. for i:=0 to 15 do
  531. for j:=0 to 15 do
  532. write(f,hexstr(i,1));
  533. writeln(f);
  534. write(f,' ');
  535. for i:=0 to 15 do
  536. write(f,'0123456789ABCDEF');
  537. writeln(f);
  538. for i:=0 to maxreg-1 do
  539. begin
  540. write(f,hexstr(i,2):4);
  541. for j:=0 to maxreg-1 do
  542. if ibitmap[i,j] then
  543. write(f,'*')
  544. else
  545. write(f,'-');
  546. writeln(f);
  547. end;
  548. close(f);
  549. end;
  550. {$endif EXTDEBUG}
  551. procedure trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
  552. begin
  553. with reginfo[u] do
  554. begin
  555. if movelist=nil then
  556. begin
  557. getmem(movelist,sizeof(tmovelistheader)+60*sizeof(pointer));
  558. movelist^.header.maxcount:=60;
  559. movelist^.header.count:=0;
  560. movelist^.header.sorted_until:=0;
  561. end
  562. else
  563. begin
  564. if movelist^.header.count>=movelist^.header.maxcount then
  565. begin
  566. movelist^.header.maxcount:=movelist^.header.maxcount*2;
  567. reallocmem(movelist,sizeof(tmovelistheader)+movelist^.header.maxcount*sizeof(pointer));
  568. end;
  569. end;
  570. movelist^.data[movelist^.header.count]:=data;
  571. inc(movelist^.header.count);
  572. end;
  573. end;
  574. procedure trgobj.add_reg_instruction(instr:Tai;r:tregister);
  575. var
  576. supreg : tsuperregister;
  577. begin
  578. supreg:=getsupreg(r);
  579. {$ifdef extdebug}
  580. if supreg>=maxreginfo then
  581. internalerror(200411061);
  582. {$endif extdebug}
  583. if supreg>=first_imaginary then
  584. with reginfo[supreg] do
  585. begin
  586. if not(extend_live_range_backwards) then
  587. begin
  588. if not assigned(live_start) then
  589. live_start:=instr;
  590. live_end:=instr;
  591. end
  592. else
  593. begin
  594. live_start := instr;
  595. if not assigned(live_end) then
  596. live_end := instr;
  597. end
  598. end;
  599. end;
  600. procedure trgobj.add_move_instruction(instr:Taicpu);
  601. {This procedure notifies a certain as a move instruction so the
  602. register allocator can try to eliminate it.}
  603. var i:Tmoveins;
  604. ssupreg,dsupreg:Tsuperregister;
  605. begin
  606. {$ifdef extdebug}
  607. if (instr.oper[O_MOV_SOURCE]^.typ<>top_reg) or
  608. (instr.oper[O_MOV_DEST]^.typ<>top_reg) then
  609. internalerror(200311291);
  610. {$endif}
  611. i:=Tmoveins.create;
  612. i.moveset:=ms_worklist_moves;
  613. worklist_moves.insert(i);
  614. ssupreg:=getsupreg(instr.oper[O_MOV_SOURCE]^.reg);
  615. add_to_movelist(ssupreg,i);
  616. dsupreg:=getsupreg(instr.oper[O_MOV_DEST]^.reg);
  617. if ssupreg<>dsupreg then
  618. {Avoid adding the same move instruction twice to a single register.}
  619. add_to_movelist(dsupreg,i);
  620. i.x:=ssupreg;
  621. i.y:=dsupreg;
  622. end;
  623. function trgobj.move_related(n:Tsuperregister):boolean;
  624. var i:cardinal;
  625. begin
  626. move_related:=false;
  627. with reginfo[n] do
  628. if movelist<>nil then
  629. with movelist^ do
  630. for i:=0 to header.count-1 do
  631. if Tmoveins(data[i]).moveset in [ms_worklist_moves,ms_active_moves] then
  632. begin
  633. move_related:=true;
  634. break;
  635. end;
  636. end;
  637. procedure Trgobj.sort_simplify_worklist;
  638. {Sorts the simplifyworklist by the number of interferences the
  639. registers in it cause. This allows simplify to execute in
  640. constant time.}
  641. var p,h,i,leni,lent:word;
  642. t:Tsuperregister;
  643. adji,adjt:Psuperregisterworklist;
  644. begin
  645. with simplifyworklist do
  646. begin
  647. if length<2 then
  648. exit;
  649. p:=1;
  650. while 2*p<length do
  651. p:=2*p;
  652. while p<>0 do
  653. begin
  654. for h:=p to length-1 do
  655. begin
  656. i:=h;
  657. t:=buf^[i];
  658. adjt:=reginfo[buf^[i]].adjlist;
  659. lent:=0;
  660. if adjt<>nil then
  661. lent:=adjt^.length;
  662. repeat
  663. adji:=reginfo[buf^[i-p]].adjlist;
  664. leni:=0;
  665. if adji<>nil then
  666. leni:=adji^.length;
  667. if leni<=lent then
  668. break;
  669. buf^[i]:=buf^[i-p];
  670. dec(i,p)
  671. until i<p;
  672. buf^[i]:=t;
  673. end;
  674. p:=p shr 1;
  675. end;
  676. end;
  677. end;
  678. procedure trgobj.make_work_list;
  679. var n:Tsuperregister;
  680. begin
  681. {If we have 7 cpu registers, and the degree of a node is 7, we cannot
  682. assign it to any of the registers, thus it is significant.}
  683. for n:=first_imaginary to maxreg-1 do
  684. with reginfo[n] do
  685. begin
  686. if adjlist=nil then
  687. degree:=0
  688. else
  689. degree:=adjlist^.length;
  690. if degree>=usable_registers_cnt then
  691. spillworklist.add(n)
  692. else if move_related(n) then
  693. freezeworklist.add(n)
  694. else
  695. simplifyworklist.add(n);
  696. end;
  697. sort_simplify_worklist;
  698. end;
  699. procedure trgobj.prepare_colouring;
  700. begin
  701. make_work_list;
  702. active_moves:=Tlinkedlist.create;
  703. frozen_moves:=Tlinkedlist.create;
  704. coalesced_moves:=Tlinkedlist.create;
  705. constrained_moves:=Tlinkedlist.create;
  706. selectstack.clear;
  707. end;
  708. procedure trgobj.enable_moves(n:Tsuperregister);
  709. var m:Tlinkedlistitem;
  710. i:cardinal;
  711. begin
  712. with reginfo[n] do
  713. if movelist<>nil then
  714. for i:=0 to movelist^.header.count-1 do
  715. begin
  716. m:=movelist^.data[i];
  717. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  718. if Tmoveins(m).moveset=ms_active_moves then
  719. begin
  720. {Move m from the set active_moves to the set worklist_moves.}
  721. active_moves.remove(m);
  722. Tmoveins(m).moveset:=ms_worklist_moves;
  723. worklist_moves.concat(m);
  724. end;
  725. end;
  726. end;
  727. procedure Trgobj.decrement_degree(m:Tsuperregister);
  728. var adj : Psuperregisterworklist;
  729. n : tsuperregister;
  730. d,i : word;
  731. begin
  732. with reginfo[m] do
  733. begin
  734. d:=degree;
  735. if d=0 then
  736. internalerror(200312151);
  737. dec(degree);
  738. if d=usable_registers_cnt then
  739. begin
  740. {Enable moves for m.}
  741. enable_moves(m);
  742. {Enable moves for adjacent.}
  743. adj:=adjlist;
  744. if adj<>nil then
  745. for i:=1 to adj^.length do
  746. begin
  747. n:=adj^.buf^[i-1];
  748. if reginfo[n].flags*[ri_selected,ri_coalesced]<>[] then
  749. enable_moves(n);
  750. end;
  751. {Remove the node from the spillworklist.}
  752. if not spillworklist.delete(m) then
  753. internalerror(200310145);
  754. if move_related(m) then
  755. freezeworklist.add(m)
  756. else
  757. simplifyworklist.add(m);
  758. end;
  759. end;
  760. end;
  761. procedure trgobj.simplify;
  762. var adj : Psuperregisterworklist;
  763. m,n : Tsuperregister;
  764. i : word;
  765. begin
  766. {We take the element with the least interferences out of the
  767. simplifyworklist. Since the simplifyworklist is now sorted, we
  768. no longer need to search, but we can simply take the first element.}
  769. m:=simplifyworklist.get;
  770. {Push it on the selectstack.}
  771. selectstack.add(m);
  772. with reginfo[m] do
  773. begin
  774. include(flags,ri_selected);
  775. adj:=adjlist;
  776. end;
  777. if adj<>nil then
  778. for i:=1 to adj^.length do
  779. begin
  780. n:=adj^.buf^[i-1];
  781. if (n>=first_imaginary) and
  782. (reginfo[n].flags*[ri_selected,ri_coalesced]=[]) then
  783. decrement_degree(n);
  784. end;
  785. end;
  786. function trgobj.get_alias(n:Tsuperregister):Tsuperregister;
  787. begin
  788. while ri_coalesced in reginfo[n].flags do
  789. n:=reginfo[n].alias;
  790. get_alias:=n;
  791. end;
  792. procedure trgobj.add_worklist(u:Tsuperregister);
  793. begin
  794. if (u>=first_imaginary) and
  795. (not move_related(u)) and
  796. (reginfo[u].degree<usable_registers_cnt) then
  797. begin
  798. if not freezeworklist.delete(u) then
  799. internalerror(200308161); {must be found}
  800. simplifyworklist.add(u);
  801. end;
  802. end;
  803. function trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
  804. {Check wether u and v should be coalesced. u is precoloured.}
  805. function ok(t,r:Tsuperregister):boolean;
  806. begin
  807. ok:=(t<first_imaginary) or
  808. (reginfo[t].degree<usable_registers_cnt) or
  809. ibitmap[r,t];
  810. end;
  811. var adj : Psuperregisterworklist;
  812. i : word;
  813. n : tsuperregister;
  814. begin
  815. with reginfo[v] do
  816. begin
  817. adjacent_ok:=true;
  818. adj:=adjlist;
  819. if adj<>nil then
  820. for i:=1 to adj^.length do
  821. begin
  822. n:=adj^.buf^[i-1];
  823. if (flags*[ri_coalesced,ri_selected]=[]) and not ok(n,u) then
  824. begin
  825. adjacent_ok:=false;
  826. break;
  827. end;
  828. end;
  829. end;
  830. end;
  831. function trgobj.conservative(u,v:Tsuperregister):boolean;
  832. var adj : Psuperregisterworklist;
  833. done : Tsuperregisterset; {To prevent that we count nodes twice.}
  834. i,k:word;
  835. n : tsuperregister;
  836. begin
  837. k:=0;
  838. supregset_reset(done,false,maxreg);
  839. with reginfo[u] do
  840. begin
  841. adj:=adjlist;
  842. if adj<>nil then
  843. for i:=1 to adj^.length do
  844. begin
  845. n:=adj^.buf^[i-1];
  846. if flags*[ri_coalesced,ri_selected]=[] then
  847. begin
  848. supregset_include(done,n);
  849. if reginfo[n].degree>=usable_registers_cnt then
  850. inc(k);
  851. end;
  852. end;
  853. end;
  854. adj:=reginfo[v].adjlist;
  855. if adj<>nil then
  856. for i:=1 to adj^.length do
  857. begin
  858. n:=adj^.buf^[i-1];
  859. if not supregset_in(done,n) and
  860. (reginfo[n].degree>=usable_registers_cnt) and
  861. (reginfo[u].flags*[ri_coalesced,ri_selected]=[]) then
  862. inc(k);
  863. end;
  864. conservative:=(k<usable_registers_cnt);
  865. end;
  866. procedure trgobj.combine(u,v:Tsuperregister);
  867. var adj : Psuperregisterworklist;
  868. i,n,p,q:cardinal;
  869. t : tsuperregister;
  870. searched:Tlinkedlistitem;
  871. label l1;
  872. begin
  873. if not freezeworklist.delete(v) then
  874. spillworklist.delete(v);
  875. coalescednodes.add(v);
  876. include(reginfo[v].flags,ri_coalesced);
  877. reginfo[v].alias:=u;
  878. {Combine both movelists. Since the movelists are sets, only add
  879. elements that are not already present. The movelists cannot be
  880. empty by definition; nodes are only coalesced if there is a move
  881. between them. To prevent quadratic time blowup (movelists of
  882. especially machine registers can get very large because of moves
  883. generated during calls) we need to go into disgusting complexity.
  884. (See webtbs/tw2242 for an example that stresses this.)
  885. We want to sort the movelist to be able to search logarithmically.
  886. Unfortunately, sorting the movelist every time before searching
  887. is counter-productive, since the movelist usually grows with a few
  888. items at a time. Therefore, we split the movelist into a sorted
  889. and an unsorted part and search through both. If the unsorted part
  890. becomes too large, we sort.}
  891. if assigned(reginfo[u].movelist) then
  892. begin
  893. {We have to weigh the cost of sorting the list against searching
  894. the cost of the unsorted part. I use factor of 8 here; if the
  895. number of items is less than 8 times the numer of unsorted items,
  896. we'll sort the list.}
  897. with reginfo[u].movelist^ do
  898. if header.count<8*(header.count-header.sorted_until) then
  899. sort_movelist(reginfo[u].movelist);
  900. if assigned(reginfo[v].movelist) then
  901. begin
  902. for n:=0 to reginfo[v].movelist^.header.count-1 do
  903. begin
  904. {Binary search the sorted part of the list.}
  905. searched:=reginfo[v].movelist^.data[n];
  906. p:=0;
  907. q:=reginfo[u].movelist^.header.sorted_until;
  908. i:=0;
  909. if q<>0 then
  910. repeat
  911. i:=(p+q) shr 1;
  912. if ptrint(searched)>ptrint(reginfo[u].movelist^.data[i]) then
  913. p:=i+1
  914. else
  915. q:=i;
  916. until p=q;
  917. with reginfo[u].movelist^ do
  918. if searched<>data[i] then
  919. begin
  920. {Linear search the unsorted part of the list.}
  921. for i:=header.sorted_until+1 to header.count-1 do
  922. if searched=data[i] then
  923. goto l1;
  924. {Not found -> add}
  925. add_to_movelist(u,searched);
  926. l1:
  927. end;
  928. end;
  929. end;
  930. end;
  931. enable_moves(v);
  932. adj:=reginfo[v].adjlist;
  933. if adj<>nil then
  934. for i:=1 to adj^.length do
  935. begin
  936. t:=adj^.buf^[i-1];
  937. with reginfo[t] do
  938. if not(ri_coalesced in flags) then
  939. begin
  940. {t has a connection to v. Since we are adding v to u, we
  941. need to connect t to u. However, beware if t was already
  942. connected to u...}
  943. if (ibitmap[t,u]) and not (ri_selected in flags) then
  944. {... because in that case, we are actually removing an edge
  945. and the degree of t decreases.}
  946. decrement_degree(t)
  947. else
  948. begin
  949. add_edge(t,u);
  950. {We have added an edge to t and u. So their degree increases.
  951. However, v is added to u. That means its neighbours will
  952. no longer point to v, but to u instead. Therefore, only the
  953. degree of u increases.}
  954. if (u>=first_imaginary) and not (ri_selected in flags) then
  955. inc(reginfo[u].degree);
  956. end;
  957. end;
  958. end;
  959. if (reginfo[u].degree>=usable_registers_cnt) and freezeworklist.delete(u) then
  960. spillworklist.add(u);
  961. end;
  962. procedure trgobj.coalesce;
  963. var m:Tmoveins;
  964. x,y,u,v:Tsuperregister;
  965. begin
  966. m:=Tmoveins(worklist_moves.getfirst);
  967. x:=get_alias(m.x);
  968. y:=get_alias(m.y);
  969. if (y<first_imaginary) then
  970. begin
  971. u:=y;
  972. v:=x;
  973. end
  974. else
  975. begin
  976. u:=x;
  977. v:=y;
  978. end;
  979. if (u=v) then
  980. begin
  981. m.moveset:=ms_coalesced_moves; {Already coalesced.}
  982. coalesced_moves.insert(m);
  983. add_worklist(u);
  984. end
  985. {Do u and v interfere? In that case the move is constrained. Two
  986. precoloured nodes interfere allways. If v is precoloured, by the above
  987. code u is precoloured, thus interference...}
  988. else if (v<first_imaginary) or ibitmap[u,v] then
  989. begin
  990. m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
  991. constrained_moves.insert(m);
  992. add_worklist(u);
  993. add_worklist(v);
  994. end
  995. {Next test: is it possible and a good idea to coalesce??}
  996. else if ((u<first_imaginary) and adjacent_ok(u,v)) or
  997. ((u>=first_imaginary) and conservative(u,v)) then
  998. begin
  999. m.moveset:=ms_coalesced_moves; {Move coalesced!}
  1000. coalesced_moves.insert(m);
  1001. combine(u,v);
  1002. add_worklist(u);
  1003. end
  1004. else
  1005. begin
  1006. m.moveset:=ms_active_moves;
  1007. active_moves.insert(m);
  1008. end;
  1009. end;
  1010. procedure trgobj.freeze_moves(u:Tsuperregister);
  1011. var i:cardinal;
  1012. m:Tlinkedlistitem;
  1013. v,x,y:Tsuperregister;
  1014. begin
  1015. if reginfo[u].movelist<>nil then
  1016. for i:=0 to reginfo[u].movelist^.header.count-1 do
  1017. begin
  1018. m:=reginfo[u].movelist^.data[i];
  1019. if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
  1020. begin
  1021. x:=Tmoveins(m).x;
  1022. y:=Tmoveins(m).y;
  1023. if get_alias(y)=get_alias(u) then
  1024. v:=get_alias(x)
  1025. else
  1026. v:=get_alias(y);
  1027. {Move m from active_moves/worklist_moves to frozen_moves.}
  1028. if Tmoveins(m).moveset=ms_active_moves then
  1029. active_moves.remove(m)
  1030. else
  1031. worklist_moves.remove(m);
  1032. Tmoveins(m).moveset:=ms_frozen_moves;
  1033. frozen_moves.insert(m);
  1034. if (v>=first_imaginary) and not(move_related(v)) and
  1035. (reginfo[v].degree<usable_registers_cnt) then
  1036. begin
  1037. freezeworklist.delete(v);
  1038. simplifyworklist.add(v);
  1039. end;
  1040. end;
  1041. end;
  1042. end;
  1043. procedure trgobj.freeze;
  1044. var n:Tsuperregister;
  1045. begin
  1046. { We need to take a random element out of the freezeworklist. We take
  1047. the last element. Dirty code! }
  1048. n:=freezeworklist.get;
  1049. {Add it to the simplifyworklist.}
  1050. simplifyworklist.add(n);
  1051. freeze_moves(n);
  1052. end;
  1053. procedure trgobj.select_spill;
  1054. var
  1055. n : tsuperregister;
  1056. adj : psuperregisterworklist;
  1057. max,p,i:word;
  1058. begin
  1059. { We must look for the element with the most interferences in the
  1060. spillworklist. This is required because those registers are creating
  1061. the most conflicts and keeping them in a register will not reduce the
  1062. complexity and even can cause the help registers for the spilling code
  1063. to get too much conflicts with the result that the spilling code
  1064. will never converge (PFV) }
  1065. max:=0;
  1066. p:=0;
  1067. with spillworklist do
  1068. begin
  1069. {Safe: This procedure is only called if length<>0}
  1070. for i:=0 to length-1 do
  1071. begin
  1072. adj:=reginfo[buf^[i]].adjlist;
  1073. if assigned(adj) and (adj^.length>max) then
  1074. begin
  1075. p:=i;
  1076. max:=adj^.length;
  1077. end;
  1078. end;
  1079. n:=buf^[p];
  1080. deleteidx(p);
  1081. end;
  1082. simplifyworklist.add(n);
  1083. freeze_moves(n);
  1084. end;
  1085. procedure trgobj.assign_colours;
  1086. {Assign_colours assigns the actual colours to the registers.}
  1087. var adj : Psuperregisterworklist;
  1088. i,j,k : word;
  1089. n,a,c : Tsuperregister;
  1090. colourednodes : Tsuperregisterset;
  1091. adj_colours:set of 0..255;
  1092. found : boolean;
  1093. begin
  1094. spillednodes.clear;
  1095. {Reset colours}
  1096. for n:=0 to maxreg-1 do
  1097. reginfo[n].colour:=n;
  1098. {Colour the cpu registers...}
  1099. supregset_reset(colourednodes,false,maxreg);
  1100. for n:=0 to first_imaginary-1 do
  1101. supregset_include(colourednodes,n);
  1102. {Now colour the imaginary registers on the select-stack.}
  1103. for i:=selectstack.length downto 1 do
  1104. begin
  1105. n:=selectstack.buf^[i-1];
  1106. {Create a list of colours that we cannot assign to n.}
  1107. adj_colours:=[];
  1108. adj:=reginfo[n].adjlist;
  1109. if adj<>nil then
  1110. for j:=0 to adj^.length-1 do
  1111. begin
  1112. a:=get_alias(adj^.buf^[j]);
  1113. if supregset_in(colourednodes,a) and (reginfo[a].colour<=255) then
  1114. include(adj_colours,reginfo[a].colour);
  1115. end;
  1116. if regtype=R_INTREGISTER then
  1117. include(adj_colours,RS_STACK_POINTER_REG);
  1118. {Assume a spill by default...}
  1119. found:=false;
  1120. {Search for a colour not in this list.}
  1121. for k:=0 to usable_registers_cnt-1 do
  1122. begin
  1123. c:=usable_registers[k];
  1124. if not(c in adj_colours) then
  1125. begin
  1126. reginfo[n].colour:=c;
  1127. found:=true;
  1128. supregset_include(colourednodes,n);
  1129. include(used_in_proc,c);
  1130. break;
  1131. end;
  1132. end;
  1133. if not found then
  1134. spillednodes.add(n);
  1135. end;
  1136. {Finally colour the nodes that were coalesced.}
  1137. for i:=1 to coalescednodes.length do
  1138. begin
  1139. n:=coalescednodes.buf^[i-1];
  1140. k:=get_alias(n);
  1141. reginfo[n].colour:=reginfo[k].colour;
  1142. if reginfo[k].colour<maxcpuregister then
  1143. include(used_in_proc,reginfo[k].colour);
  1144. end;
  1145. end;
  1146. procedure trgobj.colour_registers;
  1147. begin
  1148. repeat
  1149. if simplifyworklist.length<>0 then
  1150. simplify
  1151. else if not(worklist_moves.empty) then
  1152. coalesce
  1153. else if freezeworklist.length<>0 then
  1154. freeze
  1155. else if spillworklist.length<>0 then
  1156. select_spill;
  1157. until (simplifyworklist.length=0) and
  1158. worklist_moves.empty and
  1159. (freezeworklist.length=0) and
  1160. (spillworklist.length=0);
  1161. assign_colours;
  1162. end;
  1163. procedure trgobj.epilogue_colouring;
  1164. var
  1165. i : Tsuperregister;
  1166. begin
  1167. worklist_moves.clear;
  1168. active_moves.destroy;
  1169. active_moves:=nil;
  1170. frozen_moves.destroy;
  1171. frozen_moves:=nil;
  1172. coalesced_moves.destroy;
  1173. coalesced_moves:=nil;
  1174. constrained_moves.destroy;
  1175. constrained_moves:=nil;
  1176. for i:=0 to maxreg-1 do
  1177. with reginfo[i] do
  1178. if movelist<>nil then
  1179. begin
  1180. dispose(movelist);
  1181. movelist:=nil;
  1182. end;
  1183. end;
  1184. procedure trgobj.clear_interferences(u:Tsuperregister);
  1185. {Remove node u from the interference graph and remove all collected
  1186. move instructions it is associated with.}
  1187. var i : word;
  1188. v : Tsuperregister;
  1189. adj,adj2 : Psuperregisterworklist;
  1190. begin
  1191. adj:=reginfo[u].adjlist;
  1192. if adj<>nil then
  1193. begin
  1194. for i:=1 to adj^.length do
  1195. begin
  1196. v:=adj^.buf^[i-1];
  1197. {Remove (u,v) and (v,u) from bitmap.}
  1198. ibitmap[u,v]:=false;
  1199. ibitmap[v,u]:=false;
  1200. {Remove (v,u) from adjacency list.}
  1201. adj2:=reginfo[v].adjlist;
  1202. if adj2<>nil then
  1203. begin
  1204. adj2^.delete(u);
  1205. if adj2^.length=0 then
  1206. begin
  1207. dispose(adj2,done);
  1208. reginfo[v].adjlist:=nil;
  1209. end;
  1210. end;
  1211. end;
  1212. {Remove ( u,* ) from adjacency list.}
  1213. dispose(adj,done);
  1214. reginfo[u].adjlist:=nil;
  1215. end;
  1216. end;
  1217. function trgobj.getregisterinline(list:Taasmoutput;subreg:Tsubregister):Tregister;
  1218. var
  1219. p : Tsuperregister;
  1220. begin
  1221. p:=getnewreg(subreg);
  1222. live_registers.add(p);
  1223. result:=newreg(regtype,p,subreg);
  1224. add_edges_used(p);
  1225. add_constraints(result);
  1226. end;
  1227. procedure trgobj.ungetregisterinline(list:Taasmoutput;r:Tregister);
  1228. var
  1229. supreg:Tsuperregister;
  1230. begin
  1231. supreg:=getsupreg(r);
  1232. live_registers.delete(supreg);
  1233. insert_regalloc_info(list,supreg);
  1234. end;
  1235. procedure trgobj.insert_regalloc_info(list:Taasmoutput;u:tsuperregister);
  1236. var
  1237. p : tai;
  1238. r : tregister;
  1239. palloc,
  1240. pdealloc : tai_regalloc;
  1241. begin
  1242. { Insert regallocs for all imaginary registers }
  1243. with reginfo[u] do
  1244. begin
  1245. r:=newreg(regtype,u,subreg);
  1246. if assigned(live_start) then
  1247. begin
  1248. { Generate regalloc and bind it to an instruction, this
  1249. is needed to find all live registers belonging to an
  1250. instruction during the spilling }
  1251. if live_start.typ=ait_instruction then
  1252. palloc:=tai_regalloc.alloc(r,live_start)
  1253. else
  1254. palloc:=tai_regalloc.alloc(r,nil);
  1255. if live_end.typ=ait_instruction then
  1256. pdealloc:=tai_regalloc.dealloc(r,live_end)
  1257. else
  1258. pdealloc:=tai_regalloc.dealloc(r,nil);
  1259. { Insert live start allocation before the instruction/reg_a_sync }
  1260. list.insertbefore(palloc,live_start);
  1261. { Insert live end deallocation before reg allocations
  1262. to reduce conflicts }
  1263. p:=live_end;
  1264. while assigned(p) and
  1265. assigned(p.previous) and
  1266. (tai(p.previous).typ=ait_regalloc) and
  1267. (tai_regalloc(p.previous).ratype=ra_alloc) and
  1268. (tai_regalloc(p.previous).reg<>r) do
  1269. p:=tai(p.previous);
  1270. { , but add release after a reg_a_sync }
  1271. if assigned(p) and
  1272. (p.typ=ait_regalloc) and
  1273. (tai_regalloc(p).ratype=ra_sync) then
  1274. p:=tai(p.next);
  1275. if assigned(p) then
  1276. list.insertbefore(pdealloc,p)
  1277. else
  1278. list.concat(pdealloc);
  1279. end
  1280. {$ifdef EXTDEBUG}
  1281. else
  1282. Comment(V_Warning,'Register '+std_regname(r)+' not used');
  1283. {$endif EXTDEBUG}
  1284. end;
  1285. end;
  1286. procedure trgobj.insert_regalloc_info_all(list:Taasmoutput);
  1287. var
  1288. supreg : tsuperregister;
  1289. begin
  1290. { Insert regallocs for all imaginary registers }
  1291. for supreg:=first_imaginary to maxreg-1 do
  1292. insert_regalloc_info(list,supreg);
  1293. end;
  1294. procedure trgobj.add_cpu_interferences(p : tai);
  1295. begin
  1296. end;
  1297. procedure trgobj.generate_interference_graph(list:Taasmoutput;headertai:tai);
  1298. var
  1299. p : tai;
  1300. {$ifdef EXTDEBUG}
  1301. i : integer;
  1302. {$endif EXTDEBUG}
  1303. supreg : tsuperregister;
  1304. begin
  1305. { All allocations are available. Now we can generate the
  1306. interference graph. Walk through all instructions, we can
  1307. start with the headertai, because before the header tai is
  1308. only symbols. }
  1309. live_registers.clear;
  1310. p:=headertai;
  1311. while assigned(p) do
  1312. begin
  1313. if p.typ=ait_regalloc then
  1314. with Tai_regalloc(p) do
  1315. begin
  1316. if (getregtype(reg)=regtype) then
  1317. begin
  1318. supreg:=getsupreg(reg);
  1319. case ratype of
  1320. ra_alloc :
  1321. begin
  1322. live_registers.add(supreg);
  1323. add_edges_used(supreg);
  1324. end;
  1325. ra_dealloc :
  1326. begin
  1327. live_registers.delete(supreg);
  1328. add_edges_used(supreg);
  1329. end;
  1330. end;
  1331. { constraints needs always to be updated }
  1332. add_constraints(reg);
  1333. end;
  1334. end;
  1335. add_cpu_interferences(p);
  1336. p:=Tai(p.next);
  1337. end;
  1338. {$ifdef EXTDEBUG}
  1339. if live_registers.length>0 then
  1340. begin
  1341. for i:=0 to live_registers.length-1 do
  1342. begin
  1343. { Only report for imaginary registers }
  1344. if live_registers.buf^[i]>=first_imaginary then
  1345. Comment(V_Warning,'Register '+std_regname(newreg(R_INTREGISTER,live_registers.buf^[i],defaultsub))+' not released');
  1346. end;
  1347. end;
  1348. {$endif}
  1349. end;
  1350. procedure Trgobj.translate_registers(list:taasmoutput);
  1351. var
  1352. hp,p,q:Tai;
  1353. i:shortint;
  1354. {$ifdef arm}
  1355. so:pshifterop;
  1356. {$endif arm}
  1357. begin
  1358. { Leave when no imaginary registers are used }
  1359. if maxreg<=first_imaginary then
  1360. exit;
  1361. p:=Tai(list.first);
  1362. while assigned(p) do
  1363. begin
  1364. case p.typ of
  1365. ait_regalloc:
  1366. with Tai_regalloc(p) do
  1367. begin
  1368. if (getregtype(reg)=regtype) then
  1369. begin
  1370. { Only alloc/dealloc is needed for the optimizer, remove
  1371. other regalloc }
  1372. if not(ratype in [ra_alloc,ra_dealloc]) then
  1373. begin
  1374. q:=Tai(next);
  1375. list.remove(p);
  1376. p.free;
  1377. p:=q;
  1378. continue;
  1379. end
  1380. else
  1381. begin
  1382. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1383. {
  1384. Remove sequences of release and
  1385. allocation of the same register like. Other combinations
  1386. of release/allocate need to stay in the list.
  1387. # Register X released
  1388. # Register X allocated
  1389. }
  1390. if assigned(previous) and
  1391. (ratype=ra_alloc) and
  1392. (Tai(previous).typ=ait_regalloc) and
  1393. (Tai_regalloc(previous).reg=reg) and
  1394. (Tai_regalloc(previous).ratype=ra_dealloc) then
  1395. begin
  1396. q:=Tai(next);
  1397. hp:=tai(previous);
  1398. list.remove(hp);
  1399. hp.free;
  1400. list.remove(p);
  1401. p.free;
  1402. p:=q;
  1403. continue;
  1404. end;
  1405. end;
  1406. end;
  1407. end;
  1408. ait_instruction:
  1409. with Taicpu(p) do
  1410. begin
  1411. aktfilepos:=fileinfo;
  1412. for i:=0 to ops-1 do
  1413. with oper[i]^ do
  1414. case typ of
  1415. Top_reg:
  1416. if (getregtype(reg)=regtype) then
  1417. setsupreg(reg,reginfo[getsupreg(reg)].colour);
  1418. Top_ref:
  1419. begin
  1420. if regtype=R_INTREGISTER then
  1421. with ref^ do
  1422. begin
  1423. if base<>NR_NO then
  1424. setsupreg(base,reginfo[getsupreg(base)].colour);
  1425. if index<>NR_NO then
  1426. setsupreg(index,reginfo[getsupreg(index)].colour);
  1427. end;
  1428. end;
  1429. {$ifdef arm}
  1430. Top_shifterop:
  1431. begin
  1432. if regtype=R_INTREGISTER then
  1433. begin
  1434. so:=shifterop;
  1435. if so^.rs<>NR_NO then
  1436. setsupreg(so^.rs,reginfo[getsupreg(so^.rs)].colour);
  1437. end;
  1438. end;
  1439. {$endif arm}
  1440. end;
  1441. { Maybe the operation can be removed when
  1442. it is a move and both arguments are the same }
  1443. if is_same_reg_move(regtype) then
  1444. begin
  1445. q:=Tai(p.next);
  1446. list.remove(p);
  1447. p.free;
  1448. p:=q;
  1449. continue;
  1450. end;
  1451. end;
  1452. end;
  1453. p:=Tai(p.next);
  1454. end;
  1455. aktfilepos:=current_procinfo.exitpos;
  1456. end;
  1457. function trgobj.spill_registers(list:Taasmoutput;headertai:tai):boolean;
  1458. { Returns true if any help registers have been used }
  1459. var
  1460. i : word;
  1461. t : tsuperregister;
  1462. p,q : Tai;
  1463. regs_to_spill_set:Tsuperregisterset;
  1464. spill_temps : ^Tspill_temp_list;
  1465. supreg : tsuperregister;
  1466. templist : taasmoutput;
  1467. begin
  1468. spill_registers:=false;
  1469. live_registers.clear;
  1470. for i:=first_imaginary to maxreg-1 do
  1471. exclude(reginfo[i].flags,ri_selected);
  1472. spill_temps:=allocmem(sizeof(treference)*maxreg);
  1473. supregset_reset(regs_to_spill_set,false,$ffff);
  1474. { Allocate temps and insert in front of the list }
  1475. templist:=taasmoutput.create;
  1476. {Safe: this procedure is only called if there are spilled nodes.}
  1477. with spillednodes do
  1478. for i:=0 to length-1 do
  1479. begin
  1480. t:=buf^[i];
  1481. {Alternative representation.}
  1482. supregset_include(regs_to_spill_set,t);
  1483. {Clear all interferences of the spilled register.}
  1484. clear_interferences(t);
  1485. {Get a temp for the spilled register, the size must at least equal a complete register,
  1486. take also care of the fact that subreg can be larger than a single register like doubles
  1487. that occupy 2 registers }
  1488. tg.gettemp(templist,
  1489. max(tcgsize2size[reg_cgsize(newreg(regtype,t,R_SUBWHOLE))],
  1490. tcgsize2size[reg_cgsize(newreg(regtype,t,reginfo[t].subreg))]),
  1491. tt_noreuse,spill_temps^[t]);
  1492. end;
  1493. list.insertlistafter(headertai,templist);
  1494. templist.free;
  1495. { Walk through all instructions, we can start with the headertai,
  1496. because before the header tai is only symbols }
  1497. p:=headertai;
  1498. while assigned(p) do
  1499. begin
  1500. case p.typ of
  1501. ait_regalloc:
  1502. with Tai_regalloc(p) do
  1503. begin
  1504. if (getregtype(reg)=regtype) then
  1505. begin
  1506. {A register allocation of a spilled register can be removed.}
  1507. supreg:=getsupreg(reg);
  1508. if supregset_in(regs_to_spill_set,supreg) then
  1509. begin
  1510. q:=Tai(p.next);
  1511. list.remove(p);
  1512. p.free;
  1513. p:=q;
  1514. continue;
  1515. end
  1516. else
  1517. begin
  1518. case ratype of
  1519. ra_alloc :
  1520. live_registers.add(supreg);
  1521. ra_dealloc :
  1522. live_registers.delete(supreg);
  1523. end;
  1524. end;
  1525. end;
  1526. end;
  1527. ait_instruction:
  1528. with Taicpu(p) do
  1529. begin
  1530. aktfilepos:=fileinfo;
  1531. if instr_spill_register(list,taicpu(p),regs_to_spill_set,spill_temps^) then
  1532. spill_registers:=true;
  1533. end;
  1534. end;
  1535. p:=Tai(p.next);
  1536. end;
  1537. aktfilepos:=current_procinfo.exitpos;
  1538. {Safe: this procedure is only called if there are spilled nodes.}
  1539. with spillednodes do
  1540. for i:=0 to length-1 do
  1541. tg.ungettemp(list,spill_temps^[buf^[i]]);
  1542. freemem(spill_temps);
  1543. end;
  1544. function trgobj.do_spill_replace(list:Taasmoutput;instr:taicpu;orgreg:tsuperregister;const spilltemp:treference):boolean;
  1545. begin
  1546. result:=false;
  1547. end;
  1548. procedure Trgobj.do_spill_read(list:Taasmoutput;pos:tai;const spilltemp:treference;tempreg:tregister);
  1549. begin
  1550. list.insertafter(spilling_create_load(spilltemp,tempreg),pos);
  1551. end;
  1552. procedure Trgobj.do_spill_written(list:Taasmoutput;pos:tai;const spilltemp:treference;tempreg:tregister);
  1553. begin
  1554. list.insertafter(spilling_create_store(tempreg,spilltemp),pos);
  1555. end;
  1556. function trgobj.get_spill_subreg(r : tregister) : tsubregister;
  1557. begin
  1558. result:=defaultsub;
  1559. end;
  1560. function trgobj.instr_spill_register(list:Taasmoutput;
  1561. instr:taicpu;
  1562. const r:Tsuperregisterset;
  1563. const spilltemplist:Tspill_temp_list): boolean;
  1564. var
  1565. counter, regindex: longint;
  1566. regs: tspillregsinfo;
  1567. spilled: boolean;
  1568. procedure addreginfo(reg: tregister; operation: topertype);
  1569. var
  1570. i, tmpindex: longint;
  1571. supreg : tsuperregister;
  1572. begin
  1573. tmpindex := regindex;
  1574. supreg:=getsupreg(reg);
  1575. { did we already encounter this register? }
  1576. for i := 0 to pred(regindex) do
  1577. if (regs[i].orgreg = supreg) then
  1578. begin
  1579. tmpindex := i;
  1580. break;
  1581. end;
  1582. if tmpindex > high(regs) then
  1583. internalerror(2003120301);
  1584. regs[tmpindex].orgreg := supreg;
  1585. regs[tmpindex].spillreg:=reg;
  1586. if supregset_in(r,supreg) then
  1587. begin
  1588. { add/update info on this register }
  1589. regs[tmpindex].mustbespilled := true;
  1590. case operation of
  1591. operand_read:
  1592. regs[tmpindex].regread := true;
  1593. operand_write:
  1594. regs[tmpindex].regwritten := true;
  1595. operand_readwrite:
  1596. begin
  1597. regs[tmpindex].regread := true;
  1598. regs[tmpindex].regwritten := true;
  1599. end;
  1600. end;
  1601. spilled := true;
  1602. end;
  1603. inc(regindex,ord(regindex=tmpindex));
  1604. end;
  1605. procedure tryreplacereg(var reg: tregister);
  1606. var
  1607. i: longint;
  1608. supreg: tsuperregister;
  1609. begin
  1610. supreg:=getsupreg(reg);
  1611. for i:=0 to pred(regindex) do
  1612. if (regs[i].mustbespilled) and
  1613. (regs[i].orgreg=supreg) then
  1614. begin
  1615. { Only replace supreg }
  1616. setsupreg(reg,getsupreg(regs[i].tempreg));
  1617. break;
  1618. end;
  1619. end;
  1620. var
  1621. loadpos,
  1622. storepos : tai;
  1623. oldlive_registers : tsuperregisterworklist;
  1624. begin
  1625. result := false;
  1626. fillchar(regs,sizeof(regs),0);
  1627. for counter := low(regs) to high(regs) do
  1628. regs[counter].orgreg := RS_INVALID;
  1629. spilled := false;
  1630. regindex := 0;
  1631. { check whether and if so which and how (read/written) this instructions contains
  1632. registers that must be spilled }
  1633. for counter := 0 to instr.ops-1 do
  1634. with instr.oper[counter]^ do
  1635. begin
  1636. case typ of
  1637. top_reg:
  1638. begin
  1639. if (getregtype(reg) = regtype) then
  1640. addreginfo(reg,instr.spilling_get_operation_type(counter));
  1641. end;
  1642. top_ref:
  1643. begin
  1644. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1645. with ref^ do
  1646. begin
  1647. if (base <> NR_NO) then
  1648. addreginfo(base,instr.spilling_get_operation_type_ref(counter,base));
  1649. if (index <> NR_NO) then
  1650. addreginfo(index,instr.spilling_get_operation_type_ref(counter,index));
  1651. end;
  1652. end;
  1653. {$ifdef ARM}
  1654. top_shifterop:
  1655. begin
  1656. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1657. if shifterop^.rs<>NR_NO then
  1658. addreginfo(shifterop^.rs,operand_read);
  1659. end;
  1660. {$endif ARM}
  1661. end;
  1662. end;
  1663. { if no spilling for this instruction we can leave }
  1664. if not spilled then
  1665. exit;
  1666. {$ifdef x86}
  1667. { Try replacing the register with the spilltemp. This is usefull only
  1668. for the i386,x86_64 that support memory locations for several instructions }
  1669. for counter := 0 to pred(regindex) do
  1670. with regs[counter] do
  1671. begin
  1672. if mustbespilled then
  1673. begin
  1674. if do_spill_replace(list,instr,orgreg,spilltemplist[orgreg]) then
  1675. mustbespilled:=false;
  1676. end;
  1677. end;
  1678. {$endif x86}
  1679. {
  1680. There are registers that need are spilled. We generate the
  1681. following code for it. The used positions where code need
  1682. to be inserted are marked using #. Note that code is always inserted
  1683. before the positions using pos.previous. This way the position is always
  1684. the same since pos doesn't change, but pos.previous is modified everytime
  1685. new code is inserted.
  1686. [
  1687. - reg_allocs load spills
  1688. - load spills
  1689. ]
  1690. [#loadpos
  1691. - reg_deallocs
  1692. - reg_allocs
  1693. ]
  1694. [
  1695. - reg_deallocs for load-only spills
  1696. - reg_allocs for store-only spills
  1697. ]
  1698. [#instr
  1699. - original instruction
  1700. ]
  1701. [
  1702. - store spills
  1703. - reg_deallocs store spills
  1704. ]
  1705. [#storepos
  1706. ]
  1707. }
  1708. result := true;
  1709. oldlive_registers.copyfrom(live_registers);
  1710. { Process all tai_regallocs belonging to this instruction, ignore explicit
  1711. inserted regallocs. These can happend for example in i386:
  1712. mov ref,ireg26
  1713. <regdealloc ireg26, instr=taicpu of lea>
  1714. <regalloc edi, insrt=nil>
  1715. lea [ireg26+ireg17],edi
  1716. All released registers are also added to the live_registers because
  1717. they can't be used during the spilling }
  1718. loadpos:=tai(instr.previous);
  1719. while assigned(loadpos) and
  1720. (loadpos.typ=ait_regalloc) and
  1721. ((tai_regalloc(loadpos).instr=nil) or
  1722. (tai_regalloc(loadpos).instr=instr)) do
  1723. begin
  1724. { Only add deallocs belonging to the instruction. Explicit inserted deallocs
  1725. belong to the previous instruction and not the current instruction }
  1726. if (tai_regalloc(loadpos).instr=instr) and
  1727. (tai_regalloc(loadpos).ratype=ra_dealloc) then
  1728. live_registers.add(getsupreg(tai_regalloc(loadpos).reg));
  1729. loadpos:=tai(loadpos.previous);
  1730. end;
  1731. loadpos:=tai(loadpos.next);
  1732. { Load the spilled registers }
  1733. for counter := 0 to pred(regindex) do
  1734. with regs[counter] do
  1735. begin
  1736. if mustbespilled and regread then
  1737. begin
  1738. tempreg:=getregisterinline(list,get_spill_subreg(regs[counter].spillreg));
  1739. do_spill_read(list,tai(loadpos.previous),spilltemplist[orgreg],tempreg);
  1740. end;
  1741. end;
  1742. { Release temp registers of read-only registers, and add reference of the instruction
  1743. to the reginfo }
  1744. for counter := 0 to pred(regindex) do
  1745. with regs[counter] do
  1746. begin
  1747. if mustbespilled and regread and (not regwritten) then
  1748. begin
  1749. { The original instruction will be the next that uses this register }
  1750. add_reg_instruction(instr,tempreg);
  1751. ungetregisterinline(list,tempreg);
  1752. end;
  1753. end;
  1754. { Allocate temp registers of write-only registers, and add reference of the instruction
  1755. to the reginfo }
  1756. for counter := 0 to pred(regindex) do
  1757. with regs[counter] do
  1758. begin
  1759. if mustbespilled and regwritten then
  1760. begin
  1761. { When the register is also loaded there is already a register assigned }
  1762. if (not regread) then
  1763. tempreg:=getregisterinline(list,get_spill_subreg(regs[counter].spillreg));
  1764. { The original instruction will be the next that uses this register, this
  1765. also needs to be done for read-write registers }
  1766. add_reg_instruction(instr,tempreg);
  1767. end;
  1768. end;
  1769. { store the spilled registers }
  1770. storepos:=tai(instr.next);
  1771. for counter := 0 to pred(regindex) do
  1772. with regs[counter] do
  1773. begin
  1774. if mustbespilled and regwritten then
  1775. begin
  1776. do_spill_written(list,tai(storepos.previous),spilltemplist[orgreg],tempreg);
  1777. ungetregisterinline(list,tempreg);
  1778. end;
  1779. end;
  1780. { now all spilling code is generated we can restore the live registers. This
  1781. must be done after the store because the store can need an extra register
  1782. that also needs to conflict with the registers of the instruction }
  1783. live_registers.done;
  1784. live_registers:=oldlive_registers;
  1785. { substitute registers }
  1786. for counter:=0 to instr.ops-1 do
  1787. with instr.oper[counter]^ do
  1788. begin
  1789. case typ of
  1790. top_reg:
  1791. begin
  1792. if (getregtype(reg) = regtype) then
  1793. tryreplacereg(reg);
  1794. end;
  1795. top_ref:
  1796. begin
  1797. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1798. begin
  1799. tryreplacereg(ref^.base);
  1800. tryreplacereg(ref^.index);
  1801. end;
  1802. end;
  1803. {$ifdef ARM}
  1804. top_shifterop:
  1805. begin
  1806. if regtype in [R_INTREGISTER,R_ADDRESSREGISTER] then
  1807. tryreplacereg(shifterop^.rs);
  1808. end;
  1809. {$endif ARM}
  1810. end;
  1811. end;
  1812. end;
  1813. end.