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 "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "MCTargetDesc/HexagonMCTargetDesc.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineOperand.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"
35 #define DEBUG_TYPE "hexagon-subtarget"
37 #define GET_SUBTARGETINFO_CTOR
38 #define GET_SUBTARGETINFO_TARGET_DESC
39 #include "HexagonGenSubtargetInfo.inc"
41 static cl::opt<bool> EnableMemOps("enable-hexagon-memops",
42 cl::Hidden, cl::ZeroOrMore, cl::ValueDisallowed, cl::init(true),
43 cl::desc("Generate V4 MEMOP in code generation for Hexagon target"));
45 static cl::opt<bool> DisableMemOps("disable-hexagon-memops",
46 cl::Hidden, cl::ZeroOrMore, cl::ValueDisallowed, cl::init(false),
47 cl::desc("Do not generate V4 MEMOP in code generation for Hexagon target"));
49 static cl::opt<bool> EnableIEEERndNear("enable-hexagon-ieee-rnd-near",
50 cl::Hidden, cl::ZeroOrMore, cl::init(false),
51 cl::desc("Generate non-chopped conversion from fp to int."));
53 static cl::opt<bool> EnableBSBSched("enable-bsb-sched",
54 cl::Hidden, cl::ZeroOrMore, cl::init(true));
56 static cl::opt<bool> EnableHexagonHVXDouble("enable-hexagon-hvx-double",
57 cl::Hidden, cl::ZeroOrMore, cl::init(false),
58 cl::desc("Enable Hexagon Double Vector eXtensions"));
60 static cl::opt<bool> EnableHexagonHVX("enable-hexagon-hvx",
61 cl::Hidden, cl::ZeroOrMore, cl::init(false),
62 cl::desc("Enable Hexagon Vector eXtensions"));
64 static cl::opt<bool> EnableTCLatencySched("enable-tc-latency-sched",
65 cl::Hidden, cl::ZeroOrMore, cl::init(false));
67 static cl::opt<bool> EnableDotCurSched("enable-cur-sched",
68 cl::Hidden, cl::ZeroOrMore, cl::init(true),
69 cl::desc("Enable the scheduler to generate .cur"));
71 static cl::opt<bool> EnableVecFrwdSched("enable-evec-frwd-sched",
72 cl::Hidden, cl::ZeroOrMore, cl::init(true));
74 static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
75 cl::Hidden, cl::ZeroOrMore, cl::init(false),
76 cl::desc("Disable Hexagon MI Scheduling"));
78 static cl::opt<bool> EnableSubregLiveness("hexagon-subreg-liveness",
79 cl::Hidden, cl::ZeroOrMore, cl::init(true),
80 cl::desc("Enable subregister liveness tracking for Hexagon"));
82 static cl::opt<bool> OverrideLongCalls("hexagon-long-calls",
83 cl::Hidden, cl::ZeroOrMore, cl::init(false),
84 cl::desc("If present, forces/disables the use of long calls"));
86 static cl::opt<bool> EnablePredicatedCalls("hexagon-pred-calls",
87 cl::Hidden, cl::ZeroOrMore, cl::init(false),
88 cl::desc("Consider calls to be predicable"));
90 void HexagonSubtarget::initializeEnvironment() {
92 ModeIEEERndNear = false;
93 UseBSBScheduling = false;
97 HexagonSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS) {
98 CPUString = Hexagon_MC::selectHexagonCPU(getTargetTriple(), CPU);
100 static std::map<StringRef, HexagonArchEnum> CpuTable {
103 { "hexagonv55", V55 },
104 { "hexagonv60", V60 },
105 { "hexagonv62", V62 },
108 auto foundIt = CpuTable.find(CPUString);
109 if (foundIt != CpuTable.end())
110 HexagonArchVersion = foundIt->second;
112 llvm_unreachable("Unrecognized Hexagon processor version");
115 UseHVXDblOps = false;
116 UseLongCalls = false;
117 ParseSubtargetFeatures(CPUString, FS);
119 if (EnableHexagonHVX.getPosition())
120 UseHVXOps = EnableHexagonHVX;
121 if (EnableHexagonHVXDouble.getPosition())
122 UseHVXDblOps = EnableHexagonHVXDouble;
123 if (OverrideLongCalls.getPosition())
124 UseLongCalls = OverrideLongCalls;
129 HexagonSubtarget::HexagonSubtarget(const Triple &TT, StringRef CPU,
130 StringRef FS, const TargetMachine &TM)
131 : HexagonGenSubtargetInfo(TT, CPU, FS), CPUString(CPU),
132 InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM, *this) {
133 initializeEnvironment();
135 // Initialize scheduling itinerary for the specified CPU.
136 InstrItins = getInstrItineraryForCPU(CPUString);
138 // UseMemOps on by default unless disabled explicitly
141 else if (EnableMemOps)
146 if (EnableIEEERndNear)
147 ModeIEEERndNear = true;
149 ModeIEEERndNear = false;
151 UseBSBScheduling = hasV60TOps() && EnableBSBSched;
154 /// \brief Perform target specific adjustments to the latency of a schedule
156 void HexagonSubtarget::adjustSchedDependency(SUnit *Src, SUnit *Dst,
158 MachineInstr *SrcInst = Src->getInstr();
159 MachineInstr *DstInst = Dst->getInstr();
160 if (!Src->isInstr() || !Dst->isInstr())
163 const HexagonInstrInfo *QII = getInstrInfo();
165 // Instructions with .new operands have zero latency.
166 SmallSet<SUnit *, 4> ExclSrc;
167 SmallSet<SUnit *, 4> ExclDst;
168 if (QII->canExecuteInBundle(*SrcInst, *DstInst) &&
169 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
177 // If it's a REG_SEQUENCE, use its destination instruction to determine
178 // the correct latency.
179 if (DstInst->isRegSequence() && Dst->NumSuccs == 1) {
180 unsigned RSeqReg = DstInst->getOperand(0).getReg();
181 MachineInstr *RSeqDst = Dst->Succs[0].getSUnit()->getInstr();
182 unsigned UseIdx = -1;
183 for (unsigned OpNum = 0; OpNum < RSeqDst->getNumOperands(); OpNum++) {
184 const MachineOperand &MO = RSeqDst->getOperand(OpNum);
185 if (MO.isReg() && MO.getReg() && MO.isUse() && MO.getReg() == RSeqReg) {
190 unsigned RSeqLatency = (InstrInfo.getOperandLatency(&InstrItins, *SrcInst,
191 0, *RSeqDst, UseIdx));
192 Dep.setLatency(RSeqLatency);
195 // Try to schedule uses near definitions to generate .cur.
198 if (EnableDotCurSched && QII->isToBeScheduledASAP(*SrcInst, *DstInst) &&
199 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
204 updateLatency(*SrcInst, *DstInst, Dep);
207 void HexagonSubtarget::HexagonDAGMutation::apply(ScheduleDAGInstrs *DAG) {
208 for (auto &SU : DAG->SUnits) {
211 SmallVector<SDep, 4> Erase;
212 for (auto &D : SU.Preds)
213 if (D.getKind() == SDep::Output && D.getReg() == Hexagon::USR_OVF)
215 for (auto &E : Erase)
219 for (auto &SU : DAG->SUnits) {
220 // Update the latency of chain edges between v60 vector load or store
221 // instructions to be 1. These instruction cannot be scheduled in the
223 MachineInstr &MI1 = *SU.getInstr();
224 auto *QII = static_cast<const HexagonInstrInfo*>(DAG->TII);
225 bool IsStoreMI1 = MI1.mayStore();
226 bool IsLoadMI1 = MI1.mayLoad();
227 if (!QII->isHVXVec(MI1) || !(IsStoreMI1 || IsLoadMI1))
229 for (auto &SI : SU.Succs) {
230 if (SI.getKind() != SDep::Order || SI.getLatency() != 0)
232 MachineInstr &MI2 = *SI.getSUnit()->getInstr();
233 if (!QII->isHVXVec(MI2))
235 if ((IsStoreMI1 && MI2.mayStore()) || (IsLoadMI1 && MI2.mayLoad())) {
238 // Change the dependence in the opposite direction too.
239 for (auto &PI : SI.getSUnit()->Preds) {
240 if (PI.getSUnit() != &SU || PI.getKind() != SDep::Order)
243 SI.getSUnit()->setDepthDirty();
250 void HexagonSubtarget::getPostRAMutations(
251 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
253 llvm::make_unique<HexagonSubtarget::HexagonDAGMutation>());
256 void HexagonSubtarget::getSMSMutations(
257 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
259 llvm::make_unique<HexagonSubtarget::HexagonDAGMutation>());
262 // Pin the vtable to this file.
263 void HexagonSubtarget::anchor() {}
265 bool HexagonSubtarget::enableMachineScheduler() const {
266 if (DisableHexagonMISched.getNumOccurrences())
267 return !DisableHexagonMISched;
271 bool HexagonSubtarget::usePredicatedCalls() const {
272 return EnablePredicatedCalls;
275 void HexagonSubtarget::updateLatency(MachineInstr &SrcInst,
276 MachineInstr &DstInst, SDep &Dep) const {
277 if (Dep.isArtificial()) {
285 auto &QII = static_cast<const HexagonInstrInfo&>(*getInstrInfo());
288 if (QII.isHVXVec(SrcInst) || useBSBScheduling())
289 Dep.setLatency((Dep.getLatency() + 1) >> 1);
292 void HexagonSubtarget::restoreLatency(SUnit *Src, SUnit *Dst) const {
293 MachineInstr *SrcI = Src->getInstr();
294 for (auto &I : Src->Succs) {
295 if (!I.isAssignedRegDep() || I.getSUnit() != Dst)
297 unsigned DepR = I.getReg();
299 for (unsigned OpNum = 0; OpNum < SrcI->getNumOperands(); OpNum++) {
300 const MachineOperand &MO = SrcI->getOperand(OpNum);
301 if (MO.isReg() && MO.isDef() && MO.getReg() == DepR)
304 assert(DefIdx >= 0 && "Def Reg not found in Src MI");
305 MachineInstr *DstI = Dst->getInstr();
306 for (unsigned OpNum = 0; OpNum < DstI->getNumOperands(); OpNum++) {
307 const MachineOperand &MO = DstI->getOperand(OpNum);
308 if (MO.isReg() && MO.isUse() && MO.getReg() == DepR) {
309 int Latency = (InstrInfo.getOperandLatency(&InstrItins, *SrcI,
310 DefIdx, *DstI, OpNum));
312 // For some instructions (ex: COPY), we might end up with < 0 latency
313 // as they don't have any Itinerary class associated with them.
317 I.setLatency(Latency);
318 updateLatency(*SrcI, *DstI, I);
322 // Update the latency of opposite edge too.
323 for (auto &J : Dst->Preds) {
324 if (J.getSUnit() != Src)
326 J.setLatency(I.getLatency());
331 /// Change the latency between the two SUnits.
332 void HexagonSubtarget::changeLatency(SUnit *Src, SUnit *Dst, unsigned Lat)
334 for (auto &I : Src->Succs) {
335 if (I.getSUnit() != Dst)
340 // Update the latency of opposite edge too.
342 auto F = std::find(Dst->Preds.begin(), Dst->Preds.end(), T);
343 assert(F != Dst->Preds.end());
344 F->setLatency(I.getLatency());
348 /// If the SUnit has a zero latency edge, return the other SUnit.
349 static SUnit *getZeroLatency(SUnit *N, SmallVector<SDep, 4> &Deps) {
351 if (I.isAssignedRegDep() && I.getLatency() == 0 &&
352 !I.getSUnit()->getInstr()->isPseudo())
357 // Return true if these are the best two instructions to schedule
358 // together with a zero latency. Only one dependence should have a zero
359 // latency. If there are multiple choices, choose the best, and change
360 // the others, if needed.
361 bool HexagonSubtarget::isBestZeroLatency(SUnit *Src, SUnit *Dst,
362 const HexagonInstrInfo *TII, SmallSet<SUnit*, 4> &ExclSrc,
363 SmallSet<SUnit*, 4> &ExclDst) const {
364 MachineInstr &SrcInst = *Src->getInstr();
365 MachineInstr &DstInst = *Dst->getInstr();
367 // Ignore Boundary SU nodes as these have null instructions.
368 if (Dst->isBoundaryNode())
371 if (SrcInst.isPHI() || DstInst.isPHI())
374 if (!TII->isToBeScheduledASAP(SrcInst, DstInst) &&
375 !TII->canExecuteInBundle(SrcInst, DstInst))
378 // The architecture doesn't allow three dependent instructions in the same
379 // packet. So, if the destination has a zero latency successor, then it's
380 // not a candidate for a zero latency predecessor.
381 if (getZeroLatency(Dst, Dst->Succs) != nullptr)
384 // Check if the Dst instruction is the best candidate first.
385 SUnit *Best = nullptr;
386 SUnit *DstBest = nullptr;
387 SUnit *SrcBest = getZeroLatency(Dst, Dst->Preds);
388 if (SrcBest == nullptr || Src->NodeNum >= SrcBest->NodeNum) {
389 // Check that Src doesn't have a better candidate.
390 DstBest = getZeroLatency(Src, Src->Succs);
391 if (DstBest == nullptr || Dst->NodeNum <= DstBest->NodeNum)
397 // The caller frequently adds the same dependence twice. If so, then
398 // return true for this case too.
399 if ((Src == SrcBest && Dst == DstBest ) ||
400 (SrcBest == nullptr && Dst == DstBest) ||
401 (Src == SrcBest && Dst == nullptr))
404 // Reassign the latency for the previous bests, which requires setting
405 // the dependence edge in both directions.
406 if (SrcBest != nullptr) {
408 changeLatency(SrcBest, Dst, 1);
410 restoreLatency(SrcBest, Dst);
412 if (DstBest != nullptr) {
414 changeLatency(Src, DstBest, 1);
416 restoreLatency(Src, DstBest);
419 // Attempt to find another opprotunity for zero latency in a different
421 if (SrcBest && DstBest)
422 // If there is an edge from SrcBest to DstBst, then try to change that
424 changeLatency(SrcBest, DstBest, 0);
426 // Check if the previous best destination instruction has a new zero
427 // latency dependence opportunity.
429 for (auto &I : DstBest->Preds)
430 if (ExclSrc.count(I.getSUnit()) == 0 &&
431 isBestZeroLatency(I.getSUnit(), DstBest, TII, ExclSrc, ExclDst))
432 changeLatency(I.getSUnit(), DstBest, 0);
433 } else if (SrcBest) {
434 // Check if previous best source instruction has a new zero latency
435 // dependence opportunity.
437 for (auto &I : SrcBest->Succs)
438 if (ExclDst.count(I.getSUnit()) == 0 &&
439 isBestZeroLatency(SrcBest, I.getSUnit(), TII, ExclSrc, ExclDst))
440 changeLatency(SrcBest, I.getSUnit(), 0);
446 unsigned HexagonSubtarget::getL1CacheLineSize() const {
450 unsigned HexagonSubtarget::getL1PrefetchDistance() const {
454 bool HexagonSubtarget::enableSubRegLiveness() const {
455 return EnableSubregLiveness;