The present invention relates to optimization devices and optimization methods for optimizing a computer program, and in particular to an optimization device and an optimization method for optimizing a computer program based on a nature of data during execution.
In general, when compiling a source program written in a programming language, an operation of the source program is analyzed to optimize the source program.
Generally known optimization methods include, for example, constant folding, dead code elimination, and so on. These optimization methods include replacing calculation that can be calculated at the time of compiling with a calculation result, or deleting, from a program that has been compiled, a code and the like which are unreachable at the time of executing the program. Program optimization has conventionally been conducted in accordance with the above-described methods.
In addition, as disclosed by Non-Patent Reference 1, there is a method with which the range of analysis for a source program is expanded and interprocedural constant propagation is performed according to a result of an interprocedural analysis to perform calculation at the time of compiling as much as possible, thereby improving processing time during the execution.
Further, as disclosed by Patent Reference 1, there is a method with which frequency of appearance of a possible value of a variable is calculated by executing a program, and a specialized program is generated when the variable is a specified value, and operations for improving processing time during the execution is performed when the variable is a specified value with high frequency of appearance.
However, the optimization methods as described above are based on the result of analyzing a source program. For that reason, it is not possible to perform optimization depending on characteristics of data to be processed through execution of the program. In addition, even when performing optimization depending on characteristics of data, it is necessary to execute the program prior to the optimization.
The present invention has been conceived to present a solution to the above-stated problems and aims to provide a program optimization device that allows optimization that does not require executing a program prior to the optimization and depends on characteristics of data to be processed through execution of the program.
According to an aspect of the present invention, there is provided a program optimization device which optimizes an input program written in a programming language, the device including: an intermediate code conversion unit configured to convert the input program into an intermediate code; a variable value setting unit configured to obtain, from externally provided information, a possible value of a variable to be used in the input program, and to set the obtained possible value of the variable, on the intermediate code; an intermediate code optimization unit configured to optimize the intermediate code according to the set possible value of the variable; and an output program conversion unit configured to convert the intermediate code optimized by the intermediate code optimization unit into an output program written in a predetermined format.
The above structure allows optimizing an intermediate code according to a possible value of a variable which is used in an input program and obtained in advance. As described above, it is possible to perform optimization depending on characteristics of data to be processed by executing a program, by setting the characteristics of data to be processed by executing the program as a possible value of a variable.
More specifically the variable value setting unit may obtain the possible value of the variable to be used in the input program with an input by a user, and may set the obtained possible value of the variable, on the intermediate code.
Further, the variable value setting unit may obtain the possible value of the variable to be used in the input program from a directive written in the input program, and may set the obtained possible value of the variable, on the intermediate code.
Further, the variable value setting unit may obtain the possible value of the variable to be used in the input program from a predetermined file, and may set the obtained possible value of a variable on the intermediate code.
Further, the intermediate code optimization unit may replace, when the intermediate code is represented in a tree structure, a node that has only one possible value among a plurality of nodes included in the intermediate code, with a node that indicates a constant.
Further, the intermediate code optimization unit may delete from the intermediate code, when the intermediate code is represented in a tree structure, a branch depending on a value not included in the possible value of the node that represents a conditional branch.
Since a possible value of a variable in an input program is specified, it is also possible to generate an output program with a small program size, by using a value calculated from the possible value of the variable to replace the variable with a constant in the case where a result of the calculation is invariably a fixed value or to delete a redundant branch.
Further, the intermediate code optimization unit, when the intermediate code is represented in a tree structure, may convert a node that includes a variable node indicating a variable that has a plurality of possible values into a plurality of nodes each of which includes the variable node that branches according to the possible value, and may replace the variable node included in each of the plurality of nodes resulting from the conversion, with a constant node that indicates the possible value used as a condition for branching.
Since a possible value of a variable in an input program is specified, it is also possible to increase the number of replacement with a constant even with a small number of branches, by using a value calculated from a possible value of a variable, thereby increasing a possibility of higher execution speed.
Further, the intermediate code optimization unit, when the intermediate code is represented in a tree structure, may calculate a possible value of a node from the possible value of the variable, and may optimize the intermediate code according to the possible value of the node.
According to another aspect of the present invention, there is provided a program optimization device which optimizes an input program written in a programming language, in which the device may include: an intermediate code conversion unit configured to convert the input program into an intermediate code; a variable value setting unit configured to obtain, from externally provided information, a possible value of a variable to be used in the input program, and to set the obtained possible value of the variable, on a node in a tree structure representing the intermediate code; an intermediate code optimization unit configured to optimize the intermediate code according to the possible value of the node; and an output program conversion unit configured to convert the intermediate code optimized by the intermediate code optimization unit into an output program written in a predetermined format.
The above structure allows optimizing an intermediate code according to a possible value of a variable which is used in an input program and obtained in advance. As described above, it is possible to perform optimization depending on characteristics of data to be processed by executing a program, by setting the characteristics of data to be processed by executing the program as a possible value of a variable.
Further, the intermediate code optimization unit may replace, when the intermediate code is represented in the tree structure, a node that has only one possible value among a plurality of nodes included in the intermediate code, with a node that indicates a constant
Further, the intermediate code optimization unit may delete from the intermediate code, when the intermediate code is represented in the tree structure, a branch depending on a value not included in a possible value of the node that represents a conditional branch.
Since a possible value of a variable in an input program is specified, it is also possible to generate an output program with a small program size, by using a value calculated from the possible value of the variable to replace the variable with a constant in the case where a result of the calculation is invariably a fixed value or to delete a redundant branch.
Further, the intermediate code optimization unit, may convert, when the intermediate code is represented in the tree structure, a node that includes a variable node indicating a variable that has a plurality of possible values into a plurality of nodes each of which includes the variable node that branches according to a possible value, and may replace the variable node included in each of the plurality of nodes resulting from the conversion, with a constant node that indicates the possible value used as the a condition for branching.
Since a possible value of a variable in an input program is specified, it is also possible to increase the number of replacement with a constant even with a small number of branches, by using a value calculated from a possible value of a variable, thereby increasing a possibility of higher execution speed.
Further, the variable value setting unit may obtain the possible value of the variable to be used in the input program with an input by a user, and may set the obtained possible value of a variable, on the node of the intermediate code.
Further, the variable value setting unit may obtain the possible value of the variable to be used in the input program from a directive written in the input program, and may set the obtained possible value of the variable, on the node of the intermediate code.
Further, the variable value setting unit may obtain the possible value of the variable to be used in the input program from a predetermined file, and may set the obtained possible value of a variable, on the node of the intermediate code.
According to still another aspect of the present invention, there is provided a program optimization method of optimizing an input program written in a programming language, the method including: converting the input program into an intermediate code; obtaining, from externally provided information, a possible value of a variable to be used in the input program, and setting the obtained possible value of the variable, on a node in a tree structure representing the intermediate code; optimizing the intermediate code according to the possible value of the node; and converting the intermediate code optimized by the intermediate code optimization unit into an output program written in a predetermined format.
It should be noted that the present invention can be embodied not only as a program optimization device provided with the characteristic processing units as described above or a program optimization method including processing units included in the device as steps but also as a program which causes a computer to executed the characteristic steps included in the program optimization method.
It should be understood that such a program can be distributed via a recording medium such as a CD-ROM (compact disc read only memory) and a communication network such as the Internet.
According to the present invention, it is possible to present a program optimization device that performs optimization which depends on characteristics of data to be processed through execution of a program and which does not require execution of the program prior to the optimization.
Further, since a possible value of a variable in an input program is specified, it is also possible to generate an output program with a small program size, by using a value calculated from the possible value of the variable to replace the variable with a constant in the case where a result of the calculation is invariably a fixed value or to delete a redundant branch.
Furthermore, since a possible value of a variable in the input program is specified, it is also possible to increase the number of replacement to a constant with a small number of branches by using the value calculated from the possible value, thereby enhancing the possibility of higher execution speed.
A program optimization device according to an embodiment of the present invention will be described below with reference to the drawings.
First, a configuration of the program optimization device will be described with reference to
A program optimization device 101 is a device that converts an input program 111 in which a program to be optimized is written, into an output program 112 that is an optimized program. The program optimization device 101 includes: an intermediate code conversion unit 121; a variable value setting unit 122; a node value calculation unit 123; an intermediate code optimization unit 124; and an output program conversion unit 125. The program optimization device 101 is implemented by executing, on a general computer including a processor and a memory, a program that implements each processing unit. It is to be noted that, a program, a code, data, and so on which are used in processing performed by the program optimization device 101 are temporally stored on a memory or the like on a computer.
The intermediate code conversion unit 121 converts the input program 111 into an intermediate code 131.
The input program 111 is written in existing programming languages such as C language. The intermediate code 131 represents the content of the input program 111 in an existing expression form of programs such as abstract syntax tree.
The variable value setting unit 122 sets, on the intermediate code 131, a possible value of a variable written in the input program 111, as a set of constants. The possible value of the variable can be specified by writing a directive such as a pragma on the input program 111. In addition, it can also be specified by writing information indicating a possible value of a variable on a file that is different from the input program. Further, it can also be specified on the input program by a user input using a graphical user interface (GUI). It is to be noted that, the GUI does not necessarily need to be used in the user input, but other interfaces may be used for receiving the user input.
The node value calculation unit 123 calculates a possible value of each node in a tree structure representing the intermediate code 131 based on the set of constants for the variable determined by the variable value setting unit 122.
The intermediate code optimization unit 124 converts the intermediate code according to one of three conversion methods described below, based on the possible value of each node of the intermediate code 131 calculated by the node value calculation unit 123.
The first conversion method is, in the case where there is only one possible value of a node, a method of replacing the node with a constant node that represents the possible value of the node.
The second conversion method is, in the case where a node represents a conditional branch, a method of deleting, from an intermediate code, a conditional branch based on a value that is not included in a possible value of the node.
The third conversion method is a method of generating branches for the number of elements of set of a constant, and replacing the node with a constant node at each of the branches in the same manner as the first conversion.
The output program conversion unit 125 converts the optimized intermediate code 131 into an output program 112 written in a predetermined format.
The format of the output program 112 includes, for example, C language, assembly language, machine language, and the like in the case where the program optimization device 101 is used as a C compiler, and Verilog language and the like in the case where the program optimization device 101 is used as a high-level synthesis tool.
Next, the processing performed by the program optimization device 101 will be explained.
The intermediate code conversion unit 121 converts the input program 111 into an intermediate code 131 (S211).
Next, the variable value setting unit 122 sets, on the node that indicates a reference of a variable (a reference node of a variable), a set of constants that indicate possible values of the variable (S212). The pragma directives on the line 3 and the line 4 of the input program 301 as shown in
Next, the node value calculation unit 123 calculates a set of constants that indicate possible values of the node that is included in the intermediate code 131 (S213). For example, the intermediate code shown in
Further, the node value calculation unit 123 propagates the set of constants according to data dependency information, in the same manner as constant propagation that is a general optimization method. For example, the variable r that is defined in lines 10 to 14 of the input program 301 as shown in
The set of constants that indicates a possible value of a node is calculated for all of the nodes in the same way, so that the set of constants is calculated for each of the nodes as shown in
Next, the intermediate code optimization unit 124 converts the intermediate code 131 by using the calculated set of constants of a node (S214). There are three conversion methods as described below, for converting the intermediate code 131.
The first conversion is a conversion by which, in the case where the number of elements of the set of constants of a node is one, the node is replaced with a constant node that represents the element of the set.
The second conversion is a conversion by which a branch depending on a value that is not included in a set of constants included in a node is deleted from the intermediate code. The set of constants included by the node S1_s that indicates a condition for branching of a branching statement node S1 of the input program 301 is {1, 3, 5} as shown in
The third conversion is a conversion by which branches for the number of elements of the set of constants are generated and nodes are replaced with constant nodes in each of the branches in the same manner as the first conversion.
In the third conversion, branches for the number of elements are newly generated. For that reason, execution of the third conversion may be restricted. For example, the third conversion may be executed only for an element of which the conversion enables constant folding. Further, the third conversion may be executed by limiting to the case where calculation such as multiplication or division of which the operator strength can be reduced by the conversion is included.
Next, the program optimization device 101 determines whether or not the intermediate code is converted (S215), and in the case where the intermediate code is converted (YES in S215), processing subsequent to the node value calculation processing (S213) is performed again to obtain further optimization.
In the case where the intermediate code is not converted (No in S215), the output program conversion unit 125 generates, from the intermediate code 131 that has been optimized by the intermediate code optimization unit 124, an output program 112 written in a predetermined format (S216).
According to the embodiment of the present invention as described above, a possible value of a variable used in an input program is obtained in advance, and an intermediate code is optimized based on the value. The above structure allows optimization that depends on characteristics of data to be processed with execution of a program without executing the program before optimization. Further, it is possible to reduce dead codes by performing optimization using information on the value that the variable never takes. Thus, it is possible to generate a code with the program size smaller than that obtained through optimization using the conventional technique.
Further, since a possible value of a variable in an input program is specified, it is also possible to generate an output program with a small program size, by using a value calculated from the possible value of the variable to replace the variable with a constant in the case where a result of the calculation is invariably a fixed value or to delete a redundant branch.
Furthermore, since a possible value of the variable in the input program is specified, it is also possible to increase the number of replacement to a constant with a small number of branches by using the value calculated from the possible value, thereby enhancing the possibility of higher execution speed.
The program optimization device 101 according to the embodiment of the present invention has been described above, however, the present invention is not limited to the above embodiment.
For example, in the above-described method according to the embodiment, the input program 111 has been written in C language and a possible value of a variable is specified by the pragma directive. However, the possible value of the variable may also be specified by writing the same information as the information specified by the pragma directive in a file that is different from the input program.
The following describes a method for specifying a possible value of a variable by using a specification file that is different from the input program with reference to
It is to be noted that, in addition to specifying a possible value of a variable by using the pragma directive or the specification file, it may also be possible to display an input program on a screen and input, by using the GUI, a possible value of a variable for the variable that is specified by a user with a pointer.
The following describes with reference to
It is to be noted that, it is also possible that a user inputs possible value of a variable by not using the GUI but by using a character user interface (CUI), for example.
It is to be understood that all of the embodiments disclosed here is for purposes of illustration and not limitation in all respects. The scope of the present invention is indicated not by the above-described description, but by claims, and includes all modifications within the meaning and scope equivalent to the claims.
The program optimization device according to the present invention can be applied to a compiler that generates an object code from a source code of a program and a high-level synthesis tool that generates a register transfer level (RTL) description from a sequential processing program, and is in particular useful in the case of generating an object code or an RTL description on which optimization depending on data to be processed by a program has been performed.
Number | Date | Country | Kind |
---|---|---|---|
2008-024680 | Feb 2008 | JP | national |
Filing Document | Filing Date | Country | Kind | 371c Date |
---|---|---|---|---|
PCT/JP2008/002843 | 10/8/2008 | WO | 00 | 1/13/2010 |