1 //===- HexagonSubtarget.cpp - Hexagon Subtarget Information ---------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the Hexagon specific subclass of TargetSubtarget. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "HexagonSubtarget.h" 14 #include "Hexagon.h" 15 #include "HexagonInstrInfo.h" 16 #include "HexagonRegisterInfo.h" 17 #include "MCTargetDesc/HexagonMCTargetDesc.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallSet.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/CodeGen/MachineInstr.h" 23 #include "llvm/CodeGen/MachineOperand.h" 24 #include "llvm/CodeGen/MachineScheduler.h" 25 #include "llvm/CodeGen/ScheduleDAG.h" 26 #include "llvm/CodeGen/ScheduleDAGInstrs.h" 27 #include "llvm/Support/CommandLine.h" 28 #include "llvm/Support/ErrorHandling.h" 29 #include "llvm/Target/TargetMachine.h" 30 #include <algorithm> 31 #include <cassert> 32 #include <map> 33 34 using namespace llvm; 35 36 #define DEBUG_TYPE "hexagon-subtarget" 37 38 #define GET_SUBTARGETINFO_CTOR 39 #define GET_SUBTARGETINFO_TARGET_DESC 40 #include "HexagonGenSubtargetInfo.inc" 41 42 static cl::opt<bool> EnableBSBSched("enable-bsb-sched", 43 cl::Hidden, cl::ZeroOrMore, cl::init(true)); 44 45 static cl::opt<bool> EnableTCLatencySched("enable-tc-latency-sched", 46 cl::Hidden, cl::ZeroOrMore, cl::init(false)); 47 48 static cl::opt<bool> EnableDotCurSched("enable-cur-sched", 49 cl::Hidden, cl::ZeroOrMore, cl::init(true), 50 cl::desc("Enable the scheduler to generate .cur")); 51 52 static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched", 53 cl::Hidden, cl::ZeroOrMore, cl::init(false), 54 cl::desc("Disable Hexagon MI Scheduling")); 55 56 static cl::opt<bool> EnableSubregLiveness("hexagon-subreg-liveness", 57 cl::Hidden, cl::ZeroOrMore, cl::init(true), 58 cl::desc("Enable subregister liveness tracking for Hexagon")); 59 60 static cl::opt<bool> OverrideLongCalls("hexagon-long-calls", 61 cl::Hidden, cl::ZeroOrMore, cl::init(false), 62 cl::desc("If present, forces/disables the use of long calls")); 63 64 static cl::opt<bool> EnablePredicatedCalls("hexagon-pred-calls", 65 cl::Hidden, cl::ZeroOrMore, cl::init(false), 66 cl::desc("Consider calls to be predicable")); 67 68 static cl::opt<bool> SchedPredsCloser("sched-preds-closer", 69 cl::Hidden, cl::ZeroOrMore, cl::init(true)); 70 71 static cl::opt<bool> SchedRetvalOptimization("sched-retval-optimization", 72 cl::Hidden, cl::ZeroOrMore, cl::init(true)); 73 74 static cl::opt<bool> EnableCheckBankConflict("hexagon-check-bank-conflict", 75 cl::Hidden, cl::ZeroOrMore, cl::init(true), 76 cl::desc("Enable checking for cache bank conflicts")); 77 78 HexagonSubtarget::HexagonSubtarget(const Triple &TT, StringRef CPU, 79 StringRef FS, const TargetMachine &TM) 80 : HexagonGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS), 81 OptLevel(TM.getOptLevel()), 82 CPUString(std::string(Hexagon_MC::selectHexagonCPU(CPU))), 83 TargetTriple(TT), InstrInfo(initializeSubtargetDependencies(CPU, FS)), 84 RegInfo(getHwMode()), TLInfo(TM, *this), 85 InstrItins(getInstrItineraryForCPU(CPUString)) { 86 Hexagon_MC::addArchSubtarget(this, FS); 87 // Beware of the default constructor of InstrItineraryData: it will 88 // reset all members to 0. 89 assert(InstrItins.Itineraries != nullptr && "InstrItins not initialized"); 90 } 91 92 HexagonSubtarget & 93 HexagonSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS) { 94 Optional<Hexagon::ArchEnum> ArchVer = 95 Hexagon::GetCpu(Hexagon::CpuTable, CPUString); 96 if (ArchVer) 97 HexagonArchVersion = *ArchVer; 98 else 99 llvm_unreachable("Unrecognized Hexagon processor version"); 100 101 UseHVX128BOps = false; 102 UseHVX64BOps = false; 103 UseAudioOps = false; 104 UseLongCalls = false; 105 106 UseBSBScheduling = hasV60Ops() && EnableBSBSched; 107 108 ParseSubtargetFeatures(CPUString, /*TuneCPU*/ CPUString, FS); 109 110 if (OverrideLongCalls.getPosition()) 111 UseLongCalls = OverrideLongCalls; 112 113 if (isTinyCore()) { 114 // Tiny core has a single thread, so back-to-back scheduling is enabled by 115 // default. 116 if (!EnableBSBSched.getPosition()) 117 UseBSBScheduling = false; 118 } 119 120 FeatureBitset Features = getFeatureBits(); 121 if (HexagonDisableDuplex) 122 setFeatureBits(Features.reset(Hexagon::FeatureDuplex)); 123 setFeatureBits(Hexagon_MC::completeHVXFeatures(Features)); 124 125 return *this; 126 } 127 128 bool HexagonSubtarget::isHVXElementType(MVT Ty, bool IncludeBool) const { 129 if (!useHVXOps()) 130 return false; 131 if (Ty.isVector()) 132 Ty = Ty.getVectorElementType(); 133 if (IncludeBool && Ty == MVT::i1) 134 return true; 135 ArrayRef<MVT> ElemTypes = getHVXElementTypes(); 136 return llvm::is_contained(ElemTypes, Ty); 137 } 138 139 bool HexagonSubtarget::isHVXVectorType(MVT VecTy, bool IncludeBool) const { 140 if (!VecTy.isVector() || !useHVXOps() || VecTy.isScalableVector()) 141 return false; 142 MVT ElemTy = VecTy.getVectorElementType(); 143 if (!IncludeBool && ElemTy == MVT::i1) 144 return false; 145 146 unsigned HwLen = getVectorLength(); 147 unsigned NumElems = VecTy.getVectorNumElements(); 148 ArrayRef<MVT> ElemTypes = getHVXElementTypes(); 149 150 if (IncludeBool && ElemTy == MVT::i1) { 151 // Boolean HVX vector types are formed from regular HVX vector types 152 // by replacing the element type with i1. 153 for (MVT T : ElemTypes) 154 if (NumElems * T.getSizeInBits() == 8 * HwLen) 155 return true; 156 return false; 157 } 158 159 unsigned VecWidth = VecTy.getSizeInBits(); 160 if (VecWidth != 8 * HwLen && VecWidth != 16 * HwLen) 161 return false; 162 return llvm::is_contained(ElemTypes, ElemTy); 163 } 164 165 bool HexagonSubtarget::isTypeForHVX(Type *VecTy, bool IncludeBool) const { 166 if (!VecTy->isVectorTy() || isa<ScalableVectorType>(VecTy)) 167 return false; 168 // Avoid types like <2 x i32*>. 169 if (!cast<VectorType>(VecTy)->getElementType()->isIntegerTy()) 170 return false; 171 // The given type may be something like <17 x i32>, which is not MVT, 172 // but can be represented as (non-simple) EVT. 173 EVT Ty = EVT::getEVT(VecTy, /*HandleUnknown*/false); 174 if (Ty.getSizeInBits() <= 64 || !Ty.getVectorElementType().isSimple()) 175 return false; 176 177 auto isHvxTy = [this, IncludeBool](MVT SimpleTy) { 178 if (isHVXVectorType(SimpleTy, IncludeBool)) 179 return true; 180 auto Action = getTargetLowering()->getPreferredVectorAction(SimpleTy); 181 return Action == TargetLoweringBase::TypeWidenVector; 182 }; 183 184 // Round up EVT to have power-of-2 elements, and keep checking if it 185 // qualifies for HVX, dividing it in half after each step. 186 MVT ElemTy = Ty.getVectorElementType().getSimpleVT(); 187 unsigned VecLen = PowerOf2Ceil(Ty.getVectorNumElements()); 188 while (ElemTy.getSizeInBits() * VecLen > 64) { 189 MVT SimpleTy = MVT::getVectorVT(ElemTy, VecLen); 190 if (SimpleTy.isValid() && isHvxTy(SimpleTy)) 191 return true; 192 VecLen /= 2; 193 } 194 195 return false; 196 } 197 198 void HexagonSubtarget::UsrOverflowMutation::apply(ScheduleDAGInstrs *DAG) { 199 for (SUnit &SU : DAG->SUnits) { 200 if (!SU.isInstr()) 201 continue; 202 SmallVector<SDep, 4> Erase; 203 for (auto &D : SU.Preds) 204 if (D.getKind() == SDep::Output && D.getReg() == Hexagon::USR_OVF) 205 Erase.push_back(D); 206 for (auto &E : Erase) 207 SU.removePred(E); 208 } 209 } 210 211 void HexagonSubtarget::HVXMemLatencyMutation::apply(ScheduleDAGInstrs *DAG) { 212 for (SUnit &SU : DAG->SUnits) { 213 // Update the latency of chain edges between v60 vector load or store 214 // instructions to be 1. These instruction cannot be scheduled in the 215 // same packet. 216 MachineInstr &MI1 = *SU.getInstr(); 217 auto *QII = static_cast<const HexagonInstrInfo*>(DAG->TII); 218 bool IsStoreMI1 = MI1.mayStore(); 219 bool IsLoadMI1 = MI1.mayLoad(); 220 if (!QII->isHVXVec(MI1) || !(IsStoreMI1 || IsLoadMI1)) 221 continue; 222 for (SDep &SI : SU.Succs) { 223 if (SI.getKind() != SDep::Order || SI.getLatency() != 0) 224 continue; 225 MachineInstr &MI2 = *SI.getSUnit()->getInstr(); 226 if (!QII->isHVXVec(MI2)) 227 continue; 228 if ((IsStoreMI1 && MI2.mayStore()) || (IsLoadMI1 && MI2.mayLoad())) { 229 SI.setLatency(1); 230 SU.setHeightDirty(); 231 // Change the dependence in the opposite direction too. 232 for (SDep &PI : SI.getSUnit()->Preds) { 233 if (PI.getSUnit() != &SU || PI.getKind() != SDep::Order) 234 continue; 235 PI.setLatency(1); 236 SI.getSUnit()->setDepthDirty(); 237 } 238 } 239 } 240 } 241 } 242 243 // Check if a call and subsequent A2_tfrpi instructions should maintain 244 // scheduling affinity. We are looking for the TFRI to be consumed in 245 // the next instruction. This should help reduce the instances of 246 // double register pairs being allocated and scheduled before a call 247 // when not used until after the call. This situation is exacerbated 248 // by the fact that we allocate the pair from the callee saves list, 249 // leading to excess spills and restores. 250 bool HexagonSubtarget::CallMutation::shouldTFRICallBind( 251 const HexagonInstrInfo &HII, const SUnit &Inst1, 252 const SUnit &Inst2) const { 253 if (Inst1.getInstr()->getOpcode() != Hexagon::A2_tfrpi) 254 return false; 255 256 // TypeXTYPE are 64 bit operations. 257 unsigned Type = HII.getType(*Inst2.getInstr()); 258 return Type == HexagonII::TypeS_2op || Type == HexagonII::TypeS_3op || 259 Type == HexagonII::TypeALU64 || Type == HexagonII::TypeM; 260 } 261 262 void HexagonSubtarget::CallMutation::apply(ScheduleDAGInstrs *DAGInstrs) { 263 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs); 264 SUnit* LastSequentialCall = nullptr; 265 // Map from virtual register to physical register from the copy. 266 DenseMap<unsigned, unsigned> VRegHoldingReg; 267 // Map from the physical register to the instruction that uses virtual 268 // register. This is used to create the barrier edge. 269 DenseMap<unsigned, SUnit *> LastVRegUse; 270 auto &TRI = *DAG->MF.getSubtarget().getRegisterInfo(); 271 auto &HII = *DAG->MF.getSubtarget<HexagonSubtarget>().getInstrInfo(); 272 273 // Currently we only catch the situation when compare gets scheduled 274 // before preceding call. 275 for (unsigned su = 0, e = DAG->SUnits.size(); su != e; ++su) { 276 // Remember the call. 277 if (DAG->SUnits[su].getInstr()->isCall()) 278 LastSequentialCall = &DAG->SUnits[su]; 279 // Look for a compare that defines a predicate. 280 else if (DAG->SUnits[su].getInstr()->isCompare() && LastSequentialCall) 281 DAG->addEdge(&DAG->SUnits[su], SDep(LastSequentialCall, SDep::Barrier)); 282 // Look for call and tfri* instructions. 283 else if (SchedPredsCloser && LastSequentialCall && su > 1 && su < e-1 && 284 shouldTFRICallBind(HII, DAG->SUnits[su], DAG->SUnits[su+1])) 285 DAG->addEdge(&DAG->SUnits[su], SDep(&DAG->SUnits[su-1], SDep::Barrier)); 286 // Prevent redundant register copies due to reads and writes of physical 287 // registers. The original motivation for this was the code generated 288 // between two calls, which are caused both the return value and the 289 // argument for the next call being in %r0. 290 // Example: 291 // 1: <call1> 292 // 2: %vreg = COPY %r0 293 // 3: <use of %vreg> 294 // 4: %r0 = ... 295 // 5: <call2> 296 // The scheduler would often swap 3 and 4, so an additional register is 297 // needed. This code inserts a Barrier dependence between 3 & 4 to prevent 298 // this. 299 // The code below checks for all the physical registers, not just R0/D0/V0. 300 else if (SchedRetvalOptimization) { 301 const MachineInstr *MI = DAG->SUnits[su].getInstr(); 302 if (MI->isCopy() && 303 Register::isPhysicalRegister(MI->getOperand(1).getReg())) { 304 // %vregX = COPY %r0 305 VRegHoldingReg[MI->getOperand(0).getReg()] = MI->getOperand(1).getReg(); 306 LastVRegUse.erase(MI->getOperand(1).getReg()); 307 } else { 308 for (const MachineOperand &MO : MI->operands()) { 309 if (!MO.isReg()) 310 continue; 311 if (MO.isUse() && !MI->isCopy() && 312 VRegHoldingReg.count(MO.getReg())) { 313 // <use of %vregX> 314 LastVRegUse[VRegHoldingReg[MO.getReg()]] = &DAG->SUnits[su]; 315 } else if (MO.isDef() && Register::isPhysicalRegister(MO.getReg())) { 316 for (MCRegAliasIterator AI(MO.getReg(), &TRI, true); AI.isValid(); 317 ++AI) { 318 if (LastVRegUse.count(*AI) && 319 LastVRegUse[*AI] != &DAG->SUnits[su]) 320 // %r0 = ... 321 DAG->addEdge(&DAG->SUnits[su], SDep(LastVRegUse[*AI], SDep::Barrier)); 322 LastVRegUse.erase(*AI); 323 } 324 } 325 } 326 } 327 } 328 } 329 } 330 331 void HexagonSubtarget::BankConflictMutation::apply(ScheduleDAGInstrs *DAG) { 332 if (!EnableCheckBankConflict) 333 return; 334 335 const auto &HII = static_cast<const HexagonInstrInfo&>(*DAG->TII); 336 337 // Create artificial edges between loads that could likely cause a bank 338 // conflict. Since such loads would normally not have any dependency 339 // between them, we cannot rely on existing edges. 340 for (unsigned i = 0, e = DAG->SUnits.size(); i != e; ++i) { 341 SUnit &S0 = DAG->SUnits[i]; 342 MachineInstr &L0 = *S0.getInstr(); 343 if (!L0.mayLoad() || L0.mayStore() || 344 HII.getAddrMode(L0) != HexagonII::BaseImmOffset) 345 continue; 346 int64_t Offset0; 347 unsigned Size0; 348 MachineOperand *BaseOp0 = HII.getBaseAndOffset(L0, Offset0, Size0); 349 // Is the access size is longer than the L1 cache line, skip the check. 350 if (BaseOp0 == nullptr || !BaseOp0->isReg() || Size0 >= 32) 351 continue; 352 // Scan only up to 32 instructions ahead (to avoid n^2 complexity). 353 for (unsigned j = i+1, m = std::min(i+32, e); j != m; ++j) { 354 SUnit &S1 = DAG->SUnits[j]; 355 MachineInstr &L1 = *S1.getInstr(); 356 if (!L1.mayLoad() || L1.mayStore() || 357 HII.getAddrMode(L1) != HexagonII::BaseImmOffset) 358 continue; 359 int64_t Offset1; 360 unsigned Size1; 361 MachineOperand *BaseOp1 = HII.getBaseAndOffset(L1, Offset1, Size1); 362 if (BaseOp1 == nullptr || !BaseOp1->isReg() || Size1 >= 32 || 363 BaseOp0->getReg() != BaseOp1->getReg()) 364 continue; 365 // Check bits 3 and 4 of the offset: if they differ, a bank conflict 366 // is unlikely. 367 if (((Offset0 ^ Offset1) & 0x18) != 0) 368 continue; 369 // Bits 3 and 4 are the same, add an artificial edge and set extra 370 // latency. 371 SDep A(&S0, SDep::Artificial); 372 A.setLatency(1); 373 S1.addPred(A, true); 374 } 375 } 376 } 377 378 /// Enable use of alias analysis during code generation (during MI 379 /// scheduling, DAGCombine, etc.). 380 bool HexagonSubtarget::useAA() const { 381 if (OptLevel != CodeGenOpt::None) 382 return true; 383 return false; 384 } 385 386 /// Perform target specific adjustments to the latency of a schedule 387 /// dependency. 388 void HexagonSubtarget::adjustSchedDependency(SUnit *Src, int SrcOpIdx, 389 SUnit *Dst, int DstOpIdx, 390 SDep &Dep) const { 391 if (!Src->isInstr() || !Dst->isInstr()) 392 return; 393 394 MachineInstr *SrcInst = Src->getInstr(); 395 MachineInstr *DstInst = Dst->getInstr(); 396 const HexagonInstrInfo *QII = getInstrInfo(); 397 398 // Instructions with .new operands have zero latency. 399 SmallSet<SUnit *, 4> ExclSrc; 400 SmallSet<SUnit *, 4> ExclDst; 401 if (QII->canExecuteInBundle(*SrcInst, *DstInst) && 402 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) { 403 Dep.setLatency(0); 404 return; 405 } 406 407 if (!hasV60Ops()) 408 return; 409 410 // Set the latency for a copy to zero since we hope that is will get removed. 411 if (DstInst->isCopy()) 412 Dep.setLatency(0); 413 414 // If it's a REG_SEQUENCE/COPY, use its destination instruction to determine 415 // the correct latency. 416 if ((DstInst->isRegSequence() || DstInst->isCopy()) && Dst->NumSuccs == 1) { 417 Register DReg = DstInst->getOperand(0).getReg(); 418 MachineInstr *DDst = Dst->Succs[0].getSUnit()->getInstr(); 419 unsigned UseIdx = -1; 420 for (unsigned OpNum = 0; OpNum < DDst->getNumOperands(); OpNum++) { 421 const MachineOperand &MO = DDst->getOperand(OpNum); 422 if (MO.isReg() && MO.getReg() && MO.isUse() && MO.getReg() == DReg) { 423 UseIdx = OpNum; 424 break; 425 } 426 } 427 int DLatency = (InstrInfo.getOperandLatency(&InstrItins, *SrcInst, 428 0, *DDst, UseIdx)); 429 DLatency = std::max(DLatency, 0); 430 Dep.setLatency((unsigned)DLatency); 431 } 432 433 // Try to schedule uses near definitions to generate .cur. 434 ExclSrc.clear(); 435 ExclDst.clear(); 436 if (EnableDotCurSched && QII->isToBeScheduledASAP(*SrcInst, *DstInst) && 437 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) { 438 Dep.setLatency(0); 439 return; 440 } 441 442 updateLatency(*SrcInst, *DstInst, Dep); 443 } 444 445 void HexagonSubtarget::getPostRAMutations( 446 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const { 447 Mutations.push_back(std::make_unique<UsrOverflowMutation>()); 448 Mutations.push_back(std::make_unique<HVXMemLatencyMutation>()); 449 Mutations.push_back(std::make_unique<BankConflictMutation>()); 450 } 451 452 void HexagonSubtarget::getSMSMutations( 453 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const { 454 Mutations.push_back(std::make_unique<UsrOverflowMutation>()); 455 Mutations.push_back(std::make_unique<HVXMemLatencyMutation>()); 456 } 457 458 // Pin the vtable to this file. 459 void HexagonSubtarget::anchor() {} 460 461 bool HexagonSubtarget::enableMachineScheduler() const { 462 if (DisableHexagonMISched.getNumOccurrences()) 463 return !DisableHexagonMISched; 464 return true; 465 } 466 467 bool HexagonSubtarget::usePredicatedCalls() const { 468 return EnablePredicatedCalls; 469 } 470 471 void HexagonSubtarget::updateLatency(MachineInstr &SrcInst, 472 MachineInstr &DstInst, SDep &Dep) const { 473 if (Dep.isArtificial()) { 474 Dep.setLatency(1); 475 return; 476 } 477 478 if (!hasV60Ops()) 479 return; 480 481 auto &QII = static_cast<const HexagonInstrInfo&>(*getInstrInfo()); 482 483 // BSB scheduling. 484 if (QII.isHVXVec(SrcInst) || useBSBScheduling()) 485 Dep.setLatency((Dep.getLatency() + 1) >> 1); 486 } 487 488 void HexagonSubtarget::restoreLatency(SUnit *Src, SUnit *Dst) const { 489 MachineInstr *SrcI = Src->getInstr(); 490 for (auto &I : Src->Succs) { 491 if (!I.isAssignedRegDep() || I.getSUnit() != Dst) 492 continue; 493 Register DepR = I.getReg(); 494 int DefIdx = -1; 495 for (unsigned OpNum = 0; OpNum < SrcI->getNumOperands(); OpNum++) { 496 const MachineOperand &MO = SrcI->getOperand(OpNum); 497 bool IsSameOrSubReg = false; 498 if (MO.isReg()) { 499 Register MOReg = MO.getReg(); 500 if (DepR.isVirtual()) { 501 IsSameOrSubReg = (MOReg == DepR); 502 } else { 503 IsSameOrSubReg = getRegisterInfo()->isSubRegisterEq(DepR, MOReg); 504 } 505 if (MO.isDef() && IsSameOrSubReg) 506 DefIdx = OpNum; 507 } 508 } 509 assert(DefIdx >= 0 && "Def Reg not found in Src MI"); 510 MachineInstr *DstI = Dst->getInstr(); 511 SDep T = I; 512 for (unsigned OpNum = 0; OpNum < DstI->getNumOperands(); OpNum++) { 513 const MachineOperand &MO = DstI->getOperand(OpNum); 514 if (MO.isReg() && MO.isUse() && MO.getReg() == DepR) { 515 int Latency = (InstrInfo.getOperandLatency(&InstrItins, *SrcI, 516 DefIdx, *DstI, OpNum)); 517 518 // For some instructions (ex: COPY), we might end up with < 0 latency 519 // as they don't have any Itinerary class associated with them. 520 Latency = std::max(Latency, 0); 521 522 I.setLatency(Latency); 523 updateLatency(*SrcI, *DstI, I); 524 } 525 } 526 527 // Update the latency of opposite edge too. 528 T.setSUnit(Src); 529 auto F = find(Dst->Preds, T); 530 assert(F != Dst->Preds.end()); 531 F->setLatency(I.getLatency()); 532 } 533 } 534 535 /// Change the latency between the two SUnits. 536 void HexagonSubtarget::changeLatency(SUnit *Src, SUnit *Dst, unsigned Lat) 537 const { 538 for (auto &I : Src->Succs) { 539 if (!I.isAssignedRegDep() || I.getSUnit() != Dst) 540 continue; 541 SDep T = I; 542 I.setLatency(Lat); 543 544 // Update the latency of opposite edge too. 545 T.setSUnit(Src); 546 auto F = find(Dst->Preds, T); 547 assert(F != Dst->Preds.end()); 548 F->setLatency(Lat); 549 } 550 } 551 552 /// If the SUnit has a zero latency edge, return the other SUnit. 553 static SUnit *getZeroLatency(SUnit *N, SmallVector<SDep, 4> &Deps) { 554 for (auto &I : Deps) 555 if (I.isAssignedRegDep() && I.getLatency() == 0 && 556 !I.getSUnit()->getInstr()->isPseudo()) 557 return I.getSUnit(); 558 return nullptr; 559 } 560 561 // Return true if these are the best two instructions to schedule 562 // together with a zero latency. Only one dependence should have a zero 563 // latency. If there are multiple choices, choose the best, and change 564 // the others, if needed. 565 bool HexagonSubtarget::isBestZeroLatency(SUnit *Src, SUnit *Dst, 566 const HexagonInstrInfo *TII, SmallSet<SUnit*, 4> &ExclSrc, 567 SmallSet<SUnit*, 4> &ExclDst) const { 568 MachineInstr &SrcInst = *Src->getInstr(); 569 MachineInstr &DstInst = *Dst->getInstr(); 570 571 // Ignore Boundary SU nodes as these have null instructions. 572 if (Dst->isBoundaryNode()) 573 return false; 574 575 if (SrcInst.isPHI() || DstInst.isPHI()) 576 return false; 577 578 if (!TII->isToBeScheduledASAP(SrcInst, DstInst) && 579 !TII->canExecuteInBundle(SrcInst, DstInst)) 580 return false; 581 582 // The architecture doesn't allow three dependent instructions in the same 583 // packet. So, if the destination has a zero latency successor, then it's 584 // not a candidate for a zero latency predecessor. 585 if (getZeroLatency(Dst, Dst->Succs) != nullptr) 586 return false; 587 588 // Check if the Dst instruction is the best candidate first. 589 SUnit *Best = nullptr; 590 SUnit *DstBest = nullptr; 591 SUnit *SrcBest = getZeroLatency(Dst, Dst->Preds); 592 if (SrcBest == nullptr || Src->NodeNum >= SrcBest->NodeNum) { 593 // Check that Src doesn't have a better candidate. 594 DstBest = getZeroLatency(Src, Src->Succs); 595 if (DstBest == nullptr || Dst->NodeNum <= DstBest->NodeNum) 596 Best = Dst; 597 } 598 if (Best != Dst) 599 return false; 600 601 // The caller frequently adds the same dependence twice. If so, then 602 // return true for this case too. 603 if ((Src == SrcBest && Dst == DstBest ) || 604 (SrcBest == nullptr && Dst == DstBest) || 605 (Src == SrcBest && Dst == nullptr)) 606 return true; 607 608 // Reassign the latency for the previous bests, which requires setting 609 // the dependence edge in both directions. 610 if (SrcBest != nullptr) { 611 if (!hasV60Ops()) 612 changeLatency(SrcBest, Dst, 1); 613 else 614 restoreLatency(SrcBest, Dst); 615 } 616 if (DstBest != nullptr) { 617 if (!hasV60Ops()) 618 changeLatency(Src, DstBest, 1); 619 else 620 restoreLatency(Src, DstBest); 621 } 622 623 // Attempt to find another opprotunity for zero latency in a different 624 // dependence. 625 if (SrcBest && DstBest) 626 // If there is an edge from SrcBest to DstBst, then try to change that 627 // to 0 now. 628 changeLatency(SrcBest, DstBest, 0); 629 else if (DstBest) { 630 // Check if the previous best destination instruction has a new zero 631 // latency dependence opportunity. 632 ExclSrc.insert(Src); 633 for (auto &I : DstBest->Preds) 634 if (ExclSrc.count(I.getSUnit()) == 0 && 635 isBestZeroLatency(I.getSUnit(), DstBest, TII, ExclSrc, ExclDst)) 636 changeLatency(I.getSUnit(), DstBest, 0); 637 } else if (SrcBest) { 638 // Check if previous best source instruction has a new zero latency 639 // dependence opportunity. 640 ExclDst.insert(Dst); 641 for (auto &I : SrcBest->Succs) 642 if (ExclDst.count(I.getSUnit()) == 0 && 643 isBestZeroLatency(SrcBest, I.getSUnit(), TII, ExclSrc, ExclDst)) 644 changeLatency(SrcBest, I.getSUnit(), 0); 645 } 646 647 return true; 648 } 649 650 unsigned HexagonSubtarget::getL1CacheLineSize() const { 651 return 32; 652 } 653 654 unsigned HexagonSubtarget::getL1PrefetchDistance() const { 655 return 32; 656 } 657 658 bool HexagonSubtarget::enableSubRegLiveness() const { 659 return EnableSubregLiveness; 660 } 661