blob: a3ec0483113db2afb079969924268b9c3fa95670 [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonSubtarget.cpp - Hexagon Subtarget Information --------------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
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"
Sirish Pande69295b82012-05-10 20:20:25 +000016#include "HexagonRegisterInfo.h"
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +000017#include "llvm/CodeGen/ScheduleDAG.h"
18#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/ErrorHandling.h"
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000021#include <map>
22
Tony Linthicum1213a7a2011-12-12 21:14:40 +000023using namespace llvm;
24
Chandler Carruthe96dd892014-04-21 22:55:11 +000025#define DEBUG_TYPE "hexagon-subtarget"
26
Tony Linthicum1213a7a2011-12-12 21:14:40 +000027#define GET_SUBTARGETINFO_CTOR
28#define GET_SUBTARGETINFO_TARGET_DESC
29#include "HexagonGenSubtargetInfo.inc"
30
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000031static 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"));
Jyotsna Vermafdc660b2013-03-22 18:41:34 +000034
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000035static 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"));
Sirish Pande69295b82012-05-10 20:20:25 +000038
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000039static 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."));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000042
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000043static cl::opt<bool> EnableBSBSched("enable-bsb-sched",
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000044 cl::Hidden, cl::ZeroOrMore, cl::init(true));
45
46static 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
50static cl::opt<bool> EnableHexagonHVX("enable-hexagon-hvx",
51 cl::Hidden, cl::ZeroOrMore, cl::init(false),
52 cl::desc("Enable Hexagon Vector eXtensions"));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000053
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +000054static cl::opt<bool> EnableTCLatencySched("enable-tc-latency-sched",
55 cl::Hidden, cl::ZeroOrMore, cl::init(false));
56
57static 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
61static cl::opt<bool> EnableVecFrwdSched("enable-evec-frwd-sched",
62 cl::Hidden, cl::ZeroOrMore, cl::init(true));
63
Eric Christopher5f141b02015-03-11 22:56:10 +000064static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000065 cl::Hidden, cl::ZeroOrMore, cl::init(false),
66 cl::desc("Disable Hexagon MI Scheduling"));
67
Krzysztof Parzyszek07d75182016-05-28 02:02:51 +000068static cl::opt<bool> EnableSubregLiveness("hexagon-subreg-liveness",
Krzysztof Parzyszekb5ec4872016-08-24 17:17:39 +000069 cl::Hidden, cl::ZeroOrMore, cl::init(true),
Krzysztof Parzyszek07d75182016-05-28 02:02:51 +000070 cl::desc("Enable subregister liveness tracking for Hexagon"));
71
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +000072static 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
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000076void HexagonSubtarget::initializeEnvironment() {
77 UseMemOps = false;
78 ModeIEEERndNear = false;
79 UseBSBScheduling = false;
80}
Eric Christopher5f141b02015-03-11 22:56:10 +000081
Eric Christopherc4c63ae2014-06-27 00:27:40 +000082HexagonSubtarget &
83HexagonSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS) {
Krzysztof Parzyszek75e74ee2016-08-19 14:09:47 +000084 CPUString = Hexagon_MC::selectHexagonCPU(getTargetTriple(), CPU);
Sebastian Pop1a0bef62012-08-20 19:56:47 +000085
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000086 static std::map<StringRef, HexagonArchEnum> CpuTable {
87 { "hexagonv4", V4 },
88 { "hexagonv5", V5 },
89 { "hexagonv55", V55 },
90 { "hexagonv60", V60 },
91 };
92
93 auto foundIt = CpuTable.find(CPUString);
94 if (foundIt != CpuTable.end())
95 HexagonArchVersion = foundIt->second;
96 else
Sebastian Pop1a0bef62012-08-20 19:56:47 +000097 llvm_unreachable("Unrecognized Hexagon processor version");
Tony Linthicum1213a7a2011-12-12 21:14:40 +000098
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000099 UseHVXOps = false;
100 UseHVXDblOps = false;
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000101 UseLongCalls = false;
Sebastian Pop1a0bef62012-08-20 19:56:47 +0000102 ParseSubtargetFeatures(CPUString, FS);
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000103
104 if (EnableHexagonHVX.getPosition())
105 UseHVXOps = EnableHexagonHVX;
106 if (EnableHexagonHVXDouble.getPosition())
107 UseHVXDblOps = EnableHexagonHVXDouble;
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000108 if (OverrideLongCalls.getPosition())
109 UseLongCalls = OverrideLongCalls;
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000110
Eric Christopherc4c63ae2014-06-27 00:27:40 +0000111 return *this;
112}
113
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000114HexagonSubtarget::HexagonSubtarget(const Triple &TT, StringRef CPU,
115 StringRef FS, const TargetMachine &TM)
Daniel Sanders50f17232015-09-15 16:17:27 +0000116 : HexagonGenSubtargetInfo(TT, CPU, FS), CPUString(CPU),
Eric Christopherd737b762015-02-02 22:11:36 +0000117 InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM, *this),
Mehdi Amini157e5a62015-07-09 02:10:08 +0000118 FrameLowering() {
Sebastian Pop1a0bef62012-08-20 19:56:47 +0000119
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000120 initializeEnvironment();
121
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000122 // Initialize scheduling itinerary for the specified CPU.
123 InstrItins = getInstrItineraryForCPU(CPUString);
124
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000125 // UseMemOps on by default unless disabled explicitly
126 if (DisableMemOps)
127 UseMemOps = false;
128 else if (EnableMemOps)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129 UseMemOps = true;
130 else
131 UseMemOps = false;
Sirish Pande69295b82012-05-10 20:20:25 +0000132
133 if (EnableIEEERndNear)
134 ModeIEEERndNear = true;
135 else
136 ModeIEEERndNear = false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000137
138 UseBSBScheduling = hasV60TOps() && EnableBSBSched;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000139}
Sirish Pande69295b82012-05-10 20:20:25 +0000140
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000141
142void HexagonSubtarget::HexagonDAGMutation::apply(ScheduleDAGInstrs *DAG) {
143 for (auto &SU : DAG->SUnits) {
144 if (!SU.isInstr())
145 continue;
146 SmallVector<SDep, 4> Erase;
147 for (auto &D : SU.Preds)
148 if (D.getKind() == SDep::Output && D.getReg() == Hexagon::USR_OVF)
149 Erase.push_back(D);
150 for (auto &E : Erase)
151 SU.removePred(E);
152 }
153
154 for (auto &SU : DAG->SUnits) {
155 // Update the latency of chain edges between v60 vector load or store
156 // instructions to be 1. These instructions cannot be scheduled in the
157 // same packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000158 MachineInstr &MI1 = *SU.getInstr();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000159 auto *QII = static_cast<const HexagonInstrInfo*>(DAG->TII);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000160 bool IsStoreMI1 = MI1.mayStore();
161 bool IsLoadMI1 = MI1.mayLoad();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000162 if (!QII->isV60VectorInstruction(MI1) || !(IsStoreMI1 || IsLoadMI1))
163 continue;
164 for (auto &SI : SU.Succs) {
165 if (SI.getKind() != SDep::Order || SI.getLatency() != 0)
166 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000167 MachineInstr &MI2 = *SI.getSUnit()->getInstr();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000168 if (!QII->isV60VectorInstruction(MI2))
169 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000170 if ((IsStoreMI1 && MI2.mayStore()) || (IsLoadMI1 && MI2.mayLoad())) {
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000171 SI.setLatency(1);
172 SU.setHeightDirty();
173 // Change the dependence in the opposite direction too.
174 for (auto &PI : SI.getSUnit()->Preds) {
175 if (PI.getSUnit() != &SU || PI.getKind() != SDep::Order)
176 continue;
177 PI.setLatency(1);
178 SI.getSUnit()->setDepthDirty();
179 }
180 }
181 }
182 }
183}
184
185
186void HexagonSubtarget::getPostRAMutations(
187 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
188 Mutations.push_back(make_unique<HexagonSubtarget::HexagonDAGMutation>());
189}
190
191
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000192// Pin the vtable to this file.
193void HexagonSubtarget::anchor() {}
Eric Christopher5f141b02015-03-11 22:56:10 +0000194
195bool HexagonSubtarget::enableMachineScheduler() const {
196 if (DisableHexagonMISched.getNumOccurrences())
197 return !DisableHexagonMISched;
198 return true;
199}
Krzysztof Parzyszek07d75182016-05-28 02:02:51 +0000200
201bool HexagonSubtarget::enableSubRegLiveness() const {
202 return EnableSubregLiveness;
203}
204
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000205// This helper function is responsible for increasing the latency only.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000206void HexagonSubtarget::updateLatency(MachineInstr &SrcInst,
207 MachineInstr &DstInst, SDep &Dep) const {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000208 if (!hasV60TOps())
209 return;
210
211 auto &QII = static_cast<const HexagonInstrInfo&>(*getInstrInfo());
212
213 if (EnableVecFrwdSched && QII.addLatencyToSchedule(SrcInst, DstInst)) {
214 // Vec frwd scheduling.
215 Dep.setLatency(Dep.getLatency() + 1);
216 } else if (useBSBScheduling() &&
217 QII.isLateInstrFeedsEarlyInstr(SrcInst, DstInst)) {
218 // BSB scheduling.
219 Dep.setLatency(Dep.getLatency() + 1);
220 } else if (EnableTCLatencySched) {
221 // TClass latency scheduling.
222 // Check if SrcInst produces in 2C an operand of DstInst taken in stage 2B.
223 if (QII.isTC1(SrcInst) || QII.isTC2(SrcInst))
224 if (!QII.isTC1(DstInst) && !QII.isTC2(DstInst))
225 Dep.setLatency(Dep.getLatency() + 1);
226 }
227}
228
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000229/// If the SUnit has a zero latency edge, return the other SUnit.
230static SUnit *getZeroLatency(SUnit *N, SmallVector<SDep, 4> &Deps) {
231 for (auto &I : Deps)
232 if (I.isAssignedRegDep() && I.getLatency() == 0 &&
233 !I.getSUnit()->getInstr()->isPseudo())
234 return I.getSUnit();
235 return nullptr;
236}
237
238/// Change the latency between the two SUnits.
239void HexagonSubtarget::changeLatency(SUnit *Src, SmallVector<SDep, 4> &Deps,
240 SUnit *Dst, unsigned Lat) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000241 MachineInstr &SrcI = *Src->getInstr();
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000242 for (auto &I : Deps) {
243 if (I.getSUnit() != Dst)
244 continue;
245 I.setLatency(Lat);
246 SUnit *UpdateDst = I.getSUnit();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000247 updateLatency(SrcI, *UpdateDst->getInstr(), I);
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000248 // Update the latency of opposite edge too.
249 for (auto &PI : UpdateDst->Preds) {
250 if (PI.getSUnit() != Src || !PI.isAssignedRegDep())
251 continue;
252 PI.setLatency(Lat);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000253 updateLatency(SrcI, *UpdateDst->getInstr(), PI);
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000254 }
255 }
256}
257
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000258// Return true if these are the best two instructions to schedule
259// together with a zero latency. Only one dependence should have a zero
260// latency. If there are multiple choices, choose the best, and change
261// ther others, if needed.
262bool HexagonSubtarget::isBestZeroLatency(SUnit *Src, SUnit *Dst,
263 const HexagonInstrInfo *TII) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000264 MachineInstr &SrcInst = *Src->getInstr();
265 MachineInstr &DstInst = *Dst->getInstr();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000266
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000267 if (SrcInst.isPHI() || DstInst.isPHI())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000268 return false;
269
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000270 // Check if the Dst instruction is the best candidate first.
271 SUnit *Best = nullptr;
272 SUnit *DstBest = nullptr;
273 SUnit *SrcBest = getZeroLatency(Dst, Dst->Preds);
274 if (SrcBest == nullptr || Src->NodeNum >= SrcBest->NodeNum) {
275 // Check that Src doesn't have a better candidate.
276 DstBest = getZeroLatency(Src, Src->Succs);
277 if (DstBest == nullptr || Dst->NodeNum <= DstBest->NodeNum)
278 Best = Dst;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000279 }
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000280 if (Best != Dst)
281 return false;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000282
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000283 // The caller frequents adds the same dependence twice. If so, then
284 // return true for this case too.
285 if (Src == SrcBest && Dst == DstBest)
286 return true;
287
288 // Reassign the latency for the previous bests, which requires setting
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000289 // the dependence edge in both directions.
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000290 if (SrcBest != nullptr)
291 changeLatency(SrcBest, SrcBest->Succs, Dst, 1);
292 if (DstBest != nullptr)
293 changeLatency(Src, Src->Succs, DstBest, 1);
294 // If there is an edge from SrcBest to DstBst, then try to change that
295 // to 0 now.
296 if (SrcBest && DstBest)
297 changeLatency(SrcBest, SrcBest->Succs, DstBest, 0);
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000298
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000299 return true;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000300}
301
302// Update the latency of a Phi when the Phi bridges two instructions that
303// require a multi-cycle latency.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000304void HexagonSubtarget::changePhiLatency(MachineInstr &SrcInst, SUnit *Dst,
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000305 SDep &Dep) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000306 if (!SrcInst.isPHI() || Dst->NumPreds == 0 || Dep.getLatency() != 0)
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000307 return;
308
309 for (const SDep &PI : Dst->Preds) {
310 if (PI.getLatency() != 0)
311 continue;
312 Dep.setLatency(2);
313 break;
314 }
315}
316
317/// \brief Perform target specific adjustments to the latency of a schedule
318/// dependency.
319void HexagonSubtarget::adjustSchedDependency(SUnit *Src, SUnit *Dst,
320 SDep &Dep) const {
321 MachineInstr *SrcInst = Src->getInstr();
322 MachineInstr *DstInst = Dst->getInstr();
323 if (!Src->isInstr() || !Dst->isInstr())
324 return;
325
326 const HexagonInstrInfo *QII = static_cast<const HexagonInstrInfo *>(getInstrInfo());
327
328 // Instructions with .new operands have zero latency.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000329 if (QII->canExecuteInBundle(*SrcInst, *DstInst) &&
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000330 isBestZeroLatency(Src, Dst, QII)) {
331 Dep.setLatency(0);
332 return;
333 }
334
335 if (!hasV60TOps())
336 return;
337
338 // Don't adjust the latency of post-increment part of the instruction.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000339 if (QII->isPostIncrement(*SrcInst) && Dep.isAssignedRegDep()) {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000340 if (SrcInst->mayStore())
341 return;
342 if (Dep.getReg() != SrcInst->getOperand(0).getReg())
343 return;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000344 } else if (QII->isPostIncrement(*DstInst) && Dep.getKind() == SDep::Anti) {
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000345 if (DstInst->mayStore())
346 return;
347 if (Dep.getReg() != DstInst->getOperand(0).getReg())
348 return;
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000349 } else if (QII->isPostIncrement(*DstInst) && DstInst->mayStore() &&
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000350 Dep.isAssignedRegDep()) {
351 MachineOperand &Op = DstInst->getOperand(DstInst->getNumOperands() - 1);
352 if (Op.isReg() && Dep.getReg() != Op.getReg())
353 return;
354 }
355
356 // Check if we need to change any the latency values when Phis are added.
357 if (useBSBScheduling() && SrcInst->isPHI()) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000358 changePhiLatency(*SrcInst, Dst, Dep);
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000359 return;
360 }
361
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000362 // If it's a REG_SEQUENCE, use its destination instruction to determine
363 // the correct latency.
364 if (DstInst->isRegSequence() && Dst->NumSuccs == 1)
365 DstInst = Dst->Succs[0].getSUnit()->getInstr();
366
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000367 // Try to schedule uses near definitions to generate .cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000368 if (EnableDotCurSched && QII->isToBeScheduledASAP(*SrcInst, *DstInst) &&
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000369 isBestZeroLatency(Src, Dst, QII)) {
370 Dep.setLatency(0);
371 return;
372 }
373
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000374 updateLatency(*SrcInst, *DstInst, Dep);
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000375}
376
Krzysztof Parzyszekd3d0a4b2016-07-22 14:22:43 +0000377unsigned HexagonSubtarget::getL1CacheLineSize() const {
378 return 32;
379}
380
381unsigned HexagonSubtarget::getL1PrefetchDistance() const {
382 return 32;
383}
384