[go: up one dir, main page]

CN102360280A - Method for allocating registers for mixed length instruction set - Google Patents

Method for allocating registers for mixed length instruction set Download PDF

Info

Publication number
CN102360280A
CN102360280A CN2011103334602A CN201110333460A CN102360280A CN 102360280 A CN102360280 A CN 102360280A CN 2011103334602 A CN2011103334602 A CN 2011103334602A CN 201110333460 A CN201110333460 A CN 201110333460A CN 102360280 A CN102360280 A CN 102360280A
Authority
CN
China
Prior art keywords
register
lifetime
regs
overflow
instruction
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN2011103334602A
Other languages
Chinese (zh)
Other versions
CN102360280B (en
Inventor
李莹
闫卫斌
吴朝晖
尹建伟
邓水光
吴健
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhejiang University ZJU
Original Assignee
Zhejiang University ZJU
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Zhejiang University ZJU filed Critical Zhejiang University ZJU
Priority to CN201110333460.2A priority Critical patent/CN102360280B/en
Publication of CN102360280A publication Critical patent/CN102360280A/en
Application granted granted Critical
Publication of CN102360280B publication Critical patent/CN102360280B/en
Expired - Fee Related legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Landscapes

  • Devices For Executing Special Programs (AREA)

Abstract

The invention discloses a method for allocating registers for a mixed length instruction set. By less revising the conventional graph coloring register allocation method, namely fully utilizing the characteristics of a mixed coding instruction set, codes with higher code density are generated. The method has the characteristics of simplicity, practicability and high in reliability.

Description

一种针对混合长度指令集的寄存器分配方法A Register Allocation Method for Mixed-Length Instruction Sets

技术领域 technical field

本发明涉及一种编译技术,尤其涉及一种针对混合长度指令集的寄存器分配方法。 The invention relates to a compiling technology, in particular to a register allocation method for mixed-length instruction sets.

背景技术 Background technique

嵌入式系统常采用RISC架构,其指令集一般为定长指令集,即只具有单一长度的指令。指令长度一般为整数个字节,例如,16位指令,32位指令。较长的指令长度可以编码更多的操作数,寻址更多的寄存器,或者可使用更大的立即数等,故一般具有更好的性能;而较短的指令长度可以使得编译生成的可执行程序更小。为了在具有长指令高性能的同时具有短指令的高代码密度,现代的RISC处理器开始采用两种或者两种以上不同长度指令混合编码的指令集。例如,ARM的thumb2指令集和中天微公司的cskyv2指令集都是16位与32位指令混合编码的指令集(以下简称“混编指令集”)。 Embedded systems often adopt RISC architecture, and their instruction sets are generally fixed-length instruction sets, that is, instructions with only a single length. The instruction length is generally an integer number of bytes, for example, a 16-bit instruction and a 32-bit instruction. A longer instruction length can encode more operands, address more registers, or use a larger immediate value, etc., so it generally has better performance; while a shorter instruction length can make the compiled generated Executors are smaller. In order to have high code density of short instructions while having high performance of long instructions, modern RISC processors begin to use two or more instruction sets with mixed codes of different lengths. For example, ARM's thumb2 instruction set and Zhongtian Micro's cskyv2 instruction set are both 16-bit and 32-bit instruction mixed-coded instruction sets (hereinafter referred to as "mixed-coded instruction sets").

在混编指令集中,短指令相比长指令而言,寻址的操作数个数少,可编码的立即数范围小,或者单个地址只可使用部分寄存器。例如,在cskyv2指令集中,长指令可使用R0~R31全部共32个通用寄存器,而绝大多数短指令只可使用R0~R15共16个通用寄存器;长指令一般具有3操作数,而短指令最多只有2操作数。短指令在功能上是长指令的一个子集。通常,编译器按照长指令的功能来生成汇编指令,而汇编器生成机器指令时,会根据指令的类型和其操作数来决定生成长指令还是短指令。 In the mixed instruction set, compared with long instructions, short instructions have fewer addressable operands, a smaller range of immediate values that can be encoded, or only part of registers can be used for a single address. For example, in the cskyv2 instruction set, long instructions can use all 32 general-purpose registers R0~R31, while most short instructions can only use 16 general-purpose registers R0~R15; long instructions generally have 3 operands, while short instructions There are at most only 2 operands. Short instructions are functionally a subset of long instructions. Usually, the compiler generates assembly instructions according to the function of long instructions, and when the assembler generates machine instructions, it will decide whether to generate long instructions or short instructions according to the type of the instruction and its operands.

以cskyv2为例,如果一条指令的某个寄存器操作数被分配了R16~R31的寄存器,那么该指令将生成一条长指令(个别指令除外,下外将介绍);但一条指令即使只使用R0~R15的寄存器,它也并不一定生成短指令,因为它可能使用了超出短指令可编码范围的立即数,或者使用了3个不同的操作数等等。以cskyv2为例,如果一条指令的某个寄存器操作数被分配了R16~R31的寄存器,那么该指令将生成一条长指令(个别指令除外,下外将介绍);但一条指令即使只使用R0~R15的寄存器,它也并不一定生成短指令,因为它可能使用了超出短指令可编码范围的立即数,或者使用了3个不同的操作数,等等。如果一条指令最终生成的机器指令是长指令还是短指令取决于其分配到的寄存器(以下简称 A类指令);反之,如果不论其寄存器操作数分配到哪个寄存器,其必然生成一条长指令,或者必然生成一条短指令(以下简称 B类指令)。如何以较小的代价使得所有的A类指令最终生成的机器指令为短指令,是技术人员需要克服的难点。 Taking cskyv2 as an example, if a register operand of an instruction is assigned a register of R16~R31, then the instruction will generate a long instruction (except for individual instructions, which will be introduced below); but even if an instruction only uses R0~ The register of R15, it does not necessarily generate short instructions, because it may use immediate values beyond the encoding range of short instructions, or use 3 different operands, etc. Taking cskyv2 as an example, if a register operand of an instruction is assigned a register of R16~R31, then the instruction will generate a long instruction (except for individual instructions, which will be introduced below); but even if an instruction only uses R0~ The register of R15, it does not necessarily generate a short instruction, because it may use an immediate value beyond the encoding range of a short instruction, or use 3 different operands, and so on. If an instruction finally generates a machine instruction, whether it is a long instruction or a short instruction depends on the register it is assigned to (hereinafter referred to as a type A instruction); conversely, if it does not matter which register its register operand is assigned to, it must generate a long instruction, or A short instruction (hereinafter referred to as B type instruction) is bound to be generated. How to make the final machine instructions generated by all class A instructions into short instructions at a relatively low cost is a difficulty that technicians need to overcome.

发明内容 Contents of the invention

针对上述技术难点,本发明的提出一种针对混合长度指令集的寄存器分配方法。 In view of the above-mentioned technical difficulties, the present invention proposes a register allocation method for mixed-length instruction sets.

为了解决上述技术问题,本发明的技术方案如下: In order to solve the problems of the technologies described above, the technical solution of the present invention is as follows:

一种针对混合长度指令集的寄存器分配方法,包括如下步骤: A register allocation method for a mixed-length instruction set, comprising the steps of:

1) 查找函数中的所有生命周期,通过设置标志位是否置位来判断设该生命期为A类生命期还是B类生命期; 1) Find all life cycles in the function, and judge whether the life cycle is a type A life cycle or a B type life cycle by setting whether the flag bit is set;

2) 执行图着色寄存器分配方法的Renumber1、build1、coalesce1、spill cost1、和simplify1五个过程,为所有A类生命期分配lo-regs寄存器,得到未进行任何溢出操作的冲突图G12) Execute the five processes of Renumber 1 , build 1 , coalesce 1 , spill cost 1 , and simplify 1 of the graph coloring register allocation method, allocate lo-regs registers for all class A lifetimes, and obtain a conflict graph without any overflow operation G 1 ;

3) 计算空闲lo-regs寄存器数m,如果所述冲突图G非空,则空闲lo-regs寄存器数m为0,如果所述冲突图G为空图,则执行图着色寄存器分配方法的select1过程,并根据生成的寄存器分配方案计算空闲的lo-regs寄存器数m,并记录空闲寄存器信息; 3) Calculate the number m of free lo-regs registers, if the conflict graph G 1 is not empty, then the number m of free lo-regs registers is 0, if the conflict graph G 1 is empty, execute the graph coloring register allocation method select 1 process, and calculate the number m of free lo-regs registers according to the generated register allocation scheme, and record the free register information;

4) 执行图着色寄存器分配方法的Renumber2、build2、coalesce2、simplify、spill code2和select2过程,为B类生命期分配hi-regs寄存器和m个空闲的lo-regs寄存器,并根据生成的分配方案计算空闲的hi-regs寄存器数量n,并记录空闲寄存器信息; 4) Execute the Renumber 2 , build 2 , coalesce 2 , simplify 2 , spill code 2 and select 2 processes of the graph coloring register allocation method, allocate hi-regs registers and m free lo-regs registers for the lifetime of class B, and Calculate the number n of free hi-regs registers according to the generated allocation scheme, and record the free register information;

5) 执行图着色寄存器分配方法的spill code1和select1过程,如果select1过程已经在 5) Execute the spill code 1 and select 1 process of the graph coloring register allocation method, if the select 1 process is already in

步骤3)执行,则步骤结束,如果没有,重复执行spill code、Renumber1、build1、coalesce1、spill cost1、simplify1   过程直到所述冲突图G为空,然后执行select1过程为A类生命期生成寄存器分配方案; Step 3) Execute, then the step ends, if not, repeat the process of spill code 1 , Renumber 1 , build 1 , coalesce 1 , spill cost 1 , simplify 1 until the conflict graph G 1 is empty, and then execute the process of select 1 as Class A lifetime generation register allocation scheme;

Renumber1、build1、coalesce1、spill cost1、simplify1  、spill code1 、select1、Renumber2、build2、coalesce2、spill cost2、simplify、spill code2和select2过程均为图着色寄存器分配方法的步骤; Renumber 1 , build 1 , coalesce 1 , spill cost 1 , simplify 1 , spill code 1 , select 1 , Renumber 2 , build 2 , coalesce 2 , spill cost 2 , simplify 2 , spill code 2 and select 2 processes are graph coloring the steps of the register allocation method;

所述图着色寄存器分配方法包括如下步骤: The graph coloring register allocation method includes the following steps:

11)Renumber:在数据流分析的基础上,找到函数中的所有生命期,并为其分配唯一的编号,其中一个生命期从一个变量的一次定值开始,到对该值的最后一次使用结束; 11) Renumber: On the basis of data flow analysis, find all the lifetimes in the function and assign them unique numbers. One of the lifetimes begins with a fixed value of a variable and ends with the last use of the value ;

12) build:建立冲突图G,所述冲突图G中的结点为生命期,边则表示通过其相连的两个生命期有冲突,不能为它们分配同一个寄存器; 12) build: build a conflict graph G, the nodes in the conflict graph G are life periods, and the edges indicate that the two life periods connected through it have conflicts, and the same register cannot be allocated to them;

13)coalesce:判断是否需要合并生命期,如需要则通过合并生命期删除不必要的复制语句,合并后返回,重新执行步骤12),如不需要合并,则执行步骤14); 13) coalesce: determine whether to merge the life cycle, if necessary, delete unnecessary copy statements through the merge life cycle, return after the merge, and re-execute step 12), if no merge, execute step 14);

14)spill cost:计算每个生命期的溢出代价; 14) spill cost: calculate the overflow cost of each lifetime;

15)simplify:对所述冲突图G进行简化,反复地检查图G,删除G中度数小于可用寄存器数k的节点,删除的同时将该节点压入栈s; 15) simplify: Simplify the conflict graph G, check the graph G repeatedly, delete nodes in G whose degree is less than the number of available registers k, and push the node into the stack s while deleting;

16)spill code:当所述冲突图G中所有节点的度都大于等于k时,需要将溢出代价最小的生命期溢出,即删除其节点,将其压入栈s,并将其标记为溢出,溢出一个生命期之后返回执行步骤11); 16) Spill code: When the degree of all nodes in the conflict graph G is greater than or equal to k, it is necessary to overflow the lifetime with the least overflow cost, that is, delete its node, push it into the stack s, and mark it as overflow , return to step 11 after overflowing a lifetime);

17)select:当所述冲突图G为空时,将栈s中的生命期弹出,为其分配寄存器,并为标记为溢出的生命期插入溢出代码; 17) select: when the conflict graph G is empty, pop the lifetime in the stack s, allocate registers for it, and insert overflow code for the lifetime marked as overflow;

所述lo-regs寄存器为短指令可寻址的寄存器,剩下的为所述hi-regs寄存器,所述标志位是否置位的标准为:将有A类指令引用的生命期设为置位,将只有B类指令引用的生命期设为不置位; The lo-regs register is a register addressable by short instructions, and the rest is the hi-regs register. The standard for whether the flag is set is: set the lifetime referenced by a class A instruction to be set , set the lifetime referenced only by class B instructions to not set;

所述A类指令为一条指令最终生成的机器指令是长指令还是短指令取决于其分配到的寄存器; The class A instruction is a machine instruction finally generated by an instruction, whether it is a long instruction or a short instruction depends on the register to which it is allocated;

所述B类指令为不论其寄存器操作数分配到哪个寄存器,其必然生成一条长指令,或者必然生成一条短指令。 The type B instruction is that no matter which register its register operand is allocated to, it must generate a long instruction, or it must generate a short instruction.

进一步的,所述标志位设于记录生命期信息的结构体中。 Further, the flag is set in a structure that records lifetime information.

进一步的,所述步骤 5)中spill code的过程包括如下步骤: Further, the process of spill code 1 in step 5) includes the following steps:

31)在构造所述冲突图G1  保存一个所述冲突图G1   的备份冲突图G1-backup; 31) Save a backup conflict graph G 1 -backup of the conflict graph G 1 when constructing the conflict graph G 1 ;

32)每次simplify1   决定溢出某个生命期l时,首先判断是否有空闲的hi-regs寄存器可用于溢出,如果有,那么在l的每次赋值后和使用前插入move指令,并记录生命期l溢出到hi-regs寄存器的信息;并且当每个hi-regs寄存器都已经有关联的溢出生命期时,根据所述备份冲突图G1-backup判断是否有寄存器关联的所有溢出生命期都不与当前要溢出的生命期l冲突,如果有这样的寄存器,那么它可用于l的溢出,如果没有空闲的hi-regs用于溢出,那么spill code1使用load/store指令将生命期溢出到存储器。 32) Every time simplify 1 decides to overflow a certain lifetime l, first judge whether there are free hi-regs registers that can be used for overflow, if so, then insert a move instruction after each assignment of l and before use, and record the lifetime period l overflows to the information of the hi-regs register; and when each hi-regs register has an associated overflow lifetime, judge whether all overflow lifetimes associated with the register are judged according to the backup conflict graph G 1 -backup Does not conflict with the current lifetime l to be spilled. If there is such a register, it can be used for the overflow of l. If there are no free hi-regs for overflow, then spill code 1 uses the load/store instruction to overflow the lifetime to memory.

本发明的有益效果在于:适用于具有两种或两种以上不同长度指令,并且其中较短指令可访问寄存器的集合为较长指令可访问寄存器的集合的子集的指令集,本发明可提高生成代码的指令密度,并且简单实用,可靠性强,最终以较小的代价使得所有的A类指令最终生成的机器指令为短指令。 The beneficial effects of the present invention are: applicable to instruction sets having two or more instructions with different lengths, and wherein the set of registers accessible by shorter instructions is a subset of the set of registers accessible by longer instructions, the present invention can improve The instruction density of the generated code is simple and practical, and the reliability is strong. Finally, at a relatively small cost, all the machine instructions generated by the class A instructions are short instructions.

附图说明 Description of drawings

   图1为图着色寄存器分配方法的基本流程图; Figure 1 is the basic flowchart of the graph coloring register allocation method;

   图2为本发明的具体实施流程图; Fig. 2 is the concrete implementation flowchart of the present invention;

  图3 为标记生命期类型流程图; Figure 3 is a flow chart of the marker lifetime type;

  图4 为spill code1流程图; Figure 4 is a flowchart of spill code 1 ;

  图5为备份的冲突图的部分图。 Figure 5 is a partial diagram of the backup conflict graph.

具体实施方式 Detailed ways

    下面将结合附图和具体实施方式对本发明做进一步的说明。 The present invention will be further described below in conjunction with the accompanying drawings and specific embodiments.

    本发明是一种在图着色寄存器分配法基础上进行改进的寄存器分配方法。图着色是传统的,也是最为常用的寄存器分配方法,其基本流程如图1所示,各阶段的简要说明如下: The present invention is an improved register allocation method based on the graph coloring register allocation method. Graph coloring is a traditional and most commonly used register allocation method. Its basic flow is shown in Figure 1. A brief description of each stage is as follows:

       重命名(Renumber): 在本阶段之前,中间代码可以引用数目无限的“虚拟寄存器”。本阶段在数据流分析的基础上,找到函数中的所有生命期,并为其分配唯一的编号。一个生命期从一个变量的一次定值开始,到对该值的最后一次使用结束。 Rename (Renumber): Before this stage, the intermediate code can refer to an unlimited number of "virtual registers". In this stage, based on data flow analysis, find all lifetimes in the function and assign unique numbers to them. A lifetime begins with one setting of a variable and ends with the last use of that value.

       构造冲突图(Build): 本阶段建立“冲突图G”,G中的结点为生命期,边则表示通过其相连的两个生命期有冲突,即它们在某条指令处同时活跃,故不能为它们分配同一个寄存器。 Construct conflict graph (Build): At this stage, a "conflict graph G" is established. The nodes in G are life cycles, and the edges indicate that there is a conflict between the two life cycles connected through it, that is, they are active at a certain instruction at the same time, so They cannot be allocated the same register.

       合并生命期(Coalesce): 本阶段通过合并生命期来删除不必要的复制语句。合并生命期改变了冲突图,故合并之后需要重新执行“Build”过程。  Coalesce: In this phase, unnecessary replication statements are deleted by merging the lifespan. The merge lifetime changes the conflict graph, so the "Build" process needs to be rerun after the merge.

       溢出代价分析(Spill cost): 本阶段用于计算每个生命期的溢出代价。 Spill cost analysis (Spill cost): This stage is used to calculate the spill cost of each lifetime.

       简化冲突图(Simplify): 本阶段中对冲突图G进行简化,反复地检查图G,删除G中度数小于可用寄存器数k的节点,删除的同时将该节点压入栈s。 Simplify the conflict graph (Simplify): In this stage, the conflict graph G is simplified, the graph G is checked repeatedly, and the node in G whose degree is less than the number of available registers k is deleted, and the node is pushed into the stack s while deleting.

       插入溢出代码(Spill code): 当图G中所有节点的度都大于等于k时,“Simplify”过程无法继续。此时,需要将溢出代价最小的生命期溢出,即删除其节点,将其压入栈s,并将其标记为溢出。溢出一个生命期之后需要从“重命名”阶段重新开始执行图着色算法。 Insert Spill code: When the degree of all nodes in graph G is greater than or equal to k, the "Simplify" process cannot continue. At this time, it is necessary to overflow the lifetime with the least overflow cost, that is, delete its node, push it into the stack s, and mark it as overflow. Execution of the graph coloring algorithm needs to be restarted from the "rename" phase after overflowing a lifetime.

       着色(Select): 图G最终会被简化为一个空图,此时将栈s中的生命期弹出,为其分配寄存器,并为标记为溢出的生命期插入溢出代码。 Coloring (Select): The graph G will eventually be reduced to an empty graph. At this time, the lifetime in the stack s is popped, registers are allocated for it, and overflow code is inserted for the lifetime marked as overflow.

       首先将寄存器进行分类,其中大多数短指令可寻址的寄存器称为lo-regs,而其余的寄存器称为hi-regs。然后为每个生命期增加一个标志位,标记其是否曾被A类指令引用,将只有B类指令引用的生命期称为“B类生命期”;而将有A类指令引用的生命期称为“A类生命期”,并通过扫描整个函数的指令流为每个生命期设置该标记。 First classify the registers, most of the registers addressable by short instructions are called lo-regs, and the rest are called hi-regs. Then add a flag bit for each lifetime to mark whether it has been referenced by a class A instruction, and call the lifetime referenced by only the B class instruction "B class lifetime"; and the lifetime referenced by the A class instruction is called for "class A lifetimes" and sets that flag for each lifetime by scanning the entire function's instruction stream.

       接着执行两个图着色过程,为A类生命期分配lo-regs,为B类生命期分配hi-regs;如果lo-regs、hi-regs分别足够A类生命期、B类生命期使用,或者两个图着色过程寄存器都不足,都需要溢出生命期,那么这两个图着色过程独立进行。如果lo-regs足够A类生命期分配且有空闲,但hi-regs不够B类生命期分配,那么将lo-regs中空闲的寄存器和hi-regs寄存器一起分配给B类生命期。反之,如果hi-regs有空闲而lo-regs不足,那么在lo-regs分配过程中生成溢出代码时,优先将生命期溢出到空闲的hi-regs,当无hi-regs寄存器可进行溢出时才溢出到存储器。 Then execute two graph coloring processes, allocate lo-regs for class A lifetime, and assign hi-regs for class B lifetime; if lo-regs and hi-regs are enough for class A lifetime and class B lifetime respectively, or The registers of the two graph coloring processes are insufficient, and both need to overflow the lifetime, so the two graph coloring processes are carried out independently. If lo-regs are enough for class A lifetime allocation and there is free time, but hi-regs are not enough for class B lifetime allocation, then the free registers in lo-regs and hi-regs registers are allocated to class B lifetime. Conversely, if hi-regs are free and lo-regs are insufficient, then when overflow code is generated during lo-regs allocation, the life cycle will be overflowed to free hi-regs first, and only when there are no hi-regs registers to overflow overflow to memory.

本方法的具体实现流程如图2所示,具体步骤如下: The specific implementation process of this method is shown in Figure 2, and the specific steps are as follows:

一、生命期标记。本阶段查找函数中的所有生命期;并通过扫描当前函数的所有指令对每个生命期的类型进行标记。 1. Life cycle mark. This phase finds all lifetimes in the function; and marks the type of each lifetime by scanning all instructions of the current function.

二、Renumber1、build1、coalesce1、spill cost1、和simplify1。本步骤执行图着色算法流程中的前5个阶段,为所有A类生命期分配lo-regs寄存器。本步骤执行结束后,将得到未进行任何溢出操作的冲突图G1Two, Renumber 1 , build 1 , coalesce 1 , spill cost 1 , and simplify 1 . This step executes the first five stages in the graph coloring algorithm flow, and allocates lo-regs registers for all class A lifetimes. After this step is executed, a conflict graph G 1 without any overflow operation will be obtained.

三、计算空闲lo-regs寄存器数。本步骤计算空闲的lo-regs寄存器数m,如果G1非空,那么m为0;反之,如果G1为空图,那么我们执行select1过程,并根据生成的寄存器分配方案计算空闲的lo-regs寄存器数,并记录空闲寄存器信息。 3. Calculate the number of free lo-regs registers. This step calculates the number m of free lo-regs registers, if G 1 is not empty, then m is 0; otherwise, if G 1 is an empty graph, then we execute the select 1 process, and calculate the free lo-regs according to the generated register allocation scheme -regs register number, and record free register information.

四、Renumber2、build2、coalesce2、spill cost2、simplify、spill code2和select2。本步骤是一个完整的传统图着色寄存器分配过程。我们使用这一过程为B类生命期分配hi-regs寄存器和m个空闲的lo-regs寄存器。并根据生成的分配方案计算空闲的hi-regs寄存器数量n,并记录空闲寄存器信息。 Four, Renumber 2 , build 2 , coalesce 2 , spill cost 2 , simplify 2 , spill code 2 and select 2 . This step is a complete traditional graph coloring register allocation process. We use this procedure to allocate hi-regs and m free lo-regs for class B lifetimes. And calculate the number n of free hi-regs registers according to the generated allocation scheme, and record the free register information.

五、spill code1和select1。如果G1为空,那么select1已在步骤三中执行,整个算法结束;否则,重复执行spill code、Renumber1、build1、coalesce1、spill cost1、simplify1过程,直到G1为空,然后select1为A类生命期生成寄存器分配方案。本步骤的特殊之处在于,如果n>0,那么在执行插入spill code1时,我们优先将生命期溢出到空闲的hi-regs寄存器,只有当无hi-regs寄存器可用时,才将生命期溢出到存储器。这样做的优点在于使用move指令生成的将生命期溢出到空闲的hi-regs寄存器的代码,其执行速度一般要快于使用load/store执行生成的将生命期溢出到存储器的代码。 Five, spill code 1 and select 1 . If G 1 is empty, then select 1 has been executed in step 3, and the entire algorithm ends; otherwise, repeat the process of spill code 1 , Renumber 1 , build 1 , coalesce 1 , spill cost 1 , and simplify 1 until G 1 is empty , and then select 1 to generate a register allocation scheme for the lifetime of class A. The special feature of this step is that if n>0, then when executing spill code 1 , we first overflow the lifetime to the free hi-regs register, and only when no hi-regs register is available, the lifetime overflow to memory. The advantage of this is that code generated using move instructions that spill lifetimes into free hi-regs generally executes faster than code generated using load/store execution that spills lifetimes into memory.

       另外,混编指令集通常提供特殊的短指令——move,它是一条短指令,但可以寻址所有的寄存器。此时,使用move指令生成的将生命期溢出到空闲hi-regs寄存器的代码相比溢出到存储器将更加高效(使用了短指令进行溢出,而不是通常的长指令)。 In addition, mixed instruction sets usually provide a special short instruction-move, which is a short instruction, but can address all registers. At this point, the code generated using move instructions to spill lifetimes into free hi-regs registers will be more efficient than spilling to memory (using short instructions for spilling, rather than the usual long instructions).

本方法只需要在传统的图着色寄存器分配方法上进行少量的修改,即可充分利用混编指令集的特点,生成代码密度更高的代码,具有简单实用,可靠性强的特点。 The method only needs a small amount of modification on the traditional graph coloring register allocation method, can make full use of the characteristics of the mixed instruction set, generate codes with higher code density, and has the characteristics of simplicity, practicability and strong reliability.

       下面结合具体实例进行更加详细的说明: The following is a more detailed description combined with specific examples:

首先,在记录生命期信息的结构体中增加一个标志位flag_short,如果该标志位置位,则说明该生命期是一个A类生命期,否则其为一个B类生命期。在“生命期识别”过程结束后,增加一个“标记生命期类型”的过程,如图3所示。该过程通过依次扫描当前函数中的每一条指令,为每一个生命期设置flag_short标志。 First, add a flag bit flag_short to the structure that records lifetime information. If the flag is set, it means that the lifetime is a type A lifetime, otherwise it is a type B lifetime. After the "life cycle identification" process ends, add a "mark life cycle type" process, as shown in Figure 3. This process sets the flag_short flag for each lifetime by sequentially scanning each instruction in the current function.

       然后,需要执行两个图着色过程。其中select1为所有A类生命期分配lo-regs寄存器;select2为所有B类生命期分配hi-regs寄存器以及可能的空闲lo-regs寄存器。并且select2可能在select1完成之后执行,也可能在select1的第一次简化冲突图G1过程后执行,如图2所示。这取决于lo-regs是否足够A类生命期分配,如果足够使用,那么select1首先执行完毕,这样剩余的空闲lo-regs能够提供给select2阶段进行分配;否则,select1在执行第一次simplify1过程后,执行整个select2流程,这样如果select2过程有空闲的hi-regs的话,它们可以用于select1的插入spill code1阶段。总之,我们使用了select1、select2两个图着色过程,其中select1为所有flag_short为true的生命期分配lo-regs寄存器;select2为所有flag_short为false的生命期分配hi-regs寄存器和可能的空闲lo-regs寄存器。这两个图着色过程中,本发明只需要在传统的图着色算法中增加一个对生命期的flag_short标志的判断,使select1、select2分别处理flag_short为true、false的生命期,并简单的重新组织select1、select2的执行流程即可。 Then, two graph coloring processes need to be performed. Among them, select 1 allocates lo-regs registers for all class A lifetimes; select 2 allocates hi-regs registers and possible free lo-regs registers for all class B lifetimes. And select 2 may be executed after the completion of select 1 , or it may be executed after the first simplified conflict graph G 1 process of select 1, as shown in Figure 2. It depends on whether the lo-regs are enough for class A lifetime allocation. If it is enough, then select 1 is executed first, so that the remaining free lo-regs can be provided to select 2 for allocation; otherwise, select 1 is executed for the first time After the simplify 1 process, execute the entire select 2 process, so that if the select 2 process has free hi-regs, they can be used for the insert spill code 1 stage of select 1 . In short, we used two graph coloring processes, select 1 and select 2 , in which select 1 allocates lo-regs registers for all lifetimes where flag_short is true; select 2 allocates hi-regs registers and possibly for all lifetimes where flag_short is false Free lo-regs registers. During these two graph coloring processes, the present invention only needs to add a judgment to the flag_short sign of the life cycle in the traditional graph coloring algorithm, so that select 1 and select 2 process the life cycle when flag_short is true and false respectively, and simply Just reorganize the execution process of select 1 and select 2 .

       最后,本发明还可以修改传统图着色算法的溢出过程插入spill code1,其流程如图4所示。首先,在执行构造冲突图G1过程时,需要保存一个备份的冲突图G1-backup;然后在每次简化冲突图G1决定溢出某个生命期l时,首先判断是否有空闲的hi-regs可用于溢出,如果有,那么在l的每次赋值后和使用前插入move指令,并记录生命期l溢出到hi-regs的信息。需要注意的是,当每个hi-regs都已经有关联的溢出生命期时,并不意味着没有hi-regs可用于溢出;这时,需要根据G1-backup判断是否有某个寄存器,其关联的所有溢出生命期都不与当前要溢出的生命期l冲突,如果有这样的寄存器,那么它可用于l的溢出。如果没有空闲的hi-regs用于溢出,那么spill code1执行与传统图着色算法同样的过程,即使用load/store指令将生命期溢出到存储器。 Finally, the present invention can also modify the overflow process of the traditional graph coloring algorithm to insert spill code 1 , the flow of which is shown in FIG. 4 . First of all, when executing the process of constructing the conflict graph G 1 , it is necessary to save a backup conflict graph G 1 -backup; then each time the simplified conflict graph G 1 decides to overflow a certain lifetime l, it is first judged whether there is a free hi- regs can be used for overflow, if there is, then insert move instructions after each assignment of l and before use, and record the information of lifetime l overflow to hi-regs. It should be noted that when each hi-regs has an associated overflow lifetime, it does not mean that no hi-regs can be used for overflow; at this time, it is necessary to judge whether there is a certain register according to G 1 -backup, and its None of the associated overflow lifetimes conflict with the lifetime l currently being overflowed, and if there is such a register, then it can be used for the overflow of l. If there are no free hi-regs for overflow, then spill code 1 performs the same process as the traditional graph coloring algorithm, that is, uses the load/store instruction to spill the lifetime to memory.

以下是一个溢出到寄存器的例子,首先给出在构造冲突图G1执行结束后备份的G1-backup,如下图所示(为了简化说明,示意图中只包括4个生命期,可以认为是冲突图的一小部分): The following is an example of overflowing to registers. First, the G 1 -backup that is backed up after the execution of the construction conflict graph G 1 is completed, as shown in the figure below (to simplify the description, only 4 lifetimes are included in the schematic diagram, which can be considered as conflicts A small part of the figure):

       假设有两个空闲的hi-regs寄存器R1,R2可用于溢出,现在需要溢出生命期3,而之前的溢出操作对hi-regs的使用情况如下表: Assume that there are two free hi-regs registers R1, R2 can be used for overflow, and now overflow lifetime 3 is required, and the usage of hi-regs by the previous overflow operation is as follows:

hi-regs寄存器hi-regs register 已溢出的生命期overflowed lifetime R1R1 11 R2R2 22

表                                                

Figure 435848DEST_PATH_IMAGE001
 hi-regs寄存器溢出情况记录 surface
Figure 435848DEST_PATH_IMAGE001
hi-regs register overflow record

       如流程图4所示,首先得到第一个可用于溢出的hi-regs寄存器R1,并且根据上表可知,生命期1已经溢出到了R1,而当前要溢出生命期3,根据备份的冲突图G1-backup,可知生命期1、3有冲突,故R1不能用于溢出生命期3。接下来,得到第二个可用于溢出的hi-regs寄存器R2,根据G1-backup可知,生命期2、3无冲突,说明生命期3活跃的时间段内,生命期2已经不再使用,故可以将生命期3溢出到R2,将该信息填入表1,并在需要使用生命期2的地方插入move指令将其值从R2拷贝到相应的lo-regs寄存器,在使用结束之后插入move指令将其值从相应的lo-regs寄存器拷贝到R2。 As shown in Flowchart 4, first obtain the first hi-regs register R1 that can be used for overflow, and according to the above table, lifetime 1 has overflowed to R1, and lifetime 3 is currently overflowing, according to the backup conflict graph G 1 -backup, it can be seen that life periods 1 and 3 conflict, so R1 cannot be used to overflow life period 3. Next, get the second hi-regs register R2 that can be used for overflow. According to G 1 -backup, there is no conflict between life periods 2 and 3, which means that life period 2 is no longer used during the active period of life period 3. Therefore, lifetime 3 can be overflowed to R2, and the information can be filled in Table 1, and a move instruction can be inserted where lifetime 2 is needed to copy its value from R2 to the corresponding lo-regs register, and move can be inserted after use The instruction copies its value from the corresponding lo-regs register to R2.

以上所述仅是本发明的优选实施方式,应当指出,对于本技术领域的普通技术人员,在不脱离本发明构思的前提下,还可以做出若干改进和润饰,这些改进和润饰也应视为本发明保护范围内。 The above is only a preferred embodiment of the present invention, it should be pointed out that for those of ordinary skill in the art, without departing from the concept of the present invention, some improvements and modifications can also be made, and these improvements and modifications should also be considered Within the protection scope of the present invention.

Claims (3)

1.一种针对混合长度指令集的寄存器分配方法,其特征在于,包括如下步骤: 1. A register allocation method for a mixed-length instruction set, characterized in that, comprising the steps: 1)查找函数中的所有生命周期,通过设置标志位是否置位来判断设该生命期为A类生命期还是B类生命期; 1) Find all the life cycles in the function, and determine whether the life cycle is a type A life cycle or a B type life cycle by setting whether the flag bit is set; 2)执行图着色寄存器分配方法的Renumber1、build1、coalesce1、spill cost1、和simplify1五个过程,为所有A类生命期分配lo-regs寄存器,得到未进行任何溢出操作的冲突图G12) Execute the five processes of Renumber 1 , build 1 , coalesce 1 , spill cost 1 , and simplify 1 of the graph coloring register allocation method, allocate lo-regs registers for all class A lifetimes, and obtain a conflict graph without any overflow operation G 1 ; 3)计算空闲lo-regs寄存器数m,如果所述冲突图G非空,则空闲lo-regs寄存器数m为0,如果所述冲突图G为空图,则执行图着色寄存器分配方法的select1过程,并根据生成的寄存器分配方案计算空闲的lo-regs寄存器数m,并记录空闲寄存器信息; 3) Calculate the number m of free lo-regs registers, if the conflict graph G 1 is not empty, then the number m of free lo-regs registers is 0, if the conflict graph G 1 is an empty graph, execute the graph coloring register allocation method select 1 process, and calculate the number m of free lo-regs registers according to the generated register allocation scheme, and record the free register information; 4)执行图着色寄存器分配方法的Renumber2、build2、coalesce2、simplify、spill code2和select2过程,为B类生命期分配hi-regs寄存器和m个空闲的lo-regs寄存器,并根据生成的分配方案计算空闲的hi-regs寄存器数量n,并记录空闲寄存器信息; 4) Execute the process of Renumber 2 , build 2 , coalesce 2 , simplify 2 , spill code 2 and select 2 of the graph coloring register allocation method, allocate hi-regs registers and m free lo-regs registers for the lifetime of class B, and Calculate the number n of free hi-regs registers according to the generated allocation scheme, and record the free register information; 5)执行图着色寄存器分配方法的spill code1和select1过程,如果select1过程已经在步骤3)执行则结束所有步骤,如果没有,则重复执行spill code、Renumber1、build1、coalesce1、spill cost1、simplify1   过程直到所述冲突图G为空,然后执行select1过程为A类生命期生成寄存器分配方案; 5) Execute the process of spill code 1 and select 1 of the graph coloring register allocation method, if the process of select 1 has been executed in step 3), then end all steps, if not, repeat the execution of spill code 1 , Renumber 1 , build 1 , coalesce 1 , spill cost 1 , simplify 1 process until the conflict graph G 1 is empty, and then execute the select 1 process to generate a register allocation scheme for the lifetime of class A; Renumber1、build1、coalesce1、spill cost1、simplify1  、spill code1 、select1、Renumber2、build2、coalesce2、spill cost2、simplify、spill code2和select2过程均为图着色寄存器分配方法的步骤; Renumber 1 , build 1 , coalesce 1 , spill cost 1 , simplify 1 , spill code 1 , select 1 , Renumber 2 , build 2 , coalesce 2 , spill cost 2 , simplify 2 , spill code 2 and select 2 processes are graph coloring the steps of the register allocation method; 所述图着色寄存器分配方法包括如下步骤: The graph coloring register allocation method includes the following steps: 11)Renumber:在数据流分析的基础上,找到函数中的所有生命期,并为其分配唯一的编号,其中一个生命期从一个变量的一次定值开始,到对该值的最后一次使用结束; 11) Renumber: On the basis of data flow analysis, find all the lifetimes in the function and assign them unique numbers. One of the lifetimes begins with a fixed value of a variable and ends with the last use of the value ; 12) build:建立冲突图G,所述冲突图G中的结点为生命期,边则表示通过其相连的两个生命期有冲突,不能为它们分配同一个寄存器; 12) build: build a conflict graph G, the nodes in the conflict graph G are life periods, and the edges indicate that the two life periods connected through it have conflicts, and the same register cannot be allocated to them; 13)coalesce:判断是否需要合并生命期,如需要则通过合并生命期删除不必要的复制语句,合并后返回,重新执行步骤12),如不需要合并,则执行步骤14); 13) coalesce: determine whether to merge the life cycle, if necessary, delete unnecessary copy statements through the merge life cycle, return after the merge, and re-execute step 12), if no merge, execute step 14); 14)spill cost:计算每个生命期的溢出代价; 14) spill cost: calculate the overflow cost of each lifetime; 15)simplify:对所述冲突图G进行简化,反复地检查图G,删除G中度数小于可用寄存器数k的节点,删除的同时将该节点压入栈s; 15) simplify: Simplify the conflict graph G, check the graph G repeatedly, delete nodes in G whose degree is less than the number of available registers k, and push the node into the stack s while deleting; 16)spill code:当所述冲突图G中所有节点的度都大于等于k时,需要将溢出代价最小的生命期溢出,即删除其节点,将其压入栈s,并将其标记为溢出,溢出一个生命期之后返回执行步骤11); 16) Spill code: When the degree of all nodes in the conflict graph G is greater than or equal to k, it is necessary to overflow the lifetime with the least overflow cost, that is, delete its node, push it into the stack s, and mark it as overflow , return to step 11 after overflowing a lifetime); 17)select:当所述冲突图G为空时,将栈s中的生命期弹出,为其分配寄存器,并为标记为溢出的生命期插入溢出代码; 17) select: when the conflict graph G is empty, pop the lifetime in the stack s, allocate registers for it, and insert overflow code for the lifetime marked as overflow; 所述lo-regs寄存器为短指令可寻址的寄存器,剩下的为所述hi-regs寄存器,所述标志位是否置位的标准为:将有A类指令引用的生命期设为置位,将只有B类指令引用的生命期设为不置位; The lo-regs register is a register addressable by short instructions, and the rest is the hi-regs register. The standard for whether the flag is set is: set the lifetime referenced by a class A instruction to be set , set the lifetime referenced only by class B instructions to not set; 所述A类指令为一条指令最终生成的机器指令是长指令还是短指令取决于其分配到的寄存器; The type A instruction is a machine instruction finally generated by an instruction, whether it is a long instruction or a short instruction depends on the register to which it is allocated; 所述B类指令为不论其寄存器操作数分配到哪个寄存器,其必然生成一条长指令,或者必然生成一条短指令。 The type B instruction is that no matter which register its register operand is allocated to, it must generate a long instruction, or it must generate a short instruction. 2.根据权利要求1所述的一种针对混合长度指令集的寄存器分配方法,其特征在于,所述标志位设于记录生命期信息的结构体中。 2. A register allocation method for mixed-length instruction sets according to claim 1, wherein the flag bit is set in a structure that records lifetime information. 3.根据权利要求1所述的一种针对混合长度指令集的寄存器分配方法,其特征在于,所述步骤 5)中spill code的过程包括如下步骤: 3. A register allocation method for mixed-length instruction sets according to claim 1, characterized in that the process of spill code 1 in step 5) includes the following steps: 31)在构造所述冲突图G1  保存一个所述冲突图G1   的备份冲突图G1-backup; 31) Save a backup conflict graph G 1 -backup of the conflict graph G 1 when constructing the conflict graph G 1 ; 32)每次simplify1   决定溢出某个生命期l时,首先判断是否有空闲的hi-regs寄存器可用于溢出,如果有,那么在l的每次赋值后和使用前插入move指令,并记录生命期l溢出到hi-regs寄存器的信息;并且当每个hi-regs寄存器都已经有关联的溢出生命期时,根据所述备份冲突图G1-backup判断是否有寄存器关联的所有溢出生命期都不与当前要溢出的生命期l冲突,如果有这样的寄存器,那么它可用于l的溢出,如果没有空闲的hi-regs用于溢出,那么spill code1使用load/store指令将生命期溢出到存储器。 32) Every time simplify 1 decides to overflow a certain lifetime l, first judge whether there are free hi-regs registers that can be used for overflow, if so, then insert a move instruction after each assignment of l and before use, and record the lifetime period l overflows to the information of the hi-regs register; and when each hi-regs register has an associated overflow lifetime, judge whether all overflow lifetimes associated with the register are judged according to the backup conflict graph G 1 -backup Does not conflict with the current lifetime l to be spilled. If there is such a register, it can be used for the overflow of l. If there are no free hi-regs for overflow, then spill code 1 uses the load/store instruction to overflow the lifetime to memory.
CN201110333460.2A 2011-10-28 2011-10-28 A Register Allocation Method for Mixed-Length Instruction Sets Expired - Fee Related CN102360280B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201110333460.2A CN102360280B (en) 2011-10-28 2011-10-28 A Register Allocation Method for Mixed-Length Instruction Sets

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201110333460.2A CN102360280B (en) 2011-10-28 2011-10-28 A Register Allocation Method for Mixed-Length Instruction Sets

Publications (2)

Publication Number Publication Date
CN102360280A true CN102360280A (en) 2012-02-22
CN102360280B CN102360280B (en) 2014-04-23

Family

ID=45585614

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201110333460.2A Expired - Fee Related CN102360280B (en) 2011-10-28 2011-10-28 A Register Allocation Method for Mixed-Length Instruction Sets

Country Status (1)

Country Link
CN (1) CN102360280B (en)

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102831005A (en) * 2012-07-13 2012-12-19 天津国芯科技有限公司 Compiling method for optimizing allocation of register based on C*core processor and compiler
CN104731555A (en) * 2013-12-23 2015-06-24 中兴通讯股份有限公司 Method and device for avoiding conflict among registers
WO2016090812A1 (en) * 2014-12-10 2016-06-16 中兴通讯股份有限公司 Register conflict detection method and device
CN105912304A (en) * 2016-03-31 2016-08-31 中国人民解放军国防科学技术大学 Vector VLIW architecture diagram coloring register grouping allocation method
CN111435309A (en) * 2019-01-11 2020-07-21 中标软件有限公司 Register allocation optimization implementation method

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US4571678A (en) * 1982-11-05 1986-02-18 International Business Machines Corporation Register allocation and spilling via graph coloring
CN1973263A (en) * 2004-06-30 2007-05-30 英特尔公司 Bank assignment for partitioned register banks
CN101710291A (en) * 2009-11-27 2010-05-19 中国科学院声学研究所 Register allocation method for optimizing stack space

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US4571678A (en) * 1982-11-05 1986-02-18 International Business Machines Corporation Register allocation and spilling via graph coloring
CN1973263A (en) * 2004-06-30 2007-05-30 英特尔公司 Bank assignment for partitioned register banks
CN101710291A (en) * 2009-11-27 2010-05-19 中国科学院声学研究所 Register allocation method for optimizing stack space

Cited By (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102831005A (en) * 2012-07-13 2012-12-19 天津国芯科技有限公司 Compiling method for optimizing allocation of register based on C*core processor and compiler
CN102831005B (en) * 2012-07-13 2015-10-28 天津国芯科技有限公司 The Compilation Method of distributing for C*core processor register and compiler
CN104731555A (en) * 2013-12-23 2015-06-24 中兴通讯股份有限公司 Method and device for avoiding conflict among registers
WO2016090812A1 (en) * 2014-12-10 2016-06-16 中兴通讯股份有限公司 Register conflict detection method and device
CN105739947A (en) * 2014-12-10 2016-07-06 中兴通讯股份有限公司 Register conflict detection method and apparatus
CN105912304A (en) * 2016-03-31 2016-08-31 中国人民解放军国防科学技术大学 Vector VLIW architecture diagram coloring register grouping allocation method
CN105912304B (en) * 2016-03-31 2018-04-20 中国人民解放军国防科学技术大学 Vectorial vliw architecture graph coloring register is grouped distribution method
CN111435309A (en) * 2019-01-11 2020-07-21 中标软件有限公司 Register allocation optimization implementation method

Also Published As

Publication number Publication date
CN102360280B (en) 2014-04-23

Similar Documents

Publication Publication Date Title
CN110597616B (en) Memory allocation method and device for neural network
CN102360280B (en) A Register Allocation Method for Mixed-Length Instruction Sets
CN100517240C (en) A Memory Pool Allocation Method for Embedded Operating System
CN106777292B (en) A kind of Data Serialization method and device
US8312424B2 (en) Methods for generating code for an architecture encoding an extended register specification
CN100517242C (en) How to quickly apply for memory
CN104298605A (en) Block grouping method for garbage collection action in solid-state storage device
CN1271524C (en) Static internal storage management method
CN103455433B (en) Memory management method and system
CN101226488B (en) Method and system for solving multi-instance application program conflicts in kernel state address space
US9734620B2 (en) Apparatus and method for graphics state management
TWI444888B (en) Method for allocating registers for processor based on cycle information
AU2009202442A1 (en) Skip list generation
US8560805B1 (en) Efficient allocation of address space resources to bus devices
CN112506813B (en) Memory management method and system
CN111324354B (en) Register selection method integrating register pair requirements
WO2017065629A1 (en) Task scheduler and method for scheduling a plurality of tasks
CN108027736B (en) Runtime code parallelization using out-of-order renaming by pre-allocation of physical registers
CN113238857A (en) Map mapping table multithreading traversal method and device based on memory pool
US20110296140A1 (en) RISC processor register expansion method
CN104657089B (en) A kind of Explore of Unified Management Ideas of placement group on object storage device
US7487336B2 (en) Method for register allocation during instruction scheduling
CN108021678B (en) Key value pair storage structure with compact structure and quick key value pair searching method
CN107623524B (en) A hardware-based Huffman coding method and system
CN114090592B (en) A data processing method, apparatus, device and readable storage medium

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
C14 Grant of patent or utility model
GR01 Patent grant
CF01 Termination of patent right due to non-payment of annual fee
CF01 Termination of patent right due to non-payment of annual fee

Granted publication date: 20140423