blob: c123640b671308570af051f20592455019a79d20 [file] [log] [blame]
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001//===----- HexagonPacketizer.cpp - vliw packetizer ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements a simple VLIW packetizer using DFA. The packetizer works on
11// machine basic blocks. For each instruction I in BB, the packetizer consults
12// the DFA to see if machine resources are available to execute I. If so, the
13// packetizer checks if I depends on any instruction J in the current packet.
14// If no dependency is found, I is added to current packet and machine resource
15// is marked as taken. If any dependency is found, a target API call is made to
16// prune the dependence.
17//
18//===----------------------------------------------------------------------===//
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/CodeGen/DFAPacketizer.h"
Jyotsna Verma84256432013-03-01 17:37:13 +000020#include "Hexagon.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "HexagonMachineFunctionInfo.h"
Jyotsna Verma84256432013-03-01 17:37:13 +000022#include "HexagonRegisterInfo.h"
23#include "HexagonSubtarget.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "HexagonTargetMachine.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/CodeGen/LatencyPriorityQueue.h"
28#include "llvm/CodeGen/MachineDominators.h"
29#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineFunctionAnalysis.h"
31#include "llvm/CodeGen/MachineFunctionPass.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
33#include "llvm/CodeGen/MachineLoopInfo.h"
34#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/CodeGen/Passes.h"
36#include "llvm/CodeGen/ScheduleDAG.h"
37#include "llvm/CodeGen/ScheduleDAGInstrs.h"
38#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
39#include "llvm/CodeGen/SchedulerRegistry.h"
40#include "llvm/MC/MCInstrItineraries.h"
41#include "llvm/Support/CommandLine.h"
42#include "llvm/Support/Compiler.h"
43#include "llvm/Support/Debug.h"
44#include "llvm/Support/MathExtras.h"
45#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetMachine.h"
47#include "llvm/Target/TargetRegisterInfo.h"
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000048#include <map>
Jyotsna Verma1d297502013-05-02 15:39:30 +000049#include <vector>
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000050
51using namespace llvm;
52
Chandler Carruth84e68b22014-04-22 02:41:26 +000053#define DEBUG_TYPE "packets"
54
Jyotsna Verma1d297502013-05-02 15:39:30 +000055static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
56 cl::ZeroOrMore, cl::Hidden, cl::init(true),
57 cl::desc("Allow non-solo packetization of volatile memory references"));
58
Jyotsna Verma1d297502013-05-02 15:39:30 +000059namespace llvm {
60 void initializeHexagonPacketizerPass(PassRegistry&);
61}
62
63
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000064namespace {
65 class HexagonPacketizer : public MachineFunctionPass {
66
67 public:
68 static char ID;
Jyotsna Verma1d297502013-05-02 15:39:30 +000069 HexagonPacketizer() : MachineFunctionPass(ID) {
70 initializeHexagonPacketizerPass(*PassRegistry::getPassRegistry());
71 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000072
Craig Topper906c2cd2014-04-29 07:58:16 +000073 void getAnalysisUsage(AnalysisUsage &AU) const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000074 AU.setPreservesCFG();
75 AU.addRequired<MachineDominatorTree>();
Jyotsna Verma1d297502013-05-02 15:39:30 +000076 AU.addRequired<MachineBranchProbabilityInfo>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000077 AU.addPreserved<MachineDominatorTree>();
78 AU.addRequired<MachineLoopInfo>();
79 AU.addPreserved<MachineLoopInfo>();
80 MachineFunctionPass::getAnalysisUsage(AU);
81 }
82
Craig Topper906c2cd2014-04-29 07:58:16 +000083 const char *getPassName() const override {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000084 return "Hexagon Packetizer";
85 }
86
Craig Topper906c2cd2014-04-29 07:58:16 +000087 bool runOnMachineFunction(MachineFunction &Fn) override;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000088 };
89 char HexagonPacketizer::ID = 0;
90
91 class HexagonPacketizerList : public VLIWPacketizerList {
92
93 private:
94
95 // Has the instruction been promoted to a dot-new instruction.
96 bool PromotedToDotNew;
97
98 // Has the instruction been glued to allocframe.
99 bool GlueAllocframeStore;
100
101 // Has the feeder instruction been glued to new value jump.
102 bool GlueToNewValueJump;
103
104 // Check if there is a dependence between some instruction already in this
105 // packet and this instruction.
106 bool Dependence;
107
108 // Only check for dependence if there are resources available to
109 // schedule this instruction.
110 bool FoundSequentialDependence;
111
Jyotsna Verma1d297502013-05-02 15:39:30 +0000112 /// \brief A handle to the branch probability pass.
113 const MachineBranchProbabilityInfo *MBPI;
114
115 // Track MIs with ignored dependece.
116 std::vector<MachineInstr*> IgnoreDepMIs;
117
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000118 public:
119 // Ctor.
120 HexagonPacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000121 const MachineBranchProbabilityInfo *MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000122
123 // initPacketizerState - initialize some internal flags.
Craig Topper906c2cd2014-04-29 07:58:16 +0000124 void initPacketizerState() override;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000125
126 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
Craig Topper906c2cd2014-04-29 07:58:16 +0000127 bool ignorePseudoInstruction(MachineInstr *MI,
128 MachineBasicBlock *MBB) override;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000129
130 // isSoloInstruction - return true if instruction MI can not be packetized
131 // with any other instruction, which means that MI itself is a packet.
Craig Topper906c2cd2014-04-29 07:58:16 +0000132 bool isSoloInstruction(MachineInstr *MI) override;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000133
134 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
135 // together.
Craig Topper906c2cd2014-04-29 07:58:16 +0000136 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) override;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000137
138 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
139 // and SUJ.
Craig Topper906c2cd2014-04-29 07:58:16 +0000140 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) override;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000141
Craig Topper906c2cd2014-04-29 07:58:16 +0000142 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) override;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000143 private:
144 bool IsCallDependent(MachineInstr* MI, SDep::Kind DepType, unsigned DepReg);
145 bool PromoteToDotNew(MachineInstr* MI, SDep::Kind DepType,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000146 MachineBasicBlock::iterator &MII,
147 const TargetRegisterClass* RC);
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000148 bool CanPromoteToDotNew(MachineInstr *MI, SUnit *PacketSU, unsigned DepReg,
149 const std::map<MachineInstr *, SUnit *> &MIToSUnit,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000150 MachineBasicBlock::iterator &MII,
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000151 const TargetRegisterClass *RC);
152 bool
153 CanPromoteToNewValue(MachineInstr *MI, SUnit *PacketSU, unsigned DepReg,
154 const std::map<MachineInstr *, SUnit *> &MIToSUnit,
155 MachineBasicBlock::iterator &MII);
156 bool CanPromoteToNewValueStore(
157 MachineInstr *MI, MachineInstr *PacketMI, unsigned DepReg,
158 const std::map<MachineInstr *, SUnit *> &MIToSUnit);
159 bool DemoteToDotOld(MachineInstr *MI);
160 bool ArePredicatesComplements(
161 MachineInstr *MI1, MachineInstr *MI2,
162 const std::map<MachineInstr *, SUnit *> &MIToSUnit);
163 bool RestrictingDepExistInPacket(MachineInstr *, unsigned,
164 const std::map<MachineInstr *, SUnit *> &);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000165 bool isNewifiable(MachineInstr* MI);
166 bool isCondInst(MachineInstr* MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000167 bool tryAllocateResourcesForConstExt(MachineInstr* MI);
168 bool canReserveResourcesForConstExt(MachineInstr *MI);
169 void reserveResourcesForConstExt(MachineInstr* MI);
170 bool isNewValueInst(MachineInstr* MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000171 };
172}
173
Jyotsna Verma1d297502013-05-02 15:39:30 +0000174INITIALIZE_PASS_BEGIN(HexagonPacketizer, "packets", "Hexagon Packetizer",
175 false, false)
176INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
177INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
178INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
Krzysztof Parzyszek18ee1192013-05-06 21:58:00 +0000179INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Jyotsna Verma1d297502013-05-02 15:39:30 +0000180INITIALIZE_PASS_END(HexagonPacketizer, "packets", "Hexagon Packetizer",
181 false, false)
182
183
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000184// HexagonPacketizerList Ctor.
185HexagonPacketizerList::HexagonPacketizerList(
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000186 MachineFunction &MF, MachineLoopInfo &MLI,
187 const MachineBranchProbabilityInfo *MBPI)
188 : VLIWPacketizerList(MF, MLI, true) {
Jyotsna Verma1d297502013-05-02 15:39:30 +0000189 this->MBPI = MBPI;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000190}
191
192bool HexagonPacketizer::runOnMachineFunction(MachineFunction &Fn) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000193 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000194 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Jyotsna Verma1d297502013-05-02 15:39:30 +0000195 const MachineBranchProbabilityInfo *MBPI =
196 &getAnalysis<MachineBranchProbabilityInfo>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000197 // Instantiate the packetizer.
Alexey Samsonovea0aee62014-08-20 20:57:26 +0000198 HexagonPacketizerList Packetizer(Fn, MLI, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000199
200 // DFA state table should not be empty.
201 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
202
203 //
204 // Loop over all basic blocks and remove KILL pseudo-instructions
205 // These instructions confuse the dependence analysis. Consider:
206 // D0 = ... (Insn 0)
207 // R0 = KILL R0, D0 (Insn 1)
208 // R0 = ... (Insn 2)
209 // Here, Insn 1 will result in the dependence graph not emitting an output
210 // dependence between Insn 0 and Insn 2. This can lead to incorrect
211 // packetization
212 //
213 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
214 MBB != MBBe; ++MBB) {
215 MachineBasicBlock::iterator End = MBB->end();
216 MachineBasicBlock::iterator MI = MBB->begin();
217 while (MI != End) {
218 if (MI->isKill()) {
219 MachineBasicBlock::iterator DeleteMI = MI;
220 ++MI;
221 MBB->erase(DeleteMI);
222 End = MBB->end();
223 continue;
224 }
225 ++MI;
226 }
227 }
228
229 // Loop over all of the basic blocks.
230 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
231 MBB != MBBe; ++MBB) {
232 // Find scheduling regions and schedule / packetize each region.
233 unsigned RemainingCount = MBB->size();
234 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
235 RegionEnd != MBB->begin();) {
236 // The next region starts above the previous region. Look backward in the
237 // instruction stream until we find the nearest boundary.
238 MachineBasicBlock::iterator I = RegionEnd;
239 for(;I != MBB->begin(); --I, --RemainingCount) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000240 if (TII->isSchedulingBoundary(std::prev(I), MBB, Fn))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000241 break;
242 }
243 I = MBB->begin();
244
245 // Skip empty scheduling regions.
246 if (I == RegionEnd) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000247 RegionEnd = std::prev(RegionEnd);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000248 --RemainingCount;
249 continue;
250 }
251 // Skip regions with one instruction.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000252 if (I == std::prev(RegionEnd)) {
253 RegionEnd = std::prev(RegionEnd);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000254 continue;
255 }
256
257 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
258 RegionEnd = I;
259 }
260 }
261
262 return true;
263}
264
265
266static bool IsIndirectCall(MachineInstr* MI) {
Colin LeMahieu2e3a26d2015-01-16 17:05:27 +0000267 return MI->getOpcode() == Hexagon::J2_callr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000268}
269
270// Reserve resources for constant extender. Trigure an assertion if
271// reservation fail.
272void HexagonPacketizerList::reserveResourcesForConstExt(MachineInstr* MI) {
273 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Vermabf75aaf2012-12-20 06:45:39 +0000274 MachineFunction *MF = MI->getParent()->getParent();
Colin LeMahieud7a56fd2014-12-30 15:44:17 +0000275 MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::A4_ext),
Jyotsna Vermabf75aaf2012-12-20 06:45:39 +0000276 MI->getDebugLoc());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000277
278 if (ResourceTracker->canReserveResources(PseudoMI)) {
279 ResourceTracker->reserveResources(PseudoMI);
280 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
281 } else {
282 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
283 llvm_unreachable("can not reserve resources for constant extender.");
284 }
285 return;
286}
287
288bool HexagonPacketizerList::canReserveResourcesForConstExt(MachineInstr *MI) {
289 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Verma84256432013-03-01 17:37:13 +0000290 assert((QII->isExtended(MI) || QII->isConstExtended(MI)) &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000291 "Should only be called for constant extended instructions");
292 MachineFunction *MF = MI->getParent()->getParent();
Colin LeMahieud7a56fd2014-12-30 15:44:17 +0000293 MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::A4_ext),
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000294 MI->getDebugLoc());
295 bool CanReserve = ResourceTracker->canReserveResources(PseudoMI);
296 MF->DeleteMachineInstr(PseudoMI);
297 return CanReserve;
298}
299
300// Allocate resources (i.e. 4 bytes) for constant extender. If succeed, return
301// true, otherwise, return false.
302bool HexagonPacketizerList::tryAllocateResourcesForConstExt(MachineInstr* MI) {
303 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Vermabf75aaf2012-12-20 06:45:39 +0000304 MachineFunction *MF = MI->getParent()->getParent();
Colin LeMahieud7a56fd2014-12-30 15:44:17 +0000305 MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::A4_ext),
Jyotsna Vermabf75aaf2012-12-20 06:45:39 +0000306 MI->getDebugLoc());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000307
308 if (ResourceTracker->canReserveResources(PseudoMI)) {
309 ResourceTracker->reserveResources(PseudoMI);
310 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
311 return true;
312 } else {
313 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
314 return false;
315 }
316}
317
318
319bool HexagonPacketizerList::IsCallDependent(MachineInstr* MI,
320 SDep::Kind DepType,
321 unsigned DepReg) {
322
323 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Eric Christopherd9134482014-08-04 21:25:23 +0000324 const HexagonRegisterInfo *QRI =
Eric Christopher2a321f72014-10-14 01:03:16 +0000325 (const HexagonRegisterInfo *)MF.getSubtarget().getRegisterInfo();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000326
327 // Check for lr dependence
328 if (DepReg == QRI->getRARegister()) {
329 return true;
330 }
331
332 if (QII->isDeallocRet(MI)) {
333 if (DepReg == QRI->getFrameRegister() ||
334 DepReg == QRI->getStackRegister())
335 return true;
336 }
337
338 // Check if this is a predicate dependence
339 const TargetRegisterClass* RC = QRI->getMinimalPhysRegClass(DepReg);
340 if (RC == &Hexagon::PredRegsRegClass) {
341 return true;
342 }
343
344 //
345 // Lastly check for an operand used in an indirect call
346 // If we had an attribute for checking if an instruction is an indirect call,
347 // then we could have avoided this relatively brittle implementation of
348 // IsIndirectCall()
349 //
350 // Assumes that the first operand of the CALLr is the function address
351 //
352 if (IsIndirectCall(MI) && (DepType == SDep::Data)) {
353 MachineOperand MO = MI->getOperand(0);
354 if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg)) {
355 return true;
356 }
357 }
358
359 return false;
360}
361
362static bool IsRegDependence(const SDep::Kind DepType) {
363 return (DepType == SDep::Data || DepType == SDep::Anti ||
364 DepType == SDep::Output);
365}
366
367static bool IsDirectJump(MachineInstr* MI) {
Colin LeMahieudb0b13c2014-12-10 21:24:10 +0000368 return (MI->getOpcode() == Hexagon::J2_jump);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000369}
370
371static bool IsSchedBarrier(MachineInstr* MI) {
372 switch (MI->getOpcode()) {
Colin LeMahieub882f2b2015-02-05 18:56:28 +0000373 case Hexagon::Y2_barrier:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000374 return true;
375 }
376 return false;
377}
378
379static bool IsControlFlow(MachineInstr* MI) {
380 return (MI->getDesc().isTerminator() || MI->getDesc().isCall());
381}
382
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000383static bool IsLoopN(MachineInstr *MI) {
Colin LeMahieu5ccbb122014-12-19 00:06:53 +0000384 return (MI->getOpcode() == Hexagon::J2_loop0i ||
385 MI->getOpcode() == Hexagon::J2_loop0r);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000386}
387
388/// DoesModifyCalleeSavedReg - Returns true if the instruction modifies a
389/// callee-saved register.
390static bool DoesModifyCalleeSavedReg(MachineInstr *MI,
391 const TargetRegisterInfo *TRI) {
Craig Topper840beec2014-04-04 05:16:06 +0000392 for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(); *CSR; ++CSR) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000393 unsigned CalleeSavedReg = *CSR;
394 if (MI->modifiesRegister(CalleeSavedReg, TRI))
395 return true;
396 }
397 return false;
398}
399
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000400// Returns true if an instruction can be promoted to .new predicate
401// or new-value store.
402bool HexagonPacketizerList::isNewifiable(MachineInstr* MI) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000403 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
404 if ( isCondInst(MI) || QII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000405 return true;
406 else
407 return false;
408}
409
410bool HexagonPacketizerList::isCondInst (MachineInstr* MI) {
411 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
412 const MCInstrDesc& TID = MI->getDesc();
413 // bug 5670: until that is fixed,
414 // this portion is disabled.
415 if ( TID.isConditionalBranch() // && !IsRegisterJump(MI)) ||
416 || QII->isConditionalTransfer(MI)
417 || QII->isConditionalALU32(MI)
418 || QII->isConditionalLoad(MI)
419 || QII->isConditionalStore(MI)) {
420 return true;
421 }
422 return false;
423}
424
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000425
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000426// Promote an instructiont to its .new form.
427// At this time, we have already made a call to CanPromoteToDotNew
428// and made sure that it can *indeed* be promoted.
429bool HexagonPacketizerList::PromoteToDotNew(MachineInstr* MI,
430 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
431 const TargetRegisterClass* RC) {
432
433 assert (DepType == SDep::Data);
434 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
435
436 int NewOpcode;
437 if (RC == &Hexagon::PredRegsRegClass)
Jyotsna Verma00681dc2013-05-09 19:16:07 +0000438 NewOpcode = QII->GetDotNewPredOp(MI, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000439 else
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000440 NewOpcode = QII->GetDotNewOp(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000441 MI->setDesc(QII->get(NewOpcode));
442
443 return true;
444}
445
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000446bool HexagonPacketizerList::DemoteToDotOld(MachineInstr* MI) {
447 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Verma438cec52013-05-10 20:58:11 +0000448 int NewOpcode = QII->GetDotOldOp(MI->getOpcode());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000449 MI->setDesc(QII->get(NewOpcode));
450 return true;
451}
452
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000453enum PredicateKind {
454 PK_False,
455 PK_True,
456 PK_Unknown
457};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000458
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000459/// Returns true if an instruction is predicated on p0 and false if it's
460/// predicated on !p0.
461static PredicateKind getPredicateSense(MachineInstr* MI,
462 const HexagonInstrInfo *QII) {
463 if (!QII->isPredicated(MI))
464 return PK_Unknown;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000465
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000466 if (QII->isPredicatedTrue(MI))
467 return PK_True;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000468
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000469 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000470}
471
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000472static MachineOperand& GetPostIncrementOperand(MachineInstr *MI,
473 const HexagonInstrInfo *QII) {
474 assert(QII->isPostIncrement(MI) && "Not a post increment operation.");
475#ifndef NDEBUG
476 // Post Increment means duplicates. Use dense map to find duplicates in the
477 // list. Caution: Densemap initializes with the minimum of 64 buckets,
478 // whereas there are at most 5 operands in the post increment.
479 DenseMap<unsigned, unsigned> DefRegsSet;
480 for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++)
481 if (MI->getOperand(opNum).isReg() &&
482 MI->getOperand(opNum).isDef()) {
483 DefRegsSet[MI->getOperand(opNum).getReg()] = 1;
484 }
485
486 for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++)
487 if (MI->getOperand(opNum).isReg() &&
488 MI->getOperand(opNum).isUse()) {
489 if (DefRegsSet[MI->getOperand(opNum).getReg()]) {
490 return MI->getOperand(opNum);
491 }
492 }
493#else
494 if (MI->getDesc().mayLoad()) {
495 // The 2nd operand is always the post increment operand in load.
496 assert(MI->getOperand(1).isReg() &&
497 "Post increment operand has be to a register.");
498 return (MI->getOperand(1));
499 }
500 if (MI->getDesc().mayStore()) {
501 // The 1st operand is always the post increment operand in store.
502 assert(MI->getOperand(0).isReg() &&
503 "Post increment operand has be to a register.");
504 return (MI->getOperand(0));
505 }
506#endif
507 // we should never come here.
508 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
509}
510
511// get the value being stored
512static MachineOperand& GetStoreValueOperand(MachineInstr *MI) {
513 // value being stored is always the last operand.
514 return (MI->getOperand(MI->getNumOperands()-1));
515}
516
517// can be new value store?
518// Following restrictions are to be respected in convert a store into
519// a new value store.
520// 1. If an instruction uses auto-increment, its address register cannot
521// be a new-value register. Arch Spec 5.4.2.1
522// 2. If an instruction uses absolute-set addressing mode,
523// its address register cannot be a new-value register.
524// Arch Spec 5.4.2.1.TODO: This is not enabled as
525// as absolute-set address mode patters are not implemented.
526// 3. If an instruction produces a 64-bit result, its registers cannot be used
527// as new-value registers. Arch Spec 5.4.2.2.
528// 4. If the instruction that sets a new-value register is conditional, then
529// the instruction that uses the new-value register must also be conditional,
530// and both must always have their predicates evaluate identically.
531// Arch Spec 5.4.2.3.
532// 5. There is an implied restriction of a packet can not have another store,
533// if there is a new value store in the packet. Corollary, if there is
534// already a store in a packet, there can not be a new value store.
535// Arch Spec: 3.4.4.2
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000536bool HexagonPacketizerList::CanPromoteToNewValueStore(
537 MachineInstr *MI, MachineInstr *PacketMI, unsigned DepReg,
538 const std::map<MachineInstr *, SUnit *> &MIToSUnit) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000539 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
540 // Make sure we are looking at the store, that can be promoted.
541 if (!QII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000542 return false;
543
544 // Make sure there is dependency and can be new value'ed
545 if (GetStoreValueOperand(MI).isReg() &&
546 GetStoreValueOperand(MI).getReg() != DepReg)
547 return false;
548
Eric Christopherd9134482014-08-04 21:25:23 +0000549 const HexagonRegisterInfo *QRI =
Eric Christopher2a321f72014-10-14 01:03:16 +0000550 (const HexagonRegisterInfo *)MF.getSubtarget().getRegisterInfo();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000551 const MCInstrDesc& MCID = PacketMI->getDesc();
552 // first operand is always the result
553
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000554 const TargetRegisterClass* PacketRC = QII->getRegClass(MCID, 0, QRI, MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000555
556 // if there is already an store in the packet, no can do new value store
557 // Arch Spec 3.4.4.2.
558 for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(),
559 VE = CurrentPacketMIs.end();
560 (VI != VE); ++VI) {
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000561 SUnit *PacketSU = MIToSUnit.find(*VI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000562 if (PacketSU->getInstr()->getDesc().mayStore() ||
563 // if we have mayStore = 1 set on ALLOCFRAME and DEALLOCFRAME,
564 // then we don't need this
Colin LeMahieu651b7202014-12-29 21:33:45 +0000565 PacketSU->getInstr()->getOpcode() == Hexagon::S2_allocframe ||
Colin LeMahieuff370ed2014-12-26 20:30:58 +0000566 PacketSU->getInstr()->getOpcode() == Hexagon::L2_deallocframe)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000567 return false;
568 }
569
570 if (PacketRC == &Hexagon::DoubleRegsRegClass) {
571 // new value store constraint: double regs can not feed into new value store
572 // arch spec section: 5.4.2.2
573 return false;
574 }
575
576 // Make sure it's NOT the post increment register that we are going to
577 // new value.
578 if (QII->isPostIncrement(MI) &&
579 MI->getDesc().mayStore() &&
580 GetPostIncrementOperand(MI, QII).getReg() == DepReg) {
581 return false;
582 }
583
584 if (QII->isPostIncrement(PacketMI) &&
585 PacketMI->getDesc().mayLoad() &&
586 GetPostIncrementOperand(PacketMI, QII).getReg() == DepReg) {
587 // if source is post_inc, or absolute-set addressing,
588 // it can not feed into new value store
589 // r3 = memw(r2++#4)
590 // memw(r30 + #-1404) = r2.new -> can not be new value store
591 // arch spec section: 5.4.2.1
592 return false;
593 }
594
595 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000596 // also be predicated.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000597 if (QII->isPredicated(PacketMI)) {
598 if (!QII->isPredicated(MI))
599 return false;
600
601 // Check to make sure that they both will have their predicates
602 // evaluate identically
Sirish Pande95d01172012-05-11 20:00:34 +0000603 unsigned predRegNumSrc = 0;
604 unsigned predRegNumDst = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000605 const TargetRegisterClass* predRegClass = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000606
607 // Get predicate register used in the source instruction
608 for(unsigned opNum = 0; opNum < PacketMI->getNumOperands(); opNum++) {
609 if ( PacketMI->getOperand(opNum).isReg())
610 predRegNumSrc = PacketMI->getOperand(opNum).getReg();
611 predRegClass = QRI->getMinimalPhysRegClass(predRegNumSrc);
612 if (predRegClass == &Hexagon::PredRegsRegClass) {
613 break;
614 }
615 }
616 assert ((predRegClass == &Hexagon::PredRegsRegClass ) &&
617 ("predicate register not found in a predicated PacketMI instruction"));
618
619 // Get predicate register used in new-value store instruction
620 for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
621 if ( MI->getOperand(opNum).isReg())
622 predRegNumDst = MI->getOperand(opNum).getReg();
623 predRegClass = QRI->getMinimalPhysRegClass(predRegNumDst);
624 if (predRegClass == &Hexagon::PredRegsRegClass) {
625 break;
626 }
627 }
628 assert ((predRegClass == &Hexagon::PredRegsRegClass ) &&
629 ("predicate register not found in a predicated MI instruction"));
630
631 // New-value register producer and user (store) need to satisfy these
632 // constraints:
633 // 1) Both instructions should be predicated on the same register.
634 // 2) If producer of the new-value register is .new predicated then store
635 // should also be .new predicated and if producer is not .new predicated
636 // then store should not be .new predicated.
637 // 3) Both new-value register producer and user should have same predicate
638 // sense, i.e, either both should be negated or both should be none negated.
639
640 if (( predRegNumDst != predRegNumSrc) ||
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000641 QII->isDotNewInst(PacketMI) != QII->isDotNewInst(MI) ||
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000642 getPredicateSense(MI, QII) != getPredicateSense(PacketMI, QII)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000643 return false;
644 }
645 }
646
647 // Make sure that other than the new-value register no other store instruction
648 // register has been modified in the same packet. Predicate registers can be
649 // modified by they should not be modified between the producer and the store
650 // instruction as it will make them both conditional on different values.
651 // We already know this to be true for all the instructions before and
652 // including PacketMI. Howerver, we need to perform the check for the
653 // remaining instructions in the packet.
654
655 std::vector<MachineInstr*>::iterator VI;
656 std::vector<MachineInstr*>::iterator VE;
657 unsigned StartCheck = 0;
658
659 for (VI=CurrentPacketMIs.begin(), VE = CurrentPacketMIs.end();
660 (VI != VE); ++VI) {
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000661 SUnit *TempSU = MIToSUnit.find(*VI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000662 MachineInstr* TempMI = TempSU->getInstr();
663
664 // Following condition is true for all the instructions until PacketMI is
665 // reached (StartCheck is set to 0 before the for loop).
666 // StartCheck flag is 1 for all the instructions after PacketMI.
667 if (TempMI != PacketMI && !StartCheck) // start processing only after
668 continue; // encountering PacketMI
669
670 StartCheck = 1;
671 if (TempMI == PacketMI) // We don't want to check PacketMI for dependence
672 continue;
673
674 for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
675 if (MI->getOperand(opNum).isReg() &&
676 TempSU->getInstr()->modifiesRegister(MI->getOperand(opNum).getReg(),
677 QRI))
678 return false;
679 }
680 }
681
Alp Tokerf907b892013-12-05 05:44:44 +0000682 // Make sure that for non-POST_INC stores:
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000683 // 1. The only use of reg is DepReg and no other registers.
684 // This handles V4 base+index registers.
685 // The following store can not be dot new.
686 // Eg. r0 = add(r0, #3)a
687 // memw(r1+r0<<#2) = r0
688 if (!QII->isPostIncrement(MI) &&
689 GetStoreValueOperand(MI).isReg() &&
690 GetStoreValueOperand(MI).getReg() == DepReg) {
691 for(unsigned opNum = 0; opNum < MI->getNumOperands()-1; opNum++) {
692 if (MI->getOperand(opNum).isReg() &&
693 MI->getOperand(opNum).getReg() == DepReg) {
694 return false;
695 }
696 }
697 // 2. If data definition is because of implicit definition of the register,
698 // do not newify the store. Eg.
699 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
700 // STrih_indexed %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
701 for(unsigned opNum = 0; opNum < PacketMI->getNumOperands(); opNum++) {
702 if (PacketMI->getOperand(opNum).isReg() &&
703 PacketMI->getOperand(opNum).getReg() == DepReg &&
704 PacketMI->getOperand(opNum).isDef() &&
705 PacketMI->getOperand(opNum).isImplicit()) {
706 return false;
707 }
708 }
709 }
710
711 // Can be dot new store.
712 return true;
713}
714
715// can this MI to promoted to either
716// new value store or new value jump
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000717bool HexagonPacketizerList::CanPromoteToNewValue(
718 MachineInstr *MI, SUnit *PacketSU, unsigned DepReg,
719 const std::map<MachineInstr *, SUnit *> &MIToSUnit,
720 MachineBasicBlock::iterator &MII) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000721
Jyotsna Verma438cec52013-05-10 20:58:11 +0000722 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Colin LeMahieu4fd203d2015-02-09 21:56:37 +0000723 if (!QII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000724 return false;
725
726 MachineInstr *PacketMI = PacketSU->getInstr();
727
728 // Check to see the store can be new value'ed.
729 if (CanPromoteToNewValueStore(MI, PacketMI, DepReg, MIToSUnit))
730 return true;
731
732 // Check to see the compare/jump can be new value'ed.
733 // This is done as a pass on its own. Don't need to check it here.
734 return false;
735}
736
737// Check to see if an instruction can be dot new
738// There are three kinds.
739// 1. dot new on predicate - V2/V3/V4
740// 2. dot new on stores NV/ST - V4
741// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000742bool HexagonPacketizerList::CanPromoteToDotNew(
743 MachineInstr *MI, SUnit *PacketSU, unsigned DepReg,
744 const std::map<MachineInstr *, SUnit *> &MIToSUnit,
745 MachineBasicBlock::iterator &MII, const TargetRegisterClass *RC) {
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000746 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
747 // Already a dot new instruction.
Jyotsna Verma438cec52013-05-10 20:58:11 +0000748 if (QII->isDotNewInst(MI) && !QII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000749 return false;
750
751 if (!isNewifiable(MI))
752 return false;
753
754 // predicate .new
755 if (RC == &Hexagon::PredRegsRegClass && isCondInst(MI))
756 return true;
757 else if (RC != &Hexagon::PredRegsRegClass &&
Jyotsna Verma438cec52013-05-10 20:58:11 +0000758 !QII->mayBeNewStore(MI)) // MI is not a new-value store
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000759 return false;
760 else {
761 // Create a dot new machine instruction to see if resources can be
762 // allocated. If not, bail out now.
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000763 int NewOpcode = QII->GetDotNewOp(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000764 const MCInstrDesc &desc = QII->get(NewOpcode);
765 DebugLoc dl;
766 MachineInstr *NewMI =
767 MI->getParent()->getParent()->CreateMachineInstr(desc, dl);
768 bool ResourcesAvailable = ResourceTracker->canReserveResources(NewMI);
769 MI->getParent()->getParent()->DeleteMachineInstr(NewMI);
770
771 if (!ResourcesAvailable)
772 return false;
773
774 // new value store only
775 // new new value jump generated as a passes
776 if (!CanPromoteToNewValue(MI, PacketSU, DepReg, MIToSUnit, MII)) {
777 return false;
778 }
779 }
780 return true;
781}
782
783// Go through the packet instructions and search for anti dependency
784// between them and DepReg from MI
785// Consider this case:
786// Trying to add
787// a) %R1<def> = TFRI_cdNotPt %P3, 2
788// to this packet:
789// {
790// b) %P0<def> = OR_pp %P3<kill>, %P0<kill>
791// c) %P3<def> = TFR_PdRs %R23
792// d) %R1<def> = TFRI_cdnPt %P3, 4
793// }
794// The P3 from a) and d) will be complements after
795// a)'s P3 is converted to .new form
796// Anti Dep between c) and b) is irrelevant for this case
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000797bool HexagonPacketizerList::RestrictingDepExistInPacket(
798 MachineInstr *MI, unsigned DepReg,
799 const std::map<MachineInstr *, SUnit *> &MIToSUnit) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000800
801 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000802 SUnit *PacketSUDep = MIToSUnit.find(MI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000803
804 for (std::vector<MachineInstr*>::iterator VIN = CurrentPacketMIs.begin(),
805 VEN = CurrentPacketMIs.end(); (VIN != VEN); ++VIN) {
806
807 // We only care for dependencies to predicated instructions
808 if(!QII->isPredicated(*VIN)) continue;
809
810 // Scheduling Unit for current insn in the packet
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000811 SUnit *PacketSU = MIToSUnit.find(*VIN)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000812
813 // Look at dependencies between current members of the packet
814 // and predicate defining instruction MI.
815 // Make sure that dependency is on the exact register
816 // we care about.
817 if (PacketSU->isSucc(PacketSUDep)) {
818 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
819 if ((PacketSU->Succs[i].getSUnit() == PacketSUDep) &&
820 (PacketSU->Succs[i].getKind() == SDep::Anti) &&
821 (PacketSU->Succs[i].getReg() == DepReg)) {
822 return true;
823 }
824 }
825 }
826 }
827
828 return false;
829}
830
831
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000832/// Gets the predicate register of a predicated instruction.
Benjamin Kramere79beac2013-05-23 15:43:11 +0000833static unsigned getPredicatedRegister(MachineInstr *MI,
834 const HexagonInstrInfo *QII) {
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000835 /// We use the following rule: The first predicate register that is a use is
836 /// the predicate register of a predicated instruction.
837
838 assert(QII->isPredicated(MI) && "Must be predicated instruction");
839
840 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
841 OE = MI->operands_end(); OI != OE; ++OI) {
842 MachineOperand &Op = *OI;
843 if (Op.isReg() && Op.getReg() && Op.isUse() &&
844 Hexagon::PredRegsRegClass.contains(Op.getReg()))
845 return Op.getReg();
846 }
847
848 llvm_unreachable("Unknown instruction operand layout");
849
850 return 0;
851}
852
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000853// Given two predicated instructions, this function detects whether
854// the predicates are complements
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000855bool HexagonPacketizerList::ArePredicatesComplements(
856 MachineInstr *MI1, MachineInstr *MI2,
857 const std::map<MachineInstr *, SUnit *> &MIToSUnit) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000858
859 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000860
861 // If we don't know the predicate sense of the instructions bail out early, we
862 // need it later.
863 if (getPredicateSense(MI1, QII) == PK_Unknown ||
864 getPredicateSense(MI2, QII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000865 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000866
867 // Scheduling unit for candidate
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000868 SUnit *SU = MIToSUnit.find(MI1)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000869
870 // One corner case deals with the following scenario:
871 // Trying to add
872 // a) %R24<def> = TFR_cPt %P0, %R25
873 // to this packet:
874 //
875 // {
876 // b) %R25<def> = TFR_cNotPt %P0, %R24
877 // c) %P0<def> = CMPEQri %R26, 1
878 // }
879 //
880 // On general check a) and b) are complements, but
881 // presence of c) will convert a) to .new form, and
882 // then it is not a complement
883 // We attempt to detect it by analyzing existing
884 // dependencies in the packet
885
886 // Analyze relationships between all existing members of the packet.
887 // Look for Anti dependecy on the same predicate reg
888 // as used in the candidate
889 for (std::vector<MachineInstr*>::iterator VIN = CurrentPacketMIs.begin(),
890 VEN = CurrentPacketMIs.end(); (VIN != VEN); ++VIN) {
891
892 // Scheduling Unit for current insn in the packet
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000893 SUnit *PacketSU = MIToSUnit.find(*VIN)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000894
895 // If this instruction in the packet is succeeded by the candidate...
896 if (PacketSU->isSucc(SU)) {
897 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
898 // The corner case exist when there is true data
899 // dependency between candidate and one of current
900 // packet members, this dep is on predicate reg, and
901 // there already exist anti dep on the same pred in
902 // the packet.
903 if (PacketSU->Succs[i].getSUnit() == SU &&
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000904 PacketSU->Succs[i].getKind() == SDep::Data &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000905 Hexagon::PredRegsRegClass.contains(
906 PacketSU->Succs[i].getReg()) &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000907 // Here I know that *VIN is predicate setting instruction
908 // with true data dep to candidate on the register
909 // we care about - c) in the above example.
910 // Now I need to see if there is an anti dependency
911 // from c) to any other instruction in the
912 // same packet on the pred reg of interest
913 RestrictingDepExistInPacket(*VIN,PacketSU->Succs[i].getReg(),
914 MIToSUnit)) {
915 return false;
916 }
917 }
918 }
919 }
920
921 // If the above case does not apply, check regular
922 // complement condition.
923 // Check that the predicate register is the same and
924 // that the predicate sense is different
925 // We also need to differentiate .old vs. .new:
926 // !p0 is not complimentary to p0.new
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000927 unsigned PReg1 = getPredicatedRegister(MI1, QII);
928 unsigned PReg2 = getPredicatedRegister(MI2, QII);
929 return ((PReg1 == PReg2) &&
930 Hexagon::PredRegsRegClass.contains(PReg1) &&
931 Hexagon::PredRegsRegClass.contains(PReg2) &&
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000932 (getPredicateSense(MI1, QII) != getPredicateSense(MI2, QII)) &&
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000933 (QII->isDotNewInst(MI1) == QII->isDotNewInst(MI2)));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000934}
935
936// initPacketizerState - Initialize packetizer flags
937void HexagonPacketizerList::initPacketizerState() {
938
939 Dependence = false;
940 PromotedToDotNew = false;
941 GlueToNewValueJump = false;
942 GlueAllocframeStore = false;
943 FoundSequentialDependence = false;
944
945 return;
946}
947
948// ignorePseudoInstruction - Ignore bundling of pseudo instructions.
949bool HexagonPacketizerList::ignorePseudoInstruction(MachineInstr *MI,
950 MachineBasicBlock *MBB) {
951 if (MI->isDebugValue())
952 return true;
953
954 // We must print out inline assembly
955 if (MI->isInlineAsm())
956 return false;
957
958 // We check if MI has any functional units mapped to it.
959 // If it doesn't, we ignore the instruction.
960 const MCInstrDesc& TID = MI->getDesc();
961 unsigned SchedClass = TID.getSchedClass();
962 const InstrStage* IS =
963 ResourceTracker->getInstrItins()->beginStage(SchedClass);
Hal Finkel8db55472012-06-22 20:27:13 +0000964 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000965 return !FuncUnits;
966}
967
968// isSoloInstruction: - Returns true for instructions that must be
969// scheduled in their own packet.
970bool HexagonPacketizerList::isSoloInstruction(MachineInstr *MI) {
971
972 if (MI->isInlineAsm())
973 return true;
974
975 if (MI->isEHLabel())
976 return true;
977
978 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
979 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
980 // They must not be grouped with other instructions in a packet.
981 if (IsSchedBarrier(MI))
982 return true;
983
984 return false;
985}
986
987// isLegalToPacketizeTogether:
988// SUI is the current instruction that is out side of the current packet.
989// SUJ is the current instruction inside the current packet against which that
990// SUI will be packetized.
991bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
992 MachineInstr *I = SUI->getInstr();
993 MachineInstr *J = SUJ->getInstr();
994 assert(I && J && "Unable to packetize null instruction!");
995
996 const MCInstrDesc &MCIDI = I->getDesc();
997 const MCInstrDesc &MCIDJ = J->getDesc();
998
999 MachineBasicBlock::iterator II = I;
1000
1001 const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
Eric Christopherd9134482014-08-04 21:25:23 +00001002 const HexagonRegisterInfo *QRI =
Eric Christopher2a321f72014-10-14 01:03:16 +00001003 (const HexagonRegisterInfo *)MF.getSubtarget().getRegisterInfo();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001004 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1005
1006 // Inline asm cannot go in the packet.
1007 if (I->getOpcode() == Hexagon::INLINEASM)
1008 llvm_unreachable("Should not meet inline asm here!");
1009
1010 if (isSoloInstruction(I))
1011 llvm_unreachable("Should not meet solo instr here!");
1012
1013 // A save callee-save register function call can only be in a packet
1014 // with instructions that don't write to the callee-save registers.
1015 if ((QII->isSaveCalleeSavedRegsCall(I) &&
1016 DoesModifyCalleeSavedReg(J, QRI)) ||
1017 (QII->isSaveCalleeSavedRegsCall(J) &&
1018 DoesModifyCalleeSavedReg(I, QRI))) {
1019 Dependence = true;
1020 return false;
1021 }
1022
1023 // Two control flow instructions cannot go in the same packet.
1024 if (IsControlFlow(I) && IsControlFlow(J)) {
1025 Dependence = true;
1026 return false;
1027 }
1028
1029 // A LoopN instruction cannot appear in the same packet as a jump or call.
Jyotsna Verma438cec52013-05-10 20:58:11 +00001030 if (IsLoopN(I) &&
1031 (IsDirectJump(J) || MCIDJ.isCall() || QII->isDeallocRet(J))) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001032 Dependence = true;
1033 return false;
1034 }
Jyotsna Verma438cec52013-05-10 20:58:11 +00001035 if (IsLoopN(J) &&
1036 (IsDirectJump(I) || MCIDI.isCall() || QII->isDeallocRet(I))) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001037 Dependence = true;
1038 return false;
1039 }
1040
1041 // dealloc_return cannot appear in the same packet as a conditional or
1042 // unconditional jump.
Jyotsna Verma438cec52013-05-10 20:58:11 +00001043 if (QII->isDeallocRet(I) &&
1044 (MCIDJ.isBranch() || MCIDJ.isCall() || MCIDJ.isBarrier())) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001045 Dependence = true;
1046 return false;
1047 }
1048
1049
1050 // V4 allows dual store. But does not allow second store, if the
1051 // first store is not in SLOT0. New value store, new value jump,
1052 // dealloc_return and memop always take SLOT0.
1053 // Arch spec 3.4.4.2
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001054 if (MCIDI.mayStore() && MCIDJ.mayStore() &&
1055 (QII->isNewValueInst(J) || QII->isMemOp(J) || QII->isMemOp(I))) {
1056 Dependence = true;
1057 return false;
1058 }
1059
1060 if ((QII->isMemOp(J) && MCIDI.mayStore())
1061 || (MCIDJ.mayStore() && QII->isMemOp(I))
1062 || (QII->isMemOp(J) && QII->isMemOp(I))) {
1063 Dependence = true;
1064 return false;
1065 }
1066
1067 //if dealloc_return
1068 if (MCIDJ.mayStore() && QII->isDeallocRet(I)) {
1069 Dependence = true;
1070 return false;
1071 }
1072
1073 // If an instruction feeds new value jump, glue it.
1074 MachineBasicBlock::iterator NextMII = I;
1075 ++NextMII;
1076 if (NextMII != I->getParent()->end() && QII->isNewValueJump(NextMII)) {
1077 MachineInstr *NextMI = NextMII;
1078
1079 bool secondRegMatch = false;
1080 bool maintainNewValueJump = false;
1081
1082 if (NextMI->getOperand(1).isReg() &&
1083 I->getOperand(0).getReg() == NextMI->getOperand(1).getReg()) {
1084 secondRegMatch = true;
1085 maintainNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001086 }
1087
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001088 if (!secondRegMatch &&
1089 I->getOperand(0).getReg() == NextMI->getOperand(0).getReg()) {
1090 maintainNewValueJump = true;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001091 }
1092
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001093 for (std::vector<MachineInstr*>::iterator
1094 VI = CurrentPacketMIs.begin(),
1095 VE = CurrentPacketMIs.end();
1096 (VI != VE && maintainNewValueJump); ++VI) {
1097 SUnit *PacketSU = MIToSUnit.find(*VI)->second;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001098
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001099 // NVJ can not be part of the dual jump - Arch Spec: section 7.8
1100 if (PacketSU->getInstr()->getDesc().isCall()) {
1101 Dependence = true;
1102 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001103 }
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001104 // Validate
1105 // 1. Packet does not have a store in it.
1106 // 2. If the first operand of the nvj is newified, and the second
1107 // operand is also a reg, it (second reg) is not defined in
1108 // the same packet.
1109 // 3. If the second operand of the nvj is newified, (which means
1110 // first operand is also a reg), first reg is not defined in
1111 // the same packet.
1112 if (PacketSU->getInstr()->getDesc().mayStore() ||
1113 PacketSU->getInstr()->getOpcode() == Hexagon::S2_allocframe ||
1114 // Check #2.
1115 (!secondRegMatch && NextMI->getOperand(1).isReg() &&
1116 PacketSU->getInstr()->modifiesRegister(
1117 NextMI->getOperand(1).getReg(), QRI)) ||
1118 // Check #3.
1119 (secondRegMatch &&
1120 PacketSU->getInstr()->modifiesRegister(
1121 NextMI->getOperand(0).getReg(), QRI))) {
1122 Dependence = true;
1123 break;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001124 }
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001125 }
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001126 if (!Dependence)
1127 GlueToNewValueJump = true;
1128 else
1129 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001130 }
1131
1132 if (SUJ->isSucc(SUI)) {
1133 for (unsigned i = 0;
1134 (i < SUJ->Succs.size()) && !FoundSequentialDependence;
1135 ++i) {
1136
1137 if (SUJ->Succs[i].getSUnit() != SUI) {
1138 continue;
1139 }
1140
1141 SDep::Kind DepType = SUJ->Succs[i].getKind();
1142
1143 // For direct calls:
1144 // Ignore register dependences for call instructions for
1145 // packetization purposes except for those due to r31 and
1146 // predicate registers.
1147 //
1148 // For indirect calls:
1149 // Same as direct calls + check for true dependences to the register
1150 // used in the indirect call.
1151 //
1152 // We completely ignore Order dependences for call instructions
1153 //
1154 // For returns:
1155 // Ignore register dependences for return instructions like jumpr,
1156 // dealloc return unless we have dependencies on the explicit uses
1157 // of the registers used by jumpr (like r31) or dealloc return
1158 // (like r29 or r30).
1159 //
1160 // TODO: Currently, jumpr is handling only return of r31. So, the
1161 // following logic (specificaly IsCallDependent) is working fine.
1162 // We need to enable jumpr for register other than r31 and then,
1163 // we need to rework the last part, where it handles indirect call
1164 // of that (IsCallDependent) function. Bug 6216 is opened for this.
1165 //
1166 unsigned DepReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001167 const TargetRegisterClass* RC = nullptr;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001168 if (DepType == SDep::Data) {
1169 DepReg = SUJ->Succs[i].getReg();
1170 RC = QRI->getMinimalPhysRegClass(DepReg);
1171 }
1172 if ((MCIDI.isCall() || MCIDI.isReturn()) &&
1173 (!IsRegDependence(DepType) ||
1174 !IsCallDependent(I, DepType, SUJ->Succs[i].getReg()))) {
1175 /* do nothing */
1176 }
1177
1178 // For instructions that can be promoted to dot-new, try to promote.
1179 else if ((DepType == SDep::Data) &&
1180 CanPromoteToDotNew(I, SUJ, DepReg, MIToSUnit, II, RC) &&
1181 PromoteToDotNew(I, DepType, II, RC)) {
1182 PromotedToDotNew = true;
1183 /* do nothing */
1184 }
1185
1186 else if ((DepType == SDep::Data) &&
1187 (QII->isNewValueJump(I))) {
1188 /* do nothing */
1189 }
1190
1191 // For predicated instructions, if the predicates are complements
1192 // then there can be no dependence.
1193 else if (QII->isPredicated(I) &&
1194 QII->isPredicated(J) &&
1195 ArePredicatesComplements(I, J, MIToSUnit)) {
1196 /* do nothing */
1197
1198 }
1199 else if (IsDirectJump(I) &&
1200 !MCIDJ.isBranch() &&
1201 !MCIDJ.isCall() &&
1202 (DepType == SDep::Order)) {
1203 // Ignore Order dependences between unconditional direct branches
1204 // and non-control-flow instructions
1205 /* do nothing */
1206 }
1207 else if (MCIDI.isConditionalBranch() && (DepType != SDep::Data) &&
1208 (DepType != SDep::Output)) {
1209 // Ignore all dependences for jumps except for true and output
1210 // dependences
1211 /* do nothing */
1212 }
1213
1214 // Ignore output dependences due to superregs. We can
1215 // write to two different subregisters of R1:0 for instance
1216 // in the same cycle
1217 //
1218
1219 //
1220 // Let the
1221 // If neither I nor J defines DepReg, then this is a
1222 // superfluous output dependence. The dependence must be of the
1223 // form:
1224 // R0 = ...
1225 // R1 = ...
1226 // and there is an output dependence between the two instructions
1227 // with
1228 // DepReg = D0
1229 // We want to ignore these dependences.
1230 // Ideally, the dependence constructor should annotate such
1231 // dependences. We can then avoid this relatively expensive check.
1232 //
1233 else if (DepType == SDep::Output) {
1234 // DepReg is the register that's responsible for the dependence.
1235 unsigned DepReg = SUJ->Succs[i].getReg();
1236
1237 // Check if I and J really defines DepReg.
1238 if (I->definesRegister(DepReg) ||
1239 J->definesRegister(DepReg)) {
1240 FoundSequentialDependence = true;
1241 break;
1242 }
1243 }
1244
1245 // We ignore Order dependences for
1246 // 1. Two loads unless they are volatile.
1247 // 2. Two stores in V4 unless they are volatile.
1248 else if ((DepType == SDep::Order) &&
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001249 !I->hasOrderedMemoryRef() &&
1250 !J->hasOrderedMemoryRef()) {
Colin LeMahieu4fd203d2015-02-09 21:56:37 +00001251 if (MCIDI.mayStore() && MCIDJ.mayStore()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001252 /* do nothing */
1253 }
1254 // store followed by store-- not OK on V2
1255 // store followed by load -- not OK on all (OK if addresses
1256 // are not aliased)
1257 // load followed by store -- OK on all
1258 // load followed by load -- OK on all
1259 else if ( !MCIDJ.mayStore()) {
1260 /* do nothing */
1261 }
1262 else {
1263 FoundSequentialDependence = true;
1264 break;
1265 }
1266 }
1267
1268 // For V4, special case ALLOCFRAME. Even though there is dependency
Sid Manningac3e3252014-09-08 13:05:23 +00001269 // between ALLOCFRAME and subsequent store, allow it to be
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001270 // packetized in a same packet. This implies that the store is using
Sid Manningac3e3252014-09-08 13:05:23 +00001271 // caller's SP. Hence, offset needs to be updated accordingly.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001272 else if (DepType == SDep::Data
Colin LeMahieu651b7202014-12-29 21:33:45 +00001273 && J->getOpcode() == Hexagon::S2_allocframe
Colin LeMahieubda31b42014-12-29 20:44:51 +00001274 && (I->getOpcode() == Hexagon::S2_storerd_io
1275 || I->getOpcode() == Hexagon::S2_storeri_io
1276 || I->getOpcode() == Hexagon::S2_storerb_io)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001277 && I->getOperand(0).getReg() == QRI->getStackRegister()
1278 && QII->isValidOffset(I->getOpcode(),
1279 I->getOperand(1).getImm() -
1280 (FrameSize + HEXAGON_LRFP_SIZE)))
1281 {
1282 GlueAllocframeStore = true;
1283 // Since this store is to be glued with allocframe in the same
1284 // packet, it will use SP of the previous stack frame, i.e
1285 // caller's SP. Therefore, we need to recalculate offset according
1286 // to this change.
1287 I->getOperand(1).setImm(I->getOperand(1).getImm() -
1288 (FrameSize + HEXAGON_LRFP_SIZE));
1289 }
1290
1291 //
1292 // Skip over anti-dependences. Two instructions that are
1293 // anti-dependent can share a packet
1294 //
1295 else if (DepType != SDep::Anti) {
1296 FoundSequentialDependence = true;
1297 break;
1298 }
1299 }
1300
1301 if (FoundSequentialDependence) {
1302 Dependence = true;
1303 return false;
1304 }
1305 }
1306
1307 return true;
1308}
1309
1310// isLegalToPruneDependencies
1311bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
1312 MachineInstr *I = SUI->getInstr();
1313 assert(I && SUJ->getInstr() && "Unable to packetize null instruction!");
1314
1315 const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
1316
1317 if (Dependence) {
1318
1319 // Check if the instruction was promoted to a dot-new. If so, demote it
1320 // back into a dot-old.
1321 if (PromotedToDotNew) {
1322 DemoteToDotOld(I);
1323 }
1324
1325 // Check if the instruction (must be a store) was glued with an Allocframe
1326 // instruction. If so, restore its offset to its original value, i.e. use
1327 // curent SP instead of caller's SP.
1328 if (GlueAllocframeStore) {
1329 I->getOperand(1).setImm(I->getOperand(1).getImm() +
1330 FrameSize + HEXAGON_LRFP_SIZE);
1331 }
1332
1333 return false;
1334 }
1335 return true;
1336}
1337
1338MachineBasicBlock::iterator
1339HexagonPacketizerList::addToPacket(MachineInstr *MI) {
1340
1341 MachineBasicBlock::iterator MII = MI;
1342 MachineBasicBlock *MBB = MI->getParent();
1343
1344 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1345
1346 if (GlueToNewValueJump) {
1347
1348 ++MII;
1349 MachineInstr *nvjMI = MII;
1350 assert(ResourceTracker->canReserveResources(MI));
1351 ResourceTracker->reserveResources(MI);
Jyotsna Verma84256432013-03-01 17:37:13 +00001352 if ((QII->isExtended(MI) || QII->isConstExtended(MI)) &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001353 !tryAllocateResourcesForConstExt(MI)) {
1354 endPacket(MBB, MI);
1355 ResourceTracker->reserveResources(MI);
1356 assert(canReserveResourcesForConstExt(MI) &&
1357 "Ensure that there is a slot");
1358 reserveResourcesForConstExt(MI);
1359 // Reserve resources for new value jump constant extender.
1360 assert(canReserveResourcesForConstExt(MI) &&
1361 "Ensure that there is a slot");
1362 reserveResourcesForConstExt(nvjMI);
1363 assert(ResourceTracker->canReserveResources(nvjMI) &&
1364 "Ensure that there is a slot");
1365
1366 } else if ( // Extended instruction takes two slots in the packet.
1367 // Try reserve and allocate 4-byte in the current packet first.
1368 (QII->isExtended(nvjMI)
1369 && (!tryAllocateResourcesForConstExt(nvjMI)
1370 || !ResourceTracker->canReserveResources(nvjMI)))
1371 || // For non-extended instruction, no need to allocate extra 4 bytes.
Jyotsna Verma84256432013-03-01 17:37:13 +00001372 (!QII->isExtended(nvjMI) &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001373 !ResourceTracker->canReserveResources(nvjMI)))
1374 {
1375 endPacket(MBB, MI);
1376 // A new and empty packet starts.
1377 // We are sure that the resources requirements can be satisfied.
1378 // Therefore, do not need to call "canReserveResources" anymore.
1379 ResourceTracker->reserveResources(MI);
1380 if (QII->isExtended(nvjMI))
1381 reserveResourcesForConstExt(nvjMI);
1382 }
1383 // Here, we are sure that "reserveResources" would succeed.
1384 ResourceTracker->reserveResources(nvjMI);
1385 CurrentPacketMIs.push_back(MI);
1386 CurrentPacketMIs.push_back(nvjMI);
1387 } else {
Jyotsna Verma84256432013-03-01 17:37:13 +00001388 if ( (QII->isExtended(MI) || QII->isConstExtended(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001389 && ( !tryAllocateResourcesForConstExt(MI)
1390 || !ResourceTracker->canReserveResources(MI)))
1391 {
1392 endPacket(MBB, MI);
1393 // Check if the instruction was promoted to a dot-new. If so, demote it
1394 // back into a dot-old
1395 if (PromotedToDotNew) {
1396 DemoteToDotOld(MI);
1397 }
1398 reserveResourcesForConstExt(MI);
1399 }
1400 // In case that "MI" is not an extended insn,
1401 // the resource availability has already been checked.
1402 ResourceTracker->reserveResources(MI);
1403 CurrentPacketMIs.push_back(MI);
1404 }
1405 return MII;
1406}
1407
1408//===----------------------------------------------------------------------===//
1409// Public Constructor Functions
1410//===----------------------------------------------------------------------===//
1411
1412FunctionPass *llvm::createHexagonPacketizer() {
1413 return new HexagonPacketizer();
1414}
1415