Method and system for predicting branches
By configuring a branch predictor in the processor, using historical PC values to judge the PC value of the first instruction to obtain the branch target address in advance, the problem of clock cycle waste in the prior art is solved and the branch prediction efficiency is improved.
Patent Information
- Application Number
- CN202310649210.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-02
- Publication Date
- 2024-12-03
AI Technical Summary
After branch instructions, existing processors need to clear the next instruction that has entered the instruction acquisition stage, resulting in wasted clock cycles and it is difficult to obtain the branch target address in advance.
By configuring the branch predictor to read multiple reference PC values and corresponding predicted target addresses, we judge whether the PC value of the first instruction meets the PC value of the historical instruction, so as to make branch prediction in the second instruction acquisition stage, and obtain the branch target address in advance .
Avoid clock cycle waste, obtain branch target address in advance, and improve the processor's branch prediction efficiency.
Smart Images

Figure CN119065719A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to branch prediction, and more particularly to a method and system for predicting branches, which can obtain in advance the branch target address of a second instruction executed after the first instruction by checking whether the program counter (PC) value of the first instruction entering the instruction fetch stage matches the historical PC value of the historical instruction executed before the branch instruction. Background Art
[0002] Generally speaking, a processor executes a series of multiple instructions using a pipelined architecture. The pipelined architecture may include multiple stages such as instruction fetch, instruction decode, and execute. In order to improve the efficiency of instruction execution, multiple instructions can be processed simultaneously at different stages of the pipelined architecture. That is, when the first instruction enters the second stage of the pipelined architecture, the second instruction can enter the first stage of the pipelined architecture at the same time.
[0003] Specifically, the instruction acquisition stage of the pipeline architecture can be divided into a first instruction acquisition stage and a second instruction acquisition stage in sequence. In addition, the instruction may include a branch instruction, which is used to change the order of instruction execution. However, once the branch instruction is determined to be taken in the second instruction acquisition stage, the processor must flush the next instruction that has entered the first instruction acquisition stage, that is, flush the instruction address of the next instruction, thereby wasting a clock cycle. Summary of the invention
[0004] The technical problem to be solved by the present invention is to provide a method and system for predicting branches in view of the shortcomings of the prior art. The method and system can detect whether the PC value of the first instruction entering the instruction acquisition stage is consistent with the historical PC value of the historical instruction executed before the branch instruction, so as to obtain in advance the branch target address of the second instruction executed after the first instruction.
[0005] In order to solve the above technical problems, one of the technical solutions adopted by the present invention is to provide a method for predicting branches. The method is applicable to a processor that executes a series of multiple instructions with a pipeline architecture, wherein the instructions respectively have multiple PC values, and the pipeline architecture includes a first instruction acquisition stage and a second instruction acquisition stage, and the method includes: when the first instruction of the instructions enters the first instruction acquisition stage, a branch predictor is configured to read a branch target buffer (Branch Target Buffer, BTB) storing multiple reference PC values and multiple predicted target addresses corresponding to the reference PC values, and determine whether the PC value of the first instruction meets one of the multiple reference PC values, wherein the reference PC values stored in the branch target buffer include multiple historical PC values of multiple historical instructions executed before the multiple branch instructions, and the predicted target addresses corresponding to the historical PC values are multiple target addresses of the branch instructions; and when the first instruction enters the second instruction acquisition stage, in response to determining that the PC value of the first instruction meets one of the multiple reference PC values, branch prediction is performed according to a prediction counter value, and in response to the result of the branch prediction being branch adoption, a branch target address is selected according to the predicted target addresses stored in the branch target buffer.
[0006] In order to solve the above technical problems, another technical solution adopted by the present invention is to provide a system for predicting branches. The system is applicable to a processor that executes a series of multiple instructions with a pipeline architecture, each of which has multiple PC values. The pipeline architecture includes a first instruction acquisition stage and a second instruction acquisition stage, and the system includes a branch predictor. The branch predictor includes a branch target buffer, which stores multiple reference PC values and multiple predicted target addresses corresponding to these reference PC values, and the branch predictor is configured to perform the following steps: when the first instruction of these instructions enters the first instruction acquisition stage, read the branch target buffer and determine whether the PC value of the first instruction meets one of the multiple reference PC values, wherein the reference PC values stored in the branch target buffer include multiple historical PC values of multiple historical instructions executed before the multiple branch instructions, and the predicted target addresses corresponding to these historical PC values are respectively the multiple target addresses of these branch instructions; and when the first instruction enters the second instruction acquisition stage, in response to determining that the PC value of the first instruction meets one of the multiple reference PC values, perform branch prediction according to a prediction counter value, and in response to the result of the branch prediction being branch adoption, select a branch target address according to the predicted target addresses stored in the branch target buffer.
[0007] To further understand the features and technical contents of the present invention, please refer to the following detailed description and drawings of the present invention. However, the drawings provided are only for reference and description and are not intended to limit the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0008] Figure 1 is a flowchart of the steps of a method for predicting branches according to an embodiment of the present invention;
[0009] Figure 2 is a functional module diagram of a system for predicting branches according to a first embodiment of the present invention;
[0010] Figure 3 is a functional module diagram of a system for predicting branches according to a second embodiment of the present invention;
[0011] Figure 4 It is a functional module diagram of a predictive branching system according to a third embodiment of the present invention.
[0012] Description of reference numerals:
[0013] S110, S120: Process steps IF1: First instruction acquisition stage IF2: Second instruction acquisition stage
[0014] ID: Instruction decoding stage EX: Execution stage 1: System
[0015] 10: Branch predictor 100: Branch target buffer btb_flag, btb_flag[k]: Flag value
[0016] 104: prediction circuit 106: selection circuit btb_pc, btb_pc[k]: reference PC value
[0017] 102: Comparator circuit 40: Branch evaluation circuit 30, 402: Multitasking processing circuit
[0018] 20: buffer 400: logic circuit btb_address: predicted target address seq: continuity state value PC[x], PC[y]: PC value btb_hit: hit signal
[0019] pre_cont: predicted counter value tak: adopted signal tar_address: branch target address
[0020] pre_inf: branch prediction information is_br: branch instruction status value DETAILED DESCRIPTION
[0021] The following is an explanation of the embodiments of the present invention through specific embodiments. Those skilled in the art can understand the advantages and effects of the present invention from the contents provided in this specification. The present invention can be implemented or applied through other different specific embodiments, and the details in this specification can also be modified and changed in various ways based on different viewpoints and applications without departing from the concept of the present invention. In addition, the drawings of the present invention are only for simple schematic illustration and are not shown according to actual size. It is stated in advance. The following embodiments will further explain the relevant technical contents of the present invention in detail, but the contents provided are not intended to limit the scope of protection of the present invention.
[0022] See also Figure 1 , Figure 1 1 is a flowchart of the method for predicting branches according to an embodiment of the present invention. The method of this embodiment is applicable to a processor that executes a series of multiple instructions with a pipeline architecture, each of which has multiple PC values. The pipeline architecture includes a first instruction acquisition stage and a second instruction acquisition stage, and the method includes the following steps.
[0023] Step S110: When the first instruction of these instructions enters the first instruction acquisition stage, the branch predictor is configured to read the branch target buffer storing multiple reference PC values and multiple predicted target addresses corresponding to these reference PC values, and determine whether the PC value of the first instruction meets one of the multiple reference PC values, wherein the reference PC values stored in the branch target buffer include multiple historical PC values of multiple historical instructions respectively executed before the multiple branch instructions, and the predicted target addresses corresponding to these historical PC values are respectively the multiple target addresses of these branch instructions.
[0024] Step S120: When the first instruction enters the second instruction fetch stage, in response to determining that the PC value of the first instruction matches one of these reference PC values, a branch prediction is performed based on a prediction counter value, and in response to the result of the branch prediction being a branch taken, a branch target address is selected based on these predicted target addresses stored in the branch target buffer.
[0025] Specifically, the PC value is the instruction address, and the method of this embodiment can use multiple historical instruction addresses of multiple historical instructions executed before multiple branch instructions as indexes to determine whether there is an instruction address of the first instruction among these historical instruction addresses. Therefore, the method of this embodiment can determine that the second instruction executed after the first instruction is a branch instruction by finding out whether the PC value of the first instruction entering the instruction acquisition stage meets the historical PC value of the historical instruction executed before the branch instruction, and obtain the branch target address of the second instruction in advance. In other words, the method of this embodiment can provide a look-ahead mechanism.
[0026] However, in order to retain the existing branch prediction method, the reference PC values stored in the branch target buffer may also include multiple branch PC values of multiple branch instructions, and the predicted target addresses corresponding to these branch PC values are respectively the target addresses of these branch instructions. In addition, the branch target buffer of this embodiment may also store multiple flag values corresponding to these reference PC values, and each flag value indicates whether the corresponding reference PC value is one of these historical PC values.
[0027] In other words, each flag value may also indicate whether the corresponding reference PC value is used to help the branch predictor perform branch prediction on the second instruction executed after the first instruction. For example, a flag value of 1 may indicate that the corresponding reference PC value is one of the historical PC values and is used to help the branch predictor perform branch prediction on the second instruction executed after the first instruction. In contrast, a flag value of 0 may indicate that the corresponding reference PC value is actually a branch PC value of a branch instruction, so the reference PC value is not used to help the branch predictor perform branch prediction on the second instruction executed after the first instruction, but is used to help the branch predictor perform branch prediction on the first instruction, thereby retaining the existing branch prediction approach.
[0028] Please also read Figure 2 , which shows a functional module diagram of the prediction branch system of the first embodiment of the present invention. Figure 2 As shown, the system 1 may include a branch predictor 10, which may include a branch target buffer 100, a comparator circuit 102, a prediction circuit 104, and a selection circuit 106. The branch target buffer 100 stores a plurality of reference PC values btb_pc and a plurality of predicted target addresses btb_address corresponding to the reference PC values btb_pc.
[0029] As described above, the reference PC values btb_pc stored in the branch target buffer 100 may include multiple historical PC values of multiple historical instructions executed before multiple branch instructions, and the branch target buffer 100 may also store multiple flag values btb_flag corresponding to the reference PC values btb_pc. Each flag value indicates whether the corresponding reference PC value is one of the historical PC values. In addition, the branch predictor 10 is configured to perform the aforementioned steps S110 and S120.
[0030] Specifically, when the first instruction enters the first instruction fetch stage IF1, the branch predictor 10 reads the flag values btb_flag, the reference PC values btb_pc and the predicted target addresses btb_address stored in the branch target buffer 100, and determines through the comparator circuit 102 whether the PC value PC[x] of the first instruction matches one of the reference PC values stored in the branch target buffer 100.
[0031] For the convenience of the following description, PC[x] is used in this embodiment to represent the PC value of the first instruction. x may be the sequence number of the first instruction among the instructions executed by the processor. That is, although the terms "first" and "second" are used herein to describe two instructions, the two instructions should not be limited by these terms, which are mainly used to distinguish one of the instructions from the next instruction executed thereafter.
[0032] In addition, before determining whether the PC value PC[x] of the first instruction meets one of these reference PC values, the branch predictor 10 may also obtain the continuity status value seq of the first instruction. The continuity status value seq of the first instruction is used to indicate whether the first instruction has a jump instruction before entering the first instruction acquisition stage IF1. For example, commonly used instructions such as jumps, branches, interrupts, exceptions, etc. will cause discontinuous instruction sorting. When this happens, the continuity of the PC value of the second instruction cannot be predicted, which also means that in this case, the second instruction cannot be branch predicted. In addition, the continuity status value seq of the first instruction is 1, which means that the PC value PC[x] of the first instruction is continuous, and the continuity status value seq of the first instruction is 0, which means that the PC value PC[x] of the first instruction is not continuous, but the present invention is not limited to this.
[0033] It should be noted that even if the PC value PC[x] of the first instruction meets one of the reference PC values, if the PC value PC[x] of the first instruction is not continuous, the branch predictor 10 cannot use the foresight mechanism, but must use the existing branch prediction method. Therefore, the branch predictor 10 can input the flag value btb_flag, the reference PC value btb_pc, the continuity status value seq of the first instruction, and the PC value PC[x] into the comparator circuit 102 to determine whether the PC value PC[x] of the first instruction meets one of the reference PC values and whether the PC value PC[x] of the first instruction is continuous.
[0034] In contrast, if the PC value PC[x] of the first instruction meets one of the reference PC values and the PC value PC[x] of the first instruction is continuous, the branch predictor 10 can use the foresight mechanism. Therefore, in response to determining that the PC value PC[x] of the first instruction meets one of the reference PC values and the PC value PC[x] of the first instruction is not continuous, the comparator circuit 102 can generate a first type of hit signal btb_hit, and in response to determining that the PC value PC[x] of the first instruction meets one of the reference PC values and the PC value PC[x] of the first instruction is continuous, the comparator circuit 102 can generate a second type of hit signal btb_hit.
[0035] In other words, according to the type of the hit signal btb_hit, the branch predictor 10 can also determine whether to perform branch prediction on the first instruction or on the second instruction executed after the first instruction, that is, to use the existing branch prediction method or foreseen mechanism. Figure 2 As shown, when the first instruction enters the second instruction fetch stage IF2, the branch predictor 10 can perform branch prediction according to the prediction counter value pre_cont through the prediction circuit 104, and select the branch target address tar_address according to the predicted target addresses btb_address stored in the branch target buffer 100 through the selection circuit 106.
[0036] Specifically, when the prediction circuit 104 receives the first type of hit signal btb_hit generated by the comparator circuit 102, that is, in response to determining that the PC value PC[x] of the first instruction meets one of the reference PC values and the PC value PC[x] of the first instruction is not continuous, the prediction circuit 104 will perform branch prediction on the first instruction according to the prediction counter value pre_cont. The prediction counter value pre_cont can be the guess and confidence of the first instruction for the branch, and the result of the branch prediction is a branch taken or a branch not taken (Not Taken). If the result of the branch prediction is a branch taken, the prediction circuit 104 generates an adopted signal tak. Then, when the selection circuit 106 receives the adopted signal tak generated by the prediction circuit 104, that is, in response to the result of the branch prediction being a branch taken, the selection circuit 106 will select the branch target address tar_address of the first instruction according to the predicted target addresses btb_address stored in the branch target buffer 100, and output the branch target address tar_address. Since the first instruction itself is a branch instruction at this time, and the prediction circuit 104 immediately performs branch prediction on the first instruction entering the second instruction acquisition stage IF2, the processor at this time still needs to waste a clock cycle to clear the second instruction that has entered the first instruction acquisition stage IF1, so that the branch target address tar_address of the first instruction output by the selection circuit 106 can be sorted and executed after the first instruction.
[0037] On the other hand, when the prediction circuit 104 receives the second type of hit signal btb_hit generated by the comparator circuit 102, that is, in response to determining that the PC value PC[x] of the first instruction meets one of the reference PC values and the PC value PC[x] of the first instruction is continuous, the prediction circuit 104 will perform branch prediction for the second instruction executed after the first instruction according to the prediction counter value pre_cont. Then, when the selection circuit 106 receives the adoption signal tak generated by the prediction circuit 104, that is, in response to the result of the branch prediction being a branch adoption, the selection circuit 106 will select the branch target address tar_address of the second instruction according to the prediction target addresses btb_address stored in the branch target buffer 100, and output the branch target address tar_address. Since the second instruction is a branch instruction at this time, and the prediction circuit 104 performs branch prediction in advance for the second instruction that has not yet entered the second instruction acquisition stage IF2, the branch predictor 10 can obtain the branch target address tar_address of the second instruction in advance, and output the branch target address tar_address of the second instruction to enter the first instruction acquisition stage IF1 after the second instruction, so that there is no need to waste a clock cycle to clear the next instruction that was originally sequenced to enter the first instruction acquisition stage IF1 after the second instruction.
[0038] Furthermore, if the PC value PC[x] of the first instruction meets the reference PC value btb_pc[k], the selection circuit 106 can select the predicted target address btb_address[k] corresponding to the reference PC value btb_pc[k] as the branch target address tar_address. In addition, in response to the result of the branch prediction being a branch adoption, the selection circuit 106 can also output branch prediction information pre_inf including an adoption signal tak and a hit signal btb_hit, so as to facilitate use in branch evaluation (Evaluation) in the execution phase. Please refer to Figure 3 , Figure 3 1 is a functional module diagram of a system for predicting branches according to a second embodiment of the present invention. Figure 3 As shown, the pipeline architecture of this embodiment may further include an instruction decoding stage ID and an execution stage EX, and the system 1 may further include a branch evaluation circuit 40 .
[0039] When the branch instruction enters the execution stage EX, the branch evaluation circuit 40 can perform branch evaluation on the branch instruction according to the branch prediction information pre_inf. As mentioned above, through the historical PC value stored in the branch target buffer 100, the branch predictor 10 can make branch predictions in advance for the branch instruction that has not yet entered the second instruction acquisition stage IF2, and obtain the branch target address tar_address and branch prediction information pre_inf of the branch instruction in advance, but the branch evaluation circuit 40 in the execution stage EX cannot perform branch evaluation in advance for the branch instruction that has not yet entered the execution stage EX. That is to say, in the case where the second instruction is the branch instruction, the branch prediction information pre_inf output by the selection circuit 106 must wait until the second instruction acquisition stage IF2 processes the second instruction, and then enter the instruction decoding stage ID with the second instruction, but cannot enter the instruction decoding stage ID with the first instruction.
[0040] In order to solve the above technical problems, the system 1 may further include a buffer 20 and a multi-task processing circuit 30. Figure 3 As shown, the buffer 20 can be configured in the buffer space between the second instruction acquisition stage IF2 and the instruction decoding stage ID to store the branch prediction information pre_inf output by the selection circuit 106, so that the branch prediction information pre_inf can be output after a delay of one clock cycle. In addition, the multi-task processing circuit 30 is configured to select the branch prediction information pre_inf output by the selection circuit 106 or the branch prediction information pre_inf stored in the buffer 20 when the first instruction is about to leave the second instruction acquisition stage IF2 according to the continuity state value seq of the first instruction.
[0041] In other words, the method of this embodiment may also include: configuring the buffer 20 to store the branch prediction information pre_inf output by the selection circuit 106 in the buffer space between the second instruction acquisition stage IF2 and the instruction decoding stage ID; and when the first instruction is about to leave the second instruction acquisition stage IF2, according to the continuity state value seq of the first instruction, selecting the branch prediction information pre_inf output by the output selection circuit 106 or the branch prediction information pre_inf stored in the buffer 20 through the multi-tasking processing circuit 30.
[0042] In practice, the present embodiment may configure the first input terminal of the multi-task processing circuit 30 to receive the branch prediction information pre_inf output by the selection circuit 106, and the second input terminal of the multi-task processing circuit 30 is coupled to the buffer 20. In addition, the control terminal of the multi-task processing circuit 30 may receive the continuity status value seq of the first instruction. Therefore, when the continuity status value seq of the first instruction is 0, the multi-task processing circuit 30 may select to output the branch prediction information pre_inf output by the selection circuit 106, and when the continuity status value seq of the first instruction is 1, the multi-task processing circuit 30 may select to output the branch prediction information pre_inf stored in the buffer 20.
[0043] In addition, the method of this embodiment can also update the reference PC value stored in the branch target buffer 100 through the branch evaluation circuit 40 in the execution stage EX. Figure 4 , Figure 4 FIG. 4 is a functional module diagram of a system for predicting branches according to a third embodiment of the present invention. Figure 4 As shown, branch evaluation circuit 40 may include logic circuit 400 and multitasking circuit 402 .
[0044] Specifically, the logic circuit 400 may be an AND gate, and before updating the reference PC value stored in the branch target buffer, the branch evaluation circuit 40 may also obtain a branch instruction status value is_br used to indicate whether the second instruction is a branch instruction. For example, the branch instruction status value is_br of the second instruction is 1, which may indicate that the second instruction is a branch instruction, and the branch instruction status value is_br of the second instruction is 0, which may indicate that the second instruction is not a branch instruction. Therefore, in this embodiment, the two input terminals of the logic circuit 400 may be configured to receive the branch instruction status value is_br of the second instruction and the continuity status value seq of the second instruction respectively, and the control terminal of the multi-task processing circuit 402 may be configured to receive the output of the logic circuit 400.
[0045] In addition, the first input terminal of the multi-task processing circuit 402 receives the PC value PC[y] of the second instruction, and the second input terminal of the multi-task processing circuit 402 receives the PC value PC[x] of the first instruction executed before the second instruction. Therefore, when the branch instruction status value is_br and the continuity status value seq of the second instruction are both 1, the multi-task processing circuit 402 can choose to write the PC value PC[x] of the first instruction executed before the second instruction into the branch target buffer 100 as one of the reference PC values, such as the reference PC value btb_pc[k], and the logic circuit 400 outputs a flag value btb_flag[k] of 1. Conversely, when the branch instruction status value is_br of the second instruction is 1, but the continuity status value seq of the second instruction is 0, the multi-task processing circuit 402 can choose to write the PC value PC[y] of the second instruction into the branch target buffer 100 as one of the reference PC values, such as the reference PC value btb_pc[k], and the logic circuit 400 outputs a flag value btb_flag[k] of 0.
[0046] In other words, the method of this embodiment may also include: when the second instruction enters the execution stage EX, in response to determining that the second instruction is a branch instruction, based on the continuity status value seq of the second instruction, determining whether to write the PC value PC[y] of the second instruction or the PC value PC[x] of the first instruction executed before the second instruction as one of the reference PC values stored in the branch target buffer 100, such as the reference PC value btb_pc[k], and determining the flag value btb_flag[k] corresponding to the reference PC value btb_pc[k]; and writing the branch target address of the second instruction as the predicted target address btb_address[k] stored in the branch target buffer 100 corresponding to the reference PC value btb_pc[k].
[0047] To sum up, one beneficial effect of the present invention is that the method and system for predicting branches provided by the present invention can obtain in advance the branch target address of the second instruction executed after the first instruction by finding out whether the PC value of the first instruction entering the instruction acquisition stage is consistent with the historical PC value of the historical instruction executed before the branch instruction, so that there is no need to waste a clock cycle to clear the next instruction originally sequenced after the second instruction to enter the first instruction acquisition stage.
[0048] The contents provided above are only preferred embodiments of the present invention, and are not intended to limit the patent protection scope of the present invention. Therefore, all equivalent technical changes made using the contents of the present invention's specification and drawings are included in the patent protection scope of the present invention.
Claims
1. A method for predicting branches, applicable to a processor, wherein the processor executes a series of multiple instructions with a pipeline architecture, wherein the multiple instructions respectively have multiple program counter values, the pipeline architecture includes a first instruction fetch stage and a second instruction fetch stage, and the method comprises: When a first instruction of the plurality of instructions enters the first instruction fetch stage, a branch predictor is configured to read a branch target buffer storing a plurality of reference PC values and a plurality of predicted target addresses respectively corresponding to the plurality of reference PC values, and determine whether the PC value of the first instruction matches one of the plurality of reference PC values, wherein the plurality of reference PC values stored in the branch target buffer include a plurality of historical PC values of a plurality of historical instructions respectively executed before a plurality of branch instructions, and the plurality of predicted target addresses corresponding to the plurality of historical PC values are respectively a plurality of target addresses of the plurality of branch instructions; as well as When the first instruction enters the second instruction fetch stage, in response to determining that the PC value of the first instruction matches one of the multiple reference PC values, branch prediction is performed based on a prediction counter value, and in response to the result of the branch prediction being a branch adoption, a branch target address is selected based on the multiple predicted target addresses stored in the branch target buffer.
2. The method according to claim 1, characterized in that The branch target buffer further stores a plurality of flag values respectively corresponding to the plurality of reference PC values, and each of the plurality of flag values indicates that the corresponding reference PC value is one of the plurality of historical PC values.
3. The method according to claim 2, characterized in that The step of determining whether the PC value of the first instruction conforms to one of the plurality of reference PC values comprises: The multiple flag values, the multiple reference PC values, a continuity status value of the first instruction and the PC value are input into a comparator circuit to determine whether the PC value of the first instruction conforms to one of the multiple reference PC values and whether the PC value of the first instruction is continuous, wherein the continuity status value of the first instruction is used to indicate whether the first instruction has a jump instruction before entering the first instruction fetch stage.
4. The method according to claim 3, characterized in that The step of performing branch prediction according to the prediction counter value and selecting the branch target address according to the plurality of predicted target addresses stored in the branch target buffer further includes: In response to determining that the PC value of the first instruction is continuous, performing the branch prediction for the second instruction executed after the first instruction according to the prediction counter value through a prediction circuit, and when the branch is taken, selecting the branch target address of the second instruction according to the plurality of predicted target addresses stored in the branch target buffer through a selection circuit; and In response to determining that the PC value of the first instruction is not continuous, the prediction circuit performs the branch prediction on the first instruction according to the prediction counter value, and when the branch is taken, the selection circuit selects the branch target address of the first instruction according to the multiple predicted target addresses stored in the branch target buffer.
5. The method according to claim 4, characterized in that In response to determining that the PC value of the first instruction conforms to one of the multiple reference PC values and that the PC value of the first instruction is not continuous, the comparator circuit generates a hit signal of a first type, and in response to determining that the PC value of the first instruction conforms to one of the multiple reference PC values and that the PC value of the first instruction is continuous, the comparator circuit generates a hit signal of a second type.
6. The method according to claim 5, characterized in that In response to the result of the branch prediction being that the branch is taken, the prediction circuit generates an adopted signal, and the selection circuit further outputs branch prediction information including the adopted signal and the hit signal.
7. The method according to claim 6, characterized in that The pipeline architecture further includes an instruction decoding stage and an execution stage, and the method further includes: configuring a buffer in a buffer space between the second instruction fetch stage and the instruction decode stage to store the branch prediction information output by the selection circuit; and When the first instruction is about to leave the second instruction fetch stage, the branch prediction information output by the selection circuit or the branch prediction information stored in the buffer is selected and outputted according to the continuity state value of the first instruction.
8. The method according to claim 7, characterized in that Also includes: When the second instruction enters the execution stage, in response to determining that the second instruction is the branch instruction, determining, according to the continuity status value of the second instruction, the PC value written into the second instruction or the PC value of the first instruction executed before the second instruction as one of the plurality of reference PC values stored in the branch target buffer, and determining the flag value corresponding to the reference PC value; as well as The branch target address written into the second instruction is used as the branch target buffer to store the predicted target address corresponding to the reference PC value.
9. A system for predicting branches, applicable to a processor, wherein the processor executes a series of multiple instructions with a pipeline architecture, wherein the multiple instructions respectively have multiple PC values, the pipeline architecture includes a first instruction fetch stage and a second instruction fetch stage, and the system comprises: A branch predictor includes a branch target buffer, wherein the branch target buffer stores a plurality of reference PC values and a plurality of predicted target addresses corresponding to the plurality of reference PC values, and the branch predictor is configured to perform the following steps: When a first instruction of the plurality of instructions enters the first instruction fetch stage, the branch target buffer is read, and it is determined whether the PC value of the first instruction matches one of the plurality of reference PC values, wherein the plurality of reference PC values stored in the branch target buffer include a plurality of historical PC values of a plurality of historical instructions respectively executed before the plurality of branch instructions, and the plurality of predicted target addresses corresponding to the plurality of historical PC values are respectively a plurality of target addresses of the plurality of branch instructions; as well as When the first instruction enters the second instruction fetch stage, in response to determining that the PC value of the first instruction matches one of the multiple reference PC values, branch prediction is performed based on a prediction counter value, and in response to the result of the branch prediction being a branch adoption, a branch target address is selected based on the multiple predicted target addresses stored in the branch target buffer.
10. The system according to claim 9, characterized in that The branch target buffer further stores a plurality of flag values respectively corresponding to the plurality of reference PC values, and each of the plurality of flag values indicates whether the corresponding reference PC value is one of the plurality of historical PC values.
