12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699 |
- {
- $Id$
- Copyright (c) 1998-2002 by Florian Klaempfl
- This unit implements the base class for the register allocator
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- {$i fpcdefs.inc}
- {# @abstract(Abstract register allocator unit)
- This unit contains services to allocate, free
- references and registers which are used by
- the code generator.
- }
- {*******************************************************************************
- (applies to new register allocator)
- Register allocator introduction.
- Free Pascal uses a Chaitin style register allocator similair to the one
- described in the book "Modern compiler implementation in C" by Andrew W. Appel.,
- published by Cambridge University Press.
- Reading this book is recommended for a complete understanding. Here is a small
- introduction.
- The code generator thinks it has an infinite amount of registers. Our processorhas a limited amount of registers. Therefore we must reduce the amount of
- registers until there are less enough to fit into the processors registers.
- Registers can interfere or not interfere. If two imaginary registers interfere
- they cannot be placed into the same psysical register. Reduction of registers
- is done by:
- - "coalescing" Two registers that do not interfere are combined
- into one register.
- - "spilling" A register is changed into a memory location and the generated
- code is modified to use the memory location instead of the register.
- Register allocation is a graph colouring problem. Each register is a colour, and
- if two registers interfere there is a connection between them in the graph.
- In addition to the imaginary registers in the code generator, the psysical
- CPU registers are also present in this graph. This allows us to make
- interferences between imaginary registers and cpu registers. This is very
- usefull for describing archtectural constraints, like for example that
- the div instruction modifies edx, so variables that are in use at that time
- cannot be stored into edx. This can be modelled by making edx interfere
- with those variables.
- Graph colouring is an NP complete problem. Therefore we use an approximation
- that pushes registers to colour on to a stack. This is done in the "simplify"
- procedure.
- *******************************************************************************}
- unit rgobj;
- interface
- uses
- cutils, cpubase,
- cpuinfo,
- aasmbase,aasmtai,aasmcpu,
- cclasses,globtype,cginfo,cgbase,node
- {$ifdef delphi}
- ,dmisc
- {$endif}
- ;
- const
- ALL_INTREGISTERS=[first_supreg..last_supreg]-[RS_STACK_POINTER_REG];
- ALL_OTHERREGISTERS=[firstreg..lastreg];
- type
- regvarother_longintarray = array[firstreg..lastreg] of longint;
- regvarother_booleanarray = array[firstreg..lastreg] of boolean;
- regvarint_longintarray = array[first_supreg..last_supreg] of longint;
- regvarint_ptreearray = array[first_supreg..last_supreg] of tnode;
- tpushedsavedloc = record
- case byte of
- 0: (pushed: boolean);
- 1: (ofs: longint);
- end;
- tpushedsavedother = array[firstreg..lastreg] of tpushedsavedloc;
- {$ifndef newra}
- Tpushedsavedint = array[first_supreg..last_supreg] of Tpushedsavedloc;
- {$endif}
- Tinterferencebitmap=array[Tsuperregister] of set of Tsuperregister;
- Tinterferenceadjlist=array[Tsuperregister] of Pstring;
- Tinterferencegraph=record
- bitmap:Tinterferencebitmap;
- adjlist:Tinterferenceadjlist;
- end;
- Pinterferencegraph=^Tinterferencegraph;
- Tmovelist=record
- count:cardinal;
- data:array[0..$ffff] of Tlinkedlistitem;
- end;
- Pmovelist=^Tmovelist;
- {In the register allocator we keep track of move instructions.
- These instructions are moved between five linked lists. There
- is also a linked list per register to keep track about the moves
- it is associated with. Because we need to determine quickly in
- which of the five lists it is we add anu enumeradtion to each
- move instruction.}
- Tmoveset=(ms_coalesced_moves,ms_constrained_moves,ms_frozen_moves,
- ms_worklist_moves,ms_active_moves);
- Tmoveins=class(Tlinkedlistitem)
- moveset:Tmoveset;
- { $ifdef ra_debug}
- x,y:Tsuperregister;
- { $endif}
- instruction:Taicpu;
- end;
- {#
- This class implements the abstract register allocator
- It is used by the code generator to allocate and free
- registers which might be valid across nodes. It also
- contains utility routines related to registers.
- Some of the methods in this class should be overriden
- by cpu-specific implementations.
- }
- trgobj = class
- { The "usableregsxxx" contain all registers of type "xxx" that }
- { aren't currently allocated to a regvar. The "unusedregsxxx" }
- { contain all registers of type "xxx" that aren't currently }
- { allocated }
- lastintreg,maxintreg:Tsuperregister;
- unusedregsint,usableregsint:Tsupregset;
- unusedregsaddr,usableregsaddr:Tsupregset;
- unusedregsfpu,usableregsfpu : tregisterset;
- unusedregsmm,usableregsmm : tregisterset;
- { these counters contain the number of elements in the }
- { unusedregsxxx/usableregsxxx sets }
- {$ifndef newra}
- countunusedregsint,
- countunusedregsaddr,
- {$endif}
- countunusedregsfpu,
- countunusedregsmm : byte;
- countusableregsint,
- countusableregsaddr,
- countusableregsfpu,
- countusableregsmm : byte;
- { Contains the registers which are really used by the proc itself.
- It doesn't take care of registers used by called procedures
- }
- {$ifdef newra}
- savedintbyproc,
- {$endif}
- used_in_proc_int,
- usedaddrinproc : tsupregset;
- used_in_proc_other : tregisterset;
- reg_pushes_other : regvarother_longintarray;
- {$ifndef newra}
- reg_pushes_int : regvarint_longintarray;
- {$endif}
- is_reg_var_other : regvarother_booleanarray;
- is_reg_var_int : Tsupregset;
- regvar_loaded_other : regvarother_booleanarray;
- regvar_loaded_int : Tsupregset;
- {$ifdef newra}
- colour : array[Tsuperregister] of Tsuperregister;
- spillednodes : string;
- {$endif}
- { tries to hold the amount of times which the current tree is processed }
- t_times: longint;
- constructor create(Acpu_registers:byte);
- {# Allocate a general purpose register
- An internalerror will be generated if there
- is no more free registers which can be allocated
- }
- function getregisterint(list:Taasmoutput;size:Tcgsize):Tregister;{$ifndef newra}virtual;{$endif}
- {$ifdef newra}
- procedure add_constraints(reg:Tnewregister);virtual;
- {# Allocate an ABT register
- An internalerror will be generated if there
- is no more free registers which can be allocated
- An explanantion of abt registers can be found near the implementation.
- }
- function getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
- {$endif}
- {# Free a general purpose register
- @param(r register to free)
- }
- procedure ungetregisterint(list: taasmoutput; r : tregister); virtual;
- {# Allocate a floating point register
- An internalerror will be generated if there
- is no more free registers which can be allocated
- }
- function getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister; virtual;
- {# Free a floating point register
- @param(r register to free)
- }
- procedure ungetregisterfpu(list: taasmoutput; r : tregister;size:TCGsize); virtual;
- function getregistermm(list: taasmoutput) : tregister; virtual;
- procedure ungetregistermm(list: taasmoutput; r : tregister); virtual;
- {# Allocate an address register.
- Address registers are the only registers which can
- be used as a base register in references (treference).
- On most cpu's this is the same as a general purpose
- register.
- An internalerror will be generated if there
- is no more free registers which can be allocated
- }
- function getaddressregister(list:Taasmoutput):Tregister;virtual;
- procedure ungetaddressregister(list: taasmoutput; r: tregister); virtual;
- {# Verify if the specified register is an address or
- general purpose register. Returns TRUE if @var(reg)
- is an adress register.
- This routine should only be used to check on
- general purpose or address register. It will
- not work on multimedia or floating point
- registers
- @param(reg register to verify)
- }
- function isaddressregister(reg: tregister): boolean; virtual;
- {# Tries to allocate the passed register, if possible
- @param(r specific register to allocate)
- }
- function getexplicitregisterint(list:Taasmoutput;r:Tnewregister):Tregister;virtual;
- {# Tries to allocate the passed fpu register, if possible
- @param(r specific register to allocate)
- }
- function getexplicitregisterfpu(list : taasmoutput; r : Toldregister) : tregister;virtual;
- {# Deallocate any kind of register }
- procedure ungetregister(list: taasmoutput; r : tregister); virtual;
- {# Deallocate all registers which are allocated
- in the specified reference. On most systems,
- this will free the base and index registers
- of the specified reference.
- @param(ref reference which must have its registers freed)
- }
- procedure ungetreference(list: taasmoutput; const ref : treference); virtual;
- {# Reset the register allocator information (usable registers etc).
- Please note that it is mortal sins to call cleartempgen during
- graph colouring (that is between prepare_colouring and
- epilogue_colouring).
- }
- procedure cleartempgen;virtual;
- {# Convert a register to a specified register size, and return that register size }
- function makeregsize(reg: tregister; size: tcgsize): tregister; virtual;
- {# saves register variables (restoring happens automatically) }
- {$ifndef newra}
- procedure saveintregvars(list:Taasmoutput;const s:Tsupregset);
- {$endif}
- procedure saveotherregvars(list:Taasmoutput;const s:Tregisterset);
- {# Saves in temporary references (allocated via the temp. allocator)
- the registers defined in @var(s). The registers are only saved
- if they are currently in use, otherwise they are left as is.
- On processors which have instructions which manipulate the stack,
- this routine should be overriden for performance reasons.
- @param(list) List to add the instruction to
- @param(saved) Array of saved register information
- @param(s) Registers which might require saving
- }
- {$ifndef newra}
- procedure saveusedintregisters(list:Taasmoutput;
- var saved:Tpushedsavedint;
- const s:Tsupregset);virtual;
- {$endif}
- procedure saveusedotherregisters(list:Taasmoutput;
- var saved:Tpushedsavedother;
- const s:Tregisterset);virtual;
- {# Restores the registers which were saved with a call
- to @var(saveusedregisters).
- On processors which have instructions which manipulate the stack,
- this routine should be overriden for performance reasons.
- }
- {$ifndef newra}
- procedure restoreusedintregisters(list:Taasmoutput;
- const saved:Tpushedsavedint);virtual;
- {$endif}
- procedure restoreusedotherregisters(list:Taasmoutput;
- const saved:Tpushedsavedother);virtual;
- { used when deciding which registers to use for regvars }
- {$ifndef newra}
- procedure incrementintregisterpushed(const s:Tsupregset);
- {$endif}
- procedure incrementotherregisterpushed(const s: tregisterset);
- procedure clearregistercount;
- procedure resetusableregisters;virtual;
- procedure makeregvarint(reg:Tsuperregister);
- procedure makeregvarother(reg:Tregister);
- procedure saveStateForInline(var state: pointer);virtual;
- procedure restoreStateAfterInline(var state: pointer);virtual;
- procedure saveUnusedState(var state: pointer);virtual;
- procedure restoreUnusedState(var state: pointer);virtual;
- {$ifdef newra}
- {$ifdef ra_debug}
- procedure writegraph;
- {$endif}
- procedure add_move_instruction(instr:Taicpu);
- procedure prepare_colouring;
- procedure epilogue_colouring;
- procedure colour_registers;
- function spill_registers(list:Taasmoutput;const regs_to_spill:string):boolean;
- {$endif newra}
- protected
- cpu_registers:byte;
- {$ifdef newra}
- igraph:Tinterferencegraph;
- degree:array[0..255] of byte;
- alias:array[Tsuperregister] of Tsuperregister;
- simplifyworklist,freezeworklist,spillworklist:string;
- coalescednodes:string;
- selectstack:string;
- abtlist:string;
- movelist:array[Tsuperregister] of Pmovelist;
- worklist_moves,active_moves,frozen_moves,
- coalesced_moves,constrained_moves:Tlinkedlist;
- {$endif}
- { the following two contain the common (generic) code for all }
- { get- and ungetregisterxxx functions/procedures }
- function getregistergenother(list: taasmoutput; const lowreg, highreg: Toldregister;
- var unusedregs:Tregisterset;var countunusedregs:byte): tregister;
- function getregistergenint(list:Taasmoutput;subreg:Tsubregister;
- const lowreg,highreg:Tsuperregister;
- var fusedinproc,unusedregs:Tsupregset
- {$ifndef newra};var countunusedregs:byte{$endif}):Tregister;
- procedure ungetregistergen(list: taasmoutput; const r: tregister;
- const usableregs:tregisterset;var unusedregs: tregisterset; var countunusedregs: byte);
- procedure ungetregistergenint(list:taasmoutput;const r:Tregister;
- const usableregs:Tsupregset;
- var unusedregs:Tsupregset
- {$ifndef newra};var countunusedregs:byte{$endif});
- {$ifdef newra}
- procedure getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
- procedure ungetregisterintinline(list:Taasmoutput;position:Tai;const r:Tregister);
- {$endif}
- {$ifdef TEMPREGDEBUG}
- reg_user : regvar_ptreearray;
- reg_releaser : regvar_ptreearray;
- {$endif TEMPREGDEBUG}
- {$ifdef TEMPREGDEBUG}
- procedure testregisters;
- {$endif TEMPREGDEBUGx}
- {$ifdef newra}
- procedure add_edge(u,v:Tsuperregister);
- procedure add_edges_used(u:Tsuperregister);
- procedure add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
- function move_related(n:Tsuperregister):boolean;
- procedure make_work_list;
- procedure enable_moves(n:Tsuperregister);
- procedure decrement_degree(m:Tsuperregister);
- procedure simplify;
- function get_alias(n:Tsuperregister):Tsuperregister;
- procedure add_worklist(u:Tsuperregister);
- function adjacent_ok(u,v:Tsuperregister):boolean;
- function conservative(u,v:Tsuperregister):boolean;
- procedure combine(u,v:Tsuperregister);
- procedure coalesce;
- procedure freeze_moves(u:Tsuperregister);
- procedure freeze;
- procedure select_spill;
- procedure assign_colours;
- procedure clear_interferences(u:Tsuperregister);
- {$endif}
- end;
- const
- {# This value is used in tsaved. If the array value is equal
- to this, then this means that this register is not used.
- }
- reg_not_saved = $7fffffff;
- var
- {# This is the class instance used to access the register allocator class }
- rg: trgobj;
- { trerefence handling }
- {# Clear to zero a treference }
- procedure reference_reset(var ref : treference);
- procedure reference_reset_old(var ref : treference);
- {# Clear to zero a treference, and set is base address
- to base register.
- }
- procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
- procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
- procedure reference_release(list: taasmoutput; const ref : treference);
- { This routine verifies if two references are the same, and
- if so, returns TRUE, otherwise returns false.
- }
- function references_equal(sref : treference;dref : treference) : boolean;
- { tlocation handling }
- procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
- procedure location_release(list: taasmoutput; const l : tlocation);
- procedure location_freetemp(list: taasmoutput; const l : tlocation);
- procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
- procedure location_swap(var destloc,sourceloc : tlocation);
- type
- psavedstate = ^tsavedstate;
- tsavedstate = record
- unusedregsint,usableregsint : Tsupregset;
- unusedregsaddr,usableregsaddr : Tsupregset;
- unusedregsfpu,usableregsfpu : tregisterset;
- unusedregsmm,usableregsmm : tregisterset;
- {$ifndef newra}
- countunusedregsint,
- countunusedregsaddr,
- {$endif}
- countunusedregsfpu,
- countunusedregsmm : byte;
- countusableregsint,
- countusableregsfpu,
- countusableregsmm : byte;
- { contains the registers which are really used by the proc itself }
- used_in_proc_int : tsupregset;
- used_in_proc_other : tregisterset;
- reg_pushes_other : regvarother_longintarray;
- reg_pushes_int : regvarint_longintarray;
- is_reg_var_other : regvarother_booleanarray;
- is_reg_var_int : Tsupregset;
- regvar_loaded_other: regvarother_booleanarray;
- regvar_loaded_int: Tsupregset;
- {$ifdef TEMPREGDEBUG}
- reg_user : regvar_ptreearray;
- reg_releaser : regvar_ptreearray;
- {$endif TEMPREGDEBUG}
- end;
- punusedstate = ^tunusedstate;
- tunusedstate = record
- unusedregsint : Tsupregset;
- unusedregsaddr : Tsupregset;
- unusedregsfpu : tregisterset;
- unusedregsmm : tregisterset;
- {$ifndef newra}
- countunusedregsint,
- countunusedregsaddr,
- {$endif}
- countunusedregsfpu,
- countunusedregsmm : byte;
- end;
- implementation
- uses
- systems,
- globals,verbose,
- cgobj,tgobj,regvars;
- constructor Trgobj.create(Acpu_registers:byte);
- begin
- used_in_proc_int := [];
- used_in_proc_other:=[];
- t_times := 0;
- resetusableregisters;
- lastintreg:=0;
- maxintreg:=first_imreg;
- cpu_registers:=Acpu_registers;
- {$ifdef TEMPREGDEBUG}
- fillchar(reg_user,sizeof(reg_user),0);
- fillchar(reg_releaser,sizeof(reg_releaser),0);
- {$endif TEMPREGDEBUG}
- {$ifdef newra}
- fillchar(igraph,sizeof(igraph),0);
- fillchar(degree,sizeof(degree),0);
- fillchar(movelist,sizeof(movelist),0);
- worklist_moves:=Tlinkedlist.create;
- abtlist:='';
- {$endif}
- end;
- function trgobj.getregistergenother(list: taasmoutput; const lowreg, highreg: Toldregister;
- var unusedregs: tregisterset; var countunusedregs: byte): tregister;
- var
- i: Toldregister;
- r: Tregister;
- begin
- for i:=lowreg to highreg do
- begin
- if i in unusedregs then
- begin
- exclude(unusedregs,i);
- include(used_in_proc_other,i);
- dec(countunusedregs);
- r.enum:=i;
- list.concat(tai_regalloc.alloc(r));
- result := r;
- exit;
- end;
- end;
- internalerror(10);
- end;
- function Trgobj.getregistergenint(list:Taasmoutput;
- subreg:Tsubregister;
- const lowreg,highreg:Tsuperregister;
- var fusedinproc,unusedregs:Tsupregset
- {$ifndef newra};var countunusedregs:byte{$endif}):Tregister;
- {$ifdef powerpc}
- {$ifndef newra}
- {$define reuseregs}
- {$endif newra}
- {$endif powerpc}
- var i:Tsuperregister;
- r:Tregister;
- begin
- {$ifdef reuseregs}
- i := lowreg;
- lastintreg := highreg;
- {$else reuseregs}
- if not (lastintreg in [lowreg..highreg]) then
- lastintreg:=lowreg;
- i:=lastintreg;
- {$endif reuseregs}
- repeat
- if i=highreg then
- i:=lowreg
- else
- inc(i);
- if (i in unusedregs) {$ifdef newra} and (pos(char(i),abtlist)=0) {$endif} then
- begin
- exclude(unusedregs,i);
- include(fusedinproc,i);
- {$ifndef newra}
- dec(countunusedregs);
- {$endif}
- r.enum:=R_INTREGISTER;
- r.number:=i shl 8 or subreg;
- list.concat(Tai_regalloc.alloc(r));
- result:=r;
- lastintreg:=i;
- if i>maxintreg then
- maxintreg:=i;
- {$ifdef newra}
- add_edges_used(i);
- {$endif}
- exit;
- end;
- until i=lastintreg;
- internalerror(10);
- end;
- procedure trgobj.ungetregistergen(list: taasmoutput; const r: tregister;
- const usableregs: tregisterset; var unusedregs: tregisterset; var countunusedregs: byte);
- begin
- if r.enum>lastreg then
- internalerror(2003010801);
- { takes much time }
- if not(r.enum in usableregs) then
- exit;
- {$ifdef TEMPREGDEBUG}
- if (r.enum in unusedregs) then
- {$ifdef EXTTEMPREGDEBUG}
- begin
- Comment(V_Debug,'register freed twice '+std_reg2str[r.enum]);
- testregisters32;
- exit;
- end
- {$else EXTTEMPREGDEBUG}
- exit
- {$endif EXTTEMPREGDEBUG}
- else
- {$endif TEMPREGDEBUG}
- inc(countunusedregs);
- include(unusedregs,r.enum);
- list.concat(tai_regalloc.dealloc(r));
- end;
- procedure trgobj.ungetregistergenint(list:taasmoutput;const r:Tregister;
- const usableregs:Tsupregset;
- var unusedregs:Tsupregset
- {$ifndef newra};var countunusedregs:byte{$endif});
- var supreg:Tsuperregister;
- begin
- if r.enum<=lastreg then
- internalerror(2003010803);
- supreg:=r.number shr 8;
- { takes much time }
- {$ifndef newra}
- if not(supreg in usableregs) then
- exit;
- {$endif}
- {$ifdef TEMPREGDEBUG}
- if (supreg in unusedregs) then
- {$ifdef EXTTEMPREGDEBUG}
- begin
- comment(v_debug,'register freed twice '+supreg_name(supreg));
- testregisters32
- exit;
- end
- {$else EXTTEMPREGDEBUG}
- exit
- {$endif EXTTEMPREGDEBUG}
- else
- {$endif TEMPREGDEBUG}
- {$ifndef newra}inc(countunusedregs){$endif};
- include(unusedregs,supreg);
- list.concat(tai_regalloc.dealloc(r));
- {$ifdef newra}
- add_edges_used(supreg);
- {$endif newra}
- end;
- function trgobj.getregisterint(list:taasmoutput;size:Tcgsize):Tregister;
- var subreg:Tsubregister;
- begin
- {$ifndef newra}
- if countunusedregsint=0 then
- internalerror(10);
- {$ifdef TEMPREGDEBUG}
- if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
- internalerror(10);
- {$endif TEMPREGDEBUG}
- {$ifdef EXTTEMPREGDEBUG}
- if curptree^^.usableregs-countunusedregsint>curptree^^.reallyusedregs then
- curptree^^.reallyusedregs:=curptree^^.usableregs-countunusedregsint;
- {$endif EXTTEMPREGDEBUG}
- {$endif}
- subreg:=cgsize2subreg(size);
- result:=getregistergenint(list,
- subreg,
- {$ifdef newra}
- first_imreg,
- last_imreg,
- {$else}
- first_supreg,
- last_supreg,
- {$endif}
- used_in_proc_int,
- unusedregsint{$ifndef newra},
- countunusedregsint{$endif});
- {$ifdef TEMPREGDEBUG}
- reg_user[result]:=curptree^;
- testregisters32;
- {$endif TEMPREGDEBUG}
- {$ifdef newra}
- add_constraints(getregisterint.number);
- {$endif}
- end;
- {$ifdef newra}
- procedure Trgobj.add_constraints(reg:Tnewregister);
- begin
- end;
- {$endif}
- procedure trgobj.ungetregisterint(list : taasmoutput; r : tregister);
- begin
- ungetregistergenint(list,r,usableregsint,unusedregsint{$ifndef newra},
- countunusedregsint{$endif});
- {$ifdef TEMPREGDEBUG}
- reg_releaser[r]:=curptree^;
- testregisters32;
- {$endif TEMPREGDEBUG}
- end;
- { tries to allocate the passed register, if possible }
- function trgobj.getexplicitregisterint(list:Taasmoutput;r:Tnewregister):Tregister;
- var r2:Tregister;
- begin
- if (r shr 8) in unusedregsint then
- begin
- {$ifndef newra}
- dec(countunusedregsint);
- {$ifdef TEMPREGDEBUG}
- if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
- internalerror(10);
- reg_user[r shr 8]:=curptree^;
- {$endif TEMPREGDEBUG}
- {$endif newra}
- exclude(unusedregsint,r shr 8);
- include(used_in_proc_int,r shr 8);
- r2.enum:=R_INTREGISTER;
- r2.number:=r;
- list.concat(tai_regalloc.alloc(r2));
- {$ifdef TEMPREGDEBUG}
- testregisters32;
- {$endif TEMPREGDEBUG}
- end
- else
- internalerror(200301103);
- getexplicitregisterint:=r2;
- end;
- { tries to allocate the passed register, if possible }
- function trgobj.getexplicitregisterfpu(list : taasmoutput; r : Toldregister) : tregister;
- var r2:Tregister;
- begin
- if r in unusedregsfpu then
- begin
- dec(countunusedregsfpu);
- {$ifdef TEMPREGDEBUG}
- if curptree^^.usableregs-countunusedregsint>curptree^^.registers32 then
- internalerror(10);
- reg_user[r]:=curptree^;
- {$endif TEMPREGDEBUG}
- exclude(unusedregsfpu,r);
- include(used_in_proc_other,r);
- r2.enum:=r;
- list.concat(tai_regalloc.alloc(r2));
- getexplicitregisterfpu:=r2;
- {$ifdef TEMPREGDEBUG}
- testregisters32;
- {$endif TEMPREGDEBUG}
- end
- else
- {$warning Size for FPU reg is maybe not correct}
- getexplicitregisterfpu:=getregisterfpu(list,OS_F32);
- end;
- function trgobj.getregisterfpu(list: taasmoutput;size:Tcgsize) : tregister;
- begin
- if countunusedregsfpu=0 then
- internalerror(10);
- result := getregistergenother(list,firstsavefpureg,lastsavefpureg,
- unusedregsfpu,countunusedregsfpu);
- end;
- procedure trgobj.ungetregisterfpu(list : taasmoutput; r : tregister;size:TCGsize);
- begin
- ungetregistergen(list,r,usableregsfpu,unusedregsfpu,
- countunusedregsfpu);
- end;
- function trgobj.getregistermm(list: taasmoutput) : tregister;
- begin
- if countunusedregsmm=0 then
- internalerror(10);
- result := getregistergenother(list,firstsavemmreg,lastsavemmreg,
- unusedregsmm,countunusedregsmm);
- end;
- procedure trgobj.ungetregistermm(list: taasmoutput; r: tregister);
- begin
- ungetregistergen(list,r,usableregsmm,unusedregsmm,
- countunusedregsmm);
- end;
- function trgobj.getaddressregister(list:Taasmoutput): tregister;
- begin
- {An address register is OS_INT per definition.}
- result := getregisterint(list,OS_INT);
- end;
- procedure trgobj.ungetaddressregister(list: taasmoutput; r: tregister);
- begin
- ungetregisterint(list,r);
- end;
- function trgobj.isaddressregister(reg: tregister): boolean;
- begin
- if reg.number<>0 then; { remove warning }
- result := true;
- end;
- procedure trgobj.ungetregister(list: taasmoutput; r : tregister);
- begin
- if r.enum=R_NO then
- exit;
- if r.enum>lastreg then
- internalerror(200301081);
- if r.enum in intregs then
- ungetregisterint(list,r)
- else if r.enum in fpuregs then
- ungetregisterfpu(list,r,OS_NO)
- else if r.enum in mmregs then
- ungetregistermm(list,r)
- else if r.enum in addrregs then
- ungetaddressregister(list,r)
- else internalerror(2002070602);
- end;
- procedure Trgobj.cleartempgen;
- {$ifdef newra}
- var i:Tsuperregister;
- {$endif newra}
- begin
- {$ifndef newra}
- countunusedregsint:=countusableregsint;
- {$endif}
- countunusedregsfpu:=countusableregsfpu;
- countunusedregsmm:=countusableregsmm;
- lastintreg:=0;
- maxintreg:=first_imreg;
- {$ifdef newra}
- unusedregsint:=[0..255];
- {$else}
- unusedregsint:=usableregsint;
- {$endif}
- unusedregsfpu:=usableregsfpu;
- unusedregsmm:=usableregsmm;
- {$ifdef newra}
- saved_by_proc_int:=[];
- for i:=low(Tsuperregister) to high(Tsuperregister) do
- begin
- if igraph.adjlist[i]<>nil then
- dispose(igraph.adjlist[i]);
- if movelist[i]<>nil then
- dispose(movelist[i]);
- end;
- fillchar(movelist,sizeof(movelist),0);
- fillchar(igraph,sizeof(igraph),0);
- fillchar(degree,sizeof(degree),0);
- worklist_moves.clear;
- abtlist:='';
- {$endif}
- end;
- procedure trgobj.ungetreference(list : taasmoutput; const ref : treference);
- begin
- if ref.base.number<>NR_NO then
- ungetregisterint(list,ref.base);
- if ref.index.number<>NR_NO then
- ungetregisterint(list,ref.index);
- end;
- {$ifndef newra}
- procedure trgobj.saveintregvars(list:Taasmoutput;const s:Tsupregset);
- var r:Tsuperregister;
- hr: tregister;
- begin
- if not(cs_regalloc in aktglobalswitches) then
- exit;
- for r:=firstsaveintreg to lastsaveintreg do
- if (r in is_reg_var_int) and
- (r in s) then
- begin
- hr.number:=r shl 8;
- hr.enum:=R_INTREGISTER;
- store_regvar(list,hr);
- end;
- end;
- {$endif}
- procedure trgobj.saveotherregvars(list: taasmoutput; const s: tregisterset);
- var
- r: Tregister;
- begin
- if not(cs_regalloc in aktglobalswitches) then
- exit;
- if firstsavefpureg <> R_NO then
- for r.enum := firstsavefpureg to lastsavefpureg do
- if is_reg_var_other[r.enum] and
- (r.enum in s) then
- store_regvar(list,r);
- if firstsavemmreg <> R_NO then
- for r.enum := firstsavemmreg to lastsavemmreg do
- if is_reg_var_other[r.enum] and
- (r.enum in s) then
- store_regvar(list,r);
- end;
- {$ifndef newra}
- procedure trgobj.saveusedintregisters(list:Taasmoutput;
- var saved:Tpushedsavedint;
- const s:Tsupregset);
- var r:Tsuperregister;
- r2:Tregister;
- hr : treference;
- begin
- used_in_proc_int:=used_in_proc_int+s;
- for r:=firstsaveintreg to lastsaveintreg do
- begin
- saved[r].ofs:=reg_not_saved;
- { if the register is used by the calling subroutine and if }
- { it's not a regvar (those are handled separately) }
- if not (r in is_reg_var_int) and
- (r in s) and
- { and is present in use }
- not(r in unusedregsint) then
- begin
- { then save it }
- tg.GetTemp(list,sizeof(aword),tt_persistent,hr);
- saved[r].ofs:=hr.offset;
- r2.enum:=R_INTREGISTER;
- r2.number:=r shl 8 or R_SUBWHOLE;
- cg.a_load_reg_ref(list,OS_INT,OS_INT,r2,hr);
- cg.a_reg_dealloc(list,r2);
- include(unusedregsint,r);
- inc(countunusedregsint);
- end;
- end;
- {$ifdef TEMPREGDEBUG}
- testregisters32;
- {$endif TEMPREGDEBUG}
- end;
- {$endif}
- procedure trgobj.saveusedotherregisters(list: taasmoutput;
- var saved : tpushedsavedother; const s: tregisterset);
- var
- r : tregister;
- hr : treference;
- begin
- used_in_proc_other:=used_in_proc_other + s;
- { don't try to save the fpu registers if not desired (e.g. for }
- { the 80x86) }
- if firstsavefpureg <> R_NO then
- for r.enum:=firstsavefpureg to lastsavefpureg do
- begin
- saved[r.enum].ofs:=reg_not_saved;
- { if the register is used by the calling subroutine and if }
- { it's not a regvar (those are handled separately) }
- if not is_reg_var_other[r.enum] and
- (r.enum in s) and
- { and is present in use }
- not(r.enum in unusedregsfpu) then
- begin
- { then save it }
- tg.GetTemp(list,extended_size,tt_persistent,hr);
- saved[r.enum].ofs:=hr.offset;
- cg.a_loadfpu_reg_ref(list,OS_FLOAT,r,hr);
- cg.a_reg_dealloc(list,r);
- include(unusedregsfpu,r.enum);
- inc(countunusedregsfpu);
- end;
- end;
- { don't save the vector registers if there's no support for them }
- if firstsavemmreg <> R_NO then
- for r.enum:=firstsavemmreg to lastsavemmreg do
- begin
- saved[r.enum].ofs:=reg_not_saved;
- { if the register is in use and if it's not a regvar (those }
- { are handled separately), save it }
- if not is_reg_var_other[r.enum] and
- (r.enum in s) and
- { and is present in use }
- not(r.enum in unusedregsmm) then
- begin
- { then save it }
- tg.GetTemp(list,mmreg_size,tt_persistent,hr);
- saved[r.enum].ofs:=hr.offset;
- cg.a_loadmm_reg_ref(list,r,hr);
- cg.a_reg_dealloc(list,r);
- include(unusedregsmm,r.enum);
- inc(countunusedregsmm);
- end;
- end;
- {$ifdef TEMPREGDEBUG}
- testregisters32;
- {$endif TEMPREGDEBUG}
- end;
- {$ifndef newra}
- procedure trgobj.restoreusedintregisters(list:Taasmoutput;
- const saved:Tpushedsavedint);
- var r:Tsuperregister;
- r2:Tregister;
- hr:Treference;
- begin
- for r:=lastsaveintreg downto firstsaveintreg do
- begin
- if saved[r].ofs <> reg_not_saved then
- begin
- r2.enum:=R_INTREGISTER;
- r2.number:=NR_FRAME_POINTER_REG;
- reference_reset_base(hr,r2,saved[r].ofs);
- r2.enum:=R_INTREGISTER;
- r2.number:=r shl 8 or R_SUBWHOLE;
- cg.a_reg_alloc(list,r2);
- cg.a_load_ref_reg(list,OS_INT,OS_INT,hr,r2);
- if not (r in unusedregsint) then
- { internalerror(10)
- in n386cal we always save/restore the reg *state*
- using save/restoreunusedstate -> the current state
- may not be real (JM) }
- else
- begin
- dec(countunusedregsint);
- exclude(unusedregsint,r);
- end;
- tg.UnGetTemp(list,hr);
- end;
- end;
- {$ifdef TEMPREGDEBUG}
- testregisters32;
- {$endif TEMPREGDEBUG}
- end;
- {$endif}
- procedure trgobj.restoreusedotherregisters(list : taasmoutput;
- const saved : tpushedsavedother);
- var
- r,r2 : tregister;
- hr : treference;
- begin
- if firstsavemmreg <> R_NO then
- for r.enum:=lastsavemmreg downto firstsavemmreg do
- begin
- if saved[r.enum].ofs <> reg_not_saved then
- begin
- r2.enum:=R_INTREGISTER;
- r2.number:=NR_FRAME_POINTER_REG;
- reference_reset_base(hr,r2,saved[r.enum].ofs);
- cg.a_reg_alloc(list,r);
- cg.a_loadmm_ref_reg(list,hr,r);
- if not (r.enum in unusedregsmm) then
- { internalerror(10)
- in n386cal we always save/restore the reg *state*
- using save/restoreunusedstate -> the current state
- may not be real (JM) }
- else
- begin
- dec(countunusedregsmm);
- exclude(unusedregsmm,r.enum);
- end;
- tg.UnGetTemp(list,hr);
- end;
- end;
- if firstsavefpureg <> R_NO then
- for r.enum:=lastsavefpureg downto firstsavefpureg do
- begin
- if saved[r.enum].ofs <> reg_not_saved then
- begin
- r2.enum:=R_INTREGISTER;
- r2.number:=NR_FRAME_POINTER_REG;
- reference_reset_base(hr,r2,saved[r.enum].ofs);
- cg.a_reg_alloc(list,r);
- cg.a_loadfpu_ref_reg(list,OS_FLOAT,hr,r);
- if not (r.enum in unusedregsfpu) then
- { internalerror(10)
- in n386cal we always save/restore the reg *state*
- using save/restoreunusedstate -> the current state
- may not be real (JM) }
- else
- begin
- dec(countunusedregsfpu);
- exclude(unusedregsfpu,r.enum);
- end;
- tg.UnGetTemp(list,hr);
- end;
- end;
- {$ifdef TEMPREGDEBUG}
- testregisters32;
- {$endif TEMPREGDEBUG}
- end;
- {$ifndef newra}
- procedure trgobj.incrementintregisterpushed(const s:Tsupregset);
- {$ifdef i386}
- var
- regi:Tsuperregister;
- {$endif i386}
- begin
- {$ifdef i386}
- for regi:=firstsaveintreg to lastsaveintreg do
- begin
- if (regi in s) then
- inc(reg_pushes_int[regi],t_times*2);
- end;
- {$endif i386}
- end;
- {$endif}
- procedure trgobj.incrementotherregisterpushed(const s:Tregisterset);
- {$ifdef i386}
- var
- regi : Toldregister;
- {$endif i386}
- begin
- {$ifdef i386}
- if firstsavefpureg <> R_NO then
- for regi:=firstsavefpureg to lastsavefpureg do
- begin
- if (regi in s) then
- inc(reg_pushes_other[regi],t_times*2);
- end;
- if firstsavemmreg <> R_NO then
- for regi:=firstsavemmreg to lastsavemmreg do
- begin
- if (regi in s) then
- inc(reg_pushes_other[regi],t_times*2);
- end;
- {$endif i386}
- end;
- procedure trgobj.clearregistercount;
- begin
- {$ifndef newra}
- fillchar(reg_pushes_int,sizeof(reg_pushes_int),0);
- {$endif}
- fillchar(reg_pushes_other,sizeof(reg_pushes_other),0);
- {ifndef i386}
- { all used registers will have to be saved at the start and restored }
- { at the end, but otoh regpara's do not have to be saved to memory }
- { at the start (there is a move from regpara to regvar most of the }
- { time though) -> set cost to 100+20 }
- {$ifndef newra}
- filldword(reg_pushes_int[firstsaveintreg],lastsaveintreg-firstsaveintreg+1,120);
- {$endif}
- filldword(reg_pushes_other[firstsavefpureg],ord(lastsavefpureg)-ord(firstsavefpureg)+1,120);
- {endif not i386}
- fillchar(is_reg_var_other,sizeof(is_reg_var_other),false);
- is_reg_var_int:=[];
- fillchar(regvar_loaded_other,sizeof(regvar_loaded_other),false);
- regvar_loaded_int:=[];
- end;
- procedure trgobj.resetusableregisters;
- begin
- { initialize fields with constant values from cpubase }
- countusableregsint := cpubase.c_countusableregsint;
- countusableregsfpu := cpubase.c_countusableregsfpu;
- countusableregsmm := cpubase.c_countusableregsmm;
- usableregsint := cpubase.usableregsint;
- usableregsfpu := cpubase.usableregsfpu;
- usableregsmm := cpubase.usableregsmm;
- clearregistercount;
- end;
- procedure trgobj.makeregvarint(reg:Tsuperregister);
- begin
- dec(countusableregsint);
- {$ifndef newra}
- dec(countunusedregsint);
- {$endif}
- exclude(usableregsint,reg);
- exclude(unusedregsint,reg);
- include(is_reg_var_int,reg);
- include(used_in_proc_int,reg);
- end;
- procedure trgobj.makeregvarother(reg: tregister);
- begin
- if reg.enum>lastreg then
- internalerror(200301081);
- if reg.enum in intregs then
- internalerror(200301151)
- else if reg.enum in fpuregs then
- begin
- dec(countusableregsfpu);
- dec(countunusedregsfpu);
- exclude(usableregsfpu,reg.enum);
- exclude(unusedregsfpu,reg.enum);
- include(used_in_proc_other,reg.enum);
- end
- else if reg.enum in mmregs then
- begin
- dec(countusableregsmm);
- dec(countunusedregsmm);
- exclude(usableregsmm,reg.enum);
- exclude(unusedregsmm,reg.enum);
- include(used_in_proc_other,reg.enum);
- end;
- is_reg_var_other[reg.enum]:=true;
- end;
- {$ifdef TEMPREGDEBUG}
- procedure trgobj.testregisters;
- var
- r: tregister;
- test : byte;
- begin
- test:=0;
- for r := firstsaveintreg to lastsaveintreg do
- inc(test,ord(r in unusedregsint));
- if test<>countunusedregsint then
- internalerror(10);
- end;
- {$endif TEMPREGDEBUG}
- procedure trgobj.saveStateForInline(var state: pointer);
- begin
- new(psavedstate(state));
- psavedstate(state)^.unusedregsint := unusedregsint;
- psavedstate(state)^.usableregsint := usableregsint;
- psavedstate(state)^.unusedregsfpu := unusedregsfpu;
- psavedstate(state)^.usableregsfpu := usableregsfpu;
- psavedstate(state)^.unusedregsmm := unusedregsmm;
- psavedstate(state)^.usableregsmm := usableregsmm;
- {$ifndef newra}
- psavedstate(state)^.countunusedregsint := countunusedregsint;
- {$endif}
- psavedstate(state)^.countunusedregsfpu := countunusedregsfpu;
- psavedstate(state)^.countunusedregsmm := countunusedregsmm;
- psavedstate(state)^.countusableregsint := countusableregsint;
- psavedstate(state)^.countusableregsfpu := countusableregsfpu;
- psavedstate(state)^.countusableregsmm := countusableregsmm;
- psavedstate(state)^.used_in_proc_int := used_in_proc_int;
- psavedstate(state)^.used_in_proc_other := used_in_proc_other;
- {$ifndef newra}
- psavedstate(state)^.reg_pushes_int := reg_pushes_int;
- {$endif}
- psavedstate(state)^.reg_pushes_other := reg_pushes_other;
- psavedstate(state)^.is_reg_var_int := is_reg_var_int;
- psavedstate(state)^.is_reg_var_other := is_reg_var_other;
- psavedstate(state)^.regvar_loaded_int := regvar_loaded_int;
- psavedstate(state)^.regvar_loaded_other := regvar_loaded_other;
- {$ifdef TEMPREGDEBUG}
- psavedstate(state)^.reg_user := reg_user;
- psavedstate(state)^.reg_releaser := reg_releaser;
- {$endif TEMPREGDEBUG}
- end;
- procedure trgobj.restoreStateAfterInline(var state: pointer);
- begin
- unusedregsint := psavedstate(state)^.unusedregsint;
- usableregsint := psavedstate(state)^.usableregsint;
- unusedregsfpu := psavedstate(state)^.unusedregsfpu;
- usableregsfpu := psavedstate(state)^.usableregsfpu;
- unusedregsmm := psavedstate(state)^.unusedregsmm;
- usableregsmm := psavedstate(state)^.usableregsmm;
- {$ifndef newra}
- countunusedregsint := psavedstate(state)^.countunusedregsint;
- {$endif}
- countunusedregsfpu := psavedstate(state)^.countunusedregsfpu;
- countunusedregsmm := psavedstate(state)^.countunusedregsmm;
- countusableregsint := psavedstate(state)^.countusableregsint;
- countusableregsfpu := psavedstate(state)^.countusableregsfpu;
- countusableregsmm := psavedstate(state)^.countusableregsmm;
- used_in_proc_int := psavedstate(state)^.used_in_proc_int;
- used_in_proc_other := psavedstate(state)^.used_in_proc_other;
- {$ifndef newra}
- reg_pushes_int := psavedstate(state)^.reg_pushes_int;
- {$endif}
- reg_pushes_other := psavedstate(state)^.reg_pushes_other;
- is_reg_var_int := psavedstate(state)^.is_reg_var_int;
- is_reg_var_other := psavedstate(state)^.is_reg_var_other;
- regvar_loaded_other := psavedstate(state)^.regvar_loaded_other;
- regvar_loaded_int := psavedstate(state)^.regvar_loaded_int;
- {$ifdef TEMPREGDEBUG}
- reg_user := psavedstate(state)^.reg_user;
- reg_releaser := psavedstate(state)^.reg_releaser;
- {$endif TEMPREGDEBUG}
- dispose(psavedstate(state));
- state := nil;
- end;
- procedure trgobj.saveUnusedState(var state: pointer);
- begin
- new(punusedstate(state));
- punusedstate(state)^.unusedregsint := unusedregsint;
- punusedstate(state)^.unusedregsfpu := unusedregsfpu;
- punusedstate(state)^.unusedregsmm := unusedregsmm;
- {$ifndef newra}
- punusedstate(state)^.countunusedregsint := countunusedregsint;
- {$endif}
- punusedstate(state)^.countunusedregsfpu := countunusedregsfpu;
- punusedstate(state)^.countunusedregsmm := countunusedregsmm;
- end;
- procedure trgobj.restoreUnusedState(var state: pointer);
- begin
- unusedregsint := punusedstate(state)^.unusedregsint;
- unusedregsfpu := punusedstate(state)^.unusedregsfpu;
- unusedregsmm := punusedstate(state)^.unusedregsmm;
- {$ifndef newra}
- countunusedregsint := punusedstate(state)^.countunusedregsint;
- {$endif}
- countunusedregsfpu := punusedstate(state)^.countunusedregsfpu;
- countunusedregsmm := punusedstate(state)^.countunusedregsmm;
- dispose(punusedstate(state));
- state := nil;
- end;
- {$ifdef newra}
- procedure Trgobj.add_edge(u,v:Tsuperregister);
- {This procedure will add an edge to the virtual interference graph.}
- procedure addadj(u,v:Tsuperregister);
- begin
- if igraph.adjlist[u]=nil then
- begin
- getmem(igraph.adjlist[u],16);
- igraph.adjlist[u]^:='';
- end
- else if (length(igraph.adjlist[u]^) and 15)=15 then
- reallocmem(igraph.adjlist[u],length(igraph.adjlist[u]^)+16);
- igraph.adjlist[u]^:=igraph.adjlist[u]^+char(v);
- end;
- begin
- if (u<>v) and not(v in igraph.bitmap[u]) then
- begin
- include(igraph.bitmap[u],v);
- include(igraph.bitmap[v],u);
- {Precoloured nodes are not stored in the interference graph.}
- if not(u in [first_supreg..last_supreg]) then
- begin
- addadj(u,v);
- inc(degree[u]);
- end;
- if not(v in [first_supreg..last_supreg]) then
- begin
- addadj(v,u);
- inc(degree[v]);
- end;
- end;
- end;
- procedure Trgobj.add_edges_used(u:Tsuperregister);
- var i:Tsuperregister;
- begin
- for i:=1 to maxintreg do
- if not(i in unusedregsint) then
- add_edge(u,i);
- end;
- {$ifdef ra_debug}
- procedure Trgobj.writegraph;
- {This procedure writes out the current interference graph in the
- register allocator.}
- var f:text;
- i,j:Tsuperregister;
- begin
- assign(f,'igraph'+char(48+random(10))+char(48+random(10)));
- rewrite(f);
- writeln(f,'Interference graph');
- writeln(f);
- write(f,' ');
- for i:=0 to 15 do
- for j:=0 to 15 do
- write(f,hexstr(i,1));
- writeln(f);
- write(f,' ');
- for i:=0 to 15 do
- write(f,'0123456789ABCDEF');
- writeln(f);
- for i:=0 to 255 do
- begin
- write(f,hexstr(i,2):4);
- for j:=0 to 255 do
- if j in igraph.bitmap[i] then
- write(f,'*')
- else
- write(f,'-');
- writeln(f);
- end;
- close(f);
- end;
- {$endif}
- procedure Trgobj.add_to_movelist(u:Tsuperregister;data:Tlinkedlistitem);
- begin
- if movelist[u]=nil then
- begin
- getmem(movelist[u],64);
- movelist[u]^.count:=0;
- end
- else if (movelist[u]^.count and 15)=15 then
- reallocmem(movelist[u],(movelist[u]^.count+1)*4+64);
- movelist[u]^.data[movelist[u]^.count]:=data;
- inc(movelist[u]^.count);
- end;
- procedure Trgobj.add_move_instruction(instr:Taicpu);
- {This procedure notifies a certain as a move instruction so the
- register allocator can try to eliminate it.}
- var i:Tmoveins;
- ssupreg,dsupreg:Tsuperregister;
- begin
- i:=Tmoveins.create;
- i.moveset:=ms_worklist_moves;
- i.instruction:=instr;
- worklist_moves.insert(i);
- ssupreg:=instr.oper[0].reg.number shr 8;
- add_to_movelist(ssupreg,i);
- dsupreg:=instr.oper[1].reg.number shr 8;
- if ssupreg<>dsupreg then
- {Avoid adding the same move instruction twice to a single register.}
- add_to_movelist(dsupreg,i);
- i.x:=ssupreg;
- i.y:=dsupreg;
- end;
- function Trgobj.move_related(n:Tsuperregister):boolean;
- var i:cardinal;
- begin
- move_related:=false;
- if movelist[n]<>nil then
- begin
- for i:=0 to movelist[n]^.count-1 do
- if Tmoveins(movelist[n]^.data[i]).moveset in
- [ms_worklist_moves,ms_active_moves] then
- begin
- move_related:=true;
- break;
- end;
- end;
- end;
- procedure Trgobj.make_work_list;
- var n:Tsuperregister;
- begin
- {If we have 7 cpu registers, and the degree of a node is 7, we cannot
- assign it to any of the registers, thus it is significant.}
- for n:=first_imreg to maxintreg do
- if degree[n]>=cpu_registers then
- spillworklist:=spillworklist+char(n)
- else if move_related(n) then
- freezeworklist:=freezeworklist+char(n)
- else
- simplifyworklist:=simplifyworklist+char(n);
- end;
- procedure Trgobj.prepare_colouring;
- begin
- make_work_list;
- active_moves:=Tlinkedlist.create;
- frozen_moves:=Tlinkedlist.create;
- coalesced_moves:=Tlinkedlist.create;
- constrained_moves:=Tlinkedlist.create;
- fillchar(alias,sizeof(alias),0);
- coalescednodes:='';
- selectstack:='';
- end;
- procedure Trgobj.enable_moves(n:Tsuperregister);
- var m:Tlinkedlistitem;
- i:cardinal;
- begin
- if movelist[n]<>nil then
- for i:=0 to movelist[n]^.count-1 do
- begin
- m:=movelist[n]^.data[i];
- if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
- begin
- if Tmoveins(m).moveset=ms_active_moves then
- begin
- {Move m from the set active_moves to the set worklist_moves.}
- active_moves.remove(m);
- Tmoveins(m).moveset:=ms_worklist_moves;
- worklist_moves.concat(m);
- end;
- end;
- end;
- end;
- procedure Trgobj.decrement_degree(m:Tsuperregister);
- var adj:Pstring;
- d:byte;
- i,p:byte;
- n:char;
- begin
- d:=degree[m];
- if degree[m]>0 then
- dec(degree[m]);
- if d=cpu_registers then
- begin
- {Enable moves for m.}
- enable_moves(m);
- {Enable moves for adjacent.}
- adj:=igraph.adjlist[m];
- if adj<>nil then
- for i:=1 to length(adj^) do
- begin
- n:=adj^[i];
- if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
- enable_moves(Tsuperregister(n));
- end;
- {Remove the node from the spillworklist.}
- p:=pos(char(m),spillworklist);
- if p=0 then
- internalerror(200305301); {must be found}
- if length(spillworklist)>1 then
- spillworklist[p]:=spillworklist[length(spillworklist)];
- dec(spillworklist[0]);
- if move_related(m) then
- freezeworklist:=freezeworklist+char(m)
- else
- simplifyworklist:=simplifyworklist+char(m);
- end;
- end;
- procedure Trgobj.simplify;
- var adj:Pstring;
- i,min,p:byte;
- m:char;
- n:Tsuperregister;
- begin
- {We the element with the least interferences out of the
- simplifyworklist.}
- min:=$ff;
- p:=1;
- for i:=1 to length(simplifyworklist) do
- begin
- adj:=igraph.adjlist[Tsuperregister(simplifyworklist[i])];
- if adj=nil then
- begin
- min:=0;
- break; {We won't find smaller ones.}
- end
- else
- if length(adj^)<min then
- begin
- min:=length(adj^);
- if min=0 then
- break; {We won't find smaller ones.}
- p:=i;
- end;
- end;
- n:=Tsuperregister(simplifyworklist[p]);
- delete(simplifyworklist,p,1);
- {Push it on the selectstack.}
- selectstack:=selectstack+char(n);
- adj:=igraph.adjlist[n];
- if adj<>nil then
- for i:=1 to length(adj^) do
- begin
- m:=adj^[i];
- if (pos(m,selectstack) or pos(m,coalescednodes))=0 then
- decrement_degree(Tsuperregister(m));
- end;
- end;
- function Trgobj.get_alias(n:Tsuperregister):Tsuperregister;
- begin
- while pos(char(n),coalescednodes)<>0 do
- n:=alias[n];
- get_alias:=n;
- end;
- procedure Trgobj.add_worklist(u:Tsuperregister);
- begin
- if not(u in [first_supreg..last_supreg]) and not move_related(u) and
- (degree[u]<cpu_registers) then
- begin
- delete(freezeworklist,pos(char(u),freezeworklist),1);
- simplifyworklist:=simplifyworklist+char(u);
- end;
- end;
- function Trgobj.adjacent_ok(u,v:Tsuperregister):boolean;
- {Check wether u and v should be coalesced. u is precoloured.}
- function ok(t,r:Tsuperregister):boolean;
- begin
- ok:=(degree[t]<cpu_registers) or
- (t in [first_supreg..last_supreg]) or
- (r in igraph.bitmap[t]);
- end;
- var adj:Pstring;
- i:byte;
- t:char;
- begin
- adjacent_ok:=true;
- adj:=igraph.adjlist[v];
- if adj<>nil then
- for i:=1 to length(adj^) do
- begin
- t:=adj^[i];
- if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
- if not ok(Tsuperregister(t),u) then
- begin
- adjacent_ok:=false;
- break;
- end;
- end;
- end;
- function Trgobj.conservative(u,v:Tsuperregister):boolean;
- var adj:Pstring;
- done:set of char; {To prevent that we count nodes twice.}
- i,k:byte;
- n:char;
- begin
- k:=0;
- done:=[];
- adj:=igraph.adjlist[u];
- if adj<>nil then
- for i:=1 to length(adj^) do
- begin
- n:=adj^[i];
- if (pos(n,selectstack) or pos(n,coalescednodes))=0 then
- begin
- include(done,n);
- if degree[Tsuperregister(n)]>=cpu_registers then
- inc(k);
- end;
- end;
- adj:=igraph.adjlist[v];
- if adj<>nil then
- for i:=1 to length(adj^) do
- begin
- n:=adj^[i];
- if ((pos(n,selectstack) or pos(n,coalescednodes))=0) and
- not (n in done) and
- (degree[Tsuperregister(n)]>=cpu_registers) then
- inc(k);
- end;
- conservative:=(k<cpu_registers);
- end;
- procedure Trgobj.combine(u,v:Tsuperregister);
- var add:boolean;
- adj:Pstring;
- i,p:byte;
- n,o:cardinal;
- t:char;
- begin
- p:=pos(char(v),freezeworklist);
- if p<>0 then
- delete(freezeworklist,p,1)
- else
- delete(spillworklist,pos(char(v),spillworklist),1);
- coalescednodes:=coalescednodes+char(v);
- alias[v]:=u;
- {Combine both movelists. Since the movelists are sets, only add
- elements that are not already present.}
- for n:=0 to movelist[v]^.count-1 do
- begin
- add:=true;
- for o:=0 to movelist[u]^.count-1 do
- if movelist[u]^.data[o]=movelist[v]^.data[n] then
- begin
- add:=false;
- break;
- end;
- if add then
- add_to_movelist(u,movelist[v]^.data[n]);
- end;
- enable_moves(v);
- adj:=igraph.adjlist[v];
- if adj<>nil then
- for i:=1 to length(adj^) do
- begin
- t:=adj^[i];
- if (pos(t,selectstack) or pos(t,coalescednodes))=0 then
- begin
- decrement_degree(Tsuperregister(t));
- add_edge(Tsuperregister(t),u);
- end;
- end;
- p:=pos(char(u),freezeworklist);
- if (degree[u]>=cpu_registers) and (p<>0) then
- begin
- delete(freezeworklist,p,1);
- spillworklist:=spillworklist+char(u);
- end;
- end;
- procedure Trgobj.coalesce;
- var m:Tmoveins;
- x,y,u,v:Tsuperregister;
- begin
- m:=Tmoveins(worklist_moves.getfirst);
- x:=get_alias(m.instruction.oper[0].reg.number shr 8);
- y:=get_alias(m.instruction.oper[1].reg.number shr 8);
- if y in [first_supreg..last_supreg] then
- begin
- u:=y;
- v:=x;
- end
- else
- begin
- u:=x;
- v:=y;
- end;
- if (u=v) then
- begin
- m.moveset:=ms_coalesced_moves; {Already coalesced.}
- coalesced_moves.insert(m);
- add_worklist(u);
- end
- {Do u and v interfere? In that case the move is constrained. Two
- precoloured nodes interfere allways. If v is precoloured, by the above
- code u is precoloured, thus interference...}
- else if (v in [first_supreg..last_supreg]) or (u in igraph.bitmap[v]) then
- begin
- m.moveset:=ms_constrained_moves; {Cannot coalesce yet...}
- constrained_moves.insert(m);
- add_worklist(u);
- add_worklist(v);
- end
- {Next test: is it possible and a good idea to coalesce??}
- else if ((u in [first_supreg..last_supreg]) and adjacent_ok(u,v)) or
- (not(u in [first_supreg..last_supreg]) and conservative(u,v)) then
- begin
- m.moveset:=ms_coalesced_moves; {Move coalesced!}
- coalesced_moves.insert(m);
- combine(u,v);
- add_worklist(u);
- end
- else
- begin
- m.moveset:=ms_active_moves;
- active_moves.insert(m);
- end;
- end;
- procedure Trgobj.freeze_moves(u:Tsuperregister);
- var i:cardinal;
- m:Tlinkedlistitem;
- v,x,y:Tsuperregister;
- begin
- if movelist[u]<>nil then
- for i:=0 to movelist[u]^.count-1 do
- begin
- m:=movelist[u]^.data[i];
- if Tmoveins(m).moveset in [ms_worklist_moves,ms_active_moves] then
- begin
- x:=Tmoveins(m).instruction.oper[0].reg.number shr 8;
- y:=Tmoveins(m).instruction.oper[1].reg.number shr 8;
- if get_alias(y)=get_alias(u) then
- v:=get_alias(x)
- else
- v:=get_alias(y);
- {Move m from active_moves/worklist_moves to frozen_moves.}
- if Tmoveins(m).moveset=ms_active_moves then
- active_moves.remove(m)
- else
- worklist_moves.remove(m);
- Tmoveins(m).moveset:=ms_frozen_moves;
- frozen_moves.insert(m);
- if not(move_related(v)) and (degree[v]<cpu_registers) then
- begin
- delete(freezeworklist,pos(char(v),freezeworklist),1);
- simplifyworklist:=simplifyworklist+char(v);
- end;
- end;
- end;
- end;
- procedure Trgobj.freeze;
- var n:Tsuperregister;
- begin
- {We need to take a random element out of the freezeworklist. We take
- the last element. Dirty code!}
- n:=Tsuperregister(freezeworklist[byte(freezeworklist[0])]);
- dec(freezeworklist[0]);
- {Add it to the simplifyworklist.}
- simplifyworklist:=simplifyworklist+char(n);
- freeze_moves(n);
- end;
- procedure Trgobj.select_spill;
- var n:char;
- begin
- {This code is WAY too naive. We need not to select just a register, but
- the register that is used the least...}
- n:=spillworklist[byte(spillworklist[0])];
- dec(spillworklist[0]);
- simplifyworklist:=simplifyworklist+n;
- freeze_moves(Tsuperregister(n));
- end;
- procedure Trgobj.assign_colours;
- {Assign_colours assigns the actual colours to the registers.}
- var adj:Pstring;
- i,j,k:byte;
- n,a:Tsuperregister;
- adj_colours,colourednodes:set of Tsuperregister;
- w:char;
- begin
- spillednodes:='';
- {Colour the cpu registers...}
- colourednodes:=[first_supreg..last_supreg];
- for i:=first_supreg to last_supreg do
- colour[i]:=i;
- {Now colour the imaginary registers on the select-stack.}
- for i:=length(selectstack) downto 1 do
- begin
- n:=Tsuperregister(selectstack[i]);
- {Create a list of colours that we cannot assign to n.}
- adj_colours:=[];
- adj:=igraph.adjlist[n];
- if adj<>nil then
- for j:=1 to length(adj^) do
- begin
- w:=adj^[j];
- a:=get_alias(Tsuperregister(w));
- if a in colourednodes then
- include(adj_colours,colour[a]);
- end;
- {Assume a spill by default...}
- spillednodes:=spillednodes+char(n);
- {Search for a colour not in this list.}
- for k:=1 to cpu_registers do
- if not(k in adj_colours) then
- begin
- colour[n]:=k;
- dec(spillednodes[0]); {Colour found: no spill.}
- include(colourednodes,n);
- if n in used_in_proc_int then
- include(used_in_proc_int,k);
- break;
- end;
- end;
- {Finally colour the nodes that were coalesced.}
- for i:=1 to length(coalescednodes) do
- begin
- n:=Tsuperregister(coalescednodes[i]);
- k:=get_alias(n);
- colour[n]:=colour[k];
- if n in used_in_proc_int then
- include(used_in_proc_int,colour[k]);
- end;
- {$ifdef ra_debug}
- for i:=first_imreg to maxintreg do
- writeln(i:4,' ',colour[i]:4)
- {$endif}
- end;
- procedure Trgobj.colour_registers;
- begin
- repeat
- if length(simplifyworklist)<>0 then
- simplify
- else if not(worklist_moves.empty) then
- coalesce
- else if length(freezeworklist)<>0 then
- freeze
- else if length(spillworklist)<>0 then
- select_spill;
- until (length(simplifyworklist) or
- byte(not(worklist_moves.empty)) or
- length(freezeworklist) or
- length(spillworklist)
- )=0;
- assign_colours;
- end;
- procedure Trgobj.epilogue_colouring;
- {
- procedure move_to_worklist_moves(list:Tlinkedlist);
- var p:Tlinkedlistitem;
- begin
- p:=list.first;
- while p<>nil do
- begin
- Tmoveins(p).moveset:=ms_worklist_moves;
- p:=p.next;
- end;
- worklist_moves.concatlist(list);
- end;
- }
- var i:Tsuperregister;
- begin
- worklist_moves.clear;
- {$ifdef Principle_wrong_by_definition}
- {Move everything back to worklist_moves.}
- move_to_worklist_moves(active_moves);
- move_to_worklist_moves(frozen_moves);
- move_to_worklist_moves(coalesced_moves);
- move_to_worklist_moves(constrained_moves);
- {$endif}
- active_moves.destroy;
- active_moves:=nil;
- frozen_moves.destroy;
- frozen_moves:=nil;
- coalesced_moves.destroy;
- coalesced_moves:=nil;
- constrained_moves.destroy;
- constrained_moves:=nil;
- for i:=0 to 255 do
- if movelist[i]<>nil then
- begin
- dispose(movelist[i]);
- movelist[i]:=0;
- end;
- end;
- procedure Trgobj.clear_interferences(u:Tsuperregister);
- {Remove node u from the interference graph and remove all collected
- move instructions it is associated with.}
- var i:byte;
- j,k,count:cardinal;
- v:Tsuperregister;
- m,n:Tmoveins;
- begin
- if igraph.adjlist[u]<>nil then
- begin
- for i:=1 to length(igraph.adjlist[u]^) do
- begin
- v:=Tsuperregister(igraph.adjlist[u]^[i]);
- {Remove (u,v) and (v,u) from bitmap.}
- exclude(igraph.bitmap[u],v);
- exclude(igraph.bitmap[v],u);
- {Remove (v,u) from adjacency list.}
- if igraph.adjlist[v]<>nil then
- begin
- delete(igraph.adjlist[v]^,pos(char(v),igraph.adjlist[v]^),1);
- if length(igraph.adjlist[v]^)=0 then
- begin
- dispose(igraph.adjlist[v]);
- igraph.adjlist[v]:=nil;
- end;
- end;
- end;
- {Remove ( u,* ) from adjacency list.}
- dispose(igraph.adjlist[u]);
- igraph.adjlist[u]:=nil;
- end;
- {$ifdef Principle_wrong_by_definition}
- {Now remove the moves.}
- if movelist[u]<>nil then
- begin
- for j:=0 to movelist[u]^.count-1 do
- begin
- m:=Tmoveins(movelist[u]^.data[j]);
- {Get the other register of the move instruction.}
- v:=m.instruction.oper[0].reg.number shr 8;
- if v=u then
- v:=m.instruction.oper[1].reg.number shr 8;
- repeat
- repeat
- if (u<>v) and (movelist[v]<>nil) then
- begin
- {Remove the move from it's movelist.}
- count:=movelist[v]^.count-1;
- for k:=0 to count do
- if m=movelist[v]^.data[k] then
- begin
- if k<>count then
- movelist[v]^.data[k]:=movelist[v]^.data[count];
- dec(movelist[v]^.count);
- if count=0 then
- begin
- dispose(movelist[v]);
- movelist[v]:=nil;
- end;
- break;
- end;
- end;
- {The complexity is enourmous: the register might have been
- coalesced. In that case it's movelists have been added to
- it's coalescing alias. (DM)}
- v:=alias[v];
- until v=0;
- {And also register u might have been coalesced.}
- u:=alias[u];
- until u=0;
- case m.moveset of
- ms_coalesced_moves:
- coalesced_moves.remove(m);
- ms_constrained_moves:
- constrained_moves.remove(m);
- ms_frozen_moves:
- frozen_moves.remove(m);
- ms_worklist_moves:
- worklist_moves.remove(m);
- ms_active_moves:
- active_moves.remove(m);
- end;
- end;
- dispose(movelist[u]);
- movelist[u]:=nil;
- end;
- {$endif}
- end;
- procedure Trgobj.getregisterintinline(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister);
- var i:Tsuperregister;
- r:Tregister;
- begin
- if not (lastintreg in [first_imreg..last_imreg]) then
- lastintreg:=first_imreg;
- i:=lastintreg;
- repeat
- if i=last_imreg then
- i:=first_imreg
- else
- inc(i);
- if (i in unusedregsint) and (pos(char(i),abtlist)=0) then
- begin
- exclude(unusedregsint,i);
- include(used_in_proc_int,i);
- r.enum:=R_INTREGISTER;
- r.number:=i shl 8 or subreg;
- if position=nil then
- list.insert(Tai_regalloc.alloc(r))
- else
- list.insertafter(Tai_regalloc.alloc(r),position);
- result:=r;
- lastintreg:=i;
- if i>maxintreg then
- maxintreg:=i;
- add_edges_used(i);
- add_constraints(result.number);
- exit;
- end;
- until i=lastintreg;
- internalerror(10);
- end;
- {In some cases we can get in big trouble. See this example:
- ; register reg23d released
- ; register eax allocated
- ; register ebx allocated
- ; register ecx allocated
- ; register edx allocated
- ; register esi allocated
- ; register edi allocated
- call [reg23d]
- This code is ok, *except* when reg23d is spilled. In that case the
- spilled would introduce a help register which can never get
- allocated to a real register because it interferes with all of them.
- To solve this we introduce the ABT ("avoid big trouble :)" ) registers.
- If you allocate an ABT register you get a register that has less
- than cpu_register interferences and will not be allocated ever again
- by the normal register get procedures. In other words it is for sure it
- will never get spilled.}
- function Trgobj.getabtregisterint(list:Taasmoutput;size:Tcgsize):Tregister;
- var i:Tsuperregister;
- r:Tregister;
- found:boolean;
- begin
- if not (lastintreg in [first_imreg..last_imreg]) then
- lastintreg:=first_imreg;
- found:=false;
- for i:=1 to length(abtlist) do
- if Tsuperregister(abtlist[i]) in unusedregsint then
- begin
- found:=true;
- break;
- end;
- i:=lastintreg;
- repeat
- if i=last_imreg then
- i:=first_imreg
- else
- inc(i);
- if (i in unusedregsint) and ((igraph.adjlist[i]=nil) or (length(igraph.adjlist[i]^)<cpu_registers)) then
- begin
- found:=true;
- break;
- end;
- until i=lastintreg;
- if found then
- begin
- exclude(unusedregsint,i);
- include(used_in_proc_int,i);
- r.enum:=R_INTREGISTER;
- r.number:=i shl 8 or cgsize2subreg(size);
- list.concat(Tai_regalloc.alloc(r));
- getabtregisterint:=r;
- lastintreg:=i;
- if i>maxintreg then
- maxintreg:=i;
- add_edges_used(i);
- if pos(char(i),abtlist)=0 then
- abtlist:=abtlist+char(i);
- end
- else
- internalerror(10);
- {$ifdef newra}
- add_constraints(getabtregisterint.number);
- {$endif}
- end;
- procedure Trgobj.ungetregisterintinline(list:Taasmoutput;position:Tai;const r:Tregister);
- var supreg:Tsuperregister;
- begin
- if r.enum<=lastreg then
- internalerror(2003010803);
- supreg:=r.number shr 8;
- { takes much time }
- include(unusedregsint,supreg);
- if position=nil then
- list.insert(Tai_regalloc.dealloc(r))
- else
- list.insertafter(Tai_regalloc.dealloc(r),position);
- add_edges_used(supreg);
- end;
- function Trgobj.spill_registers(list:Taasmoutput;const regs_to_spill:string):boolean;
- {Returns true if any help registers have been used.}
- var i:byte;
- r:Tsuperregister;
- p,q:Tai;
- regs_to_spill_set:Tsupregset;
- spill_temps:^Tspill_temp_list;
- begin
- spill_registers:=false;
- unusedregsint:=[0..255];
- fillchar(degree,sizeof(degree),0);
- if current_procinfo.framepointer.number=NR_FRAME_POINTER_REG then
- {Make sure the register allocator won't allocate registers into ebp.}
- exclude(rg.unusedregsint,RS_FRAME_POINTER_REG);
- new(spill_temps);
- fillchar(spill_temps^,sizeof(spill_temps^),0);
- regs_to_spill_set:=[];
- for i:=1 to length(regs_to_spill) do
- begin
- {Alternative representation.}
- include(regs_to_spill_set,Tsuperregister(regs_to_spill[i]));
- {Clear all interferences of the spilled register.}
- clear_interferences(Tsuperregister(regs_to_spill[i]));
- {Get a temp for the spilled register.}
- tg.gettemp(list,4,tt_noreuse,spill_temps^[Tsuperregister(regs_to_spill[i])]);
- end;
- p:=Tai(list.first);
- while assigned(p) do
- begin
- case p.typ of
- ait_regalloc:
- begin
- {A register allocation of a spilled register can be removed.}
- if (Tai_regalloc(p).reg.number shr 8) in regs_to_spill_set then
- begin
- q:=p;
- p:=Tai(p.next);
- list.remove(q);
- continue;
- end
- else
- if Tai_regalloc(p).allocation then
- exclude(unusedregsint,Tai_regalloc(p).reg.number shr 8)
- else
- include(unusedregsint,Tai_regalloc(p).reg.number shr 8);
- end;
- ait_instruction:
- begin
- if Taicpu_abstract(p).spill_registers(list,@getregisterintinline,
- @ungetregisterintinline,
- regs_to_spill_set,
- unusedregsint,
- spill_temps^) then
- spill_registers:=true;
- if Taicpu_abstract(p).is_move then
- add_move_instruction(Taicpu(p));
- end;
- end;
- p:=Tai(p.next);
- end;
- for i:=1 to length(regs_to_spill) do
- begin
- tg.ungettemp(list,spill_temps^[Tsuperregister(regs_to_spill[i])]);
- end;
- dispose(spill_temps);
- end;
- {$endif newra}
- {****************************************************************************
- TReference
- ****************************************************************************}
- procedure reference_reset(var ref : treference);
- begin
- FillChar(ref,sizeof(treference),0);
- ref.base.enum:=R_INTREGISTER;
- ref.index.enum:=R_INTREGISTER;
- {$ifdef i386}
- ref.segment.enum:=R_INTREGISTER;
- {$endif}
- end;
- procedure reference_reset_old(var ref : treference);
- begin
- FillChar(ref,sizeof(treference),0);
- end;
- procedure reference_reset_base(var ref : treference;base : tregister;offset : longint);
- begin
- reference_reset(ref);
- ref.base:=base;
- ref.offset:=offset;
- end;
- procedure reference_reset_symbol(var ref : treference;sym : tasmsymbol;offset : longint);
- begin
- reference_reset(ref);
- ref.symbol:=sym;
- ref.offset:=offset;
- end;
- procedure reference_release(list: taasmoutput; const ref : treference);
- begin
- rg.ungetreference(list,ref);
- end;
- function references_equal(sref : treference;dref : treference):boolean;
- begin
- references_equal:=CompareByte(sref,dref,sizeof(treference))=0;
- end;
- { on most processors , this routine does nothing, overriden currently }
- { only by 80x86 processor. }
- function trgobj.makeregsize(reg: tregister; size: tcgsize): tregister;
- begin
- makeregsize := reg;
- end;
- {****************************************************************************
- TLocation
- ****************************************************************************}
- procedure location_reset(var l : tlocation;lt:TCGLoc;lsize:TCGSize);
- begin
- FillChar(l,sizeof(tlocation),0);
- l.loc:=lt;
- l.size:=lsize;
- case l.loc of
- LOC_REGISTER,LOC_CREGISTER:
- begin
- l.register.enum:=R_INTREGISTER;
- l.registerhigh.enum:=R_INTREGISTER;
- end;
- LOC_REFERENCE,LOC_CREFERENCE:
- begin
- l.reference.base.enum:=R_INTREGISTER;
- l.reference.index.enum:=R_INTREGISTER;
- {$ifdef i386}
- l.reference.segment.enum:=R_INTREGISTER;
- {$endif}
- end;
- end;
- end;
- procedure location_release(list: taasmoutput; const l : tlocation);
- begin
- case l.loc of
- LOC_REGISTER,LOC_CREGISTER :
- begin
- rg.ungetregisterint(list,l.register);
- if l.size in [OS_64,OS_S64] then
- rg.ungetregisterint(list,l.registerhigh);
- end;
- LOC_FPUREGISTER,LOC_CFPUREGISTER :
- rg.ungetregisterfpu(list,l.register,l.size);
- LOC_CREFERENCE,LOC_REFERENCE :
- rg.ungetreference(list, l.reference);
- end;
- end;
- procedure location_freetemp(list:taasmoutput; const l : tlocation);
- begin
- if (l.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
- tg.ungetiftemp(list,l.reference);
- end;
- procedure location_copy(var destloc:tlocation; const sourceloc : tlocation);
- begin
- destloc:=sourceloc;
- end;
- procedure location_swap(var destloc,sourceloc : tlocation);
- var
- swapl : tlocation;
- begin
- swapl := destloc;
- destloc := sourceloc;
- sourceloc := swapl;
- end;
- initialization
- ;
- finalization
- rg.free;
- end.
- {
- $Log$
- Revision 1.54 2003-06-13 21:19:31 peter
- * current_procdef removed, use current_procinfo.procdef instead
- Revision 1.53 2003/06/12 21:11:10 peter
- * ungetregisterfpu gets size parameter
- Revision 1.52 2003/06/12 16:43:07 peter
- * newra compiles for sparc
- Revision 1.51 2003/06/09 14:54:26 jonas
- * (de)allocation of registers for parameters is now performed properly
- (and checked on the ppc)
- - removed obsolete allocation of all parameter registers at the start
- of a procedure (and deallocation at the end)
- Revision 1.50 2003/06/03 21:11:09 peter
- * cg.a_load_* get a from and to size specifier
- * makeregsize only accepts newregister
- * i386 uses generic tcgnotnode,tcgunaryminus
- Revision 1.49 2003/06/03 13:01:59 daniel
- * Register allocator finished
- Revision 1.48 2003/06/01 21:38:06 peter
- * getregisterfpu size parameter added
- * op_const_reg size parameter added
- * sparc updates
- Revision 1.47 2003/05/31 20:31:11 jonas
- * set inital costs of assigning a variable to a register to 120 for
- non-i386, because the used register must be store to memory at the
- start and loaded again at the end
- Revision 1.46 2003/05/30 18:55:21 jonas
- * fixed several regvar related bugs for non-i386. make cycle with -Or now
- works for ppc
- Revision 1.45 2003/05/30 12:36:13 jonas
- * use as little different registers on the ppc until newra is released,
- since every used register must be saved
- Revision 1.44 2003/05/17 13:30:08 jonas
- * changed tt_persistant to tt_persistent :)
- * tempcreatenode now doesn't accept a boolean anymore for persistent
- temps, but a ttemptype, so you can also create ansistring temps etc
- Revision 1.43 2003/05/16 14:33:31 peter
- * regvar fixes
- Revision 1.42 2003/04/26 20:03:49 daniel
- * Bug fix in simplify
- Revision 1.41 2003/04/25 20:59:35 peter
- * removed funcretn,funcretsym, function result is now in varsym
- and aliases for result and function name are added using absolutesym
- * vs_hidden parameter for funcret passed in parameter
- * vs_hidden fixes
- * writenode changed to printnode and released from extdebug
- * -vp option added to generate a tree.log with the nodetree
- * nicer printnode for statements, callnode
- Revision 1.40 2003/04/25 08:25:26 daniel
- * Ifdefs around a lot of calls to cleartempgen
- * Fixed registers that are allocated but not freed in several nodes
- * Tweak to register allocator to cause less spills
- * 8-bit registers now interfere with esi,edi and ebp
- Compiler can now compile rtl successfully when using new register
- allocator
- Revision 1.39 2003/04/23 20:23:06 peter
- * compile fix for no-newra
- Revision 1.38 2003/04/23 14:42:07 daniel
- * Further register allocator work. Compiler now smaller with new
- allocator than without.
- * Somebody forgot to adjust ppu version number
- Revision 1.37 2003/04/22 23:50:23 peter
- * firstpass uses expectloc
- * checks if there are differences between the expectloc and
- location.loc from secondpass in EXTDEBUG
- Revision 1.36 2003/04/22 10:09:35 daniel
- + Implemented the actual register allocator
- + Scratch registers unavailable when new register allocator used
- + maybe_save/maybe_restore unavailable when new register allocator used
- Revision 1.35 2003/04/21 19:16:49 peter
- * count address regs separate
- Revision 1.34 2003/04/17 16:48:21 daniel
- * Added some code to keep track of move instructions in register
- allocator
- Revision 1.33 2003/04/17 07:50:24 daniel
- * Some work on interference graph construction
- Revision 1.32 2003/03/28 19:16:57 peter
- * generic constructor working for i386
- * remove fixed self register
- * esi added as address register for i386
- Revision 1.31 2003/03/11 21:46:24 jonas
- * lots of new regallocator fixes, both in generic and ppc-specific code
- (ppc compiler still can't compile the linux system unit though)
- Revision 1.30 2003/03/09 21:18:59 olle
- + added cutils to the uses clause
- Revision 1.29 2003/03/08 20:36:41 daniel
- + Added newra version of Ti386shlshrnode
- + Added interference graph construction code
- Revision 1.28 2003/03/08 13:59:16 daniel
- * Work to handle new register notation in ag386nsm
- + Added newra version of Ti386moddivnode
- Revision 1.27 2003/03/08 10:53:48 daniel
- * Created newra version of secondmul in n386add.pas
- Revision 1.26 2003/03/08 08:59:07 daniel
- + $define newra will enable new register allocator
- + getregisterint will return imaginary registers with $newra
- + -sr switch added, will skip register allocation so you can see
- the direct output of the code generator before register allocation
- Revision 1.25 2003/02/26 20:50:45 daniel
- * Fixed ungetreference
- Revision 1.24 2003/02/19 22:39:56 daniel
- * Fixed a few issues
- Revision 1.23 2003/02/19 22:00:14 daniel
- * Code generator converted to new register notation
- - Horribily outdated todo.txt removed
- Revision 1.22 2003/02/02 19:25:54 carl
- * Several bugfixes for m68k target (register alloc., opcode emission)
- + VIS target
- + Generic add more complete (still not verified)
- Revision 1.21 2003/01/08 18:43:57 daniel
- * Tregister changed into a record
- Revision 1.20 2002/10/05 12:43:28 carl
- * fixes for Delphi 6 compilation
- (warning : Some features do not work under Delphi)
- Revision 1.19 2002/08/23 16:14:49 peter
- * tempgen cleanup
- * tt_noreuse temp type added that will be used in genentrycode
- Revision 1.18 2002/08/17 22:09:47 florian
- * result type handling in tcgcal.pass_2 overhauled
- * better tnode.dowrite
- * some ppc stuff fixed
- Revision 1.17 2002/08/17 09:23:42 florian
- * first part of procinfo rewrite
- Revision 1.16 2002/08/06 20:55:23 florian
- * first part of ppc calling conventions fix
- Revision 1.15 2002/08/05 18:27:48 carl
- + more more more documentation
- + first version include/exclude (can't test though, not enough scratch for i386 :()...
- Revision 1.14 2002/08/04 19:06:41 carl
- + added generic exception support (still does not work!)
- + more documentation
- Revision 1.13 2002/07/07 09:52:32 florian
- * powerpc target fixed, very simple units can be compiled
- * some basic stuff for better callparanode handling, far from being finished
- Revision 1.12 2002/07/01 18:46:26 peter
- * internal linker
- * reorganized aasm layer
- Revision 1.11 2002/05/18 13:34:17 peter
- * readded missing revisions
- Revision 1.10 2002/05/16 19:46:44 carl
- + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
- + try to fix temp allocation (still in ifdef)
- + generic constructor calls
- + start of tassembler / tmodulebase class cleanup
- Revision 1.8 2002/04/21 15:23:03 carl
- + makeregsize
- + changeregsize is now a local routine
- Revision 1.7 2002/04/20 21:32:25 carl
- + generic FPC_CHECKPOINTER
- + first parameter offset in stack now portable
- * rename some constants
- + move some cpu stuff to other units
- - remove unused constents
- * fix stacksize for some targets
- * fix generic size problems which depend now on EXTEND_SIZE constant
- Revision 1.6 2002/04/15 19:03:31 carl
- + reg2str -> std_reg2str()
- Revision 1.5 2002/04/06 18:13:01 jonas
- * several powerpc-related additions and fixes
- Revision 1.4 2002/04/04 19:06:04 peter
- * removed unused units
- * use tlocation.size in cg.a_*loc*() routines
- Revision 1.3 2002/04/02 17:11:29 peter
- * tlocation,treference update
- * LOC_CONSTANT added for better constant handling
- * secondadd splitted in multiple routines
- * location_force_reg added for loading a location to a register
- of a specified size
- * secondassignment parses now first the right and then the left node
- (this is compatible with Kylix). This saves a lot of push/pop especially
- with string operations
- * adapted some routines to use the new cg methods
- Revision 1.2 2002/04/01 19:24:25 jonas
- * fixed different parameter name in interface and implementation
- declaration of a method (only 1.0.x detected this)
- Revision 1.1 2002/03/31 20:26:36 jonas
- + a_loadfpu_* and a_loadmm_* methods in tcg
- * register allocation is now handled by a class and is mostly processor
- independent (+rgobj.pas and i386/rgcpu.pas)
- * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
- * some small improvements and fixes to the optimizer
- * some register allocation fixes
- * some fpuvaroffset fixes in the unary minus node
- * push/popusedregisters is now called rg.save/restoreusedregisters and
- (for i386) uses temps instead of push/pop's when using -Op3 (that code is
- also better optimizable)
- * fixed and optimized register saving/restoring for new/dispose nodes
- * LOC_FPU locations now also require their "register" field to be set to
- R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
- - list field removed of the tnode class because it's not used currently
- and can cause hard-to-find bugs
- }
|