free_list.hh revision 8232
14202Sbinkertn@umich.edu/*
24202Sbinkertn@umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
34202Sbinkertn@umich.edu * All rights reserved.
44202Sbinkertn@umich.edu *
54202Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
64202Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
74202Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
84202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
94202Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
104202Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
114202Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
124202Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
134202Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
144202Sbinkertn@umich.edu * this software without specific prior written permission.
154202Sbinkertn@umich.edu *
164202Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174202Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184202Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194202Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204202Sbinkertn@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214202Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224202Sbinkertn@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234202Sbinkertn@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244202Sbinkertn@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254202Sbinkertn@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264202Sbinkertn@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274202Sbinkertn@umich.edu *
284202Sbinkertn@umich.edu * Authors: Kevin Lim
294202Sbinkertn@umich.edu */
304202Sbinkertn@umich.edu
314202Sbinkertn@umich.edu#ifndef __CPU_O3_FREE_LIST_HH__
324202Sbinkertn@umich.edu#define __CPU_O3_FREE_LIST_HH__
334486Sbinkertn@umich.edu
344486Sbinkertn@umich.edu#include <iostream>
354776Sgblack@eecs.umich.edu#include <queue>
364486Sbinkertn@umich.edu
374202Sbinkertn@umich.edu#include "arch/registers.hh"
384202Sbinkertn@umich.edu#include "base/misc.hh"
394202Sbinkertn@umich.edu#include "base/trace.hh"
404202Sbinkertn@umich.edu#include "config/the_isa.hh"
414202Sbinkertn@umich.edu#include "cpu/o3/comm.hh"
424202Sbinkertn@umich.edu#include "debug/FreeList.hh"
434202Sbinkertn@umich.edu
444202Sbinkertn@umich.edu/**
454202Sbinkertn@umich.edu * FreeList class that simply holds the list of free integer and floating
464202Sbinkertn@umich.edu * point registers.  Can request for a free register of either type, and
474202Sbinkertn@umich.edu * also send back free registers of either type.  This is a very simple
484202Sbinkertn@umich.edu * class, but it should be sufficient for most implementations.  Like all
494202Sbinkertn@umich.edu * other classes, it assumes that the indices for the floating point
504202Sbinkertn@umich.edu * registers starts after the integer registers end.  Hence the variable
514202Sbinkertn@umich.edu * numPhysicalIntRegs is logically equivalent to the baseFP dependency.
524202Sbinkertn@umich.edu * Note that while this most likely should be called FreeList, the name
534826Ssaidi@eecs.umich.edu * "FreeList" is used in a typedef within the CPU Policy, and therefore no
544202Sbinkertn@umich.edu * class can be named simply "FreeList".
554202Sbinkertn@umich.edu * @todo: Give a better name to the base FP dependency.
565016Sgblack@eecs.umich.edu */
574486Sbinkertn@umich.educlass SimpleFreeList
584486Sbinkertn@umich.edu{
594202Sbinkertn@umich.edu  private:
604202Sbinkertn@umich.edu    /** The list of free integer registers. */
615192Ssaidi@eecs.umich.edu    std::queue<PhysRegIndex> freeIntRegs;
625192Ssaidi@eecs.umich.edu
635192Ssaidi@eecs.umich.edu    /** The list of free floating point registers. */
645192Ssaidi@eecs.umich.edu    std::queue<PhysRegIndex> freeFloatRegs;
655192Ssaidi@eecs.umich.edu
665192Ssaidi@eecs.umich.edu    /** Number of logical integer registers. */
675192Ssaidi@eecs.umich.edu    int numLogicalIntRegs;
685192Ssaidi@eecs.umich.edu
695192Ssaidi@eecs.umich.edu    /** Number of physical integer registers. */
705192Ssaidi@eecs.umich.edu    int numPhysicalIntRegs;
715192Ssaidi@eecs.umich.edu
725192Ssaidi@eecs.umich.edu    /** Number of logical floating point registers. */
735192Ssaidi@eecs.umich.edu    int numLogicalFloatRegs;
745192Ssaidi@eecs.umich.edu
755192Ssaidi@eecs.umich.edu    /** Number of physical floating point registers. */
76    int numPhysicalFloatRegs;
77
78    /** Total number of physical registers. */
79    int numPhysicalRegs;
80
81  public:
82    /** Constructs a free list.
83     *  @param activeThreads Number of active threads.
84     *  @param _numLogicalIntRegs Number of logical integer registers.
85     *  @param _numPhysicalIntRegs Number of physical integer registers.
86     *  @param _numLogicalFloatRegs Number of logical fp registers.
87     *  @param _numPhysicalFloatRegs Number of physical fp registers.
88     */
89    SimpleFreeList(ThreadID activeThreads,
90                   unsigned _numLogicalIntRegs,
91                   unsigned _numPhysicalIntRegs,
92                   unsigned _numLogicalFloatRegs,
93                   unsigned _numPhysicalFloatRegs);
94
95    /** Gives the name of the freelist. */
96    std::string name() const;
97
98    /** Gets a free integer register. */
99    inline PhysRegIndex getIntReg();
100
101    /** Gets a free fp register. */
102    inline PhysRegIndex getFloatReg();
103
104    /** Adds a register back to the free list. */
105    inline void addReg(PhysRegIndex freed_reg);
106
107    /** Adds an integer register back to the free list. */
108    inline void addIntReg(PhysRegIndex freed_reg);
109
110    /** Adds a fp register back to the free list. */
111    inline void addFloatReg(PhysRegIndex freed_reg);
112
113    /** Checks if there are any free integer registers. */
114    bool hasFreeIntRegs()
115    { return !freeIntRegs.empty(); }
116
117    /** Checks if there are any free fp registers. */
118    bool hasFreeFloatRegs()
119    { return !freeFloatRegs.empty(); }
120
121    /** Returns the number of free integer registers. */
122    int numFreeIntRegs()
123    { return freeIntRegs.size(); }
124
125    /** Returns the number of free fp registers. */
126    int numFreeFloatRegs()
127    { return freeFloatRegs.size(); }
128};
129
130inline PhysRegIndex
131SimpleFreeList::getIntReg()
132{
133    DPRINTF(FreeList, "Trying to get free integer register.\n");
134
135    if (freeIntRegs.empty()) {
136        panic("No free integer registers!");
137    }
138
139    PhysRegIndex free_reg = freeIntRegs.front();
140
141    freeIntRegs.pop();
142
143    return(free_reg);
144}
145
146inline PhysRegIndex
147SimpleFreeList::getFloatReg()
148{
149    DPRINTF(FreeList, "Trying to get free float register.\n");
150
151    if (freeFloatRegs.empty()) {
152        panic("No free integer registers!");
153    }
154
155    PhysRegIndex free_reg = freeFloatRegs.front();
156
157    freeFloatRegs.pop();
158
159    return(free_reg);
160}
161
162inline void
163SimpleFreeList::addReg(PhysRegIndex freed_reg)
164{
165    DPRINTF(FreeList,"Freeing register %i.\n", freed_reg);
166    //Might want to add in a check for whether or not this register is
167    //already in there.  A bit vector or something similar would be useful.
168    if (freed_reg < numPhysicalIntRegs) {
169        if (freed_reg != TheISA::ZeroReg)
170            freeIntRegs.push(freed_reg);
171    } else if (freed_reg < numPhysicalRegs) {
172#if THE_ISA == ALPHA_ISA
173        if (freed_reg != (TheISA::ZeroReg + numPhysicalIntRegs))
174#endif
175            freeFloatRegs.push(freed_reg);
176    }
177
178    // These assert conditions ensure that the number of free
179    // registers are not more than the # of total Physical  Registers.
180    // If this were false, it would mean that registers
181    // have been freed twice, overflowing the free register
182    // pool and potentially crashing SMT workloads.
183    // ----
184    // Comment out for now so as to not potentially break
185    // CMP and single-threaded workloads
186    // ----
187    // assert(freeIntRegs.size() <= numPhysicalIntRegs);
188    // assert(freeFloatRegs.size() <= numPhysicalFloatRegs);
189}
190
191inline void
192SimpleFreeList::addIntReg(PhysRegIndex freed_reg)
193{
194    DPRINTF(FreeList,"Freeing int register %i.\n", freed_reg);
195
196    freeIntRegs.push(freed_reg);
197}
198
199inline void
200SimpleFreeList::addFloatReg(PhysRegIndex freed_reg)
201{
202    DPRINTF(FreeList,"Freeing float register %i.\n", freed_reg);
203
204    freeFloatRegs.push(freed_reg);
205}
206
207#endif // __CPU_O3_FREE_LIST_HH__
208