Method and circuit for delayed branch control

Abstract: In a processor employing a delayed branch method, delayed branch control which does not complicate instruction execution sequence and improves the readability of a program on the assembler level is implemented without providing a control bit in an instruction code. The delayed branch control according to the present invention involves the use of a branch-information storing circuit for storing the occurrence or nonoccurrence of a branch in a specified one of a continuous sequence of cycles immediately before a current execute cycle which are equal in number to delay slots in the processor. In executing a delayed branch instruction, when the branch-information storing circuit stores the occurrence of a branch in the specified cycle, a branch is disabled. This prevents instruction execution sequence from being complicated even when individual branch conditions for consecutive delayed branch instructions are satisfied, so that the program on the assembler level is improved in readability. The branch-information storing circuit can simply be composed of a combination of a shift register, a counter, and a latch.

Description: The present invention relates to control of a branch operation in a processor. More particularly, it relates to control of a delayed branch operation in a processor employing a delayed branch method. In so-called pipeline processing whereby instructions are executed in a pipelining manner, a method termed delayed branch has conventionally been used. By way of example, a consideration will be given to the case shown in FIG. 6, wherein instructions are executed by pipeline processing consisting of three stages of instruction fetch (F), instruction decode (D), and instruction execute (E). According to the pipeline processing shown in FIG. 6, the decoding of a branch instruction is followed by the fetching of an instruction at the branch destination. What results is a blank slot (delay slot) for at least one stage. The number of the resulting blank slots is given by the number of stages other than the fetch and execute stages in the pipeline processing. Delayed branch is a method of eliminating a useless blank slot by inserting, in the delay slot, an instruction residing at an address subsequent to a branch instruction. By using the method, improved performance is expected of a processor (see Japanese Laid-Open Patent Publication HEI 4-127237 and Japanese Laid-Open Patent Publication HEI 3-122718). A conditional branch instruction determines whether or not a branch is implemented based on a conditional flag reflecting the result of executing an operate instruction, a transfer instruction, or the like. There has been a conventional method wherein the flexibility with which instruction execution sequence is determined is increased by controlling the rewriting of a conditional flag. In a RISC processor SPARC, e.g., one control bit for determining whether or not a conditional flag is rewritten is provided in the code of an operate instruction. When the value of the control bit is "1", the result of the operation is reflected in the conditional flag. When the value of the control bit is "0", the conditional flag is not rewritten (see "SPARK Architecture Manual," Sun Microsystems Inc., 1991). The method permits the determination of whether or not the conditional flag is rewritten for each instruction, so that the flexibility with which instruction execution sequence is determined in a compiler or the like is increased. However, the conventional method has the following problems. First, in the delayed branch method as mentioned, instruction execution sequence is complicated when a sequence of delayed branch instructions are given consecutively. FIG. 7 shows an example of a program on the assembler level when a sequence of delayed branch instructions are given consecutively. A first delayed branch instruction br200 (specifying a branch to an address 200 when a condition is satisfied) resides at an address 100. A second delayed branch instruction br400 (specifying a branch to an address 400 when a condition is satisfied) resides at an address 101. FIG. 8 shows a relationship between the occurrence or nonoccurrence of a branch specified by each of the delayed branch instructions and instruction execution sequence in executing the program shown in FIG. 7. The operation of the program shown in FIG. 7 is divided into the two cases where a branch is caused or not caused by the first delayed branch instruction br200 and then further divided into the two cases where a branch is caused or not caused by the second delayed branch instruction br400. As a result, the total of four cases should be considered, as shown in FIG. 8. In the case where the condition for each of the first and second delayed branch instructions br200 and br400 is satisfied and branches occur, the instruction at the address 200 is fetched because the branch condition for the first delayed branch instruction br200 is satisfied (the instruction at the address 200 is designated at (200)) and then the instruction at the address 400 is fetched because the branch condition for the second delayed branch instruction br400 is satisfied (the instruction at the address 400 is designated at (400)), as shown in FIG. 9. Consequently, the processor performs a complicated operation of jumping to the destination (address 200) of the branch caused by the first delayed branch instruction br200 to execute only one instruction and then further jumping to the destination (address 400) of the branch caused by the second delayed branch instruction br400 to execute an instruction, as shown in FIG. 8. This seriously impairs the readability of the program on the assembler level, causing a bug in the program. In particular, an unskilled programmer who does not have full understanding of delayed branch would produce a program without expecting such an operation, so that the program inevitably suffers from a bug. In addition, since the bug is recognized only when individual conditions for consecutive delayed branch instructions are satisfied to cause branches, previous debugging is extremely difficult. Oftentimes, the bug cannot be discovered until the apparatus is activated. A skilled programmer who has better knowledge of delayed branch would make a proper modification to the program by inserting a no-operate (NOP) instruction between the consecutive delayed branch instructions with a view to avoiding the problem. However, since the modification is troublesome, it is forgotten many a time. Moreover, if the processor has an increased number of delay slots, it is necessary to insert as many no-operate instructions as the delay slots, so that the program becomes redundant and the memory capacity for storing the program is increased accordingly. The problem may arise even when the delayed branch instructions are not consecutively given but they are relatively close in sequence in the program. Although FIGS. 8 and 9 are based on the assumption that the number of delay slots in the processor is 1, a similar problem occurs when the processor has a larger number of delay slots and the spacing between inconsecutive delayed branch instructions is small relative to the number of delay slots in the processor, which complicates the sequence in which the instructions are executed when individual branch conditions for the instructions are satisfied. There is another type of processor wherein a control bit for determining whether or not a delayed branch is implemented is provided in the code of a delayed branch instruction (see Japanese Laid-Open Patent Publication HEI 4-127237). If the control bit is 0, the processor does not execute an instruction placed in a delay slot when a branch is enabled. FIG. 10 shows a relationship between the occurrence or nonoccurrence of a branch specified by each of delayed branch instructions and instruction execution sequence when the program shown in FIG. 7 is executed by the processor of this type. In FIG. 10, "***" indicates that the instruction placed in the delay slot is not executed. In this case, if the control bit for the first delayed branch instruction br200 is set to 0, the first delayed branch instruction br200 determines whether or not a branch to the address 200 is implemented and the second delayed branch instruction br400 is executed only when no branch occurs, so that the readability of the program on the assembler level is not impaired. However, the provision of the control bit for determining the occurrence or nonoccurrence of a delayed branch in the instruction code increases the bit width of the instruction code by 1 bit, so that the memory capacity for storing the program is increased disadvantageously. In particular, an increased memory capacity forms a fatal drawback to a portable telecommunication device in terms of device size, power consumption, manufacturing cost, and the like. Moreover, the processor requires an additional circuit for controlling delayed branch based on the control bit in the delayed branch instruction code, which is to be provided in the instruction decoder of the processor. Furthermore, in the case where the bit width of the instruction code is preliminarily determined by specifications, the provision of even one control bit adds constraints to device design. If the instruction code is composed of 24 bits, e.g., at most only several bits other than the bits required to represent an instruction type, a specified address, and the like can be used for design. As a result, even one control bit may significantly reduce design flexibility. The present invention has been achieved to implement delayed branch control in a processor employing a delayed branch method, wherein instruction execution sequence is not complicated and the readability of a program on the assembler level is improved without providing a control bit in an instruction code. This is achieved by the features of claim 1 for a method of controlling a branch operation and the features of claim 2 for a delayed branch control circuit Further embodiments are the subject-matter of dependent claims. Specifically, the present invention provides a method of controlling a delayed branch operation in a processor employing a delayed branch method. In executing a delayed branch instruction, when a branch has occurred in a specified one or ones of a continuous sequence of cycles immediately before an execute cycle for the delayed branch instruction which are equal in number to delay slots in the processor, the branch specified by the delayed branch instruction is disabled. In accordance with the method, even if the branch condition for the delayed branch instruction is satisfied, the branch is disabled when a branch has occurred in the specified cycle previous to the execute cycle for the delayed branch instruction, so that the branch is not implemented. Consequently, instruction execution sequence is not complicated even in the case where individual branch conditions are satisfied for delayed branch instructions close to each other relative to the number of delay slots in the processor, which improves the readability of the program on the assembler level. The present invention also provides, as a circuit implementing the delayed branch control method, a delayed branch control circuit provided in a processor employing a delayed branch method to control a branch operation, the circuit comprising: a branch-information storing circuit for storing information indicating whether or not a branch has occurred in a specified one or ones of a continuous sequence of cycles immediately before a current execute cycle which are equal in number to the number of delay slots in the processor; and a branch judging circuit for directing, in executing the delayed branch instruction, the processor to implement a branch only when a branch condition for the delayed branch instruction is satisfied and the branch-information storing circuit stores information indicating that no branch has been implemented in the specified cycle or cycles. Preferably, the branch-information storing circuit comprises a shift register composed of flip-flops connected in series which are equal in number to the delay slots in the processor, the shift register receiving an output signal from the branch judging circuit and shifting a signal held thereby every time an instruction is executed in the processor. With the arrangement, the signal held by each flip-flip of the shift register indicates the occurrence or nonoccurrence of a branch in each of the continuous sequence of cycles immediately before the current execute cycle which are equal in number to the delay slots in the processor. Hence, the branch-information storing circuit can be composed of a simple structure. Preferably, the branch-information storing circuit comprises: a counter for initiating counting of the number of instructions executed by the processor when the branch judging circuit directs the processor to implement a branch and completing the counting when the number of count reaches the number of the delay slots in the processor; and a latch circuit for clearing an output signal therefrom when the branch judging circuit directs the processor to implement a branch and setting the output signal when the counter completes the counting. With the arrangement, the output signal from the latch is cleared during the period after the branch judging circuit directed the processor to implement a branch until the instructions equal in number to the delay slots in the processor are executed. In other words, the output signal from the latch is cleared when a branch has occurred in any one of the continuous sequence of cycles immediately before the current execution cycle which are equal in number to the delay slots in the processor, while it is set when no branch has occurred. Hence, the branch-information storing circuit can be composed of a simple structure. FIG. 1 shows the structure of a delayed branch control circuit according to an embodiment of the present invention; FIG. 2 illustrates the operation of the delayed branch control circuit according to the embodiment of the present invention shown in FIG. 1; FIG. 3 shows another example of the delayed branch control circuit according to the embodiment of the present invention; FIG. 4 is a block diagram showing the structure of a processor employing a pipeline processing method, which comprises the delayed branch control circuit according to the embodiment of the present invention; FIG. 5 is a flow chart illustrating the operation of the delayed branch control circuit according to the embodiment of the present invention in the processor shown in FIG. 4; FIG. 6 shows the flow of pipeline processing during the execution of a branch instruction; FIG. 7 shows an example of a program on the assembler level when delayed branch instructions are consecutively given; FIG. 8 shows a relationship between the occurrence or nonoccurrence of a branch in each of the delayed branch instruction and instruction execution sequence when the program shown in FIG. 7 is executed; FIG. 9 shows the flow of pipeline processing when individual branch conditions are satisfied for the consecutive delayed branch instructions during the execution of the program shown in FIG. 7; and FIG. 10 shows a relationship between the occurrence or nonoccurrence of a branch in each of the delayed branch instructions and instruction execution sequence when the program shown in FIG. 7 is executed by a type of processor executing an instruction code provided with a delayed branch control bit. A description will be given first to the principle of delayed branch control according to the present invention. As described previously in "BACKGROUND OF THE INVENTION," a processor employing a delayed branch method performs such a complicated operation as to seriously impair the readability of a program on the assembler level only when individual branch conditions for consecutive delayed branch instructions are satisfied. If the branch conditions are unsatisfied, the operation caused by the delayed branch instructions is the same as caused by a NOP (Non-Operate) instruction, which is comparable to substantially no execution. When the individual branch conditions for the delayed branch instructions are satisfied, therefore, the operation of seriously impairing the readability of the program on the assembler level can be prevented under such control that the branch condition for the delayed branch instruction placed in a delay slot is constantly unsatisfied. FIG. 1 shows the structure of a delayed branch control circuit according to an embodiment of the present invention, which has been implemented based on the principle. In the drawing are shown: a shift register 11 as a branch-information storing circuit; an AND gate 12 as a branch judging circuit outputting a branch indicate signal SI; an inverter 13 for inverting the branch indicate signal SI outputted from the AND gate 12 and inputting the obtained signal to the shift register 11. The shift register 11 consists of flip-flops equal in number to delay slots in a processor. In every cycle or every time an instruction is executed by the processor, the shift register 11 receives the branch indicate signal SI inverted by the inverter 13, while shifting the signal held thereby. In short, the shift register 11 stores information indicating the occurrence or nonoccurrence of a branch in each of a continuous sequence of cycles immediately before the current execute cycle which are equal in number to the delay slots in the processor and inputs the stored information as a branch-information store signal SR to the AND gate 12. The AND gate 12 outputs the branch indicate signal SI only when it receives a branch execute signal SEa because of the satisfied branch condition for the branch instruction and the branch-information store signal SR received from the shift register 11 indicates that no branch has occurred previously. Thus, the delayed branch control circuit shown in FIG. 1 stores, in the shift register 11, information indicating a previous branch caused or not caused by a branch instruction in each of the continuous sequence of cycles immediately before the current execute cycle which are equal in number to the delay slots in the processor and unconditionally disables a branch as long as the shift register 11 stores the occurrence of a previous branch. FIG. 2 illustrates the operation of the delayed branch control circuit shown in FIG. 1 when the program shown in FIG. 7 is executed by pipeline processing. In FIG. 2, the number of delay slots in the processor is assumed to be 1. As shown in FIG. 2, when a branch condition is satisfied with the delay branch control circuit shown in FIG. 1 in operation, a branch is enabled if branch information stored in the shift register 11 is false, while a branch is disabled if branch information stored in the shift register 11 is true. Accordingly, when the delayed branch instructions are given consecutively and a branch has been caused by the first conditional branch instruction br200, the second conditional branch instruction br400 is constantly unsatisfied. Although the shift register 11 consisting of flip-flops equal in number to delay slots is provided in the delayed branch control circuit shown in FIG. 1 to store branch information for the number of delay slots, a counter 16 and an RS latch 17 may be used instead of the shift register 11, as shown in FIG. 3. In the delayed branch control circuit shown in FIG. 3, the branch-information storing circuit is composed of the counter 16 and the RS latch 17. The branch indicate signal SI clears the RS latch 17, while starting the counting operation of the counter 16. The counter 16 performs a counting operation every time the processor executes an instruction and sets the RS latch 17 when the number of count reaches the number of delay slots. As a result, an output signal from the RS latch 17 is set only when no branch has occurred in the cycles immediately before the current execute cycle which are equal in number to the delay slots. The output signal from the RS latch 17 is inputted as the branch-information store signal SR to the AND gate 18, which controls the outputting of the branch indicate signal SI based on the branch-information store signal SR. In this manner, a branch is disabled during the period after a branch is executed until instruction equal in number to delay slots are executed. Even when the branch instruction is an unconditional branch instruction in the delayed branch control circuit shown in FIGS. 1 and 3, it is not necessary to take any particular measures if the unconditional branch instruction is considered to be a conditional branch instruction which constantly enables a branch. It is not necessarily required to store the occurrence or nonoccurrence of a branch in each of the continuous sequence of cycles immediately before the current execute cycle which are equal in number to the delay slots. It is also possible to use the occurrence or nonoccurrence of a branch in part of the continuous sequence of cycles immediately before the current execute cycle which are equal in number to the delay slots as a condition for enabling or disabling a branch. In the delayed branch control circuit shown in FIG. 1, e.g., only output signals from the flip-flops composing the shift register 11 and corresponding to the cycles wherein the occurrence or nonoccurrence of a branch is used as the condition are properly inputted as the branch-information store signal SR to the AND gate 12. FIG. 4 is a block diagram showing the structure of a processor performing pipeline processing, which comprises the delayed branch control circuit according to the embodiment of the present invention. In FIG. 4, the portions other than those associated with delayed branch control and conditional-flag rewriting control shown in FIG. 4 are shown in a simplified manner. In FIG. 4, an instruction memory 101 is a memory for storing an instruction code, which outputs an instruction addressed by a program counter (PC) 102 to an instruction decoder 110. The instruction decoder 110 comprises: a branch instruction decoder 111, a compare instruction decoder 112; and an operate instruction decoder 113. The branch instruction decoder 111 selectively decodes a branch instruction among instructions inputted from the instruction memory 101 and outputs the branch signal SDb, while judging whether or not a branch is enabled by referencing the conditional flag register 103 and outputting a branch enable signal SDa when a branch is enabled. The compare instruction decoder 112 selectively decodes a compare instruction among instructions inputted from the instruction memory 101 and outputs the compare signal SDc. The operate instruction decoder 113 decodes an operate instruction among instructions inputted from the instruction memory 101 and outputs the operate signal SDd. The branch enable signal SDa, branch signal SDb, compare signal SDc, and operate signal SDd are timed by a delay circuit (FF) 104 with the branch enable execute signal SEa, branch execute signal SEd, compare execute signal SEc, and operate execute signal SDd, respectively, which are timing signals in the execute stage. A delayed branch control circuit 120 has the same structure as the delayed branch control circuit shown in FIG. 1. The delayed branch control circuit 120 consists of a branch-information storing circuit 121 composed of a single flip-flop, an inverting gate 122, and an AND gate 123 and outputs as a branch enable execute signal SEa and an AND signal between the branch enable execute signal SEa and an output signal from the branch-information storing circuit 121. A selector 105 selects an input signal to a PC102 responsive to the branch indicate signal SI. The branch indicate signal SI is held by the branch-information storing circuit 121. The operation of delayed branch control according to the embodiment of the present invention in the processor shown in FIG. 4 will be described with reference to the flow chart shown in FIG. 5. First, in Step ST11, it is judged whether or not the instruction to be executed is a delayed branch instruction. If the instruction to be executed is not a delayed branch instruction, the whole process properly proceeds to Step ST15 with no branch. If the instruction to be executed is a delayed branch instruction, the whole process proceeds to Step ST12 where it is judged whether or not the instruction executed in the immediately preceding cycle is a delayed branch instruction and a branch has occurred. If the instruction executed in the immediately preceding cycle is a delayed branch instruction and a branch has occurred, the whole step proceeds to Step ST15 with no branch. If the instruction executed in the immediately preceding cycle is not a delayed branch instruction, the whole process proceeds to Step ST13 where it is judged whether or not the branch condition is satisfied. If the branch condition is satisfied, the whole process proceeds to Step ST14 with a branch. If the branch condition is not satisfied, the whole process proceeds to Step ST15 with no branch. In a specific operation of the processor shown in FIG. 4, when delayed branch instructions are given consecutively and the first delayed branch instruction has not caused a branch, the branch indicate signal SI becomes false. Accordingly, an output signal from the inverting circuit 122 connected to the branch-information storing circuit 121 in the delayed branch control circuit 120 becomes true responsive to the subsequent delayed branch instruction, so that the occurrence or nonoccurrence of a branch is determined by the branch enable execute signal SEa. If the first delayed branch instruction has caused a branch, on the other hand, the branch indicate signal SI becomes true so that an output signal from the inverting circuit 122 connected to the branch-information storing circuit 121 in the delayed branch control circuit 120 becomes false responsive to the subsequent delayed branch instruction. Hence, the branch indicate signal SI becomes false irrespective of the branch enable execute signal SEa, which constantly disables a branch. Thus, when the delayed branch instructions are given consecutively, the first delayed branch instruction apparently causes the same operation as caused by an undelayed branch instruction. Although it is assumed that the number of delay slots in the processor shown in FIG. 4 is 1 and the branch-information storing circuit 121 is composed of a single flip-flop, it will be appreciated that, when the number of delay slots is 2 or more, the shift register 11 consisting of flip-flops which are equal in number to delay slots may be provided instead to use the AND signal between individual output signals from the flop-flops as a disable signal for the branch enable execute signal SEa as shown in FIG. 1 or the counter 16 and RS latch 17 may be provided instead to implement the delayed branch control circuit 120, as shown in FIG. 3. As described above, under delayed branch control according to the present embodiment, the program on the assembler level can retain its readability even when delayed branch instructions are given consecutively. Moreover, a circuit implementing the delayed branch control method according to the present embodiment can be implemented by a simple structure. For example, when the number of delay slots in the processor is 1, the circuit can be implemented by a small-scale circuit composed of a single flip-flop with an inverting output and a single AND gate.

Claims: A method of controlling a branch operation in a pipelined processor employing a delayed branch method, said method comprising the steps of: in executing a branch instruction, judging whether or not a branch has occurred in a specified cycle or cycles immediately before an execute cycle for the branch instruction, said specified cycles being continuous cycles equal in number to delay slots in the processor; and disabling a branch specified by said branch instruction when a branch has occurred in said specified cycle or cycles. A delayed branch control circuit provided in a pipelined processor employing a delayed branch method to control a branch operation, said circuit comprising: a branch-information storing circuit (11; 16, 17) for storing information indicating whether or not a branch has occurred in a specified cycle or cycles immediately before a current execute cycle, said specified cycles being continuous cycles equal in number to delay slots in the processor; and a branch judging circuit (12; 18) for directing, in executing a branch instruction, the processor to implement a branch only when a branch condition for the branch instruction is satisfied and said branch-information storing circuit stores information indicating that no branch has occurred in said specified cycle or cycles. A delayed branch control circuit according to claim 2, wherein said branch-information storing circuit comprises a shift regtster (11) composed of flip-flops connected in series which are equal in number to said delay slots in the processor, said shift register receiving an output signal from said branch judging circuit and shifting a signal held thereby every time an instruction is executed in said processor. A delayed branch control circuit according to claim 2, wherein said branch-information storing circuit comprises: a counter (16) for initiating counting of the number of instructions executed by said processor when said branch judging circuit directs the processor to implement a branch and completing the counting when the number of count reaches the number of said delay slots in the processor; and a latch circuit (17) for clearing an output signal therefrom when said branch judging circuit directs the processor to implement a branch and setting the output signal when said counter completes the counting.

Inventors: KABUO HIDEYUKI, YASOSHIMA HIROYUKI, KABUO, HIDEYUKI, YASOSHIMA, HIROYUKI
Applicants: MATSUSHITA ELECTRIC IND CO LTD, MATSUSHITA ELECTRIC INDUSTRIAL CO., LTD.
Application year: 1997
Main classification: G06F9
IPC codes: G06F9