blob: ec3e757651f40eac13124c4f6986ec4d04c34201 [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"
Chris Lattnerc6644182006-03-07 06:32:48 +000020using namespace llvm;
21
Chris Lattnerc6644182006-03-07 06:32:48 +000022//===----------------------------------------------------------------------===//
23// PowerPC 970 Hazard Recognizer
24//
Chris Lattner7ce64852006-03-07 06:44:19 +000025// This models the dispatch group formation of the PPC970 processor. Dispatch
Chris Lattner88d211f2006-03-12 09:13:49 +000026// groups are bundles of up to five instructions that can contain various mixes
27// of instructions. The PPC970 can dispatch a peak of 4 non-branch and one
28// branch instruction per-cycle.
Chris Lattner7ce64852006-03-07 06:44:19 +000029//
Chris Lattner88d211f2006-03-12 09:13:49 +000030// There are a number of restrictions to dispatch group formation: some
31// instructions can only be issued in the first slot of a dispatch group, & some
32// instructions fill an entire dispatch group. Additionally, only branches can
33// issue in the 5th (last) slot.
Chris Lattner7ce64852006-03-07 06:44:19 +000034//
35// Finally, there are a number of "structural" hazards on the PPC970. These
36// conditions cause large performance penalties due to misprediction, recovery,
37// and replay logic that has to happen. These cases include setting a CTR and
38// branching through it in the same dispatch group, and storing to an address,
39// then loading from the same address within a dispatch group. To avoid these
40// conditions, we insert no-op instructions when appropriate.
41//
Chris Lattnerc6644182006-03-07 06:32:48 +000042// FIXME: This is missing some significant cases:
Chris Lattnerc6644182006-03-07 06:32:48 +000043// 1. Modeling of microcoded instructions.
Chris Lattner3faad492006-03-13 05:20:04 +000044// 2. Handling of serialized operations.
45// 3. Handling of the esoteric cases in "Resource-based Instruction Grouping".
Chris Lattnerc6644182006-03-07 06:32:48 +000046//
Chris Lattnerc6644182006-03-07 06:32:48 +000047
Chris Lattner88d211f2006-03-12 09:13:49 +000048PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetInstrInfo &tii)
49 : TII(tii) {
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000050 EndDispatchGroup();
51}
52
Chris Lattnerc6644182006-03-07 06:32:48 +000053void PPCHazardRecognizer970::EndDispatchGroup() {
Bill Wendlingf5da1332006-12-07 22:21:48 +000054 DOUT << "=== Start of dispatch group\n";
Chris Lattnerc6644182006-03-07 06:32:48 +000055 NumIssued = 0;
56
57 // Structural hazard info.
58 HasCTRSet = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000059 NumStores = 0;
Chris Lattnerc6644182006-03-07 06:32:48 +000060}
61
62
Chris Lattner88d211f2006-03-12 09:13:49 +000063PPCII::PPC970_Unit
64PPCHazardRecognizer970::GetInstrType(unsigned Opcode,
65 bool &isFirst, bool &isSingle,
Chris Lattner3faad492006-03-13 05:20:04 +000066 bool &isCracked,
67 bool &isLoad, bool &isStore) {
Dan Gohmane8be6c62008-07-17 19:10:17 +000068 if ((int)Opcode >= 0) {
Chris Lattner3faad492006-03-13 05:20:04 +000069 isFirst = isSingle = isCracked = isLoad = isStore = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000070 return PPCII::PPC970_Pseudo;
71 }
Dan Gohmane8be6c62008-07-17 19:10:17 +000072 Opcode = ~Opcode;
Chris Lattnerc6644182006-03-07 06:32:48 +000073
Chris Lattner749c6f62008-01-07 07:27:27 +000074 const TargetInstrDesc &TID = TII.get(Opcode);
Chris Lattnerc6644182006-03-07 06:32:48 +000075
Dan Gohman41474ba2008-12-03 02:30:17 +000076 isLoad = TID.mayLoad();
Chris Lattnerc17d69f2008-01-07 06:37:29 +000077 isStore = TID.mayStore();
Chris Lattner88d211f2006-03-12 09:13:49 +000078
79 unsigned TSFlags = TID.TSFlags;
80
Chris Lattner3faad492006-03-13 05:20:04 +000081 isFirst = TSFlags & PPCII::PPC970_First;
82 isSingle = TSFlags & PPCII::PPC970_Single;
83 isCracked = TSFlags & PPCII::PPC970_Cracked;
Chris Lattner88d211f2006-03-12 09:13:49 +000084 return (PPCII::PPC970_Unit)(TSFlags & PPCII::PPC970_Mask);
Chris Lattnerc6644182006-03-07 06:32:48 +000085}
86
Chris Lattnerc6644182006-03-07 06:32:48 +000087/// isLoadOfStoredAddress - If we have a load from the previously stored pointer
88/// as indicated by StorePtr1/StorePtr2/StoreSize, return true.
89bool PPCHazardRecognizer970::
Dan Gohman475871a2008-07-27 21:46:04 +000090isLoadOfStoredAddress(unsigned LoadSize, SDValue Ptr1, SDValue Ptr2) const {
Chris Lattner88d211f2006-03-12 09:13:49 +000091 for (unsigned i = 0, e = NumStores; i != e; ++i) {
92 // Handle exact and commuted addresses.
93 if (Ptr1 == StorePtr1[i] && Ptr2 == StorePtr2[i])
94 return true;
95 if (Ptr2 == StorePtr1[i] && Ptr1 == StorePtr2[i])
96 return true;
97
98 // Okay, we don't have an exact match, if this is an indexed offset, see if
99 // we have overlap (which happens during fp->int conversion for example).
100 if (StorePtr2[i] == Ptr2) {
101 if (ConstantSDNode *StoreOffset = dyn_cast<ConstantSDNode>(StorePtr1[i]))
102 if (ConstantSDNode *LoadOffset = dyn_cast<ConstantSDNode>(Ptr1)) {
103 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check
104 // to see if the load and store actually overlap.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000105 int StoreOffs = StoreOffset->getZExtValue();
106 int LoadOffs = LoadOffset->getZExtValue();
Chris Lattner88d211f2006-03-12 09:13:49 +0000107 if (StoreOffs < LoadOffs) {
Chris Lattner64ce9642006-03-13 05:23:59 +0000108 if (int(StoreOffs+StoreSize[i]) > LoadOffs) return true;
Chris Lattner88d211f2006-03-12 09:13:49 +0000109 } else {
110 if (int(LoadOffs+LoadSize) > StoreOffs) return true;
111 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000112 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000113 }
Chris Lattnerc6644182006-03-07 06:32:48 +0000114 }
115 return false;
116}
117
118/// getHazardType - We return hazard for any non-branch instruction that would
119/// terminate terminate the dispatch group. We turn NoopHazard for any
120/// instructions that wouldn't terminate the dispatch group that would cause a
121/// pipeline flush.
Dan Gohmanfc54c552009-01-15 22:18:12 +0000122ScheduleHazardRecognizer::HazardType PPCHazardRecognizer970::
123getHazardType(SUnit *SU) {
124 const SDNode *Node = SU->getNode()->getFlaggedMachineNode();
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;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000130 unsigned Opcode = Node->getMachineOpcode();
Chris Lattnerc6644182006-03-07 06:32:48 +0000131
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.
Tilmann Scheller2a9ddfb2009-07-03 06:47:08 +0000162 if (HasCTRSet && (Opcode == PPC::BCTRL_Darwin || Opcode == PPC::BCTRL_SVR4))
Chris Lattnerc6644182006-03-07 06:32:48 +0000163 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 Lattnerc9dcf282006-11-13 20:11:06 +0000171 case PPC::LBZ: case PPC::LBZU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000172 case PPC::LBZX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000173 case PPC::LBZ8: case PPC::LBZU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000174 case PPC::LBZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000175 case PPC::LVEBX:
176 LoadSize = 1;
177 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000178 case PPC::LHA: case PPC::LHAU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000179 case PPC::LHAX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000180 case PPC::LHZ: case PPC::LHZU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000181 case PPC::LHZX:
182 case PPC::LVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000183 case PPC::LHBRX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000184 case PPC::LHA8: case PPC::LHAU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000185 case PPC::LHAX8:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000186 case PPC::LHZ8: case PPC::LHZU8:
Chris Lattner518f9c72006-07-14 04:42:02 +0000187 case PPC::LHZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000188 LoadSize = 2;
189 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000190 case PPC::LFS: case PPC::LFSU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000191 case PPC::LFSX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000192 case PPC::LWZ: case PPC::LWZU:
Chris Lattner20463712006-03-07 07:14:55 +0000193 case PPC::LWZX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000194 case PPC::LWA:
195 case PPC::LWAX:
196 case PPC::LVEWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000197 case PPC::LWBRX:
Chris Lattner518f9c72006-07-14 04:42:02 +0000198 case PPC::LWZ8:
199 case PPC::LWZX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000200 LoadSize = 4;
201 break;
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000202 case PPC::LFD: case PPC::LFDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000203 case PPC::LFDX:
Chris Lattnerc9dcf282006-11-13 20:11:06 +0000204 case PPC::LD: case PPC::LDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000205 case PPC::LDX:
206 LoadSize = 8;
207 break;
208 case PPC::LVX:
Bill Wendling399ea502007-09-05 23:47:12 +0000209 case PPC::LVXL:
Chris Lattner88d211f2006-03-12 09:13:49 +0000210 LoadSize = 16;
211 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000212 }
213
214 if (isLoadOfStoredAddress(LoadSize,
215 Node->getOperand(0), Node->getOperand(1)))
216 return NoopHazard;
217 }
218
219 return NoHazard;
220}
221
Dan Gohmanfc54c552009-01-15 22:18:12 +0000222void PPCHazardRecognizer970::EmitInstruction(SUnit *SU) {
223 const SDNode *Node = SU->getNode()->getFlaggedMachineNode();
Chris Lattner3faad492006-03-13 05:20:04 +0000224 bool isFirst, isSingle, isCracked, isLoad, isStore;
Chris Lattner88d211f2006-03-12 09:13:49 +0000225 PPCII::PPC970_Unit InstrType =
Chris Lattner3faad492006-03-13 05:20:04 +0000226 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked,
227 isLoad, isStore);
Chris Lattner88d211f2006-03-12 09:13:49 +0000228 if (InstrType == PPCII::PPC970_Pseudo) return;
Dan Gohmane8be6c62008-07-17 19:10:17 +0000229 unsigned Opcode = Node->getMachineOpcode();
Chris Lattnerc6644182006-03-07 06:32:48 +0000230
231 // Update structural hazard information.
232 if (Opcode == PPC::MTCTR) HasCTRSet = true;
233
234 // Track the address stored to.
Chris Lattner88d211f2006-03-12 09:13:49 +0000235 if (isStore) {
236 unsigned ThisStoreSize;
Chris Lattnerc6644182006-03-07 06:32:48 +0000237 switch (Opcode) {
238 default: assert(0 && "Unknown store instruction!");
Chris Lattner80df01d2006-11-16 00:57:19 +0000239 case PPC::STB: case PPC::STB8:
240 case PPC::STBU: case PPC::STBU8:
241 case PPC::STBX: case PPC::STBX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000242 case PPC::STVEBX:
243 ThisStoreSize = 1;
244 break;
Chris Lattner80df01d2006-11-16 00:57:19 +0000245 case PPC::STH: case PPC::STH8:
246 case PPC::STHU: case PPC::STHU8:
247 case PPC::STHX: case PPC::STHX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000248 case PPC::STVEHX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000249 case PPC::STHBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000250 ThisStoreSize = 2;
251 break;
Chris Lattner80df01d2006-11-16 00:57:19 +0000252 case PPC::STFS:
253 case PPC::STFSU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000254 case PPC::STFSX:
Chris Lattner80df01d2006-11-16 00:57:19 +0000255 case PPC::STWX: case PPC::STWX8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000256 case PPC::STWUX:
Chris Lattner80df01d2006-11-16 00:57:19 +0000257 case PPC::STW: case PPC::STW8:
258 case PPC::STWU: case PPC::STWU8:
Chris Lattner88d211f2006-03-12 09:13:49 +0000259 case PPC::STVEWX:
260 case PPC::STFIWX:
Chris Lattnerd9989382006-07-10 20:56:58 +0000261 case PPC::STWBRX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000262 ThisStoreSize = 4;
263 break;
Chris Lattnerecfe55e2006-03-22 05:30:33 +0000264 case PPC::STD_32:
265 case PPC::STDX_32:
Chris Lattner80df01d2006-11-16 00:57:19 +0000266 case PPC::STD:
267 case PPC::STDU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000268 case PPC::STFD:
269 case PPC::STFDX:
270 case PPC::STDX:
271 case PPC::STDUX:
272 ThisStoreSize = 8;
273 break;
274 case PPC::STVX:
Bill Wendling399ea502007-09-05 23:47:12 +0000275 case PPC::STVXL:
Chris Lattner88d211f2006-03-12 09:13:49 +0000276 ThisStoreSize = 16;
277 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000278 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000279
280 StoreSize[NumStores] = ThisStoreSize;
281 StorePtr1[NumStores] = Node->getOperand(1);
282 StorePtr2[NumStores] = Node->getOperand(2);
283 ++NumStores;
Chris Lattnerc6644182006-03-07 06:32:48 +0000284 }
285
Chris Lattner88d211f2006-03-12 09:13:49 +0000286 if (InstrType == PPCII::PPC970_BRU || isSingle)
287 NumIssued = 4; // Terminate a d-group.
Chris Lattnerc6644182006-03-07 06:32:48 +0000288 ++NumIssued;
289
Chris Lattner3faad492006-03-13 05:20:04 +0000290 // If this instruction is cracked into two ops by the decoder, remember that
291 // we issued two pieces.
292 if (isCracked)
293 ++NumIssued;
294
Chris Lattnerc6644182006-03-07 06:32:48 +0000295 if (NumIssued == 5)
296 EndDispatchGroup();
297}
298
299void PPCHazardRecognizer970::AdvanceCycle() {
300 assert(NumIssued < 5 && "Illegal dispatch group!");
301 ++NumIssued;
302 if (NumIssued == 5)
303 EndDispatchGroup();
304}