LLVM API Documentation
00001 //===-- PPCHazardRecognizers.cpp - PowerPC Hazard Recognizer Impls --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements hazard recognizers for scheduling on PowerPC processors. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "pre-RA-sched" 00015 #include "PPCHazardRecognizers.h" 00016 #include "PPC.h" 00017 #include "PPCInstrInfo.h" 00018 #include "llvm/CodeGen/ScheduleDAG.h" 00019 #include "llvm/Support/Debug.h" 00020 #include "llvm/Support/ErrorHandling.h" 00021 using namespace llvm; 00022 00023 //===----------------------------------------------------------------------===// 00024 // PowerPC 970 Hazard Recognizer 00025 // 00026 // This models the dispatch group formation of the PPC970 processor. Dispatch 00027 // groups are bundles of up to five instructions that can contain various mixes 00028 // of instructions. The PPC970 can dispatch a peak of 4 non-branch and one 00029 // branch instruction per-cycle. 00030 // 00031 // There are a number of restrictions to dispatch group formation: some 00032 // instructions can only be issued in the first slot of a dispatch group, & some 00033 // instructions fill an entire dispatch group. Additionally, only branches can 00034 // issue in the 5th (last) slot. 00035 // 00036 // Finally, there are a number of "structural" hazards on the PPC970. These 00037 // conditions cause large performance penalties due to misprediction, recovery, 00038 // and replay logic that has to happen. These cases include setting a CTR and 00039 // branching through it in the same dispatch group, and storing to an address, 00040 // then loading from the same address within a dispatch group. To avoid these 00041 // conditions, we insert no-op instructions when appropriate. 00042 // 00043 // FIXME: This is missing some significant cases: 00044 // 1. Modeling of microcoded instructions. 00045 // 2. Handling of serialized operations. 00046 // 3. Handling of the esoteric cases in "Resource-based Instruction Grouping". 00047 // 00048 00049 PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetInstrInfo &tii) 00050 : TII(tii) { 00051 EndDispatchGroup(); 00052 } 00053 00054 void PPCHazardRecognizer970::EndDispatchGroup() { 00055 DOUT << "=== Start of dispatch group\n"; 00056 NumIssued = 0; 00057 00058 // Structural hazard info. 00059 HasCTRSet = false; 00060 NumStores = 0; 00061 } 00062 00063 00064 PPCII::PPC970_Unit 00065 PPCHazardRecognizer970::GetInstrType(unsigned Opcode, 00066 bool &isFirst, bool &isSingle, 00067 bool &isCracked, 00068 bool &isLoad, bool &isStore) { 00069 if ((int)Opcode >= 0) { 00070 isFirst = isSingle = isCracked = isLoad = isStore = false; 00071 return PPCII::PPC970_Pseudo; 00072 } 00073 Opcode = ~Opcode; 00074 00075 const TargetInstrDesc &TID = TII.get(Opcode); 00076 00077 isLoad = TID.mayLoad(); 00078 isStore = TID.mayStore(); 00079 00080 unsigned TSFlags = TID.TSFlags; 00081 00082 isFirst = TSFlags & PPCII::PPC970_First; 00083 isSingle = TSFlags & PPCII::PPC970_Single; 00084 isCracked = TSFlags & PPCII::PPC970_Cracked; 00085 return (PPCII::PPC970_Unit)(TSFlags & PPCII::PPC970_Mask); 00086 } 00087 00088 /// isLoadOfStoredAddress - If we have a load from the previously stored pointer 00089 /// as indicated by StorePtr1/StorePtr2/StoreSize, return true. 00090 bool PPCHazardRecognizer970:: 00091 isLoadOfStoredAddress(unsigned LoadSize, SDValue Ptr1, SDValue Ptr2) const { 00092 for (unsigned i = 0, e = NumStores; i != e; ++i) { 00093 // Handle exact and commuted addresses. 00094 if (Ptr1 == StorePtr1[i] && Ptr2 == StorePtr2[i]) 00095 return true; 00096 if (Ptr2 == StorePtr1[i] && Ptr1 == StorePtr2[i]) 00097 return true; 00098 00099 // Okay, we don't have an exact match, if this is an indexed offset, see if 00100 // we have overlap (which happens during fp->int conversion for example). 00101 if (StorePtr2[i] == Ptr2) { 00102 if (ConstantSDNode *StoreOffset = dyn_cast<ConstantSDNode>(StorePtr1[i])) 00103 if (ConstantSDNode *LoadOffset = dyn_cast<ConstantSDNode>(Ptr1)) { 00104 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check 00105 // to see if the load and store actually overlap. 00106 int StoreOffs = StoreOffset->getZExtValue(); 00107 int LoadOffs = LoadOffset->getZExtValue(); 00108 if (StoreOffs < LoadOffs) { 00109 if (int(StoreOffs+StoreSize[i]) > LoadOffs) return true; 00110 } else { 00111 if (int(LoadOffs+LoadSize) > StoreOffs) return true; 00112 } 00113 } 00114 } 00115 } 00116 return false; 00117 } 00118 00119 /// getHazardType - We return hazard for any non-branch instruction that would 00120 /// terminate terminate the dispatch group. We turn NoopHazard for any 00121 /// instructions that wouldn't terminate the dispatch group that would cause a 00122 /// pipeline flush. 00123 ScheduleHazardRecognizer::HazardType PPCHazardRecognizer970:: 00124 getHazardType(SUnit *SU) { 00125 const SDNode *Node = SU->getNode()->getFlaggedMachineNode(); 00126 bool isFirst, isSingle, isCracked, isLoad, isStore; 00127 PPCII::PPC970_Unit InstrType = 00128 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked, 00129 isLoad, isStore); 00130 if (InstrType == PPCII::PPC970_Pseudo) return NoHazard; 00131 unsigned Opcode = Node->getMachineOpcode(); 00132 00133 // We can only issue a PPC970_First/PPC970_Single instruction (such as 00134 // crand/mtspr/etc) if this is the first cycle of the dispatch group. 00135 if (NumIssued != 0 && (isFirst || isSingle)) 00136 return Hazard; 00137 00138 // If this instruction is cracked into two ops by the decoder, we know that 00139 // it is not a branch and that it cannot issue if 3 other instructions are 00140 // already in the dispatch group. 00141 if (isCracked && NumIssued > 2) 00142 return Hazard; 00143 00144 switch (InstrType) { 00145 default: llvm_unreachable("Unknown instruction type!"); 00146 case PPCII::PPC970_FXU: 00147 case PPCII::PPC970_LSU: 00148 case PPCII::PPC970_FPU: 00149 case PPCII::PPC970_VALU: 00150 case PPCII::PPC970_VPERM: 00151 // We can only issue a branch as the last instruction in a group. 00152 if (NumIssued == 4) return Hazard; 00153 break; 00154 case PPCII::PPC970_CRU: 00155 // We can only issue a CR instruction in the first two slots. 00156 if (NumIssued >= 2) return Hazard; 00157 break; 00158 case PPCII::PPC970_BRU: 00159 break; 00160 } 00161 00162 // Do not allow MTCTR and BCTRL to be in the same dispatch group. 00163 if (HasCTRSet && (Opcode == PPC::BCTRL_Darwin || Opcode == PPC::BCTRL_SVR4)) 00164 return NoopHazard; 00165 00166 // If this is a load following a store, make sure it's not to the same or 00167 // overlapping address. 00168 if (isLoad && NumStores) { 00169 unsigned LoadSize; 00170 switch (Opcode) { 00171 default: llvm_unreachable("Unknown load!"); 00172 case PPC::LBZ: case PPC::LBZU: 00173 case PPC::LBZX: 00174 case PPC::LBZ8: case PPC::LBZU8: 00175 case PPC::LBZX8: 00176 case PPC::LVEBX: 00177 LoadSize = 1; 00178 break; 00179 case PPC::LHA: case PPC::LHAU: 00180 case PPC::LHAX: 00181 case PPC::LHZ: case PPC::LHZU: 00182 case PPC::LHZX: 00183 case PPC::LVEHX: 00184 case PPC::LHBRX: 00185 case PPC::LHA8: case PPC::LHAU8: 00186 case PPC::LHAX8: 00187 case PPC::LHZ8: case PPC::LHZU8: 00188 case PPC::LHZX8: 00189 LoadSize = 2; 00190 break; 00191 case PPC::LFS: case PPC::LFSU: 00192 case PPC::LFSX: 00193 case PPC::LWZ: case PPC::LWZU: 00194 case PPC::LWZX: 00195 case PPC::LWA: 00196 case PPC::LWAX: 00197 case PPC::LVEWX: 00198 case PPC::LWBRX: 00199 case PPC::LWZ8: 00200 case PPC::LWZX8: 00201 LoadSize = 4; 00202 break; 00203 case PPC::LFD: case PPC::LFDU: 00204 case PPC::LFDX: 00205 case PPC::LD: case PPC::LDU: 00206 case PPC::LDX: 00207 LoadSize = 8; 00208 break; 00209 case PPC::LVX: 00210 case PPC::LVXL: 00211 LoadSize = 16; 00212 break; 00213 } 00214 00215 if (isLoadOfStoredAddress(LoadSize, 00216 Node->getOperand(0), Node->getOperand(1))) 00217 return NoopHazard; 00218 } 00219 00220 return NoHazard; 00221 } 00222 00223 void PPCHazardRecognizer970::EmitInstruction(SUnit *SU) { 00224 const SDNode *Node = SU->getNode()->getFlaggedMachineNode(); 00225 bool isFirst, isSingle, isCracked, isLoad, isStore; 00226 PPCII::PPC970_Unit InstrType = 00227 GetInstrType(Node->getOpcode(), isFirst, isSingle, isCracked, 00228 isLoad, isStore); 00229 if (InstrType == PPCII::PPC970_Pseudo) return; 00230 unsigned Opcode = Node->getMachineOpcode(); 00231 00232 // Update structural hazard information. 00233 if (Opcode == PPC::MTCTR) HasCTRSet = true; 00234 00235 // Track the address stored to. 00236 if (isStore) { 00237 unsigned ThisStoreSize; 00238 switch (Opcode) { 00239 default: llvm_unreachable("Unknown store instruction!"); 00240 case PPC::STB: case PPC::STB8: 00241 case PPC::STBU: case PPC::STBU8: 00242 case PPC::STBX: case PPC::STBX8: 00243 case PPC::STVEBX: 00244 ThisStoreSize = 1; 00245 break; 00246 case PPC::STH: case PPC::STH8: 00247 case PPC::STHU: case PPC::STHU8: 00248 case PPC::STHX: case PPC::STHX8: 00249 case PPC::STVEHX: 00250 case PPC::STHBRX: 00251 ThisStoreSize = 2; 00252 break; 00253 case PPC::STFS: 00254 case PPC::STFSU: 00255 case PPC::STFSX: 00256 case PPC::STWX: case PPC::STWX8: 00257 case PPC::STWUX: 00258 case PPC::STW: case PPC::STW8: 00259 case PPC::STWU: case PPC::STWU8: 00260 case PPC::STVEWX: 00261 case PPC::STFIWX: 00262 case PPC::STWBRX: 00263 ThisStoreSize = 4; 00264 break; 00265 case PPC::STD_32: 00266 case PPC::STDX_32: 00267 case PPC::STD: 00268 case PPC::STDU: 00269 case PPC::STFD: 00270 case PPC::STFDX: 00271 case PPC::STDX: 00272 case PPC::STDUX: 00273 ThisStoreSize = 8; 00274 break; 00275 case PPC::STVX: 00276 case PPC::STVXL: 00277 ThisStoreSize = 16; 00278 break; 00279 } 00280 00281 StoreSize[NumStores] = ThisStoreSize; 00282 StorePtr1[NumStores] = Node->getOperand(1); 00283 StorePtr2[NumStores] = Node->getOperand(2); 00284 ++NumStores; 00285 } 00286 00287 if (InstrType == PPCII::PPC970_BRU || isSingle) 00288 NumIssued = 4; // Terminate a d-group. 00289 ++NumIssued; 00290 00291 // If this instruction is cracked into two ops by the decoder, remember that 00292 // we issued two pieces. 00293 if (isCracked) 00294 ++NumIssued; 00295 00296 if (NumIssued == 5) 00297 EndDispatchGroup(); 00298 } 00299 00300 void PPCHazardRecognizer970::AdvanceCycle() { 00301 assert(NumIssued < 5 && "Illegal dispatch group!"); 00302 ++NumIssued; 00303 if (NumIssued == 5) 00304 EndDispatchGroup(); 00305 }