2 * Copyright (c) 2007 MIPS Technologies, Inc.
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.
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.
28 * Authors: Korey Sewell
32 #include "config/the_isa.hh"
33 #include "cpu/inorder/resources/branch_predictor.hh"
36 using namespace TheISA
;
37 using namespace ThePipeline
;
39 BranchPredictor::BranchPredictor(std::string res_name
, int res_id
, int res_width
,
40 int res_latency
, InOrderCPU
*_cpu
, ThePipeline::Params
*params
)
41 : Resource(res_name
, res_id
, res_width
, res_latency
, _cpu
),
44 instSize
= sizeof(MachInst
);
48 BranchPredictor::regStats()
51 .name(name() + ".predictedTaken")
52 .desc("Number of Branches Predicted As Taken (True).");
55 .name(name() + ".predictedNotTaken")
56 .desc("Number of Branches Predicted As Not Taken (False).");
62 BranchPredictor::execute(int slot_num
)
64 // After this is working, change this to a reinterpret cast
65 // for performance considerations
66 ResourceRequest
* bpred_req
= reqMap
[slot_num
];
68 DynInstPtr inst
= bpred_req
->inst
;
69 ThreadID tid
= inst
->readTid();
70 int seq_num
= inst
->seqNum
;
71 //int stage_num = bpred_req->getStageNum();
73 bpred_req
->fault
= NoFault
;
75 switch (bpred_req
->cmd
)
79 Addr pred_PC
= inst
->readNextPC();
81 if (inst
->isControl()) {
82 // If not, the pred_PC be updated to pc+8
83 // If predicted, the pred_PC will be updated to new target value
84 bool predict_taken
= branchPred
.predict(inst
, pred_PC
, tid
);
87 DPRINTF(InOrderBPred
, "[tid:%i]: [sn:%i]: Branch predicted true.\n",
90 inst
->setPredTarg(pred_PC
);
94 DPRINTF(InOrderBPred
, "[tid:%i]: [sn:%i]: Branch predicted false.\n",
97 if (inst
->isCondDelaySlot())
99 inst
->setPredTarg(inst
->readPC() + (2 * instSize
));
101 inst
->setPredTarg(pred_PC
);
107 inst
->setBranchPred(predict_taken
);
109 DPRINTF(InOrderBPred
, "[tid:%i]: [sn:%i]: Predicted PC is %08p.\n",
110 tid
, seq_num
, pred_PC
);
113 DPRINTF(InOrderBPred
, "[tid:%i]: Ignoring [sn:%i] because this isn't "
114 "a control instruction.\n", tid
, seq_num
);
121 case UpdatePredictor
:
123 DPRINTF(InOrderBPred
, "[tid:%i]: [sn:%i]: Updating Branch Predictor.\n",
127 branchPred
.update(seq_num
, tid
);
134 fatal("Unrecognized command to %s", resName
);
139 BranchPredictor::squash(DynInstPtr inst
, int squash_stage
,
140 InstSeqNum squash_seq_num
, ThreadID tid
)
142 DPRINTF(InOrderBPred
, "Squashing...\n");
143 branchPred
.squash(squash_seq_num
, tid
);
147 BranchPredictor::instGraduated(InstSeqNum seq_num
, ThreadID tid
)
149 branchPred
.update(seq_num
, tid
);