1 //===-- PPCHazardRecognizers.cpp - PowerPC Hazard Recognizer Impls --------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // 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 "pre-RA-sched" 15 #include "PPCHazardRecognizers.h" 16 #include "PPC.h" 17 #include "PPCInstrInfo.h" 18 #include "llvm/CodeGen/ScheduleDAG.h" 19 #include "llvm/Support/Debug.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/Support/raw_ostream.h" 22 using namespace llvm; 23 24 //===----------------------------------------------------------------------===// 25 // PowerPC 440 Hazard Recognizer 26 void PPCHazardRecognizer440::EmitInstruction(SUnit *SU) { 27 const MCInstrDesc *MCID = DAG->getInstrDesc(SU); 28 if (!MCID) { 29 // This is a PPC pseudo-instruction. 30 return; 31 } 32 33 ScoreboardHazardRecognizer::EmitInstruction(SU); 34 } 35 36 //===----------------------------------------------------------------------===// 37 // PowerPC 970 Hazard Recognizer 38 // 39 // This models the dispatch group formation of the PPC970 processor. Dispatch 40 // groups are bundles of up to five instructions that can contain various mixes 41 // of instructions. The PPC970 can dispatch a peak of 4 non-branch and one 42 // branch instruction per-cycle. 43 // 44 // There are a number of restrictions to dispatch group formation: some 45 // instructions can only be issued in the first slot of a dispatch group, & some 46 // instructions fill an entire dispatch group. Additionally, only branches can 47 // issue in the 5th (last) slot. 48 // 49 // Finally, there are a number of "structural" hazards on the PPC970. These 50 // conditions cause large performance penalties due to misprediction, recovery, 51 // and replay logic that has to happen. These cases include setting a CTR and 52 // branching through it in the same dispatch group, and storing to an address, 53 // then loading from the same address within a dispatch group. To avoid these 54 // conditions, we insert no-op instructions when appropriate. 55 // 56 // FIXME: This is missing some significant cases: 57 // 1. Modeling of microcoded instructions. 58 // 2. Handling of serialized operations. 59 // 3. Handling of the esoteric cases in "Resource-based Instruction Grouping". 60 // 61 62 PPCHazardRecognizer970::PPCHazardRecognizer970(const TargetInstrInfo &tii) 63 : TII(tii) { 64 EndDispatchGroup(); 65 } 66 67 void PPCHazardRecognizer970::EndDispatchGroup() { 68 DEBUG(errs() << "=== Start of dispatch group\n"); 69 NumIssued = 0; 70 71 // Structural hazard info. 72 HasCTRSet = false; 73 NumStores = 0; 74 } 75 76 77 PPCII::PPC970_Unit 78 PPCHazardRecognizer970::GetInstrType(unsigned Opcode, 79 bool &isFirst, bool &isSingle, 80 bool &isCracked, 81 bool &isLoad, bool &isStore) { 82 const MCInstrDesc &MCID = TII.get(Opcode); 83 84 isLoad = MCID.mayLoad(); 85 isStore = MCID.mayStore(); 86 87 uint64_t TSFlags = MCID.TSFlags; 88 89 isFirst = TSFlags & PPCII::PPC970_First; 90 isSingle = TSFlags & PPCII::PPC970_Single; 91 isCracked = TSFlags & PPCII::PPC970_Cracked; 92 return (PPCII::PPC970_Unit)(TSFlags & PPCII::PPC970_Mask); 93 } 94 95 /// isLoadOfStoredAddress - If we have a load from the previously stored pointer 96 /// as indicated by StorePtr1/StorePtr2/StoreSize, return true. 97 bool PPCHazardRecognizer970:: 98 isLoadOfStoredAddress(uint64_t LoadSize, int64_t LoadOffset, 99 const Value *LoadValue) const { 100 for (unsigned i = 0, e = NumStores; i != e; ++i) { 101 // Handle exact and commuted addresses. 102 if (LoadValue == StoreValue[i] && LoadOffset == StoreOffset[i]) 103 return true; 104 105 // Okay, we don't have an exact match, if this is an indexed offset, see if 106 // we have overlap (which happens during fp->int conversion for example). 107 if (StoreValue[i] == LoadValue) { 108 // Okay the base pointers match, so we have [c1+r] vs [c2+r]. Check 109 // to see if the load and store actually overlap. 110 if (StoreOffset[i] < LoadOffset) { 111 if (int64_t(StoreOffset[i]+StoreSize[i]) > LoadOffset) return true; 112 } else { 113 if (int64_t(LoadOffset+LoadSize) > StoreOffset[i]) return true; 114 } 115 } 116 } 117 return false; 118 } 119 120 /// getHazardType - We return hazard for any non-branch instruction that would 121 /// terminate the dispatch group. We turn NoopHazard for any 122 /// instructions that wouldn't terminate the dispatch group that would cause a 123 /// pipeline flush. 124 ScheduleHazardRecognizer::HazardType PPCHazardRecognizer970:: 125 getHazardType(SUnit *SU, int Stalls) { 126 assert(Stalls == 0 && "PPC hazards don't support scoreboard lookahead"); 127 128 MachineInstr *MI = SU->getInstr(); 129 130 if (MI->isDebugValue()) 131 return NoHazard; 132 133 unsigned Opcode = MI->getOpcode(); 134 135 bool isFirst, isSingle, isCracked, isLoad, isStore; 136 PPCII::PPC970_Unit InstrType = 137 GetInstrType(Opcode, isFirst, isSingle, isCracked, 138 isLoad, isStore); 139 if (InstrType == PPCII::PPC970_Pseudo) return NoHazard; 140 141 // We can only issue a PPC970_First/PPC970_Single instruction (such as 142 // crand/mtspr/etc) if this is the first cycle of the dispatch group. 143 if (NumIssued != 0 && (isFirst || isSingle)) 144 return Hazard; 145 146 // If this instruction is cracked into two ops by the decoder, we know that 147 // it is not a branch and that it cannot issue if 3 other instructions are 148 // already in the dispatch group. 149 if (isCracked && NumIssued > 2) 150 return Hazard; 151 152 switch (InstrType) { 153 default: llvm_unreachable("Unknown instruction type!"); 154 case PPCII::PPC970_FXU: 155 case PPCII::PPC970_LSU: 156 case PPCII::PPC970_FPU: 157 case PPCII::PPC970_VALU: 158 case PPCII::PPC970_VPERM: 159 // We can only issue a branch as the last instruction in a group. 160 if (NumIssued == 4) return Hazard; 161 break; 162 case PPCII::PPC970_CRU: 163 // We can only issue a CR instruction in the first two slots. 164 if (NumIssued >= 2) return Hazard; 165 break; 166 case PPCII::PPC970_BRU: 167 break; 168 } 169 170 // Do not allow MTCTR and BCTRL to be in the same dispatch group. 171 if (HasCTRSet && (Opcode == PPC::BCTRL_Darwin || Opcode == PPC::BCTRL_SVR4)) 172 return NoopHazard; 173 174 // If this is a load following a store, make sure it's not to the same or 175 // overlapping address. 176 if (isLoad && NumStores && !MI->memoperands_empty()) { 177 MachineMemOperand *MO = *MI->memoperands_begin(); 178 if (isLoadOfStoredAddress(MO->getSize(), 179 MO->getOffset(), MO->getValue())) 180 return NoopHazard; 181 } 182 183 return NoHazard; 184 } 185 186 void PPCHazardRecognizer970::EmitInstruction(SUnit *SU) { 187 MachineInstr *MI = SU->getInstr(); 188 189 if (MI->isDebugValue()) 190 return; 191 192 unsigned Opcode = MI->getOpcode(); 193 194 bool isFirst, isSingle, isCracked, isLoad, isStore; 195 PPCII::PPC970_Unit InstrType = 196 GetInstrType(Opcode, isFirst, isSingle, isCracked, 197 isLoad, isStore); 198 if (InstrType == PPCII::PPC970_Pseudo) return; 199 200 // Update structural hazard information. 201 if (Opcode == PPC::MTCTR || Opcode == PPC::MTCTR8) HasCTRSet = true; 202 203 // Track the address stored to. 204 if (isStore && NumStores < 4 && !MI->memoperands_empty()) { 205 MachineMemOperand *MO = *MI->memoperands_begin(); 206 StoreSize[NumStores] = MO->getSize(); 207 StoreOffset[NumStores] = MO->getOffset(); 208 StoreValue[NumStores] = MO->getValue(); 209 ++NumStores; 210 } 211 212 if (InstrType == PPCII::PPC970_BRU || isSingle) 213 NumIssued = 4; // Terminate a d-group. 214 ++NumIssued; 215 216 // If this instruction is cracked into two ops by the decoder, remember that 217 // we issued two pieces. 218 if (isCracked) 219 ++NumIssued; 220 221 if (NumIssued == 5) 222 EndDispatchGroup(); 223 } 224 225 void PPCHazardRecognizer970::AdvanceCycle() { 226 assert(NumIssued < 5 && "Illegal dispatch group!"); 227 ++NumIssued; 228 if (NumIssued == 5) 229 EndDispatchGroup(); 230 } 231 232 void PPCHazardRecognizer970::Reset() { 233 EndDispatchGroup(); 234 } 235 236