bpred_unit.hh revision 1689
1/*
2 * Copyright (c) 2004-2005 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
29#ifndef __BPRED_UNIT_HH__
30#define __BPRED_UNIT_HH__
31
32// For Addr type.
33#include "arch/alpha/isa_traits.hh"
34#include "base/statistics.hh"
35#include "cpu/inst_seq.hh"
36
37#include "cpu/beta_cpu/2bit_local_pred.hh"
38#include "cpu/beta_cpu/tournament_pred.hh"
39#include "cpu/beta_cpu/btb.hh"
40#include "cpu/beta_cpu/ras.hh"
41
42#include <list>
43
44/**
45 * Basically a wrapper class to hold both the branch predictor
46 * and the BTB.  Right now I'm unsure of the implementation; it would
47 * be nicer to have something closer to the CPUPolicy or the Impl where
48 * this is just typedefs, but it forces the upper level stages to be
49 * aware of the constructors of the BP and the BTB.  The nicer thing
50 * to do is have this templated on the Impl, accept the usual Params
51 * object, and be able to call the constructors on the BP and BTB.
52 */
53template<class Impl>
54class TwobitBPredUnit
55{
56  public:
57    typedef typename Impl::Params Params;
58    typedef typename Impl::DynInstPtr DynInstPtr;
59
60    TwobitBPredUnit(Params &params);
61
62    void regStats();
63
64    bool predict(DynInstPtr &inst, Addr &PC);
65
66    void update(const InstSeqNum &done_sn);
67
68    void squash(const InstSeqNum &squashed_sn);
69
70    void squash(const InstSeqNum &squashed_sn, const Addr &corr_target,
71                bool actually_taken);
72
73    bool BPLookup(Addr &inst_PC)
74    { return BP.lookup(inst_PC); }
75
76    bool BTBValid(Addr &inst_PC)
77    { return BTB.valid(inst_PC); }
78
79    Addr BTBLookup(Addr &inst_PC)
80    { return BTB.lookup(inst_PC); }
81
82    // Will want to include global history.
83    void BPUpdate(Addr &inst_PC, bool taken)
84    { BP.update(inst_PC, taken); }
85
86    void BTBUpdate(Addr &inst_PC, Addr &target_PC)
87    { BTB.update(inst_PC, target_PC); }
88
89  private:
90    struct PredictorHistory {
91        PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC,
92                         const bool pred_taken)
93            : seqNum(seq_num), PC(inst_PC), predTaken(pred_taken),
94              globalHistory(0), usedRAS(0), wasCall(0), RASIndex(0),
95              RASTarget(0)
96        { }
97
98        InstSeqNum seqNum;
99
100        Addr PC;
101
102        bool predTaken;
103
104        unsigned globalHistory;
105
106        bool usedRAS;
107
108        bool wasCall;
109
110        unsigned RASIndex;
111
112        Addr RASTarget;
113    };
114
115    std::list<PredictorHistory> predHist;
116
117    DefaultBP BP;
118
119    DefaultBTB BTB;
120
121    ReturnAddrStack RAS;
122
123    Stats::Scalar<> lookups;
124    Stats::Scalar<> condPredicted;
125    Stats::Scalar<> condIncorrect;
126    Stats::Scalar<> BTBLookups;
127    Stats::Scalar<> BTBHits;
128    Stats::Scalar<> BTBCorrect;
129    Stats::Scalar<> usedRAS;
130    Stats::Scalar<> RASIncorrect;
131};
132
133#endif // __BPRED_UNIT_HH__
134