Abstract:
Compilers are tools that generate efficient mappings from programs to machines. A Java &#34;Just-In-Time&#34; runs as part of an application, and as such, it must be fast and efficient in its use of memory. To achieve good performance and further optimize code generation, the present invention introduces a method for eliminating common subexpressions from Java bytecodes. The method of the present invention first loads a code stream containing sequences of computer code into computer memory. The expression value for a first expression of a first code sequence is computed and stored in a memory location. A tag is assigned to the memory location holding this expression value for tracking which expression sequences&#39; values are held in memory locations. As code compilation continues, the code selector looks ahead in the code stream to see if any upcoming expression sequences already have expression values stored in a memory location. The code selector compares the expression of a second code sequence with the code sequences annotated by the tags of expression values currently stored in memory. If the second code sequence matches a sequence already associated with a memory location, then the value of the matched sequence is pushed from the memory location onto a stack, and the computations of the expression of the second code sequence is skipped. If the second expression does not match any of the expressions represented by the tags, the expression value of the second expression is calculated and stored in a memory location. This memory location is then annotated with its own expression tag for future comparisons with upcoming expressions in the code stream.

Description:
BACKGROUND OF THE INVENTION 
     1. Field of the Invention 
     This invention relates to the field of computer software optimization. More particularly, the present invention relates to a method for eliminating common subexpressions in computer code. 
     2. History of the Prior Art 
     Computer programs are generally created as source code. The source code is compiled into object code for execution on a specific processor. Hence, most programs exist as compiled object code in computer systems. The compiled object code is usually designed/compiled to operate on only one particular operating system and/or on only one particular computer processor architecture. In order to use a certain program on several different computer systems, the original source code must be compiled into object code for each different operating system and each different processor architecture. 
     The popularization of the World Wide Web has exacerbated a problem for software developers trying to create software for networked consumer devices. While millions of people around the globe are surfing the Internet and browsing web pages with their computers, not all of those computers are the same. One person may be using a Macintosh, another a PC, and yet another user with a UNIX machine. Hence computer programs need to be designed to support multiple host architectures and to allow secure delivery of its software components. As a result, Sun Microsystems Inc., developed Java. Java is a general purpose, concurrent object oriented programming language that supports a highly portable programming environment. Java was designed such that its code could survive transport access networks and operate on any client, while assuring the client that the program was safe to run. 
     A Java program is created by compiling source code written in Java&#39;s well defined source code format into a compact, architecture-neural object code known as Java bytecodes. Compilation normally consisting of translating Java source code into a machine independent bytecode representation. Bytecodes are executed at runtime by an interpreter residing on the client computer. Runtime activities may include loading and linking the classes needed to execute a program, native machine code generation and dynamic optimization of the program, and actual program execution. 
     Java bytecodes are designed to operate on a Java Virtual Machine (VM). The Java Virtual Machine is an abstract computing machine that has its own instruction set and uses various memory areas. A Java VM is not an actual hardware platform, but rather a low level software emulator that can be implemented on many different processor architectures and under many different operating systems. The Java VM reads and interprets each bytecode so that the instructions may be executed by the native microprocessor. Hence compiled Java bytecodes are capable of functioning on any platform that has a Java Virtual Machine implementation available. However, bytecode interpretation detracts from processor performance since the microprocessor has to spend part of its processing time interpreting bytecodes. Java &#34;Just-In-Time&#34; (JIT) compilers were introduced to improve the performance of Java Virtual Machines. A Java JIT compiler translates Java bytecodes into the processor&#39;s native machine code during runtime. The processor then executes the compiled native processor object code like any other native program. Such compiled Java programs execute much faster than Java programs that are executed using a Java interpreter. 
     Although a Just-In-Time compiled Java program executes faster than an interpreted Java program, the performance of such Just-In-Time compiled Java programs can be further improved. Since Just-In-Time compilations must be performed very quickly to provide an immediate response to the user, Just-In-Time compiled code is usually not very well optimized. The importance of compilation speed in a Java JIT compiler creates the additional requirement that optimization algorithms be lightweight and effective. 
     SUMMARY OF THE INVENTION 
     The present invention introduces a method for eliminating common subexpressions from Java bytecodes. The method of the present invention first loads a code stream containing sequences of computer code into computer memory. The expression value for a first expression of a first code sequence is computed and stored in a memory location. A tag is assigned to the memory location holding this expression value for tracking which expression sequences&#39; value are held in memory locations. As code compilation continues, the code selector looks ahead in the code stream to see if any coming expression sequences already have expression values stored in a memory location. The code selector compares an expression of a second code sequence with the code sequences annotated by the tags of expression values currently stored in memory. If the second code sequence matches a sequence already associated with a memory location, then the expression value of the matched sequence is pushed from the memory location onto a stack, and the expression of the second code sequence is skipped in the code stream. If the second expression does not match any of the expressions represented by the tags, the expressions value of the second expression is calculated and stored in a memory location. This memory location is then annotated with its own expression tag for future comparisons with upcoming expressions in the code stream. 
     Other objects, features, and advantages of the present invention will be apparent from the accompanying drawings and from the detailed description that follows. 
    
    
     BRIEF DESCRIPTION OF THE DRAWINGS 
     The present invention is illustrated by way of example and not limitations in the figures of the accompanying drawings in which like references indicate similar elements, and in which: 
     FIG. 1 is a flow chart illustrating the major phases of one embodiment of a Java &#34;Just-In-Time&#34; compiler; 
     FIG. 2 illustrates the steps of downloading, compiling, and running a Java program in a Java Virtual Machine that compiles the code before execution; 
     FIG. 3 illustrates a block digram of a Java Virtual Machine on a client computer system; 
     FIG. 4 is a flow chart representing the steps comprising one embodiment of the present invention; and 
     FIG. 5 shows the optimization of example source code by one embodiment of the present invention. 
    
    
     DETAILED DESCRIPTION OF THE INVENTION 
     A method for eliminating common subexpressions from Java byte codes is disclosed. In the following description, for purposes of explanation, specific nomenclature is set forth to provide a thorough understanding of the present invention. However, it will be apparent to one skilled in the art that these specific details are not required in order to practice the present invention. For example, the present invention has been described with reference to Java object-oriented programming language. Although the following embodiments are described with reference to the Java programming language and Java &#34;Just-In-Time&#34; (JIT) compilers, other embodiments are applicable to other types of programming languages that use compilers. The same techniques and teaching of the present invention can easily be applied to other types of compiled object code. 
     An intended advantage of one of more embodiments described below is the tailoring of the common subexpression elimination algorithm to byte codes. Another intended advantage is the lightweight aspect of the invention in terms of both execution time and auxiliary data structures. Other intended advantages of one or more embodiments may include speed and efficiency. 
     The Java programming language introduces new challenges to the compiler writer because of the &#34;Just-In-Time&#34; (JIT) nature of the compilation model. A static compiler converts Java source code in a verifiably secure and compact architecture-neural intermediate format called Java bytecodes. A Java Virtual Machine (VM) later interprets the bytecode instructions a runtime. In order to improve runtime performance, a JIT compiler may convert bytecodes into native code at runtime. Although offline compilation of bytecodes into native code is possible, it cannot always be performed because not all of the necessary Java class files are guaranteed to be available at the start of program execution. Therefore, a bytecode compiler needs to be prepared to execute dynamically at runtime; hence the &#34;JIT compiler.&#34; 
     As such, compilation speed becomes much more important in a Java JIT compiler than in a traditional compiler. While the traditional methodology of compiler performance measurement would ignore compilation time, performance measurements of JIT compilers must include the compilation time since the JIT compilation is included in the overall program execution time. As a result, it is extremely important for the JIT compiler organizations to be lightweight and effective. Therefore, a lightweight implementation of JIT compiler optimization such as common subexpression elimination would be desirable. 
     The present invention introduces a new common subexpression elimination (CSE) method. Traditional common subexpression elimination algorithms, which are based on data flow analysis and value numbering, are expensive in both time and space. Hence, such complex techniques should be avoided in JIT compiler implementations. The fast, lightweight CSE method of the present invention uses Java bytecodes as a compact representation of expressions to eliminate common subexpressions and focuses on common subexpressions within extended basic blocks. A basic block is defined as a control-flow region with a single entry point and any number of exits. The method of the present invention is not only time linear, but its memory requirements are also small. The present invention method requires memory for the bytecodes and a constant amount of memory proportional to the number of physical machine registers on the target machine. 
     Consider the expression &#34;x+y&#34;. Assuming &#34;x&#34; and &#34;y&#34; are local variables 1 and 2, respectively, the expression&#39;s instruction sequence is [iload --  1, iload --  2, iadd]. Because the bytecodes of the &#34;iload --  1&#34;, &#34;iload --  2&#34;, and &#34;iadd&#34; instructions are &#34;0x1b&#34;, &#34;00x1c&#34;, and &#34;0x60&#34;, respectively, the value &#34;0x1b1c60&#34; represents the expression &#34;x+y&#34;. Hence, when the value 0x1b1c60 appears as a subsequence in the bytecode instruction stream, the expression &#34;x+y&#34; can be represented with an expression tag. An expression tag comprises of a &lt;offset,length&gt; pair. The offset or index is the distance of the start of the current expression from the beginning of the byte code stream. In one embodiment, the maximum size of the code stream is 2 16 . Therefore, an expression tag can be represented concisely using a single double-word (dword) (16 bits for the offset and 16 bits for the length). So in order to detect whether the expressions represented by tags &lt;offset1,n&gt; and &lt;offset2,n&gt; represent the same syntactic expression, a simple string compare the subsequences of length &#34;n&#34; starting at &#34;offset1&#34; and&#34;offset2&#34;. The code selector tracks the expression values held in the scratch registers by annotating each scratch register R with the tag of the expression that R contains. Expression tags are initialized to hold no values at the beginning of basic blocks that have branch labels. 
     Before selecting code for a bytecode B, the code selector looks ahead in the stream to see whether the expression starting from B matches one already associated with a scratch register. In one embodiment, the registers are checked in decreasing order of subsequence length to match the largest sized expression first. Expression lengths are limited to 16 bytecodes so that the compilation time remains linear. If a code sequence matches a known expression stored in a register, then the code selector pushes the register contents onto the stack and skips over the common subexpression in the bytecode stream. But if a match is not found, the code selector selects an instruction sequence for bytecode B and updates the expression tag of the register containing the result of this instruction sequence. 
     An expression is only valid for a limited period of time. There are two ways that the availability of an expression E held in a register R is killed. (1). By instructions that modify the value of the register R that contains the computed expression E. If register R is a caller-saved register, then a call site kills the availability of E in R. In this case, the expression tag of R is updated to indicate that the R contains no value. The availability of E in R is also killed when the register manager allocates R for reuse by the code selector. Here, the code selector updates the expression tag of T to indicate that it contains a new expression or no value if R was used to hold a temporary value. (2). By assignments or method calls that potentially modify a source value is part of expression E. Expression E may have variables, array elements, and object fields as source operands. Any of which can be modified by method calls or assignments. At a method call, the code selector would kill the availability of all expressions that contain loads of array elements or object fields. During an assignment, the code selector kills the availability of all expressions that load or may load the assigned variable, object field, or array element. Hence an expression value is discarded when variables used in the corresponding expression are modified. An expression value is also discarded when any array element in an array containing an a value used by the expression is modified. 
     In the one embodiment, the information about the set of variables and object fields used by an expression is held in kill sets. One kill set is associated with each register managed by the register manager. Each variable has a unique index and each object field has a unique constant pool index. This allows a kill set to be maintained as a bit vector with the first few bits dedicated to variable indices and the rest of the bits dedicated to object field indices. Whenever an object field assignment bytecode (i.e., a putfield bytecode) assigns a new value to an object field with index I, the code selector kills the availability of a register R if the I&#39;th object field index is set in R&#39;s kill set bit vector. The code selector performs similar bookkeeping for assignments to variables. One way of saving memory space and compilation time is to limit the size of each kill set bit vector to 256 bits. 
     The code selector may take more conservative approach to killing expressions that load array elements. Rather than performing expensive alias analysis, the JIT in the preferred embodiment takes advantage of the Java feature that there is no aliasing between arrays with different element types. Each register R has a fixed-size bit vector that contains the set of array element types loaded by the expression held in R. When the code selector encounters an assignment to an array element of type T, it kills all registers containing expressions that load array elements of type T. The code selector uses this bit vector in conjunction with an additional bit flag that indicates whether an expression has any object field references to detector expressions that are killed by method calls. 
     Phases of a Java JIT Compiler 
     Referring now to FIG. 1, there is a flow chart illustrating the major phases of one embodiment of a Java &#34;Just-In-Time&#34; compiler. FIG. 1 shows the five major phases of a Java JIT as in one embodiment. The prepass phase 110 performs a linear-time traversal of the bytecodes to collect information needed for the global register allocation and for implementing garbage collection support. The global register allocation phase 120 assigns physical registers to local variables. The code generation phase 130 generates native processor instructions and performs optimizations including common subexpression elimination. The code emission phase 140 copies the generated code and data sections to their final locations in a memory. The code and data patching phase 150 fixes up relocations in the emitted code and data sections; for instance, offsets of forward branches, addresses of code labels in switch table entries, and the address of switch tables in the read-only data section. With the exception of the global register allocation phase 110, all phases are linear in time and space. 
     Running a Java Class File 
     An example of running a Java program in a networked computer environment is provided with reference to FIG. 2 and FIG. 3. FIG. 2 illustrated the steps of downloading, compiling, and running a Java program in a Java Virtual Machine that compiles the code before execution. FIG. 3 illustrates a block diagram of the elements in a client computer system 300 equipped with a Java Virtual Machine to interpret and compile Java class files. The client computer system 300 includes computer hardware 310 controlled by an operating system 320. The computer hardware further comprises of computer memory 312 and machine registers 314. The system 300 also includes a Java Virtual implementation 330 for running Java class files 360. In the present embodiment, the Java Virtual Machine 330 running on the client computer system 300 relies on services from the underlying operating system 320 and the computer hardware 310. Furthermore, the Java Virtual Machine 330 may utilize a Java Interpreter 332 for interpreting Java class files 360 or a Java &#34;Just-In-Time&#34; (JIT) computer 334 to generate compiled Java code. 
     In a networked environment, a user would first access a computer server through the network and download the desired Java class file 360 into a client computer system 300 as in step 210. After the class file 360 has been downloaded, the Java Virtual Machine 330 verifies the class file at step 220. Step 220 of verifying the class file is to ensure that the program will not cause security violations not will it cause harm to the computer system resources. After the Java program has been verified, a Java JIT compiler 334 compiles the Java class file and generates compiled Java code 340 in the form of native processor code at step 230. Then this compiled Java code 340 is directly executed on the computer hardware 310 at step 340. Java programs which have been compiled and translated into the native code of the processor in the client system 300 execute much faster than Java programs that executed using a Java interpreter. In order to maintain the state of the Java Virtual Machine 300 and make system calls, the compiled Java code 340 makes calls 350 into the Java Virtual Machine 330. 
     Although the above example describes the distribution of a Java class file via a network, Java programs may be distributed by way of other computer readable mediums. For instance, a computer program may be distributed through a computer readable medium such as a floppy disk, a CD ROM, a carrier wave, or even a transmission over the internet. 
     Eliminating Common Subexpressions 
     FIG. 4 is illustrates a flow diagram of the common subexpression elimination method of the present invention during the code generation process. Initially, at step 410, code selector takes a bytecode or expression from the code stream. Before selecting code for the bytecode, the selector looks ahead in the stream to see whether the expression starting at this bytecode matches one already associated with a scratch register. First, the compiler checks to see if any expressions are stored in the registers at step 420. If no stored expressions are available, the code selector selects an instruction sequence for the bytecode at step 460. Then the expression value is stored in a register at step 462 and the expression tag of the register containing the result of the instruction sequence is updated. The code selector reaches step 470 and checks to see if the end of the code stream has been reached. If it has not, then the code selector goes back to step 410 for the next bytecode. In the present embodiment, the code selector performs step 480 between the process of the code selector looping from step 470 to step 410. Step 480 checks to see if any of the stored values have changed in value. In other words, the check is to see if the availability of any stored expression are now invalid. Stored expression may become invalid when (1) instructions modify the value of the register R that contains the computed expression E, or (2) assignments or method calls modify a source value that is part of expression E. If the code selector discovers that a stored value has changed, then the affected stored value is killed at step 482. Killed in this sense means that the stored expression value is discarded. Then the code selector continues on back to step 410. 
     But if at step 420, the code selector finds that stored expression values are available in registers, the selector then compares the current expression with a stored expression at step 430. The registers are checked in decreasing order of subsequence length to match the largest sized expression. If the comparison fails and the code selector finds that not all the stored expressions have been compared at step 440, then the selector loops back to step 430 and compares the current bytecode with the next largest expression in the registers. If there are no more stored expressions at step 440, the code selector selects an instruction sequence for the bytecode at step 442. As before the expression value is stored in a register at step 444 and the expression tag of the register containing the result of the instruction sequence is updated. Then the code selector reaches step 470 and checks to see if the end of the cod stream has been reached. If it has not, then the code selector loops back to the step 410 for the next bytecode. 
     If the expression comparison at step 430 indicates that the expression are syntactically identical, then the code selector pushes the contents of the register onto the stack at step 450. Then at step 452, the code selector skips over the common subexpression in the bytecode stream. Now the selector reaches step 470 as in the other paths described earlier. If the end of the code stream has not been reached, the code selector returns to step 410 for the next bytecode in the code stream. If the stream has ended, the phase is completed. 
     An Example 
     FIG. 5 shows the optimization of example source code by one embodiment of the present invention. The partial code stream in FIG. 5 is an example of that found in a Java program or applet. The entries in the first column 510 show Java bytecode sequences. Each bytecode is numbered with its index in the bytecode stream. The second column 520 shows the native code generated by a Java JIT compiler. This column 520 also shows the points at which the code selector generates instructions. For instance, the code selector generates the first native instruction when it encounters the third bytecode at line 587. The last three columns of the table (columns 530, 540, and 550) show the expression tags held in the scratch registers at each point during code selection. In this example, global register allocation has assigned the edp register to variable 0, while variables 5, 18, and 20 are in memory. Accesses to variables 5, 18, and 20 are based off the stack pointer (esp). 
     When the code selector generates the first native instruction (mov ecx,[edp+04h]) at line 587, it annotates register ecx with the expression tag &lt;583,4&gt; to indicate that the ecx register holds the value of the expression formed by the first two bytecodes 583 and 584. The code selector delays generating code for bytecodes 590 (getfield #44), 593 (lload 18), and 595 (l2i), until it encounters bytecode (596 (aaload). The values of bytecodes (l2i) 595 and (getfield #44) 590 are kept in registers ecx and eax, respectively. The expression tags of these registers are updated accordingly to &lt;593,3&gt; and &lt;583,10&gt;. As the value of bytecode 596 (aaload) is loaded into the edx register, the expression tags &lt;583,10&gt; of eax and &lt;593,3&gt; of ecx are combined to form the expression tag &lt;583,14&gt; of register edx. 
     When the code selector finishes generating code for byte code 601 (istore 5) and scans the next bytecode (aload --  0 at index) 603, there are three expressions, &lt;583,18&gt;, &lt;593,3&gt; and &lt;583,14&gt; being held in the three scratch registers. The selector then attempts to match a CSE by searching the expression tags in the order of &lt;583,18&lt;, &lt;583,14&gt;, and &lt;593,3&gt;. Since the expression &lt;603,14&gt; matches &lt;583,14&gt;, a CSE has been detected. The code selector pushes the contents of register edx onto the stack. Then the selector skips bytecodes 603 to 616, and continues generating code from bytecode 617 (getfield #49). 
     In the foregoing specification, the invention has been described with reference to specific exemplary embodiments thereof. It will, however, be evident that various modifications and changes may be made thereto without departing from the broader spirit and scope of the invention as set forth in the appended claims. The specification and drawings are accordingly to be regarded as illustrative than a restrictive sense.