blob: d9fa3b2548e3db4df5f41ea4537dcad1d6e8435e [file] [log] [blame]
Eugene Zelenko8361b0a2017-06-19 22:43:19 +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
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014#include "Hexagon.h"
Eugene Zelenko8361b0a2017-06-19 22:43:19 +000015#include "HexagonInstrInfo.h"
Sirish Pande69295b82012-05-10 20:20:25 +000016#include "HexagonRegisterInfo.h"
Eugene Zelenko8361b0a2017-06-19 22:43:19 +000017#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"
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +000025#include "llvm/CodeGen/ScheduleDAG.h"
26#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko8361b0a2017-06-19 22:43:19 +000029#include <algorithm>
30#include <cassert>
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000031#include <map>
32
Tony Linthicum1213a7a2011-12-12 21:14:40 +000033using namespace llvm;
34
Chandler Carruthe96dd892014-04-21 22:55:11 +000035#define DEBUG_TYPE "hexagon-subtarget"
36
Tony Linthicum1213a7a2011-12-12 21:14:40 +000037#define GET_SUBTARGETINFO_CTOR
38#define GET_SUBTARGETINFO_TARGET_DESC
39#include "HexagonGenSubtargetInfo.inc"
40
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000041static 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"));
Jyotsna Vermafdc660b2013-03-22 18:41:34 +000044
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000045static 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"));
Sirish Pande69295b82012-05-10 20:20:25 +000048
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000049static 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."));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000052
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000053static cl::opt<bool> EnableBSBSched("enable-bsb-sched",
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000054 cl::Hidden, cl::ZeroOrMore, cl::init(true));
55
56static cl::opt<bool> EnableHexagonHVXDouble("enable-hexagon-hvx-double",
57 cl::Hidden, cl::ZeroOrMore, cl::init(false),
58 cl::desc("Enable Hexagon Double Vector eXtensions"));
59
60static cl::opt<bool> EnableHexagonHVX("enable-hexagon-hvx",
61 cl::Hidden, cl::ZeroOrMore, cl::init(false),
62 cl::desc("Enable Hexagon Vector eXtensions"));
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000063
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +000064static cl::opt<bool> EnableTCLatencySched("enable-tc-latency-sched",
65 cl::Hidden, cl::ZeroOrMore, cl::init(false));
66
67static cl::opt<bool> EnableDotCurSched("enable-cur-sched",
68 cl::Hidden, cl::ZeroOrMore, cl::init(true),
69 cl::desc("Enable the scheduler to generate .cur"));
70
71static cl::opt<bool> EnableVecFrwdSched("enable-evec-frwd-sched",
72 cl::Hidden, cl::ZeroOrMore, cl::init(true));
73
Eric Christopher5f141b02015-03-11 22:56:10 +000074static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000075 cl::Hidden, cl::ZeroOrMore, cl::init(false),
76 cl::desc("Disable Hexagon MI Scheduling"));
77
Krzysztof Parzyszek07d75182016-05-28 02:02:51 +000078static cl::opt<bool> EnableSubregLiveness("hexagon-subreg-liveness",
Krzysztof Parzyszekb5ec4872016-08-24 17:17:39 +000079 cl::Hidden, cl::ZeroOrMore, cl::init(true),
Krzysztof Parzyszek07d75182016-05-28 02:02:51 +000080 cl::desc("Enable subregister liveness tracking for Hexagon"));
81
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +000082static 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"));
85
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +000086static cl::opt<bool> EnablePredicatedCalls("hexagon-pred-calls",
87 cl::Hidden, cl::ZeroOrMore, cl::init(false),
88 cl::desc("Consider calls to be predicable"));
89
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +000090static cl::opt<bool> SchedPredsCloser("sched-preds-closer",
91 cl::Hidden, cl::ZeroOrMore, cl::init(true));
92
93static cl::opt<bool> SchedRetvalOptimization("sched-retval-optimization",
94 cl::Hidden, cl::ZeroOrMore, cl::init(true));
95
96
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000097void HexagonSubtarget::initializeEnvironment() {
98 UseMemOps = false;
99 ModeIEEERndNear = false;
100 UseBSBScheduling = false;
101}
Eric Christopher5f141b02015-03-11 22:56:10 +0000102
Eric Christopherc4c63ae2014-06-27 00:27:40 +0000103HexagonSubtarget &
104HexagonSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS) {
Krzysztof Parzyszek75e74ee2016-08-19 14:09:47 +0000105 CPUString = Hexagon_MC::selectHexagonCPU(getTargetTriple(), CPU);
Sebastian Pop1a0bef62012-08-20 19:56:47 +0000106
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000107 static std::map<StringRef, HexagonArchEnum> CpuTable {
108 { "hexagonv4", V4 },
109 { "hexagonv5", V5 },
110 { "hexagonv55", V55 },
111 { "hexagonv60", V60 },
Krzysztof Parzyszekf9015e62017-02-10 23:46:45 +0000112 { "hexagonv62", V62 },
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000113 };
114
115 auto foundIt = CpuTable.find(CPUString);
116 if (foundIt != CpuTable.end())
117 HexagonArchVersion = foundIt->second;
118 else
Sebastian Pop1a0bef62012-08-20 19:56:47 +0000119 llvm_unreachable("Unrecognized Hexagon processor version");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000120
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000121 UseHVXOps = false;
122 UseHVXDblOps = false;
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000123 UseLongCalls = false;
Sebastian Pop1a0bef62012-08-20 19:56:47 +0000124 ParseSubtargetFeatures(CPUString, FS);
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000125
126 if (EnableHexagonHVX.getPosition())
127 UseHVXOps = EnableHexagonHVX;
128 if (EnableHexagonHVXDouble.getPosition())
129 UseHVXDblOps = EnableHexagonHVXDouble;
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000130 if (OverrideLongCalls.getPosition())
131 UseLongCalls = OverrideLongCalls;
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000132
Eric Christopherc4c63ae2014-06-27 00:27:40 +0000133 return *this;
134}
135
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000136void HexagonSubtarget::UsrOverflowMutation::apply(ScheduleDAGInstrs *DAG) {
137 for (SUnit &SU : DAG->SUnits) {
138 if (!SU.isInstr())
139 continue;
140 SmallVector<SDep, 4> Erase;
141 for (auto &D : SU.Preds)
142 if (D.getKind() == SDep::Output && D.getReg() == Hexagon::USR_OVF)
143 Erase.push_back(D);
144 for (auto &E : Erase)
145 SU.removePred(E);
146 }
147}
148
149void HexagonSubtarget::HVXMemLatencyMutation::apply(ScheduleDAGInstrs *DAG) {
150 for (SUnit &SU : DAG->SUnits) {
151 // Update the latency of chain edges between v60 vector load or store
152 // instructions to be 1. These instruction cannot be scheduled in the
153 // same packet.
154 MachineInstr &MI1 = *SU.getInstr();
155 auto *QII = static_cast<const HexagonInstrInfo*>(DAG->TII);
156 bool IsStoreMI1 = MI1.mayStore();
157 bool IsLoadMI1 = MI1.mayLoad();
158 if (!QII->isHVXVec(MI1) || !(IsStoreMI1 || IsLoadMI1))
159 continue;
160 for (SDep &SI : SU.Succs) {
161 if (SI.getKind() != SDep::Order || SI.getLatency() != 0)
162 continue;
163 MachineInstr &MI2 = *SI.getSUnit()->getInstr();
164 if (!QII->isHVXVec(MI2))
165 continue;
166 if ((IsStoreMI1 && MI2.mayStore()) || (IsLoadMI1 && MI2.mayLoad())) {
167 SI.setLatency(1);
168 SU.setHeightDirty();
169 // Change the dependence in the opposite direction too.
170 for (SDep &PI : SI.getSUnit()->Preds) {
171 if (PI.getSUnit() != &SU || PI.getKind() != SDep::Order)
172 continue;
173 PI.setLatency(1);
174 SI.getSUnit()->setDepthDirty();
175 }
176 }
177 }
178 }
179}
180
181// Check if a call and subsequent A2_tfrpi instructions should maintain
182// scheduling affinity. We are looking for the TFRI to be consumed in
183// the next instruction. This should help reduce the instances of
184// double register pairs being allocated and scheduled before a call
185// when not used until after the call. This situation is exacerbated
186// by the fact that we allocate the pair from the callee saves list,
187// leading to excess spills and restores.
188bool HexagonSubtarget::CallMutation::shouldTFRICallBind(
189 const HexagonInstrInfo &HII, const SUnit &Inst1,
190 const SUnit &Inst2) const {
191 if (Inst1.getInstr()->getOpcode() != Hexagon::A2_tfrpi)
192 return false;
193
194 // TypeXTYPE are 64 bit operations.
195 unsigned Type = HII.getType(*Inst2.getInstr());
196 return Type == HexagonII::TypeS_2op || Type == HexagonII::TypeS_3op ||
197 Type == HexagonII::TypeALU64 || Type == HexagonII::TypeM;
198}
199
200void HexagonSubtarget::CallMutation::apply(ScheduleDAGInstrs *DAG) {
201 SUnit* LastSequentialCall = nullptr;
202 unsigned VRegHoldingRet = 0;
203 unsigned RetRegister;
204 SUnit* LastUseOfRet = nullptr;
205 auto &TRI = *DAG->MF.getSubtarget().getRegisterInfo();
206 auto &HII = *DAG->MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
207
208 // Currently we only catch the situation when compare gets scheduled
209 // before preceding call.
210 for (unsigned su = 0, e = DAG->SUnits.size(); su != e; ++su) {
211 // Remember the call.
212 if (DAG->SUnits[su].getInstr()->isCall())
213 LastSequentialCall = &DAG->SUnits[su];
214 // Look for a compare that defines a predicate.
215 else if (DAG->SUnits[su].getInstr()->isCompare() && LastSequentialCall)
216 DAG->SUnits[su].addPred(SDep(LastSequentialCall, SDep::Barrier));
217 // Look for call and tfri* instructions.
218 else if (SchedPredsCloser && LastSequentialCall && su > 1 && su < e-1 &&
219 shouldTFRICallBind(HII, DAG->SUnits[su], DAG->SUnits[su+1]))
220 DAG->SUnits[su].addPred(SDep(&DAG->SUnits[su-1], SDep::Barrier));
221 // Prevent redundant register copies between two calls, which are caused by
222 // both the return value and the argument for the next call being in %R0.
223 // Example:
224 // 1: <call1>
225 // 2: %VregX = COPY %R0
226 // 3: <use of %VregX>
227 // 4: %R0 = ...
228 // 5: <call2>
229 // The scheduler would often swap 3 and 4, so an additional register is
230 // needed. This code inserts a Barrier dependence between 3 & 4 to prevent
231 // this. The same applies for %D0 and %V0/%W0, which are also handled.
232 else if (SchedRetvalOptimization) {
233 const MachineInstr *MI = DAG->SUnits[su].getInstr();
234 if (MI->isCopy() && (MI->readsRegister(Hexagon::R0, &TRI) ||
235 MI->readsRegister(Hexagon::V0, &TRI))) {
236 // %vregX = COPY %R0
237 VRegHoldingRet = MI->getOperand(0).getReg();
238 RetRegister = MI->getOperand(1).getReg();
239 LastUseOfRet = nullptr;
240 } else if (VRegHoldingRet && MI->readsVirtualRegister(VRegHoldingRet))
241 // <use of %vregX>
242 LastUseOfRet = &DAG->SUnits[su];
243 else if (LastUseOfRet && MI->definesRegister(RetRegister, &TRI))
244 // %R0 = ...
245 DAG->SUnits[su].addPred(SDep(LastUseOfRet, SDep::Barrier));
246 }
247 }
248}
249
250
Daniel Sandersa73f1fd2015-06-10 12:11:26 +0000251HexagonSubtarget::HexagonSubtarget(const Triple &TT, StringRef CPU,
252 StringRef FS, const TargetMachine &TM)
Daniel Sanders50f17232015-09-15 16:17:27 +0000253 : HexagonGenSubtargetInfo(TT, CPU, FS), CPUString(CPU),
Eugene Zelenko8361b0a2017-06-19 22:43:19 +0000254 InstrInfo(initializeSubtargetDependencies(CPU, FS)), TLInfo(TM, *this) {
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000255 initializeEnvironment();
256
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000257 // Initialize scheduling itinerary for the specified CPU.
258 InstrItins = getInstrItineraryForCPU(CPUString);
259
Jyotsna Vermafdc660b2013-03-22 18:41:34 +0000260 // UseMemOps on by default unless disabled explicitly
261 if (DisableMemOps)
262 UseMemOps = false;
263 else if (EnableMemOps)
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000264 UseMemOps = true;
265 else
266 UseMemOps = false;
Sirish Pande69295b82012-05-10 20:20:25 +0000267
268 if (EnableIEEERndNear)
269 ModeIEEERndNear = true;
270 else
271 ModeIEEERndNear = false;
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +0000272
273 UseBSBScheduling = hasV60TOps() && EnableBSBSched;
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000274}
Sirish Pande69295b82012-05-10 20:20:25 +0000275
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000276/// \brief Perform target specific adjustments to the latency of a schedule
277/// dependency.
278void HexagonSubtarget::adjustSchedDependency(SUnit *Src, SUnit *Dst,
279 SDep &Dep) const {
280 MachineInstr *SrcInst = Src->getInstr();
281 MachineInstr *DstInst = Dst->getInstr();
282 if (!Src->isInstr() || !Dst->isInstr())
283 return;
284
285 const HexagonInstrInfo *QII = getInstrInfo();
286
287 // Instructions with .new operands have zero latency.
288 SmallSet<SUnit *, 4> ExclSrc;
289 SmallSet<SUnit *, 4> ExclDst;
290 if (QII->canExecuteInBundle(*SrcInst, *DstInst) &&
291 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
292 Dep.setLatency(0);
293 return;
294 }
295
296 if (!hasV60TOps())
297 return;
298
299 // If it's a REG_SEQUENCE, use its destination instruction to determine
300 // the correct latency.
301 if (DstInst->isRegSequence() && Dst->NumSuccs == 1) {
302 unsigned RSeqReg = DstInst->getOperand(0).getReg();
303 MachineInstr *RSeqDst = Dst->Succs[0].getSUnit()->getInstr();
304 unsigned UseIdx = -1;
305 for (unsigned OpNum = 0; OpNum < RSeqDst->getNumOperands(); OpNum++) {
306 const MachineOperand &MO = RSeqDst->getOperand(OpNum);
307 if (MO.isReg() && MO.getReg() && MO.isUse() && MO.getReg() == RSeqReg) {
308 UseIdx = OpNum;
309 break;
310 }
311 }
312 unsigned RSeqLatency = (InstrInfo.getOperandLatency(&InstrItins, *SrcInst,
313 0, *RSeqDst, UseIdx));
314 Dep.setLatency(RSeqLatency);
315 }
316
317 // Try to schedule uses near definitions to generate .cur.
318 ExclSrc.clear();
319 ExclDst.clear();
320 if (EnableDotCurSched && QII->isToBeScheduledASAP(*SrcInst, *DstInst) &&
321 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
322 Dep.setLatency(0);
323 return;
324 }
325
326 updateLatency(*SrcInst, *DstInst, Dep);
327}
328
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000329void HexagonSubtarget::getPostRAMutations(
Eugene Zelenko8361b0a2017-06-19 22:43:19 +0000330 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000331 Mutations.push_back(llvm::make_unique<UsrOverflowMutation>());
332 Mutations.push_back(llvm::make_unique<HVXMemLatencyMutation>());
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000333}
334
Krzysztof Parzyszek3885d872016-12-22 19:44:55 +0000335void HexagonSubtarget::getSMSMutations(
Eugene Zelenko8361b0a2017-06-19 22:43:19 +0000336 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000337 Mutations.push_back(llvm::make_unique<UsrOverflowMutation>());
338 Mutations.push_back(llvm::make_unique<HVXMemLatencyMutation>());
Krzysztof Parzyszek3885d872016-12-22 19:44:55 +0000339}
340
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000341// Pin the vtable to this file.
342void HexagonSubtarget::anchor() {}
Eric Christopher5f141b02015-03-11 22:56:10 +0000343
344bool HexagonSubtarget::enableMachineScheduler() const {
345 if (DisableHexagonMISched.getNumOccurrences())
346 return !DisableHexagonMISched;
347 return true;
348}
Krzysztof Parzyszek07d75182016-05-28 02:02:51 +0000349
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +0000350bool HexagonSubtarget::usePredicatedCalls() const {
351 return EnablePredicatedCalls;
352}
353
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000354void HexagonSubtarget::updateLatency(MachineInstr &SrcInst,
355 MachineInstr &DstInst, SDep &Dep) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000356 if (Dep.isArtificial()) {
357 Dep.setLatency(1);
358 return;
359 }
360
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000361 if (!hasV60TOps())
362 return;
363
364 auto &QII = static_cast<const HexagonInstrInfo&>(*getInstrInfo());
365
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000366 // BSB scheduling.
367 if (QII.isHVXVec(SrcInst) || useBSBScheduling())
368 Dep.setLatency((Dep.getLatency() + 1) >> 1);
369}
370
371void HexagonSubtarget::restoreLatency(SUnit *Src, SUnit *Dst) const {
372 MachineInstr *SrcI = Src->getInstr();
373 for (auto &I : Src->Succs) {
374 if (!I.isAssignedRegDep() || I.getSUnit() != Dst)
375 continue;
376 unsigned DepR = I.getReg();
377 int DefIdx = -1;
378 for (unsigned OpNum = 0; OpNum < SrcI->getNumOperands(); OpNum++) {
379 const MachineOperand &MO = SrcI->getOperand(OpNum);
380 if (MO.isReg() && MO.isDef() && MO.getReg() == DepR)
381 DefIdx = OpNum;
382 }
383 assert(DefIdx >= 0 && "Def Reg not found in Src MI");
384 MachineInstr *DstI = Dst->getInstr();
385 for (unsigned OpNum = 0; OpNum < DstI->getNumOperands(); OpNum++) {
386 const MachineOperand &MO = DstI->getOperand(OpNum);
387 if (MO.isReg() && MO.isUse() && MO.getReg() == DepR) {
388 int Latency = (InstrInfo.getOperandLatency(&InstrItins, *SrcI,
389 DefIdx, *DstI, OpNum));
390
391 // For some instructions (ex: COPY), we might end up with < 0 latency
392 // as they don't have any Itinerary class associated with them.
393 if (Latency <= 0)
394 Latency = 1;
395
396 I.setLatency(Latency);
397 updateLatency(*SrcI, *DstI, I);
398 }
399 }
400
401 // Update the latency of opposite edge too.
402 for (auto &J : Dst->Preds) {
403 if (J.getSUnit() != Src)
404 continue;
405 J.setLatency(I.getLatency());
406 }
407 }
408}
409
410/// Change the latency between the two SUnits.
411void HexagonSubtarget::changeLatency(SUnit *Src, SUnit *Dst, unsigned Lat)
412 const {
413 for (auto &I : Src->Succs) {
414 if (I.getSUnit() != Dst)
415 continue;
416 SDep T = I;
417 I.setLatency(Lat);
418
419 // Update the latency of opposite edge too.
420 T.setSUnit(Src);
421 auto F = std::find(Dst->Preds.begin(), Dst->Preds.end(), T);
422 assert(F != Dst->Preds.end());
423 F->setLatency(I.getLatency());
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000424 }
425}
426
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000427/// If the SUnit has a zero latency edge, return the other SUnit.
428static SUnit *getZeroLatency(SUnit *N, SmallVector<SDep, 4> &Deps) {
429 for (auto &I : Deps)
430 if (I.isAssignedRegDep() && I.getLatency() == 0 &&
431 !I.getSUnit()->getInstr()->isPseudo())
432 return I.getSUnit();
433 return nullptr;
434}
435
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000436// Return true if these are the best two instructions to schedule
437// together with a zero latency. Only one dependence should have a zero
438// latency. If there are multiple choices, choose the best, and change
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000439// the others, if needed.
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000440bool HexagonSubtarget::isBestZeroLatency(SUnit *Src, SUnit *Dst,
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000441 const HexagonInstrInfo *TII, SmallSet<SUnit*, 4> &ExclSrc,
442 SmallSet<SUnit*, 4> &ExclDst) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000443 MachineInstr &SrcInst = *Src->getInstr();
444 MachineInstr &DstInst = *Dst->getInstr();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000445
Ron Liebermanda5df7c2016-09-17 16:21:09 +0000446 // Ignore Boundary SU nodes as these have null instructions.
447 if (Dst->isBoundaryNode())
448 return false;
449
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000450 if (SrcInst.isPHI() || DstInst.isPHI())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000451 return false;
452
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000453 if (!TII->isToBeScheduledASAP(SrcInst, DstInst) &&
454 !TII->canExecuteInBundle(SrcInst, DstInst))
455 return false;
456
457 // The architecture doesn't allow three dependent instructions in the same
458 // packet. So, if the destination has a zero latency successor, then it's
459 // not a candidate for a zero latency predecessor.
460 if (getZeroLatency(Dst, Dst->Succs) != nullptr)
461 return false;
462
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000463 // Check if the Dst instruction is the best candidate first.
464 SUnit *Best = nullptr;
465 SUnit *DstBest = nullptr;
466 SUnit *SrcBest = getZeroLatency(Dst, Dst->Preds);
467 if (SrcBest == nullptr || Src->NodeNum >= SrcBest->NodeNum) {
468 // Check that Src doesn't have a better candidate.
469 DstBest = getZeroLatency(Src, Src->Succs);
470 if (DstBest == nullptr || Dst->NodeNum <= DstBest->NodeNum)
471 Best = Dst;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000472 }
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000473 if (Best != Dst)
474 return false;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000475
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000476 // The caller frequently adds the same dependence twice. If so, then
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000477 // return true for this case too.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000478 if ((Src == SrcBest && Dst == DstBest ) ||
479 (SrcBest == nullptr && Dst == DstBest) ||
480 (Src == SrcBest && Dst == nullptr))
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000481 return true;
482
483 // Reassign the latency for the previous bests, which requires setting
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000484 // the dependence edge in both directions.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000485 if (SrcBest != nullptr) {
486 if (!hasV60TOps())
487 changeLatency(SrcBest, Dst, 1);
488 else
489 restoreLatency(SrcBest, Dst);
490 }
491 if (DstBest != nullptr) {
492 if (!hasV60TOps())
493 changeLatency(Src, DstBest, 1);
494 else
495 restoreLatency(Src, DstBest);
496 }
497
498 // Attempt to find another opprotunity for zero latency in a different
499 // dependence.
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000500 if (SrcBest && DstBest)
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000501 // If there is an edge from SrcBest to DstBst, then try to change that
502 // to 0 now.
503 changeLatency(SrcBest, DstBest, 0);
504 else if (DstBest) {
505 // Check if the previous best destination instruction has a new zero
506 // latency dependence opportunity.
507 ExclSrc.insert(Src);
508 for (auto &I : DstBest->Preds)
509 if (ExclSrc.count(I.getSUnit()) == 0 &&
510 isBestZeroLatency(I.getSUnit(), DstBest, TII, ExclSrc, ExclDst))
511 changeLatency(I.getSUnit(), DstBest, 0);
512 } else if (SrcBest) {
513 // Check if previous best source instruction has a new zero latency
514 // dependence opportunity.
515 ExclDst.insert(Dst);
516 for (auto &I : SrcBest->Succs)
517 if (ExclDst.count(I.getSUnit()) == 0 &&
518 isBestZeroLatency(SrcBest, I.getSUnit(), TII, ExclSrc, ExclDst))
519 changeLatency(SrcBest, I.getSUnit(), 0);
520 }
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000521
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000522 return true;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000523}
524
Krzysztof Parzyszekd3d0a4b2016-07-22 14:22:43 +0000525unsigned HexagonSubtarget::getL1CacheLineSize() const {
526 return 32;
527}
528
529unsigned HexagonSubtarget::getL1PrefetchDistance() const {
530 return 32;
531}
532
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000533bool HexagonSubtarget::enableSubRegLiveness() const {
534 return EnableSubregLiveness;
535}