blob: ba98b27561ccf425349a19945bfe98d6481a4564 [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/MachineFunctionAnalysis.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/CodeGen/Passes.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/Support/Debug.h"
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000032
33using namespace llvm;
34
Chandler Carruth84e68b22014-04-22 02:41:26 +000035#define DEBUG_TYPE "packets"
36
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000037static cl::opt<bool> DisablePacketizer("disable-packetizer", cl::Hidden,
38 cl::ZeroOrMore, cl::init(false),
39 cl::desc("Disable Hexagon packetizer pass"));
40
Jyotsna Verma1d297502013-05-02 15:39:30 +000041static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000042 cl::ZeroOrMore, cl::Hidden, cl::init(true),
43 cl::desc("Allow non-solo packetization of volatile memory references"));
44
45static cl::opt<bool> EnableGenAllInsnClass("enable-gen-insn", cl::init(false),
46 cl::Hidden, cl::ZeroOrMore, cl::desc("Generate all instruction with TC"));
47
48static cl::opt<bool> DisableVecDblNVStores("disable-vecdbl-nv-stores",
49 cl::init(false), cl::Hidden, cl::ZeroOrMore,
50 cl::desc("Disable vector double new-value-stores"));
51
52extern cl::opt<bool> ScheduleInlineAsm;
Jyotsna Verma1d297502013-05-02 15:39:30 +000053
Jyotsna Verma1d297502013-05-02 15:39:30 +000054namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000055 FunctionPass *createHexagonPacketizer();
Jyotsna Verma1d297502013-05-02 15:39:30 +000056 void initializeHexagonPacketizerPass(PassRegistry&);
57}
58
59
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000060namespace {
61 class HexagonPacketizer : public MachineFunctionPass {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000062 public:
63 static char ID;
Jyotsna Verma1d297502013-05-02 15:39:30 +000064 HexagonPacketizer() : MachineFunctionPass(ID) {
65 initializeHexagonPacketizerPass(*PassRegistry::getPassRegistry());
66 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000067
Craig Topper906c2cd2014-04-29 07:58:16 +000068 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000069 AU.setPreservesCFG();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000070 AU.addRequired<AAResultsWrapperPass>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000071 AU.addRequired<MachineBranchProbabilityInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000072 AU.addRequired<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000073 AU.addRequired<MachineLoopInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000074 AU.addPreserved<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000075 AU.addPreserved<MachineLoopInfo>();
76 MachineFunctionPass::getAnalysisUsage(AU);
77 }
Craig Topper906c2cd2014-04-29 07:58:16 +000078 const char *getPassName() const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000079 return "Hexagon Packetizer";
80 }
Craig Topper906c2cd2014-04-29 07:58:16 +000081 bool runOnMachineFunction(MachineFunction &Fn) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000082 MachineFunctionProperties getRequiredProperties() const override {
83 return MachineFunctionProperties().set(
84 MachineFunctionProperties::Property::AllVRegsAllocated);
85 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000086
87 private:
88 const HexagonInstrInfo *HII;
89 const HexagonRegisterInfo *HRI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000090 };
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000091
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000092 char HexagonPacketizer::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000093}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000094
Jyotsna Verma1d297502013-05-02 15:39:30 +000095INITIALIZE_PASS_BEGIN(HexagonPacketizer, "packets", "Hexagon Packetizer",
96 false, false)
97INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
98INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
99INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000100INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000101INITIALIZE_PASS_END(HexagonPacketizer, "packets", "Hexagon Packetizer",
102 false, false)
103
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000104HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
105 MachineLoopInfo &MLI, AliasAnalysis *AA,
106 const MachineBranchProbabilityInfo *MBPI)
107 : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI) {
108 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
109 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000110
111 addMutation(make_unique<HexagonSubtarget::HexagonDAGMutation>());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000112}
113
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000114// Check if FirstI modifies a register that SecondI reads.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000115static bool hasWriteToReadDep(const MachineInstr &FirstI,
116 const MachineInstr &SecondI,
117 const TargetRegisterInfo *TRI) {
118 for (auto &MO : FirstI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000119 if (!MO.isReg() || !MO.isDef())
120 continue;
121 unsigned R = MO.getReg();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000122 if (SecondI.readsRegister(R, TRI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000123 return true;
124 }
125 return false;
126}
127
128
129static MachineBasicBlock::iterator moveInstrOut(MachineInstr *MI,
130 MachineBasicBlock::iterator BundleIt, bool Before) {
131 MachineBasicBlock::instr_iterator InsertPt;
132 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000133 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000134 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000135 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000136
137 MachineBasicBlock &B = *MI->getParent();
138 // The instruction should at least be bundled with the preceding instruction
139 // (there will always be one, i.e. BUNDLE, if nothing else).
140 assert(MI->isBundledWithPred());
141 if (MI->isBundledWithSucc()) {
142 MI->clearFlag(MachineInstr::BundledSucc);
143 MI->clearFlag(MachineInstr::BundledPred);
144 } else {
145 // If it's not bundled with the successor (i.e. it is the last one
146 // in the bundle), then we can simply unbundle it from the predecessor,
147 // which will take care of updating the predecessor's flag.
148 MI->unbundleFromPred();
149 }
150 B.splice(InsertPt, &B, MI);
151
152 // Get the size of the bundle without asserting.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000153 MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000154 MachineBasicBlock::const_instr_iterator E = B.instr_end();
155 unsigned Size = 0;
156 for (++I; I != E && I->isBundledWithPred(); ++I)
157 ++Size;
158
159 // If there are still two or more instructions, then there is nothing
160 // else to be done.
161 if (Size > 1)
162 return BundleIt;
163
164 // Otherwise, extract the single instruction out and delete the bundle.
165 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
166 MachineInstr *SingleI = BundleIt->getNextNode();
167 SingleI->unbundleFromPred();
168 assert(!SingleI->isBundledWithSucc());
169 BundleIt->eraseFromParent();
170 return NextIt;
171}
172
173
174bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000175 if (DisablePacketizer || skipFunction(*MF.getFunction()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000176 return false;
177
178 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
179 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
180 auto &MLI = getAnalysis<MachineLoopInfo>();
181 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
182 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
183
184 if (EnableGenAllInsnClass)
185 HII->genAllInsnTimingClasses(MF);
186
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000187 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000188 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000189
190 // DFA state table should not be empty.
191 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
192
193 //
194 // Loop over all basic blocks and remove KILL pseudo-instructions
195 // These instructions confuse the dependence analysis. Consider:
196 // D0 = ... (Insn 0)
197 // R0 = KILL R0, D0 (Insn 1)
198 // R0 = ... (Insn 2)
199 // Here, Insn 1 will result in the dependence graph not emitting an output
200 // dependence between Insn 0 and Insn 2. This can lead to incorrect
201 // packetization
202 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000203 for (auto &MB : MF) {
204 auto End = MB.end();
205 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000206 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000207 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000208 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000209 MB.erase(MI);
210 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000211 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000212 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000213 }
214 }
215
216 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000217 for (auto &MB : MF) {
218 auto Begin = MB.begin(), End = MB.end();
219 while (Begin != End) {
220 // First the first non-boundary starting from the end of the last
221 // scheduling region.
222 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000223 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000224 ++RB;
225 // First the first boundary starting from the beginning of the new
226 // region.
227 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000228 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000229 ++RE;
230 // Add the scheduling boundary if it's not block end.
231 if (RE != End)
232 ++RE;
233 // If RB == End, then RE == End.
234 if (RB != End)
235 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000236
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000237 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000238 }
239 }
240
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000241 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000242 return true;
243}
244
245
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000246// Reserve resources for a constant extender. Trigger an assertion if the
247// reservation fails.
248void HexagonPacketizerList::reserveResourcesForConstExt() {
249 if (!tryAllocateResourcesForConstExt(true))
250 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000251}
252
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000253bool HexagonPacketizerList::canReserveResourcesForConstExt() {
254 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000255}
256
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000257// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
258// return true, otherwise, return false.
259bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
260 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000261 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000262 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000263 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000264 MF.DeleteMachineInstr(ExtMI);
265 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000266}
267
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000268
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000269bool HexagonPacketizerList::isCallDependent(const MachineInstr* MI,
270 SDep::Kind DepType, unsigned DepReg) {
271 // Check for LR dependence.
272 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000273 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000274
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000275 if (HII->isDeallocRet(MI))
276 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000277 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000278
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000279 // Check if this is a predicate dependence.
280 const TargetRegisterClass* RC = HRI->getMinimalPhysRegClass(DepReg);
281 if (RC == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000282 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000283
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000284 // Assumes that the first operand of the CALLr is the function address.
285 if (HII->isIndirectCall(MI) && (DepType == SDep::Data)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000286 MachineOperand MO = MI->getOperand(0);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000287 if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000288 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000289 }
290
291 return false;
292}
293
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000294static bool isRegDependence(const SDep::Kind DepType) {
295 return DepType == SDep::Data || DepType == SDep::Anti ||
296 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000297}
298
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000299static bool isDirectJump(const MachineInstr* MI) {
300 return MI->getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000301}
302
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000303static bool isSchedBarrier(const MachineInstr* MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000304 switch (MI->getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000305 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000306 return true;
307 }
308 return false;
309}
310
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000311static bool isControlFlow(const MachineInstr* MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000312 return (MI->getDesc().isTerminator() || MI->getDesc().isCall());
313}
314
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000315
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000316/// Returns true if the instruction modifies a callee-saved register.
317static bool doesModifyCalleeSavedReg(const MachineInstr *MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000318 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000319 const MachineFunction &MF = *MI->getParent()->getParent();
320 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
321 if (MI->modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000322 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000323 return false;
324}
325
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000326// TODO: MI->isIndirectBranch() and IsRegisterJump(MI)
327// Returns true if an instruction can be promoted to .new predicate or
328// new-value store.
329bool HexagonPacketizerList::isNewifiable(const MachineInstr* MI) {
330 return HII->isCondInst(MI) || MI->isReturn() || HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000331}
332
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000333// Promote an instructiont to its .cur form.
334// At this time, we have already made a call to canPromoteToDotCur and made
335// sure that it can *indeed* be promoted.
336bool HexagonPacketizerList::promoteToDotCur(MachineInstr* MI,
337 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
338 const TargetRegisterClass* RC) {
339 assert(DepType == SDep::Data);
340 int CurOpcode = HII->getDotCurOp(MI);
341 MI->setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000342 return true;
343}
344
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000345void HexagonPacketizerList::cleanUpDotCur() {
346 MachineInstr *MI = NULL;
347 for (auto BI : CurrentPacketMIs) {
348 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
349 if (BI->getOpcode() == Hexagon::V6_vL32b_cur_ai) {
350 MI = BI;
351 continue;
352 }
353 if (MI) {
354 for (auto &MO : BI->operands())
355 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
356 return;
357 }
358 }
359 if (!MI)
360 return;
361 // We did not find a use of the CUR, so de-cur it.
362 MI->setDesc(HII->get(Hexagon::V6_vL32b_ai));
363 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
364}
365
366// Check to see if an instruction can be dot cur.
367bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr *MI,
368 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
369 const TargetRegisterClass *RC) {
370 if (!HII->isV60VectorInstruction(MI))
371 return false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000372 if (!HII->isV60VectorInstruction(&*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000373 return false;
374
375 // Already a dot new instruction.
376 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
377 return false;
378
379 if (!HII->mayBeCurLoad(MI))
380 return false;
381
382 // The "cur value" cannot come from inline asm.
383 if (PacketSU->getInstr()->isInlineAsm())
384 return false;
385
386 // Make sure candidate instruction uses cur.
387 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
388 MI->dump();
389 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000390 MachineInstr &MJ = *MII;
391 DEBUG({
392 dbgs() << "Checking CUR against ";
393 MJ.dump();
394 });
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000395 unsigned DestReg = MI->getOperand(0).getReg();
396 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000397 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000398 if (MO.isReg() && MO.getReg() == DestReg)
399 FoundMatch = true;
400 if (!FoundMatch)
401 return false;
402
403 // Check for existing uses of a vector register within the packet which
404 // would be affected by converting a vector load into .cur formt.
405 for (auto BI : CurrentPacketMIs) {
406 DEBUG(dbgs() << "packet has "; BI->dump(););
407 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
408 return false;
409 }
410
411 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI->dump(););
412 // We can convert the opcode into a .cur.
413 return true;
414}
415
416// Promote an instruction to its .new form. At this time, we have already
417// made a call to canPromoteToDotNew and made sure that it can *indeed* be
418// promoted.
419bool HexagonPacketizerList::promoteToDotNew(MachineInstr* MI,
420 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
421 const TargetRegisterClass* RC) {
422 assert (DepType == SDep::Data);
423 int NewOpcode;
424 if (RC == &Hexagon::PredRegsRegClass)
425 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
426 else
427 NewOpcode = HII->getDotNewOp(MI);
428 MI->setDesc(HII->get(NewOpcode));
429 return true;
430}
431
432bool HexagonPacketizerList::demoteToDotOld(MachineInstr* MI) {
433 int NewOpcode = HII->getDotOldOp(MI->getOpcode());
434 MI->setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000435 return true;
436}
437
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000438bool HexagonPacketizerList::useCallersSP(MachineInstr *MI) {
439 unsigned Opc = MI->getOpcode();
440 switch (Opc) {
441 case Hexagon::S2_storerd_io:
442 case Hexagon::S2_storeri_io:
443 case Hexagon::S2_storerh_io:
444 case Hexagon::S2_storerb_io:
445 break;
446 default:
447 llvm_unreachable("Unexpected instruction");
448 }
449 unsigned FrameSize = MF.getFrameInfo()->getStackSize();
450 MachineOperand &Off = MI->getOperand(1);
451 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
452 if (HII->isValidOffset(Opc, NewOff)) {
453 Off.setImm(NewOff);
454 return true;
455 }
456 return false;
457}
458
459void HexagonPacketizerList::useCalleesSP(MachineInstr *MI) {
460 unsigned Opc = MI->getOpcode();
461 switch (Opc) {
462 case Hexagon::S2_storerd_io:
463 case Hexagon::S2_storeri_io:
464 case Hexagon::S2_storerh_io:
465 case Hexagon::S2_storerb_io:
466 break;
467 default:
468 llvm_unreachable("Unexpected instruction");
469 }
470 unsigned FrameSize = MF.getFrameInfo()->getStackSize();
471 MachineOperand &Off = MI->getOperand(1);
472 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
473}
474
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000475enum PredicateKind {
476 PK_False,
477 PK_True,
478 PK_Unknown
479};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000480
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000481/// Returns true if an instruction is predicated on p0 and false if it's
482/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000483static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000484 const HexagonInstrInfo *HII) {
485 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000486 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000487 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000488 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000489 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000490}
491
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000492static const MachineOperand &getPostIncrementOperand(const MachineInstr *MI,
493 const HexagonInstrInfo *HII) {
494 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000495#ifndef NDEBUG
496 // Post Increment means duplicates. Use dense map to find duplicates in the
497 // list. Caution: Densemap initializes with the minimum of 64 buckets,
498 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000499 DenseSet<unsigned> DefRegsSet;
500 for (auto &MO : MI->operands())
501 if (MO.isReg() && MO.isDef())
502 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000503
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000504 for (auto &MO : MI->operands())
505 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
506 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000507#else
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000508 if (MI->mayLoad()) {
Krzysztof Parzyszek4f9164d2015-12-16 20:07:04 +0000509 const MachineOperand &Op1 = MI->getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000510 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000511 assert(Op1.isReg() && "Post increment operand has be to a register.");
512 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000513 }
514 if (MI->getDesc().mayStore()) {
Krzysztof Parzyszek4f9164d2015-12-16 20:07:04 +0000515 const MachineOperand &Op0 = MI->getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000516 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000517 assert(Op0.isReg() && "Post increment operand has be to a register.");
518 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000519 }
520#endif
521 // we should never come here.
522 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
523}
524
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000525// Get the value being stored.
526static const MachineOperand& getStoreValueOperand(const MachineInstr *MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000527 // value being stored is always the last operand.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000528 return MI->getOperand(MI->getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000529}
530
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000531static bool isLoadAbsSet(const MachineInstr *MI) {
532 unsigned Opc = MI->getOpcode();
533 switch (Opc) {
534 case Hexagon::L4_loadrd_ap:
535 case Hexagon::L4_loadrb_ap:
536 case Hexagon::L4_loadrh_ap:
537 case Hexagon::L4_loadrub_ap:
538 case Hexagon::L4_loadruh_ap:
539 case Hexagon::L4_loadri_ap:
540 return true;
541 }
542 return false;
543}
544
545static const MachineOperand &getAbsSetOperand(const MachineInstr *MI) {
546 assert(isLoadAbsSet(MI));
547 return MI->getOperand(1);
548}
549
550
551// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000552// Following restrictions are to be respected in convert a store into
553// a new value store.
554// 1. If an instruction uses auto-increment, its address register cannot
555// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000556// 2. If an instruction uses absolute-set addressing mode, its address
557// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000558// 3. If an instruction produces a 64-bit result, its registers cannot be used
559// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000560// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000561// the instruction that uses the new-value register must also be conditional,
562// and both must always have their predicates evaluate identically.
563// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000564// 5. There is an implied restriction that a packet cannot have another store,
565// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000566// already a store in a packet, there can not be a new value store.
567// Arch Spec: 3.4.4.2
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000568bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr *MI,
569 const MachineInstr *PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000570 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000571 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000572 return false;
573
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000574 // Make sure there is dependency and can be new value'd.
575 const MachineOperand &Val = getStoreValueOperand(MI);
576 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000577 return false;
578
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000579 const MCInstrDesc& MCID = PacketMI->getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000580
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000581 // First operand is always the result.
582 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
583 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
584 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000585 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000586
587 // New-value stores are of class NV (slot 0), dual stores require class ST
588 // in slot 0 (PRM 5.5).
589 for (auto I : CurrentPacketMIs) {
590 SUnit *PacketSU = MIToSUnit.find(I)->second;
591 if (PacketSU->getInstr()->mayStore())
592 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000593 }
594
595 // Make sure it's NOT the post increment register that we are going to
596 // new value.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000597 if (HII->isPostIncrement(MI) &&
598 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000599 return false;
600 }
601
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000602 if (HII->isPostIncrement(PacketMI) && PacketMI->mayLoad() &&
603 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
604 // If source is post_inc, or absolute-set addressing, it can not feed
605 // into new value store
606 // r3 = memw(r2++#4)
607 // memw(r30 + #-1404) = r2.new -> can not be new value store
608 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000609 return false;
610 }
611
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000612 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
613 return false;
614
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000615 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000616 // also be predicated.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000617 if (HII->isPredicated(*PacketMI)) {
618 if (!HII->isPredicated(*MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000619 return false;
620
621 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000622 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000623 unsigned predRegNumSrc = 0;
624 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000625 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000626
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000627 // Get predicate register used in the source instruction.
628 for (auto &MO : PacketMI->operands()) {
629 if (!MO.isReg())
630 continue;
631 predRegNumSrc = MO.getReg();
632 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
633 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000634 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000635 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000636 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
637 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000638
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000639 // Get predicate register used in new-value store instruction.
640 for (auto &MO : MI->operands()) {
641 if (!MO.isReg())
642 continue;
643 predRegNumDst = MO.getReg();
644 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
645 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000646 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000647 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000648 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
649 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000650
651 // New-value register producer and user (store) need to satisfy these
652 // constraints:
653 // 1) Both instructions should be predicated on the same register.
654 // 2) If producer of the new-value register is .new predicated then store
655 // should also be .new predicated and if producer is not .new predicated
656 // then store should not be .new predicated.
657 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000658 // sense, i.e, either both should be negated or both should be non-negated.
659 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000660 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
661 getPredicateSense(*MI, HII) != getPredicateSense(*PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000662 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000663 }
664
665 // Make sure that other than the new-value register no other store instruction
666 // register has been modified in the same packet. Predicate registers can be
667 // modified by they should not be modified between the producer and the store
668 // instruction as it will make them both conditional on different values.
669 // We already know this to be true for all the instructions before and
670 // including PacketMI. Howerver, we need to perform the check for the
671 // remaining instructions in the packet.
672
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000673 unsigned StartCheck = 0;
674
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000675 for (auto I : CurrentPacketMIs) {
676 SUnit *TempSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000677 MachineInstr* TempMI = TempSU->getInstr();
678
679 // Following condition is true for all the instructions until PacketMI is
680 // reached (StartCheck is set to 0 before the for loop).
681 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000682 if (TempMI != PacketMI && !StartCheck) // Start processing only after
683 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000684
685 StartCheck = 1;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000686 if (TempMI == PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000687 continue;
688
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000689 for (auto &MO : MI->operands())
690 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000691 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000692 }
693
Alp Tokerf907b892013-12-05 05:44:44 +0000694 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000695 // 1. The only use of reg is DepReg and no other registers.
696 // This handles V4 base+index registers.
697 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000698 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000699 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000700 if (!HII->isPostIncrement(MI)) {
701 for (unsigned opNum = 0; opNum < MI->getNumOperands()-1; opNum++) {
702 const MachineOperand &MO = MI->getOperand(opNum);
703 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000704 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000705 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000706 }
707
708 // If data definition is because of implicit definition of the register,
709 // do not newify the store. Eg.
710 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
711 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
712 for (auto &MO : PacketMI->operands()) {
713 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
714 continue;
715 unsigned R = MO.getReg();
716 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
717 return false;
718 }
719
720 // Handle imp-use of super reg case. There is a target independent side
721 // change that should prevent this situation but I am handling it for
722 // just-in-case. For example, we cannot newify R2 in the following case:
723 // %R3<def> = A2_tfrsi 0;
724 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
725 for (auto &MO : MI->operands()) {
726 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
727 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000728 }
729
730 // Can be dot new store.
731 return true;
732}
733
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000734// Can this MI to promoted to either new value store or new value jump.
735bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr *MI,
736 const SUnit *PacketSU, unsigned DepReg,
737 MachineBasicBlock::iterator &MII) {
738 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000739 return false;
740
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000741 // Check to see the store can be new value'ed.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000742 MachineInstr *PacketMI = PacketSU->getInstr();
743 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000744 return true;
745
746 // Check to see the compare/jump can be new value'ed.
747 // This is done as a pass on its own. Don't need to check it here.
748 return false;
749}
750
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000751static bool isImplicitDependency(const MachineInstr *I, unsigned DepReg) {
752 for (auto &MO : I->operands())
753 if (MO.isReg() && MO.isDef() && (MO.getReg() == DepReg) && MO.isImplicit())
754 return true;
755 return false;
756}
757
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000758// Check to see if an instruction can be dot new
759// There are three kinds.
760// 1. dot new on predicate - V2/V3/V4
761// 2. dot new on stores NV/ST - V4
762// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000763bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr *MI,
764 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
765 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000766 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000767 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000768 return false;
769
770 if (!isNewifiable(MI))
771 return false;
772
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000773 const MachineInstr *PI = PacketSU->getInstr();
774
775 // The "new value" cannot come from inline asm.
776 if (PI->isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000777 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000778
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000779 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
780 // sense.
781 if (PI->isImplicitDef())
782 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000783
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000784 // If dependency is trough an implicitly defined register, we should not
785 // newify the use.
786 if (isImplicitDependency(PI, DepReg))
787 return false;
788
789 const MCInstrDesc& MCID = PI->getDesc();
790 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
791 if (DisableVecDblNVStores && VecRC == &Hexagon::VecDblRegsRegClass)
792 return false;
793
794 // predicate .new
795 // bug 5670: until that is fixed
796 // TODO: MI->isIndirectBranch() and IsRegisterJump(MI)
797 if (RC == &Hexagon::PredRegsRegClass)
798 if (HII->isCondInst(MI) || MI->isReturn())
799 return HII->predCanBeUsedAsDotNew(PI, DepReg);
800
801 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
802 return false;
803
804 // Create a dot new machine instruction to see if resources can be
805 // allocated. If not, bail out now.
806 int NewOpcode = HII->getDotNewOp(MI);
807 const MCInstrDesc &D = HII->get(NewOpcode);
808 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000809 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000810 MF.DeleteMachineInstr(NewMI);
811 if (!ResourcesAvailable)
812 return false;
813
814 // New Value Store only. New Value Jump generated as a separate pass.
815 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
816 return false;
817
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000818 return true;
819}
820
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000821// Go through the packet instructions and search for an anti dependency between
822// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000823// Trying to add
824// a) %R1<def> = TFRI_cdNotPt %P3, 2
825// to this packet:
826// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000827// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
828// c) %P3<def> = C2_tfrrp %R23
829// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000830// }
831// The P3 from a) and d) will be complements after
832// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000833// Anti-dep between c) and b) is irrelevant for this case
834bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr* MI,
835 unsigned DepReg) {
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000836 SUnit *PacketSUDep = MIToSUnit.find(MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000837
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000838 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000839 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000840 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000841 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000842
843 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000844 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000845
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000846 // Look at dependencies between current members of the packet and
847 // predicate defining instruction MI. Make sure that dependency is
848 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000849 if (PacketSU->isSucc(PacketSUDep)) {
850 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000851 auto &Dep = PacketSU->Succs[i];
852 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
853 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000854 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000855 }
856 }
857 }
858
859 return false;
860}
861
862
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000863/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000864static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000865 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000866 /// We use the following rule: The first predicate register that is a use is
867 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000868 assert(QII->isPredicated(MI) && "Must be predicated instruction");
869
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000870 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000871 if (Op.isReg() && Op.getReg() && Op.isUse() &&
872 Hexagon::PredRegsRegClass.contains(Op.getReg()))
873 return Op.getReg();
874 }
875
876 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000877 return 0;
878}
879
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000880// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000881// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000882bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
883 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000884 // If we don't know the predicate sense of the instructions bail out early, we
885 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000886 if (getPredicateSense(MI1, HII) == PK_Unknown ||
887 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000888 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000889
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000890 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000891 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000892
893 // One corner case deals with the following scenario:
894 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000895 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000896 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000897 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000898 // b) %R25<def> = A2_tfrf %P0, %R24
899 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000900 // }
901 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000902 // On general check a) and b) are complements, but presence of c) will
903 // convert a) to .new form, and then it is not a complement.
904 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000905
906 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000907 // Look for Anti dependecy on the same predicate reg as used in the
908 // candidate.
909 for (auto I : CurrentPacketMIs) {
910 // Scheduling Unit for current insn in the packet.
911 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000912
913 // If this instruction in the packet is succeeded by the candidate...
914 if (PacketSU->isSucc(SU)) {
915 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000916 auto Dep = PacketSU->Succs[i];
917 // The corner case exist when there is true data dependency between
918 // candidate and one of current packet members, this dep is on
919 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000920 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000921 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
922 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
923 // Here I know that I is predicate setting instruction with true
924 // data dep to candidate on the register we care about - c) in the
925 // above example. Now I need to see if there is an anti dependency
926 // from c) to any other instruction in the same packet on the pred
927 // reg of interest.
928 if (restrictingDepExistInPacket(I, Dep.getReg()))
929 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000930 }
931 }
932 }
933 }
934
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000935 // If the above case does not apply, check regular complement condition.
936 // Check that the predicate register is the same and that the predicate
937 // sense is different We also need to differentiate .old vs. .new: !p0
938 // is not complementary to p0.new.
939 unsigned PReg1 = getPredicatedRegister(MI1, HII);
940 unsigned PReg2 = getPredicatedRegister(MI2, HII);
941 return PReg1 == PReg2 &&
942 Hexagon::PredRegsRegClass.contains(PReg1) &&
943 Hexagon::PredRegsRegClass.contains(PReg2) &&
944 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000945 HII->isDotNewInst(&MI1) == HII->isDotNewInst(&MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000946}
947
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000948// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000949void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000950 Dependence = false;
951 PromotedToDotNew = false;
952 GlueToNewValueJump = false;
953 GlueAllocframeStore = false;
954 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000955}
956
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000957// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000958bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
959 const MachineBasicBlock *) {
960 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000961 return true;
962
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000963 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000964 return false;
965
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000966 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000967 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000968 return false;
969
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000970 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000971 return false;
972
973 // We check if MI has any functional units mapped to it. If it doesn't,
974 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000975 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000976 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +0000977 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000978 return !FuncUnits;
979}
980
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000981bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
982 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000983 return true;
984
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000985 // Consider inline asm to not be a solo instruction by default.
986 // Inline asm will be put in a packet temporarily, but then it will be
987 // removed, and placed outside of the packet (before or after, depending
988 // on dependencies). This is to reduce the impact of inline asm as a
989 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000990 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000991 return true;
992
993 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
994 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
995 // They must not be grouped with other instructions in a packet.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000996 if (isSchedBarrier(&MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000997 return true;
998
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000999 if (HII->isSolo(&MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001000 return true;
1001
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001002 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001003 return true;
1004
1005 return false;
1006}
1007
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001008
1009// Quick check if instructions MI and MJ cannot coexist in the same packet.
1010// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1011// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1012// For full test call this function twice:
1013// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1014// Doing the test only one way saves the amount of code in this function,
1015// since every test would need to be repeated with the MI and MJ reversed.
1016static bool cannotCoexistAsymm(const MachineInstr *MI, const MachineInstr *MJ,
1017 const HexagonInstrInfo &HII) {
1018 const MachineFunction *MF = MI->getParent()->getParent();
1019 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1020 HII.isHVXMemWithAIndirect(MI, MJ))
1021 return true;
1022
1023 // An inline asm cannot be together with a branch, because we may not be
1024 // able to remove the asm out after packetizing (i.e. if the asm must be
1025 // moved past the bundle). Similarly, two asms cannot be together to avoid
1026 // complications when determining their relative order outside of a bundle.
1027 if (MI->isInlineAsm())
1028 return MJ->isInlineAsm() || MJ->isBranch() || MJ->isBarrier() ||
1029 MJ->isCall() || MJ->isTerminator();
1030
1031 // "False" really means that the quick check failed to determine if
1032 // I and J cannot coexist.
1033 return false;
1034}
1035
1036
1037// Full, symmetric check.
1038bool HexagonPacketizerList::cannotCoexist(const MachineInstr *MI,
1039 const MachineInstr *MJ) {
1040 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1041}
1042
1043void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1044 for (auto &B : MF) {
1045 MachineBasicBlock::iterator BundleIt;
1046 MachineBasicBlock::instr_iterator NextI;
1047 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1048 NextI = std::next(I);
1049 MachineInstr *MI = &*I;
1050 if (MI->isBundle())
1051 BundleIt = I;
1052 if (!MI->isInsideBundle())
1053 continue;
1054
1055 // Decide on where to insert the instruction that we are pulling out.
1056 // Debug instructions always go before the bundle, but the placement of
1057 // INLINE_ASM depends on potential dependencies. By default, try to
1058 // put it before the bundle, but if the asm writes to a register that
1059 // other instructions in the bundle read, then we need to place it
1060 // after the bundle (to preserve the bundle semantics).
1061 bool InsertBeforeBundle;
1062 if (MI->isInlineAsm())
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001063 InsertBeforeBundle = !hasWriteToReadDep(*MI, *BundleIt, HRI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001064 else if (MI->isDebugValue())
1065 InsertBeforeBundle = true;
1066 else
1067 continue;
1068
1069 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1070 }
1071 }
1072}
1073
1074// Check if a given instruction is of class "system".
1075static bool isSystemInstr(const MachineInstr *MI) {
1076 unsigned Opc = MI->getOpcode();
1077 switch (Opc) {
1078 case Hexagon::Y2_barrier:
1079 case Hexagon::Y2_dcfetchbo:
1080 return true;
1081 }
1082 return false;
1083}
1084
1085bool HexagonPacketizerList::hasDeadDependence(const MachineInstr *I,
1086 const MachineInstr *J) {
1087 // The dependence graph may not include edges between dead definitions,
1088 // so without extra checks, we could end up packetizing two instruction
1089 // defining the same (dead) register.
1090 if (I->isCall() || J->isCall())
1091 return false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001092 if (HII->isPredicated(*I) || HII->isPredicated(*J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001093 return false;
1094
1095 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
1096 for (auto &MO : I->operands()) {
1097 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1098 continue;
1099 DeadDefs[MO.getReg()] = true;
1100 }
1101
1102 for (auto &MO : J->operands()) {
1103 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1104 continue;
1105 unsigned R = MO.getReg();
1106 if (R != Hexagon::USR_OVF && DeadDefs[R])
1107 return true;
1108 }
1109 return false;
1110}
1111
1112bool HexagonPacketizerList::hasControlDependence(const MachineInstr *I,
1113 const MachineInstr *J) {
1114 // A save callee-save register function call can only be in a packet
1115 // with instructions that don't write to the callee-save registers.
1116 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1117 doesModifyCalleeSavedReg(J, HRI)) ||
1118 (HII->isSaveCalleeSavedRegsCall(J) &&
1119 doesModifyCalleeSavedReg(I, HRI)))
1120 return true;
1121
1122 // Two control flow instructions cannot go in the same packet.
1123 if (isControlFlow(I) && isControlFlow(J))
1124 return true;
1125
1126 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1127 // contain a speculative indirect jump,
1128 // a new-value compare jump or a dealloc_return.
1129 auto isBadForLoopN = [this] (const MachineInstr *MI) -> bool {
1130 if (MI->isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
1131 return true;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001132 if (HII->isPredicated(*MI) && HII->isPredicatedNew(*MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001133 return true;
1134 return false;
1135 };
1136
1137 if (HII->isLoopN(I) && isBadForLoopN(J))
1138 return true;
1139 if (HII->isLoopN(J) && isBadForLoopN(I))
1140 return true;
1141
1142 // dealloc_return cannot appear in the same packet as a conditional or
1143 // unconditional jump.
1144 return HII->isDeallocRet(I) &&
1145 (J->isBranch() || J->isCall() || J->isBarrier());
1146}
1147
1148bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr *I,
1149 const MachineInstr *J) {
1150 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
1151 bool StoreI = I->mayStore(), StoreJ = J->mayStore();
1152 if ((SysI && StoreJ) || (SysJ && StoreI))
1153 return true;
1154
1155 if (StoreI && StoreJ) {
1156 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1157 return true;
1158 } else {
1159 // A memop cannot be in the same packet with another memop or a store.
1160 // Two stores can be together, but here I and J cannot both be stores.
1161 bool MopStI = HII->isMemOp(I) || StoreI;
1162 bool MopStJ = HII->isMemOp(J) || StoreJ;
1163 if (MopStI && MopStJ)
1164 return true;
1165 }
1166
1167 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1168}
1169
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001170// SUI is the current instruction that is out side of the current packet.
1171// SUJ is the current instruction inside the current packet against which that
1172// SUI will be packetized.
1173bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
1174 MachineInstr *I = SUI->getInstr();
1175 MachineInstr *J = SUJ->getInstr();
1176 assert(I && J && "Unable to packetize null instruction!");
1177
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001178 // Clear IgnoreDepMIs when Packet starts.
1179 if (CurrentPacketMIs.size() == 1)
1180 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001181
1182 MachineBasicBlock::iterator II = I;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001183
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001184 // Solo instructions cannot go in the packet.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001185 assert(!isSoloInstruction(*I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001186
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001187 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001188 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001189
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001190 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1191 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001192 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001193
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001194 // V4 allows dual stores. It does not allow second store, if the first
1195 // store is not in SLOT0. New value store, new value jump, dealloc_return
1196 // and memop always take SLOT0. Arch spec 3.4.4.2.
1197 Dependence = hasV4SpecificDependence(I, J);
1198 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001199 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001200
1201 // If an instruction feeds new value jump, glue it.
1202 MachineBasicBlock::iterator NextMII = I;
1203 ++NextMII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001204 if (NextMII != I->getParent()->end() && HII->isNewValueJump(&*NextMII)) {
1205 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001206
1207 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001208 const MachineOperand &NOp0 = NextMI.getOperand(0);
1209 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001210
Krzysztof Parzyszek4f9164d2015-12-16 20:07:04 +00001211 if (NOp1.isReg() && I->getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001212 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001213
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001214 for (auto I : CurrentPacketMIs) {
1215 SUnit *PacketSU = MIToSUnit.find(I)->second;
1216 MachineInstr *PI = PacketSU->getInstr();
1217 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
1218 if (PI->isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001219 Dependence = true;
1220 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001221 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001222 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001223 // 1. Packet does not have a store in it.
1224 // 2. If the first operand of the nvj is newified, and the second
1225 // operand is also a reg, it (second reg) is not defined in
1226 // the same packet.
1227 // 3. If the second operand of the nvj is newified, (which means
1228 // first operand is also a reg), first reg is not defined in
1229 // the same packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001230 if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() ||
1231 HII->isLoopN(PI)) {
1232 Dependence = true;
1233 break;
1234 }
1235 // Check #2/#3.
1236 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
1237 if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001238 Dependence = true;
1239 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001240 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001241 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001242
1243 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001244 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001245 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001246 }
1247
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001248 // There no dependency between a prolog instruction and its successor.
1249 if (!SUJ->isSucc(SUI))
1250 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001251
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001252 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1253 if (FoundSequentialDependence)
1254 break;
1255
1256 if (SUJ->Succs[i].getSUnit() != SUI)
1257 continue;
1258
1259 SDep::Kind DepType = SUJ->Succs[i].getKind();
1260 // For direct calls:
1261 // Ignore register dependences for call instructions for packetization
1262 // purposes except for those due to r31 and predicate registers.
1263 //
1264 // For indirect calls:
1265 // Same as direct calls + check for true dependences to the register
1266 // used in the indirect call.
1267 //
1268 // We completely ignore Order dependences for call instructions.
1269 //
1270 // For returns:
1271 // Ignore register dependences for return instructions like jumpr,
1272 // dealloc return unless we have dependencies on the explicit uses
1273 // of the registers used by jumpr (like r31) or dealloc return
1274 // (like r29 or r30).
1275 //
1276 // TODO: Currently, jumpr is handling only return of r31. So, the
1277 // following logic (specificaly isCallDependent) is working fine.
1278 // We need to enable jumpr for register other than r31 and then,
1279 // we need to rework the last part, where it handles indirect call
1280 // of that (isCallDependent) function. Bug 6216 is opened for this.
1281 unsigned DepReg = 0;
1282 const TargetRegisterClass *RC = nullptr;
1283 if (DepType == SDep::Data) {
1284 DepReg = SUJ->Succs[i].getReg();
1285 RC = HRI->getMinimalPhysRegClass(DepReg);
1286 }
1287
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00001288 if (I->isCall() || I->isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001289 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001290 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001291 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1292 continue;
1293 }
1294
1295 if (DepType == SDep::Data) {
1296 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1297 if (promoteToDotCur(J, DepType, II, RC))
1298 continue;
1299 }
1300
1301 // Data dpendence ok if we have load.cur.
1302 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
1303 if (HII->isV60VectorInstruction(I))
1304 continue;
1305 }
1306
1307 // For instructions that can be promoted to dot-new, try to promote.
1308 if (DepType == SDep::Data) {
1309 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1310 if (promoteToDotNew(I, DepType, II, RC)) {
1311 PromotedToDotNew = true;
1312 continue;
1313 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001314 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001315 if (HII->isNewValueJump(I))
1316 continue;
1317 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001318
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001319 // For predicated instructions, if the predicates are complements then
1320 // there can be no dependence.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001321 if (HII->isPredicated(*I) && HII->isPredicated(*J) &&
1322 arePredicatesComplements(*I, *J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001323 // Not always safe to do this translation.
1324 // DAG Builder attempts to reduce dependence edges using transitive
1325 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001326 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001327 // r0 = tfr_pt ... (1)
1328 // r0 = tfr_pf ... (2)
1329 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001330 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001331 // There will be an output dependence between (1)->(2) and (2)->(3).
1332 // However, there is no dependence edge between (1)->(3). This results
1333 // in all 3 instructions going in the same packet. We ignore dependce
1334 // only once to avoid this situation.
1335 auto Itr = std::find(IgnoreDepMIs.begin(), IgnoreDepMIs.end(), J);
1336 if (Itr != IgnoreDepMIs.end()) {
1337 Dependence = true;
1338 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001339 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001340 IgnoreDepMIs.push_back(I);
1341 continue;
1342 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001343
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001344 // Ignore Order dependences between unconditional direct branches
1345 // and non-control-flow instructions.
1346 if (isDirectJump(I) && !J->isBranch() && !J->isCall() &&
1347 DepType == SDep::Order)
1348 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001349
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001350 // Ignore all dependences for jumps except for true and output
1351 // dependences.
1352 if (I->isConditionalBranch() && DepType != SDep::Data &&
1353 DepType != SDep::Output)
1354 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001355
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001356 // Ignore output dependences due to superregs. We can write to two
1357 // different subregisters of R1:0 for instance in the same cycle.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001358
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001359 // If neither I nor J defines DepReg, then this is a superfluous output
1360 // dependence. The dependence must be of the form:
1361 // R0 = ...
1362 // R1 = ...
1363 // and there is an output dependence between the two instructions with
1364 // DepReg = D0.
1365 // We want to ignore these dependences. Ideally, the dependence
1366 // constructor should annotate such dependences. We can then avoid this
1367 // relatively expensive check.
1368 //
1369 if (DepType == SDep::Output) {
1370 // DepReg is the register that's responsible for the dependence.
1371 unsigned DepReg = SUJ->Succs[i].getReg();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001372
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001373 // Check if I and J really defines DepReg.
1374 if (!I->definesRegister(DepReg) && !J->definesRegister(DepReg))
1375 continue;
1376 FoundSequentialDependence = true;
1377 break;
1378 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001379
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001380 // For Order dependences:
1381 // 1. On V4 or later, volatile loads/stores can be packetized together,
1382 // unless other rules prevent is.
1383 // 2. Store followed by a load is not allowed.
1384 // 3. Store followed by a store is only valid on V4 or later.
1385 // 4. Load followed by any memory operation is allowed.
1386 if (DepType == SDep::Order) {
1387 if (!PacketizeVolatiles) {
1388 bool OrdRefs = I->hasOrderedMemoryRef() || J->hasOrderedMemoryRef();
1389 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001390 FoundSequentialDependence = true;
1391 break;
1392 }
1393 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001394 // J is first, I is second.
1395 bool LoadJ = J->mayLoad(), StoreJ = J->mayStore();
1396 bool LoadI = I->mayLoad(), StoreI = I->mayStore();
1397 if (StoreJ) {
1398 // Two stores are only allowed on V4+. Load following store is never
1399 // allowed.
1400 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001401 FoundSequentialDependence = true;
1402 break;
1403 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001404 } else if (!LoadJ || (!LoadI && !StoreI)) {
1405 // If J is neither load nor store, assume a dependency.
1406 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001407 FoundSequentialDependence = true;
1408 break;
1409 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001410 // Store followed by store: not OK on V2.
1411 // Store followed by load: not OK on all.
1412 // Load followed by store: OK on all.
1413 // Load followed by load: OK on all.
1414 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001415 }
1416
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001417 // For V4, special case ALLOCFRAME. Even though there is dependency
1418 // between ALLOCFRAME and subsequent store, allow it to be packetized
1419 // in a same packet. This implies that the store is using the caller's
1420 // SP. Hence, offset needs to be updated accordingly.
1421 if (DepType == SDep::Data && J->getOpcode() == Hexagon::S2_allocframe) {
1422 unsigned Opc = I->getOpcode();
1423 switch (Opc) {
1424 case Hexagon::S2_storerd_io:
1425 case Hexagon::S2_storeri_io:
1426 case Hexagon::S2_storerh_io:
1427 case Hexagon::S2_storerb_io:
1428 if (I->getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001429 // Since this store is to be glued with allocframe in the same
1430 // packet, it will use SP of the previous stack frame, i.e.
1431 // caller's SP. Therefore, we need to recalculate offset
1432 // according to this change.
1433 GlueAllocframeStore = useCallersSP(I);
1434 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001435 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001436 }
1437 default:
1438 break;
1439 }
1440 }
1441
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001442 // There are certain anti-dependencies that cannot be ignored.
1443 // Specifically:
1444 // J2_call ... %R0<imp-def> ; SUJ
1445 // R0 = ... ; SUI
1446 // Those cannot be packetized together, since the call will observe
1447 // the effect of the assignment to R0.
1448 if (DepType == SDep::Anti && J->isCall()) {
1449 // Check if I defines any volatile register. We should also check
1450 // registers that the call may read, but these happen to be a
1451 // subset of the volatile register set.
1452 for (const MCPhysReg *P = J->getDesc().ImplicitDefs; P && *P; ++P) {
1453 if (!I->modifiesRegister(*P, HRI))
1454 continue;
1455 FoundSequentialDependence = true;
1456 break;
1457 }
1458 }
1459
1460 // Skip over remaining anti-dependences. Two instructions that are
1461 // anti-dependent can share a packet, since in most such cases all
1462 // operands are read before any modifications take place.
1463 // The exceptions are branch and call instructions, since they are
1464 // executed after all other instructions have completed (at least
1465 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001466 if (DepType != SDep::Anti) {
1467 FoundSequentialDependence = true;
1468 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001469 }
1470 }
1471
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001472 if (FoundSequentialDependence) {
1473 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001474 return false;
1475 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001476
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001477 return true;
1478}
1479
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001480bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
1481 MachineInstr *I = SUI->getInstr();
1482 MachineInstr *J = SUJ->getInstr();
1483 assert(I && J && "Unable to packetize null instruction!");
1484
1485 if (cannotCoexist(I, J))
1486 return false;
1487
1488 if (!Dependence)
1489 return true;
1490
1491 // Check if the instruction was promoted to a dot-new. If so, demote it
1492 // back into a dot-old.
1493 if (PromotedToDotNew)
1494 demoteToDotOld(I);
1495
1496 cleanUpDotCur();
1497 // Check if the instruction (must be a store) was glued with an allocframe
1498 // instruction. If so, restore its offset to its original value, i.e. use
1499 // current SP instead of caller's SP.
1500 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001501 useCalleesSP(I);
1502 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001503 }
1504 return false;
1505}
1506
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001507MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001508HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001509 MachineBasicBlock::iterator MII = MI;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001510 MachineBasicBlock *MBB = MI.getParent();
1511 if (MI.isImplicitDef()) {
1512 unsigned R = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001513 if (Hexagon::IntRegsRegClass.contains(R)) {
1514 MCSuperRegIterator S(R, HRI, false);
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001515 MI.addOperand(MachineOperand::CreateReg(*S, true, true));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001516 }
1517 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001518 }
1519 assert(ResourceTracker->canReserveResources(MI));
1520
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001521 bool ExtMI = HII->isExtended(&MI) || HII->isConstExtended(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001522 bool Good = true;
1523
1524 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001525 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001526 // We need to put both instructions in the same packet: MI and NvjMI.
1527 // Either of them can require a constant extender. Try to add both to
1528 // the current packet, and if that fails, end the packet and start a
1529 // new one.
1530 ResourceTracker->reserveResources(MI);
1531 if (ExtMI)
1532 Good = tryAllocateResourcesForConstExt(true);
1533
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001534 bool ExtNvjMI = HII->isExtended(&NvjMI) || HII->isConstExtended(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001535 if (Good) {
1536 if (ResourceTracker->canReserveResources(NvjMI))
1537 ResourceTracker->reserveResources(NvjMI);
1538 else
1539 Good = false;
1540 }
1541 if (Good && ExtNvjMI)
1542 Good = tryAllocateResourcesForConstExt(true);
1543
1544 if (!Good) {
1545 endPacket(MBB, MI);
1546 assert(ResourceTracker->canReserveResources(MI));
1547 ResourceTracker->reserveResources(MI);
1548 if (ExtMI) {
1549 assert(canReserveResourcesForConstExt());
1550 tryAllocateResourcesForConstExt(true);
1551 }
1552 assert(ResourceTracker->canReserveResources(NvjMI));
1553 ResourceTracker->reserveResources(NvjMI);
1554 if (ExtNvjMI) {
1555 assert(canReserveResourcesForConstExt());
1556 reserveResourcesForConstExt();
1557 }
1558 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001559 CurrentPacketMIs.push_back(&MI);
1560 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001561 return MII;
1562 }
1563
1564 ResourceTracker->reserveResources(MI);
1565 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1566 endPacket(MBB, MI);
1567 if (PromotedToDotNew)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001568 demoteToDotOld(&MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001569 if (GlueAllocframeStore) {
1570 useCalleesSP(&MI);
1571 GlueAllocframeStore = false;
1572 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001573 ResourceTracker->reserveResources(MI);
1574 reserveResourcesForConstExt();
1575 }
1576
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001577 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001578 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001579}
1580
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001581void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001582 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001583 OldPacketMIs = CurrentPacketMIs;
1584 VLIWPacketizerList::endPacket(MBB, MI);
1585}
1586
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001587bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
1588 return !producesStall(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001589}
1590
1591
1592// Return true when ConsMI uses a register defined by ProdMI.
1593static bool isDependent(const MachineInstr *ProdMI,
1594 const MachineInstr *ConsMI) {
1595 if (!ProdMI->getOperand(0).isReg())
1596 return false;
1597 unsigned DstReg = ProdMI->getOperand(0).getReg();
1598
1599 for (auto &Op : ConsMI->operands())
1600 if (Op.isReg() && Op.isUse() && Op.getReg() == DstReg)
1601 // The MIs depend on each other.
1602 return true;
1603
1604 return false;
1605}
1606
1607// V60 forward scheduling.
1608bool HexagonPacketizerList::producesStall(const MachineInstr *I) {
1609 // Check whether the previous packet is in a different loop. If this is the
1610 // case, there is little point in trying to avoid a stall because that would
1611 // favor the rare case (loop entry) over the common case (loop iteration).
1612 //
1613 // TODO: We should really be able to check all the incoming edges if this is
1614 // the first packet in a basic block, so we can avoid stalls from the loop
1615 // backedge.
1616 if (!OldPacketMIs.empty()) {
1617 auto *OldBB = OldPacketMIs.front()->getParent();
1618 auto *ThisBB = I->getParent();
1619 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1620 return false;
1621 }
1622
1623 // Check for stall between two vector instructions.
1624 if (HII->isV60VectorInstruction(I)) {
1625 for (auto J : OldPacketMIs) {
1626 if (!HII->isV60VectorInstruction(J))
1627 continue;
1628 if (isDependent(J, I) && !HII->isVecUsableNextPacket(J, I))
1629 return true;
1630 }
1631 return false;
1632 }
1633
1634 // Check for stall between two scalar instructions. First, check that
1635 // there is no definition of a use in the current packet, because it
1636 // may be a candidate for .new.
1637 for (auto J : CurrentPacketMIs)
1638 if (!HII->isV60VectorInstruction(J) && isDependent(J, I))
1639 return false;
1640
1641 // Check for stall between I and instructions in the previous packet.
1642 if (MF.getSubtarget<HexagonSubtarget>().useBSBScheduling()) {
1643 for (auto J : OldPacketMIs) {
1644 if (HII->isV60VectorInstruction(J))
1645 continue;
1646 if (!HII->isLateInstrFeedsEarlyInstr(J, I))
1647 continue;
1648 if (isDependent(J, I) && !HII->canExecuteInBundle(J, I))
1649 return true;
1650 }
1651 }
1652
1653 return false;
1654}
1655
1656
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001657//===----------------------------------------------------------------------===//
1658// Public Constructor Functions
1659//===----------------------------------------------------------------------===//
1660
1661FunctionPass *llvm::createHexagonPacketizer() {
1662 return new HexagonPacketizer();
1663}