A register allocation method
By constructing an active range table containing dead interval information through reverse traversal of the control flow graph, the problem of low register utilization in traditional methods is solved, register reuse and optimized allocation are realized, and program performance is improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- WUHAN LINGJIU MICROELECTRONICS CO LTD
- Filing Date
- 2026-03-03
- Publication Date
- 2026-06-02
AI Technical Summary
Traditional linear scan register allocation algorithms fail to identify dead sub-intervals within the active range of variables, resulting in low register utilization, wasted resources, frequent overflows, and impact on program performance.
A reverse traversal of the control flow graph is used to construct an active range table containing dead interval information. By identifying the dead intervals of variables, register reuse and optimized allocation strategies are implemented, including constructing an active linked list, an idle register pool, and a register set. Short-lived variables are reused using dead intervals, and weight calculation is improved to select overflow objects.
It improves register utilization, reduces register overflow, enhances program execution performance, and significantly improves the efficiency of register allocation and resource utilization.
Smart Images

Figure CN121764478B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of compiler optimization technology, and more specifically, to a register allocation method. Background Technology
[0002] During compiler register allocation, the traditional Linear Scan Register Allocation algorithm allocates registers based on the live range of a variable. The live range of a variable is typically defined as the instruction interval from its first definition to its last use.
[0003] However, traditional methods have the following key problems, which are discussed below. Figure 1 To explain:
[0004] Question 1: Coarse-grained representation of active range:
[0005] Traditional methods simply represent the active range as a continuous interval. The "death" subintervals that may exist within this interval are not considered.
[0006] Active range: Traditional methods consider it to be .
[0007] Actual situation: In the instruction Within the interval (BB2 basic block), It was not actually used.
[0008] Dead zone: that is Dead zones within the active range.
[0009] Waste: in period, The registered registers in use are actually idle.
[0010] Problem 2: Low register utilization:
[0011] Because traditional methods cannot identify dead regions, the following results occur:
[0012] 1. The registers are unnecessarily occupied for a long time.
[0013] For example, the above exist The register was occupied but not used during the period.
[0014] 2. Short-lived variables cannot reuse idle registers.
[0015] For example active range Completely dead zone Inside. But traditional methods believe and Conflict, registers cannot be shared.
[0016] 3. More physical registers are needed.
[0017] The above example requires 3 registers ( , , Each occupies one). If the dead interval can be reused, only 2 registers are needed ( , ).
[0018] Question 3: Frequent overflows impact performance.
[0019] When register resources are insufficient, traditional methods frequently cause variables to overflow into memory because they cannot fully utilize registers. Since memory access latency is more than 100 times that of register access, this severely degrades program execution performance. Summary of the Invention
[0020] This invention addresses the technical problems existing in the prior art by providing a register allocation method that can overcome the problem of wasted idle resources in existing register allocation methods.
[0021] This invention provides a register allocation method, comprising:
[0022] Step S1: Construct the active range of each variable containing dead interval information by reverse traversing the control flow graph CFG and the basic blocks in the control flow graph.
[0023] Step S2: Sort the active ranges of all variables according to their starting positions to obtain an ordered sequence of active ranges. ,in, Indicates the first The active range of each variable;
[0024] Step S3, based on the ordered active range sequence Construct the key data structure = {active linked list} Free register pool Register set }, where the active linked list Indicates at time The set of active ranges of occupied physical registers, and the free register pool. Indicates at time Available physical register set, register set Represents all available physical registers;
[0025] Step S4, from the ordered active range sequence Each active range is retrieved sequentially. For the current active range, the method to release expired active ranges is called to release the expired active ranges.
[0026] Step S5: For the current variable to be allocated, check if the free register pool is empty. If the free register pool is not empty, proceed to step S6; otherwise, proceed to step S7.
[0027] Step S6: Retrieve a register from the free register pool. Assign it to the current variable to be assigned, add the active scope of the current variable to be assigned to the active list, and continue to process the next variable to be assigned;
[0028] Step S7: If the free register pool is empty, attempt to reuse the dead interval. If a reusable register is found in the active list, proceed to step S8; otherwise, proceed to step S9.
[0029] Step S8: Allocate the reusable register to the currently unassigned variable;
[0030] Step S9: Overflow the variable with the largest weight in the active linked list, release the physical register occupied by that variable, and then... Assign the variable to the current variable to be assigned, and continue processing the next variable to be assigned.
[0031] The present invention provides a register allocation method that identifies dead sub-intervals within the active range of each variable in the control flow graph, enabling short-lived variables to reuse registers that are idle during the dead intervals of long-lived variables, accommodating more variables with the same number of physical registers, and maximizing the utilization of idle resources. Attached Figure Description
[0032] Figure 1 This is a schematic diagram comparing the register allocation method of the present invention with the traditional register allocation method;
[0033] Figure 2 A flowchart of a register allocation method provided in one embodiment of the present invention;
[0034] Figure 3 This is an overall flowchart of a register allocation method provided in one embodiment of the present invention;
[0035] Figure 4 A flowchart for constructing an active range table for each variable that includes dead interval information;
[0036] Figure 5 A flowchart for finding a reusable register in the active list;
[0037] Figure 6 A schematic diagram illustrating the state analysis of register allocation for the assembler;
[0038] Figure 7 A schematic diagram illustrating the comparison and analysis of variable lifetime and dead interval;
[0039] Figure 8 A diagram showing the comparison before and after register allocation optimization. Detailed Implementation
[0040] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention. In addition, the technical features of the various embodiments or individual embodiments provided by the present invention can be arbitrarily combined with each other to form feasible technical solutions. Such combinations are not constrained by the order of steps and / or structural composition patterns, but must be based on the ability of those skilled in the art to implement them. When the combination of technical solutions is contradictory or cannot be implemented, it should be considered that such a combination of technical solutions does not exist and is not within the scope of protection claimed by the present invention.
[0041] To address the shortcomings in the background technology, this invention provides a register allocation method, mainly divided into two parts. The first part focuses on constructing an active range table containing dead interval information. This part employs an innovative method of reverse scanning the control flow graph, dynamically tracking the active and dead state transitions of variables by traversing basic blocks and instructions from back to front. When a variable is detected to transition from a dead state to an active state, the corresponding dead interval node is automatically identified and created, and inserted into the dead interval linked list of the active range. The resulting active range table not only contains traditional start and end position information, but more importantly, it accurately records all unused dead sub-intervals of each variable during its lifetime, laying a solid data foundation for subsequent register reuse optimization.
[0042] The second part implements a register allocation strategy based on dead interval reuse, building upon the existing dead interval active range table. This part employs an improved linear scanning algorithm. When processing each variable to be allocated, it first attempts to obtain available registers from the free register pool. If register resources are insufficient, it traverses the currently active variable list and uses a dead interval matching algorithm to determine whether short-lived variables can be completely placed into the dead intervals of long-lived variables. If a match is successful and there are no color conflicts, the registers are reused in the time dimension, significantly improving register utilization. Simultaneously, when overflow operations are necessary, an improved weight calculation method considering dead intervals is used to select the variable that has actually occupied the register for the longest time for overflow, ensuring the rationality of the overflow decision and maximizing the optimization effect.
[0043] Figure 1 A comparison is made between the traditional register allocation method and the register allocation method of the present invention, showing the traditional representation of variable occupancy of registers and the representation of the present invention. The present invention analyzes the use of register r2: r2 does not occupy the register but is not used during the period [4,7]. The active range of r3 is [5,6]. In the dead interval of r2 [4,7], the traditional method considers that r2 and r3 conflict and cannot be reused.
[0044] Figure 2 A flowchart of a register allocation method according to an embodiment of the present invention is shown, as follows: Figure 2 and Figure 3 As shown, the method includes:
[0045] Step S1: Construct the active range of each variable containing dead interval information by reverse traversing the control flow graph (CFG) and the basic blocks in the control flow graph.
[0046] Understandably, constructing an active range table containing dead interval information by reverse traversing the control flow graph (CFG) and simultaneously reverse traversing basic blocks has the core advantage of precisely following the data flow analysis principle that "the last use of a variable determines its active endpoint." By deriving from the point of use (rather than the point of definition) of the variable, the active termination position (the beginning of the dead interval) of the variable can be directly located. This avoids the excessive expansion of the active range caused by control flow structures such as branches and loops in forward traversal. At the same time, it can completely capture the entire interval (including the dead interval) of the variable from "active" to "dead" in a single traversal. This ensures the accuracy and completeness of the active range calculation and naturally handles the merging of active information at the convergence of different paths through reverse propagation. This significantly improves the efficiency of data flow analysis, reduces redundant calculations, and provides accurate and compact variable lifecycle data for subsequent compilation optimization stages such as register allocation and code optimization.
[0047] The LiveRange structure for each variable is defined as follows:
[0048] struct LiveRange {
[0049] / / Active range: [startPoint, endPoint]
[0050] startPoint: The starting position of the active range (instruction number).
[0051] endPoint: The end position of the active range (instruction number).
[0052] / / Points to a linked list of all dead intervals within this active range, of type struct DeadInterval
[0053] deadIntervals: Head pointer of the dead interval linked list
[0054] deadIntervalAvail: A dead interval availability flag, indicating whether the dead interval of this variable can be reused.
[0055] / / Register allocation information field
[0056] color: the allocated physical register number
[0057] usedColorLR: Reuse source pointer. When this variable reuses the dead space of other variables, this pointer points to the reused source variable.
[0058] weight: Overflow weight, calculated based on the effective lifetime length (considering dead intervals).
[0059] nextActiveLR: A pointer to the active scope linked list used to organize the active variable linked list.
[0060] }
[0061] The dead interval structure is defined as follows:
[0062] struct DeadInterval = {
[0063] startPoint: The starting position of the dead interval
[0064] endPoint: The end position of the dead interval.
[0065] next: A pointer to the next dead interval node.
[0066] }
[0067] See Figure 4 The flowchart illustrates the construction of an active range table containing dead interval information for all variables. Step S1 specifically includes:
[0068] Step S101, Initialization phase, for the function The set of variables in For each variable Construct the active range set and initialize its properties:
[0069] .
[0070] Step S102: Analyze the control flow graph. Perform a reverse post-order traversal (RPO) to obtain the basic block sequence. .
[0071] Here It is a set of basic blocks of the control flow graph (CFG). The set of directed edges of a control flow graph (CFG). express The basic blocks in the.
[0072] The resulting sequence satisfies the invariant: , ,like Then the successor block In its precursor block This was previously processed. This ensures the proper dissemination of active information: .here Representing basic blocks Active episodes, ) represents a basic block Active participation in the episode, Representing basic blocks The set of successor basic blocks.
[0073] Step S103: For each basic block , call Process its set of active export variables Each variable in .
[0074] here Representing basic blocks End position number, , indicating that the variable In basic blocks End position Marked as active.
[0075] The following is Function implementation:
[0076] MarkLRLive(LR(v), pos) = { / / Mark variable v as active at position pos
[0077] if LR(v).startPoint ≠ 0 / / Change from dead to active
[0078] δ = [pos, LR(v).startPoint] / / Create dead interval
[0079] LR(v).deadIntervals = LR(v).deadIntervals∪{δ}
[0080] LR(v).startPoint = 0
[0081] Update LR(v).endPoint
[0082] }
[0083] This function is the core code used to mark variables as active and create dead intervals during reverse traversal. When encountering a usage operation on variable v during reverse scanning, the function first checks if the variable is in a dead state (starting position is not 0). If the variable was previously marked as dead, it means there is a dead interval between the current usage position and the previous dead point. In this case, a new dead interval node is created, with its range being from the current position... The function returns to the previous starting position and adds this dead interval to the variable's dead interval set. Then, it resets the starting position to 0 to restore the active state and finally updates the end position of the active range. This function is the core mechanism for dead interval identification and construction. By capturing the key moments when a variable transitions from a dead state to an active state during reverse traversal, it accurately extracts the time period during which the variable is actually not used within its lifetime.
[0084] Step S104: For basic blocks Instruction sequence within Traverse in reverse order.
[0085] Step S105, for the instruction ,like ,Right now Use variables Then execute .
[0086] here Indicates the first Instruction location, , indicating that the variable In the instructions The location is marked as active.
[0087] Step S106, for the instruction ,like ,Right now Define variables Then execute This operation updates the starting boundary of the active range.
[0088] here Indicates the first Instruction location, , indicating that the variable In the instructions The location is marked as dead.
[0089] The following is Function implementation:
[0090] MarkLRDead(LR(v), p) = { / / Mark variable v as dead at position pos.
[0091] LR(v).startPoint = p / / This marks the left boundary of the active range (the definition point in the forward direction) / / If this is the first time this variable is encountered (endPoint has not yet been initialized).
[0092] if LR(v).endPoint = -1:
[0093] LR(v).endPoint = p / / Set the end position to the current position as well.
[0094] LR(v).function = currentFunction / / Records the function to which the active scope belongs.
[0095] }
[0096] This function is used to mark variables that have entered a dead state during reverse traversal. It is used when encountering a variable during a reverse scan of the control flow graph. The definition operation sets the starting position of the variable to the current instruction position. This marks the point where the variable enters a dead state (in reverse view, the variable at the position before the definition point has not yet been defined). If this is the first time the variable is encountered (the ending position has not yet been initialized), then the ending position is initialized to the current position, and the function context to which this active range belongs is recorded. This operation is related to... When used in conjunction with other methods, dead intervals are identified and created by detecting when a variable transitions from a dead state to an active state during reverse traversal, ultimately constructing an active range table containing precise dead interval information.
[0097] Step S107: After traversal is complete, output the active range table containing dead intervals. , each of which It has the following values:
[0098] Active range: Set of dead intervals: ,in , This is the starting position of the dead interval. This represents the end of the dead interval; the effective active interval. ), This represents the union of all dead intervals.
[0099] Among them, satisfying , The dead intervals do not overlap.
[0100] Step S2: Sort the active ranges of all variables according to their starting positions to obtain an ordered sequence of active ranges. ,in, This indicates the active range of the nth variable.
[0101] Understandably, the active range of each variable is sorted according to the size of its starting position, resulting in an ordered sequence of active ranges. Satisfying the total order relation .
[0102] This sorting ensures the sequential nature of the processing: when processing... At that time, all satisfied The active range has been processed.
[0103] Step S3, based on the ordered active range sequence Construct the key data structure = {active linked list} Free register pool Register set }
[0104] Understandably, prior to register allocation, the system is based on an ordered active range sequence. Maintain the following key data structures:
[0105] Active linked list At any moment The set of active ranges of physical registers that are occupied.
[0106] Free register pool At any moment The set of available physical registers.
[0107] Register set : All available physical registers.
[0108] The above structure satisfies: and .
[0109] Step S4: Sequentially retrieve each active range from the sorted list of active ranges. For the current active range, call the method to release expired active ranges and release the expired active ranges.
[0110] The set of expired active ranges is defined as follows: .
[0111] Perform a release operation on expired active scopes:
[0112] for each lr ∈ Expired(currentLR) do
[0113] A←A\{lr} / / Remove from the active list
[0114] F←F∪{lr.color} / / Registers are returned to the free pool
[0115] end for.
[0116] This method checks all active variables in the active list. If the end position of an active variable is less than the start position of the current variable, it means that the active variable has ended. The active variable is removed from the active list and the physical register it occupies is released back to the free register pool.
[0117] Step S5: For the current variable to be allocated, check if the free register pool is empty. If the free register pool is not empty, proceed to step S6; otherwise, proceed to step S7.
[0118] Step S6: Take a register from the free register pool, allocate it to the current variable to be allocated, add the active range of the current variable to be allocated to the active list, and continue processing the next variable to be allocated.
[0119] The procedure for step S6 is as follows:
[0120] r = SelectRegister(F) / / Select a register from F
[0121] currentLR.color = r / / Allocate registers
[0122] F = F\ {r} / / Remove from the free pool
[0123] A = A∪{currentLR} / / Add to the active list
[0124] continue / / Process the next variable.
[0125] Step S7: If the free register pool is empty, attempt to reuse the dead interval. If a reusable register is found in the active list, proceed to step S8; otherwise, proceed to step S9.
[0126] Specifically, when attempting to reuse a dead region, the function to find a reusable register is called. The active list is searched to see if there is a dead interval for a certain active variable that can accommodate the current variable.
[0127]
[0128] Among them, see Figure 5 Step S7 specifically includes:
[0129] Step S701: Initialize the search result variables.
[0130] found = false / / Flag indicating whether a reusable register has been found
[0131] candidateSrcLR = NULL / / Pointer to the active range of candidate sources
[0132] candidateColor= -1 / / Candidate register color (-1 indicates an invalid value).
[0133] Step S702: For the currently active linked list Active range of each variable in Iteratively execute steps S703 to S706. , which is the set of active ranges of currently occupied physical registers.
[0134] Step S703: From the currently active linked list Select an active range in sequence as the current candidate source active range. Call the dead interval matching function: , The active range of the variable to be assigned, also known as the target variable, if Then skip the current one. Process the next active range of candidate sources; otherwise, continue with step S704.
[0135] The following is the dead interval matching function. Definition:
[0136] FitLR(targetLR, srcLR) {
[0137] (targetLR.regCount = 1) ∧ / / Prerequisite: The target is a single-register variable
[0138] (srcLR.regCount = 1) ∧ / / Premise 2: The source is a single-register variable
[0139] (targetLR.hwType = srcLR.hwType) ∧ / / Prerequisite 3: Hardware type is consistent
[0140] (srcLR.deadIntervalAvail = true) ∧ / / Prerequisite 4: Dead interval functionality is available
[0141] (∃δ∈ srcLR.deadIntervals: / / Premise 5: There exists a bounding dead interval)
[0142] targetLR.startPoint ≥ δ.startPoint ∧ targetLR.endPoint ≤ δ.endPoint)
[0143] }
[0144] This function is used to determine the active range of a target. Can the active range of the source be reused? A Boolean predicate function for the dead interval. This function returns true if and only if all five preconditions are met:
[0145] (1) The target variable is a single-register variable, because the reuse of multi-register variables requires more complex alignment processing;
[0146] (2) The source variables are all single-register variables because the reuse of multi-register variables requires more complex alignment processing;
[0147] (3) The hardware register types of the two variables must be completely consistent (e.g., both are integer registers or both are floating-point registers) to ensure hardware-level compatibility;
[0148] (4) The dead interval functionality of the source variable must be available. This means that the variable has not been marked as disabled for dead zone reuse (e.g., special system registers).
[0149] (5) This is the most crucial point: there must be at least one dead interval in the set of dead intervals of the source variable. This ensures that the entire active range of the target variable is completely contained within the dead interval (i.e., the starting position of the target variable is no earlier than the start of the dead interval and the ending position is no later than the end of the dead interval). This guarantees that the register of the source variable is indeed idle during the active period of the target variable, so that the time dimension reuse of the register can be safely realized without data races or conflicts.
[0150] Step S704: For successfully matched... Check for register color conflicts. If a conflict exists, skip the current step. Process the next candidate; otherwise, continue with step S705.
[0151] The following is a function to check for register color conflicts. Definition:
[0152] ColorConflict(targetLR, srcLR, activeList) {
[0153] ∃lr∈ activeList:
[0154] (lr≠ srcLR) ∧ / / Exclude srcLR itself
[0155] (lr.color = srcLR.color) ∧ / / Use the same physical registers
[0156] Overlap(targetLR, lr) / / There is temporal overlap
[0157] }
[0158] This function is used to determine the target's active range. Place in the source active range This function checks whether a register conflict will occur after accessing the physical registers. The function returns true if a conflict exists; the specific logic is as follows: [The function checks if a register conflict occurs in the currently active linked list.] Does the search function exist within a certain active range? ,Should Three conditions must be met: First, no Firstly, excluding interference from the source variable itself; secondly, The physical register number used and They are completely identical (i.e., they share the same physical register); finally... Active time range and target variable The active time ranges overlap (through) (Function determination). If all three conditions are met simultaneously, it means that... Put in After the register, two variables will appear ( and When multiple people compete for the same physical register within the same time period, it can lead to register allocation conflicts. Therefore, such reuse schemes must be rejected to ensure program correctness.
[0159] Among them, the determination of time overlap:
[0160] ).
[0161] Used to detect two active ranges ( and This function checks for overlap in the time dimension. It uses logical negation: first, it checks if the two intervals are completely disjoint; then, it negates the condition of "completely disjoint," thus arriving at the conclusion that "overlap exists." Specifically, as long as the time intervals of the two active ranges are not completely separated (i.e., do not satisfy the condition...), they will overlap. Completely Before or Completely Previously, it was assumed that these variables overlapped in time, even if there was only one instruction location intersection. This method of judgment ensured the safety of register allocation because if the lifetimes of two variables had any temporal overlap, they could not share the same physical register (unless through dead-range reuse mechanism).
[0162] Step S705, when simultaneously satisfying and When the time comes, record the found multiplexed register, and return the lookup result variable:
[0163] found = true / / Marks a successful search
[0164] candidateSrcLR = srcLR / / Records the active range of the source
[0165] candidateColor = srcLR.color / / Record the register color
[0166] break / / prematurely terminates the traversal (returns if found).
[0167] Reason for early termination: Under the greedy strategy, the first one that meets the conditions can be used.
[0168] Step S706: If the entire active list has been traversed No source that meets the criteria was found afterward. If the search result is not found, mark it as a failure and proceed to step S9. The search result variable is returned as follows:
[0169] found = false / / Flag indicating whether a reusable register has been found
[0170] candidateSrcLR = NULL / / Pointer to the active range of candidate sources
[0171] candidateColor = -1 / / Candidate register color (-1 indicates an invalid value).
[0172] Step S707: Encapsulate the lookup result of the reusable register into a triplet. return.
[0173] Step S708: Based on the search results Branch operations are performed based on the value of , if Execute step S8 (use a reusable register), otherwise execute step S9 (select an overflow object).
[0174] Step S8: Allocate the reusable register to the currently unassigned variable.
[0175] Understandably, when a reusable register is found, it is allocated to the currently unassigned variable. It's important to note that after a register is allocated, the unassigned variable is not added to the active list because it shares the physical register with the source variable, whose occupancy status is represented by the source variable. Then, the process continues with the next unassigned variable.
[0176] The register reuse relationships are as follows:
[0177] currentLR.color = srcLR.color / / Reuse the source variable register currentLR.usedColorLR = srcLR / / Record the pointer to reuse relationships
[0178] / / Note: currentLR ∉ A / / Do not add to the active list
[0179] continue / / Process the next variable.
[0180] The reuse invariant is:
[0181] ,in .
[0182] Step S9: Overflow the variable with the largest weight in the active list, release the physical register occupied by the variable, allocate the physical register to the current variable to be allocated, and continue processing the next variable to be allocated.
[0183] Understandably, if no reusable register is found in the active list, a variable from the active list needs to be selected for overflow.
[0184] In this embodiment of the invention, a weight calculation method is invoked to calculate the weights of all variables in the active linked list. The weight calculation employs an improved method that considers dead intervals, i.e., the effective lifetime length, and selects the variable with the largest weight as the overflow object.
[0185] The overflow variable is selected using the following formula:
[0186]
[0187] The improved weighting function is defined as follows:
[0188]
[0189] in, Representing variables The weight, Representing variables The effective lifespan.
[0190]
[0191] in, For variables The starting position of the active range, for The end position of the active range, , indicates the length of the dead interval.
[0192] The improved weighting function defines the weight of a variable as equal to its effective lifetime (EffectiveLength). The calculation method is as follows: First, calculate the total length of the variable's active range (end position minus start position). Then, subtract the sum of the lengths of all dead intervals of the variable. Finally, sum the lengths of all dead intervals using the summation symbol ∑. Accumulate.
[0193] The core innovation of this formula lies in modifying the traditional "active range length" to "effective active length." This means only calculating the time a variable actually needs to occupy registers, excluding dead intervals that are within the active range but not actually used. The resulting weights more accurately reflect the actual time a variable occupies registers: a larger weight indicates a longer actual register usage time, allowing for the release of more register resources when such variables overflow. Conversely, variables containing large dead intervals have significantly lower weights, reducing their priority during overflow selection. This avoids the unreasonable overflow of variables with long active ranges but short actual usage times, ultimately improving register allocation efficiency and program execution performance.
[0194] Specifically, the selected overflow object is removed from the active list. Remove it from memory, causing its data to overflow. Release the physical register occupied by the variable and reclaim the register. Assign to the current unassigned variable Waiting for the currently unassigned variable. After the register is allocated, the currently unallocated variable will be... Add it to the active list, and then continue processing the next variable to be assigned. The specific implementation is as follows:
[0195] A = A \ {spillLR} / / Remove from the active list
[0196] Spill(spillLR) / / Spill into memory
[0197] r = spillLR.color / / Get the released register
[0198] currentLR.color ← r / / Assigned to the current variable
[0199] A = A ∪ {currentLR} / / Add to the active list
[0200] continue / / Process the next variable.
[0201] Once all registers occupied by dead intervals within all active ranges in the active linked list have been reused, output the register allocation diagram. The register allocation diagram is as follows: .
[0202] Figure 6 Analysis of register allocation in an example assembler. (Note: The lowercase letters are used here.) For virtual registers, size The actual physical registers allocated. Assume there are only 3 physical registers. , and .
[0203] Figure 7 This diagram illustrates the comparison between variable lifetimes and dead intervals in the above assembly program. Traditional methods treat variables... Represented as a continuous active range [3-21], with a weight of 19. The method of this invention identifies dead intervals [6-18] (length 13) through reverse scanning, thus... The effective weight dropped to 6, a decrease of 68%. This made Short-lived variables can be safely reused. The registers do not require additional physical registers.
[0204] Figure 8 A comparison diagram showing the changes before and after register allocation optimization. With only three physical registers, the traditional Linear Scan method leads to increased variables. Overflowing into memory. The method of this invention identifies... The dead interval [6-18] makes... Registers can be safely reused This eliminates overflow and improves register utilization by approximately 33%.
[0205] The present invention provides a register allocation method that, compared with existing register allocation methods, constructs an active range table containing dead interval information by reverse traversing the control flow graph (CFG) and the basic blocks in the control flow graph. The aim is to achieve register reuse by identifying and utilizing dead intervals within the active range of variables, thereby improving register utilization, reducing register overflow, and enhancing program execution performance.
[0206] Calculate the weights of all variables in the active linked list. The weight calculation adopts an improved method that considers the dead interval, i.e. the effective lifetime length. Select the variable with the largest weight as the overflow object and optimize the variable overflow decision.
[0207] It should be noted that the descriptions of each embodiment in the above embodiments have different focuses. For parts that are not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.
[0208] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0209] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded computer, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0210] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0211] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0212] Although preferred embodiments of the invention have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including both the preferred embodiments and all changes and modifications falling within the scope of the invention.
[0213] Obviously, those skilled in the art can make various modifications and variations to this invention without departing from its spirit and scope. Therefore, if these modifications and variations fall within the scope of the claims of this invention and their equivalents, this invention also intends to include these modifications and variations.
Claims
1. A register allocation method, characterized in that, include: Step S1: Construct the active range of each variable containing dead interval information by reverse traversing the control flow graph CFG and the basic blocks in the control flow graph. Step S2: Sort the active ranges of all variables according to their starting positions to obtain an ordered sequence of active ranges. ,in, Indicates the first The active range of each variable; Step S3, based on the ordered active range sequence Construct the key data structure = {active linked list} Free register pool Register set }, where the active linked list Indicates at time The set of active ranges of occupied physical registers, and the free register pool. Indicates at time Available physical register set, register set Represents all available physical registers; Step S4, from the ordered active range sequence Each active range is retrieved sequentially. For the current active range, the method to release expired active ranges is called to release the expired active ranges. Step S5: For the current variable to be allocated, check if the free register pool is empty. If the free register pool is not empty, proceed to step S6; otherwise, proceed to step S7. Step S6: Retrieve a register from the free register pool. Assign it to the current variable to be assigned, add the active scope of the current variable to be assigned to the active list, and continue to process the next variable to be assigned; Step S7: If the free register pool is empty, attempt to reuse the dead interval. If a reusable register is found in the active list, proceed to step S8; otherwise, proceed to step S9. Step S8: Allocate the reusable register to the currently unassigned variable; Step S9: Overflow the variable with the largest weight in the active list, release the physical register occupied by the variable, allocate the physical register to the current variable to be allocated, and continue processing the next variable to be allocated.
2. The register allocation method according to claim 1, characterized in that, Step S1 involves constructing the active range of each variable containing dead interval information by reverse traversing the control flow graph (CFG) and its basic blocks, including: Step S101, initialization phase, for the function The set of variables in For each variable Construct the active range set The active range set Includes variables The active range start position, the active range end position, and the dead range linked list that records dead range information; Step S102, process the control flow graph Perform a reverse postorder traversal to obtain the basic block sequence. ,in It is a set of basic blocks of the control flow graph (CFG). It is the set of directed edges of the control flow graph (CFG). express The basic blocks in; Step S103, for each basic block , call Process its set of active export variables Each variable in ,in, Representing basic blocks End position number, Indicates the variable In basic blocks End position Marked as active. Representing basic blocks Active appearances; Step S104, for basic blocks Instruction sequence Traverse in reverse order; Step S105, for the instruction ,like ,Right now Use variables Then execute ,in, Indicates the first Instruction location, Indicates the variable In the instructions The location is marked as active; Step S106, for the instruction ,like ,Right now Define variables Then execute ,in, Indicates the first Instruction location, Indicates the variable In the instructions The location is marked as dead; Step S107: After traversal is complete, output the active range table containing dead intervals. , each of which The data structure is as follows: Active range: Set of dead intervals: ,in , This is the starting position of the dead interval. This represents the end of the dead interval; the effective active interval. , This represents the union of all dead intervals.
3. The register allocation method according to claim 1, characterized in that, In step S4, for the current active range, the method for releasing expired active ranges is invoked to release expired active ranges, including: Check all active variables in the active list. If the end position of an active variable is less than the start position of the current active range, it means that the active variable has ended. Remove the active variable from the active list and release the physical register occupied by the active variable back to the free register pool.
4. The register allocation method according to claim 1, characterized in that, In step S7, if the free register pool is empty, attempt to reuse the dead interval; if a reusable register is found in the active list, proceed to step S8. Otherwise, proceed to step S9, which includes: Call the function to find reusable registers The process involves searching the active list for a dead interval of an active variable that can accommodate the currently unassigned variable, where reusable register functions are involved. Defined as: , The target active range is the active range of the variable to be assigned. For the source active range in the active list, A flag indicating whether a reusable register has been found. A pointer to the source active range. Set the register color.
5. The register allocation method according to claim 4, characterized in that, The function to find reusable registers is called. The active list is searched to determine if there exists a dead interval for an active variable that can accommodate the currently unassigned variable, including: The active range of the current candidate source in the current active list Call the dead interval matching function ,like If so, skip the active range of the current candidate source. Process the active range of the next candidate source; like The active range of the currently matched candidate source Check for register color conflicts; if any exist, skip the current active range of candidate sources. Process the active range of the next candidate source; otherwise, process the active range of the current candidate source. The physical register used is a reusable register.
6. The register allocation method according to claim 5, characterized in that, The The conditions that need to be met include: The target variable is a single-register variable, the source variable is a single-register variable, the hardware register types of the target variable and the source variable are completely identical, the dead interval function of the source variable is available, and there is at least one dead interval in the set of dead intervals of the source variable, so that the entire active range of the target variable can be completely contained within the dead interval.
7. The register allocation method according to claim 5, characterized in that, The active range of the currently matched candidate sources Check for register color conflicts, including: Register-based color conflict function Determine the target's active range Place in the source active range Will register conflicts occur after modifying the physical registers? If the return value is true, then a register conflict exists. If the return value is false, then there is no register conflict; Among them, the register color conflict function To return true, the following conditions must be met: Search the currently active list to see if an active range exists. This active range Three conditions must be met: Not the source active range itself, Physical register number used and source active range The physical registers used are all numbered exactly, and Active time range and target variable The active time ranges overlap.
8. The register allocation method according to claim 1, characterized in that, Step S9, which involves overflowing the variable with the largest weight in the active linked list and releasing the physical register occupied by that variable, includes calculating the weight of each variable in the active linked list: The effective lifetime length of each variable in the active linked list is used as the weight, where the effective lifetime length of each variable is the total length of the active range minus the sum of the lengths of all dead intervals within the active range.
9. The register allocation method according to claim 1, characterized in that, Step S9 involves overflowing the variable with the largest weight in the active linked list, releasing the physical register occupied by that variable, allocating the physical register to the currently unassigned variable, and continuing to process the next unassigned variable, including: Remove the variable with the highest weight from the active list, let its data overflow into memory, release the physical register occupied by the variable, allocate the physical register to the current variable to be assigned, add the current variable to be assigned to the active list, and continue processing the next variable to be assigned.