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 "cpu/inorder/resources/branch_predictor.hh"
35 using namespace TheISA
;
36 using namespace ThePipeline
;
38 BranchPredictor::BranchPredictor(std::string res_name
, int res_id
, int res_width
,
39 int res_latency
, InOrderCPU
*_cpu
, ThePipeline::Params
*params
)
40 : Resource(res_name
, res_id
, res_width
, res_latency
, _cpu
),
43 instSize
= sizeof(MachInst
);
47 BranchPredictor::regStats()
50 .name(name() + ".predictedTaken")
51 .desc("Number of Branches Predicted As Taken (True).");
54 .name(name() + ".predictedNotTaken")
55 .desc("Number of Branches Predicted As Not Taken (False).");
61 BranchPredictor::execute(int slot_num
)
63 // After this is working, change this to a reinterpret cast
64 // for performance considerations
65 ResourceRequest
* bpred_req
= reqMap
[slot_num
];
67 DynInstPtr inst
= bpred_req
->inst
;
68 int tid
= inst
->readTid();
69 int seq_num
= inst
->seqNum
;
70 //int stage_num = bpred_req->getStageNum();
72 bpred_req
->fault
= NoFault
;
74 switch (bpred_req
->cmd
)
78 Addr pred_PC
= inst
->readNextPC();
80 if (inst
->isControl()) {
81 // If predicted, the pred_PC will be updated to new target value
82 // If not, the pred_PC be updated to pc+8
83 bool predict_taken
= branchPred
.predict(inst
, pred_PC
, tid
);
86 DPRINTF(Resource
, "[tid:%i]: [sn:%i]: Branch predicted true.\n",
89 inst
->setPredTarg(pred_PC
);
93 DPRINTF(InOrderBPred
, "[tid:%i]: [sn:%i]: Branch predicted false.\n",
96 if (inst
->isCondDelaySlot())
98 inst
->setPredTarg(inst
->readPC() + (2 * instSize
));
100 inst
->setPredTarg(pred_PC
);
106 inst
->setBranchPred(predict_taken
);
108 DPRINTF(InOrderBPred
, "[tid:%i]: [sn:%i]: Predicted PC is %08p.\n",
109 tid
, seq_num
, pred_PC
);
112 DPRINTF(InOrderBPred
, "[tid:%i]: Ignoring [sn:%i] because this isn't "
113 "a control instruction.\n", tid
, seq_num
);
120 case UpdatePredictor
:
122 DPRINTF(InOrderBPred
, "[tid:%i]: [sn:%i]: Updating Branch Predictor.\n",
126 branchPred
.update(seq_num
, tid
);
133 fatal("Unrecognized command to %s", resName
);
138 BranchPredictor::squash(DynInstPtr inst
, int squash_stage
,
139 InstSeqNum squash_seq_num
, unsigned tid
)
141 DPRINTF(InOrderBPred
, "Squashing...\n");
142 branchPred
.squash(squash_seq_num
, tid
);
146 BranchPredictor::instGraduated(InstSeqNum seq_num
,unsigned tid
)
148 branchPred
.update(seq_num
, tid
);