gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
bac.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022-2023 The University of Edinburgh
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifndef __CPU_O3_BAC_HH__
39#define __CPU_O3_BAC_HH__
40
41#include <list>
42
43#include "base/statistics.hh"
44#include "cpu/o3/comm.hh"
46#include "cpu/o3/limits.hh"
49#include "cpu/timebuf.hh"
50
51namespace gem5
52{
53
54struct BaseO3CPUParams;
55
56namespace o3
57{
58
59class CPU;
60class FTQ;
61class FetchTarget;
62typedef std::shared_ptr<FetchTarget> FetchTargetPtr;
63
64/********************************************************************
65 * BAC: Branch and Address Calculation
66 *
67 * The BAC stage handles branch prediction and next PC address
68 * calculation and mainly consists of two parts:
69 *
70 * (1) The branch prediction part that manages the branch predictor.
71 * It checks the backward communication signals from the pipleline
72 * to update the branch predictor accordingly.
73 * This part can be decoupled from the fetch stage and used to
74 * feed the FTQ with fetch targets which will be consumed by the
75 * fetch stage.
76 * When decoupling is enabled this part is active and acts as
77 * a separate pipeline stage. Otherwise the stage remains idle and
78 * everything is in sync with the fetch stage. The branch prediction
79 * itself is then triggered from the PC calculation part.
80 *
81 * (2) The PC calculation part that is tightly coupled to the
82 * fetch stage. Fetch calls the updatePC() method after each
83 * predecoded instruction to advance the PC to the next instruction.
84 *
85 * Note that this organization means that the BAC stage spans
86 * two pipeline stages and the second part is in the same stage as
87 * the fetch stage which is not the nicest organization. However,
88 * it allows to keep the fetch stage simple and most of the decoupling
89 * in the BAC stage.
90 */
91class BAC
92{
94
95 public:
100 {
103 };
104
116
117 private:
120
123
124 public:
126 BAC(CPU *_cpu, const BaseO3CPUParams &params);
127
128 // Interfaces to CPU ----------------------------------
129 // These functions are to manage the BAC stage
130 // and the interface with the remaining CPU.
131
133 std::string name() const;
134
136 void
139
142
145
147 void setFetchTargetQueue(FTQ *_ptr);
148
150 void startupStage();
151
153 void clearStates(ThreadID tid);
154
156 void drainResume();
157
159 void drainSanityCheck() const;
160
162 bool isDrained() const;
163
173 void drainStall(ThreadID tid);
174
176 void
178 {
179 resetStage();
180 }
181
183
185 void tick();
186
187 private:
189 void resetStage();
190
194 void switchToActive();
195
199 void switchToInactive();
200
202 bool checkStall(ThreadID tid) const;
203
206 void updateBACStatus();
207
212
215
216 private:
217 /* ----------------------------------------------------------------
218 * Decoupled Frontend Functionality
219 *
220 * In a decoupled frontend the Branch predictor unit (BPU)
221 * is not directly queried by Fetch once it pre-decodes
222 * a branch. Instead BPU and Fetch is separated and
223 * connected via a queue fetch target queue (FTQ). The BPU generates
224 * fetch targets, similar to basic blocks (BB) and inserts them in
225 * the queue. Fetch will consume the addresses and read them from the
226 * I-cache.
227 * The advantages are that it (1) cuts the critical path and (2)
228 * allow a precise, BPU guided prefetching of the fetch targets.
229 *
230 * For determining next PC addresses the BPU relies solely on the BTB.
231 *
232 * The following functions are only used in the decoupled scenario.
233 */
234
241
252 bool predict(ThreadID tid, const StaticInstPtr &inst,
253 const FetchTargetPtr &ft, PCStateBase &pc);
254
264 void generateFetchTargets(ThreadID tid, bool &status_change);
265
266 /* ----------------------------------------------------------------
267 * Next PC address calculation
268 *
269 * To update the PC the fetch stage will call the updatePC() method
270 * with the currently pre-decoded instruction.
271 * The BAC stage will then check if the instruction is a branch and
272 * either perform the branch prediction (non-decoupled scenario) or
273 * read the branch prediction from the currently processed fetch
274 * target (decoupled scenario).
275 */
276 public:
287 bool updatePC(const DynInstPtr &inst, PCStateBase &fetch_pc,
288 FetchTargetPtr &ft);
289
290 private:
321 bool updatePreDecode(ThreadID tid, const InstSeqNum seqNum,
322 const StaticInstPtr &inst, PCStateBase &pc,
323 const FetchTargetPtr &ft);
324
325 private:
327 void squash(const PCStateBase &new_pc, ThreadID tid);
328
334
335 private:
338
341
344
347
350
353
356
359
361 std::unique_ptr<PCStateBase> bacPC[MaxThreads];
362
367
369 struct Stalls
370 {
371 bool fetch;
372 bool drain;
373 bool bpu;
374 };
375
378
381
384
387
390
393
395 const unsigned int cacheBlkSize;
396
399 const unsigned fetchTargetWidth;
400
405 const unsigned minInstSize;
406
409
412
413 /* Max number of FT added to the FTQ per Cycle */
414 const unsigned maxFTPerCycle;
415
416 /* Max number taken prediction by the BPU per Cycle*/
417 const unsigned maxTakenPredPerCycle;
418
420 inline Addr
422 {
423 return (addr & ~(cacheBlkSize - 1));
424 }
425
426 protected:
468};
469
470} // namespace o3
471} // namespace gem5
472
473#endif // __CPU_O3_BAC_HH__
Cycles is a wrapper class for representing cycle counts, i.e.
Definition types.hh:79
Basically a wrapper class to hold both the branch predictor and the BTB.
Definition bpred_unit.hh:72
const Cycles decodeToFetchDelay
Decode to fetch delay.
Definition bac.hh:386
bool isDrained() const
Has the stage drained?
Definition bac.cc:196
CPU * cpu
Pointer to the main CPU.
Definition bac.hh:337
void drainResume()
Resume after a drain.
Definition bac.cc:174
TimeBuffer< TimeStruct >::wire fromFetch
Wire to get fetches's information from backwards time buffer.
Definition bac.hh:349
std::list< ThreadID > * activeThreads
List of Active FTQ Threads.
Definition bac.hh:408
Stalls stalls[MaxThreads]
Tracks which stages are telling the ftq to stall.
Definition bac.hh:377
bool wroteToTimeBuffer
Variable that tracks if BAC has written to the time buffer this cycle.
Definition bac.hh:366
bool checkStall(ThreadID tid) const
Checks if a thread is stalled.
Definition bac.cc:243
const unsigned fetchTargetWidth
The maximum width of a fetch target.
Definition bac.hh:399
const unsigned minInstSize
The minimum size an instruction can have in the current architecture.
Definition bac.hh:405
void deactivateThread(ThreadID tid)
Definition bac.hh:182
void startupStage()
Initialize stage.
Definition bac.cc:139
void generateFetchTargets(ThreadID tid, bool &status_change)
Main function that feeds the FTQ with new fetch targets.
Definition bac.cc:585
ThreadStatus
Individual thread status.
Definition bac.hh:107
@ ThreadStatusMax
Definition bac.hh:114
const Cycles fetchToBacDelay
Fetch to BAC delay.
Definition bac.hh:383
const unsigned int cacheBlkSize
Cache block size.
Definition bac.hh:395
void setFetchTargetQueue(FTQ *_ptr)
Connect the FTQ.
Definition bac.cc:132
const Cycles bacToFetchDelay
BAC to fetch delay.
Definition bac.hh:392
void takeOverFrom()
Takes over from another CPU's thread.
Definition bac.hh:177
void updateBACStatus()
Updates overall BAC stage status; to be called at the end of each cycle.
Definition bac.cc:261
const unsigned maxTakenPredPerCycle
Definition bac.hh:417
ThreadStatus bacStatus[MaxThreads]
Per-thread status.
Definition bac.hh:122
const unsigned maxFTPerCycle
Definition bac.hh:414
void setActiveThreads(std::list< ThreadID > *at_ptr)
Sets pointer to list of active threads.
Definition bac.cc:126
const ThreadID numThreads
Number of threads.
Definition bac.hh:411
BACStatus _status
Decode status.
Definition bac.hh:119
bool checkSignalsAndUpdate(ThreadID tid)
Checks all input signals and updates the status as necessary.
Definition bac.cc:375
bool predict(ThreadID tid, const StaticInstPtr &inst, const FetchTargetPtr &ft, PCStateBase &pc)
The prediction function for the BAC stage.
Definition bac.cc:565
bool updatePreDecode(ThreadID tid, const InstSeqNum seqNum, const StaticInstPtr &inst, PCStateBase &pc, const FetchTargetPtr &ft)
Pre-decode update --------------------------------------— After predecoding instruction in the fetch ...
Definition bac.cc:764
void regProbePoints()
Registers probes and listeners.
Definition bac.hh:137
BAC(CPU *_cpu, const BaseO3CPUParams &params)
BAC constructor.
Definition bac.cc:77
void drainStall(ThreadID tid)
Stall the fetch stage after reaching a safe drain point.
Definition bac.cc:214
void drainSanityCheck() const
Perform sanity checks after a drain.
Definition bac.cc:183
void resetStage()
Reset this pipeline stage.
Definition bac.cc:162
bool updatePC(const DynInstPtr &inst, PCStateBase &fetch_pc, FetchTargetPtr &ft)
Calculate the next PC address depending on the instruction type and the branch prediction.
Definition bac.cc:904
Addr alignToCacheBlock(Addr addr)
Align a address to the start of a cache block.
Definition bac.hh:421
const Cycles commitToFetchDelay
Commit to fetch delay.
Definition bac.hh:389
const bool decoupledFrontEnd
Enables the decoupled front-end.
Definition bac.hh:380
FetchTargetPtr newFetchTarget(ThreadID tid, const PCStateBase &start_pc)
Create a new fetch target.
Definition bac.cc:554
gem5::o3::BAC::BACStats stats
void switchToInactive()
Changes the status of this stage to inactive, and indicates this to the CPU.
Definition bac.cc:233
bool checkAndUpdateBPUSignals(ThreadID tid)
Check the backward signals that update the BPU.
Definition bac.cc:290
branch_prediction::BranchType BranchType
Definition bac.hh:93
void setTimeBuffer(TimeBuffer< TimeStruct > *tb_ptr)
Sets the main backwards communication time buffer pointer.
Definition bac.cc:115
void clearStates(ThreadID tid)
Clear all thread-specific states.
Definition bac.cc:148
std::string name() const
Returns the name of the stage.
Definition bac.cc:109
void switchToActive()
Changes the status of this stage to active, and indicates this to the CPU.
Definition bac.cc:223
TimeBuffer< FetchStruct >::wire toFetch
Wire used to write any information heading to fetch.
Definition bac.hh:358
void squashBpuHistories(ThreadID tid)
Squashes the BPU histories in the FTQ.
Definition bac.cc:464
void tick()
Process all input signals and create the next fetch target.
Definition bac.cc:503
std::unique_ptr< PCStateBase > bacPC[MaxThreads]
The decoupled PC which runs ahead of fetch.
Definition bac.hh:361
TimeBuffer< TimeStruct >::wire fromDecode
Wire to get decode's information from backwards time buffer.
Definition bac.hh:352
void squash(const PCStateBase &new_pc, ThreadID tid)
Squashes BAC for a specific thread and resets the PC.
Definition bac.cc:484
branch_prediction::BPredUnit * bpu
BPredUnit.
Definition bac.hh:340
BACStatus
Overall decoupled BPU stage status.
Definition bac.hh:100
FTQ * ftq
Fetch target Queue.
Definition bac.hh:343
TimeBuffer< TimeStruct > * timeBuffer
Time buffer interface.
Definition bac.hh:346
TimeBuffer< TimeStruct >::wire fromCommit
Wire to get commit's information from backwards time buffer.
Definition bac.hh:355
O3CPU class, has each of the stages (fetch through commit) within it, as well as all of the time buff...
Definition cpu.hh:97
FTQ class.
Definition ftq.hh:227
The fetch target class.
Definition ftq.hh:83
A simple distribution stat.
Statistics container.
Definition group.hh:93
This is a simple scalar statistic, like a counter.
A vector of scalar stats.
STL list class.
Definition stl.hh:51
Bitfield< 4 > pc
Bitfield< 3 > addr
Definition types.hh:84
enums::BranchType BranchType
std::shared_ptr< FetchTarget > FetchTargetPtr
Definition bac.hh:62
static constexpr int MaxThreads
Definition limits.hh:38
RefCountingPtr< DynInst > DynInstPtr
Copyright (c) 2024 Arm Limited All rights reserved.
Definition binary32.hh:36
int16_t ThreadID
Thread index/ID type.
Definition types.hh:235
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
RefCountingPtr< StaticInst > StaticInstPtr
uint64_t InstSeqNum
Definition inst_seq.hh:40
Declaration of Statistics objects.
statistics::Vector preDecUpdate
Number of post updates.
Definition bac.hh:454
BACStats(CPU *cpu, BAC *bac)
Definition bac.cc:973
statistics::Scalar branches
Total number of branches detected.
Definition bac.hh:439
statistics::Scalar predTakenBranches
Total number of branches predicted taken.
Definition bac.hh:441
statistics::Scalar branchesNotLastuOp
Total number of fetched branches.
Definition bac.hh:443
statistics::Scalar multiBranchInst
Definition bac.hh:460
statistics::Scalar typeMissmatch
Stat for the two corner cases.
Definition bac.hh:459
statistics::Scalar squashBranchCommit
Definition bac.hh:451
statistics::Vector noHistByType
Number of branches undetected by the BPU.
Definition bac.hh:456
statistics::Scalar squashBranchDecode
Stat for the number of squashes from decode and commit.
Definition bac.hh:450
statistics::Scalar noBranchMisspredict
Definition bac.hh:447
statistics::Scalar fetchTargets
Stat for total number fetch targets created.
Definition bac.hh:437
static std::string statusStrings[ThreadStatusMax]
Definition bac.hh:429
statistics::Distribution ftSizeDist
Distribution of number of bytes per fetch target.
Definition bac.hh:463
statistics::Distribution ftNumber
Definition bac.hh:464
statistics::Scalar branchMisspredict
Stat for total number of misspredicted instructions.
Definition bac.hh:446
statistics::Vector status
Stat for total number of cycles spent in each BAC state.
Definition bac.hh:434
Source of possible stalls.
Definition bac.hh:370

Generated on Mon Oct 27 2025 04:13:00 for gem5 by doxygen 1.14.0