blob: ae317af4823a6f1f88ed0b03ebee0489d86f786b [file] [log] [blame]
Chris Lattnerc6644182006-03-07 06:32:48 +00001//===-- PPCHazardRecognizers.cpp - PowerPC Hazard Recognizer Impls --------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lattnerc6644182006-03-07 06:32:48 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements hazard recognizers for scheduling on PowerPC processors.
11//
12//===----------------------------------------------------------------------===//
13
Dale Johannesene7e7d0d2007-07-13 17:13:54 +000014#define DEBUG_TYPE "pre-RA-sched"
Chris Lattnerc6644182006-03-07 06:32:48 +000015#include "PPCHazardRecognizers.h"
16#include "PPC.h"
Chris Lattner88d211f2006-03-12 09:13:49 +000017#include "PPCInstrInfo.h"
Dan Gohmanfc54c552009-01-15 22:18:12 +000018#include "llvm/CodeGen/ScheduleDAG.h"
Chris Lattnerc6644182006-03-07 06:32:48 +000019#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattner893e1c92009-08-23 06:49:22 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6644182006-03-07 06:32:48 +000022using namespace llvm;
23
Chris Lattnerc6644182006-03-07 06:32:48 +000024//===----------------------------------------------------------------------===//
Hal Finkelc6d08f12011-10-17 04:03:49 +000025// PowerPC 440 Hazard Recognizer
26void PPCHazardRecognizer440::EmitInstruction(SUnit *SU) {
27 const MCInstrDesc *MCID = DAG->getInstrDesc(SU);
28 if (!MCID) {
29 // This is a PPC pseudo-instruction.
Hal Finkelc6d08f12011-10-17 04:03:49 +000030 return;
31 }
32
33 ScoreboardHazardRecognizer::EmitInstruction(SU);
34}
35
36//===----------------------------------------------------------------------===//
Chris Lattnerc6644182006-03-07 06:32:48 +000037// PowerPC 970 Hazard Recognizer
38//
Chris Lattner7ce64852006-03-07 06:44:19 +000039// This models the dispatch group formation of the PPC970 processor. Dispatch
Chris Lattner88d211f2006-03-12 09:13:49 +000040// groups are bundles of up to five instructions that can contain various mixes
Andrew Trick6e8f4c42010-12-24 04:28:06 +000041// of instructions. The PPC970 can dispatch a peak of 4 non-branch and one
Chris Lattner88d211f2006-03-12 09:13:49 +000042// branch instruction per-cycle.
Chris Lattner7ce64852006-03-07 06:44:19 +000043//
Chris Lattner88d211f2006-03-12 09:13:49 +000044// There are a number of restrictions to dispatch group formation: some
45// instructions can only be issued in the first slot of a dispatch group, & some
46// instructions fill an entire dispatch group. Additionally, only branches can
47// issue in the 5th (last) slot.
Chris Lattner7ce64852006-03-07 06:44:19 +000048//
49// Finally, there are a number of "structural" hazards on the PPC970. These
50// conditions cause large performance penalties due to misprediction, recovery,
51// and replay logic that has to happen. These cases include setting a CTR and
52// branching through it in the same dispatch group, and storing to an address,
53// then loading from the same address within a dispatch group. To avoid these
54// conditions, we insert no-op instructions when appropriate.
55//
Chris Lattnerc6644182006-03-07 06:32:48 +000056// FIXME: This is missing some significant cases:
Chris Lattnerc6644182006-03-07 06:32:48 +000057// 1. Modeling of microcoded instructions.
Chris Lattner3faad492006-03-13 05:20:04 +000058// 2. Handling of serialized operations.
59// 3. Handling of the esoteric cases in "Resource-based Instruction Grouping".
Chris Lattnerc6644182006-03-07 06:32:48 +000060//
Chris Lattnerc6644182006-03-07 06:32:48 +000061
Chris Lattner88d211f2006-03-12 09:13:49 +000062PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetInstrInfo &tii)
63 : TII(tii) {
Hal Finkel7f370b62011-12-15 17:54:01 +000064 LastWasBL8_ELF = false;
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000065 EndDispatchGroup();
66}
67
Chris Lattnerc6644182006-03-07 06:32:48 +000068void PPCHazardRecognizer970::EndDispatchGroup() {
Chris Lattner893e1c92009-08-23 06:49:22 +000069 DEBUG(errs() << "=== Start of dispatch group\n");
Chris Lattnerc6644182006-03-07 06:32:48 +000070 NumIssued = 0;
Andrew Trick6e8f4c42010-12-24 04:28:06 +000071
Chris Lattnerc6644182006-03-07 06:32:48 +000072 // Structural hazard info.
73 HasCTRSet = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000074 NumStores = 0;
Chris Lattnerc6644182006-03-07 06:32:48 +000075}
76
77
Andrew Trick6e8f4c42010-12-24 04:28:06 +000078PPCII::PPC970_Unit
Chris Lattner88d211f2006-03-12 09:13:49 +000079PPCHazardRecognizer970::GetInstrType(unsigned Opcode,
80 bool &isFirst, bool &isSingle,
Chris Lattner3faad492006-03-13 05:20:04 +000081 bool &isCracked,
82 bool &isLoad, bool &isStore) {
Evan Chenge837dea2011-06-28 19:10:37 +000083 const MCInstrDesc &MCID = TII.get(Opcode);
Andrew Trick6e8f4c42010-12-24 04:28:06 +000084
Evan Chenge837dea2011-06-28 19:10:37 +000085 isLoad = MCID.mayLoad();
86 isStore = MCID.mayStore();
Andrew Trick6e8f4c42010-12-24 04:28:06 +000087
Evan Chenge837dea2011-06-28 19:10:37 +000088 uint64_t TSFlags = MCID.TSFlags;
Andrew Trick6e8f4c42010-12-24 04:28:06 +000089
Chris Lattner3faad492006-03-13 05:20:04 +000090 isFirst = TSFlags & PPCII::PPC970_First;
91 isSingle = TSFlags & PPCII::PPC970_Single;
92 isCracked = TSFlags & PPCII::PPC970_Cracked;
Chris Lattner88d211f2006-03-12 09:13:49 +000093 return (PPCII::PPC970_Unit)(TSFlags & PPCII::PPC970_Mask);
Chris Lattnerc6644182006-03-07 06:32:48 +000094}
95
Chris Lattnerc6644182006-03-07 06:32:48 +000096/// isLoadOfStoredAddress - If we have a load from the previously stored pointer
97/// as indicated by StorePtr1/StorePtr2/StoreSize, return true.
98bool PPCHazardRecognizer970::
Hal Finkel64c34e22011-12-02 04:58:02 +000099isLoadOfStoredAddress(uint64_t LoadSize, int64_t LoadOffset,
100 const Value *LoadValue) const {
Chris Lattner88d211f2006-03-12 09:13:49 +0000101 for (unsigned i = 0, e = NumStores; i != e; ++i) {
102 // Handle exact and commuted addresses.
Hal Finkel64c34e22011-12-02 04:58:02 +0000103 if (LoadValue == StoreValue[i] && LoadOffset == StoreOffset[i])
Chris Lattner88d211f2006-03-12 09:13:49 +0000104 return true;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000105
Chris Lattner88d211f2006-03-12 09:13:49 +0000106 // Okay, we don't have an exact match, if this is an indexed offset, see if
107 // we have overlap (which happens during fp->int conversion for example).
Hal Finkel64c34e22011-12-02 04:58:02 +0000108 if (StoreValue[i] == LoadValue) {
109 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check
110 // to see if the load and store actually overlap.
111 if (StoreOffset[i] < LoadOffset) {
112 if (int64_t(StoreOffset[i]+StoreSize[i]) > LoadOffset) return true;
113 } else {
114 if (int64_t(LoadOffset+LoadSize) > StoreOffset[i]) return true;
115 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000116 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000117 }
118 return false;
119}
120
121/// getHazardType - We return hazard for any non-branch instruction that would
Dan Gohmanf451cb82010-02-10 16:03:48 +0000122/// terminate the dispatch group. We turn NoopHazard for any
Chris Lattnerc6644182006-03-07 06:32:48 +0000123/// instructions that wouldn't terminate the dispatch group that would cause a
124/// pipeline flush.
Dan Gohmanfc54c552009-01-15 22:18:12 +0000125ScheduleHazardRecognizer::HazardType PPCHazardRecognizer970::
Andrew Trick2da8bc82010-12-24 05:03:26 +0000126getHazardType(SUnit *SU, int Stalls) {
127 assert(Stalls == 0 && "PPC hazards don't support scoreboard lookahead");
128
Hal Finkel64c34e22011-12-02 04:58:02 +0000129 MachineInstr *MI = SU->getInstr();
130
131 if (MI->isDebugValue())
132 return NoHazard;
133
134 unsigned Opcode = MI->getOpcode();
135
Hal Finkel7f370b62011-12-15 17:54:01 +0000136 // If the last instruction was a BL8_ELF, then the NOP must follow it
137 // directly (this is strong requirement from the linker due to the ELF ABI).
138 // We return only Hazard (and not NoopHazard) because if the NOP is necessary
139 // then it will already be in the instruction stream (it is not always
140 // necessary; tail calls, for example, do not need it).
141 if (LastWasBL8_ELF && Opcode != PPC::NOP)
142 return Hazard;
143
Chris Lattner3faad492006-03-13 05:20:04 +0000144 bool isFirst, isSingle, isCracked, isLoad, isStore;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000145 PPCII::PPC970_Unit InstrType =
Hal Finkel64c34e22011-12-02 04:58:02 +0000146 GetInstrType(Opcode, isFirst, isSingle, isCracked,
Chris Lattner3faad492006-03-13 05:20:04 +0000147 isLoad, isStore);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000148 if (InstrType == PPCII::PPC970_Pseudo) return NoHazard;
Chris Lattnerc6644182006-03-07 06:32:48 +0000149
Chris Lattner88d211f2006-03-12 09:13:49 +0000150 // We can only issue a PPC970_First/PPC970_Single instruction (such as
151 // crand/mtspr/etc) if this is the first cycle of the dispatch group.
Chris Lattner3faad492006-03-13 05:20:04 +0000152 if (NumIssued != 0 && (isFirst || isSingle))
Chris Lattner88d211f2006-03-12 09:13:49 +0000153 return Hazard;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000154
Chris Lattner3faad492006-03-13 05:20:04 +0000155 // If this instruction is cracked into two ops by the decoder, we know that
156 // it is not a branch and that it cannot issue if 3 other instructions are
157 // already in the dispatch group.
158 if (isCracked && NumIssued > 2)
159 return Hazard;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000160
Chris Lattnerc6644182006-03-07 06:32:48 +0000161 switch (InstrType) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000162 default: llvm_unreachable("Unknown instruction type!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000163 case PPCII::PPC970_FXU:
164 case PPCII::PPC970_LSU:
165 case PPCII::PPC970_FPU:
166 case PPCII::PPC970_VALU:
167 case PPCII::PPC970_VPERM:
168 // We can only issue a branch as the last instruction in a group.
169 if (NumIssued == 4) return Hazard;
170 break;
171 case PPCII::PPC970_CRU:
172 // We can only issue a CR instruction in the first two slots.
173 if (NumIssued >= 2) return Hazard;
174 break;
175 case PPCII::PPC970_BRU:
176 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000177 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000178
Chris Lattnerc6644182006-03-07 06:32:48 +0000179 // Do not allow MTCTR and BCTRL to be in the same dispatch group.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000180 if (HasCTRSet && (Opcode == PPC::BCTRL_Darwin || Opcode == PPC::BCTRL_SVR4))
Chris Lattnerc6644182006-03-07 06:32:48 +0000181 return NoopHazard;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000182
Chris Lattnerc6644182006-03-07 06:32:48 +0000183 // If this is a load following a store, make sure it's not to the same or
184 // overlapping address.
Hal Finkel64c34e22011-12-02 04:58:02 +0000185 if (isLoad && NumStores && !MI->memoperands_empty()) {
186 MachineMemOperand *MO = *MI->memoperands_begin();
187 if (isLoadOfStoredAddress(MO->getSize(),
188 MO->getOffset(), MO->getValue()))
Chris Lattnerc6644182006-03-07 06:32:48 +0000189 return NoopHazard;
190 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000191
Chris Lattnerc6644182006-03-07 06:32:48 +0000192 return NoHazard;
193}
194
Dan Gohmanfc54c552009-01-15 22:18:12 +0000195void PPCHazardRecognizer970::EmitInstruction(SUnit *SU) {
Hal Finkel64c34e22011-12-02 04:58:02 +0000196 MachineInstr *MI = SU->getInstr();
197
198 if (MI->isDebugValue())
199 return;
200
201 unsigned Opcode = MI->getOpcode();
Hal Finkel7f370b62011-12-15 17:54:01 +0000202 LastWasBL8_ELF = (Opcode == PPC::BL8_ELF);
Hal Finkel64c34e22011-12-02 04:58:02 +0000203
Chris Lattner3faad492006-03-13 05:20:04 +0000204 bool isFirst, isSingle, isCracked, isLoad, isStore;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000205 PPCII::PPC970_Unit InstrType =
Hal Finkel64c34e22011-12-02 04:58:02 +0000206 GetInstrType(Opcode, isFirst, isSingle, isCracked,
Chris Lattner3faad492006-03-13 05:20:04 +0000207 isLoad, isStore);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000208 if (InstrType == PPCII::PPC970_Pseudo) return;
Chris Lattnerc6644182006-03-07 06:32:48 +0000209
210 // Update structural hazard information.
Roman Divacky0c9b5592011-06-03 15:47:49 +0000211 if (Opcode == PPC::MTCTR || Opcode == PPC::MTCTR8) HasCTRSet = true;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000212
Chris Lattnerc6644182006-03-07 06:32:48 +0000213 // Track the address stored to.
Hal Finkel64c34e22011-12-02 04:58:02 +0000214 if (isStore && NumStores < 4 && !MI->memoperands_empty()) {
215 MachineMemOperand *MO = *MI->memoperands_begin();
216 StoreSize[NumStores] = MO->getSize();
217 StoreOffset[NumStores] = MO->getOffset();
218 StoreValue[NumStores] = MO->getValue();
Chris Lattner88d211f2006-03-12 09:13:49 +0000219 ++NumStores;
Chris Lattnerc6644182006-03-07 06:32:48 +0000220 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000221
Chris Lattner88d211f2006-03-12 09:13:49 +0000222 if (InstrType == PPCII::PPC970_BRU || isSingle)
223 NumIssued = 4; // Terminate a d-group.
Chris Lattnerc6644182006-03-07 06:32:48 +0000224 ++NumIssued;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000225
Chris Lattner3faad492006-03-13 05:20:04 +0000226 // If this instruction is cracked into two ops by the decoder, remember that
227 // we issued two pieces.
228 if (isCracked)
229 ++NumIssued;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000230
Chris Lattnerc6644182006-03-07 06:32:48 +0000231 if (NumIssued == 5)
232 EndDispatchGroup();
233}
234
235void PPCHazardRecognizer970::AdvanceCycle() {
236 assert(NumIssued < 5 && "Illegal dispatch group!");
237 ++NumIssued;
238 if (NumIssued == 5)
239 EndDispatchGroup();
240}
Hal Finkel64c34e22011-12-02 04:58:02 +0000241
242void PPCHazardRecognizer970::Reset() {
Hal Finkel7f370b62011-12-15 17:54:01 +0000243 LastWasBL8_ELF = false;
Hal Finkel64c34e22011-12-02 04:58:02 +0000244 EndDispatchGroup();
245}
246