Better Predictors: Global History, Gshare and Local History

A 2-bit counter knows one thing about a branch: which way it mostly goes. A branch that alternates has no "mostly". This page gives the predictor a memory of the last outcomes.

You Will Be Able To

  • Explain what a history register holds, and how it changes after each branch.
  • Work out which counter gshare uses, from an address and a history.
  • Say when global history, local history, or no history is the best choice.
Lesson
S2.12
Time
30 min
Runs on
Your browser
Check
6 questions

In S2.11 one branch was taken, not taken, taken, not taken. A 2-bit counter got it wrong 40 times in 40. Yet anybody who sees the last outcome can tell the next one.

Before You Start

You need the 2-bit counter from S2.11, and the counter table with its index from S2.27.

You need the XOR of two numbers, bit by bit, and a shift register.

Five questions this page answers
Question
1What does a history register hold?
2How does gshare choose its counter?
3What is the difference between global and local history?
4Why can a predictor with history be worse than a plain counter table?
5How many outcomes should a predictor remember?

The Idea: The Last Outcomes Say What Comes Next

Keep the last few outcomes in a shift register: 1 for taken, 0 for not taken. After each branch, shift the new outcome in. This is the history register, and what it holds is the branch history.

Now use the history to choose the counter. A branch then has several counters, one for each history it meets. Each counter learns what the branch does after that one history.

An alternating branch needs two of them. After "taken" the counter learns "not taken". After "not taken" the other one learns "taken". Both are soon right every time.

One history register can serve all branches. Then it holds what the last branches of the whole program did. That is global history. It also catches a branch that depends on another branch.

How do the address and the history choose one counter together? The common answer is an XOR of the two. The predictor built that way is called gshare.

The other way is a history for each branch by itself: local history. It sees the pattern of one branch, and nothing of the others.

New Words

New words on this page
WordMeaning
Branch historyThe outcomes of the last branches, in order: taken or not taken.
History registerA shift register that holds the branch history. Each decided branch shifts one bit in.
Global historyOne history for all branches of the program together.
Local historyA history for each branch by itself. Here: for each entry of a small table.
gshareA predictor whose counter is chosen by the XOR of address bits and the global history.
Correlated branchesBranches whose outcomes depend on each other, so that one tells something about the next.

One Picture: Address XOR History

Gshare: The Address and the History Choose the CounterStep through the picture. It follows one guess of the lab and what the predictor learns afterwards.
  • Processor
  • Memory
  • Control / FSM
gshare: The Address and the History Choose the CounterStep through the picture. It follows one guess of the lab and what the predictor learns afterwards. component PC the address of thebranch: its low bits component History register the last 4 outcomesof all branches component XOR mixes the two, bit bybit component Counter table sixteen 2-bitcounters component Guess taken at a count of 2or 3 component Outcome what the branchreally did, from EX entry 3T N T Tcounter 8the countcount up or downshift in
  1. component PCthe address of the branch: its low bits
    • entry 3 → XOR
  2. component History registerthe last 4 outcomes of all branches
    • T N T T → XOR
  3. component XORmixes the two, bit by bit
    • counter 8 → Counter table
  4. component Counter tablesixteen 2-bit counters
    • the count → Guess
  5. component Guesstaken at a count of 2 or 3
  6. component Outcomewhat the branch really did, from EX
    • count up or down → Counter table
    • shift in → History register
  1. Two inputs

    The branch at 0c would use entry 3 of a plain table. The history register holds the last four outcomes: T N T T.

  2. One counter

    T N T T is 1011 in bits. Entry 3 is 0011. The XOR of the two is 1000: counter 8.

  3. The guess

    Counter 8 gives the guess. It is the counter for "this branch, after exactly these four outcomes".

  4. Learning

    When the branch is decided, counter 8 counts up or down. Then the outcome is shifted into the history register, and the oldest outcome falls out.

Read the diagram as text
  • PC: the address of the branch: its low bits
  • History register: the last 4 outcomes of all branches
  • XOR: mixes the two, bit by bit
  • Counter table: sixteen 2-bit counters
  • Guess: taken at a count of 2 or 3
  • Outcome: what the branch really did, from EX
  • PC → XOR: entry 3
  • History register → XOR: T N T T
  • XOR → Counter table: counter 8
  • Counter table → Guess: the count
  • Outcome → Counter table: count up or down
  • Outcome → History register: shift in

The same branch uses different counters after different histories. Each counter learns what follows one history.

Three predictors of this page
PredictorWhat chooses the counterBits in this lab
2-bit tablethe address16 counters: 32
gsharethe address XOR the last 4 outcomes of all branches16 counters and 4 history bits: 36
Local historythe last 4 outcomes of this branch16 histories of 4 bits and 16 counters: 96

gshare costs almost nothing more than the plain table. The local predictor needs a table of histories as well.

The Smallest Example, Worked by Hand

Take a branch that alternates: T, N, T, N. Give it a history of one outcome, and one counter for each history. Both counters start at 1, and the history starts at N.

Six times of an alternating branch, with one outcome of history
TimeHistoryCounter usedSaysIt didThe counter after
1Nthe counter "after N": 1N wrongT2
2Tthe counter "after T": 1N rightN0
3Nthe counter "after N": 2T rightT3
4Tthe counter "after T": 0N rightN0
5Nthe counter "after N": 3T rightT3
6Tthe counter "after T": 0N rightN0

One wrong guess, then right every time. The plain 2-bit counter of S2.11 was wrong every time.

Each counter sees only one kind of moment. The counter for the history N always meets a taken branch, so it climbs to 3. The counter for the history T always meets the other outcome, so it falls to 0.

Neither counter has a mixed habit any more. The history has split one hard branch into two easy ones.

The Rules

  1. Rule 1: The history register shifts in one bit for each decided branch.

    The newest outcome is at one end. The oldest falls out at the other.

  2. Rule 2: A counter is chosen by the address and the history together.

    In gshare: the entry of the plain table, XOR the history bits.

  3. Rule 3: Each counter learns what follows one history.

    It is updated like any 2-bit counter: up for taken, down for not taken.

  4. Rule 4: Global history is shared by all branches.

    It can catch a branch that depends on other branches. Any branch without a pattern scrambles it for all.

  5. Rule 5: Local history belongs to one branch.

    It catches the pattern of that branch, whatever the others do.

  6. Rule 6: Every history that a branch meets needs a warm-up of its own.

    More history bits mean more counters in use for each branch, and more first times.

Sources for These Rules

Checked in the library: Patterson and Hennessy, Computer Organization and Design (ARM edition), section 4.8, part "Dynamic Branch Prediction". It describes correlating predictors, which use the behaviour of recently executed branches as well as the branch itself. It says that the global behaviour acts like extra index bits. Harris and Harris (ARM edition), section 7.7.3, says only that more history can raise the accuracy. The name gshare, the XOR of address and history, and the two-level form of the local predictor are not in these two books. They are described here as commonly taught, and they are not checked against a source. The sizes are choices of this lab. Every count is recorded from the lab. The branches come from the real core ap_rv32, which has no predictor: docs/soc/ap_rv32-blueprint.md, section 8.4.

Now Run It

The lab at the end of this page is test code. It replays the recorded branches with three predictors: the 2-bit table of S2.27, gshare, and local history.

The program "pattern": one loop of 40 rounds with four branches, at 0c, 18, 20 and 2c.
00          addi x5, x0, 40
04          addi x9, x0, 0
08  loop:   andi x6, x5, 1
0c          beq x6, x0, even
10          addi x9, x9, 1
14  even:   andi x7, x5, 3
18          bne x7, x0, skip
1c          addi x9, x9, 16
20  skip:   beq x6, x7, same
24          addi x9, x9, 256
28  same:   addi x5, x5, -1
2c          bne x5, x0, loop
30          ebreak

The first part follows the branch at 0c of "pattern". It is taken in every second round of a loop. The loop has four branches, so the history shows one whole round.

Printed by the lab on Icarus: the first 10 times of the branch at 0c, then its totals. The counts equal those of a second model in JavaScript.
 n | did | last | counter | gshare says
 1 | T   | NNNN |       3 | N wrong
 2 | N   | TNTT |       8 | N right
 3 | T   | NTNT |       6 | N wrong
 4 | N   | TTNT |      14 | T wrong
 5 | T   | NTTT |       4 | N wrong
 6 | N   | TNTT |       8 | N right
 7 | T   | NTNT |       6 | T right
 8 | N   | TTNT |      14 | N right
 9 | T   | NTTT |       4 | T right
10 | N   | TNTT |       8 | N right
the branch at 0c: 40 times | 2-bit table right=0 | gshare right=36 | local history right=37

Rule 2 at work: read row 2. The history is T N T T, and the counter is 8. Rows 6 and 10 have the same history and use the same counter.

Rule 3 at work: rows 3 and 7 both have the history N T N T and use counter 6. In row 3 it has not learned yet. In row 7 it says taken, and it is right.

From row 6 on, every guess is right. In all, gshare gets 36 of 40. The 2-bit table gets 0.

Row 4 is wrong for another reason. Counter 14 had been pushed towards taken by another branch that landed on it. gshare has aliasing too.

The second part counts all branches of the three traces.

Right guesses of each predictor, out of all conditional branches of the trace.
trace   | branches | 2-bit table | gshare | local history
loops   |      132 |         111 |    103 |           102
pattern |      160 |          77 |    139 |           139
calls   |       80 |          27 |     63 |            57

On "pattern", both predictors with history get 139 of 160, or 87 percent. The plain table gets 77.

Rules 4 and 5 at work: "calls" has one branch that follows a scrambled number. It fills the global history with noise, yet gshare still gets 63 of 80. Local history gets 57.

Rule 6 at work: on "loops" the plain table wins, 111 against 103 and 102. Its branches have no pattern to find, and every extra counter only adds first times.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
XOR of address and historyPut address bits and history bits side by sideAll index bits carry both. Two different pairs can give the same counter.
One global historyA history for each branchA few bits in all, and it sees how branches depend on each other. One noisy branch disturbs every other.
Local historyGlobal historyThe pattern of one branch stays clean. It needs a table of histories, read before the counter.
Shift in the real outcomeShift in the guessThe history is always true. It is known only when the branch is decided, two cycles after the fetch.
A short historyA long historyA short warm-up. Patterns longer than the history are not seen.

The last row of the table has no best answer. That is the trap of this page.

The Trap: A Longer History Can Be Worse

If four outcomes of history are good, six should be better. Try it: gshare with 64 counters, and a history of 0 to 6 outcomes.

Printed by the lab: right guesses on each trace for each length of the history.
gshare with 64 counters: right guesses for each length of its history
outcomes remembered |   loops | pattern |   calls
                  0 |     111 |      77 |      27
                  1 |     109 |     115 |      46
                  2 |     108 |      96 |      56
                  3 |     107 |     110 |      55
                  4 |     103 |     139 |      64
                  5 |      97 |     138 |      61
                  6 |      90 |     136 |      59
TransientRight Guesses Against the Length of the HistoryRecorded by the lab: gshare with 64 counters on the three traces, for 0 to 6 outcomes of history. Given in percent of the branches of each trace.
  • loops
  • pattern
  • calls
0102030405060708090Right guesses, percent0123456Outcomes that gshare remembers020406080100Right guesses, percent0246Outcomes that gshare remembers
  1. Outcomes that gshare remembers 0loops = 84 · pattern = 48 · calls = 34

    No history: this is the plain counter table. "loops" is at its best here, and the other two are poor.

  2. Outcomes that gshare remembers 4loops = 78 · pattern = 87 · calls = 80

    Four outcomes: "pattern" and "calls" reach their best. Their branches repeat with a period of four rounds or less.

  3. Outcomes that gshare remembers 6loops = 68 · pattern = 85 · calls = 74

    Six outcomes: every trace is lower than at four. Each branch now spreads over more counters, and each counter must be trained first.

Read the plot as a table
Outcomes that gshare remembersloopspatterncalls
0844834
1837258
2826070
3816969
4788780
5738676
6688574

More history helps a branch that follows a pattern. It costs a branch that has none, in a run as short as these.

Read the column of "loops". It only falls: 111 right guesses with no history, 90 with six outcomes.

The branches of "loops" are simple. With six outcomes of history, each of them meets many different histories. Each history has a counter of its own that starts untrained.

The other two traces peak at four outcomes and fall a little after that. Their patterns are four rounds long or less.

So no single history length is right for all branches. S2.28 lets two predictors run side by side, and picks one of them for each branch.

At Scale

This lab16 counters, 4 outcomes of history: 36 bits
A real predictorthousands of counters, a history of ten outcomes or more

This section looks ahead. The traces here have 80 to 160 branches. A real program runs billions, and a warm-up of a few hundred no longer counts.

  1. Not on ap_rv32

    The core ap_rv32 has no predictor. These counts say what a predictor beside its fetch would have guessed for the branches it executed.

  2. Long runs forgive long histories

    In a long run each counter is trained once and then used many times. So real predictors afford much longer histories than this lab. That is common practice.

  3. The history is late

    A branch is decided two cycles after it is fetched. A real fetch shifts its guess into the history at once and repairs the history after a wrong guess. That is common practice, and not built here.

  4. Which predictor for which branch

    Some branches want history and some do not. S2.28 keeps both predictors and learns which to trust for each branch.

Try It

Run the lab. In the first table, find two rows with the same history. Do they use the same counter?

Set BITS to 1 and run again. How many of its 40 times does gshare get right for the branch at 0c now? Why is one outcome of global history not enough here?

Set LOCAL to 1. Which trace loses most for the local predictor?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: n | did | last | counter | gshare says 1 | T | NNNN | 3 | N wrong 2 | N | TNTT | 8 | N right 3 | T | NTNT | 6 | N wrong 4 | N | TTNT | 14 | T wrong 5 | T | NTTT | 4 | N wrong 6 | N | TNTT | 8 | N right 7 | T | NTNT | 6 | T right 8 | N | TTNT | 14 | N right 9 | T | NTTT | 4 | T right 10 | N | TNTT | 8 | N right the branch at 0c: 40 times | 2-bit table right=0 | gshare right=36 | local history right=37 trace | branches | 2-bit table | gshare | local history loops | 132 | 111 | 103 | 102 pattern | 160 | 77 | 139 | 139 calls | 80 | 27 | 63 | 57

Check Yourself

Predict the outputThis lab follows the alternating branch of "calls": T, N, T, N, for 40 times. The local predictor remembers 2 outcomes of that branch. Its history starts at N N, and its counters start at 1. The 2-bit table gets 0 right and gshare gets 36. What does the lab print?
// The alternating branch of "calls", with a local history of two outcomes. What does the last line say?
// Test code. It replays the branches that the real core ap_rv32 executed, and counts how often a way of
// guessing would have been right. The core ap_rv32 itself has no such logic: it always fetches on in line.
module tb;
  // ---- The recorded traces. Each is every branch and jump that the real core ap_rv32 executed for one
  // program, in order. One byte for each: bits 7:4 the place (a row of the table of places), bit 3 "it left
  // the line" (a taken branch, or any jump), bits 2:0 which target (used by a return only).
  // In each line of a trace, the first event is the byte at the right-hand end of the number.
  logic [1599:0] events;
  int          count;                 // how many events the trace has
  logic [31:0] place_pc [0:15];       // the address of the branch or jump
  logic [31:0] place_target [0:15];   // where it goes when it leaves the line (not used for a return)
  logic [1:0]  place_kind [0:15];     // 0: a conditional branch. 1: a jal. 2: a return (jalr)
  logic [31:0] return_target [0:7];   // the addresses that the returns of this trace go to
  task automatic load_calls;   // the program "calls": 475 instructions, 200 branches and jumps
    count = 200;
    place_pc[0] = 32'h10;  place_target[0] = 32'h2c;  place_kind[0] = 2'd1;   // call
    place_pc[1] = 32'h30;  place_target[1] = 32'h38;  place_kind[1] = 2'd0;   // branch
    place_pc[2] = 32'h3c;  place_target[2] = 32'h00;  place_kind[2] = 2'd2;   // return
    place_pc[3] = 32'h18;  place_target[3] = 32'h2c;  place_kind[3] = 2'd1;   // call
    place_pc[4] = 32'h1c;  place_target[4] = 32'h40;  place_kind[4] = 2'd1;   // call
    place_pc[5] = 32'h54;  place_target[5] = 32'h5c;  place_kind[5] = 2'd0;   // branch
    place_pc[6] = 32'h5c;  place_target[6] = 32'h00;  place_kind[6] = 2'd2;   // return
    place_pc[7] = 32'h24;  place_target[7] = 32'h0c;  place_kind[7] = 2'd0;   // branch
    return_target[0] = 32'h14;
    return_target[1] = 32'h1c;
    return_target[2] = 32'h20;
    events[0 +: 320] = 320'h786a5848291038281808786a5048291038281808786a5048291038281808786a5048291038281808;   // events 0 to 39
    events[320 +: 320] = 320'h786a5848291038281808786a5848291038281808786a5048291038281808786a5048291038281808;   // events 40 to 79
    events[640 +: 320] = 320'h786a5048291038281808786a5048291038281808786a5848291038281808786a5048291038281808;   // events 80 to 119
    events[960 +: 320] = 320'h786a5848291038281808786a5848291038281808786a5048291038281808786a5848291038281808;   // events 120 to 159
    events[1280 +: 320] = 320'h706a5848291038281808786a5048291038281808786a5048291038281808786a5048291038281808;   // events 160 to 199
  endtask

  // ---- Three predictors. Each counter is a 2-bit counter that starts at 1 and says "taken" at 2 and 3.
  int ENTRIES = 16;          // counters of the plain table, and of gshare
  int BITS    = 4;           // gshare: how many outcomes of the last branches it remembers
  localparam int LOCAL = 2;   // local history: how many outcomes of one branch it remembers
  logic [1:0] plain [0:255];      // the plain table
  logic [1:0] shared [0:255];     // gshare: its counters
  int         recent;             // gshare: the last outcomes of all branches, the newest in bit 0. 1: taken
  int         past [0:15];        // local history: the last outcomes of the branches of one entry
  logic [1:0] habit [0:255];      // local history: one counter for each possible row of outcomes

  function automatic logic [1:0] step(input logic [1:0] old, input logic taken);
    if (taken) return (old == 2'd3) ? 2'd3 : old + 2'd1;
    return (old == 2'd0) ? 2'd0 : old - 2'd1;
  endfunction
  function automatic int low(input int value, input int width);   // the lowest bits of a number: as many as width says
    return value % (1 << width);
  endfunction
  function automatic int plain_entry(input logic [31:0] pc);
    return (pc / 4) % ENTRIES;
  endfunction
  // gshare: the same entry as the plain table would use, with the recent outcomes mixed in by XOR.
  function automatic int shared_entry(input logic [31:0] pc);
    return plain_entry(pc) ^ low(recent, BITS);
  endfunction
  function automatic string letters(input int value, input int width);   // outcomes as letters, the newest last
    string text = "";
    for (int k = width - 1; k >= 0; k--) text = {text, ((value >> k) & 1) ? "T" : "N"};
    return text;
  endfunction
  function automatic string mark(input logic guessed, input logic taken);
    return {guessed ? "T" : "N", guessed == taken ? " right" : " wrong"};
  endfunction

  int branches, right_plain, right_shared, right_local;
  task automatic replay(input int follow, input int times, input string with_what);
    logic [7:0]  e;
    logic [31:0] pc;
    logic        taken, says_plain, says_shared, says_local;
    int p, a, b, c, seen, seen_plain, seen_shared, seen_local;
    for (int k = 0; k < 256; k++) begin plain[k] = 2'd1;  shared[k] = 2'd1;  habit[k] = 2'd1; end
    for (int k = 0; k < 16; k++) past[k] = 0;
    recent = 0;  branches = 0;  right_plain = 0;  right_shared = 0;  right_local = 0;
    seen = 0;  seen_plain = 0;  seen_shared = 0;  seen_local = 0;
    for (int i = 0; i < count; i++) begin
      e     = events[8 * i +: 8];
      p     = e[7:4];
      taken = e[3];
      pc    = place_pc[p];
      if (place_kind[p] == 2'd0) begin
        a = plain_entry(pc);
        b = shared_entry(pc);
        c = low(past[(pc / 4) % 16], LOCAL);
        says_plain  = plain[a]  >= 2'd2;
        says_shared = shared[b] >= 2'd2;
        says_local  = habit[c]  >= 2'd2;
        branches++;
        if (says_plain  == taken) right_plain++;
        if (says_shared == taken) right_shared++;
        if (says_local  == taken) right_local++;
        if (p == follow) begin
          seen++;
          if (says_plain  == taken) seen_plain++;
          if (says_shared == taken) seen_shared++;
          if (says_local  == taken) seen_local++;
          if (seen <= times && with_what == "gshare") $display("%2d | %s   | %s | %7d | %s", seen, taken ? "T" : "N", letters(low(recent, BITS), BITS), b, mark(says_shared, taken));
          if (seen <= times && with_what == "local")  $display("%2d | %s   | %s | %7d | %s", seen, taken ? "T" : "N", letters(c, LOCAL), c, mark(says_local, taken));
        end
        // Learn: each predictor moves the counter it has just used. Then the outcome joins the histories.
        plain[a]  = step(plain[a], taken);
        shared[b] = step(shared[b], taken);
        habit[c]  = step(habit[c], taken);
        recent    = low(recent * 2 + taken, 30);
        past[(pc / 4) % 16] = low(past[(pc / 4) % 16] * 2 + taken, 30);
      end
    end
    if (follow >= 0) $display("the branch at %02h: %0d times | 2-bit table right=%0d | gshare right=%0d | local history right=%0d", place_pc[follow][7:0], seen, seen_plain, seen_shared, seen_local);
  endtask

  initial begin
    load_calls;
    replay(1, 0, "local");
    $finish;
  end
endmodule
ConceptWhat does a history register hold?
ConceptA plain table would use entry 5, which is 0101. The history is T T N N, which is 1100. Which counter does gshare use?
ConceptWhich predictor is the best choice for a branch whose outcome depends on the branch before it?
ConceptOn "loops", gshare with six outcomes of history gets fewer right than a plain table. Why?
ConceptWhen does the history register change?

Common Mistakes

Wrong Update the counter that the new history points to.

The guess came from the counter chosen by the old history. That one must learn the outcome.

Fix Update the counter that was used, then shift the history.

Wrong Make the history as long as the table allows.

Each branch then spreads over many counters, and each must be trained before it helps.

Fix Choose the length by measuring on real traces.

Wrong Expect global history to find the pattern of every branch.

It holds the outcomes of all branches. A branch without a pattern fills it with noise.

Fix Use local history where a branch must be seen alone.

Remember

  • A history register holds the outcomes of the last branches. Each decided branch shifts one bit in.
  • gshare chooses its counter by the XOR of address bits and the global history. Local history uses the outcomes of the branch itself.
  • Each counter learns what follows one history. An alternating branch becomes two steady ones.
  • On the recorded traces: "pattern" 77 right with a plain table, 139 with history. "loops" 111 with the plain table, 103 with gshare. ap_rv32 has no predictor.

Practice

Report a problem with this page