1/*
2 * Copyright 2019 Texas A&M University
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice,
8 *    this list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 *    this list of conditions and the following disclaimer in the documentation
12 *    and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 *    contributors may be used to endorse or promote products derived from this
16 *    software without specific prior written permission.
17 *
18 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 *  Author: Daniel A. Jiménez
31 *  Adapted to gem5 by: Javier Bueno Hedo
32 *
33 */
34
35/*
36 * Multiperspective Perceptron Predictor with TAGE (by Daniel A. Jiménez)
37 * 8 KB version
38 */
39
40#include "cpu/pred/multiperspective_perceptron_tage_8KB.hh"
41
42MPP_TAGE_8KB*
43MPP_TAGE_8KBParams::create()
44{
45    return new MPP_TAGE_8KB(this);
46}
47
48MPP_LoopPredictor_8KB*
49MPP_LoopPredictor_8KBParams::create()
50{
51    return new MPP_LoopPredictor_8KB(this);
52}
53
54MPP_StatisticalCorrector_8KB::MPP_StatisticalCorrector_8KB(
55        const MPP_StatisticalCorrector_8KBParams *p)
56  : MPP_StatisticalCorrector(p)
57{
58}
59
60MPP_StatisticalCorrector_8KB::SCThreadHistory*
61MPP_StatisticalCorrector_8KB::makeThreadHistory()
62{
63    MPP_SCThreadHistory *sh = new MPP_SCThreadHistory();
64
65    sh->setNumOrdinalHistories(1);
66    sh->initLocalHistory(1, numEntriesFirstLocalHistories, 4);
67
68    return sh;
69}
70
71void
72MPP_StatisticalCorrector_8KB::getBiasLSUM(Addr branch_pc,
73        StatisticalCorrector::BranchInfo* bi, int &lsum) const
74{
75    int8_t ctr = bias[getIndBias(branch_pc, bi, false /* unused */)];
76    lsum += 2 * ctr + 1;
77    ctr = biasSK[getIndBiasSK(branch_pc, bi)];
78    lsum += 2 * ctr + 1;
79}
80
81int
82MPP_StatisticalCorrector_8KB::gPredictions(ThreadID tid, Addr branch_pc,
83        StatisticalCorrector::BranchInfo* bi, int & lsum, int64_t phist)
84{
85    MPP_SCThreadHistory *sh = static_cast<MPP_SCThreadHistory *>(scHistory);
86    unsigned int pc = branch_pc;
87    lsum += gPredict((pc << 1) + bi->predBeforeSC, sh->globalHist << 11,
88                     gm, ggehl, gnb, logGnb, wg);
89
90    // Local History #1
91    lsum += 2 * gPredict(branch_pc, sh->getLocalHistory(1, branch_pc),
92                         lm, lgehl, lnb, logLnb, wl);
93    if (sh->getLocalHistory(1, branch_pc) == 2047) lsum += 4;
94    if (sh->getLocalHistory(1, branch_pc) == 0) lsum -= 4;
95
96    lsum += gPredict(branch_pc, sh->getHistoryStackEntry(),
97                     pm, pgehl, pnb, logPnb, wp);
98
99    int thres = pUpdateThreshold[getIndUpd(branch_pc)];
100
101    return thres;
102}
103
104void
105MPP_StatisticalCorrector_8KB::gUpdates(ThreadID tid, Addr pc, bool taken,
106        StatisticalCorrector::BranchInfo* bi, int64_t phist)
107{
108    MPP_SCThreadHistory *sh = static_cast<MPP_SCThreadHistory *>(scHistory);
109
110    gUpdate((pc << 1) + bi->predBeforeSC, taken, sh->globalHist << 11,
111            gm, ggehl, gnb, logGnb, wg, bi);
112
113    gUpdate(pc, taken, sh->getLocalHistory(1, pc),
114            lm, lgehl, lnb, logLnb, wl, bi);
115
116    gUpdate(pc, taken, sh->getHistoryStackEntry(),
117            pm, pgehl, pnb, logPnb, wp, bi);
118}
119
120void
121MPP_StatisticalCorrector_8KB::scHistoryUpdate(Addr branch_pc,
122        const StaticInstPtr &inst, bool taken,
123        StatisticalCorrector::BranchInfo *bi, Addr corrTarget)
124{
125    int brtype = inst->isDirectCtrl() ? 0 : 2;
126    if (! inst->isUncondCtrl()) {
127        ++brtype;
128    }
129
130    MPP_SCThreadHistory *sh = static_cast<MPP_SCThreadHistory *>(scHistory);
131
132    if (brtype & 1) {
133        sh->globalHist = (sh->globalHist << 1) + taken;
134    }
135    sh->updateHistoryStack(corrTarget, taken, inst->isCall(),
136                           inst->isReturn());
137
138    StatisticalCorrector::scHistoryUpdate(branch_pc, inst, taken, bi,
139                                          corrTarget);
140}
141
142size_t
143MPP_StatisticalCorrector_8KB::getSizeInBits() const
144{
145    size_t bits = 16; //global histories
146
147    bits += (1 << logSizeUp) * pUpdateThresholdWidth;
148
149    bits += scCountersWidth * 2 * (1 << logBias); //2 bias arrays
150
151    bits += (gnb - 2) * (1 << logGnb) * (scCountersWidth - 1) +
152            (1 << (logGnb - 1)) * (2 * scCountersWidth - 1);
153
154    bits += (pnb - 2) * (1 << logPnb) * (scCountersWidth - 1) +
155            (1 << (logPnb - 1)) * (2 * scCountersWidth - 1);
156
157    bits += (lnb - 2) * (1 << logLnb) * (scCountersWidth - 1) +
158            (1 << (logLnb - 1)) * (2 * scCountersWidth - 1);
159
160    bits += numEntriesFirstLocalHistories * lm[0];
161
162    bits += 16 * 16; // History stack
163    bits += 4;       // History stack pointer
164
165    bits += 3 * chooserConfWidth; // 3 chooser counters
166
167    return bits;
168}
169
170MPP_StatisticalCorrector_8KB*
171MPP_StatisticalCorrector_8KBParams::create()
172{
173    return new MPP_StatisticalCorrector_8KB(this);
174}
175
176MultiperspectivePerceptronTAGE8KB::MultiperspectivePerceptronTAGE8KB(
177        const MultiperspectivePerceptronTAGE8KBParams *p)
178    : MultiperspectivePerceptronTAGE(p)
179{
180}
181
182void
183MultiperspectivePerceptronTAGE8KB::createSpecs()
184{
185    addSpec(new BLURRYPATH(5, 15, -1, 2.25, 0, 6, *this));
186    addSpec(new RECENCYPOS(31, 3.5, 0, 6, *this));
187    addSpec(new GHISTMODPATH(3, 7, 1, 2.24, 0, 6, *this));
188    addSpec(new IMLI(1, 2.23, 0, 6, *this));
189    addSpec(new IMLI(4, 1.98, 0, 6, *this));
190}
191
192MultiperspectivePerceptronTAGE8KB*
193MultiperspectivePerceptronTAGE8KBParams::create()
194{
195    return new MultiperspectivePerceptronTAGE8KB(this);
196}
197