1/*
2 * Copyright (c) 2014 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Anthony Gutierrez
29 */
30
31/* @file
32 * Implementation of a bi-mode branch predictor
33 */
34
35#ifndef __CPU_PRED_BI_MODE_PRED_HH__
36#define __CPU_PRED_BI_MODE_PRED_HH__
37
38#include "base/sat_counter.hh"
39#include "cpu/pred/bpred_unit.hh"
40#include "params/BiModeBP.hh"
41
42/**
43 * Implements a bi-mode branch predictor. The bi-mode predictor is a two-level
44 * branch predictor that has three seprate history arrays: a taken array, a
45 * not-taken array, and a choice array. The taken/not-taken arrays are indexed
46 * by a hash of the PC and the global history. The choice array is indexed by
47 * the PC only. Because the taken/not-taken arrays use the same index, they must
48 * be the same size.
49 *
50 * The bi-mode branch predictor aims to eliminate the destructive aliasing that
51 * occurs when two branches of opposite biases share the same global history
52 * pattern. By separating the predictors into taken/not-taken arrays, and using
53 * the branch's PC to choose between the two, destructive aliasing is reduced.
54 */
55
56class BiModeBP : public BPredUnit
57{
58  public:
59    BiModeBP(const BiModeBPParams *params);
60    void uncondBranch(ThreadID tid, Addr pc, void * &bp_history);
61    void squash(ThreadID tid, void *bp_history);
62    bool lookup(ThreadID tid, Addr branch_addr, void * &bp_history);
63    void btbUpdate(ThreadID tid, Addr branch_addr, void * &bp_history);
64    void update(ThreadID tid, Addr branch_addr, bool taken, void *bp_history,
65                bool squashed, const StaticInstPtr & inst, Addr corrTarget);
66
67  private:
68    void updateGlobalHistReg(ThreadID tid, bool taken);
69
70    struct BPHistory {
71        unsigned globalHistoryReg;
72        // was the taken array's prediction used?
73        // true: takenPred used
74        // false: notPred used
75        bool takenUsed;
76        // prediction of the taken array
77        // true: predict taken
78        // false: predict not-taken
79        bool takenPred;
80        // prediction of the not-taken array
81        // true: predict taken
82        // false: predict not-taken
83        bool notTakenPred;
84        // the final taken/not-taken prediction
85        // true: predict taken
86        // false: predict not-taken
87        bool finalPred;
88    };
89
90    std::vector<unsigned> globalHistoryReg;
91    unsigned globalHistoryBits;
92    unsigned historyRegisterMask;
93
94    unsigned choicePredictorSize;
95    unsigned choiceCtrBits;
96    unsigned choiceHistoryMask;
97    unsigned globalPredictorSize;
98    unsigned globalCtrBits;
99    unsigned globalHistoryMask;
100
101    // choice predictors
102    std::vector<SatCounter> choiceCounters;
103    // taken direction predictors
104    std::vector<SatCounter> takenCounters;
105    // not-taken direction predictors
106    std::vector<SatCounter> notTakenCounters;
107
108    unsigned choiceThreshold;
109    unsigned takenThreshold;
110    unsigned notTakenThreshold;
111};
112
113#endif // __CPU_PRED_BI_MODE_PRED_HH__
114