blob: b52e9939235dadd657f5b95607364555ef85b929 [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.
45// 2. Handling of cracked instructions.
46// 3. Handling of serialized operations.
47// 4. Handling of the esoteric cases in "Resource-based Instruction Grouping",
48// e.g. integer divides that only execute in the second slot.
49//
Chris Lattnerc6644182006-03-07 06:32:48 +000050
Chris Lattner88d211f2006-03-12 09:13:49 +000051PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetInstrInfo &tii)
52 : TII(tii) {
Chris Lattnerb0d21ef2006-03-08 04:25:59 +000053 EndDispatchGroup();
54}
55
Chris Lattnerc6644182006-03-07 06:32:48 +000056void PPCHazardRecognizer970::EndDispatchGroup() {
57 DEBUG(std::cerr << "=== Start of dispatch group\n");
Chris Lattnerc6644182006-03-07 06:32:48 +000058 NumIssued = 0;
59
60 // Structural hazard info.
61 HasCTRSet = false;
Chris Lattner88d211f2006-03-12 09:13:49 +000062 NumStores = 0;
Chris Lattnerc6644182006-03-07 06:32:48 +000063}
64
65
Chris Lattner88d211f2006-03-12 09:13:49 +000066PPCII::PPC970_Unit
67PPCHazardRecognizer970::GetInstrType(unsigned Opcode,
68 bool &isFirst, bool &isSingle,
69 bool &isLoad, bool &isStore){
70 if (Opcode < ISD::BUILTIN_OP_END) {
71 isFirst = isSingle = isLoad = isStore = false;
72 return PPCII::PPC970_Pseudo;
73 }
Chris Lattnerc6644182006-03-07 06:32:48 +000074 Opcode -= ISD::BUILTIN_OP_END;
75
Chris Lattner88d211f2006-03-12 09:13:49 +000076 const TargetInstrDescriptor &TID = TII.get(Opcode);
Chris Lattnerc6644182006-03-07 06:32:48 +000077
Chris Lattner88d211f2006-03-12 09:13:49 +000078 isLoad = TID.Flags & M_LOAD_FLAG;
79 isStore = TID.Flags & M_STORE_FLAG;
80
81 unsigned TSFlags = TID.TSFlags;
82
83 isFirst = TSFlags & PPCII::PPC970_First;
84 isSingle = TSFlags & PPCII::PPC970_Single;
85 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) {
109 if (int(StoreOffs+StoreSize) > LoadOffs) return true;
110 } 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 Lattner88d211f2006-03-12 09:13:49 +0000125 bool isFirst, isSingle, isLoad, isStore;
126 PPCII::PPC970_Unit InstrType =
127 GetInstrType(Node->getOpcode(), isFirst, isSingle, isLoad, isStore);
128 if (InstrType == PPCII::PPC970_Pseudo) return NoHazard;
Chris Lattnerc6644182006-03-07 06:32:48 +0000129 unsigned Opcode = Node->getOpcode()-ISD::BUILTIN_OP_END;
130
Chris Lattner88d211f2006-03-12 09:13:49 +0000131 // We can only issue a PPC970_First/PPC970_Single instruction (such as
132 // crand/mtspr/etc) if this is the first cycle of the dispatch group.
133 if (NumIssued != 0 && (isFirst || isSingle) )
134 return Hazard;
135
Chris Lattnerc6644182006-03-07 06:32:48 +0000136 switch (InstrType) {
137 default: assert(0 && "Unknown instruction type!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000138 case PPCII::PPC970_FXU:
139 case PPCII::PPC970_LSU:
140 case PPCII::PPC970_FPU:
141 case PPCII::PPC970_VALU:
142 case PPCII::PPC970_VPERM:
143 // We can only issue a branch as the last instruction in a group.
144 if (NumIssued == 4) return Hazard;
145 break;
146 case PPCII::PPC970_CRU:
147 // We can only issue a CR instruction in the first two slots.
148 if (NumIssued >= 2) return Hazard;
149 break;
150 case PPCII::PPC970_BRU:
151 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000152 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000153
Chris Lattnerc6644182006-03-07 06:32:48 +0000154 // Do not allow MTCTR and BCTRL to be in the same dispatch group.
155 if (HasCTRSet && Opcode == PPC::BCTRL)
156 return NoopHazard;
157
158 // If this is a load following a store, make sure it's not to the same or
159 // overlapping address.
Chris Lattner88d211f2006-03-12 09:13:49 +0000160 if (isLoad && StoreSize) {
Chris Lattnerc6644182006-03-07 06:32:48 +0000161 unsigned LoadSize;
162 switch (Opcode) {
163 default: assert(0 && "Unknown load!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000164 case PPC::LBZ:
165 case PPC::LBZX:
166 case PPC::LVEBX:
167 LoadSize = 1;
168 break;
Chris Lattnerab5801c2006-03-07 16:19:46 +0000169 case PPC::LHA:
Chris Lattner88d211f2006-03-12 09:13:49 +0000170 case PPC::LHAX:
171 case PPC::LHZ:
172 case PPC::LHZX:
173 case PPC::LVEHX:
174 LoadSize = 2;
175 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000176 case PPC::LFS:
Chris Lattner88d211f2006-03-12 09:13:49 +0000177 case PPC::LFSX:
178 case PPC::LWZ:
Chris Lattner20463712006-03-07 07:14:55 +0000179 case PPC::LWZX:
Chris Lattner88d211f2006-03-12 09:13:49 +0000180 case PPC::LWZU:
181 case PPC::LWA:
182 case PPC::LWAX:
183 case PPC::LVEWX:
184 LoadSize = 4;
185 break;
186 case PPC::LFD:
187 case PPC::LFDX:
188 case PPC::LD:
189 case PPC::LDX:
190 LoadSize = 8;
191 break;
192 case PPC::LVX:
193 LoadSize = 16;
194 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000195 }
196
197 if (isLoadOfStoredAddress(LoadSize,
198 Node->getOperand(0), Node->getOperand(1)))
199 return NoopHazard;
200 }
201
202 return NoHazard;
203}
204
205void PPCHazardRecognizer970::EmitInstruction(SDNode *Node) {
Chris Lattner88d211f2006-03-12 09:13:49 +0000206 bool isFirst, isSingle, isLoad, isStore;
207 PPCII::PPC970_Unit InstrType =
208 GetInstrType(Node->getOpcode(), isFirst, isSingle, isLoad, isStore);
209 if (InstrType == PPCII::PPC970_Pseudo) return;
Chris Lattnerc6644182006-03-07 06:32:48 +0000210 unsigned Opcode = Node->getOpcode()-ISD::BUILTIN_OP_END;
211
212 // Update structural hazard information.
213 if (Opcode == PPC::MTCTR) HasCTRSet = true;
214
215 // Track the address stored to.
Chris Lattner88d211f2006-03-12 09:13:49 +0000216 if (isStore) {
217 unsigned ThisStoreSize;
Chris Lattnerc6644182006-03-07 06:32:48 +0000218 switch (Opcode) {
219 default: assert(0 && "Unknown store instruction!");
Chris Lattner88d211f2006-03-12 09:13:49 +0000220 case PPC::STBX:
221 case PPC::STB:
222 case PPC::STVEBX:
223 ThisStoreSize = 1;
224 break;
225 case PPC::STHX:
226 case PPC::STH:
227 case PPC::STVEHX:
228 ThisStoreSize = 2;
229 break;
Chris Lattnerb84225b2006-03-07 16:26:48 +0000230 case PPC::STFS:
Chris Lattner88d211f2006-03-12 09:13:49 +0000231 case PPC::STFSX:
Chris Lattnerab5801c2006-03-07 16:19:46 +0000232 case PPC::STWU:
Chris Lattner88d211f2006-03-12 09:13:49 +0000233 case PPC::STWX:
234 case PPC::STWUX:
235 case PPC::STW:
236 case PPC::STVEWX:
237 case PPC::STFIWX:
238 ThisStoreSize = 4;
239 break;
240 case PPC::STD:
241 case PPC::STDU:
242 case PPC::STFD:
243 case PPC::STFDX:
244 case PPC::STDX:
245 case PPC::STDUX:
246 ThisStoreSize = 8;
247 break;
248 case PPC::STVX:
249 ThisStoreSize = 16;
250 break;
Chris Lattnerc6644182006-03-07 06:32:48 +0000251 }
Chris Lattner88d211f2006-03-12 09:13:49 +0000252
253 StoreSize[NumStores] = ThisStoreSize;
254 StorePtr1[NumStores] = Node->getOperand(1);
255 StorePtr2[NumStores] = Node->getOperand(2);
256 ++NumStores;
Chris Lattnerc6644182006-03-07 06:32:48 +0000257 }
258
Chris Lattner88d211f2006-03-12 09:13:49 +0000259 if (InstrType == PPCII::PPC970_BRU || isSingle)
260 NumIssued = 4; // Terminate a d-group.
Chris Lattnerc6644182006-03-07 06:32:48 +0000261 ++NumIssued;
262
263 if (NumIssued == 5)
264 EndDispatchGroup();
265}
266
267void PPCHazardRecognizer970::AdvanceCycle() {
268 assert(NumIssued < 5 && "Illegal dispatch group!");
269 ++NumIssued;
270 if (NumIssued == 5)
271 EndDispatchGroup();
272}
273
274void PPCHazardRecognizer970::EmitNoop() {
275 AdvanceCycle();
276}