blob: a896700df1b07110541f1ba3c44ae68938876111 [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
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +000080FunctionPass *createHexagonPacketizer(bool Minimal);
Eugene Zelenko3b873362017-09-28 22:27:31 +000081void 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 Parzyszek39a979c2018-08-17 14:24:24 +000091 HexagonPacketizer(bool Min = false)
92 : MachineFunctionPass(ID), Minimal(Min) {}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000093
Craig Topper906c2cd2014-04-29 07:58:16 +000094 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000095 AU.setPreservesCFG();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000096 AU.addRequired<AAResultsWrapperPass>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000097 AU.addRequired<MachineBranchProbabilityInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000098 AU.addRequired<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000099 AU.addRequired<MachineLoopInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000100 AU.addPreserved<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000101 AU.addPreserved<MachineLoopInfo>();
102 MachineFunctionPass::getAnalysisUsage(AU);
103 }
Eugene Zelenko3b873362017-09-28 22:27:31 +0000104
Mehdi Amini117296c2016-10-01 02:56:57 +0000105 StringRef getPassName() const override { return "Hexagon Packetizer"; }
Craig Topper906c2cd2014-04-29 07:58:16 +0000106 bool runOnMachineFunction(MachineFunction &Fn) override;
Eugene Zelenko3b873362017-09-28 22:27:31 +0000107
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000108 MachineFunctionProperties getRequiredProperties() const override {
109 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000110 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000111 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000112
113 private:
114 const HexagonInstrInfo *HII;
115 const HexagonRegisterInfo *HRI;
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +0000116 const bool Minimal;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000117 };
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000118
Eugene Zelenko3b873362017-09-28 22:27:31 +0000119} // end anonymous namespace
120
121char HexagonPacketizer::ID = 0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000122
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000123INITIALIZE_PASS_BEGIN(HexagonPacketizer, "hexagon-packetizer",
124 "Hexagon Packetizer", false, false)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000125INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
126INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
127INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000128INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000129INITIALIZE_PASS_END(HexagonPacketizer, "hexagon-packetizer",
130 "Hexagon Packetizer", false, false)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000131
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000132HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
133 MachineLoopInfo &MLI, AliasAnalysis *AA,
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +0000134 const MachineBranchProbabilityInfo *MBPI, bool Minimal)
135 : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI),
136 Minimal(Minimal) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000137 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
138 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000139
Eugene Zelenko3b873362017-09-28 22:27:31 +0000140 addMutation(llvm::make_unique<HexagonSubtarget::UsrOverflowMutation>());
141 addMutation(llvm::make_unique<HexagonSubtarget::HVXMemLatencyMutation>());
142 addMutation(llvm::make_unique<HexagonSubtarget::BankConflictMutation>());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000143}
144
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000145// Check if FirstI modifies a register that SecondI reads.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000146static bool hasWriteToReadDep(const MachineInstr &FirstI,
147 const MachineInstr &SecondI,
148 const TargetRegisterInfo *TRI) {
149 for (auto &MO : FirstI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000150 if (!MO.isReg() || !MO.isDef())
151 continue;
152 unsigned R = MO.getReg();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000153 if (SecondI.readsRegister(R, TRI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000154 return true;
155 }
156 return false;
157}
158
159
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000160static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000161 MachineBasicBlock::iterator BundleIt, bool Before) {
162 MachineBasicBlock::instr_iterator InsertPt;
163 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000164 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000165 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000166 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000167
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000168 MachineBasicBlock &B = *MI.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000169 // The instruction should at least be bundled with the preceding instruction
170 // (there will always be one, i.e. BUNDLE, if nothing else).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000171 assert(MI.isBundledWithPred());
172 if (MI.isBundledWithSucc()) {
173 MI.clearFlag(MachineInstr::BundledSucc);
174 MI.clearFlag(MachineInstr::BundledPred);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000175 } else {
176 // If it's not bundled with the successor (i.e. it is the last one
177 // in the bundle), then we can simply unbundle it from the predecessor,
178 // which will take care of updating the predecessor's flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000179 MI.unbundleFromPred();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000180 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000181 B.splice(InsertPt, &B, MI.getIterator());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000182
183 // Get the size of the bundle without asserting.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000184 MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000185 MachineBasicBlock::const_instr_iterator E = B.instr_end();
186 unsigned Size = 0;
187 for (++I; I != E && I->isBundledWithPred(); ++I)
188 ++Size;
189
190 // If there are still two or more instructions, then there is nothing
191 // else to be done.
192 if (Size > 1)
193 return BundleIt;
194
195 // Otherwise, extract the single instruction out and delete the bundle.
196 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000197 MachineInstr &SingleI = *BundleIt->getNextNode();
198 SingleI.unbundleFromPred();
199 assert(!SingleI.isBundledWithSucc());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000200 BundleIt->eraseFromParent();
201 return NextIt;
202}
203
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000204bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Krzysztof Parzyszek5d41cc12018-03-12 17:47:46 +0000205 auto &HST = MF.getSubtarget<HexagonSubtarget>();
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 Parzyszek39a979c2018-08-17 14:24:24 +0000216 bool MinOnly = Minimal || DisablePacketizer || !HST.usePackets() ||
217 skipFunction(MF.getFunction());
218 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI, MinOnly);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000219
220 // DFA state table should not be empty.
221 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
222
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000223 // Loop over all basic blocks and remove KILL pseudo-instructions
224 // These instructions confuse the dependence analysis. Consider:
225 // D0 = ... (Insn 0)
226 // R0 = KILL R0, D0 (Insn 1)
227 // R0 = ... (Insn 2)
228 // Here, Insn 1 will result in the dependence graph not emitting an output
229 // dependence between Insn 0 and Insn 2. This can lead to incorrect
230 // packetization
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +0000231 for (MachineBasicBlock &MB : MF) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000232 auto End = MB.end();
233 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000234 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000235 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000236 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000237 MB.erase(MI);
238 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000239 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000240 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000241 }
242 }
243
244 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000245 for (auto &MB : MF) {
246 auto Begin = MB.begin(), End = MB.end();
247 while (Begin != End) {
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000248 // Find the first non-boundary starting from the end of the last
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000249 // scheduling region.
250 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000251 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000252 ++RB;
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000253 // Find the first boundary starting from the beginning of the new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000254 // region.
255 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000256 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000257 ++RE;
258 // Add the scheduling boundary if it's not block end.
259 if (RE != End)
260 ++RE;
261 // If RB == End, then RE == End.
262 if (RB != End)
263 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000264
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000265 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000266 }
267 }
268
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000269 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000270 return true;
271}
272
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000273// Reserve resources for a constant extender. Trigger an assertion if the
274// reservation fails.
275void HexagonPacketizerList::reserveResourcesForConstExt() {
276 if (!tryAllocateResourcesForConstExt(true))
277 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000278}
279
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000280bool HexagonPacketizerList::canReserveResourcesForConstExt() {
281 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000282}
283
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000284// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
285// return true, otherwise, return false.
286bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
287 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000288 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000289 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000290 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000291 MF.DeleteMachineInstr(ExtMI);
292 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000293}
294
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000295bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000296 SDep::Kind DepType, unsigned DepReg) {
297 // Check for LR dependence.
298 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000299 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000300
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000301 if (HII->isDeallocRet(MI))
302 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000303 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000304
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000305 // Call-like instructions can be packetized with preceding instructions
306 // that define registers implicitly used or modified by the call. Explicit
307 // uses are still prohibited, as in the case of indirect calls:
308 // r0 = ...
309 // J2_jumpr r0
310 if (DepType == SDep::Data) {
311 for (const MachineOperand MO : MI.operands())
312 if (MO.isReg() && MO.getReg() == DepReg && !MO.isImplicit())
313 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000314 }
315
316 return false;
317}
318
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000319static bool isRegDependence(const SDep::Kind DepType) {
320 return DepType == SDep::Data || DepType == SDep::Anti ||
321 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000322}
323
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000324static bool isDirectJump(const MachineInstr &MI) {
325 return MI.getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000326}
327
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000328static bool isSchedBarrier(const MachineInstr &MI) {
329 switch (MI.getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000330 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000331 return true;
332 }
333 return false;
334}
335
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000336static bool isControlFlow(const MachineInstr &MI) {
337 return MI.getDesc().isTerminator() || MI.getDesc().isCall();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000338}
339
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000340/// Returns true if the instruction modifies a callee-saved register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000341static bool doesModifyCalleeSavedReg(const MachineInstr &MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000342 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000343 const MachineFunction &MF = *MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000344 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000345 if (MI.modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000346 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000347 return false;
348}
349
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000350// Returns true if an instruction can be promoted to .new predicate or
351// new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000352bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI,
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000353 const TargetRegisterClass *NewRC) {
354 // Vector stores can be predicated, and can be new-value stores, but
355 // they cannot be predicated on a .new predicate value.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000356 if (NewRC == &Hexagon::PredRegsRegClass) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000357 if (HII->isHVXVec(MI) && MI.mayStore())
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000358 return false;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000359 return HII->isPredicated(MI) && HII->getDotNewPredOp(MI, nullptr) > 0;
360 }
361 // If the class is not PredRegs, it could only apply to new-value stores.
362 return HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000363}
364
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000365// Promote an instructiont to its .cur form.
366// At this time, we have already made a call to canPromoteToDotCur and made
367// sure that it can *indeed* be promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000368bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000369 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
370 const TargetRegisterClass* RC) {
371 assert(DepType == SDep::Data);
372 int CurOpcode = HII->getDotCurOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000373 MI.setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000374 return true;
375}
376
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000377void HexagonPacketizerList::cleanUpDotCur() {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000378 MachineInstr *MI = nullptr;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000379 for (auto BI : CurrentPacketMIs) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000380 LLVM_DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000381 if (HII->isDotCurInst(*BI)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000382 MI = BI;
383 continue;
384 }
385 if (MI) {
386 for (auto &MO : BI->operands())
387 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
388 return;
389 }
390 }
391 if (!MI)
392 return;
393 // We did not find a use of the CUR, so de-cur it.
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000394 MI->setDesc(HII->get(HII->getNonDotCurOp(*MI)));
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000395 LLVM_DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000396}
397
398// Check to see if an instruction can be dot cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000399bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000400 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
401 const TargetRegisterClass *RC) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000402 if (!HII->isHVXVec(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000403 return false;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000404 if (!HII->isHVXVec(*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000405 return false;
406
407 // Already a dot new instruction.
408 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
409 return false;
410
411 if (!HII->mayBeCurLoad(MI))
412 return false;
413
414 // The "cur value" cannot come from inline asm.
415 if (PacketSU->getInstr()->isInlineAsm())
416 return false;
417
418 // Make sure candidate instruction uses cur.
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000419 LLVM_DEBUG(dbgs() << "Can we DOT Cur Vector MI\n"; MI.dump();
420 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000421 MachineInstr &MJ = *MII;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000422 LLVM_DEBUG({
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000423 dbgs() << "Checking CUR against ";
424 MJ.dump();
425 });
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000426 unsigned DestReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000427 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000428 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000429 if (MO.isReg() && MO.getReg() == DestReg)
430 FoundMatch = true;
431 if (!FoundMatch)
432 return false;
433
434 // Check for existing uses of a vector register within the packet which
435 // would be affected by converting a vector load into .cur formt.
436 for (auto BI : CurrentPacketMIs) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000437 LLVM_DEBUG(dbgs() << "packet has "; BI->dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000438 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
439 return false;
440 }
441
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000442 LLVM_DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000443 // We can convert the opcode into a .cur.
444 return true;
445}
446
447// Promote an instruction to its .new form. At this time, we have already
448// made a call to canPromoteToDotNew and made sure that it can *indeed* be
449// promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000450bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000451 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
452 const TargetRegisterClass* RC) {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000453 assert(DepType == SDep::Data);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000454 int NewOpcode;
455 if (RC == &Hexagon::PredRegsRegClass)
456 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
457 else
458 NewOpcode = HII->getDotNewOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000459 MI.setDesc(HII->get(NewOpcode));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000460 return true;
461}
462
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000463bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +0000464 int NewOpcode = HII->getDotOldOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000465 MI.setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000466 return true;
467}
468
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000469bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) {
470 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000471 switch (Opc) {
472 case Hexagon::S2_storerd_io:
473 case Hexagon::S2_storeri_io:
474 case Hexagon::S2_storerh_io:
475 case Hexagon::S2_storerb_io:
476 break;
477 default:
478 llvm_unreachable("Unexpected instruction");
479 }
Matthias Braun941a7052016-07-28 18:40:00 +0000480 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000481 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000482 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000483 if (HII->isValidOffset(Opc, NewOff, HRI)) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000484 Off.setImm(NewOff);
485 return true;
486 }
487 return false;
488}
489
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000490void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) {
491 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000492 switch (Opc) {
493 case Hexagon::S2_storerd_io:
494 case Hexagon::S2_storeri_io:
495 case Hexagon::S2_storerh_io:
496 case Hexagon::S2_storerb_io:
497 break;
498 default:
499 llvm_unreachable("Unexpected instruction");
500 }
Matthias Braun941a7052016-07-28 18:40:00 +0000501 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000502 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000503 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
504}
505
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +0000506/// Return true if we can update the offset in MI so that MI and MJ
507/// can be packetized together.
508bool HexagonPacketizerList::updateOffset(SUnit *SUI, SUnit *SUJ) {
509 assert(SUI->getInstr() && SUJ->getInstr());
510 MachineInstr &MI = *SUI->getInstr();
511 MachineInstr &MJ = *SUJ->getInstr();
512
513 unsigned BPI, OPI;
514 if (!HII->getBaseAndOffsetPosition(MI, BPI, OPI))
515 return false;
516 unsigned BPJ, OPJ;
517 if (!HII->getBaseAndOffsetPosition(MJ, BPJ, OPJ))
518 return false;
519 unsigned Reg = MI.getOperand(BPI).getReg();
520 if (Reg != MJ.getOperand(BPJ).getReg())
521 return false;
522 // Make sure that the dependences do not restrict adding MI to the packet.
523 // That is, ignore anti dependences, and make sure the only data dependence
524 // involves the specific register.
525 for (const auto &PI : SUI->Preds)
526 if (PI.getKind() != SDep::Anti &&
527 (PI.getKind() != SDep::Data || PI.getReg() != Reg))
528 return false;
529 int Incr;
530 if (!HII->getIncrementValue(MJ, Incr))
531 return false;
532
533 int64_t Offset = MI.getOperand(OPI).getImm();
Krzysztof Parzyszek0f983d62018-03-30 19:28:37 +0000534 if (!HII->isValidOffset(MI.getOpcode(), Offset+Incr, HRI))
535 return false;
536
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +0000537 MI.getOperand(OPI).setImm(Offset + Incr);
538 ChangedOffset = Offset;
539 return true;
540}
541
542/// Undo the changed offset. This is needed if the instruction cannot be
543/// added to the current packet due to a different instruction.
544void HexagonPacketizerList::undoChangedOffset(MachineInstr &MI) {
545 unsigned BP, OP;
546 if (!HII->getBaseAndOffsetPosition(MI, BP, OP))
547 llvm_unreachable("Unable to find base and offset operands.");
548 MI.getOperand(OP).setImm(ChangedOffset);
549}
550
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000551enum PredicateKind {
552 PK_False,
553 PK_True,
554 PK_Unknown
555};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000556
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000557/// Returns true if an instruction is predicated on p0 and false if it's
558/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000559static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000560 const HexagonInstrInfo *HII) {
561 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000562 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000563 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000564 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000565 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000566}
567
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000568static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000569 const HexagonInstrInfo *HII) {
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000570 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000571#ifndef NDEBUG
572 // Post Increment means duplicates. Use dense map to find duplicates in the
573 // list. Caution: Densemap initializes with the minimum of 64 buckets,
574 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000575 DenseSet<unsigned> DefRegsSet;
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.isDef())
578 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000579
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000580 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000581 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
582 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000583#else
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000584 if (MI.mayLoad()) {
585 const MachineOperand &Op1 = MI.getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000586 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000587 assert(Op1.isReg() && "Post increment operand has be to a register.");
588 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000589 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000590 if (MI.getDesc().mayStore()) {
591 const MachineOperand &Op0 = MI.getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000592 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000593 assert(Op0.isReg() && "Post increment operand has be to a register.");
594 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000595 }
596#endif
597 // we should never come here.
598 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
599}
600
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000601// Get the value being stored.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000602static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000603 // value being stored is always the last operand.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000604 return MI.getOperand(MI.getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000605}
606
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000607static bool isLoadAbsSet(const MachineInstr &MI) {
608 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000609 switch (Opc) {
610 case Hexagon::L4_loadrd_ap:
611 case Hexagon::L4_loadrb_ap:
612 case Hexagon::L4_loadrh_ap:
613 case Hexagon::L4_loadrub_ap:
614 case Hexagon::L4_loadruh_ap:
615 case Hexagon::L4_loadri_ap:
616 return true;
617 }
618 return false;
619}
620
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000621static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000622 assert(isLoadAbsSet(MI));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000623 return MI.getOperand(1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000624}
625
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000626// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000627// Following restrictions are to be respected in convert a store into
628// a new value store.
629// 1. If an instruction uses auto-increment, its address register cannot
630// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000631// 2. If an instruction uses absolute-set addressing mode, its address
632// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000633// 3. If an instruction produces a 64-bit result, its registers cannot be used
634// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000635// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000636// the instruction that uses the new-value register must also be conditional,
637// and both must always have their predicates evaluate identically.
638// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000639// 5. There is an implied restriction that a packet cannot have another store,
640// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000641// already a store in a packet, there can not be a new value store.
642// Arch Spec: 3.4.4.2
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000643bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
644 const MachineInstr &PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000645 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000646 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000647 return false;
648
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000649 // Make sure there is dependency and can be new value'd.
650 const MachineOperand &Val = getStoreValueOperand(MI);
651 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000652 return false;
653
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000654 const MCInstrDesc& MCID = PacketMI.getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000655
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000656 // First operand is always the result.
657 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
658 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
659 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000660 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000661
662 // New-value stores are of class NV (slot 0), dual stores require class ST
663 // in slot 0 (PRM 5.5).
664 for (auto I : CurrentPacketMIs) {
665 SUnit *PacketSU = MIToSUnit.find(I)->second;
666 if (PacketSU->getInstr()->mayStore())
667 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000668 }
669
670 // Make sure it's NOT the post increment register that we are going to
671 // new value.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000672 if (HII->isPostIncrement(MI) &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000673 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000674 return false;
675 }
676
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000677 if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000678 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
679 // If source is post_inc, or absolute-set addressing, it can not feed
680 // into new value store
681 // r3 = memw(r2++#4)
682 // memw(r30 + #-1404) = r2.new -> can not be new value store
683 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000684 return false;
685 }
686
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000687 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
688 return false;
689
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000690 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000691 // also be predicated.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000692 if (HII->isPredicated(PacketMI)) {
693 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000694 return false;
695
696 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000697 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000698 unsigned predRegNumSrc = 0;
699 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000700 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000701
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000702 // Get predicate register used in the source instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000703 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000704 if (!MO.isReg())
705 continue;
706 predRegNumSrc = MO.getReg();
707 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
708 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000709 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000710 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000711 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
712 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000713
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000714 // Get predicate register used in new-value store instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000715 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000716 if (!MO.isReg())
717 continue;
718 predRegNumDst = MO.getReg();
719 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
720 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000721 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000722 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000723 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
724 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000725
726 // New-value register producer and user (store) need to satisfy these
727 // constraints:
728 // 1) Both instructions should be predicated on the same register.
729 // 2) If producer of the new-value register is .new predicated then store
730 // should also be .new predicated and if producer is not .new predicated
731 // then store should not be .new predicated.
732 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000733 // sense, i.e, either both should be negated or both should be non-negated.
734 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000735 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000736 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000737 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000738 }
739
740 // Make sure that other than the new-value register no other store instruction
741 // register has been modified in the same packet. Predicate registers can be
742 // modified by they should not be modified between the producer and the store
743 // instruction as it will make them both conditional on different values.
744 // We already know this to be true for all the instructions before and
745 // including PacketMI. Howerver, we need to perform the check for the
746 // remaining instructions in the packet.
747
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000748 unsigned StartCheck = 0;
749
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000750 for (auto I : CurrentPacketMIs) {
751 SUnit *TempSU = MIToSUnit.find(I)->second;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000752 MachineInstr &TempMI = *TempSU->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000753
754 // Following condition is true for all the instructions until PacketMI is
755 // reached (StartCheck is set to 0 before the for loop).
756 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000757 if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
758 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000759
760 StartCheck = 1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000761 if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000762 continue;
763
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000764 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000765 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000766 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000767 }
768
Alp Tokerf907b892013-12-05 05:44:44 +0000769 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000770 // 1. The only use of reg is DepReg and no other registers.
771 // This handles V4 base+index registers.
772 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000773 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000774 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000775 if (!HII->isPostIncrement(MI)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000776 for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
777 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000778 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000779 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000780 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000781 }
782
783 // If data definition is because of implicit definition of the register,
784 // do not newify the store. Eg.
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000785 // %r9 = ZXTH %r12, implicit %d6, implicit-def %r12
786 // S2_storerh_io %r8, 2, killed %r12; mem:ST2[%scevgep343]
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000787 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000788 if (MO.isRegMask() && MO.clobbersPhysReg(DepReg))
789 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000790 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
791 continue;
792 unsigned R = MO.getReg();
793 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
794 return false;
795 }
796
797 // Handle imp-use of super reg case. There is a target independent side
798 // change that should prevent this situation but I am handling it for
799 // just-in-case. For example, we cannot newify R2 in the following case:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000800 // %r3 = A2_tfrsi 0;
801 // S2_storeri_io killed %r0, 0, killed %r2, implicit killed %d1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000802 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000803 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
804 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000805 }
806
807 // Can be dot new store.
808 return true;
809}
810
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000811// Can this MI to promoted to either new value store or new value jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000812bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000813 const SUnit *PacketSU, unsigned DepReg,
814 MachineBasicBlock::iterator &MII) {
815 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000816 return false;
817
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000818 // Check to see the store can be new value'ed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000819 MachineInstr &PacketMI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000820 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000821 return true;
822
823 // Check to see the compare/jump can be new value'ed.
824 // This is done as a pass on its own. Don't need to check it here.
825 return false;
826}
827
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000828static bool isImplicitDependency(const MachineInstr &I, bool CheckDef,
829 unsigned DepReg) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000830 for (auto &MO : I.operands()) {
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000831 if (CheckDef && MO.isRegMask() && MO.clobbersPhysReg(DepReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000832 return true;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000833 if (!MO.isReg() || MO.getReg() != DepReg || !MO.isImplicit())
834 continue;
835 if (CheckDef == MO.isDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000836 return true;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000837 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000838 return false;
839}
840
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000841// Check to see if an instruction can be dot new
842// There are three kinds.
843// 1. dot new on predicate - V2/V3/V4
844// 2. dot new on stores NV/ST - V4
845// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000846bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000847 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
848 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000849 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000850 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000851 return false;
852
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000853 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000854 return false;
855
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000856 const MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000857
858 // The "new value" cannot come from inline asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000859 if (PI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000860 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000861
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000862 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
863 // sense.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000864 if (PI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000865 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000866
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000867 // If dependency is trough an implicitly defined register, we should not
868 // newify the use.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000869 if (isImplicitDependency(PI, true, DepReg) ||
870 isImplicitDependency(MI, false, DepReg))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000871 return false;
872
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000873 const MCInstrDesc& MCID = PI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000874 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000875 if (DisableVecDblNVStores && VecRC == &Hexagon::HvxWRRegClass)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000876 return false;
877
878 // predicate .new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000879 if (RC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000880 return HII->predCanBeUsedAsDotNew(PI, DepReg);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000881
882 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
883 return false;
884
885 // Create a dot new machine instruction to see if resources can be
886 // allocated. If not, bail out now.
887 int NewOpcode = HII->getDotNewOp(MI);
888 const MCInstrDesc &D = HII->get(NewOpcode);
889 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000890 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000891 MF.DeleteMachineInstr(NewMI);
892 if (!ResourcesAvailable)
893 return false;
894
895 // New Value Store only. New Value Jump generated as a separate pass.
896 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
897 return false;
898
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000899 return true;
900}
901
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000902// Go through the packet instructions and search for an anti dependency between
903// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000904// Trying to add
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000905// a) %r1 = TFRI_cdNotPt %p3, 2
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000906// to this packet:
907// {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000908// b) %p0 = C2_or killed %p3, killed %p0
909// c) %p3 = C2_tfrrp %r23
910// d) %r1 = C2_cmovenewit %p3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000911// }
912// The P3 from a) and d) will be complements after
913// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000914// Anti-dep between c) and b) is irrelevant for this case
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000915bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000916 unsigned DepReg) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000917 SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000918
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000919 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000920 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000921 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000922 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000923
924 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000925 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000926
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000927 // Look at dependencies between current members of the packet and
928 // predicate defining instruction MI. Make sure that dependency is
929 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000930 if (PacketSU->isSucc(PacketSUDep)) {
931 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000932 auto &Dep = PacketSU->Succs[i];
933 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
934 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000935 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000936 }
937 }
938 }
939
940 return false;
941}
942
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000943/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000944static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000945 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000946 /// We use the following rule: The first predicate register that is a use is
947 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000948 assert(QII->isPredicated(MI) && "Must be predicated instruction");
949
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000950 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000951 if (Op.isReg() && Op.getReg() && Op.isUse() &&
952 Hexagon::PredRegsRegClass.contains(Op.getReg()))
953 return Op.getReg();
954 }
955
956 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000957 return 0;
958}
959
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000960// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000961// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000962bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
963 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000964 // If we don't know the predicate sense of the instructions bail out early, we
965 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000966 if (getPredicateSense(MI1, HII) == PK_Unknown ||
967 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000968 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000969
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000970 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000971 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000972
973 // One corner case deals with the following scenario:
974 // Trying to add
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000975 // a) %r24 = A2_tfrt %p0, %r25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000976 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000977 // {
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000978 // b) %r25 = A2_tfrf %p0, %r24
979 // c) %p0 = C2_cmpeqi %r26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000980 // }
981 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000982 // On general check a) and b) are complements, but presence of c) will
983 // convert a) to .new form, and then it is not a complement.
984 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000985
986 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000987 // Look for Anti dependecy on the same predicate reg as used in the
988 // candidate.
989 for (auto I : CurrentPacketMIs) {
990 // Scheduling Unit for current insn in the packet.
991 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000992
993 // If this instruction in the packet is succeeded by the candidate...
994 if (PacketSU->isSucc(SU)) {
995 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000996 auto Dep = PacketSU->Succs[i];
997 // The corner case exist when there is true data dependency between
998 // candidate and one of current packet members, this dep is on
999 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001000 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001001 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
1002 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
1003 // Here I know that I is predicate setting instruction with true
1004 // data dep to candidate on the register we care about - c) in the
1005 // above example. Now I need to see if there is an anti dependency
1006 // from c) to any other instruction in the same packet on the pred
1007 // reg of interest.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001008 if (restrictingDepExistInPacket(*I, Dep.getReg()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001009 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001010 }
1011 }
1012 }
1013 }
1014
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001015 // If the above case does not apply, check regular complement condition.
1016 // Check that the predicate register is the same and that the predicate
1017 // sense is different We also need to differentiate .old vs. .new: !p0
1018 // is not complementary to p0.new.
1019 unsigned PReg1 = getPredicatedRegister(MI1, HII);
1020 unsigned PReg2 = getPredicatedRegister(MI2, HII);
1021 return PReg1 == PReg2 &&
1022 Hexagon::PredRegsRegClass.contains(PReg1) &&
1023 Hexagon::PredRegsRegClass.contains(PReg2) &&
1024 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001025 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001026}
1027
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001028// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001029void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001030 Dependence = false;
1031 PromotedToDotNew = false;
1032 GlueToNewValueJump = false;
1033 GlueAllocframeStore = false;
1034 FoundSequentialDependence = false;
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00001035 ChangedOffset = INT64_MAX;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001036}
1037
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001038// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001039bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
1040 const MachineBasicBlock *) {
Shiva Chen801bf7e2018-05-09 02:42:00 +00001041 if (MI.isDebugInstr())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001042 return true;
1043
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001044 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +00001045 return false;
1046
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001047 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001048 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001049 return false;
1050
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001051 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001052 return false;
1053
1054 // We check if MI has any functional units mapped to it. If it doesn't,
1055 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001056 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001057 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +00001058 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001059 return !FuncUnits;
1060}
1061
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001062bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001063 // Ensure any bundles created by gather packetize remain seperate.
1064 if (MI.isBundle())
1065 return true;
1066
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001067 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001068 return true;
1069
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001070 // Consider inline asm to not be a solo instruction by default.
1071 // Inline asm will be put in a packet temporarily, but then it will be
1072 // removed, and placed outside of the packet (before or after, depending
1073 // on dependencies). This is to reduce the impact of inline asm as a
1074 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001075 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001076 return true;
1077
1078 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
1079 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
1080 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001081 if (isSchedBarrier(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001082 return true;
1083
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001084 if (HII->isSolo(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001085 return true;
1086
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001087 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001088 return true;
1089
1090 return false;
1091}
1092
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001093// Quick check if instructions MI and MJ cannot coexist in the same packet.
1094// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1095// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1096// For full test call this function twice:
1097// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1098// Doing the test only one way saves the amount of code in this function,
1099// since every test would need to be repeated with the MI and MJ reversed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001100static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001101 const HexagonInstrInfo &HII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001102 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00001103 if (MF->getSubtarget<HexagonSubtarget>().hasV60OpsOnly() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001104 HII.isHVXMemWithAIndirect(MI, MJ))
1105 return true;
1106
1107 // An inline asm cannot be together with a branch, because we may not be
1108 // able to remove the asm out after packetizing (i.e. if the asm must be
1109 // moved past the bundle). Similarly, two asms cannot be together to avoid
1110 // complications when determining their relative order outside of a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001111 if (MI.isInlineAsm())
1112 return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
1113 MJ.isCall() || MJ.isTerminator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001114
Krzysztof Parzyszekf4ad2cb2018-09-04 21:07:27 +00001115 // New-value stores cannot coexist with any other stores.
1116 if (HII.isNewValueStore(MI) && MJ.mayStore())
1117 return true;
1118
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001119 switch (MI.getOpcode()) {
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001120 case Hexagon::S2_storew_locked:
1121 case Hexagon::S4_stored_locked:
1122 case Hexagon::L2_loadw_locked:
1123 case Hexagon::L4_loadd_locked:
Krzysztof Parzyszek5c2944c2018-06-19 17:26:20 +00001124 case Hexagon::Y2_dccleana:
1125 case Hexagon::Y2_dccleaninva:
1126 case Hexagon::Y2_dcinva:
1127 case Hexagon::Y2_dczeroa:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001128 case Hexagon::Y4_l2fetch:
1129 case Hexagon::Y5_l2fetch: {
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001130 // These instructions can only be grouped with ALU32 or non-floating-point
1131 // XTYPE instructions. Since there is no convenient way of identifying fp
1132 // XTYPE instructions, only allow grouping with ALU32 for now.
1133 unsigned TJ = HII.getType(MJ);
Krzysztof Parzyszek5ea971c2017-02-07 17:47:37 +00001134 if (TJ != HexagonII::TypeALU32_2op &&
1135 TJ != HexagonII::TypeALU32_3op &&
1136 TJ != HexagonII::TypeALU32_ADDI)
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001137 return true;
1138 break;
1139 }
1140 default:
1141 break;
1142 }
1143
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001144 // "False" really means that the quick check failed to determine if
1145 // I and J cannot coexist.
1146 return false;
1147}
1148
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001149// Full, symmetric check.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001150bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
1151 const MachineInstr &MJ) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001152 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1153}
1154
1155void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1156 for (auto &B : MF) {
1157 MachineBasicBlock::iterator BundleIt;
1158 MachineBasicBlock::instr_iterator NextI;
1159 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1160 NextI = std::next(I);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001161 MachineInstr &MI = *I;
1162 if (MI.isBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001163 BundleIt = I;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001164 if (!MI.isInsideBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001165 continue;
1166
1167 // Decide on where to insert the instruction that we are pulling out.
1168 // Debug instructions always go before the bundle, but the placement of
1169 // INLINE_ASM depends on potential dependencies. By default, try to
1170 // put it before the bundle, but if the asm writes to a register that
1171 // other instructions in the bundle read, then we need to place it
1172 // after the bundle (to preserve the bundle semantics).
1173 bool InsertBeforeBundle;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001174 if (MI.isInlineAsm())
1175 InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
1176 else if (MI.isDebugValue())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001177 InsertBeforeBundle = true;
1178 else
1179 continue;
1180
1181 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1182 }
1183 }
1184}
1185
1186// Check if a given instruction is of class "system".
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001187static bool isSystemInstr(const MachineInstr &MI) {
1188 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001189 switch (Opc) {
1190 case Hexagon::Y2_barrier:
1191 case Hexagon::Y2_dcfetchbo:
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001192 case Hexagon::Y4_l2fetch:
1193 case Hexagon::Y5_l2fetch:
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001194 return true;
1195 }
1196 return false;
1197}
1198
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001199bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
1200 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001201 // The dependence graph may not include edges between dead definitions,
1202 // so without extra checks, we could end up packetizing two instruction
1203 // defining the same (dead) register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001204 if (I.isCall() || J.isCall())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001205 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001206 if (HII->isPredicated(I) || HII->isPredicated(J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001207 return false;
1208
1209 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001210 for (auto &MO : I.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001211 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1212 continue;
1213 DeadDefs[MO.getReg()] = true;
1214 }
1215
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001216 for (auto &MO : J.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001217 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1218 continue;
1219 unsigned R = MO.getReg();
1220 if (R != Hexagon::USR_OVF && DeadDefs[R])
1221 return true;
1222 }
1223 return false;
1224}
1225
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001226bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
1227 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001228 // A save callee-save register function call can only be in a packet
1229 // with instructions that don't write to the callee-save registers.
1230 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1231 doesModifyCalleeSavedReg(J, HRI)) ||
1232 (HII->isSaveCalleeSavedRegsCall(J) &&
1233 doesModifyCalleeSavedReg(I, HRI)))
1234 return true;
1235
1236 // Two control flow instructions cannot go in the same packet.
1237 if (isControlFlow(I) && isControlFlow(J))
1238 return true;
1239
1240 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1241 // contain a speculative indirect jump,
1242 // a new-value compare jump or a dealloc_return.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001243 auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
1244 if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001245 return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001246 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001247 return true;
1248 return false;
1249 };
1250
1251 if (HII->isLoopN(I) && isBadForLoopN(J))
1252 return true;
1253 if (HII->isLoopN(J) && isBadForLoopN(I))
1254 return true;
1255
1256 // dealloc_return cannot appear in the same packet as a conditional or
1257 // unconditional jump.
1258 return HII->isDeallocRet(I) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001259 (J.isBranch() || J.isCall() || J.isBarrier());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001260}
1261
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001262bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I,
1263 const MachineInstr &J) {
1264 // Adding I to a packet that has J.
1265
1266 // Regmasks are not reflected in the scheduling dependency graph, so
1267 // we need to check them manually. This code assumes that regmasks only
1268 // occur on calls, and the problematic case is when we add an instruction
1269 // defining a register R to a packet that has a call that clobbers R via
1270 // a regmask. Those cannot be packetized together, because the call will
1271 // be executed last. That's also a reson why it is ok to add a call
1272 // clobbering R to a packet that defines R.
1273
1274 // Look for regmasks in J.
1275 for (const MachineOperand &OpJ : J.operands()) {
1276 if (!OpJ.isRegMask())
1277 continue;
1278 assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call");
1279 for (const MachineOperand &OpI : I.operands()) {
1280 if (OpI.isReg()) {
1281 if (OpJ.clobbersPhysReg(OpI.getReg()))
1282 return true;
1283 } else if (OpI.isRegMask()) {
1284 // Both are regmasks. Assume that they intersect.
1285 return true;
1286 }
1287 }
1288 }
1289 return false;
1290}
1291
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001292bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I,
1293 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001294 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001295 bool StoreI = I.mayStore(), StoreJ = J.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001296 if ((SysI && StoreJ) || (SysJ && StoreI))
1297 return true;
1298
1299 if (StoreI && StoreJ) {
1300 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1301 return true;
1302 } else {
1303 // A memop cannot be in the same packet with another memop or a store.
1304 // Two stores can be together, but here I and J cannot both be stores.
1305 bool MopStI = HII->isMemOp(I) || StoreI;
1306 bool MopStJ = HII->isMemOp(J) || StoreJ;
1307 if (MopStI && MopStJ)
1308 return true;
1309 }
1310
1311 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1312}
1313
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001314// SUI is the current instruction that is out side of the current packet.
1315// SUJ is the current instruction inside the current packet against which that
1316// SUI will be packetized.
1317bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001318 assert(SUI->getInstr() && SUJ->getInstr());
1319 MachineInstr &I = *SUI->getInstr();
1320 MachineInstr &J = *SUJ->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001321
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001322 // Clear IgnoreDepMIs when Packet starts.
1323 if (CurrentPacketMIs.size() == 1)
1324 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001325
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001326 MachineBasicBlock::iterator II = I.getIterator();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001327
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001328 // Solo instructions cannot go in the packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001329 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001330
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001331 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001332 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001333
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001334 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1335 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001336 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001337
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001338 // Regmasks are not accounted for in the scheduling graph, so we need
1339 // to explicitly check for dependencies caused by them. They should only
1340 // appear on calls, so it's not too pessimistic to reject all regmask
1341 // dependencies.
1342 Dependence = hasRegMaskDependence(I, J);
1343 if (Dependence)
1344 return false;
1345
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001346 // V4 allows dual stores. It does not allow second store, if the first
1347 // store is not in SLOT0. New value store, new value jump, dealloc_return
1348 // and memop always take SLOT0. Arch spec 3.4.4.2.
1349 Dependence = hasV4SpecificDependence(I, J);
1350 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001351 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001352
1353 // If an instruction feeds new value jump, glue it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001354 MachineBasicBlock::iterator NextMII = I.getIterator();
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001355 ++NextMII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001356 if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001357 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001358
1359 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001360 const MachineOperand &NOp0 = NextMI.getOperand(0);
1361 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001362
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001363 if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001364 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001365
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001366 for (MachineInstr *PI : CurrentPacketMIs) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001367 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001368 if (PI->isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001369 Dependence = true;
1370 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001371 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001372 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001373 // 1. Packet does not have a store in it.
1374 // 2. If the first operand of the nvj is newified, and the second
1375 // operand is also a reg, it (second reg) is not defined in
1376 // the same packet.
1377 // 3. If the second operand of the nvj is newified, (which means
1378 // first operand is also a reg), first reg is not defined in
1379 // the same packet.
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001380 if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() ||
1381 HII->isLoopN(*PI)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001382 Dependence = true;
1383 break;
1384 }
1385 // Check #2/#3.
1386 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001387 if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001388 Dependence = true;
1389 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001390 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001391 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001392
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001393 GlueToNewValueJump = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001394 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001395 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001396 }
1397
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001398 // There no dependency between a prolog instruction and its successor.
1399 if (!SUJ->isSucc(SUI))
1400 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001401
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001402 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1403 if (FoundSequentialDependence)
1404 break;
1405
1406 if (SUJ->Succs[i].getSUnit() != SUI)
1407 continue;
1408
1409 SDep::Kind DepType = SUJ->Succs[i].getKind();
1410 // For direct calls:
1411 // Ignore register dependences for call instructions for packetization
1412 // purposes except for those due to r31 and predicate registers.
1413 //
1414 // For indirect calls:
1415 // Same as direct calls + check for true dependences to the register
1416 // used in the indirect call.
1417 //
1418 // We completely ignore Order dependences for call instructions.
1419 //
1420 // For returns:
1421 // Ignore register dependences for return instructions like jumpr,
1422 // dealloc return unless we have dependencies on the explicit uses
1423 // of the registers used by jumpr (like r31) or dealloc return
1424 // (like r29 or r30).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001425 unsigned DepReg = 0;
1426 const TargetRegisterClass *RC = nullptr;
1427 if (DepType == SDep::Data) {
1428 DepReg = SUJ->Succs[i].getReg();
1429 RC = HRI->getMinimalPhysRegClass(DepReg);
1430 }
1431
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +00001432 if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001433 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001434 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001435 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1436 continue;
1437 }
1438
1439 if (DepType == SDep::Data) {
1440 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1441 if (promoteToDotCur(J, DepType, II, RC))
1442 continue;
1443 }
1444
1445 // Data dpendence ok if we have load.cur.
1446 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001447 if (HII->isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001448 continue;
1449 }
1450
1451 // For instructions that can be promoted to dot-new, try to promote.
1452 if (DepType == SDep::Data) {
1453 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1454 if (promoteToDotNew(I, DepType, II, RC)) {
1455 PromotedToDotNew = true;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001456 if (cannotCoexist(I, J))
1457 FoundSequentialDependence = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001458 continue;
1459 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001460 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001461 if (HII->isNewValueJump(I))
1462 continue;
1463 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001464
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001465 // For predicated instructions, if the predicates are complements then
1466 // there can be no dependence.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001467 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1468 arePredicatesComplements(I, J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001469 // Not always safe to do this translation.
1470 // DAG Builder attempts to reduce dependence edges using transitive
1471 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001472 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001473 // r0 = tfr_pt ... (1)
1474 // r0 = tfr_pf ... (2)
1475 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001476 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001477 // There will be an output dependence between (1)->(2) and (2)->(3).
1478 // However, there is no dependence edge between (1)->(3). This results
1479 // in all 3 instructions going in the same packet. We ignore dependce
1480 // only once to avoid this situation.
David Majnemer0d955d02016-08-11 22:21:41 +00001481 auto Itr = find(IgnoreDepMIs, &J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001482 if (Itr != IgnoreDepMIs.end()) {
1483 Dependence = true;
1484 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001485 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001486 IgnoreDepMIs.push_back(&I);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001487 continue;
1488 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001489
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001490 // Ignore Order dependences between unconditional direct branches
1491 // and non-control-flow instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001492 if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001493 DepType == SDep::Order)
1494 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001495
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001496 // Ignore all dependences for jumps except for true and output
1497 // dependences.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001498 if (I.isConditionalBranch() && DepType != SDep::Data &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001499 DepType != SDep::Output)
1500 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001501
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001502 if (DepType == SDep::Output) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001503 FoundSequentialDependence = true;
1504 break;
1505 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001506
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001507 // For Order dependences:
1508 // 1. On V4 or later, volatile loads/stores can be packetized together,
1509 // unless other rules prevent is.
1510 // 2. Store followed by a load is not allowed.
1511 // 3. Store followed by a store is only valid on V4 or later.
1512 // 4. Load followed by any memory operation is allowed.
1513 if (DepType == SDep::Order) {
1514 if (!PacketizeVolatiles) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001515 bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001516 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001517 FoundSequentialDependence = true;
1518 break;
1519 }
1520 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001521 // J is first, I is second.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001522 bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
1523 bool LoadI = I.mayLoad(), StoreI = I.mayStore();
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001524 bool NVStoreJ = HII->isNewValueStore(J);
1525 bool NVStoreI = HII->isNewValueStore(I);
1526 bool IsVecJ = HII->isHVXVec(J);
1527 bool IsVecI = HII->isHVXVec(I);
1528
Krzysztof Parzyszekd8b780d2018-06-20 13:56:09 +00001529 if (Slot1Store && MF.getSubtarget<HexagonSubtarget>().hasV65Ops() &&
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001530 ((LoadJ && StoreI && !NVStoreI) ||
1531 (StoreJ && LoadI && !NVStoreJ)) &&
1532 (J.getOpcode() != Hexagon::S2_allocframe &&
1533 I.getOpcode() != Hexagon::S2_allocframe) &&
1534 (J.getOpcode() != Hexagon::L2_deallocframe &&
1535 I.getOpcode() != Hexagon::L2_deallocframe) &&
1536 (!HII->isMemOp(J) && !HII->isMemOp(I)) && (!IsVecJ && !IsVecI))
1537 setmemShufDisabled(true);
1538 else
1539 if (StoreJ && LoadI && alias(J, I)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001540 FoundSequentialDependence = true;
1541 break;
1542 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001543
1544 if (!StoreJ)
1545 if (!LoadJ || (!LoadI && !StoreI)) {
1546 // If J is neither load nor store, assume a dependency.
1547 // If J is a load, but I is neither, also assume a dependency.
1548 FoundSequentialDependence = true;
1549 break;
1550 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001551 // Store followed by store: not OK on V2.
1552 // Store followed by load: not OK on all.
1553 // Load followed by store: OK on all.
1554 // Load followed by load: OK on all.
1555 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001556 }
1557
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001558 // For V4, special case ALLOCFRAME. Even though there is dependency
1559 // between ALLOCFRAME and subsequent store, allow it to be packetized
1560 // in a same packet. This implies that the store is using the caller's
1561 // SP. Hence, offset needs to be updated accordingly.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001562 if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
1563 unsigned Opc = I.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001564 switch (Opc) {
1565 case Hexagon::S2_storerd_io:
1566 case Hexagon::S2_storeri_io:
1567 case Hexagon::S2_storerh_io:
1568 case Hexagon::S2_storerb_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001569 if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001570 // Since this store is to be glued with allocframe in the same
1571 // packet, it will use SP of the previous stack frame, i.e.
1572 // caller's SP. Therefore, we need to recalculate offset
1573 // according to this change.
1574 GlueAllocframeStore = useCallersSP(I);
1575 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001576 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001577 }
1578 default:
1579 break;
1580 }
1581 }
1582
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001583 // There are certain anti-dependencies that cannot be ignored.
1584 // Specifically:
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +00001585 // J2_call ... implicit-def %r0 ; SUJ
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001586 // R0 = ... ; SUI
1587 // Those cannot be packetized together, since the call will observe
1588 // the effect of the assignment to R0.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001589 if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) {
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001590 // Check if I defines any volatile register. We should also check
1591 // registers that the call may read, but these happen to be a
1592 // subset of the volatile register set.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001593 for (const MachineOperand &Op : I.operands()) {
1594 if (Op.isReg() && Op.isDef()) {
1595 unsigned R = Op.getReg();
1596 if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI))
1597 continue;
1598 } else if (!Op.isRegMask()) {
1599 // If I has a regmask assume dependency.
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001600 continue;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001601 }
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001602 FoundSequentialDependence = true;
1603 break;
1604 }
1605 }
1606
1607 // Skip over remaining anti-dependences. Two instructions that are
1608 // anti-dependent can share a packet, since in most such cases all
1609 // operands are read before any modifications take place.
1610 // The exceptions are branch and call instructions, since they are
1611 // executed after all other instructions have completed (at least
1612 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001613 if (DepType != SDep::Anti) {
1614 FoundSequentialDependence = true;
1615 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001616 }
1617 }
1618
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001619 if (FoundSequentialDependence) {
1620 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001621 return false;
1622 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001623
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001624 return true;
1625}
1626
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001627bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001628 assert(SUI->getInstr() && SUJ->getInstr());
1629 MachineInstr &I = *SUI->getInstr();
1630 MachineInstr &J = *SUJ->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001631
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001632 bool Coexist = !cannotCoexist(I, J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001633
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001634 if (Coexist && !Dependence)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001635 return true;
1636
1637 // Check if the instruction was promoted to a dot-new. If so, demote it
1638 // back into a dot-old.
1639 if (PromotedToDotNew)
1640 demoteToDotOld(I);
1641
1642 cleanUpDotCur();
1643 // Check if the instruction (must be a store) was glued with an allocframe
1644 // instruction. If so, restore its offset to its original value, i.e. use
1645 // current SP instead of caller's SP.
1646 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001647 useCalleesSP(I);
1648 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001649 }
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00001650
1651 if (ChangedOffset != INT64_MAX)
1652 undoChangedOffset(I);
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001653
1654 if (GlueToNewValueJump) {
1655 // Putting I and J together would prevent the new-value jump from being
1656 // packetized with the producer. In that case I and J must be separated.
1657 GlueToNewValueJump = false;
1658 return false;
1659 }
1660
Krzysztof Parzyszekf4ad2cb2018-09-04 21:07:27 +00001661 if (!Coexist)
1662 return false;
1663
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001664 if (ChangedOffset == INT64_MAX && updateOffset(SUI, SUJ)) {
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00001665 FoundSequentialDependence = false;
1666 Dependence = false;
1667 return true;
1668 }
1669
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001670 return false;
1671}
1672
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001673
1674bool HexagonPacketizerList::foundLSInPacket() {
1675 bool FoundLoad = false;
1676 bool FoundStore = false;
1677
1678 for (auto MJ : CurrentPacketMIs) {
1679 unsigned Opc = MJ->getOpcode();
1680 if (Opc == Hexagon::S2_allocframe || Opc == Hexagon::L2_deallocframe)
1681 continue;
1682 if (HII->isMemOp(*MJ))
1683 continue;
1684 if (MJ->mayLoad())
1685 FoundLoad = true;
1686 if (MJ->mayStore() && !HII->isNewValueStore(*MJ))
1687 FoundStore = true;
1688 }
1689 return FoundLoad && FoundStore;
1690}
1691
1692
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001693MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001694HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001695 MachineBasicBlock::iterator MII = MI.getIterator();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001696 MachineBasicBlock *MBB = MI.getParent();
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001697
Eugene Zelenko3b873362017-09-28 22:27:31 +00001698 if (CurrentPacketMIs.empty())
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001699 PacketStalls = false;
1700 PacketStalls |= producesStall(MI);
1701
Krzysztof Parzyszekb7e54e82018-04-06 18:19:22 +00001702 if (MI.isImplicitDef()) {
1703 // Add to the packet to allow subsequent instructions to be checked
1704 // properly.
1705 CurrentPacketMIs.push_back(&MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001706 return MII;
Krzysztof Parzyszekb7e54e82018-04-06 18:19:22 +00001707 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001708 assert(ResourceTracker->canReserveResources(MI));
1709
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001710 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001711 bool Good = true;
1712
1713 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001714 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001715 // We need to put both instructions in the same packet: MI and NvjMI.
1716 // Either of them can require a constant extender. Try to add both to
1717 // the current packet, and if that fails, end the packet and start a
1718 // new one.
1719 ResourceTracker->reserveResources(MI);
1720 if (ExtMI)
1721 Good = tryAllocateResourcesForConstExt(true);
1722
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001723 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001724 if (Good) {
1725 if (ResourceTracker->canReserveResources(NvjMI))
1726 ResourceTracker->reserveResources(NvjMI);
1727 else
1728 Good = false;
1729 }
1730 if (Good && ExtNvjMI)
1731 Good = tryAllocateResourcesForConstExt(true);
1732
1733 if (!Good) {
1734 endPacket(MBB, MI);
1735 assert(ResourceTracker->canReserveResources(MI));
1736 ResourceTracker->reserveResources(MI);
1737 if (ExtMI) {
1738 assert(canReserveResourcesForConstExt());
1739 tryAllocateResourcesForConstExt(true);
1740 }
1741 assert(ResourceTracker->canReserveResources(NvjMI));
1742 ResourceTracker->reserveResources(NvjMI);
1743 if (ExtNvjMI) {
1744 assert(canReserveResourcesForConstExt());
1745 reserveResourcesForConstExt();
1746 }
1747 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001748 CurrentPacketMIs.push_back(&MI);
1749 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001750 return MII;
1751 }
1752
1753 ResourceTracker->reserveResources(MI);
1754 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1755 endPacket(MBB, MI);
1756 if (PromotedToDotNew)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001757 demoteToDotOld(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001758 if (GlueAllocframeStore) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001759 useCalleesSP(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001760 GlueAllocframeStore = false;
1761 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001762 ResourceTracker->reserveResources(MI);
1763 reserveResourcesForConstExt();
1764 }
1765
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001766 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001767 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001768}
1769
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001770void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +00001771 MachineBasicBlock::iterator EndMI) {
1772 // Replace VLIWPacketizerList::endPacket(MBB, EndMI).
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001773
1774 bool memShufDisabled = getmemShufDisabled();
1775 if (memShufDisabled && !foundLSInPacket()) {
1776 setmemShufDisabled(false);
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001777 LLVM_DEBUG(dbgs() << " Not added to NoShufPacket\n");
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001778 }
1779 memShufDisabled = getmemShufDisabled();
1780
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +00001781 OldPacketMIs.clear();
1782 for (MachineInstr *MI : CurrentPacketMIs) {
1783 MachineBasicBlock::instr_iterator NextMI = std::next(MI->getIterator());
1784 for (auto &I : make_range(HII->expandVGatherPseudo(*MI), NextMI))
1785 OldPacketMIs.push_back(&I);
1786 }
1787 CurrentPacketMIs.clear();
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001788
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +00001789 if (OldPacketMIs.size() > 1) {
1790 MachineBasicBlock::instr_iterator FirstMI(OldPacketMIs.front());
1791 MachineBasicBlock::instr_iterator LastMI(EndMI.getInstrIterator());
1792 finalizeBundle(*MBB, FirstMI, LastMI);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001793 auto BundleMII = std::prev(FirstMI);
1794 if (memShufDisabled)
1795 HII->setBundleNoShuf(BundleMII);
1796
1797 setmemShufDisabled(false);
1798 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +00001799
1800 ResourceTracker->clearResources();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001801 LLVM_DEBUG(dbgs() << "End packet\n");
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001802}
1803
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001804bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +00001805 if (Minimal)
1806 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001807 return !producesStall(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001808}
1809
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001810// V60 forward scheduling.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001811bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001812 // If the packet already stalls, then ignore the stall from a subsequent
1813 // instruction in the same packet.
1814 if (PacketStalls)
1815 return false;
1816
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001817 // Check whether the previous packet is in a different loop. If this is the
1818 // case, there is little point in trying to avoid a stall because that would
1819 // favor the rare case (loop entry) over the common case (loop iteration).
1820 //
1821 // TODO: We should really be able to check all the incoming edges if this is
1822 // the first packet in a basic block, so we can avoid stalls from the loop
1823 // backedge.
1824 if (!OldPacketMIs.empty()) {
1825 auto *OldBB = OldPacketMIs.front()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001826 auto *ThisBB = I.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001827 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1828 return false;
1829 }
1830
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001831 SUnit *SUI = MIToSUnit[const_cast<MachineInstr *>(&I)];
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001832
Krzysztof Parzyszekaca8f322018-04-06 18:13:11 +00001833 // If the latency is 0 and there is a data dependence between this
1834 // instruction and any instruction in the current packet, we disregard any
1835 // potential stalls due to the instructions in the previous packet. Most of
1836 // the instruction pairs that can go together in the same packet have 0
1837 // latency between them. The exceptions are
1838 // 1. NewValueJumps as they're generated much later and the latencies can't
1839 // be changed at that point.
1840 // 2. .cur instructions, if its consumer has a 0 latency successor (such as
1841 // .new). In this case, the latency between .cur and the consumer stays
1842 // non-zero even though we can have both .cur and .new in the same packet.
1843 // Changing the latency to 0 is not an option as it causes software pipeliner
1844 // to not pipeline in some cases.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001845
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001846 // For Example:
1847 // {
1848 // I1: v6.cur = vmem(r0++#1)
1849 // I2: v7 = valign(v6,v4,r2)
1850 // I3: vmem(r5++#1) = v7.new
1851 // }
1852 // Here I2 and I3 has 0 cycle latency, but I1 and I2 has 2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001853
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001854 for (auto J : CurrentPacketMIs) {
1855 SUnit *SUJ = MIToSUnit[J];
1856 for (auto &Pred : SUI->Preds)
Krzysztof Parzyszekaca8f322018-04-06 18:13:11 +00001857 if (Pred.getSUnit() == SUJ)
1858 if ((Pred.getLatency() == 0 && Pred.isAssignedRegDep()) ||
1859 HII->isNewValueJump(I) || HII->isToBeScheduledASAP(*J, I))
1860 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001861 }
1862
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001863 // Check if the latency is greater than one between this instruction and any
1864 // instruction in the previous packet.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001865 for (auto J : OldPacketMIs) {
1866 SUnit *SUJ = MIToSUnit[J];
1867 for (auto &Pred : SUI->Preds)
1868 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1869 return true;
1870 }
1871
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001872 return false;
1873}
1874
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001875//===----------------------------------------------------------------------===//
1876// Public Constructor Functions
1877//===----------------------------------------------------------------------===//
1878
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +00001879FunctionPass *llvm::createHexagonPacketizer(bool Minimal) {
1880 return new HexagonPacketizer(Minimal);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001881}