blob: 59b4fabe01deb00cb8a856934895a99bb664b54c [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//===----------------------------------------------------------------------===//
19#define DEBUG_TYPE "packets"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/DFAPacketizer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/CodeGen/Passes.h"
Jyotsna Verma84256432013-03-01 17:37:13 +000022#include "llvm/CodeGen/MachineDominators.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineLoopInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/CodeGen/ScheduleDAG.h"
26#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Jyotsna Verma84256432013-03-01 17:37:13 +000027#include "llvm/CodeGen/LatencyPriorityQueue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/CodeGen/SchedulerRegistry.h"
Jyotsna Verma84256432013-03-01 17:37:13 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/CodeGen/MachineFunctionAnalysis.h"
33#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Target/TargetMachine.h"
Jyotsna Verma84256432013-03-01 17:37:13 +000035#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetRegisterInfo.h"
Jyotsna Verma84256432013-03-01 17:37:13 +000037#include "llvm/ADT/DenseMap.h"
38#include "llvm/ADT/Statistic.h"
39#include "llvm/Support/MathExtras.h"
40#include "llvm/MC/MCInstrItineraries.h"
41#include "llvm/Support/Compiler.h"
42#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/Debug.h"
44#include "Hexagon.h"
45#include "HexagonTargetMachine.h"
46#include "HexagonRegisterInfo.h"
47#include "HexagonSubtarget.h"
48#include "HexagonMachineFunctionInfo.h"
49
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000050#include <map>
Jyotsna Verma1d297502013-05-02 15:39:30 +000051#include <vector>
Sirish Pandef8e5e3c2012-05-03 21:52:53 +000052
53using namespace llvm;
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
73 void getAnalysisUsage(AnalysisUsage &AU) const {
74 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
83 const char *getPassName() const {
84 return "Hexagon Packetizer";
85 }
86
87 bool runOnMachineFunction(MachineFunction &Fn);
88 };
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 MachineDominatorTree &MDT,
122 const MachineBranchProbabilityInfo *MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000123
124 // initPacketizerState - initialize some internal flags.
125 void initPacketizerState();
126
127 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
128 bool ignorePseudoInstruction(MachineInstr *MI, MachineBasicBlock *MBB);
129
130 // isSoloInstruction - return true if instruction MI can not be packetized
131 // with any other instruction, which means that MI itself is a packet.
132 bool isSoloInstruction(MachineInstr *MI);
133
134 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
135 // together.
136 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ);
137
138 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
139 // and SUJ.
140 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ);
141
142 MachineBasicBlock::iterator addToPacket(MachineInstr *MI);
143 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);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000148 bool CanPromoteToDotNew(MachineInstr* MI, SUnit* PacketSU,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000149 unsigned DepReg,
150 std::map <MachineInstr*, SUnit*> MIToSUnit,
151 MachineBasicBlock::iterator &MII,
152 const TargetRegisterClass* RC);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000153 bool CanPromoteToNewValue(MachineInstr* MI, SUnit* PacketSU,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000154 unsigned DepReg,
155 std::map <MachineInstr*, SUnit*> MIToSUnit,
156 MachineBasicBlock::iterator &MII);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000157 bool CanPromoteToNewValueStore(MachineInstr* MI, MachineInstr* PacketMI,
Jyotsna Verma1d297502013-05-02 15:39:30 +0000158 unsigned DepReg,
159 std::map <MachineInstr*, SUnit*> MIToSUnit);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000160 bool DemoteToDotOld(MachineInstr* MI);
161 bool ArePredicatesComplements(MachineInstr* MI1, MachineInstr* MI2,
162 std::map <MachineInstr*, SUnit*> MIToSUnit);
163 bool RestrictingDepExistInPacket(MachineInstr*,
164 unsigned, std::map <MachineInstr*, SUnit*>);
165 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(
Jyotsna Verma1d297502013-05-02 15:39:30 +0000186 MachineFunction &MF, MachineLoopInfo &MLI,MachineDominatorTree &MDT,
187 const MachineBranchProbabilityInfo *MBPI)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000188 : VLIWPacketizerList(MF, MLI, MDT, 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) {
193 const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo();
194 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
195 MachineDominatorTree &MDT = getAnalysis<MachineDominatorTree>();
Jyotsna Verma1d297502013-05-02 15:39:30 +0000196 const MachineBranchProbabilityInfo *MBPI =
197 &getAnalysis<MachineBranchProbabilityInfo>();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000198 // Instantiate the packetizer.
Jyotsna Verma1d297502013-05-02 15:39:30 +0000199 HexagonPacketizerList Packetizer(Fn, MLI, MDT, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000200
201 // DFA state table should not be empty.
202 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
203
204 //
205 // Loop over all basic blocks and remove KILL pseudo-instructions
206 // These instructions confuse the dependence analysis. Consider:
207 // D0 = ... (Insn 0)
208 // R0 = KILL R0, D0 (Insn 1)
209 // R0 = ... (Insn 2)
210 // Here, Insn 1 will result in the dependence graph not emitting an output
211 // dependence between Insn 0 and Insn 2. This can lead to incorrect
212 // packetization
213 //
214 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
215 MBB != MBBe; ++MBB) {
216 MachineBasicBlock::iterator End = MBB->end();
217 MachineBasicBlock::iterator MI = MBB->begin();
218 while (MI != End) {
219 if (MI->isKill()) {
220 MachineBasicBlock::iterator DeleteMI = MI;
221 ++MI;
222 MBB->erase(DeleteMI);
223 End = MBB->end();
224 continue;
225 }
226 ++MI;
227 }
228 }
229
230 // Loop over all of the basic blocks.
231 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
232 MBB != MBBe; ++MBB) {
233 // Find scheduling regions and schedule / packetize each region.
234 unsigned RemainingCount = MBB->size();
235 for(MachineBasicBlock::iterator RegionEnd = MBB->end();
236 RegionEnd != MBB->begin();) {
237 // The next region starts above the previous region. Look backward in the
238 // instruction stream until we find the nearest boundary.
239 MachineBasicBlock::iterator I = RegionEnd;
240 for(;I != MBB->begin(); --I, --RemainingCount) {
241 if (TII->isSchedulingBoundary(llvm::prior(I), MBB, Fn))
242 break;
243 }
244 I = MBB->begin();
245
246 // Skip empty scheduling regions.
247 if (I == RegionEnd) {
248 RegionEnd = llvm::prior(RegionEnd);
249 --RemainingCount;
250 continue;
251 }
252 // Skip regions with one instruction.
253 if (I == llvm::prior(RegionEnd)) {
254 RegionEnd = llvm::prior(RegionEnd);
255 continue;
256 }
257
258 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
259 RegionEnd = I;
260 }
261 }
262
263 return true;
264}
265
266
267static bool IsIndirectCall(MachineInstr* MI) {
268 return ((MI->getOpcode() == Hexagon::CALLR) ||
269 (MI->getOpcode() == Hexagon::CALLRv3));
270}
271
272// Reserve resources for constant extender. Trigure an assertion if
273// reservation fail.
274void HexagonPacketizerList::reserveResourcesForConstExt(MachineInstr* MI) {
275 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Vermabf75aaf2012-12-20 06:45:39 +0000276 MachineFunction *MF = MI->getParent()->getParent();
277 MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::IMMEXT_i),
278 MI->getDebugLoc());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000279
280 if (ResourceTracker->canReserveResources(PseudoMI)) {
281 ResourceTracker->reserveResources(PseudoMI);
282 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
283 } else {
284 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
285 llvm_unreachable("can not reserve resources for constant extender.");
286 }
287 return;
288}
289
290bool HexagonPacketizerList::canReserveResourcesForConstExt(MachineInstr *MI) {
291 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Verma84256432013-03-01 17:37:13 +0000292 assert((QII->isExtended(MI) || QII->isConstExtended(MI)) &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000293 "Should only be called for constant extended instructions");
294 MachineFunction *MF = MI->getParent()->getParent();
Jyotsna Vermabf75aaf2012-12-20 06:45:39 +0000295 MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::IMMEXT_i),
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000296 MI->getDebugLoc());
297 bool CanReserve = ResourceTracker->canReserveResources(PseudoMI);
298 MF->DeleteMachineInstr(PseudoMI);
299 return CanReserve;
300}
301
302// Allocate resources (i.e. 4 bytes) for constant extender. If succeed, return
303// true, otherwise, return false.
304bool HexagonPacketizerList::tryAllocateResourcesForConstExt(MachineInstr* MI) {
305 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Vermabf75aaf2012-12-20 06:45:39 +0000306 MachineFunction *MF = MI->getParent()->getParent();
307 MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::IMMEXT_i),
308 MI->getDebugLoc());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000309
310 if (ResourceTracker->canReserveResources(PseudoMI)) {
311 ResourceTracker->reserveResources(PseudoMI);
312 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
313 return true;
314 } else {
315 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
316 return false;
317 }
318}
319
320
321bool HexagonPacketizerList::IsCallDependent(MachineInstr* MI,
322 SDep::Kind DepType,
323 unsigned DepReg) {
324
325 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
326 const HexagonRegisterInfo* QRI =
327 (const HexagonRegisterInfo *) TM.getRegisterInfo();
328
329 // Check for lr dependence
330 if (DepReg == QRI->getRARegister()) {
331 return true;
332 }
333
334 if (QII->isDeallocRet(MI)) {
335 if (DepReg == QRI->getFrameRegister() ||
336 DepReg == QRI->getStackRegister())
337 return true;
338 }
339
340 // Check if this is a predicate dependence
341 const TargetRegisterClass* RC = QRI->getMinimalPhysRegClass(DepReg);
342 if (RC == &Hexagon::PredRegsRegClass) {
343 return true;
344 }
345
346 //
347 // Lastly check for an operand used in an indirect call
348 // If we had an attribute for checking if an instruction is an indirect call,
349 // then we could have avoided this relatively brittle implementation of
350 // IsIndirectCall()
351 //
352 // Assumes that the first operand of the CALLr is the function address
353 //
354 if (IsIndirectCall(MI) && (DepType == SDep::Data)) {
355 MachineOperand MO = MI->getOperand(0);
356 if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg)) {
357 return true;
358 }
359 }
360
361 return false;
362}
363
364static bool IsRegDependence(const SDep::Kind DepType) {
365 return (DepType == SDep::Data || DepType == SDep::Anti ||
366 DepType == SDep::Output);
367}
368
369static bool IsDirectJump(MachineInstr* MI) {
370 return (MI->getOpcode() == Hexagon::JMP);
371}
372
373static bool IsSchedBarrier(MachineInstr* MI) {
374 switch (MI->getOpcode()) {
375 case Hexagon::BARRIER:
376 return true;
377 }
378 return false;
379}
380
381static bool IsControlFlow(MachineInstr* MI) {
382 return (MI->getDesc().isTerminator() || MI->getDesc().isCall());
383}
384
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000385static bool IsLoopN(MachineInstr *MI) {
386 return (MI->getOpcode() == Hexagon::LOOP0_i ||
387 MI->getOpcode() == Hexagon::LOOP0_r);
388}
389
390/// DoesModifyCalleeSavedReg - Returns true if the instruction modifies a
391/// callee-saved register.
392static bool DoesModifyCalleeSavedReg(MachineInstr *MI,
393 const TargetRegisterInfo *TRI) {
394 for (const uint16_t *CSR = TRI->getCalleeSavedRegs(); *CSR; ++CSR) {
395 unsigned CalleeSavedReg = *CSR;
396 if (MI->modifiesRegister(CalleeSavedReg, TRI))
397 return true;
398 }
399 return false;
400}
401
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000402// Returns true if an instruction can be promoted to .new predicate
403// or new-value store.
404bool HexagonPacketizerList::isNewifiable(MachineInstr* MI) {
Jyotsna Verma438cec52013-05-10 20:58:11 +0000405 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
406 if ( isCondInst(MI) || QII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000407 return true;
408 else
409 return false;
410}
411
412bool HexagonPacketizerList::isCondInst (MachineInstr* MI) {
413 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
414 const MCInstrDesc& TID = MI->getDesc();
415 // bug 5670: until that is fixed,
416 // this portion is disabled.
417 if ( TID.isConditionalBranch() // && !IsRegisterJump(MI)) ||
418 || QII->isConditionalTransfer(MI)
419 || QII->isConditionalALU32(MI)
420 || QII->isConditionalLoad(MI)
421 || QII->isConditionalStore(MI)) {
422 return true;
423 }
424 return false;
425}
426
Brendon Cahoonf6b687e2012-05-14 19:35:42 +0000427
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000428// Promote an instructiont to its .new form.
429// At this time, we have already made a call to CanPromoteToDotNew
430// and made sure that it can *indeed* be promoted.
431bool HexagonPacketizerList::PromoteToDotNew(MachineInstr* MI,
432 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
433 const TargetRegisterClass* RC) {
434
435 assert (DepType == SDep::Data);
436 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
437
438 int NewOpcode;
439 if (RC == &Hexagon::PredRegsRegClass)
Jyotsna Verma00681dc2013-05-09 19:16:07 +0000440 NewOpcode = QII->GetDotNewPredOp(MI, MBPI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000441 else
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000442 NewOpcode = QII->GetDotNewOp(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000443 MI->setDesc(QII->get(NewOpcode));
444
445 return true;
446}
447
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000448bool HexagonPacketizerList::DemoteToDotOld(MachineInstr* MI) {
449 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Verma438cec52013-05-10 20:58:11 +0000450 int NewOpcode = QII->GetDotOldOp(MI->getOpcode());
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000451 MI->setDesc(QII->get(NewOpcode));
452 return true;
453}
454
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000455enum PredicateKind {
456 PK_False,
457 PK_True,
458 PK_Unknown
459};
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000460
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000461/// Returns true if an instruction is predicated on p0 and false if it's
462/// predicated on !p0.
463static PredicateKind getPredicateSense(MachineInstr* MI,
464 const HexagonInstrInfo *QII) {
465 if (!QII->isPredicated(MI))
466 return PK_Unknown;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000467
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000468 if (QII->isPredicatedTrue(MI))
469 return PK_True;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000470
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000471 return PK_False;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000472}
473
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000474static MachineOperand& GetPostIncrementOperand(MachineInstr *MI,
475 const HexagonInstrInfo *QII) {
476 assert(QII->isPostIncrement(MI) && "Not a post increment operation.");
477#ifndef NDEBUG
478 // Post Increment means duplicates. Use dense map to find duplicates in the
479 // list. Caution: Densemap initializes with the minimum of 64 buckets,
480 // whereas there are at most 5 operands in the post increment.
481 DenseMap<unsigned, unsigned> DefRegsSet;
482 for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++)
483 if (MI->getOperand(opNum).isReg() &&
484 MI->getOperand(opNum).isDef()) {
485 DefRegsSet[MI->getOperand(opNum).getReg()] = 1;
486 }
487
488 for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++)
489 if (MI->getOperand(opNum).isReg() &&
490 MI->getOperand(opNum).isUse()) {
491 if (DefRegsSet[MI->getOperand(opNum).getReg()]) {
492 return MI->getOperand(opNum);
493 }
494 }
495#else
496 if (MI->getDesc().mayLoad()) {
497 // The 2nd operand is always the post increment operand in load.
498 assert(MI->getOperand(1).isReg() &&
499 "Post increment operand has be to a register.");
500 return (MI->getOperand(1));
501 }
502 if (MI->getDesc().mayStore()) {
503 // The 1st operand is always the post increment operand in store.
504 assert(MI->getOperand(0).isReg() &&
505 "Post increment operand has be to a register.");
506 return (MI->getOperand(0));
507 }
508#endif
509 // we should never come here.
510 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
511}
512
513// get the value being stored
514static MachineOperand& GetStoreValueOperand(MachineInstr *MI) {
515 // value being stored is always the last operand.
516 return (MI->getOperand(MI->getNumOperands()-1));
517}
518
519// can be new value store?
520// Following restrictions are to be respected in convert a store into
521// a new value store.
522// 1. If an instruction uses auto-increment, its address register cannot
523// be a new-value register. Arch Spec 5.4.2.1
524// 2. If an instruction uses absolute-set addressing mode,
525// its address register cannot be a new-value register.
526// Arch Spec 5.4.2.1.TODO: This is not enabled as
527// as absolute-set address mode patters are not implemented.
528// 3. If an instruction produces a 64-bit result, its registers cannot be used
529// as new-value registers. Arch Spec 5.4.2.2.
530// 4. If the instruction that sets a new-value register is conditional, then
531// the instruction that uses the new-value register must also be conditional,
532// and both must always have their predicates evaluate identically.
533// Arch Spec 5.4.2.3.
534// 5. There is an implied restriction of a packet can not have another store,
535// if there is a new value store in the packet. Corollary, if there is
536// already a store in a packet, there can not be a new value store.
537// Arch Spec: 3.4.4.2
538bool HexagonPacketizerList::CanPromoteToNewValueStore( MachineInstr *MI,
539 MachineInstr *PacketMI, unsigned DepReg,
Jyotsna Verma438cec52013-05-10 20:58:11 +0000540 std::map <MachineInstr*, SUnit*> MIToSUnit) {
541 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
542 // Make sure we are looking at the store, that can be promoted.
543 if (!QII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000544 return false;
545
546 // Make sure there is dependency and can be new value'ed
547 if (GetStoreValueOperand(MI).isReg() &&
548 GetStoreValueOperand(MI).getReg() != DepReg)
549 return false;
550
Jyotsna Verma438cec52013-05-10 20:58:11 +0000551 const HexagonRegisterInfo* QRI =
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000552 (const HexagonRegisterInfo *) TM.getRegisterInfo();
553 const MCInstrDesc& MCID = PacketMI->getDesc();
554 // first operand is always the result
555
Jakob Stoklund Olesen3c52f022012-05-07 22:10:26 +0000556 const TargetRegisterClass* PacketRC = QII->getRegClass(MCID, 0, QRI, MF);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000557
558 // if there is already an store in the packet, no can do new value store
559 // Arch Spec 3.4.4.2.
560 for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(),
561 VE = CurrentPacketMIs.end();
562 (VI != VE); ++VI) {
563 SUnit* PacketSU = MIToSUnit[*VI];
564 if (PacketSU->getInstr()->getDesc().mayStore() ||
565 // if we have mayStore = 1 set on ALLOCFRAME and DEALLOCFRAME,
566 // then we don't need this
567 PacketSU->getInstr()->getOpcode() == Hexagon::ALLOCFRAME ||
568 PacketSU->getInstr()->getOpcode() == Hexagon::DEALLOCFRAME)
569 return false;
570 }
571
572 if (PacketRC == &Hexagon::DoubleRegsRegClass) {
573 // new value store constraint: double regs can not feed into new value store
574 // arch spec section: 5.4.2.2
575 return false;
576 }
577
578 // Make sure it's NOT the post increment register that we are going to
579 // new value.
580 if (QII->isPostIncrement(MI) &&
581 MI->getDesc().mayStore() &&
582 GetPostIncrementOperand(MI, QII).getReg() == DepReg) {
583 return false;
584 }
585
586 if (QII->isPostIncrement(PacketMI) &&
587 PacketMI->getDesc().mayLoad() &&
588 GetPostIncrementOperand(PacketMI, QII).getReg() == DepReg) {
589 // if source is post_inc, or absolute-set addressing,
590 // it can not feed into new value store
591 // r3 = memw(r2++#4)
592 // memw(r30 + #-1404) = r2.new -> can not be new value store
593 // arch spec section: 5.4.2.1
594 return false;
595 }
596
597 // If the source that feeds the store is predicated, new value store must
Jyotsna Verma438cec52013-05-10 20:58:11 +0000598 // also be predicated.
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000599 if (QII->isPredicated(PacketMI)) {
600 if (!QII->isPredicated(MI))
601 return false;
602
603 // Check to make sure that they both will have their predicates
604 // evaluate identically
Sirish Pande95d01172012-05-11 20:00:34 +0000605 unsigned predRegNumSrc = 0;
606 unsigned predRegNumDst = 0;
607 const TargetRegisterClass* predRegClass = NULL;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000608
609 // Get predicate register used in the source instruction
610 for(unsigned opNum = 0; opNum < PacketMI->getNumOperands(); opNum++) {
611 if ( PacketMI->getOperand(opNum).isReg())
612 predRegNumSrc = PacketMI->getOperand(opNum).getReg();
613 predRegClass = QRI->getMinimalPhysRegClass(predRegNumSrc);
614 if (predRegClass == &Hexagon::PredRegsRegClass) {
615 break;
616 }
617 }
618 assert ((predRegClass == &Hexagon::PredRegsRegClass ) &&
619 ("predicate register not found in a predicated PacketMI instruction"));
620
621 // Get predicate register used in new-value store instruction
622 for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
623 if ( MI->getOperand(opNum).isReg())
624 predRegNumDst = MI->getOperand(opNum).getReg();
625 predRegClass = QRI->getMinimalPhysRegClass(predRegNumDst);
626 if (predRegClass == &Hexagon::PredRegsRegClass) {
627 break;
628 }
629 }
630 assert ((predRegClass == &Hexagon::PredRegsRegClass ) &&
631 ("predicate register not found in a predicated MI instruction"));
632
633 // New-value register producer and user (store) need to satisfy these
634 // constraints:
635 // 1) Both instructions should be predicated on the same register.
636 // 2) If producer of the new-value register is .new predicated then store
637 // should also be .new predicated and if producer is not .new predicated
638 // then store should not be .new predicated.
639 // 3) Both new-value register producer and user should have same predicate
640 // sense, i.e, either both should be negated or both should be none negated.
641
642 if (( predRegNumDst != predRegNumSrc) ||
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000643 QII->isDotNewInst(PacketMI) != QII->isDotNewInst(MI) ||
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000644 getPredicateSense(MI, QII) != getPredicateSense(PacketMI, QII)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000645 return false;
646 }
647 }
648
649 // Make sure that other than the new-value register no other store instruction
650 // register has been modified in the same packet. Predicate registers can be
651 // modified by they should not be modified between the producer and the store
652 // instruction as it will make them both conditional on different values.
653 // We already know this to be true for all the instructions before and
654 // including PacketMI. Howerver, we need to perform the check for the
655 // remaining instructions in the packet.
656
657 std::vector<MachineInstr*>::iterator VI;
658 std::vector<MachineInstr*>::iterator VE;
659 unsigned StartCheck = 0;
660
661 for (VI=CurrentPacketMIs.begin(), VE = CurrentPacketMIs.end();
662 (VI != VE); ++VI) {
663 SUnit* TempSU = MIToSUnit[*VI];
664 MachineInstr* TempMI = TempSU->getInstr();
665
666 // Following condition is true for all the instructions until PacketMI is
667 // reached (StartCheck is set to 0 before the for loop).
668 // StartCheck flag is 1 for all the instructions after PacketMI.
669 if (TempMI != PacketMI && !StartCheck) // start processing only after
670 continue; // encountering PacketMI
671
672 StartCheck = 1;
673 if (TempMI == PacketMI) // We don't want to check PacketMI for dependence
674 continue;
675
676 for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
677 if (MI->getOperand(opNum).isReg() &&
678 TempSU->getInstr()->modifiesRegister(MI->getOperand(opNum).getReg(),
679 QRI))
680 return false;
681 }
682 }
683
684 // Make sure that for non POST_INC stores:
685 // 1. The only use of reg is DepReg and no other registers.
686 // This handles V4 base+index registers.
687 // The following store can not be dot new.
688 // Eg. r0 = add(r0, #3)a
689 // memw(r1+r0<<#2) = r0
690 if (!QII->isPostIncrement(MI) &&
691 GetStoreValueOperand(MI).isReg() &&
692 GetStoreValueOperand(MI).getReg() == DepReg) {
693 for(unsigned opNum = 0; opNum < MI->getNumOperands()-1; opNum++) {
694 if (MI->getOperand(opNum).isReg() &&
695 MI->getOperand(opNum).getReg() == DepReg) {
696 return false;
697 }
698 }
699 // 2. If data definition is because of implicit definition of the register,
700 // do not newify the store. Eg.
701 // %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
702 // STrih_indexed %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
703 for(unsigned opNum = 0; opNum < PacketMI->getNumOperands(); opNum++) {
704 if (PacketMI->getOperand(opNum).isReg() &&
705 PacketMI->getOperand(opNum).getReg() == DepReg &&
706 PacketMI->getOperand(opNum).isDef() &&
707 PacketMI->getOperand(opNum).isImplicit()) {
708 return false;
709 }
710 }
711 }
712
713 // Can be dot new store.
714 return true;
715}
716
717// can this MI to promoted to either
718// new value store or new value jump
719bool HexagonPacketizerList::CanPromoteToNewValue( MachineInstr *MI,
720 SUnit *PacketSU, unsigned DepReg,
721 std::map <MachineInstr*, SUnit*> MIToSUnit,
722 MachineBasicBlock::iterator &MII)
723{
724
Jyotsna Verma438cec52013-05-10 20:58:11 +0000725 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000726 const HexagonRegisterInfo* QRI =
727 (const HexagonRegisterInfo *) TM.getRegisterInfo();
728 if (!QRI->Subtarget.hasV4TOps() ||
Jyotsna Verma438cec52013-05-10 20:58:11 +0000729 !QII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000730 return false;
731
732 MachineInstr *PacketMI = PacketSU->getInstr();
733
734 // Check to see the store can be new value'ed.
735 if (CanPromoteToNewValueStore(MI, PacketMI, DepReg, MIToSUnit))
736 return true;
737
738 // Check to see the compare/jump can be new value'ed.
739 // This is done as a pass on its own. Don't need to check it here.
740 return false;
741}
742
743// Check to see if an instruction can be dot new
744// There are three kinds.
745// 1. dot new on predicate - V2/V3/V4
746// 2. dot new on stores NV/ST - V4
747// 3. dot new on jump NV/J - V4 -- This is generated in a pass.
748bool HexagonPacketizerList::CanPromoteToDotNew( MachineInstr *MI,
749 SUnit *PacketSU, unsigned DepReg,
750 std::map <MachineInstr*, SUnit*> MIToSUnit,
751 MachineBasicBlock::iterator &MII,
752 const TargetRegisterClass* RC )
753{
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000754 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
755 // Already a dot new instruction.
Jyotsna Verma438cec52013-05-10 20:58:11 +0000756 if (QII->isDotNewInst(MI) && !QII->mayBeNewStore(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000757 return false;
758
759 if (!isNewifiable(MI))
760 return false;
761
762 // predicate .new
763 if (RC == &Hexagon::PredRegsRegClass && isCondInst(MI))
764 return true;
765 else if (RC != &Hexagon::PredRegsRegClass &&
Jyotsna Verma438cec52013-05-10 20:58:11 +0000766 !QII->mayBeNewStore(MI)) // MI is not a new-value store
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000767 return false;
768 else {
769 // Create a dot new machine instruction to see if resources can be
770 // allocated. If not, bail out now.
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000771 int NewOpcode = QII->GetDotNewOp(MI);
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000772 const MCInstrDesc &desc = QII->get(NewOpcode);
773 DebugLoc dl;
774 MachineInstr *NewMI =
775 MI->getParent()->getParent()->CreateMachineInstr(desc, dl);
776 bool ResourcesAvailable = ResourceTracker->canReserveResources(NewMI);
777 MI->getParent()->getParent()->DeleteMachineInstr(NewMI);
778
779 if (!ResourcesAvailable)
780 return false;
781
782 // new value store only
783 // new new value jump generated as a passes
784 if (!CanPromoteToNewValue(MI, PacketSU, DepReg, MIToSUnit, MII)) {
785 return false;
786 }
787 }
788 return true;
789}
790
791// Go through the packet instructions and search for anti dependency
792// between them and DepReg from MI
793// Consider this case:
794// Trying to add
795// a) %R1<def> = TFRI_cdNotPt %P3, 2
796// to this packet:
797// {
798// b) %P0<def> = OR_pp %P3<kill>, %P0<kill>
799// c) %P3<def> = TFR_PdRs %R23
800// d) %R1<def> = TFRI_cdnPt %P3, 4
801// }
802// The P3 from a) and d) will be complements after
803// a)'s P3 is converted to .new form
804// Anti Dep between c) and b) is irrelevant for this case
805bool HexagonPacketizerList::RestrictingDepExistInPacket (MachineInstr* MI,
806 unsigned DepReg,
807 std::map <MachineInstr*, SUnit*> MIToSUnit) {
808
809 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
810 SUnit* PacketSUDep = MIToSUnit[MI];
811
812 for (std::vector<MachineInstr*>::iterator VIN = CurrentPacketMIs.begin(),
813 VEN = CurrentPacketMIs.end(); (VIN != VEN); ++VIN) {
814
815 // We only care for dependencies to predicated instructions
816 if(!QII->isPredicated(*VIN)) continue;
817
818 // Scheduling Unit for current insn in the packet
819 SUnit* PacketSU = MIToSUnit[*VIN];
820
821 // Look at dependencies between current members of the packet
822 // and predicate defining instruction MI.
823 // Make sure that dependency is on the exact register
824 // we care about.
825 if (PacketSU->isSucc(PacketSUDep)) {
826 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
827 if ((PacketSU->Succs[i].getSUnit() == PacketSUDep) &&
828 (PacketSU->Succs[i].getKind() == SDep::Anti) &&
829 (PacketSU->Succs[i].getReg() == DepReg)) {
830 return true;
831 }
832 }
833 }
834 }
835
836 return false;
837}
838
839
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000840/// Gets the predicate register of a predicated instruction.
841unsigned getPredicatedRegister(MachineInstr *MI, const HexagonInstrInfo *QII) {
842 /// We use the following rule: The first predicate register that is a use is
843 /// the predicate register of a predicated instruction.
844
845 assert(QII->isPredicated(MI) && "Must be predicated instruction");
846
847 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
848 OE = MI->operands_end(); OI != OE; ++OI) {
849 MachineOperand &Op = *OI;
850 if (Op.isReg() && Op.getReg() && Op.isUse() &&
851 Hexagon::PredRegsRegClass.contains(Op.getReg()))
852 return Op.getReg();
853 }
854
855 llvm_unreachable("Unknown instruction operand layout");
856
857 return 0;
858}
859
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000860// Given two predicated instructions, this function detects whether
861// the predicates are complements
862bool HexagonPacketizerList::ArePredicatesComplements (MachineInstr* MI1,
863 MachineInstr* MI2, std::map <MachineInstr*, SUnit*> MIToSUnit) {
864
865 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000866
867 // If we don't know the predicate sense of the instructions bail out early, we
868 // need it later.
869 if (getPredicateSense(MI1, QII) == PK_Unknown ||
870 getPredicateSense(MI2, QII) == PK_Unknown)
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000871 return false;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000872
873 // Scheduling unit for candidate
874 SUnit* SU = MIToSUnit[MI1];
875
876 // One corner case deals with the following scenario:
877 // Trying to add
878 // a) %R24<def> = TFR_cPt %P0, %R25
879 // to this packet:
880 //
881 // {
882 // b) %R25<def> = TFR_cNotPt %P0, %R24
883 // c) %P0<def> = CMPEQri %R26, 1
884 // }
885 //
886 // On general check a) and b) are complements, but
887 // presence of c) will convert a) to .new form, and
888 // then it is not a complement
889 // We attempt to detect it by analyzing existing
890 // dependencies in the packet
891
892 // Analyze relationships between all existing members of the packet.
893 // Look for Anti dependecy on the same predicate reg
894 // as used in the candidate
895 for (std::vector<MachineInstr*>::iterator VIN = CurrentPacketMIs.begin(),
896 VEN = CurrentPacketMIs.end(); (VIN != VEN); ++VIN) {
897
898 // Scheduling Unit for current insn in the packet
899 SUnit* PacketSU = MIToSUnit[*VIN];
900
901 // If this instruction in the packet is succeeded by the candidate...
902 if (PacketSU->isSucc(SU)) {
903 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
904 // The corner case exist when there is true data
905 // dependency between candidate and one of current
906 // packet members, this dep is on predicate reg, and
907 // there already exist anti dep on the same pred in
908 // the packet.
909 if (PacketSU->Succs[i].getSUnit() == SU &&
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000910 PacketSU->Succs[i].getKind() == SDep::Data &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000911 Hexagon::PredRegsRegClass.contains(
912 PacketSU->Succs[i].getReg()) &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000913 // Here I know that *VIN is predicate setting instruction
914 // with true data dep to candidate on the register
915 // we care about - c) in the above example.
916 // Now I need to see if there is an anti dependency
917 // from c) to any other instruction in the
918 // same packet on the pred reg of interest
919 RestrictingDepExistInPacket(*VIN,PacketSU->Succs[i].getReg(),
920 MIToSUnit)) {
921 return false;
922 }
923 }
924 }
925 }
926
927 // If the above case does not apply, check regular
928 // complement condition.
929 // Check that the predicate register is the same and
930 // that the predicate sense is different
931 // We also need to differentiate .old vs. .new:
932 // !p0 is not complimentary to p0.new
Jyotsna Verma11bd54a2013-05-14 16:36:34 +0000933 unsigned PReg1 = getPredicatedRegister(MI1, QII);
934 unsigned PReg2 = getPredicatedRegister(MI2, QII);
935 return ((PReg1 == PReg2) &&
936 Hexagon::PredRegsRegClass.contains(PReg1) &&
937 Hexagon::PredRegsRegClass.contains(PReg2) &&
Jyotsna Verma300f0b92013-05-10 20:27:34 +0000938 (getPredicateSense(MI1, QII) != getPredicateSense(MI2, QII)) &&
Jyotsna Vermaa46059b2013-03-28 19:44:04 +0000939 (QII->isDotNewInst(MI1) == QII->isDotNewInst(MI2)));
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000940}
941
942// initPacketizerState - Initialize packetizer flags
943void HexagonPacketizerList::initPacketizerState() {
944
945 Dependence = false;
946 PromotedToDotNew = false;
947 GlueToNewValueJump = false;
948 GlueAllocframeStore = false;
949 FoundSequentialDependence = false;
950
951 return;
952}
953
954// ignorePseudoInstruction - Ignore bundling of pseudo instructions.
955bool HexagonPacketizerList::ignorePseudoInstruction(MachineInstr *MI,
956 MachineBasicBlock *MBB) {
957 if (MI->isDebugValue())
958 return true;
959
960 // We must print out inline assembly
961 if (MI->isInlineAsm())
962 return false;
963
964 // We check if MI has any functional units mapped to it.
965 // If it doesn't, we ignore the instruction.
966 const MCInstrDesc& TID = MI->getDesc();
967 unsigned SchedClass = TID.getSchedClass();
968 const InstrStage* IS =
969 ResourceTracker->getInstrItins()->beginStage(SchedClass);
Hal Finkel8db55472012-06-22 20:27:13 +0000970 unsigned FuncUnits = IS->getUnits();
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000971 return !FuncUnits;
972}
973
974// isSoloInstruction: - Returns true for instructions that must be
975// scheduled in their own packet.
976bool HexagonPacketizerList::isSoloInstruction(MachineInstr *MI) {
977
978 if (MI->isInlineAsm())
979 return true;
980
981 if (MI->isEHLabel())
982 return true;
983
984 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
985 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
986 // They must not be grouped with other instructions in a packet.
987 if (IsSchedBarrier(MI))
988 return true;
989
990 return false;
991}
992
993// isLegalToPacketizeTogether:
994// SUI is the current instruction that is out side of the current packet.
995// SUJ is the current instruction inside the current packet against which that
996// SUI will be packetized.
997bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
998 MachineInstr *I = SUI->getInstr();
999 MachineInstr *J = SUJ->getInstr();
1000 assert(I && J && "Unable to packetize null instruction!");
1001
1002 const MCInstrDesc &MCIDI = I->getDesc();
1003 const MCInstrDesc &MCIDJ = J->getDesc();
1004
1005 MachineBasicBlock::iterator II = I;
1006
1007 const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
1008 const HexagonRegisterInfo* QRI =
1009 (const HexagonRegisterInfo *) TM.getRegisterInfo();
1010 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1011
1012 // Inline asm cannot go in the packet.
1013 if (I->getOpcode() == Hexagon::INLINEASM)
1014 llvm_unreachable("Should not meet inline asm here!");
1015
1016 if (isSoloInstruction(I))
1017 llvm_unreachable("Should not meet solo instr here!");
1018
1019 // A save callee-save register function call can only be in a packet
1020 // with instructions that don't write to the callee-save registers.
1021 if ((QII->isSaveCalleeSavedRegsCall(I) &&
1022 DoesModifyCalleeSavedReg(J, QRI)) ||
1023 (QII->isSaveCalleeSavedRegsCall(J) &&
1024 DoesModifyCalleeSavedReg(I, QRI))) {
1025 Dependence = true;
1026 return false;
1027 }
1028
1029 // Two control flow instructions cannot go in the same packet.
1030 if (IsControlFlow(I) && IsControlFlow(J)) {
1031 Dependence = true;
1032 return false;
1033 }
1034
1035 // A LoopN instruction cannot appear in the same packet as a jump or call.
Jyotsna Verma438cec52013-05-10 20:58:11 +00001036 if (IsLoopN(I) &&
1037 (IsDirectJump(J) || MCIDJ.isCall() || QII->isDeallocRet(J))) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001038 Dependence = true;
1039 return false;
1040 }
Jyotsna Verma438cec52013-05-10 20:58:11 +00001041 if (IsLoopN(J) &&
1042 (IsDirectJump(I) || MCIDI.isCall() || QII->isDeallocRet(I))) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001043 Dependence = true;
1044 return false;
1045 }
1046
1047 // dealloc_return cannot appear in the same packet as a conditional or
1048 // unconditional jump.
Jyotsna Verma438cec52013-05-10 20:58:11 +00001049 if (QII->isDeallocRet(I) &&
1050 (MCIDJ.isBranch() || MCIDJ.isCall() || MCIDJ.isBarrier())) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001051 Dependence = true;
1052 return false;
1053 }
1054
1055
1056 // V4 allows dual store. But does not allow second store, if the
1057 // first store is not in SLOT0. New value store, new value jump,
1058 // dealloc_return and memop always take SLOT0.
1059 // Arch spec 3.4.4.2
1060 if (QRI->Subtarget.hasV4TOps()) {
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001061 if (MCIDI.mayStore() && MCIDJ.mayStore() &&
1062 (QII->isNewValueInst(J) || QII->isMemOp(J) || QII->isMemOp(I))) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001063 Dependence = true;
1064 return false;
1065 }
1066
Jyotsna Vermaf1214a82013-03-05 18:51:42 +00001067 if ((QII->isMemOp(J) && MCIDI.mayStore())
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001068 || (MCIDJ.mayStore() && QII->isMemOp(I))
1069 || (QII->isMemOp(J) && QII->isMemOp(I))) {
1070 Dependence = true;
1071 return false;
1072 }
1073
1074 //if dealloc_return
Jyotsna Verma438cec52013-05-10 20:58:11 +00001075 if (MCIDJ.mayStore() && QII->isDeallocRet(I)) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001076 Dependence = true;
1077 return false;
1078 }
1079
1080 // If an instruction feeds new value jump, glue it.
1081 MachineBasicBlock::iterator NextMII = I;
1082 ++NextMII;
Jyotsna Verma84c47102013-05-06 18:49:23 +00001083 if (NextMII != I->getParent()->end() && QII->isNewValueJump(NextMII)) {
1084 MachineInstr *NextMI = NextMII;
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001085
1086 bool secondRegMatch = false;
1087 bool maintainNewValueJump = false;
1088
1089 if (NextMI->getOperand(1).isReg() &&
1090 I->getOperand(0).getReg() == NextMI->getOperand(1).getReg()) {
1091 secondRegMatch = true;
1092 maintainNewValueJump = true;
1093 }
1094
1095 if (!secondRegMatch &&
1096 I->getOperand(0).getReg() == NextMI->getOperand(0).getReg()) {
1097 maintainNewValueJump = true;
1098 }
1099
1100 for (std::vector<MachineInstr*>::iterator
1101 VI = CurrentPacketMIs.begin(),
1102 VE = CurrentPacketMIs.end();
1103 (VI != VE && maintainNewValueJump); ++VI) {
1104 SUnit* PacketSU = MIToSUnit[*VI];
1105
1106 // NVJ can not be part of the dual jump - Arch Spec: section 7.8
1107 if (PacketSU->getInstr()->getDesc().isCall()) {
1108 Dependence = true;
1109 break;
1110 }
1111 // Validate
1112 // 1. Packet does not have a store in it.
1113 // 2. If the first operand of the nvj is newified, and the second
1114 // operand is also a reg, it (second reg) is not defined in
1115 // the same packet.
1116 // 3. If the second operand of the nvj is newified, (which means
1117 // first operand is also a reg), first reg is not defined in
1118 // the same packet.
1119 if (PacketSU->getInstr()->getDesc().mayStore() ||
1120 PacketSU->getInstr()->getOpcode() == Hexagon::ALLOCFRAME ||
1121 // Check #2.
1122 (!secondRegMatch && NextMI->getOperand(1).isReg() &&
1123 PacketSU->getInstr()->modifiesRegister(
1124 NextMI->getOperand(1).getReg(), QRI)) ||
1125 // Check #3.
1126 (secondRegMatch &&
1127 PacketSU->getInstr()->modifiesRegister(
1128 NextMI->getOperand(0).getReg(), QRI))) {
1129 Dependence = true;
1130 break;
1131 }
1132 }
1133 if (!Dependence)
1134 GlueToNewValueJump = true;
1135 else
1136 return false;
1137 }
1138 }
1139
1140 if (SUJ->isSucc(SUI)) {
1141 for (unsigned i = 0;
1142 (i < SUJ->Succs.size()) && !FoundSequentialDependence;
1143 ++i) {
1144
1145 if (SUJ->Succs[i].getSUnit() != SUI) {
1146 continue;
1147 }
1148
1149 SDep::Kind DepType = SUJ->Succs[i].getKind();
1150
1151 // For direct calls:
1152 // Ignore register dependences for call instructions for
1153 // packetization purposes except for those due to r31 and
1154 // predicate registers.
1155 //
1156 // For indirect calls:
1157 // Same as direct calls + check for true dependences to the register
1158 // used in the indirect call.
1159 //
1160 // We completely ignore Order dependences for call instructions
1161 //
1162 // For returns:
1163 // Ignore register dependences for return instructions like jumpr,
1164 // dealloc return unless we have dependencies on the explicit uses
1165 // of the registers used by jumpr (like r31) or dealloc return
1166 // (like r29 or r30).
1167 //
1168 // TODO: Currently, jumpr is handling only return of r31. So, the
1169 // following logic (specificaly IsCallDependent) is working fine.
1170 // We need to enable jumpr for register other than r31 and then,
1171 // we need to rework the last part, where it handles indirect call
1172 // of that (IsCallDependent) function. Bug 6216 is opened for this.
1173 //
1174 unsigned DepReg = 0;
1175 const TargetRegisterClass* RC = NULL;
1176 if (DepType == SDep::Data) {
1177 DepReg = SUJ->Succs[i].getReg();
1178 RC = QRI->getMinimalPhysRegClass(DepReg);
1179 }
1180 if ((MCIDI.isCall() || MCIDI.isReturn()) &&
1181 (!IsRegDependence(DepType) ||
1182 !IsCallDependent(I, DepType, SUJ->Succs[i].getReg()))) {
1183 /* do nothing */
1184 }
1185
1186 // For instructions that can be promoted to dot-new, try to promote.
1187 else if ((DepType == SDep::Data) &&
1188 CanPromoteToDotNew(I, SUJ, DepReg, MIToSUnit, II, RC) &&
1189 PromoteToDotNew(I, DepType, II, RC)) {
1190 PromotedToDotNew = true;
1191 /* do nothing */
1192 }
1193
1194 else if ((DepType == SDep::Data) &&
1195 (QII->isNewValueJump(I))) {
1196 /* do nothing */
1197 }
1198
1199 // For predicated instructions, if the predicates are complements
1200 // then there can be no dependence.
1201 else if (QII->isPredicated(I) &&
1202 QII->isPredicated(J) &&
1203 ArePredicatesComplements(I, J, MIToSUnit)) {
1204 /* do nothing */
1205
1206 }
1207 else if (IsDirectJump(I) &&
1208 !MCIDJ.isBranch() &&
1209 !MCIDJ.isCall() &&
1210 (DepType == SDep::Order)) {
1211 // Ignore Order dependences between unconditional direct branches
1212 // and non-control-flow instructions
1213 /* do nothing */
1214 }
1215 else if (MCIDI.isConditionalBranch() && (DepType != SDep::Data) &&
1216 (DepType != SDep::Output)) {
1217 // Ignore all dependences for jumps except for true and output
1218 // dependences
1219 /* do nothing */
1220 }
1221
1222 // Ignore output dependences due to superregs. We can
1223 // write to two different subregisters of R1:0 for instance
1224 // in the same cycle
1225 //
1226
1227 //
1228 // Let the
1229 // If neither I nor J defines DepReg, then this is a
1230 // superfluous output dependence. The dependence must be of the
1231 // form:
1232 // R0 = ...
1233 // R1 = ...
1234 // and there is an output dependence between the two instructions
1235 // with
1236 // DepReg = D0
1237 // We want to ignore these dependences.
1238 // Ideally, the dependence constructor should annotate such
1239 // dependences. We can then avoid this relatively expensive check.
1240 //
1241 else if (DepType == SDep::Output) {
1242 // DepReg is the register that's responsible for the dependence.
1243 unsigned DepReg = SUJ->Succs[i].getReg();
1244
1245 // Check if I and J really defines DepReg.
1246 if (I->definesRegister(DepReg) ||
1247 J->definesRegister(DepReg)) {
1248 FoundSequentialDependence = true;
1249 break;
1250 }
1251 }
1252
1253 // We ignore Order dependences for
1254 // 1. Two loads unless they are volatile.
1255 // 2. Two stores in V4 unless they are volatile.
1256 else if ((DepType == SDep::Order) &&
Jakob Stoklund Olesencea3e772012-08-29 21:19:21 +00001257 !I->hasOrderedMemoryRef() &&
1258 !J->hasOrderedMemoryRef()) {
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001259 if (QRI->Subtarget.hasV4TOps() &&
1260 // hexagonv4 allows dual store.
1261 MCIDI.mayStore() && MCIDJ.mayStore()) {
1262 /* do nothing */
1263 }
1264 // store followed by store-- not OK on V2
1265 // store followed by load -- not OK on all (OK if addresses
1266 // are not aliased)
1267 // load followed by store -- OK on all
1268 // load followed by load -- OK on all
1269 else if ( !MCIDJ.mayStore()) {
1270 /* do nothing */
1271 }
1272 else {
1273 FoundSequentialDependence = true;
1274 break;
1275 }
1276 }
1277
1278 // For V4, special case ALLOCFRAME. Even though there is dependency
1279 // between ALLOCAFRAME and subsequent store, allow it to be
1280 // packetized in a same packet. This implies that the store is using
1281 // caller's SP. Hense, offset needs to be updated accordingly.
1282 else if (DepType == SDep::Data
1283 && QRI->Subtarget.hasV4TOps()
1284 && J->getOpcode() == Hexagon::ALLOCFRAME
1285 && (I->getOpcode() == Hexagon::STrid
1286 || I->getOpcode() == Hexagon::STriw
1287 || I->getOpcode() == Hexagon::STrib)
1288 && I->getOperand(0).getReg() == QRI->getStackRegister()
1289 && QII->isValidOffset(I->getOpcode(),
1290 I->getOperand(1).getImm() -
1291 (FrameSize + HEXAGON_LRFP_SIZE)))
1292 {
1293 GlueAllocframeStore = true;
1294 // Since this store is to be glued with allocframe in the same
1295 // packet, it will use SP of the previous stack frame, i.e
1296 // caller's SP. Therefore, we need to recalculate offset according
1297 // to this change.
1298 I->getOperand(1).setImm(I->getOperand(1).getImm() -
1299 (FrameSize + HEXAGON_LRFP_SIZE));
1300 }
1301
1302 //
1303 // Skip over anti-dependences. Two instructions that are
1304 // anti-dependent can share a packet
1305 //
1306 else if (DepType != SDep::Anti) {
1307 FoundSequentialDependence = true;
1308 break;
1309 }
1310 }
1311
1312 if (FoundSequentialDependence) {
1313 Dependence = true;
1314 return false;
1315 }
1316 }
1317
1318 return true;
1319}
1320
1321// isLegalToPruneDependencies
1322bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
1323 MachineInstr *I = SUI->getInstr();
1324 assert(I && SUJ->getInstr() && "Unable to packetize null instruction!");
1325
1326 const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
1327
1328 if (Dependence) {
1329
1330 // Check if the instruction was promoted to a dot-new. If so, demote it
1331 // back into a dot-old.
1332 if (PromotedToDotNew) {
1333 DemoteToDotOld(I);
1334 }
1335
1336 // Check if the instruction (must be a store) was glued with an Allocframe
1337 // instruction. If so, restore its offset to its original value, i.e. use
1338 // curent SP instead of caller's SP.
1339 if (GlueAllocframeStore) {
1340 I->getOperand(1).setImm(I->getOperand(1).getImm() +
1341 FrameSize + HEXAGON_LRFP_SIZE);
1342 }
1343
1344 return false;
1345 }
1346 return true;
1347}
1348
1349MachineBasicBlock::iterator
1350HexagonPacketizerList::addToPacket(MachineInstr *MI) {
1351
1352 MachineBasicBlock::iterator MII = MI;
1353 MachineBasicBlock *MBB = MI->getParent();
1354
1355 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1356
1357 if (GlueToNewValueJump) {
1358
1359 ++MII;
1360 MachineInstr *nvjMI = MII;
1361 assert(ResourceTracker->canReserveResources(MI));
1362 ResourceTracker->reserveResources(MI);
Jyotsna Verma84256432013-03-01 17:37:13 +00001363 if ((QII->isExtended(MI) || QII->isConstExtended(MI)) &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001364 !tryAllocateResourcesForConstExt(MI)) {
1365 endPacket(MBB, MI);
1366 ResourceTracker->reserveResources(MI);
1367 assert(canReserveResourcesForConstExt(MI) &&
1368 "Ensure that there is a slot");
1369 reserveResourcesForConstExt(MI);
1370 // Reserve resources for new value jump constant extender.
1371 assert(canReserveResourcesForConstExt(MI) &&
1372 "Ensure that there is a slot");
1373 reserveResourcesForConstExt(nvjMI);
1374 assert(ResourceTracker->canReserveResources(nvjMI) &&
1375 "Ensure that there is a slot");
1376
1377 } else if ( // Extended instruction takes two slots in the packet.
1378 // Try reserve and allocate 4-byte in the current packet first.
1379 (QII->isExtended(nvjMI)
1380 && (!tryAllocateResourcesForConstExt(nvjMI)
1381 || !ResourceTracker->canReserveResources(nvjMI)))
1382 || // For non-extended instruction, no need to allocate extra 4 bytes.
Jyotsna Verma84256432013-03-01 17:37:13 +00001383 (!QII->isExtended(nvjMI) &&
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001384 !ResourceTracker->canReserveResources(nvjMI)))
1385 {
1386 endPacket(MBB, MI);
1387 // A new and empty packet starts.
1388 // We are sure that the resources requirements can be satisfied.
1389 // Therefore, do not need to call "canReserveResources" anymore.
1390 ResourceTracker->reserveResources(MI);
1391 if (QII->isExtended(nvjMI))
1392 reserveResourcesForConstExt(nvjMI);
1393 }
1394 // Here, we are sure that "reserveResources" would succeed.
1395 ResourceTracker->reserveResources(nvjMI);
1396 CurrentPacketMIs.push_back(MI);
1397 CurrentPacketMIs.push_back(nvjMI);
1398 } else {
Jyotsna Verma84256432013-03-01 17:37:13 +00001399 if ( (QII->isExtended(MI) || QII->isConstExtended(MI))
Sirish Pandef8e5e3c2012-05-03 21:52:53 +00001400 && ( !tryAllocateResourcesForConstExt(MI)
1401 || !ResourceTracker->canReserveResources(MI)))
1402 {
1403 endPacket(MBB, MI);
1404 // Check if the instruction was promoted to a dot-new. If so, demote it
1405 // back into a dot-old
1406 if (PromotedToDotNew) {
1407 DemoteToDotOld(MI);
1408 }
1409 reserveResourcesForConstExt(MI);
1410 }
1411 // In case that "MI" is not an extended insn,
1412 // the resource availability has already been checked.
1413 ResourceTracker->reserveResources(MI);
1414 CurrentPacketMIs.push_back(MI);
1415 }
1416 return MII;
1417}
1418
1419//===----------------------------------------------------------------------===//
1420// Public Constructor Functions
1421//===----------------------------------------------------------------------===//
1422
1423FunctionPass *llvm::createHexagonPacketizer() {
1424 return new HexagonPacketizer();
1425}
1426