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