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() + ".cyclesExecuted")
61 .desc("Number of Cycles Execution Unit was used.");
64 .name(name() + ".utilization")
65 .desc("Utilization of Execution Unit (cycles / totalCycles).");
66 utilization
= cyclesExecuted
/ cpu
->numCycles
;
72 ExecutionUnit::execute(int slot_num
)
74 ResourceRequest
* exec_req
= reqMap
[slot_num
];
75 DynInstPtr inst
= reqMap
[slot_num
]->inst
;
76 Fault fault
= reqMap
[slot_num
]->fault
;
77 ThreadID tid
= inst
->readTid();
78 int seq_num
= inst
->seqNum
;
80 exec_req
->fault
= NoFault
;
82 DPRINTF(InOrderExecute
, "[tid:%i] Executing [sn:%i] [PC:%#x] %s.\n",
83 tid
, seq_num
, inst
->readPC(), inst
->instName());
85 switch (exec_req
->cmd
)
89 if (curTick
!= lastExecuteCycle
) {
90 lastExecuteCycle
= curTick
;
95 if (inst
->isMemRef()) {
96 panic("%s not configured to handle memory ops.\n", resName
);
97 } else if (inst
->isControl()) {
99 fault
= inst
->execute();
103 if (fault
== NoFault
) {
104 // If branch is mispredicted, then signal squash
105 // throughout all stages behind the pipeline stage
106 // that got squashed.
107 if (inst
->mispredicted()) {
108 int stage_num
= exec_req
->getStageNum();
109 ThreadID tid
= inst
->readTid();
111 // If it's a branch ...
112 if (inst
->isDirectCtrl()) {
113 assert(!inst
->isIndirectCtrl());
115 if (inst
->predTaken() && inst
->isCondDelaySlot()) {
116 inst
->bdelaySeqNum
= seq_num
;
117 inst
->setPredTarg(inst
->nextPC
);
119 DPRINTF(InOrderExecute
, "[tid:%i]: Conditional branch inst"
120 "[sn:%i] PC %#x mispredicted as taken.\n", tid
,
122 } else if (!inst
->predTaken() && inst
->isCondDelaySlot()) {
123 inst
->bdelaySeqNum
= seq_num
;
124 inst
->setPredTarg(inst
->nextPC
);
125 inst
->procDelaySlotOnMispred
= true;
127 DPRINTF(InOrderExecute
, "[tid:%i]: Conditional branch inst."
128 "[sn:%i] PC %#x mispredicted as not taken.\n", tid
,
131 #if ISA_HAS_DELAY_SLOT
132 inst
->bdelaySeqNum
= seq_num
+ 1;
133 inst
->setPredTarg(inst
->nextNPC
);
135 inst
->bdelaySeqNum
= seq_num
;
136 inst
->setPredTarg(inst
->nextPC
);
138 DPRINTF(InOrderExecute
, "[tid:%i]: Misprediction detected at "
139 "[sn:%i] PC %#x,\n\t squashing after delay slot "
140 "instruction [sn:%i].\n",
141 tid
, seq_num
, inst
->PC
, inst
->bdelaySeqNum
);
142 DPRINTF(InOrderStall
, "STALL: [tid:%i]: Branch "
143 "misprediction at %#x\n", tid
, inst
->PC
);
146 DPRINTF(InOrderExecute
, "[tid:%i] Redirecting fetch to %#x.\n", tid
,
147 inst
->readPredTarg());
149 } else if(inst
->isIndirectCtrl()){
150 #if ISA_HAS_DELAY_SLOT
151 inst
->setPredTarg(inst
->nextNPC
);
152 inst
->bdelaySeqNum
= seq_num
+ 1;
154 inst
->setPredTarg(inst
->nextPC
);
155 inst
->bdelaySeqNum
= seq_num
;
158 DPRINTF(InOrderExecute
, "[tid:%i] Redirecting fetch to %#x.\n", tid
,
159 inst
->readPredTarg());
161 panic("Non-control instruction (%s) mispredicting?!!",
162 inst
->staticInst
->getName());
165 DPRINTF(InOrderExecute
, "[tid:%i] Squashing will start from stage %i.\n",
168 cpu
->pipelineStage
[stage_num
]->squashDueToBranch(inst
, tid
);
170 inst
->squashingStage
= stage_num
;
172 // Squash throughout other resources
173 cpu
->resPool
->scheduleEvent((InOrderCPU::CPUEventType
)ResourcePool::SquashAll
,
176 if (inst
->predTaken()) {
177 predictedTakenIncorrect
++;
179 predictedNotTakenIncorrect
++;
182 DPRINTF(InOrderExecute
, "[tid:%i]: [sn:%i]: Prediction Correct.\n",
183 inst
->readTid(), seq_num
);
188 warn("inst [sn:%i] had a %s fault", seq_num
, fault
->name());
191 // Regular ALU instruction
192 fault
= inst
->execute();
194 if (fault
== NoFault
) {
197 DPRINTF(InOrderExecute
, "[tid:%i]: [sn:%i]: The result of execution is 0x%x.\n",
198 inst
->readTid(), seq_num
, (inst
->resultType(0) == InOrderDynInst::Float
) ?
199 inst
->readFloatResult(0) : inst
->readIntResult(0));
203 warn("inst [sn:%i] had a %s fault", seq_num
, fault
->name());
204 cpu
->trap(fault
, tid
);
211 fatal("Unrecognized command to %s", resName
);