blob: ef879591b19b63848de9a23df8cb232491faf7dd [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.
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000329bool HexagonPacketizerList::isNewifiable(const MachineInstr* MI,
330 const TargetRegisterClass *NewRC) {
331 // Vector stores can be predicated, and can be new-value stores, but
332 // they cannot be predicated on a .new predicate value.
333 if (NewRC == &Hexagon::PredRegsRegClass)
334 if (HII->isV60VectorInstruction(MI) && MI->mayStore())
335 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000336 return HII->isCondInst(MI) || MI->isReturn() || HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000337}
338
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000339// Promote an instructiont to its .cur form.
340// At this time, we have already made a call to canPromoteToDotCur and made
341// sure that it can *indeed* be promoted.
342bool HexagonPacketizerList::promoteToDotCur(MachineInstr* MI,
343 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
344 const TargetRegisterClass* RC) {
345 assert(DepType == SDep::Data);
346 int CurOpcode = HII->getDotCurOp(MI);
347 MI->setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000348 return true;
349}
350
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000351void HexagonPacketizerList::cleanUpDotCur() {
352 MachineInstr *MI = NULL;
353 for (auto BI : CurrentPacketMIs) {
354 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
355 if (BI->getOpcode() == Hexagon::V6_vL32b_cur_ai) {
356 MI = BI;
357 continue;
358 }
359 if (MI) {
360 for (auto &MO : BI->operands())
361 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
362 return;
363 }
364 }
365 if (!MI)
366 return;
367 // We did not find a use of the CUR, so de-cur it.
368 MI->setDesc(HII->get(Hexagon::V6_vL32b_ai));
369 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
370}
371
372// Check to see if an instruction can be dot cur.
373bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr *MI,
374 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
375 const TargetRegisterClass *RC) {
376 if (!HII->isV60VectorInstruction(MI))
377 return false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000378 if (!HII->isV60VectorInstruction(&*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000379 return false;
380
381 // Already a dot new instruction.
382 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
383 return false;
384
385 if (!HII->mayBeCurLoad(MI))
386 return false;
387
388 // The "cur value" cannot come from inline asm.
389 if (PacketSU->getInstr()->isInlineAsm())
390 return false;
391
392 // Make sure candidate instruction uses cur.
393 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
394 MI->dump();
395 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000396 MachineInstr &MJ = *MII;
397 DEBUG({
398 dbgs() << "Checking CUR against ";
399 MJ.dump();
400 });
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000401 unsigned DestReg = MI->getOperand(0).getReg();
402 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000403 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000404 if (MO.isReg() && MO.getReg() == DestReg)
405 FoundMatch = true;
406 if (!FoundMatch)
407 return false;
408
409 // Check for existing uses of a vector register within the packet which
410 // would be affected by converting a vector load into .cur formt.
411 for (auto BI : CurrentPacketMIs) {
412 DEBUG(dbgs() << "packet has "; BI->dump(););
413 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
414 return false;
415 }
416
417 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI->dump(););
418 // We can convert the opcode into a .cur.
419 return true;
420}
421
422// Promote an instruction to its .new form. At this time, we have already
423// made a call to canPromoteToDotNew and made sure that it can *indeed* be
424// promoted.
425bool HexagonPacketizerList::promoteToDotNew(MachineInstr* MI,
426 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
427 const TargetRegisterClass* RC) {
428 assert (DepType == SDep::Data);
429 int NewOpcode;
430 if (RC == &Hexagon::PredRegsRegClass)
431 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
432 else
433 NewOpcode = HII->getDotNewOp(MI);
434 MI->setDesc(HII->get(NewOpcode));
435 return true;
436}
437
438bool HexagonPacketizerList::demoteToDotOld(MachineInstr* MI) {
439 int NewOpcode = HII->getDotOldOp(MI->getOpcode());
440 MI->setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000441 return true;
442}
443
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000444bool HexagonPacketizerList::useCallersSP(MachineInstr *MI) {
445 unsigned Opc = MI->getOpcode();
446 switch (Opc) {
447 case Hexagon::S2_storerd_io:
448 case Hexagon::S2_storeri_io:
449 case Hexagon::S2_storerh_io:
450 case Hexagon::S2_storerb_io:
451 break;
452 default:
453 llvm_unreachable("Unexpected instruction");
454 }
455 unsigned FrameSize = MF.getFrameInfo()->getStackSize();
456 MachineOperand &Off = MI->getOperand(1);
457 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
458 if (HII->isValidOffset(Opc, NewOff)) {
459 Off.setImm(NewOff);
460 return true;
461 }
462 return false;
463}
464
465void HexagonPacketizerList::useCalleesSP(MachineInstr *MI) {
466 unsigned Opc = MI->getOpcode();
467 switch (Opc) {
468 case Hexagon::S2_storerd_io:
469 case Hexagon::S2_storeri_io:
470 case Hexagon::S2_storerh_io:
471 case Hexagon::S2_storerb_io:
472 break;
473 default:
474 llvm_unreachable("Unexpected instruction");
475 }
476 unsigned FrameSize = MF.getFrameInfo()->getStackSize();
477 MachineOperand &Off = MI->getOperand(1);
478 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
479}
480
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000481enum PredicateKind {
482 PK_False,
483 PK_True,
484 PK_Unknown
485};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000486
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000487/// Returns true if an instruction is predicated on p0 and false if it's
488/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000489static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000490 const HexagonInstrInfo *HII) {
491 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000492 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000493 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000494 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000495 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000496}
497
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000498static const MachineOperand &getPostIncrementOperand(const MachineInstr *MI,
499 const HexagonInstrInfo *HII) {
500 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000501#ifndef NDEBUG
502 // Post Increment means duplicates. Use dense map to find duplicates in the
503 // list. Caution: Densemap initializes with the minimum of 64 buckets,
504 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000505 DenseSet<unsigned> DefRegsSet;
506 for (auto &MO : MI->operands())
507 if (MO.isReg() && MO.isDef())
508 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000509
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000510 for (auto &MO : MI->operands())
511 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
512 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000513#else
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000514 if (MI->mayLoad()) {
Krzysztof Parzyszek4f9164d2015-12-16 20:07:04 +0000515 const MachineOperand &Op1 = MI->getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000516 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000517 assert(Op1.isReg() && "Post increment operand has be to a register.");
518 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000519 }
520 if (MI->getDesc().mayStore()) {
Krzysztof Parzyszek4f9164d2015-12-16 20:07:04 +0000521 const MachineOperand &Op0 = MI->getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000522 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000523 assert(Op0.isReg() && "Post increment operand has be to a register.");
524 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000525 }
526#endif
527 // we should never come here.
528 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
529}
530
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000531// Get the value being stored.
532static const MachineOperand& getStoreValueOperand(const MachineInstr *MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000533 // value being stored is always the last operand.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000534 return MI->getOperand(MI->getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000535}
536
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000537static bool isLoadAbsSet(const MachineInstr *MI) {
538 unsigned Opc = MI->getOpcode();
539 switch (Opc) {
540 case Hexagon::L4_loadrd_ap:
541 case Hexagon::L4_loadrb_ap:
542 case Hexagon::L4_loadrh_ap:
543 case Hexagon::L4_loadrub_ap:
544 case Hexagon::L4_loadruh_ap:
545 case Hexagon::L4_loadri_ap:
546 return true;
547 }
548 return false;
549}
550
551static const MachineOperand &getAbsSetOperand(const MachineInstr *MI) {
552 assert(isLoadAbsSet(MI));
553 return MI->getOperand(1);
554}
555
556
557// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000558// Following restrictions are to be respected in convert a store into
559// a new value store.
560// 1. If an instruction uses auto-increment, its address register cannot
561// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000562// 2. If an instruction uses absolute-set addressing mode, its address
563// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000564// 3. If an instruction produces a 64-bit result, its registers cannot be used
565// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000566// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000567// the instruction that uses the new-value register must also be conditional,
568// and both must always have their predicates evaluate identically.
569// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000570// 5. There is an implied restriction that a packet cannot have another store,
571// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000572// already a store in a packet, there can not be a new value store.
573// Arch Spec: 3.4.4.2
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000574bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr *MI,
575 const MachineInstr *PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000576 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000577 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000578 return false;
579
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000580 // Make sure there is dependency and can be new value'd.
581 const MachineOperand &Val = getStoreValueOperand(MI);
582 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000583 return false;
584
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000585 const MCInstrDesc& MCID = PacketMI->getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000586
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000587 // First operand is always the result.
588 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
589 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
590 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000591 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000592
593 // New-value stores are of class NV (slot 0), dual stores require class ST
594 // in slot 0 (PRM 5.5).
595 for (auto I : CurrentPacketMIs) {
596 SUnit *PacketSU = MIToSUnit.find(I)->second;
597 if (PacketSU->getInstr()->mayStore())
598 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000599 }
600
601 // Make sure it's NOT the post increment register that we are going to
602 // new value.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000603 if (HII->isPostIncrement(MI) &&
604 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000605 return false;
606 }
607
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000608 if (HII->isPostIncrement(PacketMI) && PacketMI->mayLoad() &&
609 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
610 // If source is post_inc, or absolute-set addressing, it can not feed
611 // into new value store
612 // r3 = memw(r2++#4)
613 // memw(r30 + #-1404) = r2.new -> can not be new value store
614 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000615 return false;
616 }
617
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000618 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
619 return false;
620
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000621 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000622 // also be predicated.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000623 if (HII->isPredicated(*PacketMI)) {
624 if (!HII->isPredicated(*MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000625 return false;
626
627 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000628 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000629 unsigned predRegNumSrc = 0;
630 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000631 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000632
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000633 // Get predicate register used in the source instruction.
634 for (auto &MO : PacketMI->operands()) {
635 if (!MO.isReg())
636 continue;
637 predRegNumSrc = MO.getReg();
638 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
639 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000640 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000641 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000642 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
643 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000644
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000645 // Get predicate register used in new-value store instruction.
646 for (auto &MO : MI->operands()) {
647 if (!MO.isReg())
648 continue;
649 predRegNumDst = MO.getReg();
650 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
651 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000652 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000653 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000654 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
655 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000656
657 // New-value register producer and user (store) need to satisfy these
658 // constraints:
659 // 1) Both instructions should be predicated on the same register.
660 // 2) If producer of the new-value register is .new predicated then store
661 // should also be .new predicated and if producer is not .new predicated
662 // then store should not be .new predicated.
663 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000664 // sense, i.e, either both should be negated or both should be non-negated.
665 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000666 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
667 getPredicateSense(*MI, HII) != getPredicateSense(*PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000668 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000669 }
670
671 // Make sure that other than the new-value register no other store instruction
672 // register has been modified in the same packet. Predicate registers can be
673 // modified by they should not be modified between the producer and the store
674 // instruction as it will make them both conditional on different values.
675 // We already know this to be true for all the instructions before and
676 // including PacketMI. Howerver, we need to perform the check for the
677 // remaining instructions in the packet.
678
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000679 unsigned StartCheck = 0;
680
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000681 for (auto I : CurrentPacketMIs) {
682 SUnit *TempSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000683 MachineInstr* TempMI = TempSU->getInstr();
684
685 // Following condition is true for all the instructions until PacketMI is
686 // reached (StartCheck is set to 0 before the for loop).
687 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000688 if (TempMI != PacketMI && !StartCheck) // Start processing only after
689 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000690
691 StartCheck = 1;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000692 if (TempMI == PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000693 continue;
694
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000695 for (auto &MO : MI->operands())
696 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000697 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000698 }
699
Alp Tokerf907b892013-12-05 05:44:44 +0000700 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000701 // 1. The only use of reg is DepReg and no other registers.
702 // This handles V4 base+index registers.
703 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000704 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000705 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000706 if (!HII->isPostIncrement(MI)) {
707 for (unsigned opNum = 0; opNum < MI->getNumOperands()-1; opNum++) {
708 const MachineOperand &MO = MI->getOperand(opNum);
709 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000710 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000711 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000712 }
713
714 // If data definition is because of implicit definition of the register,
715 // do not newify the store. Eg.
716 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
717 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
718 for (auto &MO : PacketMI->operands()) {
719 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
720 continue;
721 unsigned R = MO.getReg();
722 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
723 return false;
724 }
725
726 // Handle imp-use of super reg case. There is a target independent side
727 // change that should prevent this situation but I am handling it for
728 // just-in-case. For example, we cannot newify R2 in the following case:
729 // %R3<def> = A2_tfrsi 0;
730 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
731 for (auto &MO : MI->operands()) {
732 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
733 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000734 }
735
736 // Can be dot new store.
737 return true;
738}
739
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000740// Can this MI to promoted to either new value store or new value jump.
741bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr *MI,
742 const SUnit *PacketSU, unsigned DepReg,
743 MachineBasicBlock::iterator &MII) {
744 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000745 return false;
746
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000747 // Check to see the store can be new value'ed.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000748 MachineInstr *PacketMI = PacketSU->getInstr();
749 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000750 return true;
751
752 // Check to see the compare/jump can be new value'ed.
753 // This is done as a pass on its own. Don't need to check it here.
754 return false;
755}
756
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000757static bool isImplicitDependency(const MachineInstr *I, unsigned DepReg) {
758 for (auto &MO : I->operands())
759 if (MO.isReg() && MO.isDef() && (MO.getReg() == DepReg) && MO.isImplicit())
760 return true;
761 return false;
762}
763
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000764// Check to see if an instruction can be dot new
765// There are three kinds.
766// 1. dot new on predicate - V2/V3/V4
767// 2. dot new on stores NV/ST - V4
768// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000769bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr *MI,
770 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
771 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000772 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000773 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000774 return false;
775
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000776 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000777 return false;
778
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000779 const MachineInstr *PI = PacketSU->getInstr();
780
781 // The "new value" cannot come from inline asm.
782 if (PI->isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000783 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000784
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000785 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
786 // sense.
787 if (PI->isImplicitDef())
788 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000789
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000790 // If dependency is trough an implicitly defined register, we should not
791 // newify the use.
792 if (isImplicitDependency(PI, DepReg))
793 return false;
794
795 const MCInstrDesc& MCID = PI->getDesc();
796 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
797 if (DisableVecDblNVStores && VecRC == &Hexagon::VecDblRegsRegClass)
798 return false;
799
800 // predicate .new
801 // bug 5670: until that is fixed
802 // TODO: MI->isIndirectBranch() and IsRegisterJump(MI)
803 if (RC == &Hexagon::PredRegsRegClass)
804 if (HII->isCondInst(MI) || MI->isReturn())
805 return HII->predCanBeUsedAsDotNew(PI, DepReg);
806
807 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
808 return false;
809
810 // Create a dot new machine instruction to see if resources can be
811 // allocated. If not, bail out now.
812 int NewOpcode = HII->getDotNewOp(MI);
813 const MCInstrDesc &D = HII->get(NewOpcode);
814 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000815 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000816 MF.DeleteMachineInstr(NewMI);
817 if (!ResourcesAvailable)
818 return false;
819
820 // New Value Store only. New Value Jump generated as a separate pass.
821 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
822 return false;
823
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000824 return true;
825}
826
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000827// Go through the packet instructions and search for an anti dependency between
828// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000829// Trying to add
830// a) %R1<def> = TFRI_cdNotPt %P3, 2
831// to this packet:
832// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000833// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
834// c) %P3<def> = C2_tfrrp %R23
835// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000836// }
837// The P3 from a) and d) will be complements after
838// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000839// Anti-dep between c) and b) is irrelevant for this case
840bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr* MI,
841 unsigned DepReg) {
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000842 SUnit *PacketSUDep = MIToSUnit.find(MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000843
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000844 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000845 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000846 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000847 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000848
849 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000850 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000851
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000852 // Look at dependencies between current members of the packet and
853 // predicate defining instruction MI. Make sure that dependency is
854 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000855 if (PacketSU->isSucc(PacketSUDep)) {
856 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000857 auto &Dep = PacketSU->Succs[i];
858 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
859 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000860 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000861 }
862 }
863 }
864
865 return false;
866}
867
868
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000869/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000870static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000871 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000872 /// We use the following rule: The first predicate register that is a use is
873 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000874 assert(QII->isPredicated(MI) && "Must be predicated instruction");
875
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000876 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000877 if (Op.isReg() && Op.getReg() && Op.isUse() &&
878 Hexagon::PredRegsRegClass.contains(Op.getReg()))
879 return Op.getReg();
880 }
881
882 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000883 return 0;
884}
885
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000886// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000887// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000888bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
889 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000890 // If we don't know the predicate sense of the instructions bail out early, we
891 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000892 if (getPredicateSense(MI1, HII) == PK_Unknown ||
893 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000894 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000895
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000896 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000897 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000898
899 // One corner case deals with the following scenario:
900 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000901 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000902 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000903 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000904 // b) %R25<def> = A2_tfrf %P0, %R24
905 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000906 // }
907 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000908 // On general check a) and b) are complements, but presence of c) will
909 // convert a) to .new form, and then it is not a complement.
910 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000911
912 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000913 // Look for Anti dependecy on the same predicate reg as used in the
914 // candidate.
915 for (auto I : CurrentPacketMIs) {
916 // Scheduling Unit for current insn in the packet.
917 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000918
919 // If this instruction in the packet is succeeded by the candidate...
920 if (PacketSU->isSucc(SU)) {
921 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000922 auto Dep = PacketSU->Succs[i];
923 // The corner case exist when there is true data dependency between
924 // candidate and one of current packet members, this dep is on
925 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000926 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000927 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
928 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
929 // Here I know that I is predicate setting instruction with true
930 // data dep to candidate on the register we care about - c) in the
931 // above example. Now I need to see if there is an anti dependency
932 // from c) to any other instruction in the same packet on the pred
933 // reg of interest.
934 if (restrictingDepExistInPacket(I, Dep.getReg()))
935 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000936 }
937 }
938 }
939 }
940
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000941 // If the above case does not apply, check regular complement condition.
942 // Check that the predicate register is the same and that the predicate
943 // sense is different We also need to differentiate .old vs. .new: !p0
944 // is not complementary to p0.new.
945 unsigned PReg1 = getPredicatedRegister(MI1, HII);
946 unsigned PReg2 = getPredicatedRegister(MI2, HII);
947 return PReg1 == PReg2 &&
948 Hexagon::PredRegsRegClass.contains(PReg1) &&
949 Hexagon::PredRegsRegClass.contains(PReg2) &&
950 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000951 HII->isDotNewInst(&MI1) == HII->isDotNewInst(&MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000952}
953
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000954// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000955void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000956 Dependence = false;
957 PromotedToDotNew = false;
958 GlueToNewValueJump = false;
959 GlueAllocframeStore = false;
960 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000961}
962
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000963// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000964bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
965 const MachineBasicBlock *) {
966 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000967 return true;
968
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000969 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000970 return false;
971
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000972 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000973 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000974 return false;
975
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000976 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000977 return false;
978
979 // We check if MI has any functional units mapped to it. If it doesn't,
980 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000981 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000982 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +0000983 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000984 return !FuncUnits;
985}
986
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000987bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
988 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000989 return true;
990
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000991 // Consider inline asm to not be a solo instruction by default.
992 // Inline asm will be put in a packet temporarily, but then it will be
993 // removed, and placed outside of the packet (before or after, depending
994 // on dependencies). This is to reduce the impact of inline asm as a
995 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000996 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000997 return true;
998
999 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
1000 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
1001 // They must not be grouped with other instructions in a packet.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001002 if (isSchedBarrier(&MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001003 return true;
1004
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001005 if (HII->isSolo(&MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001006 return true;
1007
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001008 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001009 return true;
1010
1011 return false;
1012}
1013
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001014
1015// Quick check if instructions MI and MJ cannot coexist in the same packet.
1016// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1017// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1018// For full test call this function twice:
1019// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1020// Doing the test only one way saves the amount of code in this function,
1021// since every test would need to be repeated with the MI and MJ reversed.
1022static bool cannotCoexistAsymm(const MachineInstr *MI, const MachineInstr *MJ,
1023 const HexagonInstrInfo &HII) {
1024 const MachineFunction *MF = MI->getParent()->getParent();
1025 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1026 HII.isHVXMemWithAIndirect(MI, MJ))
1027 return true;
1028
1029 // An inline asm cannot be together with a branch, because we may not be
1030 // able to remove the asm out after packetizing (i.e. if the asm must be
1031 // moved past the bundle). Similarly, two asms cannot be together to avoid
1032 // complications when determining their relative order outside of a bundle.
1033 if (MI->isInlineAsm())
1034 return MJ->isInlineAsm() || MJ->isBranch() || MJ->isBarrier() ||
1035 MJ->isCall() || MJ->isTerminator();
1036
1037 // "False" really means that the quick check failed to determine if
1038 // I and J cannot coexist.
1039 return false;
1040}
1041
1042
1043// Full, symmetric check.
1044bool HexagonPacketizerList::cannotCoexist(const MachineInstr *MI,
1045 const MachineInstr *MJ) {
1046 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1047}
1048
1049void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1050 for (auto &B : MF) {
1051 MachineBasicBlock::iterator BundleIt;
1052 MachineBasicBlock::instr_iterator NextI;
1053 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1054 NextI = std::next(I);
1055 MachineInstr *MI = &*I;
1056 if (MI->isBundle())
1057 BundleIt = I;
1058 if (!MI->isInsideBundle())
1059 continue;
1060
1061 // Decide on where to insert the instruction that we are pulling out.
1062 // Debug instructions always go before the bundle, but the placement of
1063 // INLINE_ASM depends on potential dependencies. By default, try to
1064 // put it before the bundle, but if the asm writes to a register that
1065 // other instructions in the bundle read, then we need to place it
1066 // after the bundle (to preserve the bundle semantics).
1067 bool InsertBeforeBundle;
1068 if (MI->isInlineAsm())
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001069 InsertBeforeBundle = !hasWriteToReadDep(*MI, *BundleIt, HRI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001070 else if (MI->isDebugValue())
1071 InsertBeforeBundle = true;
1072 else
1073 continue;
1074
1075 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1076 }
1077 }
1078}
1079
1080// Check if a given instruction is of class "system".
1081static bool isSystemInstr(const MachineInstr *MI) {
1082 unsigned Opc = MI->getOpcode();
1083 switch (Opc) {
1084 case Hexagon::Y2_barrier:
1085 case Hexagon::Y2_dcfetchbo:
1086 return true;
1087 }
1088 return false;
1089}
1090
1091bool HexagonPacketizerList::hasDeadDependence(const MachineInstr *I,
1092 const MachineInstr *J) {
1093 // The dependence graph may not include edges between dead definitions,
1094 // so without extra checks, we could end up packetizing two instruction
1095 // defining the same (dead) register.
1096 if (I->isCall() || J->isCall())
1097 return false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001098 if (HII->isPredicated(*I) || HII->isPredicated(*J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001099 return false;
1100
1101 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
1102 for (auto &MO : I->operands()) {
1103 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1104 continue;
1105 DeadDefs[MO.getReg()] = true;
1106 }
1107
1108 for (auto &MO : J->operands()) {
1109 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1110 continue;
1111 unsigned R = MO.getReg();
1112 if (R != Hexagon::USR_OVF && DeadDefs[R])
1113 return true;
1114 }
1115 return false;
1116}
1117
1118bool HexagonPacketizerList::hasControlDependence(const MachineInstr *I,
1119 const MachineInstr *J) {
1120 // A save callee-save register function call can only be in a packet
1121 // with instructions that don't write to the callee-save registers.
1122 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1123 doesModifyCalleeSavedReg(J, HRI)) ||
1124 (HII->isSaveCalleeSavedRegsCall(J) &&
1125 doesModifyCalleeSavedReg(I, HRI)))
1126 return true;
1127
1128 // Two control flow instructions cannot go in the same packet.
1129 if (isControlFlow(I) && isControlFlow(J))
1130 return true;
1131
1132 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1133 // contain a speculative indirect jump,
1134 // a new-value compare jump or a dealloc_return.
1135 auto isBadForLoopN = [this] (const MachineInstr *MI) -> bool {
1136 if (MI->isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
1137 return true;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001138 if (HII->isPredicated(*MI) && HII->isPredicatedNew(*MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001139 return true;
1140 return false;
1141 };
1142
1143 if (HII->isLoopN(I) && isBadForLoopN(J))
1144 return true;
1145 if (HII->isLoopN(J) && isBadForLoopN(I))
1146 return true;
1147
1148 // dealloc_return cannot appear in the same packet as a conditional or
1149 // unconditional jump.
1150 return HII->isDeallocRet(I) &&
1151 (J->isBranch() || J->isCall() || J->isBarrier());
1152}
1153
1154bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr *I,
1155 const MachineInstr *J) {
1156 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
1157 bool StoreI = I->mayStore(), StoreJ = J->mayStore();
1158 if ((SysI && StoreJ) || (SysJ && StoreI))
1159 return true;
1160
1161 if (StoreI && StoreJ) {
1162 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1163 return true;
1164 } else {
1165 // A memop cannot be in the same packet with another memop or a store.
1166 // Two stores can be together, but here I and J cannot both be stores.
1167 bool MopStI = HII->isMemOp(I) || StoreI;
1168 bool MopStJ = HII->isMemOp(J) || StoreJ;
1169 if (MopStI && MopStJ)
1170 return true;
1171 }
1172
1173 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1174}
1175
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001176// SUI is the current instruction that is out side of the current packet.
1177// SUJ is the current instruction inside the current packet against which that
1178// SUI will be packetized.
1179bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
1180 MachineInstr *I = SUI->getInstr();
1181 MachineInstr *J = SUJ->getInstr();
1182 assert(I && J && "Unable to packetize null instruction!");
1183
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001184 // Clear IgnoreDepMIs when Packet starts.
1185 if (CurrentPacketMIs.size() == 1)
1186 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001187
1188 MachineBasicBlock::iterator II = I;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001189
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001190 // Solo instructions cannot go in the packet.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001191 assert(!isSoloInstruction(*I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001192
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001193 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001194 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001195
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001196 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1197 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001198 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001199
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001200 // V4 allows dual stores. It does not allow second store, if the first
1201 // store is not in SLOT0. New value store, new value jump, dealloc_return
1202 // and memop always take SLOT0. Arch spec 3.4.4.2.
1203 Dependence = hasV4SpecificDependence(I, J);
1204 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001205 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001206
1207 // If an instruction feeds new value jump, glue it.
1208 MachineBasicBlock::iterator NextMII = I;
1209 ++NextMII;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001210 if (NextMII != I->getParent()->end() && HII->isNewValueJump(&*NextMII)) {
1211 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001212
1213 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001214 const MachineOperand &NOp0 = NextMI.getOperand(0);
1215 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001216
Krzysztof Parzyszek4f9164d2015-12-16 20:07:04 +00001217 if (NOp1.isReg() && I->getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001218 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001219
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001220 for (auto I : CurrentPacketMIs) {
1221 SUnit *PacketSU = MIToSUnit.find(I)->second;
1222 MachineInstr *PI = PacketSU->getInstr();
1223 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
1224 if (PI->isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001225 Dependence = true;
1226 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001227 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001228 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001229 // 1. Packet does not have a store in it.
1230 // 2. If the first operand of the nvj is newified, and the second
1231 // operand is also a reg, it (second reg) is not defined in
1232 // the same packet.
1233 // 3. If the second operand of the nvj is newified, (which means
1234 // first operand is also a reg), first reg is not defined in
1235 // the same packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001236 if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() ||
1237 HII->isLoopN(PI)) {
1238 Dependence = true;
1239 break;
1240 }
1241 // Check #2/#3.
1242 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
1243 if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001244 Dependence = true;
1245 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001246 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001247 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001248
1249 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001250 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001251 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001252 }
1253
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001254 // There no dependency between a prolog instruction and its successor.
1255 if (!SUJ->isSucc(SUI))
1256 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001257
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001258 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1259 if (FoundSequentialDependence)
1260 break;
1261
1262 if (SUJ->Succs[i].getSUnit() != SUI)
1263 continue;
1264
1265 SDep::Kind DepType = SUJ->Succs[i].getKind();
1266 // For direct calls:
1267 // Ignore register dependences for call instructions for packetization
1268 // purposes except for those due to r31 and predicate registers.
1269 //
1270 // For indirect calls:
1271 // Same as direct calls + check for true dependences to the register
1272 // used in the indirect call.
1273 //
1274 // We completely ignore Order dependences for call instructions.
1275 //
1276 // For returns:
1277 // Ignore register dependences for return instructions like jumpr,
1278 // dealloc return unless we have dependencies on the explicit uses
1279 // of the registers used by jumpr (like r31) or dealloc return
1280 // (like r29 or r30).
1281 //
1282 // TODO: Currently, jumpr is handling only return of r31. So, the
1283 // following logic (specificaly isCallDependent) is working fine.
1284 // We need to enable jumpr for register other than r31 and then,
1285 // we need to rework the last part, where it handles indirect call
1286 // of that (isCallDependent) function. Bug 6216 is opened for this.
1287 unsigned DepReg = 0;
1288 const TargetRegisterClass *RC = nullptr;
1289 if (DepType == SDep::Data) {
1290 DepReg = SUJ->Succs[i].getReg();
1291 RC = HRI->getMinimalPhysRegClass(DepReg);
1292 }
1293
Krzysztof Parzyszekecea07c2016-07-14 19:30:55 +00001294 if (I->isCall() || I->isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001295 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001296 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001297 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1298 continue;
1299 }
1300
1301 if (DepType == SDep::Data) {
1302 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1303 if (promoteToDotCur(J, DepType, II, RC))
1304 continue;
1305 }
1306
1307 // Data dpendence ok if we have load.cur.
1308 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
1309 if (HII->isV60VectorInstruction(I))
1310 continue;
1311 }
1312
1313 // For instructions that can be promoted to dot-new, try to promote.
1314 if (DepType == SDep::Data) {
1315 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1316 if (promoteToDotNew(I, DepType, II, RC)) {
1317 PromotedToDotNew = true;
1318 continue;
1319 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001320 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001321 if (HII->isNewValueJump(I))
1322 continue;
1323 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001324
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001325 // For predicated instructions, if the predicates are complements then
1326 // there can be no dependence.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001327 if (HII->isPredicated(*I) && HII->isPredicated(*J) &&
1328 arePredicatesComplements(*I, *J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001329 // Not always safe to do this translation.
1330 // DAG Builder attempts to reduce dependence edges using transitive
1331 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001332 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001333 // r0 = tfr_pt ... (1)
1334 // r0 = tfr_pf ... (2)
1335 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001336 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001337 // There will be an output dependence between (1)->(2) and (2)->(3).
1338 // However, there is no dependence edge between (1)->(3). This results
1339 // in all 3 instructions going in the same packet. We ignore dependce
1340 // only once to avoid this situation.
1341 auto Itr = std::find(IgnoreDepMIs.begin(), IgnoreDepMIs.end(), J);
1342 if (Itr != IgnoreDepMIs.end()) {
1343 Dependence = true;
1344 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001345 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001346 IgnoreDepMIs.push_back(I);
1347 continue;
1348 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001349
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001350 // Ignore Order dependences between unconditional direct branches
1351 // and non-control-flow instructions.
1352 if (isDirectJump(I) && !J->isBranch() && !J->isCall() &&
1353 DepType == SDep::Order)
1354 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001355
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001356 // Ignore all dependences for jumps except for true and output
1357 // dependences.
1358 if (I->isConditionalBranch() && DepType != SDep::Data &&
1359 DepType != SDep::Output)
1360 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001361
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001362 // Ignore output dependences due to superregs. We can write to two
1363 // different subregisters of R1:0 for instance in the same cycle.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001364
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001365 // If neither I nor J defines DepReg, then this is a superfluous output
1366 // dependence. The dependence must be of the form:
1367 // R0 = ...
1368 // R1 = ...
1369 // and there is an output dependence between the two instructions with
1370 // DepReg = D0.
1371 // We want to ignore these dependences. Ideally, the dependence
1372 // constructor should annotate such dependences. We can then avoid this
1373 // relatively expensive check.
1374 //
1375 if (DepType == SDep::Output) {
1376 // DepReg is the register that's responsible for the dependence.
1377 unsigned DepReg = SUJ->Succs[i].getReg();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001378
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001379 // Check if I and J really defines DepReg.
1380 if (!I->definesRegister(DepReg) && !J->definesRegister(DepReg))
1381 continue;
1382 FoundSequentialDependence = true;
1383 break;
1384 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001385
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001386 // For Order dependences:
1387 // 1. On V4 or later, volatile loads/stores can be packetized together,
1388 // unless other rules prevent is.
1389 // 2. Store followed by a load is not allowed.
1390 // 3. Store followed by a store is only valid on V4 or later.
1391 // 4. Load followed by any memory operation is allowed.
1392 if (DepType == SDep::Order) {
1393 if (!PacketizeVolatiles) {
1394 bool OrdRefs = I->hasOrderedMemoryRef() || J->hasOrderedMemoryRef();
1395 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001396 FoundSequentialDependence = true;
1397 break;
1398 }
1399 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001400 // J is first, I is second.
1401 bool LoadJ = J->mayLoad(), StoreJ = J->mayStore();
1402 bool LoadI = I->mayLoad(), StoreI = I->mayStore();
1403 if (StoreJ) {
1404 // Two stores are only allowed on V4+. Load following store is never
1405 // allowed.
1406 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001407 FoundSequentialDependence = true;
1408 break;
1409 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001410 } else if (!LoadJ || (!LoadI && !StoreI)) {
1411 // If J is neither load nor store, assume a dependency.
1412 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001413 FoundSequentialDependence = true;
1414 break;
1415 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001416 // Store followed by store: not OK on V2.
1417 // Store followed by load: not OK on all.
1418 // Load followed by store: OK on all.
1419 // Load followed by load: OK on all.
1420 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001421 }
1422
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001423 // For V4, special case ALLOCFRAME. Even though there is dependency
1424 // between ALLOCFRAME and subsequent store, allow it to be packetized
1425 // in a same packet. This implies that the store is using the caller's
1426 // SP. Hence, offset needs to be updated accordingly.
1427 if (DepType == SDep::Data && J->getOpcode() == Hexagon::S2_allocframe) {
1428 unsigned Opc = I->getOpcode();
1429 switch (Opc) {
1430 case Hexagon::S2_storerd_io:
1431 case Hexagon::S2_storeri_io:
1432 case Hexagon::S2_storerh_io:
1433 case Hexagon::S2_storerb_io:
1434 if (I->getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001435 // Since this store is to be glued with allocframe in the same
1436 // packet, it will use SP of the previous stack frame, i.e.
1437 // caller's SP. Therefore, we need to recalculate offset
1438 // according to this change.
1439 GlueAllocframeStore = useCallersSP(I);
1440 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001441 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001442 }
1443 default:
1444 break;
1445 }
1446 }
1447
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001448 // There are certain anti-dependencies that cannot be ignored.
1449 // Specifically:
1450 // J2_call ... %R0<imp-def> ; SUJ
1451 // R0 = ... ; SUI
1452 // Those cannot be packetized together, since the call will observe
1453 // the effect of the assignment to R0.
1454 if (DepType == SDep::Anti && J->isCall()) {
1455 // Check if I defines any volatile register. We should also check
1456 // registers that the call may read, but these happen to be a
1457 // subset of the volatile register set.
1458 for (const MCPhysReg *P = J->getDesc().ImplicitDefs; P && *P; ++P) {
1459 if (!I->modifiesRegister(*P, HRI))
1460 continue;
1461 FoundSequentialDependence = true;
1462 break;
1463 }
1464 }
1465
1466 // Skip over remaining anti-dependences. Two instructions that are
1467 // anti-dependent can share a packet, since in most such cases all
1468 // operands are read before any modifications take place.
1469 // The exceptions are branch and call instructions, since they are
1470 // executed after all other instructions have completed (at least
1471 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001472 if (DepType != SDep::Anti) {
1473 FoundSequentialDependence = true;
1474 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001475 }
1476 }
1477
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001478 if (FoundSequentialDependence) {
1479 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001480 return false;
1481 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001482
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001483 return true;
1484}
1485
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001486bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
1487 MachineInstr *I = SUI->getInstr();
1488 MachineInstr *J = SUJ->getInstr();
1489 assert(I && J && "Unable to packetize null instruction!");
1490
1491 if (cannotCoexist(I, J))
1492 return false;
1493
1494 if (!Dependence)
1495 return true;
1496
1497 // Check if the instruction was promoted to a dot-new. If so, demote it
1498 // back into a dot-old.
1499 if (PromotedToDotNew)
1500 demoteToDotOld(I);
1501
1502 cleanUpDotCur();
1503 // Check if the instruction (must be a store) was glued with an allocframe
1504 // instruction. If so, restore its offset to its original value, i.e. use
1505 // current SP instead of caller's SP.
1506 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001507 useCalleesSP(I);
1508 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001509 }
1510 return false;
1511}
1512
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001513MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001514HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001515 MachineBasicBlock::iterator MII = MI;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001516 MachineBasicBlock *MBB = MI.getParent();
1517 if (MI.isImplicitDef()) {
1518 unsigned R = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001519 if (Hexagon::IntRegsRegClass.contains(R)) {
1520 MCSuperRegIterator S(R, HRI, false);
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001521 MI.addOperand(MachineOperand::CreateReg(*S, true, true));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001522 }
1523 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001524 }
1525 assert(ResourceTracker->canReserveResources(MI));
1526
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001527 bool ExtMI = HII->isExtended(&MI) || HII->isConstExtended(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001528 bool Good = true;
1529
1530 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001531 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001532 // We need to put both instructions in the same packet: MI and NvjMI.
1533 // Either of them can require a constant extender. Try to add both to
1534 // the current packet, and if that fails, end the packet and start a
1535 // new one.
1536 ResourceTracker->reserveResources(MI);
1537 if (ExtMI)
1538 Good = tryAllocateResourcesForConstExt(true);
1539
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001540 bool ExtNvjMI = HII->isExtended(&NvjMI) || HII->isConstExtended(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001541 if (Good) {
1542 if (ResourceTracker->canReserveResources(NvjMI))
1543 ResourceTracker->reserveResources(NvjMI);
1544 else
1545 Good = false;
1546 }
1547 if (Good && ExtNvjMI)
1548 Good = tryAllocateResourcesForConstExt(true);
1549
1550 if (!Good) {
1551 endPacket(MBB, MI);
1552 assert(ResourceTracker->canReserveResources(MI));
1553 ResourceTracker->reserveResources(MI);
1554 if (ExtMI) {
1555 assert(canReserveResourcesForConstExt());
1556 tryAllocateResourcesForConstExt(true);
1557 }
1558 assert(ResourceTracker->canReserveResources(NvjMI));
1559 ResourceTracker->reserveResources(NvjMI);
1560 if (ExtNvjMI) {
1561 assert(canReserveResourcesForConstExt());
1562 reserveResourcesForConstExt();
1563 }
1564 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001565 CurrentPacketMIs.push_back(&MI);
1566 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001567 return MII;
1568 }
1569
1570 ResourceTracker->reserveResources(MI);
1571 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1572 endPacket(MBB, MI);
1573 if (PromotedToDotNew)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001574 demoteToDotOld(&MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001575 if (GlueAllocframeStore) {
1576 useCalleesSP(&MI);
1577 GlueAllocframeStore = false;
1578 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001579 ResourceTracker->reserveResources(MI);
1580 reserveResourcesForConstExt();
1581 }
1582
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001583 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001584 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001585}
1586
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001587void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001588 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001589 OldPacketMIs = CurrentPacketMIs;
1590 VLIWPacketizerList::endPacket(MBB, MI);
1591}
1592
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001593bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
1594 return !producesStall(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001595}
1596
1597
1598// Return true when ConsMI uses a register defined by ProdMI.
1599static bool isDependent(const MachineInstr *ProdMI,
1600 const MachineInstr *ConsMI) {
1601 if (!ProdMI->getOperand(0).isReg())
1602 return false;
1603 unsigned DstReg = ProdMI->getOperand(0).getReg();
1604
1605 for (auto &Op : ConsMI->operands())
1606 if (Op.isReg() && Op.isUse() && Op.getReg() == DstReg)
1607 // The MIs depend on each other.
1608 return true;
1609
1610 return false;
1611}
1612
1613// V60 forward scheduling.
1614bool HexagonPacketizerList::producesStall(const MachineInstr *I) {
1615 // Check whether the previous packet is in a different loop. If this is the
1616 // case, there is little point in trying to avoid a stall because that would
1617 // favor the rare case (loop entry) over the common case (loop iteration).
1618 //
1619 // TODO: We should really be able to check all the incoming edges if this is
1620 // the first packet in a basic block, so we can avoid stalls from the loop
1621 // backedge.
1622 if (!OldPacketMIs.empty()) {
1623 auto *OldBB = OldPacketMIs.front()->getParent();
1624 auto *ThisBB = I->getParent();
1625 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1626 return false;
1627 }
1628
1629 // Check for stall between two vector instructions.
1630 if (HII->isV60VectorInstruction(I)) {
1631 for (auto J : OldPacketMIs) {
1632 if (!HII->isV60VectorInstruction(J))
1633 continue;
1634 if (isDependent(J, I) && !HII->isVecUsableNextPacket(J, I))
1635 return true;
1636 }
1637 return false;
1638 }
1639
1640 // Check for stall between two scalar instructions. First, check that
1641 // there is no definition of a use in the current packet, because it
1642 // may be a candidate for .new.
1643 for (auto J : CurrentPacketMIs)
1644 if (!HII->isV60VectorInstruction(J) && isDependent(J, I))
1645 return false;
1646
1647 // Check for stall between I and instructions in the previous packet.
1648 if (MF.getSubtarget<HexagonSubtarget>().useBSBScheduling()) {
1649 for (auto J : OldPacketMIs) {
1650 if (HII->isV60VectorInstruction(J))
1651 continue;
1652 if (!HII->isLateInstrFeedsEarlyInstr(J, I))
1653 continue;
1654 if (isDependent(J, I) && !HII->canExecuteInBundle(J, I))
1655 return true;
1656 }
1657 }
1658
1659 return false;
1660}
1661
1662
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001663//===----------------------------------------------------------------------===//
1664// Public Constructor Functions
1665//===----------------------------------------------------------------------===//
1666
1667FunctionPass *llvm::createHexagonPacketizer() {
1668 return new HexagonPacketizer();
1669}