2 * Copyright (c) 2012 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 #ifndef __CPU_O3_DEP_GRAPH_HH__
42 #define __CPU_O3_DEP_GRAPH_HH__
44 #include "cpu/o3/comm.hh"
46 /** Node in a linked list. */
47 template <class DynInstPtr>
52 : inst(NULL), next(NULL)
56 //Might want to include data about what arch. register the
57 //dependence is waiting on.
58 DependencyEntry<DynInstPtr> *next;
61 /** Array of linked list that maintains the dependencies between
62 * producing instructions and consuming instructions. Each linked
63 * list represents a single physical register, having the future
64 * producer of the register's value, and all consumers waiting on that
65 * value on the list. The head node of each linked list represents
66 * the producing instruction of that register. Instructions are put
67 * on the list upon reaching the IQ, and are removed from the list
68 * either when the producer completes, or the instruction is squashed.
70 template <class DynInstPtr>
74 typedef DependencyEntry<DynInstPtr> DepEntry;
76 /** Default construction. Must call resize() prior to use. */
78 : numEntries(0), memAllocCounter(0), nodesTraversed(0), nodesRemoved(0)
83 /** Resize the dependency graph to have num_entries registers. */
84 void resize(int num_entries);
86 /** Clears all of the linked lists. */
89 /** Inserts an instruction to be dependent on the given index. */
90 void insert(PhysRegIndex idx, const DynInstPtr &new_inst);
92 /** Sets the producing instruction of a given register. */
93 void setInst(PhysRegIndex idx, const DynInstPtr &new_inst)
94 { dependGraph[idx].inst = new_inst; }
96 /** Clears the producing instruction. */
97 void clearInst(PhysRegIndex idx)
98 { dependGraph[idx].inst = NULL; }
100 /** Removes an instruction from a single linked list. */
101 void remove(PhysRegIndex idx, const DynInstPtr &inst_to_remove);
103 /** Removes and returns the newest dependent of a specific register. */
104 DynInstPtr pop(PhysRegIndex idx);
106 /** Checks if the entire dependency graph is empty. */
109 /** Checks if there are any dependents on a specific register. */
110 bool empty(PhysRegIndex idx) const { return !dependGraph[idx].next; }
112 /** Debugging function to dump out the dependency graph.
117 /** Array of linked lists. Each linked list is a list of all the
118 * instructions that depend upon a given register. The actual
119 * register's index is used to index into the graph; ie all
120 * instructions in flight that are dependent upon r34 will be
121 * in the linked list of dependGraph[34].
123 std::vector<DepEntry> dependGraph;
125 /** Number of linked lists; identical to the number of registers. */
128 // Debug variable, remove when done testing.
129 unsigned memAllocCounter;
132 // Debug variable, remove when done testing.
133 uint64_t nodesTraversed;
134 // Debug variable, remove when done testing.
135 uint64_t nodesRemoved;
138 template <class DynInstPtr>
139 DependencyGraph<DynInstPtr>::~DependencyGraph()
143 template <class DynInstPtr>
145 DependencyGraph<DynInstPtr>::resize(int num_entries)
147 numEntries = num_entries;
148 dependGraph.resize(numEntries);
151 template <class DynInstPtr>
153 DependencyGraph<DynInstPtr>::reset()
155 // Clear the dependency graph
159 for (int i = 0; i < numEntries; ++i) {
160 curr = dependGraph[i].next;
172 if (dependGraph[i].inst) {
173 dependGraph[i].inst = NULL;
176 dependGraph[i].next = NULL;
180 template <class DynInstPtr>
182 DependencyGraph<DynInstPtr>::insert(PhysRegIndex idx,
183 const DynInstPtr &new_inst)
185 //Add this new, dependent instruction at the head of the dependency
188 // First create the entry that will be added to the head of the
190 DepEntry *new_entry = new DepEntry;
191 new_entry->next = dependGraph[idx].next;
192 new_entry->inst = new_inst;
194 // Then actually add it to the chain.
195 dependGraph[idx].next = new_entry;
201 template <class DynInstPtr>
203 DependencyGraph<DynInstPtr>::remove(PhysRegIndex idx,
204 const DynInstPtr &inst_to_remove)
206 DepEntry *prev = &dependGraph[idx];
207 DepEntry *curr = dependGraph[idx].next;
209 // Make sure curr isn't NULL. Because this instruction is being
210 // removed from a dependency list, it must have been placed there at
211 // an earlier time. The dependency chain should not be empty,
212 // unless the instruction dependent upon it is already ready.
219 // Find the instruction to remove within the dependency linked list.
220 while (curr->inst != inst_to_remove) {
225 assert(curr != NULL);
228 // Now remove this instruction from the list.
229 prev->next = curr->next;
233 // Could push this off to the destructor of DependencyEntry
239 template <class DynInstPtr>
241 DependencyGraph<DynInstPtr>::pop(PhysRegIndex idx)
244 node = dependGraph[idx].next;
245 DynInstPtr inst = NULL;
248 dependGraph[idx].next = node->next;
256 template <class DynInstPtr>
258 DependencyGraph<DynInstPtr>::empty() const
260 for (int i = 0; i < numEntries; ++i) {
267 template <class DynInstPtr>
269 DependencyGraph<DynInstPtr>::dump()
273 for (int i = 0; i < numEntries; ++i)
275 curr = &dependGraph[i];
278 cprintf("dependGraph[%i]: producer: %s [sn:%lli] consumer: ",
279 i, curr->inst->pcState(), curr->inst->seqNum);
281 cprintf("dependGraph[%i]: No producer. consumer: ", i);
284 while (curr->next != NULL) {
287 cprintf("%s [sn:%lli] ",
288 curr->inst->pcState(), curr->inst->seqNum);
293 cprintf("memAllocCounter: %i\n", memAllocCounter);
296 #endif // __CPU_O3_DEP_GRAPH_HH__