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