blob: 852b9c8e32dfd46101db518fb023772153bc4533 [file] [log] [blame]
Chris Lattnerc6644182006-03-07 06:32:48 +00001//===-- PPCHazardRecognizers.cpp - PowerPC Hazard Recognizer Impls --------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements hazard recognizers for scheduling on PowerPC processors.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "sched"
15#include "PPCHazardRecognizers.h"
16#include "PPC.h"
17#include "llvm/Support/Debug.h"
18#include <iostream>
19using namespace llvm;
20
21
22//===----------------------------------------------------------------------===//
23// PowerPC 970 Hazard Recognizer
24//
Chris Lattner7ce64852006-03-07 06:44:19 +000025// This models the dispatch group formation of the PPC970 processor. Dispatch
26// groups are bundles of up to five instructions that can contain up to two ALU
27// (aka FXU) ops, two FPU ops, two Load/Store ops, one CR op, one VALU op, one
28// VPERM op, and one BRANCH op. If the code contains more instructions in a
29// sequence than the dispatch group can contain (e.g. three loads in a row) the
30// processor terminates the dispatch group early, wasting execution resources.
31//
32// In addition to these restrictions, there are a number of other restrictions:
33// some instructions, e.g. branches, are required to be the last instruction in
34// a group. Additionally, only branches can issue in the 5th (last) slot.
35//
36// Finally, there are a number of "structural" hazards on the PPC970. These
37// conditions cause large performance penalties due to misprediction, recovery,
38// and replay logic that has to happen. These cases include setting a CTR and
39// branching through it in the same dispatch group, and storing to an address,
40// then loading from the same address within a dispatch group. To avoid these
41// conditions, we insert no-op instructions when appropriate.
42//
Chris Lattnerc6644182006-03-07 06:32:48 +000043// FIXME: This is missing some significant cases:
Chris Lattner20463712006-03-07 07:14:55 +000044// -1. Handle all of the instruction types in GetInstrType.
Chris Lattnerc6644182006-03-07 06:32:48 +000045// 0. Handling of instructions that must be the first/last in a group.
46// 1. Modeling of microcoded instructions.
47// 2. Handling of cracked instructions.
48// 3. Handling of serialized operations.
49// 4. Handling of the esoteric cases in "Resource-based Instruction Grouping",
50// e.g. integer divides that only execute in the second slot.
51//
Chris Lattnerc6644182006-03-07 06:32:48 +000052
53void PPCHazardRecognizer970::EndDispatchGroup() {
54 DEBUG(std::cerr << "=== Start of dispatch group\n");
55 // Pipeline units.
56 NumFXU = NumLSU = NumFPU = 0;
Nate Begeman3acbe5d2006-03-07 08:30:27 +000057 HasCR = HasSPR = HasVALU = HasVPERM = false;
Chris Lattnerc6644182006-03-07 06:32:48 +000058 NumIssued = 0;
59
60 // Structural hazard info.
61 HasCTRSet = false;
62 StorePtr1 = StorePtr2 = SDOperand();
63 StoreSize = 0;
64}
65
66
67PPCHazardRecognizer970::PPC970InstrType
68PPCHazardRecognizer970::GetInstrType(unsigned Opcode) {
69 if (Opcode < ISD::BUILTIN_OP_END)
70 return PseudoInst;
71 Opcode -= ISD::BUILTIN_OP_END;
72
73 switch (Opcode) {
74 case PPC::FMRSD: return PseudoInst; // Usually coallesced away.
75 case PPC::BCTRL:
76 case PPC::BL:
77 case PPC::BLA:
78 return BR;
Nate Begeman3acbe5d2006-03-07 08:30:27 +000079 case PPC::MCRF:
80 case PPC::MFCR:
81 case PPC::MFOCRF:
82 return CR;
83 case PPC::MFLR:
84 case PPC::MFCTR:
85 case PPC::MTLR:
86 case PPC::MTCTR:
87 return SPR;
Chris Lattnerc6644182006-03-07 06:32:48 +000088 case PPC::LFS:
Chris Lattner20463712006-03-07 07:14:55 +000089 case PPC::LFD:
Chris Lattnerc6644182006-03-07 06:32:48 +000090 case PPC::LWZ:
Chris Lattner20463712006-03-07 07:14:55 +000091 case PPC::LFSX:
92 case PPC::LWZX:
Chris Lattnerab5801c2006-03-07 16:19:46 +000093 case PPC::LBZ:
94 case PPC::LHA:
95 case PPC::LHZ:
96 case PPC::LWZU:
Chris Lattnerc6644182006-03-07 06:32:48 +000097 return LSU_LD;
Chris Lattnerb84225b2006-03-07 16:26:48 +000098 case PPC::STFS:
Chris Lattnerc6644182006-03-07 06:32:48 +000099 case PPC::STFD:
Chris Lattner20463712006-03-07 07:14:55 +0000100 case PPC::STW:
Chris Lattnerab5801c2006-03-07 16:19:46 +0000101 case PPC::STB:
102 case PPC::STH:
103 case PPC::STWU:
Chris Lattnerc6644182006-03-07 06:32:48 +0000104 return LSU_ST;
Nate Begeman3acbe5d2006-03-07 08:30:27 +0000105 case PPC::DIVW:
106 case PPC::DIVWU:
107 case PPC::DIVD:
108 case PPC::DIVDU:
109 return FXU_FIRST;
Chris Lattnerc6644182006-03-07 06:32:48 +0000110 case PPC::FADDS:
111 case PPC::FCTIWZ:
Chris Lattner20463712006-03-07 07:14:55 +0000112 case PPC::FRSP:
113 case PPC::FSUB:
Chris Lattnerc6644182006-03-07 06:32:48 +0000114 return FPU;
115 }
116
117 return FXU;
118}
119
120
121/// StartBasicBlock - Initiate a new dispatch group.
122void PPCHazardRecognizer970::StartBasicBlock() {
123 EndDispatchGroup();
124}
125
126/// isLoadOfStoredAddress - If we have a load from the previously stored pointer
127/// as indicated by StorePtr1/StorePtr2/StoreSize, return true.
128bool PPCHazardRecognizer970::
129isLoadOfStoredAddress(unsigned LoadSize, SDOperand Ptr1, SDOperand Ptr2) const {
130 // Handle exact and commuted addresses.
131 if (Ptr1 == StorePtr1 && Ptr2 == StorePtr2)
132 return true;
133 if (Ptr2 == StorePtr1 && Ptr1 == StorePtr2)
134 return true;
135
136 // Okay, we don't have an exact match, if this is an indexed offset, see if we
137 // have overlap (which happens during fp->int conversion for example).
138 if (StorePtr2 == Ptr2) {
139 if (ConstantSDNode *StoreOffset = dyn_cast<ConstantSDNode>(StorePtr1))
140 if (ConstantSDNode *LoadOffset = dyn_cast<ConstantSDNode>(Ptr1)) {
141 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check to
142 // see if the load and store actually overlap.
143 int StoreOffs = StoreOffset->getValue();
144 int LoadOffs = LoadOffset->getValue();
145 if (StoreOffs < LoadOffs) {
146 if (int(StoreOffs+StoreSize) > LoadOffs) return true;
147 } else {
148 if (int(LoadOffs+LoadSize) > StoreOffs) return true;
149 }
150 }
151 }
152 return false;
153}
154
155/// getHazardType - We return hazard for any non-branch instruction that would
156/// terminate terminate the dispatch group. We turn NoopHazard for any
157/// instructions that wouldn't terminate the dispatch group that would cause a
158/// pipeline flush.
159HazardRecognizer::HazardType PPCHazardRecognizer970::
160getHazardType(SDNode *Node) {
161 PPC970InstrType InstrType = GetInstrType(Node->getOpcode());
162 if (InstrType == PseudoInst) return NoHazard;
163 unsigned Opcode = Node->getOpcode()-ISD::BUILTIN_OP_END;
164
165 switch (InstrType) {
166 default: assert(0 && "Unknown instruction type!");
Nate Begeman3acbe5d2006-03-07 08:30:27 +0000167 case FXU:
168 case FXU_FIRST: if (NumFXU == 2) return Hazard;
Chris Lattnerc6644182006-03-07 06:32:48 +0000169 case LSU_ST:
Nate Begeman3acbe5d2006-03-07 08:30:27 +0000170 case LSU_LD: if (NumLSU == 2) return Hazard;
171 case FPU: if (NumFPU == 2) return Hazard;
172 case CR: if (HasCR) return Hazard;
173 case SPR: if (HasSPR) return Hazard;
174 case VALU: if (HasVALU) return Hazard;
175 case VPERM: if (HasVPERM) return Hazard;
176 case BR: break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000177 }
Nate Begeman3acbe5d2006-03-07 08:30:27 +0000178
179 // We can only issue a CR or SPR instruction, or an FXU instruction that needs
180 // to lead a dispatch group as the first instruction in the group.
181 if (NumIssued != 0 &&
182 (InstrType == CR || InstrType == SPR || InstrType == FXU_FIRST))
183 return Hazard;
184
Chris Lattnerc6644182006-03-07 06:32:48 +0000185 // We can only issue a branch as the last instruction in a group.
186 if (NumIssued == 4 && InstrType != BR)
187 return Hazard;
188
189 // Do not allow MTCTR and BCTRL to be in the same dispatch group.
190 if (HasCTRSet && Opcode == PPC::BCTRL)
191 return NoopHazard;
192
193 // If this is a load following a store, make sure it's not to the same or
194 // overlapping address.
195 if (InstrType == LSU_LD && StoreSize) {
196 unsigned LoadSize;
197 switch (Opcode) {
198 default: assert(0 && "Unknown load!");
Chris Lattnerab5801c2006-03-07 16:19:46 +0000199 case PPC::LBZ: LoadSize = 1; break;
200 case PPC::LHA:
201 case PPC::LHZ: LoadSize = 2; break;
202 case PPC::LWZU:
Chris Lattner20463712006-03-07 07:14:55 +0000203 case PPC::LFSX:
Chris Lattnerc6644182006-03-07 06:32:48 +0000204 case PPC::LFS:
Chris Lattner20463712006-03-07 07:14:55 +0000205 case PPC::LWZX:
Chris Lattnerc6644182006-03-07 06:32:48 +0000206 case PPC::LWZ: LoadSize = 4; break;
Chris Lattner20463712006-03-07 07:14:55 +0000207 case PPC::LFD: LoadSize = 8; break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000208 }
209
210 if (isLoadOfStoredAddress(LoadSize,
211 Node->getOperand(0), Node->getOperand(1)))
212 return NoopHazard;
213 }
214
215 return NoHazard;
216}
217
218void PPCHazardRecognizer970::EmitInstruction(SDNode *Node) {
219 PPC970InstrType InstrType = GetInstrType(Node->getOpcode());
220 if (InstrType == PseudoInst) return;
221 unsigned Opcode = Node->getOpcode()-ISD::BUILTIN_OP_END;
222
223 // Update structural hazard information.
224 if (Opcode == PPC::MTCTR) HasCTRSet = true;
225
226 // Track the address stored to.
227 if (InstrType == LSU_ST) {
228 StorePtr1 = Node->getOperand(1);
229 StorePtr2 = Node->getOperand(2);
230 switch (Opcode) {
231 default: assert(0 && "Unknown store instruction!");
Chris Lattnerab5801c2006-03-07 16:19:46 +0000232 case PPC::STB: StoreSize = 1; break;
233 case PPC::STH: StoreSize = 2; break;
Chris Lattnerb84225b2006-03-07 16:26:48 +0000234 case PPC::STFS:
Chris Lattnerab5801c2006-03-07 16:19:46 +0000235 case PPC::STWU:
Chris Lattner20463712006-03-07 07:14:55 +0000236 case PPC::STW: StoreSize = 4; break;
Chris Lattnerab5801c2006-03-07 16:19:46 +0000237 case PPC::STFD: StoreSize = 8; break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000238 }
239 }
240
241 switch (InstrType) {
242 default: assert(0 && "Unknown instruction type!");
Nate Begeman3acbe5d2006-03-07 08:30:27 +0000243 case FXU:
244 case FXU_FIRST: ++NumFXU; break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000245 case LSU_LD:
Nate Begeman3acbe5d2006-03-07 08:30:27 +0000246 case LSU_ST: ++NumLSU; break;
247 case FPU: ++NumFPU; break;
248 case CR: HasCR = true; break;
249 case SPR: HasSPR = true; break;
250 case VALU: HasVALU = true; break;
251 case VPERM: HasVPERM = true; break;
252 case BR: NumIssued = 4; return; // ends a d-group.
Chris Lattnerc6644182006-03-07 06:32:48 +0000253 }
254 ++NumIssued;
255
256 if (NumIssued == 5)
257 EndDispatchGroup();
258}
259
260void PPCHazardRecognizer970::AdvanceCycle() {
261 assert(NumIssued < 5 && "Illegal dispatch group!");
262 ++NumIssued;
263 if (NumIssued == 5)
264 EndDispatchGroup();
265}
266
267void PPCHazardRecognizer970::EmitNoop() {
268 AdvanceCycle();
269}