blob: 0f407c2d8366be56dad1f56c4f8c08830afdbb28 [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
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000499enum PredicateKind {
500 PK_False,
501 PK_True,
502 PK_Unknown
503};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000504
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000505/// Returns true if an instruction is predicated on p0 and false if it's
506/// predicated on !p0.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000507static PredicateKind getPredicateSense(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000508 const HexagonInstrInfo *HII) {
509 if (!HII->isPredicated(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000510 return PK_Unknown;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000511 if (HII->isPredicatedTrue(MI))
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000512 return PK_True;
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000513 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000514}
515
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000516static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000517 const HexagonInstrInfo *HII) {
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000518 assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000519#ifndef NDEBUG
520 // Post Increment means duplicates. Use dense map to find duplicates in the
521 // list. Caution: Densemap initializes with the minimum of 64 buckets,
522 // whereas there are at most 5 operands in the post increment.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000523 DenseSet<unsigned> DefRegsSet;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000524 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000525 if (MO.isReg() && MO.isDef())
526 DefRegsSet.insert(MO.getReg());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000527
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000528 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000529 if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
530 return MO;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000531#else
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000532 if (MI.mayLoad()) {
533 const MachineOperand &Op1 = MI.getOperand(1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000534 // The 2nd operand is always the post increment operand in load.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000535 assert(Op1.isReg() && "Post increment operand has be to a register.");
536 return Op1;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000537 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000538 if (MI.getDesc().mayStore()) {
539 const MachineOperand &Op0 = MI.getOperand(0);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000540 // The 1st operand is always the post increment operand in store.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000541 assert(Op0.isReg() && "Post increment operand has be to a register.");
542 return Op0;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000543 }
544#endif
545 // we should never come here.
546 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
547}
548
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000549// Get the value being stored.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000550static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000551 // value being stored is always the last operand.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000552 return MI.getOperand(MI.getNumOperands()-1);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000553}
554
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000555static bool isLoadAbsSet(const MachineInstr &MI) {
556 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000557 switch (Opc) {
558 case Hexagon::L4_loadrd_ap:
559 case Hexagon::L4_loadrb_ap:
560 case Hexagon::L4_loadrh_ap:
561 case Hexagon::L4_loadrub_ap:
562 case Hexagon::L4_loadruh_ap:
563 case Hexagon::L4_loadri_ap:
564 return true;
565 }
566 return false;
567}
568
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000569static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000570 assert(isLoadAbsSet(MI));
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000571 return MI.getOperand(1);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000572}
573
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000574// Can be new value store?
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000575// Following restrictions are to be respected in convert a store into
576// a new value store.
577// 1. If an instruction uses auto-increment, its address register cannot
578// be a new-value register. Arch Spec 5.4.2.1
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000579// 2. If an instruction uses absolute-set addressing mode, its address
580// register cannot be a new-value register. Arch Spec 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000581// 3. If an instruction produces a 64-bit result, its registers cannot be used
582// as new-value registers. Arch Spec 5.4.2.2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000583// 4. If the instruction that sets the new-value register is conditional, then
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000584// the instruction that uses the new-value register must also be conditional,
585// and both must always have their predicates evaluate identically.
586// Arch Spec 5.4.2.3.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000587// 5. There is an implied restriction that a packet cannot have another store,
588// if there is a new value store in the packet. Corollary: if there is
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000589// already a store in a packet, there can not be a new value store.
590// Arch Spec: 3.4.4.2
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000591bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
592 const MachineInstr &PacketMI, unsigned DepReg) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000593 // Make sure we are looking at the store, that can be promoted.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000594 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000595 return false;
596
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000597 // Make sure there is dependency and can be new value'd.
598 const MachineOperand &Val = getStoreValueOperand(MI);
599 if (Val.isReg() && Val.getReg() != DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000600 return false;
601
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000602 const MCInstrDesc& MCID = PacketMI.getDesc();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000603
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000604 // First operand is always the result.
605 const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
606 // Double regs can not feed into new value store: PRM section: 5.4.2.2.
607 if (PacketRC == &Hexagon::DoubleRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000608 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000609
610 // New-value stores are of class NV (slot 0), dual stores require class ST
611 // in slot 0 (PRM 5.5).
612 for (auto I : CurrentPacketMIs) {
613 SUnit *PacketSU = MIToSUnit.find(I)->second;
614 if (PacketSU->getInstr()->mayStore())
615 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000616 }
617
618 // Make sure it's NOT the post increment register that we are going to
619 // new value.
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000620 if (HII->isPostIncrement(MI) &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000621 getPostIncrementOperand(MI, HII).getReg() == DepReg) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000622 return false;
623 }
624
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000625 if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000626 getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
627 // If source is post_inc, or absolute-set addressing, it can not feed
628 // into new value store
629 // r3 = memw(r2++#4)
630 // memw(r30 + #-1404) = r2.new -> can not be new value store
631 // arch spec section: 5.4.2.1.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000632 return false;
633 }
634
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000635 if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
636 return false;
637
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000638 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000639 // also be predicated.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000640 if (HII->isPredicated(PacketMI)) {
641 if (!HII->isPredicated(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000642 return false;
643
644 // Check to make sure that they both will have their predicates
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000645 // evaluate identically.
Sirish Pande95d01172012-05-11 20:00:34 +0000646 unsigned predRegNumSrc = 0;
647 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000648 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000649
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000650 // Get predicate register used in the source instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000651 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000652 if (!MO.isReg())
653 continue;
654 predRegNumSrc = MO.getReg();
655 predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
656 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000657 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000658 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000659 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
660 "predicate register not found in a predicated PacketMI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000661
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000662 // Get predicate register used in new-value store instruction.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000663 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000664 if (!MO.isReg())
665 continue;
666 predRegNumDst = MO.getReg();
667 predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
668 if (predRegClass == &Hexagon::PredRegsRegClass)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000669 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000670 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000671 assert((predRegClass == &Hexagon::PredRegsRegClass) &&
672 "predicate register not found in a predicated MI instruction");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000673
674 // New-value register producer and user (store) need to satisfy these
675 // constraints:
676 // 1) Both instructions should be predicated on the same register.
677 // 2) If producer of the new-value register is .new predicated then store
678 // should also be .new predicated and if producer is not .new predicated
679 // then store should not be .new predicated.
680 // 3) Both new-value register producer and user should have same predicate
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000681 // sense, i.e, either both should be negated or both should be non-negated.
682 if (predRegNumDst != predRegNumSrc ||
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000683 HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000684 getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000685 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000686 }
687
688 // Make sure that other than the new-value register no other store instruction
689 // register has been modified in the same packet. Predicate registers can be
690 // modified by they should not be modified between the producer and the store
691 // instruction as it will make them both conditional on different values.
692 // We already know this to be true for all the instructions before and
693 // including PacketMI. Howerver, we need to perform the check for the
694 // remaining instructions in the packet.
695
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000696 unsigned StartCheck = 0;
697
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000698 for (auto I : CurrentPacketMIs) {
699 SUnit *TempSU = MIToSUnit.find(I)->second;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000700 MachineInstr &TempMI = *TempSU->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000701
702 // Following condition is true for all the instructions until PacketMI is
703 // reached (StartCheck is set to 0 before the for loop).
704 // StartCheck flag is 1 for all the instructions after PacketMI.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000705 if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
706 continue; // encountering PacketMI.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000707
708 StartCheck = 1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000709 if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000710 continue;
711
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000712 for (auto &MO : MI.operands())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000713 if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000714 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000715 }
716
Alp Tokerf907b892013-12-05 05:44:44 +0000717 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000718 // 1. The only use of reg is DepReg and no other registers.
719 // This handles V4 base+index registers.
720 // The following store can not be dot new.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000721 // Eg. r0 = add(r0, #3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000722 // memw(r1+r0<<#2) = r0
Krzysztof Parzyszek8fb181c2016-08-01 17:55:48 +0000723 if (!HII->isPostIncrement(MI)) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000724 for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
725 const MachineOperand &MO = MI.getOperand(opNum);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000726 if (MO.isReg() && MO.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000727 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000728 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000729 }
730
731 // If data definition is because of implicit definition of the register,
732 // do not newify the store. Eg.
733 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
734 // S2_storerh_io %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000735 for (auto &MO : PacketMI.operands()) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000736 if (MO.isRegMask() && MO.clobbersPhysReg(DepReg))
737 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000738 if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
739 continue;
740 unsigned R = MO.getReg();
741 if (R == DepReg || HRI->isSuperRegister(DepReg, R))
742 return false;
743 }
744
745 // Handle imp-use of super reg case. There is a target independent side
746 // change that should prevent this situation but I am handling it for
747 // just-in-case. For example, we cannot newify R2 in the following case:
748 // %R3<def> = A2_tfrsi 0;
749 // S2_storeri_io %R0<kill>, 0, %R2<kill>, %D1<imp-use,kill>;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000750 for (auto &MO : MI.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000751 if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
752 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000753 }
754
755 // Can be dot new store.
756 return true;
757}
758
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000759// Can this MI to promoted to either new value store or new value jump.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000760bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000761 const SUnit *PacketSU, unsigned DepReg,
762 MachineBasicBlock::iterator &MII) {
763 if (!HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000764 return false;
765
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000766 // Check to see the store can be new value'ed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000767 MachineInstr &PacketMI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000768 if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000769 return true;
770
771 // Check to see the compare/jump can be new value'ed.
772 // This is done as a pass on its own. Don't need to check it here.
773 return false;
774}
775
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000776static bool isImplicitDependency(const MachineInstr &I, bool CheckDef,
777 unsigned DepReg) {
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000778 for (auto &MO : I.operands()) {
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000779 if (CheckDef && MO.isRegMask() && MO.clobbersPhysReg(DepReg))
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000780 return true;
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000781 if (!MO.isReg() || MO.getReg() != DepReg || !MO.isImplicit())
782 continue;
783 if (CheckDef == MO.isDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000784 return true;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +0000785 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000786 return false;
787}
788
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000789// Check to see if an instruction can be dot new
790// There are three kinds.
791// 1. dot new on predicate - V2/V3/V4
792// 2. dot new on stores NV/ST - V4
793// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000794bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000795 const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
796 const TargetRegisterClass* RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000797 // Already a dot new instruction.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000798 if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000799 return false;
800
Krzysztof Parzyszek2a480592016-07-26 20:30:30 +0000801 if (!isNewifiable(MI, RC))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000802 return false;
803
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000804 const MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000805
806 // The "new value" cannot come from inline asm.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000807 if (PI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000808 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000809
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000810 // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
811 // sense.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000812 if (PI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000813 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000814
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000815 // If dependency is trough an implicitly defined register, we should not
816 // newify the use.
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000817 if (isImplicitDependency(PI, true, DepReg) ||
818 isImplicitDependency(MI, false, DepReg))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000819 return false;
820
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000821 const MCInstrDesc& MCID = PI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000822 const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
Krzysztof Parzyszek55772972017-09-15 15:46:05 +0000823 if (DisableVecDblNVStores && VecRC == &Hexagon::HvxWRRegClass)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000824 return false;
825
826 // predicate .new
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000827 if (RC == &Hexagon::PredRegsRegClass)
Krzysztof Parzyszek3cf16572017-06-01 18:02:40 +0000828 return HII->predCanBeUsedAsDotNew(PI, DepReg);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000829
830 if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
831 return false;
832
833 // Create a dot new machine instruction to see if resources can be
834 // allocated. If not, bail out now.
835 int NewOpcode = HII->getDotNewOp(MI);
836 const MCInstrDesc &D = HII->get(NewOpcode);
837 MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000838 bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000839 MF.DeleteMachineInstr(NewMI);
840 if (!ResourcesAvailable)
841 return false;
842
843 // New Value Store only. New Value Jump generated as a separate pass.
844 if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
845 return false;
846
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000847 return true;
848}
849
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000850// Go through the packet instructions and search for an anti dependency between
851// them and DepReg from MI. Consider this case:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000852// Trying to add
853// a) %R1<def> = TFRI_cdNotPt %P3, 2
854// to this packet:
855// {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000856// b) %P0<def> = C2_or %P3<kill>, %P0<kill>
857// c) %P3<def> = C2_tfrrp %R23
858// d) %R1<def> = C2_cmovenewit %P3, 4
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000859// }
860// The P3 from a) and d) will be complements after
861// a)'s P3 is converted to .new form
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000862// Anti-dep between c) and b) is irrelevant for this case
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000863bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000864 unsigned DepReg) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000865 SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000866
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000867 for (auto I : CurrentPacketMIs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000868 // We only care for dependencies to predicated instructions
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000869 if (!HII->isPredicated(*I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000870 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000871
872 // Scheduling Unit for current insn in the packet
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000873 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000874
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000875 // Look at dependencies between current members of the packet and
876 // predicate defining instruction MI. Make sure that dependency is
877 // on the exact register we care about.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000878 if (PacketSU->isSucc(PacketSUDep)) {
879 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000880 auto &Dep = PacketSU->Succs[i];
881 if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
882 Dep.getReg() == DepReg)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000883 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000884 }
885 }
886 }
887
888 return false;
889}
890
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000891/// Gets the predicate register of a predicated instruction.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000892static unsigned getPredicatedRegister(MachineInstr &MI,
Benjamin Kramere79beac2013-05-23 15:43:11 +0000893 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000894 /// We use the following rule: The first predicate register that is a use is
895 /// the predicate register of a predicated instruction.
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000896 assert(QII->isPredicated(MI) && "Must be predicated instruction");
897
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000898 for (auto &Op : MI.operands()) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000899 if (Op.isReg() && Op.getReg() && Op.isUse() &&
900 Hexagon::PredRegsRegClass.contains(Op.getReg()))
901 return Op.getReg();
902 }
903
904 llvm_unreachable("Unknown instruction operand layout");
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000905 return 0;
906}
907
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000908// Given two predicated instructions, this function detects whether
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000909// the predicates are complements.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000910bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
911 MachineInstr &MI2) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000912 // If we don't know the predicate sense of the instructions bail out early, we
913 // need it later.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000914 if (getPredicateSense(MI1, HII) == PK_Unknown ||
915 getPredicateSense(MI2, HII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000916 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000917
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000918 // Scheduling unit for candidate.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000919 SUnit *SU = MIToSUnit[&MI1];
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000920
921 // One corner case deals with the following scenario:
922 // Trying to add
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000923 // a) %R24<def> = A2_tfrt %P0, %R25
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000924 // to this packet:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000925 // {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000926 // b) %R25<def> = A2_tfrf %P0, %R24
927 // c) %P0<def> = C2_cmpeqi %R26, 1
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000928 // }
929 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000930 // On general check a) and b) are complements, but presence of c) will
931 // convert a) to .new form, and then it is not a complement.
932 // We attempt to detect it by analyzing existing dependencies in the packet.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000933
934 // Analyze relationships between all existing members of the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000935 // Look for Anti dependecy on the same predicate reg as used in the
936 // candidate.
937 for (auto I : CurrentPacketMIs) {
938 // Scheduling Unit for current insn in the packet.
939 SUnit *PacketSU = MIToSUnit.find(I)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000940
941 // If this instruction in the packet is succeeded by the candidate...
942 if (PacketSU->isSucc(SU)) {
943 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000944 auto Dep = PacketSU->Succs[i];
945 // The corner case exist when there is true data dependency between
946 // candidate and one of current packet members, this dep is on
947 // predicate reg, and there already exist anti dep on the same pred in
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000948 // the packet.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000949 if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
950 Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
951 // Here I know that I is predicate setting instruction with true
952 // data dep to candidate on the register we care about - c) in the
953 // above example. Now I need to see if there is an anti dependency
954 // from c) to any other instruction in the same packet on the pred
955 // reg of interest.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000956 if (restrictingDepExistInPacket(*I, Dep.getReg()))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000957 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000958 }
959 }
960 }
961 }
962
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000963 // If the above case does not apply, check regular complement condition.
964 // Check that the predicate register is the same and that the predicate
965 // sense is different We also need to differentiate .old vs. .new: !p0
966 // is not complementary to p0.new.
967 unsigned PReg1 = getPredicatedRegister(MI1, HII);
968 unsigned PReg2 = getPredicatedRegister(MI2, HII);
969 return PReg1 == PReg2 &&
970 Hexagon::PredRegsRegClass.contains(PReg1) &&
971 Hexagon::PredRegsRegClass.contains(PReg2) &&
972 getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000973 HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000974}
975
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000976// Initialize packetizer flags.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000977void HexagonPacketizerList::initPacketizerState() {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000978 Dependence = false;
979 PromotedToDotNew = false;
980 GlueToNewValueJump = false;
981 GlueAllocframeStore = false;
982 FoundSequentialDependence = false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000983}
984
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000985// Ignore bundling of pseudo instructions.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000986bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
987 const MachineBasicBlock *) {
988 if (MI.isDebugValue())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000989 return true;
990
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000991 if (MI.isCFIInstruction())
Krzysztof Parzyszek6bbcb312015-04-22 15:47:35 +0000992 return false;
993
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000994 // We must print out inline assembly.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000995 if (MI.isInlineAsm())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000996 return false;
997
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +0000998 if (MI.isImplicitDef())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +0000999 return false;
1000
1001 // We check if MI has any functional units mapped to it. If it doesn't,
1002 // we ignore the instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001003 const MCInstrDesc& TID = MI.getDesc();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001004 auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
Hal Finkel8db55472012-06-22 20:27:13 +00001005 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001006 return !FuncUnits;
1007}
1008
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001009bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
1010 if (MI.isEHLabel() || MI.isCFIInstruction())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001011 return true;
1012
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001013 // Consider inline asm to not be a solo instruction by default.
1014 // Inline asm will be put in a packet temporarily, but then it will be
1015 // removed, and placed outside of the packet (before or after, depending
1016 // on dependencies). This is to reduce the impact of inline asm as a
1017 // "packet splitting" instruction.
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001018 if (MI.isInlineAsm() && !ScheduleInlineAsm)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001019 return true;
1020
1021 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
1022 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
1023 // They must not be grouped with other instructions in a packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001024 if (isSchedBarrier(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001025 return true;
1026
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001027 if (HII->isSolo(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001028 return true;
1029
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001030 if (MI.getOpcode() == Hexagon::A2_nop)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001031 return true;
1032
1033 return false;
1034}
1035
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001036// Quick check if instructions MI and MJ cannot coexist in the same packet.
1037// Limit the tests to be "one-way", e.g. "if MI->isBranch and MJ->isInlineAsm",
1038// but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
1039// For full test call this function twice:
1040// cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
1041// Doing the test only one way saves the amount of code in this function,
1042// since every test would need to be repeated with the MI and MJ reversed.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001043static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001044 const HexagonInstrInfo &HII) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001045 const MachineFunction *MF = MI.getParent()->getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001046 if (MF->getSubtarget<HexagonSubtarget>().hasV60TOpsOnly() &&
1047 HII.isHVXMemWithAIndirect(MI, MJ))
1048 return true;
1049
1050 // An inline asm cannot be together with a branch, because we may not be
1051 // able to remove the asm out after packetizing (i.e. if the asm must be
1052 // moved past the bundle). Similarly, two asms cannot be together to avoid
1053 // complications when determining their relative order outside of a bundle.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001054 if (MI.isInlineAsm())
1055 return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
1056 MJ.isCall() || MJ.isTerminator();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001057
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001058 switch (MI.getOpcode()) {
1059 case (Hexagon::S2_storew_locked):
1060 case (Hexagon::S4_stored_locked):
1061 case (Hexagon::L2_loadw_locked):
1062 case (Hexagon::L4_loadd_locked):
1063 case (Hexagon::Y4_l2fetch): {
1064 // These instructions can only be grouped with ALU32 or non-floating-point
1065 // XTYPE instructions. Since there is no convenient way of identifying fp
1066 // XTYPE instructions, only allow grouping with ALU32 for now.
1067 unsigned TJ = HII.getType(MJ);
Krzysztof Parzyszek5ea971c2017-02-07 17:47:37 +00001068 if (TJ != HexagonII::TypeALU32_2op &&
1069 TJ != HexagonII::TypeALU32_3op &&
1070 TJ != HexagonII::TypeALU32_ADDI)
Krzysztof Parzyszek639545b2016-08-19 16:57:05 +00001071 return true;
1072 break;
1073 }
1074 default:
1075 break;
1076 }
1077
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001078 // "False" really means that the quick check failed to determine if
1079 // I and J cannot coexist.
1080 return false;
1081}
1082
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001083// Full, symmetric check.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001084bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
1085 const MachineInstr &MJ) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001086 return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
1087}
1088
1089void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
1090 for (auto &B : MF) {
1091 MachineBasicBlock::iterator BundleIt;
1092 MachineBasicBlock::instr_iterator NextI;
1093 for (auto I = B.instr_begin(), E = B.instr_end(); I != E; I = NextI) {
1094 NextI = std::next(I);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001095 MachineInstr &MI = *I;
1096 if (MI.isBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001097 BundleIt = I;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001098 if (!MI.isInsideBundle())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001099 continue;
1100
1101 // Decide on where to insert the instruction that we are pulling out.
1102 // Debug instructions always go before the bundle, but the placement of
1103 // INLINE_ASM depends on potential dependencies. By default, try to
1104 // put it before the bundle, but if the asm writes to a register that
1105 // other instructions in the bundle read, then we need to place it
1106 // after the bundle (to preserve the bundle semantics).
1107 bool InsertBeforeBundle;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001108 if (MI.isInlineAsm())
1109 InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
1110 else if (MI.isDebugValue())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001111 InsertBeforeBundle = true;
1112 else
1113 continue;
1114
1115 BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
1116 }
1117 }
1118}
1119
1120// Check if a given instruction is of class "system".
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001121static bool isSystemInstr(const MachineInstr &MI) {
1122 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001123 switch (Opc) {
1124 case Hexagon::Y2_barrier:
1125 case Hexagon::Y2_dcfetchbo:
1126 return true;
1127 }
1128 return false;
1129}
1130
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001131bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
1132 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001133 // The dependence graph may not include edges between dead definitions,
1134 // so without extra checks, we could end up packetizing two instruction
1135 // defining the same (dead) register.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001136 if (I.isCall() || J.isCall())
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001137 return false;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001138 if (HII->isPredicated(I) || HII->isPredicated(J))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001139 return false;
1140
1141 BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001142 for (auto &MO : I.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001143 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1144 continue;
1145 DeadDefs[MO.getReg()] = true;
1146 }
1147
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001148 for (auto &MO : J.operands()) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001149 if (!MO.isReg() || !MO.isDef() || !MO.isDead())
1150 continue;
1151 unsigned R = MO.getReg();
1152 if (R != Hexagon::USR_OVF && DeadDefs[R])
1153 return true;
1154 }
1155 return false;
1156}
1157
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001158bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
1159 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001160 // A save callee-save register function call can only be in a packet
1161 // with instructions that don't write to the callee-save registers.
1162 if ((HII->isSaveCalleeSavedRegsCall(I) &&
1163 doesModifyCalleeSavedReg(J, HRI)) ||
1164 (HII->isSaveCalleeSavedRegsCall(J) &&
1165 doesModifyCalleeSavedReg(I, HRI)))
1166 return true;
1167
1168 // Two control flow instructions cannot go in the same packet.
1169 if (isControlFlow(I) && isControlFlow(J))
1170 return true;
1171
1172 // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
1173 // contain a speculative indirect jump,
1174 // a new-value compare jump or a dealloc_return.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001175 auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
1176 if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001177 return true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001178 if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001179 return true;
1180 return false;
1181 };
1182
1183 if (HII->isLoopN(I) && isBadForLoopN(J))
1184 return true;
1185 if (HII->isLoopN(J) && isBadForLoopN(I))
1186 return true;
1187
1188 // dealloc_return cannot appear in the same packet as a conditional or
1189 // unconditional jump.
1190 return HII->isDeallocRet(I) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001191 (J.isBranch() || J.isCall() || J.isBarrier());
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001192}
1193
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001194bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I,
1195 const MachineInstr &J) {
1196 // Adding I to a packet that has J.
1197
1198 // Regmasks are not reflected in the scheduling dependency graph, so
1199 // we need to check them manually. This code assumes that regmasks only
1200 // occur on calls, and the problematic case is when we add an instruction
1201 // defining a register R to a packet that has a call that clobbers R via
1202 // a regmask. Those cannot be packetized together, because the call will
1203 // be executed last. That's also a reson why it is ok to add a call
1204 // clobbering R to a packet that defines R.
1205
1206 // Look for regmasks in J.
1207 for (const MachineOperand &OpJ : J.operands()) {
1208 if (!OpJ.isRegMask())
1209 continue;
1210 assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call");
1211 for (const MachineOperand &OpI : I.operands()) {
1212 if (OpI.isReg()) {
1213 if (OpJ.clobbersPhysReg(OpI.getReg()))
1214 return true;
1215 } else if (OpI.isRegMask()) {
1216 // Both are regmasks. Assume that they intersect.
1217 return true;
1218 }
1219 }
1220 }
1221 return false;
1222}
1223
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001224bool HexagonPacketizerList::hasV4SpecificDependence(const MachineInstr &I,
1225 const MachineInstr &J) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001226 bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001227 bool StoreI = I.mayStore(), StoreJ = J.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001228 if ((SysI && StoreJ) || (SysJ && StoreI))
1229 return true;
1230
1231 if (StoreI && StoreJ) {
1232 if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
1233 return true;
1234 } else {
1235 // A memop cannot be in the same packet with another memop or a store.
1236 // Two stores can be together, but here I and J cannot both be stores.
1237 bool MopStI = HII->isMemOp(I) || StoreI;
1238 bool MopStJ = HII->isMemOp(J) || StoreJ;
1239 if (MopStI && MopStJ)
1240 return true;
1241 }
1242
1243 return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
1244}
1245
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001246// SUI is the current instruction that is out side of the current packet.
1247// SUJ is the current instruction inside the current packet against which that
1248// SUI will be packetized.
1249bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001250 assert(SUI->getInstr() && SUJ->getInstr());
1251 MachineInstr &I = *SUI->getInstr();
1252 MachineInstr &J = *SUJ->getInstr();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001253
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001254 // Clear IgnoreDepMIs when Packet starts.
1255 if (CurrentPacketMIs.size() == 1)
1256 IgnoreDepMIs.clear();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001257
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001258 MachineBasicBlock::iterator II = I.getIterator();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001259
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001260 // Solo instructions cannot go in the packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001261 assert(!isSoloInstruction(I) && "Unexpected solo instr!");
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001262
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001263 if (cannotCoexist(I, J))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001264 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001265
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001266 Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
1267 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001268 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001269
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001270 // Regmasks are not accounted for in the scheduling graph, so we need
1271 // to explicitly check for dependencies caused by them. They should only
1272 // appear on calls, so it's not too pessimistic to reject all regmask
1273 // dependencies.
1274 Dependence = hasRegMaskDependence(I, J);
1275 if (Dependence)
1276 return false;
1277
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001278 // V4 allows dual stores. It does not allow second store, if the first
1279 // store is not in SLOT0. New value store, new value jump, dealloc_return
1280 // and memop always take SLOT0. Arch spec 3.4.4.2.
1281 Dependence = hasV4SpecificDependence(I, J);
1282 if (Dependence)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001283 return false;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001284
1285 // If an instruction feeds new value jump, glue it.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001286 MachineBasicBlock::iterator NextMII = I.getIterator();
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001287 ++NextMII;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001288 if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001289 MachineInstr &NextMI = *NextMII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001290
1291 bool secondRegMatch = false;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001292 const MachineOperand &NOp0 = NextMI.getOperand(0);
1293 const MachineOperand &NOp1 = NextMI.getOperand(1);
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001294
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001295 if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001296 secondRegMatch = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001297
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001298 for (auto T : CurrentPacketMIs) {
1299 SUnit *PacketSU = MIToSUnit.find(T)->second;
1300 MachineInstr &PI = *PacketSU->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001301 // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001302 if (PI.isCall()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001303 Dependence = true;
1304 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001305 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001306 // Validate:
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001307 // 1. Packet does not have a store in it.
1308 // 2. If the first operand of the nvj is newified, and the second
1309 // operand is also a reg, it (second reg) is not defined in
1310 // the same packet.
1311 // 3. If the second operand of the nvj is newified, (which means
1312 // first operand is also a reg), first reg is not defined in
1313 // the same packet.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001314 if (PI.getOpcode() == Hexagon::S2_allocframe || PI.mayStore() ||
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001315 HII->isLoopN(PI)) {
1316 Dependence = true;
1317 break;
1318 }
1319 // Check #2/#3.
1320 const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001321 if (OpR.isReg() && PI.modifiesRegister(OpR.getReg(), HRI)) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001322 Dependence = true;
1323 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001324 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001325 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001326
1327 if (Dependence)
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001328 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001329 GlueToNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001330 }
1331
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001332 // There no dependency between a prolog instruction and its successor.
1333 if (!SUJ->isSucc(SUI))
1334 return true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001335
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001336 for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
1337 if (FoundSequentialDependence)
1338 break;
1339
1340 if (SUJ->Succs[i].getSUnit() != SUI)
1341 continue;
1342
1343 SDep::Kind DepType = SUJ->Succs[i].getKind();
1344 // For direct calls:
1345 // Ignore register dependences for call instructions for packetization
1346 // purposes except for those due to r31 and predicate registers.
1347 //
1348 // For indirect calls:
1349 // Same as direct calls + check for true dependences to the register
1350 // used in the indirect call.
1351 //
1352 // We completely ignore Order dependences for call instructions.
1353 //
1354 // For returns:
1355 // Ignore register dependences for return instructions like jumpr,
1356 // dealloc return unless we have dependencies on the explicit uses
1357 // of the registers used by jumpr (like r31) or dealloc return
1358 // (like r29 or r30).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001359 unsigned DepReg = 0;
1360 const TargetRegisterClass *RC = nullptr;
1361 if (DepType == SDep::Data) {
1362 DepReg = SUJ->Succs[i].getReg();
1363 RC = HRI->getMinimalPhysRegClass(DepReg);
1364 }
1365
Krzysztof Parzyszek38e2ccc2016-08-23 16:01:01 +00001366 if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001367 if (!isRegDependence(DepType))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001368 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001369 if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
1370 continue;
1371 }
1372
1373 if (DepType == SDep::Data) {
1374 if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
1375 if (promoteToDotCur(J, DepType, II, RC))
1376 continue;
1377 }
1378
1379 // Data dpendence ok if we have load.cur.
1380 if (DepType == SDep::Data && HII->isDotCurInst(J)) {
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001381 if (HII->isHVXVec(I))
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001382 continue;
1383 }
1384
1385 // For instructions that can be promoted to dot-new, try to promote.
1386 if (DepType == SDep::Data) {
1387 if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
1388 if (promoteToDotNew(I, DepType, II, RC)) {
1389 PromotedToDotNew = true;
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001390 if (cannotCoexist(I, J))
1391 FoundSequentialDependence = true;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001392 continue;
1393 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001394 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001395 if (HII->isNewValueJump(I))
1396 continue;
1397 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001398
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001399 // For predicated instructions, if the predicates are complements then
1400 // there can be no dependence.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001401 if (HII->isPredicated(I) && HII->isPredicated(J) &&
1402 arePredicatesComplements(I, J)) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001403 // Not always safe to do this translation.
1404 // DAG Builder attempts to reduce dependence edges using transitive
1405 // nature of dependencies. Here is an example:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001406 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001407 // r0 = tfr_pt ... (1)
1408 // r0 = tfr_pf ... (2)
1409 // r0 = tfr_pt ... (3)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001410 //
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001411 // There will be an output dependence between (1)->(2) and (2)->(3).
1412 // However, there is no dependence edge between (1)->(3). This results
1413 // in all 3 instructions going in the same packet. We ignore dependce
1414 // only once to avoid this situation.
David Majnemer0d955d02016-08-11 22:21:41 +00001415 auto Itr = find(IgnoreDepMIs, &J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001416 if (Itr != IgnoreDepMIs.end()) {
1417 Dependence = true;
1418 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001419 }
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001420 IgnoreDepMIs.push_back(&I);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001421 continue;
1422 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001423
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001424 // Ignore Order dependences between unconditional direct branches
1425 // and non-control-flow instructions.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001426 if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001427 DepType == SDep::Order)
1428 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001429
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001430 // Ignore all dependences for jumps except for true and output
1431 // dependences.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001432 if (I.isConditionalBranch() && DepType != SDep::Data &&
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001433 DepType != SDep::Output)
1434 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001435
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001436 if (DepType == SDep::Output) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001437 FoundSequentialDependence = true;
1438 break;
1439 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001440
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001441 // For Order dependences:
1442 // 1. On V4 or later, volatile loads/stores can be packetized together,
1443 // unless other rules prevent is.
1444 // 2. Store followed by a load is not allowed.
1445 // 3. Store followed by a store is only valid on V4 or later.
1446 // 4. Load followed by any memory operation is allowed.
1447 if (DepType == SDep::Order) {
1448 if (!PacketizeVolatiles) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001449 bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001450 if (OrdRefs) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001451 FoundSequentialDependence = true;
1452 break;
1453 }
1454 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001455 // J is first, I is second.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001456 bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
1457 bool LoadI = I.mayLoad(), StoreI = I.mayStore();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001458 if (StoreJ) {
1459 // Two stores are only allowed on V4+. Load following store is never
1460 // allowed.
1461 if (LoadI) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001462 FoundSequentialDependence = true;
1463 break;
1464 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001465 } else if (!LoadJ || (!LoadI && !StoreI)) {
1466 // If J is neither load nor store, assume a dependency.
1467 // If J is a load, but I is neither, also assume a dependency.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001468 FoundSequentialDependence = true;
1469 break;
1470 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001471 // Store followed by store: not OK on V2.
1472 // Store followed by load: not OK on all.
1473 // Load followed by store: OK on all.
1474 // Load followed by load: OK on all.
1475 continue;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001476 }
1477
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001478 // For V4, special case ALLOCFRAME. Even though there is dependency
1479 // between ALLOCFRAME and subsequent store, allow it to be packetized
1480 // in a same packet. This implies that the store is using the caller's
1481 // SP. Hence, offset needs to be updated accordingly.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001482 if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
1483 unsigned Opc = I.getOpcode();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001484 switch (Opc) {
1485 case Hexagon::S2_storerd_io:
1486 case Hexagon::S2_storeri_io:
1487 case Hexagon::S2_storerh_io:
1488 case Hexagon::S2_storerb_io:
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001489 if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001490 // Since this store is to be glued with allocframe in the same
1491 // packet, it will use SP of the previous stack frame, i.e.
1492 // caller's SP. Therefore, we need to recalculate offset
1493 // according to this change.
1494 GlueAllocframeStore = useCallersSP(I);
1495 if (GlueAllocframeStore)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001496 continue;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001497 }
1498 default:
1499 break;
1500 }
1501 }
1502
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001503 // There are certain anti-dependencies that cannot be ignored.
1504 // Specifically:
1505 // J2_call ... %R0<imp-def> ; SUJ
1506 // R0 = ... ; SUI
1507 // Those cannot be packetized together, since the call will observe
1508 // the effect of the assignment to R0.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001509 if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) {
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001510 // Check if I defines any volatile register. We should also check
1511 // registers that the call may read, but these happen to be a
1512 // subset of the volatile register set.
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001513 for (const MachineOperand &Op : I.operands()) {
1514 if (Op.isReg() && Op.isDef()) {
1515 unsigned R = Op.getReg();
1516 if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI))
1517 continue;
1518 } else if (!Op.isRegMask()) {
1519 // If I has a regmask assume dependency.
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001520 continue;
Krzysztof Parzyszek1aaf41a2017-02-17 22:14:51 +00001521 }
Krzysztof Parzyszekadb7ff02016-05-06 19:13:38 +00001522 FoundSequentialDependence = true;
1523 break;
1524 }
1525 }
1526
1527 // Skip over remaining anti-dependences. Two instructions that are
1528 // anti-dependent can share a packet, since in most such cases all
1529 // operands are read before any modifications take place.
1530 // The exceptions are branch and call instructions, since they are
1531 // executed after all other instructions have completed (at least
1532 // conceptually).
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001533 if (DepType != SDep::Anti) {
1534 FoundSequentialDependence = true;
1535 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001536 }
1537 }
1538
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001539 if (FoundSequentialDependence) {
1540 Dependence = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001541 return false;
1542 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001543
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001544 return true;
1545}
1546
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001547bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001548 assert(SUI->getInstr() && SUJ->getInstr());
1549 MachineInstr &I = *SUI->getInstr();
1550 MachineInstr &J = *SUJ->getInstr();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001551
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001552 bool Coexist = !cannotCoexist(I, J);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001553
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001554 if (Coexist && !Dependence)
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001555 return true;
1556
1557 // Check if the instruction was promoted to a dot-new. If so, demote it
1558 // back into a dot-old.
1559 if (PromotedToDotNew)
1560 demoteToDotOld(I);
1561
1562 cleanUpDotCur();
1563 // Check if the instruction (must be a store) was glued with an allocframe
1564 // instruction. If so, restore its offset to its original value, i.e. use
1565 // current SP instead of caller's SP.
1566 if (GlueAllocframeStore) {
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001567 useCalleesSP(I);
1568 GlueAllocframeStore = false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001569 }
1570 return false;
1571}
1572
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001573MachineBasicBlock::iterator
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001574HexagonPacketizerList::addToPacket(MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001575 MachineBasicBlock::iterator MII = MI.getIterator();
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001576 MachineBasicBlock *MBB = MI.getParent();
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001577
Eugene Zelenko3b873362017-09-28 22:27:31 +00001578 if (CurrentPacketMIs.empty())
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001579 PacketStalls = false;
1580 PacketStalls |= producesStall(MI);
1581
1582 if (MI.isImplicitDef())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001583 return MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001584 assert(ResourceTracker->canReserveResources(MI));
1585
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001586 bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001587 bool Good = true;
1588
1589 if (GlueToNewValueJump) {
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001590 MachineInstr &NvjMI = *++MII;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001591 // We need to put both instructions in the same packet: MI and NvjMI.
1592 // Either of them can require a constant extender. Try to add both to
1593 // the current packet, and if that fails, end the packet and start a
1594 // new one.
1595 ResourceTracker->reserveResources(MI);
1596 if (ExtMI)
1597 Good = tryAllocateResourcesForConstExt(true);
1598
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001599 bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001600 if (Good) {
1601 if (ResourceTracker->canReserveResources(NvjMI))
1602 ResourceTracker->reserveResources(NvjMI);
1603 else
1604 Good = false;
1605 }
1606 if (Good && ExtNvjMI)
1607 Good = tryAllocateResourcesForConstExt(true);
1608
1609 if (!Good) {
1610 endPacket(MBB, MI);
1611 assert(ResourceTracker->canReserveResources(MI));
1612 ResourceTracker->reserveResources(MI);
1613 if (ExtMI) {
1614 assert(canReserveResourcesForConstExt());
1615 tryAllocateResourcesForConstExt(true);
1616 }
1617 assert(ResourceTracker->canReserveResources(NvjMI));
1618 ResourceTracker->reserveResources(NvjMI);
1619 if (ExtNvjMI) {
1620 assert(canReserveResourcesForConstExt());
1621 reserveResourcesForConstExt();
1622 }
1623 }
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001624 CurrentPacketMIs.push_back(&MI);
1625 CurrentPacketMIs.push_back(&NvjMI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001626 return MII;
1627 }
1628
1629 ResourceTracker->reserveResources(MI);
1630 if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
1631 endPacket(MBB, MI);
1632 if (PromotedToDotNew)
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001633 demoteToDotOld(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001634 if (GlueAllocframeStore) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001635 useCalleesSP(MI);
Krzysztof Parzyszek3b4682f2016-07-26 14:24:46 +00001636 GlueAllocframeStore = false;
1637 }
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001638 ResourceTracker->reserveResources(MI);
1639 reserveResourcesForConstExt();
1640 }
1641
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001642 CurrentPacketMIs.push_back(&MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001643 return MII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001644}
1645
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001646void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001647 MachineBasicBlock::iterator MI) {
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001648 OldPacketMIs = CurrentPacketMIs;
1649 VLIWPacketizerList::endPacket(MBB, MI);
1650}
1651
Duncan P. N. Exon Smith57022872016-02-27 19:09:00 +00001652bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001653 return !producesStall(MI);
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001654}
1655
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001656// V60 forward scheduling.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001657bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001658 // If the packet already stalls, then ignore the stall from a subsequent
1659 // instruction in the same packet.
1660 if (PacketStalls)
1661 return false;
1662
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001663 // Check whether the previous packet is in a different loop. If this is the
1664 // case, there is little point in trying to avoid a stall because that would
1665 // favor the rare case (loop entry) over the common case (loop iteration).
1666 //
1667 // TODO: We should really be able to check all the incoming edges if this is
1668 // the first packet in a basic block, so we can avoid stalls from the loop
1669 // backedge.
1670 if (!OldPacketMIs.empty()) {
1671 auto *OldBB = OldPacketMIs.front()->getParent();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +00001672 auto *ThisBB = I.getParent();
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001673 if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
1674 return false;
1675 }
1676
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001677 SUnit *SUI = MIToSUnit[const_cast<MachineInstr *>(&I)];
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001678
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001679 // Check if the latency is 0 between this instruction and any instruction
1680 // in the current packet. If so, we disregard any potential stalls due to
1681 // the instructions in the previous packet. Most of the instruction pairs
1682 // that can go together in the same packet have 0 latency between them.
1683 // Only exceptions are newValueJumps as they're generated much later and
1684 // the latencies can't be changed at that point. Another is .cur
1685 // instructions if its consumer has a 0 latency successor (such as .new).
1686 // In this case, the latency between .cur and the consumer stays non-zero
1687 // even though we can have both .cur and .new in the same packet. Changing
1688 // the latency to 0 is not an option as it causes software pipeliner to
1689 // not pipeline in some cases.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001690
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001691 // For Example:
1692 // {
1693 // I1: v6.cur = vmem(r0++#1)
1694 // I2: v7 = valign(v6,v4,r2)
1695 // I3: vmem(r5++#1) = v7.new
1696 // }
1697 // Here I2 and I3 has 0 cycle latency, but I1 and I2 has 2.
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001698
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001699 for (auto J : CurrentPacketMIs) {
1700 SUnit *SUJ = MIToSUnit[J];
1701 for (auto &Pred : SUI->Preds)
1702 if (Pred.getSUnit() == SUJ &&
1703 (Pred.getLatency() == 0 || HII->isNewValueJump(I) ||
1704 HII->isToBeScheduledASAP(*J, I)))
1705 return false;
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001706 }
1707
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001708 // Check if the latency is greater than one between this instruction and any
1709 // instruction in the previous packet.
Krzysztof Parzyszek2af50372017-05-03 20:10:36 +00001710 for (auto J : OldPacketMIs) {
1711 SUnit *SUJ = MIToSUnit[J];
1712 for (auto &Pred : SUI->Preds)
1713 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1714 return true;
1715 }
1716
1717 // Check if the latency is greater than one between this instruction and any
1718 // instruction in the previous packet.
Krzysztof Parzyszek9aaf9232017-05-02 18:12:19 +00001719 for (auto J : OldPacketMIs) {
1720 SUnit *SUJ = MIToSUnit[J];
1721 for (auto &Pred : SUI->Preds)
1722 if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
1723 return true;
1724 }
1725
Krzysztof Parzyszek56bbf542015-12-16 19:36:12 +00001726 return false;
1727}
1728
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001729//===----------------------------------------------------------------------===//
1730// Public Constructor Functions
1731//===----------------------------------------------------------------------===//
1732
1733FunctionPass *llvm::createHexagonPacketizer() {
1734 return new HexagonPacketizer();
1735}