1 //===- HexagonSubtarget.cpp - Hexagon Subtarget Information ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Hexagon specific subclass of TargetSubtarget.
12 //===----------------------------------------------------------------------===//
15 #include "HexagonInstrInfo.h"
16 #include "HexagonRegisterInfo.h"
17 #include "HexagonSubtarget.h"
18 #include "MCTargetDesc/HexagonMCTargetDesc.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/CodeGen/MachineScheduler.h"
26 #include "llvm/CodeGen/ScheduleDAG.h"
27 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/ErrorHandling.h"
36 #define DEBUG_TYPE "hexagon-subtarget"
38 #define GET_SUBTARGETINFO_CTOR
39 #define GET_SUBTARGETINFO_TARGET_DESC
40 #include "HexagonGenSubtargetInfo.inc"
43 static cl::opt<bool> EnableBSBSched("enable-bsb-sched",
44 cl::Hidden, cl::ZeroOrMore, cl::init(true));
46 static cl::opt<bool> EnableTCLatencySched("enable-tc-latency-sched",
47 cl::Hidden, cl::ZeroOrMore, cl::init(false));
49 static cl::opt<bool> EnableDotCurSched("enable-cur-sched",
50 cl::Hidden, cl::ZeroOrMore, cl::init(true),
51 cl::desc("Enable the scheduler to generate .cur"));
53 static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
54 cl::Hidden, cl::ZeroOrMore, cl::init(false),
55 cl::desc("Disable Hexagon MI Scheduling"));
57 static cl::opt<bool> EnableSubregLiveness("hexagon-subreg-liveness",
58 cl::Hidden, cl::ZeroOrMore, cl::init(true),
59 cl::desc("Enable subregister liveness tracking for Hexagon"));
61 static cl::opt<bool> OverrideLongCalls("hexagon-long-calls",
62 cl::Hidden, cl::ZeroOrMore, cl::init(false),
63 cl::desc("If present, forces/disables the use of long calls"));
65 static cl::opt<bool> EnablePredicatedCalls("hexagon-pred-calls",
66 cl::Hidden, cl::ZeroOrMore, cl::init(false),
67 cl::desc("Consider calls to be predicable"));
69 static cl::opt<bool> SchedPredsCloser("sched-preds-closer",
70 cl::Hidden, cl::ZeroOrMore, cl::init(true));
72 static cl::opt<bool> SchedRetvalOptimization("sched-retval-optimization",
73 cl::Hidden, cl::ZeroOrMore, cl::init(true));
75 static cl::opt<bool> EnableCheckBankConflict("hexagon-check-bank-conflict",
76 cl::Hidden, cl::ZeroOrMore, cl::init(true),
77 cl::desc("Enable checking for cache bank conflicts"));
80 HexagonSubtarget::HexagonSubtarget(const Triple &TT, StringRef CPU,
81 StringRef FS, const TargetMachine &TM)
82 : HexagonGenSubtargetInfo(TT, CPU, FS), OptLevel(TM.getOptLevel()),
83 CPUString(Hexagon_MC::selectHexagonCPU(CPU)),
84 InstrInfo(initializeSubtargetDependencies(CPU, FS)),
85 RegInfo(getHwMode()), TLInfo(TM, *this),
86 InstrItins(getInstrItineraryForCPU(CPUString)) {
87 // Beware of the default constructor of InstrItineraryData: it will
88 // reset all members to 0.
89 assert(InstrItins.Itineraries != nullptr && "InstrItins not initialized");
93 HexagonSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS) {
94 static std::map<StringRef, Hexagon::ArchEnum> CpuTable{
95 {"generic", Hexagon::ArchEnum::V60},
96 {"hexagonv4", Hexagon::ArchEnum::V4},
97 {"hexagonv5", Hexagon::ArchEnum::V5},
98 {"hexagonv55", Hexagon::ArchEnum::V55},
99 {"hexagonv60", Hexagon::ArchEnum::V60},
100 {"hexagonv62", Hexagon::ArchEnum::V62},
101 {"hexagonv65", Hexagon::ArchEnum::V65},
104 auto FoundIt = CpuTable.find(CPUString);
105 if (FoundIt != CpuTable.end())
106 HexagonArchVersion = FoundIt->second;
108 llvm_unreachable("Unrecognized Hexagon processor version");
110 UseHVX128BOps = false;
111 UseHVX64BOps = false;
112 UseLongCalls = false;
114 UseBSBScheduling = hasV60Ops() && EnableBSBSched;
116 ParseSubtargetFeatures(CPUString, FS);
118 if (OverrideLongCalls.getPosition())
119 UseLongCalls = OverrideLongCalls;
121 FeatureBitset Features = getFeatureBits();
122 if (HexagonDisableDuplex)
123 setFeatureBits(Features.set(Hexagon::FeatureDuplex, false));
124 setFeatureBits(Hexagon_MC::completeHVXFeatures(Features));
129 void HexagonSubtarget::UsrOverflowMutation::apply(ScheduleDAGInstrs *DAG) {
130 for (SUnit &SU : DAG->SUnits) {
133 SmallVector<SDep, 4> Erase;
134 for (auto &D : SU.Preds)
135 if (D.getKind() == SDep::Output && D.getReg() == Hexagon::USR_OVF)
137 for (auto &E : Erase)
142 void HexagonSubtarget::HVXMemLatencyMutation::apply(ScheduleDAGInstrs *DAG) {
143 for (SUnit &SU : DAG->SUnits) {
144 // Update the latency of chain edges between v60 vector load or store
145 // instructions to be 1. These instruction cannot be scheduled in the
147 MachineInstr &MI1 = *SU.getInstr();
148 auto *QII = static_cast<const HexagonInstrInfo*>(DAG->TII);
149 bool IsStoreMI1 = MI1.mayStore();
150 bool IsLoadMI1 = MI1.mayLoad();
151 if (!QII->isHVXVec(MI1) || !(IsStoreMI1 || IsLoadMI1))
153 for (SDep &SI : SU.Succs) {
154 if (SI.getKind() != SDep::Order || SI.getLatency() != 0)
156 MachineInstr &MI2 = *SI.getSUnit()->getInstr();
157 if (!QII->isHVXVec(MI2))
159 if ((IsStoreMI1 && MI2.mayStore()) || (IsLoadMI1 && MI2.mayLoad())) {
162 // Change the dependence in the opposite direction too.
163 for (SDep &PI : SI.getSUnit()->Preds) {
164 if (PI.getSUnit() != &SU || PI.getKind() != SDep::Order)
167 SI.getSUnit()->setDepthDirty();
174 // Check if a call and subsequent A2_tfrpi instructions should maintain
175 // scheduling affinity. We are looking for the TFRI to be consumed in
176 // the next instruction. This should help reduce the instances of
177 // double register pairs being allocated and scheduled before a call
178 // when not used until after the call. This situation is exacerbated
179 // by the fact that we allocate the pair from the callee saves list,
180 // leading to excess spills and restores.
181 bool HexagonSubtarget::CallMutation::shouldTFRICallBind(
182 const HexagonInstrInfo &HII, const SUnit &Inst1,
183 const SUnit &Inst2) const {
184 if (Inst1.getInstr()->getOpcode() != Hexagon::A2_tfrpi)
187 // TypeXTYPE are 64 bit operations.
188 unsigned Type = HII.getType(*Inst2.getInstr());
189 return Type == HexagonII::TypeS_2op || Type == HexagonII::TypeS_3op ||
190 Type == HexagonII::TypeALU64 || Type == HexagonII::TypeM;
193 void HexagonSubtarget::CallMutation::apply(ScheduleDAGInstrs *DAGInstrs) {
194 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
195 SUnit* LastSequentialCall = nullptr;
196 // Map from virtual register to physical register from the copy.
197 DenseMap<unsigned, unsigned> VRegHoldingReg;
198 // Map from the physical register to the instruction that uses virtual
199 // register. This is used to create the barrier edge.
200 DenseMap<unsigned, SUnit *> LastVRegUse;
201 auto &TRI = *DAG->MF.getSubtarget().getRegisterInfo();
202 auto &HII = *DAG->MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
204 // Currently we only catch the situation when compare gets scheduled
205 // before preceding call.
206 for (unsigned su = 0, e = DAG->SUnits.size(); su != e; ++su) {
207 // Remember the call.
208 if (DAG->SUnits[su].getInstr()->isCall())
209 LastSequentialCall = &DAG->SUnits[su];
210 // Look for a compare that defines a predicate.
211 else if (DAG->SUnits[su].getInstr()->isCompare() && LastSequentialCall)
212 DAG->addEdge(&DAG->SUnits[su], SDep(LastSequentialCall, SDep::Barrier));
213 // Look for call and tfri* instructions.
214 else if (SchedPredsCloser && LastSequentialCall && su > 1 && su < e-1 &&
215 shouldTFRICallBind(HII, DAG->SUnits[su], DAG->SUnits[su+1]))
216 DAG->addEdge(&DAG->SUnits[su], SDep(&DAG->SUnits[su-1], SDep::Barrier));
217 // Prevent redundant register copies due to reads and writes of physical
218 // registers. The original motivation for this was the code generated
219 // between two calls, which are caused both the return value and the
220 // argument for the next call being in %r0.
223 // 2: %vreg = COPY %r0
227 // The scheduler would often swap 3 and 4, so an additional register is
228 // needed. This code inserts a Barrier dependence between 3 & 4 to prevent
230 // The code below checks for all the physical registers, not just R0/D0/V0.
231 else if (SchedRetvalOptimization) {
232 const MachineInstr *MI = DAG->SUnits[su].getInstr();
234 TargetRegisterInfo::isPhysicalRegister(MI->getOperand(1).getReg())) {
236 VRegHoldingReg[MI->getOperand(0).getReg()] = MI->getOperand(1).getReg();
237 LastVRegUse.erase(MI->getOperand(1).getReg());
239 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
240 const MachineOperand &MO = MI->getOperand(i);
243 if (MO.isUse() && !MI->isCopy() &&
244 VRegHoldingReg.count(MO.getReg())) {
246 LastVRegUse[VRegHoldingReg[MO.getReg()]] = &DAG->SUnits[su];
247 } else if (MO.isDef() &&
248 TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
249 for (MCRegAliasIterator AI(MO.getReg(), &TRI, true); AI.isValid();
251 if (LastVRegUse.count(*AI) &&
252 LastVRegUse[*AI] != &DAG->SUnits[su])
254 DAG->addEdge(&DAG->SUnits[su], SDep(LastVRegUse[*AI], SDep::Barrier));
255 LastVRegUse.erase(*AI);
264 void HexagonSubtarget::BankConflictMutation::apply(ScheduleDAGInstrs *DAG) {
265 if (!EnableCheckBankConflict)
268 const auto &HII = static_cast<const HexagonInstrInfo&>(*DAG->TII);
270 // Create artificial edges between loads that could likely cause a bank
271 // conflict. Since such loads would normally not have any dependency
272 // between them, we cannot rely on existing edges.
273 for (unsigned i = 0, e = DAG->SUnits.size(); i != e; ++i) {
274 SUnit &S0 = DAG->SUnits[i];
275 MachineInstr &L0 = *S0.getInstr();
276 if (!L0.mayLoad() || L0.mayStore() ||
277 HII.getAddrMode(L0) != HexagonII::BaseImmOffset)
281 unsigned Base0 = HII.getBaseAndOffset(L0, Offset0, Size0);
282 // Is the access size is longer than the L1 cache line, skip the check.
283 if (Base0 == 0 || Size0 >= 32)
285 // Scan only up to 32 instructions ahead (to avoid n^2 complexity).
286 for (unsigned j = i+1, m = std::min(i+32, e); j != m; ++j) {
287 SUnit &S1 = DAG->SUnits[j];
288 MachineInstr &L1 = *S1.getInstr();
289 if (!L1.mayLoad() || L1.mayStore() ||
290 HII.getAddrMode(L1) != HexagonII::BaseImmOffset)
294 unsigned Base1 = HII.getBaseAndOffset(L1, Offset1, Size1);
295 if (Base1 == 0 || Size1 >= 32 || Base0 != Base1)
297 // Check bits 3 and 4 of the offset: if they differ, a bank conflict
299 if (((Offset0 ^ Offset1) & 0x18) != 0)
301 // Bits 3 and 4 are the same, add an artificial edge and set extra
303 SDep A(&S0, SDep::Artificial);
310 /// Enable use of alias analysis during code generation (during MI
311 /// scheduling, DAGCombine, etc.).
312 bool HexagonSubtarget::useAA() const {
313 if (OptLevel != CodeGenOpt::None)
318 /// Perform target specific adjustments to the latency of a schedule
320 void HexagonSubtarget::adjustSchedDependency(SUnit *Src, SUnit *Dst,
322 MachineInstr *SrcInst = Src->getInstr();
323 MachineInstr *DstInst = Dst->getInstr();
324 if (!Src->isInstr() || !Dst->isInstr())
327 const HexagonInstrInfo *QII = getInstrInfo();
329 // Instructions with .new operands have zero latency.
330 SmallSet<SUnit *, 4> ExclSrc;
331 SmallSet<SUnit *, 4> ExclDst;
332 if (QII->canExecuteInBundle(*SrcInst, *DstInst) &&
333 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
341 // Set the latency for a copy to zero since we hope that is will get removed.
342 if (DstInst->isCopy())
345 // If it's a REG_SEQUENCE/COPY, use its destination instruction to determine
346 // the correct latency.
347 if ((DstInst->isRegSequence() || DstInst->isCopy()) && Dst->NumSuccs == 1) {
348 unsigned DReg = DstInst->getOperand(0).getReg();
349 MachineInstr *DDst = Dst->Succs[0].getSUnit()->getInstr();
350 unsigned UseIdx = -1;
351 for (unsigned OpNum = 0; OpNum < DDst->getNumOperands(); OpNum++) {
352 const MachineOperand &MO = DDst->getOperand(OpNum);
353 if (MO.isReg() && MO.getReg() && MO.isUse() && MO.getReg() == DReg) {
358 int DLatency = (InstrInfo.getOperandLatency(&InstrItins, *SrcInst,
360 DLatency = std::max(DLatency, 0);
361 Dep.setLatency((unsigned)DLatency);
364 // Try to schedule uses near definitions to generate .cur.
367 if (EnableDotCurSched && QII->isToBeScheduledASAP(*SrcInst, *DstInst) &&
368 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
373 updateLatency(*SrcInst, *DstInst, Dep);
376 void HexagonSubtarget::getPostRAMutations(
377 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
378 Mutations.push_back(llvm::make_unique<UsrOverflowMutation>());
379 Mutations.push_back(llvm::make_unique<HVXMemLatencyMutation>());
380 Mutations.push_back(llvm::make_unique<BankConflictMutation>());
383 void HexagonSubtarget::getSMSMutations(
384 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
385 Mutations.push_back(llvm::make_unique<UsrOverflowMutation>());
386 Mutations.push_back(llvm::make_unique<HVXMemLatencyMutation>());
389 // Pin the vtable to this file.
390 void HexagonSubtarget::anchor() {}
392 bool HexagonSubtarget::enableMachineScheduler() const {
393 if (DisableHexagonMISched.getNumOccurrences())
394 return !DisableHexagonMISched;
398 bool HexagonSubtarget::usePredicatedCalls() const {
399 return EnablePredicatedCalls;
402 void HexagonSubtarget::updateLatency(MachineInstr &SrcInst,
403 MachineInstr &DstInst, SDep &Dep) const {
404 if (Dep.isArtificial()) {
412 auto &QII = static_cast<const HexagonInstrInfo&>(*getInstrInfo());
415 if (QII.isHVXVec(SrcInst) || useBSBScheduling())
416 Dep.setLatency((Dep.getLatency() + 1) >> 1);
419 void HexagonSubtarget::restoreLatency(SUnit *Src, SUnit *Dst) const {
420 MachineInstr *SrcI = Src->getInstr();
421 for (auto &I : Src->Succs) {
422 if (!I.isAssignedRegDep() || I.getSUnit() != Dst)
424 unsigned DepR = I.getReg();
426 for (unsigned OpNum = 0; OpNum < SrcI->getNumOperands(); OpNum++) {
427 const MachineOperand &MO = SrcI->getOperand(OpNum);
428 if (MO.isReg() && MO.isDef() && MO.getReg() == DepR)
431 assert(DefIdx >= 0 && "Def Reg not found in Src MI");
432 MachineInstr *DstI = Dst->getInstr();
434 for (unsigned OpNum = 0; OpNum < DstI->getNumOperands(); OpNum++) {
435 const MachineOperand &MO = DstI->getOperand(OpNum);
436 if (MO.isReg() && MO.isUse() && MO.getReg() == DepR) {
437 int Latency = (InstrInfo.getOperandLatency(&InstrItins, *SrcI,
438 DefIdx, *DstI, OpNum));
440 // For some instructions (ex: COPY), we might end up with < 0 latency
441 // as they don't have any Itinerary class associated with them.
442 Latency = std::max(Latency, 0);
444 I.setLatency(Latency);
445 updateLatency(*SrcI, *DstI, I);
449 // Update the latency of opposite edge too.
451 auto F = std::find(Dst->Preds.begin(), Dst->Preds.end(), T);
452 assert(F != Dst->Preds.end());
453 F->setLatency(I.getLatency());
457 /// Change the latency between the two SUnits.
458 void HexagonSubtarget::changeLatency(SUnit *Src, SUnit *Dst, unsigned Lat)
460 for (auto &I : Src->Succs) {
461 if (!I.isAssignedRegDep() || I.getSUnit() != Dst)
466 // Update the latency of opposite edge too.
468 auto F = std::find(Dst->Preds.begin(), Dst->Preds.end(), T);
469 assert(F != Dst->Preds.end());
474 /// If the SUnit has a zero latency edge, return the other SUnit.
475 static SUnit *getZeroLatency(SUnit *N, SmallVector<SDep, 4> &Deps) {
477 if (I.isAssignedRegDep() && I.getLatency() == 0 &&
478 !I.getSUnit()->getInstr()->isPseudo())
483 // Return true if these are the best two instructions to schedule
484 // together with a zero latency. Only one dependence should have a zero
485 // latency. If there are multiple choices, choose the best, and change
486 // the others, if needed.
487 bool HexagonSubtarget::isBestZeroLatency(SUnit *Src, SUnit *Dst,
488 const HexagonInstrInfo *TII, SmallSet<SUnit*, 4> &ExclSrc,
489 SmallSet<SUnit*, 4> &ExclDst) const {
490 MachineInstr &SrcInst = *Src->getInstr();
491 MachineInstr &DstInst = *Dst->getInstr();
493 // Ignore Boundary SU nodes as these have null instructions.
494 if (Dst->isBoundaryNode())
497 if (SrcInst.isPHI() || DstInst.isPHI())
500 if (!TII->isToBeScheduledASAP(SrcInst, DstInst) &&
501 !TII->canExecuteInBundle(SrcInst, DstInst))
504 // The architecture doesn't allow three dependent instructions in the same
505 // packet. So, if the destination has a zero latency successor, then it's
506 // not a candidate for a zero latency predecessor.
507 if (getZeroLatency(Dst, Dst->Succs) != nullptr)
510 // Check if the Dst instruction is the best candidate first.
511 SUnit *Best = nullptr;
512 SUnit *DstBest = nullptr;
513 SUnit *SrcBest = getZeroLatency(Dst, Dst->Preds);
514 if (SrcBest == nullptr || Src->NodeNum >= SrcBest->NodeNum) {
515 // Check that Src doesn't have a better candidate.
516 DstBest = getZeroLatency(Src, Src->Succs);
517 if (DstBest == nullptr || Dst->NodeNum <= DstBest->NodeNum)
523 // The caller frequently adds the same dependence twice. If so, then
524 // return true for this case too.
525 if ((Src == SrcBest && Dst == DstBest ) ||
526 (SrcBest == nullptr && Dst == DstBest) ||
527 (Src == SrcBest && Dst == nullptr))
530 // Reassign the latency for the previous bests, which requires setting
531 // the dependence edge in both directions.
532 if (SrcBest != nullptr) {
534 changeLatency(SrcBest, Dst, 1);
536 restoreLatency(SrcBest, Dst);
538 if (DstBest != nullptr) {
540 changeLatency(Src, DstBest, 1);
542 restoreLatency(Src, DstBest);
545 // Attempt to find another opprotunity for zero latency in a different
547 if (SrcBest && DstBest)
548 // If there is an edge from SrcBest to DstBst, then try to change that
550 changeLatency(SrcBest, DstBest, 0);
552 // Check if the previous best destination instruction has a new zero
553 // latency dependence opportunity.
555 for (auto &I : DstBest->Preds)
556 if (ExclSrc.count(I.getSUnit()) == 0 &&
557 isBestZeroLatency(I.getSUnit(), DstBest, TII, ExclSrc, ExclDst))
558 changeLatency(I.getSUnit(), DstBest, 0);
559 } else if (SrcBest) {
560 // Check if previous best source instruction has a new zero latency
561 // dependence opportunity.
563 for (auto &I : SrcBest->Succs)
564 if (ExclDst.count(I.getSUnit()) == 0 &&
565 isBestZeroLatency(SrcBest, I.getSUnit(), TII, ExclSrc, ExclDst))
566 changeLatency(SrcBest, I.getSUnit(), 0);
572 unsigned HexagonSubtarget::getL1CacheLineSize() const {
576 unsigned HexagonSubtarget::getL1PrefetchDistance() const {
580 bool HexagonSubtarget::enableSubRegLiveness() const {
581 return EnableSubregLiveness;