blob: 3197fc85d71d48abee511fdcbd8c67212995039d [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.
30 // FIXME: Should something else be done?
31 return;
32 }
33
34 ScoreboardHazardRecognizer::EmitInstruction(SU);
35}
36
37//===----------------------------------------------------------------------===//
Chris Lattnerc6644182006-03-07 06:32:48 +000038// PowerPC 970 Hazard Recognizer
39//
Chris Lattner7ce64852006-03-07 06:44:19 +000040// This models the dispatch group formation of the PPC970 processor. Dispatch
Chris Lattner88d211f2006-03-12 09:13:49 +000041// groups are bundles of up to five instructions that can contain various mixes
Andrew Trick6e8f4c42010-12-24 04:28:06 +000042// of instructions. The PPC970 can dispatch a peak of 4 non-branch and one
Chris Lattner88d211f2006-03-12 09:13:49 +000043// branch instruction per-cycle.
Chris Lattner7ce64852006-03-07 06:44:19 +000044//
Chris Lattner88d211f2006-03-12 09:13:49 +000045// There are a number of restrictions to dispatch group formation: some
46// instructions can only be issued in the first slot of a dispatch group, & some
47// instructions fill an entire dispatch group. Additionally, only branches can
48// issue in the 5th (last) slot.
Chris Lattner7ce64852006-03-07 06:44:19 +000049//
50// Finally, there are a number of "structural" hazards on the PPC970. These
51// conditions cause large performance penalties due to misprediction, recovery,
52// and replay logic that has to happen. These cases include setting a CTR and
53// branching through it in the same dispatch group, and storing to an address,
54// then loading from the same address within a dispatch group. To avoid these
55// conditions, we insert no-op instructions when appropriate.
56//
Chris Lattnerc6644182006-03-07 06:32:48 +000057// FIXME: This is missing some significant cases:
Chris Lattnerc6644182006-03-07 06:32:48 +000058// 1. Modeling of microcoded instructions.
Chris Lattner3faad492006-03-13 05:20:04 +000059// 2. Handling of serialized operations.
60// 3. Handling of the esoteric cases in "Resource-based Instruction Grouping".
Chris Lattnerc6644182006-03-07 06:32:48 +000061//
Chris Lattnerc6644182006-03-07 06:32:48 +000062
Chris Lattner88d211f2006-03-12 09:13:49 +000063PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetInstrInfo &tii)
64 : TII(tii) {
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) {
Dan Gohmane8be6c62008-07-17 19:10:17 +000083 if ((int)Opcode >= 0) {
Chris Lattner3faad492006-03-13 05:20:04 +000084 isFirst = isSingle = isCracked = isLoad = isStore = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000085 return PPCII::PPC970_Pseudo;
86 }
Dan Gohmane8be6c62008-07-17 19:10:17 +000087 Opcode = ~Opcode;
Andrew Trick6e8f4c42010-12-24 04:28:06 +000088
Evan Chenge837dea2011-06-28 19:10:37 +000089 const MCInstrDesc &MCID = TII.get(Opcode);
Andrew Trick6e8f4c42010-12-24 04:28:06 +000090
Evan Chenge837dea2011-06-28 19:10:37 +000091 isLoad = MCID.mayLoad();
92 isStore = MCID.mayStore();
Andrew Trick6e8f4c42010-12-24 04:28:06 +000093
Evan Chenge837dea2011-06-28 19:10:37 +000094 uint64_t TSFlags = MCID.TSFlags;
Andrew Trick6e8f4c42010-12-24 04:28:06 +000095
Chris Lattner3faad492006-03-13 05:20:04 +000096 isFirst = TSFlags & PPCII::PPC970_First;
97 isSingle = TSFlags & PPCII::PPC970_Single;
98 isCracked = TSFlags & PPCII::PPC970_Cracked;
Chris Lattner88d211f2006-03-12 09:13:49 +000099 return (PPCII::PPC970_Unit)(TSFlags & PPCII::PPC970_Mask);
Chris Lattnerc6644182006-03-07 06:32:48 +0000100}
101
Chris Lattnerc6644182006-03-07 06:32:48 +0000102/// isLoadOfStoredAddress - If we have a load from the previously stored pointer
103/// as indicated by StorePtr1/StorePtr2/StoreSize, return true.
104bool PPCHazardRecognizer970::
Dan Gohman475871a2008-07-27 21:46:04 +0000105isLoadOfStoredAddress(unsigned LoadSize, SDValue Ptr1, SDValue Ptr2) const {
Chris Lattner88d211f2006-03-12 09:13:49 +0000106 for (unsigned i = 0, e = NumStores; i != e; ++i) {
107 // Handle exact and commuted addresses.
108 if (Ptr1 == StorePtr1[i] && Ptr2 == StorePtr2[i])
109 return true;
110 if (Ptr2 == StorePtr1[i] && Ptr1 == StorePtr2[i])
111 return true;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000112
Chris Lattner88d211f2006-03-12 09:13:49 +0000113 // Okay, we don't have an exact match, if this is an indexed offset, see if
114 // we have overlap (which happens during fp->int conversion for example).
115 if (StorePtr2[i] == Ptr2) {
116 if (ConstantSDNode *StoreOffset = dyn_cast<ConstantSDNode>(StorePtr1[i]))
117 if (ConstantSDNode *LoadOffset = dyn_cast<ConstantSDNode>(Ptr1)) {
118 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check
119 // to see if the load and store actually overlap.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000120 int StoreOffs = StoreOffset->getZExtValue();
121 int LoadOffs = LoadOffset->getZExtValue();
Chris Lattner88d211f2006-03-12 09:13:49 +0000122 if (StoreOffs < LoadOffs) {
Chris Lattner64ce9642006-03-13 05:23:59 +0000123 if (int(StoreOffs+StoreSize[i]) > LoadOffs) return true;
Chris Lattner88d211f2006-03-12 09:13:49 +0000124 } else {
125 if (int(LoadOffs+LoadSize) > StoreOffs) return true;
126 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000127 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000128 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000129 }
130 return false;
131}
132
133/// getHazardType - We return hazard for any non-branch instruction that would
Dan Gohmanf451cb82010-02-10 16:03:48 +0000134/// terminate the dispatch group. We turn NoopHazard for any
Chris Lattnerc6644182006-03-07 06:32:48 +0000135/// instructions that wouldn't terminate the dispatch group that would cause a
136/// pipeline flush.
Dan Gohmanfc54c552009-01-15 22:18:12 +0000137ScheduleHazardRecognizer::HazardType PPCHazardRecognizer970::
Andrew Trick2da8bc82010-12-24 05:03:26 +0000138getHazardType(SUnit *SU, int Stalls) {
139 assert(Stalls == 0 && "PPC hazards don't support scoreboard lookahead");
140
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000141 const SDNode *Node = SU->getNode()->getGluedMachineNode();
Chris Lattner3faad492006-03-13 05:20:04 +0000142 bool isFirst, isSingle, isCracked, isLoad, isStore;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000143 PPCII::PPC970_Unit InstrType =
Chris Lattner3faad492006-03-13 05:20:04 +0000144 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked,
145 isLoad, isStore);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000146 if (InstrType == PPCII::PPC970_Pseudo) return NoHazard;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000147 unsigned Opcode = Node->getMachineOpcode();
Chris Lattnerc6644182006-03-07 06:32:48 +0000148
Chris Lattner88d211f2006-03-12 09:13:49 +0000149 // We can only issue a PPC970_First/PPC970_Single instruction (such as
150 // crand/mtspr/etc) if this is the first cycle of the dispatch group.
Chris Lattner3faad492006-03-13 05:20:04 +0000151 if (NumIssued != 0 && (isFirst || isSingle))
Chris Lattner88d211f2006-03-12 09:13:49 +0000152 return Hazard;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000153
Chris Lattner3faad492006-03-13 05:20:04 +0000154 // If this instruction is cracked into two ops by the decoder, we know that
155 // it is not a branch and that it cannot issue if 3 other instructions are
156 // already in the dispatch group.
157 if (isCracked && NumIssued > 2)
158 return Hazard;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000159
Chris Lattnerc6644182006-03-07 06:32:48 +0000160 switch (InstrType) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000161 default: llvm_unreachable("Unknown instruction type!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000162 case PPCII::PPC970_FXU:
163 case PPCII::PPC970_LSU:
164 case PPCII::PPC970_FPU:
165 case PPCII::PPC970_VALU:
166 case PPCII::PPC970_VPERM:
167 // We can only issue a branch as the last instruction in a group.
168 if (NumIssued == 4) return Hazard;
169 break;
170 case PPCII::PPC970_CRU:
171 // We can only issue a CR instruction in the first two slots.
172 if (NumIssued >= 2) return Hazard;
173 break;
174 case PPCII::PPC970_BRU:
175 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000176 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000177
Chris Lattnerc6644182006-03-07 06:32:48 +0000178 // Do not allow MTCTR and BCTRL to be in the same dispatch group.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000179 if (HasCTRSet && (Opcode == PPC::BCTRL_Darwin || Opcode == PPC::BCTRL_SVR4))
Chris Lattnerc6644182006-03-07 06:32:48 +0000180 return NoopHazard;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000181
Chris Lattnerc6644182006-03-07 06:32:48 +0000182 // If this is a load following a store, make sure it's not to the same or
183 // overlapping address.
Chris Lattner64ce9642006-03-13 05:23:59 +0000184 if (isLoad && NumStores) {
Chris Lattnerc6644182006-03-07 06:32:48 +0000185 unsigned LoadSize;
186 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000187 default: llvm_unreachable("Unknown load!");
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000188 case PPC::LBZ: case PPC::LBZU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000189 case PPC::LBZX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000190 case PPC::LBZ8: case PPC::LBZU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000191 case PPC::LBZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000192 case PPC::LVEBX:
193 LoadSize = 1;
194 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000195 case PPC::LHA: case PPC::LHAU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000196 case PPC::LHAX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000197 case PPC::LHZ: case PPC::LHZU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000198 case PPC::LHZX:
199 case PPC::LVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000200 case PPC::LHBRX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000201 case PPC::LHA8: case PPC::LHAU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000202 case PPC::LHAX8:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000203 case PPC::LHZ8: case PPC::LHZU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000204 case PPC::LHZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000205 LoadSize = 2;
206 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000207 case PPC::LFS: case PPC::LFSU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000208 case PPC::LFSX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000209 case PPC::LWZ: case PPC::LWZU:
Chris Lattner20463712006-03-07 07:14:55 +0000210 case PPC::LWZX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000211 case PPC::LWA:
212 case PPC::LWAX:
213 case PPC::LVEWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000214 case PPC::LWBRX:
Chris Lattner518f9c72006-07-14 04:42:02 +0000215 case PPC::LWZ8:
216 case PPC::LWZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000217 LoadSize = 4;
218 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000219 case PPC::LFD: case PPC::LFDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000220 case PPC::LFDX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000221 case PPC::LD: case PPC::LDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000222 case PPC::LDX:
223 LoadSize = 8;
224 break;
225 case PPC::LVX:
Bill Wendling399ea502007-09-05 23:47:12 +0000226 case PPC::LVXL:
Chris Lattner88d211f2006-03-12 09:13:49 +0000227 LoadSize = 16;
228 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000229 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000230
231 if (isLoadOfStoredAddress(LoadSize,
Chris Lattnerc6644182006-03-07 06:32:48 +0000232 Node->getOperand(0), Node->getOperand(1)))
233 return NoopHazard;
234 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000235
Chris Lattnerc6644182006-03-07 06:32:48 +0000236 return NoHazard;
237}
238
Dan Gohmanfc54c552009-01-15 22:18:12 +0000239void PPCHazardRecognizer970::EmitInstruction(SUnit *SU) {
Chris Lattner29d8f0c2010-12-23 17:24:32 +0000240 const SDNode *Node = SU->getNode()->getGluedMachineNode();
Chris Lattner3faad492006-03-13 05:20:04 +0000241 bool isFirst, isSingle, isCracked, isLoad, isStore;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000242 PPCII::PPC970_Unit InstrType =
Chris Lattner3faad492006-03-13 05:20:04 +0000243 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked,
244 isLoad, isStore);
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000245 if (InstrType == PPCII::PPC970_Pseudo) return;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000246 unsigned Opcode = Node->getMachineOpcode();
Chris Lattnerc6644182006-03-07 06:32:48 +0000247
248 // Update structural hazard information.
Roman Divacky0c9b5592011-06-03 15:47:49 +0000249 if (Opcode == PPC::MTCTR || Opcode == PPC::MTCTR8) HasCTRSet = true;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000250
Chris Lattnerc6644182006-03-07 06:32:48 +0000251 // Track the address stored to.
Chris Lattner88d211f2006-03-12 09:13:49 +0000252 if (isStore) {
253 unsigned ThisStoreSize;
Chris Lattnerc6644182006-03-07 06:32:48 +0000254 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000255 default: llvm_unreachable("Unknown store instruction!");
Chris Lattner80df01d2006-11-16 00:57:19 +0000256 case PPC::STB: case PPC::STB8:
257 case PPC::STBU: case PPC::STBU8:
258 case PPC::STBX: case PPC::STBX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000259 case PPC::STVEBX:
260 ThisStoreSize = 1;
261 break;
Chris Lattner80df01d2006-11-16 00:57:19 +0000262 case PPC::STH: case PPC::STH8:
263 case PPC::STHU: case PPC::STHU8:
264 case PPC::STHX: case PPC::STHX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000265 case PPC::STVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000266 case PPC::STHBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000267 ThisStoreSize = 2;
268 break;
Chris Lattner80df01d2006-11-16 00:57:19 +0000269 case PPC::STFS:
270 case PPC::STFSU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000271 case PPC::STFSX:
Chris Lattner80df01d2006-11-16 00:57:19 +0000272 case PPC::STWX: case PPC::STWX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000273 case PPC::STWUX:
Chris Lattner80df01d2006-11-16 00:57:19 +0000274 case PPC::STW: case PPC::STW8:
Chris Lattner9fa200d2010-02-27 21:15:32 +0000275 case PPC::STWU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000276 case PPC::STVEWX:
277 case PPC::STFIWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000278 case PPC::STWBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000279 ThisStoreSize = 4;
280 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000281 case PPC::STD_32:
282 case PPC::STDX_32:
Chris Lattner80df01d2006-11-16 00:57:19 +0000283 case PPC::STD:
284 case PPC::STDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000285 case PPC::STFD:
286 case PPC::STFDX:
287 case PPC::STDX:
288 case PPC::STDUX:
289 ThisStoreSize = 8;
290 break;
291 case PPC::STVX:
Bill Wendling399ea502007-09-05 23:47:12 +0000292 case PPC::STVXL:
Chris Lattner88d211f2006-03-12 09:13:49 +0000293 ThisStoreSize = 16;
294 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000295 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000296
Chris Lattner88d211f2006-03-12 09:13:49 +0000297 StoreSize[NumStores] = ThisStoreSize;
298 StorePtr1[NumStores] = Node->getOperand(1);
299 StorePtr2[NumStores] = Node->getOperand(2);
300 ++NumStores;
Chris Lattnerc6644182006-03-07 06:32:48 +0000301 }
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000302
Chris Lattner88d211f2006-03-12 09:13:49 +0000303 if (InstrType == PPCII::PPC970_BRU || isSingle)
304 NumIssued = 4; // Terminate a d-group.
Chris Lattnerc6644182006-03-07 06:32:48 +0000305 ++NumIssued;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000306
Chris Lattner3faad492006-03-13 05:20:04 +0000307 // If this instruction is cracked into two ops by the decoder, remember that
308 // we issued two pieces.
309 if (isCracked)
310 ++NumIssued;
Andrew Trick6e8f4c42010-12-24 04:28:06 +0000311
Chris Lattnerc6644182006-03-07 06:32:48 +0000312 if (NumIssued == 5)
313 EndDispatchGroup();
314}
315
316void PPCHazardRecognizer970::AdvanceCycle() {
317 assert(NumIssued < 5 && "Illegal dispatch group!");
318 ++NumIssued;
319 if (NumIssued == 5)
320 EndDispatchGroup();
321}