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
34 #include "cpu/inorder/resources/execution_unit.hh"
35 #include "cpu/inorder/resource_pool.hh"
36 #include "cpu/inorder/cpu.hh"
39 using namespace ThePipeline
;
41 ExecutionUnit::ExecutionUnit(string res_name
, int res_id
, int res_width
,
42 int res_latency
, InOrderCPU
*_cpu
, ThePipeline::Params
*params
)
43 : Resource(res_name
, res_id
, res_width
, res_latency
, _cpu
)
47 ExecutionUnit::regStats()
49 predictedTakenIncorrect
50 .name(name() + ".predictedTakenIncorrect")
51 .desc("Number of Branches Incorrectly Predicted As Taken.");
53 predictedNotTakenIncorrect
54 .name(name() + ".predictedNotTakenIncorrect")
55 .desc("Number of Branches Incorrectly Predicted As Not Taken).");
57 lastExecuteCycle
= curTick
;
60 .name(name() + ".executions")
61 .desc("Number of Instructions Executed.");
65 .name(name() + ".mispredicted")
66 .desc("Number of Branches Incorrectly Predicted");
69 .name(name() + ".predicted")
70 .desc("Number of Branches Incorrectly Predicted");
73 .name(name() + ".mispredictPct")
74 .desc("Percentage of Incorrect Branches Predicts")
76 mispredictPct
= (predictedIncorrect
/
77 (predictedCorrect
+ predictedIncorrect
)) * 100;
83 ExecutionUnit::execute(int slot_num
)
85 ResourceRequest
* exec_req
= reqMap
[slot_num
];
86 DynInstPtr inst
= reqMap
[slot_num
]->inst
;
87 Fault fault
= reqMap
[slot_num
]->fault
;
88 ThreadID tid
= inst
->readTid();
89 int seq_num
= inst
->seqNum
;
91 exec_req
->fault
= NoFault
;
93 DPRINTF(InOrderExecute
, "[tid:%i] Executing [sn:%i] [PC:%#x] %s.\n",
94 tid
, seq_num
, inst
->readPC(), inst
->instName());
96 switch (exec_req
->cmd
)
100 if (curTick
!= lastExecuteCycle
) {
101 lastExecuteCycle
= curTick
;
105 if (inst
->isMemRef()) {
106 panic("%s not configured to handle memory ops.\n", resName
);
107 } else if (inst
->isControl()) {
109 fault
= inst
->execute();
114 if (fault
== NoFault
) {
115 // If branch is mispredicted, then signal squash
116 // throughout all stages behind the pipeline stage
117 // that got squashed.
118 if (inst
->mispredicted()) {
119 int stage_num
= exec_req
->getStageNum();
120 ThreadID tid
= inst
->readTid();
122 // If it's a branch ...
123 if (inst
->isDirectCtrl()) {
124 assert(!inst
->isIndirectCtrl());
126 if (inst
->predTaken() && inst
->isCondDelaySlot()) {
127 inst
->bdelaySeqNum
= seq_num
;
128 inst
->setPredTarg(inst
->nextPC
);
130 DPRINTF(InOrderExecute
, "[tid:%i]: Conditional branch inst"
131 "[sn:%i] PC %#x mispredicted as taken.\n", tid
,
133 } else if (!inst
->predTaken() && inst
->isCondDelaySlot()) {
134 inst
->bdelaySeqNum
= seq_num
;
135 inst
->setPredTarg(inst
->nextPC
);
136 inst
->procDelaySlotOnMispred
= true;
138 DPRINTF(InOrderExecute
, "[tid:%i]: Conditional branch inst."
139 "[sn:%i] PC %#x mispredicted as not taken.\n", tid
,
142 #if ISA_HAS_DELAY_SLOT
143 inst
->bdelaySeqNum
= seq_num
+ 1;
144 inst
->setPredTarg(inst
->nextNPC
);
146 inst
->bdelaySeqNum
= seq_num
;
147 inst
->setPredTarg(inst
->nextPC
);
149 DPRINTF(InOrderExecute
, "[tid:%i]: Misprediction detected at "
150 "[sn:%i] PC %#x,\n\t squashing after delay slot "
151 "instruction [sn:%i].\n",
152 tid
, seq_num
, inst
->PC
, inst
->bdelaySeqNum
);
153 DPRINTF(InOrderStall
, "STALL: [tid:%i]: Branch "
154 "misprediction at %#x\n", tid
, inst
->PC
);
157 DPRINTF(InOrderExecute
, "[tid:%i] Redirecting fetch to %#x.\n", tid
,
158 inst
->readPredTarg());
160 } else if(inst
->isIndirectCtrl()){
161 #if ISA_HAS_DELAY_SLOT
162 inst
->setPredTarg(inst
->nextNPC
);
163 inst
->bdelaySeqNum
= seq_num
+ 1;
165 inst
->setPredTarg(inst
->nextPC
);
166 inst
->bdelaySeqNum
= seq_num
;
169 DPRINTF(InOrderExecute
, "[tid:%i] Redirecting fetch to %#x.\n", tid
,
170 inst
->readPredTarg());
172 panic("Non-control instruction (%s) mispredicting?!!",
173 inst
->staticInst
->getName());
176 DPRINTF(InOrderExecute
, "[tid:%i] Squashing will start from stage %i.\n",
179 cpu
->pipelineStage
[stage_num
]->squashDueToBranch(inst
, tid
);
181 inst
->squashingStage
= stage_num
;
183 // Squash throughout other resources
184 cpu
->resPool
->scheduleEvent((InOrderCPU::CPUEventType
)ResourcePool::SquashAll
,
187 if (inst
->predTaken()) {
188 predictedTakenIncorrect
++;
189 DPRINTF(InOrderExecute
, "[tid:%i] [sn:%i] %s ... PC%#x ... Mispredicts! (Taken)\n",
190 tid
, inst
->seqNum
, inst
->staticInst
->disassemble(inst
->PC
),
193 predictedNotTakenIncorrect
++;
194 DPRINTF(InOrderExecute
, "[tid:%i] [sn:%i] %s ... PC%#x ... Mispredicts! (Not Taken)\n",
195 tid
, inst
->seqNum
, inst
->staticInst
->disassemble(inst
->PC
),
199 DPRINTF(InOrderExecute
, "[tid:%i]: [sn:%i]: Prediction Correct.\n",
200 inst
->readTid(), seq_num
);
205 warn("inst [sn:%i] had a %s fault", seq_num
, fault
->name());
208 // Regular ALU instruction
209 fault
= inst
->execute();
212 if (fault
== NoFault
) {
215 DPRINTF(InOrderExecute
, "[tid:%i]: [sn:%i]: The result of execution is 0x%x.\n",
216 inst
->readTid(), seq_num
, (inst
->resultType(0) == InOrderDynInst::Float
) ?
217 inst
->readFloatResult(0) : inst
->readIntResult(0));
221 warn("inst [sn:%i] had a %s fault", seq_num
, fault
->name());
222 cpu
->trap(fault
, tid
);
229 fatal("Unrecognized command to %s", resName
);