blob: 37c85b37351d1ccd4f2eef693f52b4527bbaff80 [file] [log] [blame]
Chris Lattner2cab1352006-03-07 06:32:48 +00001//===-- PPCHazardRecognizers.cpp - PowerPC Hazard Recognizer Impls --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner2cab1352006-03-07 06:32:48 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements hazard recognizers for scheduling on PowerPC processors.
11//
12//===----------------------------------------------------------------------===//
13
Dale Johannesen2182f062007-07-13 17:13:54 +000014#define DEBUG_TYPE "pre-RA-sched"
Chris Lattner2cab1352006-03-07 06:32:48 +000015#include "PPCHazardRecognizers.h"
16#include "PPC.h"
Chris Lattner51348c52006-03-12 09:13:49 +000017#include "PPCInstrInfo.h"
Hal Finkelceb1f122013-12-12 00:19:11 +000018#include "PPCTargetMachine.h"
Dan Gohman7e105f02009-01-15 22:18:12 +000019#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattner2cab1352006-03-07 06:32:48 +000020#include "llvm/Support/Debug.h"
Torok Edwin56d06592009-07-11 20:10:48 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattneraf29ea62009-08-23 06:49:22 +000022#include "llvm/Support/raw_ostream.h"
Chris Lattner2cab1352006-03-07 06:32:48 +000023using namespace llvm;
24
Hal Finkelceb1f122013-12-12 00:19:11 +000025bool PPCDispatchGroupSBHazardRecognizer::isLoadAfterStore(SUnit *SU) {
26 // FIXME: Move this.
27 if (isBCTRAfterSet(SU))
28 return true;
29
30 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
31 if (!MCID)
32 return false;
33
34 if (!MCID->mayLoad())
35 return false;
36
37 // SU is a load; for any predecessors in this dispatch group, that are stores,
38 // and with which we have an ordering dependency, return true.
39 for (unsigned i = 0, ie = (unsigned) SU->Preds.size(); i != ie; ++i) {
40 const MCInstrDesc *PredMCID = DAG->getInstrDesc(SU->Preds[i].getSUnit());
41 if (!PredMCID || !PredMCID->mayStore())
42 continue;
43
44 if (!SU->Preds[i].isNormalMemory() && !SU->Preds[i].isBarrier())
45 continue;
46
47 for (unsigned j = 0, je = CurGroup.size(); j != je; ++j)
48 if (SU->Preds[i].getSUnit() == CurGroup[j])
49 return true;
50 }
51
52 return false;
53}
54
55bool PPCDispatchGroupSBHazardRecognizer::isBCTRAfterSet(SUnit *SU) {
56 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
57 if (!MCID)
58 return false;
59
60 if (!MCID->isBranch())
61 return false;
62
63 // SU is a branch; for any predecessors in this dispatch group, with which we
64 // have a data dependence and set the counter register, return true.
65 for (unsigned i = 0, ie = (unsigned) SU->Preds.size(); i != ie; ++i) {
66 const MCInstrDesc *PredMCID = DAG->getInstrDesc(SU->Preds[i].getSUnit());
67 if (!PredMCID || PredMCID->getSchedClass() != PPC::Sched::IIC_SprMTSPR)
68 continue;
69
70 if (SU->Preds[i].isCtrl())
71 continue;
72
73 for (unsigned j = 0, je = CurGroup.size(); j != je; ++j)
74 if (SU->Preds[i].getSUnit() == CurGroup[j])
75 return true;
76 }
77
78 return false;
79}
80
81// FIXME: Remove this when we don't need this:
82namespace llvm { namespace PPC { extern int getNonRecordFormOpcode(uint16_t); } }
83
84// FIXME: A lot of code in PPCDispatchGroupSBHazardRecognizer is P7 specific.
85
86bool PPCDispatchGroupSBHazardRecognizer::mustComeFirst(const MCInstrDesc *MCID,
87 unsigned &NSlots) {
88 // FIXME: Indirectly, this information is contained in the itinerary, and
89 // we should derive it from there instead of separately specifying it
90 // here.
91 unsigned IIC = MCID->getSchedClass();
92 switch (IIC) {
93 default:
94 NSlots = 1;
95 break;
96 case PPC::Sched::IIC_IntDivW:
97 case PPC::Sched::IIC_IntDivD:
98 case PPC::Sched::IIC_LdStLoadUpd:
99 case PPC::Sched::IIC_LdStLDU:
100 case PPC::Sched::IIC_LdStLFDU:
101 case PPC::Sched::IIC_LdStLFDUX:
102 case PPC::Sched::IIC_LdStLHA:
103 case PPC::Sched::IIC_LdStLHAU:
104 case PPC::Sched::IIC_LdStLWA:
105 case PPC::Sched::IIC_LdStSTDU:
106 case PPC::Sched::IIC_LdStSTFDU:
107 NSlots = 2;
108 break;
109 case PPC::Sched::IIC_LdStLoadUpdX:
110 case PPC::Sched::IIC_LdStLDUX:
111 case PPC::Sched::IIC_LdStLHAUX:
112 case PPC::Sched::IIC_LdStLWARX:
113 case PPC::Sched::IIC_LdStLDARX:
114 case PPC::Sched::IIC_LdStSTDUX:
115 case PPC::Sched::IIC_LdStSTDCX:
116 case PPC::Sched::IIC_LdStSTWCX:
117 case PPC::Sched::IIC_BrMCRX: // mtcr
118 // FIXME: Add sync/isync (here and in the itinerary).
119 NSlots = 4;
120 break;
121 }
122
123 // FIXME: record-form instructions need a different itinerary class.
124 if (NSlots == 1 && PPC::getNonRecordFormOpcode(MCID->getOpcode()) != -1)
125 NSlots = 2;
126
127 switch (IIC) {
128 default:
129 // All multi-slot instructions must come first.
130 return NSlots > 1;
Hal Finkel1d429f22014-01-02 21:38:26 +0000131 case PPC::Sched::IIC_BrCR: // cr logicals
Hal Finkelceb1f122013-12-12 00:19:11 +0000132 case PPC::Sched::IIC_SprMFCR:
133 case PPC::Sched::IIC_SprMFCRF:
134 case PPC::Sched::IIC_SprMTSPR:
135 return true;
136 }
137}
138
139ScheduleHazardRecognizer::HazardType
140PPCDispatchGroupSBHazardRecognizer::getHazardType(SUnit *SU, int Stalls) {
141 if (Stalls == 0 && isLoadAfterStore(SU))
142 return NoopHazard;
143
144 return ScoreboardHazardRecognizer::getHazardType(SU, Stalls);
145}
146
147bool PPCDispatchGroupSBHazardRecognizer::ShouldPreferAnother(SUnit *SU) {
148 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
149 unsigned NSlots;
150 if (MCID && mustComeFirst(MCID, NSlots) && CurSlots)
151 return true;
152
153 return ScoreboardHazardRecognizer::ShouldPreferAnother(SU);
154}
155
156unsigned PPCDispatchGroupSBHazardRecognizer::PreEmitNoops(SUnit *SU) {
157 // We only need to fill out a maximum of 5 slots here: The 6th slot could
158 // only be a second branch, and otherwise the next instruction will start a
159 // new group.
160 if (isLoadAfterStore(SU) && CurSlots < 6) {
161 unsigned Directive =
162 DAG->TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
163 // If we're using a special group-terminating nop, then we need only one.
164 if (Directive == PPC::DIR_PWR6 || Directive == PPC::DIR_PWR7)
165 return 1;
166
167 return 5 - CurSlots;
168 }
169
170 return ScoreboardHazardRecognizer::PreEmitNoops(SU);
171}
172
173void PPCDispatchGroupSBHazardRecognizer::EmitInstruction(SUnit *SU) {
174 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
175 if (MCID) {
176 if (CurSlots == 5 || (MCID->isBranch() && CurBranches == 1)) {
177 CurGroup.clear();
178 CurSlots = CurBranches = 0;
179 } else {
180 DEBUG(dbgs() << "**** Adding to dispatch group: SU(" <<
181 SU->NodeNum << "): ");
182 DEBUG(DAG->dumpNode(SU));
183
184 unsigned NSlots;
185 bool MustBeFirst = mustComeFirst(MCID, NSlots);
186
187 // If this instruction must come first, but does not, then it starts a
188 // new group.
189 if (MustBeFirst && CurSlots) {
190 CurSlots = CurBranches = 0;
191 CurGroup.clear();
192 }
193
194 CurSlots += NSlots;
195 CurGroup.push_back(SU);
196
197 if (MCID->isBranch())
198 ++CurBranches;
199 }
200 }
201
202 return ScoreboardHazardRecognizer::EmitInstruction(SU);
203}
204
205void PPCDispatchGroupSBHazardRecognizer::AdvanceCycle() {
206 return ScoreboardHazardRecognizer::AdvanceCycle();
207}
208
209void PPCDispatchGroupSBHazardRecognizer::RecedeCycle() {
210 llvm_unreachable("Bottom-up scheduling not supported");
211}
212
213void PPCDispatchGroupSBHazardRecognizer::Reset() {
214 CurGroup.clear();
215 CurSlots = CurBranches = 0;
216 return ScoreboardHazardRecognizer::Reset();
217}
218
219void PPCDispatchGroupSBHazardRecognizer::EmitNoop() {
220 unsigned Directive =
221 DAG->TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
222 // If the group has now filled all of its slots, or if we're using a special
223 // group-terminating nop, the group is complete.
224 if (Directive == PPC::DIR_PWR6 || Directive == PPC::DIR_PWR7 ||
225 CurSlots == 6) {
226 CurGroup.clear();
227 CurSlots = CurBranches = 0;
228 } else {
229 CurGroup.push_back(0);
230 ++CurSlots;
231 }
232}
233
Chris Lattner2cab1352006-03-07 06:32:48 +0000234//===----------------------------------------------------------------------===//
235// PowerPC 970 Hazard Recognizer
236//
Chris Lattner05ad1282006-03-07 06:44:19 +0000237// This models the dispatch group formation of the PPC970 processor. Dispatch
Chris Lattner51348c52006-03-12 09:13:49 +0000238// groups are bundles of up to five instructions that can contain various mixes
Andrew Trickc416ba62010-12-24 04:28:06 +0000239// of instructions. The PPC970 can dispatch a peak of 4 non-branch and one
Chris Lattner51348c52006-03-12 09:13:49 +0000240// branch instruction per-cycle.
Chris Lattner05ad1282006-03-07 06:44:19 +0000241//
Chris Lattner51348c52006-03-12 09:13:49 +0000242// There are a number of restrictions to dispatch group formation: some
243// instructions can only be issued in the first slot of a dispatch group, & some
244// instructions fill an entire dispatch group. Additionally, only branches can
245// issue in the 5th (last) slot.
Chris Lattner05ad1282006-03-07 06:44:19 +0000246//
247// Finally, there are a number of "structural" hazards on the PPC970. These
248// conditions cause large performance penalties due to misprediction, recovery,
249// and replay logic that has to happen. These cases include setting a CTR and
250// branching through it in the same dispatch group, and storing to an address,
251// then loading from the same address within a dispatch group. To avoid these
252// conditions, we insert no-op instructions when appropriate.
253//
Chris Lattner2cab1352006-03-07 06:32:48 +0000254// FIXME: This is missing some significant cases:
Chris Lattner2cab1352006-03-07 06:32:48 +0000255// 1. Modeling of microcoded instructions.
Chris Lattner4fbb6122006-03-13 05:20:04 +0000256// 2. Handling of serialized operations.
257// 3. Handling of the esoteric cases in "Resource-based Instruction Grouping".
Chris Lattner2cab1352006-03-07 06:32:48 +0000258//
Chris Lattner2cab1352006-03-07 06:32:48 +0000259
Bill Wendling5e7656b2013-06-07 07:55:53 +0000260PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetMachine &TM)
261 : TM(TM) {
Chris Lattner543832d2006-03-08 04:25:59 +0000262 EndDispatchGroup();
263}
264
Chris Lattner2cab1352006-03-07 06:32:48 +0000265void PPCHazardRecognizer970::EndDispatchGroup() {
Chris Lattneraf29ea62009-08-23 06:49:22 +0000266 DEBUG(errs() << "=== Start of dispatch group\n");
Chris Lattner2cab1352006-03-07 06:32:48 +0000267 NumIssued = 0;
Andrew Trickc416ba62010-12-24 04:28:06 +0000268
Chris Lattner2cab1352006-03-07 06:32:48 +0000269 // Structural hazard info.
270 HasCTRSet = false;
Chris Lattner51348c52006-03-12 09:13:49 +0000271 NumStores = 0;
Chris Lattner2cab1352006-03-07 06:32:48 +0000272}
273
274
Andrew Trickc416ba62010-12-24 04:28:06 +0000275PPCII::PPC970_Unit
Chris Lattner51348c52006-03-12 09:13:49 +0000276PPCHazardRecognizer970::GetInstrType(unsigned Opcode,
277 bool &isFirst, bool &isSingle,
Chris Lattner4fbb6122006-03-13 05:20:04 +0000278 bool &isCracked,
279 bool &isLoad, bool &isStore) {
Bill Wendling5e7656b2013-06-07 07:55:53 +0000280 const MCInstrDesc &MCID = TM.getInstrInfo()->get(Opcode);
Andrew Trickc416ba62010-12-24 04:28:06 +0000281
Evan Cheng6cc775f2011-06-28 19:10:37 +0000282 isLoad = MCID.mayLoad();
283 isStore = MCID.mayStore();
Andrew Trickc416ba62010-12-24 04:28:06 +0000284
Evan Cheng6cc775f2011-06-28 19:10:37 +0000285 uint64_t TSFlags = MCID.TSFlags;
Andrew Trickc416ba62010-12-24 04:28:06 +0000286
Chris Lattner4fbb6122006-03-13 05:20:04 +0000287 isFirst = TSFlags & PPCII::PPC970_First;
288 isSingle = TSFlags & PPCII::PPC970_Single;
289 isCracked = TSFlags & PPCII::PPC970_Cracked;
Chris Lattner51348c52006-03-12 09:13:49 +0000290 return (PPCII::PPC970_Unit)(TSFlags & PPCII::PPC970_Mask);
Chris Lattner2cab1352006-03-07 06:32:48 +0000291}
292
Chris Lattner2cab1352006-03-07 06:32:48 +0000293/// isLoadOfStoredAddress - If we have a load from the previously stored pointer
294/// as indicated by StorePtr1/StorePtr2/StoreSize, return true.
295bool PPCHazardRecognizer970::
Hal Finkel58ca3602011-12-02 04:58:02 +0000296isLoadOfStoredAddress(uint64_t LoadSize, int64_t LoadOffset,
297 const Value *LoadValue) const {
Chris Lattner51348c52006-03-12 09:13:49 +0000298 for (unsigned i = 0, e = NumStores; i != e; ++i) {
299 // Handle exact and commuted addresses.
Hal Finkel58ca3602011-12-02 04:58:02 +0000300 if (LoadValue == StoreValue[i] && LoadOffset == StoreOffset[i])
Chris Lattner51348c52006-03-12 09:13:49 +0000301 return true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000302
Chris Lattner51348c52006-03-12 09:13:49 +0000303 // Okay, we don't have an exact match, if this is an indexed offset, see if
304 // we have overlap (which happens during fp->int conversion for example).
Hal Finkel58ca3602011-12-02 04:58:02 +0000305 if (StoreValue[i] == LoadValue) {
306 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check
307 // to see if the load and store actually overlap.
308 if (StoreOffset[i] < LoadOffset) {
309 if (int64_t(StoreOffset[i]+StoreSize[i]) > LoadOffset) return true;
310 } else {
311 if (int64_t(LoadOffset+LoadSize) > StoreOffset[i]) return true;
312 }
Chris Lattner51348c52006-03-12 09:13:49 +0000313 }
Chris Lattner2cab1352006-03-07 06:32:48 +0000314 }
315 return false;
316}
317
318/// getHazardType - We return hazard for any non-branch instruction that would
Dan Gohman4a618822010-02-10 16:03:48 +0000319/// terminate the dispatch group. We turn NoopHazard for any
Chris Lattner2cab1352006-03-07 06:32:48 +0000320/// instructions that wouldn't terminate the dispatch group that would cause a
321/// pipeline flush.
Dan Gohman7e105f02009-01-15 22:18:12 +0000322ScheduleHazardRecognizer::HazardType PPCHazardRecognizer970::
Andrew Trick10ffc2b2010-12-24 05:03:26 +0000323getHazardType(SUnit *SU, int Stalls) {
324 assert(Stalls == 0 && "PPC hazards don't support scoreboard lookahead");
325
Hal Finkel58ca3602011-12-02 04:58:02 +0000326 MachineInstr *MI = SU->getInstr();
327
328 if (MI->isDebugValue())
329 return NoHazard;
330
331 unsigned Opcode = MI->getOpcode();
Chris Lattner4fbb6122006-03-13 05:20:04 +0000332 bool isFirst, isSingle, isCracked, isLoad, isStore;
Andrew Trickc416ba62010-12-24 04:28:06 +0000333 PPCII::PPC970_Unit InstrType =
Hal Finkel58ca3602011-12-02 04:58:02 +0000334 GetInstrType(Opcode, isFirst, isSingle, isCracked,
Chris Lattner4fbb6122006-03-13 05:20:04 +0000335 isLoad, isStore);
Andrew Trickc416ba62010-12-24 04:28:06 +0000336 if (InstrType == PPCII::PPC970_Pseudo) return NoHazard;
Chris Lattner2cab1352006-03-07 06:32:48 +0000337
Chris Lattner51348c52006-03-12 09:13:49 +0000338 // We can only issue a PPC970_First/PPC970_Single instruction (such as
339 // crand/mtspr/etc) if this is the first cycle of the dispatch group.
Chris Lattner4fbb6122006-03-13 05:20:04 +0000340 if (NumIssued != 0 && (isFirst || isSingle))
Chris Lattner51348c52006-03-12 09:13:49 +0000341 return Hazard;
Andrew Trickc416ba62010-12-24 04:28:06 +0000342
Chris Lattner4fbb6122006-03-13 05:20:04 +0000343 // If this instruction is cracked into two ops by the decoder, we know that
344 // it is not a branch and that it cannot issue if 3 other instructions are
345 // already in the dispatch group.
346 if (isCracked && NumIssued > 2)
347 return Hazard;
Andrew Trickc416ba62010-12-24 04:28:06 +0000348
Chris Lattner2cab1352006-03-07 06:32:48 +0000349 switch (InstrType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000350 default: llvm_unreachable("Unknown instruction type!");
Chris Lattner51348c52006-03-12 09:13:49 +0000351 case PPCII::PPC970_FXU:
352 case PPCII::PPC970_LSU:
353 case PPCII::PPC970_FPU:
354 case PPCII::PPC970_VALU:
355 case PPCII::PPC970_VPERM:
356 // We can only issue a branch as the last instruction in a group.
357 if (NumIssued == 4) return Hazard;
358 break;
359 case PPCII::PPC970_CRU:
360 // We can only issue a CR instruction in the first two slots.
361 if (NumIssued >= 2) return Hazard;
362 break;
363 case PPCII::PPC970_BRU:
364 break;
Chris Lattner2cab1352006-03-07 06:32:48 +0000365 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000366
Chris Lattner2cab1352006-03-07 06:32:48 +0000367 // Do not allow MTCTR and BCTRL to be in the same dispatch group.
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000368 if (HasCTRSet && Opcode == PPC::BCTRL)
Chris Lattner2cab1352006-03-07 06:32:48 +0000369 return NoopHazard;
Andrew Trickc416ba62010-12-24 04:28:06 +0000370
Chris Lattner2cab1352006-03-07 06:32:48 +0000371 // If this is a load following a store, make sure it's not to the same or
372 // overlapping address.
Hal Finkel58ca3602011-12-02 04:58:02 +0000373 if (isLoad && NumStores && !MI->memoperands_empty()) {
374 MachineMemOperand *MO = *MI->memoperands_begin();
375 if (isLoadOfStoredAddress(MO->getSize(),
376 MO->getOffset(), MO->getValue()))
Chris Lattner2cab1352006-03-07 06:32:48 +0000377 return NoopHazard;
378 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000379
Chris Lattner2cab1352006-03-07 06:32:48 +0000380 return NoHazard;
381}
382
Dan Gohman7e105f02009-01-15 22:18:12 +0000383void PPCHazardRecognizer970::EmitInstruction(SUnit *SU) {
Hal Finkel58ca3602011-12-02 04:58:02 +0000384 MachineInstr *MI = SU->getInstr();
385
386 if (MI->isDebugValue())
387 return;
388
389 unsigned Opcode = MI->getOpcode();
Chris Lattner4fbb6122006-03-13 05:20:04 +0000390 bool isFirst, isSingle, isCracked, isLoad, isStore;
Andrew Trickc416ba62010-12-24 04:28:06 +0000391 PPCII::PPC970_Unit InstrType =
Hal Finkel58ca3602011-12-02 04:58:02 +0000392 GetInstrType(Opcode, isFirst, isSingle, isCracked,
Chris Lattner4fbb6122006-03-13 05:20:04 +0000393 isLoad, isStore);
Andrew Trickc416ba62010-12-24 04:28:06 +0000394 if (InstrType == PPCII::PPC970_Pseudo) return;
Chris Lattner2cab1352006-03-07 06:32:48 +0000395
396 // Update structural hazard information.
Roman Divackya4a59ae2011-06-03 15:47:49 +0000397 if (Opcode == PPC::MTCTR || Opcode == PPC::MTCTR8) HasCTRSet = true;
Andrew Trickc416ba62010-12-24 04:28:06 +0000398
Chris Lattner2cab1352006-03-07 06:32:48 +0000399 // Track the address stored to.
Hal Finkel58ca3602011-12-02 04:58:02 +0000400 if (isStore && NumStores < 4 && !MI->memoperands_empty()) {
401 MachineMemOperand *MO = *MI->memoperands_begin();
402 StoreSize[NumStores] = MO->getSize();
403 StoreOffset[NumStores] = MO->getOffset();
404 StoreValue[NumStores] = MO->getValue();
Chris Lattner51348c52006-03-12 09:13:49 +0000405 ++NumStores;
Chris Lattner2cab1352006-03-07 06:32:48 +0000406 }
Andrew Trickc416ba62010-12-24 04:28:06 +0000407
Chris Lattner51348c52006-03-12 09:13:49 +0000408 if (InstrType == PPCII::PPC970_BRU || isSingle)
409 NumIssued = 4; // Terminate a d-group.
Chris Lattner2cab1352006-03-07 06:32:48 +0000410 ++NumIssued;
Andrew Trickc416ba62010-12-24 04:28:06 +0000411
Chris Lattner4fbb6122006-03-13 05:20:04 +0000412 // If this instruction is cracked into two ops by the decoder, remember that
413 // we issued two pieces.
414 if (isCracked)
415 ++NumIssued;
Andrew Trickc416ba62010-12-24 04:28:06 +0000416
Chris Lattner2cab1352006-03-07 06:32:48 +0000417 if (NumIssued == 5)
418 EndDispatchGroup();
419}
420
421void PPCHazardRecognizer970::AdvanceCycle() {
422 assert(NumIssued < 5 && "Illegal dispatch group!");
423 ++NumIssued;
424 if (NumIssued == 5)
425 EndDispatchGroup();
426}
Hal Finkel58ca3602011-12-02 04:58:02 +0000427
428void PPCHazardRecognizer970::Reset() {
429 EndDispatchGroup();
430}
431