blob: d2f81c673f92e26f5d28160aa5d5dfff21dcbd2f [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"
Chris Lattner88d211f2006-03-12 09:13:49 +000017#include "PPCInstrInfo.h"
Chris Lattnerc6644182006-03-07 06:32:48 +000018#include "llvm/Support/Debug.h"
19#include <iostream>
20using namespace llvm;
21
22
23//===----------------------------------------------------------------------===//
24// PowerPC 970 Hazard Recognizer
25//
Chris Lattner7ce64852006-03-07 06:44:19 +000026// This models the dispatch group formation of the PPC970 processor. Dispatch
Chris Lattner88d211f2006-03-12 09:13:49 +000027// groups are bundles of up to five instructions that can contain various mixes
28// of instructions. The PPC970 can dispatch a peak of 4 non-branch and one
29// branch instruction per-cycle.
Chris Lattner7ce64852006-03-07 06:44:19 +000030//
Chris Lattner88d211f2006-03-12 09:13:49 +000031// There are a number of restrictions to dispatch group formation: some
32// instructions can only be issued in the first slot of a dispatch group, & some
33// instructions fill an entire dispatch group. Additionally, only branches can
34// issue in the 5th (last) slot.
Chris Lattner7ce64852006-03-07 06:44:19 +000035//
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 Lattnerc6644182006-03-07 06:32:48 +000044// 1. Modeling of microcoded instructions.
Chris Lattner3faad492006-03-13 05:20:04 +000045// 2. Handling of serialized operations.
46// 3. Handling of the esoteric cases in "Resource-based Instruction Grouping".
Chris Lattnerc6644182006-03-07 06:32:48 +000047//
Chris Lattnerc6644182006-03-07 06:32:48 +000048
Chris Lattner88d211f2006-03-12 09:13:49 +000049PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetInstrInfo &tii)
50 : TII(tii) {
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000051 EndDispatchGroup();
52}
53
Chris Lattnerc6644182006-03-07 06:32:48 +000054void PPCHazardRecognizer970::EndDispatchGroup() {
55 DEBUG(std::cerr << "=== Start of dispatch group\n");
Chris Lattnerc6644182006-03-07 06:32:48 +000056 NumIssued = 0;
57
58 // Structural hazard info.
59 HasCTRSet = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000060 NumStores = 0;
Chris Lattnerc6644182006-03-07 06:32:48 +000061}
62
63
Chris Lattner88d211f2006-03-12 09:13:49 +000064PPCII::PPC970_Unit
65PPCHazardRecognizer970::GetInstrType(unsigned Opcode,
66 bool &isFirst, bool &isSingle,
Chris Lattner3faad492006-03-13 05:20:04 +000067 bool &isCracked,
68 bool &isLoad, bool &isStore) {
Chris Lattner88d211f2006-03-12 09:13:49 +000069 if (Opcode < ISD::BUILTIN_OP_END) {
Chris Lattner3faad492006-03-13 05:20:04 +000070 isFirst = isSingle = isCracked = isLoad = isStore = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000071 return PPCII::PPC970_Pseudo;
72 }
Chris Lattnerc6644182006-03-07 06:32:48 +000073 Opcode -= ISD::BUILTIN_OP_END;
74
Chris Lattner88d211f2006-03-12 09:13:49 +000075 const TargetInstrDescriptor &TID = TII.get(Opcode);
Chris Lattnerc6644182006-03-07 06:32:48 +000076
Chris Lattner88d211f2006-03-12 09:13:49 +000077 isLoad = TID.Flags & M_LOAD_FLAG;
78 isStore = TID.Flags & M_STORE_FLAG;
79
80 unsigned TSFlags = TID.TSFlags;
81
Chris Lattner3faad492006-03-13 05:20:04 +000082 isFirst = TSFlags & PPCII::PPC970_First;
83 isSingle = TSFlags & PPCII::PPC970_Single;
84 isCracked = TSFlags & PPCII::PPC970_Cracked;
Chris Lattner88d211f2006-03-12 09:13:49 +000085 return (PPCII::PPC970_Unit)(TSFlags & PPCII::PPC970_Mask);
Chris Lattnerc6644182006-03-07 06:32:48 +000086}
87
Chris Lattnerc6644182006-03-07 06:32:48 +000088/// isLoadOfStoredAddress - If we have a load from the previously stored pointer
89/// as indicated by StorePtr1/StorePtr2/StoreSize, return true.
90bool PPCHazardRecognizer970::
91isLoadOfStoredAddress(unsigned LoadSize, SDOperand Ptr1, SDOperand Ptr2) const {
Chris Lattner88d211f2006-03-12 09:13:49 +000092 for (unsigned i = 0, e = NumStores; i != e; ++i) {
93 // Handle exact and commuted addresses.
94 if (Ptr1 == StorePtr1[i] && Ptr2 == StorePtr2[i])
95 return true;
96 if (Ptr2 == StorePtr1[i] && Ptr1 == StorePtr2[i])
97 return true;
98
99 // Okay, we don't have an exact match, if this is an indexed offset, see if
100 // we have overlap (which happens during fp->int conversion for example).
101 if (StorePtr2[i] == Ptr2) {
102 if (ConstantSDNode *StoreOffset = dyn_cast<ConstantSDNode>(StorePtr1[i]))
103 if (ConstantSDNode *LoadOffset = dyn_cast<ConstantSDNode>(Ptr1)) {
104 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check
105 // to see if the load and store actually overlap.
106 int StoreOffs = StoreOffset->getValue();
107 int LoadOffs = LoadOffset->getValue();
108 if (StoreOffs < LoadOffs) {
Chris Lattner64ce9642006-03-13 05:23:59 +0000109 if (int(StoreOffs+StoreSize[i]) > LoadOffs) return true;
Chris Lattner88d211f2006-03-12 09:13:49 +0000110 } else {
111 if (int(LoadOffs+LoadSize) > StoreOffs) return true;
112 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000113 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000114 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000115 }
116 return false;
117}
118
119/// getHazardType - We return hazard for any non-branch instruction that would
120/// terminate terminate the dispatch group. We turn NoopHazard for any
121/// instructions that wouldn't terminate the dispatch group that would cause a
122/// pipeline flush.
123HazardRecognizer::HazardType PPCHazardRecognizer970::
124getHazardType(SDNode *Node) {
Chris Lattner3faad492006-03-13 05:20:04 +0000125 bool isFirst, isSingle, isCracked, isLoad, isStore;
Chris Lattner88d211f2006-03-12 09:13:49 +0000126 PPCII::PPC970_Unit InstrType =
Chris Lattner3faad492006-03-13 05:20:04 +0000127 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked,
128 isLoad, isStore);
Chris Lattner88d211f2006-03-12 09:13:49 +0000129 if (InstrType == PPCII::PPC970_Pseudo) return NoHazard;
Chris Lattnerc6644182006-03-07 06:32:48 +0000130 unsigned Opcode = Node->getOpcode()-ISD::BUILTIN_OP_END;
131
Chris Lattner88d211f2006-03-12 09:13:49 +0000132 // We can only issue a PPC970_First/PPC970_Single instruction (such as
133 // crand/mtspr/etc) if this is the first cycle of the dispatch group.
Chris Lattner3faad492006-03-13 05:20:04 +0000134 if (NumIssued != 0 && (isFirst || isSingle))
Chris Lattner88d211f2006-03-12 09:13:49 +0000135 return Hazard;
136
Chris Lattner3faad492006-03-13 05:20:04 +0000137 // If this instruction is cracked into two ops by the decoder, we know that
138 // it is not a branch and that it cannot issue if 3 other instructions are
139 // already in the dispatch group.
140 if (isCracked && NumIssued > 2)
141 return Hazard;
142
Chris Lattnerc6644182006-03-07 06:32:48 +0000143 switch (InstrType) {
144 default: assert(0 && "Unknown instruction type!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000145 case PPCII::PPC970_FXU:
146 case PPCII::PPC970_LSU:
147 case PPCII::PPC970_FPU:
148 case PPCII::PPC970_VALU:
149 case PPCII::PPC970_VPERM:
150 // We can only issue a branch as the last instruction in a group.
151 if (NumIssued == 4) return Hazard;
152 break;
153 case PPCII::PPC970_CRU:
154 // We can only issue a CR instruction in the first two slots.
155 if (NumIssued >= 2) return Hazard;
156 break;
157 case PPCII::PPC970_BRU:
158 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000159 }
Chris Lattner3faad492006-03-13 05:20:04 +0000160
Chris Lattnerc6644182006-03-07 06:32:48 +0000161 // Do not allow MTCTR and BCTRL to be in the same dispatch group.
162 if (HasCTRSet && Opcode == PPC::BCTRL)
163 return NoopHazard;
164
165 // If this is a load following a store, make sure it's not to the same or
166 // overlapping address.
Chris Lattner64ce9642006-03-13 05:23:59 +0000167 if (isLoad && NumStores) {
Chris Lattnerc6644182006-03-07 06:32:48 +0000168 unsigned LoadSize;
169 switch (Opcode) {
170 default: assert(0 && "Unknown load!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000171 case PPC::LBZ:
172 case PPC::LBZX:
173 case PPC::LVEBX:
174 LoadSize = 1;
175 break;
Chris Lattnerab5801c2006-03-07 16:19:46 +0000176 case PPC::LHA:
Chris Lattner88d211f2006-03-12 09:13:49 +0000177 case PPC::LHAX:
178 case PPC::LHZ:
179 case PPC::LHZX:
180 case PPC::LVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000181 case PPC::LHBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000182 LoadSize = 2;
183 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000184 case PPC::LFS:
Chris Lattner88d211f2006-03-12 09:13:49 +0000185 case PPC::LFSX:
186 case PPC::LWZ:
Chris Lattner20463712006-03-07 07:14:55 +0000187 case PPC::LWZX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000188 case PPC::LWZU:
189 case PPC::LWA:
190 case PPC::LWAX:
191 case PPC::LVEWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000192 case PPC::LWBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000193 LoadSize = 4;
194 break;
195 case PPC::LFD:
196 case PPC::LFDX:
197 case PPC::LD:
198 case PPC::LDX:
199 LoadSize = 8;
200 break;
201 case PPC::LVX:
202 LoadSize = 16;
203 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000204 }
205
206 if (isLoadOfStoredAddress(LoadSize,
207 Node->getOperand(0), Node->getOperand(1)))
208 return NoopHazard;
209 }
210
211 return NoHazard;
212}
213
214void PPCHazardRecognizer970::EmitInstruction(SDNode *Node) {
Chris Lattner3faad492006-03-13 05:20:04 +0000215 bool isFirst, isSingle, isCracked, isLoad, isStore;
Chris Lattner88d211f2006-03-12 09:13:49 +0000216 PPCII::PPC970_Unit InstrType =
Chris Lattner3faad492006-03-13 05:20:04 +0000217 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked,
218 isLoad, isStore);
Chris Lattner88d211f2006-03-12 09:13:49 +0000219 if (InstrType == PPCII::PPC970_Pseudo) return;
Chris Lattnerc6644182006-03-07 06:32:48 +0000220 unsigned Opcode = Node->getOpcode()-ISD::BUILTIN_OP_END;
221
222 // Update structural hazard information.
223 if (Opcode == PPC::MTCTR) HasCTRSet = true;
224
225 // Track the address stored to.
Chris Lattner88d211f2006-03-12 09:13:49 +0000226 if (isStore) {
227 unsigned ThisStoreSize;
Chris Lattnerc6644182006-03-07 06:32:48 +0000228 switch (Opcode) {
229 default: assert(0 && "Unknown store instruction!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000230 case PPC::STBX:
231 case PPC::STB:
232 case PPC::STVEBX:
233 ThisStoreSize = 1;
234 break;
235 case PPC::STHX:
236 case PPC::STH:
237 case PPC::STVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000238 case PPC::STHBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000239 ThisStoreSize = 2;
240 break;
Chris Lattnerb84225b2006-03-07 16:26:48 +0000241 case PPC::STFS:
Chris Lattner88d211f2006-03-12 09:13:49 +0000242 case PPC::STFSX:
Chris Lattnerab5801c2006-03-07 16:19:46 +0000243 case PPC::STWU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000244 case PPC::STWX:
245 case PPC::STWUX:
246 case PPC::STW:
247 case PPC::STVEWX:
248 case PPC::STFIWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000249 case PPC::STWBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000250 ThisStoreSize = 4;
251 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000252 case PPC::STD_32:
253 case PPC::STDX_32:
Chris Lattner88d211f2006-03-12 09:13:49 +0000254 case PPC::STD:
Chris Lattner88d211f2006-03-12 09:13:49 +0000255 case PPC::STFD:
256 case PPC::STFDX:
257 case PPC::STDX:
258 case PPC::STDUX:
259 ThisStoreSize = 8;
260 break;
261 case PPC::STVX:
262 ThisStoreSize = 16;
263 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000264 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000265
266 StoreSize[NumStores] = ThisStoreSize;
267 StorePtr1[NumStores] = Node->getOperand(1);
268 StorePtr2[NumStores] = Node->getOperand(2);
269 ++NumStores;
Chris Lattnerc6644182006-03-07 06:32:48 +0000270 }
271
Chris Lattner88d211f2006-03-12 09:13:49 +0000272 if (InstrType == PPCII::PPC970_BRU || isSingle)
273 NumIssued = 4; // Terminate a d-group.
Chris Lattnerc6644182006-03-07 06:32:48 +0000274 ++NumIssued;
275
Chris Lattner3faad492006-03-13 05:20:04 +0000276 // If this instruction is cracked into two ops by the decoder, remember that
277 // we issued two pieces.
278 if (isCracked)
279 ++NumIssued;
280
Chris Lattnerc6644182006-03-07 06:32:48 +0000281 if (NumIssued == 5)
282 EndDispatchGroup();
283}
284
285void PPCHazardRecognizer970::AdvanceCycle() {
286 assert(NumIssued < 5 && "Illegal dispatch group!");
287 ++NumIssued;
288 if (NumIssued == 5)
289 EndDispatchGroup();
290}
291
292void PPCHazardRecognizer970::EmitNoop() {
293 AdvanceCycle();
294}