btb.hh revision 2307
12SN/A/*
21762SN/A * Copyright (c) 2004-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665SN/A */
282665SN/A
292665SN/A#ifndef __CPU_O3_BTB_HH__
302665SN/A#define __CPU_O3_BTB_HH__
312665SN/A
322SN/A// For Addr type.
332SN/A#include "arch/isa_traits.hh"
341722SN/A
355480Snate@binkert.orgclass DefaultBTB
362SN/A{
372SN/A  private:
38146SN/A    struct BTBEntry
392SN/A    {
402SN/A        BTBEntry()
418739Sgblack@eecs.umich.edu            : tag(0), target(0), valid(false)
428739Sgblack@eecs.umich.edu        {
438739Sgblack@eecs.umich.edu        }
442158SN/A
458739Sgblack@eecs.umich.edu        /** The entry's tag. */
46146SN/A        Addr tag;
471805SN/A
48146SN/A        /** The entry's target. */
491717SN/A        Addr target;
502680SN/A
518232Snate@binkert.org        /** The entry's thread id. */
525480Snate@binkert.org        unsigned tid;
538741Sgblack@eecs.umich.edu
548741Sgblack@eecs.umich.edu        /** Whether or not the entry is valid. */
558741Sgblack@eecs.umich.edu        bool valid;
562521SN/A    };
5756SN/A
585478SN/A  public:
593348SN/A    /** Creates a BTB with the given number of entries, number of bits per
603348SN/A     *  tag, and instruction offset amount.
612521SN/A     *  @param numEntries Number of entries for the BTB.
625480Snate@binkert.org     *  @param tagBits Number of bits for each tag in the BTB.
631805SN/A     *  @param instShiftAmt Offset amount for instructions to ignore alignment.
642SN/A     */
652SN/A    DefaultBTB(unsigned numEntries, unsigned tagBits,
662107SN/A               unsigned instShiftAmt);
672SN/A
685480Snate@binkert.org    void reset();
695478SN/A
708739Sgblack@eecs.umich.edu    /** Looks up an address in the BTB. Must call valid() first on the address.
718739Sgblack@eecs.umich.edu     *  @param inst_PC The address of the branch to look up.
728739Sgblack@eecs.umich.edu     *  @param tid The thread id.
738739Sgblack@eecs.umich.edu     *  @return Returns the target of the branch.
742SN/A     */
75545SN/A    Addr lookup(const Addr &inst_PC, unsigned tid);
762521SN/A
772521SN/A    /** Checks if a branch is in the BTB.
782521SN/A     *  @param inst_PC The address of the branch to look up.
792521SN/A     *  @param tid The thread id.
802SN/A     *  @return Whether or not the branch exists in the BTB.
812SN/A     */
822SN/A    bool valid(const Addr &inst_PC, unsigned tid);
83926SN/A
84926SN/A    /** Updates the BTB with the target of a branch.
85926SN/A     *  @param inst_PC The address of the branch being updated.
86926SN/A     *  @param target_PC The target address of the branch.
87926SN/A     *  @param tid The thread id.
88926SN/A     */
89926SN/A    void update(const Addr &inst_PC, const Addr &target_PC,
904395SN/A                unsigned tid);
911805SN/A
922SN/A  private:
932SN/A    /** Returns the index into the BTB, based on the branch's PC.
941634SN/A     *  @param inst_PC The branch to look up.
955480Snate@binkert.org     *  @return Returns the index into the BTB.
961634SN/A     */
978739Sgblack@eecs.umich.edu    inline unsigned getIndex(const Addr &inst_PC);
982549SN/A
995714Shsul@eecs.umich.edu    /** Returns the tag bits of a given address.
1001634SN/A     *  @param inst_PC The branch's address.
1011634SN/A     *  @return Returns the tag bits.
1021634SN/A     */
1031634SN/A    inline Addr getTag(const Addr &inst_PC);
1041634SN/A
1058741Sgblack@eecs.umich.edu    /** The actual BTB. */
1068741Sgblack@eecs.umich.edu    std::vector<BTBEntry> btb;
1078741Sgblack@eecs.umich.edu
1088739Sgblack@eecs.umich.edu    /** The number of entries in the BTB. */
1091634SN/A    unsigned numEntries;
1101634SN/A
1112512SN/A    /** The index mask. */
1125480Snate@binkert.org    unsigned idxMask;
1132SN/A
1142SN/A    /** The number of tag bits per entry. */
1152512SN/A    unsigned tagBits;
1162512SN/A
1172512SN/A    /** The tag mask. */
1182512SN/A    unsigned tagMask;
119540SN/A
1202641SN/A    /** Number of bits to shift PC when calculating index. */
1212522SN/A    unsigned instShiftAmt;
1222641SN/A
1232512SN/A    /** Number of bits to shift PC when calculating tag. */
1242630SN/A    unsigned tagShiftAmt;
1254986SN/A};
1262521SN/A
1272641SN/A#endif // __CPU_O3_BTB_HH__
128873SN/A