1 //===----- HexagonPacketizer.cpp - vliw packetizer ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
18 //===----------------------------------------------------------------------===//
19 #include "llvm/CodeGen/DFAPacketizer.h"
21 #include "HexagonMachineFunctionInfo.h"
22 #include "HexagonRegisterInfo.h"
23 #include "HexagonSubtarget.h"
24 #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"
53 #define DEBUG_TYPE "packets"
55 static 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"));
60 void initializeHexagonPacketizerPass(PassRegistry&);
65 class HexagonPacketizer : public MachineFunctionPass {
69 HexagonPacketizer() : MachineFunctionPass(ID) {
70 initializeHexagonPacketizerPass(*PassRegistry::getPassRegistry());
73 void getAnalysisUsage(AnalysisUsage &AU) const override {
75 AU.addRequired<MachineDominatorTree>();
76 AU.addRequired<MachineBranchProbabilityInfo>();
77 AU.addPreserved<MachineDominatorTree>();
78 AU.addRequired<MachineLoopInfo>();
79 AU.addPreserved<MachineLoopInfo>();
80 MachineFunctionPass::getAnalysisUsage(AU);
83 const char *getPassName() const override {
84 return "Hexagon Packetizer";
87 bool runOnMachineFunction(MachineFunction &Fn) override;
89 char HexagonPacketizer::ID = 0;
91 class HexagonPacketizerList : public VLIWPacketizerList {
95 // Has the instruction been promoted to a dot-new instruction.
96 bool PromotedToDotNew;
98 // Has the instruction been glued to allocframe.
99 bool GlueAllocframeStore;
101 // Has the feeder instruction been glued to new value jump.
102 bool GlueToNewValueJump;
104 // Check if there is a dependence between some instruction already in this
105 // packet and this instruction.
108 // Only check for dependence if there are resources available to
109 // schedule this instruction.
110 bool FoundSequentialDependence;
112 /// \brief A handle to the branch probability pass.
113 const MachineBranchProbabilityInfo *MBPI;
115 // Track MIs with ignored dependece.
116 std::vector<MachineInstr*> IgnoreDepMIs;
120 HexagonPacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
121 const MachineBranchProbabilityInfo *MBPI);
123 // initPacketizerState - initialize some internal flags.
124 void initPacketizerState() override;
126 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
127 bool ignorePseudoInstruction(MachineInstr *MI,
128 MachineBasicBlock *MBB) override;
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) override;
134 // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
136 bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) override;
138 // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
140 bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) override;
142 MachineBasicBlock::iterator addToPacket(MachineInstr *MI) override;
144 bool IsCallDependent(MachineInstr* MI, SDep::Kind DepType, unsigned DepReg);
145 bool PromoteToDotNew(MachineInstr* MI, SDep::Kind DepType,
146 MachineBasicBlock::iterator &MII,
147 const TargetRegisterClass* RC);
148 bool CanPromoteToDotNew(MachineInstr *MI, SUnit *PacketSU, unsigned DepReg,
149 const std::map<MachineInstr *, SUnit *> &MIToSUnit,
150 MachineBasicBlock::iterator &MII,
151 const TargetRegisterClass *RC);
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 *> &);
165 bool isNewifiable(MachineInstr* MI);
166 bool isCondInst(MachineInstr* MI);
167 bool tryAllocateResourcesForConstExt(MachineInstr* MI);
168 bool canReserveResourcesForConstExt(MachineInstr *MI);
169 void reserveResourcesForConstExt(MachineInstr* MI);
170 bool isNewValueInst(MachineInstr* MI);
174 INITIALIZE_PASS_BEGIN(HexagonPacketizer, "packets", "Hexagon Packetizer",
176 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
177 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
178 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
179 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
180 INITIALIZE_PASS_END(HexagonPacketizer, "packets", "Hexagon Packetizer",
184 // HexagonPacketizerList Ctor.
185 HexagonPacketizerList::HexagonPacketizerList(
186 MachineFunction &MF, MachineLoopInfo &MLI,
187 const MachineBranchProbabilityInfo *MBPI)
188 : VLIWPacketizerList(MF, MLI, true) {
192 bool HexagonPacketizer::runOnMachineFunction(MachineFunction &Fn) {
193 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
194 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
195 const MachineBranchProbabilityInfo *MBPI =
196 &getAnalysis<MachineBranchProbabilityInfo>();
197 // Instantiate the packetizer.
198 HexagonPacketizerList Packetizer(Fn, MLI, MBPI);
200 // DFA state table should not be empty.
201 assert(Packetizer.getResourceTracker() && "Empty DFA table!");
204 // Loop over all basic blocks and remove KILL pseudo-instructions
205 // These instructions confuse the dependence analysis. Consider:
207 // R0 = KILL R0, D0 (Insn 1)
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
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();
219 MachineBasicBlock::iterator DeleteMI = MI;
221 MBB->erase(DeleteMI);
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) {
240 if (TII->isSchedulingBoundary(std::prev(I), MBB, Fn))
245 // Skip empty scheduling regions.
246 if (I == RegionEnd) {
247 RegionEnd = std::prev(RegionEnd);
251 // Skip regions with one instruction.
252 if (I == std::prev(RegionEnd)) {
253 RegionEnd = std::prev(RegionEnd);
257 Packetizer.PacketizeMIs(MBB, I, RegionEnd);
266 static bool IsIndirectCall(MachineInstr* MI) {
267 return MI->getOpcode() == Hexagon::J2_callr;
270 // Reserve resources for constant extender. Trigure an assertion if
272 void HexagonPacketizerList::reserveResourcesForConstExt(MachineInstr* MI) {
273 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
274 MachineFunction *MF = MI->getParent()->getParent();
275 MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::A4_ext),
278 if (ResourceTracker->canReserveResources(PseudoMI)) {
279 ResourceTracker->reserveResources(PseudoMI);
280 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
282 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
283 llvm_unreachable("can not reserve resources for constant extender.");
288 bool HexagonPacketizerList::canReserveResourcesForConstExt(MachineInstr *MI) {
289 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
290 assert((QII->isExtended(MI) || QII->isConstExtended(MI)) &&
291 "Should only be called for constant extended instructions");
292 MachineFunction *MF = MI->getParent()->getParent();
293 MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::A4_ext),
295 bool CanReserve = ResourceTracker->canReserveResources(PseudoMI);
296 MF->DeleteMachineInstr(PseudoMI);
300 // Allocate resources (i.e. 4 bytes) for constant extender. If succeed, return
301 // true, otherwise, return false.
302 bool HexagonPacketizerList::tryAllocateResourcesForConstExt(MachineInstr* MI) {
303 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
304 MachineFunction *MF = MI->getParent()->getParent();
305 MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::A4_ext),
308 if (ResourceTracker->canReserveResources(PseudoMI)) {
309 ResourceTracker->reserveResources(PseudoMI);
310 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
313 MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
319 bool HexagonPacketizerList::IsCallDependent(MachineInstr* MI,
323 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
324 const HexagonRegisterInfo *QRI =
325 (const HexagonRegisterInfo *)MF.getSubtarget().getRegisterInfo();
327 // Check for lr dependence
328 if (DepReg == QRI->getRARegister()) {
332 if (QII->isDeallocRet(MI)) {
333 if (DepReg == QRI->getFrameRegister() ||
334 DepReg == QRI->getStackRegister())
338 // Check if this is a predicate dependence
339 const TargetRegisterClass* RC = QRI->getMinimalPhysRegClass(DepReg);
340 if (RC == &Hexagon::PredRegsRegClass) {
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
350 // Assumes that the first operand of the CALLr is the function address
352 if (IsIndirectCall(MI) && (DepType == SDep::Data)) {
353 MachineOperand MO = MI->getOperand(0);
354 if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg)) {
362 static bool IsRegDependence(const SDep::Kind DepType) {
363 return (DepType == SDep::Data || DepType == SDep::Anti ||
364 DepType == SDep::Output);
367 static bool IsDirectJump(MachineInstr* MI) {
368 return (MI->getOpcode() == Hexagon::J2_jump);
371 static bool IsSchedBarrier(MachineInstr* MI) {
372 switch (MI->getOpcode()) {
373 case Hexagon::BARRIER:
379 static bool IsControlFlow(MachineInstr* MI) {
380 return (MI->getDesc().isTerminator() || MI->getDesc().isCall());
383 static bool IsLoopN(MachineInstr *MI) {
384 return (MI->getOpcode() == Hexagon::J2_loop0i ||
385 MI->getOpcode() == Hexagon::J2_loop0r);
388 /// DoesModifyCalleeSavedReg - Returns true if the instruction modifies a
389 /// callee-saved register.
390 static bool DoesModifyCalleeSavedReg(MachineInstr *MI,
391 const TargetRegisterInfo *TRI) {
392 for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(); *CSR; ++CSR) {
393 unsigned CalleeSavedReg = *CSR;
394 if (MI->modifiesRegister(CalleeSavedReg, TRI))
400 // Returns true if an instruction can be promoted to .new predicate
401 // or new-value store.
402 bool HexagonPacketizerList::isNewifiable(MachineInstr* MI) {
403 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
404 if ( isCondInst(MI) || QII->mayBeNewStore(MI))
410 bool 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)) {
426 // 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.
429 bool HexagonPacketizerList::PromoteToDotNew(MachineInstr* MI,
430 SDep::Kind DepType, MachineBasicBlock::iterator &MII,
431 const TargetRegisterClass* RC) {
433 assert (DepType == SDep::Data);
434 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
437 if (RC == &Hexagon::PredRegsRegClass)
438 NewOpcode = QII->GetDotNewPredOp(MI, MBPI);
440 NewOpcode = QII->GetDotNewOp(MI);
441 MI->setDesc(QII->get(NewOpcode));
446 bool HexagonPacketizerList::DemoteToDotOld(MachineInstr* MI) {
447 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
448 int NewOpcode = QII->GetDotOldOp(MI->getOpcode());
449 MI->setDesc(QII->get(NewOpcode));
459 /// Returns true if an instruction is predicated on p0 and false if it's
460 /// predicated on !p0.
461 static PredicateKind getPredicateSense(MachineInstr* MI,
462 const HexagonInstrInfo *QII) {
463 if (!QII->isPredicated(MI))
466 if (QII->isPredicatedTrue(MI))
472 static MachineOperand& GetPostIncrementOperand(MachineInstr *MI,
473 const HexagonInstrInfo *QII) {
474 assert(QII->isPostIncrement(MI) && "Not a post increment operation.");
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;
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);
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));
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));
507 // we should never come here.
508 llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
511 // get the value being stored
512 static MachineOperand& GetStoreValueOperand(MachineInstr *MI) {
513 // value being stored is always the last operand.
514 return (MI->getOperand(MI->getNumOperands()-1));
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
536 bool HexagonPacketizerList::CanPromoteToNewValueStore(
537 MachineInstr *MI, MachineInstr *PacketMI, unsigned DepReg,
538 const std::map<MachineInstr *, SUnit *> &MIToSUnit) {
539 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
540 // Make sure we are looking at the store, that can be promoted.
541 if (!QII->mayBeNewStore(MI))
544 // Make sure there is dependency and can be new value'ed
545 if (GetStoreValueOperand(MI).isReg() &&
546 GetStoreValueOperand(MI).getReg() != DepReg)
549 const HexagonRegisterInfo *QRI =
550 (const HexagonRegisterInfo *)MF.getSubtarget().getRegisterInfo();
551 const MCInstrDesc& MCID = PacketMI->getDesc();
552 // first operand is always the result
554 const TargetRegisterClass* PacketRC = QII->getRegClass(MCID, 0, QRI, MF);
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();
561 SUnit *PacketSU = MIToSUnit.find(*VI)->second;
562 if (PacketSU->getInstr()->getDesc().mayStore() ||
563 // if we have mayStore = 1 set on ALLOCFRAME and DEALLOCFRAME,
564 // then we don't need this
565 PacketSU->getInstr()->getOpcode() == Hexagon::S2_allocframe ||
566 PacketSU->getInstr()->getOpcode() == Hexagon::L2_deallocframe)
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
576 // Make sure it's NOT the post increment register that we are going to
578 if (QII->isPostIncrement(MI) &&
579 MI->getDesc().mayStore() &&
580 GetPostIncrementOperand(MI, QII).getReg() == DepReg) {
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
590 // memw(r30 + #-1404) = r2.new -> can not be new value store
591 // arch spec section: 5.4.2.1
595 // If the source that feeds the store is predicated, new value store must
596 // also be predicated.
597 if (QII->isPredicated(PacketMI)) {
598 if (!QII->isPredicated(MI))
601 // Check to make sure that they both will have their predicates
602 // evaluate identically
603 unsigned predRegNumSrc = 0;
604 unsigned predRegNumDst = 0;
605 const TargetRegisterClass* predRegClass = nullptr;
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) {
616 assert ((predRegClass == &Hexagon::PredRegsRegClass ) &&
617 ("predicate register not found in a predicated PacketMI instruction"));
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) {
628 assert ((predRegClass == &Hexagon::PredRegsRegClass ) &&
629 ("predicate register not found in a predicated MI instruction"));
631 // New-value register producer and user (store) need to satisfy these
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.
640 if (( predRegNumDst != predRegNumSrc) ||
641 QII->isDotNewInst(PacketMI) != QII->isDotNewInst(MI) ||
642 getPredicateSense(MI, QII) != getPredicateSense(PacketMI, QII)) {
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.
655 std::vector<MachineInstr*>::iterator VI;
656 std::vector<MachineInstr*>::iterator VE;
657 unsigned StartCheck = 0;
659 for (VI=CurrentPacketMIs.begin(), VE = CurrentPacketMIs.end();
661 SUnit *TempSU = MIToSUnit.find(*VI)->second;
662 MachineInstr* TempMI = TempSU->getInstr();
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
671 if (TempMI == PacketMI) // We don't want to check PacketMI for dependence
674 for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
675 if (MI->getOperand(opNum).isReg() &&
676 TempSU->getInstr()->modifiesRegister(MI->getOperand(opNum).getReg(),
682 // Make sure that for non-POST_INC stores:
683 // 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) {
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()) {
711 // Can be dot new store.
715 // can this MI to promoted to either
716 // new value store or new value jump
717 bool HexagonPacketizerList::CanPromoteToNewValue(
718 MachineInstr *MI, SUnit *PacketSU, unsigned DepReg,
719 const std::map<MachineInstr *, SUnit *> &MIToSUnit,
720 MachineBasicBlock::iterator &MII) {
722 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
723 const HexagonRegisterInfo *QRI =
724 (const HexagonRegisterInfo *)MF.getSubtarget().getRegisterInfo();
725 if (!QRI->Subtarget.hasV4TOps() ||
726 !QII->mayBeNewStore(MI))
729 MachineInstr *PacketMI = PacketSU->getInstr();
731 // Check to see the store can be new value'ed.
732 if (CanPromoteToNewValueStore(MI, PacketMI, DepReg, MIToSUnit))
735 // Check to see the compare/jump can be new value'ed.
736 // This is done as a pass on its own. Don't need to check it here.
740 // Check to see if an instruction can be dot new
741 // There are three kinds.
742 // 1. dot new on predicate - V2/V3/V4
743 // 2. dot new on stores NV/ST - V4
744 // 3. dot new on jump NV/J - V4 -- This is generated in a pass.
745 bool HexagonPacketizerList::CanPromoteToDotNew(
746 MachineInstr *MI, SUnit *PacketSU, unsigned DepReg,
747 const std::map<MachineInstr *, SUnit *> &MIToSUnit,
748 MachineBasicBlock::iterator &MII, const TargetRegisterClass *RC) {
749 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
750 // Already a dot new instruction.
751 if (QII->isDotNewInst(MI) && !QII->mayBeNewStore(MI))
754 if (!isNewifiable(MI))
758 if (RC == &Hexagon::PredRegsRegClass && isCondInst(MI))
760 else if (RC != &Hexagon::PredRegsRegClass &&
761 !QII->mayBeNewStore(MI)) // MI is not a new-value store
764 // Create a dot new machine instruction to see if resources can be
765 // allocated. If not, bail out now.
766 int NewOpcode = QII->GetDotNewOp(MI);
767 const MCInstrDesc &desc = QII->get(NewOpcode);
769 MachineInstr *NewMI =
770 MI->getParent()->getParent()->CreateMachineInstr(desc, dl);
771 bool ResourcesAvailable = ResourceTracker->canReserveResources(NewMI);
772 MI->getParent()->getParent()->DeleteMachineInstr(NewMI);
774 if (!ResourcesAvailable)
777 // new value store only
778 // new new value jump generated as a passes
779 if (!CanPromoteToNewValue(MI, PacketSU, DepReg, MIToSUnit, MII)) {
786 // Go through the packet instructions and search for anti dependency
787 // between them and DepReg from MI
788 // Consider this case:
790 // a) %R1<def> = TFRI_cdNotPt %P3, 2
793 // b) %P0<def> = OR_pp %P3<kill>, %P0<kill>
794 // c) %P3<def> = TFR_PdRs %R23
795 // d) %R1<def> = TFRI_cdnPt %P3, 4
797 // The P3 from a) and d) will be complements after
798 // a)'s P3 is converted to .new form
799 // Anti Dep between c) and b) is irrelevant for this case
800 bool HexagonPacketizerList::RestrictingDepExistInPacket(
801 MachineInstr *MI, unsigned DepReg,
802 const std::map<MachineInstr *, SUnit *> &MIToSUnit) {
804 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
805 SUnit *PacketSUDep = MIToSUnit.find(MI)->second;
807 for (std::vector<MachineInstr*>::iterator VIN = CurrentPacketMIs.begin(),
808 VEN = CurrentPacketMIs.end(); (VIN != VEN); ++VIN) {
810 // We only care for dependencies to predicated instructions
811 if(!QII->isPredicated(*VIN)) continue;
813 // Scheduling Unit for current insn in the packet
814 SUnit *PacketSU = MIToSUnit.find(*VIN)->second;
816 // Look at dependencies between current members of the packet
817 // and predicate defining instruction MI.
818 // Make sure that dependency is on the exact register
820 if (PacketSU->isSucc(PacketSUDep)) {
821 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
822 if ((PacketSU->Succs[i].getSUnit() == PacketSUDep) &&
823 (PacketSU->Succs[i].getKind() == SDep::Anti) &&
824 (PacketSU->Succs[i].getReg() == DepReg)) {
835 /// Gets the predicate register of a predicated instruction.
836 static unsigned getPredicatedRegister(MachineInstr *MI,
837 const HexagonInstrInfo *QII) {
838 /// We use the following rule: The first predicate register that is a use is
839 /// the predicate register of a predicated instruction.
841 assert(QII->isPredicated(MI) && "Must be predicated instruction");
843 for (MachineInstr::mop_iterator OI = MI->operands_begin(),
844 OE = MI->operands_end(); OI != OE; ++OI) {
845 MachineOperand &Op = *OI;
846 if (Op.isReg() && Op.getReg() && Op.isUse() &&
847 Hexagon::PredRegsRegClass.contains(Op.getReg()))
851 llvm_unreachable("Unknown instruction operand layout");
856 // Given two predicated instructions, this function detects whether
857 // the predicates are complements
858 bool HexagonPacketizerList::ArePredicatesComplements(
859 MachineInstr *MI1, MachineInstr *MI2,
860 const std::map<MachineInstr *, SUnit *> &MIToSUnit) {
862 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
864 // If we don't know the predicate sense of the instructions bail out early, we
866 if (getPredicateSense(MI1, QII) == PK_Unknown ||
867 getPredicateSense(MI2, QII) == PK_Unknown)
870 // Scheduling unit for candidate
871 SUnit *SU = MIToSUnit.find(MI1)->second;
873 // One corner case deals with the following scenario:
875 // a) %R24<def> = TFR_cPt %P0, %R25
879 // b) %R25<def> = TFR_cNotPt %P0, %R24
880 // c) %P0<def> = CMPEQri %R26, 1
883 // On general check a) and b) are complements, but
884 // presence of c) will convert a) to .new form, and
885 // then it is not a complement
886 // We attempt to detect it by analyzing existing
887 // dependencies in the packet
889 // Analyze relationships between all existing members of the packet.
890 // Look for Anti dependecy on the same predicate reg
891 // as used in the candidate
892 for (std::vector<MachineInstr*>::iterator VIN = CurrentPacketMIs.begin(),
893 VEN = CurrentPacketMIs.end(); (VIN != VEN); ++VIN) {
895 // Scheduling Unit for current insn in the packet
896 SUnit *PacketSU = MIToSUnit.find(*VIN)->second;
898 // If this instruction in the packet is succeeded by the candidate...
899 if (PacketSU->isSucc(SU)) {
900 for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
901 // The corner case exist when there is true data
902 // dependency between candidate and one of current
903 // packet members, this dep is on predicate reg, and
904 // there already exist anti dep on the same pred in
906 if (PacketSU->Succs[i].getSUnit() == SU &&
907 PacketSU->Succs[i].getKind() == SDep::Data &&
908 Hexagon::PredRegsRegClass.contains(
909 PacketSU->Succs[i].getReg()) &&
910 // Here I know that *VIN is predicate setting instruction
911 // with true data dep to candidate on the register
912 // we care about - c) in the above example.
913 // Now I need to see if there is an anti dependency
914 // from c) to any other instruction in the
915 // same packet on the pred reg of interest
916 RestrictingDepExistInPacket(*VIN,PacketSU->Succs[i].getReg(),
924 // If the above case does not apply, check regular
925 // complement condition.
926 // Check that the predicate register is the same and
927 // that the predicate sense is different
928 // We also need to differentiate .old vs. .new:
929 // !p0 is not complimentary to p0.new
930 unsigned PReg1 = getPredicatedRegister(MI1, QII);
931 unsigned PReg2 = getPredicatedRegister(MI2, QII);
932 return ((PReg1 == PReg2) &&
933 Hexagon::PredRegsRegClass.contains(PReg1) &&
934 Hexagon::PredRegsRegClass.contains(PReg2) &&
935 (getPredicateSense(MI1, QII) != getPredicateSense(MI2, QII)) &&
936 (QII->isDotNewInst(MI1) == QII->isDotNewInst(MI2)));
939 // initPacketizerState - Initialize packetizer flags
940 void HexagonPacketizerList::initPacketizerState() {
943 PromotedToDotNew = false;
944 GlueToNewValueJump = false;
945 GlueAllocframeStore = false;
946 FoundSequentialDependence = false;
951 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
952 bool HexagonPacketizerList::ignorePseudoInstruction(MachineInstr *MI,
953 MachineBasicBlock *MBB) {
954 if (MI->isDebugValue())
957 // We must print out inline assembly
958 if (MI->isInlineAsm())
961 // We check if MI has any functional units mapped to it.
962 // If it doesn't, we ignore the instruction.
963 const MCInstrDesc& TID = MI->getDesc();
964 unsigned SchedClass = TID.getSchedClass();
965 const InstrStage* IS =
966 ResourceTracker->getInstrItins()->beginStage(SchedClass);
967 unsigned FuncUnits = IS->getUnits();
971 // isSoloInstruction: - Returns true for instructions that must be
972 // scheduled in their own packet.
973 bool HexagonPacketizerList::isSoloInstruction(MachineInstr *MI) {
975 if (MI->isInlineAsm())
981 // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
982 // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
983 // They must not be grouped with other instructions in a packet.
984 if (IsSchedBarrier(MI))
990 // isLegalToPacketizeTogether:
991 // SUI is the current instruction that is out side of the current packet.
992 // SUJ is the current instruction inside the current packet against which that
993 // SUI will be packetized.
994 bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
995 MachineInstr *I = SUI->getInstr();
996 MachineInstr *J = SUJ->getInstr();
997 assert(I && J && "Unable to packetize null instruction!");
999 const MCInstrDesc &MCIDI = I->getDesc();
1000 const MCInstrDesc &MCIDJ = J->getDesc();
1002 MachineBasicBlock::iterator II = I;
1004 const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
1005 const HexagonRegisterInfo *QRI =
1006 (const HexagonRegisterInfo *)MF.getSubtarget().getRegisterInfo();
1007 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1009 // Inline asm cannot go in the packet.
1010 if (I->getOpcode() == Hexagon::INLINEASM)
1011 llvm_unreachable("Should not meet inline asm here!");
1013 if (isSoloInstruction(I))
1014 llvm_unreachable("Should not meet solo instr here!");
1016 // A save callee-save register function call can only be in a packet
1017 // with instructions that don't write to the callee-save registers.
1018 if ((QII->isSaveCalleeSavedRegsCall(I) &&
1019 DoesModifyCalleeSavedReg(J, QRI)) ||
1020 (QII->isSaveCalleeSavedRegsCall(J) &&
1021 DoesModifyCalleeSavedReg(I, QRI))) {
1026 // Two control flow instructions cannot go in the same packet.
1027 if (IsControlFlow(I) && IsControlFlow(J)) {
1032 // A LoopN instruction cannot appear in the same packet as a jump or call.
1034 (IsDirectJump(J) || MCIDJ.isCall() || QII->isDeallocRet(J))) {
1039 (IsDirectJump(I) || MCIDI.isCall() || QII->isDeallocRet(I))) {
1044 // dealloc_return cannot appear in the same packet as a conditional or
1045 // unconditional jump.
1046 if (QII->isDeallocRet(I) &&
1047 (MCIDJ.isBranch() || MCIDJ.isCall() || MCIDJ.isBarrier())) {
1053 // V4 allows dual store. But does not allow second store, if the
1054 // first store is not in SLOT0. New value store, new value jump,
1055 // dealloc_return and memop always take SLOT0.
1056 // Arch spec 3.4.4.2
1057 if (QRI->Subtarget.hasV4TOps()) {
1058 if (MCIDI.mayStore() && MCIDJ.mayStore() &&
1059 (QII->isNewValueInst(J) || QII->isMemOp(J) || QII->isMemOp(I))) {
1064 if ((QII->isMemOp(J) && MCIDI.mayStore())
1065 || (MCIDJ.mayStore() && QII->isMemOp(I))
1066 || (QII->isMemOp(J) && QII->isMemOp(I))) {
1072 if (MCIDJ.mayStore() && QII->isDeallocRet(I)) {
1077 // If an instruction feeds new value jump, glue it.
1078 MachineBasicBlock::iterator NextMII = I;
1080 if (NextMII != I->getParent()->end() && QII->isNewValueJump(NextMII)) {
1081 MachineInstr *NextMI = NextMII;
1083 bool secondRegMatch = false;
1084 bool maintainNewValueJump = false;
1086 if (NextMI->getOperand(1).isReg() &&
1087 I->getOperand(0).getReg() == NextMI->getOperand(1).getReg()) {
1088 secondRegMatch = true;
1089 maintainNewValueJump = true;
1092 if (!secondRegMatch &&
1093 I->getOperand(0).getReg() == NextMI->getOperand(0).getReg()) {
1094 maintainNewValueJump = true;
1097 for (std::vector<MachineInstr*>::iterator
1098 VI = CurrentPacketMIs.begin(),
1099 VE = CurrentPacketMIs.end();
1100 (VI != VE && maintainNewValueJump); ++VI) {
1101 SUnit *PacketSU = MIToSUnit.find(*VI)->second;
1103 // NVJ can not be part of the dual jump - Arch Spec: section 7.8
1104 if (PacketSU->getInstr()->getDesc().isCall()) {
1109 // 1. Packet does not have a store in it.
1110 // 2. If the first operand of the nvj is newified, and the second
1111 // operand is also a reg, it (second reg) is not defined in
1113 // 3. If the second operand of the nvj is newified, (which means
1114 // first operand is also a reg), first reg is not defined in
1116 if (PacketSU->getInstr()->getDesc().mayStore() ||
1117 PacketSU->getInstr()->getOpcode() == Hexagon::S2_allocframe ||
1119 (!secondRegMatch && NextMI->getOperand(1).isReg() &&
1120 PacketSU->getInstr()->modifiesRegister(
1121 NextMI->getOperand(1).getReg(), QRI)) ||
1124 PacketSU->getInstr()->modifiesRegister(
1125 NextMI->getOperand(0).getReg(), QRI))) {
1131 GlueToNewValueJump = true;
1137 if (SUJ->isSucc(SUI)) {
1138 for (unsigned i = 0;
1139 (i < SUJ->Succs.size()) && !FoundSequentialDependence;
1142 if (SUJ->Succs[i].getSUnit() != SUI) {
1146 SDep::Kind DepType = SUJ->Succs[i].getKind();
1148 // For direct calls:
1149 // Ignore register dependences for call instructions for
1150 // packetization purposes except for those due to r31 and
1151 // predicate registers.
1153 // For indirect calls:
1154 // Same as direct calls + check for true dependences to the register
1155 // used in the indirect call.
1157 // We completely ignore Order dependences for call instructions
1160 // Ignore register dependences for return instructions like jumpr,
1161 // dealloc return unless we have dependencies on the explicit uses
1162 // of the registers used by jumpr (like r31) or dealloc return
1163 // (like r29 or r30).
1165 // TODO: Currently, jumpr is handling only return of r31. So, the
1166 // following logic (specificaly IsCallDependent) is working fine.
1167 // We need to enable jumpr for register other than r31 and then,
1168 // we need to rework the last part, where it handles indirect call
1169 // of that (IsCallDependent) function. Bug 6216 is opened for this.
1171 unsigned DepReg = 0;
1172 const TargetRegisterClass* RC = nullptr;
1173 if (DepType == SDep::Data) {
1174 DepReg = SUJ->Succs[i].getReg();
1175 RC = QRI->getMinimalPhysRegClass(DepReg);
1177 if ((MCIDI.isCall() || MCIDI.isReturn()) &&
1178 (!IsRegDependence(DepType) ||
1179 !IsCallDependent(I, DepType, SUJ->Succs[i].getReg()))) {
1183 // For instructions that can be promoted to dot-new, try to promote.
1184 else if ((DepType == SDep::Data) &&
1185 CanPromoteToDotNew(I, SUJ, DepReg, MIToSUnit, II, RC) &&
1186 PromoteToDotNew(I, DepType, II, RC)) {
1187 PromotedToDotNew = true;
1191 else if ((DepType == SDep::Data) &&
1192 (QII->isNewValueJump(I))) {
1196 // For predicated instructions, if the predicates are complements
1197 // then there can be no dependence.
1198 else if (QII->isPredicated(I) &&
1199 QII->isPredicated(J) &&
1200 ArePredicatesComplements(I, J, MIToSUnit)) {
1204 else if (IsDirectJump(I) &&
1205 !MCIDJ.isBranch() &&
1207 (DepType == SDep::Order)) {
1208 // Ignore Order dependences between unconditional direct branches
1209 // and non-control-flow instructions
1212 else if (MCIDI.isConditionalBranch() && (DepType != SDep::Data) &&
1213 (DepType != SDep::Output)) {
1214 // Ignore all dependences for jumps except for true and output
1219 // Ignore output dependences due to superregs. We can
1220 // write to two different subregisters of R1:0 for instance
1221 // in the same cycle
1226 // If neither I nor J defines DepReg, then this is a
1227 // superfluous output dependence. The dependence must be of the
1231 // and there is an output dependence between the two instructions
1234 // We want to ignore these dependences.
1235 // Ideally, the dependence constructor should annotate such
1236 // dependences. We can then avoid this relatively expensive check.
1238 else if (DepType == SDep::Output) {
1239 // DepReg is the register that's responsible for the dependence.
1240 unsigned DepReg = SUJ->Succs[i].getReg();
1242 // Check if I and J really defines DepReg.
1243 if (I->definesRegister(DepReg) ||
1244 J->definesRegister(DepReg)) {
1245 FoundSequentialDependence = true;
1250 // We ignore Order dependences for
1251 // 1. Two loads unless they are volatile.
1252 // 2. Two stores in V4 unless they are volatile.
1253 else if ((DepType == SDep::Order) &&
1254 !I->hasOrderedMemoryRef() &&
1255 !J->hasOrderedMemoryRef()) {
1256 if (QRI->Subtarget.hasV4TOps() &&
1257 // hexagonv4 allows dual store.
1258 MCIDI.mayStore() && MCIDJ.mayStore()) {
1261 // store followed by store-- not OK on V2
1262 // store followed by load -- not OK on all (OK if addresses
1264 // load followed by store -- OK on all
1265 // load followed by load -- OK on all
1266 else if ( !MCIDJ.mayStore()) {
1270 FoundSequentialDependence = true;
1275 // For V4, special case ALLOCFRAME. Even though there is dependency
1276 // between ALLOCFRAME and subsequent store, allow it to be
1277 // packetized in a same packet. This implies that the store is using
1278 // caller's SP. Hence, offset needs to be updated accordingly.
1279 else if (DepType == SDep::Data
1280 && QRI->Subtarget.hasV4TOps()
1281 && J->getOpcode() == Hexagon::S2_allocframe
1282 && (I->getOpcode() == Hexagon::S2_storerd_io
1283 || I->getOpcode() == Hexagon::S2_storeri_io
1284 || I->getOpcode() == Hexagon::S2_storerb_io)
1285 && I->getOperand(0).getReg() == QRI->getStackRegister()
1286 && QII->isValidOffset(I->getOpcode(),
1287 I->getOperand(1).getImm() -
1288 (FrameSize + HEXAGON_LRFP_SIZE)))
1290 GlueAllocframeStore = true;
1291 // Since this store is to be glued with allocframe in the same
1292 // packet, it will use SP of the previous stack frame, i.e
1293 // caller's SP. Therefore, we need to recalculate offset according
1295 I->getOperand(1).setImm(I->getOperand(1).getImm() -
1296 (FrameSize + HEXAGON_LRFP_SIZE));
1300 // Skip over anti-dependences. Two instructions that are
1301 // anti-dependent can share a packet
1303 else if (DepType != SDep::Anti) {
1304 FoundSequentialDependence = true;
1309 if (FoundSequentialDependence) {
1318 // isLegalToPruneDependencies
1319 bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
1320 MachineInstr *I = SUI->getInstr();
1321 assert(I && SUJ->getInstr() && "Unable to packetize null instruction!");
1323 const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
1327 // Check if the instruction was promoted to a dot-new. If so, demote it
1328 // back into a dot-old.
1329 if (PromotedToDotNew) {
1333 // Check if the instruction (must be a store) was glued with an Allocframe
1334 // instruction. If so, restore its offset to its original value, i.e. use
1335 // curent SP instead of caller's SP.
1336 if (GlueAllocframeStore) {
1337 I->getOperand(1).setImm(I->getOperand(1).getImm() +
1338 FrameSize + HEXAGON_LRFP_SIZE);
1346 MachineBasicBlock::iterator
1347 HexagonPacketizerList::addToPacket(MachineInstr *MI) {
1349 MachineBasicBlock::iterator MII = MI;
1350 MachineBasicBlock *MBB = MI->getParent();
1352 const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
1354 if (GlueToNewValueJump) {
1357 MachineInstr *nvjMI = MII;
1358 assert(ResourceTracker->canReserveResources(MI));
1359 ResourceTracker->reserveResources(MI);
1360 if ((QII->isExtended(MI) || QII->isConstExtended(MI)) &&
1361 !tryAllocateResourcesForConstExt(MI)) {
1363 ResourceTracker->reserveResources(MI);
1364 assert(canReserveResourcesForConstExt(MI) &&
1365 "Ensure that there is a slot");
1366 reserveResourcesForConstExt(MI);
1367 // Reserve resources for new value jump constant extender.
1368 assert(canReserveResourcesForConstExt(MI) &&
1369 "Ensure that there is a slot");
1370 reserveResourcesForConstExt(nvjMI);
1371 assert(ResourceTracker->canReserveResources(nvjMI) &&
1372 "Ensure that there is a slot");
1374 } else if ( // Extended instruction takes two slots in the packet.
1375 // Try reserve and allocate 4-byte in the current packet first.
1376 (QII->isExtended(nvjMI)
1377 && (!tryAllocateResourcesForConstExt(nvjMI)
1378 || !ResourceTracker->canReserveResources(nvjMI)))
1379 || // For non-extended instruction, no need to allocate extra 4 bytes.
1380 (!QII->isExtended(nvjMI) &&
1381 !ResourceTracker->canReserveResources(nvjMI)))
1384 // A new and empty packet starts.
1385 // We are sure that the resources requirements can be satisfied.
1386 // Therefore, do not need to call "canReserveResources" anymore.
1387 ResourceTracker->reserveResources(MI);
1388 if (QII->isExtended(nvjMI))
1389 reserveResourcesForConstExt(nvjMI);
1391 // Here, we are sure that "reserveResources" would succeed.
1392 ResourceTracker->reserveResources(nvjMI);
1393 CurrentPacketMIs.push_back(MI);
1394 CurrentPacketMIs.push_back(nvjMI);
1396 if ( (QII->isExtended(MI) || QII->isConstExtended(MI))
1397 && ( !tryAllocateResourcesForConstExt(MI)
1398 || !ResourceTracker->canReserveResources(MI)))
1401 // Check if the instruction was promoted to a dot-new. If so, demote it
1402 // back into a dot-old
1403 if (PromotedToDotNew) {
1406 reserveResourcesForConstExt(MI);
1408 // In case that "MI" is not an extended insn,
1409 // the resource availability has already been checked.
1410 ResourceTracker->reserveResources(MI);
1411 CurrentPacketMIs.push_back(MI);
1416 //===----------------------------------------------------------------------===//
1417 // Public Constructor Functions
1418 //===----------------------------------------------------------------------===//
1420 FunctionPass *llvm::createHexagonPacketizer() {
1421 return new HexagonPacketizer();