Faults: Pass the StaticInst involved, if any, to a Fault's invoke method.
[gem5.git] / src / cpu / inorder / resources / execution_unit.cc
1 /*
2 * Copyright (c) 2007 MIPS Technologies, Inc.
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Korey Sewell
29 *
30 */
31
32 #include <vector>
33 #include <list>
34 #include "cpu/inorder/resources/execution_unit.hh"
35 #include "cpu/inorder/resource_pool.hh"
36 #include "cpu/inorder/cpu.hh"
37
38 using namespace std;
39 using namespace ThePipeline;
40
41 ExecutionUnit::ExecutionUnit(string res_name, int res_id, int res_width,
42 int res_latency, InOrderCPU *_cpu,
43 ThePipeline::Params *params)
44 : Resource(res_name, res_id, res_width, res_latency, _cpu)
45 { }
46
47 void
48 ExecutionUnit::regStats()
49 {
50 predictedTakenIncorrect
51 .name(name() + ".predictedTakenIncorrect")
52 .desc("Number of Branches Incorrectly Predicted As Taken.");
53
54 predictedNotTakenIncorrect
55 .name(name() + ".predictedNotTakenIncorrect")
56 .desc("Number of Branches Incorrectly Predicted As Not Taken).");
57
58 lastExecuteCycle = curTick;
59
60 executions
61 .name(name() + ".executions")
62 .desc("Number of Instructions Executed.");
63
64
65 predictedIncorrect
66 .name(name() + ".mispredicted")
67 .desc("Number of Branches Incorrectly Predicted");
68
69 predictedCorrect
70 .name(name() + ".predicted")
71 .desc("Number of Branches Incorrectly Predicted");
72
73 mispredictPct
74 .name(name() + ".mispredictPct")
75 .desc("Percentage of Incorrect Branches Predicts")
76 .precision(6);
77 mispredictPct = (predictedIncorrect /
78 (predictedCorrect + predictedIncorrect)) * 100;
79
80 Resource::regStats();
81 }
82
83 void
84 ExecutionUnit::execute(int slot_num)
85 {
86 ResourceRequest* exec_req = reqMap[slot_num];
87 DynInstPtr inst = reqMap[slot_num]->inst;
88 Fault fault = reqMap[slot_num]->fault;
89 ThreadID tid = inst->readTid();
90 int seq_num = inst->seqNum;
91
92 exec_req->fault = NoFault;
93
94 DPRINTF(InOrderExecute, "[tid:%i] Executing [sn:%i] [PC:%#x] %s.\n",
95 tid, seq_num, inst->readPC(), inst->instName());
96
97 switch (exec_req->cmd)
98 {
99 case ExecuteInst:
100 {
101 if (curTick != lastExecuteCycle) {
102 lastExecuteCycle = curTick;
103 }
104
105
106 if (inst->isMemRef()) {
107 panic("%s not configured to handle memory ops.\n", resName);
108 } else if (inst->isControl()) {
109 // Evaluate Branch
110 fault = inst->execute();
111 executions++;
112
113 inst->setExecuted();
114
115 if (fault == NoFault) {
116 // If branch is mispredicted, then signal squash
117 // throughout all stages behind the pipeline stage
118 // that got squashed.
119 if (inst->mispredicted()) {
120 int stage_num = exec_req->getStageNum();
121 ThreadID tid = inst->readTid();
122
123 // If it's a branch ...
124 if (inst->isDirectCtrl()) {
125 assert(!inst->isIndirectCtrl());
126
127 if (inst->predTaken() && inst->isCondDelaySlot()) {
128 inst->bdelaySeqNum = seq_num;
129 inst->setPredTarg(inst->nextPC);
130
131 DPRINTF(InOrderExecute, "[tid:%i]: Conditional"
132 " branch inst [sn:%i] PC %#x mis"
133 "predicted as taken.\n", tid,
134 seq_num, inst->PC);
135 } else if (!inst->predTaken() &&
136 inst->isCondDelaySlot()) {
137 inst->bdelaySeqNum = seq_num;
138 inst->setPredTarg(inst->nextPC);
139 inst->procDelaySlotOnMispred = true;
140
141 DPRINTF(InOrderExecute, "[tid:%i]: Conditional"
142 " branch inst [sn:%i] PC %#x mis"
143 "predicted as not taken.\n", tid,
144 seq_num, inst->PC);
145 } else {
146 #if ISA_HAS_DELAY_SLOT
147 inst->bdelaySeqNum = seq_num + 1;
148 inst->setPredTarg(inst->nextNPC);
149 #else
150 inst->bdelaySeqNum = seq_num;
151 inst->setPredTarg(inst->nextPC);
152 #endif
153 DPRINTF(InOrderExecute, "[tid:%i]: "
154 "Misprediction detected at "
155 "[sn:%i] PC %#x,\n\t squashing after "
156 "delay slot instruction [sn:%i].\n",
157 tid, seq_num, inst->PC,
158 inst->bdelaySeqNum);
159 DPRINTF(InOrderStall, "STALL: [tid:%i]: Branch"
160 " misprediction at %#x\n",
161 tid, inst->PC);
162 }
163
164 DPRINTF(InOrderExecute, "[tid:%i] Redirecting "
165 "fetch to %#x.\n", tid,
166 inst->readPredTarg());
167
168 } else if(inst->isIndirectCtrl()){
169 #if ISA_HAS_DELAY_SLOT
170 inst->setPredTarg(inst->nextNPC);
171 inst->bdelaySeqNum = seq_num + 1;
172 #else
173 inst->setPredTarg(inst->nextPC);
174 inst->bdelaySeqNum = seq_num;
175 #endif
176
177 DPRINTF(InOrderExecute, "[tid:%i] Redirecting"
178 " fetch to %#x.\n", tid,
179 inst->readPredTarg());
180 } else {
181 panic("Non-control instruction (%s) mispredict"
182 "ing?!!", inst->staticInst->getName());
183 }
184
185 DPRINTF(InOrderExecute, "[tid:%i] Squashing will "
186 "start from stage %i.\n", tid, stage_num);
187
188 cpu->pipelineStage[stage_num]->squashDueToBranch(inst,
189 tid);
190
191 inst->squashingStage = stage_num;
192
193 // Squash throughout other resources
194 cpu->resPool->scheduleEvent((InOrderCPU::CPUEventType)
195 ResourcePool::SquashAll,
196 inst, 0, 0, tid);
197
198 if (inst->predTaken()) {
199 predictedTakenIncorrect++;
200 DPRINTF(InOrderExecute, "[tid:%i] [sn:%i] %s ... PC%#x ... Mispredicts! (Taken)\n",
201 tid, inst->seqNum, inst->staticInst->disassemble(inst->PC),
202 inst->readPC());
203 } else {
204 predictedNotTakenIncorrect++;
205 DPRINTF(InOrderExecute, "[tid:%i] [sn:%i] %s ... PC%#x ... Mispredicts! (Not Taken)\n",
206 tid, inst->seqNum, inst->staticInst->disassemble(inst->PC),
207 inst->readPC());
208 }
209 predictedIncorrect++;
210 } else {
211 DPRINTF(InOrderExecute, "[tid:%i]: [sn:%i]: Prediction"
212 "Correct.\n", inst->readTid(), seq_num);
213 predictedCorrect++;
214 }
215
216 exec_req->done();
217 } else {
218 warn("inst [sn:%i] had a %s fault",
219 seq_num, fault->name());
220 }
221 } else {
222 // Regular ALU instruction
223 fault = inst->execute();
224 executions++;
225
226 if (fault == NoFault) {
227 inst->setExecuted();
228
229 DPRINTF(InOrderExecute, "[tid:%i]: [sn:%i]: The result "
230 "of execution is 0x%x.\n", inst->readTid(),
231 seq_num,
232 (inst->resultType(0) == InOrderDynInst::Float) ?
233 inst->readFloatResult(0) : inst->readIntResult(0));
234
235 exec_req->done();
236 } else {
237 warn("inst [sn:%i] had a %s fault",
238 seq_num, fault->name());
239 cpu->trap(fault, tid, inst);
240 }
241 }
242 }
243 break;
244
245 default:
246 fatal("Unrecognized command to %s", resName);
247 }
248 }
249
250