xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/HexagonVLIWPacketizer.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===- HexagonPacketizer.cpp - VLIW packetizer ----------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This implements a simple VLIW packetizer using DFA. The packetizer works on
100b57cec5SDimitry Andric // machine basic blocks. For each instruction I in BB, the packetizer consults
110b57cec5SDimitry Andric // the DFA to see if machine resources are available to execute I. If so, the
120b57cec5SDimitry Andric // packetizer checks if I depends on any instruction J in the current packet.
130b57cec5SDimitry Andric // If no dependency is found, I is added to current packet and machine resource
140b57cec5SDimitry Andric // is marked as taken. If any dependency is found, a target API call is made to
150b57cec5SDimitry Andric // prune the dependence.
160b57cec5SDimitry Andric //
170b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #include "HexagonVLIWPacketizer.h"
200b57cec5SDimitry Andric #include "Hexagon.h"
210b57cec5SDimitry Andric #include "HexagonInstrInfo.h"
220b57cec5SDimitry Andric #include "HexagonRegisterInfo.h"
230b57cec5SDimitry Andric #include "HexagonSubtarget.h"
240b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
250b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
260b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
278bcb0991SDimitry Andric #include "llvm/ADT/StringExtras.h"
280b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBundle.h"
370b57cec5SDimitry Andric #include "llvm/CodeGen/MachineLoopInfo.h"
380b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
390b57cec5SDimitry Andric #include "llvm/CodeGen/ScheduleDAG.h"
400b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
410b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
420b57cec5SDimitry Andric #include "llvm/IR/DebugLoc.h"
43480093f4SDimitry Andric #include "llvm/InitializePasses.h"
440b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
450b57cec5SDimitry Andric #include "llvm/Pass.h"
460b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
470b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
480b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
490b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
500b57cec5SDimitry Andric #include <cassert>
510b57cec5SDimitry Andric #include <cstdint>
520b57cec5SDimitry Andric #include <iterator>
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric using namespace llvm;
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric #define DEBUG_TYPE "packets"
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric static cl::opt<bool> DisablePacketizer("disable-packetizer", cl::Hidden,
590b57cec5SDimitry Andric   cl::ZeroOrMore, cl::init(false),
600b57cec5SDimitry Andric   cl::desc("Disable Hexagon packetizer pass"));
610b57cec5SDimitry Andric 
628bcb0991SDimitry Andric static cl::opt<bool> Slot1Store("slot1-store-slot0-load", cl::Hidden,
630b57cec5SDimitry Andric                                 cl::ZeroOrMore, cl::init(true),
640b57cec5SDimitry Andric                                 cl::desc("Allow slot1 store and slot0 load"));
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
670b57cec5SDimitry Andric   cl::ZeroOrMore, cl::Hidden, cl::init(true),
680b57cec5SDimitry Andric   cl::desc("Allow non-solo packetization of volatile memory references"));
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric static cl::opt<bool> EnableGenAllInsnClass("enable-gen-insn", cl::init(false),
710b57cec5SDimitry Andric   cl::Hidden, cl::ZeroOrMore, cl::desc("Generate all instruction with TC"));
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric static cl::opt<bool> DisableVecDblNVStores("disable-vecdbl-nv-stores",
740b57cec5SDimitry Andric   cl::init(false), cl::Hidden, cl::ZeroOrMore,
750b57cec5SDimitry Andric   cl::desc("Disable vector double new-value-stores"));
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric extern cl::opt<bool> ScheduleInlineAsm;
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric namespace llvm {
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric FunctionPass *createHexagonPacketizer(bool Minimal);
820b57cec5SDimitry Andric void initializeHexagonPacketizerPass(PassRegistry&);
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric } // end namespace llvm
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric namespace {
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   class HexagonPacketizer : public MachineFunctionPass {
890b57cec5SDimitry Andric   public:
900b57cec5SDimitry Andric     static char ID;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric     HexagonPacketizer(bool Min = false)
930b57cec5SDimitry Andric       : MachineFunctionPass(ID), Minimal(Min) {}
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
960b57cec5SDimitry Andric       AU.setPreservesCFG();
970b57cec5SDimitry Andric       AU.addRequired<AAResultsWrapperPass>();
980b57cec5SDimitry Andric       AU.addRequired<MachineBranchProbabilityInfo>();
990b57cec5SDimitry Andric       AU.addRequired<MachineDominatorTree>();
1000b57cec5SDimitry Andric       AU.addRequired<MachineLoopInfo>();
1010b57cec5SDimitry Andric       AU.addPreserved<MachineDominatorTree>();
1020b57cec5SDimitry Andric       AU.addPreserved<MachineLoopInfo>();
1030b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
1040b57cec5SDimitry Andric     }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric     StringRef getPassName() const override { return "Hexagon Packetizer"; }
1070b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &Fn) override;
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric     MachineFunctionProperties getRequiredProperties() const override {
1100b57cec5SDimitry Andric       return MachineFunctionProperties().set(
1110b57cec5SDimitry Andric           MachineFunctionProperties::Property::NoVRegs);
1120b57cec5SDimitry Andric     }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   private:
115480093f4SDimitry Andric     const HexagonInstrInfo *HII = nullptr;
116480093f4SDimitry Andric     const HexagonRegisterInfo *HRI = nullptr;
117480093f4SDimitry Andric     const bool Minimal = false;
1180b57cec5SDimitry Andric   };
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric } // end anonymous namespace
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric char HexagonPacketizer::ID = 0;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(HexagonPacketizer, "hexagon-packetizer",
1250b57cec5SDimitry Andric                       "Hexagon Packetizer", false, false)
1260b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
1270b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
1280b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
1290b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
1300b57cec5SDimitry Andric INITIALIZE_PASS_END(HexagonPacketizer, "hexagon-packetizer",
1310b57cec5SDimitry Andric                     "Hexagon Packetizer", false, false)
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric HexagonPacketizerList::HexagonPacketizerList(MachineFunction &MF,
1348bcb0991SDimitry Andric       MachineLoopInfo &MLI, AAResults *AA,
1350b57cec5SDimitry Andric       const MachineBranchProbabilityInfo *MBPI, bool Minimal)
1360b57cec5SDimitry Andric     : VLIWPacketizerList(MF, MLI, AA), MBPI(MBPI), MLI(&MLI),
1370b57cec5SDimitry Andric       Minimal(Minimal) {
1380b57cec5SDimitry Andric   HII = MF.getSubtarget<HexagonSubtarget>().getInstrInfo();
1390b57cec5SDimitry Andric   HRI = MF.getSubtarget<HexagonSubtarget>().getRegisterInfo();
1400b57cec5SDimitry Andric 
1418bcb0991SDimitry Andric   addMutation(std::make_unique<HexagonSubtarget::UsrOverflowMutation>());
1428bcb0991SDimitry Andric   addMutation(std::make_unique<HexagonSubtarget::HVXMemLatencyMutation>());
1438bcb0991SDimitry Andric   addMutation(std::make_unique<HexagonSubtarget::BankConflictMutation>());
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric // Check if FirstI modifies a register that SecondI reads.
1470b57cec5SDimitry Andric static bool hasWriteToReadDep(const MachineInstr &FirstI,
1480b57cec5SDimitry Andric                               const MachineInstr &SecondI,
1490b57cec5SDimitry Andric                               const TargetRegisterInfo *TRI) {
1500b57cec5SDimitry Andric   for (auto &MO : FirstI.operands()) {
1510b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isDef())
1520b57cec5SDimitry Andric       continue;
1538bcb0991SDimitry Andric     Register R = MO.getReg();
1540b57cec5SDimitry Andric     if (SecondI.readsRegister(R, TRI))
1550b57cec5SDimitry Andric       return true;
1560b57cec5SDimitry Andric   }
1570b57cec5SDimitry Andric   return false;
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric static MachineBasicBlock::iterator moveInstrOut(MachineInstr &MI,
1620b57cec5SDimitry Andric       MachineBasicBlock::iterator BundleIt, bool Before) {
1630b57cec5SDimitry Andric   MachineBasicBlock::instr_iterator InsertPt;
1640b57cec5SDimitry Andric   if (Before)
1650b57cec5SDimitry Andric     InsertPt = BundleIt.getInstrIterator();
1660b57cec5SDimitry Andric   else
1670b57cec5SDimitry Andric     InsertPt = std::next(BundleIt).getInstrIterator();
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   MachineBasicBlock &B = *MI.getParent();
1700b57cec5SDimitry Andric   // The instruction should at least be bundled with the preceding instruction
1710b57cec5SDimitry Andric   // (there will always be one, i.e. BUNDLE, if nothing else).
1720b57cec5SDimitry Andric   assert(MI.isBundledWithPred());
1730b57cec5SDimitry Andric   if (MI.isBundledWithSucc()) {
1740b57cec5SDimitry Andric     MI.clearFlag(MachineInstr::BundledSucc);
1750b57cec5SDimitry Andric     MI.clearFlag(MachineInstr::BundledPred);
1760b57cec5SDimitry Andric   } else {
1770b57cec5SDimitry Andric     // If it's not bundled with the successor (i.e. it is the last one
1780b57cec5SDimitry Andric     // in the bundle), then we can simply unbundle it from the predecessor,
1790b57cec5SDimitry Andric     // which will take care of updating the predecessor's flag.
1800b57cec5SDimitry Andric     MI.unbundleFromPred();
1810b57cec5SDimitry Andric   }
1820b57cec5SDimitry Andric   B.splice(InsertPt, &B, MI.getIterator());
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   // Get the size of the bundle without asserting.
1850b57cec5SDimitry Andric   MachineBasicBlock::const_instr_iterator I = BundleIt.getInstrIterator();
1860b57cec5SDimitry Andric   MachineBasicBlock::const_instr_iterator E = B.instr_end();
1870b57cec5SDimitry Andric   unsigned Size = 0;
1880b57cec5SDimitry Andric   for (++I; I != E && I->isBundledWithPred(); ++I)
1890b57cec5SDimitry Andric     ++Size;
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   // If there are still two or more instructions, then there is nothing
1920b57cec5SDimitry Andric   // else to be done.
1930b57cec5SDimitry Andric   if (Size > 1)
1940b57cec5SDimitry Andric     return BundleIt;
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric   // Otherwise, extract the single instruction out and delete the bundle.
1970b57cec5SDimitry Andric   MachineBasicBlock::iterator NextIt = std::next(BundleIt);
1980b57cec5SDimitry Andric   MachineInstr &SingleI = *BundleIt->getNextNode();
1990b57cec5SDimitry Andric   SingleI.unbundleFromPred();
2000b57cec5SDimitry Andric   assert(!SingleI.isBundledWithSucc());
2010b57cec5SDimitry Andric   BundleIt->eraseFromParent();
2020b57cec5SDimitry Andric   return NextIt;
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric bool HexagonPacketizer::runOnMachineFunction(MachineFunction &MF) {
206*349cc55cSDimitry Andric   // FIXME: This pass causes verification failures.
207*349cc55cSDimitry Andric   MF.getProperties().set(
208*349cc55cSDimitry Andric       MachineFunctionProperties::Property::FailsVerification);
209*349cc55cSDimitry Andric 
2100b57cec5SDimitry Andric   auto &HST = MF.getSubtarget<HexagonSubtarget>();
2110b57cec5SDimitry Andric   HII = HST.getInstrInfo();
2120b57cec5SDimitry Andric   HRI = HST.getRegisterInfo();
2130b57cec5SDimitry Andric   auto &MLI = getAnalysis<MachineLoopInfo>();
2140b57cec5SDimitry Andric   auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
2150b57cec5SDimitry Andric   auto *MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric   if (EnableGenAllInsnClass)
2180b57cec5SDimitry Andric     HII->genAllInsnTimingClasses(MF);
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric   // Instantiate the packetizer.
2210b57cec5SDimitry Andric   bool MinOnly = Minimal || DisablePacketizer || !HST.usePackets() ||
2220b57cec5SDimitry Andric                  skipFunction(MF.getFunction());
2230b57cec5SDimitry Andric   HexagonPacketizerList Packetizer(MF, MLI, AA, MBPI, MinOnly);
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric   // DFA state table should not be empty.
2260b57cec5SDimitry Andric   assert(Packetizer.getResourceTracker() && "Empty DFA table!");
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   // Loop over all basic blocks and remove KILL pseudo-instructions
2290b57cec5SDimitry Andric   // These instructions confuse the dependence analysis. Consider:
2300b57cec5SDimitry Andric   // D0 = ...   (Insn 0)
2310b57cec5SDimitry Andric   // R0 = KILL R0, D0 (Insn 1)
2320b57cec5SDimitry Andric   // R0 = ... (Insn 2)
2330b57cec5SDimitry Andric   // Here, Insn 1 will result in the dependence graph not emitting an output
2340b57cec5SDimitry Andric   // dependence between Insn 0 and Insn 2. This can lead to incorrect
2350b57cec5SDimitry Andric   // packetization
2360b57cec5SDimitry Andric   for (MachineBasicBlock &MB : MF) {
237*349cc55cSDimitry Andric     for (MachineInstr &MI : llvm::make_early_inc_range(MB))
238*349cc55cSDimitry Andric       if (MI.isKill())
239*349cc55cSDimitry Andric         MB.erase(&MI);
2400b57cec5SDimitry Andric   }
2410b57cec5SDimitry Andric 
2425ffd83dbSDimitry Andric   // TinyCore with Duplexes: Translate to big-instructions.
2435ffd83dbSDimitry Andric   if (HST.isTinyCoreWithDuplex())
2445ffd83dbSDimitry Andric     HII->translateInstrsForDup(MF, true);
2455ffd83dbSDimitry Andric 
2460b57cec5SDimitry Andric   // Loop over all of the basic blocks.
2470b57cec5SDimitry Andric   for (auto &MB : MF) {
2480b57cec5SDimitry Andric     auto Begin = MB.begin(), End = MB.end();
2490b57cec5SDimitry Andric     while (Begin != End) {
2500b57cec5SDimitry Andric       // Find the first non-boundary starting from the end of the last
2510b57cec5SDimitry Andric       // scheduling region.
2520b57cec5SDimitry Andric       MachineBasicBlock::iterator RB = Begin;
2530b57cec5SDimitry Andric       while (RB != End && HII->isSchedulingBoundary(*RB, &MB, MF))
2540b57cec5SDimitry Andric         ++RB;
2550b57cec5SDimitry Andric       // Find the first boundary starting from the beginning of the new
2560b57cec5SDimitry Andric       // region.
2570b57cec5SDimitry Andric       MachineBasicBlock::iterator RE = RB;
2580b57cec5SDimitry Andric       while (RE != End && !HII->isSchedulingBoundary(*RE, &MB, MF))
2590b57cec5SDimitry Andric         ++RE;
2600b57cec5SDimitry Andric       // Add the scheduling boundary if it's not block end.
2610b57cec5SDimitry Andric       if (RE != End)
2620b57cec5SDimitry Andric         ++RE;
2630b57cec5SDimitry Andric       // If RB == End, then RE == End.
2640b57cec5SDimitry Andric       if (RB != End)
2650b57cec5SDimitry Andric         Packetizer.PacketizeMIs(&MB, RB, RE);
2660b57cec5SDimitry Andric 
2670b57cec5SDimitry Andric       Begin = RE;
2680b57cec5SDimitry Andric     }
2690b57cec5SDimitry Andric   }
2700b57cec5SDimitry Andric 
2715ffd83dbSDimitry Andric   // TinyCore with Duplexes: Translate to tiny-instructions.
2725ffd83dbSDimitry Andric   if (HST.isTinyCoreWithDuplex())
2735ffd83dbSDimitry Andric     HII->translateInstrsForDup(MF, false);
2745ffd83dbSDimitry Andric 
2750b57cec5SDimitry Andric   Packetizer.unpacketizeSoloInstrs(MF);
2760b57cec5SDimitry Andric   return true;
2770b57cec5SDimitry Andric }
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric // Reserve resources for a constant extender. Trigger an assertion if the
2800b57cec5SDimitry Andric // reservation fails.
2810b57cec5SDimitry Andric void HexagonPacketizerList::reserveResourcesForConstExt() {
2820b57cec5SDimitry Andric   if (!tryAllocateResourcesForConstExt(true))
2830b57cec5SDimitry Andric     llvm_unreachable("Resources not available");
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric bool HexagonPacketizerList::canReserveResourcesForConstExt() {
2870b57cec5SDimitry Andric   return tryAllocateResourcesForConstExt(false);
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric // Allocate resources (i.e. 4 bytes) for constant extender. If succeeded,
2910b57cec5SDimitry Andric // return true, otherwise, return false.
2920b57cec5SDimitry Andric bool HexagonPacketizerList::tryAllocateResourcesForConstExt(bool Reserve) {
2930b57cec5SDimitry Andric   auto *ExtMI = MF.CreateMachineInstr(HII->get(Hexagon::A4_ext), DebugLoc());
2940b57cec5SDimitry Andric   bool Avail = ResourceTracker->canReserveResources(*ExtMI);
2950b57cec5SDimitry Andric   if (Reserve && Avail)
2960b57cec5SDimitry Andric     ResourceTracker->reserveResources(*ExtMI);
2970b57cec5SDimitry Andric   MF.DeleteMachineInstr(ExtMI);
2980b57cec5SDimitry Andric   return Avail;
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric bool HexagonPacketizerList::isCallDependent(const MachineInstr &MI,
3020b57cec5SDimitry Andric       SDep::Kind DepType, unsigned DepReg) {
3030b57cec5SDimitry Andric   // Check for LR dependence.
3040b57cec5SDimitry Andric   if (DepReg == HRI->getRARegister())
3050b57cec5SDimitry Andric     return true;
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   if (HII->isDeallocRet(MI))
3080b57cec5SDimitry Andric     if (DepReg == HRI->getFrameRegister() || DepReg == HRI->getStackRegister())
3090b57cec5SDimitry Andric       return true;
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric   // Call-like instructions can be packetized with preceding instructions
3120b57cec5SDimitry Andric   // that define registers implicitly used or modified by the call. Explicit
3130b57cec5SDimitry Andric   // uses are still prohibited, as in the case of indirect calls:
3140b57cec5SDimitry Andric   //   r0 = ...
3150b57cec5SDimitry Andric   //   J2_jumpr r0
3160b57cec5SDimitry Andric   if (DepType == SDep::Data) {
317480093f4SDimitry Andric     for (const MachineOperand &MO : MI.operands())
3180b57cec5SDimitry Andric       if (MO.isReg() && MO.getReg() == DepReg && !MO.isImplicit())
3190b57cec5SDimitry Andric         return true;
3200b57cec5SDimitry Andric   }
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   return false;
3230b57cec5SDimitry Andric }
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric static bool isRegDependence(const SDep::Kind DepType) {
3260b57cec5SDimitry Andric   return DepType == SDep::Data || DepType == SDep::Anti ||
3270b57cec5SDimitry Andric          DepType == SDep::Output;
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric static bool isDirectJump(const MachineInstr &MI) {
3310b57cec5SDimitry Andric   return MI.getOpcode() == Hexagon::J2_jump;
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric static bool isSchedBarrier(const MachineInstr &MI) {
3350b57cec5SDimitry Andric   switch (MI.getOpcode()) {
3360b57cec5SDimitry Andric   case Hexagon::Y2_barrier:
3370b57cec5SDimitry Andric     return true;
3380b57cec5SDimitry Andric   }
3390b57cec5SDimitry Andric   return false;
3400b57cec5SDimitry Andric }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric static bool isControlFlow(const MachineInstr &MI) {
3430b57cec5SDimitry Andric   return MI.getDesc().isTerminator() || MI.getDesc().isCall();
3440b57cec5SDimitry Andric }
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric /// Returns true if the instruction modifies a callee-saved register.
3470b57cec5SDimitry Andric static bool doesModifyCalleeSavedReg(const MachineInstr &MI,
3480b57cec5SDimitry Andric                                      const TargetRegisterInfo *TRI) {
3490b57cec5SDimitry Andric   const MachineFunction &MF = *MI.getParent()->getParent();
3500b57cec5SDimitry Andric   for (auto *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
3510b57cec5SDimitry Andric     if (MI.modifiesRegister(*CSR, TRI))
3520b57cec5SDimitry Andric       return true;
3530b57cec5SDimitry Andric   return false;
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric // Returns true if an instruction can be promoted to .new predicate or
3570b57cec5SDimitry Andric // new-value store.
3580b57cec5SDimitry Andric bool HexagonPacketizerList::isNewifiable(const MachineInstr &MI,
3590b57cec5SDimitry Andric       const TargetRegisterClass *NewRC) {
3600b57cec5SDimitry Andric   // Vector stores can be predicated, and can be new-value stores, but
3610b57cec5SDimitry Andric   // they cannot be predicated on a .new predicate value.
3620b57cec5SDimitry Andric   if (NewRC == &Hexagon::PredRegsRegClass) {
3630b57cec5SDimitry Andric     if (HII->isHVXVec(MI) && MI.mayStore())
3640b57cec5SDimitry Andric       return false;
3650b57cec5SDimitry Andric     return HII->isPredicated(MI) && HII->getDotNewPredOp(MI, nullptr) > 0;
3660b57cec5SDimitry Andric   }
3670b57cec5SDimitry Andric   // If the class is not PredRegs, it could only apply to new-value stores.
3680b57cec5SDimitry Andric   return HII->mayBeNewStore(MI);
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric // Promote an instructiont to its .cur form.
3720b57cec5SDimitry Andric // At this time, we have already made a call to canPromoteToDotCur and made
3730b57cec5SDimitry Andric // sure that it can *indeed* be promoted.
3740b57cec5SDimitry Andric bool HexagonPacketizerList::promoteToDotCur(MachineInstr &MI,
3750b57cec5SDimitry Andric       SDep::Kind DepType, MachineBasicBlock::iterator &MII,
3760b57cec5SDimitry Andric       const TargetRegisterClass* RC) {
3770b57cec5SDimitry Andric   assert(DepType == SDep::Data);
3780b57cec5SDimitry Andric   int CurOpcode = HII->getDotCurOp(MI);
3790b57cec5SDimitry Andric   MI.setDesc(HII->get(CurOpcode));
3800b57cec5SDimitry Andric   return true;
3810b57cec5SDimitry Andric }
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric void HexagonPacketizerList::cleanUpDotCur() {
3840b57cec5SDimitry Andric   MachineInstr *MI = nullptr;
3850b57cec5SDimitry Andric   for (auto BI : CurrentPacketMIs) {
3860b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Cleanup packet has "; BI->dump(););
3870b57cec5SDimitry Andric     if (HII->isDotCurInst(*BI)) {
3880b57cec5SDimitry Andric       MI = BI;
3890b57cec5SDimitry Andric       continue;
3900b57cec5SDimitry Andric     }
3910b57cec5SDimitry Andric     if (MI) {
3920b57cec5SDimitry Andric       for (auto &MO : BI->operands())
3930b57cec5SDimitry Andric         if (MO.isReg() && MO.getReg() == MI->getOperand(0).getReg())
3940b57cec5SDimitry Andric           return;
3950b57cec5SDimitry Andric     }
3960b57cec5SDimitry Andric   }
3970b57cec5SDimitry Andric   if (!MI)
3980b57cec5SDimitry Andric     return;
3990b57cec5SDimitry Andric   // We did not find a use of the CUR, so de-cur it.
4000b57cec5SDimitry Andric   MI->setDesc(HII->get(HII->getNonDotCurOp(*MI)));
4010b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Demoted CUR "; MI->dump(););
4020b57cec5SDimitry Andric }
4030b57cec5SDimitry Andric 
4040b57cec5SDimitry Andric // Check to see if an instruction can be dot cur.
4050b57cec5SDimitry Andric bool HexagonPacketizerList::canPromoteToDotCur(const MachineInstr &MI,
4060b57cec5SDimitry Andric       const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
4070b57cec5SDimitry Andric       const TargetRegisterClass *RC) {
4080b57cec5SDimitry Andric   if (!HII->isHVXVec(MI))
4090b57cec5SDimitry Andric     return false;
4100b57cec5SDimitry Andric   if (!HII->isHVXVec(*MII))
4110b57cec5SDimitry Andric     return false;
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric   // Already a dot new instruction.
4140b57cec5SDimitry Andric   if (HII->isDotCurInst(MI) && !HII->mayBeCurLoad(MI))
4150b57cec5SDimitry Andric     return false;
4160b57cec5SDimitry Andric 
4170b57cec5SDimitry Andric   if (!HII->mayBeCurLoad(MI))
4180b57cec5SDimitry Andric     return false;
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric   // The "cur value" cannot come from inline asm.
4210b57cec5SDimitry Andric   if (PacketSU->getInstr()->isInlineAsm())
4220b57cec5SDimitry Andric     return false;
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric   // Make sure candidate instruction uses cur.
4250b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Can we DOT Cur Vector MI\n"; MI.dump();
4260b57cec5SDimitry Andric              dbgs() << "in packet\n";);
4270b57cec5SDimitry Andric   MachineInstr &MJ = *MII;
4280b57cec5SDimitry Andric   LLVM_DEBUG({
4290b57cec5SDimitry Andric     dbgs() << "Checking CUR against ";
4300b57cec5SDimitry Andric     MJ.dump();
4310b57cec5SDimitry Andric   });
4328bcb0991SDimitry Andric   Register DestReg = MI.getOperand(0).getReg();
4330b57cec5SDimitry Andric   bool FoundMatch = false;
4340b57cec5SDimitry Andric   for (auto &MO : MJ.operands())
4350b57cec5SDimitry Andric     if (MO.isReg() && MO.getReg() == DestReg)
4360b57cec5SDimitry Andric       FoundMatch = true;
4370b57cec5SDimitry Andric   if (!FoundMatch)
4380b57cec5SDimitry Andric     return false;
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   // Check for existing uses of a vector register within the packet which
4410b57cec5SDimitry Andric   // would be affected by converting a vector load into .cur formt.
4420b57cec5SDimitry Andric   for (auto BI : CurrentPacketMIs) {
4430b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "packet has "; BI->dump(););
4440b57cec5SDimitry Andric     if (BI->readsRegister(DepReg, MF.getSubtarget().getRegisterInfo()))
4450b57cec5SDimitry Andric       return false;
4460b57cec5SDimitry Andric   }
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Can Dot CUR MI\n"; MI.dump(););
4490b57cec5SDimitry Andric   // We can convert the opcode into a .cur.
4500b57cec5SDimitry Andric   return true;
4510b57cec5SDimitry Andric }
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric // Promote an instruction to its .new form. At this time, we have already
4540b57cec5SDimitry Andric // made a call to canPromoteToDotNew and made sure that it can *indeed* be
4550b57cec5SDimitry Andric // promoted.
4560b57cec5SDimitry Andric bool HexagonPacketizerList::promoteToDotNew(MachineInstr &MI,
4570b57cec5SDimitry Andric       SDep::Kind DepType, MachineBasicBlock::iterator &MII,
4580b57cec5SDimitry Andric       const TargetRegisterClass* RC) {
4590b57cec5SDimitry Andric   assert(DepType == SDep::Data);
4600b57cec5SDimitry Andric   int NewOpcode;
4610b57cec5SDimitry Andric   if (RC == &Hexagon::PredRegsRegClass)
4620b57cec5SDimitry Andric     NewOpcode = HII->getDotNewPredOp(MI, MBPI);
4630b57cec5SDimitry Andric   else
4640b57cec5SDimitry Andric     NewOpcode = HII->getDotNewOp(MI);
4650b57cec5SDimitry Andric   MI.setDesc(HII->get(NewOpcode));
4660b57cec5SDimitry Andric   return true;
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric bool HexagonPacketizerList::demoteToDotOld(MachineInstr &MI) {
4700b57cec5SDimitry Andric   int NewOpcode = HII->getDotOldOp(MI);
4710b57cec5SDimitry Andric   MI.setDesc(HII->get(NewOpcode));
4720b57cec5SDimitry Andric   return true;
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric bool HexagonPacketizerList::useCallersSP(MachineInstr &MI) {
4760b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
4770b57cec5SDimitry Andric   switch (Opc) {
4780b57cec5SDimitry Andric     case Hexagon::S2_storerd_io:
4790b57cec5SDimitry Andric     case Hexagon::S2_storeri_io:
4800b57cec5SDimitry Andric     case Hexagon::S2_storerh_io:
4810b57cec5SDimitry Andric     case Hexagon::S2_storerb_io:
4820b57cec5SDimitry Andric       break;
4830b57cec5SDimitry Andric     default:
4840b57cec5SDimitry Andric       llvm_unreachable("Unexpected instruction");
4850b57cec5SDimitry Andric   }
4860b57cec5SDimitry Andric   unsigned FrameSize = MF.getFrameInfo().getStackSize();
4870b57cec5SDimitry Andric   MachineOperand &Off = MI.getOperand(1);
4880b57cec5SDimitry Andric   int64_t NewOff = Off.getImm() - (FrameSize + HEXAGON_LRFP_SIZE);
4890b57cec5SDimitry Andric   if (HII->isValidOffset(Opc, NewOff, HRI)) {
4900b57cec5SDimitry Andric     Off.setImm(NewOff);
4910b57cec5SDimitry Andric     return true;
4920b57cec5SDimitry Andric   }
4930b57cec5SDimitry Andric   return false;
4940b57cec5SDimitry Andric }
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric void HexagonPacketizerList::useCalleesSP(MachineInstr &MI) {
4970b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
4980b57cec5SDimitry Andric   switch (Opc) {
4990b57cec5SDimitry Andric     case Hexagon::S2_storerd_io:
5000b57cec5SDimitry Andric     case Hexagon::S2_storeri_io:
5010b57cec5SDimitry Andric     case Hexagon::S2_storerh_io:
5020b57cec5SDimitry Andric     case Hexagon::S2_storerb_io:
5030b57cec5SDimitry Andric       break;
5040b57cec5SDimitry Andric     default:
5050b57cec5SDimitry Andric       llvm_unreachable("Unexpected instruction");
5060b57cec5SDimitry Andric   }
5070b57cec5SDimitry Andric   unsigned FrameSize = MF.getFrameInfo().getStackSize();
5080b57cec5SDimitry Andric   MachineOperand &Off = MI.getOperand(1);
5090b57cec5SDimitry Andric   Off.setImm(Off.getImm() + FrameSize + HEXAGON_LRFP_SIZE);
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric /// Return true if we can update the offset in MI so that MI and MJ
5130b57cec5SDimitry Andric /// can be packetized together.
5140b57cec5SDimitry Andric bool HexagonPacketizerList::updateOffset(SUnit *SUI, SUnit *SUJ) {
5150b57cec5SDimitry Andric   assert(SUI->getInstr() && SUJ->getInstr());
5160b57cec5SDimitry Andric   MachineInstr &MI = *SUI->getInstr();
5170b57cec5SDimitry Andric   MachineInstr &MJ = *SUJ->getInstr();
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   unsigned BPI, OPI;
5200b57cec5SDimitry Andric   if (!HII->getBaseAndOffsetPosition(MI, BPI, OPI))
5210b57cec5SDimitry Andric     return false;
5220b57cec5SDimitry Andric   unsigned BPJ, OPJ;
5230b57cec5SDimitry Andric   if (!HII->getBaseAndOffsetPosition(MJ, BPJ, OPJ))
5240b57cec5SDimitry Andric     return false;
5258bcb0991SDimitry Andric   Register Reg = MI.getOperand(BPI).getReg();
5260b57cec5SDimitry Andric   if (Reg != MJ.getOperand(BPJ).getReg())
5270b57cec5SDimitry Andric     return false;
5280b57cec5SDimitry Andric   // Make sure that the dependences do not restrict adding MI to the packet.
5290b57cec5SDimitry Andric   // That is, ignore anti dependences, and make sure the only data dependence
5300b57cec5SDimitry Andric   // involves the specific register.
5310b57cec5SDimitry Andric   for (const auto &PI : SUI->Preds)
5320b57cec5SDimitry Andric     if (PI.getKind() != SDep::Anti &&
5330b57cec5SDimitry Andric         (PI.getKind() != SDep::Data || PI.getReg() != Reg))
5340b57cec5SDimitry Andric       return false;
5350b57cec5SDimitry Andric   int Incr;
5360b57cec5SDimitry Andric   if (!HII->getIncrementValue(MJ, Incr))
5370b57cec5SDimitry Andric     return false;
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric   int64_t Offset = MI.getOperand(OPI).getImm();
5400b57cec5SDimitry Andric   if (!HII->isValidOffset(MI.getOpcode(), Offset+Incr, HRI))
5410b57cec5SDimitry Andric     return false;
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric   MI.getOperand(OPI).setImm(Offset + Incr);
5440b57cec5SDimitry Andric   ChangedOffset = Offset;
5450b57cec5SDimitry Andric   return true;
5460b57cec5SDimitry Andric }
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric /// Undo the changed offset. This is needed if the instruction cannot be
5490b57cec5SDimitry Andric /// added to the current packet due to a different instruction.
5500b57cec5SDimitry Andric void HexagonPacketizerList::undoChangedOffset(MachineInstr &MI) {
5510b57cec5SDimitry Andric   unsigned BP, OP;
5520b57cec5SDimitry Andric   if (!HII->getBaseAndOffsetPosition(MI, BP, OP))
5530b57cec5SDimitry Andric     llvm_unreachable("Unable to find base and offset operands.");
5540b57cec5SDimitry Andric   MI.getOperand(OP).setImm(ChangedOffset);
5550b57cec5SDimitry Andric }
5560b57cec5SDimitry Andric 
5570b57cec5SDimitry Andric enum PredicateKind {
5580b57cec5SDimitry Andric   PK_False,
5590b57cec5SDimitry Andric   PK_True,
5600b57cec5SDimitry Andric   PK_Unknown
5610b57cec5SDimitry Andric };
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric /// Returns true if an instruction is predicated on p0 and false if it's
5640b57cec5SDimitry Andric /// predicated on !p0.
5650b57cec5SDimitry Andric static PredicateKind getPredicateSense(const MachineInstr &MI,
5660b57cec5SDimitry Andric                                        const HexagonInstrInfo *HII) {
5670b57cec5SDimitry Andric   if (!HII->isPredicated(MI))
5680b57cec5SDimitry Andric     return PK_Unknown;
5690b57cec5SDimitry Andric   if (HII->isPredicatedTrue(MI))
5700b57cec5SDimitry Andric     return PK_True;
5710b57cec5SDimitry Andric   return PK_False;
5720b57cec5SDimitry Andric }
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric static const MachineOperand &getPostIncrementOperand(const MachineInstr &MI,
5750b57cec5SDimitry Andric       const HexagonInstrInfo *HII) {
5760b57cec5SDimitry Andric   assert(HII->isPostIncrement(MI) && "Not a post increment operation.");
5770b57cec5SDimitry Andric #ifndef NDEBUG
5780b57cec5SDimitry Andric   // Post Increment means duplicates. Use dense map to find duplicates in the
5790b57cec5SDimitry Andric   // list. Caution: Densemap initializes with the minimum of 64 buckets,
5800b57cec5SDimitry Andric   // whereas there are at most 5 operands in the post increment.
5810b57cec5SDimitry Andric   DenseSet<unsigned> DefRegsSet;
5820b57cec5SDimitry Andric   for (auto &MO : MI.operands())
5830b57cec5SDimitry Andric     if (MO.isReg() && MO.isDef())
5840b57cec5SDimitry Andric       DefRegsSet.insert(MO.getReg());
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   for (auto &MO : MI.operands())
5870b57cec5SDimitry Andric     if (MO.isReg() && MO.isUse() && DefRegsSet.count(MO.getReg()))
5880b57cec5SDimitry Andric       return MO;
5890b57cec5SDimitry Andric #else
5900b57cec5SDimitry Andric   if (MI.mayLoad()) {
5910b57cec5SDimitry Andric     const MachineOperand &Op1 = MI.getOperand(1);
5920b57cec5SDimitry Andric     // The 2nd operand is always the post increment operand in load.
5930b57cec5SDimitry Andric     assert(Op1.isReg() && "Post increment operand has be to a register.");
5940b57cec5SDimitry Andric     return Op1;
5950b57cec5SDimitry Andric   }
5960b57cec5SDimitry Andric   if (MI.getDesc().mayStore()) {
5970b57cec5SDimitry Andric     const MachineOperand &Op0 = MI.getOperand(0);
5980b57cec5SDimitry Andric     // The 1st operand is always the post increment operand in store.
5990b57cec5SDimitry Andric     assert(Op0.isReg() && "Post increment operand has be to a register.");
6000b57cec5SDimitry Andric     return Op0;
6010b57cec5SDimitry Andric   }
6020b57cec5SDimitry Andric #endif
6030b57cec5SDimitry Andric   // we should never come here.
6040b57cec5SDimitry Andric   llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
6050b57cec5SDimitry Andric }
6060b57cec5SDimitry Andric 
6070b57cec5SDimitry Andric // Get the value being stored.
6080b57cec5SDimitry Andric static const MachineOperand& getStoreValueOperand(const MachineInstr &MI) {
6090b57cec5SDimitry Andric   // value being stored is always the last operand.
6100b57cec5SDimitry Andric   return MI.getOperand(MI.getNumOperands()-1);
6110b57cec5SDimitry Andric }
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric static bool isLoadAbsSet(const MachineInstr &MI) {
6140b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
6150b57cec5SDimitry Andric   switch (Opc) {
6160b57cec5SDimitry Andric     case Hexagon::L4_loadrd_ap:
6170b57cec5SDimitry Andric     case Hexagon::L4_loadrb_ap:
6180b57cec5SDimitry Andric     case Hexagon::L4_loadrh_ap:
6190b57cec5SDimitry Andric     case Hexagon::L4_loadrub_ap:
6200b57cec5SDimitry Andric     case Hexagon::L4_loadruh_ap:
6210b57cec5SDimitry Andric     case Hexagon::L4_loadri_ap:
6220b57cec5SDimitry Andric       return true;
6230b57cec5SDimitry Andric   }
6240b57cec5SDimitry Andric   return false;
6250b57cec5SDimitry Andric }
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric static const MachineOperand &getAbsSetOperand(const MachineInstr &MI) {
6280b57cec5SDimitry Andric   assert(isLoadAbsSet(MI));
6290b57cec5SDimitry Andric   return MI.getOperand(1);
6300b57cec5SDimitry Andric }
6310b57cec5SDimitry Andric 
6320b57cec5SDimitry Andric // Can be new value store?
6330b57cec5SDimitry Andric // Following restrictions are to be respected in convert a store into
6340b57cec5SDimitry Andric // a new value store.
6350b57cec5SDimitry Andric // 1. If an instruction uses auto-increment, its address register cannot
6360b57cec5SDimitry Andric //    be a new-value register. Arch Spec 5.4.2.1
6370b57cec5SDimitry Andric // 2. If an instruction uses absolute-set addressing mode, its address
6380b57cec5SDimitry Andric //    register cannot be a new-value register. Arch Spec 5.4.2.1.
6390b57cec5SDimitry Andric // 3. If an instruction produces a 64-bit result, its registers cannot be used
6400b57cec5SDimitry Andric //    as new-value registers. Arch Spec 5.4.2.2.
6410b57cec5SDimitry Andric // 4. If the instruction that sets the new-value register is conditional, then
6420b57cec5SDimitry Andric //    the instruction that uses the new-value register must also be conditional,
6430b57cec5SDimitry Andric //    and both must always have their predicates evaluate identically.
6440b57cec5SDimitry Andric //    Arch Spec 5.4.2.3.
6450b57cec5SDimitry Andric // 5. There is an implied restriction that a packet cannot have another store,
6460b57cec5SDimitry Andric //    if there is a new value store in the packet. Corollary: if there is
6470b57cec5SDimitry Andric //    already a store in a packet, there can not be a new value store.
6480b57cec5SDimitry Andric //    Arch Spec: 3.4.4.2
6490b57cec5SDimitry Andric bool HexagonPacketizerList::canPromoteToNewValueStore(const MachineInstr &MI,
6500b57cec5SDimitry Andric       const MachineInstr &PacketMI, unsigned DepReg) {
6510b57cec5SDimitry Andric   // Make sure we are looking at the store, that can be promoted.
6520b57cec5SDimitry Andric   if (!HII->mayBeNewStore(MI))
6530b57cec5SDimitry Andric     return false;
6540b57cec5SDimitry Andric 
6550b57cec5SDimitry Andric   // Make sure there is dependency and can be new value'd.
6560b57cec5SDimitry Andric   const MachineOperand &Val = getStoreValueOperand(MI);
6570b57cec5SDimitry Andric   if (Val.isReg() && Val.getReg() != DepReg)
6580b57cec5SDimitry Andric     return false;
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric   const MCInstrDesc& MCID = PacketMI.getDesc();
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   // First operand is always the result.
6630b57cec5SDimitry Andric   const TargetRegisterClass *PacketRC = HII->getRegClass(MCID, 0, HRI, MF);
6640b57cec5SDimitry Andric   // Double regs can not feed into new value store: PRM section: 5.4.2.2.
6650b57cec5SDimitry Andric   if (PacketRC == &Hexagon::DoubleRegsRegClass)
6660b57cec5SDimitry Andric     return false;
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric   // New-value stores are of class NV (slot 0), dual stores require class ST
6690b57cec5SDimitry Andric   // in slot 0 (PRM 5.5).
6700b57cec5SDimitry Andric   for (auto I : CurrentPacketMIs) {
6710b57cec5SDimitry Andric     SUnit *PacketSU = MIToSUnit.find(I)->second;
6720b57cec5SDimitry Andric     if (PacketSU->getInstr()->mayStore())
6730b57cec5SDimitry Andric       return false;
6740b57cec5SDimitry Andric   }
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric   // Make sure it's NOT the post increment register that we are going to
6770b57cec5SDimitry Andric   // new value.
6780b57cec5SDimitry Andric   if (HII->isPostIncrement(MI) &&
6790b57cec5SDimitry Andric       getPostIncrementOperand(MI, HII).getReg() == DepReg) {
6800b57cec5SDimitry Andric     return false;
6810b57cec5SDimitry Andric   }
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   if (HII->isPostIncrement(PacketMI) && PacketMI.mayLoad() &&
6840b57cec5SDimitry Andric       getPostIncrementOperand(PacketMI, HII).getReg() == DepReg) {
6850b57cec5SDimitry Andric     // If source is post_inc, or absolute-set addressing, it can not feed
6860b57cec5SDimitry Andric     // into new value store
6870b57cec5SDimitry Andric     //   r3 = memw(r2++#4)
6880b57cec5SDimitry Andric     //   memw(r30 + #-1404) = r2.new -> can not be new value store
6890b57cec5SDimitry Andric     // arch spec section: 5.4.2.1.
6900b57cec5SDimitry Andric     return false;
6910b57cec5SDimitry Andric   }
6920b57cec5SDimitry Andric 
6930b57cec5SDimitry Andric   if (isLoadAbsSet(PacketMI) && getAbsSetOperand(PacketMI).getReg() == DepReg)
6940b57cec5SDimitry Andric     return false;
6950b57cec5SDimitry Andric 
6960b57cec5SDimitry Andric   // If the source that feeds the store is predicated, new value store must
6970b57cec5SDimitry Andric   // also be predicated.
6980b57cec5SDimitry Andric   if (HII->isPredicated(PacketMI)) {
6990b57cec5SDimitry Andric     if (!HII->isPredicated(MI))
7000b57cec5SDimitry Andric       return false;
7010b57cec5SDimitry Andric 
7020b57cec5SDimitry Andric     // Check to make sure that they both will have their predicates
7030b57cec5SDimitry Andric     // evaluate identically.
7040b57cec5SDimitry Andric     unsigned predRegNumSrc = 0;
7050b57cec5SDimitry Andric     unsigned predRegNumDst = 0;
7060b57cec5SDimitry Andric     const TargetRegisterClass* predRegClass = nullptr;
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric     // Get predicate register used in the source instruction.
7090b57cec5SDimitry Andric     for (auto &MO : PacketMI.operands()) {
7100b57cec5SDimitry Andric       if (!MO.isReg())
7110b57cec5SDimitry Andric         continue;
7120b57cec5SDimitry Andric       predRegNumSrc = MO.getReg();
7130b57cec5SDimitry Andric       predRegClass = HRI->getMinimalPhysRegClass(predRegNumSrc);
7140b57cec5SDimitry Andric       if (predRegClass == &Hexagon::PredRegsRegClass)
7150b57cec5SDimitry Andric         break;
7160b57cec5SDimitry Andric     }
7170b57cec5SDimitry Andric     assert((predRegClass == &Hexagon::PredRegsRegClass) &&
7180b57cec5SDimitry Andric         "predicate register not found in a predicated PacketMI instruction");
7190b57cec5SDimitry Andric 
7200b57cec5SDimitry Andric     // Get predicate register used in new-value store instruction.
7210b57cec5SDimitry Andric     for (auto &MO : MI.operands()) {
7220b57cec5SDimitry Andric       if (!MO.isReg())
7230b57cec5SDimitry Andric         continue;
7240b57cec5SDimitry Andric       predRegNumDst = MO.getReg();
7250b57cec5SDimitry Andric       predRegClass = HRI->getMinimalPhysRegClass(predRegNumDst);
7260b57cec5SDimitry Andric       if (predRegClass == &Hexagon::PredRegsRegClass)
7270b57cec5SDimitry Andric         break;
7280b57cec5SDimitry Andric     }
7290b57cec5SDimitry Andric     assert((predRegClass == &Hexagon::PredRegsRegClass) &&
7300b57cec5SDimitry Andric            "predicate register not found in a predicated MI instruction");
7310b57cec5SDimitry Andric 
7320b57cec5SDimitry Andric     // New-value register producer and user (store) need to satisfy these
7330b57cec5SDimitry Andric     // constraints:
7340b57cec5SDimitry Andric     // 1) Both instructions should be predicated on the same register.
7350b57cec5SDimitry Andric     // 2) If producer of the new-value register is .new predicated then store
7360b57cec5SDimitry Andric     // should also be .new predicated and if producer is not .new predicated
7370b57cec5SDimitry Andric     // then store should not be .new predicated.
7380b57cec5SDimitry Andric     // 3) Both new-value register producer and user should have same predicate
7390b57cec5SDimitry Andric     // sense, i.e, either both should be negated or both should be non-negated.
7400b57cec5SDimitry Andric     if (predRegNumDst != predRegNumSrc ||
7410b57cec5SDimitry Andric         HII->isDotNewInst(PacketMI) != HII->isDotNewInst(MI) ||
7420b57cec5SDimitry Andric         getPredicateSense(MI, HII) != getPredicateSense(PacketMI, HII))
7430b57cec5SDimitry Andric       return false;
7440b57cec5SDimitry Andric   }
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric   // Make sure that other than the new-value register no other store instruction
7470b57cec5SDimitry Andric   // register has been modified in the same packet. Predicate registers can be
7480b57cec5SDimitry Andric   // modified by they should not be modified between the producer and the store
7490b57cec5SDimitry Andric   // instruction as it will make them both conditional on different values.
7500b57cec5SDimitry Andric   // We already know this to be true for all the instructions before and
7510b57cec5SDimitry Andric   // including PacketMI. Howerver, we need to perform the check for the
7520b57cec5SDimitry Andric   // remaining instructions in the packet.
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric   unsigned StartCheck = 0;
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric   for (auto I : CurrentPacketMIs) {
7570b57cec5SDimitry Andric     SUnit *TempSU = MIToSUnit.find(I)->second;
7580b57cec5SDimitry Andric     MachineInstr &TempMI = *TempSU->getInstr();
7590b57cec5SDimitry Andric 
7600b57cec5SDimitry Andric     // Following condition is true for all the instructions until PacketMI is
7610b57cec5SDimitry Andric     // reached (StartCheck is set to 0 before the for loop).
7620b57cec5SDimitry Andric     // StartCheck flag is 1 for all the instructions after PacketMI.
7630b57cec5SDimitry Andric     if (&TempMI != &PacketMI && !StartCheck) // Start processing only after
7640b57cec5SDimitry Andric       continue;                              // encountering PacketMI.
7650b57cec5SDimitry Andric 
7660b57cec5SDimitry Andric     StartCheck = 1;
7670b57cec5SDimitry Andric     if (&TempMI == &PacketMI) // We don't want to check PacketMI for dependence.
7680b57cec5SDimitry Andric       continue;
7690b57cec5SDimitry Andric 
7700b57cec5SDimitry Andric     for (auto &MO : MI.operands())
7710b57cec5SDimitry Andric       if (MO.isReg() && TempSU->getInstr()->modifiesRegister(MO.getReg(), HRI))
7720b57cec5SDimitry Andric         return false;
7730b57cec5SDimitry Andric   }
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric   // Make sure that for non-POST_INC stores:
7760b57cec5SDimitry Andric   // 1. The only use of reg is DepReg and no other registers.
7770b57cec5SDimitry Andric   //    This handles base+index registers.
7780b57cec5SDimitry Andric   //    The following store can not be dot new.
7790b57cec5SDimitry Andric   //    Eg.   r0 = add(r0, #3)
7800b57cec5SDimitry Andric   //          memw(r1+r0<<#2) = r0
7810b57cec5SDimitry Andric   if (!HII->isPostIncrement(MI)) {
7820b57cec5SDimitry Andric     for (unsigned opNum = 0; opNum < MI.getNumOperands()-1; opNum++) {
7830b57cec5SDimitry Andric       const MachineOperand &MO = MI.getOperand(opNum);
7840b57cec5SDimitry Andric       if (MO.isReg() && MO.getReg() == DepReg)
7850b57cec5SDimitry Andric         return false;
7860b57cec5SDimitry Andric     }
7870b57cec5SDimitry Andric   }
7880b57cec5SDimitry Andric 
7890b57cec5SDimitry Andric   // If data definition is because of implicit definition of the register,
7900b57cec5SDimitry Andric   // do not newify the store. Eg.
7910b57cec5SDimitry Andric   // %r9 = ZXTH %r12, implicit %d6, implicit-def %r12
7920b57cec5SDimitry Andric   // S2_storerh_io %r8, 2, killed %r12; mem:ST2[%scevgep343]
7930b57cec5SDimitry Andric   for (auto &MO : PacketMI.operands()) {
7940b57cec5SDimitry Andric     if (MO.isRegMask() && MO.clobbersPhysReg(DepReg))
7950b57cec5SDimitry Andric       return false;
7960b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isDef() || !MO.isImplicit())
7970b57cec5SDimitry Andric       continue;
7988bcb0991SDimitry Andric     Register R = MO.getReg();
7990b57cec5SDimitry Andric     if (R == DepReg || HRI->isSuperRegister(DepReg, R))
8000b57cec5SDimitry Andric       return false;
8010b57cec5SDimitry Andric   }
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric   // Handle imp-use of super reg case. There is a target independent side
8040b57cec5SDimitry Andric   // change that should prevent this situation but I am handling it for
8050b57cec5SDimitry Andric   // just-in-case. For example, we cannot newify R2 in the following case:
8060b57cec5SDimitry Andric   // %r3 = A2_tfrsi 0;
8070b57cec5SDimitry Andric   // S2_storeri_io killed %r0, 0, killed %r2, implicit killed %d1;
8080b57cec5SDimitry Andric   for (auto &MO : MI.operands()) {
8090b57cec5SDimitry Andric     if (MO.isReg() && MO.isUse() && MO.isImplicit() && MO.getReg() == DepReg)
8100b57cec5SDimitry Andric       return false;
8110b57cec5SDimitry Andric   }
8120b57cec5SDimitry Andric 
8130b57cec5SDimitry Andric   // Can be dot new store.
8140b57cec5SDimitry Andric   return true;
8150b57cec5SDimitry Andric }
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric // Can this MI to promoted to either new value store or new value jump.
8180b57cec5SDimitry Andric bool HexagonPacketizerList::canPromoteToNewValue(const MachineInstr &MI,
8190b57cec5SDimitry Andric       const SUnit *PacketSU, unsigned DepReg,
8200b57cec5SDimitry Andric       MachineBasicBlock::iterator &MII) {
8210b57cec5SDimitry Andric   if (!HII->mayBeNewStore(MI))
8220b57cec5SDimitry Andric     return false;
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric   // Check to see the store can be new value'ed.
8250b57cec5SDimitry Andric   MachineInstr &PacketMI = *PacketSU->getInstr();
8260b57cec5SDimitry Andric   if (canPromoteToNewValueStore(MI, PacketMI, DepReg))
8270b57cec5SDimitry Andric     return true;
8280b57cec5SDimitry Andric 
8290b57cec5SDimitry Andric   // Check to see the compare/jump can be new value'ed.
8300b57cec5SDimitry Andric   // This is done as a pass on its own. Don't need to check it here.
8310b57cec5SDimitry Andric   return false;
8320b57cec5SDimitry Andric }
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric static bool isImplicitDependency(const MachineInstr &I, bool CheckDef,
8350b57cec5SDimitry Andric       unsigned DepReg) {
8360b57cec5SDimitry Andric   for (auto &MO : I.operands()) {
8370b57cec5SDimitry Andric     if (CheckDef && MO.isRegMask() && MO.clobbersPhysReg(DepReg))
8380b57cec5SDimitry Andric       return true;
8390b57cec5SDimitry Andric     if (!MO.isReg() || MO.getReg() != DepReg || !MO.isImplicit())
8400b57cec5SDimitry Andric       continue;
8410b57cec5SDimitry Andric     if (CheckDef == MO.isDef())
8420b57cec5SDimitry Andric       return true;
8430b57cec5SDimitry Andric   }
8440b57cec5SDimitry Andric   return false;
8450b57cec5SDimitry Andric }
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric // Check to see if an instruction can be dot new.
8480b57cec5SDimitry Andric bool HexagonPacketizerList::canPromoteToDotNew(const MachineInstr &MI,
8490b57cec5SDimitry Andric       const SUnit *PacketSU, unsigned DepReg, MachineBasicBlock::iterator &MII,
8500b57cec5SDimitry Andric       const TargetRegisterClass* RC) {
8510b57cec5SDimitry Andric   // Already a dot new instruction.
8520b57cec5SDimitry Andric   if (HII->isDotNewInst(MI) && !HII->mayBeNewStore(MI))
8530b57cec5SDimitry Andric     return false;
8540b57cec5SDimitry Andric 
8550b57cec5SDimitry Andric   if (!isNewifiable(MI, RC))
8560b57cec5SDimitry Andric     return false;
8570b57cec5SDimitry Andric 
8580b57cec5SDimitry Andric   const MachineInstr &PI = *PacketSU->getInstr();
8590b57cec5SDimitry Andric 
8600b57cec5SDimitry Andric   // The "new value" cannot come from inline asm.
8610b57cec5SDimitry Andric   if (PI.isInlineAsm())
8620b57cec5SDimitry Andric     return false;
8630b57cec5SDimitry Andric 
8640b57cec5SDimitry Andric   // IMPLICIT_DEFs won't materialize as real instructions, so .new makes no
8650b57cec5SDimitry Andric   // sense.
8660b57cec5SDimitry Andric   if (PI.isImplicitDef())
8670b57cec5SDimitry Andric     return false;
8680b57cec5SDimitry Andric 
8690b57cec5SDimitry Andric   // If dependency is trough an implicitly defined register, we should not
8700b57cec5SDimitry Andric   // newify the use.
8710b57cec5SDimitry Andric   if (isImplicitDependency(PI, true, DepReg) ||
8720b57cec5SDimitry Andric       isImplicitDependency(MI, false, DepReg))
8730b57cec5SDimitry Andric     return false;
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric   const MCInstrDesc& MCID = PI.getDesc();
8760b57cec5SDimitry Andric   const TargetRegisterClass *VecRC = HII->getRegClass(MCID, 0, HRI, MF);
8770b57cec5SDimitry Andric   if (DisableVecDblNVStores && VecRC == &Hexagon::HvxWRRegClass)
8780b57cec5SDimitry Andric     return false;
8790b57cec5SDimitry Andric 
8800b57cec5SDimitry Andric   // predicate .new
8810b57cec5SDimitry Andric   if (RC == &Hexagon::PredRegsRegClass)
8820b57cec5SDimitry Andric     return HII->predCanBeUsedAsDotNew(PI, DepReg);
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric   if (RC != &Hexagon::PredRegsRegClass && !HII->mayBeNewStore(MI))
8850b57cec5SDimitry Andric     return false;
8860b57cec5SDimitry Andric 
8870b57cec5SDimitry Andric   // Create a dot new machine instruction to see if resources can be
8880b57cec5SDimitry Andric   // allocated. If not, bail out now.
8890b57cec5SDimitry Andric   int NewOpcode = HII->getDotNewOp(MI);
8900b57cec5SDimitry Andric   const MCInstrDesc &D = HII->get(NewOpcode);
8910b57cec5SDimitry Andric   MachineInstr *NewMI = MF.CreateMachineInstr(D, DebugLoc());
8920b57cec5SDimitry Andric   bool ResourcesAvailable = ResourceTracker->canReserveResources(*NewMI);
8930b57cec5SDimitry Andric   MF.DeleteMachineInstr(NewMI);
8940b57cec5SDimitry Andric   if (!ResourcesAvailable)
8950b57cec5SDimitry Andric     return false;
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric   // New Value Store only. New Value Jump generated as a separate pass.
8980b57cec5SDimitry Andric   if (!canPromoteToNewValue(MI, PacketSU, DepReg, MII))
8990b57cec5SDimitry Andric     return false;
9000b57cec5SDimitry Andric 
9010b57cec5SDimitry Andric   return true;
9020b57cec5SDimitry Andric }
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric // Go through the packet instructions and search for an anti dependency between
9050b57cec5SDimitry Andric // them and DepReg from MI. Consider this case:
9060b57cec5SDimitry Andric // Trying to add
9070b57cec5SDimitry Andric // a) %r1 = TFRI_cdNotPt %p3, 2
9080b57cec5SDimitry Andric // to this packet:
9090b57cec5SDimitry Andric // {
9100b57cec5SDimitry Andric //   b) %p0 = C2_or killed %p3, killed %p0
9110b57cec5SDimitry Andric //   c) %p3 = C2_tfrrp %r23
9120b57cec5SDimitry Andric //   d) %r1 = C2_cmovenewit %p3, 4
9130b57cec5SDimitry Andric //  }
9140b57cec5SDimitry Andric // The P3 from a) and d) will be complements after
9150b57cec5SDimitry Andric // a)'s P3 is converted to .new form
9160b57cec5SDimitry Andric // Anti-dep between c) and b) is irrelevant for this case
9170b57cec5SDimitry Andric bool HexagonPacketizerList::restrictingDepExistInPacket(MachineInstr &MI,
9180b57cec5SDimitry Andric                                                         unsigned DepReg) {
9190b57cec5SDimitry Andric   SUnit *PacketSUDep = MIToSUnit.find(&MI)->second;
9200b57cec5SDimitry Andric 
9210b57cec5SDimitry Andric   for (auto I : CurrentPacketMIs) {
9220b57cec5SDimitry Andric     // We only care for dependencies to predicated instructions
9230b57cec5SDimitry Andric     if (!HII->isPredicated(*I))
9240b57cec5SDimitry Andric       continue;
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric     // Scheduling Unit for current insn in the packet
9270b57cec5SDimitry Andric     SUnit *PacketSU = MIToSUnit.find(I)->second;
9280b57cec5SDimitry Andric 
9290b57cec5SDimitry Andric     // Look at dependencies between current members of the packet and
9300b57cec5SDimitry Andric     // predicate defining instruction MI. Make sure that dependency is
9310b57cec5SDimitry Andric     // on the exact register we care about.
9320b57cec5SDimitry Andric     if (PacketSU->isSucc(PacketSUDep)) {
9330b57cec5SDimitry Andric       for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
9340b57cec5SDimitry Andric         auto &Dep = PacketSU->Succs[i];
9350b57cec5SDimitry Andric         if (Dep.getSUnit() == PacketSUDep && Dep.getKind() == SDep::Anti &&
9360b57cec5SDimitry Andric             Dep.getReg() == DepReg)
9370b57cec5SDimitry Andric           return true;
9380b57cec5SDimitry Andric       }
9390b57cec5SDimitry Andric     }
9400b57cec5SDimitry Andric   }
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric   return false;
9430b57cec5SDimitry Andric }
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric /// Gets the predicate register of a predicated instruction.
9460b57cec5SDimitry Andric static unsigned getPredicatedRegister(MachineInstr &MI,
9470b57cec5SDimitry Andric                                       const HexagonInstrInfo *QII) {
9480b57cec5SDimitry Andric   /// We use the following rule: The first predicate register that is a use is
9490b57cec5SDimitry Andric   /// the predicate register of a predicated instruction.
9500b57cec5SDimitry Andric   assert(QII->isPredicated(MI) && "Must be predicated instruction");
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric   for (auto &Op : MI.operands()) {
9530b57cec5SDimitry Andric     if (Op.isReg() && Op.getReg() && Op.isUse() &&
9540b57cec5SDimitry Andric         Hexagon::PredRegsRegClass.contains(Op.getReg()))
9550b57cec5SDimitry Andric       return Op.getReg();
9560b57cec5SDimitry Andric   }
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric   llvm_unreachable("Unknown instruction operand layout");
9590b57cec5SDimitry Andric   return 0;
9600b57cec5SDimitry Andric }
9610b57cec5SDimitry Andric 
9620b57cec5SDimitry Andric // Given two predicated instructions, this function detects whether
9630b57cec5SDimitry Andric // the predicates are complements.
9640b57cec5SDimitry Andric bool HexagonPacketizerList::arePredicatesComplements(MachineInstr &MI1,
9650b57cec5SDimitry Andric                                                      MachineInstr &MI2) {
9660b57cec5SDimitry Andric   // If we don't know the predicate sense of the instructions bail out early, we
9670b57cec5SDimitry Andric   // need it later.
9680b57cec5SDimitry Andric   if (getPredicateSense(MI1, HII) == PK_Unknown ||
9690b57cec5SDimitry Andric       getPredicateSense(MI2, HII) == PK_Unknown)
9700b57cec5SDimitry Andric     return false;
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric   // Scheduling unit for candidate.
9730b57cec5SDimitry Andric   SUnit *SU = MIToSUnit[&MI1];
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric   // One corner case deals with the following scenario:
9760b57cec5SDimitry Andric   // Trying to add
9770b57cec5SDimitry Andric   // a) %r24 = A2_tfrt %p0, %r25
9780b57cec5SDimitry Andric   // to this packet:
9790b57cec5SDimitry Andric   // {
9800b57cec5SDimitry Andric   //   b) %r25 = A2_tfrf %p0, %r24
9810b57cec5SDimitry Andric   //   c) %p0 = C2_cmpeqi %r26, 1
9820b57cec5SDimitry Andric   // }
9830b57cec5SDimitry Andric   //
9840b57cec5SDimitry Andric   // On general check a) and b) are complements, but presence of c) will
9850b57cec5SDimitry Andric   // convert a) to .new form, and then it is not a complement.
9860b57cec5SDimitry Andric   // We attempt to detect it by analyzing existing dependencies in the packet.
9870b57cec5SDimitry Andric 
9880b57cec5SDimitry Andric   // Analyze relationships between all existing members of the packet.
9890b57cec5SDimitry Andric   // Look for Anti dependecy on the same predicate reg as used in the
9900b57cec5SDimitry Andric   // candidate.
9910b57cec5SDimitry Andric   for (auto I : CurrentPacketMIs) {
9920b57cec5SDimitry Andric     // Scheduling Unit for current insn in the packet.
9930b57cec5SDimitry Andric     SUnit *PacketSU = MIToSUnit.find(I)->second;
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric     // If this instruction in the packet is succeeded by the candidate...
9960b57cec5SDimitry Andric     if (PacketSU->isSucc(SU)) {
9970b57cec5SDimitry Andric       for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
9980b57cec5SDimitry Andric         auto Dep = PacketSU->Succs[i];
9990b57cec5SDimitry Andric         // The corner case exist when there is true data dependency between
10000b57cec5SDimitry Andric         // candidate and one of current packet members, this dep is on
10010b57cec5SDimitry Andric         // predicate reg, and there already exist anti dep on the same pred in
10020b57cec5SDimitry Andric         // the packet.
10030b57cec5SDimitry Andric         if (Dep.getSUnit() == SU && Dep.getKind() == SDep::Data &&
10040b57cec5SDimitry Andric             Hexagon::PredRegsRegClass.contains(Dep.getReg())) {
10050b57cec5SDimitry Andric           // Here I know that I is predicate setting instruction with true
10060b57cec5SDimitry Andric           // data dep to candidate on the register we care about - c) in the
10070b57cec5SDimitry Andric           // above example. Now I need to see if there is an anti dependency
10080b57cec5SDimitry Andric           // from c) to any other instruction in the same packet on the pred
10090b57cec5SDimitry Andric           // reg of interest.
10100b57cec5SDimitry Andric           if (restrictingDepExistInPacket(*I, Dep.getReg()))
10110b57cec5SDimitry Andric             return false;
10120b57cec5SDimitry Andric         }
10130b57cec5SDimitry Andric       }
10140b57cec5SDimitry Andric     }
10150b57cec5SDimitry Andric   }
10160b57cec5SDimitry Andric 
10170b57cec5SDimitry Andric   // If the above case does not apply, check regular complement condition.
10180b57cec5SDimitry Andric   // Check that the predicate register is the same and that the predicate
10190b57cec5SDimitry Andric   // sense is different We also need to differentiate .old vs. .new: !p0
10200b57cec5SDimitry Andric   // is not complementary to p0.new.
10210b57cec5SDimitry Andric   unsigned PReg1 = getPredicatedRegister(MI1, HII);
10220b57cec5SDimitry Andric   unsigned PReg2 = getPredicatedRegister(MI2, HII);
10230b57cec5SDimitry Andric   return PReg1 == PReg2 &&
10240b57cec5SDimitry Andric          Hexagon::PredRegsRegClass.contains(PReg1) &&
10250b57cec5SDimitry Andric          Hexagon::PredRegsRegClass.contains(PReg2) &&
10260b57cec5SDimitry Andric          getPredicateSense(MI1, HII) != getPredicateSense(MI2, HII) &&
10270b57cec5SDimitry Andric          HII->isDotNewInst(MI1) == HII->isDotNewInst(MI2);
10280b57cec5SDimitry Andric }
10290b57cec5SDimitry Andric 
10300b57cec5SDimitry Andric // Initialize packetizer flags.
10310b57cec5SDimitry Andric void HexagonPacketizerList::initPacketizerState() {
10320b57cec5SDimitry Andric   Dependence = false;
10330b57cec5SDimitry Andric   PromotedToDotNew = false;
10340b57cec5SDimitry Andric   GlueToNewValueJump = false;
10350b57cec5SDimitry Andric   GlueAllocframeStore = false;
10360b57cec5SDimitry Andric   FoundSequentialDependence = false;
10370b57cec5SDimitry Andric   ChangedOffset = INT64_MAX;
10380b57cec5SDimitry Andric }
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric // Ignore bundling of pseudo instructions.
10410b57cec5SDimitry Andric bool HexagonPacketizerList::ignorePseudoInstruction(const MachineInstr &MI,
10420b57cec5SDimitry Andric                                                     const MachineBasicBlock *) {
10430b57cec5SDimitry Andric   if (MI.isDebugInstr())
10440b57cec5SDimitry Andric     return true;
10450b57cec5SDimitry Andric 
10460b57cec5SDimitry Andric   if (MI.isCFIInstruction())
10470b57cec5SDimitry Andric     return false;
10480b57cec5SDimitry Andric 
10490b57cec5SDimitry Andric   // We must print out inline assembly.
10500b57cec5SDimitry Andric   if (MI.isInlineAsm())
10510b57cec5SDimitry Andric     return false;
10520b57cec5SDimitry Andric 
10530b57cec5SDimitry Andric   if (MI.isImplicitDef())
10540b57cec5SDimitry Andric     return false;
10550b57cec5SDimitry Andric 
10560b57cec5SDimitry Andric   // We check if MI has any functional units mapped to it. If it doesn't,
10570b57cec5SDimitry Andric   // we ignore the instruction.
10580b57cec5SDimitry Andric   const MCInstrDesc& TID = MI.getDesc();
10590b57cec5SDimitry Andric   auto *IS = ResourceTracker->getInstrItins()->beginStage(TID.getSchedClass());
10605ffd83dbSDimitry Andric   return !IS->getUnits();
10610b57cec5SDimitry Andric }
10620b57cec5SDimitry Andric 
10630b57cec5SDimitry Andric bool HexagonPacketizerList::isSoloInstruction(const MachineInstr &MI) {
10645ffd83dbSDimitry Andric   // Ensure any bundles created by gather packetize remain separate.
10650b57cec5SDimitry Andric   if (MI.isBundle())
10660b57cec5SDimitry Andric     return true;
10670b57cec5SDimitry Andric 
10680b57cec5SDimitry Andric   if (MI.isEHLabel() || MI.isCFIInstruction())
10690b57cec5SDimitry Andric     return true;
10700b57cec5SDimitry Andric 
10710b57cec5SDimitry Andric   // Consider inline asm to not be a solo instruction by default.
10720b57cec5SDimitry Andric   // Inline asm will be put in a packet temporarily, but then it will be
10730b57cec5SDimitry Andric   // removed, and placed outside of the packet (before or after, depending
10740b57cec5SDimitry Andric   // on dependencies).  This is to reduce the impact of inline asm as a
10750b57cec5SDimitry Andric   // "packet splitting" instruction.
10760b57cec5SDimitry Andric   if (MI.isInlineAsm() && !ScheduleInlineAsm)
10770b57cec5SDimitry Andric     return true;
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   if (isSchedBarrier(MI))
10800b57cec5SDimitry Andric     return true;
10810b57cec5SDimitry Andric 
10820b57cec5SDimitry Andric   if (HII->isSolo(MI))
10830b57cec5SDimitry Andric     return true;
10840b57cec5SDimitry Andric 
10850b57cec5SDimitry Andric   if (MI.getOpcode() == Hexagon::A2_nop)
10860b57cec5SDimitry Andric     return true;
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric   return false;
10890b57cec5SDimitry Andric }
10900b57cec5SDimitry Andric 
10910b57cec5SDimitry Andric // Quick check if instructions MI and MJ cannot coexist in the same packet.
10920b57cec5SDimitry Andric // Limit the tests to be "one-way", e.g.  "if MI->isBranch and MJ->isInlineAsm",
10930b57cec5SDimitry Andric // but not the symmetric case: "if MJ->isBranch and MI->isInlineAsm".
10940b57cec5SDimitry Andric // For full test call this function twice:
10950b57cec5SDimitry Andric //   cannotCoexistAsymm(MI, MJ) || cannotCoexistAsymm(MJ, MI)
10960b57cec5SDimitry Andric // Doing the test only one way saves the amount of code in this function,
10970b57cec5SDimitry Andric // since every test would need to be repeated with the MI and MJ reversed.
10980b57cec5SDimitry Andric static bool cannotCoexistAsymm(const MachineInstr &MI, const MachineInstr &MJ,
10990b57cec5SDimitry Andric       const HexagonInstrInfo &HII) {
11000b57cec5SDimitry Andric   const MachineFunction *MF = MI.getParent()->getParent();
11010b57cec5SDimitry Andric   if (MF->getSubtarget<HexagonSubtarget>().hasV60OpsOnly() &&
11020b57cec5SDimitry Andric       HII.isHVXMemWithAIndirect(MI, MJ))
11030b57cec5SDimitry Andric     return true;
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric   // An inline asm cannot be together with a branch, because we may not be
11060b57cec5SDimitry Andric   // able to remove the asm out after packetizing (i.e. if the asm must be
11070b57cec5SDimitry Andric   // moved past the bundle).  Similarly, two asms cannot be together to avoid
11080b57cec5SDimitry Andric   // complications when determining their relative order outside of a bundle.
11090b57cec5SDimitry Andric   if (MI.isInlineAsm())
11100b57cec5SDimitry Andric     return MJ.isInlineAsm() || MJ.isBranch() || MJ.isBarrier() ||
11110b57cec5SDimitry Andric            MJ.isCall() || MJ.isTerminator();
11120b57cec5SDimitry Andric 
11130b57cec5SDimitry Andric   // New-value stores cannot coexist with any other stores.
11140b57cec5SDimitry Andric   if (HII.isNewValueStore(MI) && MJ.mayStore())
11150b57cec5SDimitry Andric     return true;
11160b57cec5SDimitry Andric 
11170b57cec5SDimitry Andric   switch (MI.getOpcode()) {
11180b57cec5SDimitry Andric   case Hexagon::S2_storew_locked:
11190b57cec5SDimitry Andric   case Hexagon::S4_stored_locked:
11200b57cec5SDimitry Andric   case Hexagon::L2_loadw_locked:
11210b57cec5SDimitry Andric   case Hexagon::L4_loadd_locked:
11220b57cec5SDimitry Andric   case Hexagon::Y2_dccleana:
11230b57cec5SDimitry Andric   case Hexagon::Y2_dccleaninva:
11240b57cec5SDimitry Andric   case Hexagon::Y2_dcinva:
11250b57cec5SDimitry Andric   case Hexagon::Y2_dczeroa:
11260b57cec5SDimitry Andric   case Hexagon::Y4_l2fetch:
11270b57cec5SDimitry Andric   case Hexagon::Y5_l2fetch: {
11280b57cec5SDimitry Andric     // These instructions can only be grouped with ALU32 or non-floating-point
11290b57cec5SDimitry Andric     // XTYPE instructions.  Since there is no convenient way of identifying fp
11300b57cec5SDimitry Andric     // XTYPE instructions, only allow grouping with ALU32 for now.
11310b57cec5SDimitry Andric     unsigned TJ = HII.getType(MJ);
11320b57cec5SDimitry Andric     if (TJ != HexagonII::TypeALU32_2op &&
11330b57cec5SDimitry Andric         TJ != HexagonII::TypeALU32_3op &&
11340b57cec5SDimitry Andric         TJ != HexagonII::TypeALU32_ADDI)
11350b57cec5SDimitry Andric       return true;
11360b57cec5SDimitry Andric     break;
11370b57cec5SDimitry Andric   }
11380b57cec5SDimitry Andric   default:
11390b57cec5SDimitry Andric     break;
11400b57cec5SDimitry Andric   }
11410b57cec5SDimitry Andric 
11420b57cec5SDimitry Andric   // "False" really means that the quick check failed to determine if
11430b57cec5SDimitry Andric   // I and J cannot coexist.
11440b57cec5SDimitry Andric   return false;
11450b57cec5SDimitry Andric }
11460b57cec5SDimitry Andric 
11470b57cec5SDimitry Andric // Full, symmetric check.
11480b57cec5SDimitry Andric bool HexagonPacketizerList::cannotCoexist(const MachineInstr &MI,
11490b57cec5SDimitry Andric       const MachineInstr &MJ) {
11500b57cec5SDimitry Andric   return cannotCoexistAsymm(MI, MJ, *HII) || cannotCoexistAsymm(MJ, MI, *HII);
11510b57cec5SDimitry Andric }
11520b57cec5SDimitry Andric 
11530b57cec5SDimitry Andric void HexagonPacketizerList::unpacketizeSoloInstrs(MachineFunction &MF) {
11540b57cec5SDimitry Andric   for (auto &B : MF) {
11550b57cec5SDimitry Andric     MachineBasicBlock::iterator BundleIt;
1156*349cc55cSDimitry Andric     for (MachineInstr &MI : llvm::make_early_inc_range(B.instrs())) {
11570b57cec5SDimitry Andric       if (MI.isBundle())
1158*349cc55cSDimitry Andric         BundleIt = MI.getIterator();
11590b57cec5SDimitry Andric       if (!MI.isInsideBundle())
11600b57cec5SDimitry Andric         continue;
11610b57cec5SDimitry Andric 
11620b57cec5SDimitry Andric       // Decide on where to insert the instruction that we are pulling out.
11630b57cec5SDimitry Andric       // Debug instructions always go before the bundle, but the placement of
11640b57cec5SDimitry Andric       // INLINE_ASM depends on potential dependencies.  By default, try to
11650b57cec5SDimitry Andric       // put it before the bundle, but if the asm writes to a register that
11660b57cec5SDimitry Andric       // other instructions in the bundle read, then we need to place it
11670b57cec5SDimitry Andric       // after the bundle (to preserve the bundle semantics).
11680b57cec5SDimitry Andric       bool InsertBeforeBundle;
11690b57cec5SDimitry Andric       if (MI.isInlineAsm())
11700b57cec5SDimitry Andric         InsertBeforeBundle = !hasWriteToReadDep(MI, *BundleIt, HRI);
11710b57cec5SDimitry Andric       else if (MI.isDebugValue())
11720b57cec5SDimitry Andric         InsertBeforeBundle = true;
11730b57cec5SDimitry Andric       else
11740b57cec5SDimitry Andric         continue;
11750b57cec5SDimitry Andric 
11760b57cec5SDimitry Andric       BundleIt = moveInstrOut(MI, BundleIt, InsertBeforeBundle);
11770b57cec5SDimitry Andric     }
11780b57cec5SDimitry Andric   }
11790b57cec5SDimitry Andric }
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric // Check if a given instruction is of class "system".
11820b57cec5SDimitry Andric static bool isSystemInstr(const MachineInstr &MI) {
11830b57cec5SDimitry Andric   unsigned Opc = MI.getOpcode();
11840b57cec5SDimitry Andric   switch (Opc) {
11850b57cec5SDimitry Andric     case Hexagon::Y2_barrier:
11860b57cec5SDimitry Andric     case Hexagon::Y2_dcfetchbo:
11870b57cec5SDimitry Andric     case Hexagon::Y4_l2fetch:
11880b57cec5SDimitry Andric     case Hexagon::Y5_l2fetch:
11890b57cec5SDimitry Andric       return true;
11900b57cec5SDimitry Andric   }
11910b57cec5SDimitry Andric   return false;
11920b57cec5SDimitry Andric }
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric bool HexagonPacketizerList::hasDeadDependence(const MachineInstr &I,
11950b57cec5SDimitry Andric                                               const MachineInstr &J) {
11960b57cec5SDimitry Andric   // The dependence graph may not include edges between dead definitions,
11970b57cec5SDimitry Andric   // so without extra checks, we could end up packetizing two instruction
11980b57cec5SDimitry Andric   // defining the same (dead) register.
11990b57cec5SDimitry Andric   if (I.isCall() || J.isCall())
12000b57cec5SDimitry Andric     return false;
12010b57cec5SDimitry Andric   if (HII->isPredicated(I) || HII->isPredicated(J))
12020b57cec5SDimitry Andric     return false;
12030b57cec5SDimitry Andric 
12040b57cec5SDimitry Andric   BitVector DeadDefs(Hexagon::NUM_TARGET_REGS);
12050b57cec5SDimitry Andric   for (auto &MO : I.operands()) {
12060b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isDef() || !MO.isDead())
12070b57cec5SDimitry Andric       continue;
12080b57cec5SDimitry Andric     DeadDefs[MO.getReg()] = true;
12090b57cec5SDimitry Andric   }
12100b57cec5SDimitry Andric 
12110b57cec5SDimitry Andric   for (auto &MO : J.operands()) {
12120b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isDef() || !MO.isDead())
12130b57cec5SDimitry Andric       continue;
12148bcb0991SDimitry Andric     Register R = MO.getReg();
12150b57cec5SDimitry Andric     if (R != Hexagon::USR_OVF && DeadDefs[R])
12160b57cec5SDimitry Andric       return true;
12170b57cec5SDimitry Andric   }
12180b57cec5SDimitry Andric   return false;
12190b57cec5SDimitry Andric }
12200b57cec5SDimitry Andric 
12210b57cec5SDimitry Andric bool HexagonPacketizerList::hasControlDependence(const MachineInstr &I,
12220b57cec5SDimitry Andric                                                  const MachineInstr &J) {
12230b57cec5SDimitry Andric   // A save callee-save register function call can only be in a packet
12240b57cec5SDimitry Andric   // with instructions that don't write to the callee-save registers.
12250b57cec5SDimitry Andric   if ((HII->isSaveCalleeSavedRegsCall(I) &&
12260b57cec5SDimitry Andric        doesModifyCalleeSavedReg(J, HRI)) ||
12270b57cec5SDimitry Andric       (HII->isSaveCalleeSavedRegsCall(J) &&
12280b57cec5SDimitry Andric        doesModifyCalleeSavedReg(I, HRI)))
12290b57cec5SDimitry Andric     return true;
12300b57cec5SDimitry Andric 
12310b57cec5SDimitry Andric   // Two control flow instructions cannot go in the same packet.
12320b57cec5SDimitry Andric   if (isControlFlow(I) && isControlFlow(J))
12330b57cec5SDimitry Andric     return true;
12340b57cec5SDimitry Andric 
12350b57cec5SDimitry Andric   // \ref-manual (7.3.4) A loop setup packet in loopN or spNloop0 cannot
12360b57cec5SDimitry Andric   // contain a speculative indirect jump,
12370b57cec5SDimitry Andric   // a new-value compare jump or a dealloc_return.
12380b57cec5SDimitry Andric   auto isBadForLoopN = [this] (const MachineInstr &MI) -> bool {
12390b57cec5SDimitry Andric     if (MI.isCall() || HII->isDeallocRet(MI) || HII->isNewValueJump(MI))
12400b57cec5SDimitry Andric       return true;
12410b57cec5SDimitry Andric     if (HII->isPredicated(MI) && HII->isPredicatedNew(MI) && HII->isJumpR(MI))
12420b57cec5SDimitry Andric       return true;
12430b57cec5SDimitry Andric     return false;
12440b57cec5SDimitry Andric   };
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric   if (HII->isLoopN(I) && isBadForLoopN(J))
12470b57cec5SDimitry Andric     return true;
12480b57cec5SDimitry Andric   if (HII->isLoopN(J) && isBadForLoopN(I))
12490b57cec5SDimitry Andric     return true;
12500b57cec5SDimitry Andric 
12510b57cec5SDimitry Andric   // dealloc_return cannot appear in the same packet as a conditional or
12520b57cec5SDimitry Andric   // unconditional jump.
12530b57cec5SDimitry Andric   return HII->isDeallocRet(I) &&
12540b57cec5SDimitry Andric          (J.isBranch() || J.isCall() || J.isBarrier());
12550b57cec5SDimitry Andric }
12560b57cec5SDimitry Andric 
12570b57cec5SDimitry Andric bool HexagonPacketizerList::hasRegMaskDependence(const MachineInstr &I,
12580b57cec5SDimitry Andric                                                  const MachineInstr &J) {
12590b57cec5SDimitry Andric   // Adding I to a packet that has J.
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric   // Regmasks are not reflected in the scheduling dependency graph, so
12620b57cec5SDimitry Andric   // we need to check them manually. This code assumes that regmasks only
12630b57cec5SDimitry Andric   // occur on calls, and the problematic case is when we add an instruction
12640b57cec5SDimitry Andric   // defining a register R to a packet that has a call that clobbers R via
12650b57cec5SDimitry Andric   // a regmask. Those cannot be packetized together, because the call will
12660b57cec5SDimitry Andric   // be executed last. That's also a reson why it is ok to add a call
12670b57cec5SDimitry Andric   // clobbering R to a packet that defines R.
12680b57cec5SDimitry Andric 
12690b57cec5SDimitry Andric   // Look for regmasks in J.
12700b57cec5SDimitry Andric   for (const MachineOperand &OpJ : J.operands()) {
12710b57cec5SDimitry Andric     if (!OpJ.isRegMask())
12720b57cec5SDimitry Andric       continue;
12730b57cec5SDimitry Andric     assert((J.isCall() || HII->isTailCall(J)) && "Regmask on a non-call");
12740b57cec5SDimitry Andric     for (const MachineOperand &OpI : I.operands()) {
12750b57cec5SDimitry Andric       if (OpI.isReg()) {
12760b57cec5SDimitry Andric         if (OpJ.clobbersPhysReg(OpI.getReg()))
12770b57cec5SDimitry Andric           return true;
12780b57cec5SDimitry Andric       } else if (OpI.isRegMask()) {
12790b57cec5SDimitry Andric         // Both are regmasks. Assume that they intersect.
12800b57cec5SDimitry Andric         return true;
12810b57cec5SDimitry Andric       }
12820b57cec5SDimitry Andric     }
12830b57cec5SDimitry Andric   }
12840b57cec5SDimitry Andric   return false;
12850b57cec5SDimitry Andric }
12860b57cec5SDimitry Andric 
12870b57cec5SDimitry Andric bool HexagonPacketizerList::hasDualStoreDependence(const MachineInstr &I,
12880b57cec5SDimitry Andric                                                    const MachineInstr &J) {
12890b57cec5SDimitry Andric   bool SysI = isSystemInstr(I), SysJ = isSystemInstr(J);
12900b57cec5SDimitry Andric   bool StoreI = I.mayStore(), StoreJ = J.mayStore();
12910b57cec5SDimitry Andric   if ((SysI && StoreJ) || (SysJ && StoreI))
12920b57cec5SDimitry Andric     return true;
12930b57cec5SDimitry Andric 
12940b57cec5SDimitry Andric   if (StoreI && StoreJ) {
12950b57cec5SDimitry Andric     if (HII->isNewValueInst(J) || HII->isMemOp(J) || HII->isMemOp(I))
12960b57cec5SDimitry Andric       return true;
12970b57cec5SDimitry Andric   } else {
12980b57cec5SDimitry Andric     // A memop cannot be in the same packet with another memop or a store.
12990b57cec5SDimitry Andric     // Two stores can be together, but here I and J cannot both be stores.
13000b57cec5SDimitry Andric     bool MopStI = HII->isMemOp(I) || StoreI;
13010b57cec5SDimitry Andric     bool MopStJ = HII->isMemOp(J) || StoreJ;
13020b57cec5SDimitry Andric     if (MopStI && MopStJ)
13030b57cec5SDimitry Andric       return true;
13040b57cec5SDimitry Andric   }
13050b57cec5SDimitry Andric 
13060b57cec5SDimitry Andric   return (StoreJ && HII->isDeallocRet(I)) || (StoreI && HII->isDeallocRet(J));
13070b57cec5SDimitry Andric }
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric // SUI is the current instruction that is out side of the current packet.
13100b57cec5SDimitry Andric // SUJ is the current instruction inside the current packet against which that
13110b57cec5SDimitry Andric // SUI will be packetized.
13120b57cec5SDimitry Andric bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
13130b57cec5SDimitry Andric   assert(SUI->getInstr() && SUJ->getInstr());
13140b57cec5SDimitry Andric   MachineInstr &I = *SUI->getInstr();
13150b57cec5SDimitry Andric   MachineInstr &J = *SUJ->getInstr();
13160b57cec5SDimitry Andric 
13170b57cec5SDimitry Andric   // Clear IgnoreDepMIs when Packet starts.
13180b57cec5SDimitry Andric   if (CurrentPacketMIs.size() == 1)
13190b57cec5SDimitry Andric     IgnoreDepMIs.clear();
13200b57cec5SDimitry Andric 
13210b57cec5SDimitry Andric   MachineBasicBlock::iterator II = I.getIterator();
13220b57cec5SDimitry Andric 
13230b57cec5SDimitry Andric   // Solo instructions cannot go in the packet.
13240b57cec5SDimitry Andric   assert(!isSoloInstruction(I) && "Unexpected solo instr!");
13250b57cec5SDimitry Andric 
13260b57cec5SDimitry Andric   if (cannotCoexist(I, J))
13270b57cec5SDimitry Andric     return false;
13280b57cec5SDimitry Andric 
13290b57cec5SDimitry Andric   Dependence = hasDeadDependence(I, J) || hasControlDependence(I, J);
13300b57cec5SDimitry Andric   if (Dependence)
13310b57cec5SDimitry Andric     return false;
13320b57cec5SDimitry Andric 
13330b57cec5SDimitry Andric   // Regmasks are not accounted for in the scheduling graph, so we need
13340b57cec5SDimitry Andric   // to explicitly check for dependencies caused by them. They should only
13350b57cec5SDimitry Andric   // appear on calls, so it's not too pessimistic to reject all regmask
13360b57cec5SDimitry Andric   // dependencies.
13370b57cec5SDimitry Andric   Dependence = hasRegMaskDependence(I, J);
13380b57cec5SDimitry Andric   if (Dependence)
13390b57cec5SDimitry Andric     return false;
13400b57cec5SDimitry Andric 
13410b57cec5SDimitry Andric   // Dual-store does not allow second store, if the first store is not
13420b57cec5SDimitry Andric   // in SLOT0. New value store, new value jump, dealloc_return and memop
13430b57cec5SDimitry Andric   // always take SLOT0. Arch spec 3.4.4.2.
13440b57cec5SDimitry Andric   Dependence = hasDualStoreDependence(I, J);
13450b57cec5SDimitry Andric   if (Dependence)
13460b57cec5SDimitry Andric     return false;
13470b57cec5SDimitry Andric 
13480b57cec5SDimitry Andric   // If an instruction feeds new value jump, glue it.
13490b57cec5SDimitry Andric   MachineBasicBlock::iterator NextMII = I.getIterator();
13500b57cec5SDimitry Andric   ++NextMII;
13510b57cec5SDimitry Andric   if (NextMII != I.getParent()->end() && HII->isNewValueJump(*NextMII)) {
13520b57cec5SDimitry Andric     MachineInstr &NextMI = *NextMII;
13530b57cec5SDimitry Andric 
13540b57cec5SDimitry Andric     bool secondRegMatch = false;
13550b57cec5SDimitry Andric     const MachineOperand &NOp0 = NextMI.getOperand(0);
13560b57cec5SDimitry Andric     const MachineOperand &NOp1 = NextMI.getOperand(1);
13570b57cec5SDimitry Andric 
13580b57cec5SDimitry Andric     if (NOp1.isReg() && I.getOperand(0).getReg() == NOp1.getReg())
13590b57cec5SDimitry Andric       secondRegMatch = true;
13600b57cec5SDimitry Andric 
13610b57cec5SDimitry Andric     for (MachineInstr *PI : CurrentPacketMIs) {
13620b57cec5SDimitry Andric       // NVJ can not be part of the dual jump - Arch Spec: section 7.8.
13630b57cec5SDimitry Andric       if (PI->isCall()) {
13640b57cec5SDimitry Andric         Dependence = true;
13650b57cec5SDimitry Andric         break;
13660b57cec5SDimitry Andric       }
13670b57cec5SDimitry Andric       // Validate:
13680b57cec5SDimitry Andric       // 1. Packet does not have a store in it.
13690b57cec5SDimitry Andric       // 2. If the first operand of the nvj is newified, and the second
13700b57cec5SDimitry Andric       //    operand is also a reg, it (second reg) is not defined in
13710b57cec5SDimitry Andric       //    the same packet.
13720b57cec5SDimitry Andric       // 3. If the second operand of the nvj is newified, (which means
13730b57cec5SDimitry Andric       //    first operand is also a reg), first reg is not defined in
13740b57cec5SDimitry Andric       //    the same packet.
13750b57cec5SDimitry Andric       if (PI->getOpcode() == Hexagon::S2_allocframe || PI->mayStore() ||
13760b57cec5SDimitry Andric           HII->isLoopN(*PI)) {
13770b57cec5SDimitry Andric         Dependence = true;
13780b57cec5SDimitry Andric         break;
13790b57cec5SDimitry Andric       }
13800b57cec5SDimitry Andric       // Check #2/#3.
13810b57cec5SDimitry Andric       const MachineOperand &OpR = secondRegMatch ? NOp0 : NOp1;
13820b57cec5SDimitry Andric       if (OpR.isReg() && PI->modifiesRegister(OpR.getReg(), HRI)) {
13830b57cec5SDimitry Andric         Dependence = true;
13840b57cec5SDimitry Andric         break;
13850b57cec5SDimitry Andric       }
13860b57cec5SDimitry Andric     }
13870b57cec5SDimitry Andric 
13880b57cec5SDimitry Andric     GlueToNewValueJump = true;
13890b57cec5SDimitry Andric     if (Dependence)
13900b57cec5SDimitry Andric       return false;
13910b57cec5SDimitry Andric   }
13920b57cec5SDimitry Andric 
13930b57cec5SDimitry Andric   // There no dependency between a prolog instruction and its successor.
13940b57cec5SDimitry Andric   if (!SUJ->isSucc(SUI))
13950b57cec5SDimitry Andric     return true;
13960b57cec5SDimitry Andric 
13970b57cec5SDimitry Andric   for (unsigned i = 0; i < SUJ->Succs.size(); ++i) {
13980b57cec5SDimitry Andric     if (FoundSequentialDependence)
13990b57cec5SDimitry Andric       break;
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric     if (SUJ->Succs[i].getSUnit() != SUI)
14020b57cec5SDimitry Andric       continue;
14030b57cec5SDimitry Andric 
14040b57cec5SDimitry Andric     SDep::Kind DepType = SUJ->Succs[i].getKind();
14050b57cec5SDimitry Andric     // For direct calls:
14060b57cec5SDimitry Andric     // Ignore register dependences for call instructions for packetization
14070b57cec5SDimitry Andric     // purposes except for those due to r31 and predicate registers.
14080b57cec5SDimitry Andric     //
14090b57cec5SDimitry Andric     // For indirect calls:
14100b57cec5SDimitry Andric     // Same as direct calls + check for true dependences to the register
14110b57cec5SDimitry Andric     // used in the indirect call.
14120b57cec5SDimitry Andric     //
14130b57cec5SDimitry Andric     // We completely ignore Order dependences for call instructions.
14140b57cec5SDimitry Andric     //
14150b57cec5SDimitry Andric     // For returns:
14160b57cec5SDimitry Andric     // Ignore register dependences for return instructions like jumpr,
14170b57cec5SDimitry Andric     // dealloc return unless we have dependencies on the explicit uses
14180b57cec5SDimitry Andric     // of the registers used by jumpr (like r31) or dealloc return
14190b57cec5SDimitry Andric     // (like r29 or r30).
14200b57cec5SDimitry Andric     unsigned DepReg = 0;
14210b57cec5SDimitry Andric     const TargetRegisterClass *RC = nullptr;
14220b57cec5SDimitry Andric     if (DepType == SDep::Data) {
14230b57cec5SDimitry Andric       DepReg = SUJ->Succs[i].getReg();
14240b57cec5SDimitry Andric       RC = HRI->getMinimalPhysRegClass(DepReg);
14250b57cec5SDimitry Andric     }
14260b57cec5SDimitry Andric 
14270b57cec5SDimitry Andric     if (I.isCall() || HII->isJumpR(I) || I.isReturn() || HII->isTailCall(I)) {
14280b57cec5SDimitry Andric       if (!isRegDependence(DepType))
14290b57cec5SDimitry Andric         continue;
14300b57cec5SDimitry Andric       if (!isCallDependent(I, DepType, SUJ->Succs[i].getReg()))
14310b57cec5SDimitry Andric         continue;
14320b57cec5SDimitry Andric     }
14330b57cec5SDimitry Andric 
14340b57cec5SDimitry Andric     if (DepType == SDep::Data) {
14350b57cec5SDimitry Andric       if (canPromoteToDotCur(J, SUJ, DepReg, II, RC))
14360b57cec5SDimitry Andric         if (promoteToDotCur(J, DepType, II, RC))
14370b57cec5SDimitry Andric           continue;
14380b57cec5SDimitry Andric     }
14390b57cec5SDimitry Andric 
14400b57cec5SDimitry Andric     // Data dpendence ok if we have load.cur.
14410b57cec5SDimitry Andric     if (DepType == SDep::Data && HII->isDotCurInst(J)) {
14420b57cec5SDimitry Andric       if (HII->isHVXVec(I))
14430b57cec5SDimitry Andric         continue;
14440b57cec5SDimitry Andric     }
14450b57cec5SDimitry Andric 
14460b57cec5SDimitry Andric     // For instructions that can be promoted to dot-new, try to promote.
14470b57cec5SDimitry Andric     if (DepType == SDep::Data) {
14480b57cec5SDimitry Andric       if (canPromoteToDotNew(I, SUJ, DepReg, II, RC)) {
14490b57cec5SDimitry Andric         if (promoteToDotNew(I, DepType, II, RC)) {
14500b57cec5SDimitry Andric           PromotedToDotNew = true;
14510b57cec5SDimitry Andric           if (cannotCoexist(I, J))
14520b57cec5SDimitry Andric             FoundSequentialDependence = true;
14530b57cec5SDimitry Andric           continue;
14540b57cec5SDimitry Andric         }
14550b57cec5SDimitry Andric       }
14560b57cec5SDimitry Andric       if (HII->isNewValueJump(I))
14570b57cec5SDimitry Andric         continue;
14580b57cec5SDimitry Andric     }
14590b57cec5SDimitry Andric 
14600b57cec5SDimitry Andric     // For predicated instructions, if the predicates are complements then
14610b57cec5SDimitry Andric     // there can be no dependence.
14620b57cec5SDimitry Andric     if (HII->isPredicated(I) && HII->isPredicated(J) &&
14630b57cec5SDimitry Andric         arePredicatesComplements(I, J)) {
14640b57cec5SDimitry Andric       // Not always safe to do this translation.
14650b57cec5SDimitry Andric       // DAG Builder attempts to reduce dependence edges using transitive
14660b57cec5SDimitry Andric       // nature of dependencies. Here is an example:
14670b57cec5SDimitry Andric       //
14680b57cec5SDimitry Andric       // r0 = tfr_pt ... (1)
14690b57cec5SDimitry Andric       // r0 = tfr_pf ... (2)
14700b57cec5SDimitry Andric       // r0 = tfr_pt ... (3)
14710b57cec5SDimitry Andric       //
14720b57cec5SDimitry Andric       // There will be an output dependence between (1)->(2) and (2)->(3).
14730b57cec5SDimitry Andric       // However, there is no dependence edge between (1)->(3). This results
14740b57cec5SDimitry Andric       // in all 3 instructions going in the same packet. We ignore dependce
14750b57cec5SDimitry Andric       // only once to avoid this situation.
14760b57cec5SDimitry Andric       auto Itr = find(IgnoreDepMIs, &J);
14770b57cec5SDimitry Andric       if (Itr != IgnoreDepMIs.end()) {
14780b57cec5SDimitry Andric         Dependence = true;
14790b57cec5SDimitry Andric         return false;
14800b57cec5SDimitry Andric       }
14810b57cec5SDimitry Andric       IgnoreDepMIs.push_back(&I);
14820b57cec5SDimitry Andric       continue;
14830b57cec5SDimitry Andric     }
14840b57cec5SDimitry Andric 
14850b57cec5SDimitry Andric     // Ignore Order dependences between unconditional direct branches
14860b57cec5SDimitry Andric     // and non-control-flow instructions.
14870b57cec5SDimitry Andric     if (isDirectJump(I) && !J.isBranch() && !J.isCall() &&
14880b57cec5SDimitry Andric         DepType == SDep::Order)
14890b57cec5SDimitry Andric       continue;
14900b57cec5SDimitry Andric 
14910b57cec5SDimitry Andric     // Ignore all dependences for jumps except for true and output
14920b57cec5SDimitry Andric     // dependences.
14930b57cec5SDimitry Andric     if (I.isConditionalBranch() && DepType != SDep::Data &&
14940b57cec5SDimitry Andric         DepType != SDep::Output)
14950b57cec5SDimitry Andric       continue;
14960b57cec5SDimitry Andric 
14970b57cec5SDimitry Andric     if (DepType == SDep::Output) {
14980b57cec5SDimitry Andric       FoundSequentialDependence = true;
14990b57cec5SDimitry Andric       break;
15000b57cec5SDimitry Andric     }
15010b57cec5SDimitry Andric 
15020b57cec5SDimitry Andric     // For Order dependences:
15030b57cec5SDimitry Andric     // 1. Volatile loads/stores can be packetized together, unless other
15040b57cec5SDimitry Andric     //    rules prevent is.
15050b57cec5SDimitry Andric     // 2. Store followed by a load is not allowed.
15060b57cec5SDimitry Andric     // 3. Store followed by a store is valid.
15070b57cec5SDimitry Andric     // 4. Load followed by any memory operation is allowed.
15080b57cec5SDimitry Andric     if (DepType == SDep::Order) {
15090b57cec5SDimitry Andric       if (!PacketizeVolatiles) {
15100b57cec5SDimitry Andric         bool OrdRefs = I.hasOrderedMemoryRef() || J.hasOrderedMemoryRef();
15110b57cec5SDimitry Andric         if (OrdRefs) {
15120b57cec5SDimitry Andric           FoundSequentialDependence = true;
15130b57cec5SDimitry Andric           break;
15140b57cec5SDimitry Andric         }
15150b57cec5SDimitry Andric       }
15160b57cec5SDimitry Andric       // J is first, I is second.
15170b57cec5SDimitry Andric       bool LoadJ = J.mayLoad(), StoreJ = J.mayStore();
15180b57cec5SDimitry Andric       bool LoadI = I.mayLoad(), StoreI = I.mayStore();
15190b57cec5SDimitry Andric       bool NVStoreJ = HII->isNewValueStore(J);
15200b57cec5SDimitry Andric       bool NVStoreI = HII->isNewValueStore(I);
15210b57cec5SDimitry Andric       bool IsVecJ = HII->isHVXVec(J);
15220b57cec5SDimitry Andric       bool IsVecI = HII->isHVXVec(I);
15230b57cec5SDimitry Andric 
15240b57cec5SDimitry Andric       if (Slot1Store && MF.getSubtarget<HexagonSubtarget>().hasV65Ops() &&
15250b57cec5SDimitry Andric           ((LoadJ && StoreI && !NVStoreI) ||
15260b57cec5SDimitry Andric            (StoreJ && LoadI && !NVStoreJ)) &&
15270b57cec5SDimitry Andric           (J.getOpcode() != Hexagon::S2_allocframe &&
15280b57cec5SDimitry Andric            I.getOpcode() != Hexagon::S2_allocframe) &&
15290b57cec5SDimitry Andric           (J.getOpcode() != Hexagon::L2_deallocframe &&
15300b57cec5SDimitry Andric            I.getOpcode() != Hexagon::L2_deallocframe) &&
15310b57cec5SDimitry Andric           (!HII->isMemOp(J) && !HII->isMemOp(I)) && (!IsVecJ && !IsVecI))
15320b57cec5SDimitry Andric         setmemShufDisabled(true);
15330b57cec5SDimitry Andric       else
15340b57cec5SDimitry Andric         if (StoreJ && LoadI && alias(J, I)) {
15350b57cec5SDimitry Andric           FoundSequentialDependence = true;
15360b57cec5SDimitry Andric           break;
15370b57cec5SDimitry Andric         }
15380b57cec5SDimitry Andric 
15390b57cec5SDimitry Andric       if (!StoreJ)
15400b57cec5SDimitry Andric         if (!LoadJ || (!LoadI && !StoreI)) {
15410b57cec5SDimitry Andric           // If J is neither load nor store, assume a dependency.
15420b57cec5SDimitry Andric           // If J is a load, but I is neither, also assume a dependency.
15430b57cec5SDimitry Andric           FoundSequentialDependence = true;
15440b57cec5SDimitry Andric           break;
15450b57cec5SDimitry Andric         }
15460b57cec5SDimitry Andric       // Store followed by store: not OK on V2.
15470b57cec5SDimitry Andric       // Store followed by load: not OK on all.
15480b57cec5SDimitry Andric       // Load followed by store: OK on all.
15490b57cec5SDimitry Andric       // Load followed by load: OK on all.
15500b57cec5SDimitry Andric       continue;
15510b57cec5SDimitry Andric     }
15520b57cec5SDimitry Andric 
15530b57cec5SDimitry Andric     // Special case for ALLOCFRAME: even though there is dependency
15540b57cec5SDimitry Andric     // between ALLOCFRAME and subsequent store, allow it to be packetized
15550b57cec5SDimitry Andric     // in a same packet. This implies that the store is using the caller's
15560b57cec5SDimitry Andric     // SP. Hence, offset needs to be updated accordingly.
15570b57cec5SDimitry Andric     if (DepType == SDep::Data && J.getOpcode() == Hexagon::S2_allocframe) {
15580b57cec5SDimitry Andric       unsigned Opc = I.getOpcode();
15590b57cec5SDimitry Andric       switch (Opc) {
15600b57cec5SDimitry Andric         case Hexagon::S2_storerd_io:
15610b57cec5SDimitry Andric         case Hexagon::S2_storeri_io:
15620b57cec5SDimitry Andric         case Hexagon::S2_storerh_io:
15630b57cec5SDimitry Andric         case Hexagon::S2_storerb_io:
15640b57cec5SDimitry Andric           if (I.getOperand(0).getReg() == HRI->getStackRegister()) {
15650b57cec5SDimitry Andric             // Since this store is to be glued with allocframe in the same
15660b57cec5SDimitry Andric             // packet, it will use SP of the previous stack frame, i.e.
15670b57cec5SDimitry Andric             // caller's SP. Therefore, we need to recalculate offset
15680b57cec5SDimitry Andric             // according to this change.
15690b57cec5SDimitry Andric             GlueAllocframeStore = useCallersSP(I);
15700b57cec5SDimitry Andric             if (GlueAllocframeStore)
15710b57cec5SDimitry Andric               continue;
15720b57cec5SDimitry Andric           }
15730b57cec5SDimitry Andric           break;
15740b57cec5SDimitry Andric         default:
15750b57cec5SDimitry Andric           break;
15760b57cec5SDimitry Andric       }
15770b57cec5SDimitry Andric     }
15780b57cec5SDimitry Andric 
15790b57cec5SDimitry Andric     // There are certain anti-dependencies that cannot be ignored.
15800b57cec5SDimitry Andric     // Specifically:
15810b57cec5SDimitry Andric     //   J2_call ... implicit-def %r0   ; SUJ
15820b57cec5SDimitry Andric     //   R0 = ...                   ; SUI
15830b57cec5SDimitry Andric     // Those cannot be packetized together, since the call will observe
15840b57cec5SDimitry Andric     // the effect of the assignment to R0.
15850b57cec5SDimitry Andric     if ((DepType == SDep::Anti || DepType == SDep::Output) && J.isCall()) {
15860b57cec5SDimitry Andric       // Check if I defines any volatile register. We should also check
15870b57cec5SDimitry Andric       // registers that the call may read, but these happen to be a
15880b57cec5SDimitry Andric       // subset of the volatile register set.
15890b57cec5SDimitry Andric       for (const MachineOperand &Op : I.operands()) {
15900b57cec5SDimitry Andric         if (Op.isReg() && Op.isDef()) {
15918bcb0991SDimitry Andric           Register R = Op.getReg();
15920b57cec5SDimitry Andric           if (!J.readsRegister(R, HRI) && !J.modifiesRegister(R, HRI))
15930b57cec5SDimitry Andric             continue;
15940b57cec5SDimitry Andric         } else if (!Op.isRegMask()) {
15950b57cec5SDimitry Andric           // If I has a regmask assume dependency.
15960b57cec5SDimitry Andric           continue;
15970b57cec5SDimitry Andric         }
15980b57cec5SDimitry Andric         FoundSequentialDependence = true;
15990b57cec5SDimitry Andric         break;
16000b57cec5SDimitry Andric       }
16010b57cec5SDimitry Andric     }
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric     // Skip over remaining anti-dependences. Two instructions that are
16040b57cec5SDimitry Andric     // anti-dependent can share a packet, since in most such cases all
16050b57cec5SDimitry Andric     // operands are read before any modifications take place.
16060b57cec5SDimitry Andric     // The exceptions are branch and call instructions, since they are
16070b57cec5SDimitry Andric     // executed after all other instructions have completed (at least
16080b57cec5SDimitry Andric     // conceptually).
16090b57cec5SDimitry Andric     if (DepType != SDep::Anti) {
16100b57cec5SDimitry Andric       FoundSequentialDependence = true;
16110b57cec5SDimitry Andric       break;
16120b57cec5SDimitry Andric     }
16130b57cec5SDimitry Andric   }
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric   if (FoundSequentialDependence) {
16160b57cec5SDimitry Andric     Dependence = true;
16170b57cec5SDimitry Andric     return false;
16180b57cec5SDimitry Andric   }
16190b57cec5SDimitry Andric 
16200b57cec5SDimitry Andric   return true;
16210b57cec5SDimitry Andric }
16220b57cec5SDimitry Andric 
16230b57cec5SDimitry Andric bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
16240b57cec5SDimitry Andric   assert(SUI->getInstr() && SUJ->getInstr());
16250b57cec5SDimitry Andric   MachineInstr &I = *SUI->getInstr();
16260b57cec5SDimitry Andric   MachineInstr &J = *SUJ->getInstr();
16270b57cec5SDimitry Andric 
16280b57cec5SDimitry Andric   bool Coexist = !cannotCoexist(I, J);
16290b57cec5SDimitry Andric 
16300b57cec5SDimitry Andric   if (Coexist && !Dependence)
16310b57cec5SDimitry Andric     return true;
16320b57cec5SDimitry Andric 
16330b57cec5SDimitry Andric   // Check if the instruction was promoted to a dot-new. If so, demote it
16340b57cec5SDimitry Andric   // back into a dot-old.
16350b57cec5SDimitry Andric   if (PromotedToDotNew)
16360b57cec5SDimitry Andric     demoteToDotOld(I);
16370b57cec5SDimitry Andric 
16380b57cec5SDimitry Andric   cleanUpDotCur();
16390b57cec5SDimitry Andric   // Check if the instruction (must be a store) was glued with an allocframe
16400b57cec5SDimitry Andric   // instruction. If so, restore its offset to its original value, i.e. use
16410b57cec5SDimitry Andric   // current SP instead of caller's SP.
16420b57cec5SDimitry Andric   if (GlueAllocframeStore) {
16430b57cec5SDimitry Andric     useCalleesSP(I);
16440b57cec5SDimitry Andric     GlueAllocframeStore = false;
16450b57cec5SDimitry Andric   }
16460b57cec5SDimitry Andric 
16470b57cec5SDimitry Andric   if (ChangedOffset != INT64_MAX)
16480b57cec5SDimitry Andric     undoChangedOffset(I);
16490b57cec5SDimitry Andric 
16500b57cec5SDimitry Andric   if (GlueToNewValueJump) {
16510b57cec5SDimitry Andric     // Putting I and J together would prevent the new-value jump from being
16520b57cec5SDimitry Andric     // packetized with the producer. In that case I and J must be separated.
16530b57cec5SDimitry Andric     GlueToNewValueJump = false;
16540b57cec5SDimitry Andric     return false;
16550b57cec5SDimitry Andric   }
16560b57cec5SDimitry Andric 
16570b57cec5SDimitry Andric   if (!Coexist)
16580b57cec5SDimitry Andric     return false;
16590b57cec5SDimitry Andric 
16600b57cec5SDimitry Andric   if (ChangedOffset == INT64_MAX && updateOffset(SUI, SUJ)) {
16610b57cec5SDimitry Andric     FoundSequentialDependence = false;
16620b57cec5SDimitry Andric     Dependence = false;
16630b57cec5SDimitry Andric     return true;
16640b57cec5SDimitry Andric   }
16650b57cec5SDimitry Andric 
16660b57cec5SDimitry Andric   return false;
16670b57cec5SDimitry Andric }
16680b57cec5SDimitry Andric 
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric bool HexagonPacketizerList::foundLSInPacket() {
16710b57cec5SDimitry Andric   bool FoundLoad = false;
16720b57cec5SDimitry Andric   bool FoundStore = false;
16730b57cec5SDimitry Andric 
16740b57cec5SDimitry Andric   for (auto MJ : CurrentPacketMIs) {
16750b57cec5SDimitry Andric     unsigned Opc = MJ->getOpcode();
16760b57cec5SDimitry Andric     if (Opc == Hexagon::S2_allocframe || Opc == Hexagon::L2_deallocframe)
16770b57cec5SDimitry Andric       continue;
16780b57cec5SDimitry Andric     if (HII->isMemOp(*MJ))
16790b57cec5SDimitry Andric       continue;
16800b57cec5SDimitry Andric     if (MJ->mayLoad())
16810b57cec5SDimitry Andric       FoundLoad = true;
16820b57cec5SDimitry Andric     if (MJ->mayStore() && !HII->isNewValueStore(*MJ))
16830b57cec5SDimitry Andric       FoundStore = true;
16840b57cec5SDimitry Andric   }
16850b57cec5SDimitry Andric   return FoundLoad && FoundStore;
16860b57cec5SDimitry Andric }
16870b57cec5SDimitry Andric 
16880b57cec5SDimitry Andric 
16890b57cec5SDimitry Andric MachineBasicBlock::iterator
16900b57cec5SDimitry Andric HexagonPacketizerList::addToPacket(MachineInstr &MI) {
16910b57cec5SDimitry Andric   MachineBasicBlock::iterator MII = MI.getIterator();
16920b57cec5SDimitry Andric   MachineBasicBlock *MBB = MI.getParent();
16930b57cec5SDimitry Andric 
16940b57cec5SDimitry Andric   if (CurrentPacketMIs.empty())
16950b57cec5SDimitry Andric     PacketStalls = false;
16960b57cec5SDimitry Andric   PacketStalls |= producesStall(MI);
16970b57cec5SDimitry Andric 
16980b57cec5SDimitry Andric   if (MI.isImplicitDef()) {
16990b57cec5SDimitry Andric     // Add to the packet to allow subsequent instructions to be checked
17000b57cec5SDimitry Andric     // properly.
17010b57cec5SDimitry Andric     CurrentPacketMIs.push_back(&MI);
17020b57cec5SDimitry Andric     return MII;
17030b57cec5SDimitry Andric   }
17040b57cec5SDimitry Andric   assert(ResourceTracker->canReserveResources(MI));
17050b57cec5SDimitry Andric 
17060b57cec5SDimitry Andric   bool ExtMI = HII->isExtended(MI) || HII->isConstExtended(MI);
17070b57cec5SDimitry Andric   bool Good = true;
17080b57cec5SDimitry Andric 
17090b57cec5SDimitry Andric   if (GlueToNewValueJump) {
17100b57cec5SDimitry Andric     MachineInstr &NvjMI = *++MII;
17110b57cec5SDimitry Andric     // We need to put both instructions in the same packet: MI and NvjMI.
17120b57cec5SDimitry Andric     // Either of them can require a constant extender. Try to add both to
17130b57cec5SDimitry Andric     // the current packet, and if that fails, end the packet and start a
17140b57cec5SDimitry Andric     // new one.
17150b57cec5SDimitry Andric     ResourceTracker->reserveResources(MI);
17160b57cec5SDimitry Andric     if (ExtMI)
17170b57cec5SDimitry Andric       Good = tryAllocateResourcesForConstExt(true);
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric     bool ExtNvjMI = HII->isExtended(NvjMI) || HII->isConstExtended(NvjMI);
17200b57cec5SDimitry Andric     if (Good) {
17210b57cec5SDimitry Andric       if (ResourceTracker->canReserveResources(NvjMI))
17220b57cec5SDimitry Andric         ResourceTracker->reserveResources(NvjMI);
17230b57cec5SDimitry Andric       else
17240b57cec5SDimitry Andric         Good = false;
17250b57cec5SDimitry Andric     }
17260b57cec5SDimitry Andric     if (Good && ExtNvjMI)
17270b57cec5SDimitry Andric       Good = tryAllocateResourcesForConstExt(true);
17280b57cec5SDimitry Andric 
17290b57cec5SDimitry Andric     if (!Good) {
17300b57cec5SDimitry Andric       endPacket(MBB, MI);
17310b57cec5SDimitry Andric       assert(ResourceTracker->canReserveResources(MI));
17320b57cec5SDimitry Andric       ResourceTracker->reserveResources(MI);
17330b57cec5SDimitry Andric       if (ExtMI) {
17340b57cec5SDimitry Andric         assert(canReserveResourcesForConstExt());
17350b57cec5SDimitry Andric         tryAllocateResourcesForConstExt(true);
17360b57cec5SDimitry Andric       }
17370b57cec5SDimitry Andric       assert(ResourceTracker->canReserveResources(NvjMI));
17380b57cec5SDimitry Andric       ResourceTracker->reserveResources(NvjMI);
17390b57cec5SDimitry Andric       if (ExtNvjMI) {
17400b57cec5SDimitry Andric         assert(canReserveResourcesForConstExt());
17410b57cec5SDimitry Andric         reserveResourcesForConstExt();
17420b57cec5SDimitry Andric       }
17430b57cec5SDimitry Andric     }
17440b57cec5SDimitry Andric     CurrentPacketMIs.push_back(&MI);
17450b57cec5SDimitry Andric     CurrentPacketMIs.push_back(&NvjMI);
17460b57cec5SDimitry Andric     return MII;
17470b57cec5SDimitry Andric   }
17480b57cec5SDimitry Andric 
17490b57cec5SDimitry Andric   ResourceTracker->reserveResources(MI);
17500b57cec5SDimitry Andric   if (ExtMI && !tryAllocateResourcesForConstExt(true)) {
17510b57cec5SDimitry Andric     endPacket(MBB, MI);
17520b57cec5SDimitry Andric     if (PromotedToDotNew)
17530b57cec5SDimitry Andric       demoteToDotOld(MI);
17540b57cec5SDimitry Andric     if (GlueAllocframeStore) {
17550b57cec5SDimitry Andric       useCalleesSP(MI);
17560b57cec5SDimitry Andric       GlueAllocframeStore = false;
17570b57cec5SDimitry Andric     }
17580b57cec5SDimitry Andric     ResourceTracker->reserveResources(MI);
17590b57cec5SDimitry Andric     reserveResourcesForConstExt();
17600b57cec5SDimitry Andric   }
17610b57cec5SDimitry Andric 
17620b57cec5SDimitry Andric   CurrentPacketMIs.push_back(&MI);
17630b57cec5SDimitry Andric   return MII;
17640b57cec5SDimitry Andric }
17650b57cec5SDimitry Andric 
17660b57cec5SDimitry Andric void HexagonPacketizerList::endPacket(MachineBasicBlock *MBB,
17670b57cec5SDimitry Andric                                       MachineBasicBlock::iterator EndMI) {
17680b57cec5SDimitry Andric   // Replace VLIWPacketizerList::endPacket(MBB, EndMI).
17698bcb0991SDimitry Andric   LLVM_DEBUG({
17708bcb0991SDimitry Andric     if (!CurrentPacketMIs.empty()) {
17718bcb0991SDimitry Andric       dbgs() << "Finalizing packet:\n";
17728bcb0991SDimitry Andric       unsigned Idx = 0;
17738bcb0991SDimitry Andric       for (MachineInstr *MI : CurrentPacketMIs) {
17748bcb0991SDimitry Andric         unsigned R = ResourceTracker->getUsedResources(Idx++);
17758bcb0991SDimitry Andric         dbgs() << " * [res:0x" << utohexstr(R) << "] " << *MI;
17768bcb0991SDimitry Andric       }
17778bcb0991SDimitry Andric     }
17788bcb0991SDimitry Andric   });
17790b57cec5SDimitry Andric 
17800b57cec5SDimitry Andric   bool memShufDisabled = getmemShufDisabled();
17810b57cec5SDimitry Andric   if (memShufDisabled && !foundLSInPacket()) {
17820b57cec5SDimitry Andric     setmemShufDisabled(false);
17830b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "  Not added to NoShufPacket\n");
17840b57cec5SDimitry Andric   }
17850b57cec5SDimitry Andric   memShufDisabled = getmemShufDisabled();
17860b57cec5SDimitry Andric 
17870b57cec5SDimitry Andric   OldPacketMIs.clear();
17880b57cec5SDimitry Andric   for (MachineInstr *MI : CurrentPacketMIs) {
17890b57cec5SDimitry Andric     MachineBasicBlock::instr_iterator NextMI = std::next(MI->getIterator());
17900b57cec5SDimitry Andric     for (auto &I : make_range(HII->expandVGatherPseudo(*MI), NextMI))
17910b57cec5SDimitry Andric       OldPacketMIs.push_back(&I);
17920b57cec5SDimitry Andric   }
17930b57cec5SDimitry Andric   CurrentPacketMIs.clear();
17940b57cec5SDimitry Andric 
17950b57cec5SDimitry Andric   if (OldPacketMIs.size() > 1) {
17960b57cec5SDimitry Andric     MachineBasicBlock::instr_iterator FirstMI(OldPacketMIs.front());
17970b57cec5SDimitry Andric     MachineBasicBlock::instr_iterator LastMI(EndMI.getInstrIterator());
17980b57cec5SDimitry Andric     finalizeBundle(*MBB, FirstMI, LastMI);
17990b57cec5SDimitry Andric     auto BundleMII = std::prev(FirstMI);
18000b57cec5SDimitry Andric     if (memShufDisabled)
18010b57cec5SDimitry Andric       HII->setBundleNoShuf(BundleMII);
18020b57cec5SDimitry Andric 
18030b57cec5SDimitry Andric     setmemShufDisabled(false);
18040b57cec5SDimitry Andric   }
18050b57cec5SDimitry Andric 
18065ffd83dbSDimitry Andric   PacketHasDuplex = false;
18075ffd83dbSDimitry Andric   PacketHasSLOT0OnlyInsn = false;
18080b57cec5SDimitry Andric   ResourceTracker->clearResources();
18090b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "End packet\n");
18100b57cec5SDimitry Andric }
18110b57cec5SDimitry Andric 
18120b57cec5SDimitry Andric bool HexagonPacketizerList::shouldAddToPacket(const MachineInstr &MI) {
18130b57cec5SDimitry Andric   if (Minimal)
18140b57cec5SDimitry Andric     return false;
18155ffd83dbSDimitry Andric 
18165ffd83dbSDimitry Andric   // Constrainst for not packetizing this MI with existing instructions in a
18175ffd83dbSDimitry Andric   // packet.
18185ffd83dbSDimitry Andric   //	MI is a store instruction.
18195ffd83dbSDimitry Andric   //	CurrentPacketMIs has a SLOT0 only instruction with constraint
18205ffd83dbSDimitry Andric   //    A_RESTRICT_NOSLOT1_STORE/isRestrictNoSlot1Store.
18215ffd83dbSDimitry Andric   if (MI.mayStore() && isPureSlot0InsnWithNoSlot1Store(MI))
18225ffd83dbSDimitry Andric     return false;
18235ffd83dbSDimitry Andric 
18245ffd83dbSDimitry Andric   if (producesStall(MI))
18255ffd83dbSDimitry Andric     return false;
18265ffd83dbSDimitry Andric 
18275ffd83dbSDimitry Andric   // If TinyCore with Duplexes is enabled, check if this MI can form a Duplex
18285ffd83dbSDimitry Andric   // with any other instruction in the existing packet.
18295ffd83dbSDimitry Andric   auto &HST = MI.getParent()->getParent()->getSubtarget<HexagonSubtarget>();
18305ffd83dbSDimitry Andric   // Constraint 1: Only one duplex allowed per packet.
18315ffd83dbSDimitry Andric   // Constraint 2: Consider duplex checks only if there is atleast one
18325ffd83dbSDimitry Andric   // instruction in a packet.
18335ffd83dbSDimitry Andric   // Constraint 3: If one of the existing instructions in the packet has a
18345ffd83dbSDimitry Andric   // SLOT0 only instruction that can not be duplexed, do not attempt to form
18355ffd83dbSDimitry Andric   // duplexes. (TODO: This will invalidate the L4_return* instructions to form a
18365ffd83dbSDimitry Andric   // duplex)
18375ffd83dbSDimitry Andric   if (HST.isTinyCoreWithDuplex() && CurrentPacketMIs.size() > 0 &&
18385ffd83dbSDimitry Andric       !PacketHasDuplex) {
18395ffd83dbSDimitry Andric     // Check for SLOT0 only non-duplexable instruction in packet.
18405ffd83dbSDimitry Andric     for (auto &MJ : CurrentPacketMIs)
18415ffd83dbSDimitry Andric       PacketHasSLOT0OnlyInsn |= HII->isPureSlot0(*MJ);
18425ffd83dbSDimitry Andric     // Get the Big Core Opcode (dup_*).
18435ffd83dbSDimitry Andric     int Opcode = HII->getDuplexOpcode(MI, false);
18445ffd83dbSDimitry Andric     if (Opcode >= 0) {
18455ffd83dbSDimitry Andric       // We now have an instruction that can be duplexed.
18465ffd83dbSDimitry Andric       for (auto &MJ : CurrentPacketMIs) {
18475ffd83dbSDimitry Andric         if (HII->isDuplexPair(MI, *MJ) && !PacketHasSLOT0OnlyInsn) {
18485ffd83dbSDimitry Andric           PacketHasDuplex = true;
18495ffd83dbSDimitry Andric           return true;
18505ffd83dbSDimitry Andric         }
18515ffd83dbSDimitry Andric       }
18525ffd83dbSDimitry Andric       // If it can not be duplexed, check if there is a valid transition in DFA
18535ffd83dbSDimitry Andric       // with the original opcode.
18545ffd83dbSDimitry Andric       MachineInstr &MIRef = const_cast<MachineInstr &>(MI);
18555ffd83dbSDimitry Andric       MIRef.setDesc(HII->get(Opcode));
18565ffd83dbSDimitry Andric       return ResourceTracker->canReserveResources(MIRef);
18575ffd83dbSDimitry Andric     }
18585ffd83dbSDimitry Andric   }
18595ffd83dbSDimitry Andric 
18605ffd83dbSDimitry Andric   return true;
18615ffd83dbSDimitry Andric }
18625ffd83dbSDimitry Andric 
18635ffd83dbSDimitry Andric bool HexagonPacketizerList::isPureSlot0InsnWithNoSlot1Store(
18645ffd83dbSDimitry Andric     const MachineInstr &MI) {
18655ffd83dbSDimitry Andric   bool noSlot1Store = false;
18665ffd83dbSDimitry Andric   bool isSlot0Only = false;
18675ffd83dbSDimitry Andric   for (auto J : CurrentPacketMIs) {
18685ffd83dbSDimitry Andric     noSlot1Store |= HII->isRestrictNoSlot1Store(*J);
18695ffd83dbSDimitry Andric     isSlot0Only |= HII->isPureSlot0(*J);
18705ffd83dbSDimitry Andric   }
18715ffd83dbSDimitry Andric 
18725ffd83dbSDimitry Andric   return (noSlot1Store && isSlot0Only);
18730b57cec5SDimitry Andric }
18740b57cec5SDimitry Andric 
18750b57cec5SDimitry Andric // V60 forward scheduling.
18760b57cec5SDimitry Andric bool HexagonPacketizerList::producesStall(const MachineInstr &I) {
18770b57cec5SDimitry Andric   // If the packet already stalls, then ignore the stall from a subsequent
18780b57cec5SDimitry Andric   // instruction in the same packet.
18790b57cec5SDimitry Andric   if (PacketStalls)
18800b57cec5SDimitry Andric     return false;
18810b57cec5SDimitry Andric 
18820b57cec5SDimitry Andric   // Check whether the previous packet is in a different loop. If this is the
18830b57cec5SDimitry Andric   // case, there is little point in trying to avoid a stall because that would
18840b57cec5SDimitry Andric   // favor the rare case (loop entry) over the common case (loop iteration).
18850b57cec5SDimitry Andric   //
18860b57cec5SDimitry Andric   // TODO: We should really be able to check all the incoming edges if this is
18870b57cec5SDimitry Andric   // the first packet in a basic block, so we can avoid stalls from the loop
18880b57cec5SDimitry Andric   // backedge.
18890b57cec5SDimitry Andric   if (!OldPacketMIs.empty()) {
18900b57cec5SDimitry Andric     auto *OldBB = OldPacketMIs.front()->getParent();
18910b57cec5SDimitry Andric     auto *ThisBB = I.getParent();
18920b57cec5SDimitry Andric     if (MLI->getLoopFor(OldBB) != MLI->getLoopFor(ThisBB))
18930b57cec5SDimitry Andric       return false;
18940b57cec5SDimitry Andric   }
18950b57cec5SDimitry Andric 
18960b57cec5SDimitry Andric   SUnit *SUI = MIToSUnit[const_cast<MachineInstr *>(&I)];
18970b57cec5SDimitry Andric 
18980b57cec5SDimitry Andric   // If the latency is 0 and there is a data dependence between this
18990b57cec5SDimitry Andric   // instruction and any instruction in the current packet, we disregard any
19000b57cec5SDimitry Andric   // potential stalls due to the instructions in the previous packet. Most of
19010b57cec5SDimitry Andric   // the instruction pairs that can go together in the same packet have 0
19020b57cec5SDimitry Andric   // latency between them. The exceptions are
19030b57cec5SDimitry Andric   // 1. NewValueJumps as they're generated much later and the latencies can't
19040b57cec5SDimitry Andric   // be changed at that point.
19050b57cec5SDimitry Andric   // 2. .cur instructions, if its consumer has a 0 latency successor (such as
19060b57cec5SDimitry Andric   // .new). In this case, the latency between .cur and the consumer stays
19070b57cec5SDimitry Andric   // non-zero even though we can have  both .cur and .new in the same packet.
19080b57cec5SDimitry Andric   // Changing the latency to 0 is not an option as it causes software pipeliner
19090b57cec5SDimitry Andric   // to not pipeline in some cases.
19100b57cec5SDimitry Andric 
19110b57cec5SDimitry Andric   // For Example:
19120b57cec5SDimitry Andric   // {
19130b57cec5SDimitry Andric   //   I1:  v6.cur = vmem(r0++#1)
19140b57cec5SDimitry Andric   //   I2:  v7 = valign(v6,v4,r2)
19150b57cec5SDimitry Andric   //   I3:  vmem(r5++#1) = v7.new
19160b57cec5SDimitry Andric   // }
19170b57cec5SDimitry Andric   // Here I2 and I3 has 0 cycle latency, but I1 and I2 has 2.
19180b57cec5SDimitry Andric 
19190b57cec5SDimitry Andric   for (auto J : CurrentPacketMIs) {
19200b57cec5SDimitry Andric     SUnit *SUJ = MIToSUnit[J];
19210b57cec5SDimitry Andric     for (auto &Pred : SUI->Preds)
19220b57cec5SDimitry Andric       if (Pred.getSUnit() == SUJ)
19230b57cec5SDimitry Andric         if ((Pred.getLatency() == 0 && Pred.isAssignedRegDep()) ||
19240b57cec5SDimitry Andric             HII->isNewValueJump(I) || HII->isToBeScheduledASAP(*J, I))
19250b57cec5SDimitry Andric           return false;
19260b57cec5SDimitry Andric   }
19270b57cec5SDimitry Andric 
19280b57cec5SDimitry Andric   // Check if the latency is greater than one between this instruction and any
19290b57cec5SDimitry Andric   // instruction in the previous packet.
19300b57cec5SDimitry Andric   for (auto J : OldPacketMIs) {
19310b57cec5SDimitry Andric     SUnit *SUJ = MIToSUnit[J];
19320b57cec5SDimitry Andric     for (auto &Pred : SUI->Preds)
19330b57cec5SDimitry Andric       if (Pred.getSUnit() == SUJ && Pred.getLatency() > 1)
19340b57cec5SDimitry Andric         return true;
19350b57cec5SDimitry Andric   }
19360b57cec5SDimitry Andric 
19370b57cec5SDimitry Andric   return false;
19380b57cec5SDimitry Andric }
19390b57cec5SDimitry Andric 
19400b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
19410b57cec5SDimitry Andric //                         Public Constructor Functions
19420b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
19430b57cec5SDimitry Andric 
19440b57cec5SDimitry Andric FunctionPass *llvm::createHexagonPacketizer(bool Minimal) {
19450b57cec5SDimitry Andric   return new HexagonPacketizer(Minimal);
19460b57cec5SDimitry Andric }
1947