blob: a3021e3dfe432c3bda3fb546f744666b38e5cf06 [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
106 addMutation(make_unique<HexagonSubtarget::HexagonDAGMutation>());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000107}
108
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000109// Check if FirstI modifies a register that SecondI reads.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000110static bool hasWriteToReadDep(const MachineInstr &FirstI,
111 const MachineInstr &SecondI,
112 const TargetRegisterInfo *TRI) {
113 for (auto &MO : FirstI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000114 if (!MO.isReg() || !MO.isDef())
115 continue;
116 unsigned R = MO.getReg();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000117 if (SecondI.readsRegister(R, TRI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000118 return true;
119 }
120 return false;
121}
122
123
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000124static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000125 MachineBasicBlock::iterator BundleIt, bool Before) {
126 MachineBasicBlock::instr_iterator InsertPt;
127 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000128 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000129 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000130 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000131
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000132 MachineBasicBlock &B = *MI.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000133 // The instruction should at least be bundled with the preceding instruction
134 // (there will always be one, i.e. BUNDLE, if nothing else).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000135 assert(MI.isBundledWithPred());
136 if (MI.isBundledWithSucc()) {
137 MI.clearFlag(MachineInstr::BundledSucc);
138 MI.clearFlag(MachineInstr::BundledPred);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000139 } else {
140 // If it's not bundled with the successor (i.e. it is the last one
141 // in the bundle), then we can simply unbundle it from the predecessor,
142 // which will take care of updating the predecessor's flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000143 MI.unbundleFromPred();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000144 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000145 B.splice(InsertPt, &B, MI.getIterator());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000146
147 // Get the size of the bundle without asserting.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000148 MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000149 MachineBasicBlock::const_instr_iterator E = B.instr_end();
150 unsigned Size = 0;
151 for (++I; I != E && I->isBundledWithPred(); ++I)
152 ++Size;
153
154 // If there are still two or more instructions, then there is nothing
155 // else to be done.
156 if (Size > 1)
157 return BundleIt;
158
159 // Otherwise, extract the single instruction out and delete the bundle.
160 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000161 MachineInstr &SingleI = *BundleIt->getNextNode();
162 SingleI.unbundleFromPred();
163 assert(!SingleI.isBundledWithSucc());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000164 BundleIt->eraseFromParent();
165 return NextIt;
166}
167
168
169bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000170 if (DisablePacketizer || skipFunction(*MF.getFunction()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000171 return false;
172
173 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
174 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
175 auto &MLI = getAnalysis<MachineLoopInfo>();
176 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
177 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
178
179 if (EnableGenAllInsnClass)
180 HII->genAllInsnTimingClasses(MF);
181
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000182 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000183 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000184
185 // DFA state table should not be empty.
186 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
187
188 //
189 // Loop over all basic blocks and remove KILL pseudo-instructions
190 // These instructions confuse the dependence analysis. Consider:
191 // D0 = ... (Insn 0)
192 // R0 = KILL R0, D0 (Insn 1)
193 // R0 = ... (Insn 2)
194 // Here, Insn 1 will result in the dependence graph not emitting an output
195 // dependence between Insn 0 and Insn 2. This can lead to incorrect
196 // packetization
197 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000198 for (auto &MB : MF) {
199 auto End = MB.end();
200 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000201 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000202 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000203 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000204 MB.erase(MI);
205 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000206 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000207 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000208 }
209 }
210
211 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000212 for (auto &MB : MF) {
213 auto Begin = MB.begin(), End = MB.end();
214 while (Begin != End) {
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000215 // Find the first non-boundary starting from the end of the last
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000216 // scheduling region.
217 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000218 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000219 ++RB;
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000220 // Find the first boundary starting from the beginning of the new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000221 // region.
222 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000223 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000224 ++RE;
225 // Add the scheduling boundary if it's not block end.
226 if (RE != End)
227 ++RE;
228 // If RB == End, then RE == End.
229 if (RB != End)
230 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000231
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000232 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000233 }
234 }
235
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000236 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000237 return true;
238}
239
240
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000241// Reserve resources for a constant extender. Trigger an assertion if the
242// reservation fails.
243void HexagonPacketizerList::reserveResourcesForConstExt() {
244 if (!tryAllocateResourcesForConstExt(true))
245 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000246}
247
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000248bool HexagonPacketizerList::canReserveResourcesForConstExt() {
249 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000250}
251
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000252// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
253// return true, otherwise, return false.
254bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
255 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000256 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000257 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000258 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000259 MF.DeleteMachineInstr(ExtMI);
260 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000261}
262
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000263
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000264bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000265 SDep::Kind DepType, unsigned DepReg) {
266 // Check for LR dependence.
267 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000268 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000269
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000270 if (HII->isDeallocRet(MI))
271 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000272 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000273
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000274 // Call-like instructions can be packetized with preceding instructions
275 // that define registers implicitly used or modified by the call. Explicit
276 // uses are still prohibited, as in the case of indirect calls:
277 // r0 = ...
278 // J2_jumpr r0
279 if (DepType == SDep::Data) {
280 for (const MachineOperand MO : MI.operands())
281 if (MO.isReg() && MO.getReg() == DepReg && !MO.isImplicit())
282 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000283 }
284
285 return false;
286}
287
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000288static bool isRegDependence(const SDep::Kind DepType) {
289 return DepType == SDep::Data || DepType == SDep::Anti ||
290 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000291}
292
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000293static bool isDirectJump(const MachineInstr &MI) {
294 return MI.getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000295}
296
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000297static bool isSchedBarrier(const MachineInstr &MI) {
298 switch (MI.getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000299 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000300 return true;
301 }
302 return false;
303}
304
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000305static bool isControlFlow(const MachineInstr &MI) {
306 return MI.getDesc().isTerminator() || MI.getDesc().isCall();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000307}
308
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000309
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000310/// Returns true if the instruction modifies a callee-saved register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000311static bool doesModifyCalleeSavedReg(const MachineInstr &MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000312 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000313 const MachineFunction &MF = *MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000314 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000315 if (MI.modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000316 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000317 return false;
318}
319
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000320// Returns true if an instruction can be promoted to .new predicate or
321// new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000322bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI,
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000323 const TargetRegisterClass *NewRC) {
324 // Vector stores can be predicated, and can be new-value stores, but
325 // they cannot be predicated on a .new predicate value.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000326 if (NewRC == &Hexagon::PredRegsRegClass) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000327 if (HII->isHVXVec(MI) && MI.mayStore())
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000328 return false;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000329 return HII->isPredicated(MI) && HII->getDotNewPredOp(MI, nullptr) > 0;
330 }
331 // If the class is not PredRegs, it could only apply to new-value stores.
332 return HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000333}
334
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000335// Promote an instructiont to its .cur form.
336// At this time, we have already made a call to canPromoteToDotCur and made
337// sure that it can *indeed* be promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000338bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000339 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
340 const TargetRegisterClass* RC) {
341 assert(DepType == SDep::Data);
342 int CurOpcode = HII->getDotCurOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000343 MI.setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000344 return true;
345}
346
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000347void HexagonPacketizerList::cleanUpDotCur() {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000348 MachineInstr *MI = nullptr;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000349 for (auto BI : CurrentPacketMIs) {
350 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000351 if (HII->isDotCurInst(*BI)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000352 MI = BI;
353 continue;
354 }
355 if (MI) {
356 for (auto &MO : BI->operands())
357 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
358 return;
359 }
360 }
361 if (!MI)
362 return;
363 // We did not find a use of the CUR, so de-cur it.
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000364 MI->setDesc(HII->get(HII->getNonDotCurOp(*MI)));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000365 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
366}
367
368// Check to see if an instruction can be dot cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000369bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000370 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
371 const TargetRegisterClass *RC) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000372 if (!HII->isHVXVec(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000373 return false;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000374 if (!HII->isHVXVec(*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000375 return false;
376
377 // Already a dot new instruction.
378 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
379 return false;
380
381 if (!HII->mayBeCurLoad(MI))
382 return false;
383
384 // The "cur value" cannot come from inline asm.
385 if (PacketSU->getInstr()->isInlineAsm())
386 return false;
387
388 // Make sure candidate instruction uses cur.
389 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000390 MI.dump();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000391 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000392 MachineInstr &MJ = *MII;
393 DEBUG({
394 dbgs() << "Checking CUR against ";
395 MJ.dump();
396 });
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000397 unsigned DestReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000398 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000399 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000400 if (MO.isReg() && MO.getReg() == DestReg)
401 FoundMatch = true;
402 if (!FoundMatch)
403 return false;
404
405 // Check for existing uses of a vector register within the packet which
406 // would be affected by converting a vector load into .cur formt.
407 for (auto BI : CurrentPacketMIs) {
408 DEBUG(dbgs() << "packet has "; BI->dump(););
409 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
410 return false;
411 }
412
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000413 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000414 // We can convert the opcode into a .cur.
415 return true;
416}
417
418// Promote an instruction to its .new form. At this time, we have already
419// made a call to canPromoteToDotNew and made sure that it can *indeed* be
420// promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000421bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000422 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
423 const TargetRegisterClass* RC) {
424 assert (DepType == SDep::Data);
425 int NewOpcode;
426 if (RC == &Hexagon::PredRegsRegClass)
427 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
428 else
429 NewOpcode = HII->getDotNewOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000430 MI.setDesc(HII->get(NewOpcode));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000431 return true;
432}
433
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000434bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +0000435 int NewOpcode = HII->getDotOldOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000436 MI.setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000437 return true;
438}
439
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000440bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) {
441 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000442 switch (Opc) {
443 case Hexagon::S2_storerd_io:
444 case Hexagon::S2_storeri_io:
445 case Hexagon::S2_storerh_io:
446 case Hexagon::S2_storerb_io:
447 break;
448 default:
449 llvm_unreachable("Unexpected instruction");
450 }
Matthias Braun941a7052016-07-28 18:40:00 +0000451 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000452 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000453 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
454 if (HII->isValidOffset(Opc, NewOff)) {
455 Off.setImm(NewOff);
456 return true;
457 }
458 return false;
459}
460
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000461void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) {
462 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000463 switch (Opc) {
464 case Hexagon::S2_storerd_io:
465 case Hexagon::S2_storeri_io:
466 case Hexagon::S2_storerh_io:
467 case Hexagon::S2_storerb_io:
468 break;
469 default:
470 llvm_unreachable("Unexpected instruction");
471 }
Matthias Braun941a7052016-07-28 18:40:00 +0000472 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000473 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000474 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
475}
476
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000477enum PredicateKind {
478 PK_False,
479 PK_True,
480 PK_Unknown
481};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000482
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000483/// Returns true if an instruction is predicated on p0 and false if it's
484/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000485static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000486 const HexagonInstrInfo *HII) {
487 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000488 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000489 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000490 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000491 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000492}
493
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000494static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000495 const HexagonInstrInfo *HII) {
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000496 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000497#ifndef NDEBUG
498 // Post Increment means duplicates. Use dense map to find duplicates in the
499 // list. Caution: Densemap initializes with the minimum of 64 buckets,
500 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000501 DenseSet<unsigned> DefRegsSet;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000502 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000503 if (MO.isReg() && MO.isDef())
504 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000505
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000506 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000507 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
508 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000509#else
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000510 if (MI.mayLoad()) {
511 const MachineOperand &Op1 = MI.getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000512 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000513 assert(Op1.isReg() && "Post increment operand has be to a register.");
514 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000515 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000516 if (MI.getDesc().mayStore()) {
517 const MachineOperand &Op0 = MI.getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000518 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000519 assert(Op0.isReg() && "Post increment operand has be to a register.");
520 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000521 }
522#endif
523 // we should never come here.
524 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
525}
526
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000527// Get the value being stored.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000528static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000529 // value being stored is always the last operand.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000530 return MI.getOperand(MI.getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000531}
532
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000533static bool isLoadAbsSet(const MachineInstr &MI) {
534 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000535 switch (Opc) {
536 case Hexagon::L4_loadrd_ap:
537 case Hexagon::L4_loadrb_ap:
538 case Hexagon::L4_loadrh_ap:
539 case Hexagon::L4_loadrub_ap:
540 case Hexagon::L4_loadruh_ap:
541 case Hexagon::L4_loadri_ap:
542 return true;
543 }
544 return false;
545}
546
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000547static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000548 assert(isLoadAbsSet(MI));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000549 return MI.getOperand(1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000550}
551
552
553// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000554// Following restrictions are to be respected in convert a store into
555// a new value store.
556// 1. If an instruction uses auto-increment, its address register cannot
557// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000558// 2. If an instruction uses absolute-set addressing mode, its address
559// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000560// 3. If an instruction produces a 64-bit result, its registers cannot be used
561// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000562// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000563// the instruction that uses the new-value register must also be conditional,
564// and both must always have their predicates evaluate identically.
565// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000566// 5. There is an implied restriction that a packet cannot have another store,
567// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000568// already a store in a packet, there can not be a new value store.
569// Arch Spec: 3.4.4.2
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000570bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
571 const MachineInstr &PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000572 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000573 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000574 return false;
575
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000576 // Make sure there is dependency and can be new value'd.
577 const MachineOperand &Val = getStoreValueOperand(MI);
578 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000579 return false;
580
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000581 const MCInstrDesc& MCID = PacketMI.getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000582
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000583 // First operand is always the result.
584 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
585 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
586 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000587 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000588
589 // New-value stores are of class NV (slot 0), dual stores require class ST
590 // in slot 0 (PRM 5.5).
591 for (auto I : CurrentPacketMIs) {
592 SUnit *PacketSU = MIToSUnit.find(I)->second;
593 if (PacketSU->getInstr()->mayStore())
594 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000595 }
596
597 // Make sure it's NOT the post increment register that we are going to
598 // new value.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000599 if (HII->isPostIncrement(MI) &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000600 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000601 return false;
602 }
603
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000604 if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000605 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
606 // If source is post_inc, or absolute-set addressing, it can not feed
607 // into new value store
608 // r3 = memw(r2++#4)
609 // memw(r30 + #-1404) = r2.new -> can not be new value store
610 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000611 return false;
612 }
613
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000614 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
615 return false;
616
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000617 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000618 // also be predicated.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000619 if (HII->isPredicated(PacketMI)) {
620 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000621 return false;
622
623 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000624 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000625 unsigned predRegNumSrc = 0;
626 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000627 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000628
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000629 // Get predicate register used in the source instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000630 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000631 if (!MO.isReg())
632 continue;
633 predRegNumSrc = MO.getReg();
634 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
635 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000636 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000637 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000638 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
639 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000640
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000641 // Get predicate register used in new-value store instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000642 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000643 if (!MO.isReg())
644 continue;
645 predRegNumDst = MO.getReg();
646 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
647 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000648 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000649 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000650 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
651 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000652
653 // New-value register producer and user (store) need to satisfy these
654 // constraints:
655 // 1) Both instructions should be predicated on the same register.
656 // 2) If producer of the new-value register is .new predicated then store
657 // should also be .new predicated and if producer is not .new predicated
658 // then store should not be .new predicated.
659 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000660 // sense, i.e, either both should be negated or both should be non-negated.
661 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000662 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000663 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000664 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000665 }
666
667 // Make sure that other than the new-value register no other store instruction
668 // register has been modified in the same packet. Predicate registers can be
669 // modified by they should not be modified between the producer and the store
670 // instruction as it will make them both conditional on different values.
671 // We already know this to be true for all the instructions before and
672 // including PacketMI. Howerver, we need to perform the check for the
673 // remaining instructions in the packet.
674
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000675 unsigned StartCheck = 0;
676
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000677 for (auto I : CurrentPacketMIs) {
678 SUnit *TempSU = MIToSUnit.find(I)->second;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000679 MachineInstr &TempMI = *TempSU->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000680
681 // Following condition is true for all the instructions until PacketMI is
682 // reached (StartCheck is set to 0 before the for loop).
683 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000684 if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
685 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000686
687 StartCheck = 1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000688 if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000689 continue;
690
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000691 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000692 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000693 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000694 }
695
Alp Tokerf907b892013-12-05 05:44:44 +0000696 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000697 // 1. The only use of reg is DepReg and no other registers.
698 // This handles V4 base+index registers.
699 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000700 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000701 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000702 if (!HII->isPostIncrement(MI)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000703 for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
704 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000705 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000706 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000707 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000708 }
709
710 // If data definition is because of implicit definition of the register,
711 // do not newify the store. Eg.
712 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
713 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000714 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000715 if (MO.isRegMask() && MO.clobbersPhysReg(DepReg))
716 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000717 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
718 continue;
719 unsigned R = MO.getReg();
720 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
721 return false;
722 }
723
724 // Handle imp-use of super reg case. There is a target independent side
725 // change that should prevent this situation but I am handling it for
726 // just-in-case. For example, we cannot newify R2 in the following case:
727 // %R3<def> = A2_tfrsi 0;
728 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000729 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000730 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
731 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000732 }
733
734 // Can be dot new store.
735 return true;
736}
737
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000738// Can this MI to promoted to either new value store or new value jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000739bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000740 const SUnit *PacketSU, unsigned DepReg,
741 MachineBasicBlock::iterator &MII) {
742 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000743 return false;
744
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000745 // Check to see the store can be new value'ed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000746 MachineInstr &PacketMI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000747 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000748 return true;
749
750 // Check to see the compare/jump can be new value'ed.
751 // This is done as a pass on its own. Don't need to check it here.
752 return false;
753}
754
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000755static bool isImplicitDependency(const MachineInstr &I, bool CheckDef,
756 unsigned DepReg) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000757 for (auto &MO : I.operands()) {
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000758 if (CheckDef && MO.isRegMask() && MO.clobbersPhysReg(DepReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000759 return true;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000760 if (!MO.isReg() || MO.getReg() != DepReg || !MO.isImplicit())
761 continue;
762 if (CheckDef == MO.isDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000763 return true;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000764 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000765 return false;
766}
767
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000768// Check to see if an instruction can be dot new
769// There are three kinds.
770// 1. dot new on predicate - V2/V3/V4
771// 2. dot new on stores NV/ST - V4
772// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000773bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000774 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
775 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000776 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000777 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000778 return false;
779
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000780 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000781 return false;
782
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000783 const MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000784
785 // The "new value" cannot come from inline asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000786 if (PI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000787 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000788
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000789 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
790 // sense.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000791 if (PI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000792 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000793
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000794 // If dependency is trough an implicitly defined register, we should not
795 // newify the use.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000796 if (isImplicitDependency(PI, true, DepReg) ||
797 isImplicitDependency(MI, false, DepReg))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000798 return false;
799
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000800 const MCInstrDesc& MCID = PI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000801 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
802 if (DisableVecDblNVStores && VecRC == &Hexagon::VecDblRegsRegClass)
803 return false;
804
805 // predicate .new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000806 if (RC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000807 return HII->predCanBeUsedAsDotNew(PI, DepReg);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000808
809 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
810 return false;
811
812 // Create a dot new machine instruction to see if resources can be
813 // allocated. If not, bail out now.
814 int NewOpcode = HII->getDotNewOp(MI);
815 const MCInstrDesc &D = HII->get(NewOpcode);
816 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000817 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000818 MF.DeleteMachineInstr(NewMI);
819 if (!ResourcesAvailable)
820 return false;
821
822 // New Value Store only. New Value Jump generated as a separate pass.
823 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
824 return false;
825
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000826 return true;
827}
828
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000829// Go through the packet instructions and search for an anti dependency between
830// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000831// Trying to add
832// a) %R1<def> = TFRI_cdNotPt %P3, 2
833// to this packet:
834// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000835// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
836// c) %P3<def> = C2_tfrrp %R23
837// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000838// }
839// The P3 from a) and d) will be complements after
840// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000841// Anti-dep between c) and b) is irrelevant for this case
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000842bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000843 unsigned DepReg) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000844 SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000845
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000846 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000847 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000848 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000849 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000850
851 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000852 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000853
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000854 // Look at dependencies between current members of the packet and
855 // predicate defining instruction MI. Make sure that dependency is
856 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000857 if (PacketSU->isSucc(PacketSUDep)) {
858 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000859 auto &Dep = PacketSU->Succs[i];
860 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
861 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000862 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000863 }
864 }
865 }
866
867 return false;
868}
869
870
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000871/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000872static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000873 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000874 /// We use the following rule: The first predicate register that is a use is
875 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000876 assert(QII->isPredicated(MI) && "Must be predicated instruction");
877
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000878 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000879 if (Op.isReg() && Op.getReg() && Op.isUse() &&
880 Hexagon::PredRegsRegClass.contains(Op.getReg()))
881 return Op.getReg();
882 }
883
884 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000885 return 0;
886}
887
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000888// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000889// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000890bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
891 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000892 // If we don't know the predicate sense of the instructions bail out early, we
893 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000894 if (getPredicateSense(MI1, HII) == PK_Unknown ||
895 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000896 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000897
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000898 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000899 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000900
901 // One corner case deals with the following scenario:
902 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000903 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000904 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000905 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000906 // b) %R25<def> = A2_tfrf %P0, %R24
907 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000908 // }
909 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000910 // On general check a) and b) are complements, but presence of c) will
911 // convert a) to .new form, and then it is not a complement.
912 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000913
914 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000915 // Look for Anti dependecy on the same predicate reg as used in the
916 // candidate.
917 for (auto I : CurrentPacketMIs) {
918 // Scheduling Unit for current insn in the packet.
919 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000920
921 // If this instruction in the packet is succeeded by the candidate...
922 if (PacketSU->isSucc(SU)) {
923 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000924 auto Dep = PacketSU->Succs[i];
925 // The corner case exist when there is true data dependency between
926 // candidate and one of current packet members, this dep is on
927 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000928 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000929 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
930 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
931 // Here I know that I is predicate setting instruction with true
932 // data dep to candidate on the register we care about - c) in the
933 // above example. Now I need to see if there is an anti dependency
934 // from c) to any other instruction in the same packet on the pred
935 // reg of interest.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000936 if (restrictingDepExistInPacket(*I, Dep.getReg()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000937 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000938 }
939 }
940 }
941 }
942
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000943 // If the above case does not apply, check regular complement condition.
944 // Check that the predicate register is the same and that the predicate
945 // sense is different We also need to differentiate .old vs. .new: !p0
946 // is not complementary to p0.new.
947 unsigned PReg1 = getPredicatedRegister(MI1, HII);
948 unsigned PReg2 = getPredicatedRegister(MI2, HII);
949 return PReg1 == PReg2 &&
950 Hexagon::PredRegsRegClass.contains(PReg1) &&
951 Hexagon::PredRegsRegClass.contains(PReg2) &&
952 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000953 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000954}
955
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000956// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000957void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000958 Dependence = false;
959 PromotedToDotNew = false;
960 GlueToNewValueJump = false;
961 GlueAllocframeStore = false;
962 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000963}
964
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000965// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000966bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
967 const MachineBasicBlock *) {
968 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000969 return true;
970
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000971 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000972 return false;
973
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000974 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000975 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000976 return false;
977
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000978 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000979 return false;
980
981 // We check if MI has any functional units mapped to it. If it doesn't,
982 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000983 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000984 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +0000985 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000986 return !FuncUnits;
987}
988
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000989bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
990 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000991 return true;
992
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000993 // Consider inline asm to not be a solo instruction by default.
994 // Inline asm will be put in a packet temporarily, but then it will be
995 // removed, and placed outside of the packet (before or after, depending
996 // on dependencies). This is to reduce the impact of inline asm as a
997 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000998 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000999 return true;
1000
1001 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
1002 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
1003 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001004 if (isSchedBarrier(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001005 return true;
1006
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001007 if (HII->isSolo(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001008 return true;
1009
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001010 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001011 return true;
1012
1013 return false;
1014}
1015
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001016
1017// Quick check if instructions MI and MJ cannot coexist in the same packet.
1018// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1019// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1020// For full test call this function twice:
1021// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1022// Doing the test only one way saves the amount of code in this function,
1023// since every test would need to be repeated with the MI and MJ reversed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001024static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001025 const HexagonInstrInfo &HII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001026 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001027 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1028 HII.isHVXMemWithAIndirect(MI, MJ))
1029 return true;
1030
1031 // An inline asm cannot be together with a branch, because we may not be
1032 // able to remove the asm out after packetizing (i.e. if the asm must be
1033 // moved past the bundle). Similarly, two asms cannot be together to avoid
1034 // complications when determining their relative order outside of a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001035 if (MI.isInlineAsm())
1036 return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
1037 MJ.isCall() || MJ.isTerminator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001038
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001039 switch (MI.getOpcode()) {
1040 case (Hexagon::S2_storew_locked):
1041 case (Hexagon::S4_stored_locked):
1042 case (Hexagon::L2_loadw_locked):
1043 case (Hexagon::L4_loadd_locked):
1044 case (Hexagon::Y4_l2fetch): {
1045 // These instructions can only be grouped with ALU32 or non-floating-point
1046 // XTYPE instructions. Since there is no convenient way of identifying fp
1047 // XTYPE instructions, only allow grouping with ALU32 for now.
1048 unsigned TJ = HII.getType(MJ);
Krzysztof Parzyszek5ea971c2017-02-07 17:47:37 +00001049 if (TJ != HexagonII::TypeALU32_2op &&
1050 TJ != HexagonII::TypeALU32_3op &&
1051 TJ != HexagonII::TypeALU32_ADDI)
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001052 return true;
1053 break;
1054 }
1055 default:
1056 break;
1057 }
1058
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001059 // "False" really means that the quick check failed to determine if
1060 // I and J cannot coexist.
1061 return false;
1062}
1063
1064
1065// Full, symmetric check.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001066bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
1067 const MachineInstr &MJ) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001068 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1069}
1070
1071void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1072 for (auto &B : MF) {
1073 MachineBasicBlock::iterator BundleIt;
1074 MachineBasicBlock::instr_iterator NextI;
1075 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1076 NextI = std::next(I);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001077 MachineInstr &MI = *I;
1078 if (MI.isBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001079 BundleIt = I;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001080 if (!MI.isInsideBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001081 continue;
1082
1083 // Decide on where to insert the instruction that we are pulling out.
1084 // Debug instructions always go before the bundle, but the placement of
1085 // INLINE_ASM depends on potential dependencies. By default, try to
1086 // put it before the bundle, but if the asm writes to a register that
1087 // other instructions in the bundle read, then we need to place it
1088 // after the bundle (to preserve the bundle semantics).
1089 bool InsertBeforeBundle;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001090 if (MI.isInlineAsm())
1091 InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
1092 else if (MI.isDebugValue())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001093 InsertBeforeBundle = true;
1094 else
1095 continue;
1096
1097 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1098 }
1099 }
1100}
1101
1102// Check if a given instruction is of class "system".
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001103static bool isSystemInstr(const MachineInstr &MI) {
1104 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001105 switch (Opc) {
1106 case Hexagon::Y2_barrier:
1107 case Hexagon::Y2_dcfetchbo:
1108 return true;
1109 }
1110 return false;
1111}
1112
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001113bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
1114 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001115 // The dependence graph may not include edges between dead definitions,
1116 // so without extra checks, we could end up packetizing two instruction
1117 // defining the same (dead) register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001118 if (I.isCall() || J.isCall())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001119 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001120 if (HII->isPredicated(I) || HII->isPredicated(J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001121 return false;
1122
1123 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001124 for (auto &MO : I.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001125 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1126 continue;
1127 DeadDefs[MO.getReg()] = true;
1128 }
1129
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001130 for (auto &MO : J.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001131 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1132 continue;
1133 unsigned R = MO.getReg();
1134 if (R != Hexagon::USR_OVF && DeadDefs[R])
1135 return true;
1136 }
1137 return false;
1138}
1139
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001140bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
1141 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001142 // A save callee-save register function call can only be in a packet
1143 // with instructions that don't write to the callee-save registers.
1144 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1145 doesModifyCalleeSavedReg(J, HRI)) ||
1146 (HII->isSaveCalleeSavedRegsCall(J) &&
1147 doesModifyCalleeSavedReg(I, HRI)))
1148 return true;
1149
1150 // Two control flow instructions cannot go in the same packet.
1151 if (isControlFlow(I) && isControlFlow(J))
1152 return true;
1153
1154 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1155 // contain a speculative indirect jump,
1156 // a new-value compare jump or a dealloc_return.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001157 auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
1158 if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001159 return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001160 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001161 return true;
1162 return false;
1163 };
1164
1165 if (HII->isLoopN(I) && isBadForLoopN(J))
1166 return true;
1167 if (HII->isLoopN(J) && isBadForLoopN(I))
1168 return true;
1169
1170 // dealloc_return cannot appear in the same packet as a conditional or
1171 // unconditional jump.
1172 return HII->isDeallocRet(I) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001173 (J.isBranch() || J.isCall() || J.isBarrier());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001174}
1175
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001176bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I,
1177 const MachineInstr &J) {
1178 // Adding I to a packet that has J.
1179
1180 // Regmasks are not reflected in the scheduling dependency graph, so
1181 // we need to check them manually. This code assumes that regmasks only
1182 // occur on calls, and the problematic case is when we add an instruction
1183 // defining a register R to a packet that has a call that clobbers R via
1184 // a regmask. Those cannot be packetized together, because the call will
1185 // be executed last. That's also a reson why it is ok to add a call
1186 // clobbering R to a packet that defines R.
1187
1188 // Look for regmasks in J.
1189 for (const MachineOperand &OpJ : J.operands()) {
1190 if (!OpJ.isRegMask())
1191 continue;
1192 assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call");
1193 for (const MachineOperand &OpI : I.operands()) {
1194 if (OpI.isReg()) {
1195 if (OpJ.clobbersPhysReg(OpI.getReg()))
1196 return true;
1197 } else if (OpI.isRegMask()) {
1198 // Both are regmasks. Assume that they intersect.
1199 return true;
1200 }
1201 }
1202 }
1203 return false;
1204}
1205
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001206bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I,
1207 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001208 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001209 bool StoreI = I.mayStore(), StoreJ = J.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001210 if ((SysI && StoreJ) || (SysJ && StoreI))
1211 return true;
1212
1213 if (StoreI && StoreJ) {
1214 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1215 return true;
1216 } else {
1217 // A memop cannot be in the same packet with another memop or a store.
1218 // Two stores can be together, but here I and J cannot both be stores.
1219 bool MopStI = HII->isMemOp(I) || StoreI;
1220 bool MopStJ = HII->isMemOp(J) || StoreJ;
1221 if (MopStI && MopStJ)
1222 return true;
1223 }
1224
1225 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1226}
1227
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001228// SUI is the current instruction that is out side of the current packet.
1229// SUJ is the current instruction inside the current packet against which that
1230// SUI will be packetized.
1231bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001232 assert(SUI->getInstr() && SUJ->getInstr());
1233 MachineInstr &I = *SUI->getInstr();
1234 MachineInstr &J = *SUJ->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001235
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001236 // Clear IgnoreDepMIs when Packet starts.
1237 if (CurrentPacketMIs.size() == 1)
1238 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001239
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001240 MachineBasicBlock::iterator II = I.getIterator();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001241
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001242 // Solo instructions cannot go in the packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001243 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001244
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001245 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001246 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001247
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001248 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1249 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001250 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001251
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001252 // Regmasks are not accounted for in the scheduling graph, so we need
1253 // to explicitly check for dependencies caused by them. They should only
1254 // appear on calls, so it's not too pessimistic to reject all regmask
1255 // dependencies.
1256 Dependence = hasRegMaskDependence(I, J);
1257 if (Dependence)
1258 return false;
1259
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001260 // V4 allows dual stores. It does not allow second store, if the first
1261 // store is not in SLOT0. New value store, new value jump, dealloc_return
1262 // and memop always take SLOT0. Arch spec 3.4.4.2.
1263 Dependence = hasV4SpecificDependence(I, J);
1264 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001265 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001266
1267 // If an instruction feeds new value jump, glue it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001268 MachineBasicBlock::iterator NextMII = I.getIterator();
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001269 ++NextMII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001270 if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001271 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001272
1273 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001274 const MachineOperand &NOp0 = NextMI.getOperand(0);
1275 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001276
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001277 if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001278 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001279
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001280 for (auto T : CurrentPacketMIs) {
1281 SUnit *PacketSU = MIToSUnit.find(T)->second;
1282 MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001283 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001284 if (PI.isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001285 Dependence = true;
1286 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001287 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001288 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001289 // 1. Packet does not have a store in it.
1290 // 2. If the first operand of the nvj is newified, and the second
1291 // operand is also a reg, it (second reg) is not defined in
1292 // the same packet.
1293 // 3. If the second operand of the nvj is newified, (which means
1294 // first operand is also a reg), first reg is not defined in
1295 // the same packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001296 if (PI.getOpcode() == Hexagon::S2_allocframe || PI.mayStore() ||
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001297 HII->isLoopN(PI)) {
1298 Dependence = true;
1299 break;
1300 }
1301 // Check #2/#3.
1302 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001303 if (OpR.isReg() && PI.modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001304 Dependence = true;
1305 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001306 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001307 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001308
1309 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001310 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001311 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001312 }
1313
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001314 // There no dependency between a prolog instruction and its successor.
1315 if (!SUJ->isSucc(SUI))
1316 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001317
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001318 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1319 if (FoundSequentialDependence)
1320 break;
1321
1322 if (SUJ->Succs[i].getSUnit() != SUI)
1323 continue;
1324
1325 SDep::Kind DepType = SUJ->Succs[i].getKind();
1326 // For direct calls:
1327 // Ignore register dependences for call instructions for packetization
1328 // purposes except for those due to r31 and predicate registers.
1329 //
1330 // For indirect calls:
1331 // Same as direct calls + check for true dependences to the register
1332 // used in the indirect call.
1333 //
1334 // We completely ignore Order dependences for call instructions.
1335 //
1336 // For returns:
1337 // Ignore register dependences for return instructions like jumpr,
1338 // dealloc return unless we have dependencies on the explicit uses
1339 // of the registers used by jumpr (like r31) or dealloc return
1340 // (like r29 or r30).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001341 unsigned DepReg = 0;
1342 const TargetRegisterClass *RC = nullptr;
1343 if (DepType == SDep::Data) {
1344 DepReg = SUJ->Succs[i].getReg();
1345 RC = HRI->getMinimalPhysRegClass(DepReg);
1346 }
1347
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +00001348 if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001349 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001350 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001351 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1352 continue;
1353 }
1354
1355 if (DepType == SDep::Data) {
1356 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1357 if (promoteToDotCur(J, DepType, II, RC))
1358 continue;
1359 }
1360
1361 // Data dpendence ok if we have load.cur.
1362 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001363 if (HII->isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001364 continue;
1365 }
1366
1367 // For instructions that can be promoted to dot-new, try to promote.
1368 if (DepType == SDep::Data) {
1369 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1370 if (promoteToDotNew(I, DepType, II, RC)) {
1371 PromotedToDotNew = true;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001372 if (cannotCoexist(I, J))
1373 FoundSequentialDependence = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001374 continue;
1375 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001376 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001377 if (HII->isNewValueJump(I))
1378 continue;
1379 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001380
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001381 // For predicated instructions, if the predicates are complements then
1382 // there can be no dependence.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001383 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1384 arePredicatesComplements(I, J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001385 // Not always safe to do this translation.
1386 // DAG Builder attempts to reduce dependence edges using transitive
1387 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001388 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001389 // r0 = tfr_pt ... (1)
1390 // r0 = tfr_pf ... (2)
1391 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001392 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001393 // There will be an output dependence between (1)->(2) and (2)->(3).
1394 // However, there is no dependence edge between (1)->(3). This results
1395 // in all 3 instructions going in the same packet. We ignore dependce
1396 // only once to avoid this situation.
David Majnemer0d955d02016-08-11 22:21:41 +00001397 auto Itr = find(IgnoreDepMIs, &J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001398 if (Itr != IgnoreDepMIs.end()) {
1399 Dependence = true;
1400 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001401 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001402 IgnoreDepMIs.push_back(&I);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001403 continue;
1404 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001405
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001406 // Ignore Order dependences between unconditional direct branches
1407 // and non-control-flow instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001408 if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001409 DepType == SDep::Order)
1410 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001411
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001412 // Ignore all dependences for jumps except for true and output
1413 // dependences.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001414 if (I.isConditionalBranch() && DepType != SDep::Data &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001415 DepType != SDep::Output)
1416 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001417
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001418 if (DepType == SDep::Output) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001419 FoundSequentialDependence = true;
1420 break;
1421 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001422
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001423 // For Order dependences:
1424 // 1. On V4 or later, volatile loads/stores can be packetized together,
1425 // unless other rules prevent is.
1426 // 2. Store followed by a load is not allowed.
1427 // 3. Store followed by a store is only valid on V4 or later.
1428 // 4. Load followed by any memory operation is allowed.
1429 if (DepType == SDep::Order) {
1430 if (!PacketizeVolatiles) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001431 bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001432 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001433 FoundSequentialDependence = true;
1434 break;
1435 }
1436 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001437 // J is first, I is second.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001438 bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
1439 bool LoadI = I.mayLoad(), StoreI = I.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001440 if (StoreJ) {
1441 // Two stores are only allowed on V4+. Load following store is never
1442 // allowed.
1443 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001444 FoundSequentialDependence = true;
1445 break;
1446 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001447 } else if (!LoadJ || (!LoadI && !StoreI)) {
1448 // If J is neither load nor store, assume a dependency.
1449 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001450 FoundSequentialDependence = true;
1451 break;
1452 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001453 // Store followed by store: not OK on V2.
1454 // Store followed by load: not OK on all.
1455 // Load followed by store: OK on all.
1456 // Load followed by load: OK on all.
1457 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001458 }
1459
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001460 // For V4, special case ALLOCFRAME. Even though there is dependency
1461 // between ALLOCFRAME and subsequent store, allow it to be packetized
1462 // in a same packet. This implies that the store is using the caller's
1463 // SP. Hence, offset needs to be updated accordingly.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001464 if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
1465 unsigned Opc = I.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001466 switch (Opc) {
1467 case Hexagon::S2_storerd_io:
1468 case Hexagon::S2_storeri_io:
1469 case Hexagon::S2_storerh_io:
1470 case Hexagon::S2_storerb_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001471 if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001472 // Since this store is to be glued with allocframe in the same
1473 // packet, it will use SP of the previous stack frame, i.e.
1474 // caller's SP. Therefore, we need to recalculate offset
1475 // according to this change.
1476 GlueAllocframeStore = useCallersSP(I);
1477 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001478 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001479 }
1480 default:
1481 break;
1482 }
1483 }
1484
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001485 // There are certain anti-dependencies that cannot be ignored.
1486 // Specifically:
1487 // J2_call ... %R0<imp-def> ; SUJ
1488 // R0 = ... ; SUI
1489 // Those cannot be packetized together, since the call will observe
1490 // the effect of the assignment to R0.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001491 if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) {
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001492 // Check if I defines any volatile register. We should also check
1493 // registers that the call may read, but these happen to be a
1494 // subset of the volatile register set.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001495 for (const MachineOperand &Op : I.operands()) {
1496 if (Op.isReg() && Op.isDef()) {
1497 unsigned R = Op.getReg();
1498 if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI))
1499 continue;
1500 } else if (!Op.isRegMask()) {
1501 // If I has a regmask assume dependency.
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001502 continue;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001503 }
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001504 FoundSequentialDependence = true;
1505 break;
1506 }
1507 }
1508
1509 // Skip over remaining anti-dependences. Two instructions that are
1510 // anti-dependent can share a packet, since in most such cases all
1511 // operands are read before any modifications take place.
1512 // The exceptions are branch and call instructions, since they are
1513 // executed after all other instructions have completed (at least
1514 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001515 if (DepType != SDep::Anti) {
1516 FoundSequentialDependence = true;
1517 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001518 }
1519 }
1520
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001521 if (FoundSequentialDependence) {
1522 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001523 return false;
1524 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001525
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001526 return true;
1527}
1528
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001529bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001530 assert(SUI->getInstr() && SUJ->getInstr());
1531 MachineInstr &I = *SUI->getInstr();
1532 MachineInstr &J = *SUJ->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001533
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001534 bool Coexist = !cannotCoexist(I, J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001535
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001536 if (Coexist && !Dependence)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001537 return true;
1538
1539 // Check if the instruction was promoted to a dot-new. If so, demote it
1540 // back into a dot-old.
1541 if (PromotedToDotNew)
1542 demoteToDotOld(I);
1543
1544 cleanUpDotCur();
1545 // Check if the instruction (must be a store) was glued with an allocframe
1546 // instruction. If so, restore its offset to its original value, i.e. use
1547 // current SP instead of caller's SP.
1548 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001549 useCalleesSP(I);
1550 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001551 }
1552 return false;
1553}
1554
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001555MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001556HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001557 MachineBasicBlock::iterator MII = MI.getIterator();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001558 MachineBasicBlock *MBB = MI.getParent();
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001559
1560 if (CurrentPacketMIs.size() == 0)
1561 PacketStalls = false;
1562 PacketStalls |= producesStall(MI);
1563
1564 if (MI.isImplicitDef())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001565 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001566 assert(ResourceTracker->canReserveResources(MI));
1567
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001568 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001569 bool Good = true;
1570
1571 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001572 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001573 // We need to put both instructions in the same packet: MI and NvjMI.
1574 // Either of them can require a constant extender. Try to add both to
1575 // the current packet, and if that fails, end the packet and start a
1576 // new one.
1577 ResourceTracker->reserveResources(MI);
1578 if (ExtMI)
1579 Good = tryAllocateResourcesForConstExt(true);
1580
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001581 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001582 if (Good) {
1583 if (ResourceTracker->canReserveResources(NvjMI))
1584 ResourceTracker->reserveResources(NvjMI);
1585 else
1586 Good = false;
1587 }
1588 if (Good && ExtNvjMI)
1589 Good = tryAllocateResourcesForConstExt(true);
1590
1591 if (!Good) {
1592 endPacket(MBB, MI);
1593 assert(ResourceTracker->canReserveResources(MI));
1594 ResourceTracker->reserveResources(MI);
1595 if (ExtMI) {
1596 assert(canReserveResourcesForConstExt());
1597 tryAllocateResourcesForConstExt(true);
1598 }
1599 assert(ResourceTracker->canReserveResources(NvjMI));
1600 ResourceTracker->reserveResources(NvjMI);
1601 if (ExtNvjMI) {
1602 assert(canReserveResourcesForConstExt());
1603 reserveResourcesForConstExt();
1604 }
1605 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001606 CurrentPacketMIs.push_back(&MI);
1607 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001608 return MII;
1609 }
1610
1611 ResourceTracker->reserveResources(MI);
1612 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1613 endPacket(MBB, MI);
1614 if (PromotedToDotNew)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001615 demoteToDotOld(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001616 if (GlueAllocframeStore) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001617 useCalleesSP(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001618 GlueAllocframeStore = false;
1619 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001620 ResourceTracker->reserveResources(MI);
1621 reserveResourcesForConstExt();
1622 }
1623
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001624 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001625 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001626}
1627
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001628void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001629 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001630 OldPacketMIs = CurrentPacketMIs;
1631 VLIWPacketizerList::endPacket(MBB, MI);
1632}
1633
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001634bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001635 return !producesStall(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001636}
1637
1638
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001639// V60 forward scheduling.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001640bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001641 // If the packet already stalls, then ignore the stall from a subsequent
1642 // instruction in the same packet.
1643 if (PacketStalls)
1644 return false;
1645
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001646 // Check whether the previous packet is in a different loop. If this is the
1647 // case, there is little point in trying to avoid a stall because that would
1648 // favor the rare case (loop entry) over the common case (loop iteration).
1649 //
1650 // TODO: We should really be able to check all the incoming edges if this is
1651 // the first packet in a basic block, so we can avoid stalls from the loop
1652 // backedge.
1653 if (!OldPacketMIs.empty()) {
1654 auto *OldBB = OldPacketMIs.front()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001655 auto *ThisBB = I.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001656 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1657 return false;
1658 }
1659
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001660 SUnit *SUI = MIToSUnit[const_cast<MachineInstr *>(&I)];
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001661
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001662 // Check if the latency is 0 between this instruction and any instruction
1663 // in the current packet. If so, we disregard any potential stalls due to
1664 // the instructions in the previous packet. Most of the instruction pairs
1665 // that can go together in the same packet have 0 latency between them.
1666 // Only exceptions are newValueJumps as they're generated much later and
1667 // the latencies can't be changed at that point. Another is .cur
1668 // instructions if its consumer has a 0 latency successor (such as .new).
1669 // In this case, the latency between .cur and the consumer stays non-zero
1670 // even though we can have both .cur and .new in the same packet. Changing
1671 // the latency to 0 is not an option as it causes software pipeliner to
1672 // not pipeline in some cases.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001673
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001674 // For Example:
1675 // {
1676 // I1: v6.cur = vmem(r0++#1)
1677 // I2: v7 = valign(v6,v4,r2)
1678 // I3: vmem(r5++#1) = v7.new
1679 // }
1680 // Here I2 and I3 has 0 cycle latency, but I1 and I2 has 2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001681
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001682 for (auto J : CurrentPacketMIs) {
1683 SUnit *SUJ = MIToSUnit[J];
1684 for (auto &Pred : SUI->Preds)
1685 if (Pred.getSUnit() == SUJ &&
1686 (Pred.getLatency() == 0 || HII->isNewValueJump(I) ||
1687 HII->isToBeScheduledASAP(*J, I)))
1688 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001689 }
1690
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001691 // Check if the latency is greater than one between this instruction and any
1692 // instruction in the previous packet.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001693 for (auto J : OldPacketMIs) {
1694 SUnit *SUJ = MIToSUnit[J];
1695 for (auto &Pred : SUI->Preds)
1696 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1697 return true;
1698 }
1699
1700 // Check if the latency is greater than one between this instruction and any
1701 // instruction in the previous packet.
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001702 for (auto J : OldPacketMIs) {
1703 SUnit *SUJ = MIToSUnit[J];
1704 for (auto &Pred : SUI->Preds)
1705 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1706 return true;
1707 }
1708
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001709 return false;
1710}
1711
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001712//===----------------------------------------------------------------------===//
1713// Public Constructor Functions
1714//===----------------------------------------------------------------------===//
1715
1716FunctionPass *llvm::createHexagonPacketizer() {
1717 return new HexagonPacketizer();
1718}