blob: 52b026aad7965c100c9216746995184287c6950c [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#include <map>
Jyotsna Verma1d297502013-05-02 15:39:30 +000033#include <vector>
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000034
35using namespace llvm;
36
Chandler Carruth84e68b22014-04-22 02:41:26 +000037#define DEBUG_TYPE "packets"
38
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000039static cl::opt<bool> DisablePacketizer("disable-packetizer", cl::Hidden,
40 cl::ZeroOrMore, cl::init(false),
41 cl::desc("Disable Hexagon packetizer pass"));
42
Jyotsna Verma1d297502013-05-02 15:39:30 +000043static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000044 cl::ZeroOrMore, cl::Hidden, cl::init(true),
45 cl::desc("Allow non-solo packetization of volatile memory references"));
46
47static cl::opt<bool> EnableGenAllInsnClass("enable-gen-insn", cl::init(false),
48 cl::Hidden, cl::ZeroOrMore, cl::desc("Generate all instruction with TC"));
49
50static cl::opt<bool> DisableVecDblNVStores("disable-vecdbl-nv-stores",
51 cl::init(false), cl::Hidden, cl::ZeroOrMore,
52 cl::desc("Disable vector double new-value-stores"));
53
54extern cl::opt<bool> ScheduleInlineAsm;
Jyotsna Verma1d297502013-05-02 15:39:30 +000055
Jyotsna Verma1d297502013-05-02 15:39:30 +000056namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000057 FunctionPass *createHexagonPacketizer();
Jyotsna Verma1d297502013-05-02 15:39:30 +000058 void initializeHexagonPacketizerPass(PassRegistry&);
59}
60
61
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000062namespace {
63 class HexagonPacketizer : public MachineFunctionPass {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000064 public:
65 static char ID;
Jyotsna Verma1d297502013-05-02 15:39:30 +000066 HexagonPacketizer() : MachineFunctionPass(ID) {
67 initializeHexagonPacketizerPass(*PassRegistry::getPassRegistry());
68 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000069
Craig Topper906c2cd2014-04-29 07:58:16 +000070 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000071 AU.setPreservesCFG();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000072 AU.addRequired<AAResultsWrapperPass>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000073 AU.addRequired<MachineBranchProbabilityInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000074 AU.addRequired<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000075 AU.addRequired<MachineLoopInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000076 AU.addPreserved<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000077 AU.addPreserved<MachineLoopInfo>();
78 MachineFunctionPass::getAnalysisUsage(AU);
79 }
Craig Topper906c2cd2014-04-29 07:58:16 +000080 const char *getPassName() const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000081 return "Hexagon Packetizer";
82 }
Craig Topper906c2cd2014-04-29 07:58:16 +000083 bool runOnMachineFunction(MachineFunction &Fn) override;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000084
85 private:
86 const HexagonInstrInfo *HII;
87 const HexagonRegisterInfo *HRI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000088 };
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000089
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000090 char HexagonPacketizer::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000091}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000092
Jyotsna Verma1d297502013-05-02 15:39:30 +000093INITIALIZE_PASS_BEGIN(HexagonPacketizer, "packets", "Hexagon Packetizer",
94 false, false)
95INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
96INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
97INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Chandler Carruth7b560d42015-09-09 17:55:00 +000098INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Jyotsna Verma1d297502013-05-02 15:39:30 +000099INITIALIZE_PASS_END(HexagonPacketizer, "packets", "Hexagon Packetizer",
100 false, false)
101
102
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000103HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
104 MachineLoopInfo &MLI, AliasAnalysis *AA,
105 const MachineBranchProbabilityInfo *MBPI)
106 : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI) {
107 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
108 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000109}
110
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000111// Check if FirstI modifies a register that SecondI reads.
112static bool hasWriteToReadDep(const MachineInstr *FirstI,
113 const MachineInstr *SecondI, const TargetRegisterInfo *TRI) {
114 for (auto &MO : FirstI->operands()) {
115 if (!MO.isReg() || !MO.isDef())
116 continue;
117 unsigned R = MO.getReg();
118 if (SecondI->readsRegister(R, TRI))
119 return true;
120 }
121 return false;
122}
123
124
125static MachineBasicBlock::iterator moveInstrOut(MachineInstr *MI,
126 MachineBasicBlock::iterator BundleIt, bool Before) {
127 MachineBasicBlock::instr_iterator InsertPt;
128 if (Before)
129 InsertPt = BundleIt.getInstrIterator();
130 else
131 InsertPt = std::next(BundleIt).getInstrIterator();
132
133 MachineBasicBlock &B = *MI->getParent();
134 // The instruction should at least be bundled with the preceding instruction
135 // (there will always be one, i.e. BUNDLE, if nothing else).
136 assert(MI->isBundledWithPred());
137 if (MI->isBundledWithSucc()) {
138 MI->clearFlag(MachineInstr::BundledSucc);
139 MI->clearFlag(MachineInstr::BundledPred);
140 } else {
141 // If it's not bundled with the successor (i.e. it is the last one
142 // in the bundle), then we can simply unbundle it from the predecessor,
143 // which will take care of updating the predecessor's flag.
144 MI->unbundleFromPred();
145 }
146 B.splice(InsertPt, &B, MI);
147
148 // Get the size of the bundle without asserting.
149 MachineBasicBlock::const_instr_iterator I(BundleIt);
150 MachineBasicBlock::const_instr_iterator E = B.instr_end();
151 unsigned Size = 0;
152 for (++I; I != E && I->isBundledWithPred(); ++I)
153 ++Size;
154
155 // If there are still two or more instructions, then there is nothing
156 // else to be done.
157 if (Size > 1)
158 return BundleIt;
159
160 // Otherwise, extract the single instruction out and delete the bundle.
161 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
162 MachineInstr *SingleI = BundleIt->getNextNode();
163 SingleI->unbundleFromPred();
164 assert(!SingleI->isBundledWithSucc());
165 BundleIt->eraseFromParent();
166 return NextIt;
167}
168
169
170bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
171 if (DisablePacketizer)
172 return false;
173
174 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
175 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
176 auto &MLI = getAnalysis<MachineLoopInfo>();
177 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
178 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
179
180 if (EnableGenAllInsnClass)
181 HII->genAllInsnTimingClasses(MF);
182
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000183 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000184 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000185
186 // DFA state table should not be empty.
187 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
188
189 //
190 // Loop over all basic blocks and remove KILL pseudo-instructions
191 // These instructions confuse the dependence analysis. Consider:
192 // D0 = ... (Insn 0)
193 // R0 = KILL R0, D0 (Insn 1)
194 // R0 = ... (Insn 2)
195 // Here, Insn 1 will result in the dependence graph not emitting an output
196 // dependence between Insn 0 and Insn 2. This can lead to incorrect
197 // packetization
198 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000199 for (auto &MB : MF) {
200 auto End = MB.end();
201 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000202 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000203 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000204 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000205 MB.erase(MI);
206 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000207 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000208 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000209 }
210 }
211
212 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000213 for (auto &MB : MF) {
214 auto Begin = MB.begin(), End = MB.end();
215 while (Begin != End) {
216 // First the first non-boundary starting from the end of the last
217 // scheduling region.
218 MachineBasicBlock::iterator RB = Begin;
219 while (RB != End && HII->isSchedulingBoundary(RB, &MB, MF))
220 ++RB;
221 // First the first boundary starting from the beginning of the new
222 // region.
223 MachineBasicBlock::iterator RE = RB;
224 while (RE != End && !HII->isSchedulingBoundary(RE, &MB, MF))
225 ++RE;
226 // Add the scheduling boundary if it's not block end.
227 if (RE != End)
228 ++RE;
229 // If RB == End, then RE == End.
230 if (RB != End)
231 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000232
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000233 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000234 }
235 }
236
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000237 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000238 return true;
239}
240
241
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000242// Reserve resources for a constant extender. Trigger an assertion if the
243// reservation fails.
244void HexagonPacketizerList::reserveResourcesForConstExt() {
245 if (!tryAllocateResourcesForConstExt(true))
246 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000247}
248
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000249bool HexagonPacketizerList::canReserveResourcesForConstExt() {
250 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000251}
252
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000253// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
254// return true, otherwise, return false.
255bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
256 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
257 bool Avail = ResourceTracker->canReserveResources(ExtMI);
258 if (Reserve && Avail)
259 ResourceTracker->reserveResources(ExtMI);
260 MF.DeleteMachineInstr(ExtMI);
261 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000262}
263
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000264
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000265bool HexagonPacketizerList::isCallDependent(const MachineInstr* MI,
266 SDep::Kind DepType, unsigned DepReg) {
267 // Check for LR dependence.
268 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000269 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000270
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000271 if (HII->isDeallocRet(MI))
272 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
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 // Check if this is a predicate dependence.
276 const TargetRegisterClass* RC = HRI->getMinimalPhysRegClass(DepReg);
277 if (RC == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000278 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000279
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000280 // Assumes that the first operand of the CALLr is the function address.
281 if (HII->isIndirectCall(MI) && (DepType == SDep::Data)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000282 MachineOperand MO = MI->getOperand(0);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000283 if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000284 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000285 }
286
287 return false;
288}
289
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000290static bool isRegDependence(const SDep::Kind DepType) {
291 return DepType == SDep::Data || DepType == SDep::Anti ||
292 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000293}
294
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000295static bool isDirectJump(const MachineInstr* MI) {
296 return MI->getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000297}
298
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000299static bool isSchedBarrier(const MachineInstr* MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000300 switch (MI->getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000301 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000302 return true;
303 }
304 return false;
305}
306
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000307static bool isControlFlow(const MachineInstr* MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000308 return (MI->getDesc().isTerminator() || MI->getDesc().isCall());
309}
310
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000311
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000312/// Returns true if the instruction modifies a callee-saved register.
313static bool doesModifyCalleeSavedReg(const MachineInstr *MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000314 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000315 const MachineFunction &MF = *MI->getParent()->getParent();
316 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
317 if (MI->modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000318 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000319 return false;
320}
321
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000322// TODO: MI->isIndirectBranch() and IsRegisterJump(MI)
323// Returns true if an instruction can be promoted to .new predicate or
324// new-value store.
325bool HexagonPacketizerList::isNewifiable(const MachineInstr* MI) {
326 return HII->isCondInst(MI) || MI->isReturn() || HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000327}
328
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000329// Promote an instructiont to its .cur form.
330// At this time, we have already made a call to canPromoteToDotCur and made
331// sure that it can *indeed* be promoted.
332bool HexagonPacketizerList::promoteToDotCur(MachineInstr* MI,
333 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
334 const TargetRegisterClass* RC) {
335 assert(DepType == SDep::Data);
336 int CurOpcode = HII->getDotCurOp(MI);
337 MI->setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000338 return true;
339}
340
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000341void HexagonPacketizerList::cleanUpDotCur() {
342 MachineInstr *MI = NULL;
343 for (auto BI : CurrentPacketMIs) {
344 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
345 if (BI->getOpcode() == Hexagon::V6_vL32b_cur_ai) {
346 MI = BI;
347 continue;
348 }
349 if (MI) {
350 for (auto &MO : BI->operands())
351 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
352 return;
353 }
354 }
355 if (!MI)
356 return;
357 // We did not find a use of the CUR, so de-cur it.
358 MI->setDesc(HII->get(Hexagon::V6_vL32b_ai));
359 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
360}
361
362// Check to see if an instruction can be dot cur.
363bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr *MI,
364 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
365 const TargetRegisterClass *RC) {
366 if (!HII->isV60VectorInstruction(MI))
367 return false;
368 if (!HII->isV60VectorInstruction(MII))
369 return false;
370
371 // Already a dot new instruction.
372 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
373 return false;
374
375 if (!HII->mayBeCurLoad(MI))
376 return false;
377
378 // The "cur value" cannot come from inline asm.
379 if (PacketSU->getInstr()->isInlineAsm())
380 return false;
381
382 // Make sure candidate instruction uses cur.
383 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
384 MI->dump();
385 dbgs() << "in packet\n";);
386 MachineInstr *MJ = MII;
387 DEBUG(dbgs() << "Checking CUR against "; MJ->dump(););
388 unsigned DestReg = MI->getOperand(0).getReg();
389 bool FoundMatch = false;
390 for (auto &MO : MJ->operands())
391 if (MO.isReg() && MO.getReg() == DestReg)
392 FoundMatch = true;
393 if (!FoundMatch)
394 return false;
395
396 // Check for existing uses of a vector register within the packet which
397 // would be affected by converting a vector load into .cur formt.
398 for (auto BI : CurrentPacketMIs) {
399 DEBUG(dbgs() << "packet has "; BI->dump(););
400 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
401 return false;
402 }
403
404 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI->dump(););
405 // We can convert the opcode into a .cur.
406 return true;
407}
408
409// Promote an instruction to its .new form. At this time, we have already
410// made a call to canPromoteToDotNew and made sure that it can *indeed* be
411// promoted.
412bool HexagonPacketizerList::promoteToDotNew(MachineInstr* MI,
413 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
414 const TargetRegisterClass* RC) {
415 assert (DepType == SDep::Data);
416 int NewOpcode;
417 if (RC == &Hexagon::PredRegsRegClass)
418 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
419 else
420 NewOpcode = HII->getDotNewOp(MI);
421 MI->setDesc(HII->get(NewOpcode));
422 return true;
423}
424
425bool HexagonPacketizerList::demoteToDotOld(MachineInstr* MI) {
426 int NewOpcode = HII->getDotOldOp(MI->getOpcode());
427 MI->setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000428 return true;
429}
430
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000431enum PredicateKind {
432 PK_False,
433 PK_True,
434 PK_Unknown
435};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000436
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000437/// Returns true if an instruction is predicated on p0 and false if it's
438/// predicated on !p0.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000439static PredicateKind getPredicateSense(const MachineInstr *MI,
440 const HexagonInstrInfo *HII) {
441 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000442 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000443 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000444 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000445 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000446}
447
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000448static const MachineOperand &getPostIncrementOperand(const MachineInstr *MI,
449 const HexagonInstrInfo *HII) {
450 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000451#ifndef NDEBUG
452 // Post Increment means duplicates. Use dense map to find duplicates in the
453 // list. Caution: Densemap initializes with the minimum of 64 buckets,
454 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000455 DenseSet<unsigned> DefRegsSet;
456 for (auto &MO : MI->operands())
457 if (MO.isReg() && MO.isDef())
458 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000459
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000460 for (auto &MO : MI->operands())
461 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
462 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000463#else
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000464 if (MI->mayLoad()) {
465 MachineOperand &Op1 = MI->getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000466 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000467 assert(Op1.isReg() && "Post increment operand has be to a register.");
468 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000469 }
470 if (MI->getDesc().mayStore()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000471 MachineOperand &Op0 = MI->getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000472 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000473 assert(Op0.isReg() && "Post increment operand has be to a register.");
474 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000475 }
476#endif
477 // we should never come here.
478 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
479}
480
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000481// Get the value being stored.
482static const MachineOperand& getStoreValueOperand(const MachineInstr *MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000483 // value being stored is always the last operand.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000484 return MI->getOperand(MI->getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000485}
486
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000487static bool isLoadAbsSet(const MachineInstr *MI) {
488 unsigned Opc = MI->getOpcode();
489 switch (Opc) {
490 case Hexagon::L4_loadrd_ap:
491 case Hexagon::L4_loadrb_ap:
492 case Hexagon::L4_loadrh_ap:
493 case Hexagon::L4_loadrub_ap:
494 case Hexagon::L4_loadruh_ap:
495 case Hexagon::L4_loadri_ap:
496 return true;
497 }
498 return false;
499}
500
501static const MachineOperand &getAbsSetOperand(const MachineInstr *MI) {
502 assert(isLoadAbsSet(MI));
503 return MI->getOperand(1);
504}
505
506
507// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000508// Following restrictions are to be respected in convert a store into
509// a new value store.
510// 1. If an instruction uses auto-increment, its address register cannot
511// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000512// 2. If an instruction uses absolute-set addressing mode, its address
513// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000514// 3. If an instruction produces a 64-bit result, its registers cannot be used
515// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000516// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000517// the instruction that uses the new-value register must also be conditional,
518// and both must always have their predicates evaluate identically.
519// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000520// 5. There is an implied restriction that a packet cannot have another store,
521// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000522// already a store in a packet, there can not be a new value store.
523// Arch Spec: 3.4.4.2
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000524bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr *MI,
525 const MachineInstr *PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000526 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000527 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000528 return false;
529
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000530 // Make sure there is dependency and can be new value'd.
531 const MachineOperand &Val = getStoreValueOperand(MI);
532 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000533 return false;
534
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000535 const MCInstrDesc& MCID = PacketMI->getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000536
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000537 // First operand is always the result.
538 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
539 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
540 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000541 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000542
543 // New-value stores are of class NV (slot 0), dual stores require class ST
544 // in slot 0 (PRM 5.5).
545 for (auto I : CurrentPacketMIs) {
546 SUnit *PacketSU = MIToSUnit.find(I)->second;
547 if (PacketSU->getInstr()->mayStore())
548 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000549 }
550
551 // Make sure it's NOT the post increment register that we are going to
552 // new value.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000553 if (HII->isPostIncrement(MI) &&
554 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000555 return false;
556 }
557
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000558 if (HII->isPostIncrement(PacketMI) && PacketMI->mayLoad() &&
559 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
560 // If source is post_inc, or absolute-set addressing, it can not feed
561 // into new value store
562 // r3 = memw(r2++#4)
563 // memw(r30 + #-1404) = r2.new -> can not be new value store
564 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000565 return false;
566 }
567
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000568 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
569 return false;
570
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000571 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000572 // also be predicated.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000573 if (HII->isPredicated(PacketMI)) {
574 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000575 return false;
576
577 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000578 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000579 unsigned predRegNumSrc = 0;
580 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000581 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000582
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000583 // Get predicate register used in the source instruction.
584 for (auto &MO : PacketMI->operands()) {
585 if (!MO.isReg())
586 continue;
587 predRegNumSrc = MO.getReg();
588 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
589 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000590 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000591 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000592 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
593 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000594
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000595 // Get predicate register used in new-value store instruction.
596 for (auto &MO : MI->operands()) {
597 if (!MO.isReg())
598 continue;
599 predRegNumDst = MO.getReg();
600 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
601 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000602 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000603 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000604 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
605 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000606
607 // New-value register producer and user (store) need to satisfy these
608 // constraints:
609 // 1) Both instructions should be predicated on the same register.
610 // 2) If producer of the new-value register is .new predicated then store
611 // should also be .new predicated and if producer is not .new predicated
612 // then store should not be .new predicated.
613 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000614 // sense, i.e, either both should be negated or both should be non-negated.
615 if (predRegNumDst != predRegNumSrc ||
616 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
617 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000618 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000619 }
620
621 // Make sure that other than the new-value register no other store instruction
622 // register has been modified in the same packet. Predicate registers can be
623 // modified by they should not be modified between the producer and the store
624 // instruction as it will make them both conditional on different values.
625 // We already know this to be true for all the instructions before and
626 // including PacketMI. Howerver, we need to perform the check for the
627 // remaining instructions in the packet.
628
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000629 unsigned StartCheck = 0;
630
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000631 for (auto I : CurrentPacketMIs) {
632 SUnit *TempSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000633 MachineInstr* TempMI = TempSU->getInstr();
634
635 // Following condition is true for all the instructions until PacketMI is
636 // reached (StartCheck is set to 0 before the for loop).
637 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000638 if (TempMI != PacketMI && !StartCheck) // Start processing only after
639 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000640
641 StartCheck = 1;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000642 if (TempMI == PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000643 continue;
644
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000645 for (auto &MO : MI->operands())
646 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000647 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000648 }
649
Alp Tokerf907b892013-12-05 05:44:44 +0000650 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000651 // 1. The only use of reg is DepReg and no other registers.
652 // This handles V4 base+index registers.
653 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000654 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000655 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000656 if (!HII->isPostIncrement(MI)) {
657 for (unsigned opNum = 0; opNum < MI->getNumOperands()-1; opNum++) {
658 const MachineOperand &MO = MI->getOperand(opNum);
659 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000660 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000661 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000662 }
663
664 // If data definition is because of implicit definition of the register,
665 // do not newify the store. Eg.
666 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
667 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
668 for (auto &MO : PacketMI->operands()) {
669 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
670 continue;
671 unsigned R = MO.getReg();
672 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
673 return false;
674 }
675
676 // Handle imp-use of super reg case. There is a target independent side
677 // change that should prevent this situation but I am handling it for
678 // just-in-case. For example, we cannot newify R2 in the following case:
679 // %R3<def> = A2_tfrsi 0;
680 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
681 for (auto &MO : MI->operands()) {
682 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
683 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000684 }
685
686 // Can be dot new store.
687 return true;
688}
689
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000690// Can this MI to promoted to either new value store or new value jump.
691bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr *MI,
692 const SUnit *PacketSU, unsigned DepReg,
693 MachineBasicBlock::iterator &MII) {
694 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000695 return false;
696
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000697 // Check to see the store can be new value'ed.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000698 MachineInstr *PacketMI = PacketSU->getInstr();
699 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000700 return true;
701
702 // Check to see the compare/jump can be new value'ed.
703 // This is done as a pass on its own. Don't need to check it here.
704 return false;
705}
706
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000707static bool isImplicitDependency(const MachineInstr *I, unsigned DepReg) {
708 for (auto &MO : I->operands())
709 if (MO.isReg() && MO.isDef() && (MO.getReg() == DepReg) && MO.isImplicit())
710 return true;
711 return false;
712}
713
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000714// Check to see if an instruction can be dot new
715// There are three kinds.
716// 1. dot new on predicate - V2/V3/V4
717// 2. dot new on stores NV/ST - V4
718// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000719bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr *MI,
720 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
721 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000722 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000723 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000724 return false;
725
726 if (!isNewifiable(MI))
727 return false;
728
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000729 const MachineInstr *PI = PacketSU->getInstr();
730
731 // The "new value" cannot come from inline asm.
732 if (PI->isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000733 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000734
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000735 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
736 // sense.
737 if (PI->isImplicitDef())
738 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000739
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000740 // If dependency is trough an implicitly defined register, we should not
741 // newify the use.
742 if (isImplicitDependency(PI, DepReg))
743 return false;
744
745 const MCInstrDesc& MCID = PI->getDesc();
746 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
747 if (DisableVecDblNVStores && VecRC == &Hexagon::VecDblRegsRegClass)
748 return false;
749
750 // predicate .new
751 // bug 5670: until that is fixed
752 // TODO: MI->isIndirectBranch() and IsRegisterJump(MI)
753 if (RC == &Hexagon::PredRegsRegClass)
754 if (HII->isCondInst(MI) || MI->isReturn())
755 return HII->predCanBeUsedAsDotNew(PI, DepReg);
756
757 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
758 return false;
759
760 // Create a dot new machine instruction to see if resources can be
761 // allocated. If not, bail out now.
762 int NewOpcode = HII->getDotNewOp(MI);
763 const MCInstrDesc &D = HII->get(NewOpcode);
764 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
765 bool ResourcesAvailable = ResourceTracker->canReserveResources(NewMI);
766 MF.DeleteMachineInstr(NewMI);
767 if (!ResourcesAvailable)
768 return false;
769
770 // New Value Store only. New Value Jump generated as a separate pass.
771 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
772 return false;
773
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000774 return true;
775}
776
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000777// Go through the packet instructions and search for an anti dependency between
778// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000779// Trying to add
780// a) %R1<def> = TFRI_cdNotPt %P3, 2
781// to this packet:
782// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000783// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
784// c) %P3<def> = C2_tfrrp %R23
785// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000786// }
787// The P3 from a) and d) will be complements after
788// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000789// Anti-dep between c) and b) is irrelevant for this case
790bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr* MI,
791 unsigned DepReg) {
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000792 SUnit *PacketSUDep = MIToSUnit.find(MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000793
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000794 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000795 // We only care for dependencies to predicated instructions
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000796 if (!HII->isPredicated(I))
797 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000798
799 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000800 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000801
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000802 // Look at dependencies between current members of the packet and
803 // predicate defining instruction MI. Make sure that dependency is
804 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000805 if (PacketSU->isSucc(PacketSUDep)) {
806 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000807 auto &Dep = PacketSU->Succs[i];
808 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
809 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000810 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000811 }
812 }
813 }
814
815 return false;
816}
817
818
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000819/// Gets the predicate register of a predicated instruction.
Benjamin Kramere79beac2013-05-23 15:43:11 +0000820static unsigned getPredicatedRegister(MachineInstr *MI,
821 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000822 /// We use the following rule: The first predicate register that is a use is
823 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000824 assert(QII->isPredicated(MI) && "Must be predicated instruction");
825
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000826 for (auto &Op : MI->operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000827 if (Op.isReg() && Op.getReg() && Op.isUse() &&
828 Hexagon::PredRegsRegClass.contains(Op.getReg()))
829 return Op.getReg();
830 }
831
832 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000833 return 0;
834}
835
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000836// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000837// the predicates are complements.
838bool HexagonPacketizerList::arePredicatesComplements(MachineInstr *MI1,
839 MachineInstr *MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000840 // If we don't know the predicate sense of the instructions bail out early, we
841 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000842 if (getPredicateSense(MI1, HII) == PK_Unknown ||
843 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000844 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000845
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000846 // Scheduling unit for candidate.
847 SUnit *SU = MIToSUnit[MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000848
849 // One corner case deals with the following scenario:
850 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000851 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000852 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000853 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000854 // b) %R25<def> = A2_tfrf %P0, %R24
855 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000856 // }
857 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000858 // On general check a) and b) are complements, but presence of c) will
859 // convert a) to .new form, and then it is not a complement.
860 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000861
862 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000863 // Look for Anti dependecy on the same predicate reg as used in the
864 // candidate.
865 for (auto I : CurrentPacketMIs) {
866 // Scheduling Unit for current insn in the packet.
867 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000868
869 // If this instruction in the packet is succeeded by the candidate...
870 if (PacketSU->isSucc(SU)) {
871 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000872 auto Dep = PacketSU->Succs[i];
873 // The corner case exist when there is true data dependency between
874 // candidate and one of current packet members, this dep is on
875 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000876 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000877 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
878 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
879 // Here I know that I is predicate setting instruction with true
880 // data dep to candidate on the register we care about - c) in the
881 // above example. Now I need to see if there is an anti dependency
882 // from c) to any other instruction in the same packet on the pred
883 // reg of interest.
884 if (restrictingDepExistInPacket(I, Dep.getReg()))
885 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000886 }
887 }
888 }
889 }
890
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000891 // If the above case does not apply, check regular complement condition.
892 // Check that the predicate register is the same and that the predicate
893 // sense is different We also need to differentiate .old vs. .new: !p0
894 // is not complementary to p0.new.
895 unsigned PReg1 = getPredicatedRegister(MI1, HII);
896 unsigned PReg2 = getPredicatedRegister(MI2, HII);
897 return PReg1 == PReg2 &&
898 Hexagon::PredRegsRegClass.contains(PReg1) &&
899 Hexagon::PredRegsRegClass.contains(PReg2) &&
900 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
901 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000902}
903
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000904// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000905void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000906 Dependence = false;
907 PromotedToDotNew = false;
908 GlueToNewValueJump = false;
909 GlueAllocframeStore = false;
910 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000911}
912
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000913// Ignore bundling of pseudo instructions.
Krzysztof Parzyszekd44a1fd2015-12-14 18:54:44 +0000914bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr *MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000915 const MachineBasicBlock*) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000916 if (MI->isDebugValue())
917 return true;
918
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000919 if (MI->isCFIInstruction())
920 return false;
921
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000922 // We must print out inline assembly.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000923 if (MI->isInlineAsm())
924 return false;
925
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000926 if (MI->isImplicitDef())
927 return false;
928
929 // We check if MI has any functional units mapped to it. If it doesn't,
930 // we ignore the instruction.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000931 const MCInstrDesc& TID = MI->getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000932 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +0000933 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000934 return !FuncUnits;
935}
936
Krzysztof Parzyszekd44a1fd2015-12-14 18:54:44 +0000937bool HexagonPacketizerList::isSoloInstruction(const MachineInstr *MI) {
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000938 if (MI->isEHLabel() || MI->isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000939 return true;
940
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000941 // Consider inline asm to not be a solo instruction by default.
942 // Inline asm will be put in a packet temporarily, but then it will be
943 // removed, and placed outside of the packet (before or after, depending
944 // on dependencies). This is to reduce the impact of inline asm as a
945 // "packet splitting" instruction.
946 if (MI->isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000947 return true;
948
949 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
950 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
951 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000952 if (isSchedBarrier(MI))
953 return true;
954
955 if (HII->isSolo(MI))
956 return true;
957
958 if (MI->getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000959 return true;
960
961 return false;
962}
963
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000964
965// Quick check if instructions MI and MJ cannot coexist in the same packet.
966// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
967// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
968// For full test call this function twice:
969// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
970// Doing the test only one way saves the amount of code in this function,
971// since every test would need to be repeated with the MI and MJ reversed.
972static bool cannotCoexistAsymm(const MachineInstr *MI, const MachineInstr *MJ,
973 const HexagonInstrInfo &HII) {
974 const MachineFunction *MF = MI->getParent()->getParent();
975 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
976 HII.isHVXMemWithAIndirect(MI, MJ))
977 return true;
978
979 // An inline asm cannot be together with a branch, because we may not be
980 // able to remove the asm out after packetizing (i.e. if the asm must be
981 // moved past the bundle). Similarly, two asms cannot be together to avoid
982 // complications when determining their relative order outside of a bundle.
983 if (MI->isInlineAsm())
984 return MJ->isInlineAsm() || MJ->isBranch() || MJ->isBarrier() ||
985 MJ->isCall() || MJ->isTerminator();
986
987 // "False" really means that the quick check failed to determine if
988 // I and J cannot coexist.
989 return false;
990}
991
992
993// Full, symmetric check.
994bool HexagonPacketizerList::cannotCoexist(const MachineInstr *MI,
995 const MachineInstr *MJ) {
996 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
997}
998
999void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1000 for (auto &B : MF) {
1001 MachineBasicBlock::iterator BundleIt;
1002 MachineBasicBlock::instr_iterator NextI;
1003 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1004 NextI = std::next(I);
1005 MachineInstr *MI = &*I;
1006 if (MI->isBundle())
1007 BundleIt = I;
1008 if (!MI->isInsideBundle())
1009 continue;
1010
1011 // Decide on where to insert the instruction that we are pulling out.
1012 // Debug instructions always go before the bundle, but the placement of
1013 // INLINE_ASM depends on potential dependencies. By default, try to
1014 // put it before the bundle, but if the asm writes to a register that
1015 // other instructions in the bundle read, then we need to place it
1016 // after the bundle (to preserve the bundle semantics).
1017 bool InsertBeforeBundle;
1018 if (MI->isInlineAsm())
1019 InsertBeforeBundle = !hasWriteToReadDep(MI, BundleIt, HRI);
1020 else if (MI->isDebugValue())
1021 InsertBeforeBundle = true;
1022 else
1023 continue;
1024
1025 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1026 }
1027 }
1028}
1029
1030// Check if a given instruction is of class "system".
1031static bool isSystemInstr(const MachineInstr *MI) {
1032 unsigned Opc = MI->getOpcode();
1033 switch (Opc) {
1034 case Hexagon::Y2_barrier:
1035 case Hexagon::Y2_dcfetchbo:
1036 return true;
1037 }
1038 return false;
1039}
1040
1041bool HexagonPacketizerList::hasDeadDependence(const MachineInstr *I,
1042 const MachineInstr *J) {
1043 // The dependence graph may not include edges between dead definitions,
1044 // so without extra checks, we could end up packetizing two instruction
1045 // defining the same (dead) register.
1046 if (I->isCall() || J->isCall())
1047 return false;
1048 if (HII->isPredicated(I) || HII->isPredicated(J))
1049 return false;
1050
1051 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
1052 for (auto &MO : I->operands()) {
1053 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1054 continue;
1055 DeadDefs[MO.getReg()] = true;
1056 }
1057
1058 for (auto &MO : J->operands()) {
1059 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1060 continue;
1061 unsigned R = MO.getReg();
1062 if (R != Hexagon::USR_OVF && DeadDefs[R])
1063 return true;
1064 }
1065 return false;
1066}
1067
1068bool HexagonPacketizerList::hasControlDependence(const MachineInstr *I,
1069 const MachineInstr *J) {
1070 // A save callee-save register function call can only be in a packet
1071 // with instructions that don't write to the callee-save registers.
1072 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1073 doesModifyCalleeSavedReg(J, HRI)) ||
1074 (HII->isSaveCalleeSavedRegsCall(J) &&
1075 doesModifyCalleeSavedReg(I, HRI)))
1076 return true;
1077
1078 // Two control flow instructions cannot go in the same packet.
1079 if (isControlFlow(I) && isControlFlow(J))
1080 return true;
1081
1082 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1083 // contain a speculative indirect jump,
1084 // a new-value compare jump or a dealloc_return.
1085 auto isBadForLoopN = [this] (const MachineInstr *MI) -> bool {
1086 if (MI->isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
1087 return true;
1088 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
1089 return true;
1090 return false;
1091 };
1092
1093 if (HII->isLoopN(I) && isBadForLoopN(J))
1094 return true;
1095 if (HII->isLoopN(J) && isBadForLoopN(I))
1096 return true;
1097
1098 // dealloc_return cannot appear in the same packet as a conditional or
1099 // unconditional jump.
1100 return HII->isDeallocRet(I) &&
1101 (J->isBranch() || J->isCall() || J->isBarrier());
1102}
1103
1104bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr *I,
1105 const MachineInstr *J) {
1106 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
1107 bool StoreI = I->mayStore(), StoreJ = J->mayStore();
1108 if ((SysI && StoreJ) || (SysJ && StoreI))
1109 return true;
1110
1111 if (StoreI && StoreJ) {
1112 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1113 return true;
1114 } else {
1115 // A memop cannot be in the same packet with another memop or a store.
1116 // Two stores can be together, but here I and J cannot both be stores.
1117 bool MopStI = HII->isMemOp(I) || StoreI;
1118 bool MopStJ = HII->isMemOp(J) || StoreJ;
1119 if (MopStI && MopStJ)
1120 return true;
1121 }
1122
1123 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1124}
1125
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001126// SUI is the current instruction that is out side of the current packet.
1127// SUJ is the current instruction inside the current packet against which that
1128// SUI will be packetized.
1129bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
1130 MachineInstr *I = SUI->getInstr();
1131 MachineInstr *J = SUJ->getInstr();
1132 assert(I && J && "Unable to packetize null instruction!");
1133
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001134 // Clear IgnoreDepMIs when Packet starts.
1135 if (CurrentPacketMIs.size() == 1)
1136 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001137
1138 MachineBasicBlock::iterator II = I;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001139 const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001140
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001141 // Solo instructions cannot go in the packet.
1142 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001143
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001144 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001145 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001146
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001147 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1148 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001149 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001150
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001151 // V4 allows dual stores. It does not allow second store, if the first
1152 // store is not in SLOT0. New value store, new value jump, dealloc_return
1153 // and memop always take SLOT0. Arch spec 3.4.4.2.
1154 Dependence = hasV4SpecificDependence(I, J);
1155 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001156 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001157
1158 // If an instruction feeds new value jump, glue it.
1159 MachineBasicBlock::iterator NextMII = I;
1160 ++NextMII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001161 if (NextMII != I->getParent()->end() && HII->isNewValueJump(NextMII)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001162 MachineInstr *NextMI = NextMII;
1163
1164 bool secondRegMatch = false;
1165 bool maintainNewValueJump = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001166 const MachineOperand &NOp0 = NextMI->getOperand(0);
1167 const MachineOperand &NOp1 = NextMI->getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001168
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001169 if (NOp1.isReg() && I->getOperand(0).getReg() == NOp1.getReg()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001170 secondRegMatch = true;
1171 maintainNewValueJump = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001172 } else if (I->getOperand(0).getReg() == NOp0.getReg()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001173 maintainNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001174 }
1175
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001176 for (auto I : CurrentPacketMIs) {
1177 SUnit *PacketSU = MIToSUnit.find(I)->second;
1178 MachineInstr *PI = PacketSU->getInstr();
1179 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
1180 if (PI->isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001181 Dependence = true;
1182 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001183 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001184 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001185 // 1. Packet does not have a store in it.
1186 // 2. If the first operand of the nvj is newified, and the second
1187 // operand is also a reg, it (second reg) is not defined in
1188 // the same packet.
1189 // 3. If the second operand of the nvj is newified, (which means
1190 // first operand is also a reg), first reg is not defined in
1191 // the same packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001192 if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() ||
1193 HII->isLoopN(PI)) {
1194 Dependence = true;
1195 break;
1196 }
1197 // Check #2/#3.
1198 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
1199 if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001200 Dependence = true;
1201 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001202 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001203 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001204
1205 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001206 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001207 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001208 }
1209
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001210 // There no dependency between a prolog instruction and its successor.
1211 if (!SUJ->isSucc(SUI))
1212 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001213
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001214 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1215 if (FoundSequentialDependence)
1216 break;
1217
1218 if (SUJ->Succs[i].getSUnit() != SUI)
1219 continue;
1220
1221 SDep::Kind DepType = SUJ->Succs[i].getKind();
1222 // For direct calls:
1223 // Ignore register dependences for call instructions for packetization
1224 // purposes except for those due to r31 and predicate registers.
1225 //
1226 // For indirect calls:
1227 // Same as direct calls + check for true dependences to the register
1228 // used in the indirect call.
1229 //
1230 // We completely ignore Order dependences for call instructions.
1231 //
1232 // For returns:
1233 // Ignore register dependences for return instructions like jumpr,
1234 // dealloc return unless we have dependencies on the explicit uses
1235 // of the registers used by jumpr (like r31) or dealloc return
1236 // (like r29 or r30).
1237 //
1238 // TODO: Currently, jumpr is handling only return of r31. So, the
1239 // following logic (specificaly isCallDependent) is working fine.
1240 // We need to enable jumpr for register other than r31 and then,
1241 // we need to rework the last part, where it handles indirect call
1242 // of that (isCallDependent) function. Bug 6216 is opened for this.
1243 unsigned DepReg = 0;
1244 const TargetRegisterClass *RC = nullptr;
1245 if (DepType == SDep::Data) {
1246 DepReg = SUJ->Succs[i].getReg();
1247 RC = HRI->getMinimalPhysRegClass(DepReg);
1248 }
1249
1250 if (I->isCall() || I->isReturn()) {
1251 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001252 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001253 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1254 continue;
1255 }
1256
1257 if (DepType == SDep::Data) {
1258 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1259 if (promoteToDotCur(J, DepType, II, RC))
1260 continue;
1261 }
1262
1263 // Data dpendence ok if we have load.cur.
1264 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
1265 if (HII->isV60VectorInstruction(I))
1266 continue;
1267 }
1268
1269 // For instructions that can be promoted to dot-new, try to promote.
1270 if (DepType == SDep::Data) {
1271 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1272 if (promoteToDotNew(I, DepType, II, RC)) {
1273 PromotedToDotNew = true;
1274 continue;
1275 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001276 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001277 if (HII->isNewValueJump(I))
1278 continue;
1279 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001280
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001281 // For predicated instructions, if the predicates are complements then
1282 // there can be no dependence.
1283 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1284 arePredicatesComplements(I, J)) {
1285 // Not always safe to do this translation.
1286 // DAG Builder attempts to reduce dependence edges using transitive
1287 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001288 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001289 // r0 = tfr_pt ... (1)
1290 // r0 = tfr_pf ... (2)
1291 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001292 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001293 // There will be an output dependence between (1)->(2) and (2)->(3).
1294 // However, there is no dependence edge between (1)->(3). This results
1295 // in all 3 instructions going in the same packet. We ignore dependce
1296 // only once to avoid this situation.
1297 auto Itr = std::find(IgnoreDepMIs.begin(), IgnoreDepMIs.end(), J);
1298 if (Itr != IgnoreDepMIs.end()) {
1299 Dependence = true;
1300 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001301 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001302 IgnoreDepMIs.push_back(I);
1303 continue;
1304 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001305
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001306 // Ignore Order dependences between unconditional direct branches
1307 // and non-control-flow instructions.
1308 if (isDirectJump(I) && !J->isBranch() && !J->isCall() &&
1309 DepType == SDep::Order)
1310 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001311
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001312 // Ignore all dependences for jumps except for true and output
1313 // dependences.
1314 if (I->isConditionalBranch() && DepType != SDep::Data &&
1315 DepType != SDep::Output)
1316 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001317
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001318 // Ignore output dependences due to superregs. We can write to two
1319 // different subregisters of R1:0 for instance in the same cycle.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001320
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001321 // If neither I nor J defines DepReg, then this is a superfluous output
1322 // dependence. The dependence must be of the form:
1323 // R0 = ...
1324 // R1 = ...
1325 // and there is an output dependence between the two instructions with
1326 // DepReg = D0.
1327 // We want to ignore these dependences. Ideally, the dependence
1328 // constructor should annotate such dependences. We can then avoid this
1329 // relatively expensive check.
1330 //
1331 if (DepType == SDep::Output) {
1332 // DepReg is the register that's responsible for the dependence.
1333 unsigned DepReg = SUJ->Succs[i].getReg();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001334
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001335 // Check if I and J really defines DepReg.
1336 if (!I->definesRegister(DepReg) && !J->definesRegister(DepReg))
1337 continue;
1338 FoundSequentialDependence = true;
1339 break;
1340 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001341
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001342 // For Order dependences:
1343 // 1. On V4 or later, volatile loads/stores can be packetized together,
1344 // unless other rules prevent is.
1345 // 2. Store followed by a load is not allowed.
1346 // 3. Store followed by a store is only valid on V4 or later.
1347 // 4. Load followed by any memory operation is allowed.
1348 if (DepType == SDep::Order) {
1349 if (!PacketizeVolatiles) {
1350 bool OrdRefs = I->hasOrderedMemoryRef() || J->hasOrderedMemoryRef();
1351 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001352 FoundSequentialDependence = true;
1353 break;
1354 }
1355 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001356 // J is first, I is second.
1357 bool LoadJ = J->mayLoad(), StoreJ = J->mayStore();
1358 bool LoadI = I->mayLoad(), StoreI = I->mayStore();
1359 if (StoreJ) {
1360 // Two stores are only allowed on V4+. Load following store is never
1361 // allowed.
1362 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001363 FoundSequentialDependence = true;
1364 break;
1365 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001366 } else if (!LoadJ || (!LoadI && !StoreI)) {
1367 // If J is neither load nor store, assume a dependency.
1368 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001369 FoundSequentialDependence = true;
1370 break;
1371 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001372 // Store followed by store: not OK on V2.
1373 // Store followed by load: not OK on all.
1374 // Load followed by store: OK on all.
1375 // Load followed by load: OK on all.
1376 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001377 }
1378
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001379 // For V4, special case ALLOCFRAME. Even though there is dependency
1380 // between ALLOCFRAME and subsequent store, allow it to be packetized
1381 // in a same packet. This implies that the store is using the caller's
1382 // SP. Hence, offset needs to be updated accordingly.
1383 if (DepType == SDep::Data && J->getOpcode() == Hexagon::S2_allocframe) {
1384 unsigned Opc = I->getOpcode();
1385 switch (Opc) {
1386 case Hexagon::S2_storerd_io:
1387 case Hexagon::S2_storeri_io:
1388 case Hexagon::S2_storerh_io:
1389 case Hexagon::S2_storerb_io:
1390 if (I->getOperand(0).getReg() == HRI->getStackRegister()) {
1391 int64_t Imm = I->getOperand(1).getImm();
1392 int64_t NewOff = Imm - (FrameSize + HEXAGON_LRFP_SIZE);
1393 if (HII->isValidOffset(Opc, NewOff)) {
1394 GlueAllocframeStore = true;
1395 // Since this store is to be glued with allocframe in the same
1396 // packet, it will use SP of the previous stack frame, i.e.
1397 // caller's SP. Therefore, we need to recalculate offset
1398 // according to this change.
1399 I->getOperand(1).setImm(NewOff);
1400 continue;
1401 }
1402 }
1403 default:
1404 break;
1405 }
1406 }
1407
1408 // Skip over anti-dependences. Two instructions that are anti-dependent
1409 // can share a packet.
1410 if (DepType != SDep::Anti) {
1411 FoundSequentialDependence = true;
1412 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001413 }
1414 }
1415
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001416 if (FoundSequentialDependence) {
1417 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001418 return false;
1419 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001420
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001421 return true;
1422}
1423
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001424bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
1425 MachineInstr *I = SUI->getInstr();
1426 MachineInstr *J = SUJ->getInstr();
1427 assert(I && J && "Unable to packetize null instruction!");
1428
1429 if (cannotCoexist(I, J))
1430 return false;
1431
1432 if (!Dependence)
1433 return true;
1434
1435 // Check if the instruction was promoted to a dot-new. If so, demote it
1436 // back into a dot-old.
1437 if (PromotedToDotNew)
1438 demoteToDotOld(I);
1439
1440 cleanUpDotCur();
1441 // Check if the instruction (must be a store) was glued with an allocframe
1442 // instruction. If so, restore its offset to its original value, i.e. use
1443 // current SP instead of caller's SP.
1444 if (GlueAllocframeStore) {
1445 unsigned FrameSize = MF.getFrameInfo()->getStackSize();
1446 MachineOperand &MOff = I->getOperand(1);
1447 MOff.setImm(MOff.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
1448 }
1449 return false;
1450}
1451
1452
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001453MachineBasicBlock::iterator
1454HexagonPacketizerList::addToPacket(MachineInstr *MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001455 MachineBasicBlock::iterator MII = MI;
1456 MachineBasicBlock *MBB = MI->getParent();
1457 if (MI->isImplicitDef()) {
1458 unsigned R = MI->getOperand(0).getReg();
1459 if (Hexagon::IntRegsRegClass.contains(R)) {
1460 MCSuperRegIterator S(R, HRI, false);
1461 MI->addOperand(MachineOperand::CreateReg(*S, true, true));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001462 }
1463 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001464 }
1465 assert(ResourceTracker->canReserveResources(MI));
1466
1467 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
1468 bool Good = true;
1469
1470 if (GlueToNewValueJump) {
1471 MachineInstr *NvjMI = ++MII;
1472 // We need to put both instructions in the same packet: MI and NvjMI.
1473 // Either of them can require a constant extender. Try to add both to
1474 // the current packet, and if that fails, end the packet and start a
1475 // new one.
1476 ResourceTracker->reserveResources(MI);
1477 if (ExtMI)
1478 Good = tryAllocateResourcesForConstExt(true);
1479
1480 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
1481 if (Good) {
1482 if (ResourceTracker->canReserveResources(NvjMI))
1483 ResourceTracker->reserveResources(NvjMI);
1484 else
1485 Good = false;
1486 }
1487 if (Good && ExtNvjMI)
1488 Good = tryAllocateResourcesForConstExt(true);
1489
1490 if (!Good) {
1491 endPacket(MBB, MI);
1492 assert(ResourceTracker->canReserveResources(MI));
1493 ResourceTracker->reserveResources(MI);
1494 if (ExtMI) {
1495 assert(canReserveResourcesForConstExt());
1496 tryAllocateResourcesForConstExt(true);
1497 }
1498 assert(ResourceTracker->canReserveResources(NvjMI));
1499 ResourceTracker->reserveResources(NvjMI);
1500 if (ExtNvjMI) {
1501 assert(canReserveResourcesForConstExt());
1502 reserveResourcesForConstExt();
1503 }
1504 }
1505 CurrentPacketMIs.push_back(MI);
1506 CurrentPacketMIs.push_back(NvjMI);
1507 return MII;
1508 }
1509
1510 ResourceTracker->reserveResources(MI);
1511 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1512 endPacket(MBB, MI);
1513 if (PromotedToDotNew)
1514 demoteToDotOld(MI);
1515 ResourceTracker->reserveResources(MI);
1516 reserveResourcesForConstExt();
1517 }
1518
1519 CurrentPacketMIs.push_back(MI);
1520 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001521}
1522
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001523void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
1524 MachineInstr *MI) {
1525 OldPacketMIs = CurrentPacketMIs;
1526 VLIWPacketizerList::endPacket(MBB, MI);
1527}
1528
1529bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr *MI) {
1530 return !producesStall(MI);
1531}
1532
1533
1534// Return true when ConsMI uses a register defined by ProdMI.
1535static bool isDependent(const MachineInstr *ProdMI,
1536 const MachineInstr *ConsMI) {
1537 if (!ProdMI->getOperand(0).isReg())
1538 return false;
1539 unsigned DstReg = ProdMI->getOperand(0).getReg();
1540
1541 for (auto &Op : ConsMI->operands())
1542 if (Op.isReg() && Op.isUse() && Op.getReg() == DstReg)
1543 // The MIs depend on each other.
1544 return true;
1545
1546 return false;
1547}
1548
1549// V60 forward scheduling.
1550bool HexagonPacketizerList::producesStall(const MachineInstr *I) {
1551 // Check whether the previous packet is in a different loop. If this is the
1552 // case, there is little point in trying to avoid a stall because that would
1553 // favor the rare case (loop entry) over the common case (loop iteration).
1554 //
1555 // TODO: We should really be able to check all the incoming edges if this is
1556 // the first packet in a basic block, so we can avoid stalls from the loop
1557 // backedge.
1558 if (!OldPacketMIs.empty()) {
1559 auto *OldBB = OldPacketMIs.front()->getParent();
1560 auto *ThisBB = I->getParent();
1561 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1562 return false;
1563 }
1564
1565 // Check for stall between two vector instructions.
1566 if (HII->isV60VectorInstruction(I)) {
1567 for (auto J : OldPacketMIs) {
1568 if (!HII->isV60VectorInstruction(J))
1569 continue;
1570 if (isDependent(J, I) && !HII->isVecUsableNextPacket(J, I))
1571 return true;
1572 }
1573 return false;
1574 }
1575
1576 // Check for stall between two scalar instructions. First, check that
1577 // there is no definition of a use in the current packet, because it
1578 // may be a candidate for .new.
1579 for (auto J : CurrentPacketMIs)
1580 if (!HII->isV60VectorInstruction(J) && isDependent(J, I))
1581 return false;
1582
1583 // Check for stall between I and instructions in the previous packet.
1584 if (MF.getSubtarget<HexagonSubtarget>().useBSBScheduling()) {
1585 for (auto J : OldPacketMIs) {
1586 if (HII->isV60VectorInstruction(J))
1587 continue;
1588 if (!HII->isLateInstrFeedsEarlyInstr(J, I))
1589 continue;
1590 if (isDependent(J, I) && !HII->canExecuteInBundle(J, I))
1591 return true;
1592 }
1593 }
1594
1595 return false;
1596}
1597
1598
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001599//===----------------------------------------------------------------------===//
1600// Public Constructor Functions
1601//===----------------------------------------------------------------------===//
1602
1603FunctionPass *llvm::createHexagonPacketizer() {
1604 return new HexagonPacketizer();
1605}
1606