blob: 6af7e0ffbc1a0d30dcc445517c9818fc485ad2b1 [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//===----------------------------------------------------------------------===//
25// PowerPC 970 Hazard Recognizer
26//
Chris Lattner7ce64852006-03-07 06:44:19 +000027// This models the dispatch group formation of the PPC970 processor. Dispatch
Chris Lattner88d211f2006-03-12 09:13:49 +000028// groups are bundles of up to five instructions that can contain various mixes
29// of instructions. The PPC970 can dispatch a peak of 4 non-branch and one
30// branch instruction per-cycle.
Chris Lattner7ce64852006-03-07 06:44:19 +000031//
Chris Lattner88d211f2006-03-12 09:13:49 +000032// There are a number of restrictions to dispatch group formation: some
33// instructions can only be issued in the first slot of a dispatch group, & some
34// instructions fill an entire dispatch group. Additionally, only branches can
35// issue in the 5th (last) slot.
Chris Lattner7ce64852006-03-07 06:44:19 +000036//
37// Finally, there are a number of "structural" hazards on the PPC970. These
38// conditions cause large performance penalties due to misprediction, recovery,
39// and replay logic that has to happen. These cases include setting a CTR and
40// branching through it in the same dispatch group, and storing to an address,
41// then loading from the same address within a dispatch group. To avoid these
42// conditions, we insert no-op instructions when appropriate.
43//
Chris Lattnerc6644182006-03-07 06:32:48 +000044// FIXME: This is missing some significant cases:
Chris Lattnerc6644182006-03-07 06:32:48 +000045// 1. Modeling of microcoded instructions.
Chris Lattner3faad492006-03-13 05:20:04 +000046// 2. Handling of serialized operations.
47// 3. Handling of the esoteric cases in "Resource-based Instruction Grouping".
Chris Lattnerc6644182006-03-07 06:32:48 +000048//
Chris Lattnerc6644182006-03-07 06:32:48 +000049
Chris Lattner88d211f2006-03-12 09:13:49 +000050PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetInstrInfo &tii)
51 : TII(tii) {
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000052 EndDispatchGroup();
53}
54
Chris Lattnerc6644182006-03-07 06:32:48 +000055void PPCHazardRecognizer970::EndDispatchGroup() {
Chris Lattner893e1c92009-08-23 06:49:22 +000056 DEBUG(errs() << "=== Start of dispatch group\n");
Chris Lattnerc6644182006-03-07 06:32:48 +000057 NumIssued = 0;
58
59 // Structural hazard info.
60 HasCTRSet = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000061 NumStores = 0;
Chris Lattnerc6644182006-03-07 06:32:48 +000062}
63
64
Chris Lattner88d211f2006-03-12 09:13:49 +000065PPCII::PPC970_Unit
66PPCHazardRecognizer970::GetInstrType(unsigned Opcode,
67 bool &isFirst, bool &isSingle,
Chris Lattner3faad492006-03-13 05:20:04 +000068 bool &isCracked,
69 bool &isLoad, bool &isStore) {
Dan Gohmane8be6c62008-07-17 19:10:17 +000070 if ((int)Opcode >= 0) {
Chris Lattner3faad492006-03-13 05:20:04 +000071 isFirst = isSingle = isCracked = isLoad = isStore = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000072 return PPCII::PPC970_Pseudo;
73 }
Dan Gohmane8be6c62008-07-17 19:10:17 +000074 Opcode = ~Opcode;
Chris Lattnerc6644182006-03-07 06:32:48 +000075
Chris Lattner749c6f62008-01-07 07:27:27 +000076 const TargetInstrDesc &TID = TII.get(Opcode);
Chris Lattnerc6644182006-03-07 06:32:48 +000077
Dan Gohman41474ba2008-12-03 02:30:17 +000078 isLoad = TID.mayLoad();
Chris Lattnerc17d69f2008-01-07 06:37:29 +000079 isStore = TID.mayStore();
Chris Lattner88d211f2006-03-12 09:13:49 +000080
81 unsigned TSFlags = TID.TSFlags;
82
Chris Lattner3faad492006-03-13 05:20:04 +000083 isFirst = TSFlags & PPCII::PPC970_First;
84 isSingle = TSFlags & PPCII::PPC970_Single;
85 isCracked = TSFlags & PPCII::PPC970_Cracked;
Chris Lattner88d211f2006-03-12 09:13:49 +000086 return (PPCII::PPC970_Unit)(TSFlags & PPCII::PPC970_Mask);
Chris Lattnerc6644182006-03-07 06:32:48 +000087}
88
Chris Lattnerc6644182006-03-07 06:32:48 +000089/// isLoadOfStoredAddress - If we have a load from the previously stored pointer
90/// as indicated by StorePtr1/StorePtr2/StoreSize, return true.
91bool PPCHazardRecognizer970::
Dan Gohman475871a2008-07-27 21:46:04 +000092isLoadOfStoredAddress(unsigned LoadSize, SDValue Ptr1, SDValue Ptr2) const {
Chris Lattner88d211f2006-03-12 09:13:49 +000093 for (unsigned i = 0, e = NumStores; i != e; ++i) {
94 // Handle exact and commuted addresses.
95 if (Ptr1 == StorePtr1[i] && Ptr2 == StorePtr2[i])
96 return true;
97 if (Ptr2 == StorePtr1[i] && Ptr1 == StorePtr2[i])
98 return true;
99
100 // Okay, we don't have an exact match, if this is an indexed offset, see if
101 // we have overlap (which happens during fp->int conversion for example).
102 if (StorePtr2[i] == Ptr2) {
103 if (ConstantSDNode *StoreOffset = dyn_cast<ConstantSDNode>(StorePtr1[i]))
104 if (ConstantSDNode *LoadOffset = dyn_cast<ConstantSDNode>(Ptr1)) {
105 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check
106 // to see if the load and store actually overlap.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000107 int StoreOffs = StoreOffset->getZExtValue();
108 int LoadOffs = LoadOffset->getZExtValue();
Chris Lattner88d211f2006-03-12 09:13:49 +0000109 if (StoreOffs < LoadOffs) {
Chris Lattner64ce9642006-03-13 05:23:59 +0000110 if (int(StoreOffs+StoreSize[i]) > LoadOffs) return true;
Chris Lattner88d211f2006-03-12 09:13:49 +0000111 } else {
112 if (int(LoadOffs+LoadSize) > StoreOffs) return true;
113 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000114 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000115 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000116 }
117 return false;
118}
119
120/// getHazardType - We return hazard for any non-branch instruction that would
121/// terminate terminate the dispatch group. We turn NoopHazard for any
122/// instructions that wouldn't terminate the dispatch group that would cause a
123/// pipeline flush.
Dan Gohmanfc54c552009-01-15 22:18:12 +0000124ScheduleHazardRecognizer::HazardType PPCHazardRecognizer970::
125getHazardType(SUnit *SU) {
126 const SDNode *Node = SU->getNode()->getFlaggedMachineNode();
Chris Lattner3faad492006-03-13 05:20:04 +0000127 bool isFirst, isSingle, isCracked, isLoad, isStore;
Chris Lattner88d211f2006-03-12 09:13:49 +0000128 PPCII::PPC970_Unit InstrType =
Chris Lattner3faad492006-03-13 05:20:04 +0000129 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked,
130 isLoad, isStore);
Chris Lattner88d211f2006-03-12 09:13:49 +0000131 if (InstrType == PPCII::PPC970_Pseudo) return NoHazard;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000132 unsigned Opcode = Node->getMachineOpcode();
Chris Lattnerc6644182006-03-07 06:32:48 +0000133
Chris Lattner88d211f2006-03-12 09:13:49 +0000134 // We can only issue a PPC970_First/PPC970_Single instruction (such as
135 // crand/mtspr/etc) if this is the first cycle of the dispatch group.
Chris Lattner3faad492006-03-13 05:20:04 +0000136 if (NumIssued != 0 && (isFirst || isSingle))
Chris Lattner88d211f2006-03-12 09:13:49 +0000137 return Hazard;
138
Chris Lattner3faad492006-03-13 05:20:04 +0000139 // If this instruction is cracked into two ops by the decoder, we know that
140 // it is not a branch and that it cannot issue if 3 other instructions are
141 // already in the dispatch group.
142 if (isCracked && NumIssued > 2)
143 return Hazard;
144
Chris Lattnerc6644182006-03-07 06:32:48 +0000145 switch (InstrType) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000146 default: llvm_unreachable("Unknown instruction type!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000147 case PPCII::PPC970_FXU:
148 case PPCII::PPC970_LSU:
149 case PPCII::PPC970_FPU:
150 case PPCII::PPC970_VALU:
151 case PPCII::PPC970_VPERM:
152 // We can only issue a branch as the last instruction in a group.
153 if (NumIssued == 4) return Hazard;
154 break;
155 case PPCII::PPC970_CRU:
156 // We can only issue a CR instruction in the first two slots.
157 if (NumIssued >= 2) return Hazard;
158 break;
159 case PPCII::PPC970_BRU:
160 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000161 }
Chris Lattner3faad492006-03-13 05:20:04 +0000162
Chris Lattnerc6644182006-03-07 06:32:48 +0000163 // Do not allow MTCTR and BCTRL to be in the same dispatch group.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000164 if (HasCTRSet && (Opcode == PPC::BCTRL_Darwin || Opcode == PPC::BCTRL_SVR4))
Chris Lattnerc6644182006-03-07 06:32:48 +0000165 return NoopHazard;
166
167 // If this is a load following a store, make sure it's not to the same or
168 // overlapping address.
Chris Lattner64ce9642006-03-13 05:23:59 +0000169 if (isLoad && NumStores) {
Chris Lattnerc6644182006-03-07 06:32:48 +0000170 unsigned LoadSize;
171 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000172 default: llvm_unreachable("Unknown load!");
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000173 case PPC::LBZ: case PPC::LBZU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000174 case PPC::LBZX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000175 case PPC::LBZ8: case PPC::LBZU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000176 case PPC::LBZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000177 case PPC::LVEBX:
178 LoadSize = 1;
179 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000180 case PPC::LHA: case PPC::LHAU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000181 case PPC::LHAX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000182 case PPC::LHZ: case PPC::LHZU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000183 case PPC::LHZX:
184 case PPC::LVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000185 case PPC::LHBRX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000186 case PPC::LHA8: case PPC::LHAU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000187 case PPC::LHAX8:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000188 case PPC::LHZ8: case PPC::LHZU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000189 case PPC::LHZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000190 LoadSize = 2;
191 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000192 case PPC::LFS: case PPC::LFSU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000193 case PPC::LFSX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000194 case PPC::LWZ: case PPC::LWZU:
Chris Lattner20463712006-03-07 07:14:55 +0000195 case PPC::LWZX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000196 case PPC::LWA:
197 case PPC::LWAX:
198 case PPC::LVEWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000199 case PPC::LWBRX:
Chris Lattner518f9c72006-07-14 04:42:02 +0000200 case PPC::LWZ8:
201 case PPC::LWZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000202 LoadSize = 4;
203 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000204 case PPC::LFD: case PPC::LFDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000205 case PPC::LFDX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000206 case PPC::LD: case PPC::LDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000207 case PPC::LDX:
208 LoadSize = 8;
209 break;
210 case PPC::LVX:
Bill Wendling399ea502007-09-05 23:47:12 +0000211 case PPC::LVXL:
Chris Lattner88d211f2006-03-12 09:13:49 +0000212 LoadSize = 16;
213 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000214 }
215
216 if (isLoadOfStoredAddress(LoadSize,
217 Node->getOperand(0), Node->getOperand(1)))
218 return NoopHazard;
219 }
220
221 return NoHazard;
222}
223
Dan Gohmanfc54c552009-01-15 22:18:12 +0000224void PPCHazardRecognizer970::EmitInstruction(SUnit *SU) {
225 const SDNode *Node = SU->getNode()->getFlaggedMachineNode();
Chris Lattner3faad492006-03-13 05:20:04 +0000226 bool isFirst, isSingle, isCracked, isLoad, isStore;
Chris Lattner88d211f2006-03-12 09:13:49 +0000227 PPCII::PPC970_Unit InstrType =
Chris Lattner3faad492006-03-13 05:20:04 +0000228 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked,
229 isLoad, isStore);
Chris Lattner88d211f2006-03-12 09:13:49 +0000230 if (InstrType == PPCII::PPC970_Pseudo) return;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000231 unsigned Opcode = Node->getMachineOpcode();
Chris Lattnerc6644182006-03-07 06:32:48 +0000232
233 // Update structural hazard information.
234 if (Opcode == PPC::MTCTR) HasCTRSet = true;
235
236 // Track the address stored to.
Chris Lattner88d211f2006-03-12 09:13:49 +0000237 if (isStore) {
238 unsigned ThisStoreSize;
Chris Lattnerc6644182006-03-07 06:32:48 +0000239 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000240 default: llvm_unreachable("Unknown store instruction!");
Chris Lattner80df01d2006-11-16 00:57:19 +0000241 case PPC::STB: case PPC::STB8:
242 case PPC::STBU: case PPC::STBU8:
243 case PPC::STBX: case PPC::STBX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000244 case PPC::STVEBX:
245 ThisStoreSize = 1;
246 break;
Chris Lattner80df01d2006-11-16 00:57:19 +0000247 case PPC::STH: case PPC::STH8:
248 case PPC::STHU: case PPC::STHU8:
249 case PPC::STHX: case PPC::STHX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000250 case PPC::STVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000251 case PPC::STHBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000252 ThisStoreSize = 2;
253 break;
Chris Lattner80df01d2006-11-16 00:57:19 +0000254 case PPC::STFS:
255 case PPC::STFSU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000256 case PPC::STFSX:
Chris Lattner80df01d2006-11-16 00:57:19 +0000257 case PPC::STWX: case PPC::STWX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000258 case PPC::STWUX:
Chris Lattner80df01d2006-11-16 00:57:19 +0000259 case PPC::STW: case PPC::STW8:
260 case PPC::STWU: case PPC::STWU8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000261 case PPC::STVEWX:
262 case PPC::STFIWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000263 case PPC::STWBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000264 ThisStoreSize = 4;
265 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000266 case PPC::STD_32:
267 case PPC::STDX_32:
Chris Lattner80df01d2006-11-16 00:57:19 +0000268 case PPC::STD:
269 case PPC::STDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000270 case PPC::STFD:
271 case PPC::STFDX:
272 case PPC::STDX:
273 case PPC::STDUX:
274 ThisStoreSize = 8;
275 break;
276 case PPC::STVX:
Bill Wendling399ea502007-09-05 23:47:12 +0000277 case PPC::STVXL:
Chris Lattner88d211f2006-03-12 09:13:49 +0000278 ThisStoreSize = 16;
279 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000280 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000281
282 StoreSize[NumStores] = ThisStoreSize;
283 StorePtr1[NumStores] = Node->getOperand(1);
284 StorePtr2[NumStores] = Node->getOperand(2);
285 ++NumStores;
Chris Lattnerc6644182006-03-07 06:32:48 +0000286 }
287
Chris Lattner88d211f2006-03-12 09:13:49 +0000288 if (InstrType == PPCII::PPC970_BRU || isSingle)
289 NumIssued = 4; // Terminate a d-group.
Chris Lattnerc6644182006-03-07 06:32:48 +0000290 ++NumIssued;
291
Chris Lattner3faad492006-03-13 05:20:04 +0000292 // If this instruction is cracked into two ops by the decoder, remember that
293 // we issued two pieces.
294 if (isCracked)
295 ++NumIssued;
296
Chris Lattnerc6644182006-03-07 06:32:48 +0000297 if (NumIssued == 5)
298 EndDispatchGroup();
299}
300
301void PPCHazardRecognizer970::AdvanceCycle() {
302 assert(NumIssued < 5 && "Illegal dispatch group!");
303 ++NumIssued;
304 if (NumIssued == 5)
305 EndDispatchGroup();
306}