Introduction to Linear Scan Register Allocation Algorithm

Line scan register allocation

  • Paper address:

Linear Scan Register Allocation

​ We describe a new algorithm for fast global register allocation called linear scan. The algorithm is not based on graph coloring, but allocates registers to variables in a single linear time scan of the variable's lifetime. Line scan algorithms are much faster and easier to implement than graph shading-based algorithms, and the generated code is nearly as efficient as that obtained using the more complex and time-consuming register allocators based on graph shading. The algorithm is suitable forapplicationswhere compile time is a concern, such as dynamic compilation systems, "just-in-time" compilers, and interactive development environments.

The fundamental reason for not using the graph coloring algorithm is that it requiresto construct a complete interference pattern before it can color the graph, and interference Graph construction is too expensive. It should be said that most of the overhead in the graph shading process is concentrated in the construction phase of the interference map. Therefore, although the code generated by graph coloring is of high quality, its time cost cannot be ignored for modern compilers that pay attention to compilation efficiency. This is especially true for JIT compilers that are more sensitive to compile time. In addition, during the actual compilation work, people discovered another problem. Since the number of registers is limited in all current hardware architectures, for large programs, it is inevitable that there are not enough registers. In other words, large programs are bound to generate a lot of overflows. The problem arises here, becausethe graph coloring algorithm focuses on solving "how to allocate all program variables to registers as much as possible", if The program will definitely generate a large number of overflows, so it is more valuable to focus on "how to overflow efficiently" than on "how to minimize overflows".

Spill: Overflow means that the current registers are not enough, and this part of the data needs to be cached and coordinated with the help of sram or stack area space.

The number of registers is limited, so when too many variables are used in the program or a large number of temporary calculation results are generated, the compiler may run out of registers, causing register overflow. This may cause the compiler to store certain variables or data in memory instead of registers, reducing program execution efficiency.

1. Algorithm introduction

The linear scan register allocation algorithm is an algorithm used to allocate registers in the compiler. It is designed to map variables in the program to the computer's registers to improve the execution efficiency of the program. This algorithm is typically used in the code generation phase of a compiler to generate object code.

The basic idea of ​​the linear scan register allocation algorithm is to allocate registers through a linear scan, thereby allocating registers to variables at different locations in the program. This algorithm does not require complex data flow analysis and is therefore relatively fast and simple. The following are the main steps of the linear scan register allocation algorithm:

  1. Construct the active variable range (Live Range): First, calculate the live range of each variable through data flow analysis, that is, the range in which the variable is used in the program. The active range is usually the lifetime of the variable in the program.
  2. Sort active variable intervals: Sort all active variable intervals according to their end positions from small to large.
  3. Traverse active variable intervals: From the sorted list of active variable intervals, traverse each interval in order. During the traversal, a usable register is allocated for each range, and previously allocated registers are released when needed.
  4. Determine overflow: If there are not enough available registers in a certain interval, an overflow occurs, and the algorithm will try to move the value of a certain register into memory to make room for allocation to new variables.

The advantage of the linear scan register allocation algorithm is that it is simple and easy to implement, and is suitable for medium-scale code generation. However, it may not be as good as other more complex register allocation algorithms (such as shading) which can achieve higher performance in some cases. Different compilers may use different register allocation algorithms to optimize based on specific needs and target platforms.

2. Related concepts

live interval:life interval
live range:life cycle

interference:相交(conflict)

active list: A collection of nodes that have allocated physical memory

3. Implementation of algorithm

3.1 Pseudocode

image-20230814221420865

3.2 Illustration

image-20230814221317558

image-20230814221239700

references

[1] Register Allocation in LLVM 3.0
[2] Hacker News - Register Allocation
[3] Linear Scan Register Allocation
[4] Register Allocation
[5] 博客圆-线性扫描寄存器分配

Guess you like

Origin blog.csdn.net/sexyluna/article/details/132287291