LLVM API Documentation

HexagonVLIWPacketizer.cpp
Go to the documentation of this file.
00001 //===----- HexagonPacketizer.cpp - vliw packetizer ---------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This implements a simple VLIW packetizer using DFA. The packetizer works on
00011 // machine basic blocks. For each instruction I in BB, the packetizer consults
00012 // the DFA to see if machine resources are available to execute I. If so, the
00013 // packetizer checks if I depends on any instruction J in the current packet.
00014 // If no dependency is found, I is added to current packet and machine resource
00015 // is marked as taken. If any dependency is found, a target API call is made to
00016 // prune the dependence.
00017 //
00018 //===----------------------------------------------------------------------===//
00019 #include "llvm/CodeGen/DFAPacketizer.h"
00020 #include "Hexagon.h"
00021 #include "HexagonMachineFunctionInfo.h"
00022 #include "HexagonRegisterInfo.h"
00023 #include "HexagonSubtarget.h"
00024 #include "HexagonTargetMachine.h"
00025 #include "llvm/ADT/DenseMap.h"
00026 #include "llvm/ADT/Statistic.h"
00027 #include "llvm/CodeGen/LatencyPriorityQueue.h"
00028 #include "llvm/CodeGen/MachineDominators.h"
00029 #include "llvm/CodeGen/MachineFrameInfo.h"
00030 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
00031 #include "llvm/CodeGen/MachineFunctionPass.h"
00032 #include "llvm/CodeGen/MachineInstrBuilder.h"
00033 #include "llvm/CodeGen/MachineLoopInfo.h"
00034 #include "llvm/CodeGen/MachineRegisterInfo.h"
00035 #include "llvm/CodeGen/Passes.h"
00036 #include "llvm/CodeGen/ScheduleDAG.h"
00037 #include "llvm/CodeGen/ScheduleDAGInstrs.h"
00038 #include "llvm/CodeGen/ScheduleHazardRecognizer.h"
00039 #include "llvm/CodeGen/SchedulerRegistry.h"
00040 #include "llvm/MC/MCInstrItineraries.h"
00041 #include "llvm/Support/CommandLine.h"
00042 #include "llvm/Support/Compiler.h"
00043 #include "llvm/Support/Debug.h"
00044 #include "llvm/Support/MathExtras.h"
00045 #include "llvm/Target/TargetInstrInfo.h"
00046 #include "llvm/Target/TargetMachine.h"
00047 #include "llvm/Target/TargetRegisterInfo.h"
00048 #include <map>
00049 #include <vector>
00050 
00051 using namespace llvm;
00052 
00053 #define DEBUG_TYPE "packets"
00054 
00055 static cl::opt<bool> PacketizeVolatiles("hexagon-packetize-volatiles",
00056       cl::ZeroOrMore, cl::Hidden, cl::init(true),
00057       cl::desc("Allow non-solo packetization of volatile memory references"));
00058 
00059 namespace llvm {
00060   void initializeHexagonPacketizerPass(PassRegistry&);
00061 }
00062 
00063 
00064 namespace {
00065   class HexagonPacketizer : public MachineFunctionPass {
00066 
00067   public:
00068     static char ID;
00069     HexagonPacketizer() : MachineFunctionPass(ID) {
00070       initializeHexagonPacketizerPass(*PassRegistry::getPassRegistry());
00071     }
00072 
00073     void getAnalysisUsage(AnalysisUsage &AU) const override {
00074       AU.setPreservesCFG();
00075       AU.addRequired<MachineDominatorTree>();
00076       AU.addRequired<MachineBranchProbabilityInfo>();
00077       AU.addPreserved<MachineDominatorTree>();
00078       AU.addRequired<MachineLoopInfo>();
00079       AU.addPreserved<MachineLoopInfo>();
00080       MachineFunctionPass::getAnalysisUsage(AU);
00081     }
00082 
00083     const char *getPassName() const override {
00084       return "Hexagon Packetizer";
00085     }
00086 
00087     bool runOnMachineFunction(MachineFunction &Fn) override;
00088   };
00089   char HexagonPacketizer::ID = 0;
00090 
00091   class HexagonPacketizerList : public VLIWPacketizerList {
00092 
00093   private:
00094 
00095     // Has the instruction been promoted to a dot-new instruction.
00096     bool PromotedToDotNew;
00097 
00098     // Has the instruction been glued to allocframe.
00099     bool GlueAllocframeStore;
00100 
00101     // Has the feeder instruction been glued to new value jump.
00102     bool GlueToNewValueJump;
00103 
00104     // Check if there is a dependence between some instruction already in this
00105     // packet and this instruction.
00106     bool Dependence;
00107 
00108     // Only check for dependence if there are resources available to
00109     // schedule this instruction.
00110     bool FoundSequentialDependence;
00111 
00112     /// \brief A handle to the branch probability pass.
00113    const MachineBranchProbabilityInfo *MBPI;
00114 
00115    // Track MIs with ignored dependece.
00116    std::vector<MachineInstr*> IgnoreDepMIs;
00117 
00118   public:
00119     // Ctor.
00120     HexagonPacketizerList(MachineFunction &MF, MachineLoopInfo &MLI,
00121                           const MachineBranchProbabilityInfo *MBPI);
00122 
00123     // initPacketizerState - initialize some internal flags.
00124     void initPacketizerState() override;
00125 
00126     // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
00127     bool ignorePseudoInstruction(MachineInstr *MI,
00128                                  MachineBasicBlock *MBB) override;
00129 
00130     // isSoloInstruction - return true if instruction MI can not be packetized
00131     // with any other instruction, which means that MI itself is a packet.
00132     bool isSoloInstruction(MachineInstr *MI) override;
00133 
00134     // isLegalToPacketizeTogether - Is it legal to packetize SUI and SUJ
00135     // together.
00136     bool isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) override;
00137 
00138     // isLegalToPruneDependencies - Is it legal to prune dependece between SUI
00139     // and SUJ.
00140     bool isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) override;
00141 
00142     MachineBasicBlock::iterator addToPacket(MachineInstr *MI) override;
00143   private:
00144     bool IsCallDependent(MachineInstr* MI, SDep::Kind DepType, unsigned DepReg);
00145     bool PromoteToDotNew(MachineInstr* MI, SDep::Kind DepType,
00146                          MachineBasicBlock::iterator &MII,
00147                          const TargetRegisterClass* RC);
00148     bool CanPromoteToDotNew(MachineInstr* MI, SUnit* PacketSU,
00149                             unsigned DepReg,
00150                             std::map <MachineInstr*, SUnit*> MIToSUnit,
00151                             MachineBasicBlock::iterator &MII,
00152                             const TargetRegisterClass* RC);
00153     bool CanPromoteToNewValue(MachineInstr* MI, SUnit* PacketSU,
00154                               unsigned DepReg,
00155                               std::map <MachineInstr*, SUnit*> MIToSUnit,
00156                               MachineBasicBlock::iterator &MII);
00157     bool CanPromoteToNewValueStore(MachineInstr* MI, MachineInstr* PacketMI,
00158                                    unsigned DepReg,
00159                                    std::map <MachineInstr*, SUnit*> MIToSUnit);
00160     bool DemoteToDotOld(MachineInstr* MI);
00161     bool ArePredicatesComplements(MachineInstr* MI1, MachineInstr* MI2,
00162                     std::map <MachineInstr*, SUnit*> MIToSUnit);
00163     bool RestrictingDepExistInPacket(MachineInstr*,
00164                     unsigned, std::map <MachineInstr*, SUnit*>);
00165     bool isNewifiable(MachineInstr* MI);
00166     bool isCondInst(MachineInstr* MI);
00167     bool tryAllocateResourcesForConstExt(MachineInstr* MI);
00168     bool canReserveResourcesForConstExt(MachineInstr *MI);
00169     void reserveResourcesForConstExt(MachineInstr* MI);
00170     bool isNewValueInst(MachineInstr* MI);
00171   };
00172 }
00173 
00174 INITIALIZE_PASS_BEGIN(HexagonPacketizer, "packets", "Hexagon Packetizer",
00175                       false, false)
00176 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
00177 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
00178 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
00179 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
00180 INITIALIZE_PASS_END(HexagonPacketizer, "packets", "Hexagon Packetizer",
00181                     false, false)
00182 
00183 
00184 // HexagonPacketizerList Ctor.
00185 HexagonPacketizerList::HexagonPacketizerList(
00186     MachineFunction &MF, MachineLoopInfo &MLI,
00187     const MachineBranchProbabilityInfo *MBPI)
00188     : VLIWPacketizerList(MF, MLI, true) {
00189   this->MBPI = MBPI;
00190 }
00191 
00192 bool HexagonPacketizer::runOnMachineFunction(MachineFunction &Fn) {
00193   const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
00194   MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
00195   const MachineBranchProbabilityInfo *MBPI =
00196     &getAnalysis<MachineBranchProbabilityInfo>();
00197   // Instantiate the packetizer.
00198   HexagonPacketizerList Packetizer(Fn, MLI, MBPI);
00199 
00200   // DFA state table should not be empty.
00201   assert(Packetizer.getResourceTracker() && "Empty DFA table!");
00202 
00203   //
00204   // Loop over all basic blocks and remove KILL pseudo-instructions
00205   // These instructions confuse the dependence analysis. Consider:
00206   // D0 = ...   (Insn 0)
00207   // R0 = KILL R0, D0 (Insn 1)
00208   // R0 = ... (Insn 2)
00209   // Here, Insn 1 will result in the dependence graph not emitting an output
00210   // dependence between Insn 0 and Insn 2. This can lead to incorrect
00211   // packetization
00212   //
00213   for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
00214        MBB != MBBe; ++MBB) {
00215     MachineBasicBlock::iterator End = MBB->end();
00216     MachineBasicBlock::iterator MI = MBB->begin();
00217     while (MI != End) {
00218       if (MI->isKill()) {
00219         MachineBasicBlock::iterator DeleteMI = MI;
00220         ++MI;
00221         MBB->erase(DeleteMI);
00222         End = MBB->end();
00223         continue;
00224       }
00225       ++MI;
00226     }
00227   }
00228 
00229   // Loop over all of the basic blocks.
00230   for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
00231        MBB != MBBe; ++MBB) {
00232     // Find scheduling regions and schedule / packetize each region.
00233     unsigned RemainingCount = MBB->size();
00234     for(MachineBasicBlock::iterator RegionEnd = MBB->end();
00235         RegionEnd != MBB->begin();) {
00236       // The next region starts above the previous region. Look backward in the
00237       // instruction stream until we find the nearest boundary.
00238       MachineBasicBlock::iterator I = RegionEnd;
00239       for(;I != MBB->begin(); --I, --RemainingCount) {
00240         if (TII->isSchedulingBoundary(std::prev(I), MBB, Fn))
00241           break;
00242       }
00243       I = MBB->begin();
00244 
00245       // Skip empty scheduling regions.
00246       if (I == RegionEnd) {
00247         RegionEnd = std::prev(RegionEnd);
00248         --RemainingCount;
00249         continue;
00250       }
00251       // Skip regions with one instruction.
00252       if (I == std::prev(RegionEnd)) {
00253         RegionEnd = std::prev(RegionEnd);
00254         continue;
00255       }
00256 
00257       Packetizer.PacketizeMIs(MBB, I, RegionEnd);
00258       RegionEnd = I;
00259     }
00260   }
00261 
00262   return true;
00263 }
00264 
00265 
00266 static bool IsIndirectCall(MachineInstr* MI) {
00267   return ((MI->getOpcode() == Hexagon::CALLR) ||
00268           (MI->getOpcode() == Hexagon::CALLRv3));
00269 }
00270 
00271 // Reserve resources for constant extender. Trigure an assertion if
00272 // reservation fail.
00273 void HexagonPacketizerList::reserveResourcesForConstExt(MachineInstr* MI) {
00274   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00275   MachineFunction *MF = MI->getParent()->getParent();
00276   MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::IMMEXT_i),
00277                                                   MI->getDebugLoc());
00278 
00279   if (ResourceTracker->canReserveResources(PseudoMI)) {
00280     ResourceTracker->reserveResources(PseudoMI);
00281     MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
00282   } else {
00283     MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
00284     llvm_unreachable("can not reserve resources for constant extender.");
00285   }
00286   return;
00287 }
00288 
00289 bool HexagonPacketizerList::canReserveResourcesForConstExt(MachineInstr *MI) {
00290   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00291   assert((QII->isExtended(MI) || QII->isConstExtended(MI)) &&
00292          "Should only be called for constant extended instructions");
00293   MachineFunction *MF = MI->getParent()->getParent();
00294   MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::IMMEXT_i),
00295                                                   MI->getDebugLoc());
00296   bool CanReserve = ResourceTracker->canReserveResources(PseudoMI);
00297   MF->DeleteMachineInstr(PseudoMI);
00298   return CanReserve;
00299 }
00300 
00301 // Allocate resources (i.e. 4 bytes) for constant extender. If succeed, return
00302 // true, otherwise, return false.
00303 bool HexagonPacketizerList::tryAllocateResourcesForConstExt(MachineInstr* MI) {
00304   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00305   MachineFunction *MF = MI->getParent()->getParent();
00306   MachineInstr *PseudoMI = MF->CreateMachineInstr(QII->get(Hexagon::IMMEXT_i),
00307                                                   MI->getDebugLoc());
00308 
00309   if (ResourceTracker->canReserveResources(PseudoMI)) {
00310     ResourceTracker->reserveResources(PseudoMI);
00311     MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
00312     return true;
00313   } else {
00314     MI->getParent()->getParent()->DeleteMachineInstr(PseudoMI);
00315     return false;
00316   }
00317 }
00318 
00319 
00320 bool HexagonPacketizerList::IsCallDependent(MachineInstr* MI,
00321                                           SDep::Kind DepType,
00322                                           unsigned DepReg) {
00323 
00324   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00325   const HexagonRegisterInfo *QRI =
00326       (const HexagonRegisterInfo *)TM.getSubtargetImpl()->getRegisterInfo();
00327 
00328   // Check for lr dependence
00329   if (DepReg == QRI->getRARegister()) {
00330     return true;
00331   }
00332 
00333   if (QII->isDeallocRet(MI)) {
00334     if (DepReg == QRI->getFrameRegister() ||
00335         DepReg == QRI->getStackRegister())
00336       return true;
00337   }
00338 
00339   // Check if this is a predicate dependence
00340   const TargetRegisterClass* RC = QRI->getMinimalPhysRegClass(DepReg);
00341   if (RC == &Hexagon::PredRegsRegClass) {
00342     return true;
00343   }
00344 
00345   //
00346   // Lastly check for an operand used in an indirect call
00347   // If we had an attribute for checking if an instruction is an indirect call,
00348   // then we could have avoided this relatively brittle implementation of
00349   // IsIndirectCall()
00350   //
00351   // Assumes that the first operand of the CALLr is the function address
00352   //
00353   if (IsIndirectCall(MI) && (DepType == SDep::Data)) {
00354     MachineOperand MO = MI->getOperand(0);
00355     if (MO.isReg() && MO.isUse() && (MO.getReg() == DepReg)) {
00356       return true;
00357     }
00358   }
00359 
00360   return false;
00361 }
00362 
00363 static bool IsRegDependence(const SDep::Kind DepType) {
00364   return (DepType == SDep::Data || DepType == SDep::Anti ||
00365           DepType == SDep::Output);
00366 }
00367 
00368 static bool IsDirectJump(MachineInstr* MI) {
00369   return (MI->getOpcode() == Hexagon::JMP);
00370 }
00371 
00372 static bool IsSchedBarrier(MachineInstr* MI) {
00373   switch (MI->getOpcode()) {
00374   case Hexagon::BARRIER:
00375     return true;
00376   }
00377   return false;
00378 }
00379 
00380 static bool IsControlFlow(MachineInstr* MI) {
00381   return (MI->getDesc().isTerminator() || MI->getDesc().isCall());
00382 }
00383 
00384 static bool IsLoopN(MachineInstr *MI) {
00385   return (MI->getOpcode() == Hexagon::LOOP0_i ||
00386           MI->getOpcode() == Hexagon::LOOP0_r);
00387 }
00388 
00389 /// DoesModifyCalleeSavedReg - Returns true if the instruction modifies a
00390 /// callee-saved register.
00391 static bool DoesModifyCalleeSavedReg(MachineInstr *MI,
00392                                      const TargetRegisterInfo *TRI) {
00393   for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(); *CSR; ++CSR) {
00394     unsigned CalleeSavedReg = *CSR;
00395     if (MI->modifiesRegister(CalleeSavedReg, TRI))
00396       return true;
00397   }
00398   return false;
00399 }
00400 
00401 // Returns true if an instruction can be promoted to .new predicate
00402 // or new-value store.
00403 bool HexagonPacketizerList::isNewifiable(MachineInstr* MI) {
00404   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00405   if ( isCondInst(MI) || QII->mayBeNewStore(MI))
00406     return true;
00407   else
00408     return false;
00409 }
00410 
00411 bool HexagonPacketizerList::isCondInst (MachineInstr* MI) {
00412   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00413   const MCInstrDesc& TID = MI->getDesc();
00414                                     // bug 5670: until that is fixed,
00415                                     // this portion is disabled.
00416   if (   TID.isConditionalBranch()  // && !IsRegisterJump(MI)) ||
00417       || QII->isConditionalTransfer(MI)
00418       || QII->isConditionalALU32(MI)
00419       || QII->isConditionalLoad(MI)
00420       || QII->isConditionalStore(MI)) {
00421     return true;
00422   }
00423   return false;
00424 }
00425 
00426 
00427 // Promote an instructiont to its .new form.
00428 // At this time, we have already made a call to CanPromoteToDotNew
00429 // and made sure that it can *indeed* be promoted.
00430 bool HexagonPacketizerList::PromoteToDotNew(MachineInstr* MI,
00431                         SDep::Kind DepType, MachineBasicBlock::iterator &MII,
00432                         const TargetRegisterClass* RC) {
00433 
00434   assert (DepType == SDep::Data);
00435   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00436 
00437   int NewOpcode;
00438   if (RC == &Hexagon::PredRegsRegClass)
00439     NewOpcode = QII->GetDotNewPredOp(MI, MBPI);
00440   else
00441     NewOpcode = QII->GetDotNewOp(MI);
00442   MI->setDesc(QII->get(NewOpcode));
00443 
00444   return true;
00445 }
00446 
00447 bool HexagonPacketizerList::DemoteToDotOld(MachineInstr* MI) {
00448   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00449   int NewOpcode = QII->GetDotOldOp(MI->getOpcode());
00450   MI->setDesc(QII->get(NewOpcode));
00451   return true;
00452 }
00453 
00454 enum PredicateKind {
00455   PK_False,
00456   PK_True,
00457   PK_Unknown
00458 };
00459 
00460 /// Returns true if an instruction is predicated on p0 and false if it's
00461 /// predicated on !p0.
00462 static PredicateKind getPredicateSense(MachineInstr* MI,
00463                                        const HexagonInstrInfo *QII) {
00464   if (!QII->isPredicated(MI))
00465     return PK_Unknown;
00466 
00467   if (QII->isPredicatedTrue(MI))
00468     return PK_True;
00469 
00470   return PK_False;
00471 }
00472 
00473 static MachineOperand& GetPostIncrementOperand(MachineInstr *MI,
00474                                                const HexagonInstrInfo *QII) {
00475   assert(QII->isPostIncrement(MI) && "Not a post increment operation.");
00476 #ifndef NDEBUG
00477   // Post Increment means duplicates. Use dense map to find duplicates in the
00478   // list. Caution: Densemap initializes with the minimum of 64 buckets,
00479   // whereas there are at most 5 operands in the post increment.
00480   DenseMap<unsigned,  unsigned> DefRegsSet;
00481   for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++)
00482     if (MI->getOperand(opNum).isReg() &&
00483         MI->getOperand(opNum).isDef()) {
00484       DefRegsSet[MI->getOperand(opNum).getReg()] = 1;
00485     }
00486 
00487   for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++)
00488     if (MI->getOperand(opNum).isReg() &&
00489         MI->getOperand(opNum).isUse()) {
00490       if (DefRegsSet[MI->getOperand(opNum).getReg()]) {
00491         return MI->getOperand(opNum);
00492       }
00493     }
00494 #else
00495   if (MI->getDesc().mayLoad()) {
00496     // The 2nd operand is always the post increment operand in load.
00497     assert(MI->getOperand(1).isReg() &&
00498                 "Post increment operand has be to a register.");
00499     return (MI->getOperand(1));
00500   }
00501   if (MI->getDesc().mayStore()) {
00502     // The 1st operand is always the post increment operand in store.
00503     assert(MI->getOperand(0).isReg() &&
00504                 "Post increment operand has be to a register.");
00505     return (MI->getOperand(0));
00506   }
00507 #endif
00508   // we should never come here.
00509   llvm_unreachable("mayLoad or mayStore not set for Post Increment operation");
00510 }
00511 
00512 // get the value being stored
00513 static MachineOperand& GetStoreValueOperand(MachineInstr *MI) {
00514   // value being stored is always the last operand.
00515   return (MI->getOperand(MI->getNumOperands()-1));
00516 }
00517 
00518 // can be new value store?
00519 // Following restrictions are to be respected in convert a store into
00520 // a new value store.
00521 // 1. If an instruction uses auto-increment, its address register cannot
00522 //    be a new-value register. Arch Spec 5.4.2.1
00523 // 2. If an instruction uses absolute-set addressing mode,
00524 //    its address register cannot be a new-value register.
00525 //    Arch Spec 5.4.2.1.TODO: This is not enabled as
00526 //    as absolute-set address mode patters are not implemented.
00527 // 3. If an instruction produces a 64-bit result, its registers cannot be used
00528 //    as new-value registers. Arch Spec 5.4.2.2.
00529 // 4. If the instruction that sets a new-value register is conditional, then
00530 //    the instruction that uses the new-value register must also be conditional,
00531 //    and both must always have their predicates evaluate identically.
00532 //    Arch Spec 5.4.2.3.
00533 // 5. There is an implied restriction of a packet can not have another store,
00534 //    if there is a  new value store in the packet. Corollary, if there is
00535 //    already a store in a packet, there can not be a new value store.
00536 //    Arch Spec: 3.4.4.2
00537 bool HexagonPacketizerList::CanPromoteToNewValueStore( MachineInstr *MI,
00538                 MachineInstr *PacketMI, unsigned DepReg,
00539                 std::map <MachineInstr*, SUnit*> MIToSUnit) {
00540   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00541   // Make sure we are looking at the store, that can be promoted.
00542   if (!QII->mayBeNewStore(MI))
00543     return false;
00544 
00545   // Make sure there is dependency and can be new value'ed
00546   if (GetStoreValueOperand(MI).isReg() &&
00547       GetStoreValueOperand(MI).getReg() != DepReg)
00548     return false;
00549 
00550   const HexagonRegisterInfo *QRI =
00551       (const HexagonRegisterInfo *)TM.getSubtargetImpl()->getRegisterInfo();
00552   const MCInstrDesc& MCID = PacketMI->getDesc();
00553   // first operand is always the result
00554 
00555   const TargetRegisterClass* PacketRC = QII->getRegClass(MCID, 0, QRI, MF);
00556 
00557   // if there is already an store in the packet, no can do new value store
00558   // Arch Spec 3.4.4.2.
00559   for (std::vector<MachineInstr*>::iterator VI = CurrentPacketMIs.begin(),
00560          VE = CurrentPacketMIs.end();
00561        (VI != VE); ++VI) {
00562     SUnit* PacketSU = MIToSUnit[*VI];
00563     if (PacketSU->getInstr()->getDesc().mayStore() ||
00564         // if we have mayStore = 1 set on ALLOCFRAME and DEALLOCFRAME,
00565         // then we don't need this
00566         PacketSU->getInstr()->getOpcode() == Hexagon::ALLOCFRAME ||
00567         PacketSU->getInstr()->getOpcode() == Hexagon::DEALLOCFRAME)
00568       return false;
00569   }
00570 
00571   if (PacketRC == &Hexagon::DoubleRegsRegClass) {
00572     // new value store constraint: double regs can not feed into new value store
00573     // arch spec section: 5.4.2.2
00574     return false;
00575   }
00576 
00577   // Make sure it's NOT the post increment register that we are going to
00578   // new value.
00579   if (QII->isPostIncrement(MI) &&
00580       MI->getDesc().mayStore() &&
00581       GetPostIncrementOperand(MI, QII).getReg() == DepReg) {
00582     return false;
00583   }
00584 
00585   if (QII->isPostIncrement(PacketMI) &&
00586       PacketMI->getDesc().mayLoad() &&
00587       GetPostIncrementOperand(PacketMI, QII).getReg() == DepReg) {
00588     // if source is post_inc, or absolute-set addressing,
00589     // it can not feed into new value store
00590     //  r3 = memw(r2++#4)
00591     //  memw(r30 + #-1404) = r2.new -> can not be new value store
00592     // arch spec section: 5.4.2.1
00593     return false;
00594   }
00595 
00596   // If the source that feeds the store is predicated, new value store must
00597   // also be predicated.
00598   if (QII->isPredicated(PacketMI)) {
00599     if (!QII->isPredicated(MI))
00600       return false;
00601 
00602     // Check to make sure that they both will have their predicates
00603     // evaluate identically
00604     unsigned predRegNumSrc = 0;
00605     unsigned predRegNumDst = 0;
00606     const TargetRegisterClass* predRegClass = nullptr;
00607 
00608     // Get predicate register used in the source instruction
00609     for(unsigned opNum = 0; opNum < PacketMI->getNumOperands(); opNum++) {
00610       if ( PacketMI->getOperand(opNum).isReg())
00611       predRegNumSrc = PacketMI->getOperand(opNum).getReg();
00612       predRegClass = QRI->getMinimalPhysRegClass(predRegNumSrc);
00613       if (predRegClass == &Hexagon::PredRegsRegClass) {
00614         break;
00615       }
00616     }
00617     assert ((predRegClass == &Hexagon::PredRegsRegClass ) &&
00618         ("predicate register not found in a predicated PacketMI instruction"));
00619 
00620     // Get predicate register used in new-value store instruction
00621     for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
00622       if ( MI->getOperand(opNum).isReg())
00623       predRegNumDst = MI->getOperand(opNum).getReg();
00624       predRegClass = QRI->getMinimalPhysRegClass(predRegNumDst);
00625       if (predRegClass == &Hexagon::PredRegsRegClass) {
00626         break;
00627       }
00628     }
00629     assert ((predRegClass == &Hexagon::PredRegsRegClass ) &&
00630             ("predicate register not found in a predicated MI instruction"));
00631 
00632     // New-value register producer and user (store) need to satisfy these
00633     // constraints:
00634     // 1) Both instructions should be predicated on the same register.
00635     // 2) If producer of the new-value register is .new predicated then store
00636     // should also be .new predicated and if producer is not .new predicated
00637     // then store should not be .new predicated.
00638     // 3) Both new-value register producer and user should have same predicate
00639     // sense, i.e, either both should be negated or both should be none negated.
00640 
00641     if (( predRegNumDst != predRegNumSrc) ||
00642           QII->isDotNewInst(PacketMI) != QII->isDotNewInst(MI)  ||
00643           getPredicateSense(MI, QII) != getPredicateSense(PacketMI, QII)) {
00644       return false;
00645     }
00646   }
00647 
00648   // Make sure that other than the new-value register no other store instruction
00649   // register has been modified in the same packet. Predicate registers can be
00650   // modified by they should not be modified between the producer and the store
00651   // instruction as it will make them both conditional on different values.
00652   // We already know this to be true for all the instructions before and
00653   // including PacketMI. Howerver, we need to perform the check for the
00654   // remaining instructions in the packet.
00655 
00656   std::vector<MachineInstr*>::iterator VI;
00657   std::vector<MachineInstr*>::iterator VE;
00658   unsigned StartCheck = 0;
00659 
00660   for (VI=CurrentPacketMIs.begin(), VE = CurrentPacketMIs.end();
00661       (VI != VE); ++VI) {
00662     SUnit* TempSU = MIToSUnit[*VI];
00663     MachineInstr* TempMI = TempSU->getInstr();
00664 
00665     // Following condition is true for all the instructions until PacketMI is
00666     // reached (StartCheck is set to 0 before the for loop).
00667     // StartCheck flag is 1 for all the instructions after PacketMI.
00668     if (TempMI != PacketMI && !StartCheck) // start processing only after
00669       continue;                            // encountering PacketMI
00670 
00671     StartCheck = 1;
00672     if (TempMI == PacketMI) // We don't want to check PacketMI for dependence
00673       continue;
00674 
00675     for(unsigned opNum = 0; opNum < MI->getNumOperands(); opNum++) {
00676       if (MI->getOperand(opNum).isReg() &&
00677           TempSU->getInstr()->modifiesRegister(MI->getOperand(opNum).getReg(),
00678                                                QRI))
00679         return false;
00680     }
00681   }
00682 
00683   // Make sure that for non-POST_INC stores:
00684   // 1. The only use of reg is DepReg and no other registers.
00685   //    This handles V4 base+index registers.
00686   //    The following store can not be dot new.
00687   //    Eg.   r0 = add(r0, #3)a
00688   //          memw(r1+r0<<#2) = r0
00689   if (!QII->isPostIncrement(MI) &&
00690       GetStoreValueOperand(MI).isReg() &&
00691       GetStoreValueOperand(MI).getReg() == DepReg) {
00692     for(unsigned opNum = 0; opNum < MI->getNumOperands()-1; opNum++) {
00693       if (MI->getOperand(opNum).isReg() &&
00694           MI->getOperand(opNum).getReg() == DepReg) {
00695         return false;
00696       }
00697     }
00698     // 2. If data definition is because of implicit definition of the register,
00699     //    do not newify the store. Eg.
00700     //    %R9<def> = ZXTH %R12, %D6<imp-use>, %R12<imp-def>
00701     //    STrih_indexed %R8, 2, %R12<kill>; mem:ST2[%scevgep343]
00702     for(unsigned opNum = 0; opNum < PacketMI->getNumOperands(); opNum++) {
00703       if (PacketMI->getOperand(opNum).isReg() &&
00704           PacketMI->getOperand(opNum).getReg() == DepReg &&
00705           PacketMI->getOperand(opNum).isDef() &&
00706           PacketMI->getOperand(opNum).isImplicit()) {
00707         return false;
00708       }
00709     }
00710   }
00711 
00712   // Can be dot new store.
00713   return true;
00714 }
00715 
00716 // can this MI to promoted to either
00717 // new value store or new value jump
00718 bool HexagonPacketizerList::CanPromoteToNewValue( MachineInstr *MI,
00719                 SUnit *PacketSU, unsigned DepReg,
00720                 std::map <MachineInstr*, SUnit*> MIToSUnit,
00721                 MachineBasicBlock::iterator &MII)
00722 {
00723 
00724   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00725   const HexagonRegisterInfo *QRI =
00726       (const HexagonRegisterInfo *)TM.getSubtargetImpl()->getRegisterInfo();
00727   if (!QRI->Subtarget.hasV4TOps() ||
00728       !QII->mayBeNewStore(MI))
00729     return false;
00730 
00731   MachineInstr *PacketMI = PacketSU->getInstr();
00732 
00733   // Check to see the store can be new value'ed.
00734   if (CanPromoteToNewValueStore(MI, PacketMI, DepReg, MIToSUnit))
00735     return true;
00736 
00737   // Check to see the compare/jump can be new value'ed.
00738   // This is done as a pass on its own. Don't need to check it here.
00739   return false;
00740 }
00741 
00742 // Check to see if an instruction can be dot new
00743 // There are three kinds.
00744 // 1. dot new on predicate - V2/V3/V4
00745 // 2. dot new on stores NV/ST - V4
00746 // 3. dot new on jump NV/J - V4 -- This is generated in a pass.
00747 bool HexagonPacketizerList::CanPromoteToDotNew( MachineInstr *MI,
00748                               SUnit *PacketSU, unsigned DepReg,
00749                               std::map <MachineInstr*, SUnit*> MIToSUnit,
00750                               MachineBasicBlock::iterator &MII,
00751                               const TargetRegisterClass* RC )
00752 {
00753   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00754   // Already a dot new instruction.
00755   if (QII->isDotNewInst(MI) && !QII->mayBeNewStore(MI))
00756     return false;
00757 
00758   if (!isNewifiable(MI))
00759     return false;
00760 
00761   // predicate .new
00762   if (RC == &Hexagon::PredRegsRegClass && isCondInst(MI))
00763       return true;
00764   else if (RC != &Hexagon::PredRegsRegClass &&
00765       !QII->mayBeNewStore(MI)) // MI is not a new-value store
00766     return false;
00767   else {
00768     // Create a dot new machine instruction to see if resources can be
00769     // allocated. If not, bail out now.
00770     int NewOpcode = QII->GetDotNewOp(MI);
00771     const MCInstrDesc &desc = QII->get(NewOpcode);
00772     DebugLoc dl;
00773     MachineInstr *NewMI =
00774                     MI->getParent()->getParent()->CreateMachineInstr(desc, dl);
00775     bool ResourcesAvailable = ResourceTracker->canReserveResources(NewMI);
00776     MI->getParent()->getParent()->DeleteMachineInstr(NewMI);
00777 
00778     if (!ResourcesAvailable)
00779       return false;
00780 
00781     // new value store only
00782     // new new value jump generated as a passes
00783     if (!CanPromoteToNewValue(MI, PacketSU, DepReg, MIToSUnit, MII)) {
00784       return false;
00785     }
00786   }
00787   return true;
00788 }
00789 
00790 // Go through the packet instructions and search for anti dependency
00791 // between them and DepReg from MI
00792 // Consider this case:
00793 // Trying to add
00794 // a) %R1<def> = TFRI_cdNotPt %P3, 2
00795 // to this packet:
00796 // {
00797 //   b) %P0<def> = OR_pp %P3<kill>, %P0<kill>
00798 //   c) %P3<def> = TFR_PdRs %R23
00799 //   d) %R1<def> = TFRI_cdnPt %P3, 4
00800 //  }
00801 // The P3 from a) and d) will be complements after
00802 // a)'s P3 is converted to .new form
00803 // Anti Dep between c) and b) is irrelevant for this case
00804 bool HexagonPacketizerList::RestrictingDepExistInPacket (MachineInstr* MI,
00805       unsigned DepReg,
00806       std::map <MachineInstr*, SUnit*> MIToSUnit) {
00807 
00808   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00809   SUnit* PacketSUDep = MIToSUnit[MI];
00810 
00811   for (std::vector<MachineInstr*>::iterator VIN = CurrentPacketMIs.begin(),
00812        VEN = CurrentPacketMIs.end(); (VIN != VEN); ++VIN) {
00813 
00814     // We only care for dependencies to predicated instructions
00815     if(!QII->isPredicated(*VIN)) continue;
00816 
00817     // Scheduling Unit for current insn in the packet
00818     SUnit* PacketSU = MIToSUnit[*VIN];
00819 
00820     // Look at dependencies between current members of the packet
00821     // and predicate defining instruction MI.
00822     // Make sure that dependency is on the exact register
00823     // we care about.
00824     if (PacketSU->isSucc(PacketSUDep)) {
00825       for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
00826         if ((PacketSU->Succs[i].getSUnit() == PacketSUDep) &&
00827             (PacketSU->Succs[i].getKind() == SDep::Anti) &&
00828             (PacketSU->Succs[i].getReg() == DepReg)) {
00829           return true;
00830         }
00831       }
00832     }
00833   }
00834 
00835   return false;
00836 }
00837 
00838 
00839 /// Gets the predicate register of a predicated instruction.
00840 static unsigned getPredicatedRegister(MachineInstr *MI,
00841                                       const HexagonInstrInfo *QII) {
00842   /// We use the following rule: The first predicate register that is a use is
00843   /// the predicate register of a predicated instruction.
00844 
00845   assert(QII->isPredicated(MI) && "Must be predicated instruction");
00846 
00847   for (MachineInstr::mop_iterator OI = MI->operands_begin(),
00848        OE = MI->operands_end(); OI != OE; ++OI) {
00849     MachineOperand &Op = *OI;
00850     if (Op.isReg() && Op.getReg() && Op.isUse() &&
00851         Hexagon::PredRegsRegClass.contains(Op.getReg()))
00852       return Op.getReg();
00853   }
00854 
00855   llvm_unreachable("Unknown instruction operand layout");
00856 
00857   return 0;
00858 }
00859 
00860 // Given two predicated instructions, this function detects whether
00861 // the predicates are complements
00862 bool HexagonPacketizerList::ArePredicatesComplements (MachineInstr* MI1,
00863      MachineInstr* MI2, std::map <MachineInstr*, SUnit*> MIToSUnit) {
00864 
00865   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
00866 
00867   // If we don't know the predicate sense of the instructions bail out early, we
00868   // need it later.
00869   if (getPredicateSense(MI1, QII) == PK_Unknown ||
00870       getPredicateSense(MI2, QII) == PK_Unknown)
00871     return false;
00872 
00873   // Scheduling unit for candidate
00874   SUnit* SU = MIToSUnit[MI1];
00875 
00876   // One corner case deals with the following scenario:
00877   // Trying to add
00878   // a) %R24<def> = TFR_cPt %P0, %R25
00879   // to this packet:
00880   //
00881   // {
00882   //   b) %R25<def> = TFR_cNotPt %P0, %R24
00883   //   c) %P0<def> = CMPEQri %R26, 1
00884   // }
00885   //
00886   // On general check a) and b) are complements, but
00887   // presence of c) will convert a) to .new form, and
00888   // then it is not a complement
00889   // We attempt to detect it by analyzing  existing
00890   // dependencies in the packet
00891 
00892   // Analyze relationships between all existing members of the packet.
00893   // Look for Anti dependecy on the same predicate reg
00894   // as used in the candidate
00895   for (std::vector<MachineInstr*>::iterator VIN = CurrentPacketMIs.begin(),
00896        VEN = CurrentPacketMIs.end(); (VIN != VEN); ++VIN) {
00897 
00898     // Scheduling Unit for current insn in the packet
00899     SUnit* PacketSU = MIToSUnit[*VIN];
00900 
00901     // If this instruction in the packet is succeeded by the candidate...
00902     if (PacketSU->isSucc(SU)) {
00903       for (unsigned i = 0; i < PacketSU->Succs.size(); ++i) {
00904         // The corner case exist when there is true data
00905         // dependency between candidate and one of current
00906         // packet members, this dep is on predicate reg, and
00907         // there already exist anti dep on the same pred in
00908         // the packet.
00909         if (PacketSU->Succs[i].getSUnit() == SU &&
00910             PacketSU->Succs[i].getKind() == SDep::Data &&
00911             Hexagon::PredRegsRegClass.contains(
00912               PacketSU->Succs[i].getReg()) &&
00913             // Here I know that *VIN is predicate setting instruction
00914             // with true data dep to candidate on the register
00915             // we care about - c) in the above example.
00916             // Now I need to see if there is an anti dependency
00917             // from c) to any other instruction in the
00918             // same packet on the pred reg of interest
00919             RestrictingDepExistInPacket(*VIN,PacketSU->Succs[i].getReg(),
00920                                         MIToSUnit)) {
00921            return false;
00922         }
00923       }
00924     }
00925   }
00926 
00927   // If the above case does not apply, check regular
00928   // complement condition.
00929   // Check that the predicate register is the same and
00930   // that the predicate sense is different
00931   // We also need to differentiate .old vs. .new:
00932   // !p0 is not complimentary to p0.new
00933   unsigned PReg1 = getPredicatedRegister(MI1, QII);
00934   unsigned PReg2 = getPredicatedRegister(MI2, QII);
00935   return ((PReg1 == PReg2) &&
00936           Hexagon::PredRegsRegClass.contains(PReg1) &&
00937           Hexagon::PredRegsRegClass.contains(PReg2) &&
00938           (getPredicateSense(MI1, QII) != getPredicateSense(MI2, QII)) &&
00939           (QII->isDotNewInst(MI1) == QII->isDotNewInst(MI2)));
00940 }
00941 
00942 // initPacketizerState - Initialize packetizer flags
00943 void HexagonPacketizerList::initPacketizerState() {
00944 
00945   Dependence = false;
00946   PromotedToDotNew = false;
00947   GlueToNewValueJump = false;
00948   GlueAllocframeStore = false;
00949   FoundSequentialDependence = false;
00950 
00951   return;
00952 }
00953 
00954 // ignorePseudoInstruction - Ignore bundling of pseudo instructions.
00955 bool HexagonPacketizerList::ignorePseudoInstruction(MachineInstr *MI,
00956                                                     MachineBasicBlock *MBB) {
00957   if (MI->isDebugValue())
00958     return true;
00959 
00960   // We must print out inline assembly
00961   if (MI->isInlineAsm())
00962     return false;
00963 
00964   // We check if MI has any functional units mapped to it.
00965   // If it doesn't, we ignore the instruction.
00966   const MCInstrDesc& TID = MI->getDesc();
00967   unsigned SchedClass = TID.getSchedClass();
00968   const InstrStage* IS =
00969                     ResourceTracker->getInstrItins()->beginStage(SchedClass);
00970   unsigned FuncUnits = IS->getUnits();
00971   return !FuncUnits;
00972 }
00973 
00974 // isSoloInstruction: - Returns true for instructions that must be
00975 // scheduled in their own packet.
00976 bool HexagonPacketizerList::isSoloInstruction(MachineInstr *MI) {
00977 
00978   if (MI->isInlineAsm())
00979     return true;
00980 
00981   if (MI->isEHLabel())
00982     return true;
00983 
00984   // From Hexagon V4 Programmer's Reference Manual 3.4.4 Grouping constraints:
00985   // trap, pause, barrier, icinva, isync, and syncht are solo instructions.
00986   // They must not be grouped with other instructions in a packet.
00987   if (IsSchedBarrier(MI))
00988     return true;
00989 
00990   return false;
00991 }
00992 
00993 // isLegalToPacketizeTogether:
00994 // SUI is the current instruction that is out side of the current packet.
00995 // SUJ is the current instruction inside the current packet against which that
00996 // SUI will be packetized.
00997 bool HexagonPacketizerList::isLegalToPacketizeTogether(SUnit *SUI, SUnit *SUJ) {
00998   MachineInstr *I = SUI->getInstr();
00999   MachineInstr *J = SUJ->getInstr();
01000   assert(I && J && "Unable to packetize null instruction!");
01001 
01002   const MCInstrDesc &MCIDI = I->getDesc();
01003   const MCInstrDesc &MCIDJ = J->getDesc();
01004 
01005   MachineBasicBlock::iterator II = I;
01006 
01007   const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
01008   const HexagonRegisterInfo *QRI =
01009       (const HexagonRegisterInfo *)TM.getSubtargetImpl()->getRegisterInfo();
01010   const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
01011 
01012   // Inline asm cannot go in the packet.
01013   if (I->getOpcode() == Hexagon::INLINEASM)
01014     llvm_unreachable("Should not meet inline asm here!");
01015 
01016   if (isSoloInstruction(I))
01017     llvm_unreachable("Should not meet solo instr here!");
01018 
01019   // A save callee-save register function call can only be in a packet
01020   // with instructions that don't write to the callee-save registers.
01021   if ((QII->isSaveCalleeSavedRegsCall(I) &&
01022        DoesModifyCalleeSavedReg(J, QRI)) ||
01023       (QII->isSaveCalleeSavedRegsCall(J) &&
01024        DoesModifyCalleeSavedReg(I, QRI))) {
01025     Dependence = true;
01026     return false;
01027   }
01028 
01029   // Two control flow instructions cannot go in the same packet.
01030   if (IsControlFlow(I) && IsControlFlow(J)) {
01031     Dependence = true;
01032     return false;
01033   }
01034 
01035   // A LoopN instruction cannot appear in the same packet as a jump or call.
01036   if (IsLoopN(I) &&
01037      (IsDirectJump(J) || MCIDJ.isCall() || QII->isDeallocRet(J))) {
01038     Dependence = true;
01039     return false;
01040   }
01041   if (IsLoopN(J) &&
01042      (IsDirectJump(I) || MCIDI.isCall() || QII->isDeallocRet(I))) {
01043     Dependence = true;
01044     return false;
01045   }
01046 
01047   // dealloc_return cannot appear in the same packet as a conditional or
01048   // unconditional jump.
01049   if (QII->isDeallocRet(I) &&
01050      (MCIDJ.isBranch() || MCIDJ.isCall() || MCIDJ.isBarrier())) {
01051     Dependence = true;
01052     return false;
01053   }
01054 
01055 
01056   // V4 allows dual store. But does not allow second store, if the
01057   // first store is not in SLOT0. New value store, new value jump,
01058   // dealloc_return and memop always take SLOT0.
01059   // Arch spec 3.4.4.2
01060   if (QRI->Subtarget.hasV4TOps()) {
01061     if (MCIDI.mayStore() && MCIDJ.mayStore() &&
01062        (QII->isNewValueInst(J) || QII->isMemOp(J) || QII->isMemOp(I))) {
01063       Dependence = true;
01064       return false;
01065     }
01066 
01067     if ((QII->isMemOp(J) && MCIDI.mayStore())
01068         || (MCIDJ.mayStore() && QII->isMemOp(I))
01069         || (QII->isMemOp(J) && QII->isMemOp(I))) {
01070       Dependence = true;
01071       return false;
01072     }
01073 
01074     //if dealloc_return
01075     if (MCIDJ.mayStore() && QII->isDeallocRet(I)) {
01076       Dependence = true;
01077       return false;
01078     }
01079 
01080     // If an instruction feeds new value jump, glue it.
01081     MachineBasicBlock::iterator NextMII = I;
01082     ++NextMII;
01083     if (NextMII != I->getParent()->end() && QII->isNewValueJump(NextMII)) {
01084       MachineInstr *NextMI = NextMII;
01085 
01086       bool secondRegMatch = false;
01087       bool maintainNewValueJump = false;
01088 
01089       if (NextMI->getOperand(1).isReg() &&
01090           I->getOperand(0).getReg() == NextMI->getOperand(1).getReg()) {
01091         secondRegMatch = true;
01092         maintainNewValueJump = true;
01093       }
01094 
01095       if (!secondRegMatch &&
01096            I->getOperand(0).getReg() == NextMI->getOperand(0).getReg()) {
01097         maintainNewValueJump = true;
01098       }
01099 
01100       for (std::vector<MachineInstr*>::iterator
01101             VI = CurrentPacketMIs.begin(),
01102              VE = CurrentPacketMIs.end();
01103            (VI != VE && maintainNewValueJump); ++VI) {
01104         SUnit* PacketSU = MIToSUnit[*VI];
01105 
01106         // NVJ can not be part of the dual jump - Arch Spec: section 7.8
01107         if (PacketSU->getInstr()->getDesc().isCall()) {
01108           Dependence = true;
01109           break;
01110         }
01111         // Validate
01112         // 1. Packet does not have a store in it.
01113         // 2. If the first operand of the nvj is newified, and the second
01114         //    operand is also a reg, it (second reg) is not defined in
01115         //    the same packet.
01116         // 3. If the second operand of the nvj is newified, (which means
01117         //    first operand is also a reg), first reg is not defined in
01118         //    the same packet.
01119         if (PacketSU->getInstr()->getDesc().mayStore()               ||
01120             PacketSU->getInstr()->getOpcode() == Hexagon::ALLOCFRAME ||
01121             // Check #2.
01122             (!secondRegMatch && NextMI->getOperand(1).isReg() &&
01123              PacketSU->getInstr()->modifiesRegister(
01124                                NextMI->getOperand(1).getReg(), QRI)) ||
01125             // Check #3.
01126             (secondRegMatch &&
01127              PacketSU->getInstr()->modifiesRegister(
01128                                NextMI->getOperand(0).getReg(), QRI))) {
01129           Dependence = true;
01130           break;
01131         }
01132       }
01133       if (!Dependence)
01134         GlueToNewValueJump = true;
01135       else
01136         return false;
01137     }
01138   }
01139 
01140   if (SUJ->isSucc(SUI)) {
01141     for (unsigned i = 0;
01142          (i < SUJ->Succs.size()) && !FoundSequentialDependence;
01143          ++i) {
01144 
01145       if (SUJ->Succs[i].getSUnit() != SUI) {
01146         continue;
01147       }
01148 
01149       SDep::Kind DepType = SUJ->Succs[i].getKind();
01150 
01151       // For direct calls:
01152       // Ignore register dependences for call instructions for
01153       // packetization purposes except for those due to r31 and
01154       // predicate registers.
01155       //
01156       // For indirect calls:
01157       // Same as direct calls + check for true dependences to the register
01158       // used in the indirect call.
01159       //
01160       // We completely ignore Order dependences for call instructions
01161       //
01162       // For returns:
01163       // Ignore register dependences for return instructions like jumpr,
01164       // dealloc return unless we have dependencies on the explicit uses
01165       // of the registers used by jumpr (like r31) or dealloc return
01166       // (like r29 or r30).
01167       //
01168       // TODO: Currently, jumpr is handling only return of r31. So, the
01169       // following logic (specificaly IsCallDependent) is working fine.
01170       // We need to enable jumpr for register other than r31 and then,
01171       // we need to rework the last part, where it handles indirect call
01172       // of that (IsCallDependent) function. Bug 6216 is opened for this.
01173       //
01174       unsigned DepReg = 0;
01175       const TargetRegisterClass* RC = nullptr;
01176       if (DepType == SDep::Data) {
01177         DepReg = SUJ->Succs[i].getReg();
01178         RC = QRI->getMinimalPhysRegClass(DepReg);
01179       }
01180       if ((MCIDI.isCall() || MCIDI.isReturn()) &&
01181           (!IsRegDependence(DepType) ||
01182             !IsCallDependent(I, DepType, SUJ->Succs[i].getReg()))) {
01183         /* do nothing */
01184       }
01185 
01186       // For instructions that can be promoted to dot-new, try to promote.
01187       else if ((DepType == SDep::Data) &&
01188                CanPromoteToDotNew(I, SUJ, DepReg, MIToSUnit, II, RC) &&
01189                PromoteToDotNew(I, DepType, II, RC)) {
01190         PromotedToDotNew = true;
01191         /* do nothing */
01192       }
01193 
01194       else if ((DepType == SDep::Data) &&
01195                (QII->isNewValueJump(I))) {
01196         /* do nothing */
01197       }
01198 
01199       // For predicated instructions, if the predicates are complements
01200       // then there can be no dependence.
01201       else if (QII->isPredicated(I) &&
01202                QII->isPredicated(J) &&
01203           ArePredicatesComplements(I, J, MIToSUnit)) {
01204         /* do nothing */
01205 
01206       }
01207       else if (IsDirectJump(I) &&
01208                !MCIDJ.isBranch() &&
01209                !MCIDJ.isCall() &&
01210                (DepType == SDep::Order)) {
01211         // Ignore Order dependences between unconditional direct branches
01212         // and non-control-flow instructions
01213         /* do nothing */
01214       }
01215       else if (MCIDI.isConditionalBranch() && (DepType != SDep::Data) &&
01216                (DepType != SDep::Output)) {
01217         // Ignore all dependences for jumps except for true and output
01218         // dependences
01219         /* do nothing */
01220       }
01221 
01222       // Ignore output dependences due to superregs. We can
01223       // write to two different subregisters of R1:0 for instance
01224       // in the same cycle
01225       //
01226 
01227       //
01228       // Let the
01229       // If neither I nor J defines DepReg, then this is a
01230       // superfluous output dependence. The dependence must be of the
01231       // form:
01232       //  R0 = ...
01233       //  R1 = ...
01234       // and there is an output dependence between the two instructions
01235       // with
01236       // DepReg = D0
01237       // We want to ignore these dependences.
01238       // Ideally, the dependence constructor should annotate such
01239       // dependences. We can then avoid this relatively expensive check.
01240       //
01241       else if (DepType == SDep::Output) {
01242         // DepReg is the register that's responsible for the dependence.
01243         unsigned DepReg = SUJ->Succs[i].getReg();
01244 
01245         // Check if I and J really defines DepReg.
01246         if (I->definesRegister(DepReg) ||
01247             J->definesRegister(DepReg)) {
01248           FoundSequentialDependence = true;
01249           break;
01250         }
01251       }
01252 
01253       // We ignore Order dependences for
01254       // 1. Two loads unless they are volatile.
01255       // 2. Two stores in V4 unless they are volatile.
01256       else if ((DepType == SDep::Order) &&
01257                !I->hasOrderedMemoryRef() &&
01258                !J->hasOrderedMemoryRef()) {
01259         if (QRI->Subtarget.hasV4TOps() &&
01260             // hexagonv4 allows dual store.
01261             MCIDI.mayStore() && MCIDJ.mayStore()) {
01262           /* do nothing */
01263         }
01264         // store followed by store-- not OK on V2
01265         // store followed by load -- not OK on all (OK if addresses
01266         // are not aliased)
01267         // load followed by store -- OK on all
01268         // load followed by load  -- OK on all
01269         else if ( !MCIDJ.mayStore()) {
01270           /* do nothing */
01271         }
01272         else {
01273           FoundSequentialDependence = true;
01274           break;
01275         }
01276       }
01277 
01278       // For V4, special case ALLOCFRAME. Even though there is dependency
01279       // between ALLOCFRAME and subsequent store, allow it to be
01280       // packetized in a same packet. This implies that the store is using
01281       // caller's SP. Hence, offset needs to be updated accordingly.
01282       else if (DepType == SDep::Data
01283                && QRI->Subtarget.hasV4TOps()
01284                && J->getOpcode() == Hexagon::ALLOCFRAME
01285                && (I->getOpcode() == Hexagon::STrid
01286                    || I->getOpcode() == Hexagon::STriw
01287                    || I->getOpcode() == Hexagon::STrib)
01288                && I->getOperand(0).getReg() == QRI->getStackRegister()
01289                && QII->isValidOffset(I->getOpcode(),
01290                                      I->getOperand(1).getImm() -
01291                                      (FrameSize + HEXAGON_LRFP_SIZE)))
01292       {
01293         GlueAllocframeStore = true;
01294         // Since this store is to be glued with allocframe in the same
01295         // packet, it will use SP of the previous stack frame, i.e
01296         // caller's SP. Therefore, we need to recalculate offset according
01297         // to this change.
01298         I->getOperand(1).setImm(I->getOperand(1).getImm() -
01299                                         (FrameSize + HEXAGON_LRFP_SIZE));
01300       }
01301 
01302       //
01303       // Skip over anti-dependences. Two instructions that are
01304       // anti-dependent can share a packet
01305       //
01306       else if (DepType != SDep::Anti) {
01307         FoundSequentialDependence = true;
01308         break;
01309       }
01310     }
01311 
01312     if (FoundSequentialDependence) {
01313       Dependence = true;
01314       return false;
01315     }
01316   }
01317 
01318   return true;
01319 }
01320 
01321 // isLegalToPruneDependencies
01322 bool HexagonPacketizerList::isLegalToPruneDependencies(SUnit *SUI, SUnit *SUJ) {
01323   MachineInstr *I = SUI->getInstr();
01324   assert(I && SUJ->getInstr() && "Unable to packetize null instruction!");
01325 
01326   const unsigned FrameSize = MF.getFrameInfo()->getStackSize();
01327 
01328   if (Dependence) {
01329 
01330     // Check if the instruction was promoted to a dot-new. If so, demote it
01331     // back into a dot-old.
01332     if (PromotedToDotNew) {
01333       DemoteToDotOld(I);
01334     }
01335 
01336     // Check if the instruction (must be a store) was glued with an Allocframe
01337     // instruction. If so, restore its offset to its original value, i.e. use
01338     // curent SP instead of caller's SP.
01339     if (GlueAllocframeStore) {
01340       I->getOperand(1).setImm(I->getOperand(1).getImm() +
01341                                              FrameSize + HEXAGON_LRFP_SIZE);
01342     }
01343 
01344     return false;
01345   }
01346   return true;
01347 }
01348 
01349 MachineBasicBlock::iterator
01350 HexagonPacketizerList::addToPacket(MachineInstr *MI) {
01351 
01352     MachineBasicBlock::iterator MII = MI;
01353     MachineBasicBlock *MBB = MI->getParent();
01354 
01355     const HexagonInstrInfo *QII = (const HexagonInstrInfo *) TII;
01356 
01357     if (GlueToNewValueJump) {
01358 
01359       ++MII;
01360       MachineInstr *nvjMI = MII;
01361       assert(ResourceTracker->canReserveResources(MI));
01362       ResourceTracker->reserveResources(MI);
01363       if ((QII->isExtended(MI) || QII->isConstExtended(MI)) &&
01364           !tryAllocateResourcesForConstExt(MI)) {
01365         endPacket(MBB, MI);
01366         ResourceTracker->reserveResources(MI);
01367         assert(canReserveResourcesForConstExt(MI) &&
01368                "Ensure that there is a slot");
01369         reserveResourcesForConstExt(MI);
01370         // Reserve resources for new value jump constant extender.
01371         assert(canReserveResourcesForConstExt(MI) &&
01372                "Ensure that there is a slot");
01373         reserveResourcesForConstExt(nvjMI);
01374         assert(ResourceTracker->canReserveResources(nvjMI) &&
01375                "Ensure that there is a slot");
01376 
01377       } else if (   // Extended instruction takes two slots in the packet.
01378         // Try reserve and allocate 4-byte in the current packet first.
01379         (QII->isExtended(nvjMI)
01380             && (!tryAllocateResourcesForConstExt(nvjMI)
01381                 || !ResourceTracker->canReserveResources(nvjMI)))
01382         || // For non-extended instruction, no need to allocate extra 4 bytes.
01383         (!QII->isExtended(nvjMI) &&
01384               !ResourceTracker->canReserveResources(nvjMI)))
01385       {
01386         endPacket(MBB, MI);
01387         // A new and empty packet starts.
01388         // We are sure that the resources requirements can be satisfied.
01389         // Therefore, do not need to call "canReserveResources" anymore.
01390         ResourceTracker->reserveResources(MI);
01391         if (QII->isExtended(nvjMI))
01392           reserveResourcesForConstExt(nvjMI);
01393       }
01394       // Here, we are sure that "reserveResources" would succeed.
01395       ResourceTracker->reserveResources(nvjMI);
01396       CurrentPacketMIs.push_back(MI);
01397       CurrentPacketMIs.push_back(nvjMI);
01398     } else {
01399       if (   (QII->isExtended(MI) || QII->isConstExtended(MI))
01400           && (   !tryAllocateResourcesForConstExt(MI)
01401               || !ResourceTracker->canReserveResources(MI)))
01402       {
01403         endPacket(MBB, MI);
01404         // Check if the instruction was promoted to a dot-new. If so, demote it
01405         // back into a dot-old
01406         if (PromotedToDotNew) {
01407           DemoteToDotOld(MI);
01408         }
01409         reserveResourcesForConstExt(MI);
01410       }
01411       // In case that "MI" is not an extended insn,
01412       // the resource availability has already been checked.
01413       ResourceTracker->reserveResources(MI);
01414       CurrentPacketMIs.push_back(MI);
01415     }
01416     return MII;
01417 }
01418 
01419 //===----------------------------------------------------------------------===//
01420 //                         Public Constructor Functions
01421 //===----------------------------------------------------------------------===//
01422 
01423 FunctionPass *llvm::createHexagonPacketizer() {
01424   return new HexagonPacketizer();
01425 }
01426