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 //===----------------------------------------------------------------------===//
14 #include "HexagonSubtarget.h"
16 #include "HexagonRegisterInfo.h"
17 #include "llvm/CodeGen/ScheduleDAG.h"
18 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/ErrorHandling.h"
25 #define DEBUG_TYPE "hexagon-subtarget"
27 #define GET_SUBTARGETINFO_CTOR
28 #define GET_SUBTARGETINFO_TARGET_DESC
29 #include "HexagonGenSubtargetInfo.inc"
31 static cl::opt<bool> EnableMemOps("enable-hexagon-memops",
32 cl::Hidden, cl::ZeroOrMore, cl::ValueDisallowed, cl::init(true),
33 cl::desc("Generate V4 MEMOP in code generation for Hexagon target"));
35 static cl::opt<bool> DisableMemOps("disable-hexagon-memops",
36 cl::Hidden, cl::ZeroOrMore, cl::ValueDisallowed, cl::init(false),
37 cl::desc("Do not generate V4 MEMOP in code generation for Hexagon target"));
39 static cl::opt<bool> EnableIEEERndNear("enable-hexagon-ieee-rnd-near",
40 cl::Hidden, cl::ZeroOrMore, cl::init(false),
41 cl::desc("Generate non-chopped conversion from fp to int."));
43 static cl::opt<bool> EnableBSBSched("enable-bsb-sched",
44 cl::Hidden, cl::ZeroOrMore, cl::init(true));
46 static cl::opt<bool> EnableHexagonHVXDouble("enable-hexagon-hvx-double",
47 cl::Hidden, cl::ZeroOrMore, cl::init(false),
48 cl::desc("Enable Hexagon Double Vector eXtensions"));
50 static cl::opt<bool> EnableHexagonHVX("enable-hexagon-hvx",
51 cl::Hidden, cl::ZeroOrMore, cl::init(false),
52 cl::desc("Enable Hexagon Vector eXtensions"));
54 static cl::opt<bool> EnableTCLatencySched("enable-tc-latency-sched",
55 cl::Hidden, cl::ZeroOrMore, cl::init(false));
57 static cl::opt<bool> EnableDotCurSched("enable-cur-sched",
58 cl::Hidden, cl::ZeroOrMore, cl::init(true),
59 cl::desc("Enable the scheduler to generate .cur"));
61 static cl::opt<bool> EnableVecFrwdSched("enable-evec-frwd-sched",
62 cl::Hidden, cl::ZeroOrMore, cl::init(true));
64 static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
65 cl::Hidden, cl::ZeroOrMore, cl::init(false),
66 cl::desc("Disable Hexagon MI Scheduling"));
68 static cl::opt<bool> EnableSubregLiveness("hexagon-subreg-liveness",
69 cl::Hidden, cl::ZeroOrMore, cl::init(true),
70 cl::desc("Enable subregister liveness tracking for Hexagon"));
72 static cl::opt<bool> OverrideLongCalls("hexagon-long-calls",
73 cl::Hidden, cl::ZeroOrMore, cl::init(false),
74 cl::desc("If present, forces/disables the use of long calls"));
76 static cl::opt<bool> EnablePredicatedCalls("hexagon-pred-calls",
77 cl::Hidden, cl::ZeroOrMore, cl::init(false),
78 cl::desc("Consider calls to be predicable"));
80 void HexagonSubtarget::initializeEnvironment() {
82 ModeIEEERndNear = false;
83 UseBSBScheduling = false;
87 HexagonSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS) {
88 CPUString = Hexagon_MC::selectHexagonCPU(getTargetTriple(), CPU);
90 static std::map<StringRef, HexagonArchEnum> CpuTable {
93 { "hexagonv55", V55 },
94 { "hexagonv60", V60 },
95 { "hexagonv62", V62 },
98 auto foundIt = CpuTable.find(CPUString);
99 if (foundIt != CpuTable.end())
100 HexagonArchVersion = foundIt->second;
102 llvm_unreachable("Unrecognized Hexagon processor version");
105 UseHVXDblOps = false;
106 UseLongCalls = false;
107 ParseSubtargetFeatures(CPUString, FS);
109 if (EnableHexagonHVX.getPosition())
110 UseHVXOps = EnableHexagonHVX;
111 if (EnableHexagonHVXDouble.getPosition())
112 UseHVXDblOps = EnableHexagonHVXDouble;
113 if (OverrideLongCalls.getPosition())
114 UseLongCalls = OverrideLongCalls;
119 HexagonSubtarget::HexagonSubtarget(const Triple &TT, StringRef CPU,
120 StringRef FS, const TargetMachine &TM)
121 : HexagonGenSubtargetInfo(TT, CPU, FS), CPUString(CPU),
122 InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
125 initializeEnvironment();
127 // Initialize scheduling itinerary for the specified CPU.
128 InstrItins = getInstrItineraryForCPU(CPUString);
130 // UseMemOps on by default unless disabled explicitly
133 else if (EnableMemOps)
138 if (EnableIEEERndNear)
139 ModeIEEERndNear = true;
141 ModeIEEERndNear = false;
143 UseBSBScheduling = hasV60TOps() && EnableBSBSched;
146 /// \brief Perform target specific adjustments to the latency of a schedule
148 void HexagonSubtarget::adjustSchedDependency(SUnit *Src, SUnit *Dst,
150 MachineInstr *SrcInst = Src->getInstr();
151 MachineInstr *DstInst = Dst->getInstr();
152 if (!Src->isInstr() || !Dst->isInstr())
155 const HexagonInstrInfo *QII = getInstrInfo();
157 // Instructions with .new operands have zero latency.
158 SmallSet<SUnit *, 4> ExclSrc;
159 SmallSet<SUnit *, 4> ExclDst;
160 if (QII->canExecuteInBundle(*SrcInst, *DstInst) &&
161 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
169 // If it's a REG_SEQUENCE, use its destination instruction to determine
170 // the correct latency.
171 if (DstInst->isRegSequence() && Dst->NumSuccs == 1) {
172 unsigned RSeqReg = DstInst->getOperand(0).getReg();
173 MachineInstr *RSeqDst = Dst->Succs[0].getSUnit()->getInstr();
174 unsigned UseIdx = -1;
175 for (unsigned OpNum = 0; OpNum < RSeqDst->getNumOperands(); OpNum++) {
176 const MachineOperand &MO = RSeqDst->getOperand(OpNum);
177 if (MO.isReg() && MO.getReg() && MO.isUse() && MO.getReg() == RSeqReg) {
182 unsigned RSeqLatency = (InstrInfo.getOperandLatency(&InstrItins, *SrcInst,
183 0, *RSeqDst, UseIdx));
184 Dep.setLatency(RSeqLatency);
187 // Try to schedule uses near definitions to generate .cur.
190 if (EnableDotCurSched && QII->isToBeScheduledASAP(*SrcInst, *DstInst) &&
191 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
196 updateLatency(*SrcInst, *DstInst, Dep);
200 void HexagonSubtarget::HexagonDAGMutation::apply(ScheduleDAGInstrs *DAG) {
201 for (auto &SU : DAG->SUnits) {
204 SmallVector<SDep, 4> Erase;
205 for (auto &D : SU.Preds)
206 if (D.getKind() == SDep::Output && D.getReg() == Hexagon::USR_OVF)
208 for (auto &E : Erase)
212 for (auto &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
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))
222 for (auto &SI : SU.Succs) {
223 if (SI.getKind() != SDep::Order || SI.getLatency() != 0)
225 MachineInstr &MI2 = *SI.getSUnit()->getInstr();
226 if (!QII->isHVXVec(MI2))
228 if ((IsStoreMI1 && MI2.mayStore()) || (IsLoadMI1 && MI2.mayLoad())) {
231 // Change the dependence in the opposite direction too.
232 for (auto &PI : SI.getSUnit()->Preds) {
233 if (PI.getSUnit() != &SU || PI.getKind() != SDep::Order)
236 SI.getSUnit()->setDepthDirty();
244 void HexagonSubtarget::getPostRAMutations(
245 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
246 Mutations.push_back(make_unique<HexagonSubtarget::HexagonDAGMutation>());
249 void HexagonSubtarget::getSMSMutations(
250 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
251 Mutations.push_back(make_unique<HexagonSubtarget::HexagonDAGMutation>());
255 // Pin the vtable to this file.
256 void HexagonSubtarget::anchor() {}
258 bool HexagonSubtarget::enableMachineScheduler() const {
259 if (DisableHexagonMISched.getNumOccurrences())
260 return !DisableHexagonMISched;
264 bool HexagonSubtarget::usePredicatedCalls() const {
265 return EnablePredicatedCalls;
268 void HexagonSubtarget::updateLatency(MachineInstr &SrcInst,
269 MachineInstr &DstInst, SDep &Dep) const {
270 if (Dep.isArtificial()) {
278 auto &QII = static_cast<const HexagonInstrInfo&>(*getInstrInfo());
281 if (QII.isHVXVec(SrcInst) || useBSBScheduling())
282 Dep.setLatency((Dep.getLatency() + 1) >> 1);
285 void HexagonSubtarget::restoreLatency(SUnit *Src, SUnit *Dst) const {
286 MachineInstr *SrcI = Src->getInstr();
287 for (auto &I : Src->Succs) {
288 if (!I.isAssignedRegDep() || I.getSUnit() != Dst)
290 unsigned DepR = I.getReg();
292 for (unsigned OpNum = 0; OpNum < SrcI->getNumOperands(); OpNum++) {
293 const MachineOperand &MO = SrcI->getOperand(OpNum);
294 if (MO.isReg() && MO.isDef() && MO.getReg() == DepR)
297 assert(DefIdx >= 0 && "Def Reg not found in Src MI");
298 MachineInstr *DstI = Dst->getInstr();
299 for (unsigned OpNum = 0; OpNum < DstI->getNumOperands(); OpNum++) {
300 const MachineOperand &MO = DstI->getOperand(OpNum);
301 if (MO.isReg() && MO.isUse() && MO.getReg() == DepR) {
302 int Latency = (InstrInfo.getOperandLatency(&InstrItins, *SrcI,
303 DefIdx, *DstI, OpNum));
305 // For some instructions (ex: COPY), we might end up with < 0 latency
306 // as they don't have any Itinerary class associated with them.
310 I.setLatency(Latency);
311 updateLatency(*SrcI, *DstI, I);
315 // Update the latency of opposite edge too.
316 for (auto &J : Dst->Preds) {
317 if (J.getSUnit() != Src)
319 J.setLatency(I.getLatency());
324 /// Change the latency between the two SUnits.
325 void HexagonSubtarget::changeLatency(SUnit *Src, SUnit *Dst, unsigned Lat)
327 for (auto &I : Src->Succs) {
328 if (I.getSUnit() != Dst)
333 // Update the latency of opposite edge too.
335 auto F = std::find(Dst->Preds.begin(), Dst->Preds.end(), T);
336 assert(F != Dst->Preds.end());
337 F->setLatency(I.getLatency());
341 /// If the SUnit has a zero latency edge, return the other SUnit.
342 static SUnit *getZeroLatency(SUnit *N, SmallVector<SDep, 4> &Deps) {
344 if (I.isAssignedRegDep() && I.getLatency() == 0 &&
345 !I.getSUnit()->getInstr()->isPseudo())
350 // Return true if these are the best two instructions to schedule
351 // together with a zero latency. Only one dependence should have a zero
352 // latency. If there are multiple choices, choose the best, and change
353 // the others, if needed.
354 bool HexagonSubtarget::isBestZeroLatency(SUnit *Src, SUnit *Dst,
355 const HexagonInstrInfo *TII, SmallSet<SUnit*, 4> &ExclSrc,
356 SmallSet<SUnit*, 4> &ExclDst) const {
357 MachineInstr &SrcInst = *Src->getInstr();
358 MachineInstr &DstInst = *Dst->getInstr();
360 // Ignore Boundary SU nodes as these have null instructions.
361 if (Dst->isBoundaryNode())
364 if (SrcInst.isPHI() || DstInst.isPHI())
367 if (!TII->isToBeScheduledASAP(SrcInst, DstInst) &&
368 !TII->canExecuteInBundle(SrcInst, DstInst))
371 // The architecture doesn't allow three dependent instructions in the same
372 // packet. So, if the destination has a zero latency successor, then it's
373 // not a candidate for a zero latency predecessor.
374 if (getZeroLatency(Dst, Dst->Succs) != nullptr)
377 // Check if the Dst instruction is the best candidate first.
378 SUnit *Best = nullptr;
379 SUnit *DstBest = nullptr;
380 SUnit *SrcBest = getZeroLatency(Dst, Dst->Preds);
381 if (SrcBest == nullptr || Src->NodeNum >= SrcBest->NodeNum) {
382 // Check that Src doesn't have a better candidate.
383 DstBest = getZeroLatency(Src, Src->Succs);
384 if (DstBest == nullptr || Dst->NodeNum <= DstBest->NodeNum)
390 // The caller frequently adds the same dependence twice. If so, then
391 // return true for this case too.
392 if ((Src == SrcBest && Dst == DstBest ) ||
393 (SrcBest == nullptr && Dst == DstBest) ||
394 (Src == SrcBest && Dst == nullptr))
397 // Reassign the latency for the previous bests, which requires setting
398 // the dependence edge in both directions.
399 if (SrcBest != nullptr) {
401 changeLatency(SrcBest, Dst, 1);
403 restoreLatency(SrcBest, Dst);
405 if (DstBest != nullptr) {
407 changeLatency(Src, DstBest, 1);
409 restoreLatency(Src, DstBest);
412 // Attempt to find another opprotunity for zero latency in a different
414 if (SrcBest && DstBest)
415 // If there is an edge from SrcBest to DstBst, then try to change that
417 changeLatency(SrcBest, DstBest, 0);
419 // Check if the previous best destination instruction has a new zero
420 // latency dependence opportunity.
422 for (auto &I : DstBest->Preds)
423 if (ExclSrc.count(I.getSUnit()) == 0 &&
424 isBestZeroLatency(I.getSUnit(), DstBest, TII, ExclSrc, ExclDst))
425 changeLatency(I.getSUnit(), DstBest, 0);
426 } else if (SrcBest) {
427 // Check if previous best source instruction has a new zero latency
428 // dependence opportunity.
430 for (auto &I : SrcBest->Succs)
431 if (ExclDst.count(I.getSUnit()) == 0 &&
432 isBestZeroLatency(SrcBest, I.getSUnit(), TII, ExclSrc, ExclDst))
433 changeLatency(SrcBest, I.getSUnit(), 0);
439 unsigned HexagonSubtarget::getL1CacheLineSize() const {
443 unsigned HexagonSubtarget::getL1PrefetchDistance() const {
447 bool HexagonSubtarget::enableSubRegLiveness() const {
448 return EnableSubregLiveness;