blob: 1a7c4b30797dd14595b581feb010be42df53a39e [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/MachineFunctionPass.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000026#include "llvm/CodeGen/MachineLoopInfo.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/CodeGen/Passes.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000029#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/Support/Debug.h"
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000031
32using namespace llvm;
33
Chandler Carruth84e68b22014-04-22 02:41:26 +000034#define DEBUG_TYPE "packets"
35
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000036static cl::opt<bool> DisablePacketizer("disable-packetizer", cl::Hidden,
37 cl::ZeroOrMore, cl::init(false),
38 cl::desc("Disable Hexagon packetizer pass"));
39
Jyotsna Verma1d297502013-05-02 15:39:30 +000040static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000041 cl::ZeroOrMore, cl::Hidden, cl::init(true),
42 cl::desc("Allow non-solo packetization of volatile memory references"));
43
44static cl::opt<bool> EnableGenAllInsnClass("enable-gen-insn", cl::init(false),
45 cl::Hidden, cl::ZeroOrMore, cl::desc("Generate all instruction with TC"));
46
47static cl::opt<bool> DisableVecDblNVStores("disable-vecdbl-nv-stores",
48 cl::init(false), cl::Hidden, cl::ZeroOrMore,
49 cl::desc("Disable vector double new-value-stores"));
50
51extern cl::opt<bool> ScheduleInlineAsm;
Jyotsna Verma1d297502013-05-02 15:39:30 +000052
Jyotsna Verma1d297502013-05-02 15:39:30 +000053namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000054 FunctionPass *createHexagonPacketizer();
Jyotsna Verma1d297502013-05-02 15:39:30 +000055 void initializeHexagonPacketizerPass(PassRegistry&);
56}
57
58
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000059namespace {
60 class HexagonPacketizer : public MachineFunctionPass {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000061 public:
62 static char ID;
Jyotsna Verma1d297502013-05-02 15:39:30 +000063 HexagonPacketizer() : MachineFunctionPass(ID) {
64 initializeHexagonPacketizerPass(*PassRegistry::getPassRegistry());
65 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000066
Craig Topper906c2cd2014-04-29 07:58:16 +000067 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000068 AU.setPreservesCFG();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000069 AU.addRequired<AAResultsWrapperPass>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000070 AU.addRequired<MachineBranchProbabilityInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000071 AU.addRequired<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000072 AU.addRequired<MachineLoopInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000073 AU.addPreserved<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000074 AU.addPreserved<MachineLoopInfo>();
75 MachineFunctionPass::getAnalysisUsage(AU);
76 }
Craig Topper906c2cd2014-04-29 07:58:16 +000077 const char *getPassName() const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000078 return "Hexagon Packetizer";
79 }
Craig Topper906c2cd2014-04-29 07:58:16 +000080 bool runOnMachineFunction(MachineFunction &Fn) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000081 MachineFunctionProperties getRequiredProperties() const override {
82 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +000083 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +000084 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000085
86 private:
87 const HexagonInstrInfo *HII;
88 const HexagonRegisterInfo *HRI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000089 };
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000090
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000091 char HexagonPacketizer::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000092}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000093
Jyotsna Verma1d297502013-05-02 15:39:30 +000094INITIALIZE_PASS_BEGIN(HexagonPacketizer, "packets", "Hexagon Packetizer",
95 false, false)
96INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
97INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
98INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Chandler Carruth7b560d42015-09-09 17:55:00 +000099INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000100INITIALIZE_PASS_END(HexagonPacketizer, "packets", "Hexagon Packetizer",
101 false, false)
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();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000109
110 addMutation(make_unique<HexagonSubtarget::HexagonDAGMutation>());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000111}
112
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000113// Check if FirstI modifies a register that SecondI reads.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000114static bool hasWriteToReadDep(const MachineInstr &FirstI,
115 const MachineInstr &SecondI,
116 const TargetRegisterInfo *TRI) {
117 for (auto &MO : FirstI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000118 if (!MO.isReg() || !MO.isDef())
119 continue;
120 unsigned R = MO.getReg();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000121 if (SecondI.readsRegister(R, TRI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000122 return true;
123 }
124 return false;
125}
126
127
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000128static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000129 MachineBasicBlock::iterator BundleIt, bool Before) {
130 MachineBasicBlock::instr_iterator InsertPt;
131 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000132 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000133 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000134 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000135
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000136 MachineBasicBlock &B = *MI.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000137 // The instruction should at least be bundled with the preceding instruction
138 // (there will always be one, i.e. BUNDLE, if nothing else).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000139 assert(MI.isBundledWithPred());
140 if (MI.isBundledWithSucc()) {
141 MI.clearFlag(MachineInstr::BundledSucc);
142 MI.clearFlag(MachineInstr::BundledPred);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000143 } else {
144 // If it's not bundled with the successor (i.e. it is the last one
145 // in the bundle), then we can simply unbundle it from the predecessor,
146 // which will take care of updating the predecessor's flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000147 MI.unbundleFromPred();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000148 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000149 B.splice(InsertPt, &B, MI.getIterator());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000150
151 // Get the size of the bundle without asserting.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000152 MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000153 MachineBasicBlock::const_instr_iterator E = B.instr_end();
154 unsigned Size = 0;
155 for (++I; I != E && I->isBundledWithPred(); ++I)
156 ++Size;
157
158 // If there are still two or more instructions, then there is nothing
159 // else to be done.
160 if (Size > 1)
161 return BundleIt;
162
163 // Otherwise, extract the single instruction out and delete the bundle.
164 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000165 MachineInstr &SingleI = *BundleIt->getNextNode();
166 SingleI.unbundleFromPred();
167 assert(!SingleI.isBundledWithSucc());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000168 BundleIt->eraseFromParent();
169 return NextIt;
170}
171
172
173bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000174 if (DisablePacketizer || skipFunction(*MF.getFunction()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000175 return false;
176
177 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
178 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
179 auto &MLI = getAnalysis<MachineLoopInfo>();
180 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
181 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
182
183 if (EnableGenAllInsnClass)
184 HII->genAllInsnTimingClasses(MF);
185
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000186 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000187 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000188
189 // DFA state table should not be empty.
190 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
191
192 //
193 // Loop over all basic blocks and remove KILL pseudo-instructions
194 // These instructions confuse the dependence analysis. Consider:
195 // D0 = ... (Insn 0)
196 // R0 = KILL R0, D0 (Insn 1)
197 // R0 = ... (Insn 2)
198 // Here, Insn 1 will result in the dependence graph not emitting an output
199 // dependence between Insn 0 and Insn 2. This can lead to incorrect
200 // packetization
201 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000202 for (auto &MB : MF) {
203 auto End = MB.end();
204 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000205 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000206 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000207 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000208 MB.erase(MI);
209 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000210 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000211 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000212 }
213 }
214
215 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000216 for (auto &MB : MF) {
217 auto Begin = MB.begin(), End = MB.end();
218 while (Begin != End) {
219 // First the first non-boundary starting from the end of the last
220 // scheduling region.
221 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000222 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000223 ++RB;
224 // First the first boundary starting from the beginning of the new
225 // region.
226 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000227 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000228 ++RE;
229 // Add the scheduling boundary if it's not block end.
230 if (RE != End)
231 ++RE;
232 // If RB == End, then RE == End.
233 if (RB != End)
234 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000235
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000236 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000237 }
238 }
239
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000240 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000241 return true;
242}
243
244
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000245// Reserve resources for a constant extender. Trigger an assertion if the
246// reservation fails.
247void HexagonPacketizerList::reserveResourcesForConstExt() {
248 if (!tryAllocateResourcesForConstExt(true))
249 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000250}
251
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000252bool HexagonPacketizerList::canReserveResourcesForConstExt() {
253 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000254}
255
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000256// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
257// return true, otherwise, return false.
258bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
259 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000260 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000261 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000262 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000263 MF.DeleteMachineInstr(ExtMI);
264 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000265}
266
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000267
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000268bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000269 SDep::Kind DepType, unsigned DepReg) {
270 // Check for LR dependence.
271 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000272 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000273
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000274 if (HII->isDeallocRet(MI))
275 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000276 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000277
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000278 // Check if this is a predicate dependence.
279 const TargetRegisterClass* RC = HRI->getMinimalPhysRegClass(DepReg);
280 if (RC == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000281 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000282
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000283 // Assumes that the first operand of the CALLr is the function address.
284 if (HII->isIndirectCall(MI) && (DepType == SDep::Data)) {
Krzysztof Parzyszek29a6a2e2016-08-19 21:07:35 +0000285 const MachineOperand MO = MI.getOperand(0);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000286 if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000287 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000288 }
289
Krzysztof Parzyszek29a6a2e2016-08-19 21:07:35 +0000290 if (HII->isJumpR(MI)) {
291 const MachineOperand &MO = HII->isPredicated(MI) ? MI.getOperand(1)
292 : MI.getOperand(0);
293 assert(MO.isReg() && MO.isUse());
294 if (MO.getReg() == DepReg)
295 return true;
296 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000297 return false;
298}
299
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000300static bool isRegDependence(const SDep::Kind DepType) {
301 return DepType == SDep::Data || DepType == SDep::Anti ||
302 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000303}
304
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000305static bool isDirectJump(const MachineInstr &MI) {
306 return MI.getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000307}
308
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000309static bool isSchedBarrier(const MachineInstr &MI) {
310 switch (MI.getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000311 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000312 return true;
313 }
314 return false;
315}
316
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000317static bool isControlFlow(const MachineInstr &MI) {
318 return MI.getDesc().isTerminator() || MI.getDesc().isCall();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000319}
320
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000321
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000322/// Returns true if the instruction modifies a callee-saved register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000323static bool doesModifyCalleeSavedReg(const MachineInstr &MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000324 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000325 const MachineFunction &MF = *MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000326 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000327 if (MI.modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000328 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000329 return false;
330}
331
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000332// Returns true if an instruction can be promoted to .new predicate or
333// new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000334bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI,
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000335 const TargetRegisterClass *NewRC) {
336 // Vector stores can be predicated, and can be new-value stores, but
337 // they cannot be predicated on a .new predicate value.
338 if (NewRC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000339 if (HII->isV60VectorInstruction(MI) && MI.mayStore())
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000340 return false;
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +0000341 return HII->isCondInst(MI) || HII->isJumpR(MI) || MI.isReturn() ||
342 HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000343}
344
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000345// Promote an instructiont to its .cur form.
346// At this time, we have already made a call to canPromoteToDotCur and made
347// sure that it can *indeed* be promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000348bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000349 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
350 const TargetRegisterClass* RC) {
351 assert(DepType == SDep::Data);
352 int CurOpcode = HII->getDotCurOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000353 MI.setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000354 return true;
355}
356
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000357void HexagonPacketizerList::cleanUpDotCur() {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000358 MachineInstr *MI = nullptr;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000359 for (auto BI : CurrentPacketMIs) {
360 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
361 if (BI->getOpcode() == Hexagon::V6_vL32b_cur_ai) {
362 MI = BI;
363 continue;
364 }
365 if (MI) {
366 for (auto &MO : BI->operands())
367 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
368 return;
369 }
370 }
371 if (!MI)
372 return;
373 // We did not find a use of the CUR, so de-cur it.
374 MI->setDesc(HII->get(Hexagon::V6_vL32b_ai));
375 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
376}
377
378// Check to see if an instruction can be dot cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000379bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000380 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
381 const TargetRegisterClass *RC) {
382 if (!HII->isV60VectorInstruction(MI))
383 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000384 if (!HII->isV60VectorInstruction(*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000385 return false;
386
387 // Already a dot new instruction.
388 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
389 return false;
390
391 if (!HII->mayBeCurLoad(MI))
392 return false;
393
394 // The "cur value" cannot come from inline asm.
395 if (PacketSU->getInstr()->isInlineAsm())
396 return false;
397
398 // Make sure candidate instruction uses cur.
399 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000400 MI.dump();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000401 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000402 MachineInstr &MJ = *MII;
403 DEBUG({
404 dbgs() << "Checking CUR against ";
405 MJ.dump();
406 });
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000407 unsigned DestReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000408 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000409 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000410 if (MO.isReg() && MO.getReg() == DestReg)
411 FoundMatch = true;
412 if (!FoundMatch)
413 return false;
414
415 // Check for existing uses of a vector register within the packet which
416 // would be affected by converting a vector load into .cur formt.
417 for (auto BI : CurrentPacketMIs) {
418 DEBUG(dbgs() << "packet has "; BI->dump(););
419 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
420 return false;
421 }
422
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000423 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000424 // We can convert the opcode into a .cur.
425 return true;
426}
427
428// Promote an instruction to its .new form. At this time, we have already
429// made a call to canPromoteToDotNew and made sure that it can *indeed* be
430// promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000431bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000432 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
433 const TargetRegisterClass* RC) {
434 assert (DepType == SDep::Data);
435 int NewOpcode;
436 if (RC == &Hexagon::PredRegsRegClass)
437 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
438 else
439 NewOpcode = HII->getDotNewOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000440 MI.setDesc(HII->get(NewOpcode));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000441 return true;
442}
443
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000444bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) {
445 int NewOpcode = HII->getDotOldOp(MI.getOpcode());
446 MI.setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000447 return true;
448}
449
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000450bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) {
451 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000452 switch (Opc) {
453 case Hexagon::S2_storerd_io:
454 case Hexagon::S2_storeri_io:
455 case Hexagon::S2_storerh_io:
456 case Hexagon::S2_storerb_io:
457 break;
458 default:
459 llvm_unreachable("Unexpected instruction");
460 }
Matthias Braun941a7052016-07-28 18:40:00 +0000461 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000462 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000463 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
464 if (HII->isValidOffset(Opc, NewOff)) {
465 Off.setImm(NewOff);
466 return true;
467 }
468 return false;
469}
470
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000471void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) {
472 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000473 switch (Opc) {
474 case Hexagon::S2_storerd_io:
475 case Hexagon::S2_storeri_io:
476 case Hexagon::S2_storerh_io:
477 case Hexagon::S2_storerb_io:
478 break;
479 default:
480 llvm_unreachable("Unexpected instruction");
481 }
Matthias Braun941a7052016-07-28 18:40:00 +0000482 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000483 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000484 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
485}
486
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000487enum PredicateKind {
488 PK_False,
489 PK_True,
490 PK_Unknown
491};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000492
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000493/// Returns true if an instruction is predicated on p0 and false if it's
494/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000495static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000496 const HexagonInstrInfo *HII) {
497 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000498 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000499 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000500 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000501 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000502}
503
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000504static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000505 const HexagonInstrInfo *HII) {
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000506 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000507#ifndef NDEBUG
508 // Post Increment means duplicates. Use dense map to find duplicates in the
509 // list. Caution: Densemap initializes with the minimum of 64 buckets,
510 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000511 DenseSet<unsigned> DefRegsSet;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000512 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000513 if (MO.isReg() && MO.isDef())
514 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000515
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000516 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000517 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
518 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000519#else
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000520 if (MI.mayLoad()) {
521 const MachineOperand &Op1 = MI.getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000522 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000523 assert(Op1.isReg() && "Post increment operand has be to a register.");
524 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000525 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000526 if (MI.getDesc().mayStore()) {
527 const MachineOperand &Op0 = MI.getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000528 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000529 assert(Op0.isReg() && "Post increment operand has be to a register.");
530 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000531 }
532#endif
533 // we should never come here.
534 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
535}
536
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000537// Get the value being stored.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000538static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000539 // value being stored is always the last operand.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000540 return MI.getOperand(MI.getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000541}
542
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000543static bool isLoadAbsSet(const MachineInstr &MI) {
544 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000545 switch (Opc) {
546 case Hexagon::L4_loadrd_ap:
547 case Hexagon::L4_loadrb_ap:
548 case Hexagon::L4_loadrh_ap:
549 case Hexagon::L4_loadrub_ap:
550 case Hexagon::L4_loadruh_ap:
551 case Hexagon::L4_loadri_ap:
552 return true;
553 }
554 return false;
555}
556
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000557static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000558 assert(isLoadAbsSet(MI));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000559 return MI.getOperand(1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000560}
561
562
563// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000564// Following restrictions are to be respected in convert a store into
565// a new value store.
566// 1. If an instruction uses auto-increment, its address register cannot
567// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000568// 2. If an instruction uses absolute-set addressing mode, its address
569// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000570// 3. If an instruction produces a 64-bit result, its registers cannot be used
571// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000572// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000573// the instruction that uses the new-value register must also be conditional,
574// and both must always have their predicates evaluate identically.
575// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000576// 5. There is an implied restriction that a packet cannot have another store,
577// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000578// already a store in a packet, there can not be a new value store.
579// Arch Spec: 3.4.4.2
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000580bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
581 const MachineInstr &PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000582 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000583 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000584 return false;
585
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000586 // Make sure there is dependency and can be new value'd.
587 const MachineOperand &Val = getStoreValueOperand(MI);
588 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000589 return false;
590
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000591 const MCInstrDesc& MCID = PacketMI.getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000592
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000593 // First operand is always the result.
594 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
595 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
596 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000597 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000598
599 // New-value stores are of class NV (slot 0), dual stores require class ST
600 // in slot 0 (PRM 5.5).
601 for (auto I : CurrentPacketMIs) {
602 SUnit *PacketSU = MIToSUnit.find(I)->second;
603 if (PacketSU->getInstr()->mayStore())
604 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000605 }
606
607 // Make sure it's NOT the post increment register that we are going to
608 // new value.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000609 if (HII->isPostIncrement(MI) &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000610 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000611 return false;
612 }
613
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000614 if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000615 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
616 // If source is post_inc, or absolute-set addressing, it can not feed
617 // into new value store
618 // r3 = memw(r2++#4)
619 // memw(r30 + #-1404) = r2.new -> can not be new value store
620 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000621 return false;
622 }
623
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000624 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
625 return false;
626
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000627 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000628 // also be predicated.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000629 if (HII->isPredicated(PacketMI)) {
630 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000631 return false;
632
633 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000634 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000635 unsigned predRegNumSrc = 0;
636 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000637 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000638
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000639 // Get predicate register used in the source instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000640 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000641 if (!MO.isReg())
642 continue;
643 predRegNumSrc = MO.getReg();
644 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
645 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000646 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000647 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000648 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
649 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000650
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000651 // Get predicate register used in new-value store instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000652 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000653 if (!MO.isReg())
654 continue;
655 predRegNumDst = MO.getReg();
656 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
657 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000658 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000659 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000660 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
661 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000662
663 // New-value register producer and user (store) need to satisfy these
664 // constraints:
665 // 1) Both instructions should be predicated on the same register.
666 // 2) If producer of the new-value register is .new predicated then store
667 // should also be .new predicated and if producer is not .new predicated
668 // then store should not be .new predicated.
669 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000670 // sense, i.e, either both should be negated or both should be non-negated.
671 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000672 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000673 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000674 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000675 }
676
677 // Make sure that other than the new-value register no other store instruction
678 // register has been modified in the same packet. Predicate registers can be
679 // modified by they should not be modified between the producer and the store
680 // instruction as it will make them both conditional on different values.
681 // We already know this to be true for all the instructions before and
682 // including PacketMI. Howerver, we need to perform the check for the
683 // remaining instructions in the packet.
684
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000685 unsigned StartCheck = 0;
686
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000687 for (auto I : CurrentPacketMIs) {
688 SUnit *TempSU = MIToSUnit.find(I)->second;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000689 MachineInstr &TempMI = *TempSU->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000690
691 // Following condition is true for all the instructions until PacketMI is
692 // reached (StartCheck is set to 0 before the for loop).
693 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000694 if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
695 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000696
697 StartCheck = 1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000698 if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000699 continue;
700
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000701 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000702 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000703 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000704 }
705
Alp Tokerf907b892013-12-05 05:44:44 +0000706 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000707 // 1. The only use of reg is DepReg and no other registers.
708 // This handles V4 base+index registers.
709 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000710 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000711 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000712 if (!HII->isPostIncrement(MI)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000713 for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
714 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000715 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000716 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000717 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000718 }
719
720 // If data definition is because of implicit definition of the register,
721 // do not newify the store. Eg.
722 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
723 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000724 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000725 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
726 continue;
727 unsigned R = MO.getReg();
728 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
729 return false;
730 }
731
732 // Handle imp-use of super reg case. There is a target independent side
733 // change that should prevent this situation but I am handling it for
734 // just-in-case. For example, we cannot newify R2 in the following case:
735 // %R3<def> = A2_tfrsi 0;
736 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000737 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000738 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
739 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000740 }
741
742 // Can be dot new store.
743 return true;
744}
745
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000746// Can this MI to promoted to either new value store or new value jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000747bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000748 const SUnit *PacketSU, unsigned DepReg,
749 MachineBasicBlock::iterator &MII) {
750 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000751 return false;
752
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000753 // Check to see the store can be new value'ed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000754 MachineInstr &PacketMI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000755 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000756 return true;
757
758 // Check to see the compare/jump can be new value'ed.
759 // This is done as a pass on its own. Don't need to check it here.
760 return false;
761}
762
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000763static bool isImplicitDependency(const MachineInstr &I, unsigned DepReg) {
764 for (auto &MO : I.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000765 if (MO.isReg() && MO.isDef() && (MO.getReg() == DepReg) && MO.isImplicit())
766 return true;
767 return false;
768}
769
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000770// Check to see if an instruction can be dot new
771// There are three kinds.
772// 1. dot new on predicate - V2/V3/V4
773// 2. dot new on stores NV/ST - V4
774// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000775bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000776 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
777 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000778 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000779 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000780 return false;
781
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000782 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000783 return false;
784
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000785 const MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000786
787 // The "new value" cannot come from inline asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000788 if (PI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000789 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000790
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000791 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
792 // sense.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000793 if (PI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000794 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000795
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000796 // If dependency is trough an implicitly defined register, we should not
797 // newify the use.
798 if (isImplicitDependency(PI, DepReg))
799 return false;
800
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000801 const MCInstrDesc& MCID = PI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000802 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
803 if (DisableVecDblNVStores && VecRC == &Hexagon::VecDblRegsRegClass)
804 return false;
805
806 // predicate .new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000807 if (RC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +0000808 if (HII->isCondInst(MI) || HII->isJumpR(MI) || MI.isReturn())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000809 return HII->predCanBeUsedAsDotNew(PI, DepReg);
810
811 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
812 return false;
813
814 // Create a dot new machine instruction to see if resources can be
815 // allocated. If not, bail out now.
816 int NewOpcode = HII->getDotNewOp(MI);
817 const MCInstrDesc &D = HII->get(NewOpcode);
818 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000819 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000820 MF.DeleteMachineInstr(NewMI);
821 if (!ResourcesAvailable)
822 return false;
823
824 // New Value Store only. New Value Jump generated as a separate pass.
825 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
826 return false;
827
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000828 return true;
829}
830
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000831// Go through the packet instructions and search for an anti dependency between
832// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000833// Trying to add
834// a) %R1<def> = TFRI_cdNotPt %P3, 2
835// to this packet:
836// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000837// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
838// c) %P3<def> = C2_tfrrp %R23
839// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000840// }
841// The P3 from a) and d) will be complements after
842// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000843// Anti-dep between c) and b) is irrelevant for this case
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000844bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000845 unsigned DepReg) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000846 SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000847
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000848 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000849 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000850 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000851 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000852
853 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000854 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000855
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000856 // Look at dependencies between current members of the packet and
857 // predicate defining instruction MI. Make sure that dependency is
858 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000859 if (PacketSU->isSucc(PacketSUDep)) {
860 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000861 auto &Dep = PacketSU->Succs[i];
862 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
863 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000864 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000865 }
866 }
867 }
868
869 return false;
870}
871
872
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000873/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000874static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000875 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000876 /// We use the following rule: The first predicate register that is a use is
877 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000878 assert(QII->isPredicated(MI) && "Must be predicated instruction");
879
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000880 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000881 if (Op.isReg() && Op.getReg() && Op.isUse() &&
882 Hexagon::PredRegsRegClass.contains(Op.getReg()))
883 return Op.getReg();
884 }
885
886 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000887 return 0;
888}
889
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000890// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000891// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000892bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
893 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000894 // If we don't know the predicate sense of the instructions bail out early, we
895 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000896 if (getPredicateSense(MI1, HII) == PK_Unknown ||
897 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000898 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000899
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000900 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000901 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000902
903 // One corner case deals with the following scenario:
904 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000905 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000906 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000907 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000908 // b) %R25<def> = A2_tfrf %P0, %R24
909 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000910 // }
911 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000912 // On general check a) and b) are complements, but presence of c) will
913 // convert a) to .new form, and then it is not a complement.
914 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000915
916 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000917 // Look for Anti dependecy on the same predicate reg as used in the
918 // candidate.
919 for (auto I : CurrentPacketMIs) {
920 // Scheduling Unit for current insn in the packet.
921 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000922
923 // If this instruction in the packet is succeeded by the candidate...
924 if (PacketSU->isSucc(SU)) {
925 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000926 auto Dep = PacketSU->Succs[i];
927 // The corner case exist when there is true data dependency between
928 // candidate and one of current packet members, this dep is on
929 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000930 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000931 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
932 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
933 // Here I know that I is predicate setting instruction with true
934 // data dep to candidate on the register we care about - c) in the
935 // above example. Now I need to see if there is an anti dependency
936 // from c) to any other instruction in the same packet on the pred
937 // reg of interest.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000938 if (restrictingDepExistInPacket(*I, Dep.getReg()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000939 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000940 }
941 }
942 }
943 }
944
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000945 // If the above case does not apply, check regular complement condition.
946 // Check that the predicate register is the same and that the predicate
947 // sense is different We also need to differentiate .old vs. .new: !p0
948 // is not complementary to p0.new.
949 unsigned PReg1 = getPredicatedRegister(MI1, HII);
950 unsigned PReg2 = getPredicatedRegister(MI2, HII);
951 return PReg1 == PReg2 &&
952 Hexagon::PredRegsRegClass.contains(PReg1) &&
953 Hexagon::PredRegsRegClass.contains(PReg2) &&
954 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000955 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000956}
957
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000958// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000959void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000960 Dependence = false;
961 PromotedToDotNew = false;
962 GlueToNewValueJump = false;
963 GlueAllocframeStore = false;
964 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000965}
966
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000967// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000968bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
969 const MachineBasicBlock *) {
970 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000971 return true;
972
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000973 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000974 return false;
975
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000976 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000977 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000978 return false;
979
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000980 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000981 return false;
982
983 // We check if MI has any functional units mapped to it. If it doesn't,
984 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000985 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000986 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +0000987 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000988 return !FuncUnits;
989}
990
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000991bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
992 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000993 return true;
994
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000995 // Consider inline asm to not be a solo instruction by default.
996 // Inline asm will be put in a packet temporarily, but then it will be
997 // removed, and placed outside of the packet (before or after, depending
998 // on dependencies). This is to reduce the impact of inline asm as a
999 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001000 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001001 return true;
1002
1003 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
1004 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
1005 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001006 if (isSchedBarrier(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001007 return true;
1008
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001009 if (HII->isSolo(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001010 return true;
1011
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001012 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001013 return true;
1014
1015 return false;
1016}
1017
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001018
1019// Quick check if instructions MI and MJ cannot coexist in the same packet.
1020// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1021// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1022// For full test call this function twice:
1023// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1024// Doing the test only one way saves the amount of code in this function,
1025// since every test would need to be repeated with the MI and MJ reversed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001026static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001027 const HexagonInstrInfo &HII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001028 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001029 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1030 HII.isHVXMemWithAIndirect(MI, MJ))
1031 return true;
1032
1033 // An inline asm cannot be together with a branch, because we may not be
1034 // able to remove the asm out after packetizing (i.e. if the asm must be
1035 // moved past the bundle). Similarly, two asms cannot be together to avoid
1036 // complications when determining their relative order outside of a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001037 if (MI.isInlineAsm())
1038 return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
1039 MJ.isCall() || MJ.isTerminator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001040
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001041 switch (MI.getOpcode()) {
1042 case (Hexagon::S2_storew_locked):
1043 case (Hexagon::S4_stored_locked):
1044 case (Hexagon::L2_loadw_locked):
1045 case (Hexagon::L4_loadd_locked):
1046 case (Hexagon::Y4_l2fetch): {
1047 // These instructions can only be grouped with ALU32 or non-floating-point
1048 // XTYPE instructions. Since there is no convenient way of identifying fp
1049 // XTYPE instructions, only allow grouping with ALU32 for now.
1050 unsigned TJ = HII.getType(MJ);
1051 if (TJ != HexagonII::TypeALU32)
1052 return true;
1053 break;
1054 }
1055 default:
1056 break;
1057 }
1058
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001059 // "False" really means that the quick check failed to determine if
1060 // I and J cannot coexist.
1061 return false;
1062}
1063
1064
1065// Full, symmetric check.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001066bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
1067 const MachineInstr &MJ) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001068 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1069}
1070
1071void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1072 for (auto &B : MF) {
1073 MachineBasicBlock::iterator BundleIt;
1074 MachineBasicBlock::instr_iterator NextI;
1075 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1076 NextI = std::next(I);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001077 MachineInstr &MI = *I;
1078 if (MI.isBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001079 BundleIt = I;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001080 if (!MI.isInsideBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001081 continue;
1082
1083 // Decide on where to insert the instruction that we are pulling out.
1084 // Debug instructions always go before the bundle, but the placement of
1085 // INLINE_ASM depends on potential dependencies. By default, try to
1086 // put it before the bundle, but if the asm writes to a register that
1087 // other instructions in the bundle read, then we need to place it
1088 // after the bundle (to preserve the bundle semantics).
1089 bool InsertBeforeBundle;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001090 if (MI.isInlineAsm())
1091 InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
1092 else if (MI.isDebugValue())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001093 InsertBeforeBundle = true;
1094 else
1095 continue;
1096
1097 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1098 }
1099 }
1100}
1101
1102// Check if a given instruction is of class "system".
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001103static bool isSystemInstr(const MachineInstr &MI) {
1104 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001105 switch (Opc) {
1106 case Hexagon::Y2_barrier:
1107 case Hexagon::Y2_dcfetchbo:
1108 return true;
1109 }
1110 return false;
1111}
1112
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001113bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
1114 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001115 // The dependence graph may not include edges between dead definitions,
1116 // so without extra checks, we could end up packetizing two instruction
1117 // defining the same (dead) register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001118 if (I.isCall() || J.isCall())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001119 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001120 if (HII->isPredicated(I) || HII->isPredicated(J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001121 return false;
1122
1123 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001124 for (auto &MO : I.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001125 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1126 continue;
1127 DeadDefs[MO.getReg()] = true;
1128 }
1129
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001130 for (auto &MO : J.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001131 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1132 continue;
1133 unsigned R = MO.getReg();
1134 if (R != Hexagon::USR_OVF && DeadDefs[R])
1135 return true;
1136 }
1137 return false;
1138}
1139
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001140bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
1141 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001142 // A save callee-save register function call can only be in a packet
1143 // with instructions that don't write to the callee-save registers.
1144 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1145 doesModifyCalleeSavedReg(J, HRI)) ||
1146 (HII->isSaveCalleeSavedRegsCall(J) &&
1147 doesModifyCalleeSavedReg(I, HRI)))
1148 return true;
1149
1150 // Two control flow instructions cannot go in the same packet.
1151 if (isControlFlow(I) && isControlFlow(J))
1152 return true;
1153
1154 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1155 // contain a speculative indirect jump,
1156 // a new-value compare jump or a dealloc_return.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001157 auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
1158 if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001159 return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001160 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001161 return true;
1162 return false;
1163 };
1164
1165 if (HII->isLoopN(I) && isBadForLoopN(J))
1166 return true;
1167 if (HII->isLoopN(J) && isBadForLoopN(I))
1168 return true;
1169
1170 // dealloc_return cannot appear in the same packet as a conditional or
1171 // unconditional jump.
1172 return HII->isDeallocRet(I) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001173 (J.isBranch() || J.isCall() || J.isBarrier());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001174}
1175
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001176bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I,
1177 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001178 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001179 bool StoreI = I.mayStore(), StoreJ = J.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001180 if ((SysI && StoreJ) || (SysJ && StoreI))
1181 return true;
1182
1183 if (StoreI && StoreJ) {
1184 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1185 return true;
1186 } else {
1187 // A memop cannot be in the same packet with another memop or a store.
1188 // Two stores can be together, but here I and J cannot both be stores.
1189 bool MopStI = HII->isMemOp(I) || StoreI;
1190 bool MopStJ = HII->isMemOp(J) || StoreJ;
1191 if (MopStI && MopStJ)
1192 return true;
1193 }
1194
1195 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1196}
1197
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001198// SUI is the current instruction that is out side of the current packet.
1199// SUJ is the current instruction inside the current packet against which that
1200// SUI will be packetized.
1201bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001202 assert(SUI->getInstr() && SUJ->getInstr());
1203 MachineInstr &I = *SUI->getInstr();
1204 MachineInstr &J = *SUJ->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001205
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001206 // Clear IgnoreDepMIs when Packet starts.
1207 if (CurrentPacketMIs.size() == 1)
1208 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001209
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001210 MachineBasicBlock::iterator II = I.getIterator();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001211
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001212 // Solo instructions cannot go in the packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001213 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001214
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001215 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001216 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001217
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001218 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1219 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001220 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001221
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001222 // V4 allows dual stores. It does not allow second store, if the first
1223 // store is not in SLOT0. New value store, new value jump, dealloc_return
1224 // and memop always take SLOT0. Arch spec 3.4.4.2.
1225 Dependence = hasV4SpecificDependence(I, J);
1226 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001227 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001228
1229 // If an instruction feeds new value jump, glue it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001230 MachineBasicBlock::iterator NextMII = I.getIterator();
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001231 ++NextMII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001232 if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001233 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001234
1235 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001236 const MachineOperand &NOp0 = NextMI.getOperand(0);
1237 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001238
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001239 if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001240 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001241
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001242 for (auto T : CurrentPacketMIs) {
1243 SUnit *PacketSU = MIToSUnit.find(T)->second;
1244 MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001245 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001246 if (PI.isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001247 Dependence = true;
1248 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001249 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001250 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001251 // 1. Packet does not have a store in it.
1252 // 2. If the first operand of the nvj is newified, and the second
1253 // operand is also a reg, it (second reg) is not defined in
1254 // the same packet.
1255 // 3. If the second operand of the nvj is newified, (which means
1256 // first operand is also a reg), first reg is not defined in
1257 // the same packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001258 if (PI.getOpcode() == Hexagon::S2_allocframe || PI.mayStore() ||
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001259 HII->isLoopN(PI)) {
1260 Dependence = true;
1261 break;
1262 }
1263 // Check #2/#3.
1264 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001265 if (OpR.isReg() && PI.modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001266 Dependence = true;
1267 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001268 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001269 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001270
1271 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001272 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001273 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001274 }
1275
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001276 // There no dependency between a prolog instruction and its successor.
1277 if (!SUJ->isSucc(SUI))
1278 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001279
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001280 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1281 if (FoundSequentialDependence)
1282 break;
1283
1284 if (SUJ->Succs[i].getSUnit() != SUI)
1285 continue;
1286
1287 SDep::Kind DepType = SUJ->Succs[i].getKind();
1288 // For direct calls:
1289 // Ignore register dependences for call instructions for packetization
1290 // purposes except for those due to r31 and predicate registers.
1291 //
1292 // For indirect calls:
1293 // Same as direct calls + check for true dependences to the register
1294 // used in the indirect call.
1295 //
1296 // We completely ignore Order dependences for call instructions.
1297 //
1298 // For returns:
1299 // Ignore register dependences for return instructions like jumpr,
1300 // dealloc return unless we have dependencies on the explicit uses
1301 // of the registers used by jumpr (like r31) or dealloc return
1302 // (like r29 or r30).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001303 unsigned DepReg = 0;
1304 const TargetRegisterClass *RC = nullptr;
1305 if (DepType == SDep::Data) {
1306 DepReg = SUJ->Succs[i].getReg();
1307 RC = HRI->getMinimalPhysRegClass(DepReg);
1308 }
1309
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +00001310 if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001311 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001312 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001313 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1314 continue;
1315 }
1316
1317 if (DepType == SDep::Data) {
1318 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1319 if (promoteToDotCur(J, DepType, II, RC))
1320 continue;
1321 }
1322
1323 // Data dpendence ok if we have load.cur.
1324 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
1325 if (HII->isV60VectorInstruction(I))
1326 continue;
1327 }
1328
1329 // For instructions that can be promoted to dot-new, try to promote.
1330 if (DepType == SDep::Data) {
1331 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1332 if (promoteToDotNew(I, DepType, II, RC)) {
1333 PromotedToDotNew = true;
1334 continue;
1335 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001336 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001337 if (HII->isNewValueJump(I))
1338 continue;
1339 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001340
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001341 // For predicated instructions, if the predicates are complements then
1342 // there can be no dependence.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001343 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1344 arePredicatesComplements(I, J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001345 // Not always safe to do this translation.
1346 // DAG Builder attempts to reduce dependence edges using transitive
1347 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001348 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001349 // r0 = tfr_pt ... (1)
1350 // r0 = tfr_pf ... (2)
1351 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001352 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001353 // There will be an output dependence between (1)->(2) and (2)->(3).
1354 // However, there is no dependence edge between (1)->(3). This results
1355 // in all 3 instructions going in the same packet. We ignore dependce
1356 // only once to avoid this situation.
David Majnemer0d955d02016-08-11 22:21:41 +00001357 auto Itr = find(IgnoreDepMIs, &J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001358 if (Itr != IgnoreDepMIs.end()) {
1359 Dependence = true;
1360 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001361 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001362 IgnoreDepMIs.push_back(&I);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001363 continue;
1364 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001365
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001366 // Ignore Order dependences between unconditional direct branches
1367 // and non-control-flow instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001368 if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001369 DepType == SDep::Order)
1370 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001371
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001372 // Ignore all dependences for jumps except for true and output
1373 // dependences.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001374 if (I.isConditionalBranch() && DepType != SDep::Data &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001375 DepType != SDep::Output)
1376 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001377
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001378 // Ignore output dependences due to superregs. We can write to two
1379 // different subregisters of R1:0 for instance in the same cycle.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001380
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001381 // If neither I nor J defines DepReg, then this is a superfluous output
1382 // dependence. The dependence must be of the form:
1383 // R0 = ...
1384 // R1 = ...
1385 // and there is an output dependence between the two instructions with
1386 // DepReg = D0.
1387 // We want to ignore these dependences. Ideally, the dependence
1388 // constructor should annotate such dependences. We can then avoid this
1389 // relatively expensive check.
1390 //
1391 if (DepType == SDep::Output) {
1392 // DepReg is the register that's responsible for the dependence.
1393 unsigned DepReg = SUJ->Succs[i].getReg();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001394
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001395 // Check if I and J really defines DepReg.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001396 if (!I.definesRegister(DepReg) && !J.definesRegister(DepReg))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001397 continue;
1398 FoundSequentialDependence = true;
1399 break;
1400 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001401
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001402 // For Order dependences:
1403 // 1. On V4 or later, volatile loads/stores can be packetized together,
1404 // unless other rules prevent is.
1405 // 2. Store followed by a load is not allowed.
1406 // 3. Store followed by a store is only valid on V4 or later.
1407 // 4. Load followed by any memory operation is allowed.
1408 if (DepType == SDep::Order) {
1409 if (!PacketizeVolatiles) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001410 bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001411 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001412 FoundSequentialDependence = true;
1413 break;
1414 }
1415 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001416 // J is first, I is second.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001417 bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
1418 bool LoadI = I.mayLoad(), StoreI = I.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001419 if (StoreJ) {
1420 // Two stores are only allowed on V4+. Load following store is never
1421 // allowed.
1422 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001423 FoundSequentialDependence = true;
1424 break;
1425 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001426 } else if (!LoadJ || (!LoadI && !StoreI)) {
1427 // If J is neither load nor store, assume a dependency.
1428 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001429 FoundSequentialDependence = true;
1430 break;
1431 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001432 // Store followed by store: not OK on V2.
1433 // Store followed by load: not OK on all.
1434 // Load followed by store: OK on all.
1435 // Load followed by load: OK on all.
1436 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001437 }
1438
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001439 // For V4, special case ALLOCFRAME. Even though there is dependency
1440 // between ALLOCFRAME and subsequent store, allow it to be packetized
1441 // in a same packet. This implies that the store is using the caller's
1442 // SP. Hence, offset needs to be updated accordingly.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001443 if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
1444 unsigned Opc = I.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001445 switch (Opc) {
1446 case Hexagon::S2_storerd_io:
1447 case Hexagon::S2_storeri_io:
1448 case Hexagon::S2_storerh_io:
1449 case Hexagon::S2_storerb_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001450 if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001451 // Since this store is to be glued with allocframe in the same
1452 // packet, it will use SP of the previous stack frame, i.e.
1453 // caller's SP. Therefore, we need to recalculate offset
1454 // according to this change.
1455 GlueAllocframeStore = useCallersSP(I);
1456 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001457 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001458 }
1459 default:
1460 break;
1461 }
1462 }
1463
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001464 // There are certain anti-dependencies that cannot be ignored.
1465 // Specifically:
1466 // J2_call ... %R0<imp-def> ; SUJ
1467 // R0 = ... ; SUI
1468 // Those cannot be packetized together, since the call will observe
1469 // the effect of the assignment to R0.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001470 if (DepType == SDep::Anti && J.isCall()) {
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001471 // Check if I defines any volatile register. We should also check
1472 // registers that the call may read, but these happen to be a
1473 // subset of the volatile register set.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001474 for (const MCPhysReg *P = J.getDesc().ImplicitDefs; P && *P; ++P) {
1475 if (!I.modifiesRegister(*P, HRI))
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001476 continue;
1477 FoundSequentialDependence = true;
1478 break;
1479 }
1480 }
1481
1482 // Skip over remaining anti-dependences. Two instructions that are
1483 // anti-dependent can share a packet, since in most such cases all
1484 // operands are read before any modifications take place.
1485 // The exceptions are branch and call instructions, since they are
1486 // executed after all other instructions have completed (at least
1487 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001488 if (DepType != SDep::Anti) {
1489 FoundSequentialDependence = true;
1490 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001491 }
1492 }
1493
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001494 if (FoundSequentialDependence) {
1495 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001496 return false;
1497 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001498
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001499 return true;
1500}
1501
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001502bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001503 assert(SUI->getInstr() && SUJ->getInstr());
1504 MachineInstr &I = *SUI->getInstr();
1505 MachineInstr &J = *SUJ->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001506
1507 if (cannotCoexist(I, J))
1508 return false;
1509
1510 if (!Dependence)
1511 return true;
1512
1513 // Check if the instruction was promoted to a dot-new. If so, demote it
1514 // back into a dot-old.
1515 if (PromotedToDotNew)
1516 demoteToDotOld(I);
1517
1518 cleanUpDotCur();
1519 // Check if the instruction (must be a store) was glued with an allocframe
1520 // instruction. If so, restore its offset to its original value, i.e. use
1521 // current SP instead of caller's SP.
1522 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001523 useCalleesSP(I);
1524 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001525 }
1526 return false;
1527}
1528
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001529MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001530HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001531 MachineBasicBlock::iterator MII = MI.getIterator();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001532 MachineBasicBlock *MBB = MI.getParent();
1533 if (MI.isImplicitDef()) {
1534 unsigned R = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001535 if (Hexagon::IntRegsRegClass.contains(R)) {
1536 MCSuperRegIterator S(R, HRI, false);
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001537 MI.addOperand(MachineOperand::CreateReg(*S, true, true));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001538 }
1539 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001540 }
1541 assert(ResourceTracker->canReserveResources(MI));
1542
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001543 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001544 bool Good = true;
1545
1546 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001547 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001548 // We need to put both instructions in the same packet: MI and NvjMI.
1549 // Either of them can require a constant extender. Try to add both to
1550 // the current packet, and if that fails, end the packet and start a
1551 // new one.
1552 ResourceTracker->reserveResources(MI);
1553 if (ExtMI)
1554 Good = tryAllocateResourcesForConstExt(true);
1555
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001556 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001557 if (Good) {
1558 if (ResourceTracker->canReserveResources(NvjMI))
1559 ResourceTracker->reserveResources(NvjMI);
1560 else
1561 Good = false;
1562 }
1563 if (Good && ExtNvjMI)
1564 Good = tryAllocateResourcesForConstExt(true);
1565
1566 if (!Good) {
1567 endPacket(MBB, MI);
1568 assert(ResourceTracker->canReserveResources(MI));
1569 ResourceTracker->reserveResources(MI);
1570 if (ExtMI) {
1571 assert(canReserveResourcesForConstExt());
1572 tryAllocateResourcesForConstExt(true);
1573 }
1574 assert(ResourceTracker->canReserveResources(NvjMI));
1575 ResourceTracker->reserveResources(NvjMI);
1576 if (ExtNvjMI) {
1577 assert(canReserveResourcesForConstExt());
1578 reserveResourcesForConstExt();
1579 }
1580 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001581 CurrentPacketMIs.push_back(&MI);
1582 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001583 return MII;
1584 }
1585
1586 ResourceTracker->reserveResources(MI);
1587 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1588 endPacket(MBB, MI);
1589 if (PromotedToDotNew)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001590 demoteToDotOld(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001591 if (GlueAllocframeStore) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001592 useCalleesSP(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001593 GlueAllocframeStore = false;
1594 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001595 ResourceTracker->reserveResources(MI);
1596 reserveResourcesForConstExt();
1597 }
1598
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001599 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001600 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001601}
1602
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001603void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001604 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001605 OldPacketMIs = CurrentPacketMIs;
1606 VLIWPacketizerList::endPacket(MBB, MI);
1607}
1608
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001609bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001610 return !producesStall(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001611}
1612
1613
1614// Return true when ConsMI uses a register defined by ProdMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001615static bool isDependent(const MachineInstr &ProdMI,
1616 const MachineInstr &ConsMI) {
1617 if (!ProdMI.getOperand(0).isReg())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001618 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001619 unsigned DstReg = ProdMI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001620
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001621 for (auto &Op : ConsMI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001622 if (Op.isReg() && Op.isUse() && Op.getReg() == DstReg)
1623 // The MIs depend on each other.
1624 return true;
1625
1626 return false;
1627}
1628
1629// V60 forward scheduling.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001630bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001631 // Check whether the previous packet is in a different loop. If this is the
1632 // case, there is little point in trying to avoid a stall because that would
1633 // favor the rare case (loop entry) over the common case (loop iteration).
1634 //
1635 // TODO: We should really be able to check all the incoming edges if this is
1636 // the first packet in a basic block, so we can avoid stalls from the loop
1637 // backedge.
1638 if (!OldPacketMIs.empty()) {
1639 auto *OldBB = OldPacketMIs.front()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001640 auto *ThisBB = I.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001641 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1642 return false;
1643 }
1644
1645 // Check for stall between two vector instructions.
1646 if (HII->isV60VectorInstruction(I)) {
1647 for (auto J : OldPacketMIs) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001648 if (!HII->isV60VectorInstruction(*J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001649 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001650 if (isDependent(*J, I) && !HII->isVecUsableNextPacket(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001651 return true;
1652 }
1653 return false;
1654 }
1655
1656 // Check for stall between two scalar instructions. First, check that
1657 // there is no definition of a use in the current packet, because it
1658 // may be a candidate for .new.
1659 for (auto J : CurrentPacketMIs)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001660 if (!HII->isV60VectorInstruction(*J) && isDependent(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001661 return false;
1662
1663 // Check for stall between I and instructions in the previous packet.
1664 if (MF.getSubtarget<HexagonSubtarget>().useBSBScheduling()) {
1665 for (auto J : OldPacketMIs) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001666 if (HII->isV60VectorInstruction(*J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001667 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001668 if (!HII->isLateInstrFeedsEarlyInstr(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001669 continue;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001670 if (isDependent(*J, I) && !HII->canExecuteInBundle(*J, I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001671 return true;
1672 }
1673 }
1674
1675 return false;
1676}
1677
1678
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001679//===----------------------------------------------------------------------===//
1680// Public Constructor Functions
1681//===----------------------------------------------------------------------===//
1682
1683FunctionPass *llvm::createHexagonPacketizer() {
1684 return new HexagonPacketizer();
1685}