blob: 7051ee32abf89eb27fb310a2b61ab38dda6d09b9 [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
104
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000105HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
106 MachineLoopInfo &MLI, AliasAnalysis *AA,
107 const MachineBranchProbabilityInfo *MBPI)
108 : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI) {
109 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
110 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000111}
112
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000113// Check if FirstI modifies a register that SecondI reads.
114static bool hasWriteToReadDep(const MachineInstr *FirstI,
115 const MachineInstr *SecondI, const TargetRegisterInfo *TRI) {
116 for (auto &MO : FirstI->operands()) {
117 if (!MO.isReg() || !MO.isDef())
118 continue;
119 unsigned R = MO.getReg();
120 if (SecondI->readsRegister(R, TRI))
121 return true;
122 }
123 return false;
124}
125
126
127static MachineBasicBlock::iterator moveInstrOut(MachineInstr *MI,
128 MachineBasicBlock::iterator BundleIt, bool Before) {
129 MachineBasicBlock::instr_iterator InsertPt;
130 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000131 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000132 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000133 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000134
135 MachineBasicBlock &B = *MI->getParent();
136 // The instruction should at least be bundled with the preceding instruction
137 // (there will always be one, i.e. BUNDLE, if nothing else).
138 assert(MI->isBundledWithPred());
139 if (MI->isBundledWithSucc()) {
140 MI->clearFlag(MachineInstr::BundledSucc);
141 MI->clearFlag(MachineInstr::BundledPred);
142 } else {
143 // If it's not bundled with the successor (i.e. it is the last one
144 // in the bundle), then we can simply unbundle it from the predecessor,
145 // which will take care of updating the predecessor's flag.
146 MI->unbundleFromPred();
147 }
148 B.splice(InsertPt, &B, MI);
149
150 // Get the size of the bundle without asserting.
151 MachineBasicBlock::const_instr_iterator I(BundleIt);
152 MachineBasicBlock::const_instr_iterator E = B.instr_end();
153 unsigned Size = 0;
154 for (++I; I != E && I->isBundledWithPred(); ++I)
155 ++Size;
156
157 // If there are still two or more instructions, then there is nothing
158 // else to be done.
159 if (Size > 1)
160 return BundleIt;
161
162 // Otherwise, extract the single instruction out and delete the bundle.
163 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
164 MachineInstr *SingleI = BundleIt->getNextNode();
165 SingleI->unbundleFromPred();
166 assert(!SingleI->isBundledWithSucc());
167 BundleIt->eraseFromParent();
168 return NextIt;
169}
170
171
172bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000173 if (DisablePacketizer || skipFunction(*MF.getFunction()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000174 return false;
175
176 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
177 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
178 auto &MLI = getAnalysis<MachineLoopInfo>();
179 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
180 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
181
182 if (EnableGenAllInsnClass)
183 HII->genAllInsnTimingClasses(MF);
184
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000185 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000186 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000187
188 // DFA state table should not be empty.
189 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
190
191 //
192 // Loop over all basic blocks and remove KILL pseudo-instructions
193 // These instructions confuse the dependence analysis. Consider:
194 // D0 = ... (Insn 0)
195 // R0 = KILL R0, D0 (Insn 1)
196 // R0 = ... (Insn 2)
197 // Here, Insn 1 will result in the dependence graph not emitting an output
198 // dependence between Insn 0 and Insn 2. This can lead to incorrect
199 // packetization
200 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000201 for (auto &MB : MF) {
202 auto End = MB.end();
203 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000204 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000205 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000206 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000207 MB.erase(MI);
208 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000209 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000210 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000211 }
212 }
213
214 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000215 for (auto &MB : MF) {
216 auto Begin = MB.begin(), End = MB.end();
217 while (Begin != End) {
218 // First the first non-boundary starting from the end of the last
219 // scheduling region.
220 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000221 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000222 ++RB;
223 // First the first boundary starting from the beginning of the new
224 // region.
225 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000226 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000227 ++RE;
228 // Add the scheduling boundary if it's not block end.
229 if (RE != End)
230 ++RE;
231 // If RB == End, then RE == End.
232 if (RB != End)
233 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000234
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000235 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000236 }
237 }
238
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000239 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000240 return true;
241}
242
243
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000244// Reserve resources for a constant extender. Trigger an assertion if the
245// reservation fails.
246void HexagonPacketizerList::reserveResourcesForConstExt() {
247 if (!tryAllocateResourcesForConstExt(true))
248 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000249}
250
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000251bool HexagonPacketizerList::canReserveResourcesForConstExt() {
252 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000253}
254
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000255// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
256// return true, otherwise, return false.
257bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
258 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000259 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000260 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000261 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000262 MF.DeleteMachineInstr(ExtMI);
263 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000264}
265
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000266
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000267bool HexagonPacketizerList::isCallDependent(const MachineInstr* MI,
268 SDep::Kind DepType, unsigned DepReg) {
269 // Check for LR dependence.
270 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000271 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000272
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000273 if (HII->isDeallocRet(MI))
274 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000275 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000276
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000277 // Check if this is a predicate dependence.
278 const TargetRegisterClass* RC = HRI->getMinimalPhysRegClass(DepReg);
279 if (RC == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000280 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000281
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000282 // Assumes that the first operand of the CALLr is the function address.
283 if (HII->isIndirectCall(MI) && (DepType == SDep::Data)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000284 MachineOperand MO = MI->getOperand(0);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000285 if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000286 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000287 }
288
289 return false;
290}
291
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000292static bool isRegDependence(const SDep::Kind DepType) {
293 return DepType == SDep::Data || DepType == SDep::Anti ||
294 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000295}
296
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000297static bool isDirectJump(const MachineInstr* MI) {
298 return MI->getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000299}
300
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000301static bool isSchedBarrier(const MachineInstr* MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000302 switch (MI->getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000303 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000304 return true;
305 }
306 return false;
307}
308
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000309static bool isControlFlow(const MachineInstr* MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000310 return (MI->getDesc().isTerminator() || MI->getDesc().isCall());
311}
312
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000313
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000314/// Returns true if the instruction modifies a callee-saved register.
315static bool doesModifyCalleeSavedReg(const MachineInstr *MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000316 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000317 const MachineFunction &MF = *MI->getParent()->getParent();
318 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
319 if (MI->modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000320 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000321 return false;
322}
323
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000324// TODO: MI->isIndirectBranch() and IsRegisterJump(MI)
325// Returns true if an instruction can be promoted to .new predicate or
326// new-value store.
327bool HexagonPacketizerList::isNewifiable(const MachineInstr* MI) {
328 return HII->isCondInst(MI) || MI->isReturn() || HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000329}
330
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000331// Promote an instructiont to its .cur form.
332// At this time, we have already made a call to canPromoteToDotCur and made
333// sure that it can *indeed* be promoted.
334bool HexagonPacketizerList::promoteToDotCur(MachineInstr* MI,
335 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
336 const TargetRegisterClass* RC) {
337 assert(DepType == SDep::Data);
338 int CurOpcode = HII->getDotCurOp(MI);
339 MI->setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000340 return true;
341}
342
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000343void HexagonPacketizerList::cleanUpDotCur() {
344 MachineInstr *MI = NULL;
345 for (auto BI : CurrentPacketMIs) {
346 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
347 if (BI->getOpcode() == Hexagon::V6_vL32b_cur_ai) {
348 MI = BI;
349 continue;
350 }
351 if (MI) {
352 for (auto &MO : BI->operands())
353 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
354 return;
355 }
356 }
357 if (!MI)
358 return;
359 // We did not find a use of the CUR, so de-cur it.
360 MI->setDesc(HII->get(Hexagon::V6_vL32b_ai));
361 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
362}
363
364// Check to see if an instruction can be dot cur.
365bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr *MI,
366 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
367 const TargetRegisterClass *RC) {
368 if (!HII->isV60VectorInstruction(MI))
369 return false;
370 if (!HII->isV60VectorInstruction(MII))
371 return false;
372
373 // Already a dot new instruction.
374 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
375 return false;
376
377 if (!HII->mayBeCurLoad(MI))
378 return false;
379
380 // The "cur value" cannot come from inline asm.
381 if (PacketSU->getInstr()->isInlineAsm())
382 return false;
383
384 // Make sure candidate instruction uses cur.
385 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
386 MI->dump();
387 dbgs() << "in packet\n";);
388 MachineInstr *MJ = MII;
389 DEBUG(dbgs() << "Checking CUR against "; MJ->dump(););
390 unsigned DestReg = MI->getOperand(0).getReg();
391 bool FoundMatch = false;
392 for (auto &MO : MJ->operands())
393 if (MO.isReg() && MO.getReg() == DestReg)
394 FoundMatch = true;
395 if (!FoundMatch)
396 return false;
397
398 // Check for existing uses of a vector register within the packet which
399 // would be affected by converting a vector load into .cur formt.
400 for (auto BI : CurrentPacketMIs) {
401 DEBUG(dbgs() << "packet has "; BI->dump(););
402 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
403 return false;
404 }
405
406 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI->dump(););
407 // We can convert the opcode into a .cur.
408 return true;
409}
410
411// Promote an instruction to its .new form. At this time, we have already
412// made a call to canPromoteToDotNew and made sure that it can *indeed* be
413// promoted.
414bool HexagonPacketizerList::promoteToDotNew(MachineInstr* MI,
415 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
416 const TargetRegisterClass* RC) {
417 assert (DepType == SDep::Data);
418 int NewOpcode;
419 if (RC == &Hexagon::PredRegsRegClass)
420 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
421 else
422 NewOpcode = HII->getDotNewOp(MI);
423 MI->setDesc(HII->get(NewOpcode));
424 return true;
425}
426
427bool HexagonPacketizerList::demoteToDotOld(MachineInstr* MI) {
428 int NewOpcode = HII->getDotOldOp(MI->getOpcode());
429 MI->setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000430 return true;
431}
432
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000433enum PredicateKind {
434 PK_False,
435 PK_True,
436 PK_Unknown
437};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000438
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000439/// Returns true if an instruction is predicated on p0 and false if it's
440/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000441static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000442 const HexagonInstrInfo *HII) {
443 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000444 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000445 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000446 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000447 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000448}
449
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000450static const MachineOperand &getPostIncrementOperand(const MachineInstr *MI,
451 const HexagonInstrInfo *HII) {
452 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000453#ifndef NDEBUG
454 // Post Increment means duplicates. Use dense map to find duplicates in the
455 // list. Caution: Densemap initializes with the minimum of 64 buckets,
456 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000457 DenseSet<unsigned> DefRegsSet;
458 for (auto &MO : MI->operands())
459 if (MO.isReg() && MO.isDef())
460 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000461
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000462 for (auto &MO : MI->operands())
463 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
464 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000465#else
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000466 if (MI->mayLoad()) {
Krzysztof Parzyszek4f9164d2015-12-16 20:07:04 +0000467 const MachineOperand &Op1 = MI->getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000468 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000469 assert(Op1.isReg() && "Post increment operand has be to a register.");
470 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000471 }
472 if (MI->getDesc().mayStore()) {
Krzysztof Parzyszek4f9164d2015-12-16 20:07:04 +0000473 const MachineOperand &Op0 = MI->getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000474 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000475 assert(Op0.isReg() && "Post increment operand has be to a register.");
476 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000477 }
478#endif
479 // we should never come here.
480 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
481}
482
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000483// Get the value being stored.
484static const MachineOperand& getStoreValueOperand(const MachineInstr *MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000485 // value being stored is always the last operand.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000486 return MI->getOperand(MI->getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000487}
488
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000489static bool isLoadAbsSet(const MachineInstr *MI) {
490 unsigned Opc = MI->getOpcode();
491 switch (Opc) {
492 case Hexagon::L4_loadrd_ap:
493 case Hexagon::L4_loadrb_ap:
494 case Hexagon::L4_loadrh_ap:
495 case Hexagon::L4_loadrub_ap:
496 case Hexagon::L4_loadruh_ap:
497 case Hexagon::L4_loadri_ap:
498 return true;
499 }
500 return false;
501}
502
503static const MachineOperand &getAbsSetOperand(const MachineInstr *MI) {
504 assert(isLoadAbsSet(MI));
505 return MI->getOperand(1);
506}
507
508
509// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000510// Following restrictions are to be respected in convert a store into
511// a new value store.
512// 1. If an instruction uses auto-increment, its address register cannot
513// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000514// 2. If an instruction uses absolute-set addressing mode, its address
515// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000516// 3. If an instruction produces a 64-bit result, its registers cannot be used
517// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000518// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000519// the instruction that uses the new-value register must also be conditional,
520// and both must always have their predicates evaluate identically.
521// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000522// 5. There is an implied restriction that a packet cannot have another store,
523// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000524// already a store in a packet, there can not be a new value store.
525// Arch Spec: 3.4.4.2
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000526bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr *MI,
527 const MachineInstr *PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000528 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000529 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000530 return false;
531
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000532 // Make sure there is dependency and can be new value'd.
533 const MachineOperand &Val = getStoreValueOperand(MI);
534 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000535 return false;
536
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000537 const MCInstrDesc& MCID = PacketMI->getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000538
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000539 // First operand is always the result.
540 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
541 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
542 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000543 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000544
545 // New-value stores are of class NV (slot 0), dual stores require class ST
546 // in slot 0 (PRM 5.5).
547 for (auto I : CurrentPacketMIs) {
548 SUnit *PacketSU = MIToSUnit.find(I)->second;
549 if (PacketSU->getInstr()->mayStore())
550 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000551 }
552
553 // Make sure it's NOT the post increment register that we are going to
554 // new value.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000555 if (HII->isPostIncrement(MI) &&
556 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000557 return false;
558 }
559
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000560 if (HII->isPostIncrement(PacketMI) && PacketMI->mayLoad() &&
561 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
562 // If source is post_inc, or absolute-set addressing, it can not feed
563 // into new value store
564 // r3 = memw(r2++#4)
565 // memw(r30 + #-1404) = r2.new -> can not be new value store
566 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000567 return false;
568 }
569
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000570 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
571 return false;
572
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000573 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000574 // also be predicated.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000575 if (HII->isPredicated(*PacketMI)) {
576 if (!HII->isPredicated(*MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000577 return false;
578
579 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000580 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000581 unsigned predRegNumSrc = 0;
582 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000583 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000584
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000585 // Get predicate register used in the source instruction.
586 for (auto &MO : PacketMI->operands()) {
587 if (!MO.isReg())
588 continue;
589 predRegNumSrc = MO.getReg();
590 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
591 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000592 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000593 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000594 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
595 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000596
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000597 // Get predicate register used in new-value store instruction.
598 for (auto &MO : MI->operands()) {
599 if (!MO.isReg())
600 continue;
601 predRegNumDst = MO.getReg();
602 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
603 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000604 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000605 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000606 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
607 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000608
609 // New-value register producer and user (store) need to satisfy these
610 // constraints:
611 // 1) Both instructions should be predicated on the same register.
612 // 2) If producer of the new-value register is .new predicated then store
613 // should also be .new predicated and if producer is not .new predicated
614 // then store should not be .new predicated.
615 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000616 // sense, i.e, either both should be negated or both should be non-negated.
617 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000618 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
619 getPredicateSense(*MI, HII) != getPredicateSense(*PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000620 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000621 }
622
623 // Make sure that other than the new-value register no other store instruction
624 // register has been modified in the same packet. Predicate registers can be
625 // modified by they should not be modified between the producer and the store
626 // instruction as it will make them both conditional on different values.
627 // We already know this to be true for all the instructions before and
628 // including PacketMI. Howerver, we need to perform the check for the
629 // remaining instructions in the packet.
630
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000631 unsigned StartCheck = 0;
632
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000633 for (auto I : CurrentPacketMIs) {
634 SUnit *TempSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000635 MachineInstr* TempMI = TempSU->getInstr();
636
637 // Following condition is true for all the instructions until PacketMI is
638 // reached (StartCheck is set to 0 before the for loop).
639 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000640 if (TempMI != PacketMI && !StartCheck) // Start processing only after
641 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000642
643 StartCheck = 1;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000644 if (TempMI == PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000645 continue;
646
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000647 for (auto &MO : MI->operands())
648 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000649 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000650 }
651
Alp Tokerf907b892013-12-05 05:44:44 +0000652 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000653 // 1. The only use of reg is DepReg and no other registers.
654 // This handles V4 base+index registers.
655 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000656 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000657 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000658 if (!HII->isPostIncrement(MI)) {
659 for (unsigned opNum = 0; opNum < MI->getNumOperands()-1; opNum++) {
660 const MachineOperand &MO = MI->getOperand(opNum);
661 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000662 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000663 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000664 }
665
666 // If data definition is because of implicit definition of the register,
667 // do not newify the store. Eg.
668 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
669 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
670 for (auto &MO : PacketMI->operands()) {
671 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
672 continue;
673 unsigned R = MO.getReg();
674 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
675 return false;
676 }
677
678 // Handle imp-use of super reg case. There is a target independent side
679 // change that should prevent this situation but I am handling it for
680 // just-in-case. For example, we cannot newify R2 in the following case:
681 // %R3<def> = A2_tfrsi 0;
682 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
683 for (auto &MO : MI->operands()) {
684 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
685 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000686 }
687
688 // Can be dot new store.
689 return true;
690}
691
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000692// Can this MI to promoted to either new value store or new value jump.
693bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr *MI,
694 const SUnit *PacketSU, unsigned DepReg,
695 MachineBasicBlock::iterator &MII) {
696 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000697 return false;
698
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000699 // Check to see the store can be new value'ed.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000700 MachineInstr *PacketMI = PacketSU->getInstr();
701 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000702 return true;
703
704 // Check to see the compare/jump can be new value'ed.
705 // This is done as a pass on its own. Don't need to check it here.
706 return false;
707}
708
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000709static bool isImplicitDependency(const MachineInstr *I, unsigned DepReg) {
710 for (auto &MO : I->operands())
711 if (MO.isReg() && MO.isDef() && (MO.getReg() == DepReg) && MO.isImplicit())
712 return true;
713 return false;
714}
715
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000716// Check to see if an instruction can be dot new
717// There are three kinds.
718// 1. dot new on predicate - V2/V3/V4
719// 2. dot new on stores NV/ST - V4
720// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000721bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr *MI,
722 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
723 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000724 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000725 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000726 return false;
727
728 if (!isNewifiable(MI))
729 return false;
730
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000731 const MachineInstr *PI = PacketSU->getInstr();
732
733 // The "new value" cannot come from inline asm.
734 if (PI->isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000735 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000736
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000737 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
738 // sense.
739 if (PI->isImplicitDef())
740 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000741
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000742 // If dependency is trough an implicitly defined register, we should not
743 // newify the use.
744 if (isImplicitDependency(PI, DepReg))
745 return false;
746
747 const MCInstrDesc& MCID = PI->getDesc();
748 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
749 if (DisableVecDblNVStores && VecRC == &Hexagon::VecDblRegsRegClass)
750 return false;
751
752 // predicate .new
753 // bug 5670: until that is fixed
754 // TODO: MI->isIndirectBranch() and IsRegisterJump(MI)
755 if (RC == &Hexagon::PredRegsRegClass)
756 if (HII->isCondInst(MI) || MI->isReturn())
757 return HII->predCanBeUsedAsDotNew(PI, DepReg);
758
759 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
760 return false;
761
762 // Create a dot new machine instruction to see if resources can be
763 // allocated. If not, bail out now.
764 int NewOpcode = HII->getDotNewOp(MI);
765 const MCInstrDesc &D = HII->get(NewOpcode);
766 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000767 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000768 MF.DeleteMachineInstr(NewMI);
769 if (!ResourcesAvailable)
770 return false;
771
772 // New Value Store only. New Value Jump generated as a separate pass.
773 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
774 return false;
775
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000776 return true;
777}
778
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000779// Go through the packet instructions and search for an anti dependency between
780// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000781// Trying to add
782// a) %R1<def> = TFRI_cdNotPt %P3, 2
783// to this packet:
784// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000785// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
786// c) %P3<def> = C2_tfrrp %R23
787// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000788// }
789// The P3 from a) and d) will be complements after
790// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000791// Anti-dep between c) and b) is irrelevant for this case
792bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr* MI,
793 unsigned DepReg) {
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000794 SUnit *PacketSUDep = MIToSUnit.find(MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000795
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000796 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000797 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000798 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000799 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000800
801 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000802 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000803
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000804 // Look at dependencies between current members of the packet and
805 // predicate defining instruction MI. Make sure that dependency is
806 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000807 if (PacketSU->isSucc(PacketSUDep)) {
808 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000809 auto &Dep = PacketSU->Succs[i];
810 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
811 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000812 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000813 }
814 }
815 }
816
817 return false;
818}
819
820
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000821/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000822static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000823 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000824 /// We use the following rule: The first predicate register that is a use is
825 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000826 assert(QII->isPredicated(MI) && "Must be predicated instruction");
827
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000828 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000829 if (Op.isReg() && Op.getReg() && Op.isUse() &&
830 Hexagon::PredRegsRegClass.contains(Op.getReg()))
831 return Op.getReg();
832 }
833
834 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000835 return 0;
836}
837
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000838// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000839// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000840bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
841 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000842 // If we don't know the predicate sense of the instructions bail out early, we
843 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000844 if (getPredicateSense(MI1, HII) == PK_Unknown ||
845 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000846 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000847
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000848 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000849 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000850
851 // One corner case deals with the following scenario:
852 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000853 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000854 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000855 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000856 // b) %R25<def> = A2_tfrf %P0, %R24
857 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000858 // }
859 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000860 // On general check a) and b) are complements, but presence of c) will
861 // convert a) to .new form, and then it is not a complement.
862 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000863
864 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000865 // Look for Anti dependecy on the same predicate reg as used in the
866 // candidate.
867 for (auto I : CurrentPacketMIs) {
868 // Scheduling Unit for current insn in the packet.
869 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000870
871 // If this instruction in the packet is succeeded by the candidate...
872 if (PacketSU->isSucc(SU)) {
873 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000874 auto Dep = PacketSU->Succs[i];
875 // The corner case exist when there is true data dependency between
876 // candidate and one of current packet members, this dep is on
877 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000878 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000879 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
880 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
881 // Here I know that I is predicate setting instruction with true
882 // data dep to candidate on the register we care about - c) in the
883 // above example. Now I need to see if there is an anti dependency
884 // from c) to any other instruction in the same packet on the pred
885 // reg of interest.
886 if (restrictingDepExistInPacket(I, Dep.getReg()))
887 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000888 }
889 }
890 }
891 }
892
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000893 // If the above case does not apply, check regular complement condition.
894 // Check that the predicate register is the same and that the predicate
895 // sense is different We also need to differentiate .old vs. .new: !p0
896 // is not complementary to p0.new.
897 unsigned PReg1 = getPredicatedRegister(MI1, HII);
898 unsigned PReg2 = getPredicatedRegister(MI2, HII);
899 return PReg1 == PReg2 &&
900 Hexagon::PredRegsRegClass.contains(PReg1) &&
901 Hexagon::PredRegsRegClass.contains(PReg2) &&
902 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000903 HII->isDotNewInst(&MI1) == HII->isDotNewInst(&MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000904}
905
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000906// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000907void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000908 Dependence = false;
909 PromotedToDotNew = false;
910 GlueToNewValueJump = false;
911 GlueAllocframeStore = false;
912 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000913}
914
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000915// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000916bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
917 const MachineBasicBlock *) {
918 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000919 return true;
920
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000921 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000922 return false;
923
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000924 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000925 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000926 return false;
927
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000928 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000929 return false;
930
931 // We check if MI has any functional units mapped to it. If it doesn't,
932 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000933 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000934 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +0000935 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000936 return !FuncUnits;
937}
938
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000939bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
940 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000941 return true;
942
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000943 // Consider inline asm to not be a solo instruction by default.
944 // Inline asm will be put in a packet temporarily, but then it will be
945 // removed, and placed outside of the packet (before or after, depending
946 // on dependencies). This is to reduce the impact of inline asm as a
947 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000948 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000949 return true;
950
951 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
952 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
953 // They must not be grouped with other instructions in a packet.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000954 if (isSchedBarrier(&MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000955 return true;
956
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000957 if (HII->isSolo(&MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000958 return true;
959
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000960 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000961 return true;
962
963 return false;
964}
965
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000966
967// Quick check if instructions MI and MJ cannot coexist in the same packet.
968// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
969// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
970// For full test call this function twice:
971// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
972// Doing the test only one way saves the amount of code in this function,
973// since every test would need to be repeated with the MI and MJ reversed.
974static bool cannotCoexistAsymm(const MachineInstr *MI, const MachineInstr *MJ,
975 const HexagonInstrInfo &HII) {
976 const MachineFunction *MF = MI->getParent()->getParent();
977 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
978 HII.isHVXMemWithAIndirect(MI, MJ))
979 return true;
980
981 // An inline asm cannot be together with a branch, because we may not be
982 // able to remove the asm out after packetizing (i.e. if the asm must be
983 // moved past the bundle). Similarly, two asms cannot be together to avoid
984 // complications when determining their relative order outside of a bundle.
985 if (MI->isInlineAsm())
986 return MJ->isInlineAsm() || MJ->isBranch() || MJ->isBarrier() ||
987 MJ->isCall() || MJ->isTerminator();
988
989 // "False" really means that the quick check failed to determine if
990 // I and J cannot coexist.
991 return false;
992}
993
994
995// Full, symmetric check.
996bool HexagonPacketizerList::cannotCoexist(const MachineInstr *MI,
997 const MachineInstr *MJ) {
998 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
999}
1000
1001void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1002 for (auto &B : MF) {
1003 MachineBasicBlock::iterator BundleIt;
1004 MachineBasicBlock::instr_iterator NextI;
1005 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1006 NextI = std::next(I);
1007 MachineInstr *MI = &*I;
1008 if (MI->isBundle())
1009 BundleIt = I;
1010 if (!MI->isInsideBundle())
1011 continue;
1012
1013 // Decide on where to insert the instruction that we are pulling out.
1014 // Debug instructions always go before the bundle, but the placement of
1015 // INLINE_ASM depends on potential dependencies. By default, try to
1016 // put it before the bundle, but if the asm writes to a register that
1017 // other instructions in the bundle read, then we need to place it
1018 // after the bundle (to preserve the bundle semantics).
1019 bool InsertBeforeBundle;
1020 if (MI->isInlineAsm())
1021 InsertBeforeBundle = !hasWriteToReadDep(MI, BundleIt, HRI);
1022 else if (MI->isDebugValue())
1023 InsertBeforeBundle = true;
1024 else
1025 continue;
1026
1027 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1028 }
1029 }
1030}
1031
1032// Check if a given instruction is of class "system".
1033static bool isSystemInstr(const MachineInstr *MI) {
1034 unsigned Opc = MI->getOpcode();
1035 switch (Opc) {
1036 case Hexagon::Y2_barrier:
1037 case Hexagon::Y2_dcfetchbo:
1038 return true;
1039 }
1040 return false;
1041}
1042
1043bool HexagonPacketizerList::hasDeadDependence(const MachineInstr *I,
1044 const MachineInstr *J) {
1045 // The dependence graph may not include edges between dead definitions,
1046 // so without extra checks, we could end up packetizing two instruction
1047 // defining the same (dead) register.
1048 if (I->isCall() || J->isCall())
1049 return false;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001050 if (HII->isPredicated(*I) || HII->isPredicated(*J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001051 return false;
1052
1053 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
1054 for (auto &MO : I->operands()) {
1055 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1056 continue;
1057 DeadDefs[MO.getReg()] = true;
1058 }
1059
1060 for (auto &MO : J->operands()) {
1061 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1062 continue;
1063 unsigned R = MO.getReg();
1064 if (R != Hexagon::USR_OVF && DeadDefs[R])
1065 return true;
1066 }
1067 return false;
1068}
1069
1070bool HexagonPacketizerList::hasControlDependence(const MachineInstr *I,
1071 const MachineInstr *J) {
1072 // A save callee-save register function call can only be in a packet
1073 // with instructions that don't write to the callee-save registers.
1074 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1075 doesModifyCalleeSavedReg(J, HRI)) ||
1076 (HII->isSaveCalleeSavedRegsCall(J) &&
1077 doesModifyCalleeSavedReg(I, HRI)))
1078 return true;
1079
1080 // Two control flow instructions cannot go in the same packet.
1081 if (isControlFlow(I) && isControlFlow(J))
1082 return true;
1083
1084 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1085 // contain a speculative indirect jump,
1086 // a new-value compare jump or a dealloc_return.
1087 auto isBadForLoopN = [this] (const MachineInstr *MI) -> bool {
1088 if (MI->isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
1089 return true;
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001090 if (HII->isPredicated(*MI) && HII->isPredicatedNew(*MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001091 return true;
1092 return false;
1093 };
1094
1095 if (HII->isLoopN(I) && isBadForLoopN(J))
1096 return true;
1097 if (HII->isLoopN(J) && isBadForLoopN(I))
1098 return true;
1099
1100 // dealloc_return cannot appear in the same packet as a conditional or
1101 // unconditional jump.
1102 return HII->isDeallocRet(I) &&
1103 (J->isBranch() || J->isCall() || J->isBarrier());
1104}
1105
1106bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr *I,
1107 const MachineInstr *J) {
1108 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
1109 bool StoreI = I->mayStore(), StoreJ = J->mayStore();
1110 if ((SysI && StoreJ) || (SysJ && StoreI))
1111 return true;
1112
1113 if (StoreI && StoreJ) {
1114 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1115 return true;
1116 } else {
1117 // A memop cannot be in the same packet with another memop or a store.
1118 // Two stores can be together, but here I and J cannot both be stores.
1119 bool MopStI = HII->isMemOp(I) || StoreI;
1120 bool MopStJ = HII->isMemOp(J) || StoreJ;
1121 if (MopStI && MopStJ)
1122 return true;
1123 }
1124
1125 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1126}
1127
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001128// SUI is the current instruction that is out side of the current packet.
1129// SUJ is the current instruction inside the current packet against which that
1130// SUI will be packetized.
1131bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
1132 MachineInstr *I = SUI->getInstr();
1133 MachineInstr *J = SUJ->getInstr();
1134 assert(I && J && "Unable to packetize null instruction!");
1135
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001136 // Clear IgnoreDepMIs when Packet starts.
1137 if (CurrentPacketMIs.size() == 1)
1138 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001139
1140 MachineBasicBlock::iterator II = I;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001141 const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001142
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001143 // Solo instructions cannot go in the packet.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001144 assert(!isSoloInstruction(*I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001145
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001146 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001147 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001148
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001149 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1150 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001151 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001152
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001153 // V4 allows dual stores. It does not allow second store, if the first
1154 // store is not in SLOT0. New value store, new value jump, dealloc_return
1155 // and memop always take SLOT0. Arch spec 3.4.4.2.
1156 Dependence = hasV4SpecificDependence(I, J);
1157 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001158 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001159
1160 // If an instruction feeds new value jump, glue it.
1161 MachineBasicBlock::iterator NextMII = I;
1162 ++NextMII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001163 if (NextMII != I->getParent()->end() && HII->isNewValueJump(NextMII)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001164 MachineInstr *NextMI = NextMII;
1165
1166 bool secondRegMatch = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001167 const MachineOperand &NOp0 = NextMI->getOperand(0);
1168 const MachineOperand &NOp1 = NextMI->getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001169
Krzysztof Parzyszek4f9164d2015-12-16 20:07:04 +00001170 if (NOp1.isReg() && I->getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001171 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001172
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001173 for (auto I : CurrentPacketMIs) {
1174 SUnit *PacketSU = MIToSUnit.find(I)->second;
1175 MachineInstr *PI = PacketSU->getInstr();
1176 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
1177 if (PI->isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001178 Dependence = true;
1179 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001180 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001181 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001182 // 1. Packet does not have a store in it.
1183 // 2. If the first operand of the nvj is newified, and the second
1184 // operand is also a reg, it (second reg) is not defined in
1185 // the same packet.
1186 // 3. If the second operand of the nvj is newified, (which means
1187 // first operand is also a reg), first reg is not defined in
1188 // the same packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001189 if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() ||
1190 HII->isLoopN(PI)) {
1191 Dependence = true;
1192 break;
1193 }
1194 // Check #2/#3.
1195 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
1196 if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001197 Dependence = true;
1198 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001199 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001200 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001201
1202 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001203 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001204 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001205 }
1206
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001207 // There no dependency between a prolog instruction and its successor.
1208 if (!SUJ->isSucc(SUI))
1209 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001210
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001211 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1212 if (FoundSequentialDependence)
1213 break;
1214
1215 if (SUJ->Succs[i].getSUnit() != SUI)
1216 continue;
1217
1218 SDep::Kind DepType = SUJ->Succs[i].getKind();
1219 // For direct calls:
1220 // Ignore register dependences for call instructions for packetization
1221 // purposes except for those due to r31 and predicate registers.
1222 //
1223 // For indirect calls:
1224 // Same as direct calls + check for true dependences to the register
1225 // used in the indirect call.
1226 //
1227 // We completely ignore Order dependences for call instructions.
1228 //
1229 // For returns:
1230 // Ignore register dependences for return instructions like jumpr,
1231 // dealloc return unless we have dependencies on the explicit uses
1232 // of the registers used by jumpr (like r31) or dealloc return
1233 // (like r29 or r30).
1234 //
1235 // TODO: Currently, jumpr is handling only return of r31. So, the
1236 // following logic (specificaly isCallDependent) is working fine.
1237 // We need to enable jumpr for register other than r31 and then,
1238 // we need to rework the last part, where it handles indirect call
1239 // of that (isCallDependent) function. Bug 6216 is opened for this.
1240 unsigned DepReg = 0;
1241 const TargetRegisterClass *RC = nullptr;
1242 if (DepType == SDep::Data) {
1243 DepReg = SUJ->Succs[i].getReg();
1244 RC = HRI->getMinimalPhysRegClass(DepReg);
1245 }
1246
1247 if (I->isCall() || I->isReturn()) {
1248 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001249 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001250 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1251 continue;
1252 }
1253
1254 if (DepType == SDep::Data) {
1255 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1256 if (promoteToDotCur(J, DepType, II, RC))
1257 continue;
1258 }
1259
1260 // Data dpendence ok if we have load.cur.
1261 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
1262 if (HII->isV60VectorInstruction(I))
1263 continue;
1264 }
1265
1266 // For instructions that can be promoted to dot-new, try to promote.
1267 if (DepType == SDep::Data) {
1268 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1269 if (promoteToDotNew(I, DepType, II, RC)) {
1270 PromotedToDotNew = true;
1271 continue;
1272 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001273 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001274 if (HII->isNewValueJump(I))
1275 continue;
1276 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001277
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001278 // For predicated instructions, if the predicates are complements then
1279 // there can be no dependence.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +00001280 if (HII->isPredicated(*I) && HII->isPredicated(*J) &&
1281 arePredicatesComplements(*I, *J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001282 // Not always safe to do this translation.
1283 // DAG Builder attempts to reduce dependence edges using transitive
1284 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001285 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001286 // r0 = tfr_pt ... (1)
1287 // r0 = tfr_pf ... (2)
1288 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001289 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001290 // There will be an output dependence between (1)->(2) and (2)->(3).
1291 // However, there is no dependence edge between (1)->(3). This results
1292 // in all 3 instructions going in the same packet. We ignore dependce
1293 // only once to avoid this situation.
1294 auto Itr = std::find(IgnoreDepMIs.begin(), IgnoreDepMIs.end(), J);
1295 if (Itr != IgnoreDepMIs.end()) {
1296 Dependence = true;
1297 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001298 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001299 IgnoreDepMIs.push_back(I);
1300 continue;
1301 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001302
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001303 // Ignore Order dependences between unconditional direct branches
1304 // and non-control-flow instructions.
1305 if (isDirectJump(I) && !J->isBranch() && !J->isCall() &&
1306 DepType == SDep::Order)
1307 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001308
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001309 // Ignore all dependences for jumps except for true and output
1310 // dependences.
1311 if (I->isConditionalBranch() && DepType != SDep::Data &&
1312 DepType != SDep::Output)
1313 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001314
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001315 // Ignore output dependences due to superregs. We can write to two
1316 // different subregisters of R1:0 for instance in the same cycle.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001317
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001318 // If neither I nor J defines DepReg, then this is a superfluous output
1319 // dependence. The dependence must be of the form:
1320 // R0 = ...
1321 // R1 = ...
1322 // and there is an output dependence between the two instructions with
1323 // DepReg = D0.
1324 // We want to ignore these dependences. Ideally, the dependence
1325 // constructor should annotate such dependences. We can then avoid this
1326 // relatively expensive check.
1327 //
1328 if (DepType == SDep::Output) {
1329 // DepReg is the register that's responsible for the dependence.
1330 unsigned DepReg = SUJ->Succs[i].getReg();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001331
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001332 // Check if I and J really defines DepReg.
1333 if (!I->definesRegister(DepReg) && !J->definesRegister(DepReg))
1334 continue;
1335 FoundSequentialDependence = true;
1336 break;
1337 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001338
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001339 // For Order dependences:
1340 // 1. On V4 or later, volatile loads/stores can be packetized together,
1341 // unless other rules prevent is.
1342 // 2. Store followed by a load is not allowed.
1343 // 3. Store followed by a store is only valid on V4 or later.
1344 // 4. Load followed by any memory operation is allowed.
1345 if (DepType == SDep::Order) {
1346 if (!PacketizeVolatiles) {
1347 bool OrdRefs = I->hasOrderedMemoryRef() || J->hasOrderedMemoryRef();
1348 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001349 FoundSequentialDependence = true;
1350 break;
1351 }
1352 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001353 // J is first, I is second.
1354 bool LoadJ = J->mayLoad(), StoreJ = J->mayStore();
1355 bool LoadI = I->mayLoad(), StoreI = I->mayStore();
1356 if (StoreJ) {
1357 // Two stores are only allowed on V4+. Load following store is never
1358 // allowed.
1359 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001360 FoundSequentialDependence = true;
1361 break;
1362 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001363 } else if (!LoadJ || (!LoadI && !StoreI)) {
1364 // If J is neither load nor store, assume a dependency.
1365 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001366 FoundSequentialDependence = true;
1367 break;
1368 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001369 // Store followed by store: not OK on V2.
1370 // Store followed by load: not OK on all.
1371 // Load followed by store: OK on all.
1372 // Load followed by load: OK on all.
1373 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001374 }
1375
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001376 // For V4, special case ALLOCFRAME. Even though there is dependency
1377 // between ALLOCFRAME and subsequent store, allow it to be packetized
1378 // in a same packet. This implies that the store is using the caller's
1379 // SP. Hence, offset needs to be updated accordingly.
1380 if (DepType == SDep::Data && J->getOpcode() == Hexagon::S2_allocframe) {
1381 unsigned Opc = I->getOpcode();
1382 switch (Opc) {
1383 case Hexagon::S2_storerd_io:
1384 case Hexagon::S2_storeri_io:
1385 case Hexagon::S2_storerh_io:
1386 case Hexagon::S2_storerb_io:
1387 if (I->getOperand(0).getReg() == HRI->getStackRegister()) {
1388 int64_t Imm = I->getOperand(1).getImm();
1389 int64_t NewOff = Imm - (FrameSize + HEXAGON_LRFP_SIZE);
1390 if (HII->isValidOffset(Opc, NewOff)) {
1391 GlueAllocframeStore = true;
1392 // Since this store is to be glued with allocframe in the same
1393 // packet, it will use SP of the previous stack frame, i.e.
1394 // caller's SP. Therefore, we need to recalculate offset
1395 // according to this change.
1396 I->getOperand(1).setImm(NewOff);
1397 continue;
1398 }
1399 }
1400 default:
1401 break;
1402 }
1403 }
1404
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001405 // There are certain anti-dependencies that cannot be ignored.
1406 // Specifically:
1407 // J2_call ... %R0<imp-def> ; SUJ
1408 // R0 = ... ; SUI
1409 // Those cannot be packetized together, since the call will observe
1410 // the effect of the assignment to R0.
1411 if (DepType == SDep::Anti && J->isCall()) {
1412 // Check if I defines any volatile register. We should also check
1413 // registers that the call may read, but these happen to be a
1414 // subset of the volatile register set.
1415 for (const MCPhysReg *P = J->getDesc().ImplicitDefs; P && *P; ++P) {
1416 if (!I->modifiesRegister(*P, HRI))
1417 continue;
1418 FoundSequentialDependence = true;
1419 break;
1420 }
1421 }
1422
1423 // Skip over remaining anti-dependences. Two instructions that are
1424 // anti-dependent can share a packet, since in most such cases all
1425 // operands are read before any modifications take place.
1426 // The exceptions are branch and call instructions, since they are
1427 // executed after all other instructions have completed (at least
1428 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001429 if (DepType != SDep::Anti) {
1430 FoundSequentialDependence = true;
1431 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001432 }
1433 }
1434
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001435 if (FoundSequentialDependence) {
1436 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001437 return false;
1438 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001439
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001440 return true;
1441}
1442
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001443bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
1444 MachineInstr *I = SUI->getInstr();
1445 MachineInstr *J = SUJ->getInstr();
1446 assert(I && J && "Unable to packetize null instruction!");
1447
1448 if (cannotCoexist(I, J))
1449 return false;
1450
1451 if (!Dependence)
1452 return true;
1453
1454 // Check if the instruction was promoted to a dot-new. If so, demote it
1455 // back into a dot-old.
1456 if (PromotedToDotNew)
1457 demoteToDotOld(I);
1458
1459 cleanUpDotCur();
1460 // Check if the instruction (must be a store) was glued with an allocframe
1461 // instruction. If so, restore its offset to its original value, i.e. use
1462 // current SP instead of caller's SP.
1463 if (GlueAllocframeStore) {
1464 unsigned FrameSize = MF.getFrameInfo()->getStackSize();
1465 MachineOperand &MOff = I->getOperand(1);
1466 MOff.setImm(MOff.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
1467 }
1468 return false;
1469}
1470
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001471MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001472HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001473 MachineBasicBlock::iterator MII = MI;
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001474 MachineBasicBlock *MBB = MI.getParent();
1475 if (MI.isImplicitDef()) {
1476 unsigned R = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001477 if (Hexagon::IntRegsRegClass.contains(R)) {
1478 MCSuperRegIterator S(R, HRI, false);
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001479 MI.addOperand(MachineOperand::CreateReg(*S, true, true));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001480 }
1481 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001482 }
1483 assert(ResourceTracker->canReserveResources(MI));
1484
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001485 bool ExtMI = HII->isExtended(&MI) || HII->isConstExtended(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001486 bool Good = true;
1487
1488 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001489 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001490 // We need to put both instructions in the same packet: MI and NvjMI.
1491 // Either of them can require a constant extender. Try to add both to
1492 // the current packet, and if that fails, end the packet and start a
1493 // new one.
1494 ResourceTracker->reserveResources(MI);
1495 if (ExtMI)
1496 Good = tryAllocateResourcesForConstExt(true);
1497
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001498 bool ExtNvjMI = HII->isExtended(&NvjMI) || HII->isConstExtended(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001499 if (Good) {
1500 if (ResourceTracker->canReserveResources(NvjMI))
1501 ResourceTracker->reserveResources(NvjMI);
1502 else
1503 Good = false;
1504 }
1505 if (Good && ExtNvjMI)
1506 Good = tryAllocateResourcesForConstExt(true);
1507
1508 if (!Good) {
1509 endPacket(MBB, MI);
1510 assert(ResourceTracker->canReserveResources(MI));
1511 ResourceTracker->reserveResources(MI);
1512 if (ExtMI) {
1513 assert(canReserveResourcesForConstExt());
1514 tryAllocateResourcesForConstExt(true);
1515 }
1516 assert(ResourceTracker->canReserveResources(NvjMI));
1517 ResourceTracker->reserveResources(NvjMI);
1518 if (ExtNvjMI) {
1519 assert(canReserveResourcesForConstExt());
1520 reserveResourcesForConstExt();
1521 }
1522 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001523 CurrentPacketMIs.push_back(&MI);
1524 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001525 return MII;
1526 }
1527
1528 ResourceTracker->reserveResources(MI);
1529 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1530 endPacket(MBB, MI);
1531 if (PromotedToDotNew)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001532 demoteToDotOld(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001533 ResourceTracker->reserveResources(MI);
1534 reserveResourcesForConstExt();
1535 }
1536
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001537 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001538 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001539}
1540
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001541void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001542 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001543 OldPacketMIs = CurrentPacketMIs;
1544 VLIWPacketizerList::endPacket(MBB, MI);
1545}
1546
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001547bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
1548 return !producesStall(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001549}
1550
1551
1552// Return true when ConsMI uses a register defined by ProdMI.
1553static bool isDependent(const MachineInstr *ProdMI,
1554 const MachineInstr *ConsMI) {
1555 if (!ProdMI->getOperand(0).isReg())
1556 return false;
1557 unsigned DstReg = ProdMI->getOperand(0).getReg();
1558
1559 for (auto &Op : ConsMI->operands())
1560 if (Op.isReg() && Op.isUse() && Op.getReg() == DstReg)
1561 // The MIs depend on each other.
1562 return true;
1563
1564 return false;
1565}
1566
1567// V60 forward scheduling.
1568bool HexagonPacketizerList::producesStall(const MachineInstr *I) {
1569 // Check whether the previous packet is in a different loop. If this is the
1570 // case, there is little point in trying to avoid a stall because that would
1571 // favor the rare case (loop entry) over the common case (loop iteration).
1572 //
1573 // TODO: We should really be able to check all the incoming edges if this is
1574 // the first packet in a basic block, so we can avoid stalls from the loop
1575 // backedge.
1576 if (!OldPacketMIs.empty()) {
1577 auto *OldBB = OldPacketMIs.front()->getParent();
1578 auto *ThisBB = I->getParent();
1579 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1580 return false;
1581 }
1582
1583 // Check for stall between two vector instructions.
1584 if (HII->isV60VectorInstruction(I)) {
1585 for (auto J : OldPacketMIs) {
1586 if (!HII->isV60VectorInstruction(J))
1587 continue;
1588 if (isDependent(J, I) && !HII->isVecUsableNextPacket(J, I))
1589 return true;
1590 }
1591 return false;
1592 }
1593
1594 // Check for stall between two scalar instructions. First, check that
1595 // there is no definition of a use in the current packet, because it
1596 // may be a candidate for .new.
1597 for (auto J : CurrentPacketMIs)
1598 if (!HII->isV60VectorInstruction(J) && isDependent(J, I))
1599 return false;
1600
1601 // Check for stall between I and instructions in the previous packet.
1602 if (MF.getSubtarget<HexagonSubtarget>().useBSBScheduling()) {
1603 for (auto J : OldPacketMIs) {
1604 if (HII->isV60VectorInstruction(J))
1605 continue;
1606 if (!HII->isLateInstrFeedsEarlyInstr(J, I))
1607 continue;
1608 if (isDependent(J, I) && !HII->canExecuteInBundle(J, I))
1609 return true;
1610 }
1611 }
1612
1613 return false;
1614}
1615
1616
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001617//===----------------------------------------------------------------------===//
1618// Public Constructor Functions
1619//===----------------------------------------------------------------------===//
1620
1621FunctionPass *llvm::createHexagonPacketizer() {
1622 return new HexagonPacketizer();
1623}