btb.hh revision 6226:f1076450ab2b
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 * Authors: Kevin Lim
29 */
30
31#ifndef __CPU_O3_BTB_HH__
32#define __CPU_O3_BTB_HH__
33
34#include "base/misc.hh"
35#include "base/types.hh"
36
37class DefaultBTB
38{
39  private:
40    struct BTBEntry
41    {
42        BTBEntry()
43            : tag(0), target(0), valid(false)
44        {
45        }
46
47        /** The entry's tag. */
48        Addr tag;
49
50        /** The entry's target. */
51        Addr target;
52
53        /** The entry's thread id. */
54        ThreadID tid;
55
56        /** Whether or not the entry is valid. */
57        bool valid;
58    };
59
60  public:
61    /** Creates a BTB with the given number of entries, number of bits per
62     *  tag, and instruction offset amount.
63     *  @param numEntries Number of entries for the BTB.
64     *  @param tagBits Number of bits for each tag in the BTB.
65     *  @param instShiftAmt Offset amount for instructions to ignore alignment.
66     */
67    DefaultBTB(unsigned numEntries, unsigned tagBits,
68               unsigned instShiftAmt);
69
70    void reset();
71
72    /** Looks up an address in the BTB. Must call valid() first on the address.
73     *  @param inst_PC The address of the branch to look up.
74     *  @param tid The thread id.
75     *  @return Returns the target of the branch.
76     */
77    Addr lookup(const Addr &inst_PC, ThreadID tid);
78
79    /** Checks if a branch is in the BTB.
80     *  @param inst_PC The address of the branch to look up.
81     *  @param tid The thread id.
82     *  @return Whether or not the branch exists in the BTB.
83     */
84    bool valid(const Addr &inst_PC, ThreadID tid);
85
86    /** Updates the BTB with the target of a branch.
87     *  @param inst_PC The address of the branch being updated.
88     *  @param target_PC The target address of the branch.
89     *  @param tid The thread id.
90     */
91    void update(const Addr &inst_PC, const Addr &target_PC,
92                ThreadID tid);
93
94  private:
95    /** Returns the index into the BTB, based on the branch's PC.
96     *  @param inst_PC The branch to look up.
97     *  @return Returns the index into the BTB.
98     */
99    inline unsigned getIndex(const Addr &inst_PC);
100
101    /** Returns the tag bits of a given address.
102     *  @param inst_PC The branch's address.
103     *  @return Returns the tag bits.
104     */
105    inline Addr getTag(const Addr &inst_PC);
106
107    /** The actual BTB. */
108    std::vector<BTBEntry> btb;
109
110    /** The number of entries in the BTB. */
111    unsigned numEntries;
112
113    /** The index mask. */
114    unsigned idxMask;
115
116    /** The number of tag bits per entry. */
117    unsigned tagBits;
118
119    /** The tag mask. */
120    unsigned tagMask;
121
122    /** Number of bits to shift PC when calculating index. */
123    unsigned instShiftAmt;
124
125    /** Number of bits to shift PC when calculating tag. */
126    unsigned tagShiftAmt;
127};
128
129#endif // __CPU_O3_BTB_HH__
130