blob: 781d3887959d81733c3b9bdc11767bbf22cc9efe [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"
Krzysztof Parzyszekdca38312018-03-20 12:28:43 +000018#include "MCTargetDesc/HexagonMCTargetDesc.h"
Eugene Zelenko8361b0a2017-06-19 22:43:19 +000019#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/SmallSet.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
Eugene Zelenko8361b0a2017-06-19 22:43:19 +000023#include "llvm/CodeGen/MachineInstr.h"
24#include "llvm/CodeGen/MachineOperand.h"
Krzysztof Parzyszekdca38312018-03-20 12:28:43 +000025#include "llvm/CodeGen/MachineScheduler.h"
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +000026#include "llvm/CodeGen/ScheduleDAG.h"
27#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000028#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko8361b0a2017-06-19 22:43:19 +000030#include <algorithm>
31#include <cassert>
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000032#include <map>
33
Tony Linthicum1213a7a2011-12-12 21:14:40 +000034using namespace llvm;
35
Chandler Carruthe96dd892014-04-21 22:55:11 +000036#define DEBUG_TYPE "hexagon-subtarget"
37
Tony Linthicum1213a7a2011-12-12 21:14:40 +000038#define GET_SUBTARGETINFO_CTOR
39#define GET_SUBTARGETINFO_TARGET_DESC
40#include "HexagonGenSubtargetInfo.inc"
41
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000042static cl::opt<bool> EnableMemOps("enable-hexagon-memops",
43 cl::Hidden, cl::ZeroOrMore, cl::ValueDisallowed, cl::init(true),
44 cl::desc("Generate V4 MEMOP in code generation for Hexagon target"));
Jyotsna Vermafdc660b2013-03-22 18:41:34 +000045
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000046static cl::opt<bool> DisableMemOps("disable-hexagon-memops",
47 cl::Hidden, cl::ZeroOrMore, cl::ValueDisallowed, cl::init(false),
48 cl::desc("Do not generate V4 MEMOP in code generation for Hexagon target"));
Sirish Pande69295b82012-05-10 20:20:25 +000049
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000050static cl::opt<bool> EnableIEEERndNear("enable-hexagon-ieee-rnd-near",
51 cl::Hidden, cl::ZeroOrMore, cl::init(false),
52 cl::desc("Generate non-chopped conversion from fp to int."));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000053
Krzysztof Parzyszekb9a1c3a2015-11-24 14:55:26 +000054static cl::opt<bool> EnableBSBSched("enable-bsb-sched",
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000055 cl::Hidden, cl::ZeroOrMore, cl::init(true));
56
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +000057static cl::opt<bool> EnableTCLatencySched("enable-tc-latency-sched",
58 cl::Hidden, cl::ZeroOrMore, cl::init(false));
59
60static cl::opt<bool> EnableDotCurSched("enable-cur-sched",
61 cl::Hidden, cl::ZeroOrMore, cl::init(true),
62 cl::desc("Enable the scheduler to generate .cur"));
63
64static cl::opt<bool> EnableVecFrwdSched("enable-evec-frwd-sched",
65 cl::Hidden, cl::ZeroOrMore, cl::init(true));
66
Eric Christopher5f141b02015-03-11 22:56:10 +000067static cl::opt<bool> DisableHexagonMISched("disable-hexagon-misched",
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +000068 cl::Hidden, cl::ZeroOrMore, cl::init(false),
69 cl::desc("Disable Hexagon MI Scheduling"));
70
Krzysztof Parzyszek07d75182016-05-28 02:02:51 +000071static cl::opt<bool> EnableSubregLiveness("hexagon-subreg-liveness",
Krzysztof Parzyszekb5ec4872016-08-24 17:17:39 +000072 cl::Hidden, cl::ZeroOrMore, cl::init(true),
Krzysztof Parzyszek07d75182016-05-28 02:02:51 +000073 cl::desc("Enable subregister liveness tracking for Hexagon"));
74
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +000075static cl::opt<bool> OverrideLongCalls("hexagon-long-calls",
76 cl::Hidden, cl::ZeroOrMore, cl::init(false),
77 cl::desc("If present, forces/disables the use of long calls"));
78
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +000079static cl::opt<bool> EnablePredicatedCalls("hexagon-pred-calls",
80 cl::Hidden, cl::ZeroOrMore, cl::init(false),
81 cl::desc("Consider calls to be predicable"));
82
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +000083static cl::opt<bool> SchedPredsCloser("sched-preds-closer",
84 cl::Hidden, cl::ZeroOrMore, cl::init(true));
85
86static cl::opt<bool> SchedRetvalOptimization("sched-retval-optimization",
87 cl::Hidden, cl::ZeroOrMore, cl::init(true));
88
Krzysztof Parzyszek2164a272017-08-28 18:36:21 +000089static cl::opt<bool> EnableCheckBankConflict("hexagon-check-bank-conflict",
90 cl::Hidden, cl::ZeroOrMore, cl::init(true),
91 cl::desc("Enable checking for cache bank conflicts"));
92
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +000093
Krzysztof Parzyszek1665b3d2017-09-26 15:06:37 +000094HexagonSubtarget::HexagonSubtarget(const Triple &TT, StringRef CPU,
95 StringRef FS, const TargetMachine &TM)
Krzysztof Parzyszekd7681422017-11-30 21:25:28 +000096 : HexagonGenSubtargetInfo(TT, CPU, FS), OptLevel(TM.getOptLevel()),
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000097 CPUString(Hexagon_MC::selectHexagonCPU(CPU)),
Krzysztof Parzyszek1665b3d2017-09-26 15:06:37 +000098 InstrInfo(initializeSubtargetDependencies(CPU, FS)),
99 RegInfo(getHwMode()), TLInfo(TM, *this),
100 InstrItins(getInstrItineraryForCPU(CPUString)) {
101 // Beware of the default constructor of InstrItineraryData: it will
102 // reset all members to 0.
103 assert(InstrItins.Itineraries != nullptr && "InstrItins not initialized");
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000104}
Eric Christopher5f141b02015-03-11 22:56:10 +0000105
Eric Christopherc4c63ae2014-06-27 00:27:40 +0000106HexagonSubtarget &
107HexagonSubtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS) {
Sumanth Gundapaneni9d954c42017-10-18 17:45:22 +0000108 static std::map<StringRef, Hexagon::ArchEnum> CpuTable{
109 {"hexagonv4", Hexagon::ArchEnum::V4},
110 {"hexagonv5", Hexagon::ArchEnum::V5},
111 {"hexagonv55", Hexagon::ArchEnum::V55},
112 {"hexagonv60", Hexagon::ArchEnum::V60},
113 {"hexagonv62", Hexagon::ArchEnum::V62},
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000114 {"hexagonv65", Hexagon::ArchEnum::V65},
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000115 };
116
Krzysztof Parzyszek1665b3d2017-09-26 15:06:37 +0000117 auto FoundIt = CpuTable.find(CPUString);
118 if (FoundIt != CpuTable.end())
119 HexagonArchVersion = FoundIt->second;
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000120 else
Sebastian Pop1a0bef62012-08-20 19:56:47 +0000121 llvm_unreachable("Unrecognized Hexagon processor version");
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000122
Sumanth Gundapanenie1983bc2017-10-18 18:07:07 +0000123 UseHVX128BOps = false;
124 UseHVX64BOps = false;
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000125 UseLongCalls = false;
Krzysztof Parzyszek1665b3d2017-09-26 15:06:37 +0000126
127 UseMemOps = DisableMemOps ? false : EnableMemOps;
128 ModeIEEERndNear = EnableIEEERndNear;
129 UseBSBScheduling = hasV60TOps() && EnableBSBSched;
130
Sebastian Pop1a0bef62012-08-20 19:56:47 +0000131 ParseSubtargetFeatures(CPUString, FS);
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000132
Krzysztof Parzyszek080bebd2016-07-25 14:42:11 +0000133 if (OverrideLongCalls.getPosition())
134 UseLongCalls = OverrideLongCalls;
Krzysztof Parzyszek207c13f2015-11-25 20:30:59 +0000135
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000136 FeatureBitset Features = getFeatureBits();
137 if (HexagonDisableDuplex)
138 setFeatureBits(Features.set(Hexagon::FeatureDuplex, false));
139 setFeatureBits(Hexagon_MC::completeHVXFeatures(Features));
140
Eric Christopherc4c63ae2014-06-27 00:27:40 +0000141 return *this;
142}
143
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000144void HexagonSubtarget::UsrOverflowMutation::apply(ScheduleDAGInstrs *DAG) {
145 for (SUnit &SU : DAG->SUnits) {
146 if (!SU.isInstr())
147 continue;
148 SmallVector<SDep, 4> Erase;
149 for (auto &D : SU.Preds)
150 if (D.getKind() == SDep::Output && D.getReg() == Hexagon::USR_OVF)
151 Erase.push_back(D);
152 for (auto &E : Erase)
153 SU.removePred(E);
154 }
155}
156
157void HexagonSubtarget::HVXMemLatencyMutation::apply(ScheduleDAGInstrs *DAG) {
158 for (SUnit &SU : DAG->SUnits) {
159 // Update the latency of chain edges between v60 vector load or store
160 // instructions to be 1. These instruction cannot be scheduled in the
161 // same packet.
162 MachineInstr &MI1 = *SU.getInstr();
163 auto *QII = static_cast<const HexagonInstrInfo*>(DAG->TII);
164 bool IsStoreMI1 = MI1.mayStore();
165 bool IsLoadMI1 = MI1.mayLoad();
166 if (!QII->isHVXVec(MI1) || !(IsStoreMI1 || IsLoadMI1))
167 continue;
168 for (SDep &SI : SU.Succs) {
169 if (SI.getKind() != SDep::Order || SI.getLatency() != 0)
170 continue;
171 MachineInstr &MI2 = *SI.getSUnit()->getInstr();
172 if (!QII->isHVXVec(MI2))
173 continue;
174 if ((IsStoreMI1 && MI2.mayStore()) || (IsLoadMI1 && MI2.mayLoad())) {
175 SI.setLatency(1);
176 SU.setHeightDirty();
177 // Change the dependence in the opposite direction too.
178 for (SDep &PI : SI.getSUnit()->Preds) {
179 if (PI.getSUnit() != &SU || PI.getKind() != SDep::Order)
180 continue;
181 PI.setLatency(1);
182 SI.getSUnit()->setDepthDirty();
183 }
184 }
185 }
186 }
187}
188
189// Check if a call and subsequent A2_tfrpi instructions should maintain
190// scheduling affinity. We are looking for the TFRI to be consumed in
191// the next instruction. This should help reduce the instances of
192// double register pairs being allocated and scheduled before a call
193// when not used until after the call. This situation is exacerbated
194// by the fact that we allocate the pair from the callee saves list,
195// leading to excess spills and restores.
196bool HexagonSubtarget::CallMutation::shouldTFRICallBind(
197 const HexagonInstrInfo &HII, const SUnit &Inst1,
198 const SUnit &Inst2) const {
199 if (Inst1.getInstr()->getOpcode() != Hexagon::A2_tfrpi)
200 return false;
201
202 // TypeXTYPE are 64 bit operations.
203 unsigned Type = HII.getType(*Inst2.getInstr());
204 return Type == HexagonII::TypeS_2op || Type == HexagonII::TypeS_3op ||
205 Type == HexagonII::TypeALU64 || Type == HexagonII::TypeM;
206}
207
Krzysztof Parzyszekdca38312018-03-20 12:28:43 +0000208void HexagonSubtarget::CallMutation::apply(ScheduleDAGInstrs *DAGInstrs) {
209 ScheduleDAGMI *DAG = static_cast<ScheduleDAGMI*>(DAGInstrs);
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000210 SUnit* LastSequentialCall = nullptr;
Krzysztof Parzyszekb4bb75d2018-03-21 17:23:32 +0000211 // Map from virtual register to physical register from the copy.
212 DenseMap<unsigned, unsigned> VRegHoldingReg;
213 // Map from the physical register to the instruction that uses virtual
214 // register. This is used to create the barrier edge.
215 DenseMap<unsigned, SUnit *> LastVRegUse;
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000216 auto &TRI = *DAG->MF.getSubtarget().getRegisterInfo();
217 auto &HII = *DAG->MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
218
219 // Currently we only catch the situation when compare gets scheduled
220 // before preceding call.
221 for (unsigned su = 0, e = DAG->SUnits.size(); su != e; ++su) {
222 // Remember the call.
223 if (DAG->SUnits[su].getInstr()->isCall())
224 LastSequentialCall = &DAG->SUnits[su];
225 // Look for a compare that defines a predicate.
226 else if (DAG->SUnits[su].getInstr()->isCompare() && LastSequentialCall)
Krzysztof Parzyszekdca38312018-03-20 12:28:43 +0000227 DAG->addEdge(&DAG->SUnits[su], SDep(LastSequentialCall, SDep::Barrier));
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000228 // Look for call and tfri* instructions.
229 else if (SchedPredsCloser && LastSequentialCall && su > 1 && su < e-1 &&
230 shouldTFRICallBind(HII, DAG->SUnits[su], DAG->SUnits[su+1]))
Krzysztof Parzyszekdca38312018-03-20 12:28:43 +0000231 DAG->addEdge(&DAG->SUnits[su], SDep(&DAG->SUnits[su-1], SDep::Barrier));
Krzysztof Parzyszekb4bb75d2018-03-21 17:23:32 +0000232 // Prevent redundant register copies due to reads and writes of physical
233 // registers. The original motivation for this was the code generated
234 // between two calls, which are caused both the return value and the
235 // argument for the next call being in %r0.
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000236 // Example:
237 // 1: <call1>
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +0000238 // 2: %vreg = COPY %r0
239 // 3: <use of %vreg>
Francis Visoiu Mistrih9d7bb0c2017-11-28 17:15:09 +0000240 // 4: %r0 = ...
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000241 // 5: <call2>
242 // The scheduler would often swap 3 and 4, so an additional register is
243 // needed. This code inserts a Barrier dependence between 3 & 4 to prevent
Krzysztof Parzyszekb4bb75d2018-03-21 17:23:32 +0000244 // this.
245 // The code below checks for all the physical registers, not just R0/D0/V0.
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000246 else if (SchedRetvalOptimization) {
247 const MachineInstr *MI = DAG->SUnits[su].getInstr();
Krzysztof Parzyszekb4bb75d2018-03-21 17:23:32 +0000248 if (MI->isCopy() &&
249 TargetRegisterInfo::isPhysicalRegister(MI->getOperand(1).getReg())) {
250 // %vregX = COPY %r0
251 VRegHoldingReg[MI->getOperand(0).getReg()] = MI->getOperand(1).getReg();
252 LastVRegUse.erase(MI->getOperand(1).getReg());
253 } else {
254 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
255 const MachineOperand &MO = MI->getOperand(i);
256 if (!MO.isReg())
257 continue;
258 if (MO.isUse() && !MI->isCopy() &&
259 VRegHoldingReg.count(MO.getReg())) {
260 // <use of %vregX>
261 LastVRegUse[VRegHoldingReg[MO.getReg()]] = &DAG->SUnits[su];
262 } else if (MO.isDef() &&
263 TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
264 for (MCRegAliasIterator AI(MO.getReg(), &TRI, true); AI.isValid();
265 ++AI) {
266 if (LastVRegUse.count(*AI) &&
267 LastVRegUse[*AI] != &DAG->SUnits[su])
268 // %r0 = ...
269 DAG->addEdge(&DAG->SUnits[su], SDep(LastVRegUse[*AI], SDep::Barrier));
270 LastVRegUse.erase(*AI);
271 }
272 }
273 }
274 }
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000275 }
276 }
277}
278
Krzysztof Parzyszek2164a272017-08-28 18:36:21 +0000279void HexagonSubtarget::BankConflictMutation::apply(ScheduleDAGInstrs *DAG) {
280 if (!EnableCheckBankConflict)
281 return;
282
283 const auto &HII = static_cast<const HexagonInstrInfo&>(*DAG->TII);
284
285 // Create artificial edges between loads that could likely cause a bank
286 // conflict. Since such loads would normally not have any dependency
287 // between them, we cannot rely on existing edges.
288 for (unsigned i = 0, e = DAG->SUnits.size(); i != e; ++i) {
289 SUnit &S0 = DAG->SUnits[i];
290 MachineInstr &L0 = *S0.getInstr();
291 if (!L0.mayLoad() || L0.mayStore() ||
292 HII.getAddrMode(L0) != HexagonII::BaseImmOffset)
293 continue;
294 int Offset0;
295 unsigned Size0;
296 unsigned Base0 = HII.getBaseAndOffset(L0, Offset0, Size0);
297 // Is the access size is longer than the L1 cache line, skip the check.
298 if (Base0 == 0 || Size0 >= 32)
299 continue;
300 // Scan only up to 32 instructions ahead (to avoid n^2 complexity).
301 for (unsigned j = i+1, m = std::min(i+32, e); j != m; ++j) {
302 SUnit &S1 = DAG->SUnits[j];
303 MachineInstr &L1 = *S1.getInstr();
304 if (!L1.mayLoad() || L1.mayStore() ||
305 HII.getAddrMode(L1) != HexagonII::BaseImmOffset)
306 continue;
307 int Offset1;
308 unsigned Size1;
309 unsigned Base1 = HII.getBaseAndOffset(L1, Offset1, Size1);
310 if (Base1 == 0 || Size1 >= 32 || Base0 != Base1)
311 continue;
312 // Check bits 3 and 4 of the offset: if they differ, a bank conflict
313 // is unlikely.
314 if (((Offset0 ^ Offset1) & 0x18) != 0)
315 continue;
316 // Bits 3 and 4 are the same, add an artificial edge and set extra
317 // latency.
318 SDep A(&S0, SDep::Artificial);
319 A.setLatency(1);
320 S1.addPred(A, true);
321 }
322 }
323}
324
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000325/// Enable use of alias analysis during code generation (during MI
Krzysztof Parzyszekd7681422017-11-30 21:25:28 +0000326/// scheduling, DAGCombine, etc.).
327bool HexagonSubtarget::useAA() const {
328 if (OptLevel != CodeGenOpt::None)
329 return true;
330 return false;
331}
332
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000333/// Perform target specific adjustments to the latency of a schedule
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000334/// dependency.
335void HexagonSubtarget::adjustSchedDependency(SUnit *Src, SUnit *Dst,
336 SDep &Dep) const {
337 MachineInstr *SrcInst = Src->getInstr();
338 MachineInstr *DstInst = Dst->getInstr();
339 if (!Src->isInstr() || !Dst->isInstr())
340 return;
341
342 const HexagonInstrInfo *QII = getInstrInfo();
343
344 // Instructions with .new operands have zero latency.
345 SmallSet<SUnit *, 4> ExclSrc;
346 SmallSet<SUnit *, 4> ExclDst;
347 if (QII->canExecuteInBundle(*SrcInst, *DstInst) &&
348 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
349 Dep.setLatency(0);
350 return;
351 }
352
353 if (!hasV60TOps())
354 return;
355
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +0000356 // Set the latency for a copy to zero since we hope that is will get removed.
357 if (DstInst->isCopy())
358 Dep.setLatency(0);
359
360 // If it's a REG_SEQUENCE/COPY, use its destination instruction to determine
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000361 // the correct latency.
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +0000362 if ((DstInst->isRegSequence() || DstInst->isCopy()) && Dst->NumSuccs == 1) {
363 unsigned DReg = DstInst->getOperand(0).getReg();
364 MachineInstr *DDst = Dst->Succs[0].getSUnit()->getInstr();
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000365 unsigned UseIdx = -1;
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +0000366 for (unsigned OpNum = 0; OpNum < DDst->getNumOperands(); OpNum++) {
367 const MachineOperand &MO = DDst->getOperand(OpNum);
368 if (MO.isReg() && MO.getReg() && MO.isUse() && MO.getReg() == DReg) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000369 UseIdx = OpNum;
370 break;
371 }
372 }
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +0000373 int DLatency = (InstrInfo.getOperandLatency(&InstrItins, *SrcInst,
374 0, *DDst, UseIdx));
375 DLatency = std::max(DLatency, 0);
376 Dep.setLatency((unsigned)DLatency);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000377 }
378
379 // Try to schedule uses near definitions to generate .cur.
380 ExclSrc.clear();
381 ExclDst.clear();
382 if (EnableDotCurSched && QII->isToBeScheduledASAP(*SrcInst, *DstInst) &&
383 isBestZeroLatency(Src, Dst, QII, ExclSrc, ExclDst)) {
384 Dep.setLatency(0);
385 return;
386 }
387
388 updateLatency(*SrcInst, *DstInst, Dep);
389}
390
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000391void HexagonSubtarget::getPostRAMutations(
Eugene Zelenko8361b0a2017-06-19 22:43:19 +0000392 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000393 Mutations.push_back(llvm::make_unique<UsrOverflowMutation>());
394 Mutations.push_back(llvm::make_unique<HVXMemLatencyMutation>());
Krzysztof Parzyszek2164a272017-08-28 18:36:21 +0000395 Mutations.push_back(llvm::make_unique<BankConflictMutation>());
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000396}
397
Krzysztof Parzyszek3885d872016-12-22 19:44:55 +0000398void HexagonSubtarget::getSMSMutations(
Eugene Zelenko8361b0a2017-06-19 22:43:19 +0000399 std::vector<std::unique_ptr<ScheduleDAGMutation>> &Mutations) const {
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000400 Mutations.push_back(llvm::make_unique<UsrOverflowMutation>());
401 Mutations.push_back(llvm::make_unique<HVXMemLatencyMutation>());
Krzysztof Parzyszek3885d872016-12-22 19:44:55 +0000402}
403
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +0000404// Pin the vtable to this file.
405void HexagonSubtarget::anchor() {}
Eric Christopher5f141b02015-03-11 22:56:10 +0000406
407bool HexagonSubtarget::enableMachineScheduler() const {
408 if (DisableHexagonMISched.getNumOccurrences())
409 return !DisableHexagonMISched;
410 return true;
411}
Krzysztof Parzyszek07d75182016-05-28 02:02:51 +0000412
Krzysztof Parzyszekee93e002017-05-05 22:13:57 +0000413bool HexagonSubtarget::usePredicatedCalls() const {
414 return EnablePredicatedCalls;
415}
416
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000417void HexagonSubtarget::updateLatency(MachineInstr &SrcInst,
418 MachineInstr &DstInst, SDep &Dep) const {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000419 if (Dep.isArtificial()) {
420 Dep.setLatency(1);
421 return;
422 }
423
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000424 if (!hasV60TOps())
425 return;
426
427 auto &QII = static_cast<const HexagonInstrInfo&>(*getInstrInfo());
428
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000429 // BSB scheduling.
430 if (QII.isHVXVec(SrcInst) || useBSBScheduling())
431 Dep.setLatency((Dep.getLatency() + 1) >> 1);
432}
433
434void HexagonSubtarget::restoreLatency(SUnit *Src, SUnit *Dst) const {
435 MachineInstr *SrcI = Src->getInstr();
436 for (auto &I : Src->Succs) {
437 if (!I.isAssignedRegDep() || I.getSUnit() != Dst)
438 continue;
439 unsigned DepR = I.getReg();
440 int DefIdx = -1;
441 for (unsigned OpNum = 0; OpNum < SrcI->getNumOperands(); OpNum++) {
442 const MachineOperand &MO = SrcI->getOperand(OpNum);
443 if (MO.isReg() && MO.isDef() && MO.getReg() == DepR)
444 DefIdx = OpNum;
445 }
446 assert(DefIdx >= 0 && "Def Reg not found in Src MI");
447 MachineInstr *DstI = Dst->getInstr();
Krzysztof Parzyszek4a5a80c2018-03-26 19:04:58 +0000448 SDep T = I;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000449 for (unsigned OpNum = 0; OpNum < DstI->getNumOperands(); OpNum++) {
450 const MachineOperand &MO = DstI->getOperand(OpNum);
451 if (MO.isReg() && MO.isUse() && MO.getReg() == DepR) {
452 int Latency = (InstrInfo.getOperandLatency(&InstrItins, *SrcI,
453 DefIdx, *DstI, OpNum));
454
455 // For some instructions (ex: COPY), we might end up with < 0 latency
456 // as they don't have any Itinerary class associated with them.
Krzysztof Parzyszeka2122042018-03-26 16:33:16 +0000457 Latency = std::max(Latency, 0);
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000458
459 I.setLatency(Latency);
460 updateLatency(*SrcI, *DstI, I);
461 }
462 }
463
464 // Update the latency of opposite edge too.
Krzysztof Parzyszek4a5a80c2018-03-26 19:04:58 +0000465 T.setSUnit(Src);
466 auto F = std::find(Dst->Preds.begin(), Dst->Preds.end(), T);
467 assert(F != Dst->Preds.end());
468 F->setLatency(I.getLatency());
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000469 }
470}
471
472/// Change the latency between the two SUnits.
473void HexagonSubtarget::changeLatency(SUnit *Src, SUnit *Dst, unsigned Lat)
474 const {
475 for (auto &I : Src->Succs) {
Krzysztof Parzyszek4a5a80c2018-03-26 19:04:58 +0000476 if (!I.isAssignedRegDep() || I.getSUnit() != Dst)
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000477 continue;
478 SDep T = I;
479 I.setLatency(Lat);
480
481 // Update the latency of opposite edge too.
482 T.setSUnit(Src);
483 auto F = std::find(Dst->Preds.begin(), Dst->Preds.end(), T);
484 assert(F != Dst->Preds.end());
Krzysztof Parzyszek4a5a80c2018-03-26 19:04:58 +0000485 F->setLatency(Lat);
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000486 }
487}
488
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000489/// If the SUnit has a zero latency edge, return the other SUnit.
490static SUnit *getZeroLatency(SUnit *N, SmallVector<SDep, 4> &Deps) {
491 for (auto &I : Deps)
492 if (I.isAssignedRegDep() && I.getLatency() == 0 &&
493 !I.getSUnit()->getInstr()->isPseudo())
494 return I.getSUnit();
495 return nullptr;
496}
497
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000498// Return true if these are the best two instructions to schedule
499// together with a zero latency. Only one dependence should have a zero
500// latency. If there are multiple choices, choose the best, and change
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000501// the others, if needed.
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000502bool HexagonSubtarget::isBestZeroLatency(SUnit *Src, SUnit *Dst,
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000503 const HexagonInstrInfo *TII, SmallSet<SUnit*, 4> &ExclSrc,
504 SmallSet<SUnit*, 4> &ExclDst) const {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000505 MachineInstr &SrcInst = *Src->getInstr();
506 MachineInstr &DstInst = *Dst->getInstr();
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000507
Ron Liebermanda5df7c2016-09-17 16:21:09 +0000508 // Ignore Boundary SU nodes as these have null instructions.
509 if (Dst->isBoundaryNode())
510 return false;
511
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000512 if (SrcInst.isPHI() || DstInst.isPHI())
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000513 return false;
514
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000515 if (!TII->isToBeScheduledASAP(SrcInst, DstInst) &&
516 !TII->canExecuteInBundle(SrcInst, DstInst))
517 return false;
518
519 // The architecture doesn't allow three dependent instructions in the same
520 // packet. So, if the destination has a zero latency successor, then it's
521 // not a candidate for a zero latency predecessor.
522 if (getZeroLatency(Dst, Dst->Succs) != nullptr)
523 return false;
524
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000525 // Check if the Dst instruction is the best candidate first.
526 SUnit *Best = nullptr;
527 SUnit *DstBest = nullptr;
528 SUnit *SrcBest = getZeroLatency(Dst, Dst->Preds);
529 if (SrcBest == nullptr || Src->NodeNum >= SrcBest->NodeNum) {
530 // Check that Src doesn't have a better candidate.
531 DstBest = getZeroLatency(Src, Src->Succs);
532 if (DstBest == nullptr || Dst->NodeNum <= DstBest->NodeNum)
533 Best = Dst;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000534 }
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000535 if (Best != Dst)
536 return false;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000537
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000538 // The caller frequently adds the same dependence twice. If so, then
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000539 // return true for this case too.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000540 if ((Src == SrcBest && Dst == DstBest ) ||
541 (SrcBest == nullptr && Dst == DstBest) ||
542 (Src == SrcBest && Dst == nullptr))
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000543 return true;
544
545 // Reassign the latency for the previous bests, which requires setting
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000546 // the dependence edge in both directions.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000547 if (SrcBest != nullptr) {
548 if (!hasV60TOps())
549 changeLatency(SrcBest, Dst, 1);
550 else
551 restoreLatency(SrcBest, Dst);
552 }
553 if (DstBest != nullptr) {
554 if (!hasV60TOps())
555 changeLatency(Src, DstBest, 1);
556 else
557 restoreLatency(Src, DstBest);
558 }
559
560 // Attempt to find another opprotunity for zero latency in a different
561 // dependence.
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000562 if (SrcBest && DstBest)
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000563 // If there is an edge from SrcBest to DstBst, then try to change that
564 // to 0 now.
565 changeLatency(SrcBest, DstBest, 0);
566 else if (DstBest) {
567 // Check if the previous best destination instruction has a new zero
568 // latency dependence opportunity.
569 ExclSrc.insert(Src);
570 for (auto &I : DstBest->Preds)
571 if (ExclSrc.count(I.getSUnit()) == 0 &&
572 isBestZeroLatency(I.getSUnit(), DstBest, TII, ExclSrc, ExclDst))
573 changeLatency(I.getSUnit(), DstBest, 0);
574 } else if (SrcBest) {
575 // Check if previous best source instruction has a new zero latency
576 // dependence opportunity.
577 ExclDst.insert(Dst);
578 for (auto &I : SrcBest->Succs)
579 if (ExclDst.count(I.getSUnit()) == 0 &&
580 isBestZeroLatency(SrcBest, I.getSUnit(), TII, ExclSrc, ExclDst))
581 changeLatency(SrcBest, I.getSUnit(), 0);
582 }
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000583
Krzysztof Parzyszek748d3ef2016-07-18 14:23:10 +0000584 return true;
Krzysztof Parzyszek408e3002016-07-15 21:34:02 +0000585}
586
Krzysztof Parzyszekd3d0a4b2016-07-22 14:22:43 +0000587unsigned HexagonSubtarget::getL1CacheLineSize() const {
588 return 32;
589}
590
591unsigned HexagonSubtarget::getL1PrefetchDistance() const {
592 return 32;
593}
594
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000595bool HexagonSubtarget::enableSubRegLiveness() const {
596 return EnableSubregLiveness;
597}