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
35 #include "cpu/inorder/resources/execution_unit.hh"
36 #include "cpu/inorder/cpu.hh"
37 #include "cpu/inorder/resource_pool.hh"
38 #include "debug/InOrderExecute.hh"
39 #include "debug/InOrderStall.hh"
42 using namespace ThePipeline
;
44 ExecutionUnit::ExecutionUnit(string res_name
, int res_id
, int res_width
,
45 int res_latency
, InOrderCPU
*_cpu
,
46 ThePipeline::Params
*params
)
47 : Resource(res_name
, res_id
, res_width
, res_latency
, _cpu
),
48 lastExecuteTick(0), lastControlTick(0), serializeTick(0)
52 ExecutionUnit::regStats()
54 predictedTakenIncorrect
55 .name(name() + ".predictedTakenIncorrect")
56 .desc("Number of Branches Incorrectly Predicted As Taken.");
58 predictedNotTakenIncorrect
59 .name(name() + ".predictedNotTakenIncorrect")
60 .desc("Number of Branches Incorrectly Predicted As Not Taken).");
63 .name(name() + ".executions")
64 .desc("Number of Instructions Executed.");
68 .name(name() + ".mispredicted")
69 .desc("Number of Branches Incorrectly Predicted");
72 .name(name() + ".predicted")
73 .desc("Number of Branches Incorrectly Predicted");
76 .name(name() + ".mispredictPct")
77 .desc("Percentage of Incorrect Branches Predicts")
79 mispredictPct
= (predictedIncorrect
/
80 (predictedCorrect
+ predictedIncorrect
)) * 100;
86 ExecutionUnit::execute(int slot_num
)
88 ResourceRequest
* exec_req
= reqs
[slot_num
];
89 DynInstPtr inst
= reqs
[slot_num
]->inst
;
90 Fault fault
= NoFault
;
91 InstSeqNum seq_num
= inst
->seqNum
;
92 Tick cur_tick
= curTick();
94 if (cur_tick
== serializeTick
) {
95 DPRINTF(InOrderExecute
, "Can not execute [tid:%i][sn:%i][PC:%s] %s. "
96 "All instructions are being serialized this cycle\n",
97 inst
->readTid(), seq_num
, inst
->pcState(), inst
->instName());
98 exec_req
->done(false);
103 switch (exec_req
->cmd
)
108 DPRINTF(InOrderExecute
, "[tid:%i] [sn:%i] [PC:%s] Ignoring execution"
109 "of %s.\n", inst
->readTid(), seq_num
, inst
->pcState(),
115 DPRINTF(InOrderExecute
, "[tid:%i] Executing [sn:%i] [PC:%s] %s.\n",
116 inst
->readTid(), seq_num
, inst
->pcState(), inst
->instName());
119 if (cur_tick
!= lastExecuteTick
) {
120 lastExecuteTick
= cur_tick
;
123 assert(!inst
->isMemRef());
125 if (inst
->isSerializeAfter()) {
126 serializeTick
= cur_tick
;
127 DPRINTF(InOrderExecute
, "Serializing execution after [tid:%i] "
128 "[sn:%i] [PC:%s] %s.\n", inst
->readTid(), seq_num
,
129 inst
->pcState(), inst
->instName());
132 if (inst
->isControl()) {
133 if (lastControlTick
== cur_tick
) {
134 DPRINTF(InOrderExecute
, "Can not Execute More than One Control "
135 "Inst Per Cycle. Blocking Request.\n");
136 exec_req
->done(false);
139 lastControlTick
= curTick();
142 fault
= inst
->execute();
147 if (fault
== NoFault
) {
148 // If branch is mispredicted, then signal squash
149 // throughout all stages behind the pipeline stage
150 // that got squashed.
151 if (inst
->mispredicted()) {
152 int stage_num
= exec_req
->getStageNum();
153 ThreadID tid
= inst
->readTid();
154 // If it's a branch ...
155 if (inst
->isDirectCtrl()) {
156 assert(!inst
->isIndirectCtrl());
158 TheISA::PCState pc
= inst
->pcState();
159 TheISA::advancePC(pc
, inst
->staticInst
);
160 inst
->setPredTarg(pc
);
162 if (inst
->predTaken() && inst
->isCondDelaySlot()) {
163 inst
->bdelaySeqNum
= seq_num
;
165 DPRINTF(InOrderExecute
, "[tid:%i]: Conditional"
166 " branch inst [sn:%i] PC %s mis"
167 "predicted as taken.\n", tid
,
168 seq_num
, inst
->pcState());
169 } else if (!inst
->predTaken() &&
170 inst
->isCondDelaySlot()) {
171 inst
->bdelaySeqNum
= seq_num
;
172 inst
->procDelaySlotOnMispred
= true;
174 DPRINTF(InOrderExecute
, "[tid:%i]: Conditional"
175 " branch inst [sn:%i] PC %s mis"
176 "predicted as not taken.\n", tid
,
177 seq_num
, inst
->pcState());
179 #if ISA_HAS_DELAY_SLOT
180 inst
->bdelaySeqNum
= seq_num
+ 1;
182 inst
->bdelaySeqNum
= seq_num
;
184 DPRINTF(InOrderExecute
, "[tid:%i]: "
185 "Misprediction detected at "
186 "[sn:%i] PC %s,\n\t squashing after "
187 "delay slot instruction [sn:%i].\n",
188 tid
, seq_num
, inst
->pcState(),
190 DPRINTF(InOrderStall
, "STALL: [tid:%i]: Branch"
191 " misprediction at %s\n",
192 tid
, inst
->pcState());
195 DPRINTF(InOrderExecute
, "[tid:%i] Redirecting "
196 "fetch to %s.\n", tid
,
197 inst
->readPredTarg());
199 } else if (inst
->isIndirectCtrl()){
200 TheISA::PCState pc
= inst
->pcState();
201 TheISA::advancePC(pc
, inst
->staticInst
);
202 inst
->seqNum
= seq_num
;
203 inst
->setPredTarg(pc
);
205 #if ISA_HAS_DELAY_SLOT
206 inst
->bdelaySeqNum
= seq_num
+ 1;
208 inst
->bdelaySeqNum
= seq_num
;
211 DPRINTF(InOrderExecute
, "[tid:%i] Redirecting"
212 " fetch to %s.\n", tid
,
213 inst
->readPredTarg());
215 panic("Non-control instruction (%s) mispredict"
216 "ing?!!", inst
->staticInst
->getName());
219 DPRINTF(InOrderExecute
, "[tid:%i] Squashing will "
220 "start from stage %i.\n", tid
, stage_num
);
222 cpu
->pipelineStage
[stage_num
]->squashDueToBranch(inst
,
225 inst
->squashingStage
= stage_num
;
227 // Squash throughout other resources
228 cpu
->resPool
->scheduleEvent((InOrderCPU::CPUEventType
)
229 ResourcePool::SquashAll
,
232 if (inst
->predTaken()) {
233 predictedTakenIncorrect
++;
234 DPRINTF(InOrderExecute
, "[tid:%i] [sn:%i] %s ..."
235 "PC %s ... Mispredicts! (Taken)\n",
237 inst
->staticInst
->disassemble(
241 predictedNotTakenIncorrect
++;
242 DPRINTF(InOrderExecute
, "[tid:%i] [sn:%i] %s ..."
243 "PC %s ... Mispredicts! (Not Taken)\n",
245 inst
->staticInst
->disassemble(
249 predictedIncorrect
++;
251 DPRINTF(InOrderExecute
, "[tid:%i]: [sn:%i]: Prediction"
252 "Correct.\n", inst
->readTid(), seq_num
);
258 warn("inst [sn:%i] had a %s fault",
259 seq_num
, fault
->name());
262 // Regular ALU instruction
263 fault
= inst
->execute();
266 if (fault
== NoFault
) {
269 DPRINTF(InOrderExecute
, "[tid:%i]: [sn:%i]: The result "
270 "of execution is 0x%x.\n", inst
->readTid(),
272 (inst
->resultType(0) == InOrderDynInst::Float
) ?
273 inst
->readFloatResult(0) : inst
->readIntResult(0));
275 DPRINTF(InOrderExecute
, "[tid:%i]: [sn:%i]: had a %s "
276 "fault.\n", inst
->readTid(), seq_num
, fault
->name());
286 fatal("Unrecognized command to %s", resName
);