Compiler with delayed conditional branching5450585Abstract An optimization method or apparatus adapted for use on a compiler for generating machine code optimized for a pipeline processor. A compute-compare-branch sequence in a loop is replaced with a compare-compute-branch sequence. A compute-compare-branch sequence is a sequence of instructions to compute the value of one or more variables, execute a comparison involving the variables, and execute a conditional branch conditioned on the comparison. In the compare-compute-branch sequence, the instructions of the compute-compare-branch sequence are reordered as follows. First, the comparison is executed. In the compare-compute-branch sequence, the comparison involves previously set values of the variables. Second, the computation is executed to compute the current values of the variables. Finally, the conditional branch conditioned on the latter comparison is executed so as to have the effect of executing during the previous execution of the sequence. One or more temporary variables store the previous values of the variables. They are set to the values of the variables at the end of the compare-compute-branch sequence. Before execution of the loop, the temporary variables are set so that the condition will not be met the first time the sequence executes. After execution of the loop, a comparison and a conditional branch are executed. The comparison involves the temporary variables, and the conditional branch is conditioned on the comparison. Claims What is claimed is: Description TECHNICAL FIELD
______________________________________
compute.sub.1
compare.sub.1
condition.sub.1
compute.sub.2
compare.sub.2
condition.sub.2
.
.
.
compute.sub.n
compare.sub.n
condition.sub.n
______________________________________
In the above representation, and in the representations below, only the compute-compare-branch sequences of the compute-compare-branch loop are shown. The loop could contain additional instructions. Also, compute.sub.i represents the computation operation of the i.sup.th iteration of the sequence, compare.sub.i represents the comparison operation of the i.sup.th iteration, and condition.sub.i represents the conditional branch operation of the i.sup.th iteration. As used in this document, operation means a task which is performed by the execution of one or more machine instructions. An example of a compute-compare-branch loop would be a loop that computed a value of a variable x, compared x to a variable Xmax which represents the greatest value of x computed so far, and assigns Xmax to x if x is greater than Xmax. Pseudocode for the example loop is as follows:
______________________________________
begin loop
compute x
compare x to Xmax
if (x > Xmax) then
Xmax <- x
end if
end loop
______________________________________
FIG. 2A shows two iterations of a compute-compare-branch sequence on the pipeline processor of FIG. 1. In FIG. 2A, as well as in FIG. 2B, blocks in the right-hand column represent the activities of the branch processor 114, and blocks in the left-hand column represent the activities of one of the arithmetic processors 115. Time proceeds from the top of the columns to the bottom. Each arrow indicates a dependency of the operation to which it points on the operation from which it points. Each arrow pointing from right to left represents a delay between the dispatch of an operation from the branch processor 114 and the execution of that operation by the arithmetic processor 115. Each arrow pointing from left to right represents a delay between the execution of a comparison instruction by the arithmetic processor 115 and the setting of the condition register 120 in the branch processor 114. Each bubble indicates a period of inactivity in the arithmetic processor 115. Looking at FIG. 2A, the branch processor 114 dispatches compute.sub.1 in block 202. Immediately thereafter, the branch processor 114 dispatches compare.sub.1 in block 204. The branch processor 114 then attempts to execute conditions.sub.1. However, it cannot do so until the condition register is set for compare.sub.1. The condition code will be set after the delay indicated by arrow 214 from block 212. Meanwhile, the arithmetic processor 115 processes the first iteration of the sequence as follows. After the delay indicated by arrow 206 from block 202, it receives compute.sub.1 from the branch processor 114 and executes it in block 208. After the delay indicated by arrow 210 from block 204, the arithmetic processor 115 receives compare.sub.1 from the branch processor 114 and executes it in block. 212. Although the operations of blocks 208 and 212 depend on the operations of blocks 202 and 204, there would be no bubble in the arithmetic processor 115 between block 208 and block 212 for the following reasons. First, the branch processor 114 dispatches compare.sub.1 immediately after dispatching compute.sub.1. Second, dispatching an operation takes no longer than executing it. Third, the amount of time it takes for a dispatched instruction to reach the arithmetic processor 115 is constant. Therefore, the arithmetic processor 115 will have received compare.sub.1 by the time it has finished executing compute.sub.1 . After the delay indicated by arrow 214 from block 212, the branch processor 114 executes branch.sub.1 in block 216. Branch.sub.i represents the branch instruction of the i.sup.th iteration. Assuming the condition of condition.sub.1 is not met, the branch processor 114 next dispatches compute.sub.2 in block 218. Immediately thereafter, it dispatches compare.sub.2 in block 220. The branch processor 114 cannot execute condition.sub.2 until the condition register is set for compare.sub.2. After the sum of the amount of the delay indicated by arrow 214 from block 212, the execution time of block 216 and the delay indicated by arrow 222 from block 218, the arithmetic processor 115 receives compute.sub.2 from the branch processor and executes it in block 224. This sum is the amount of time between the execution of blocks 212 and 224 and is represented by bubble 225. After the delay indicated by arrow 226 from block 220, the arithmetic processor 115 receives compare.sub.2 from the branch processor 114 and executes it in block 228. Although the operations of blocks 224 and 228 depend on the operations of blocks 218 and 220, there is no bubble between block 224 and block 228 for the same reason there is no bubble between blocks 208 and 212. After the delay indicated by arrow 230 from block 228, the branch processor 114 executes branch.sub.2 in block 232. Assuming the condition of branch.sub.2 is met, the branch processor 114 next dispatches conditional.sub.-- block in block 234. Conditional.sub.-- block represents the conditional block associated with branch.sub.2. After the sum of the amount of the delay indicated by arrow 230 from block 228, the execution time of block 232 and the delay indicated by arrow 236 from block 234, the arithmetic processor 115 receives conditional.sub.-- block from the branch processor and executes it in block 238. This sum is the amount of time between the execution of blocks 228 and 238 and is represented by bubble 240. A second optimization technique, called loop unrolling, can reduce bubbles resulting from a conditional branch in a compute-compare-branch loop. Loop unrolling is performed by combining the instructions that would have been executed in two or more iterations of the original loop into each iteration of an unrolled loop. The number of iterations of the unrolled loop is reduced accordingly. An unrolled loop which contains the instructions of i iterations of the original loop in each iteration is said to be unrolled to a level of i. Unrolling the loop provides additional independent instructions. The method of the first optimization technique can then be performed to rearrange the unrolled loop so that execution of the independent instructions overlaps the processing of the instructions associated with the conditional branch. A compute-compare-branch loop unrolled to a level of two executes essentially as follows:
______________________________________
compute.sub.1
compare.sub.1
compute.sub.2
compare.sub.2
condition.sub.1
condition.sub.2
compute.sub.3
compare.sub.3
compute.sub.4
compare.sub.4
condition.sub.3
condition.sub.4
.
.
.
compute.sub.(n-1)
compare.sub.(n-1)
compute.sub.n
compare.sub.n
condition.sub.(n-1)
condition.sub.n
______________________________________
The above representation shows only the compute-compare-branch sequences of a loop which could contain additional instructions. The subscripts in the above representation indicate the iteration of the original loop with which the computations, comparisons and conditional blocks are associated. Note that if the original number of iterations is odd, execution is slightly different. For example, the first iteration of the original loop could be executed explicitly before the loop. The loop would then execute essentially as follows:
______________________________________
compute.sub.1
compare.sub.1
condition.sub.1
compute.sub.2
compare.sub.2
compute.sub.3
compare.sub.3
condition.sub.2
condition.sub.3
compute.sub.4
compare.sub.4
compute.sub.5
compare.sub.5
condition.sub.4
condition.sub.5
.
.
.
compute.sub.(n-1)
compare.sub.(n-1)
compute.sub.n
compare.sub.n
condition.sub.(n-1)
condition.sub.n
______________________________________
Whether loop unrolling optimization provides enough independent instructions to eliminate bubbles depends on the nature of the specific loop unrolled. For example, in processing an iteration of an unrolled loop having the operations of original loop iterations i and i-1 on the pipeline processor of FIG. 1, loop unrolling would reduce the bubble preceding condition.sub.(i-1) by the amount of time it would take the arithmetic processor 115 to process compute.sub.i and compare.sub.i. It would reduce the bubble preceding condition.sub.i by the amount of time it would take the arithmetic branch processor 115 to process condition.sub.(i-1). Loop unrolling optimization has several potential weaknesses. First, if the conditional block associated with condition.sub.(i-1) of the compute-compare-branch sequence is either rarely executed or requires little time to execute, loop unrolling would not substantially reduce the bubble associated with condition.sub.i. Because condition.sub.i represents half of the conditional branches in the loop, the performance benefits of unrolling such a loop would be limited. Furthermore, loop unrolling optimization can be performed only on a compute-compare-branch sequence which executes on every iteration of the compute-compare-branch loop. Such a sequence is called an unconditional compute-compare-branch sequence. Loop unrolling therefore does not reduce bubbles associated with a compute-compare-branch sequence within a conditional block in a compute-compare-branch loop. Such a sequence is called a conditional compute-compare-branch sequence. Although the problem of delays associated with conditional branches has been explained in the context the parallel processor of FIG. 1, the problem extends to all pipeline processors. What is needed, therefore, is an optimization technique which improves pipeline processor efficiency in processing a compute-compare-branch loop in which the average computation time of the conditional block associated with the compute-compare-branch sequence is small or in which the compute-compare-branch sequence is conditional. DISCLOSURE OF INVENTION The present invention is directed towards a technique called delayed conditional branching optimization for generating machine code optimized for a pipeline processor. Delayed conditional branching optimization replaces a compute-compare-branch sequence in a compute-compare-branch loop with a compare-compute-branch sequence. A compute-compare-branch loop is a loop having a compute-compare-branch sequence, that is a sequence of instructions to compute the value of one or more variables, execute a comparison involving the one or more variables, and execute a conditional branch conditioned on the comparison. In the compare-compute-branch sequence, the instructions of the compute-compare-branch sequence are reordered as follows. First, the comparison is executed. The comparison involves previously set value or values of the one or more variables. Second, the computation is executed to compute the current value or values of the one or more variables. Finally, the conditional branch conditioned on the comparison is executed. Appropriate measures are taken so that the conditional branch executes as it would have during the previous iteration of the original loop. Note, however, that if the compute-compare-branch sequence is conditional, the conditional branch actually executes as it would have during the last iteration in which the sequence executed. One or more temporary variables could store the previous values of the variables. At the end of the sequence, the temporary variables could be set to the values of the variables. Initializing and terminating operations are performed to assure that the effect of the compare-compute-branch sequence is identical to the effect of the compute-compare-branch sequence. The initializing operation comprises initializing the temporary variables before execution of the compare-compute-branch sequence. Specifically, the temporary variables are set so that the condition will not be met the first time the sequence executes. The terminating operations comprise executing a comparison and a conditional branch after execution of the loop. The comparison involves the temporary variables, and the conditional branch is conditioned on the comparison. If the sequence did not execute, the conditional block will not be executed. Otherwise, appropriate measures are taken so that the conditional branch executes as it would have during the last iteration of the original loop in which the sequence executed. The computation instructions often require substantial processing time. Therefore, placing these instructions between the each comparison and conditional branch often minimizes bubbles. Delayed conditional branching optimization is not limited to a compute-compare-branch sequence which is executed during each iteration of a loop. After the first execution of the sequence, the temporary variables represent the values of the variables computed the previous time the sequence executed, whether or not it executed during the previous iteration of the loop. Delayed conditional branching optimization can therefore be performed on a compute-compare-branch sequences which is part of a conditional block within the loop. The foregoing and other objects, features and advantages of the invention will be apparent from the following more particular description of preferred embodiments of the invention, as illustrated in the accompanying drawings. BRIEF DESCRIPTION OF DRAWINGS The present invention will be more fully understood with reference to the accompanying drawings in which: FIG. 1 shows a block diagram of an example of a computer with pipeline architecture; FIG. 2A shows the execution of a compute-compare-branch sequence on the pipeline processor of FIG. 1; FIG. 2B shows the execution of a compare-compute-branch sequence on the pipeline processor of FIG. 1; FIG. 3 shows a flowchart of the operation of a compiler having delayed conditional branching optimization; FIG. 4 shows a flowchart of the operation of the delayed conditional branch optimization technique of FIG. 3; FIG. 5 shows a block diagram of the compiler having delayed conditional branching optimization of FIG. 3; and FIG. 6 shows a block diagram of the delayed conditional branch optimizer of FIG. 5. BEST MODES FOR CARRYING OUT THE INVENTION The present invention is directed to an optimization technique called delayed conditional branching optimization. Delayed conditional branching optimization restructures a compute-compare-branch loop so as to improve performance by reducing or eliminating the bubbles associated with conditional branches. On pipeline processors with bubble reducing hardware, delayed conditional branching optimization reduces or eliminates bubbles following conditional branches, as well as bubbles in front of conditional branches. When compiled with the compiler having delayed conditional branching optimization, execution of a compute-compare-branch loop which iterates n times is represented as follows:
______________________________________
compute.sub.initial
execution.sub.1
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to .sub.-- previous
execution.sub.2
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
.
.
.
execution.sub.n
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
compare.sub.previous
condition.sub.previous
______________________________________
In the above representation, the i.sup.th execution of the sequence is represented by the four operations under execution.sub.i. If the compute-compare-branch sequence was conditional, execution.sub.i would not necessarily be in the i.sup.th execution of the sequence. An initialization consisting of operation compute.sub.initial is executed before execution of the loop. Compute.sub.initial represents instructions to set a temporary variable, array or other data structure called previous.sub.-- value so that the condition on which the conditional branch depends will not exist the first time compare.sub.previous is executed. Execution of the loop involves executing the four operations associated with execution.sub.i each time the compare-compute-branch sequence is executed. First, compare.sub.previous represents instructions to execute a comparison involving the value or values of previous.sub.-- value. Second, compute.sub.current represents instructions to compute the value of the variable or variables on which the next comparison will depend. Third, condition.sub.previous represents a conditional block dependent on the results of compare.sub.previous. If the results of condition.sub.previous indicate that the conditional block is executed, it executes as it would have during the previous iteration of the original loop. Note, however, that if the compute-compare-branch sequence is conditional, the conditional branch actually executes as it would have during the last iteration in which the sequence executed. Finally, assign.sub.-- current.sub.-- to.sub.-- previous represents instructions to assign the value or values of the variable or variables computed in compute.sub.current to previous.sub.-- value. After execution of the loop, two terminating operations are performed. First, compare.sub.previous represents instructions to execute a comparison involving the value or values of previous.sub.-- value. If the compute-compare-branch sequence executed at least once, this comparison involves the value or values calculated by the compute.sub.current of the last execution. Otherwise, it involves the value or values set by compute.sub.initial. Second, condition.sub.previous represents a conditional block dependent on compare.sub.previous. If the sequence did not execute, condition.sub.previous will not be executed. Otherwise, appropriate measures are taken so that condition.sub.previous executes as it would have during the last iteration of the original loop in which the sequence executed. A compute-compare-branch loop could contain multiple compute-compare-branch sequences independent of each other. If compiled with a compiler having delayed conditional branching optimization, execution of such a loop is similar to the execution of a loop with one compute-compare-branch sequence, except that associated with each sequence would be a previous.sub.-- value and initialization, execution and termination operations. A compute-compare-branch loop could also contain multiple compute-compare-branch sequences which depend on each other. For example, the conditional branch of an outer compute-compare-branch sequence could contain an inner compute-compare-branch sequence. If compiled on a compiler having delayed conditional branching optimization, execution of such a loop would be as follows:
______________________________________
compute.sub.initial
COMPUTE.sub.initial
execution.sub.1
compare.sub.previous
compute.sub.current
condition.sub.previous
COMPARE.sub.previous
COMPUTE.sub.current
CONDITION.sub.previous
ASSIGN.sub.-- CURRENT.sub.-- TO.sub.-- PREVIOUS
assign.sub.-- current.sub.-- to.sub.-- previous
execution.sub.2
compare.sub.previous
compute.sub.current
condition.sub.previous
COMPARE.sub.previous
COMPUTE.sub.current
CONDITION.sub.previous
ASSIGN.sub.-- CURRENT.sub.-- TO.sub.-- PREVIOUS
assign.sub.-- current.sub.-- to.sub.-- previous
.
.
.
execution.sub.n
compare.sub.previous
compute.sub.current
condition.sub.previous
COMPARE.sub.previous
COMPUTE.sub.current
CONDITION.sub.previous
ASSIGN.sub.-- CURRENT.sub.-- TO.sub.-- PREVIOUS
assign.sub.-- current.sub.-- to .sub.-- previous
compare.sub.previous
condition.sub.previous
COMPARE.sub.previous
COMPUTE.sub.current
CONDITION.sub.previous
ASSIGN.sub.-- CURRENT.sub.-- TO.sub.-- PREVIOUS
COMPARE.sub.previous
CONDITION.sub.previous
______________________________________
In the above representation, the i.sup.th execution of the outer sequence is represented by the four operations under execution.sub.i. Operations associated with the outer compute-compare-branch sequence are written in lower case letters, and operations associated with the inner compute-compare-branch sequence are written in capital letters. Execution of the loop is similar to the execution of the loop with a single compute-compare-branch sequences, except that associated with each sequence would be a previous.sub.-- value and initialization, execution and termination operations. Note that the last execution of the inner compute-compare-branch sequence might be executed during the termination operations of the first sequence. Therefore, the termination operations of the outer sequence must be executed before the termination operations of the inner sequence. FIG. 2B illustrates how delayed conditional branching optimization reduces or eliminates bubbles. FIG. 2B shows two executions of the compare-compute-branch sequence on the pipeline processor of FIG. 1. Looking at FIG. 2B, the branch processor 114 dispatches compare.sub.previous in block 250. Immediately thereafter, the branch processor 114 dispatches compute.sub.current. Meanwhile, after the delay indicated by arrow 254 from block 250, the arithmetic processor 115 receives compare.sub.previous from the branch processor 114 and executes the operation in block 256. After the delay indicated by arrow 258 from block 252, the arithmetic processor 115 receives compute.sub.current and executes the operation in block 260. Although the operations of blocks 256 and 260 depend on the operations of blocks 250 and 252, there is no bubble between block 256 and block 260 for the following reasons. First, the branch processor dispatches compute.sub.current immediately after dispatching compare.sub.previous. Second, dispatching an operation takes no longer than executing it. Third, the amount of time it takes for a dispatched instruction to reach the arithmetic processor 115 is constant. Therefore, the arithmetic processor 115 will have received compute.sub.current by the time it has finished executing compare.sub.previous. After the delay indicated by arrow 262 from block 256, the condition register 120 is set for compare.sub.previous. The branch processor 114 then executes branch.sub.previous in block 253. Assuming the condition of branch.sub.previous is not met in the first execution of the compare-compute-branch sequence, the branch processor next dispatches compare.sub.previous, the first operation of the second execution of the compare-compute-branch sequence, in block 264. After the sum of the amount of the delay indicated by arrow 262 from block 256, the execution time of block 253 and the delay indicated by arrow 268 from block. 264, the arithmetic processor 115 receives compare.sub.previous from the branch processor and executes it in block 270. This sum would often be no greater than the execution time of block 260. Therefore, there would often be no bubble between block 260 and block 270. In block 266, the branch processor 114 dispatches compute.sub.current. After the delay indicated by arrow 272 from block 266, the arithmetic processor 115 receives compute.sub.current, and it processes the operation in block 274. After the delay indicated by arrow 276 from block 270, the condition register for compare.sub.previous is set for branch.sub.previous. The branch processor 114 then executes branch.sub.previous in block 278. Assuming the condition of branch.sub.previous is met in the second execution of the compare-compute-branch sequence, the branch processor next dispatches conditional.sub.-- block, the conditional block associated with branch.sub.previous, in block 280. After the sum of the amount of the delay indicated by arrow 276 from block 270, the execution time of block 278 and the delay indicated by arrow 282 from block 280, the arithmetic processor 115 receives conditional.sub.-- block from the branch processor and executes it in block 284. This sum would often be no greater than the execution time of block 274. Therefore, there would often be no bubble between block 274 and block 284. FIG. 3 shows a flowchart of the operation of the compiler having delayed conditional branching optimization. First, block 310 is executed to convert source code to intermediate code. Second, block 312 is executed to convert the intermediate code into optimized intermediate code. Finally, block 314 is executed to convert the optimized intermediate code to optimized machine code. Note that the block 314 could instead operate before block 312. In such a configuration, block 314 would convert intermediate code to machine code and block 312 would convert the machine code to optimized machine code. FIG. 4 shows a flowchart of the operation of the delayed conditional branching optimization technique. This technique is part of the optimization performed in block 312 of FIG. 3. First, block 410 is executed to read the next block of intermediate code. Second, diamond 412 is executed to determine whether the block is a compute-compare-branch loop. If not, flow of control returns to block 410. Otherwise, flow of control goes to block 414. Note that block 410 and diamond 412 operate to detect a compute-compare-branch loop. Various other ways to detect such a loop would be obvious to one of ordinary skill in the art. Also, the loop detection method could be incorporated into the optimizer, and not separate for the delayed conditional branching optimization technique. Block 414 performs the initialization for each compute-compare-branch sequence in the loop by generating the instructions represented by compute.sub.initial. The compute.sub.initial operations set the previous.sub.-- value associated with each sequence so that the condition on which the conditional branch of that sequence depends will not exist the first time compare.sub.previous is executed. After execution of block 414, block 416 executes to restructure the loop. Block 416 replaces each compute-compare-branch sequence with the instructions represented by compare.sub.previous, compute.sub.current, condition.sub.previous and assign.sub.-- current.sub.-- to.sub.-- previous. After execution of block 416, block 418 executes to generate the loop termination instructions associated with each compute-compare-branch sequence in the loop. The loop termination instructions are the instructions represented by compare.sub.previous and condition.sub.previous. Block 418 places the termination instructions associated with each compute-compare-branch sequence after the termination instructions of any compute-compare-branch sequence on which it depends. Note that once diamond 412 detects a compute-compare-branch loop, blocks 414, 416 and 418 could execute in any order. After these three blocks are executed, flow of control returns to block 410 to search for the next compute-compare-branch loop. Thus, flow of control returns to block 410 after execution of block 418. FIG. 5 shows a block diagram of a compiler having delayed conditional branching optimization. A front end 510 converts source code into intermediate code. The front end 510 corresponds to block 310 of FIG. 3. A back end 512 converts the intermediate code into optimized machine code. The back end includes an optimizer 514 and a machine code generator 516. The optimizer 514 converts the intermediate code into optimized intermediate code and corresponds to block 312 of FIG. 3. The optimizer includes a delayed conditional branch optimizer 518. The delayed conditional branch optimizer 518 corresponds to the delayed conditional branch optimization technique of FIG. 4. The machine code generator 516 converts the optimized intermediate code into optimized machine code. It corresponds to block 314 of FIG. 3. Note that the positions of the machine code generator 516 and the optimizer 514 could be reversed. In such a configuration, the machine code generator 516 would convert intermediate code into machine code, and the optimizer 514 would convert machine code into optimized machine code. FIG. 6 is a functional block diagram of the delayed conditional branch optimizer 518. The delayed conditional branch optimizer 518 consists of a compute-compare-branch detector 610 and a loop restructurer 611. The loop restructurer 611 contains an initial branch precluder 612, a sequence replacer 614 and a loop terminator 616. The compute-compare-branch detector 610 continually scans the intermediate code for compute-compare-branch loops. It detects such loops in the manner explained in the text accompanying block 410 and diamond 412 of FIG. 4. As with block 410 and diamond 412, the compute-compare-branch detector 610 could be an element of the optimizer 514 which is not separate for the delayed conditional branch optimizer 518. As discussed with respect to FIGS. 3 and 4, the input to the compute-compare-branch detector 610 may be intermediate code or machine code. This input to the computer-compare-branch detector 610 is referred to as the delayed conditional branch input code. Until detecting such a loop, the compute-compare-branch detector 610 outputs the intermediate code unmodified as delayed conditional branch output. Each time it detects such a loop, it sends compute-compare-branch code which includes the loop to the loop restructurer 611. The components of the loop restructurer 611 (that is, the initial branch precluder 612, the sequence replacer 614 and the loop terminator 616) then operate in any order to generate the delayed conditional branch output. The initial branch precluder 612 generates the instructions represented by compute.sub.initial to perform the initialization for each compute-compare-branch sequence in the loop. It operates in the manner explained in the text accompanying block 414 of FIG. 4. The sequence replacer 614 generates the instructions represented by compare.sub.previous, compute.sub.current, condition.sub.previous and assign.sub.-- current.sub.-- to.sub.-- previous to replace the compute-compare-branch sequence with a compare-compute-branch sequence. It operates in the manner explained in the text accompanying block 416 of FIG. 4. The loop terminator 616 generates the instructions represented by compare.sub.previous and condition.sub.previous to terminate each compute-compare-branch sequence in the loop. It operates in the manner explained in the text accompanying block 418 of FIG. 4. Once the initial branch precluder 612, the sequence replacer 614 and the loop terminator 616 have executed, the compute-compare-branch detector 610 scans the intermediate code searching for the next compute-compare-branch loop. Several variations of the compiler having delayed conditional branching optimization are possible. For example, one variation performs delayed conditional branching optimization only on unconditional compute-compare-branch sequences of compute-compare-branch loops. This variation could be implemented by having diamond 412 of FIG. 4 detect only unconditional compute-compare-branch sequences. A second variation performs delayed conditional branching optimization only on conditional compute-compare-branch sequences of compute-compare-branch loops. This variation could be implemented by having diamond 412 of FIG. 4 detect only conditional compute-compare-branch sequences. A third variation performs delayed conditional branching optimization on conditional compute-compare-branch sequences and loop unrolling optimization on unconditional compute-compare-branch sequences of compute-compare-branch loops. It could, for example, unroll unconditional loops to a level of two. When compiled on the third variation of the compiler, a compute-compare-branch loop with a compute-compare-branch sequence conditioned on the conditional branch of an unconditional compute-compare-branch sequence executes essentially as follows:
______________________________________
compute.sub.initial
COMPUTE.sub.1
COMPARE.sub.1
COMPUTE.sub.2
COMPARE.sub.2
CONDITION.sub.1
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
CONDITION.sub.2
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
COMPUTE.sub.3
COMPARE.sub.3
COMPUTE.sub.4
COMPARE.sub.4
CONDITION.sub.3
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
CONDITION.sub.4
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
.
.
COMPUTE.sub.(n-1)
COMPARE.sub.(n-1)
COMPUTE.sub.n
COMPARE.sub.n
CONDITION.sub.(n-1)
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
CONDITION.sub.n
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
compare.sub.current
condition.sub.current
______________________________________
In the above representation, the numeric subscripts to the operations indicate the iteration of the original loop with which the operation is associated. The operations generated by loop unrolling optimization are written in capital letters. The operations generated by delayed conditional branching optimization are written in lower case letters. Note that if the original number of iterations is odd, execution is slightly different. For example, the first iteration of the original loop could be executed explicitly before the loop. The loop would then execute essentially as follows:
______________________________________
compute.sub.initial
COMPUTE.sub.1
COMPARE.sub.1
CONDITION.sub.1
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
COMPUTE.sub.2
COMPARE.sub.2
COMPUTE.sub.3
COMPARE.sub.3
CONDITION.sub.2
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
CONDITION.sub.3
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
COMPUTE.sub.4
COMPARE.sub.4
COMPUTE.sub.5
COMPARE.sub.5
CONDITION.sub.4
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
CONDITION.sub.5
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
.
.
COMPUTE.sub.(n-1)
COMPARE.sub.(n-1)
COMPUTE.sub.n
COMPARE.sub.n
CONDITION.sub.(n-1)
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub. -- to.sub.-- previous
CONDITION.sub.n
compare.sub.previous
compute.sub.current
condition.sub.previous
assign.sub.-- current.sub.-- to.sub.-- previous
compare.sub.current
condition.sub.current
______________________________________
The third variation of the compiler having delayed conditional branching optimization would be implemented by having diamond 412 of FIG. 4 detect only conditional compute-compare-branch loops, and by including a loop unrolling optimizer 520, as well as the delayed conditional branching optimizer 518, in the optimizer 514 of FIG. 5. The loop unrolling optimizer 520 would perform loop unrolling in the manner described in the Background section of this document. While the invention has been particularly shown and described with reference to preferred embodiments thereof, it will be understood by those skilled in the art that the foregoing and other changes in form and details may be made therein without departing from the spirit and scope of the invention.
|
Same subclass Same class Consider this |
||||||||||
