blob: 16d3733b92ed7c28263e689b0f60d8771c3c7c2d [file] [log] [blame]
Eugene Zelenko3b873362017-09-28 22:27:31 +00001//===- HexagonPacketizer.cpp - VLIW packetizer ----------------------------===//
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00002//
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//===----------------------------------------------------------------------===//
Eugene Zelenko3b873362017-09-28 22:27:31 +000019
Chandler Carruth6bda14b2017-06-06 11:49:48 +000020#include "HexagonVLIWPacketizer.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000021#include "Hexagon.h"
22#include "HexagonInstrInfo.h"
Jyotsna Verma84256432013-03-01 17:37:13 +000023#include "HexagonRegisterInfo.h"
24#include "HexagonSubtarget.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000025#include "llvm/ADT/BitVector.h"
26#include "llvm/ADT/DenseSet.h"
27#include "llvm/ADT/STLExtras.h"
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000028#include "llvm/Analysis/AliasAnalysis.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000029#include "llvm/CodeGen/MachineBasicBlock.h"
30#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/CodeGen/MachineDominators.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
33#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000034#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000035#include "llvm/CodeGen/MachineInstr.h"
Matthias Braunf8422972017-12-13 02:51:04 +000036#include "llvm/CodeGen/MachineInstrBundle.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000037#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000038#include "llvm/CodeGen/MachineOperand.h"
39#include "llvm/CodeGen/ScheduleDAG.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000040#include "llvm/CodeGen/TargetRegisterInfo.h"
41#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000042#include "llvm/IR/DebugLoc.h"
43#include "llvm/MC/MCInstrDesc.h"
44#include "llvm/Pass.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000045#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000046#include "llvm/Support/Debug.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000047#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000049#include <cassert>
50#include <cstdint>
51#include <iterator>
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000052
53using namespace llvm;
54
Chandler Carruth84e68b22014-04-22 02:41:26 +000055#define DEBUG_TYPE "packets"
56
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000057static cl::opt<bool> DisablePacketizer("disable-packetizer", cl::Hidden,
58 cl::ZeroOrMore, cl::init(false),
59 cl::desc("Disable Hexagon packetizer pass"));
60
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +000061cl::opt<bool> Slot1Store("slot1-store-slot0-load", cl::Hidden,
62 cl::ZeroOrMore, cl::init(true),
63 cl::desc("Allow slot1 store and slot0 load"));
64
Jyotsna Verma1d297502013-05-02 15:39:30 +000065static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000066 cl::ZeroOrMore, cl::Hidden, cl::init(true),
67 cl::desc("Allow non-solo packetization of volatile memory references"));
68
69static cl::opt<bool> EnableGenAllInsnClass("enable-gen-insn", cl::init(false),
70 cl::Hidden, cl::ZeroOrMore, cl::desc("Generate all instruction with TC"));
71
72static cl::opt<bool> DisableVecDblNVStores("disable-vecdbl-nv-stores",
73 cl::init(false), cl::Hidden, cl::ZeroOrMore,
74 cl::desc("Disable vector double new-value-stores"));
75
76extern cl::opt<bool> ScheduleInlineAsm;
Jyotsna Verma1d297502013-05-02 15:39:30 +000077
Jyotsna Verma1d297502013-05-02 15:39:30 +000078namespace llvm {
Jyotsna Verma1d297502013-05-02 15:39:30 +000079
Eugene Zelenko3b873362017-09-28 22:27:31 +000080FunctionPass *createHexagonPacketizer();
81void initializeHexagonPacketizerPass(PassRegistry&);
82
83} // end namespace llvm
Jyotsna Verma1d297502013-05-02 15:39:30 +000084
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000085namespace {
Eugene Zelenko3b873362017-09-28 22:27:31 +000086
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000087 class HexagonPacketizer : public MachineFunctionPass {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000088 public:
89 static char ID;
Eugene Zelenko3b873362017-09-28 22:27:31 +000090
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +000091 HexagonPacketizer() : MachineFunctionPass(ID) {}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000092
Craig Topper906c2cd2014-04-29 07:58:16 +000093 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000094 AU.setPreservesCFG();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000095 AU.addRequired<AAResultsWrapperPass>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000096 AU.addRequired<MachineBranchProbabilityInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000097 AU.addRequired<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000098 AU.addRequired<MachineLoopInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000099 AU.addPreserved<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000100 AU.addPreserved<MachineLoopInfo>();
101 MachineFunctionPass::getAnalysisUsage(AU);
102 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000103
Mehdi Amini117296c2016-10-01 02:56:57 +0000104 StringRef getPassName() const override { return "Hexagon Packetizer"; }
Craig Topper906c2cd2014-04-29 07:58:16 +0000105 bool runOnMachineFunction(MachineFunction &Fn) override;
Eugene Zelenko3b873362017-09-28 22:27:31 +0000106
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000107 MachineFunctionProperties getRequiredProperties() const override {
108 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000109 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000110 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000111
112 private:
113 const HexagonInstrInfo *HII;
114 const HexagonRegisterInfo *HRI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000115 };
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000116
Eugene Zelenko3b873362017-09-28 22:27:31 +0000117} // end anonymous namespace
118
119char HexagonPacketizer::ID = 0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000120
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000121INITIALIZE_PASS_BEGIN(HexagonPacketizer, "hexagon-packetizer",
122 "Hexagon Packetizer", false, false)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000123INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
124INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
125INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000126INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000127INITIALIZE_PASS_END(HexagonPacketizer, "hexagon-packetizer",
128 "Hexagon Packetizer", false, false)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000129
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000130HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
131 MachineLoopInfo &MLI, AliasAnalysis *AA,
132 const MachineBranchProbabilityInfo *MBPI)
133 : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI) {
134 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
135 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000136
Eugene Zelenko3b873362017-09-28 22:27:31 +0000137 addMutation(llvm::make_unique<HexagonSubtarget::UsrOverflowMutation>());
138 addMutation(llvm::make_unique<HexagonSubtarget::HVXMemLatencyMutation>());
139 addMutation(llvm::make_unique<HexagonSubtarget::BankConflictMutation>());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000140}
141
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000142// Check if FirstI modifies a register that SecondI reads.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000143static bool hasWriteToReadDep(const MachineInstr &FirstI,
144 const MachineInstr &SecondI,
145 const TargetRegisterInfo *TRI) {
146 for (auto &MO : FirstI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000147 if (!MO.isReg() || !MO.isDef())
148 continue;
149 unsigned R = MO.getReg();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000150 if (SecondI.readsRegister(R, TRI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000151 return true;
152 }
153 return false;
154}
155
156
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000157static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000158 MachineBasicBlock::iterator BundleIt, bool Before) {
159 MachineBasicBlock::instr_iterator InsertPt;
160 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000161 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000162 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000163 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000164
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000165 MachineBasicBlock &B = *MI.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000166 // The instruction should at least be bundled with the preceding instruction
167 // (there will always be one, i.e. BUNDLE, if nothing else).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000168 assert(MI.isBundledWithPred());
169 if (MI.isBundledWithSucc()) {
170 MI.clearFlag(MachineInstr::BundledSucc);
171 MI.clearFlag(MachineInstr::BundledPred);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000172 } else {
173 // If it's not bundled with the successor (i.e. it is the last one
174 // in the bundle), then we can simply unbundle it from the predecessor,
175 // which will take care of updating the predecessor's flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000176 MI.unbundleFromPred();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000177 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000178 B.splice(InsertPt, &B, MI.getIterator());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000179
180 // Get the size of the bundle without asserting.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000181 MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000182 MachineBasicBlock::const_instr_iterator E = B.instr_end();
183 unsigned Size = 0;
184 for (++I; I != E && I->isBundledWithPred(); ++I)
185 ++Size;
186
187 // If there are still two or more instructions, then there is nothing
188 // else to be done.
189 if (Size > 1)
190 return BundleIt;
191
192 // Otherwise, extract the single instruction out and delete the bundle.
193 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000194 MachineInstr &SingleI = *BundleIt->getNextNode();
195 SingleI.unbundleFromPred();
196 assert(!SingleI.isBundledWithSucc());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000197 BundleIt->eraseFromParent();
198 return NextIt;
199}
200
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000201bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +0000202 auto &HST = MF.getSubtarget<HexagonSubtarget>();
203 if (DisablePacketizer || !HST.usePackets() || skipFunction(MF.getFunction()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000204 return false;
205
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +0000206 HII = HST.getInstrInfo();
207 HRI = HST.getRegisterInfo();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000208 auto &MLI = getAnalysis<MachineLoopInfo>();
209 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
210 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
211
212 if (EnableGenAllInsnClass)
213 HII->genAllInsnTimingClasses(MF);
214
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000215 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000216 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000217
218 // DFA state table should not be empty.
219 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
220
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000221 // Loop over all basic blocks and remove KILL pseudo-instructions
222 // These instructions confuse the dependence analysis. Consider:
223 // D0 = ... (Insn 0)
224 // R0 = KILL R0, D0 (Insn 1)
225 // R0 = ... (Insn 2)
226 // Here, Insn 1 will result in the dependence graph not emitting an output
227 // dependence between Insn 0 and Insn 2. This can lead to incorrect
228 // packetization
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000229 for (auto &MB : MF) {
230 auto End = MB.end();
231 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000232 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000233 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000234 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000235 MB.erase(MI);
236 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000237 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000238 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000239 }
240 }
241
242 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000243 for (auto &MB : MF) {
244 auto Begin = MB.begin(), End = MB.end();
245 while (Begin != End) {
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000246 // Find the first non-boundary starting from the end of the last
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000247 // scheduling region.
248 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000249 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000250 ++RB;
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000251 // Find the first boundary starting from the beginning of the new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000252 // region.
253 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000254 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000255 ++RE;
256 // Add the scheduling boundary if it's not block end.
257 if (RE != End)
258 ++RE;
259 // If RB == End, then RE == End.
260 if (RB != End)
261 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000262
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000263 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000264 }
265 }
266
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000267 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000268 return true;
269}
270
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000271// Reserve resources for a constant extender. Trigger an assertion if the
272// reservation fails.
273void HexagonPacketizerList::reserveResourcesForConstExt() {
274 if (!tryAllocateResourcesForConstExt(true))
275 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000276}
277
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000278bool HexagonPacketizerList::canReserveResourcesForConstExt() {
279 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000280}
281
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000282// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
283// return true, otherwise, return false.
284bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
285 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000286 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000287 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000288 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000289 MF.DeleteMachineInstr(ExtMI);
290 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000291}
292
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000293bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000294 SDep::Kind DepType, unsigned DepReg) {
295 // Check for LR dependence.
296 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000297 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000298
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000299 if (HII->isDeallocRet(MI))
300 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000301 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000302
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000303 // Call-like instructions can be packetized with preceding instructions
304 // that define registers implicitly used or modified by the call. Explicit
305 // uses are still prohibited, as in the case of indirect calls:
306 // r0 = ...
307 // J2_jumpr r0
308 if (DepType == SDep::Data) {
309 for (const MachineOperand MO : MI.operands())
310 if (MO.isReg() && MO.getReg() == DepReg && !MO.isImplicit())
311 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000312 }
313
314 return false;
315}
316
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000317static bool isRegDependence(const SDep::Kind DepType) {
318 return DepType == SDep::Data || DepType == SDep::Anti ||
319 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000320}
321
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000322static bool isDirectJump(const MachineInstr &MI) {
323 return MI.getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000324}
325
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000326static bool isSchedBarrier(const MachineInstr &MI) {
327 switch (MI.getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000328 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000329 return true;
330 }
331 return false;
332}
333
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000334static bool isControlFlow(const MachineInstr &MI) {
335 return MI.getDesc().isTerminator() || MI.getDesc().isCall();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000336}
337
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000338/// Returns true if the instruction modifies a callee-saved register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000339static bool doesModifyCalleeSavedReg(const MachineInstr &MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000340 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000341 const MachineFunction &MF = *MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000342 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000343 if (MI.modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000344 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000345 return false;
346}
347
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000348// Returns true if an instruction can be promoted to .new predicate or
349// new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000350bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI,
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000351 const TargetRegisterClass *NewRC) {
352 // Vector stores can be predicated, and can be new-value stores, but
353 // they cannot be predicated on a .new predicate value.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000354 if (NewRC == &Hexagon::PredRegsRegClass) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000355 if (HII->isHVXVec(MI) && MI.mayStore())
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000356 return false;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000357 return HII->isPredicated(MI) && HII->getDotNewPredOp(MI, nullptr) > 0;
358 }
359 // If the class is not PredRegs, it could only apply to new-value stores.
360 return HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000361}
362
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000363// Promote an instructiont to its .cur form.
364// At this time, we have already made a call to canPromoteToDotCur and made
365// sure that it can *indeed* be promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000366bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000367 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
368 const TargetRegisterClass* RC) {
369 assert(DepType == SDep::Data);
370 int CurOpcode = HII->getDotCurOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000371 MI.setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000372 return true;
373}
374
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000375void HexagonPacketizerList::cleanUpDotCur() {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000376 MachineInstr *MI = nullptr;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000377 for (auto BI : CurrentPacketMIs) {
378 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000379 if (HII->isDotCurInst(*BI)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000380 MI = BI;
381 continue;
382 }
383 if (MI) {
384 for (auto &MO : BI->operands())
385 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
386 return;
387 }
388 }
389 if (!MI)
390 return;
391 // We did not find a use of the CUR, so de-cur it.
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000392 MI->setDesc(HII->get(HII->getNonDotCurOp(*MI)));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000393 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
394}
395
396// Check to see if an instruction can be dot cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000397bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000398 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
399 const TargetRegisterClass *RC) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000400 if (!HII->isHVXVec(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000401 return false;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000402 if (!HII->isHVXVec(*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000403 return false;
404
405 // Already a dot new instruction.
406 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
407 return false;
408
409 if (!HII->mayBeCurLoad(MI))
410 return false;
411
412 // The "cur value" cannot come from inline asm.
413 if (PacketSU->getInstr()->isInlineAsm())
414 return false;
415
416 // Make sure candidate instruction uses cur.
417 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000418 MI.dump();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000419 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000420 MachineInstr &MJ = *MII;
421 DEBUG({
422 dbgs() << "Checking CUR against ";
423 MJ.dump();
424 });
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000425 unsigned DestReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000426 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000427 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000428 if (MO.isReg() && MO.getReg() == DestReg)
429 FoundMatch = true;
430 if (!FoundMatch)
431 return false;
432
433 // Check for existing uses of a vector register within the packet which
434 // would be affected by converting a vector load into .cur formt.
435 for (auto BI : CurrentPacketMIs) {
436 DEBUG(dbgs() << "packet has "; BI->dump(););
437 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
438 return false;
439 }
440
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000441 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000442 // We can convert the opcode into a .cur.
443 return true;
444}
445
446// Promote an instruction to its .new form. At this time, we have already
447// made a call to canPromoteToDotNew and made sure that it can *indeed* be
448// promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000449bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000450 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
451 const TargetRegisterClass* RC) {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000452 assert(DepType == SDep::Data);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000453 int NewOpcode;
454 if (RC == &Hexagon::PredRegsRegClass)
455 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
456 else
457 NewOpcode = HII->getDotNewOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000458 MI.setDesc(HII->get(NewOpcode));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000459 return true;
460}
461
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000462bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +0000463 int NewOpcode = HII->getDotOldOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000464 MI.setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000465 return true;
466}
467
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000468bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) {
469 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000470 switch (Opc) {
471 case Hexagon::S2_storerd_io:
472 case Hexagon::S2_storeri_io:
473 case Hexagon::S2_storerh_io:
474 case Hexagon::S2_storerb_io:
475 break;
476 default:
477 llvm_unreachable("Unexpected instruction");
478 }
Matthias Braun941a7052016-07-28 18:40:00 +0000479 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000480 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000481 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000482 if (HII->isValidOffset(Opc, NewOff, HRI)) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000483 Off.setImm(NewOff);
484 return true;
485 }
486 return false;
487}
488
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000489void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) {
490 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000491 switch (Opc) {
492 case Hexagon::S2_storerd_io:
493 case Hexagon::S2_storeri_io:
494 case Hexagon::S2_storerh_io:
495 case Hexagon::S2_storerb_io:
496 break;
497 default:
498 llvm_unreachable("Unexpected instruction");
499 }
Matthias Braun941a7052016-07-28 18:40:00 +0000500 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000501 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000502 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
503}
504
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +0000505/// Return true if we can update the offset in MI so that MI and MJ
506/// can be packetized together.
507bool HexagonPacketizerList::updateOffset(SUnit *SUI, SUnit *SUJ) {
508 assert(SUI->getInstr() && SUJ->getInstr());
509 MachineInstr &MI = *SUI->getInstr();
510 MachineInstr &MJ = *SUJ->getInstr();
511
512 unsigned BPI, OPI;
513 if (!HII->getBaseAndOffsetPosition(MI, BPI, OPI))
514 return false;
515 unsigned BPJ, OPJ;
516 if (!HII->getBaseAndOffsetPosition(MJ, BPJ, OPJ))
517 return false;
518 unsigned Reg = MI.getOperand(BPI).getReg();
519 if (Reg != MJ.getOperand(BPJ).getReg())
520 return false;
521 // Make sure that the dependences do not restrict adding MI to the packet.
522 // That is, ignore anti dependences, and make sure the only data dependence
523 // involves the specific register.
524 for (const auto &PI : SUI->Preds)
525 if (PI.getKind() != SDep::Anti &&
526 (PI.getKind() != SDep::Data || PI.getReg() != Reg))
527 return false;
528 int Incr;
529 if (!HII->getIncrementValue(MJ, Incr))
530 return false;
531
532 int64_t Offset = MI.getOperand(OPI).getImm();
533 MI.getOperand(OPI).setImm(Offset + Incr);
534 ChangedOffset = Offset;
535 return true;
536}
537
538/// Undo the changed offset. This is needed if the instruction cannot be
539/// added to the current packet due to a different instruction.
540void HexagonPacketizerList::undoChangedOffset(MachineInstr &MI) {
541 unsigned BP, OP;
542 if (!HII->getBaseAndOffsetPosition(MI, BP, OP))
543 llvm_unreachable("Unable to find base and offset operands.");
544 MI.getOperand(OP).setImm(ChangedOffset);
545}
546
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000547enum PredicateKind {
548 PK_False,
549 PK_True,
550 PK_Unknown
551};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000552
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000553/// Returns true if an instruction is predicated on p0 and false if it's
554/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000555static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000556 const HexagonInstrInfo *HII) {
557 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000558 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000559 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000560 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000561 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000562}
563
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000564static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000565 const HexagonInstrInfo *HII) {
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000566 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000567#ifndef NDEBUG
568 // Post Increment means duplicates. Use dense map to find duplicates in the
569 // list. Caution: Densemap initializes with the minimum of 64 buckets,
570 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000571 DenseSet<unsigned> DefRegsSet;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000572 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000573 if (MO.isReg() && MO.isDef())
574 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000575
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000576 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000577 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
578 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000579#else
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000580 if (MI.mayLoad()) {
581 const MachineOperand &Op1 = MI.getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000582 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000583 assert(Op1.isReg() && "Post increment operand has be to a register.");
584 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000585 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000586 if (MI.getDesc().mayStore()) {
587 const MachineOperand &Op0 = MI.getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000588 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000589 assert(Op0.isReg() && "Post increment operand has be to a register.");
590 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000591 }
592#endif
593 // we should never come here.
594 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
595}
596
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000597// Get the value being stored.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000598static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000599 // value being stored is always the last operand.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000600 return MI.getOperand(MI.getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000601}
602
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000603static bool isLoadAbsSet(const MachineInstr &MI) {
604 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000605 switch (Opc) {
606 case Hexagon::L4_loadrd_ap:
607 case Hexagon::L4_loadrb_ap:
608 case Hexagon::L4_loadrh_ap:
609 case Hexagon::L4_loadrub_ap:
610 case Hexagon::L4_loadruh_ap:
611 case Hexagon::L4_loadri_ap:
612 return true;
613 }
614 return false;
615}
616
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000617static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000618 assert(isLoadAbsSet(MI));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000619 return MI.getOperand(1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000620}
621
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000622// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000623// Following restrictions are to be respected in convert a store into
624// a new value store.
625// 1. If an instruction uses auto-increment, its address register cannot
626// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000627// 2. If an instruction uses absolute-set addressing mode, its address
628// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000629// 3. If an instruction produces a 64-bit result, its registers cannot be used
630// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000631// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000632// the instruction that uses the new-value register must also be conditional,
633// and both must always have their predicates evaluate identically.
634// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000635// 5. There is an implied restriction that a packet cannot have another store,
636// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000637// already a store in a packet, there can not be a new value store.
638// Arch Spec: 3.4.4.2
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000639bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
640 const MachineInstr &PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000641 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000642 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000643 return false;
644
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000645 // Make sure there is dependency and can be new value'd.
646 const MachineOperand &Val = getStoreValueOperand(MI);
647 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000648 return false;
649
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000650 const MCInstrDesc& MCID = PacketMI.getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000651
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000652 // First operand is always the result.
653 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
654 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
655 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000656 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000657
658 // New-value stores are of class NV (slot 0), dual stores require class ST
659 // in slot 0 (PRM 5.5).
660 for (auto I : CurrentPacketMIs) {
661 SUnit *PacketSU = MIToSUnit.find(I)->second;
662 if (PacketSU->getInstr()->mayStore())
663 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000664 }
665
666 // Make sure it's NOT the post increment register that we are going to
667 // new value.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000668 if (HII->isPostIncrement(MI) &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000669 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000670 return false;
671 }
672
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000673 if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000674 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
675 // If source is post_inc, or absolute-set addressing, it can not feed
676 // into new value store
677 // r3 = memw(r2++#4)
678 // memw(r30 + #-1404) = r2.new -> can not be new value store
679 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000680 return false;
681 }
682
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000683 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
684 return false;
685
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000686 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000687 // also be predicated.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000688 if (HII->isPredicated(PacketMI)) {
689 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000690 return false;
691
692 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000693 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000694 unsigned predRegNumSrc = 0;
695 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000696 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000697
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000698 // Get predicate register used in the source instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000699 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000700 if (!MO.isReg())
701 continue;
702 predRegNumSrc = MO.getReg();
703 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
704 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000705 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000706 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000707 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
708 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000709
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000710 // Get predicate register used in new-value store instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000711 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000712 if (!MO.isReg())
713 continue;
714 predRegNumDst = MO.getReg();
715 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
716 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000717 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000718 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000719 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
720 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000721
722 // New-value register producer and user (store) need to satisfy these
723 // constraints:
724 // 1) Both instructions should be predicated on the same register.
725 // 2) If producer of the new-value register is .new predicated then store
726 // should also be .new predicated and if producer is not .new predicated
727 // then store should not be .new predicated.
728 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000729 // sense, i.e, either both should be negated or both should be non-negated.
730 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000731 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000732 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000733 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000734 }
735
736 // Make sure that other than the new-value register no other store instruction
737 // register has been modified in the same packet. Predicate registers can be
738 // modified by they should not be modified between the producer and the store
739 // instruction as it will make them both conditional on different values.
740 // We already know this to be true for all the instructions before and
741 // including PacketMI. Howerver, we need to perform the check for the
742 // remaining instructions in the packet.
743
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000744 unsigned StartCheck = 0;
745
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000746 for (auto I : CurrentPacketMIs) {
747 SUnit *TempSU = MIToSUnit.find(I)->second;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000748 MachineInstr &TempMI = *TempSU->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000749
750 // Following condition is true for all the instructions until PacketMI is
751 // reached (StartCheck is set to 0 before the for loop).
752 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000753 if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
754 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000755
756 StartCheck = 1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000757 if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000758 continue;
759
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000760 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000761 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000762 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000763 }
764
Alp Tokerf907b892013-12-05 05:44:44 +0000765 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000766 // 1. The only use of reg is DepReg and no other registers.
767 // This handles V4 base+index registers.
768 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000769 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000770 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000771 if (!HII->isPostIncrement(MI)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000772 for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
773 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000774 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000775 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000776 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000777 }
778
779 // If data definition is because of implicit definition of the register,
780 // do not newify the store. Eg.
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000781 // %r9 = ZXTH %r12, implicit %d6, implicit-def %r12
782 // S2_storerh_io %r8, 2, killed %r12; mem:ST2[%scevgep343]
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000783 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000784 if (MO.isRegMask() && MO.clobbersPhysReg(DepReg))
785 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000786 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
787 continue;
788 unsigned R = MO.getReg();
789 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
790 return false;
791 }
792
793 // Handle imp-use of super reg case. There is a target independent side
794 // change that should prevent this situation but I am handling it for
795 // just-in-case. For example, we cannot newify R2 in the following case:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000796 // %r3 = A2_tfrsi 0;
797 // S2_storeri_io killed %r0, 0, killed %r2, implicit killed %d1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000798 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000799 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
800 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000801 }
802
803 // Can be dot new store.
804 return true;
805}
806
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000807// Can this MI to promoted to either new value store or new value jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000808bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000809 const SUnit *PacketSU, unsigned DepReg,
810 MachineBasicBlock::iterator &MII) {
811 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000812 return false;
813
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000814 // Check to see the store can be new value'ed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000815 MachineInstr &PacketMI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000816 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000817 return true;
818
819 // Check to see the compare/jump can be new value'ed.
820 // This is done as a pass on its own. Don't need to check it here.
821 return false;
822}
823
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000824static bool isImplicitDependency(const MachineInstr &I, bool CheckDef,
825 unsigned DepReg) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000826 for (auto &MO : I.operands()) {
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000827 if (CheckDef && MO.isRegMask() && MO.clobbersPhysReg(DepReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000828 return true;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000829 if (!MO.isReg() || MO.getReg() != DepReg || !MO.isImplicit())
830 continue;
831 if (CheckDef == MO.isDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000832 return true;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000833 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000834 return false;
835}
836
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000837// Check to see if an instruction can be dot new
838// There are three kinds.
839// 1. dot new on predicate - V2/V3/V4
840// 2. dot new on stores NV/ST - V4
841// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000842bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000843 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
844 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000845 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000846 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000847 return false;
848
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000849 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000850 return false;
851
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000852 const MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000853
854 // The "new value" cannot come from inline asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000855 if (PI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000856 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000857
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000858 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
859 // sense.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000860 if (PI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000861 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000862
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000863 // If dependency is trough an implicitly defined register, we should not
864 // newify the use.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000865 if (isImplicitDependency(PI, true, DepReg) ||
866 isImplicitDependency(MI, false, DepReg))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000867 return false;
868
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000869 const MCInstrDesc& MCID = PI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000870 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000871 if (DisableVecDblNVStores && VecRC == &Hexagon::HvxWRRegClass)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000872 return false;
873
874 // predicate .new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000875 if (RC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000876 return HII->predCanBeUsedAsDotNew(PI, DepReg);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000877
878 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
879 return false;
880
881 // Create a dot new machine instruction to see if resources can be
882 // allocated. If not, bail out now.
883 int NewOpcode = HII->getDotNewOp(MI);
884 const MCInstrDesc &D = HII->get(NewOpcode);
885 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000886 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000887 MF.DeleteMachineInstr(NewMI);
888 if (!ResourcesAvailable)
889 return false;
890
891 // New Value Store only. New Value Jump generated as a separate pass.
892 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
893 return false;
894
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000895 return true;
896}
897
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000898// Go through the packet instructions and search for an anti dependency between
899// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000900// Trying to add
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000901// a) %r1 = TFRI_cdNotPt %p3, 2
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000902// to this packet:
903// {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000904// b) %p0 = C2_or killed %p3, killed %p0
905// c) %p3 = C2_tfrrp %r23
906// d) %r1 = C2_cmovenewit %p3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000907// }
908// The P3 from a) and d) will be complements after
909// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000910// Anti-dep between c) and b) is irrelevant for this case
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000911bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000912 unsigned DepReg) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000913 SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000914
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000915 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000916 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000917 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000918 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000919
920 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000921 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000922
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000923 // Look at dependencies between current members of the packet and
924 // predicate defining instruction MI. Make sure that dependency is
925 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000926 if (PacketSU->isSucc(PacketSUDep)) {
927 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000928 auto &Dep = PacketSU->Succs[i];
929 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
930 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000931 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000932 }
933 }
934 }
935
936 return false;
937}
938
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000939/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000940static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000941 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000942 /// We use the following rule: The first predicate register that is a use is
943 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000944 assert(QII->isPredicated(MI) && "Must be predicated instruction");
945
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000946 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000947 if (Op.isReg() && Op.getReg() && Op.isUse() &&
948 Hexagon::PredRegsRegClass.contains(Op.getReg()))
949 return Op.getReg();
950 }
951
952 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000953 return 0;
954}
955
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000956// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000957// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000958bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
959 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000960 // If we don't know the predicate sense of the instructions bail out early, we
961 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000962 if (getPredicateSense(MI1, HII) == PK_Unknown ||
963 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000964 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000965
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000966 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000967 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000968
969 // One corner case deals with the following scenario:
970 // Trying to add
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000971 // a) %r24 = A2_tfrt %p0, %r25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000972 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000973 // {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000974 // b) %r25 = A2_tfrf %p0, %r24
975 // c) %p0 = C2_cmpeqi %r26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000976 // }
977 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000978 // On general check a) and b) are complements, but presence of c) will
979 // convert a) to .new form, and then it is not a complement.
980 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000981
982 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000983 // Look for Anti dependecy on the same predicate reg as used in the
984 // candidate.
985 for (auto I : CurrentPacketMIs) {
986 // Scheduling Unit for current insn in the packet.
987 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000988
989 // If this instruction in the packet is succeeded by the candidate...
990 if (PacketSU->isSucc(SU)) {
991 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000992 auto Dep = PacketSU->Succs[i];
993 // The corner case exist when there is true data dependency between
994 // candidate and one of current packet members, this dep is on
995 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000996 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000997 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
998 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
999 // Here I know that I is predicate setting instruction with true
1000 // data dep to candidate on the register we care about - c) in the
1001 // above example. Now I need to see if there is an anti dependency
1002 // from c) to any other instruction in the same packet on the pred
1003 // reg of interest.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001004 if (restrictingDepExistInPacket(*I, Dep.getReg()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001005 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001006 }
1007 }
1008 }
1009 }
1010
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001011 // If the above case does not apply, check regular complement condition.
1012 // Check that the predicate register is the same and that the predicate
1013 // sense is different We also need to differentiate .old vs. .new: !p0
1014 // is not complementary to p0.new.
1015 unsigned PReg1 = getPredicatedRegister(MI1, HII);
1016 unsigned PReg2 = getPredicatedRegister(MI2, HII);
1017 return PReg1 == PReg2 &&
1018 Hexagon::PredRegsRegClass.contains(PReg1) &&
1019 Hexagon::PredRegsRegClass.contains(PReg2) &&
1020 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001021 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001022}
1023
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001024// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001025void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001026 Dependence = false;
1027 PromotedToDotNew = false;
1028 GlueToNewValueJump = false;
1029 GlueAllocframeStore = false;
1030 FoundSequentialDependence = false;
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00001031 ChangedOffset = INT64_MAX;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001032}
1033
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001034// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001035bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
1036 const MachineBasicBlock *) {
1037 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001038 return true;
1039
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001040 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +00001041 return false;
1042
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001043 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001044 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001045 return false;
1046
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001047 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001048 return false;
1049
1050 // We check if MI has any functional units mapped to it. If it doesn't,
1051 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001052 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001053 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +00001054 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001055 return !FuncUnits;
1056}
1057
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001058bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001059 // Ensure any bundles created by gather packetize remain seperate.
1060 if (MI.isBundle())
1061 return true;
1062
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001063 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001064 return true;
1065
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001066 // Consider inline asm to not be a solo instruction by default.
1067 // Inline asm will be put in a packet temporarily, but then it will be
1068 // removed, and placed outside of the packet (before or after, depending
1069 // on dependencies). This is to reduce the impact of inline asm as a
1070 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001071 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001072 return true;
1073
1074 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
1075 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
1076 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001077 if (isSchedBarrier(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001078 return true;
1079
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001080 if (HII->isSolo(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001081 return true;
1082
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001083 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001084 return true;
1085
1086 return false;
1087}
1088
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001089// Quick check if instructions MI and MJ cannot coexist in the same packet.
1090// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1091// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1092// For full test call this function twice:
1093// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1094// Doing the test only one way saves the amount of code in this function,
1095// since every test would need to be repeated with the MI and MJ reversed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001096static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001097 const HexagonInstrInfo &HII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001098 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001099 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1100 HII.isHVXMemWithAIndirect(MI, MJ))
1101 return true;
1102
1103 // An inline asm cannot be together with a branch, because we may not be
1104 // able to remove the asm out after packetizing (i.e. if the asm must be
1105 // moved past the bundle). Similarly, two asms cannot be together to avoid
1106 // complications when determining their relative order outside of a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001107 if (MI.isInlineAsm())
1108 return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
1109 MJ.isCall() || MJ.isTerminator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001110
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001111 switch (MI.getOpcode()) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001112 case Hexagon::S2_storew_locked:
1113 case Hexagon::S4_stored_locked:
1114 case Hexagon::L2_loadw_locked:
1115 case Hexagon::L4_loadd_locked:
1116 case Hexagon::Y4_l2fetch:
1117 case Hexagon::Y5_l2fetch: {
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001118 // These instructions can only be grouped with ALU32 or non-floating-point
1119 // XTYPE instructions. Since there is no convenient way of identifying fp
1120 // XTYPE instructions, only allow grouping with ALU32 for now.
1121 unsigned TJ = HII.getType(MJ);
Krzysztof Parzyszek5ea971c2017-02-07 17:47:37 +00001122 if (TJ != HexagonII::TypeALU32_2op &&
1123 TJ != HexagonII::TypeALU32_3op &&
1124 TJ != HexagonII::TypeALU32_ADDI)
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001125 return true;
1126 break;
1127 }
1128 default:
1129 break;
1130 }
1131
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001132 // "False" really means that the quick check failed to determine if
1133 // I and J cannot coexist.
1134 return false;
1135}
1136
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001137// Full, symmetric check.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001138bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
1139 const MachineInstr &MJ) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001140 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1141}
1142
1143void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1144 for (auto &B : MF) {
1145 MachineBasicBlock::iterator BundleIt;
1146 MachineBasicBlock::instr_iterator NextI;
1147 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1148 NextI = std::next(I);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001149 MachineInstr &MI = *I;
1150 if (MI.isBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001151 BundleIt = I;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001152 if (!MI.isInsideBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001153 continue;
1154
1155 // Decide on where to insert the instruction that we are pulling out.
1156 // Debug instructions always go before the bundle, but the placement of
1157 // INLINE_ASM depends on potential dependencies. By default, try to
1158 // put it before the bundle, but if the asm writes to a register that
1159 // other instructions in the bundle read, then we need to place it
1160 // after the bundle (to preserve the bundle semantics).
1161 bool InsertBeforeBundle;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001162 if (MI.isInlineAsm())
1163 InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
1164 else if (MI.isDebugValue())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001165 InsertBeforeBundle = true;
1166 else
1167 continue;
1168
1169 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1170 }
1171 }
1172}
1173
1174// Check if a given instruction is of class "system".
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001175static bool isSystemInstr(const MachineInstr &MI) {
1176 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001177 switch (Opc) {
1178 case Hexagon::Y2_barrier:
1179 case Hexagon::Y2_dcfetchbo:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001180 case Hexagon::Y4_l2fetch:
1181 case Hexagon::Y5_l2fetch:
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001182 return true;
1183 }
1184 return false;
1185}
1186
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001187bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
1188 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001189 // The dependence graph may not include edges between dead definitions,
1190 // so without extra checks, we could end up packetizing two instruction
1191 // defining the same (dead) register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001192 if (I.isCall() || J.isCall())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001193 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001194 if (HII->isPredicated(I) || HII->isPredicated(J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001195 return false;
1196
1197 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001198 for (auto &MO : I.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001199 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1200 continue;
1201 DeadDefs[MO.getReg()] = true;
1202 }
1203
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001204 for (auto &MO : J.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001205 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1206 continue;
1207 unsigned R = MO.getReg();
1208 if (R != Hexagon::USR_OVF && DeadDefs[R])
1209 return true;
1210 }
1211 return false;
1212}
1213
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001214bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
1215 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001216 // A save callee-save register function call can only be in a packet
1217 // with instructions that don't write to the callee-save registers.
1218 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1219 doesModifyCalleeSavedReg(J, HRI)) ||
1220 (HII->isSaveCalleeSavedRegsCall(J) &&
1221 doesModifyCalleeSavedReg(I, HRI)))
1222 return true;
1223
1224 // Two control flow instructions cannot go in the same packet.
1225 if (isControlFlow(I) && isControlFlow(J))
1226 return true;
1227
1228 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1229 // contain a speculative indirect jump,
1230 // a new-value compare jump or a dealloc_return.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001231 auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
1232 if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001233 return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001234 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001235 return true;
1236 return false;
1237 };
1238
1239 if (HII->isLoopN(I) && isBadForLoopN(J))
1240 return true;
1241 if (HII->isLoopN(J) && isBadForLoopN(I))
1242 return true;
1243
1244 // dealloc_return cannot appear in the same packet as a conditional or
1245 // unconditional jump.
1246 return HII->isDeallocRet(I) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001247 (J.isBranch() || J.isCall() || J.isBarrier());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001248}
1249
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001250bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I,
1251 const MachineInstr &J) {
1252 // Adding I to a packet that has J.
1253
1254 // Regmasks are not reflected in the scheduling dependency graph, so
1255 // we need to check them manually. This code assumes that regmasks only
1256 // occur on calls, and the problematic case is when we add an instruction
1257 // defining a register R to a packet that has a call that clobbers R via
1258 // a regmask. Those cannot be packetized together, because the call will
1259 // be executed last. That's also a reson why it is ok to add a call
1260 // clobbering R to a packet that defines R.
1261
1262 // Look for regmasks in J.
1263 for (const MachineOperand &OpJ : J.operands()) {
1264 if (!OpJ.isRegMask())
1265 continue;
1266 assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call");
1267 for (const MachineOperand &OpI : I.operands()) {
1268 if (OpI.isReg()) {
1269 if (OpJ.clobbersPhysReg(OpI.getReg()))
1270 return true;
1271 } else if (OpI.isRegMask()) {
1272 // Both are regmasks. Assume that they intersect.
1273 return true;
1274 }
1275 }
1276 }
1277 return false;
1278}
1279
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001280bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I,
1281 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001282 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001283 bool StoreI = I.mayStore(), StoreJ = J.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001284 if ((SysI && StoreJ) || (SysJ && StoreI))
1285 return true;
1286
1287 if (StoreI && StoreJ) {
1288 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1289 return true;
1290 } else {
1291 // A memop cannot be in the same packet with another memop or a store.
1292 // Two stores can be together, but here I and J cannot both be stores.
1293 bool MopStI = HII->isMemOp(I) || StoreI;
1294 bool MopStJ = HII->isMemOp(J) || StoreJ;
1295 if (MopStI && MopStJ)
1296 return true;
1297 }
1298
1299 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1300}
1301
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001302// SUI is the current instruction that is out side of the current packet.
1303// SUJ is the current instruction inside the current packet against which that
1304// SUI will be packetized.
1305bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001306 assert(SUI->getInstr() && SUJ->getInstr());
1307 MachineInstr &I = *SUI->getInstr();
1308 MachineInstr &J = *SUJ->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001309
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001310 // Clear IgnoreDepMIs when Packet starts.
1311 if (CurrentPacketMIs.size() == 1)
1312 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001313
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001314 MachineBasicBlock::iterator II = I.getIterator();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001315
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001316 // Solo instructions cannot go in the packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001317 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001318
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001319 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001320 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001321
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001322 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1323 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001324 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001325
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001326 // Regmasks are not accounted for in the scheduling graph, so we need
1327 // to explicitly check for dependencies caused by them. They should only
1328 // appear on calls, so it's not too pessimistic to reject all regmask
1329 // dependencies.
1330 Dependence = hasRegMaskDependence(I, J);
1331 if (Dependence)
1332 return false;
1333
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001334 // V4 allows dual stores. It does not allow second store, if the first
1335 // store is not in SLOT0. New value store, new value jump, dealloc_return
1336 // and memop always take SLOT0. Arch spec 3.4.4.2.
1337 Dependence = hasV4SpecificDependence(I, J);
1338 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001339 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001340
1341 // If an instruction feeds new value jump, glue it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001342 MachineBasicBlock::iterator NextMII = I.getIterator();
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001343 ++NextMII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001344 if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001345 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001346
1347 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001348 const MachineOperand &NOp0 = NextMI.getOperand(0);
1349 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001350
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001351 if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001352 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001353
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001354 for (MachineInstr *PI : CurrentPacketMIs) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001355 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001356 if (PI->isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001357 Dependence = true;
1358 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001359 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001360 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001361 // 1. Packet does not have a store in it.
1362 // 2. If the first operand of the nvj is newified, and the second
1363 // operand is also a reg, it (second reg) is not defined in
1364 // the same packet.
1365 // 3. If the second operand of the nvj is newified, (which means
1366 // first operand is also a reg), first reg is not defined in
1367 // the same packet.
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001368 if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() ||
1369 HII->isLoopN(*PI)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001370 Dependence = true;
1371 break;
1372 }
1373 // Check #2/#3.
1374 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001375 if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001376 Dependence = true;
1377 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001378 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001379 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001380
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001381 GlueToNewValueJump = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001382 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001383 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001384 }
1385
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001386 // There no dependency between a prolog instruction and its successor.
1387 if (!SUJ->isSucc(SUI))
1388 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001389
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001390 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1391 if (FoundSequentialDependence)
1392 break;
1393
1394 if (SUJ->Succs[i].getSUnit() != SUI)
1395 continue;
1396
1397 SDep::Kind DepType = SUJ->Succs[i].getKind();
1398 // For direct calls:
1399 // Ignore register dependences for call instructions for packetization
1400 // purposes except for those due to r31 and predicate registers.
1401 //
1402 // For indirect calls:
1403 // Same as direct calls + check for true dependences to the register
1404 // used in the indirect call.
1405 //
1406 // We completely ignore Order dependences for call instructions.
1407 //
1408 // For returns:
1409 // Ignore register dependences for return instructions like jumpr,
1410 // dealloc return unless we have dependencies on the explicit uses
1411 // of the registers used by jumpr (like r31) or dealloc return
1412 // (like r29 or r30).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001413 unsigned DepReg = 0;
1414 const TargetRegisterClass *RC = nullptr;
1415 if (DepType == SDep::Data) {
1416 DepReg = SUJ->Succs[i].getReg();
1417 RC = HRI->getMinimalPhysRegClass(DepReg);
1418 }
1419
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +00001420 if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001421 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001422 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001423 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1424 continue;
1425 }
1426
1427 if (DepType == SDep::Data) {
1428 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1429 if (promoteToDotCur(J, DepType, II, RC))
1430 continue;
1431 }
1432
1433 // Data dpendence ok if we have load.cur.
1434 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001435 if (HII->isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001436 continue;
1437 }
1438
1439 // For instructions that can be promoted to dot-new, try to promote.
1440 if (DepType == SDep::Data) {
1441 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1442 if (promoteToDotNew(I, DepType, II, RC)) {
1443 PromotedToDotNew = true;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001444 if (cannotCoexist(I, J))
1445 FoundSequentialDependence = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001446 continue;
1447 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001448 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001449 if (HII->isNewValueJump(I))
1450 continue;
1451 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001452
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001453 // For predicated instructions, if the predicates are complements then
1454 // there can be no dependence.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001455 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1456 arePredicatesComplements(I, J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001457 // Not always safe to do this translation.
1458 // DAG Builder attempts to reduce dependence edges using transitive
1459 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001460 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001461 // r0 = tfr_pt ... (1)
1462 // r0 = tfr_pf ... (2)
1463 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001464 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001465 // There will be an output dependence between (1)->(2) and (2)->(3).
1466 // However, there is no dependence edge between (1)->(3). This results
1467 // in all 3 instructions going in the same packet. We ignore dependce
1468 // only once to avoid this situation.
David Majnemer0d955d02016-08-11 22:21:41 +00001469 auto Itr = find(IgnoreDepMIs, &J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001470 if (Itr != IgnoreDepMIs.end()) {
1471 Dependence = true;
1472 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001473 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001474 IgnoreDepMIs.push_back(&I);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001475 continue;
1476 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001477
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001478 // Ignore Order dependences between unconditional direct branches
1479 // and non-control-flow instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001480 if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001481 DepType == SDep::Order)
1482 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001483
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001484 // Ignore all dependences for jumps except for true and output
1485 // dependences.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001486 if (I.isConditionalBranch() && DepType != SDep::Data &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001487 DepType != SDep::Output)
1488 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001489
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001490 if (DepType == SDep::Output) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001491 FoundSequentialDependence = true;
1492 break;
1493 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001494
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001495 // For Order dependences:
1496 // 1. On V4 or later, volatile loads/stores can be packetized together,
1497 // unless other rules prevent is.
1498 // 2. Store followed by a load is not allowed.
1499 // 3. Store followed by a store is only valid on V4 or later.
1500 // 4. Load followed by any memory operation is allowed.
1501 if (DepType == SDep::Order) {
1502 if (!PacketizeVolatiles) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001503 bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001504 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001505 FoundSequentialDependence = true;
1506 break;
1507 }
1508 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001509 // J is first, I is second.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001510 bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
1511 bool LoadI = I.mayLoad(), StoreI = I.mayStore();
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001512 bool NVStoreJ = HII->isNewValueStore(J);
1513 bool NVStoreI = HII->isNewValueStore(I);
1514 bool IsVecJ = HII->isHVXVec(J);
1515 bool IsVecI = HII->isHVXVec(I);
1516
1517 if (Slot1Store && MF.getSubtarget<HexagonSubtarget>().hasV65TOps() &&
1518 ((LoadJ && StoreI && !NVStoreI) ||
1519 (StoreJ && LoadI && !NVStoreJ)) &&
1520 (J.getOpcode() != Hexagon::S2_allocframe &&
1521 I.getOpcode() != Hexagon::S2_allocframe) &&
1522 (J.getOpcode() != Hexagon::L2_deallocframe &&
1523 I.getOpcode() != Hexagon::L2_deallocframe) &&
1524 (!HII->isMemOp(J) && !HII->isMemOp(I)) && (!IsVecJ && !IsVecI))
1525 setmemShufDisabled(true);
1526 else
1527 if (StoreJ && LoadI && alias(J, I)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001528 FoundSequentialDependence = true;
1529 break;
1530 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001531
1532 if (!StoreJ)
1533 if (!LoadJ || (!LoadI && !StoreI)) {
1534 // If J is neither load nor store, assume a dependency.
1535 // If J is a load, but I is neither, also assume a dependency.
1536 FoundSequentialDependence = true;
1537 break;
1538 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001539 // Store followed by store: not OK on V2.
1540 // Store followed by load: not OK on all.
1541 // Load followed by store: OK on all.
1542 // Load followed by load: OK on all.
1543 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001544 }
1545
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001546 // For V4, special case ALLOCFRAME. Even though there is dependency
1547 // between ALLOCFRAME and subsequent store, allow it to be packetized
1548 // in a same packet. This implies that the store is using the caller's
1549 // SP. Hence, offset needs to be updated accordingly.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001550 if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
1551 unsigned Opc = I.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001552 switch (Opc) {
1553 case Hexagon::S2_storerd_io:
1554 case Hexagon::S2_storeri_io:
1555 case Hexagon::S2_storerh_io:
1556 case Hexagon::S2_storerb_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001557 if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001558 // Since this store is to be glued with allocframe in the same
1559 // packet, it will use SP of the previous stack frame, i.e.
1560 // caller's SP. Therefore, we need to recalculate offset
1561 // according to this change.
1562 GlueAllocframeStore = useCallersSP(I);
1563 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001564 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001565 }
1566 default:
1567 break;
1568 }
1569 }
1570
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001571 // There are certain anti-dependencies that cannot be ignored.
1572 // Specifically:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001573 // J2_call ... implicit-def %r0 ; SUJ
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001574 // R0 = ... ; SUI
1575 // Those cannot be packetized together, since the call will observe
1576 // the effect of the assignment to R0.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001577 if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) {
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001578 // Check if I defines any volatile register. We should also check
1579 // registers that the call may read, but these happen to be a
1580 // subset of the volatile register set.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001581 for (const MachineOperand &Op : I.operands()) {
1582 if (Op.isReg() && Op.isDef()) {
1583 unsigned R = Op.getReg();
1584 if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI))
1585 continue;
1586 } else if (!Op.isRegMask()) {
1587 // If I has a regmask assume dependency.
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001588 continue;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001589 }
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001590 FoundSequentialDependence = true;
1591 break;
1592 }
1593 }
1594
1595 // Skip over remaining anti-dependences. Two instructions that are
1596 // anti-dependent can share a packet, since in most such cases all
1597 // operands are read before any modifications take place.
1598 // The exceptions are branch and call instructions, since they are
1599 // executed after all other instructions have completed (at least
1600 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001601 if (DepType != SDep::Anti) {
1602 FoundSequentialDependence = true;
1603 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001604 }
1605 }
1606
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001607 if (FoundSequentialDependence) {
1608 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001609 return false;
1610 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001611
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001612 return true;
1613}
1614
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001615bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001616 assert(SUI->getInstr() && SUJ->getInstr());
1617 MachineInstr &I = *SUI->getInstr();
1618 MachineInstr &J = *SUJ->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001619
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001620 bool Coexist = !cannotCoexist(I, J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001621
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001622 if (Coexist && !Dependence)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001623 return true;
1624
1625 // Check if the instruction was promoted to a dot-new. If so, demote it
1626 // back into a dot-old.
1627 if (PromotedToDotNew)
1628 demoteToDotOld(I);
1629
1630 cleanUpDotCur();
1631 // Check if the instruction (must be a store) was glued with an allocframe
1632 // instruction. If so, restore its offset to its original value, i.e. use
1633 // current SP instead of caller's SP.
1634 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001635 useCalleesSP(I);
1636 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001637 }
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00001638
1639 if (ChangedOffset != INT64_MAX)
1640 undoChangedOffset(I);
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001641
1642 if (GlueToNewValueJump) {
1643 // Putting I and J together would prevent the new-value jump from being
1644 // packetized with the producer. In that case I and J must be separated.
1645 GlueToNewValueJump = false;
1646 return false;
1647 }
1648
1649 if (ChangedOffset == INT64_MAX && updateOffset(SUI, SUJ)) {
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00001650 FoundSequentialDependence = false;
1651 Dependence = false;
1652 return true;
1653 }
1654
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001655 return false;
1656}
1657
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001658
1659bool HexagonPacketizerList::foundLSInPacket() {
1660 bool FoundLoad = false;
1661 bool FoundStore = false;
1662
1663 for (auto MJ : CurrentPacketMIs) {
1664 unsigned Opc = MJ->getOpcode();
1665 if (Opc == Hexagon::S2_allocframe || Opc == Hexagon::L2_deallocframe)
1666 continue;
1667 if (HII->isMemOp(*MJ))
1668 continue;
1669 if (MJ->mayLoad())
1670 FoundLoad = true;
1671 if (MJ->mayStore() && !HII->isNewValueStore(*MJ))
1672 FoundStore = true;
1673 }
1674 return FoundLoad && FoundStore;
1675}
1676
1677
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001678MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001679HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001680 MachineBasicBlock::iterator MII = MI.getIterator();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001681 MachineBasicBlock *MBB = MI.getParent();
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001682
Eugene Zelenko3b873362017-09-28 22:27:31 +00001683 if (CurrentPacketMIs.empty())
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001684 PacketStalls = false;
1685 PacketStalls |= producesStall(MI);
1686
1687 if (MI.isImplicitDef())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001688 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001689 assert(ResourceTracker->canReserveResources(MI));
1690
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001691 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001692 bool Good = true;
1693
1694 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001695 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001696 // We need to put both instructions in the same packet: MI and NvjMI.
1697 // Either of them can require a constant extender. Try to add both to
1698 // the current packet, and if that fails, end the packet and start a
1699 // new one.
1700 ResourceTracker->reserveResources(MI);
1701 if (ExtMI)
1702 Good = tryAllocateResourcesForConstExt(true);
1703
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001704 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001705 if (Good) {
1706 if (ResourceTracker->canReserveResources(NvjMI))
1707 ResourceTracker->reserveResources(NvjMI);
1708 else
1709 Good = false;
1710 }
1711 if (Good && ExtNvjMI)
1712 Good = tryAllocateResourcesForConstExt(true);
1713
1714 if (!Good) {
1715 endPacket(MBB, MI);
1716 assert(ResourceTracker->canReserveResources(MI));
1717 ResourceTracker->reserveResources(MI);
1718 if (ExtMI) {
1719 assert(canReserveResourcesForConstExt());
1720 tryAllocateResourcesForConstExt(true);
1721 }
1722 assert(ResourceTracker->canReserveResources(NvjMI));
1723 ResourceTracker->reserveResources(NvjMI);
1724 if (ExtNvjMI) {
1725 assert(canReserveResourcesForConstExt());
1726 reserveResourcesForConstExt();
1727 }
1728 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001729 CurrentPacketMIs.push_back(&MI);
1730 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001731 return MII;
1732 }
1733
1734 ResourceTracker->reserveResources(MI);
1735 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1736 endPacket(MBB, MI);
1737 if (PromotedToDotNew)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001738 demoteToDotOld(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001739 if (GlueAllocframeStore) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001740 useCalleesSP(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001741 GlueAllocframeStore = false;
1742 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001743 ResourceTracker->reserveResources(MI);
1744 reserveResourcesForConstExt();
1745 }
1746
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001747 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001748 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001749}
1750
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001751void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001752 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001753 // Replace VLIWPacketizerList::endPacket(MBB, MI).
1754
1755 bool memShufDisabled = getmemShufDisabled();
1756 if (memShufDisabled && !foundLSInPacket()) {
1757 setmemShufDisabled(false);
1758 DEBUG(dbgs() << " Not added to NoShufPacket\n");
1759 }
1760 memShufDisabled = getmemShufDisabled();
1761
1762 if (CurrentPacketMIs.size() > 1) {
1763 MachineBasicBlock::instr_iterator FirstMI(CurrentPacketMIs.front());
1764 MachineBasicBlock::instr_iterator LastMI(MI.getInstrIterator());
1765 finalizeBundle(*MBB, FirstMI, LastMI);
1766
1767 auto BundleMII = std::prev(FirstMI);
1768 if (memShufDisabled)
1769 HII->setBundleNoShuf(BundleMII);
1770
1771 setmemShufDisabled(false);
1772 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001773 OldPacketMIs = CurrentPacketMIs;
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001774 CurrentPacketMIs.clear();
1775
1776 ResourceTracker->clearResources();
1777 DEBUG(dbgs() << "End packet\n");
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001778}
1779
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001780bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001781 return !producesStall(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001782}
1783
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001784// V60 forward scheduling.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001785bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001786 // If the packet already stalls, then ignore the stall from a subsequent
1787 // instruction in the same packet.
1788 if (PacketStalls)
1789 return false;
1790
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001791 // Check whether the previous packet is in a different loop. If this is the
1792 // case, there is little point in trying to avoid a stall because that would
1793 // favor the rare case (loop entry) over the common case (loop iteration).
1794 //
1795 // TODO: We should really be able to check all the incoming edges if this is
1796 // the first packet in a basic block, so we can avoid stalls from the loop
1797 // backedge.
1798 if (!OldPacketMIs.empty()) {
1799 auto *OldBB = OldPacketMIs.front()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001800 auto *ThisBB = I.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001801 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1802 return false;
1803 }
1804
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001805 SUnit *SUI = MIToSUnit[const_cast<MachineInstr *>(&I)];
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001806
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001807 // Check if the latency is 0 between this instruction and any instruction
1808 // in the current packet. If so, we disregard any potential stalls due to
1809 // the instructions in the previous packet. Most of the instruction pairs
1810 // that can go together in the same packet have 0 latency between them.
1811 // Only exceptions are newValueJumps as they're generated much later and
1812 // the latencies can't be changed at that point. Another is .cur
1813 // instructions if its consumer has a 0 latency successor (such as .new).
1814 // In this case, the latency between .cur and the consumer stays non-zero
1815 // even though we can have both .cur and .new in the same packet. Changing
1816 // the latency to 0 is not an option as it causes software pipeliner to
1817 // not pipeline in some cases.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001818
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001819 // For Example:
1820 // {
1821 // I1: v6.cur = vmem(r0++#1)
1822 // I2: v7 = valign(v6,v4,r2)
1823 // I3: vmem(r5++#1) = v7.new
1824 // }
1825 // Here I2 and I3 has 0 cycle latency, but I1 and I2 has 2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001826
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001827 for (auto J : CurrentPacketMIs) {
1828 SUnit *SUJ = MIToSUnit[J];
1829 for (auto &Pred : SUI->Preds)
1830 if (Pred.getSUnit() == SUJ &&
1831 (Pred.getLatency() == 0 || HII->isNewValueJump(I) ||
1832 HII->isToBeScheduledASAP(*J, I)))
1833 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001834 }
1835
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001836 // Check if the latency is greater than one between this instruction and any
1837 // instruction in the previous packet.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001838 for (auto J : OldPacketMIs) {
1839 SUnit *SUJ = MIToSUnit[J];
1840 for (auto &Pred : SUI->Preds)
1841 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1842 return true;
1843 }
1844
1845 // Check if the latency is greater than one between this instruction and any
1846 // instruction in the previous packet.
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001847 for (auto J : OldPacketMIs) {
1848 SUnit *SUJ = MIToSUnit[J];
1849 for (auto &Pred : SUI->Preds)
1850 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1851 return true;
1852 }
1853
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001854 return false;
1855}
1856
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001857//===----------------------------------------------------------------------===//
1858// Public Constructor Functions
1859//===----------------------------------------------------------------------===//
1860
1861FunctionPass *llvm::createHexagonPacketizer() {
1862 return new HexagonPacketizer();
1863}