]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/Hexagon/HexagonSubtarget.cpp
Merge ^/head r319251 through r319479.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / Hexagon / HexagonSubtarget.cpp
1 //===-- HexagonSubtarget.cpp - Hexagon Subtarget Information --------------===//
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 the Hexagon specific subclass of TargetSubtarget.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "HexagonSubtarget.h"
15 #include "Hexagon.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"
21 #include <map>
22
23 using namespace llvm;
24
25 #define DEBUG_TYPE "hexagon-subtarget"
26
27 #define GET_SUBTARGETINFO_CTOR
28 #define GET_SUBTARGETINFO_TARGET_DESC
29 #include "HexagonGenSubtargetInfo.inc"
30
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"));
34
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"));
38
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."));
42
43 static cl::opt<bool> EnableBSBSched("enable-bsb-sched",
44   cl::Hidden, cl::ZeroOrMore, cl::init(true));
45
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"));
49
50 static cl::opt<bool> EnableHexagonHVX("enable-hexagon-hvx",
51   cl::Hidden, cl::ZeroOrMore, cl::init(false),
52   cl::desc("Enable Hexagon Vector eXtensions"));
53
54 static cl::opt<bool> EnableTCLatencySched("enable-tc-latency-sched",
55   cl::Hidden, cl::ZeroOrMore, cl::init(false));
56
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"));
60
61 static cl::opt<bool> EnableVecFrwdSched("enable-evec-frwd-sched",
62   cl::Hidden, cl::ZeroOrMore, cl::init(true));
63
64 static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
65   cl::Hidden, cl::ZeroOrMore, cl::init(false),
66   cl::desc("Disable Hexagon MI Scheduling"));
67
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"));
71
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"));
75
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"));
79
80 void HexagonSubtarget::initializeEnvironment() {
81   UseMemOps = false;
82   ModeIEEERndNear = false;
83   UseBSBScheduling = false;
84 }
85
86 HexagonSubtarget &
87 HexagonSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS) {
88   CPUString = Hexagon_MC::selectHexagonCPU(getTargetTriple(), CPU);
89
90   static std::map<StringRef, HexagonArchEnum> CpuTable {
91     { "hexagonv4", V4 },
92     { "hexagonv5", V5 },
93     { "hexagonv55", V55 },
94     { "hexagonv60", V60 },
95     { "hexagonv62", V62 },
96   };
97
98   auto foundIt = CpuTable.find(CPUString);
99   if (foundIt != CpuTable.end())
100     HexagonArchVersion = foundIt->second;
101   else
102     llvm_unreachable("Unrecognized Hexagon processor version");
103
104   UseHVXOps = false;
105   UseHVXDblOps = false;
106   UseLongCalls = false;
107   ParseSubtargetFeatures(CPUString, FS);
108
109   if (EnableHexagonHVX.getPosition())
110     UseHVXOps = EnableHexagonHVX;
111   if (EnableHexagonHVXDouble.getPosition())
112     UseHVXDblOps = EnableHexagonHVXDouble;
113   if (OverrideLongCalls.getPosition())
114     UseLongCalls = OverrideLongCalls;
115
116   return *this;
117 }
118
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),
123       FrameLowering() {
124
125   initializeEnvironment();
126
127   // Initialize scheduling itinerary for the specified CPU.
128   InstrItins = getInstrItineraryForCPU(CPUString);
129
130   // UseMemOps on by default unless disabled explicitly
131   if (DisableMemOps)
132     UseMemOps = false;
133   else if (EnableMemOps)
134     UseMemOps = true;
135   else
136     UseMemOps = false;
137
138   if (EnableIEEERndNear)
139     ModeIEEERndNear = true;
140   else
141     ModeIEEERndNear = false;
142
143   UseBSBScheduling = hasV60TOps() && EnableBSBSched;
144 }
145
146 /// \brief Perform target specific adjustments to the latency of a schedule
147 /// dependency.
148 void HexagonSubtarget::adjustSchedDependency(SUnit *Src, SUnit *Dst,
149                                              SDep &Dep) const {
150   MachineInstr *SrcInst = Src->getInstr();
151   MachineInstr *DstInst = Dst->getInstr();
152   if (!Src->isInstr() || !Dst->isInstr())
153     return;
154
155   const HexagonInstrInfo *QII = getInstrInfo();
156
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)) {
162     Dep.setLatency(0);
163     return;
164   }
165
166   if (!hasV60TOps())
167     return;
168
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) {
178         UseIdx = OpNum;
179         break;
180       }
181     }
182     unsigned RSeqLatency = (InstrInfo.getOperandLatency(&InstrItins, *SrcInst,
183                                                         0, *RSeqDst, UseIdx));
184     Dep.setLatency(RSeqLatency);
185   }
186
187   // Try to schedule uses near definitions to generate .cur.
188   ExclSrc.clear();
189   ExclDst.clear();
190   if (EnableDotCurSched && QII->isToBeScheduledASAP(*SrcInst, *DstInst) &&
191       isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
192     Dep.setLatency(0);
193     return;
194   }
195
196   updateLatency(*SrcInst, *DstInst, Dep);
197 }
198
199
200 void HexagonSubtarget::HexagonDAGMutation::apply(ScheduleDAGInstrs *DAG) {
201   for (auto &SU : DAG->SUnits) {
202     if (!SU.isInstr())
203       continue;
204     SmallVector<SDep, 4> Erase;
205     for (auto &D : SU.Preds)
206       if (D.getKind() == SDep::Output && D.getReg() == Hexagon::USR_OVF)
207         Erase.push_back(D);
208     for (auto &E : Erase)
209       SU.removePred(E);
210   }
211
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
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 (auto &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 (auto &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
244 void HexagonSubtarget::getPostRAMutations(
245       std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
246   Mutations.push_back(make_unique<HexagonSubtarget::HexagonDAGMutation>());
247 }
248
249 void HexagonSubtarget::getSMSMutations(
250       std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
251   Mutations.push_back(make_unique<HexagonSubtarget::HexagonDAGMutation>());
252 }
253
254
255 // Pin the vtable to this file.
256 void HexagonSubtarget::anchor() {}
257
258 bool HexagonSubtarget::enableMachineScheduler() const {
259   if (DisableHexagonMISched.getNumOccurrences())
260     return !DisableHexagonMISched;
261   return true;
262 }
263
264 bool HexagonSubtarget::usePredicatedCalls() const {
265   return EnablePredicatedCalls;
266 }
267
268 void HexagonSubtarget::updateLatency(MachineInstr &SrcInst,
269       MachineInstr &DstInst, SDep &Dep) const {
270   if (Dep.isArtificial()) {
271     Dep.setLatency(1);
272     return;
273   }
274
275   if (!hasV60TOps())
276     return;
277
278   auto &QII = static_cast<const HexagonInstrInfo&>(*getInstrInfo());
279
280   // BSB scheduling.
281   if (QII.isHVXVec(SrcInst) || useBSBScheduling())
282     Dep.setLatency((Dep.getLatency() + 1) >> 1);
283 }
284
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)
289       continue;
290     unsigned DepR = I.getReg();
291     int DefIdx = -1;
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)
295         DefIdx = OpNum;
296     }
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));
304
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.
307         if (Latency <= 0)
308           Latency = 1;
309
310         I.setLatency(Latency);
311         updateLatency(*SrcI, *DstI, I);
312       }
313     }
314
315     // Update the latency of opposite edge too.
316     for (auto &J : Dst->Preds) {
317       if (J.getSUnit() != Src)
318         continue;
319       J.setLatency(I.getLatency());
320     }
321   }
322 }
323
324 /// Change the latency between the two SUnits.
325 void HexagonSubtarget::changeLatency(SUnit *Src, SUnit *Dst, unsigned Lat)
326       const {
327   for (auto &I : Src->Succs) {
328     if (I.getSUnit() != Dst)
329       continue;
330     SDep T = I;
331     I.setLatency(Lat);
332
333     // Update the latency of opposite edge too.
334     T.setSUnit(Src);
335     auto F = std::find(Dst->Preds.begin(), Dst->Preds.end(), T);
336     assert(F != Dst->Preds.end());
337     F->setLatency(I.getLatency());
338   }
339 }
340
341 /// If the SUnit has a zero latency edge, return the other SUnit.
342 static SUnit *getZeroLatency(SUnit *N, SmallVector<SDep, 4> &Deps) {
343   for (auto &I : Deps)
344     if (I.isAssignedRegDep() && I.getLatency() == 0 &&
345         !I.getSUnit()->getInstr()->isPseudo())
346       return I.getSUnit();
347   return nullptr;
348 }
349
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();
359
360   // Ignore Boundary SU nodes as these have null instructions.
361   if (Dst->isBoundaryNode())
362     return false;
363
364   if (SrcInst.isPHI() || DstInst.isPHI())
365     return false;
366
367   if (!TII->isToBeScheduledASAP(SrcInst, DstInst) &&
368       !TII->canExecuteInBundle(SrcInst, DstInst))
369     return false;
370
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)
375     return false;
376
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)
385       Best = Dst;
386   }
387   if (Best != Dst)
388     return false;
389
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))
395     return true;
396
397   // Reassign the latency for the previous bests, which requires setting
398   // the dependence edge in both directions.
399   if (SrcBest != nullptr) {
400     if (!hasV60TOps())
401       changeLatency(SrcBest, Dst, 1);
402     else
403       restoreLatency(SrcBest, Dst);
404   }
405   if (DstBest != nullptr) {
406     if (!hasV60TOps())
407       changeLatency(Src, DstBest, 1);
408     else
409       restoreLatency(Src, DstBest);
410   }
411
412   // Attempt to find another opprotunity for zero latency in a different
413   // dependence.
414   if (SrcBest && DstBest)
415     // If there is an edge from SrcBest to DstBst, then try to change that
416     // to 0 now.
417     changeLatency(SrcBest, DstBest, 0);
418   else if (DstBest) {
419     // Check if the previous best destination instruction has a new zero
420     // latency dependence opportunity.
421     ExclSrc.insert(Src);
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.
429     ExclDst.insert(Dst);
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);
434   }
435
436   return true;
437 }
438
439 unsigned HexagonSubtarget::getL1CacheLineSize() const {
440   return 32;
441 }
442
443 unsigned HexagonSubtarget::getL1PrefetchDistance() const {
444   return 32;
445 }
446
447 bool HexagonSubtarget::enableSubRegLiveness() const {
448   return EnableSubregLiveness;
449 }
450