blob: 3a789a5f7e0b22b85ad02273795003ffeb19b665 [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//===----------------------------------------------------------------------===//
Jyotsna Verma84256432013-03-01 17:37:13 +000019#include "HexagonRegisterInfo.h"
20#include "HexagonSubtarget.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "HexagonTargetMachine.h"
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000022#include "HexagonVLIWPacketizer.h"
23#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;
Jyotsna Verma1d297502013-05-02 15:39:30 +000063 HexagonPacketizer() : MachineFunctionPass(ID) {
64 initializeHexagonPacketizerPass(*PassRegistry::getPassRegistry());
65 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000066
Craig Topper906c2cd2014-04-29 07:58:16 +000067 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000068 AU.setPreservesCFG();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000069 AU.addRequired<AAResultsWrapperPass>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000070 AU.addRequired<MachineBranchProbabilityInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000071 AU.addRequired<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000072 AU.addRequired<MachineLoopInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000073 AU.addPreserved<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000074 AU.addPreserved<MachineLoopInfo>();
75 MachineFunctionPass::getAnalysisUsage(AU);
76 }
Mehdi Amini117296c2016-10-01 02:56:57 +000077 StringRef getPassName() const override { return "Hexagon Packetizer"; }
Craig Topper906c2cd2014-04-29 07:58:16 +000078 bool runOnMachineFunction(MachineFunction &Fn) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000079 MachineFunctionProperties getRequiredProperties() const override {
80 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000081 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000082 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000083
84 private:
85 const HexagonInstrInfo *HII;
86 const HexagonRegisterInfo *HRI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000087 };
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000088
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000089 char HexagonPacketizer::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000090}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000091
Jyotsna Verma1d297502013-05-02 15:39:30 +000092INITIALIZE_PASS_BEGIN(HexagonPacketizer, "packets", "Hexagon Packetizer",
93 false, false)
94INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
95INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
96INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Chandler Carruth7b560d42015-09-09 17:55:00 +000097INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Jyotsna Verma1d297502013-05-02 15:39:30 +000098INITIALIZE_PASS_END(HexagonPacketizer, "packets", "Hexagon Packetizer",
99 false, false)
100
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000101HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
102 MachineLoopInfo &MLI, AliasAnalysis *AA,
103 const MachineBranchProbabilityInfo *MBPI)
104 : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI) {
105 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
106 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000107
108 addMutation(make_unique<HexagonSubtarget::HexagonDAGMutation>());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000109}
110
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000111// Check if FirstI modifies a register that SecondI reads.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000112static bool hasWriteToReadDep(const MachineInstr &FirstI,
113 const MachineInstr &SecondI,
114 const TargetRegisterInfo *TRI) {
115 for (auto &MO : FirstI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000116 if (!MO.isReg() || !MO.isDef())
117 continue;
118 unsigned R = MO.getReg();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000119 if (SecondI.readsRegister(R, TRI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000120 return true;
121 }
122 return false;
123}
124
125
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000126static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000127 MachineBasicBlock::iterator BundleIt, bool Before) {
128 MachineBasicBlock::instr_iterator InsertPt;
129 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000130 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000131 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000132 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000133
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000134 MachineBasicBlock &B = *MI.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000135 // The instruction should at least be bundled with the preceding instruction
136 // (there will always be one, i.e. BUNDLE, if nothing else).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000137 assert(MI.isBundledWithPred());
138 if (MI.isBundledWithSucc()) {
139 MI.clearFlag(MachineInstr::BundledSucc);
140 MI.clearFlag(MachineInstr::BundledPred);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000141 } else {
142 // If it's not bundled with the successor (i.e. it is the last one
143 // in the bundle), then we can simply unbundle it from the predecessor,
144 // which will take care of updating the predecessor's flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000145 MI.unbundleFromPred();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000146 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000147 B.splice(InsertPt, &B, MI.getIterator());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000148
149 // Get the size of the bundle without asserting.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000150 MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000151 MachineBasicBlock::const_instr_iterator E = B.instr_end();
152 unsigned Size = 0;
153 for (++I; I != E && I->isBundledWithPred(); ++I)
154 ++Size;
155
156 // If there are still two or more instructions, then there is nothing
157 // else to be done.
158 if (Size > 1)
159 return BundleIt;
160
161 // Otherwise, extract the single instruction out and delete the bundle.
162 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000163 MachineInstr &SingleI = *BundleIt->getNextNode();
164 SingleI.unbundleFromPred();
165 assert(!SingleI.isBundledWithSucc());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000166 BundleIt->eraseFromParent();
167 return NextIt;
168}
169
170
171bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000172 if (DisablePacketizer || skipFunction(*MF.getFunction()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000173 return false;
174
175 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
176 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
177 auto &MLI = getAnalysis<MachineLoopInfo>();
178 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
179 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
180
181 if (EnableGenAllInsnClass)
182 HII->genAllInsnTimingClasses(MF);
183
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000184 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000185 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000186
187 // DFA state table should not be empty.
188 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
189
190 //
191 // Loop over all basic blocks and remove KILL pseudo-instructions
192 // These instructions confuse the dependence analysis. Consider:
193 // D0 = ... (Insn 0)
194 // R0 = KILL R0, D0 (Insn 1)
195 // R0 = ... (Insn 2)
196 // Here, Insn 1 will result in the dependence graph not emitting an output
197 // dependence between Insn 0 and Insn 2. This can lead to incorrect
198 // packetization
199 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000200 for (auto &MB : MF) {
201 auto End = MB.end();
202 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000203 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000204 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000205 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000206 MB.erase(MI);
207 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000208 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000209 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000210 }
211 }
212
213 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000214 for (auto &MB : MF) {
215 auto Begin = MB.begin(), End = MB.end();
216 while (Begin != End) {
217 // First the first non-boundary starting from the end of the last
218 // scheduling region.
219 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000220 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000221 ++RB;
222 // First the first boundary starting from the beginning of the new
223 // region.
224 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000225 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000226 ++RE;
227 // Add the scheduling boundary if it's not block end.
228 if (RE != End)
229 ++RE;
230 // If RB == End, then RE == End.
231 if (RB != End)
232 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000233
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000234 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000235 }
236 }
237
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000238 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000239 return true;
240}
241
242
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000243// Reserve resources for a constant extender. Trigger an assertion if the
244// reservation fails.
245void HexagonPacketizerList::reserveResourcesForConstExt() {
246 if (!tryAllocateResourcesForConstExt(true))
247 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000248}
249
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000250bool HexagonPacketizerList::canReserveResourcesForConstExt() {
251 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000252}
253
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000254// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
255// return true, otherwise, return false.
256bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
257 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000258 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000259 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000260 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000261 MF.DeleteMachineInstr(ExtMI);
262 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000263}
264
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000265
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000266bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000267 SDep::Kind DepType, unsigned DepReg) {
268 // Check for LR dependence.
269 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000270 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000271
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000272 if (HII->isDeallocRet(MI))
273 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000274 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000275
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000276 // Check if this is a predicate dependence.
277 const TargetRegisterClass* RC = HRI->getMinimalPhysRegClass(DepReg);
278 if (RC == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000279 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000280
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000281 // Assumes that the first operand of the CALLr is the function address.
282 if (HII->isIndirectCall(MI) && (DepType == SDep::Data)) {
Krzysztof Parzyszek29a6a2e2016-08-19 21:07:35 +0000283 const MachineOperand MO = MI.getOperand(0);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000284 if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000285 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000286 }
287
Krzysztof Parzyszek29a6a2e2016-08-19 21:07:35 +0000288 if (HII->isJumpR(MI)) {
289 const MachineOperand &MO = HII->isPredicated(MI) ? MI.getOperand(1)
290 : MI.getOperand(0);
291 assert(MO.isReg() && MO.isUse());
292 if (MO.getReg() == DepReg)
293 return true;
294 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000295 return false;
296}
297
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000298static bool isRegDependence(const SDep::Kind DepType) {
299 return DepType == SDep::Data || DepType == SDep::Anti ||
300 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000301}
302
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000303static bool isDirectJump(const MachineInstr &MI) {
304 return MI.getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000305}
306
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000307static bool isSchedBarrier(const MachineInstr &MI) {
308 switch (MI.getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000309 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000310 return true;
311 }
312 return false;
313}
314
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000315static bool isControlFlow(const MachineInstr &MI) {
316 return MI.getDesc().isTerminator() || MI.getDesc().isCall();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000317}
318
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000319
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000320/// Returns true if the instruction modifies a callee-saved register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000321static bool doesModifyCalleeSavedReg(const MachineInstr &MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000322 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000323 const MachineFunction &MF = *MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000324 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000325 if (MI.modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000326 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000327 return false;
328}
329
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000330// Returns true if an instruction can be promoted to .new predicate or
331// new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000332bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI,
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000333 const TargetRegisterClass *NewRC) {
334 // Vector stores can be predicated, and can be new-value stores, but
335 // they cannot be predicated on a .new predicate value.
336 if (NewRC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000337 if (HII->isV60VectorInstruction(MI) && MI.mayStore())
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000338 return false;
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +0000339 return HII->isCondInst(MI) || HII->isJumpR(MI) || MI.isReturn() ||
340 HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000341}
342
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000343// Promote an instructiont to its .cur form.
344// At this time, we have already made a call to canPromoteToDotCur and made
345// sure that it can *indeed* be promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000346bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000347 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
348 const TargetRegisterClass* RC) {
349 assert(DepType == SDep::Data);
350 int CurOpcode = HII->getDotCurOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000351 MI.setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000352 return true;
353}
354
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000355void HexagonPacketizerList::cleanUpDotCur() {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000356 MachineInstr *MI = nullptr;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000357 for (auto BI : CurrentPacketMIs) {
358 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
359 if (BI->getOpcode() == Hexagon::V6_vL32b_cur_ai) {
360 MI = BI;
361 continue;
362 }
363 if (MI) {
364 for (auto &MO : BI->operands())
365 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
366 return;
367 }
368 }
369 if (!MI)
370 return;
371 // We did not find a use of the CUR, so de-cur it.
372 MI->setDesc(HII->get(Hexagon::V6_vL32b_ai));
373 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
374}
375
376// Check to see if an instruction can be dot cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000377bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000378 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
379 const TargetRegisterClass *RC) {
380 if (!HII->isV60VectorInstruction(MI))
381 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000382 if (!HII->isV60VectorInstruction(*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000383 return false;
384
385 // Already a dot new instruction.
386 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
387 return false;
388
389 if (!HII->mayBeCurLoad(MI))
390 return false;
391
392 // The "cur value" cannot come from inline asm.
393 if (PacketSU->getInstr()->isInlineAsm())
394 return false;
395
396 // Make sure candidate instruction uses cur.
397 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000398 MI.dump();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000399 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000400 MachineInstr &MJ = *MII;
401 DEBUG({
402 dbgs() << "Checking CUR against ";
403 MJ.dump();
404 });
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000405 unsigned DestReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000406 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000407 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000408 if (MO.isReg() && MO.getReg() == DestReg)
409 FoundMatch = true;
410 if (!FoundMatch)
411 return false;
412
413 // Check for existing uses of a vector register within the packet which
414 // would be affected by converting a vector load into .cur formt.
415 for (auto BI : CurrentPacketMIs) {
416 DEBUG(dbgs() << "packet has "; BI->dump(););
417 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
418 return false;
419 }
420
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000421 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000422 // We can convert the opcode into a .cur.
423 return true;
424}
425
426// Promote an instruction to its .new form. At this time, we have already
427// made a call to canPromoteToDotNew and made sure that it can *indeed* be
428// promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000429bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000430 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
431 const TargetRegisterClass* RC) {
432 assert (DepType == SDep::Data);
433 int NewOpcode;
434 if (RC == &Hexagon::PredRegsRegClass)
435 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
436 else
437 NewOpcode = HII->getDotNewOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000438 MI.setDesc(HII->get(NewOpcode));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000439 return true;
440}
441
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000442bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +0000443 int NewOpcode = HII->getDotOldOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000444 MI.setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000445 return true;
446}
447
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000448bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) {
449 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000450 switch (Opc) {
451 case Hexagon::S2_storerd_io:
452 case Hexagon::S2_storeri_io:
453 case Hexagon::S2_storerh_io:
454 case Hexagon::S2_storerb_io:
455 break;
456 default:
457 llvm_unreachable("Unexpected instruction");
458 }
Matthias Braun941a7052016-07-28 18:40:00 +0000459 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000460 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000461 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
462 if (HII->isValidOffset(Opc, NewOff)) {
463 Off.setImm(NewOff);
464 return true;
465 }
466 return false;
467}
468
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000469void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) {
470 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000471 switch (Opc) {
472 case Hexagon::S2_storerd_io:
473 case Hexagon::S2_storeri_io:
474 case Hexagon::S2_storerh_io:
475 case Hexagon::S2_storerb_io:
476 break;
477 default:
478 llvm_unreachable("Unexpected instruction");
479 }
Matthias Braun941a7052016-07-28 18:40:00 +0000480 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000481 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000482 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
483}
484
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000485enum PredicateKind {
486 PK_False,
487 PK_True,
488 PK_Unknown
489};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000490
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000491/// Returns true if an instruction is predicated on p0 and false if it's
492/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000493static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000494 const HexagonInstrInfo *HII) {
495 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000496 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000497 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000498 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000499 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000500}
501
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000502static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000503 const HexagonInstrInfo *HII) {
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000504 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000505#ifndef NDEBUG
506 // Post Increment means duplicates. Use dense map to find duplicates in the
507 // list. Caution: Densemap initializes with the minimum of 64 buckets,
508 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000509 DenseSet<unsigned> DefRegsSet;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000510 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000511 if (MO.isReg() && MO.isDef())
512 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000513
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000514 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000515 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
516 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000517#else
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000518 if (MI.mayLoad()) {
519 const MachineOperand &Op1 = MI.getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000520 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000521 assert(Op1.isReg() && "Post increment operand has be to a register.");
522 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000523 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000524 if (MI.getDesc().mayStore()) {
525 const MachineOperand &Op0 = MI.getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000526 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000527 assert(Op0.isReg() && "Post increment operand has be to a register.");
528 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000529 }
530#endif
531 // we should never come here.
532 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
533}
534
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000535// Get the value being stored.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000536static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000537 // value being stored is always the last operand.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000538 return MI.getOperand(MI.getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000539}
540
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000541static bool isLoadAbsSet(const MachineInstr &MI) {
542 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000543 switch (Opc) {
544 case Hexagon::L4_loadrd_ap:
545 case Hexagon::L4_loadrb_ap:
546 case Hexagon::L4_loadrh_ap:
547 case Hexagon::L4_loadrub_ap:
548 case Hexagon::L4_loadruh_ap:
549 case Hexagon::L4_loadri_ap:
550 return true;
551 }
552 return false;
553}
554
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000555static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000556 assert(isLoadAbsSet(MI));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000557 return MI.getOperand(1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000558}
559
560
561// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000562// Following restrictions are to be respected in convert a store into
563// a new value store.
564// 1. If an instruction uses auto-increment, its address register cannot
565// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000566// 2. If an instruction uses absolute-set addressing mode, its address
567// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000568// 3. If an instruction produces a 64-bit result, its registers cannot be used
569// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000570// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000571// the instruction that uses the new-value register must also be conditional,
572// and both must always have their predicates evaluate identically.
573// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000574// 5. There is an implied restriction that a packet cannot have another store,
575// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000576// already a store in a packet, there can not be a new value store.
577// Arch Spec: 3.4.4.2
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000578bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
579 const MachineInstr &PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000580 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000581 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000582 return false;
583
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000584 // Make sure there is dependency and can be new value'd.
585 const MachineOperand &Val = getStoreValueOperand(MI);
586 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000587 return false;
588
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000589 const MCInstrDesc& MCID = PacketMI.getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000590
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000591 // First operand is always the result.
592 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
593 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
594 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000595 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000596
597 // New-value stores are of class NV (slot 0), dual stores require class ST
598 // in slot 0 (PRM 5.5).
599 for (auto I : CurrentPacketMIs) {
600 SUnit *PacketSU = MIToSUnit.find(I)->second;
601 if (PacketSU->getInstr()->mayStore())
602 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000603 }
604
605 // Make sure it's NOT the post increment register that we are going to
606 // new value.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000607 if (HII->isPostIncrement(MI) &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000608 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000609 return false;
610 }
611
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000612 if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000613 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
614 // If source is post_inc, or absolute-set addressing, it can not feed
615 // into new value store
616 // r3 = memw(r2++#4)
617 // memw(r30 + #-1404) = r2.new -> can not be new value store
618 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000619 return false;
620 }
621
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000622 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
623 return false;
624
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000625 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000626 // also be predicated.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000627 if (HII->isPredicated(PacketMI)) {
628 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000629 return false;
630
631 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000632 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000633 unsigned predRegNumSrc = 0;
634 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000635 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000636
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000637 // Get predicate register used in the source instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000638 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000639 if (!MO.isReg())
640 continue;
641 predRegNumSrc = MO.getReg();
642 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
643 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000644 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000645 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000646 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
647 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000648
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000649 // Get predicate register used in new-value store instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000650 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000651 if (!MO.isReg())
652 continue;
653 predRegNumDst = MO.getReg();
654 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
655 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000656 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000657 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000658 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
659 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000660
661 // New-value register producer and user (store) need to satisfy these
662 // constraints:
663 // 1) Both instructions should be predicated on the same register.
664 // 2) If producer of the new-value register is .new predicated then store
665 // should also be .new predicated and if producer is not .new predicated
666 // then store should not be .new predicated.
667 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000668 // sense, i.e, either both should be negated or both should be non-negated.
669 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000670 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000671 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000672 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000673 }
674
675 // Make sure that other than the new-value register no other store instruction
676 // register has been modified in the same packet. Predicate registers can be
677 // modified by they should not be modified between the producer and the store
678 // instruction as it will make them both conditional on different values.
679 // We already know this to be true for all the instructions before and
680 // including PacketMI. Howerver, we need to perform the check for the
681 // remaining instructions in the packet.
682
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000683 unsigned StartCheck = 0;
684
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000685 for (auto I : CurrentPacketMIs) {
686 SUnit *TempSU = MIToSUnit.find(I)->second;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000687 MachineInstr &TempMI = *TempSU->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000688
689 // Following condition is true for all the instructions until PacketMI is
690 // reached (StartCheck is set to 0 before the for loop).
691 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000692 if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
693 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000694
695 StartCheck = 1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000696 if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000697 continue;
698
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000699 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000700 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000701 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000702 }
703
Alp Tokerf907b892013-12-05 05:44:44 +0000704 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000705 // 1. The only use of reg is DepReg and no other registers.
706 // This handles V4 base+index registers.
707 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000708 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000709 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000710 if (!HII->isPostIncrement(MI)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000711 for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
712 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000713 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000714 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000715 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000716 }
717
718 // If data definition is because of implicit definition of the register,
719 // do not newify the store. Eg.
720 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
721 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000722 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000723 if (MO.isRegMask() && MO.clobbersPhysReg(DepReg))
724 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000725 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
726 continue;
727 unsigned R = MO.getReg();
728 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
729 return false;
730 }
731
732 // Handle imp-use of super reg case. There is a target independent side
733 // change that should prevent this situation but I am handling it for
734 // just-in-case. For example, we cannot newify R2 in the following case:
735 // %R3<def> = A2_tfrsi 0;
736 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000737 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000738 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
739 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000740 }
741
742 // Can be dot new store.
743 return true;
744}
745
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000746// Can this MI to promoted to either new value store or new value jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000747bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000748 const SUnit *PacketSU, unsigned DepReg,
749 MachineBasicBlock::iterator &MII) {
750 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000751 return false;
752
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000753 // Check to see the store can be new value'ed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000754 MachineInstr &PacketMI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000755 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000756 return true;
757
758 // Check to see the compare/jump can be new value'ed.
759 // This is done as a pass on its own. Don't need to check it here.
760 return false;
761}
762
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000763static bool isImplicitDependency(const MachineInstr &I, unsigned DepReg) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000764 for (auto &MO : I.operands()) {
765 if (MO.isRegMask() && MO.clobbersPhysReg(DepReg))
766 return true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000767 if (MO.isReg() && MO.isDef() && (MO.getReg() == DepReg) && MO.isImplicit())
768 return true;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000769 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000770 return false;
771}
772
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000773// Check to see if an instruction can be dot new
774// There are three kinds.
775// 1. dot new on predicate - V2/V3/V4
776// 2. dot new on stores NV/ST - V4
777// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000778bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000779 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
780 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000781 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000782 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000783 return false;
784
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000785 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000786 return false;
787
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000788 const MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000789
790 // The "new value" cannot come from inline asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000791 if (PI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000792 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000793
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000794 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
795 // sense.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000796 if (PI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000797 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000798
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000799 // If dependency is trough an implicitly defined register, we should not
800 // newify the use.
801 if (isImplicitDependency(PI, DepReg))
802 return false;
803
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000804 const MCInstrDesc& MCID = PI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000805 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
806 if (DisableVecDblNVStores && VecRC == &Hexagon::VecDblRegsRegClass)
807 return false;
808
809 // predicate .new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000810 if (RC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +0000811 if (HII->isCondInst(MI) || HII->isJumpR(MI) || MI.isReturn())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000812 return HII->predCanBeUsedAsDotNew(PI, DepReg);
813
814 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
815 return false;
816
817 // Create a dot new machine instruction to see if resources can be
818 // allocated. If not, bail out now.
819 int NewOpcode = HII->getDotNewOp(MI);
820 const MCInstrDesc &D = HII->get(NewOpcode);
821 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000822 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000823 MF.DeleteMachineInstr(NewMI);
824 if (!ResourcesAvailable)
825 return false;
826
827 // New Value Store only. New Value Jump generated as a separate pass.
828 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
829 return false;
830
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000831 return true;
832}
833
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000834// Go through the packet instructions and search for an anti dependency between
835// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000836// Trying to add
837// a) %R1<def> = TFRI_cdNotPt %P3, 2
838// to this packet:
839// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000840// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
841// c) %P3<def> = C2_tfrrp %R23
842// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000843// }
844// The P3 from a) and d) will be complements after
845// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000846// Anti-dep between c) and b) is irrelevant for this case
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000847bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000848 unsigned DepReg) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000849 SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000850
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000851 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000852 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000853 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000854 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000855
856 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000857 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000858
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000859 // Look at dependencies between current members of the packet and
860 // predicate defining instruction MI. Make sure that dependency is
861 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000862 if (PacketSU->isSucc(PacketSUDep)) {
863 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000864 auto &Dep = PacketSU->Succs[i];
865 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
866 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000867 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000868 }
869 }
870 }
871
872 return false;
873}
874
875
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000876/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000877static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000878 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000879 /// We use the following rule: The first predicate register that is a use is
880 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000881 assert(QII->isPredicated(MI) && "Must be predicated instruction");
882
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000883 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000884 if (Op.isReg() && Op.getReg() && Op.isUse() &&
885 Hexagon::PredRegsRegClass.contains(Op.getReg()))
886 return Op.getReg();
887 }
888
889 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000890 return 0;
891}
892
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000893// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000894// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000895bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
896 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000897 // If we don't know the predicate sense of the instructions bail out early, we
898 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000899 if (getPredicateSense(MI1, HII) == PK_Unknown ||
900 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000901 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000902
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000903 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000904 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000905
906 // One corner case deals with the following scenario:
907 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000908 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000909 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000910 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000911 // b) %R25<def> = A2_tfrf %P0, %R24
912 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000913 // }
914 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000915 // On general check a) and b) are complements, but presence of c) will
916 // convert a) to .new form, and then it is not a complement.
917 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000918
919 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000920 // Look for Anti dependecy on the same predicate reg as used in the
921 // candidate.
922 for (auto I : CurrentPacketMIs) {
923 // Scheduling Unit for current insn in the packet.
924 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000925
926 // If this instruction in the packet is succeeded by the candidate...
927 if (PacketSU->isSucc(SU)) {
928 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000929 auto Dep = PacketSU->Succs[i];
930 // The corner case exist when there is true data dependency between
931 // candidate and one of current packet members, this dep is on
932 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000933 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000934 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
935 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
936 // Here I know that I is predicate setting instruction with true
937 // data dep to candidate on the register we care about - c) in the
938 // above example. Now I need to see if there is an anti dependency
939 // from c) to any other instruction in the same packet on the pred
940 // reg of interest.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000941 if (restrictingDepExistInPacket(*I, Dep.getReg()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000942 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000943 }
944 }
945 }
946 }
947
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000948 // If the above case does not apply, check regular complement condition.
949 // Check that the predicate register is the same and that the predicate
950 // sense is different We also need to differentiate .old vs. .new: !p0
951 // is not complementary to p0.new.
952 unsigned PReg1 = getPredicatedRegister(MI1, HII);
953 unsigned PReg2 = getPredicatedRegister(MI2, HII);
954 return PReg1 == PReg2 &&
955 Hexagon::PredRegsRegClass.contains(PReg1) &&
956 Hexagon::PredRegsRegClass.contains(PReg2) &&
957 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000958 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000959}
960
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000961// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000962void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000963 Dependence = false;
964 PromotedToDotNew = false;
965 GlueToNewValueJump = false;
966 GlueAllocframeStore = false;
967 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000968}
969
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000970// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000971bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
972 const MachineBasicBlock *) {
973 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000974 return true;
975
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000976 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000977 return false;
978
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000979 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000980 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000981 return false;
982
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000983 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000984 return false;
985
986 // We check if MI has any functional units mapped to it. If it doesn't,
987 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000988 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000989 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +0000990 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000991 return !FuncUnits;
992}
993
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000994bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
995 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000996 return true;
997
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000998 // Consider inline asm to not be a solo instruction by default.
999 // Inline asm will be put in a packet temporarily, but then it will be
1000 // removed, and placed outside of the packet (before or after, depending
1001 // on dependencies). This is to reduce the impact of inline asm as a
1002 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001003 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001004 return true;
1005
1006 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
1007 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
1008 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001009 if (isSchedBarrier(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001010 return true;
1011
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001012 if (HII->isSolo(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001013 return true;
1014
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001015 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001016 return true;
1017
1018 return false;
1019}
1020
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001021
1022// Quick check if instructions MI and MJ cannot coexist in the same packet.
1023// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1024// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1025// For full test call this function twice:
1026// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1027// Doing the test only one way saves the amount of code in this function,
1028// since every test would need to be repeated with the MI and MJ reversed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001029static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001030 const HexagonInstrInfo &HII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001031 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001032 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1033 HII.isHVXMemWithAIndirect(MI, MJ))
1034 return true;
1035
1036 // An inline asm cannot be together with a branch, because we may not be
1037 // able to remove the asm out after packetizing (i.e. if the asm must be
1038 // moved past the bundle). Similarly, two asms cannot be together to avoid
1039 // complications when determining their relative order outside of a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001040 if (MI.isInlineAsm())
1041 return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
1042 MJ.isCall() || MJ.isTerminator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001043
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001044 switch (MI.getOpcode()) {
1045 case (Hexagon::S2_storew_locked):
1046 case (Hexagon::S4_stored_locked):
1047 case (Hexagon::L2_loadw_locked):
1048 case (Hexagon::L4_loadd_locked):
1049 case (Hexagon::Y4_l2fetch): {
1050 // These instructions can only be grouped with ALU32 or non-floating-point
1051 // XTYPE instructions. Since there is no convenient way of identifying fp
1052 // XTYPE instructions, only allow grouping with ALU32 for now.
1053 unsigned TJ = HII.getType(MJ);
Krzysztof Parzyszek5ea971c2017-02-07 17:47:37 +00001054 if (TJ != HexagonII::TypeALU32_2op &&
1055 TJ != HexagonII::TypeALU32_3op &&
1056 TJ != HexagonII::TypeALU32_ADDI)
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001057 return true;
1058 break;
1059 }
1060 default:
1061 break;
1062 }
1063
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001064 // "False" really means that the quick check failed to determine if
1065 // I and J cannot coexist.
1066 return false;
1067}
1068
1069
1070// Full, symmetric check.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001071bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
1072 const MachineInstr &MJ) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001073 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1074}
1075
1076void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1077 for (auto &B : MF) {
1078 MachineBasicBlock::iterator BundleIt;
1079 MachineBasicBlock::instr_iterator NextI;
1080 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1081 NextI = std::next(I);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001082 MachineInstr &MI = *I;
1083 if (MI.isBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001084 BundleIt = I;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001085 if (!MI.isInsideBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001086 continue;
1087
1088 // Decide on where to insert the instruction that we are pulling out.
1089 // Debug instructions always go before the bundle, but the placement of
1090 // INLINE_ASM depends on potential dependencies. By default, try to
1091 // put it before the bundle, but if the asm writes to a register that
1092 // other instructions in the bundle read, then we need to place it
1093 // after the bundle (to preserve the bundle semantics).
1094 bool InsertBeforeBundle;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001095 if (MI.isInlineAsm())
1096 InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
1097 else if (MI.isDebugValue())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001098 InsertBeforeBundle = true;
1099 else
1100 continue;
1101
1102 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1103 }
1104 }
1105}
1106
1107// Check if a given instruction is of class "system".
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001108static bool isSystemInstr(const MachineInstr &MI) {
1109 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001110 switch (Opc) {
1111 case Hexagon::Y2_barrier:
1112 case Hexagon::Y2_dcfetchbo:
1113 return true;
1114 }
1115 return false;
1116}
1117
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001118bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
1119 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001120 // The dependence graph may not include edges between dead definitions,
1121 // so without extra checks, we could end up packetizing two instruction
1122 // defining the same (dead) register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001123 if (I.isCall() || J.isCall())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001124 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001125 if (HII->isPredicated(I) || HII->isPredicated(J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001126 return false;
1127
1128 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001129 for (auto &MO : I.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001130 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1131 continue;
1132 DeadDefs[MO.getReg()] = true;
1133 }
1134
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001135 for (auto &MO : J.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001136 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1137 continue;
1138 unsigned R = MO.getReg();
1139 if (R != Hexagon::USR_OVF && DeadDefs[R])
1140 return true;
1141 }
1142 return false;
1143}
1144
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001145bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
1146 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001147 // A save callee-save register function call can only be in a packet
1148 // with instructions that don't write to the callee-save registers.
1149 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1150 doesModifyCalleeSavedReg(J, HRI)) ||
1151 (HII->isSaveCalleeSavedRegsCall(J) &&
1152 doesModifyCalleeSavedReg(I, HRI)))
1153 return true;
1154
1155 // Two control flow instructions cannot go in the same packet.
1156 if (isControlFlow(I) && isControlFlow(J))
1157 return true;
1158
1159 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1160 // contain a speculative indirect jump,
1161 // a new-value compare jump or a dealloc_return.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001162 auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
1163 if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001164 return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001165 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001166 return true;
1167 return false;
1168 };
1169
1170 if (HII->isLoopN(I) && isBadForLoopN(J))
1171 return true;
1172 if (HII->isLoopN(J) && isBadForLoopN(I))
1173 return true;
1174
1175 // dealloc_return cannot appear in the same packet as a conditional or
1176 // unconditional jump.
1177 return HII->isDeallocRet(I) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001178 (J.isBranch() || J.isCall() || J.isBarrier());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001179}
1180
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001181bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I,
1182 const MachineInstr &J) {
1183 // Adding I to a packet that has J.
1184
1185 // Regmasks are not reflected in the scheduling dependency graph, so
1186 // we need to check them manually. This code assumes that regmasks only
1187 // occur on calls, and the problematic case is when we add an instruction
1188 // defining a register R to a packet that has a call that clobbers R via
1189 // a regmask. Those cannot be packetized together, because the call will
1190 // be executed last. That's also a reson why it is ok to add a call
1191 // clobbering R to a packet that defines R.
1192
1193 // Look for regmasks in J.
1194 for (const MachineOperand &OpJ : J.operands()) {
1195 if (!OpJ.isRegMask())
1196 continue;
1197 assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call");
1198 for (const MachineOperand &OpI : I.operands()) {
1199 if (OpI.isReg()) {
1200 if (OpJ.clobbersPhysReg(OpI.getReg()))
1201 return true;
1202 } else if (OpI.isRegMask()) {
1203 // Both are regmasks. Assume that they intersect.
1204 return true;
1205 }
1206 }
1207 }
1208 return false;
1209}
1210
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001211bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I,
1212 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001213 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001214 bool StoreI = I.mayStore(), StoreJ = J.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001215 if ((SysI && StoreJ) || (SysJ && StoreI))
1216 return true;
1217
1218 if (StoreI && StoreJ) {
1219 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1220 return true;
1221 } else {
1222 // A memop cannot be in the same packet with another memop or a store.
1223 // Two stores can be together, but here I and J cannot both be stores.
1224 bool MopStI = HII->isMemOp(I) || StoreI;
1225 bool MopStJ = HII->isMemOp(J) || StoreJ;
1226 if (MopStI && MopStJ)
1227 return true;
1228 }
1229
1230 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1231}
1232
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001233// SUI is the current instruction that is out side of the current packet.
1234// SUJ is the current instruction inside the current packet against which that
1235// SUI will be packetized.
1236bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001237 assert(SUI->getInstr() && SUJ->getInstr());
1238 MachineInstr &I = *SUI->getInstr();
1239 MachineInstr &J = *SUJ->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001240
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001241 // Clear IgnoreDepMIs when Packet starts.
1242 if (CurrentPacketMIs.size() == 1)
1243 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001244
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001245 MachineBasicBlock::iterator II = I.getIterator();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001246
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001247 // Solo instructions cannot go in the packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001248 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001249
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001250 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001251 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001252
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001253 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1254 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001255 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001256
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001257 // Regmasks are not accounted for in the scheduling graph, so we need
1258 // to explicitly check for dependencies caused by them. They should only
1259 // appear on calls, so it's not too pessimistic to reject all regmask
1260 // dependencies.
1261 Dependence = hasRegMaskDependence(I, J);
1262 if (Dependence)
1263 return false;
1264
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001265 // V4 allows dual stores. It does not allow second store, if the first
1266 // store is not in SLOT0. New value store, new value jump, dealloc_return
1267 // and memop always take SLOT0. Arch spec 3.4.4.2.
1268 Dependence = hasV4SpecificDependence(I, J);
1269 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001270 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001271
1272 // If an instruction feeds new value jump, glue it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001273 MachineBasicBlock::iterator NextMII = I.getIterator();
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001274 ++NextMII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001275 if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001276 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001277
1278 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001279 const MachineOperand &NOp0 = NextMI.getOperand(0);
1280 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001281
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001282 if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001283 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001284
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001285 for (auto T : CurrentPacketMIs) {
1286 SUnit *PacketSU = MIToSUnit.find(T)->second;
1287 MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001288 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001289 if (PI.isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001290 Dependence = true;
1291 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001292 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001293 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001294 // 1. Packet does not have a store in it.
1295 // 2. If the first operand of the nvj is newified, and the second
1296 // operand is also a reg, it (second reg) is not defined in
1297 // the same packet.
1298 // 3. If the second operand of the nvj is newified, (which means
1299 // first operand is also a reg), first reg is not defined in
1300 // the same packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001301 if (PI.getOpcode() == Hexagon::S2_allocframe || PI.mayStore() ||
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001302 HII->isLoopN(PI)) {
1303 Dependence = true;
1304 break;
1305 }
1306 // Check #2/#3.
1307 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001308 if (OpR.isReg() && PI.modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001309 Dependence = true;
1310 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001311 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001312 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001313
1314 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001315 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001316 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001317 }
1318
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001319 // There no dependency between a prolog instruction and its successor.
1320 if (!SUJ->isSucc(SUI))
1321 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001322
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001323 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1324 if (FoundSequentialDependence)
1325 break;
1326
1327 if (SUJ->Succs[i].getSUnit() != SUI)
1328 continue;
1329
1330 SDep::Kind DepType = SUJ->Succs[i].getKind();
1331 // For direct calls:
1332 // Ignore register dependences for call instructions for packetization
1333 // purposes except for those due to r31 and predicate registers.
1334 //
1335 // For indirect calls:
1336 // Same as direct calls + check for true dependences to the register
1337 // used in the indirect call.
1338 //
1339 // We completely ignore Order dependences for call instructions.
1340 //
1341 // For returns:
1342 // Ignore register dependences for return instructions like jumpr,
1343 // dealloc return unless we have dependencies on the explicit uses
1344 // of the registers used by jumpr (like r31) or dealloc return
1345 // (like r29 or r30).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001346 unsigned DepReg = 0;
1347 const TargetRegisterClass *RC = nullptr;
1348 if (DepType == SDep::Data) {
1349 DepReg = SUJ->Succs[i].getReg();
1350 RC = HRI->getMinimalPhysRegClass(DepReg);
1351 }
1352
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +00001353 if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001354 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001355 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001356 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1357 continue;
1358 }
1359
1360 if (DepType == SDep::Data) {
1361 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1362 if (promoteToDotCur(J, DepType, II, RC))
1363 continue;
1364 }
1365
1366 // Data dpendence ok if we have load.cur.
1367 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
1368 if (HII->isV60VectorInstruction(I))
1369 continue;
1370 }
1371
1372 // For instructions that can be promoted to dot-new, try to promote.
1373 if (DepType == SDep::Data) {
1374 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1375 if (promoteToDotNew(I, DepType, II, RC)) {
1376 PromotedToDotNew = true;
1377 continue;
1378 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001379 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001380 if (HII->isNewValueJump(I))
1381 continue;
1382 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001383
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001384 // For predicated instructions, if the predicates are complements then
1385 // there can be no dependence.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001386 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1387 arePredicatesComplements(I, J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001388 // Not always safe to do this translation.
1389 // DAG Builder attempts to reduce dependence edges using transitive
1390 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001391 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001392 // r0 = tfr_pt ... (1)
1393 // r0 = tfr_pf ... (2)
1394 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001395 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001396 // There will be an output dependence between (1)->(2) and (2)->(3).
1397 // However, there is no dependence edge between (1)->(3). This results
1398 // in all 3 instructions going in the same packet. We ignore dependce
1399 // only once to avoid this situation.
David Majnemer0d955d02016-08-11 22:21:41 +00001400 auto Itr = find(IgnoreDepMIs, &J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001401 if (Itr != IgnoreDepMIs.end()) {
1402 Dependence = true;
1403 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001404 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001405 IgnoreDepMIs.push_back(&I);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001406 continue;
1407 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001408
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001409 // Ignore Order dependences between unconditional direct branches
1410 // and non-control-flow instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001411 if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001412 DepType == SDep::Order)
1413 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001414
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001415 // Ignore all dependences for jumps except for true and output
1416 // dependences.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001417 if (I.isConditionalBranch() && DepType != SDep::Data &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001418 DepType != SDep::Output)
1419 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001420
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001421 // Ignore output dependences due to superregs. We can write to two
1422 // different subregisters of R1:0 for instance in the same cycle.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001423
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001424 // If neither I nor J defines DepReg, then this is a superfluous output
1425 // dependence. The dependence must be of the form:
1426 // R0 = ...
1427 // R1 = ...
1428 // and there is an output dependence between the two instructions with
1429 // DepReg = D0.
1430 // We want to ignore these dependences. Ideally, the dependence
1431 // constructor should annotate such dependences. We can then avoid this
1432 // relatively expensive check.
1433 //
1434 if (DepType == SDep::Output) {
1435 // DepReg is the register that's responsible for the dependence.
1436 unsigned DepReg = SUJ->Succs[i].getReg();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001437
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001438 // Check if I and J really defines DepReg.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001439 if (!I.definesRegister(DepReg) && !J.definesRegister(DepReg))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001440 continue;
1441 FoundSequentialDependence = true;
1442 break;
1443 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001444
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001445 // For Order dependences:
1446 // 1. On V4 or later, volatile loads/stores can be packetized together,
1447 // unless other rules prevent is.
1448 // 2. Store followed by a load is not allowed.
1449 // 3. Store followed by a store is only valid on V4 or later.
1450 // 4. Load followed by any memory operation is allowed.
1451 if (DepType == SDep::Order) {
1452 if (!PacketizeVolatiles) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001453 bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001454 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001455 FoundSequentialDependence = true;
1456 break;
1457 }
1458 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001459 // J is first, I is second.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001460 bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
1461 bool LoadI = I.mayLoad(), StoreI = I.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001462 if (StoreJ) {
1463 // Two stores are only allowed on V4+. Load following store is never
1464 // allowed.
1465 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001466 FoundSequentialDependence = true;
1467 break;
1468 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001469 } else if (!LoadJ || (!LoadI && !StoreI)) {
1470 // If J is neither load nor store, assume a dependency.
1471 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001472 FoundSequentialDependence = true;
1473 break;
1474 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001475 // Store followed by store: not OK on V2.
1476 // Store followed by load: not OK on all.
1477 // Load followed by store: OK on all.
1478 // Load followed by load: OK on all.
1479 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001480 }
1481
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001482 // For V4, special case ALLOCFRAME. Even though there is dependency
1483 // between ALLOCFRAME and subsequent store, allow it to be packetized
1484 // in a same packet. This implies that the store is using the caller's
1485 // SP. Hence, offset needs to be updated accordingly.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001486 if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
1487 unsigned Opc = I.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001488 switch (Opc) {
1489 case Hexagon::S2_storerd_io:
1490 case Hexagon::S2_storeri_io:
1491 case Hexagon::S2_storerh_io:
1492 case Hexagon::S2_storerb_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001493 if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001494 // Since this store is to be glued with allocframe in the same
1495 // packet, it will use SP of the previous stack frame, i.e.
1496 // caller's SP. Therefore, we need to recalculate offset
1497 // according to this change.
1498 GlueAllocframeStore = useCallersSP(I);
1499 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001500 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001501 }
1502 default:
1503 break;
1504 }
1505 }
1506
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001507 // There are certain anti-dependencies that cannot be ignored.
1508 // Specifically:
1509 // J2_call ... %R0<imp-def> ; SUJ
1510 // R0 = ... ; SUI
1511 // Those cannot be packetized together, since the call will observe
1512 // the effect of the assignment to R0.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001513 if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) {
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001514 // Check if I defines any volatile register. We should also check
1515 // registers that the call may read, but these happen to be a
1516 // subset of the volatile register set.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001517 for (const MachineOperand &Op : I.operands()) {
1518 if (Op.isReg() && Op.isDef()) {
1519 unsigned R = Op.getReg();
1520 if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI))
1521 continue;
1522 } else if (!Op.isRegMask()) {
1523 // If I has a regmask assume dependency.
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001524 continue;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001525 }
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001526 FoundSequentialDependence = true;
1527 break;
1528 }
1529 }
1530
1531 // Skip over remaining anti-dependences. Two instructions that are
1532 // anti-dependent can share a packet, since in most such cases all
1533 // operands are read before any modifications take place.
1534 // The exceptions are branch and call instructions, since they are
1535 // executed after all other instructions have completed (at least
1536 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001537 if (DepType != SDep::Anti) {
1538 FoundSequentialDependence = true;
1539 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001540 }
1541 }
1542
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001543 if (FoundSequentialDependence) {
1544 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001545 return false;
1546 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001547
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001548 return true;
1549}
1550
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001551bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001552 assert(SUI->getInstr() && SUJ->getInstr());
1553 MachineInstr &I = *SUI->getInstr();
1554 MachineInstr &J = *SUJ->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001555
1556 if (cannotCoexist(I, J))
1557 return false;
1558
1559 if (!Dependence)
1560 return true;
1561
1562 // Check if the instruction was promoted to a dot-new. If so, demote it
1563 // back into a dot-old.
1564 if (PromotedToDotNew)
1565 demoteToDotOld(I);
1566
1567 cleanUpDotCur();
1568 // Check if the instruction (must be a store) was glued with an allocframe
1569 // instruction. If so, restore its offset to its original value, i.e. use
1570 // current SP instead of caller's SP.
1571 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001572 useCalleesSP(I);
1573 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001574 }
1575 return false;
1576}
1577
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001578MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001579HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001580 MachineBasicBlock::iterator MII = MI.getIterator();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001581 MachineBasicBlock *MBB = MI.getParent();
1582 if (MI.isImplicitDef()) {
1583 unsigned R = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001584 if (Hexagon::IntRegsRegClass.contains(R)) {
1585 MCSuperRegIterator S(R, HRI, false);
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001586 MI.addOperand(MachineOperand::CreateReg(*S, true, true));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001587 }
1588 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001589 }
1590 assert(ResourceTracker->canReserveResources(MI));
1591
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001592 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001593 bool Good = true;
1594
1595 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001596 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001597 // We need to put both instructions in the same packet: MI and NvjMI.
1598 // Either of them can require a constant extender. Try to add both to
1599 // the current packet, and if that fails, end the packet and start a
1600 // new one.
1601 ResourceTracker->reserveResources(MI);
1602 if (ExtMI)
1603 Good = tryAllocateResourcesForConstExt(true);
1604
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001605 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001606 if (Good) {
1607 if (ResourceTracker->canReserveResources(NvjMI))
1608 ResourceTracker->reserveResources(NvjMI);
1609 else
1610 Good = false;
1611 }
1612 if (Good && ExtNvjMI)
1613 Good = tryAllocateResourcesForConstExt(true);
1614
1615 if (!Good) {
1616 endPacket(MBB, MI);
1617 assert(ResourceTracker->canReserveResources(MI));
1618 ResourceTracker->reserveResources(MI);
1619 if (ExtMI) {
1620 assert(canReserveResourcesForConstExt());
1621 tryAllocateResourcesForConstExt(true);
1622 }
1623 assert(ResourceTracker->canReserveResources(NvjMI));
1624 ResourceTracker->reserveResources(NvjMI);
1625 if (ExtNvjMI) {
1626 assert(canReserveResourcesForConstExt());
1627 reserveResourcesForConstExt();
1628 }
1629 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001630 CurrentPacketMIs.push_back(&MI);
1631 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001632 return MII;
1633 }
1634
1635 ResourceTracker->reserveResources(MI);
1636 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1637 endPacket(MBB, MI);
1638 if (PromotedToDotNew)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001639 demoteToDotOld(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001640 if (GlueAllocframeStore) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001641 useCalleesSP(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001642 GlueAllocframeStore = false;
1643 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001644 ResourceTracker->reserveResources(MI);
1645 reserveResourcesForConstExt();
1646 }
1647
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001648 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001649 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001650}
1651
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001652void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001653 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001654 OldPacketMIs = CurrentPacketMIs;
1655 VLIWPacketizerList::endPacket(MBB, MI);
1656}
1657
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001658bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001659 return !producesStall(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001660}
1661
1662
1663// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001664static bool isDependent(const MachineInstr &ProdMI,
1665 const MachineInstr &ConsMI) {
1666 if (!ProdMI.getOperand(0).isReg())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001667 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001668 unsigned DstReg = ProdMI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001669
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001670 for (auto &Op : ConsMI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001671 if (Op.isReg() && Op.isUse() && Op.getReg() == DstReg)
1672 // The MIs depend on each other.
1673 return true;
1674
1675 return false;
1676}
1677
1678// V60 forward scheduling.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001679bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001680 // Check whether the previous packet is in a different loop. If this is the
1681 // case, there is little point in trying to avoid a stall because that would
1682 // favor the rare case (loop entry) over the common case (loop iteration).
1683 //
1684 // TODO: We should really be able to check all the incoming edges if this is
1685 // the first packet in a basic block, so we can avoid stalls from the loop
1686 // backedge.
1687 if (!OldPacketMIs.empty()) {
1688 auto *OldBB = OldPacketMIs.front()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001689 auto *ThisBB = I.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001690 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1691 return false;
1692 }
1693
1694 // Check for stall between two vector instructions.
1695 if (HII->isV60VectorInstruction(I)) {
1696 for (auto J : OldPacketMIs) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001697 if (!HII->isV60VectorInstruction(*J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001698 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001699 if (isDependent(*J, I) && !HII->isVecUsableNextPacket(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001700 return true;
1701 }
1702 return false;
1703 }
1704
1705 // Check for stall between two scalar instructions. First, check that
1706 // there is no definition of a use in the current packet, because it
1707 // may be a candidate for .new.
1708 for (auto J : CurrentPacketMIs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001709 if (!HII->isV60VectorInstruction(*J) && isDependent(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001710 return false;
1711
1712 // Check for stall between I and instructions in the previous packet.
1713 if (MF.getSubtarget<HexagonSubtarget>().useBSBScheduling()) {
1714 for (auto J : OldPacketMIs) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001715 if (HII->isV60VectorInstruction(*J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001716 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001717 if (!HII->isLateInstrFeedsEarlyInstr(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001718 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001719 if (isDependent(*J, I) && !HII->canExecuteInBundle(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001720 return true;
1721 }
1722 }
1723
1724 return false;
1725}
1726
1727
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001728//===----------------------------------------------------------------------===//
1729// Public Constructor Functions
1730//===----------------------------------------------------------------------===//
1731
1732FunctionPass *llvm::createHexagonPacketizer() {
1733 return new HexagonPacketizer();
1734}