blob: e0f8b44a801766addb5aae4bad28dc8e0a87c766 [file] [log] [blame]
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001//===----- HexagonPacketizer.cpp - vliw packetizer ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements a simple VLIW packetizer using DFA. The packetizer works on
11// machine basic blocks. For each instruction I in BB, the packetizer consults
12// the DFA to see if machine resources are available to execute I. If so, the
13// packetizer checks if I depends on any instruction J in the current packet.
14// If no dependency is found, I is added to current packet and machine resource
15// is marked as taken. If any dependency is found, a target API call is made to
16// prune the dependence.
17//
18//===----------------------------------------------------------------------===//
Jyotsna Verma84256432013-03-01 17:37:13 +000019#include "HexagonRegisterInfo.h"
20#include "HexagonSubtarget.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "HexagonTargetMachine.h"
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000022#include "HexagonVLIWPacketizer.h"
23#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "llvm/CodeGen/MachineDominators.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "llvm/CodeGen/MachineFunctionAnalysis.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000027#include "llvm/CodeGen/MachineLoopInfo.h"
28#include "llvm/CodeGen/MachineRegisterInfo.h"
29#include "llvm/CodeGen/Passes.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/Support/Debug.h"
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000032
33using namespace llvm;
34
Chandler Carruth84e68b22014-04-22 02:41:26 +000035#define DEBUG_TYPE "packets"
36
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000037static cl::opt<bool> DisablePacketizer("disable-packetizer", cl::Hidden,
38 cl::ZeroOrMore, cl::init(false),
39 cl::desc("Disable Hexagon packetizer pass"));
40
Jyotsna Verma1d297502013-05-02 15:39:30 +000041static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000042 cl::ZeroOrMore, cl::Hidden, cl::init(true),
43 cl::desc("Allow non-solo packetization of volatile memory references"));
44
45static cl::opt<bool> EnableGenAllInsnClass("enable-gen-insn", cl::init(false),
46 cl::Hidden, cl::ZeroOrMore, cl::desc("Generate all instruction with TC"));
47
48static cl::opt<bool> DisableVecDblNVStores("disable-vecdbl-nv-stores",
49 cl::init(false), cl::Hidden, cl::ZeroOrMore,
50 cl::desc("Disable vector double new-value-stores"));
51
52extern cl::opt<bool> ScheduleInlineAsm;
Jyotsna Verma1d297502013-05-02 15:39:30 +000053
Jyotsna Verma1d297502013-05-02 15:39:30 +000054namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000055 FunctionPass *createHexagonPacketizer();
Jyotsna Verma1d297502013-05-02 15:39:30 +000056 void initializeHexagonPacketizerPass(PassRegistry&);
57}
58
59
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000060namespace {
61 class HexagonPacketizer : public MachineFunctionPass {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000062 public:
63 static char ID;
Jyotsna Verma1d297502013-05-02 15:39:30 +000064 HexagonPacketizer() : MachineFunctionPass(ID) {
65 initializeHexagonPacketizerPass(*PassRegistry::getPassRegistry());
66 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000067
Craig Topper906c2cd2014-04-29 07:58:16 +000068 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000069 AU.setPreservesCFG();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000070 AU.addRequired<AAResultsWrapperPass>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000071 AU.addRequired<MachineBranchProbabilityInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000072 AU.addRequired<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000073 AU.addRequired<MachineLoopInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000074 AU.addPreserved<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000075 AU.addPreserved<MachineLoopInfo>();
76 MachineFunctionPass::getAnalysisUsage(AU);
77 }
Craig Topper906c2cd2014-04-29 07:58:16 +000078 const char *getPassName() const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000079 return "Hexagon Packetizer";
80 }
Craig Topper906c2cd2014-04-29 07:58:16 +000081 bool runOnMachineFunction(MachineFunction &Fn) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000082 MachineFunctionProperties getRequiredProperties() const override {
83 return MachineFunctionProperties().set(
84 MachineFunctionProperties::Property::AllVRegsAllocated);
85 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000086
87 private:
88 const HexagonInstrInfo *HII;
89 const HexagonRegisterInfo *HRI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000090 };
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000091
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000092 char HexagonPacketizer::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000093}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000094
Jyotsna Verma1d297502013-05-02 15:39:30 +000095INITIALIZE_PASS_BEGIN(HexagonPacketizer, "packets", "Hexagon Packetizer",
96 false, false)
97INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
98INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
99INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000100INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000101INITIALIZE_PASS_END(HexagonPacketizer, "packets", "Hexagon Packetizer",
102 false, false)
103
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000104HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
105 MachineLoopInfo &MLI, AliasAnalysis *AA,
106 const MachineBranchProbabilityInfo *MBPI)
107 : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI) {
108 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
109 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000110
111 addMutation(make_unique<HexagonSubtarget::HexagonDAGMutation>());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000112}
113
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000114// Check if FirstI modifies a register that SecondI reads.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000115static bool hasWriteToReadDep(const MachineInstr &FirstI,
116 const MachineInstr &SecondI,
117 const TargetRegisterInfo *TRI) {
118 for (auto &MO : FirstI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000119 if (!MO.isReg() || !MO.isDef())
120 continue;
121 unsigned R = MO.getReg();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000122 if (SecondI.readsRegister(R, TRI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000123 return true;
124 }
125 return false;
126}
127
128
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000129static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000130 MachineBasicBlock::iterator BundleIt, bool Before) {
131 MachineBasicBlock::instr_iterator InsertPt;
132 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000133 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000134 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000135 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000136
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000137 MachineBasicBlock &B = *MI.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000138 // The instruction should at least be bundled with the preceding instruction
139 // (there will always be one, i.e. BUNDLE, if nothing else).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000140 assert(MI.isBundledWithPred());
141 if (MI.isBundledWithSucc()) {
142 MI.clearFlag(MachineInstr::BundledSucc);
143 MI.clearFlag(MachineInstr::BundledPred);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000144 } else {
145 // If it's not bundled with the successor (i.e. it is the last one
146 // in the bundle), then we can simply unbundle it from the predecessor,
147 // which will take care of updating the predecessor's flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000148 MI.unbundleFromPred();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000149 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000150 B.splice(InsertPt, &B, MI.getIterator());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000151
152 // Get the size of the bundle without asserting.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000153 MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000154 MachineBasicBlock::const_instr_iterator E = B.instr_end();
155 unsigned Size = 0;
156 for (++I; I != E && I->isBundledWithPred(); ++I)
157 ++Size;
158
159 // If there are still two or more instructions, then there is nothing
160 // else to be done.
161 if (Size > 1)
162 return BundleIt;
163
164 // Otherwise, extract the single instruction out and delete the bundle.
165 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000166 MachineInstr &SingleI = *BundleIt->getNextNode();
167 SingleI.unbundleFromPred();
168 assert(!SingleI.isBundledWithSucc());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000169 BundleIt->eraseFromParent();
170 return NextIt;
171}
172
173
174bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000175 if (DisablePacketizer || skipFunction(*MF.getFunction()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000176 return false;
177
178 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
179 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
180 auto &MLI = getAnalysis<MachineLoopInfo>();
181 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
182 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
183
184 if (EnableGenAllInsnClass)
185 HII->genAllInsnTimingClasses(MF);
186
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000187 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000188 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000189
190 // DFA state table should not be empty.
191 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
192
193 //
194 // Loop over all basic blocks and remove KILL pseudo-instructions
195 // These instructions confuse the dependence analysis. Consider:
196 // D0 = ... (Insn 0)
197 // R0 = KILL R0, D0 (Insn 1)
198 // R0 = ... (Insn 2)
199 // Here, Insn 1 will result in the dependence graph not emitting an output
200 // dependence between Insn 0 and Insn 2. This can lead to incorrect
201 // packetization
202 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000203 for (auto &MB : MF) {
204 auto End = MB.end();
205 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000206 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000207 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000208 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000209 MB.erase(MI);
210 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000211 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000212 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000213 }
214 }
215
216 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000217 for (auto &MB : MF) {
218 auto Begin = MB.begin(), End = MB.end();
219 while (Begin != End) {
220 // First the first non-boundary starting from the end of the last
221 // scheduling region.
222 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000223 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000224 ++RB;
225 // First the first boundary starting from the beginning of the new
226 // region.
227 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000228 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000229 ++RE;
230 // Add the scheduling boundary if it's not block end.
231 if (RE != End)
232 ++RE;
233 // If RB == End, then RE == End.
234 if (RB != End)
235 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000236
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000237 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000238 }
239 }
240
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000241 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000242 return true;
243}
244
245
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000246// Reserve resources for a constant extender. Trigger an assertion if the
247// reservation fails.
248void HexagonPacketizerList::reserveResourcesForConstExt() {
249 if (!tryAllocateResourcesForConstExt(true))
250 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000251}
252
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000253bool HexagonPacketizerList::canReserveResourcesForConstExt() {
254 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000255}
256
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000257// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
258// return true, otherwise, return false.
259bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
260 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000261 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000262 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000263 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000264 MF.DeleteMachineInstr(ExtMI);
265 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000266}
267
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000268
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000269bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000270 SDep::Kind DepType, unsigned DepReg) {
271 // Check for LR dependence.
272 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000273 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000274
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000275 if (HII->isDeallocRet(MI))
276 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000277 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000278
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000279 // Check if this is a predicate dependence.
280 const TargetRegisterClass* RC = HRI->getMinimalPhysRegClass(DepReg);
281 if (RC == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000282 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000283
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000284 // Assumes that the first operand of the CALLr is the function address.
285 if (HII->isIndirectCall(MI) && (DepType == SDep::Data)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000286 MachineOperand MO = MI.getOperand(0);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000287 if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000288 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000289 }
290
291 return false;
292}
293
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000294static bool isRegDependence(const SDep::Kind DepType) {
295 return DepType == SDep::Data || DepType == SDep::Anti ||
296 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000297}
298
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000299static bool isDirectJump(const MachineInstr &MI) {
300 return MI.getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000301}
302
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000303static bool isSchedBarrier(const MachineInstr &MI) {
304 switch (MI.getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000305 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000306 return true;
307 }
308 return false;
309}
310
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000311static bool isControlFlow(const MachineInstr &MI) {
312 return MI.getDesc().isTerminator() || MI.getDesc().isCall();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000313}
314
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000315
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000316/// Returns true if the instruction modifies a callee-saved register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000317static bool doesModifyCalleeSavedReg(const MachineInstr &MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000318 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000319 const MachineFunction &MF = *MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000320 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000321 if (MI.modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000322 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000323 return false;
324}
325
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000326// Returns true if an instruction can be promoted to .new predicate or
327// new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000328bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI,
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000329 const TargetRegisterClass *NewRC) {
330 // Vector stores can be predicated, and can be new-value stores, but
331 // they cannot be predicated on a .new predicate value.
332 if (NewRC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000333 if (HII->isV60VectorInstruction(MI) && MI.mayStore())
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000334 return false;
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +0000335 return HII->isCondInst(MI) || HII->isJumpR(MI) || HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000336}
337
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000338// Promote an instructiont to its .cur form.
339// At this time, we have already made a call to canPromoteToDotCur and made
340// sure that it can *indeed* be promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000341bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000342 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
343 const TargetRegisterClass* RC) {
344 assert(DepType == SDep::Data);
345 int CurOpcode = HII->getDotCurOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000346 MI.setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000347 return true;
348}
349
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000350void HexagonPacketizerList::cleanUpDotCur() {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000351 MachineInstr *MI = nullptr;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000352 for (auto BI : CurrentPacketMIs) {
353 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
354 if (BI->getOpcode() == Hexagon::V6_vL32b_cur_ai) {
355 MI = BI;
356 continue;
357 }
358 if (MI) {
359 for (auto &MO : BI->operands())
360 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
361 return;
362 }
363 }
364 if (!MI)
365 return;
366 // We did not find a use of the CUR, so de-cur it.
367 MI->setDesc(HII->get(Hexagon::V6_vL32b_ai));
368 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
369}
370
371// Check to see if an instruction can be dot cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000372bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000373 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
374 const TargetRegisterClass *RC) {
375 if (!HII->isV60VectorInstruction(MI))
376 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000377 if (!HII->isV60VectorInstruction(*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000378 return false;
379
380 // Already a dot new instruction.
381 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
382 return false;
383
384 if (!HII->mayBeCurLoad(MI))
385 return false;
386
387 // The "cur value" cannot come from inline asm.
388 if (PacketSU->getInstr()->isInlineAsm())
389 return false;
390
391 // Make sure candidate instruction uses cur.
392 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000393 MI.dump();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000394 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000395 MachineInstr &MJ = *MII;
396 DEBUG({
397 dbgs() << "Checking CUR against ";
398 MJ.dump();
399 });
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000400 unsigned DestReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000401 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000402 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000403 if (MO.isReg() && MO.getReg() == DestReg)
404 FoundMatch = true;
405 if (!FoundMatch)
406 return false;
407
408 // Check for existing uses of a vector register within the packet which
409 // would be affected by converting a vector load into .cur formt.
410 for (auto BI : CurrentPacketMIs) {
411 DEBUG(dbgs() << "packet has "; BI->dump(););
412 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
413 return false;
414 }
415
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000416 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000417 // We can convert the opcode into a .cur.
418 return true;
419}
420
421// Promote an instruction to its .new form. At this time, we have already
422// made a call to canPromoteToDotNew and made sure that it can *indeed* be
423// promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000424bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000425 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
426 const TargetRegisterClass* RC) {
427 assert (DepType == SDep::Data);
428 int NewOpcode;
429 if (RC == &Hexagon::PredRegsRegClass)
430 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
431 else
432 NewOpcode = HII->getDotNewOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000433 MI.setDesc(HII->get(NewOpcode));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000434 return true;
435}
436
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000437bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) {
438 int NewOpcode = HII->getDotOldOp(MI.getOpcode());
439 MI.setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000440 return true;
441}
442
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000443bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) {
444 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000445 switch (Opc) {
446 case Hexagon::S2_storerd_io:
447 case Hexagon::S2_storeri_io:
448 case Hexagon::S2_storerh_io:
449 case Hexagon::S2_storerb_io:
450 break;
451 default:
452 llvm_unreachable("Unexpected instruction");
453 }
Matthias Braun941a7052016-07-28 18:40:00 +0000454 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000455 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000456 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
457 if (HII->isValidOffset(Opc, NewOff)) {
458 Off.setImm(NewOff);
459 return true;
460 }
461 return false;
462}
463
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000464void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) {
465 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000466 switch (Opc) {
467 case Hexagon::S2_storerd_io:
468 case Hexagon::S2_storeri_io:
469 case Hexagon::S2_storerh_io:
470 case Hexagon::S2_storerb_io:
471 break;
472 default:
473 llvm_unreachable("Unexpected instruction");
474 }
Matthias Braun941a7052016-07-28 18:40:00 +0000475 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000476 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000477 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
478}
479
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000480enum PredicateKind {
481 PK_False,
482 PK_True,
483 PK_Unknown
484};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000485
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000486/// Returns true if an instruction is predicated on p0 and false if it's
487/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000488static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000489 const HexagonInstrInfo *HII) {
490 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000491 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000492 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000493 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000494 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000495}
496
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000497static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000498 const HexagonInstrInfo *HII) {
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000499 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000500#ifndef NDEBUG
501 // Post Increment means duplicates. Use dense map to find duplicates in the
502 // list. Caution: Densemap initializes with the minimum of 64 buckets,
503 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000504 DenseSet<unsigned> DefRegsSet;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000505 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000506 if (MO.isReg() && MO.isDef())
507 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000508
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000509 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000510 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
511 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000512#else
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000513 if (MI.mayLoad()) {
514 const MachineOperand &Op1 = MI.getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000515 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000516 assert(Op1.isReg() && "Post increment operand has be to a register.");
517 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000518 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000519 if (MI.getDesc().mayStore()) {
520 const MachineOperand &Op0 = MI.getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000521 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000522 assert(Op0.isReg() && "Post increment operand has be to a register.");
523 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000524 }
525#endif
526 // we should never come here.
527 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
528}
529
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000530// Get the value being stored.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000531static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000532 // value being stored is always the last operand.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000533 return MI.getOperand(MI.getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000534}
535
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000536static bool isLoadAbsSet(const MachineInstr &MI) {
537 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000538 switch (Opc) {
539 case Hexagon::L4_loadrd_ap:
540 case Hexagon::L4_loadrb_ap:
541 case Hexagon::L4_loadrh_ap:
542 case Hexagon::L4_loadrub_ap:
543 case Hexagon::L4_loadruh_ap:
544 case Hexagon::L4_loadri_ap:
545 return true;
546 }
547 return false;
548}
549
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000550static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000551 assert(isLoadAbsSet(MI));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000552 return MI.getOperand(1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000553}
554
555
556// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000557// Following restrictions are to be respected in convert a store into
558// a new value store.
559// 1. If an instruction uses auto-increment, its address register cannot
560// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000561// 2. If an instruction uses absolute-set addressing mode, its address
562// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000563// 3. If an instruction produces a 64-bit result, its registers cannot be used
564// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000565// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000566// the instruction that uses the new-value register must also be conditional,
567// and both must always have their predicates evaluate identically.
568// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000569// 5. There is an implied restriction that a packet cannot have another store,
570// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000571// already a store in a packet, there can not be a new value store.
572// Arch Spec: 3.4.4.2
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000573bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
574 const MachineInstr &PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000575 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000576 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000577 return false;
578
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000579 // Make sure there is dependency and can be new value'd.
580 const MachineOperand &Val = getStoreValueOperand(MI);
581 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000582 return false;
583
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000584 const MCInstrDesc& MCID = PacketMI.getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000585
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000586 // First operand is always the result.
587 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
588 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
589 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000590 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000591
592 // New-value stores are of class NV (slot 0), dual stores require class ST
593 // in slot 0 (PRM 5.5).
594 for (auto I : CurrentPacketMIs) {
595 SUnit *PacketSU = MIToSUnit.find(I)->second;
596 if (PacketSU->getInstr()->mayStore())
597 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000598 }
599
600 // Make sure it's NOT the post increment register that we are going to
601 // new value.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000602 if (HII->isPostIncrement(MI) &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000603 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000604 return false;
605 }
606
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000607 if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000608 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
609 // If source is post_inc, or absolute-set addressing, it can not feed
610 // into new value store
611 // r3 = memw(r2++#4)
612 // memw(r30 + #-1404) = r2.new -> can not be new value store
613 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000614 return false;
615 }
616
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000617 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
618 return false;
619
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000620 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000621 // also be predicated.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000622 if (HII->isPredicated(PacketMI)) {
623 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000624 return false;
625
626 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000627 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000628 unsigned predRegNumSrc = 0;
629 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000630 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000631
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000632 // Get predicate register used in the source instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000633 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000634 if (!MO.isReg())
635 continue;
636 predRegNumSrc = MO.getReg();
637 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
638 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000639 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000640 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000641 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
642 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000643
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000644 // Get predicate register used in new-value store instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000645 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000646 if (!MO.isReg())
647 continue;
648 predRegNumDst = MO.getReg();
649 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
650 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000651 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000652 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000653 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
654 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000655
656 // New-value register producer and user (store) need to satisfy these
657 // constraints:
658 // 1) Both instructions should be predicated on the same register.
659 // 2) If producer of the new-value register is .new predicated then store
660 // should also be .new predicated and if producer is not .new predicated
661 // then store should not be .new predicated.
662 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000663 // sense, i.e, either both should be negated or both should be non-negated.
664 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000665 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000666 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000667 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000668 }
669
670 // Make sure that other than the new-value register no other store instruction
671 // register has been modified in the same packet. Predicate registers can be
672 // modified by they should not be modified between the producer and the store
673 // instruction as it will make them both conditional on different values.
674 // We already know this to be true for all the instructions before and
675 // including PacketMI. Howerver, we need to perform the check for the
676 // remaining instructions in the packet.
677
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000678 unsigned StartCheck = 0;
679
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000680 for (auto I : CurrentPacketMIs) {
681 SUnit *TempSU = MIToSUnit.find(I)->second;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000682 MachineInstr &TempMI = *TempSU->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000683
684 // Following condition is true for all the instructions until PacketMI is
685 // reached (StartCheck is set to 0 before the for loop).
686 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000687 if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
688 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000689
690 StartCheck = 1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000691 if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000692 continue;
693
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000694 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000695 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000696 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000697 }
698
Alp Tokerf907b892013-12-05 05:44:44 +0000699 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000700 // 1. The only use of reg is DepReg and no other registers.
701 // This handles V4 base+index registers.
702 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000703 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000704 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000705 if (!HII->isPostIncrement(MI)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000706 for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
707 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000708 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000709 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000710 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000711 }
712
713 // If data definition is because of implicit definition of the register,
714 // do not newify the store. Eg.
715 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
716 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000717 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000718 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
719 continue;
720 unsigned R = MO.getReg();
721 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
722 return false;
723 }
724
725 // Handle imp-use of super reg case. There is a target independent side
726 // change that should prevent this situation but I am handling it for
727 // just-in-case. For example, we cannot newify R2 in the following case:
728 // %R3<def> = A2_tfrsi 0;
729 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000730 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000731 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
732 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000733 }
734
735 // Can be dot new store.
736 return true;
737}
738
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000739// Can this MI to promoted to either new value store or new value jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000740bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000741 const SUnit *PacketSU, unsigned DepReg,
742 MachineBasicBlock::iterator &MII) {
743 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000744 return false;
745
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000746 // Check to see the store can be new value'ed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000747 MachineInstr &PacketMI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000748 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000749 return true;
750
751 // Check to see the compare/jump can be new value'ed.
752 // This is done as a pass on its own. Don't need to check it here.
753 return false;
754}
755
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000756static bool isImplicitDependency(const MachineInstr &I, unsigned DepReg) {
757 for (auto &MO : I.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000758 if (MO.isReg() && MO.isDef() && (MO.getReg() == DepReg) && MO.isImplicit())
759 return true;
760 return false;
761}
762
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000763// Check to see if an instruction can be dot new
764// There are three kinds.
765// 1. dot new on predicate - V2/V3/V4
766// 2. dot new on stores NV/ST - V4
767// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000768bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000769 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
770 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000771 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000772 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000773 return false;
774
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000775 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000776 return false;
777
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000778 const MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000779
780 // The "new value" cannot come from inline asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000781 if (PI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000782 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000783
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000784 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
785 // sense.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000786 if (PI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000787 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000788
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000789 // If dependency is trough an implicitly defined register, we should not
790 // newify the use.
791 if (isImplicitDependency(PI, DepReg))
792 return false;
793
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000794 const MCInstrDesc& MCID = PI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000795 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
796 if (DisableVecDblNVStores && VecRC == &Hexagon::VecDblRegsRegClass)
797 return false;
798
799 // predicate .new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000800 if (RC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +0000801 if (HII->isCondInst(MI) || HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000802 return HII->predCanBeUsedAsDotNew(PI, DepReg);
803
804 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
805 return false;
806
807 // Create a dot new machine instruction to see if resources can be
808 // allocated. If not, bail out now.
809 int NewOpcode = HII->getDotNewOp(MI);
810 const MCInstrDesc &D = HII->get(NewOpcode);
811 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000812 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000813 MF.DeleteMachineInstr(NewMI);
814 if (!ResourcesAvailable)
815 return false;
816
817 // New Value Store only. New Value Jump generated as a separate pass.
818 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
819 return false;
820
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000821 return true;
822}
823
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000824// Go through the packet instructions and search for an anti dependency between
825// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000826// Trying to add
827// a) %R1<def> = TFRI_cdNotPt %P3, 2
828// to this packet:
829// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000830// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
831// c) %P3<def> = C2_tfrrp %R23
832// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000833// }
834// The P3 from a) and d) will be complements after
835// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000836// Anti-dep between c) and b) is irrelevant for this case
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000837bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000838 unsigned DepReg) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000839 SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000840
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000841 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000842 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000843 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000844 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000845
846 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000847 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000848
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000849 // Look at dependencies between current members of the packet and
850 // predicate defining instruction MI. Make sure that dependency is
851 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000852 if (PacketSU->isSucc(PacketSUDep)) {
853 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000854 auto &Dep = PacketSU->Succs[i];
855 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
856 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000857 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000858 }
859 }
860 }
861
862 return false;
863}
864
865
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000866/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000867static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000868 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000869 /// We use the following rule: The first predicate register that is a use is
870 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000871 assert(QII->isPredicated(MI) && "Must be predicated instruction");
872
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000873 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000874 if (Op.isReg() && Op.getReg() && Op.isUse() &&
875 Hexagon::PredRegsRegClass.contains(Op.getReg()))
876 return Op.getReg();
877 }
878
879 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000880 return 0;
881}
882
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000883// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000884// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000885bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
886 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000887 // If we don't know the predicate sense of the instructions bail out early, we
888 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000889 if (getPredicateSense(MI1, HII) == PK_Unknown ||
890 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000891 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000892
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000893 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000894 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000895
896 // One corner case deals with the following scenario:
897 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000898 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000899 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000900 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000901 // b) %R25<def> = A2_tfrf %P0, %R24
902 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000903 // }
904 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000905 // On general check a) and b) are complements, but presence of c) will
906 // convert a) to .new form, and then it is not a complement.
907 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000908
909 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000910 // Look for Anti dependecy on the same predicate reg as used in the
911 // candidate.
912 for (auto I : CurrentPacketMIs) {
913 // Scheduling Unit for current insn in the packet.
914 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000915
916 // If this instruction in the packet is succeeded by the candidate...
917 if (PacketSU->isSucc(SU)) {
918 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000919 auto Dep = PacketSU->Succs[i];
920 // The corner case exist when there is true data dependency between
921 // candidate and one of current packet members, this dep is on
922 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000923 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000924 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
925 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
926 // Here I know that I is predicate setting instruction with true
927 // data dep to candidate on the register we care about - c) in the
928 // above example. Now I need to see if there is an anti dependency
929 // from c) to any other instruction in the same packet on the pred
930 // reg of interest.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000931 if (restrictingDepExistInPacket(*I, Dep.getReg()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000932 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000933 }
934 }
935 }
936 }
937
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000938 // If the above case does not apply, check regular complement condition.
939 // Check that the predicate register is the same and that the predicate
940 // sense is different We also need to differentiate .old vs. .new: !p0
941 // is not complementary to p0.new.
942 unsigned PReg1 = getPredicatedRegister(MI1, HII);
943 unsigned PReg2 = getPredicatedRegister(MI2, HII);
944 return PReg1 == PReg2 &&
945 Hexagon::PredRegsRegClass.contains(PReg1) &&
946 Hexagon::PredRegsRegClass.contains(PReg2) &&
947 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000948 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000949}
950
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000951// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000952void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000953 Dependence = false;
954 PromotedToDotNew = false;
955 GlueToNewValueJump = false;
956 GlueAllocframeStore = false;
957 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000958}
959
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000960// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000961bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
962 const MachineBasicBlock *) {
963 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000964 return true;
965
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000966 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000967 return false;
968
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000969 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000970 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000971 return false;
972
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000973 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000974 return false;
975
976 // We check if MI has any functional units mapped to it. If it doesn't,
977 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000978 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000979 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +0000980 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000981 return !FuncUnits;
982}
983
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000984bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
985 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000986 return true;
987
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000988 // Consider inline asm to not be a solo instruction by default.
989 // Inline asm will be put in a packet temporarily, but then it will be
990 // removed, and placed outside of the packet (before or after, depending
991 // on dependencies). This is to reduce the impact of inline asm as a
992 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000993 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000994 return true;
995
996 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
997 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
998 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000999 if (isSchedBarrier(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001000 return true;
1001
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001002 if (HII->isSolo(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001003 return true;
1004
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001005 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001006 return true;
1007
1008 return false;
1009}
1010
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001011
1012// Quick check if instructions MI and MJ cannot coexist in the same packet.
1013// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1014// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1015// For full test call this function twice:
1016// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1017// Doing the test only one way saves the amount of code in this function,
1018// since every test would need to be repeated with the MI and MJ reversed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001019static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001020 const HexagonInstrInfo &HII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001021 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001022 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1023 HII.isHVXMemWithAIndirect(MI, MJ))
1024 return true;
1025
1026 // An inline asm cannot be together with a branch, because we may not be
1027 // able to remove the asm out after packetizing (i.e. if the asm must be
1028 // moved past the bundle). Similarly, two asms cannot be together to avoid
1029 // complications when determining their relative order outside of a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001030 if (MI.isInlineAsm())
1031 return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
1032 MJ.isCall() || MJ.isTerminator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001033
1034 // "False" really means that the quick check failed to determine if
1035 // I and J cannot coexist.
1036 return false;
1037}
1038
1039
1040// Full, symmetric check.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001041bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
1042 const MachineInstr &MJ) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001043 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1044}
1045
1046void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1047 for (auto &B : MF) {
1048 MachineBasicBlock::iterator BundleIt;
1049 MachineBasicBlock::instr_iterator NextI;
1050 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1051 NextI = std::next(I);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001052 MachineInstr &MI = *I;
1053 if (MI.isBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001054 BundleIt = I;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001055 if (!MI.isInsideBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001056 continue;
1057
1058 // Decide on where to insert the instruction that we are pulling out.
1059 // Debug instructions always go before the bundle, but the placement of
1060 // INLINE_ASM depends on potential dependencies. By default, try to
1061 // put it before the bundle, but if the asm writes to a register that
1062 // other instructions in the bundle read, then we need to place it
1063 // after the bundle (to preserve the bundle semantics).
1064 bool InsertBeforeBundle;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001065 if (MI.isInlineAsm())
1066 InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
1067 else if (MI.isDebugValue())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001068 InsertBeforeBundle = true;
1069 else
1070 continue;
1071
1072 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1073 }
1074 }
1075}
1076
1077// Check if a given instruction is of class "system".
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001078static bool isSystemInstr(const MachineInstr &MI) {
1079 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001080 switch (Opc) {
1081 case Hexagon::Y2_barrier:
1082 case Hexagon::Y2_dcfetchbo:
1083 return true;
1084 }
1085 return false;
1086}
1087
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001088bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
1089 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001090 // The dependence graph may not include edges between dead definitions,
1091 // so without extra checks, we could end up packetizing two instruction
1092 // defining the same (dead) register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001093 if (I.isCall() || J.isCall())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001094 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001095 if (HII->isPredicated(I) || HII->isPredicated(J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001096 return false;
1097
1098 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001099 for (auto &MO : I.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001100 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1101 continue;
1102 DeadDefs[MO.getReg()] = true;
1103 }
1104
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001105 for (auto &MO : J.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001106 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1107 continue;
1108 unsigned R = MO.getReg();
1109 if (R != Hexagon::USR_OVF && DeadDefs[R])
1110 return true;
1111 }
1112 return false;
1113}
1114
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001115bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
1116 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001117 // A save callee-save register function call can only be in a packet
1118 // with instructions that don't write to the callee-save registers.
1119 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1120 doesModifyCalleeSavedReg(J, HRI)) ||
1121 (HII->isSaveCalleeSavedRegsCall(J) &&
1122 doesModifyCalleeSavedReg(I, HRI)))
1123 return true;
1124
1125 // Two control flow instructions cannot go in the same packet.
1126 if (isControlFlow(I) && isControlFlow(J))
1127 return true;
1128
1129 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1130 // contain a speculative indirect jump,
1131 // a new-value compare jump or a dealloc_return.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001132 auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
1133 if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001134 return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001135 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001136 return true;
1137 return false;
1138 };
1139
1140 if (HII->isLoopN(I) && isBadForLoopN(J))
1141 return true;
1142 if (HII->isLoopN(J) && isBadForLoopN(I))
1143 return true;
1144
1145 // dealloc_return cannot appear in the same packet as a conditional or
1146 // unconditional jump.
1147 return HII->isDeallocRet(I) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001148 (J.isBranch() || J.isCall() || J.isBarrier());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001149}
1150
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001151bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I,
1152 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001153 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001154 bool StoreI = I.mayStore(), StoreJ = J.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001155 if ((SysI && StoreJ) || (SysJ && StoreI))
1156 return true;
1157
1158 if (StoreI && StoreJ) {
1159 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1160 return true;
1161 } else {
1162 // A memop cannot be in the same packet with another memop or a store.
1163 // Two stores can be together, but here I and J cannot both be stores.
1164 bool MopStI = HII->isMemOp(I) || StoreI;
1165 bool MopStJ = HII->isMemOp(J) || StoreJ;
1166 if (MopStI && MopStJ)
1167 return true;
1168 }
1169
1170 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1171}
1172
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001173// SUI is the current instruction that is out side of the current packet.
1174// SUJ is the current instruction inside the current packet against which that
1175// SUI will be packetized.
1176bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001177 assert(SUI->getInstr() && SUJ->getInstr());
1178 MachineInstr &I = *SUI->getInstr();
1179 MachineInstr &J = *SUJ->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001180
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001181 // Clear IgnoreDepMIs when Packet starts.
1182 if (CurrentPacketMIs.size() == 1)
1183 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001184
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001185 MachineBasicBlock::iterator II = I.getIterator();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001186
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001187 // Solo instructions cannot go in the packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001188 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001189
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001190 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001191 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001192
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001193 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1194 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001195 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001196
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001197 // V4 allows dual stores. It does not allow second store, if the first
1198 // store is not in SLOT0. New value store, new value jump, dealloc_return
1199 // and memop always take SLOT0. Arch spec 3.4.4.2.
1200 Dependence = hasV4SpecificDependence(I, J);
1201 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001202 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001203
1204 // If an instruction feeds new value jump, glue it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001205 MachineBasicBlock::iterator NextMII = I.getIterator();
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001206 ++NextMII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001207 if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001208 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001209
1210 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001211 const MachineOperand &NOp0 = NextMI.getOperand(0);
1212 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001213
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001214 if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001215 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001216
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001217 for (auto T : CurrentPacketMIs) {
1218 SUnit *PacketSU = MIToSUnit.find(T)->second;
1219 MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001220 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001221 if (PI.isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001222 Dependence = true;
1223 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001224 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001225 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001226 // 1. Packet does not have a store in it.
1227 // 2. If the first operand of the nvj is newified, and the second
1228 // operand is also a reg, it (second reg) is not defined in
1229 // the same packet.
1230 // 3. If the second operand of the nvj is newified, (which means
1231 // first operand is also a reg), first reg is not defined in
1232 // the same packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001233 if (PI.getOpcode() == Hexagon::S2_allocframe || PI.mayStore() ||
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001234 HII->isLoopN(PI)) {
1235 Dependence = true;
1236 break;
1237 }
1238 // Check #2/#3.
1239 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001240 if (OpR.isReg() && PI.modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001241 Dependence = true;
1242 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001243 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001244 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001245
1246 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001247 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001248 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001249 }
1250
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001251 // There no dependency between a prolog instruction and its successor.
1252 if (!SUJ->isSucc(SUI))
1253 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001254
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001255 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1256 if (FoundSequentialDependence)
1257 break;
1258
1259 if (SUJ->Succs[i].getSUnit() != SUI)
1260 continue;
1261
1262 SDep::Kind DepType = SUJ->Succs[i].getKind();
1263 // For direct calls:
1264 // Ignore register dependences for call instructions for packetization
1265 // purposes except for those due to r31 and predicate registers.
1266 //
1267 // For indirect calls:
1268 // Same as direct calls + check for true dependences to the register
1269 // used in the indirect call.
1270 //
1271 // We completely ignore Order dependences for call instructions.
1272 //
1273 // For returns:
1274 // Ignore register dependences for return instructions like jumpr,
1275 // dealloc return unless we have dependencies on the explicit uses
1276 // of the registers used by jumpr (like r31) or dealloc return
1277 // (like r29 or r30).
1278 //
1279 // TODO: Currently, jumpr is handling only return of r31. So, the
1280 // following logic (specificaly isCallDependent) is working fine.
1281 // We need to enable jumpr for register other than r31 and then,
1282 // we need to rework the last part, where it handles indirect call
1283 // of that (isCallDependent) function. Bug 6216 is opened for this.
1284 unsigned DepReg = 0;
1285 const TargetRegisterClass *RC = nullptr;
1286 if (DepType == SDep::Data) {
1287 DepReg = SUJ->Succs[i].getReg();
1288 RC = HRI->getMinimalPhysRegClass(DepReg);
1289 }
1290
Krzysztof Parzyszek6421b932016-08-19 14:04:45 +00001291 if (I.isCall() || HII->isJumpR(I) || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001292 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001293 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001294 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1295 continue;
1296 }
1297
1298 if (DepType == SDep::Data) {
1299 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1300 if (promoteToDotCur(J, DepType, II, RC))
1301 continue;
1302 }
1303
1304 // Data dpendence ok if we have load.cur.
1305 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
1306 if (HII->isV60VectorInstruction(I))
1307 continue;
1308 }
1309
1310 // For instructions that can be promoted to dot-new, try to promote.
1311 if (DepType == SDep::Data) {
1312 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1313 if (promoteToDotNew(I, DepType, II, RC)) {
1314 PromotedToDotNew = true;
1315 continue;
1316 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001317 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001318 if (HII->isNewValueJump(I))
1319 continue;
1320 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001321
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001322 // For predicated instructions, if the predicates are complements then
1323 // there can be no dependence.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001324 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1325 arePredicatesComplements(I, J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001326 // Not always safe to do this translation.
1327 // DAG Builder attempts to reduce dependence edges using transitive
1328 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001329 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001330 // r0 = tfr_pt ... (1)
1331 // r0 = tfr_pf ... (2)
1332 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001333 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001334 // There will be an output dependence between (1)->(2) and (2)->(3).
1335 // However, there is no dependence edge between (1)->(3). This results
1336 // in all 3 instructions going in the same packet. We ignore dependce
1337 // only once to avoid this situation.
David Majnemer0d955d02016-08-11 22:21:41 +00001338 auto Itr = find(IgnoreDepMIs, &J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001339 if (Itr != IgnoreDepMIs.end()) {
1340 Dependence = true;
1341 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001342 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001343 IgnoreDepMIs.push_back(&I);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001344 continue;
1345 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001346
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001347 // Ignore Order dependences between unconditional direct branches
1348 // and non-control-flow instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001349 if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001350 DepType == SDep::Order)
1351 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001352
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001353 // Ignore all dependences for jumps except for true and output
1354 // dependences.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001355 if (I.isConditionalBranch() && DepType != SDep::Data &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001356 DepType != SDep::Output)
1357 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001358
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001359 // Ignore output dependences due to superregs. We can write to two
1360 // different subregisters of R1:0 for instance in the same cycle.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001361
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001362 // If neither I nor J defines DepReg, then this is a superfluous output
1363 // dependence. The dependence must be of the form:
1364 // R0 = ...
1365 // R1 = ...
1366 // and there is an output dependence between the two instructions with
1367 // DepReg = D0.
1368 // We want to ignore these dependences. Ideally, the dependence
1369 // constructor should annotate such dependences. We can then avoid this
1370 // relatively expensive check.
1371 //
1372 if (DepType == SDep::Output) {
1373 // DepReg is the register that's responsible for the dependence.
1374 unsigned DepReg = SUJ->Succs[i].getReg();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001375
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001376 // Check if I and J really defines DepReg.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001377 if (!I.definesRegister(DepReg) && !J.definesRegister(DepReg))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001378 continue;
1379 FoundSequentialDependence = true;
1380 break;
1381 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001382
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001383 // For Order dependences:
1384 // 1. On V4 or later, volatile loads/stores can be packetized together,
1385 // unless other rules prevent is.
1386 // 2. Store followed by a load is not allowed.
1387 // 3. Store followed by a store is only valid on V4 or later.
1388 // 4. Load followed by any memory operation is allowed.
1389 if (DepType == SDep::Order) {
1390 if (!PacketizeVolatiles) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001391 bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001392 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001393 FoundSequentialDependence = true;
1394 break;
1395 }
1396 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001397 // J is first, I is second.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001398 bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
1399 bool LoadI = I.mayLoad(), StoreI = I.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001400 if (StoreJ) {
1401 // Two stores are only allowed on V4+. Load following store is never
1402 // allowed.
1403 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001404 FoundSequentialDependence = true;
1405 break;
1406 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001407 } else if (!LoadJ || (!LoadI && !StoreI)) {
1408 // If J is neither load nor store, assume a dependency.
1409 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001410 FoundSequentialDependence = true;
1411 break;
1412 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001413 // Store followed by store: not OK on V2.
1414 // Store followed by load: not OK on all.
1415 // Load followed by store: OK on all.
1416 // Load followed by load: OK on all.
1417 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001418 }
1419
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001420 // For V4, special case ALLOCFRAME. Even though there is dependency
1421 // between ALLOCFRAME and subsequent store, allow it to be packetized
1422 // in a same packet. This implies that the store is using the caller's
1423 // SP. Hence, offset needs to be updated accordingly.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001424 if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
1425 unsigned Opc = I.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001426 switch (Opc) {
1427 case Hexagon::S2_storerd_io:
1428 case Hexagon::S2_storeri_io:
1429 case Hexagon::S2_storerh_io:
1430 case Hexagon::S2_storerb_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001431 if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001432 // Since this store is to be glued with allocframe in the same
1433 // packet, it will use SP of the previous stack frame, i.e.
1434 // caller's SP. Therefore, we need to recalculate offset
1435 // according to this change.
1436 GlueAllocframeStore = useCallersSP(I);
1437 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001438 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001439 }
1440 default:
1441 break;
1442 }
1443 }
1444
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001445 // There are certain anti-dependencies that cannot be ignored.
1446 // Specifically:
1447 // J2_call ... %R0<imp-def> ; SUJ
1448 // R0 = ... ; SUI
1449 // Those cannot be packetized together, since the call will observe
1450 // the effect of the assignment to R0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001451 if (DepType == SDep::Anti && J.isCall()) {
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001452 // Check if I defines any volatile register. We should also check
1453 // registers that the call may read, but these happen to be a
1454 // subset of the volatile register set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001455 for (const MCPhysReg *P = J.getDesc().ImplicitDefs; P && *P; ++P) {
1456 if (!I.modifiesRegister(*P, HRI))
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001457 continue;
1458 FoundSequentialDependence = true;
1459 break;
1460 }
1461 }
1462
1463 // Skip over remaining anti-dependences. Two instructions that are
1464 // anti-dependent can share a packet, since in most such cases all
1465 // operands are read before any modifications take place.
1466 // The exceptions are branch and call instructions, since they are
1467 // executed after all other instructions have completed (at least
1468 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001469 if (DepType != SDep::Anti) {
1470 FoundSequentialDependence = true;
1471 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001472 }
1473 }
1474
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001475 if (FoundSequentialDependence) {
1476 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001477 return false;
1478 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001479
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001480 return true;
1481}
1482
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001483bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001484 assert(SUI->getInstr() && SUJ->getInstr());
1485 MachineInstr &I = *SUI->getInstr();
1486 MachineInstr &J = *SUJ->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001487
1488 if (cannotCoexist(I, J))
1489 return false;
1490
1491 if (!Dependence)
1492 return true;
1493
1494 // Check if the instruction was promoted to a dot-new. If so, demote it
1495 // back into a dot-old.
1496 if (PromotedToDotNew)
1497 demoteToDotOld(I);
1498
1499 cleanUpDotCur();
1500 // Check if the instruction (must be a store) was glued with an allocframe
1501 // instruction. If so, restore its offset to its original value, i.e. use
1502 // current SP instead of caller's SP.
1503 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001504 useCalleesSP(I);
1505 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001506 }
1507 return false;
1508}
1509
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001510MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001511HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001512 MachineBasicBlock::iterator MII = MI.getIterator();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001513 MachineBasicBlock *MBB = MI.getParent();
1514 if (MI.isImplicitDef()) {
1515 unsigned R = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001516 if (Hexagon::IntRegsRegClass.contains(R)) {
1517 MCSuperRegIterator S(R, HRI, false);
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001518 MI.addOperand(MachineOperand::CreateReg(*S, true, true));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001519 }
1520 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001521 }
1522 assert(ResourceTracker->canReserveResources(MI));
1523
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001524 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001525 bool Good = true;
1526
1527 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001528 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001529 // We need to put both instructions in the same packet: MI and NvjMI.
1530 // Either of them can require a constant extender. Try to add both to
1531 // the current packet, and if that fails, end the packet and start a
1532 // new one.
1533 ResourceTracker->reserveResources(MI);
1534 if (ExtMI)
1535 Good = tryAllocateResourcesForConstExt(true);
1536
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001537 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001538 if (Good) {
1539 if (ResourceTracker->canReserveResources(NvjMI))
1540 ResourceTracker->reserveResources(NvjMI);
1541 else
1542 Good = false;
1543 }
1544 if (Good && ExtNvjMI)
1545 Good = tryAllocateResourcesForConstExt(true);
1546
1547 if (!Good) {
1548 endPacket(MBB, MI);
1549 assert(ResourceTracker->canReserveResources(MI));
1550 ResourceTracker->reserveResources(MI);
1551 if (ExtMI) {
1552 assert(canReserveResourcesForConstExt());
1553 tryAllocateResourcesForConstExt(true);
1554 }
1555 assert(ResourceTracker->canReserveResources(NvjMI));
1556 ResourceTracker->reserveResources(NvjMI);
1557 if (ExtNvjMI) {
1558 assert(canReserveResourcesForConstExt());
1559 reserveResourcesForConstExt();
1560 }
1561 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001562 CurrentPacketMIs.push_back(&MI);
1563 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001564 return MII;
1565 }
1566
1567 ResourceTracker->reserveResources(MI);
1568 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1569 endPacket(MBB, MI);
1570 if (PromotedToDotNew)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001571 demoteToDotOld(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001572 if (GlueAllocframeStore) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001573 useCalleesSP(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001574 GlueAllocframeStore = false;
1575 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001576 ResourceTracker->reserveResources(MI);
1577 reserveResourcesForConstExt();
1578 }
1579
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001580 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001581 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001582}
1583
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001584void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001585 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001586 OldPacketMIs = CurrentPacketMIs;
1587 VLIWPacketizerList::endPacket(MBB, MI);
1588}
1589
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001590bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001591 return !producesStall(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001592}
1593
1594
1595// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001596static bool isDependent(const MachineInstr &ProdMI,
1597 const MachineInstr &ConsMI) {
1598 if (!ProdMI.getOperand(0).isReg())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001599 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001600 unsigned DstReg = ProdMI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001601
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001602 for (auto &Op : ConsMI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001603 if (Op.isReg() && Op.isUse() && Op.getReg() == DstReg)
1604 // The MIs depend on each other.
1605 return true;
1606
1607 return false;
1608}
1609
1610// V60 forward scheduling.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001611bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001612 // Check whether the previous packet is in a different loop. If this is the
1613 // case, there is little point in trying to avoid a stall because that would
1614 // favor the rare case (loop entry) over the common case (loop iteration).
1615 //
1616 // TODO: We should really be able to check all the incoming edges if this is
1617 // the first packet in a basic block, so we can avoid stalls from the loop
1618 // backedge.
1619 if (!OldPacketMIs.empty()) {
1620 auto *OldBB = OldPacketMIs.front()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001621 auto *ThisBB = I.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001622 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1623 return false;
1624 }
1625
1626 // Check for stall between two vector instructions.
1627 if (HII->isV60VectorInstruction(I)) {
1628 for (auto J : OldPacketMIs) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001629 if (!HII->isV60VectorInstruction(*J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001630 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001631 if (isDependent(*J, I) && !HII->isVecUsableNextPacket(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001632 return true;
1633 }
1634 return false;
1635 }
1636
1637 // Check for stall between two scalar instructions. First, check that
1638 // there is no definition of a use in the current packet, because it
1639 // may be a candidate for .new.
1640 for (auto J : CurrentPacketMIs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001641 if (!HII->isV60VectorInstruction(*J) && isDependent(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001642 return false;
1643
1644 // Check for stall between I and instructions in the previous packet.
1645 if (MF.getSubtarget<HexagonSubtarget>().useBSBScheduling()) {
1646 for (auto J : OldPacketMIs) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001647 if (HII->isV60VectorInstruction(*J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001648 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001649 if (!HII->isLateInstrFeedsEarlyInstr(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001650 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001651 if (isDependent(*J, I) && !HII->canExecuteInBundle(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001652 return true;
1653 }
1654 }
1655
1656 return false;
1657}
1658
1659
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001660//===----------------------------------------------------------------------===//
1661// Public Constructor Functions
1662//===----------------------------------------------------------------------===//
1663
1664FunctionPass *llvm::createHexagonPacketizer() {
1665 return new HexagonPacketizer();
1666}