blob: a6d134365a62b8b1a5a74a2ed2ae3e5c713cb040 [file] [log] [blame]
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001//===----- HexagonPacketizer.cpp - vliw packetizer ---------------------===//
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 implements a simple VLIW packetizer using DFA. The packetizer works on
11// machine basic blocks. For each instruction I in BB, the packetizer consults
12// the DFA to see if machine resources are available to execute I. If so, the
13// packetizer checks if I depends on any instruction J in the current packet.
14// If no dependency is found, I is added to current packet and machine resource
15// is marked as taken. If any dependency is found, a target API call is made to
16// prune the dependence.
17//
18//===----------------------------------------------------------------------===//
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "HexagonVLIWPacketizer.h"
Jyotsna Verma84256432013-03-01 17:37:13 +000020#include "HexagonRegisterInfo.h"
21#include "HexagonSubtarget.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "HexagonTargetMachine.h"
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000023#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "llvm/CodeGen/MachineDominators.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000026#include "llvm/CodeGen/MachineLoopInfo.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/CodeGen/Passes.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000029#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/Debug.h"
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000031
32using namespace llvm;
33
Chandler Carruth84e68b22014-04-22 02:41:26 +000034#define DEBUG_TYPE "packets"
35
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000036static cl::opt<bool> DisablePacketizer("disable-packetizer", cl::Hidden,
37 cl::ZeroOrMore, cl::init(false),
38 cl::desc("Disable Hexagon packetizer pass"));
39
Jyotsna Verma1d297502013-05-02 15:39:30 +000040static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000041 cl::ZeroOrMore, cl::Hidden, cl::init(true),
42 cl::desc("Allow non-solo packetization of volatile memory references"));
43
44static cl::opt<bool> EnableGenAllInsnClass("enable-gen-insn", cl::init(false),
45 cl::Hidden, cl::ZeroOrMore, cl::desc("Generate all instruction with TC"));
46
47static cl::opt<bool> DisableVecDblNVStores("disable-vecdbl-nv-stores",
48 cl::init(false), cl::Hidden, cl::ZeroOrMore,
49 cl::desc("Disable vector double new-value-stores"));
50
51extern cl::opt<bool> ScheduleInlineAsm;
Jyotsna Verma1d297502013-05-02 15:39:30 +000052
Jyotsna Verma1d297502013-05-02 15:39:30 +000053namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000054 FunctionPass *createHexagonPacketizer();
Jyotsna Verma1d297502013-05-02 15:39:30 +000055 void initializeHexagonPacketizerPass(PassRegistry&);
56}
57
58
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000059namespace {
60 class HexagonPacketizer : public MachineFunctionPass {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000061 public:
62 static char ID;
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +000063 HexagonPacketizer() : MachineFunctionPass(ID) {}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000064
Craig Topper906c2cd2014-04-29 07:58:16 +000065 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000066 AU.setPreservesCFG();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000067 AU.addRequired<AAResultsWrapperPass>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000068 AU.addRequired<MachineBranchProbabilityInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000069 AU.addRequired<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000070 AU.addRequired<MachineLoopInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000071 AU.addPreserved<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000072 AU.addPreserved<MachineLoopInfo>();
73 MachineFunctionPass::getAnalysisUsage(AU);
74 }
Mehdi Amini117296c2016-10-01 02:56:57 +000075 StringRef getPassName() const override { return "Hexagon Packetizer"; }
Craig Topper906c2cd2014-04-29 07:58:16 +000076 bool runOnMachineFunction(MachineFunction &Fn) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000077 MachineFunctionProperties getRequiredProperties() const override {
78 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000079 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000080 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000081
82 private:
83 const HexagonInstrInfo *HII;
84 const HexagonRegisterInfo *HRI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000085 };
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000086
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000087 char HexagonPacketizer::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000088}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000089
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +000090INITIALIZE_PASS_BEGIN(HexagonPacketizer, "hexagon-packetizer",
91 "Hexagon Packetizer", false, false)
Jyotsna Verma1d297502013-05-02 15:39:30 +000092INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
93INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
94INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Chandler Carruth7b560d42015-09-09 17:55:00 +000095INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +000096INITIALIZE_PASS_END(HexagonPacketizer, "hexagon-packetizer",
97 "Hexagon Packetizer", false, false)
Jyotsna Verma1d297502013-05-02 15:39:30 +000098
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000099HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
100 MachineLoopInfo &MLI, AliasAnalysis *AA,
101 const MachineBranchProbabilityInfo *MBPI)
102 : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI) {
103 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
104 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000105
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000106 addMutation(make_unique<HexagonSubtarget::UsrOverflowMutation>());
107 addMutation(make_unique<HexagonSubtarget::HVXMemLatencyMutation>());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000108}
109
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000110// Check if FirstI modifies a register that SecondI reads.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000111static bool hasWriteToReadDep(const MachineInstr &FirstI,
112 const MachineInstr &SecondI,
113 const TargetRegisterInfo *TRI) {
114 for (auto &MO : FirstI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000115 if (!MO.isReg() || !MO.isDef())
116 continue;
117 unsigned R = MO.getReg();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000118 if (SecondI.readsRegister(R, TRI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000119 return true;
120 }
121 return false;
122}
123
124
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000125static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000126 MachineBasicBlock::iterator BundleIt, bool Before) {
127 MachineBasicBlock::instr_iterator InsertPt;
128 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000129 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000130 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000131 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000132
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000133 MachineBasicBlock &B = *MI.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000134 // The instruction should at least be bundled with the preceding instruction
135 // (there will always be one, i.e. BUNDLE, if nothing else).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000136 assert(MI.isBundledWithPred());
137 if (MI.isBundledWithSucc()) {
138 MI.clearFlag(MachineInstr::BundledSucc);
139 MI.clearFlag(MachineInstr::BundledPred);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000140 } else {
141 // If it's not bundled with the successor (i.e. it is the last one
142 // in the bundle), then we can simply unbundle it from the predecessor,
143 // which will take care of updating the predecessor's flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000144 MI.unbundleFromPred();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000145 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000146 B.splice(InsertPt, &B, MI.getIterator());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000147
148 // Get the size of the bundle without asserting.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000149 MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000150 MachineBasicBlock::const_instr_iterator E = B.instr_end();
151 unsigned Size = 0;
152 for (++I; I != E && I->isBundledWithPred(); ++I)
153 ++Size;
154
155 // If there are still two or more instructions, then there is nothing
156 // else to be done.
157 if (Size > 1)
158 return BundleIt;
159
160 // Otherwise, extract the single instruction out and delete the bundle.
161 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000162 MachineInstr &SingleI = *BundleIt->getNextNode();
163 SingleI.unbundleFromPred();
164 assert(!SingleI.isBundledWithSucc());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000165 BundleIt->eraseFromParent();
166 return NextIt;
167}
168
169
170bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000171 if (DisablePacketizer || skipFunction(*MF.getFunction()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000172 return false;
173
174 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
175 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
176 auto &MLI = getAnalysis<MachineLoopInfo>();
177 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
178 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
179
180 if (EnableGenAllInsnClass)
181 HII->genAllInsnTimingClasses(MF);
182
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000183 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000184 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000185
186 // DFA state table should not be empty.
187 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
188
189 //
190 // Loop over all basic blocks and remove KILL pseudo-instructions
191 // These instructions confuse the dependence analysis. Consider:
192 // D0 = ... (Insn 0)
193 // R0 = KILL R0, D0 (Insn 1)
194 // R0 = ... (Insn 2)
195 // Here, Insn 1 will result in the dependence graph not emitting an output
196 // dependence between Insn 0 and Insn 2. This can lead to incorrect
197 // packetization
198 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000199 for (auto &MB : MF) {
200 auto End = MB.end();
201 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000202 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000203 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000204 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000205 MB.erase(MI);
206 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000207 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000208 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000209 }
210 }
211
212 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000213 for (auto &MB : MF) {
214 auto Begin = MB.begin(), End = MB.end();
215 while (Begin != End) {
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000216 // Find the first non-boundary starting from the end of the last
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000217 // scheduling region.
218 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000219 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000220 ++RB;
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000221 // Find the first boundary starting from the beginning of the new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000222 // region.
223 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000224 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000225 ++RE;
226 // Add the scheduling boundary if it's not block end.
227 if (RE != End)
228 ++RE;
229 // If RB == End, then RE == End.
230 if (RB != End)
231 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000232
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000233 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000234 }
235 }
236
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000237 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000238 return true;
239}
240
241
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000242// Reserve resources for a constant extender. Trigger an assertion if the
243// reservation fails.
244void HexagonPacketizerList::reserveResourcesForConstExt() {
245 if (!tryAllocateResourcesForConstExt(true))
246 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000247}
248
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000249bool HexagonPacketizerList::canReserveResourcesForConstExt() {
250 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000251}
252
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000253// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
254// return true, otherwise, return false.
255bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
256 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000257 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000258 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000259 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000260 MF.DeleteMachineInstr(ExtMI);
261 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000262}
263
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000264
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000265bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000266 SDep::Kind DepType, unsigned DepReg) {
267 // Check for LR dependence.
268 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000269 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000270
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000271 if (HII->isDeallocRet(MI))
272 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000273 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000274
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000275 // Call-like instructions can be packetized with preceding instructions
276 // that define registers implicitly used or modified by the call. Explicit
277 // uses are still prohibited, as in the case of indirect calls:
278 // r0 = ...
279 // J2_jumpr r0
280 if (DepType == SDep::Data) {
281 for (const MachineOperand MO : MI.operands())
282 if (MO.isReg() && MO.getReg() == DepReg && !MO.isImplicit())
283 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000284 }
285
286 return false;
287}
288
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000289static bool isRegDependence(const SDep::Kind DepType) {
290 return DepType == SDep::Data || DepType == SDep::Anti ||
291 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000292}
293
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000294static bool isDirectJump(const MachineInstr &MI) {
295 return MI.getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000296}
297
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000298static bool isSchedBarrier(const MachineInstr &MI) {
299 switch (MI.getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000300 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000301 return true;
302 }
303 return false;
304}
305
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000306static bool isControlFlow(const MachineInstr &MI) {
307 return MI.getDesc().isTerminator() || MI.getDesc().isCall();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000308}
309
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000310
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000311/// Returns true if the instruction modifies a callee-saved register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000312static bool doesModifyCalleeSavedReg(const MachineInstr &MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000313 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000314 const MachineFunction &MF = *MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000315 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000316 if (MI.modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000317 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000318 return false;
319}
320
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000321// Returns true if an instruction can be promoted to .new predicate or
322// new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000323bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI,
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000324 const TargetRegisterClass *NewRC) {
325 // Vector stores can be predicated, and can be new-value stores, but
326 // they cannot be predicated on a .new predicate value.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000327 if (NewRC == &Hexagon::PredRegsRegClass) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000328 if (HII->isHVXVec(MI) && MI.mayStore())
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000329 return false;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000330 return HII->isPredicated(MI) && HII->getDotNewPredOp(MI, nullptr) > 0;
331 }
332 // If the class is not PredRegs, it could only apply to new-value stores.
333 return HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000334}
335
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000336// Promote an instructiont to its .cur form.
337// At this time, we have already made a call to canPromoteToDotCur and made
338// sure that it can *indeed* be promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000339bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000340 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
341 const TargetRegisterClass* RC) {
342 assert(DepType == SDep::Data);
343 int CurOpcode = HII->getDotCurOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000344 MI.setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000345 return true;
346}
347
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000348void HexagonPacketizerList::cleanUpDotCur() {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000349 MachineInstr *MI = nullptr;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000350 for (auto BI : CurrentPacketMIs) {
351 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000352 if (HII->isDotCurInst(*BI)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000353 MI = BI;
354 continue;
355 }
356 if (MI) {
357 for (auto &MO : BI->operands())
358 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
359 return;
360 }
361 }
362 if (!MI)
363 return;
364 // We did not find a use of the CUR, so de-cur it.
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000365 MI->setDesc(HII->get(HII->getNonDotCurOp(*MI)));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000366 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
367}
368
369// Check to see if an instruction can be dot cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000370bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000371 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
372 const TargetRegisterClass *RC) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000373 if (!HII->isHVXVec(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000374 return false;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000375 if (!HII->isHVXVec(*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000376 return false;
377
378 // Already a dot new instruction.
379 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
380 return false;
381
382 if (!HII->mayBeCurLoad(MI))
383 return false;
384
385 // The "cur value" cannot come from inline asm.
386 if (PacketSU->getInstr()->isInlineAsm())
387 return false;
388
389 // Make sure candidate instruction uses cur.
390 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000391 MI.dump();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000392 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000393 MachineInstr &MJ = *MII;
394 DEBUG({
395 dbgs() << "Checking CUR against ";
396 MJ.dump();
397 });
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000398 unsigned DestReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000399 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000400 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000401 if (MO.isReg() && MO.getReg() == DestReg)
402 FoundMatch = true;
403 if (!FoundMatch)
404 return false;
405
406 // Check for existing uses of a vector register within the packet which
407 // would be affected by converting a vector load into .cur formt.
408 for (auto BI : CurrentPacketMIs) {
409 DEBUG(dbgs() << "packet has "; BI->dump(););
410 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
411 return false;
412 }
413
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000414 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000415 // We can convert the opcode into a .cur.
416 return true;
417}
418
419// Promote an instruction to its .new form. At this time, we have already
420// made a call to canPromoteToDotNew and made sure that it can *indeed* be
421// promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000422bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000423 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
424 const TargetRegisterClass* RC) {
425 assert (DepType == SDep::Data);
426 int NewOpcode;
427 if (RC == &Hexagon::PredRegsRegClass)
428 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
429 else
430 NewOpcode = HII->getDotNewOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000431 MI.setDesc(HII->get(NewOpcode));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000432 return true;
433}
434
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000435bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +0000436 int NewOpcode = HII->getDotOldOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000437 MI.setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000438 return true;
439}
440
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000441bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) {
442 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000443 switch (Opc) {
444 case Hexagon::S2_storerd_io:
445 case Hexagon::S2_storeri_io:
446 case Hexagon::S2_storerh_io:
447 case Hexagon::S2_storerb_io:
448 break;
449 default:
450 llvm_unreachable("Unexpected instruction");
451 }
Matthias Braun941a7052016-07-28 18:40:00 +0000452 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000453 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000454 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
455 if (HII->isValidOffset(Opc, NewOff)) {
456 Off.setImm(NewOff);
457 return true;
458 }
459 return false;
460}
461
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000462void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) {
463 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000464 switch (Opc) {
465 case Hexagon::S2_storerd_io:
466 case Hexagon::S2_storeri_io:
467 case Hexagon::S2_storerh_io:
468 case Hexagon::S2_storerb_io:
469 break;
470 default:
471 llvm_unreachable("Unexpected instruction");
472 }
Matthias Braun941a7052016-07-28 18:40:00 +0000473 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000474 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000475 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
476}
477
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000478enum PredicateKind {
479 PK_False,
480 PK_True,
481 PK_Unknown
482};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000483
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000484/// Returns true if an instruction is predicated on p0 and false if it's
485/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000486static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000487 const HexagonInstrInfo *HII) {
488 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000489 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000490 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000491 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000492 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000493}
494
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000495static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000496 const HexagonInstrInfo *HII) {
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000497 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000498#ifndef NDEBUG
499 // Post Increment means duplicates. Use dense map to find duplicates in the
500 // list. Caution: Densemap initializes with the minimum of 64 buckets,
501 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000502 DenseSet<unsigned> DefRegsSet;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000503 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000504 if (MO.isReg() && MO.isDef())
505 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000506
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000507 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000508 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
509 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000510#else
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000511 if (MI.mayLoad()) {
512 const MachineOperand &Op1 = MI.getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000513 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000514 assert(Op1.isReg() && "Post increment operand has be to a register.");
515 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000516 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000517 if (MI.getDesc().mayStore()) {
518 const MachineOperand &Op0 = MI.getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000519 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000520 assert(Op0.isReg() && "Post increment operand has be to a register.");
521 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000522 }
523#endif
524 // we should never come here.
525 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
526}
527
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000528// Get the value being stored.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000529static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000530 // value being stored is always the last operand.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000531 return MI.getOperand(MI.getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000532}
533
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000534static bool isLoadAbsSet(const MachineInstr &MI) {
535 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000536 switch (Opc) {
537 case Hexagon::L4_loadrd_ap:
538 case Hexagon::L4_loadrb_ap:
539 case Hexagon::L4_loadrh_ap:
540 case Hexagon::L4_loadrub_ap:
541 case Hexagon::L4_loadruh_ap:
542 case Hexagon::L4_loadri_ap:
543 return true;
544 }
545 return false;
546}
547
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000548static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000549 assert(isLoadAbsSet(MI));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000550 return MI.getOperand(1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000551}
552
553
554// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000555// Following restrictions are to be respected in convert a store into
556// a new value store.
557// 1. If an instruction uses auto-increment, its address register cannot
558// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000559// 2. If an instruction uses absolute-set addressing mode, its address
560// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000561// 3. If an instruction produces a 64-bit result, its registers cannot be used
562// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000563// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000564// the instruction that uses the new-value register must also be conditional,
565// and both must always have their predicates evaluate identically.
566// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000567// 5. There is an implied restriction that a packet cannot have another store,
568// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000569// already a store in a packet, there can not be a new value store.
570// Arch Spec: 3.4.4.2
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000571bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
572 const MachineInstr &PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000573 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000574 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000575 return false;
576
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000577 // Make sure there is dependency and can be new value'd.
578 const MachineOperand &Val = getStoreValueOperand(MI);
579 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000580 return false;
581
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000582 const MCInstrDesc& MCID = PacketMI.getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000583
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000584 // First operand is always the result.
585 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
586 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
587 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000588 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000589
590 // New-value stores are of class NV (slot 0), dual stores require class ST
591 // in slot 0 (PRM 5.5).
592 for (auto I : CurrentPacketMIs) {
593 SUnit *PacketSU = MIToSUnit.find(I)->second;
594 if (PacketSU->getInstr()->mayStore())
595 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000596 }
597
598 // Make sure it's NOT the post increment register that we are going to
599 // new value.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000600 if (HII->isPostIncrement(MI) &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000601 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000602 return false;
603 }
604
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000605 if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000606 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
607 // If source is post_inc, or absolute-set addressing, it can not feed
608 // into new value store
609 // r3 = memw(r2++#4)
610 // memw(r30 + #-1404) = r2.new -> can not be new value store
611 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000612 return false;
613 }
614
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000615 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
616 return false;
617
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000618 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000619 // also be predicated.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000620 if (HII->isPredicated(PacketMI)) {
621 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000622 return false;
623
624 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000625 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000626 unsigned predRegNumSrc = 0;
627 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000628 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000629
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000630 // Get predicate register used in the source instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000631 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000632 if (!MO.isReg())
633 continue;
634 predRegNumSrc = MO.getReg();
635 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
636 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000637 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000638 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000639 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
640 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000641
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000642 // Get predicate register used in new-value store instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000643 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000644 if (!MO.isReg())
645 continue;
646 predRegNumDst = MO.getReg();
647 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
648 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000649 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000650 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000651 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
652 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000653
654 // New-value register producer and user (store) need to satisfy these
655 // constraints:
656 // 1) Both instructions should be predicated on the same register.
657 // 2) If producer of the new-value register is .new predicated then store
658 // should also be .new predicated and if producer is not .new predicated
659 // then store should not be .new predicated.
660 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000661 // sense, i.e, either both should be negated or both should be non-negated.
662 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000663 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000664 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000665 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000666 }
667
668 // Make sure that other than the new-value register no other store instruction
669 // register has been modified in the same packet. Predicate registers can be
670 // modified by they should not be modified between the producer and the store
671 // instruction as it will make them both conditional on different values.
672 // We already know this to be true for all the instructions before and
673 // including PacketMI. Howerver, we need to perform the check for the
674 // remaining instructions in the packet.
675
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000676 unsigned StartCheck = 0;
677
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000678 for (auto I : CurrentPacketMIs) {
679 SUnit *TempSU = MIToSUnit.find(I)->second;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000680 MachineInstr &TempMI = *TempSU->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000681
682 // Following condition is true for all the instructions until PacketMI is
683 // reached (StartCheck is set to 0 before the for loop).
684 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000685 if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
686 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000687
688 StartCheck = 1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000689 if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000690 continue;
691
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000692 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000693 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000694 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000695 }
696
Alp Tokerf907b892013-12-05 05:44:44 +0000697 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000698 // 1. The only use of reg is DepReg and no other registers.
699 // This handles V4 base+index registers.
700 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000701 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000702 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000703 if (!HII->isPostIncrement(MI)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000704 for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
705 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000706 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000707 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000708 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000709 }
710
711 // If data definition is because of implicit definition of the register,
712 // do not newify the store. Eg.
713 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
714 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000715 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000716 if (MO.isRegMask() && MO.clobbersPhysReg(DepReg))
717 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000718 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
719 continue;
720 unsigned R = MO.getReg();
721 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
722 return false;
723 }
724
725 // Handle imp-use of super reg case. There is a target independent side
726 // change that should prevent this situation but I am handling it for
727 // just-in-case. For example, we cannot newify R2 in the following case:
728 // %R3<def> = A2_tfrsi 0;
729 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000730 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000731 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
732 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000733 }
734
735 // Can be dot new store.
736 return true;
737}
738
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000739// Can this MI to promoted to either new value store or new value jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000740bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000741 const SUnit *PacketSU, unsigned DepReg,
742 MachineBasicBlock::iterator &MII) {
743 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000744 return false;
745
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000746 // Check to see the store can be new value'ed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000747 MachineInstr &PacketMI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000748 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000749 return true;
750
751 // Check to see the compare/jump can be new value'ed.
752 // This is done as a pass on its own. Don't need to check it here.
753 return false;
754}
755
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000756static bool isImplicitDependency(const MachineInstr &I, bool CheckDef,
757 unsigned DepReg) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000758 for (auto &MO : I.operands()) {
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000759 if (CheckDef && MO.isRegMask() && MO.clobbersPhysReg(DepReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000760 return true;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000761 if (!MO.isReg() || MO.getReg() != DepReg || !MO.isImplicit())
762 continue;
763 if (CheckDef == MO.isDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000764 return true;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000765 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000766 return false;
767}
768
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000769// Check to see if an instruction can be dot new
770// There are three kinds.
771// 1. dot new on predicate - V2/V3/V4
772// 2. dot new on stores NV/ST - V4
773// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000774bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000775 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
776 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000777 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000778 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000779 return false;
780
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000781 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000782 return false;
783
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000784 const MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000785
786 // The "new value" cannot come from inline asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000787 if (PI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000788 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000789
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000790 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
791 // sense.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000792 if (PI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000793 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000794
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000795 // If dependency is trough an implicitly defined register, we should not
796 // newify the use.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000797 if (isImplicitDependency(PI, true, DepReg) ||
798 isImplicitDependency(MI, false, DepReg))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000799 return false;
800
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000801 const MCInstrDesc& MCID = PI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000802 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
803 if (DisableVecDblNVStores && VecRC == &Hexagon::VecDblRegsRegClass)
804 return false;
805
806 // predicate .new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000807 if (RC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000808 return HII->predCanBeUsedAsDotNew(PI, DepReg);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000809
810 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
811 return false;
812
813 // Create a dot new machine instruction to see if resources can be
814 // allocated. If not, bail out now.
815 int NewOpcode = HII->getDotNewOp(MI);
816 const MCInstrDesc &D = HII->get(NewOpcode);
817 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000818 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000819 MF.DeleteMachineInstr(NewMI);
820 if (!ResourcesAvailable)
821 return false;
822
823 // New Value Store only. New Value Jump generated as a separate pass.
824 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
825 return false;
826
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000827 return true;
828}
829
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000830// Go through the packet instructions and search for an anti dependency between
831// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000832// Trying to add
833// a) %R1<def> = TFRI_cdNotPt %P3, 2
834// to this packet:
835// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000836// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
837// c) %P3<def> = C2_tfrrp %R23
838// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000839// }
840// The P3 from a) and d) will be complements after
841// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000842// Anti-dep between c) and b) is irrelevant for this case
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000843bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000844 unsigned DepReg) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000845 SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000846
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000847 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000848 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000849 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000850 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000851
852 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000853 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000854
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000855 // Look at dependencies between current members of the packet and
856 // predicate defining instruction MI. Make sure that dependency is
857 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000858 if (PacketSU->isSucc(PacketSUDep)) {
859 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000860 auto &Dep = PacketSU->Succs[i];
861 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
862 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000863 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000864 }
865 }
866 }
867
868 return false;
869}
870
871
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000872/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000873static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000874 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000875 /// We use the following rule: The first predicate register that is a use is
876 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000877 assert(QII->isPredicated(MI) && "Must be predicated instruction");
878
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000879 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000880 if (Op.isReg() && Op.getReg() && Op.isUse() &&
881 Hexagon::PredRegsRegClass.contains(Op.getReg()))
882 return Op.getReg();
883 }
884
885 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000886 return 0;
887}
888
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000889// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000890// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000891bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
892 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000893 // If we don't know the predicate sense of the instructions bail out early, we
894 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000895 if (getPredicateSense(MI1, HII) == PK_Unknown ||
896 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000897 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000898
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000899 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000900 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000901
902 // One corner case deals with the following scenario:
903 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000904 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000905 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000906 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000907 // b) %R25<def> = A2_tfrf %P0, %R24
908 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000909 // }
910 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000911 // On general check a) and b) are complements, but presence of c) will
912 // convert a) to .new form, and then it is not a complement.
913 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000914
915 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000916 // Look for Anti dependecy on the same predicate reg as used in the
917 // candidate.
918 for (auto I : CurrentPacketMIs) {
919 // Scheduling Unit for current insn in the packet.
920 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000921
922 // If this instruction in the packet is succeeded by the candidate...
923 if (PacketSU->isSucc(SU)) {
924 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000925 auto Dep = PacketSU->Succs[i];
926 // The corner case exist when there is true data dependency between
927 // candidate and one of current packet members, this dep is on
928 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000929 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000930 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
931 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
932 // Here I know that I is predicate setting instruction with true
933 // data dep to candidate on the register we care about - c) in the
934 // above example. Now I need to see if there is an anti dependency
935 // from c) to any other instruction in the same packet on the pred
936 // reg of interest.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000937 if (restrictingDepExistInPacket(*I, Dep.getReg()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000938 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000939 }
940 }
941 }
942 }
943
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000944 // If the above case does not apply, check regular complement condition.
945 // Check that the predicate register is the same and that the predicate
946 // sense is different We also need to differentiate .old vs. .new: !p0
947 // is not complementary to p0.new.
948 unsigned PReg1 = getPredicatedRegister(MI1, HII);
949 unsigned PReg2 = getPredicatedRegister(MI2, HII);
950 return PReg1 == PReg2 &&
951 Hexagon::PredRegsRegClass.contains(PReg1) &&
952 Hexagon::PredRegsRegClass.contains(PReg2) &&
953 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000954 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000955}
956
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000957// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000958void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000959 Dependence = false;
960 PromotedToDotNew = false;
961 GlueToNewValueJump = false;
962 GlueAllocframeStore = false;
963 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000964}
965
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000966// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000967bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
968 const MachineBasicBlock *) {
969 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000970 return true;
971
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000972 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000973 return false;
974
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000975 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000976 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000977 return false;
978
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000979 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000980 return false;
981
982 // We check if MI has any functional units mapped to it. If it doesn't,
983 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000984 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000985 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +0000986 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000987 return !FuncUnits;
988}
989
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000990bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
991 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000992 return true;
993
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000994 // Consider inline asm to not be a solo instruction by default.
995 // Inline asm will be put in a packet temporarily, but then it will be
996 // removed, and placed outside of the packet (before or after, depending
997 // on dependencies). This is to reduce the impact of inline asm as a
998 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000999 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001000 return true;
1001
1002 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
1003 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
1004 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001005 if (isSchedBarrier(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001006 return true;
1007
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001008 if (HII->isSolo(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001009 return true;
1010
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001011 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001012 return true;
1013
1014 return false;
1015}
1016
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001017
1018// Quick check if instructions MI and MJ cannot coexist in the same packet.
1019// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1020// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1021// For full test call this function twice:
1022// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1023// Doing the test only one way saves the amount of code in this function,
1024// since every test would need to be repeated with the MI and MJ reversed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001025static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001026 const HexagonInstrInfo &HII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001027 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001028 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1029 HII.isHVXMemWithAIndirect(MI, MJ))
1030 return true;
1031
1032 // An inline asm cannot be together with a branch, because we may not be
1033 // able to remove the asm out after packetizing (i.e. if the asm must be
1034 // moved past the bundle). Similarly, two asms cannot be together to avoid
1035 // complications when determining their relative order outside of a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001036 if (MI.isInlineAsm())
1037 return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
1038 MJ.isCall() || MJ.isTerminator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001039
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001040 switch (MI.getOpcode()) {
1041 case (Hexagon::S2_storew_locked):
1042 case (Hexagon::S4_stored_locked):
1043 case (Hexagon::L2_loadw_locked):
1044 case (Hexagon::L4_loadd_locked):
1045 case (Hexagon::Y4_l2fetch): {
1046 // These instructions can only be grouped with ALU32 or non-floating-point
1047 // XTYPE instructions. Since there is no convenient way of identifying fp
1048 // XTYPE instructions, only allow grouping with ALU32 for now.
1049 unsigned TJ = HII.getType(MJ);
Krzysztof Parzyszek5ea971c2017-02-07 17:47:37 +00001050 if (TJ != HexagonII::TypeALU32_2op &&
1051 TJ != HexagonII::TypeALU32_3op &&
1052 TJ != HexagonII::TypeALU32_ADDI)
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001053 return true;
1054 break;
1055 }
1056 default:
1057 break;
1058 }
1059
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001060 // "False" really means that the quick check failed to determine if
1061 // I and J cannot coexist.
1062 return false;
1063}
1064
1065
1066// Full, symmetric check.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001067bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
1068 const MachineInstr &MJ) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001069 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1070}
1071
1072void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1073 for (auto &B : MF) {
1074 MachineBasicBlock::iterator BundleIt;
1075 MachineBasicBlock::instr_iterator NextI;
1076 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1077 NextI = std::next(I);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001078 MachineInstr &MI = *I;
1079 if (MI.isBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001080 BundleIt = I;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001081 if (!MI.isInsideBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001082 continue;
1083
1084 // Decide on where to insert the instruction that we are pulling out.
1085 // Debug instructions always go before the bundle, but the placement of
1086 // INLINE_ASM depends on potential dependencies. By default, try to
1087 // put it before the bundle, but if the asm writes to a register that
1088 // other instructions in the bundle read, then we need to place it
1089 // after the bundle (to preserve the bundle semantics).
1090 bool InsertBeforeBundle;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001091 if (MI.isInlineAsm())
1092 InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
1093 else if (MI.isDebugValue())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001094 InsertBeforeBundle = true;
1095 else
1096 continue;
1097
1098 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1099 }
1100 }
1101}
1102
1103// Check if a given instruction is of class "system".
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001104static bool isSystemInstr(const MachineInstr &MI) {
1105 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001106 switch (Opc) {
1107 case Hexagon::Y2_barrier:
1108 case Hexagon::Y2_dcfetchbo:
1109 return true;
1110 }
1111 return false;
1112}
1113
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001114bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
1115 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001116 // The dependence graph may not include edges between dead definitions,
1117 // so without extra checks, we could end up packetizing two instruction
1118 // defining the same (dead) register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001119 if (I.isCall() || J.isCall())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001120 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001121 if (HII->isPredicated(I) || HII->isPredicated(J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001122 return false;
1123
1124 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001125 for (auto &MO : I.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001126 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1127 continue;
1128 DeadDefs[MO.getReg()] = true;
1129 }
1130
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001131 for (auto &MO : J.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001132 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1133 continue;
1134 unsigned R = MO.getReg();
1135 if (R != Hexagon::USR_OVF && DeadDefs[R])
1136 return true;
1137 }
1138 return false;
1139}
1140
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001141bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
1142 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001143 // A save callee-save register function call can only be in a packet
1144 // with instructions that don't write to the callee-save registers.
1145 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1146 doesModifyCalleeSavedReg(J, HRI)) ||
1147 (HII->isSaveCalleeSavedRegsCall(J) &&
1148 doesModifyCalleeSavedReg(I, HRI)))
1149 return true;
1150
1151 // Two control flow instructions cannot go in the same packet.
1152 if (isControlFlow(I) && isControlFlow(J))
1153 return true;
1154
1155 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1156 // contain a speculative indirect jump,
1157 // a new-value compare jump or a dealloc_return.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001158 auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
1159 if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001160 return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001161 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001162 return true;
1163 return false;
1164 };
1165
1166 if (HII->isLoopN(I) && isBadForLoopN(J))
1167 return true;
1168 if (HII->isLoopN(J) && isBadForLoopN(I))
1169 return true;
1170
1171 // dealloc_return cannot appear in the same packet as a conditional or
1172 // unconditional jump.
1173 return HII->isDeallocRet(I) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001174 (J.isBranch() || J.isCall() || J.isBarrier());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001175}
1176
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001177bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I,
1178 const MachineInstr &J) {
1179 // Adding I to a packet that has J.
1180
1181 // Regmasks are not reflected in the scheduling dependency graph, so
1182 // we need to check them manually. This code assumes that regmasks only
1183 // occur on calls, and the problematic case is when we add an instruction
1184 // defining a register R to a packet that has a call that clobbers R via
1185 // a regmask. Those cannot be packetized together, because the call will
1186 // be executed last. That's also a reson why it is ok to add a call
1187 // clobbering R to a packet that defines R.
1188
1189 // Look for regmasks in J.
1190 for (const MachineOperand &OpJ : J.operands()) {
1191 if (!OpJ.isRegMask())
1192 continue;
1193 assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call");
1194 for (const MachineOperand &OpI : I.operands()) {
1195 if (OpI.isReg()) {
1196 if (OpJ.clobbersPhysReg(OpI.getReg()))
1197 return true;
1198 } else if (OpI.isRegMask()) {
1199 // Both are regmasks. Assume that they intersect.
1200 return true;
1201 }
1202 }
1203 }
1204 return false;
1205}
1206
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001207bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I,
1208 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001209 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001210 bool StoreI = I.mayStore(), StoreJ = J.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001211 if ((SysI && StoreJ) || (SysJ && StoreI))
1212 return true;
1213
1214 if (StoreI && StoreJ) {
1215 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1216 return true;
1217 } else {
1218 // A memop cannot be in the same packet with another memop or a store.
1219 // Two stores can be together, but here I and J cannot both be stores.
1220 bool MopStI = HII->isMemOp(I) || StoreI;
1221 bool MopStJ = HII->isMemOp(J) || StoreJ;
1222 if (MopStI && MopStJ)
1223 return true;
1224 }
1225
1226 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1227}
1228
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001229// SUI is the current instruction that is out side of the current packet.
1230// SUJ is the current instruction inside the current packet against which that
1231// SUI will be packetized.
1232bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001233 assert(SUI->getInstr() && SUJ->getInstr());
1234 MachineInstr &I = *SUI->getInstr();
1235 MachineInstr &J = *SUJ->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001236
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001237 // Clear IgnoreDepMIs when Packet starts.
1238 if (CurrentPacketMIs.size() == 1)
1239 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001240
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001241 MachineBasicBlock::iterator II = I.getIterator();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001242
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001243 // Solo instructions cannot go in the packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001244 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001245
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001246 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001247 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001248
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001249 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1250 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001251 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001252
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001253 // Regmasks are not accounted for in the scheduling graph, so we need
1254 // to explicitly check for dependencies caused by them. They should only
1255 // appear on calls, so it's not too pessimistic to reject all regmask
1256 // dependencies.
1257 Dependence = hasRegMaskDependence(I, J);
1258 if (Dependence)
1259 return false;
1260
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001261 // V4 allows dual stores. It does not allow second store, if the first
1262 // store is not in SLOT0. New value store, new value jump, dealloc_return
1263 // and memop always take SLOT0. Arch spec 3.4.4.2.
1264 Dependence = hasV4SpecificDependence(I, J);
1265 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001266 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001267
1268 // If an instruction feeds new value jump, glue it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001269 MachineBasicBlock::iterator NextMII = I.getIterator();
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001270 ++NextMII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001271 if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001272 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001273
1274 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001275 const MachineOperand &NOp0 = NextMI.getOperand(0);
1276 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001277
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001278 if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001279 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001280
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001281 for (auto T : CurrentPacketMIs) {
1282 SUnit *PacketSU = MIToSUnit.find(T)->second;
1283 MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001284 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001285 if (PI.isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001286 Dependence = true;
1287 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001288 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001289 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001290 // 1. Packet does not have a store in it.
1291 // 2. If the first operand of the nvj is newified, and the second
1292 // operand is also a reg, it (second reg) is not defined in
1293 // the same packet.
1294 // 3. If the second operand of the nvj is newified, (which means
1295 // first operand is also a reg), first reg is not defined in
1296 // the same packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001297 if (PI.getOpcode() == Hexagon::S2_allocframe || PI.mayStore() ||
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001298 HII->isLoopN(PI)) {
1299 Dependence = true;
1300 break;
1301 }
1302 // Check #2/#3.
1303 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001304 if (OpR.isReg() && PI.modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001305 Dependence = true;
1306 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001307 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001308 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001309
1310 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001311 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001312 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001313 }
1314
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001315 // There no dependency between a prolog instruction and its successor.
1316 if (!SUJ->isSucc(SUI))
1317 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001318
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001319 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1320 if (FoundSequentialDependence)
1321 break;
1322
1323 if (SUJ->Succs[i].getSUnit() != SUI)
1324 continue;
1325
1326 SDep::Kind DepType = SUJ->Succs[i].getKind();
1327 // For direct calls:
1328 // Ignore register dependences for call instructions for packetization
1329 // purposes except for those due to r31 and predicate registers.
1330 //
1331 // For indirect calls:
1332 // Same as direct calls + check for true dependences to the register
1333 // used in the indirect call.
1334 //
1335 // We completely ignore Order dependences for call instructions.
1336 //
1337 // For returns:
1338 // Ignore register dependences for return instructions like jumpr,
1339 // dealloc return unless we have dependencies on the explicit uses
1340 // of the registers used by jumpr (like r31) or dealloc return
1341 // (like r29 or r30).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001342 unsigned DepReg = 0;
1343 const TargetRegisterClass *RC = nullptr;
1344 if (DepType == SDep::Data) {
1345 DepReg = SUJ->Succs[i].getReg();
1346 RC = HRI->getMinimalPhysRegClass(DepReg);
1347 }
1348
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +00001349 if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001350 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001351 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001352 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1353 continue;
1354 }
1355
1356 if (DepType == SDep::Data) {
1357 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1358 if (promoteToDotCur(J, DepType, II, RC))
1359 continue;
1360 }
1361
1362 // Data dpendence ok if we have load.cur.
1363 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001364 if (HII->isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001365 continue;
1366 }
1367
1368 // For instructions that can be promoted to dot-new, try to promote.
1369 if (DepType == SDep::Data) {
1370 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1371 if (promoteToDotNew(I, DepType, II, RC)) {
1372 PromotedToDotNew = true;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001373 if (cannotCoexist(I, J))
1374 FoundSequentialDependence = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001375 continue;
1376 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001377 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001378 if (HII->isNewValueJump(I))
1379 continue;
1380 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001381
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001382 // For predicated instructions, if the predicates are complements then
1383 // there can be no dependence.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001384 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1385 arePredicatesComplements(I, J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001386 // Not always safe to do this translation.
1387 // DAG Builder attempts to reduce dependence edges using transitive
1388 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001389 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001390 // r0 = tfr_pt ... (1)
1391 // r0 = tfr_pf ... (2)
1392 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001393 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001394 // There will be an output dependence between (1)->(2) and (2)->(3).
1395 // However, there is no dependence edge between (1)->(3). This results
1396 // in all 3 instructions going in the same packet. We ignore dependce
1397 // only once to avoid this situation.
David Majnemer0d955d02016-08-11 22:21:41 +00001398 auto Itr = find(IgnoreDepMIs, &J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001399 if (Itr != IgnoreDepMIs.end()) {
1400 Dependence = true;
1401 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001402 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001403 IgnoreDepMIs.push_back(&I);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001404 continue;
1405 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001406
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001407 // Ignore Order dependences between unconditional direct branches
1408 // and non-control-flow instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001409 if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001410 DepType == SDep::Order)
1411 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001412
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001413 // Ignore all dependences for jumps except for true and output
1414 // dependences.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001415 if (I.isConditionalBranch() && DepType != SDep::Data &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001416 DepType != SDep::Output)
1417 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001418
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001419 if (DepType == SDep::Output) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001420 FoundSequentialDependence = true;
1421 break;
1422 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001423
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001424 // For Order dependences:
1425 // 1. On V4 or later, volatile loads/stores can be packetized together,
1426 // unless other rules prevent is.
1427 // 2. Store followed by a load is not allowed.
1428 // 3. Store followed by a store is only valid on V4 or later.
1429 // 4. Load followed by any memory operation is allowed.
1430 if (DepType == SDep::Order) {
1431 if (!PacketizeVolatiles) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001432 bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001433 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001434 FoundSequentialDependence = true;
1435 break;
1436 }
1437 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001438 // J is first, I is second.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001439 bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
1440 bool LoadI = I.mayLoad(), StoreI = I.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001441 if (StoreJ) {
1442 // Two stores are only allowed on V4+. Load following store is never
1443 // allowed.
1444 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001445 FoundSequentialDependence = true;
1446 break;
1447 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001448 } else if (!LoadJ || (!LoadI && !StoreI)) {
1449 // If J is neither load nor store, assume a dependency.
1450 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001451 FoundSequentialDependence = true;
1452 break;
1453 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001454 // Store followed by store: not OK on V2.
1455 // Store followed by load: not OK on all.
1456 // Load followed by store: OK on all.
1457 // Load followed by load: OK on all.
1458 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001459 }
1460
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001461 // For V4, special case ALLOCFRAME. Even though there is dependency
1462 // between ALLOCFRAME and subsequent store, allow it to be packetized
1463 // in a same packet. This implies that the store is using the caller's
1464 // SP. Hence, offset needs to be updated accordingly.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001465 if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
1466 unsigned Opc = I.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001467 switch (Opc) {
1468 case Hexagon::S2_storerd_io:
1469 case Hexagon::S2_storeri_io:
1470 case Hexagon::S2_storerh_io:
1471 case Hexagon::S2_storerb_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001472 if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001473 // Since this store is to be glued with allocframe in the same
1474 // packet, it will use SP of the previous stack frame, i.e.
1475 // caller's SP. Therefore, we need to recalculate offset
1476 // according to this change.
1477 GlueAllocframeStore = useCallersSP(I);
1478 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001479 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001480 }
1481 default:
1482 break;
1483 }
1484 }
1485
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001486 // There are certain anti-dependencies that cannot be ignored.
1487 // Specifically:
1488 // J2_call ... %R0<imp-def> ; SUJ
1489 // R0 = ... ; SUI
1490 // Those cannot be packetized together, since the call will observe
1491 // the effect of the assignment to R0.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001492 if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) {
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001493 // Check if I defines any volatile register. We should also check
1494 // registers that the call may read, but these happen to be a
1495 // subset of the volatile register set.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001496 for (const MachineOperand &Op : I.operands()) {
1497 if (Op.isReg() && Op.isDef()) {
1498 unsigned R = Op.getReg();
1499 if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI))
1500 continue;
1501 } else if (!Op.isRegMask()) {
1502 // If I has a regmask assume dependency.
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001503 continue;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001504 }
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001505 FoundSequentialDependence = true;
1506 break;
1507 }
1508 }
1509
1510 // Skip over remaining anti-dependences. Two instructions that are
1511 // anti-dependent can share a packet, since in most such cases all
1512 // operands are read before any modifications take place.
1513 // The exceptions are branch and call instructions, since they are
1514 // executed after all other instructions have completed (at least
1515 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001516 if (DepType != SDep::Anti) {
1517 FoundSequentialDependence = true;
1518 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001519 }
1520 }
1521
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001522 if (FoundSequentialDependence) {
1523 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001524 return false;
1525 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001526
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001527 return true;
1528}
1529
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001530bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001531 assert(SUI->getInstr() && SUJ->getInstr());
1532 MachineInstr &I = *SUI->getInstr();
1533 MachineInstr &J = *SUJ->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001534
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001535 bool Coexist = !cannotCoexist(I, J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001536
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001537 if (Coexist && !Dependence)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001538 return true;
1539
1540 // Check if the instruction was promoted to a dot-new. If so, demote it
1541 // back into a dot-old.
1542 if (PromotedToDotNew)
1543 demoteToDotOld(I);
1544
1545 cleanUpDotCur();
1546 // Check if the instruction (must be a store) was glued with an allocframe
1547 // instruction. If so, restore its offset to its original value, i.e. use
1548 // current SP instead of caller's SP.
1549 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001550 useCalleesSP(I);
1551 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001552 }
1553 return false;
1554}
1555
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001556MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001557HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001558 MachineBasicBlock::iterator MII = MI.getIterator();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001559 MachineBasicBlock *MBB = MI.getParent();
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001560
1561 if (CurrentPacketMIs.size() == 0)
1562 PacketStalls = false;
1563 PacketStalls |= producesStall(MI);
1564
1565 if (MI.isImplicitDef())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001566 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001567 assert(ResourceTracker->canReserveResources(MI));
1568
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001569 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001570 bool Good = true;
1571
1572 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001573 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001574 // We need to put both instructions in the same packet: MI and NvjMI.
1575 // Either of them can require a constant extender. Try to add both to
1576 // the current packet, and if that fails, end the packet and start a
1577 // new one.
1578 ResourceTracker->reserveResources(MI);
1579 if (ExtMI)
1580 Good = tryAllocateResourcesForConstExt(true);
1581
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001582 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001583 if (Good) {
1584 if (ResourceTracker->canReserveResources(NvjMI))
1585 ResourceTracker->reserveResources(NvjMI);
1586 else
1587 Good = false;
1588 }
1589 if (Good && ExtNvjMI)
1590 Good = tryAllocateResourcesForConstExt(true);
1591
1592 if (!Good) {
1593 endPacket(MBB, MI);
1594 assert(ResourceTracker->canReserveResources(MI));
1595 ResourceTracker->reserveResources(MI);
1596 if (ExtMI) {
1597 assert(canReserveResourcesForConstExt());
1598 tryAllocateResourcesForConstExt(true);
1599 }
1600 assert(ResourceTracker->canReserveResources(NvjMI));
1601 ResourceTracker->reserveResources(NvjMI);
1602 if (ExtNvjMI) {
1603 assert(canReserveResourcesForConstExt());
1604 reserveResourcesForConstExt();
1605 }
1606 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001607 CurrentPacketMIs.push_back(&MI);
1608 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001609 return MII;
1610 }
1611
1612 ResourceTracker->reserveResources(MI);
1613 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1614 endPacket(MBB, MI);
1615 if (PromotedToDotNew)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001616 demoteToDotOld(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001617 if (GlueAllocframeStore) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001618 useCalleesSP(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001619 GlueAllocframeStore = false;
1620 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001621 ResourceTracker->reserveResources(MI);
1622 reserveResourcesForConstExt();
1623 }
1624
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001625 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001626 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001627}
1628
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001629void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001630 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001631 OldPacketMIs = CurrentPacketMIs;
1632 VLIWPacketizerList::endPacket(MBB, MI);
1633}
1634
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001635bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001636 return !producesStall(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001637}
1638
1639
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001640// V60 forward scheduling.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001641bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001642 // If the packet already stalls, then ignore the stall from a subsequent
1643 // instruction in the same packet.
1644 if (PacketStalls)
1645 return false;
1646
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001647 // Check whether the previous packet is in a different loop. If this is the
1648 // case, there is little point in trying to avoid a stall because that would
1649 // favor the rare case (loop entry) over the common case (loop iteration).
1650 //
1651 // TODO: We should really be able to check all the incoming edges if this is
1652 // the first packet in a basic block, so we can avoid stalls from the loop
1653 // backedge.
1654 if (!OldPacketMIs.empty()) {
1655 auto *OldBB = OldPacketMIs.front()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001656 auto *ThisBB = I.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001657 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1658 return false;
1659 }
1660
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001661 SUnit *SUI = MIToSUnit[const_cast<MachineInstr *>(&I)];
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001662
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001663 // Check if the latency is 0 between this instruction and any instruction
1664 // in the current packet. If so, we disregard any potential stalls due to
1665 // the instructions in the previous packet. Most of the instruction pairs
1666 // that can go together in the same packet have 0 latency between them.
1667 // Only exceptions are newValueJumps as they're generated much later and
1668 // the latencies can't be changed at that point. Another is .cur
1669 // instructions if its consumer has a 0 latency successor (such as .new).
1670 // In this case, the latency between .cur and the consumer stays non-zero
1671 // even though we can have both .cur and .new in the same packet. Changing
1672 // the latency to 0 is not an option as it causes software pipeliner to
1673 // not pipeline in some cases.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001674
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001675 // For Example:
1676 // {
1677 // I1: v6.cur = vmem(r0++#1)
1678 // I2: v7 = valign(v6,v4,r2)
1679 // I3: vmem(r5++#1) = v7.new
1680 // }
1681 // Here I2 and I3 has 0 cycle latency, but I1 and I2 has 2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001682
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001683 for (auto J : CurrentPacketMIs) {
1684 SUnit *SUJ = MIToSUnit[J];
1685 for (auto &Pred : SUI->Preds)
1686 if (Pred.getSUnit() == SUJ &&
1687 (Pred.getLatency() == 0 || HII->isNewValueJump(I) ||
1688 HII->isToBeScheduledASAP(*J, I)))
1689 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001690 }
1691
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001692 // Check if the latency is greater than one between this instruction and any
1693 // instruction in the previous packet.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001694 for (auto J : OldPacketMIs) {
1695 SUnit *SUJ = MIToSUnit[J];
1696 for (auto &Pred : SUI->Preds)
1697 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1698 return true;
1699 }
1700
1701 // Check if the latency is greater than one between this instruction and any
1702 // instruction in the previous packet.
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001703 for (auto J : OldPacketMIs) {
1704 SUnit *SUJ = MIToSUnit[J];
1705 for (auto &Pred : SUI->Preds)
1706 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1707 return true;
1708 }
1709
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001710 return false;
1711}
1712
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001713//===----------------------------------------------------------------------===//
1714// Public Constructor Functions
1715//===----------------------------------------------------------------------===//
1716
1717FunctionPass *llvm::createHexagonPacketizer() {
1718 return new HexagonPacketizer();
1719}