blob: c11e0dd01d0739f3aa21c84d529446f186c1c690 [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"
Chris Lattnerc6644182006-03-07 06:32:48 +000018#include "llvm/Support/Debug.h"
Chris Lattnerc6644182006-03-07 06:32:48 +000019using namespace llvm;
20
Chris Lattnerc6644182006-03-07 06:32:48 +000021//===----------------------------------------------------------------------===//
22// PowerPC 970 Hazard Recognizer
23//
Chris Lattner7ce64852006-03-07 06:44:19 +000024// This models the dispatch group formation of the PPC970 processor. Dispatch
Chris Lattner88d211f2006-03-12 09:13:49 +000025// groups are bundles of up to five instructions that can contain various mixes
26// of instructions. The PPC970 can dispatch a peak of 4 non-branch and one
27// branch instruction per-cycle.
Chris Lattner7ce64852006-03-07 06:44:19 +000028//
Chris Lattner88d211f2006-03-12 09:13:49 +000029// There are a number of restrictions to dispatch group formation: some
30// instructions can only be issued in the first slot of a dispatch group, & some
31// instructions fill an entire dispatch group. Additionally, only branches can
32// issue in the 5th (last) slot.
Chris Lattner7ce64852006-03-07 06:44:19 +000033//
34// Finally, there are a number of "structural" hazards on the PPC970. These
35// conditions cause large performance penalties due to misprediction, recovery,
36// and replay logic that has to happen. These cases include setting a CTR and
37// branching through it in the same dispatch group, and storing to an address,
38// then loading from the same address within a dispatch group. To avoid these
39// conditions, we insert no-op instructions when appropriate.
40//
Chris Lattnerc6644182006-03-07 06:32:48 +000041// FIXME: This is missing some significant cases:
Chris Lattnerc6644182006-03-07 06:32:48 +000042// 1. Modeling of microcoded instructions.
Chris Lattner3faad492006-03-13 05:20:04 +000043// 2. Handling of serialized operations.
44// 3. Handling of the esoteric cases in "Resource-based Instruction Grouping".
Chris Lattnerc6644182006-03-07 06:32:48 +000045//
Chris Lattnerc6644182006-03-07 06:32:48 +000046
Chris Lattner88d211f2006-03-12 09:13:49 +000047PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetInstrInfo &tii)
48 : TII(tii) {
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000049 EndDispatchGroup();
50}
51
Chris Lattnerc6644182006-03-07 06:32:48 +000052void PPCHazardRecognizer970::EndDispatchGroup() {
Bill Wendlingf5da1332006-12-07 22:21:48 +000053 DOUT << "=== Start of dispatch group\n";
Chris Lattnerc6644182006-03-07 06:32:48 +000054 NumIssued = 0;
55
56 // Structural hazard info.
57 HasCTRSet = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000058 NumStores = 0;
Chris Lattnerc6644182006-03-07 06:32:48 +000059}
60
61
Chris Lattner88d211f2006-03-12 09:13:49 +000062PPCII::PPC970_Unit
63PPCHazardRecognizer970::GetInstrType(unsigned Opcode,
64 bool &isFirst, bool &isSingle,
Chris Lattner3faad492006-03-13 05:20:04 +000065 bool &isCracked,
66 bool &isLoad, bool &isStore) {
Chris Lattner88d211f2006-03-12 09:13:49 +000067 if (Opcode < ISD::BUILTIN_OP_END) {
Chris Lattner3faad492006-03-13 05:20:04 +000068 isFirst = isSingle = isCracked = isLoad = isStore = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000069 return PPCII::PPC970_Pseudo;
70 }
Chris Lattnerc6644182006-03-07 06:32:48 +000071 Opcode -= ISD::BUILTIN_OP_END;
72
Chris Lattner88d211f2006-03-12 09:13:49 +000073 const TargetInstrDescriptor &TID = TII.get(Opcode);
Chris Lattnerc6644182006-03-07 06:32:48 +000074
Chris Lattner88d211f2006-03-12 09:13:49 +000075 isLoad = TID.Flags & M_LOAD_FLAG;
76 isStore = TID.Flags & M_STORE_FLAG;
77
78 unsigned TSFlags = TID.TSFlags;
79
Chris Lattner3faad492006-03-13 05:20:04 +000080 isFirst = TSFlags & PPCII::PPC970_First;
81 isSingle = TSFlags & PPCII::PPC970_Single;
82 isCracked = TSFlags & PPCII::PPC970_Cracked;
Chris Lattner88d211f2006-03-12 09:13:49 +000083 return (PPCII::PPC970_Unit)(TSFlags & PPCII::PPC970_Mask);
Chris Lattnerc6644182006-03-07 06:32:48 +000084}
85
Chris Lattnerc6644182006-03-07 06:32:48 +000086/// isLoadOfStoredAddress - If we have a load from the previously stored pointer
87/// as indicated by StorePtr1/StorePtr2/StoreSize, return true.
88bool PPCHazardRecognizer970::
89isLoadOfStoredAddress(unsigned LoadSize, SDOperand Ptr1, SDOperand Ptr2) const {
Chris Lattner88d211f2006-03-12 09:13:49 +000090 for (unsigned i = 0, e = NumStores; i != e; ++i) {
91 // Handle exact and commuted addresses.
92 if (Ptr1 == StorePtr1[i] && Ptr2 == StorePtr2[i])
93 return true;
94 if (Ptr2 == StorePtr1[i] && Ptr1 == StorePtr2[i])
95 return true;
96
97 // Okay, we don't have an exact match, if this is an indexed offset, see if
98 // we have overlap (which happens during fp->int conversion for example).
99 if (StorePtr2[i] == Ptr2) {
100 if (ConstantSDNode *StoreOffset = dyn_cast<ConstantSDNode>(StorePtr1[i]))
101 if (ConstantSDNode *LoadOffset = dyn_cast<ConstantSDNode>(Ptr1)) {
102 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check
103 // to see if the load and store actually overlap.
104 int StoreOffs = StoreOffset->getValue();
105 int LoadOffs = LoadOffset->getValue();
106 if (StoreOffs < LoadOffs) {
Chris Lattner64ce9642006-03-13 05:23:59 +0000107 if (int(StoreOffs+StoreSize[i]) > LoadOffs) return true;
Chris Lattner88d211f2006-03-12 09:13:49 +0000108 } else {
109 if (int(LoadOffs+LoadSize) > StoreOffs) return true;
110 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000111 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000112 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000113 }
114 return false;
115}
116
117/// getHazardType - We return hazard for any non-branch instruction that would
118/// terminate terminate the dispatch group. We turn NoopHazard for any
119/// instructions that wouldn't terminate the dispatch group that would cause a
120/// pipeline flush.
121HazardRecognizer::HazardType PPCHazardRecognizer970::
122getHazardType(SDNode *Node) {
Chris Lattner3faad492006-03-13 05:20:04 +0000123 bool isFirst, isSingle, isCracked, isLoad, isStore;
Chris Lattner88d211f2006-03-12 09:13:49 +0000124 PPCII::PPC970_Unit InstrType =
Chris Lattner3faad492006-03-13 05:20:04 +0000125 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked,
126 isLoad, isStore);
Chris Lattner88d211f2006-03-12 09:13:49 +0000127 if (InstrType == PPCII::PPC970_Pseudo) return NoHazard;
Chris Lattnerc6644182006-03-07 06:32:48 +0000128 unsigned Opcode = Node->getOpcode()-ISD::BUILTIN_OP_END;
129
Chris Lattner88d211f2006-03-12 09:13:49 +0000130 // We can only issue a PPC970_First/PPC970_Single instruction (such as
131 // crand/mtspr/etc) if this is the first cycle of the dispatch group.
Chris Lattner3faad492006-03-13 05:20:04 +0000132 if (NumIssued != 0 && (isFirst || isSingle))
Chris Lattner88d211f2006-03-12 09:13:49 +0000133 return Hazard;
134
Chris Lattner3faad492006-03-13 05:20:04 +0000135 // If this instruction is cracked into two ops by the decoder, we know that
136 // it is not a branch and that it cannot issue if 3 other instructions are
137 // already in the dispatch group.
138 if (isCracked && NumIssued > 2)
139 return Hazard;
140
Chris Lattnerc6644182006-03-07 06:32:48 +0000141 switch (InstrType) {
142 default: assert(0 && "Unknown instruction type!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000143 case PPCII::PPC970_FXU:
144 case PPCII::PPC970_LSU:
145 case PPCII::PPC970_FPU:
146 case PPCII::PPC970_VALU:
147 case PPCII::PPC970_VPERM:
148 // We can only issue a branch as the last instruction in a group.
149 if (NumIssued == 4) return Hazard;
150 break;
151 case PPCII::PPC970_CRU:
152 // We can only issue a CR instruction in the first two slots.
153 if (NumIssued >= 2) return Hazard;
154 break;
155 case PPCII::PPC970_BRU:
156 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000157 }
Chris Lattner3faad492006-03-13 05:20:04 +0000158
Chris Lattnerc6644182006-03-07 06:32:48 +0000159 // Do not allow MTCTR and BCTRL to be in the same dispatch group.
Nicolas Geoffray05c180b2007-02-27 13:10:41 +0000160 if (HasCTRSet && (Opcode == PPC::BCTRL_Macho || Opcode == PPC::BCTRL_ELF))
Chris Lattnerc6644182006-03-07 06:32:48 +0000161 return NoopHazard;
162
163 // If this is a load following a store, make sure it's not to the same or
164 // overlapping address.
Chris Lattner64ce9642006-03-13 05:23:59 +0000165 if (isLoad && NumStores) {
Chris Lattnerc6644182006-03-07 06:32:48 +0000166 unsigned LoadSize;
167 switch (Opcode) {
168 default: assert(0 && "Unknown load!");
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000169 case PPC::LBZ: case PPC::LBZU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000170 case PPC::LBZX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000171 case PPC::LBZ8: case PPC::LBZU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000172 case PPC::LBZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000173 case PPC::LVEBX:
174 LoadSize = 1;
175 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000176 case PPC::LHA: case PPC::LHAU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000177 case PPC::LHAX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000178 case PPC::LHZ: case PPC::LHZU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000179 case PPC::LHZX:
180 case PPC::LVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000181 case PPC::LHBRX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000182 case PPC::LHA8: case PPC::LHAU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000183 case PPC::LHAX8:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000184 case PPC::LHZ8: case PPC::LHZU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000185 case PPC::LHZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000186 LoadSize = 2;
187 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000188 case PPC::LFS: case PPC::LFSU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000189 case PPC::LFSX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000190 case PPC::LWZ: case PPC::LWZU:
Chris Lattner20463712006-03-07 07:14:55 +0000191 case PPC::LWZX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000192 case PPC::LWA:
193 case PPC::LWAX:
194 case PPC::LVEWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000195 case PPC::LWBRX:
Chris Lattner518f9c72006-07-14 04:42:02 +0000196 case PPC::LWZ8:
197 case PPC::LWZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000198 LoadSize = 4;
199 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000200 case PPC::LFD: case PPC::LFDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000201 case PPC::LFDX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000202 case PPC::LD: case PPC::LDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000203 case PPC::LDX:
204 LoadSize = 8;
205 break;
206 case PPC::LVX:
Bill Wendling399ea502007-09-05 23:47:12 +0000207 case PPC::LVXL:
Chris Lattner88d211f2006-03-12 09:13:49 +0000208 LoadSize = 16;
209 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000210 }
211
212 if (isLoadOfStoredAddress(LoadSize,
213 Node->getOperand(0), Node->getOperand(1)))
214 return NoopHazard;
215 }
216
217 return NoHazard;
218}
219
220void PPCHazardRecognizer970::EmitInstruction(SDNode *Node) {
Chris Lattner3faad492006-03-13 05:20:04 +0000221 bool isFirst, isSingle, isCracked, isLoad, isStore;
Chris Lattner88d211f2006-03-12 09:13:49 +0000222 PPCII::PPC970_Unit InstrType =
Chris Lattner3faad492006-03-13 05:20:04 +0000223 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked,
224 isLoad, isStore);
Chris Lattner88d211f2006-03-12 09:13:49 +0000225 if (InstrType == PPCII::PPC970_Pseudo) return;
Chris Lattnerc6644182006-03-07 06:32:48 +0000226 unsigned Opcode = Node->getOpcode()-ISD::BUILTIN_OP_END;
227
228 // Update structural hazard information.
229 if (Opcode == PPC::MTCTR) HasCTRSet = true;
230
231 // Track the address stored to.
Chris Lattner88d211f2006-03-12 09:13:49 +0000232 if (isStore) {
233 unsigned ThisStoreSize;
Chris Lattnerc6644182006-03-07 06:32:48 +0000234 switch (Opcode) {
235 default: assert(0 && "Unknown store instruction!");
Chris Lattner80df01d2006-11-16 00:57:19 +0000236 case PPC::STB: case PPC::STB8:
237 case PPC::STBU: case PPC::STBU8:
238 case PPC::STBX: case PPC::STBX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000239 case PPC::STVEBX:
240 ThisStoreSize = 1;
241 break;
Chris Lattner80df01d2006-11-16 00:57:19 +0000242 case PPC::STH: case PPC::STH8:
243 case PPC::STHU: case PPC::STHU8:
244 case PPC::STHX: case PPC::STHX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000245 case PPC::STVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000246 case PPC::STHBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000247 ThisStoreSize = 2;
248 break;
Chris Lattner80df01d2006-11-16 00:57:19 +0000249 case PPC::STFS:
250 case PPC::STFSU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000251 case PPC::STFSX:
Chris Lattner80df01d2006-11-16 00:57:19 +0000252 case PPC::STWX: case PPC::STWX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000253 case PPC::STWUX:
Chris Lattner80df01d2006-11-16 00:57:19 +0000254 case PPC::STW: case PPC::STW8:
255 case PPC::STWU: case PPC::STWU8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000256 case PPC::STVEWX:
257 case PPC::STFIWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000258 case PPC::STWBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000259 ThisStoreSize = 4;
260 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000261 case PPC::STD_32:
262 case PPC::STDX_32:
Chris Lattner80df01d2006-11-16 00:57:19 +0000263 case PPC::STD:
264 case PPC::STDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000265 case PPC::STFD:
266 case PPC::STFDX:
267 case PPC::STDX:
268 case PPC::STDUX:
269 ThisStoreSize = 8;
270 break;
271 case PPC::STVX:
Bill Wendling399ea502007-09-05 23:47:12 +0000272 case PPC::STVXL:
Chris Lattner88d211f2006-03-12 09:13:49 +0000273 ThisStoreSize = 16;
274 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000275 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000276
277 StoreSize[NumStores] = ThisStoreSize;
278 StorePtr1[NumStores] = Node->getOperand(1);
279 StorePtr2[NumStores] = Node->getOperand(2);
280 ++NumStores;
Chris Lattnerc6644182006-03-07 06:32:48 +0000281 }
282
Chris Lattner88d211f2006-03-12 09:13:49 +0000283 if (InstrType == PPCII::PPC970_BRU || isSingle)
284 NumIssued = 4; // Terminate a d-group.
Chris Lattnerc6644182006-03-07 06:32:48 +0000285 ++NumIssued;
286
Chris Lattner3faad492006-03-13 05:20:04 +0000287 // If this instruction is cracked into two ops by the decoder, remember that
288 // we issued two pieces.
289 if (isCracked)
290 ++NumIssued;
291
Chris Lattnerc6644182006-03-07 06:32:48 +0000292 if (NumIssued == 5)
293 EndDispatchGroup();
294}
295
296void PPCHazardRecognizer970::AdvanceCycle() {
297 assert(NumIssued < 5 && "Illegal dispatch group!");
298 ++NumIssued;
299 if (NumIssued == 5)
300 EndDispatchGroup();
301}
302
303void PPCHazardRecognizer970::EmitNoop() {
304 AdvanceCycle();
305}