13.07.2015 Views

The Power of Belady's Algorithm in Register Allocation for Long ...

The Power of Belady's Algorithm in Register Allocation for Long ...

The Power of Belady's Algorithm in Register Allocation for Long ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

quently. In the next section, we expla<strong>in</strong> the compiler we designedto per<strong>for</strong>m the register allocation on the FFT and MM codes.3 A Compiler <strong>for</strong> <strong>Long</strong> Straight-l<strong>in</strong>e CodeBelady’s MIN [2] is a replacement algorithm <strong>for</strong> pages <strong>in</strong> virtualmemory. On a replacement, the page to replace is the one withthe farthest next use. <strong>The</strong> MIN algorithm is optimal because itgenerates the m<strong>in</strong>imal number <strong>of</strong> physical memory block replacements.However, <strong>in</strong> this context <strong>of</strong> virtual memory the MIN algorithmis impractical <strong>in</strong> most <strong>of</strong> the applications, because it isusually not known which memory block will be referenced <strong>in</strong> thefuture.Belady’s MIN algorithm has been proposed to use <strong>for</strong> registerallocation <strong>in</strong> long basic blocks, where the compiler knows exactlythe values that will be used <strong>in</strong> the future. In this context, the MINalgorithm is also known as Farthest First(FF) [6] s<strong>in</strong>ce, on a registerreplacement, the register to replace first is the one hold<strong>in</strong>g thevalue with the farthest next use.<strong>The</strong> MIN algorithm is not optimal <strong>for</strong> register allocation s<strong>in</strong>cethe replacement decision is simply based on the distance and noton whether the register has been modified. When a register holdsa value that is not consistent with the value <strong>in</strong> memory, we say thatthe register is dirty. Otherwise, we say that the register is clean. Ifthe register to be replaced is dirty, the value <strong>of</strong> the register needs tobe stored back to memory be<strong>for</strong>e a new value can be loaded <strong>in</strong>to it.Thus, <strong>for</strong> a given <strong>in</strong>struction schedul<strong>in</strong>g the MIN algorithm guaranteesthe m<strong>in</strong>imum number <strong>of</strong> register replacements, but it doesnot guarantee the m<strong>in</strong>imum traffic with memory, that is, the m<strong>in</strong>imumnumber <strong>of</strong> load/stores. In our implementation, when thereare several candidates <strong>for</strong> replacement with the same distance, ourcompiler chooses the one with the clean state to avoid an extrastore.In order to further reduce the number <strong>of</strong> stores, another simpleheuristic called Clean First (CF) was proposed <strong>in</strong> [6]. Withthis heuristic, when a live register needs to be replaced, CF firstsearches <strong>in</strong> the clean registers. <strong>The</strong> clean register which conta<strong>in</strong>sthe value with the farthest next use is chosen. If there are no cleanregister, the most distant dirty one is chosen.We implemented a back-end compiler that uses the MIN andthe CF algorithms <strong>for</strong> register allocation. Next, we describe theimplementation details <strong>of</strong> our compiler.3.1 Implementations DetailsWe built a simple compiler that translates high-level code <strong>in</strong>toassembly code. Our compiler assumes that all the optimizations,<strong>in</strong>clud<strong>in</strong>g <strong>in</strong>struction schedul<strong>in</strong>g, have been applied be<strong>for</strong>e thehigh-level code is generated. It only per<strong>for</strong>ms register allocationus<strong>in</strong>g Belady’s MIN or the CF heuristic expla<strong>in</strong>ed above.<strong>The</strong> compiler has two steps. In the first step we trans<strong>for</strong>m thelong straight-l<strong>in</strong>e code <strong>in</strong>to a static s<strong>in</strong>gle-assignment (SSA) <strong>for</strong>mand build the def<strong>in</strong>ition-use cha<strong>in</strong> <strong>for</strong> all the variables. At the secondstep we do register allocation as shown <strong>in</strong> Figure 2. <strong>The</strong> secondstep is fairly much along the l<strong>in</strong>es <strong>of</strong> the simple algorithmdescribed <strong>in</strong> [1].<strong>The</strong> algorithm can be implemented more efficiently. <strong>The</strong> registerfile can be a priority queue implemented with a b<strong>in</strong>ary heap,where the higher priority is given to the farther next use. Operationssuch as extract<strong>in</strong>g the register with the farthest next use canbe executed <strong>in</strong> Ç´ÐÓʵ, where R is the number <strong>of</strong> registers. Sothe time complexity <strong>for</strong> MIN algorithm is × ¢ Ç´ÐÓʵ, where ×is the number <strong>of</strong> references to the variables <strong>in</strong> the program.DATA STRUCTURES:register file:Array <strong>of</strong> registers. Each register r has 3 fields:- state: Indicates whether the register is clean.- var: <strong>The</strong> current variable <strong>in</strong> the register.- addr: <strong>The</strong> address <strong>of</strong> the variable <strong>in</strong> the register.next use list:List used to keep the stmt where each variable is used/def<strong>in</strong>ed.Each node <strong>in</strong> the list has 2 fields:- stmt: <strong>The</strong> statement number.- status: Def<strong>in</strong>ition or use.FUNCTIONS:NEXT USE (reg, stmt):Returns the statement number where reg.var is used next after stmt.MIN REG ALLOC (var, stmt):Returns the register that is go<strong>in</strong>g to be used <strong>for</strong> the variable var at stmt.MAIN<strong>for</strong> each stmt <strong>in</strong> the program do beg<strong>in</strong><strong>for</strong> each v on RHS do beg<strong>in</strong>reg MIN REG ALLOC (v, stmt)generate <strong>in</strong>struction ”load reg, reg.addr”end<strong>for</strong> v on LHS do beg<strong>in</strong>reg MIN REG ALLOC (v, stmt)endendgenerate assembly <strong>in</strong>struction “op r1 r2 r3”MIN REG ALLOC (v, stmt)if vis<strong>in</strong>regreturn regfarthestUse 0regToUse 0<strong>for</strong> each reg <strong>in</strong> register file do beg<strong>in</strong>if reg is emptyreturn regif variable <strong>in</strong> reg is deadreturn regnextUse NEXT USE (reg, stmt)if farthestUse nextUsefarthestUse nextUseregToUse regelse if farthestUse=nextUse and reg.state = CLEANregToUse regendif regToUse.state = DIRTYgenerate register spill “store regToUse, regToUse.addr”return regToUseFigure 2. Pseudocode <strong>for</strong> Belady’s MIN algorithm.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!