blob: f43db53950fd3ce8459d72ddc07001be5c134c15 [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"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000036#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000037#include "llvm/CodeGen/MachineOperand.h"
38#include "llvm/CodeGen/ScheduleDAG.h"
39#include "llvm/IR/DebugLoc.h"
40#include "llvm/MC/MCInstrDesc.h"
41#include "llvm/Pass.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000042#include "llvm/Support/CommandLine.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000043#include "llvm/Support/Debug.h"
Eugene Zelenko3b873362017-09-28 22:27:31 +000044#include "llvm/Support/ErrorHandling.h"
45#include "llvm/Support/raw_ostream.h"
46#include "llvm/Target/TargetRegisterInfo.h"
47#include "llvm/Target/TargetSubtargetInfo.h"
48#include <cassert>
49#include <cstdint>
50#include <iterator>
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000051
52using namespace llvm;
53
Chandler Carruth84e68b22014-04-22 02:41:26 +000054#define DEBUG_TYPE "packets"
55
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000056static cl::opt<bool> DisablePacketizer("disable-packetizer", cl::Hidden,
57 cl::ZeroOrMore, cl::init(false),
58 cl::desc("Disable Hexagon packetizer pass"));
59
Jyotsna Verma1d297502013-05-02 15:39:30 +000060static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000061 cl::ZeroOrMore, cl::Hidden, cl::init(true),
62 cl::desc("Allow non-solo packetization of volatile memory references"));
63
64static cl::opt<bool> EnableGenAllInsnClass("enable-gen-insn", cl::init(false),
65 cl::Hidden, cl::ZeroOrMore, cl::desc("Generate all instruction with TC"));
66
67static cl::opt<bool> DisableVecDblNVStores("disable-vecdbl-nv-stores",
68 cl::init(false), cl::Hidden, cl::ZeroOrMore,
69 cl::desc("Disable vector double new-value-stores"));
70
71extern cl::opt<bool> ScheduleInlineAsm;
Jyotsna Verma1d297502013-05-02 15:39:30 +000072
Jyotsna Verma1d297502013-05-02 15:39:30 +000073namespace llvm {
Jyotsna Verma1d297502013-05-02 15:39:30 +000074
Eugene Zelenko3b873362017-09-28 22:27:31 +000075FunctionPass *createHexagonPacketizer();
76void initializeHexagonPacketizerPass(PassRegistry&);
77
78} // end namespace llvm
Jyotsna Verma1d297502013-05-02 15:39:30 +000079
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000080namespace {
Eugene Zelenko3b873362017-09-28 22:27:31 +000081
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000082 class HexagonPacketizer : public MachineFunctionPass {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000083 public:
84 static char ID;
Eugene Zelenko3b873362017-09-28 22:27:31 +000085
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +000086 HexagonPacketizer() : MachineFunctionPass(ID) {}
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000087
Craig Topper906c2cd2014-04-29 07:58:16 +000088 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000089 AU.setPreservesCFG();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000090 AU.addRequired<AAResultsWrapperPass>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000091 AU.addRequired<MachineBranchProbabilityInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000092 AU.addRequired<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000093 AU.addRequired<MachineLoopInfo>();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +000094 AU.addPreserved<MachineDominatorTree>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000095 AU.addPreserved<MachineLoopInfo>();
96 MachineFunctionPass::getAnalysisUsage(AU);
97 }
Eugene Zelenko3b873362017-09-28 22:27:31 +000098
Mehdi Amini117296c2016-10-01 02:56:57 +000099 StringRef getPassName() const override { return "Hexagon Packetizer"; }
Craig Topper906c2cd2014-04-29 07:58:16 +0000100 bool runOnMachineFunction(MachineFunction &Fn) override;
Eugene Zelenko3b873362017-09-28 22:27:31 +0000101
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000102 MachineFunctionProperties getRequiredProperties() const override {
103 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000104 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000105 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000106
107 private:
108 const HexagonInstrInfo *HII;
109 const HexagonRegisterInfo *HRI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000110 };
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000111
Eugene Zelenko3b873362017-09-28 22:27:31 +0000112} // end anonymous namespace
113
114char HexagonPacketizer::ID = 0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000115
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000116INITIALIZE_PASS_BEGIN(HexagonPacketizer, "hexagon-packetizer",
117 "Hexagon Packetizer", false, false)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000118INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
119INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
120INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000121INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000122INITIALIZE_PASS_END(HexagonPacketizer, "hexagon-packetizer",
123 "Hexagon Packetizer", false, false)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000124
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000125HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
126 MachineLoopInfo &MLI, AliasAnalysis *AA,
127 const MachineBranchProbabilityInfo *MBPI)
128 : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI) {
129 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
130 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
Krzysztof Parzyszek9be66732016-07-15 17:48:09 +0000131
Eugene Zelenko3b873362017-09-28 22:27:31 +0000132 addMutation(llvm::make_unique<HexagonSubtarget::UsrOverflowMutation>());
133 addMutation(llvm::make_unique<HexagonSubtarget::HVXMemLatencyMutation>());
134 addMutation(llvm::make_unique<HexagonSubtarget::BankConflictMutation>());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000135}
136
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000137// Check if FirstI modifies a register that SecondI reads.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000138static bool hasWriteToReadDep(const MachineInstr &FirstI,
139 const MachineInstr &SecondI,
140 const TargetRegisterInfo *TRI) {
141 for (auto &MO : FirstI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000142 if (!MO.isReg() || !MO.isDef())
143 continue;
144 unsigned R = MO.getReg();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000145 if (SecondI.readsRegister(R, TRI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000146 return true;
147 }
148 return false;
149}
150
151
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000152static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000153 MachineBasicBlock::iterator BundleIt, bool Before) {
154 MachineBasicBlock::instr_iterator InsertPt;
155 if (Before)
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000156 InsertPt = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000157 else
Duncan P. N. Exon Smithd84f6002016-02-22 21:30:15 +0000158 InsertPt = std::next(BundleIt).getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000159
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000160 MachineBasicBlock &B = *MI.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000161 // The instruction should at least be bundled with the preceding instruction
162 // (there will always be one, i.e. BUNDLE, if nothing else).
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000163 assert(MI.isBundledWithPred());
164 if (MI.isBundledWithSucc()) {
165 MI.clearFlag(MachineInstr::BundledSucc);
166 MI.clearFlag(MachineInstr::BundledPred);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000167 } else {
168 // If it's not bundled with the successor (i.e. it is the last one
169 // in the bundle), then we can simply unbundle it from the predecessor,
170 // which will take care of updating the predecessor's flag.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000171 MI.unbundleFromPred();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000172 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000173 B.splice(InsertPt, &B, MI.getIterator());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000174
175 // Get the size of the bundle without asserting.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000176 MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000177 MachineBasicBlock::const_instr_iterator E = B.instr_end();
178 unsigned Size = 0;
179 for (++I; I != E && I->isBundledWithPred(); ++I)
180 ++Size;
181
182 // If there are still two or more instructions, then there is nothing
183 // else to be done.
184 if (Size > 1)
185 return BundleIt;
186
187 // Otherwise, extract the single instruction out and delete the bundle.
188 MachineBasicBlock::iterator NextIt = std::next(BundleIt);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000189 MachineInstr &SingleI = *BundleIt->getNextNode();
190 SingleI.unbundleFromPred();
191 assert(!SingleI.isBundledWithSucc());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000192 BundleIt->eraseFromParent();
193 return NextIt;
194}
195
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000196bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
Andrew Kaylor5b444a22016-04-26 19:46:28 +0000197 if (DisablePacketizer || skipFunction(*MF.getFunction()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000198 return false;
199
200 HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
201 HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
202 auto &MLI = getAnalysis<MachineLoopInfo>();
203 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
204 auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
205
206 if (EnableGenAllInsnClass)
207 HII->genAllInsnTimingClasses(MF);
208
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000209 // Instantiate the packetizer.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000210 HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000211
212 // DFA state table should not be empty.
213 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
214
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000215 // Loop over all basic blocks and remove KILL pseudo-instructions
216 // These instructions confuse the dependence analysis. Consider:
217 // D0 = ... (Insn 0)
218 // R0 = KILL R0, D0 (Insn 1)
219 // R0 = ... (Insn 2)
220 // Here, Insn 1 will result in the dependence graph not emitting an output
221 // dependence between Insn 0 and Insn 2. This can lead to incorrect
222 // packetization
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000223 for (auto &MB : MF) {
224 auto End = MB.end();
225 auto MI = MB.begin();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000226 while (MI != End) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000227 auto NextI = std::next(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000228 if (MI->isKill()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000229 MB.erase(MI);
230 End = MB.end();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000231 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000232 MI = NextI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000233 }
234 }
235
236 // Loop over all of the basic blocks.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000237 for (auto &MB : MF) {
238 auto Begin = MB.begin(), End = MB.end();
239 while (Begin != End) {
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000240 // Find the first non-boundary starting from the end of the last
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000241 // scheduling region.
242 MachineBasicBlock::iterator RB = Begin;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000243 while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000244 ++RB;
Krzysztof Parzyszeke3ec97b2017-05-24 13:43:42 +0000245 // Find the first boundary starting from the beginning of the new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000246 // region.
247 MachineBasicBlock::iterator RE = RB;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000248 while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000249 ++RE;
250 // Add the scheduling boundary if it's not block end.
251 if (RE != End)
252 ++RE;
253 // If RB == End, then RE == End.
254 if (RB != End)
255 Packetizer.PacketizeMIs(&MB, RB, RE);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000256
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000257 Begin = RE;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000258 }
259 }
260
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000261 Packetizer.unpacketizeSoloInstrs(MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000262 return true;
263}
264
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000265// Reserve resources for a constant extender. Trigger an assertion if the
266// reservation fails.
267void HexagonPacketizerList::reserveResourcesForConstExt() {
268 if (!tryAllocateResourcesForConstExt(true))
269 llvm_unreachable("Resources not available");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000270}
271
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000272bool HexagonPacketizerList::canReserveResourcesForConstExt() {
273 return tryAllocateResourcesForConstExt(false);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000274}
275
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000276// Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
277// return true, otherwise, return false.
278bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
279 auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000280 bool Avail = ResourceTracker->canReserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000281 if (Reserve && Avail)
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000282 ResourceTracker->reserveResources(*ExtMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000283 MF.DeleteMachineInstr(ExtMI);
284 return Avail;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000285}
286
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000287bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000288 SDep::Kind DepType, unsigned DepReg) {
289 // Check for LR dependence.
290 if (DepReg == HRI->getRARegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000291 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000292
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000293 if (HII->isDeallocRet(MI))
294 if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000295 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000296
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000297 // Call-like instructions can be packetized with preceding instructions
298 // that define registers implicitly used or modified by the call. Explicit
299 // uses are still prohibited, as in the case of indirect calls:
300 // r0 = ...
301 // J2_jumpr r0
302 if (DepType == SDep::Data) {
303 for (const MachineOperand MO : MI.operands())
304 if (MO.isReg() && MO.getReg() == DepReg && !MO.isImplicit())
305 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000306 }
307
308 return false;
309}
310
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000311static bool isRegDependence(const SDep::Kind DepType) {
312 return DepType == SDep::Data || DepType == SDep::Anti ||
313 DepType == SDep::Output;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000314}
315
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000316static bool isDirectJump(const MachineInstr &MI) {
317 return MI.getOpcode() == Hexagon::J2_jump;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000318}
319
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000320static bool isSchedBarrier(const MachineInstr &MI) {
321 switch (MI.getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000322 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000323 return true;
324 }
325 return false;
326}
327
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000328static bool isControlFlow(const MachineInstr &MI) {
329 return MI.getDesc().isTerminator() || MI.getDesc().isCall();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000330}
331
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000332/// Returns true if the instruction modifies a callee-saved register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000333static bool doesModifyCalleeSavedReg(const MachineInstr &MI,
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000334 const TargetRegisterInfo *TRI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000335 const MachineFunction &MF = *MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000336 for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000337 if (MI.modifiesRegister(*CSR, TRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000338 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000339 return false;
340}
341
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000342// Returns true if an instruction can be promoted to .new predicate or
343// new-value store.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000344bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI,
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000345 const TargetRegisterClass *NewRC) {
346 // Vector stores can be predicated, and can be new-value stores, but
347 // they cannot be predicated on a .new predicate value.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000348 if (NewRC == &Hexagon::PredRegsRegClass) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000349 if (HII->isHVXVec(MI) && MI.mayStore())
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000350 return false;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000351 return HII->isPredicated(MI) && HII->getDotNewPredOp(MI, nullptr) > 0;
352 }
353 // If the class is not PredRegs, it could only apply to new-value stores.
354 return HII->mayBeNewStore(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000355}
356
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000357// Promote an instructiont to its .cur form.
358// At this time, we have already made a call to canPromoteToDotCur and made
359// sure that it can *indeed* be promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000360bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000361 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
362 const TargetRegisterClass* RC) {
363 assert(DepType == SDep::Data);
364 int CurOpcode = HII->getDotCurOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000365 MI.setDesc(HII->get(CurOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000366 return true;
367}
368
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000369void HexagonPacketizerList::cleanUpDotCur() {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000370 MachineInstr *MI = nullptr;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000371 for (auto BI : CurrentPacketMIs) {
372 DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000373 if (HII->isDotCurInst(*BI)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000374 MI = BI;
375 continue;
376 }
377 if (MI) {
378 for (auto &MO : BI->operands())
379 if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
380 return;
381 }
382 }
383 if (!MI)
384 return;
385 // We did not find a use of the CUR, so de-cur it.
Krzysztof Parzyszek0a8043e2017-05-03 15:28:56 +0000386 MI->setDesc(HII->get(HII->getNonDotCurOp(*MI)));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000387 DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
388}
389
390// Check to see if an instruction can be dot cur.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000391bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000392 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
393 const TargetRegisterClass *RC) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000394 if (!HII->isHVXVec(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000395 return false;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +0000396 if (!HII->isHVXVec(*MII))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000397 return false;
398
399 // Already a dot new instruction.
400 if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
401 return false;
402
403 if (!HII->mayBeCurLoad(MI))
404 return false;
405
406 // The "cur value" cannot come from inline asm.
407 if (PacketSU->getInstr()->isInlineAsm())
408 return false;
409
410 // Make sure candidate instruction uses cur.
411 DEBUG(dbgs() << "Can we DOT Cur Vector MI\n";
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000412 MI.dump();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000413 dbgs() << "in packet\n";);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000414 MachineInstr &MJ = *MII;
415 DEBUG({
416 dbgs() << "Checking CUR against ";
417 MJ.dump();
418 });
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000419 unsigned DestReg = MI.getOperand(0).getReg();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000420 bool FoundMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000421 for (auto &MO : MJ.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000422 if (MO.isReg() && MO.getReg() == DestReg)
423 FoundMatch = true;
424 if (!FoundMatch)
425 return false;
426
427 // Check for existing uses of a vector register within the packet which
428 // would be affected by converting a vector load into .cur formt.
429 for (auto BI : CurrentPacketMIs) {
430 DEBUG(dbgs() << "packet has "; BI->dump(););
431 if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
432 return false;
433 }
434
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000435 DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump(););
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000436 // We can convert the opcode into a .cur.
437 return true;
438}
439
440// Promote an instruction to its .new form. At this time, we have already
441// made a call to canPromoteToDotNew and made sure that it can *indeed* be
442// promoted.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000443bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000444 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
445 const TargetRegisterClass* RC) {
Eugene Zelenko3b873362017-09-28 22:27:31 +0000446 assert(DepType == SDep::Data);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000447 int NewOpcode;
448 if (RC == &Hexagon::PredRegsRegClass)
449 NewOpcode = HII->getDotNewPredOp(MI, MBPI);
450 else
451 NewOpcode = HII->getDotNewOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000452 MI.setDesc(HII->get(NewOpcode));
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000453 return true;
454}
455
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000456bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) {
Krzysztof Parzyszek143158b2017-03-06 17:03:16 +0000457 int NewOpcode = HII->getDotOldOp(MI);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000458 MI.setDesc(HII->get(NewOpcode));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000459 return true;
460}
461
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000462bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) {
463 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000464 switch (Opc) {
465 case Hexagon::S2_storerd_io:
466 case Hexagon::S2_storeri_io:
467 case Hexagon::S2_storerh_io:
468 case Hexagon::S2_storerb_io:
469 break;
470 default:
471 llvm_unreachable("Unexpected instruction");
472 }
Matthias Braun941a7052016-07-28 18:40:00 +0000473 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000474 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000475 int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000476 if (HII->isValidOffset(Opc, NewOff, HRI)) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000477 Off.setImm(NewOff);
478 return true;
479 }
480 return false;
481}
482
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000483void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) {
484 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000485 switch (Opc) {
486 case Hexagon::S2_storerd_io:
487 case Hexagon::S2_storeri_io:
488 case Hexagon::S2_storerh_io:
489 case Hexagon::S2_storerb_io:
490 break;
491 default:
492 llvm_unreachable("Unexpected instruction");
493 }
Matthias Braun941a7052016-07-28 18:40:00 +0000494 unsigned FrameSize = MF.getFrameInfo().getStackSize();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000495 MachineOperand &Off = MI.getOperand(1);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +0000496 Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
497}
498
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +0000499/// Return true if we can update the offset in MI so that MI and MJ
500/// can be packetized together.
501bool HexagonPacketizerList::updateOffset(SUnit *SUI, SUnit *SUJ) {
502 assert(SUI->getInstr() && SUJ->getInstr());
503 MachineInstr &MI = *SUI->getInstr();
504 MachineInstr &MJ = *SUJ->getInstr();
505
506 unsigned BPI, OPI;
507 if (!HII->getBaseAndOffsetPosition(MI, BPI, OPI))
508 return false;
509 unsigned BPJ, OPJ;
510 if (!HII->getBaseAndOffsetPosition(MJ, BPJ, OPJ))
511 return false;
512 unsigned Reg = MI.getOperand(BPI).getReg();
513 if (Reg != MJ.getOperand(BPJ).getReg())
514 return false;
515 // Make sure that the dependences do not restrict adding MI to the packet.
516 // That is, ignore anti dependences, and make sure the only data dependence
517 // involves the specific register.
518 for (const auto &PI : SUI->Preds)
519 if (PI.getKind() != SDep::Anti &&
520 (PI.getKind() != SDep::Data || PI.getReg() != Reg))
521 return false;
522 int Incr;
523 if (!HII->getIncrementValue(MJ, Incr))
524 return false;
525
526 int64_t Offset = MI.getOperand(OPI).getImm();
527 MI.getOperand(OPI).setImm(Offset + Incr);
528 ChangedOffset = Offset;
529 return true;
530}
531
532/// Undo the changed offset. This is needed if the instruction cannot be
533/// added to the current packet due to a different instruction.
534void HexagonPacketizerList::undoChangedOffset(MachineInstr &MI) {
535 unsigned BP, OP;
536 if (!HII->getBaseAndOffsetPosition(MI, BP, OP))
537 llvm_unreachable("Unable to find base and offset operands.");
538 MI.getOperand(OP).setImm(ChangedOffset);
539}
540
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000541enum PredicateKind {
542 PK_False,
543 PK_True,
544 PK_Unknown
545};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000546
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000547/// Returns true if an instruction is predicated on p0 and false if it's
548/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000549static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000550 const HexagonInstrInfo *HII) {
551 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000552 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000553 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000554 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000555 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000556}
557
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000558static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000559 const HexagonInstrInfo *HII) {
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000560 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000561#ifndef NDEBUG
562 // Post Increment means duplicates. Use dense map to find duplicates in the
563 // list. Caution: Densemap initializes with the minimum of 64 buckets,
564 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000565 DenseSet<unsigned> DefRegsSet;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000566 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000567 if (MO.isReg() && MO.isDef())
568 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000569
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000570 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000571 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
572 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000573#else
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000574 if (MI.mayLoad()) {
575 const MachineOperand &Op1 = MI.getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000576 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000577 assert(Op1.isReg() && "Post increment operand has be to a register.");
578 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000579 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000580 if (MI.getDesc().mayStore()) {
581 const MachineOperand &Op0 = MI.getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000582 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000583 assert(Op0.isReg() && "Post increment operand has be to a register.");
584 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000585 }
586#endif
587 // we should never come here.
588 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
589}
590
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000591// Get the value being stored.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000592static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000593 // value being stored is always the last operand.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000594 return MI.getOperand(MI.getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000595}
596
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000597static bool isLoadAbsSet(const MachineInstr &MI) {
598 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000599 switch (Opc) {
600 case Hexagon::L4_loadrd_ap:
601 case Hexagon::L4_loadrb_ap:
602 case Hexagon::L4_loadrh_ap:
603 case Hexagon::L4_loadrub_ap:
604 case Hexagon::L4_loadruh_ap:
605 case Hexagon::L4_loadri_ap:
606 return true;
607 }
608 return false;
609}
610
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000611static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000612 assert(isLoadAbsSet(MI));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000613 return MI.getOperand(1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000614}
615
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000616// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000617// Following restrictions are to be respected in convert a store into
618// a new value store.
619// 1. If an instruction uses auto-increment, its address register cannot
620// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000621// 2. If an instruction uses absolute-set addressing mode, its address
622// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000623// 3. If an instruction produces a 64-bit result, its registers cannot be used
624// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000625// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000626// the instruction that uses the new-value register must also be conditional,
627// and both must always have their predicates evaluate identically.
628// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000629// 5. There is an implied restriction that a packet cannot have another store,
630// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000631// already a store in a packet, there can not be a new value store.
632// Arch Spec: 3.4.4.2
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000633bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
634 const MachineInstr &PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000635 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000636 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000637 return false;
638
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000639 // Make sure there is dependency and can be new value'd.
640 const MachineOperand &Val = getStoreValueOperand(MI);
641 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000642 return false;
643
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000644 const MCInstrDesc& MCID = PacketMI.getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000645
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000646 // First operand is always the result.
647 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
648 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
649 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000650 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000651
652 // New-value stores are of class NV (slot 0), dual stores require class ST
653 // in slot 0 (PRM 5.5).
654 for (auto I : CurrentPacketMIs) {
655 SUnit *PacketSU = MIToSUnit.find(I)->second;
656 if (PacketSU->getInstr()->mayStore())
657 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000658 }
659
660 // Make sure it's NOT the post increment register that we are going to
661 // new value.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000662 if (HII->isPostIncrement(MI) &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000663 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000664 return false;
665 }
666
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000667 if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000668 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
669 // If source is post_inc, or absolute-set addressing, it can not feed
670 // into new value store
671 // r3 = memw(r2++#4)
672 // memw(r30 + #-1404) = r2.new -> can not be new value store
673 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000674 return false;
675 }
676
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000677 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
678 return false;
679
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000680 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000681 // also be predicated.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000682 if (HII->isPredicated(PacketMI)) {
683 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000684 return false;
685
686 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000687 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000688 unsigned predRegNumSrc = 0;
689 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000690 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000691
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000692 // Get predicate register used in the source instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000693 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000694 if (!MO.isReg())
695 continue;
696 predRegNumSrc = MO.getReg();
697 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
698 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000699 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000700 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000701 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
702 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000703
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000704 // Get predicate register used in new-value store instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000705 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000706 if (!MO.isReg())
707 continue;
708 predRegNumDst = MO.getReg();
709 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
710 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000711 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000712 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000713 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
714 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000715
716 // New-value register producer and user (store) need to satisfy these
717 // constraints:
718 // 1) Both instructions should be predicated on the same register.
719 // 2) If producer of the new-value register is .new predicated then store
720 // should also be .new predicated and if producer is not .new predicated
721 // then store should not be .new predicated.
722 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000723 // sense, i.e, either both should be negated or both should be non-negated.
724 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000725 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000726 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000727 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000728 }
729
730 // Make sure that other than the new-value register no other store instruction
731 // register has been modified in the same packet. Predicate registers can be
732 // modified by they should not be modified between the producer and the store
733 // instruction as it will make them both conditional on different values.
734 // We already know this to be true for all the instructions before and
735 // including PacketMI. Howerver, we need to perform the check for the
736 // remaining instructions in the packet.
737
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000738 unsigned StartCheck = 0;
739
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000740 for (auto I : CurrentPacketMIs) {
741 SUnit *TempSU = MIToSUnit.find(I)->second;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000742 MachineInstr &TempMI = *TempSU->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000743
744 // Following condition is true for all the instructions until PacketMI is
745 // reached (StartCheck is set to 0 before the for loop).
746 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000747 if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
748 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000749
750 StartCheck = 1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000751 if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000752 continue;
753
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000754 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000755 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000756 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000757 }
758
Alp Tokerf907b892013-12-05 05:44:44 +0000759 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000760 // 1. The only use of reg is DepReg and no other registers.
761 // This handles V4 base+index registers.
762 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000763 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000764 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000765 if (!HII->isPostIncrement(MI)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000766 for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
767 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000768 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000769 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000770 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000771 }
772
773 // If data definition is because of implicit definition of the register,
774 // do not newify the store. Eg.
775 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
776 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000777 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000778 if (MO.isRegMask() && MO.clobbersPhysReg(DepReg))
779 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000780 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
781 continue;
782 unsigned R = MO.getReg();
783 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
784 return false;
785 }
786
787 // Handle imp-use of super reg case. There is a target independent side
788 // change that should prevent this situation but I am handling it for
789 // just-in-case. For example, we cannot newify R2 in the following case:
790 // %R3<def> = A2_tfrsi 0;
791 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000792 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000793 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
794 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000795 }
796
797 // Can be dot new store.
798 return true;
799}
800
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000801// Can this MI to promoted to either new value store or new value jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000802bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000803 const SUnit *PacketSU, unsigned DepReg,
804 MachineBasicBlock::iterator &MII) {
805 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000806 return false;
807
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000808 // Check to see the store can be new value'ed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000809 MachineInstr &PacketMI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000810 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000811 return true;
812
813 // Check to see the compare/jump can be new value'ed.
814 // This is done as a pass on its own. Don't need to check it here.
815 return false;
816}
817
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000818static bool isImplicitDependency(const MachineInstr &I, bool CheckDef,
819 unsigned DepReg) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000820 for (auto &MO : I.operands()) {
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000821 if (CheckDef && MO.isRegMask() && MO.clobbersPhysReg(DepReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000822 return true;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000823 if (!MO.isReg() || MO.getReg() != DepReg || !MO.isImplicit())
824 continue;
825 if (CheckDef == MO.isDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000826 return true;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000827 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000828 return false;
829}
830
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000831// Check to see if an instruction can be dot new
832// There are three kinds.
833// 1. dot new on predicate - V2/V3/V4
834// 2. dot new on stores NV/ST - V4
835// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000836bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000837 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
838 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000839 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000840 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000841 return false;
842
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000843 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000844 return false;
845
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000846 const MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000847
848 // The "new value" cannot come from inline asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000849 if (PI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000850 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000851
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000852 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
853 // sense.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000854 if (PI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000855 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000856
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000857 // If dependency is trough an implicitly defined register, we should not
858 // newify the use.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000859 if (isImplicitDependency(PI, true, DepReg) ||
860 isImplicitDependency(MI, false, DepReg))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000861 return false;
862
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000863 const MCInstrDesc& MCID = PI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000864 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000865 if (DisableVecDblNVStores && VecRC == &Hexagon::HvxWRRegClass)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000866 return false;
867
868 // predicate .new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000869 if (RC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000870 return HII->predCanBeUsedAsDotNew(PI, DepReg);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000871
872 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
873 return false;
874
875 // Create a dot new machine instruction to see if resources can be
876 // allocated. If not, bail out now.
877 int NewOpcode = HII->getDotNewOp(MI);
878 const MCInstrDesc &D = HII->get(NewOpcode);
879 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000880 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000881 MF.DeleteMachineInstr(NewMI);
882 if (!ResourcesAvailable)
883 return false;
884
885 // New Value Store only. New Value Jump generated as a separate pass.
886 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
887 return false;
888
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000889 return true;
890}
891
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000892// Go through the packet instructions and search for an anti dependency between
893// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000894// Trying to add
895// a) %R1<def> = TFRI_cdNotPt %P3, 2
896// to this packet:
897// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000898// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
899// c) %P3<def> = C2_tfrrp %R23
900// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000901// }
902// The P3 from a) and d) will be complements after
903// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000904// Anti-dep between c) and b) is irrelevant for this case
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000905bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000906 unsigned DepReg) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000907 SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000908
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000909 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000910 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000911 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000912 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000913
914 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000915 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000916
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000917 // Look at dependencies between current members of the packet and
918 // predicate defining instruction MI. Make sure that dependency is
919 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000920 if (PacketSU->isSucc(PacketSUDep)) {
921 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000922 auto &Dep = PacketSU->Succs[i];
923 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
924 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000925 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000926 }
927 }
928 }
929
930 return false;
931}
932
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000933/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000934static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000935 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000936 /// We use the following rule: The first predicate register that is a use is
937 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000938 assert(QII->isPredicated(MI) && "Must be predicated instruction");
939
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000940 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000941 if (Op.isReg() && Op.getReg() && Op.isUse() &&
942 Hexagon::PredRegsRegClass.contains(Op.getReg()))
943 return Op.getReg();
944 }
945
946 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000947 return 0;
948}
949
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000950// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000951// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000952bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
953 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000954 // If we don't know the predicate sense of the instructions bail out early, we
955 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000956 if (getPredicateSense(MI1, HII) == PK_Unknown ||
957 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000958 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000959
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000960 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000961 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000962
963 // One corner case deals with the following scenario:
964 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000965 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000966 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000967 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000968 // b) %R25<def> = A2_tfrf %P0, %R24
969 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000970 // }
971 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000972 // On general check a) and b) are complements, but presence of c) will
973 // convert a) to .new form, and then it is not a complement.
974 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000975
976 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000977 // Look for Anti dependecy on the same predicate reg as used in the
978 // candidate.
979 for (auto I : CurrentPacketMIs) {
980 // Scheduling Unit for current insn in the packet.
981 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000982
983 // If this instruction in the packet is succeeded by the candidate...
984 if (PacketSU->isSucc(SU)) {
985 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000986 auto Dep = PacketSU->Succs[i];
987 // The corner case exist when there is true data dependency between
988 // candidate and one of current packet members, this dep is on
989 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000990 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000991 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
992 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
993 // Here I know that I is predicate setting instruction with true
994 // data dep to candidate on the register we care about - c) in the
995 // above example. Now I need to see if there is an anti dependency
996 // from c) to any other instruction in the same packet on the pred
997 // reg of interest.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000998 if (restrictingDepExistInPacket(*I, Dep.getReg()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000999 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001000 }
1001 }
1002 }
1003 }
1004
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001005 // If the above case does not apply, check regular complement condition.
1006 // Check that the predicate register is the same and that the predicate
1007 // sense is different We also need to differentiate .old vs. .new: !p0
1008 // is not complementary to p0.new.
1009 unsigned PReg1 = getPredicatedRegister(MI1, HII);
1010 unsigned PReg2 = getPredicatedRegister(MI2, HII);
1011 return PReg1 == PReg2 &&
1012 Hexagon::PredRegsRegClass.contains(PReg1) &&
1013 Hexagon::PredRegsRegClass.contains(PReg2) &&
1014 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001015 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001016}
1017
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001018// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001019void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001020 Dependence = false;
1021 PromotedToDotNew = false;
1022 GlueToNewValueJump = false;
1023 GlueAllocframeStore = false;
1024 FoundSequentialDependence = false;
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00001025 ChangedOffset = INT64_MAX;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001026}
1027
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001028// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001029bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
1030 const MachineBasicBlock *) {
1031 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001032 return true;
1033
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001034 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +00001035 return false;
1036
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001037 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001038 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001039 return false;
1040
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001041 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001042 return false;
1043
1044 // We check if MI has any functional units mapped to it. If it doesn't,
1045 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001046 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001047 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +00001048 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001049 return !FuncUnits;
1050}
1051
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001052bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
1053 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001054 return true;
1055
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001056 // Consider inline asm to not be a solo instruction by default.
1057 // Inline asm will be put in a packet temporarily, but then it will be
1058 // removed, and placed outside of the packet (before or after, depending
1059 // on dependencies). This is to reduce the impact of inline asm as a
1060 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001061 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001062 return true;
1063
1064 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
1065 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
1066 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001067 if (isSchedBarrier(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001068 return true;
1069
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001070 if (HII->isSolo(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001071 return true;
1072
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001073 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001074 return true;
1075
1076 return false;
1077}
1078
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001079// Quick check if instructions MI and MJ cannot coexist in the same packet.
1080// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1081// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1082// For full test call this function twice:
1083// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1084// Doing the test only one way saves the amount of code in this function,
1085// since every test would need to be repeated with the MI and MJ reversed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001086static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001087 const HexagonInstrInfo &HII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001088 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001089 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1090 HII.isHVXMemWithAIndirect(MI, MJ))
1091 return true;
1092
1093 // An inline asm cannot be together with a branch, because we may not be
1094 // able to remove the asm out after packetizing (i.e. if the asm must be
1095 // moved past the bundle). Similarly, two asms cannot be together to avoid
1096 // complications when determining their relative order outside of a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001097 if (MI.isInlineAsm())
1098 return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
1099 MJ.isCall() || MJ.isTerminator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001100
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001101 switch (MI.getOpcode()) {
1102 case (Hexagon::S2_storew_locked):
1103 case (Hexagon::S4_stored_locked):
1104 case (Hexagon::L2_loadw_locked):
1105 case (Hexagon::L4_loadd_locked):
1106 case (Hexagon::Y4_l2fetch): {
1107 // These instructions can only be grouped with ALU32 or non-floating-point
1108 // XTYPE instructions. Since there is no convenient way of identifying fp
1109 // XTYPE instructions, only allow grouping with ALU32 for now.
1110 unsigned TJ = HII.getType(MJ);
Krzysztof Parzyszek5ea971c2017-02-07 17:47:37 +00001111 if (TJ != HexagonII::TypeALU32_2op &&
1112 TJ != HexagonII::TypeALU32_3op &&
1113 TJ != HexagonII::TypeALU32_ADDI)
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001114 return true;
1115 break;
1116 }
1117 default:
1118 break;
1119 }
1120
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001121 // "False" really means that the quick check failed to determine if
1122 // I and J cannot coexist.
1123 return false;
1124}
1125
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001126// Full, symmetric check.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001127bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
1128 const MachineInstr &MJ) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001129 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1130}
1131
1132void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1133 for (auto &B : MF) {
1134 MachineBasicBlock::iterator BundleIt;
1135 MachineBasicBlock::instr_iterator NextI;
1136 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1137 NextI = std::next(I);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001138 MachineInstr &MI = *I;
1139 if (MI.isBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001140 BundleIt = I;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001141 if (!MI.isInsideBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001142 continue;
1143
1144 // Decide on where to insert the instruction that we are pulling out.
1145 // Debug instructions always go before the bundle, but the placement of
1146 // INLINE_ASM depends on potential dependencies. By default, try to
1147 // put it before the bundle, but if the asm writes to a register that
1148 // other instructions in the bundle read, then we need to place it
1149 // after the bundle (to preserve the bundle semantics).
1150 bool InsertBeforeBundle;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001151 if (MI.isInlineAsm())
1152 InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
1153 else if (MI.isDebugValue())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001154 InsertBeforeBundle = true;
1155 else
1156 continue;
1157
1158 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1159 }
1160 }
1161}
1162
1163// Check if a given instruction is of class "system".
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001164static bool isSystemInstr(const MachineInstr &MI) {
1165 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001166 switch (Opc) {
1167 case Hexagon::Y2_barrier:
1168 case Hexagon::Y2_dcfetchbo:
1169 return true;
1170 }
1171 return false;
1172}
1173
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001174bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
1175 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001176 // The dependence graph may not include edges between dead definitions,
1177 // so without extra checks, we could end up packetizing two instruction
1178 // defining the same (dead) register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001179 if (I.isCall() || J.isCall())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001180 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001181 if (HII->isPredicated(I) || HII->isPredicated(J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001182 return false;
1183
1184 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001185 for (auto &MO : I.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001186 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1187 continue;
1188 DeadDefs[MO.getReg()] = true;
1189 }
1190
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001191 for (auto &MO : J.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001192 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1193 continue;
1194 unsigned R = MO.getReg();
1195 if (R != Hexagon::USR_OVF && DeadDefs[R])
1196 return true;
1197 }
1198 return false;
1199}
1200
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001201bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
1202 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001203 // A save callee-save register function call can only be in a packet
1204 // with instructions that don't write to the callee-save registers.
1205 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1206 doesModifyCalleeSavedReg(J, HRI)) ||
1207 (HII->isSaveCalleeSavedRegsCall(J) &&
1208 doesModifyCalleeSavedReg(I, HRI)))
1209 return true;
1210
1211 // Two control flow instructions cannot go in the same packet.
1212 if (isControlFlow(I) && isControlFlow(J))
1213 return true;
1214
1215 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1216 // contain a speculative indirect jump,
1217 // a new-value compare jump or a dealloc_return.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001218 auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
1219 if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001220 return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001221 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001222 return true;
1223 return false;
1224 };
1225
1226 if (HII->isLoopN(I) && isBadForLoopN(J))
1227 return true;
1228 if (HII->isLoopN(J) && isBadForLoopN(I))
1229 return true;
1230
1231 // dealloc_return cannot appear in the same packet as a conditional or
1232 // unconditional jump.
1233 return HII->isDeallocRet(I) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001234 (J.isBranch() || J.isCall() || J.isBarrier());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001235}
1236
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001237bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I,
1238 const MachineInstr &J) {
1239 // Adding I to a packet that has J.
1240
1241 // Regmasks are not reflected in the scheduling dependency graph, so
1242 // we need to check them manually. This code assumes that regmasks only
1243 // occur on calls, and the problematic case is when we add an instruction
1244 // defining a register R to a packet that has a call that clobbers R via
1245 // a regmask. Those cannot be packetized together, because the call will
1246 // be executed last. That's also a reson why it is ok to add a call
1247 // clobbering R to a packet that defines R.
1248
1249 // Look for regmasks in J.
1250 for (const MachineOperand &OpJ : J.operands()) {
1251 if (!OpJ.isRegMask())
1252 continue;
1253 assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call");
1254 for (const MachineOperand &OpI : I.operands()) {
1255 if (OpI.isReg()) {
1256 if (OpJ.clobbersPhysReg(OpI.getReg()))
1257 return true;
1258 } else if (OpI.isRegMask()) {
1259 // Both are regmasks. Assume that they intersect.
1260 return true;
1261 }
1262 }
1263 }
1264 return false;
1265}
1266
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001267bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I,
1268 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001269 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001270 bool StoreI = I.mayStore(), StoreJ = J.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001271 if ((SysI && StoreJ) || (SysJ && StoreI))
1272 return true;
1273
1274 if (StoreI && StoreJ) {
1275 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1276 return true;
1277 } else {
1278 // A memop cannot be in the same packet with another memop or a store.
1279 // Two stores can be together, but here I and J cannot both be stores.
1280 bool MopStI = HII->isMemOp(I) || StoreI;
1281 bool MopStJ = HII->isMemOp(J) || StoreJ;
1282 if (MopStI && MopStJ)
1283 return true;
1284 }
1285
1286 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1287}
1288
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001289// SUI is the current instruction that is out side of the current packet.
1290// SUJ is the current instruction inside the current packet against which that
1291// SUI will be packetized.
1292bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001293 assert(SUI->getInstr() && SUJ->getInstr());
1294 MachineInstr &I = *SUI->getInstr();
1295 MachineInstr &J = *SUJ->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001296
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001297 // Clear IgnoreDepMIs when Packet starts.
1298 if (CurrentPacketMIs.size() == 1)
1299 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001300
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001301 MachineBasicBlock::iterator II = I.getIterator();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001302
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001303 // Solo instructions cannot go in the packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001304 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001305
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001306 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001307 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001308
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001309 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1310 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001311 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001312
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001313 // Regmasks are not accounted for in the scheduling graph, so we need
1314 // to explicitly check for dependencies caused by them. They should only
1315 // appear on calls, so it's not too pessimistic to reject all regmask
1316 // dependencies.
1317 Dependence = hasRegMaskDependence(I, J);
1318 if (Dependence)
1319 return false;
1320
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001321 // V4 allows dual stores. It does not allow second store, if the first
1322 // store is not in SLOT0. New value store, new value jump, dealloc_return
1323 // and memop always take SLOT0. Arch spec 3.4.4.2.
1324 Dependence = hasV4SpecificDependence(I, J);
1325 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001326 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001327
1328 // If an instruction feeds new value jump, glue it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001329 MachineBasicBlock::iterator NextMII = I.getIterator();
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001330 ++NextMII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001331 if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001332 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001333
1334 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001335 const MachineOperand &NOp0 = NextMI.getOperand(0);
1336 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001337
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001338 if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001339 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001340
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001341 for (MachineInstr *PI : CurrentPacketMIs) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001342 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001343 if (PI->isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001344 Dependence = true;
1345 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001346 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001347 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001348 // 1. Packet does not have a store in it.
1349 // 2. If the first operand of the nvj is newified, and the second
1350 // operand is also a reg, it (second reg) is not defined in
1351 // the same packet.
1352 // 3. If the second operand of the nvj is newified, (which means
1353 // first operand is also a reg), first reg is not defined in
1354 // the same packet.
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001355 if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() ||
1356 HII->isLoopN(*PI)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001357 Dependence = true;
1358 break;
1359 }
1360 // Check #2/#3.
1361 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001362 if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001363 Dependence = true;
1364 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001365 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001366 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001367
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001368 GlueToNewValueJump = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001369 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001370 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001371 }
1372
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001373 // There no dependency between a prolog instruction and its successor.
1374 if (!SUJ->isSucc(SUI))
1375 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001376
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001377 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1378 if (FoundSequentialDependence)
1379 break;
1380
1381 if (SUJ->Succs[i].getSUnit() != SUI)
1382 continue;
1383
1384 SDep::Kind DepType = SUJ->Succs[i].getKind();
1385 // For direct calls:
1386 // Ignore register dependences for call instructions for packetization
1387 // purposes except for those due to r31 and predicate registers.
1388 //
1389 // For indirect calls:
1390 // Same as direct calls + check for true dependences to the register
1391 // used in the indirect call.
1392 //
1393 // We completely ignore Order dependences for call instructions.
1394 //
1395 // For returns:
1396 // Ignore register dependences for return instructions like jumpr,
1397 // dealloc return unless we have dependencies on the explicit uses
1398 // of the registers used by jumpr (like r31) or dealloc return
1399 // (like r29 or r30).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001400 unsigned DepReg = 0;
1401 const TargetRegisterClass *RC = nullptr;
1402 if (DepType == SDep::Data) {
1403 DepReg = SUJ->Succs[i].getReg();
1404 RC = HRI->getMinimalPhysRegClass(DepReg);
1405 }
1406
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +00001407 if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001408 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001409 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001410 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1411 continue;
1412 }
1413
1414 if (DepType == SDep::Data) {
1415 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1416 if (promoteToDotCur(J, DepType, II, RC))
1417 continue;
1418 }
1419
1420 // Data dpendence ok if we have load.cur.
1421 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001422 if (HII->isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001423 continue;
1424 }
1425
1426 // For instructions that can be promoted to dot-new, try to promote.
1427 if (DepType == SDep::Data) {
1428 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1429 if (promoteToDotNew(I, DepType, II, RC)) {
1430 PromotedToDotNew = true;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001431 if (cannotCoexist(I, J))
1432 FoundSequentialDependence = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001433 continue;
1434 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001435 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001436 if (HII->isNewValueJump(I))
1437 continue;
1438 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001439
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001440 // For predicated instructions, if the predicates are complements then
1441 // there can be no dependence.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001442 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1443 arePredicatesComplements(I, J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001444 // Not always safe to do this translation.
1445 // DAG Builder attempts to reduce dependence edges using transitive
1446 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001447 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001448 // r0 = tfr_pt ... (1)
1449 // r0 = tfr_pf ... (2)
1450 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001451 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001452 // There will be an output dependence between (1)->(2) and (2)->(3).
1453 // However, there is no dependence edge between (1)->(3). This results
1454 // in all 3 instructions going in the same packet. We ignore dependce
1455 // only once to avoid this situation.
David Majnemer0d955d02016-08-11 22:21:41 +00001456 auto Itr = find(IgnoreDepMIs, &J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001457 if (Itr != IgnoreDepMIs.end()) {
1458 Dependence = true;
1459 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001460 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001461 IgnoreDepMIs.push_back(&I);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001462 continue;
1463 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001464
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001465 // Ignore Order dependences between unconditional direct branches
1466 // and non-control-flow instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001467 if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001468 DepType == SDep::Order)
1469 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001470
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001471 // Ignore all dependences for jumps except for true and output
1472 // dependences.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001473 if (I.isConditionalBranch() && DepType != SDep::Data &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001474 DepType != SDep::Output)
1475 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001476
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001477 if (DepType == SDep::Output) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001478 FoundSequentialDependence = true;
1479 break;
1480 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001481
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001482 // For Order dependences:
1483 // 1. On V4 or later, volatile loads/stores can be packetized together,
1484 // unless other rules prevent is.
1485 // 2. Store followed by a load is not allowed.
1486 // 3. Store followed by a store is only valid on V4 or later.
1487 // 4. Load followed by any memory operation is allowed.
1488 if (DepType == SDep::Order) {
1489 if (!PacketizeVolatiles) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001490 bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001491 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001492 FoundSequentialDependence = true;
1493 break;
1494 }
1495 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001496 // J is first, I is second.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001497 bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
1498 bool LoadI = I.mayLoad(), StoreI = I.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001499 if (StoreJ) {
1500 // Two stores are only allowed on V4+. Load following store is never
1501 // allowed.
Krzysztof Parzyszek9d19c8c2017-10-20 22:08:40 +00001502 if (LoadI && alias(J, I)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001503 FoundSequentialDependence = true;
1504 break;
1505 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001506 } else if (!LoadJ || (!LoadI && !StoreI)) {
1507 // If J is neither load nor store, assume a dependency.
1508 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001509 FoundSequentialDependence = true;
1510 break;
1511 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001512 // Store followed by store: not OK on V2.
1513 // Store followed by load: not OK on all.
1514 // Load followed by store: OK on all.
1515 // Load followed by load: OK on all.
1516 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001517 }
1518
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001519 // For V4, special case ALLOCFRAME. Even though there is dependency
1520 // between ALLOCFRAME and subsequent store, allow it to be packetized
1521 // in a same packet. This implies that the store is using the caller's
1522 // SP. Hence, offset needs to be updated accordingly.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001523 if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
1524 unsigned Opc = I.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001525 switch (Opc) {
1526 case Hexagon::S2_storerd_io:
1527 case Hexagon::S2_storeri_io:
1528 case Hexagon::S2_storerh_io:
1529 case Hexagon::S2_storerb_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001530 if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001531 // Since this store is to be glued with allocframe in the same
1532 // packet, it will use SP of the previous stack frame, i.e.
1533 // caller's SP. Therefore, we need to recalculate offset
1534 // according to this change.
1535 GlueAllocframeStore = useCallersSP(I);
1536 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001537 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001538 }
1539 default:
1540 break;
1541 }
1542 }
1543
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001544 // There are certain anti-dependencies that cannot be ignored.
1545 // Specifically:
1546 // J2_call ... %R0<imp-def> ; SUJ
1547 // R0 = ... ; SUI
1548 // Those cannot be packetized together, since the call will observe
1549 // the effect of the assignment to R0.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001550 if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) {
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001551 // Check if I defines any volatile register. We should also check
1552 // registers that the call may read, but these happen to be a
1553 // subset of the volatile register set.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001554 for (const MachineOperand &Op : I.operands()) {
1555 if (Op.isReg() && Op.isDef()) {
1556 unsigned R = Op.getReg();
1557 if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI))
1558 continue;
1559 } else if (!Op.isRegMask()) {
1560 // If I has a regmask assume dependency.
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001561 continue;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001562 }
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001563 FoundSequentialDependence = true;
1564 break;
1565 }
1566 }
1567
1568 // Skip over remaining anti-dependences. Two instructions that are
1569 // anti-dependent can share a packet, since in most such cases all
1570 // operands are read before any modifications take place.
1571 // The exceptions are branch and call instructions, since they are
1572 // executed after all other instructions have completed (at least
1573 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001574 if (DepType != SDep::Anti) {
1575 FoundSequentialDependence = true;
1576 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001577 }
1578 }
1579
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001580 if (FoundSequentialDependence) {
1581 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001582 return false;
1583 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001584
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001585 return true;
1586}
1587
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001588bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001589 assert(SUI->getInstr() && SUJ->getInstr());
1590 MachineInstr &I = *SUI->getInstr();
1591 MachineInstr &J = *SUJ->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001592
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001593 bool Coexist = !cannotCoexist(I, J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001594
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001595 if (Coexist && !Dependence)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001596 return true;
1597
1598 // Check if the instruction was promoted to a dot-new. If so, demote it
1599 // back into a dot-old.
1600 if (PromotedToDotNew)
1601 demoteToDotOld(I);
1602
1603 cleanUpDotCur();
1604 // Check if the instruction (must be a store) was glued with an allocframe
1605 // instruction. If so, restore its offset to its original value, i.e. use
1606 // current SP instead of caller's SP.
1607 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001608 useCalleesSP(I);
1609 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001610 }
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00001611
1612 if (ChangedOffset != INT64_MAX)
1613 undoChangedOffset(I);
Krzysztof Parzyszekc4a9a8d2017-10-11 21:20:43 +00001614
1615 if (GlueToNewValueJump) {
1616 // Putting I and J together would prevent the new-value jump from being
1617 // packetized with the producer. In that case I and J must be separated.
1618 GlueToNewValueJump = false;
1619 return false;
1620 }
1621
1622 if (ChangedOffset == INT64_MAX && updateOffset(SUI, SUJ)) {
Krzysztof Parzyszek8f174dd2017-10-11 15:51:44 +00001623 FoundSequentialDependence = false;
1624 Dependence = false;
1625 return true;
1626 }
1627
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001628 return false;
1629}
1630
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001631MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001632HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001633 MachineBasicBlock::iterator MII = MI.getIterator();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001634 MachineBasicBlock *MBB = MI.getParent();
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001635
Eugene Zelenko3b873362017-09-28 22:27:31 +00001636 if (CurrentPacketMIs.empty())
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001637 PacketStalls = false;
1638 PacketStalls |= producesStall(MI);
1639
1640 if (MI.isImplicitDef())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001641 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001642 assert(ResourceTracker->canReserveResources(MI));
1643
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001644 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001645 bool Good = true;
1646
1647 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001648 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001649 // We need to put both instructions in the same packet: MI and NvjMI.
1650 // Either of them can require a constant extender. Try to add both to
1651 // the current packet, and if that fails, end the packet and start a
1652 // new one.
1653 ResourceTracker->reserveResources(MI);
1654 if (ExtMI)
1655 Good = tryAllocateResourcesForConstExt(true);
1656
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001657 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001658 if (Good) {
1659 if (ResourceTracker->canReserveResources(NvjMI))
1660 ResourceTracker->reserveResources(NvjMI);
1661 else
1662 Good = false;
1663 }
1664 if (Good && ExtNvjMI)
1665 Good = tryAllocateResourcesForConstExt(true);
1666
1667 if (!Good) {
1668 endPacket(MBB, MI);
1669 assert(ResourceTracker->canReserveResources(MI));
1670 ResourceTracker->reserveResources(MI);
1671 if (ExtMI) {
1672 assert(canReserveResourcesForConstExt());
1673 tryAllocateResourcesForConstExt(true);
1674 }
1675 assert(ResourceTracker->canReserveResources(NvjMI));
1676 ResourceTracker->reserveResources(NvjMI);
1677 if (ExtNvjMI) {
1678 assert(canReserveResourcesForConstExt());
1679 reserveResourcesForConstExt();
1680 }
1681 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001682 CurrentPacketMIs.push_back(&MI);
1683 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001684 return MII;
1685 }
1686
1687 ResourceTracker->reserveResources(MI);
1688 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1689 endPacket(MBB, MI);
1690 if (PromotedToDotNew)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001691 demoteToDotOld(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001692 if (GlueAllocframeStore) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001693 useCalleesSP(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001694 GlueAllocframeStore = false;
1695 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001696 ResourceTracker->reserveResources(MI);
1697 reserveResourcesForConstExt();
1698 }
1699
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001700 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001701 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001702}
1703
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001704void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001705 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001706 OldPacketMIs = CurrentPacketMIs;
1707 VLIWPacketizerList::endPacket(MBB, MI);
1708}
1709
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001710bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001711 return !producesStall(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001712}
1713
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001714// V60 forward scheduling.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001715bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001716 // If the packet already stalls, then ignore the stall from a subsequent
1717 // instruction in the same packet.
1718 if (PacketStalls)
1719 return false;
1720
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001721 // Check whether the previous packet is in a different loop. If this is the
1722 // case, there is little point in trying to avoid a stall because that would
1723 // favor the rare case (loop entry) over the common case (loop iteration).
1724 //
1725 // TODO: We should really be able to check all the incoming edges if this is
1726 // the first packet in a basic block, so we can avoid stalls from the loop
1727 // backedge.
1728 if (!OldPacketMIs.empty()) {
1729 auto *OldBB = OldPacketMIs.front()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001730 auto *ThisBB = I.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001731 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1732 return false;
1733 }
1734
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001735 SUnit *SUI = MIToSUnit[const_cast<MachineInstr *>(&I)];
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001736
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001737 // Check if the latency is 0 between this instruction and any instruction
1738 // in the current packet. If so, we disregard any potential stalls due to
1739 // the instructions in the previous packet. Most of the instruction pairs
1740 // that can go together in the same packet have 0 latency between them.
1741 // Only exceptions are newValueJumps as they're generated much later and
1742 // the latencies can't be changed at that point. Another is .cur
1743 // instructions if its consumer has a 0 latency successor (such as .new).
1744 // In this case, the latency between .cur and the consumer stays non-zero
1745 // even though we can have both .cur and .new in the same packet. Changing
1746 // the latency to 0 is not an option as it causes software pipeliner to
1747 // not pipeline in some cases.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001748
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001749 // For Example:
1750 // {
1751 // I1: v6.cur = vmem(r0++#1)
1752 // I2: v7 = valign(v6,v4,r2)
1753 // I3: vmem(r5++#1) = v7.new
1754 // }
1755 // Here I2 and I3 has 0 cycle latency, but I1 and I2 has 2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001756
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001757 for (auto J : CurrentPacketMIs) {
1758 SUnit *SUJ = MIToSUnit[J];
1759 for (auto &Pred : SUI->Preds)
1760 if (Pred.getSUnit() == SUJ &&
1761 (Pred.getLatency() == 0 || HII->isNewValueJump(I) ||
1762 HII->isToBeScheduledASAP(*J, I)))
1763 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001764 }
1765
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001766 // Check if the latency is greater than one between this instruction and any
1767 // instruction in the previous packet.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001768 for (auto J : OldPacketMIs) {
1769 SUnit *SUJ = MIToSUnit[J];
1770 for (auto &Pred : SUI->Preds)
1771 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1772 return true;
1773 }
1774
1775 // Check if the latency is greater than one between this instruction and any
1776 // instruction in the previous packet.
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001777 for (auto J : OldPacketMIs) {
1778 SUnit *SUJ = MIToSUnit[J];
1779 for (auto &Pred : SUI->Preds)
1780 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1781 return true;
1782 }
1783
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001784 return false;
1785}
1786
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001787//===----------------------------------------------------------------------===//
1788// Public Constructor Functions
1789//===----------------------------------------------------------------------===//
1790
1791FunctionPass *llvm::createHexagonPacketizer() {
1792 return new HexagonPacketizer();
1793}