205#include <unordered_map>
207#define DEBUG_TYPE "global_schedV2"
211STATISTIC(HexagonGSLocalPullups,
"Number of instructions pull-ups locally");
212STATISTIC(HexagonGSGlobalPullups,
"Number of instructions pull-ups globally");
216 cl::desc(
"Enable global scheduler optimizer"));
220 cl::desc(
"Use pullup predication"));
224 cl::desc(
"Speculate local MIs in parent BUNDLEs"));
228 cl::desc(
"Push down local MIs into the next packet"));
231 cl::desc(
"Optimize after global scheduling"));
235 cl::desc(
"Packetize unbundled MIs after global scheduling"));
239 cl::desc(
"Do not put predicated instructions in bundle"));
243 cl::desc(
"Do not put speculated instructions in bundle"));
247 cl::desc(
"Try putting 5 instructions in bundle"));
253 "Prevent separation of duplex while pullup in global scheduler V2"));
257 cl::desc(
"Pull up loads which have no memoperands"));
262 cl::desc(
"Run the machine verifier at several stages of global scheduler"));
266 cl::desc(
"Try predicating all possible instructions."));
270 cl::desc(
"Try speculating all possible instructions."));
274 cl::desc(
"Maximum number of bundles to check for local pull-up"));
278 cl::desc(
"Enable superblocks consisting of single block loops."));
282 cl::desc(
"Enable the HVX local pull-up pass (load bypass only)."));
286 cl::desc(
"Enable the HVX last chance global pull-up for load-bypass-only"));
293 cl::desc(
"Enable the two-load .cur load-bypass case in local pull-up."));
297 cl::desc(
"Enable the HVX local pull-up pass."));
301 cl::desc(
"Enable the HVX local pull-up pass on single block innermost "
306 cl::desc(
"Disable single block loops in superblocks and the HVX local "
311 cl::desc(
"Enable the HVX last chance global pull-up pass."));
315 cl::desc(
"Run HVX scheduling only on blocks whose ratio of HVX "
316 "instructions exceeds -hvx-sched-profit-ratio."));
320 cl::desc(
"Ratio of HVX instruction/Total instructions "
321 "for the profitability check."));
325 cl::desc(
"Pull up HLX .tmp load uses and .new HLX stores to remove the "
326 "copies inserted by HLX load/store expansion"));
330 cl::desc(
"Enable pullup to/from sparse packets"));
339 cl::desc(
"Enable pullup for dependent instruction groups"));
365 :
TBB(NULL), FBB(NULL), FirstTerm(NULL), SecondTerm(NULL),
366 AnalysisValid(
false) {}
367 bool analyzeBranch(MachineBasicBlock *
MBB,
const HexagonInstrInfo *QII,
371 if (FBB)
dbgs() <<
", FBB: " << FBB->getNumber();
372 if (FirstTerm)
dbgs() <<
", FirstTerm: " << *FirstTerm;
373 if (SecondTerm)
dbgs() <<
", SecondTerm: " << *SecondTerm);
376 struct AnalyzeBranchInfo_t {
377 typedef std::map<MachineBasicBlock *, ABI> BranchAnalyses_t;
378 BranchAnalyses_t BranchAnalyses;
382 ABI &getOrCreate(MachineBasicBlock *
MBB,
const HexagonInstrInfo *QII,
383 bool PostSched =
false) {
384 ABI &abi = BranchAnalyses[
MBB];
385 if (!abi.AnalysisValid)
386 abi.analyzeBranch(
MBB, QII, PostSched);
389 void invalidate(MachineBasicBlock *
MBB) {
390 iterator it = BranchAnalyses.find(
MBB);
391 assert((it != BranchAnalyses.end()) &&
392 "Cannot invalidate un-analyzed branch.");
393 BranchAnalyses.erase(it);
395 bool hasABInfo(MachineBasicBlock *
MBB)
const {
396 return BranchAnalyses.find(
MBB) != BranchAnalyses.end();
399 typedef BranchAnalyses_t::iterator iterator;
400 typedef BranchAnalyses_t::const_iterator const_iterator;
401 typedef BranchAnalyses_t::size_type size_type;
403 iterator
begin() {
return BranchAnalyses.begin(); }
404 const_iterator
begin()
const {
return BranchAnalyses.begin(); }
405 iterator
end() {
return BranchAnalyses.end(); }
406 const_iterator
end()
const {
return BranchAnalyses.end(); }
407 bool empty()
const {
return BranchAnalyses.empty(); }
408 void clear() { BranchAnalyses.clear(); }
409 size_type
size()
const {
return BranchAnalyses.size(); }
413 struct SchedulingRegion_t {
414 typedef SmallVector<MachineBasicBlock *, 2> SRMembers_t;
415 typedef MachineBasicBlock SRAtom_t;
416 BranchProbability EdgeProb;
418 SchedulingRegion_t() : EdgeProb(BranchProbability::getZero()) {}
420 void addSRMember(MachineBasicBlock *
MBB) { Members.push_back(
MBB); }
423 SmallVector<MachineBasicBlock *, 2>::iterator
MBBI = Members.
begin();
424 while (
MBBI != Members.
end()) {
435 iterator
begin() {
return Members.begin(); }
436 const_iterator
begin()
const {
return Members.begin(); }
437 reverse_iterator
rbegin() {
return Members.rbegin(); }
438 const_reverse_iterator
rbegin()
const {
return Members.rbegin(); }
439 iterator
end() {
return Members.end(); }
440 const_iterator
end()
const {
return Members.end(); }
441 reverse_iterator
rend() {
return Members.rend(); }
442 const_reverse_iterator
rend()
const {
return Members.rend(); }
443 bool empty()
const {
return Members.empty(); }
444 void clear() { Members.clear(); }
445 size_type
size()
const {
return Members.size(); }
447 return EdgeProb <
RHS.EdgeProb;
450 return EdgeProb <=
RHS.EdgeProb;
453 return EdgeProb >
RHS.EdgeProb;
456 return EdgeProb >=
RHS.EdgeProb;
460 using Dependency = HexagonInstrInfo::Dependency;
463 typedef SmallVector<SchedulingRegion_t, 2> SchedulingRegions_t;
464 typedef DenseMap<SchedulingRegion_t::SRAtom_t *,
469 GlobalScheduler(
const HexagonTargetMachine *tm,
const HexagonInstrInfo *qii,
472 const MachineBranchProbabilityInfo *mbpi,
473 HexagonLiveVariables *hlv)
474 : HexagonPacketizerList(mf, mli,
aa, mbpi,
false), TM(tm), QII(qii),
475 TRI(tri), HLV(hlv), MF(mf), MLI(mli), GlobalSpeculatedIns(0),
476 GlobalPredicatedIns(0), LocalPullups(0), SafetyBuffer(200),
477 SafetyBufferDelta(50) {
478 useHVXOps = mf.
getSubtarget<HexagonSubtarget>().useHVXOps();
479 useHLXOps = mf.
getSubtarget<HexagonSubtarget>().useHLXOps();
482 IsTinyCore = mf.
getSubtarget<HexagonSubtarget>().isTinyCore();
487 void initPacketizerStateLoc();
489 bool isLegalToPacketizeWith(MIIterType DestPack, MachineInstr *
MI);
491 inline bool isLegalToPacketizeTogetherMI(MachineInstr *BI, MachineInstr *
MI);
493 inline bool shouldNotBePulledUp(
const MachineInstr *
MI)
const;
503 inline bool shouldNotBeCrossed(
const MachineInstr *
MI,
504 const MachineInstr *ToMove,
505 bool IsMIInDestBundle =
false)
const;
508 void getResourcesReadyForBundle(MIInstIterType MII,
bool Squeeze =
false,
509 MachineInstr *InstrToSkip =
nullptr);
510 MachineInstr *generateCompoundIfPossible(MIInstIterType MII,
515 void generateCompoundsPostSched();
518 bool failsSpecialConditions(MachineInstr *BundleMI,
Register DestReg);
519 bool canNewifyPredReg(
unsigned Reg, MICInstIterType Producer,
521 bool getDefinitions(MachineInstr *
MI, MachineInstr *&Def1,
522 MachineInstr *&Def2, MachineInstr *&Def3);
528 bool canReserveResources(MIInstIterType MII)
const;
530 void replaceUsageOfReg(
unsigned Reg,
unsigned ScratchReg,
531 MachineBasicBlock *
MBB,
532 const MachineInstr *ExceptMI = NULL)
const;
533 inline void markLiveInsAsUsed(std::unique_ptr<RegScavenger> &RS,
534 MachineBasicBlock *
MBB)
const;
535 inline void markLiveOutsAsUsed(std::unique_ptr<RegScavenger> &RS,
536 MachineBasicBlock *
MBB)
const;
537 void markAllRegsAsUsed(std::unique_ptr<RegScavenger> &RS,
538 const MachineBasicBlock *
MBB)
const;
539 MachineBasicBlock *splitCriticalEdge(MachineBasicBlock *FromBB,
540 MachineBasicBlock *ToBB,
541 MachineBasicBlock *LP);
542 bool hasOneDef(
const MachineInstr *
MI,
unsigned &DefR)
const;
543 bool hasOneUse(
const MachineInstr *
MI,
unsigned &UseR)
const;
544 bool hasOneDefAndUse(
const MachineInstr *
MI,
unsigned &Def,
545 unsigned &Use)
const;
546 MICInstIterType laterOfTheTwo(MICInstIterType I1, MICInstIterType I2,
550 bool branchesInBetweenMBB(MIInstIterType MII, MachineBasicBlock *
MBB);
552 bool splitMBBAt(MIInstIterType MII, MachineBasicBlock *
MBB);
557 virtual void schedule() = 0;
560 void performExposedOptimizations();
561 void removeEmptyMBB(MachineBasicBlock *
MBB);
562 bool makeAsLayoutSuccessor(MachineBasicBlock *BB1, MachineBasicBlock *BB2);
563 bool removeRedundantBranches(MachineBasicBlock *
MBB, MachineBasicBlock *
TBB,
564 MachineInstr *FirstTerm, MachineBasicBlock *FBB,
565 MachineInstr *SecondTerm);
566 bool optimizeDualJumps(MachineBasicBlock *
MBB, MachineBasicBlock *
TBB,
567 MachineInstr *FirstTerm, MachineBasicBlock *FBB,
568 MachineInstr *SecondTerm);
569 MachineBasicBlock *optimizeBranches(MachineBasicBlock *
MBB,
570 MachineBasicBlock *
TBB,
571 MachineInstr *FirstTerm,
572 MachineBasicBlock *FBB);
588 void printSchedulingRegions() {
590 SchedulingRegions.begin();
591 SRI != SchedulingRegions.end(); ++SRI) {
592 dbgs() <<
"\nScheduling Region: ";
594 }
dbgs() <<
"\nTotal Scheduling Regions: "
595 << SchedulingRegions.size() <<
"\n";);
599 typedef bool (GlobalScheduler::*PruneCriteriaFP)(SchedulingRegion_t &)
const;
603 bool pruneCriteria(SchedulingRegion_t &SR)
const {
604 assert(!SR.Members.empty() &&
"Empty Scheduling region.");
605 if (SR.Members.size() == 1)
610 MachineLoop *HeadLI = MLI.getLoopFor(SR.Members[0]);
611 MachineLoop *LeafLI = MLI.getLoopFor(SR.Members[1]);
613 bool speculateInnerToOuter =
false;
616 if (HeadLI !=
nullptr && LeafLI !=
nullptr) {
618 speculateInnerToOuter =
true;
622 if (HeadLI != LeafLI && !speculateInnerToOuter)
628 virtual void pruneSchedulingRegions(PruneCriteriaFP PC);
631 virtual void sortSchedulingRegions();
633 bool isSuperblockLoop(MachineLoop *L);
634 virtual bool isProfitableToScheduleHVX(MachineLoop *L);
635 virtual bool isProfitableToScheduleHVX(MachineBasicBlock *
MBB);
639 const HexagonTargetMachine *TM;
640 const HexagonInstrInfo *QII;
641 const HexagonRegisterInfo *TRI;
642 HexagonLiveVariables *HLV;
644 MachineLoopInfo &MLI;
649 AnalyzeBranchInfo_t AnalyzeBranchInfo;
650 SchedulingRegions_t SchedulingRegions;
652 SmallVector<MachineInstr *, 4> SpecPacketOpp;
655 SmallSetVector<MachineInstr *, 4> ToBeSqueezed;
659 PulledUpII SpeculatedIns;
662 SetVector<MachineBasicBlock *> LocalMergePackCand;
664 std::unordered_map<MachineBasicBlock *, float> ProfitableHVXBlocks;
665 unsigned GlobalSpeculatedIns;
666 unsigned GlobalPredicatedIns;
667 unsigned LocalPullups;
668 const unsigned SafetyBuffer;
670 int SafetyBufferDelta;
672 bool useHVXOps =
false;
674 bool useHLXOps =
false;
677 bool IsTinyCore =
false;
680class GreedyPullup :
public GlobalScheduler {
682 GreedyPullup(
const HexagonTargetMachine *tm,
const HexagonInstrInfo *qii,
685 const MachineBranchProbabilityInfo *mbpi,
686 HexagonLiveVariables *hlv)
687 : GlobalScheduler(tm, qii, tri, mf, mli,
aa, mbpi, hlv) {}
689 void schedule()
override {
690 for (SchedulingRegions_t::iterator SRI = SchedulingRegions.begin();
691 SRI != SchedulingRegions.end(); ++SRI) {
692 unsigned currSpec = GlobalSpeculatedIns;
693 unsigned currPred = GlobalPredicatedIns;
699 dbgs() <<
"\n\nCurrent BB Global Pullup stats: Speculated Ins: "
700 << GlobalSpeculatedIns - currSpec
701 <<
", Predicated Ins: " << GlobalPredicatedIns - currPred;);
710 std::find(IgnorePostSchedOpt.begin(), IgnorePostSchedOpt.end(),
711 &*
MBBI) == IgnorePostSchedOpt.end()) {
712 localPullupHLX(&*
MBBI);
714 localPullupHVX(&*
MBBI);
719 dbgs() <<
"\n\nCurrent BB Local Pullup stats: Speculated Ins: "
724 HexagonGSLocalPullups += LocalPullups;
731 HexagonGSGlobalPullups = GlobalPredicatedIns + GlobalSpeculatedIns;
734 dbgs() <<
"\n\n**********Total Greedy Pullup stats**********"
735 <<
"\nGlobal speculated instructions: " << GlobalSpeculatedIns
736 <<
"\nGlobal predicated instructions: " << GlobalPredicatedIns
737 <<
"\nLocal speculated instruction: " << HexagonGSLocalPullups
742 bool canCauseInterlockStall(MIInstIterType DestBundle, MachineInstr *SrcMI,
743 const InstrItineraryData *
II);
745 void genLoadBypass(MachineInstr *LoadInstr, MachineInstr *InstrToMove);
750 bool canGenCurBypass(MIInstIterType DestBundle, MachineInstr *DepTrue,
751 MIInstIterType PktEnd,
int DstDep,
752 MachineInstr *Consumer,
753 const MachineInstr *Exempt =
nullptr);
758 bool tryTwoLoadCurBypass(MIIterType &BI, SchedulingRegion_t::SRAtom_t *SR,
759 MIInstIterType &DestBundle, MIIterType &SrcBundle,
760 MIIterType &NextBI,
bool isNextBISrc,
761 MIIterType &
Next, MachineInstr *SrcMI,
762 SmallPtrSetImpl<MachineInstr *> &PulledUpSet);
763 void localPullupHVX(SchedulingRegion_t::SRAtom_t *SR);
766 void localPullupImpl(SchedulingRegion_t::SRAtom_t *SR,
bool HVXMode);
769 void localPullupHLX(SchedulingRegion_t::SRAtom_t *SR);
770 void localPullupHLXLoadUses(SchedulingRegion_t::SRAtom_t *SR,
771 MachineInstr *HLXLoadMI, MachineInstr *HLXCopyMI);
772 void localPullupHLXStore(SchedulingRegion_t::SRAtom_t *SR,
773 MachineInstr *HLXStoreMI, MachineInstr *HLXCopyMI);
775 void localPullup(SchedulingRegion_t::SRAtom_t *SR);
777 bool mergePacketsAround(
const SmallVectorImpl<MachineInstr *> &Candidates);
780 void globalPullup(SchedulingRegion_t &SR);
783 bool canMIBeSpeculated(MachineInstr *
MI, MachineBasicBlock *SrcBB,
784 MachineBasicBlock *DstBB, MIIterType &TargetPacket);
786 bool safeToSpeculate(MachineInstr *
MI, MachineBasicBlock *DstBB)
const;
788 bool preciousMICanBeSpeculated(MachineInstr *
MI, MachineBasicBlock *SrcBB,
789 MachineBasicBlock *DstBB,
790 MachineBasicBlock *OtherBB,
791 MIIterType &TargetPacket,
792 unsigned ApproachNum);
793 inline bool preciousToSpeculate(
const MachineInstr *
MI,
794 const MachineBasicBlock *SrcBB,
795 const MachineBasicBlock *DstBB,
796 const MachineBasicBlock *OtherBB)
const;
797 inline bool preciousToPredicate(
const MachineInstr *
MI,
798 const MachineBasicBlock *SrcBB,
799 const MachineBasicBlock *DstBB,
800 const MachineBasicBlock *OtherBB)
const;
804 bool canMIBePredicated(MachineInstr *
MI, MachineBasicBlock *SrcBB,
805 MachineBasicBlock *DstBB, MIIterType &TargetPacket,
806 MICInstIterType &WithinPacket,
bool &NeedToNewify,
812 MIIterType findInsertPtSpec(MachineInstr *SrcMI, MachineBasicBlock *DstBB,
813 bool SpecPreciousMI,
bool SpecPreciousMIComp,
814 bool GlobalPrecious =
false);
818 MIIterType findInsertPtPred(MachineInstr *SrcMI, MachineBasicBlock *DstBB,
819 bool &NeedToNewify, MICInstIterType &WithinPacket,
820 bool PredPreciousMI =
false);
822 bool speculateMI(MachineInstr *
MI, MachineBasicBlock *SrcBB,
823 MachineBasicBlock *DstBB, MIIterType DstPos);
825 bool predicateMI(MachineInstr *
MI, MachineBasicBlock *SrcBB,
826 MachineBasicBlock *DstBB, MIIterType DstPack,
827 MICInstIterType WithinPacket,
bool NeedToNewify,
830 MIIterType findInsertionPtSpecLocal(MachineInstr *
MI);
845 int dstDependence(MICInstIterType DestBeg, MICInstIterType DestEnd,
846 MICInstIterType MII, MICInstIterType &DepTrue,
847 MICInstIterType &DepAnti,
unsigned HLXUseReg = 0,
848 bool IsPullDown =
false)
const;
851 int dstDependence(MICInstIterType DestBeg, MICInstIterType DestEnd,
853 std::vector<const MachineInstr *> &DepTrueVec,
854 std::vector<const MachineInstr *> &DepAntiVec,
855 std::vector<const MachineInstr *> &DepOutputVec,
856 std::vector<unsigned> *Regs =
nullptr)
const;
858 int srcDependence(MIInstIterType SrcBeg, MIInstIterType SrcEnd,
860 SmallSetVector<MachineInstr *, 4> *DepSet =
nullptr)
const;
861 bool singleTrueDep(MICInstIterType SrcBeg, MICInstIterType SrcEnd,
862 MICInstIterType MII,
unsigned &
Reg)
const;
863 bool lastChanceGlobalPullup(MIInstIterType &BundleHead,
864 MIInstIterType InstrToMove,
865 const MachineInstr *LoadBypassCand =
nullptr);
866 bool isLoopInvariant(MachineInstr &
MI);
867 MachineInstr *isLastChanceGlobalPullupCand(
const MachineInstr *
MI);
874 HexagonGlobalSchedulerV2() : MachineFunctionPass(ID), GS(nullptr) {
878 StringRef getPassName()
const override {
879 return "Hexagon Global Scheduler V2";
882 void getAnalysisUsage(AnalysisUsage &AU)
const override {
885 AU.
addRequired<MachineBranchProbabilityInfoWrapperPass>();
904 const HexagonTargetMachine *TM;
905 const HexagonInstrInfo *QII;
906 const HexagonRegisterInfo *TRI;
907 std::unique_ptr<GlobalScheduler> GS;
910char HexagonGlobalSchedulerV2::ID = 0;
915 "Hexagon Global Scheduler V2",
false,
false)
921 return MI->getOpcode() == Hexagon::Y2_barrier;
928 auto [It,
Inserted] = ProfitableHVXBlocks.try_emplace(
MBB);
932 unsigned NumHVXInst = 0, NumTotalInst = 0;
941 if (NumTotalInst == 0)
944 It->second = (float)NumHVXInst / (
float)NumTotalInst;
951bool GlobalScheduler::isProfitableToScheduleHVX(MachineLoop *L) {
952 assert(
L->getNumBlocks() == 1 &&
"Expecting a single block loop");
955 auto *BB =
L->getBlocks()[0];
956 return isProfitableToScheduleHVX(BB);
961bool GlobalScheduler::isSuperblockLoop(MachineLoop *L) {
965 if (!L ||
L->getNumBlocks() != 1 || !
L->isInnermost())
967 if (
L->getBlocks()[0]->succ_size() < 2)
969 if (
L->getBlocks()[0]->pred_size() != 2)
972 return isProfitableToScheduleHVX(L);
975bool GlobalScheduler::shouldNotBePulledUp(
const MachineInstr *
MI)
const {
977 if (
MI->getOpcode() == Hexagon::LX_S16b_new)
979 if (
MI->mayStore() ||
MI->isCall() ||
MI->isBarrier() ||
MI->isInlineAsm() ||
982 if (
MI->isBundle() ||
MI->isDebugInstr())
988 return MI->hasOrderedMemoryRef();
1004bool GlobalScheduler::shouldNotBeCrossed(
const MachineInstr *
MI,
1005 const MachineInstr *ToMove,
1006 bool IsMIInDestBundle)
const {
1007 if (
MI->isBundle() ||
MI->isDebugInstr())
1010 if (
MI->isReturn() ||
MI->isCall() ||
MI->hasUnmodeledSideEffects() ||
1015 if (!
MI->mayLoadOrStore())
1026 if (
MI->mayLoad()) {
1040static std::vector<const MachineOperand *>
1042 std::vector<const MachineOperand *> regmaskMOs;
1043 for (MICInstIterType
II =
MBB.instr_begin(),
End =
MBB.instr_end();
II !=
End;
1045 if (
II->isBundle() ||
II->isDebugInstr())
1047 for (
auto &MO :
II->operands())
1049 regmaskMOs.push_back(&MO);
1058bool GreedyPullup::isLoopInvariant(MachineInstr &
MI) {
1060 MachineLoop *
Loop = MLI.getLoopFor(
MBB);
1063 if (!isSuperblockLoop(
Loop))
1067 for (
const MachineOperand &MO :
MI.operands()) {
1070 unsigned Op = MO.getReg();
1075 }
else if (MO.isDef()) {
1077 if (HLV->isDefinedWithin(std::next(
MI.getIterator()),
MBB->
instr_end(),
1084 for (MCRegAliasIterator AI(
Op,
TRI,
true); AI.isValid(); ++AI)
1085 if (HLV->isLiveOut(
MBB, *AI))
1090 for (
auto *MO : regmaskMOs)
1091 if (MO->clobbersPhysReg(
Op))
1100static std::pair<MIInstIterType, MIInstIterType>
1104 return std::make_pair(BundleStart, BundleEnd);
1109static MachineInstr *
1121 while (MIter != BundleEnd) {
1123 MI->unbundleFromPred();
1125 SpecScope->push_back(
MI);
1136static MachineInstr *
1142 if (
MI->isBundled()) {
1144 MIP =
MI->removeFromBundle();
1145 NewSingleInstr =
updateBundle(Bundle, QII, SpecPacketOpp);
1147 MIP =
MI->removeFromParent();
1150 *singleInstr = NewSingleInstr;
1159 while (MII !=
MBB->instr_end()) {
1160 if ((MII->getOpcode() == Hexagon::ENDLOOP0) ||
1161 (MII->getOpcode() == Hexagon::ENDLOOP1))
1182 MICIterType TestIt = DstBB->
begin();
1184 while (TestIt != Iter) {
1185 if (Iter->isBarrier())
1190 if (!Iter->isBarrier())
1193 while (Iter != DstBB->
end()) {
1194 if (Iter->isBarrier())
1204 if (
MBB->isEHPad() ||
MBB->hasAddressTaken() ||
MBB->pred_size() != 1 ||
1209 if (!
MBBI->isDebugInstr())
1219 MIRIterType Rb = DstBB->
rbegin();
1220 MIRIterType Re = DstBB->
rend();
1227 while ((&*Rb != &*
B.first) && (Rb != Re))
1236bool GlobalScheduler::ABI::analyzeBranch(MachineBasicBlock *
MBB,
1237 const HexagonInstrInfo *QII,
1240 if (AnalysisValid) {
1245 unsigned jumpers_size = Jumpers.
size();
1246 assert((jumpers_size == 1) || (jumpers_size == 2));
1247 FirstTerm = Jumpers.
back();
1248 if (jumpers_size == 2)
1249 SecondTerm = Jumpers[0];
1256 AnalysisValid =
false;
1259 return AnalysisValid;
1265bool GlobalScheduler::branchesInBetweenMBB(MIInstIterType MII,
1266 MachineBasicBlock *
MBB) {
1267 if (!MII->isBranch())
1270 while (MII->isInsideBundle())
1279 if (MII->isReturn())
1283 if (MII->isBranch())
1288bool GlobalScheduler::splitMBBAt(MIInstIterType MII, MachineBasicBlock *
MBB) {
1289 MachineBasicBlock *OrigTBB = NULL, *OrigFBB = NULL;
1290 SmallVector<MachineOperand, 4> OrigCond;
1301 if (OrigTBB == Succ1)
1314 MachineBasicBlock *FirstTBB = NULL, *FirstFBB = NULL;
1315 SmallVector<MachineOperand, 4> FirstCond;
1337 MachineBasicBlock *
MBB = &*
MBBI;
1340 MachineInstr *
MI = &*MII;
1342 if (branchesInBetweenMBB(
MI->getIterator(),
MBB)) {
1347 if (!splitMBBAt(std::next(
MI->getIterator()),
MBB))
1350 }
else if (
MI->getOpcode() == Hexagon::J2_jump &&
1351 MI->getOperand(0).isMBB()) {
1353 MachineBasicBlock *
Target =
MI->getOperand(0).getMBB();
1356 if (
MI->isBundled()) {
1358 MI->eraseFromBundle();
1361 MI->eraseFromParent();
1372bool GlobalScheduler::makeAsLayoutSuccessor(MachineBasicBlock *BB1,
1373 MachineBasicBlock *BB2) {
1381 if (SafetyBufferDelta < InsSize)
1383 SafetyBufferDelta -= InsSize;
1386 if (Jumpers.
size() > 1 || Jumpers.
empty())
1391 <<
" as LayoutSuccessor of " << BB1->
getNumber(););
1394 if (BB1LS != MF.
end()) {
1401void GlobalScheduler::removeEmptyMBB(MachineBasicBlock *
MBB) {
1415 <<
") from parent.\n");
1421bool GlobalScheduler::removeRedundantBranches(MachineBasicBlock *
MBB,
1422 MachineBasicBlock *
TBB,
1423 MachineInstr *FirstTerm,
1424 MachineBasicBlock *FBB,
1425 MachineInstr *SecondTerm) {
1426 bool Analyzed =
false;
1428 MachineInstr *Head = NULL, *ToErase = NULL;
1429 if (!FBB && (FirstTerm->
getOpcode() == Hexagon::J2_jump) &&
1433 dbgs() <<
"\nRemoving Uncond. jump to the layout successor in BB#"
1435 ToErase = FirstTerm;
1436 }
else if (SecondTerm && (
TBB == FBB) &&
1437 (SecondTerm->
getOpcode() == Hexagon::J2_jump)) {
1446 if (++FirstTermIter == SecondTermIter &&
1448 LLVM_DEBUG(
dbgs() <<
"\nRemoving multiple branching to same target in BB#"
1452 ToErase = FirstTerm;
1454 }
else if (SecondTerm && (SecondTerm->
getOpcode() == Hexagon::J2_jump) &&
1460 ToErase = SecondTerm;
1466 ToErase = FirstTerm;
1470 if (ToErase->isBundled()) {
1472 ToErase->eraseFromBundle();
1475 ToErase->eraseFromParent();
1489bool GlobalScheduler::optimizeDualJumps(MachineBasicBlock *
MBB,
1490 MachineBasicBlock *
TBB,
1491 MachineInstr *FirstTerm,
1492 MachineBasicBlock *FBB,
1493 MachineInstr *SecondTerm) {
1496 bool Analyzed =
false;
1499 (SecondTerm->
getOpcode() == Hexagon::J2_jump)) {
1515 <<
" out of range.");
1529 MachineInstr *SecondHead, *FirstHead;
1540 else if (!FirstHead) {
1544 }
else if (FirstHead == SecondHead) {
1546 assert((FirstHead && SecondHead) &&
"Unbundled Instruction");
1571MachineBasicBlock *GlobalScheduler::optimizeBranches(MachineBasicBlock *
MBB,
1572 MachineBasicBlock *
TBB,
1573 MachineInstr *FirstTerm,
1574 MachineBasicBlock *FBB) {
1584 MachineInstr *TBBMIb = &*
TBB->getFirstNonDebugInstr();
1585 if (TBBMIb->
getOpcode() == Hexagon::J2_jump &&
1588 if (
TBB->hasAddressTaken() ||
TBB->isEHPad() ||
1589 TBB == NewTarget || (
TBB->pred_size() != 1))
1598 <<
" out of range.");
1603 TBB->removeSuccessor(
TBB->succ_begin());
1608 SmallVector<MachineOperand, 4> PredCond;
1610 if (PredCond.
empty())
1613 if ((Opcode != Hexagon::PS_jmpret) && (Opcode != Hexagon::L4_return))
1615 MachineInstr *NewMI = FirstTerm;
1629 const MachineInstr *MI2Clone = TBBMIb;
1639 NewMI->
setDesc(QII->get(NewOpcode));
1644 !
TBB->hasAddressTaken() &&
TBB->pred_empty() &&
TBB->succ_empty())
1658 if (FBBMIb->
getOpcode() == Hexagon::J2_jump &&
1662 (FBB == NewTarget ) || (FBB->
pred_size() != 1))
1674 <<
" out of range.");
1689void GlobalScheduler::performExposedOptimizations() {
1693 MachineBasicBlock *
MBB = &*
MBBI;
1700 if (AnalyzeBranchInfo.hasABInfo(*pi))
1701 AnalyzeBranchInfo.invalidate(*pi);
1703 if (AnalyzeBranchInfo.hasABInfo(
MBB))
1704 AnalyzeBranchInfo.invalidate(
MBB);
1705 removeEmptyMBB(
MBB);
1709 MachineBasicBlock *
MBB = &*
MBBI;
1713 if (std::find(IgnorePostSchedOpt.begin(), IgnorePostSchedOpt.end(),
MBB) !=
1714 IgnorePostSchedOpt.end())
1717 ABI &abi = AnalyzeBranchInfo.getOrCreate(
MBB, QII,
true);
1718 if (!abi.AnalysisValid)
1720 MachineBasicBlock *
TBB = abi.TBB, *FBB = abi.FBB;
1721 MachineInstr *FirstTerm = abi.FirstTerm, *SecondTerm = abi.SecondTerm;
1724 if (
TBB && FirstTerm &&
1725 removeRedundantBranches(
MBB,
TBB, FirstTerm, FBB, SecondTerm)) {
1728 }
else if (FirstTerm && SecondTerm &&
1729 optimizeDualJumps(
MBB,
TBB, FirstTerm, FBB, SecondTerm)) {
1731 }
else if (
TBB && FBB && FirstTerm && !SecondTerm) {
1732 MachineBasicBlock *BBToErase = optimizeBranches(
MBB,
TBB, FirstTerm, FBB);
1734 if (AnalyzeBranchInfo.hasABInfo(BBToErase))
1735 AnalyzeBranchInfo.invalidate(BBToErase);
1746 AnalyzeBranchInfo.invalidate(
MBB);
1767 if (((MBBPredSize > 1) && ((MBBSuccSize == 1) || (MBBSuccSize == 2))) ||
1768 (MBBSuccSize == 2)) {
1769 SchedulingRegion_t SR;
1770 SR.addSRMember(&*
MBBI);
1780 SchedulingRegions_t::iterator SRI = SchedulingRegions.
begin();
1781 SchedulingRegions_t::iterator SRIPrev = SchedulingRegions.
end();
1782 LLVM_DEBUG(
dbgs() <<
"\nConstructing Scheduling Region for HEADs at";);
1783 while (SRI != SchedulingRegions.
end()) {
1784 MachineBasicBlock *RegHead = SRI->Members[0];
1792 if ((SRIPrev != SchedulingRegions.
end()) &&
1793 (SRIPrev->Members[0] == SRI->Members[0]))
1799 bool SuccIsSingleBlockLoop =
1800 isSuperblockLoop(L) && RegMBB->getNumber() != RegHead->
getNumber();
1803 if ((RegMBB->pred_size() > 1 && !SuccIsSingleBlockLoop) ||
1804 (RegMBB->succ_empty() && RegMBB->empty()))
1806 SRI->addSRMember(&*RegMBB);
1808 if (RegMBB->succ_size() != 1) {
1811 RegMBB = (*RegMBB->succ_begin())->getIterator();
1818void GlobalScheduler::pruneSchedulingRegions(PruneCriteriaFP PC) {
1819 LLVM_DEBUG(
dbgs() <<
"\nErasing Scheduling Region with HEAD at";);
1820 for (SchedulingRegions_t::iterator SRI = SchedulingRegions.
begin();
1821 SRI != SchedulingRegions.
end();) {
1822 if ((this->*PC)(*SRI)) {
1824 SRI = SchedulingRegions.
erase(SRI);
1831void GlobalScheduler::sortSchedulingRegions() {
1832 for (SchedulingRegions_t::iterator SRI = SchedulingRegions.
begin();
1833 SRI != SchedulingRegions.
end(); ++SRI) {
1834 SchedulingRegion_t &SR = *SRI;
1837 SR.EdgeProb = MBPI->getEdgeProbability(SR.Members[0], SR.Members[1]);
1838 if (SR.EdgeProb.getCompl() == SR.EdgeProb) {
1839 ABI &abi = AnalyzeBranchInfo.getOrCreate(SR.Members[0], QII);
1840 if (!abi.AnalysisValid)
1843 if (abi.TBB == SR.Members[1])
1844 SR.EdgeProb = BranchProbability(2, 3);
1846 SR.EdgeProb = BranchProbability(1, 3);
1849 SR.EdgeProb.dump(););
1853 std::greater<SchedulingRegions_t::value_type>());
1857GlobalScheduler::addToPacketPseudo(MachineInstr *
MI) {
1861 if (!
TII->isZeroCost(
MI->getOpcode()))
1862 ResourceTracker->reserveResources(*
MI);
1864 assert(canReserveResourcesForConstExt());
1865 reserveResourcesForConstExt();
1867 CurrentPacketMIs.push_back(
MI);
1871void GlobalScheduler::initPacketizerStateLoc() {
1873 CurrentPacketMIs.clear();
1877bool GlobalScheduler::isLegalToPacketizeWith(MIIterType DestPack,
1880 assert(DestPack->isBundle());
1882 if (isSoloInstruction(*
MI))
1887 MIInstIterType BI = (&*DestPack)->getIterator();
1889 const MachineInstr *BranchMI = NULL;
1891 for (++BI; (BI !=
MBB->
instr_end()) && BI->isInsideBundle(); ++BI) {
1895 if (!isLegalToPacketizeTogetherMI(&*BI,
MI))
1906 const MachineOperand &MO = BranchMI->
getOperand(i);
1910 LLVM_DEBUG(
dbgs() <<
"\nBranch might be constant extended later";);
1920bool GlobalScheduler::isLegalToPacketizeTogetherMI(MachineInstr *BI,
1922 if (cannotCoexist(*BI, *
MI))
1925 if (hasDeadDependence(*BI, *
MI))
1927 if (hasControlDependence(*BI, *
MI))
1929 if (hasDualStoreDependence(*BI, *
MI))
1968 (
MI->mayStore() ||
MI->getOpcode() == Hexagon::S2_allocframe ||
1986 if ((QII->
isMemOp(*BI) &&
MI->mayStore()) ||
1988 LLVM_DEBUG(
dbgs() <<
"\nSlot 0 not available for store because of memop.");
1999MachineInstr *GlobalScheduler::generateCompoundIfPossible(MIInstIterType MII,
2001 MIInstIterType &GB) {
2003 MachineInstr *NewMI = NULL;
2008 while ((MII !=
MBB->
instr_end()) && MII->isInsideBundle()) {
2009 if (MII->isCompare() &&
2017 if (MII->isBranch() &&
2030 if (CmpOpcode != -1) {
2032 assert(GA->getNumOperands() == 3);
2034 NewMI =
BuildMI(*
MBB, GB, dl, QII->get(CmpOpcode));
2035 Register PredReg = GA->getOperand(0).getReg();
2036 MachineOperand *PredUse =
2039 "Missing compound predicate use");
2047 SafetyBufferDelta)) {
2058void GlobalScheduler::generateCompoundsPostSched() {
2059 auto it = ToBeSqueezed.begin();
2060 while (it != ToBeSqueezed.end()) {
2063 MachineInstr *NewMI =
2064 generateCompoundIfPossible((*it)->getIterator(), GA, GB);
2066 assert(NewMI &&
"Wrong Analysis of compound packets.");
2067 GA->eraseFromBundle();
2068 GB->eraseFromBundle();
2091bool GlobalScheduler::failsSpecialConditions(MachineInstr *BundleMI,
2094 if (!BundleMI || !BundleMI->
getParent())
2099 MachineInstr &
MI = *
E;
2100 auto OpC =
MI.getOpcode();
2102 if (QII->
isEndLoopN(OpC) && DestReg == Hexagon::P3)
2105 if ((OpC == Hexagon::C2_tfrrp) &&
MI.getNumOperands() > 0 &&
2106 (DestReg ==
MI.getOperand(0).getReg()))
2109 if ((OpC == Hexagon::S2_cabacdecbin) ||
2112 MI.getNumOperands() > 0 &&
MI.getOperand(0).getReg() == DestReg) ||
2113 ((OpC == Hexagon::S2_storew_locked ||
2114 OpC == Hexagon::S4_stored_locked || OpC == Hexagon::A6_vminub_RdP ||
2115 OpC == Hexagon::A5_ACS || OpC == Hexagon::F2_sfinvsqrta ||
2116 OpC == Hexagon::F2_sfrecipa) &&
2117 MI.getNumOperands() > 1 &&
MI.getOperand(1).getReg() == DestReg) ||
2118 ((OpC == Hexagon::A4_addp_c || OpC == Hexagon::A4_subp_c) &&
2119 MI.getNumOperands() > 2 &&
MI.getOperand(3).getReg() == DestReg))
2129bool GlobalScheduler::canNewifyPredReg(
unsigned Reg, MICInstIterType Producer,
2133 if (
TRI->getMinimalPhysRegClass(
Reg) != &Hexagon::PredRegsRegClass)
2142bool GlobalScheduler::getDefinitions(MachineInstr *
MI, MachineInstr *&Def1,
2143 MachineInstr *&Def2, MachineInstr *&Def3) {
2146 if (
MI->getNumOperands() < 3)
2151 (
MI->getNumOperands() == 4) ?
MI->getOperand(3).getReg() :
Register(0);
2155 for (
auto DefSearch = std::prev(
MI->getIterator());
2157 if (DefSearch->isBundle())
2161 if (DefIter == MIUseDefs.
end())
2164 const BitVector &Defs = DefIter->second.second;
2169 if (Reg3 && Defs[Reg3])
2172 if (Def1 && Def2 && (!Reg3 || (Reg3 && Def3)))
2183 SmallVector<MachineInstr *, 4> ToBeErased;
2184 for (
auto &
MBB : Fn) {
2186 unsigned OpC = MII->getOpcode();
2188 if (OpC != Hexagon::C2_and && OpC != Hexagon::C2_andn &&
2189 OpC != Hexagon::C4_and_and && OpC != Hexagon::C4_and_andn)
2192 Register DestPred = MII->getOperand(0).getReg();
2193 Register Reg1 = MII->getOperand(1).getReg();
2194 Register Reg2 = MII->getOperand(2).getReg();
2195 auto Reg3 = MII->getNumOperands() == 4 ? MII->getOperand(3).getReg()
2201 if (Reg3 != 0 && Reg3 == Reg2)
2206 if (!MII->getOperand(1).isKill() || !MII->getOperand(2).isKill() ||
2207 (Reg3 != 0 && !MII->getOperand(3).isKill()))
2214 bool UsedByDotNew =
false;
2217 for (
auto BundledII = AndBundleStart; BundledII != AndBundleEnd;
2219 if (&*BundledII == &*MII)
2221 int UseIdx = BundledII->findRegisterUseOperandIdx(DestPred,
TRI);
2223 UsedByDotNew =
true;
2231 MachineInstr *Def1 =
nullptr;
2232 MachineInstr *Def2 =
nullptr;
2233 MachineInstr *Def3 =
nullptr;
2234 if (!getDefinitions(&*MII, Def1, Def2, Def3))
2251 MICInstIterType
Use;
2252 SmallPtrSet<MachineInstr *, 2>
Exception;
2256 if (HLV->
isUsedWithin(MIStart, MIEnd, Reg1, Use, &Exception) ||
2257 HLV->
isUsedWithin(MIStart, MIEnd, Reg2, Use, &Exception) ||
2258 (Def3 && HLV->
isUsedWithin(MIStart, MIEnd, Reg3, Use, &Exception)))
2262 if (failsSpecialConditions(&*MIStart, DestPred))
2268 std::optional<unsigned> NewOpC;
2272 case Hexagon::C2_and:
2281 case Hexagon::C2_andn:
2286 Def2->
setDesc(QII->get(NewOpC.value()));
2292 case Hexagon::C4_and_and:
2300 case Hexagon::C4_and_andn:
2305 Def3->
setDesc(QII->get(NewOpC.value()));
2320 for (MachineInstr *It : ToBeErased) {
2321 if (It->isBundled()) {
2323 It->eraseFromBundle();
2326 It->eraseFromParent();
2330void GlobalScheduler::packetize() {
2331 DenseSet<unsigned> DoneMBBs;
2332 HexagonPacketizerList
Packetizer(MF, MLI, AA, MBPI,
false);
2336 for (; PacketOpp != SpecPacketOpp.
end(); ++PacketOpp) {
2342 for (; RegionBeg !=
MBB->
end(); RegionBeg = RegionEnd) {
2345 while (RegionBeg !=
MBB->
end() && RegionBeg->isBundled())
2347 if (RegionBeg ==
MBB->
end())
2349 RegionEnd = std::next(RegionBeg);
2350 while (RegionEnd !=
MBB->
end() && !RegionEnd->isBundled())
2353 if (RegionBeg == std::prev(RegionEnd))
2363void GlobalScheduler::getResourcesReadyForBundle(MIInstIterType MII,
2365 MachineInstr *InstrToSkip) {
2368 bool GeneratedCompoundInstr =
false;
2369 MachineInstr *NewMI = NULL;
2373 MachineInstr *GAPos, *GBPos;
2378 NewMI = generateCompoundIfPossible(MII, GA, GB);
2379 if (NewMI != NULL) {
2380 GeneratedCompoundInstr =
true;
2381 MIInstIterType GANext = std::next(GA);
2382 MIInstIterType GBNext = std::next(GB);
2383 GAPos = GANext !=
MBB->
instr_end() ? &*GANext :
nullptr;
2384 GBPos = GBNext !=
MBB->
instr_end() ? &*GBNext :
nullptr;
2385 GA = GA->removeFromBundle()->getIterator();
2386 GB = GB->removeFromBundle()->getIterator();
2390 initPacketizerStateLoc();
2391 ResourceTracker->clearResources();
2396 MIInstIterType SavedMII = MII;
2397 auto &
ST = MII->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2398 if (
ST.isTinyCoreWithDuplex())
2401 while ((MII !=
MBB->
instr_end()) && MII->isInsideBundle()) {
2402 assert(ResourceTracker->canReserveResources(*MII) ||
2403 TII->isZeroCost(MII->getOpcode()));
2404 if (!(InstrToSkip && InstrToSkip == &*MII)) {
2405 addToPacketPseudo(&*MII);
2411 if (
ST.isTinyCoreWithDuplex())
2414 if (GeneratedCompoundInstr) {
2418 if (!GB->isBundled())
2419 GB->bundleWithPred();
2423 NewMI->eraseFromBundle();
2429bool GlobalScheduler::canReserveResources(MIInstIterType MII)
const {
2432 return ResourceTracker->canReserveResources(*MII);
2437void GlobalScheduler::replaceUsageOfReg(
unsigned Reg,
unsigned ScratchReg,
2438 MachineBasicBlock *
MBB,
2439 const MachineInstr *ExceptMI)
const {
2442 MII != MIE; ++MII) {
2443 MachineInstr *
MI = &*MII;
2446 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
2447 MachineOperand &MO =
MI->getOperand(i);
2454void GlobalScheduler::markLiveInsAsUsed(std::unique_ptr<RegScavenger> &RS,
2455 MachineBasicBlock *
MBB)
const {
2458 RS->setRegUsed((*lit).PhysReg);
2463void GlobalScheduler::markLiveOutsAsUsed(std::unique_ptr<RegScavenger> &RS,
2464 MachineBasicBlock *
MBB)
const {
2466 int Reg =
LO.find_first();
2468 RS->setRegUsed(
Reg);
2474void GlobalScheduler::markAllRegsAsUsed(std::unique_ptr<RegScavenger> &RS,
2475 const MachineBasicBlock *
MBB)
const {
2480 BitVector MBBUseDef(
TRI->getNumRegs());
2482 if (
I->isBundle() ||
I->isDebugInstr())
2486 MBBUseDef |= MIUseDef->second.first;
2487 MBBUseDef |= MIUseDef->second.second;
2489 int Reg = MBBUseDef.find_first();
2491 RS->setRegUsed(
Reg);
2493 Reg = MBBUseDef.find_next(
Reg);
2507 if ((*pi)->isLayoutSuccessor(ToBB))
2512MachineBasicBlock *GlobalScheduler::splitCriticalEdge(MachineBasicBlock *FromBB,
2513 MachineBasicBlock *ToBB,
2514 MachineBasicBlock *LP) {
2530 if ((LP != FromBB) &&
2531 (Jumpers.
empty() || !Jumpers[0]->isUnconditionalBranch())) {
2533 MachineInstr *JumpMI =
2543 MachineInstr *JumpMI =
2548 if (AnalyzeBranchInfo.hasABInfo(LP))
2549 AnalyzeBranchInfo.invalidate(LP);
2550 if (AnalyzeBranchInfo.hasABInfo(FromBB))
2551 AnalyzeBranchInfo.invalidate(FromBB);
2556bool GlobalScheduler::hasOneDef(
const MachineInstr *
MI,
unsigned &DefR)
const {
2560 if (MIUseDef == MIUseDefs.
end())
2562 const BitVector &Defs = MIUseDef->second.second;
2563 unsigned DefsCount = Defs.
count();
2571 return !MCSubRegIterator(DefR,
TRI,
false).isValid();
2575bool GlobalScheduler::hasOneUse(
const MachineInstr *
MI,
unsigned &UseR)
const {
2579 if (MIUseDef == MIUseDefs.
end())
2581 const BitVector &
Uses = MIUseDef->second.first;
2582 unsigned UsesCount =
Uses.count();
2586 UseR =
Uses.find_first();
2588 return !MCSubRegIterator(UseR,
TRI,
false).isValid();
2593bool GlobalScheduler::hasOneDefAndUse(
const MachineInstr *
MI,
unsigned &Def,
2594 unsigned &Use)
const {
2595 return hasOneDef(
MI, Def) && hasOneUse(
MI, Use);
2601MICInstIterType GlobalScheduler::laterOfTheTwo(MICInstIterType I1,
2606 MICInstIterType Temp =
I1;
2617int GreedyPullup::dstDependence(MICInstIterType DestBeg,
2618 MICInstIterType DestEnd, MICInstIterType MII,
2619 std::vector<const MachineInstr *> &DepTrueVec,
2620 std::vector<const MachineInstr *> &DepAntiVec,
2621 std::vector<const MachineInstr *> &DepOutputVec,
2622 std::vector<unsigned> *Regs)
const {
2624 if (DestBeg == DestEnd)
2627 unsigned dep = Dependency::Independent;
2629 SmallVector<unsigned> UseRegs;
2630 SmallVector<unsigned> DefRegs;
2631 for (
const MachineOperand &MO : MII->operands()) {
2635 else if (MO.
isDef())
2640 bool addedTrueDep =
false, addedFalseDep =
false;
2645 if (!
I->isBundle()) {
2646 if (shouldNotBeCrossed(&*
I, &*MII)) {
2647 dep = Dependency::Unknown;
2650 if (
I->isDebugInstr())
2652 for (
auto RegI = UseRegs.
begin(), RegE = UseRegs.
end(); RegI != RegE;
2654 for (MCRegAliasIterator AI(*RegI,
TRI,
true); AI.isValid(); ++AI) {
2655 if (MIUseDefs[&*
I].second[*AI]) {
2656 DepTrueVec.push_back(&*
I);
2658 Regs->push_back(*RegI);
2659 addedTrueDep =
true;
2660 dep |= Dependency::RAW;
2666 UseRegs.
erase(RegI);
2667 addedTrueDep =
false;
2672 for (
auto RegI = DefRegs.
begin(), RegE = DefRegs.
end(); RegI != RegE;
2674 for (MCRegAliasIterator AI(*RegI,
TRI,
true); AI.isValid(); ++AI) {
2675 if (MIUseDefs[&*
I].second[*AI]) {
2676 addedFalseDep =
true;
2677 dep |= Dependency::WAW;
2678 DepOutputVec.push_back(&*
I);
2683 for (MCRegAliasIterator AI(*RegI,
TRI,
true); AI.isValid(); ++AI) {
2684 if (MIUseDefs[&*
I].first[*AI]) {
2685 DepAntiVec.push_back(&*
I);
2686 dep |= Dependency::WAR;
2691 if (addedFalseDep) {
2692 DefRegs.
erase(RegI);
2693 addedFalseDep =
false;
2702 }
while (
I != DestBeg);
2719int GreedyPullup::dstDependence(MICInstIterType DestBeg,
2720 MICInstIterType DestEnd, MICInstIterType MII,
2721 MICInstIterType &DepTrue,
2722 MICInstIterType &DepAnti,
unsigned HLXUseReg,
2723 bool IsPullDown)
const {
2725 const MachineInstr *
MI = &*MII;
2726 const MachineBasicBlock *SrcMBB = MII->getParent();
2728 MICInstIterType DepTrueP = SrcMBB->
instr_end(),
2734 assert(!shouldNotBePulledUp(&*MII));
2735 MICInstIterType DestBundle =
2737 MICInstIterType MIBegin = DestBeg;
2738 bool IsMIInDestBundle =
false;
2739 for (; MIBegin != DestEnd; ++MIBegin) {
2742 IsMIInDestBundle =
false;
2743 if (!IsPullDown && DestBundle->isBundle() && MIBegin->isBundled())
2745 if (!shouldNotBeCrossed(&*MIBegin, &*MII, IsMIInDestBundle))
2748 return Dependency::Unknown;
2752 if (MII->isBranch() || MII->isTerminator() || MII->isReturn()) {
2757 if (SrcBundle->getBundleSize() != 1 || SrcMBB != DestBeg->getParent() ||
2758 (laterOfTheTwo(MII, DestBeg, SrcMBB->
instr_end()) == DestBeg) ||
2761 return Dependency::Unknown;
2764 unsigned dep = Dependency::Independent;
2766 auto CheckDep = [&](
const MachineOperand &MO,
unsigned Reg) ->
void {
2771 bool destIsBundle = (DestBeg->isBundle() || DestBeg->isBundledWithPred());
2776 bool depInDestBundle =
2782 bool regularDef = !tmpDef;
2783 if (depInDestBundle &&
2786 regularDef |= !tmpDef;
2789 bool hasHLXDep = depInDestBundle && (HLXUseReg ==
Reg);
2797 dep |= Dependency::RAW;
2799 if (hasHLXDep && !regularDef)
2802 dep |= Dependency::WAW;
2804 assert(DepTrueP != DestEnd);
2811 MICInstIterType LastII = DestEnd;
2813 bool depInDestBundle =
2815 const MachineInstr *DepMI = &*DepAntiP;
2816 int UseIdx = DepMI->findRegisterUseOperandIdx(
Reg,
TRI);
2819 bool DefViaRegMask = !MO.
isReg();
2823 dep |= Dependency::WAR;
2824 assert(DepAntiP != DestEnd);
2830 dep |= Dependency::WAR_SameBundle;
2837 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
2838 const MachineOperand &MO =
MI->getOperand(i);
2841 if (MCSubRegIterator(MO.
getReg(),
TRI,
false).isValid()) {
2845 CheckDep(MO, MO.
getReg());
2847 for (
unsigned R = 1, NR =
TRI->getNumRegs(); R != NR; ++R)
2859int GreedyPullup::srcDependence(
2860 MIInstIterType SrcBeg, MIInstIterType SrcEnd, MIInstIterType RMII,
2861 SmallSetVector<MachineInstr *, 4> *DepSet)
const {
2867 PreventDuplex,
false);
2870bool GreedyPullup::singleTrueDep(MICInstIterType SrcBeg, MICInstIterType SrcEnd,
2871 MICInstIterType MII,
unsigned &
Reg)
const {
2872 const MachineInstr *
MI = &*MII;
2874 MICInstIterType DepTrueP = SrcEnd;
2875 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
2876 const MachineOperand &MO =
MI->getOperand(i);
2890MIIterType GreedyPullup::findInsertionPtSpecLocal(MachineInstr *
MI) {
2900 if (
MI->isBundled()) {
2903 unsigned SrcDep = srcDependence(ParentBundle.first, ParentBundle.second,
2906 if (!Dependency::hasOnly(SrcDep, Dependency::Independent))
2914 MIIterType InsPos = findInsertPtSpec(
MI,
MBB,
false,
false);
2922void GreedyPullup::localPullupHVX(SchedulingRegion_t::SRAtom_t *SR) {
2931 if (!isSuperblockLoop(L))
2943 localPullupImpl(SR,
true);
2949void GreedyPullup::genLoadBypass(MachineInstr *LoadInstr,
2950 MachineInstr *InstrToMove) {
2954 LoadInstr->
setDesc(QII->get(CurOpcode));
2955 MIBundleBuilder Bundle(DestBundle);
2956 Bundle.insert(std::next(LoadInstr->
getIterator()), InstrToMove);
2958 for (
auto &MO : InstrToMove->
operands()) {
2963 for (MCRegAliasIterator AI(MO.
getReg(),
TRI,
true); AI.isValid(); ++AI) {
2977bool GreedyPullup::canGenCurBypass(MIInstIterType DestBundle,
2978 MachineInstr *DepTrue, MIInstIterType PktEnd,
2979 int DstDep, MachineInstr *Consumer,
2980 const MachineInstr *Exempt) {
2981 assert(Consumer &&
"Consumer is required for .cur bypass correctness checks");
2982 if (!DepTrue || Dependency::has(DstDep, Dependency::WAW))
2991 if (DestBundle->isBundle() && !DepTrue->
isBundled())
2999 for (
const MachineOperand &MOP :
MI.operands()) {
3000 if (MOP.isReg() && MOP.isUse() && !MOP.isDebug() && MOP.getReg() &&
3001 !MOP.isUndef() &&
TRI->regsOverlap(MOP.getReg(), DestReg))
3009 for (
const MachineOperand &Def : DepTrue->
operands()) {
3010 if (!
Def.isReg() || !
Def.isDef() || !
Def.getReg() ||
3011 Def.getReg() == DestReg)
3013 for (
const MachineOperand &Use : Consumer->
operands()) {
3014 if (
Use.isReg() &&
Use.isUse() && !
Use.isUndef() &&
Use.getReg() &&
3015 TRI->regsOverlap(
Use.getReg(),
Def.getReg()))
3024 for (MIInstIterType
MI = std::next(DestBundle);
3028 if (&*
MI == DepTrue || &*
MI == Exempt ||
MI->isBundle())
3030 for (
const MachineOperand &Def :
MI->operands()) {
3031 if (!
Def.isReg() || !
Def.isDef() || !
Def.getReg())
3033 bool ConsumerReads =
3035 return Use.isReg() && Use.isUse() && !Use.isUndef() &&
3036 Use.getReg() && TRI->regsOverlap(Use.getReg(), Def.getReg());
3046bool GreedyPullup::canCauseInterlockStall(MIInstIterType DestBundle,
3047 MachineInstr *SrcMI,
3048 const InstrItineraryData *
II) {
3050 auto PrevEnd = std::prev(DestBundle);
3052 MachineInstr *PrevStart =
nullptr;
3053 if (PrevEnd !=
MBB->
end()) {
3054 if (PrevEnd->isBundled())
3057 PrevStart = &*PrevEnd;
3060 if (PrevEnd !=
MBB->
end() && PrevStart !=
nullptr) {
3061 std::vector<const MachineInstr *> DepTruePrevVec, DepAntiPrevVec,
3063 std::vector<unsigned> TrueRegs;
3064 int dep = dstDependence(PrevStart->
getIterator(), DestBundle,
3066 DepAntiPrevVec, DepOutputPrevVec, &TrueRegs);
3067 if (Dependency::has(dep, Dependency::RAW)) {
3068 unsigned RegIdx = 0;
3069 for (
auto DepTruePrev : DepTruePrevVec) {
3070 int DefIdx = -1, UseIdx = -1;
3071 for (MCRegAliasIterator AI(TrueRegs[RegIdx],
TRI,
true);
3072 AI.isValid() && DefIdx == -1; ++AI)
3073 DefIdx = DepTruePrev->findRegisterDefOperandIdx(*AI,
nullptr);
3074 for (MCRegAliasIterator AI(TrueRegs[RegIdx],
TRI,
true);
3075 AI.isValid() && UseIdx == -1; ++AI)
3077 std::optional<unsigned> OpLatency =
3084 *SrcMI,
const_cast<MachineInstr &
>(*DepTruePrev),
false,
3099void GreedyPullup::localPullupHLXLoadUses(SchedulingRegion_t::SRAtom_t *SR,
3100 MachineInstr *HLXLoadMI,
3101 MachineInstr *HLXCopyMI) {
3106 MIInstIterType DestBundle = BI.getInstrIterator();
3108 unsigned NumLoadUsesPullups = 0;
3110 SmallVector<MachineInstr *, 4> LoadUses;
3112 if (!HLXCopyMI || !HLXLoadDestReg ||
3116 dbgs() <<
"\nFinding uses of HLX load for pullup into: "
3119 MIInstIterType LoadBundleEnd =
getBundleEnd(DestBundle);
3121 if (LoadBundleEnd ==
MBB->
end())
3127 while (HLV->
isDefinedWithin(LoadBundleEnd, NextLoadRegDef, HLXLoadDestReg,
3129 NextLoadRegDef = DepTrueP;
3130 MICInstIterType PrevLoadRegUse = NextLoadRegDef;
3135 while (HLV->
isUsedWithin(LoadBundleEnd, PrevLoadRegUse, HLXLoadDestReg,
3138 LoadUses.
push_back(&*DepTrueP.getNonConst());
3139 PrevLoadRegUse = DepTrueP;
3143 bool LoadRegIsLiveOut = HLV->
isLiveOut(
MBB, HLXLoadDestReg) &&
3150 bool BundleHasCallUse =
3155 for (MachineInstr *SrcMI :
reverse(LoadUses)) {
3163 std::pair<MIInstIterType, MIInstIterType> ParentBundle =
3167 if (!isLegalToPacketizeWith(BI, SrcMI) || isSoloInstruction(*SrcMI) ||
3168 shouldNotBePulledUp(SrcMI))
3171 int DstDep = dstDependence(DestBundle, ParentBundle.first, SrcMII, DepTrue,
3172 DepAnti, HLXLoadDestReg);
3173 int SrcDep = srcDependence(ParentBundle.first, ParentBundle.second, SrcMII);
3174 MIInstIterType DestPos = DestBundle;
3175 getResourcesReadyForBundle(DestPos);
3177 if ((!Dependency::hasOnly(DstDep, Dependency::Independent) ||
3178 !canReserveResources(SrcMII)) &&
3179 !LoadRegIsLiveOut && (NumLoadUsesPullups == (LoadUses.
size() - 1)) &&
3180 SrcMI == LoadUses.
front() && !BundleHasCallUse) {
3181 LLVM_DEBUG(
dbgs() <<
"\nAttempting to pullup last use by erasing copy: ";
3182 HLXCopyMI->
dump(););
3184 DstDep = dstDependence(DestBundle, ParentBundle.first, SrcMII, DepTrue,
3185 DepAnti, HLXLoadDestReg);
3186 getResourcesReadyForBundle(DestPos);
3189 if (!(Dependency::hasOnly(DstDep, Dependency::Independent)) ||
3190 !(Dependency::hasOnly(SrcDep, Dependency::Independent)))
3193 if (!canReserveResources(SrcMII)) {
3198 MachineInstr *InstrToMove =
removeMI(SrcMI, QII,
nullptr);
3200 MachineOperand *
Op =
nullptr;
3201 for (MCRegAliasIterator AI(HLXLoadDestReg,
TRI,
true); AI.isValid(); ++AI) {
3202 Op = InstrToMove->findRegisterUseOperand(*AI,
nullptr);
3206 assert(
Op &&
"Could not find use operand!");
3207 Op->setIsInternalRead();
3212 MIBundleBuilder Bundle(&*DestBundle);
3213 Bundle.insert(std::next(HLXLoadMI->
getIterator()), InstrToMove);
3218 ++NumLoadUsesPullups;
3220 <<
"\nMBB: " << *InstrToMove->getParent());
3226 if (NumLoadUsesPullups != LoadUses.
size() || LoadRegIsLiveOut ||
3229 if (!HLXCopyMI->isBundled()) {
3231 MIBundleBuilder Bundle(&*DestBundle);
3232 Bundle.insert(std::next(HLXLoadMI->
getIterator()), HLXCopyMI);
3237 if (HLXCopyMI->isBundled())
3238 HLXCopyMI->eraseFromBundle();
3240 MF.deleteMachineInstr(HLXCopyMI);
3247void GreedyPullup::localPullupHLXStore(SchedulingRegion_t::SRAtom_t *SR,
3248 MachineInstr *HLXStoreMI,
3249 MachineInstr *HLXCopyMI) {
3254 MIInstIterType SrcBundle = BI.getInstrIterator();
3258 MachineInstr *DestMI =
nullptr;
3260 if (!HLXCopyMI || !HLXStoreSrcReg ||
3264 dbgs() <<
"\nFinding def for HLX store to be pulled up from: "
3271 HLXStoreSrcReg, DepTrueP))
3272 DestMI = &*DepTrueP.getNonConst();
3280 std::pair<MICInstIterType, MICInstIterType> ParentBundle =
3284 LLVM_DEBUG(
dbgs() <<
"\nChecking Resources for: " << *HLXStoreMI;);
3286 if (!isLegalToPacketizeWith(&*DestPos, HLXStoreMI))
3290 dstDependence(ParentBundle.first, SrcBundle, HLXStoreMI->
getIterator(),
3291 DepTrue, DepAnti, HLXStoreSrcReg);
3297 if (!Dependency::hasOnly(DstDep, Dependency::Independent))
3300 getResourcesReadyForBundle(DestPos);
3301 if (!canReserveResources(HLXStoreMI->
getIterator())) {
3309 MachineInstr *InstrToMove =
removeMI(HLXStoreMI, QII,
nullptr);
3313 MIBundleBuilder Bundle(&*(DestPos));
3314 Bundle.insert(std::next(DestMII), InstrToMove);
3325 <<
"\nMBB: " << *InstrToMove->getParent());
3334void GreedyPullup::localPullupHLX(SchedulingRegion_t::SRAtom_t *SR) {
3339 LLVM_DEBUG(
dbgs() <<
"\n\n\t\t[LocalPullupHLX]\tBB#" << SR->getNumber(););
3342 for (MIIterType BI = SR->begin(); BI != SR->end(); ++BI) {
3344 if (BI->isDebugInstr())
3348 if (!BI->isBundle() || (!BI->mayLoad() && !BI->mayStore()))
3350 MachineInstr *HLXLoadMI =
nullptr;
3351 MachineInstr *HLXStoreMI =
nullptr;
3352 SmallVector<MachineInstr *, 2> HLXCopies;
3353 MIInstIterType MII = BI.getInstrIterator();
3355 (MII != SR->instr_end()) && MII->isInsideBundle(); ++MII) {
3358 switch (MII->getOpcode()) {
3359 case Hexagon::LX_L16b_tmp:
3362 case Hexagon::LX_S16b_new:
3365 case Hexagon::LX_assign:
3366 if (MII->getOperand(0).getReg() == MII->getOperand(1).getReg()) {
3377 MachineInstr *HLXLoadCopyMI =
nullptr;
3378 MachineInstr *HLXStoreCopyMI =
nullptr;
3379 for (MachineInstr *CopyMI : HLXCopies) {
3380 Register HLXCopyReg = CopyMI->getOperand(0).getReg();
3383 bool IsInternal = CopyMI->getOperand(1).isInternalRead();
3386 if (HLXStoreMI && !HLXStoreCopyMI && !IsInternal &&
3388 HLXStoreCopyMI = CopyMI;
3389 else if (HLXLoadMI && !HLXLoadCopyMI &&
3391 HLXLoadCopyMI = CopyMI;
3394 HLXLoadPseudos.
push_back(std::make_pair(HLXLoadMI, HLXLoadCopyMI));
3396 HLXStorePseudos.
push_back(std::make_pair(HLXStoreMI, HLXStoreCopyMI));
3399 for (
auto &LoadPseudo : HLXLoadPseudos)
3400 localPullupHLXLoadUses(SR, LoadPseudo.first, LoadPseudo.second);
3402 for (
auto &StorePseudo : HLXStorePseudos)
3403 localPullupHLXStore(SR, StorePseudo.first, StorePseudo.second);
3419bool GreedyPullup::tryTwoLoadCurBypass(
3420 MIIterType &BI, SchedulingRegion_t::SRAtom_t *SR,
3421 MIInstIterType &DestBundle, MIIterType &SrcBundle, MIIterType &NextBI,
3422 bool isNextBISrc, MIIterType &
Next, MachineInstr *SrcMI,
3423 SmallPtrSetImpl<MachineInstr *> &PulledUpSet) {
3425 std::pair<MIInstIterType, MIInstIterType> ParentBundle =
3430 std::vector<const MachineInstr *> DepTrueVec, DepAntiVec, DepOutputVec;
3431 int VecDstDep = dstDependence(DestBundle, ParentBundle.first, SrcMII,
3432 DepTrueVec, DepAntiVec, DepOutputVec);
3434 LLVM_DEBUG(
dbgs() <<
"\n[TwoLoadCur] Candidate consumer: " << *SrcMI
3435 <<
"\t\tRAW producers=" << DepTrueVec.size()
3436 <<
" output deps=" << DepOutputVec.size() <<
"\n");
3438 if (!Dependency::has(VecDstDep, Dependency::RAW) || DepTrueVec.size() > 2) {
3439 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: no RAW dep or >2 producers.\n");
3443 bool GenCur =
false;
3444 bool PotentialGenCur =
false;
3445 const MachineInstr *DepTrue =
nullptr;
3446 const MachineInstr *
Other =
nullptr;
3448 auto IsCurLoad = [&](
const MachineInstr *
MI) {
3452 if (DepTrueVec.size() == 1) {
3453 const MachineInstr *
V0 = DepTrueVec[0];
3454 if (!Dependency::has(VecDstDep, Dependency::WAW) && IsCurLoad(V0) &&
3459 }
else if (DepTrueVec.size() == 2 && DepOutputVec.size() <= 1) {
3460 const MachineInstr *
V0 = DepTrueVec[0];
3461 const MachineInstr *
V1 = DepTrueVec[1];
3464 if (IsCurLoad(V0)) {
3467 }
else if (IsCurLoad(
V1)) {
3471 if (
Other && (!Dependency::has(VecDstDep, Dependency::WAW) ||
3472 Other == DepOutputVec[0]))
3473 PotentialGenCur =
true;
3477 if (!GenCur && !PotentialGenCur)
3481 << (DepTrueVec.size() == 1 ?
"single-load" :
"two-load")
3482 <<
" case: GenCur=" << GenCur
3483 <<
" PotentialGenCur=" << PotentialGenCur <<
"\n");
3486 if (GenCur && DestBundle->isBundle()) {
3497 for (MIInstIterType
MI = std::next(DestBundle);
3498 MI != NextBI.getInstrIterator(); ++
MI) {
3501 bool Reads =
llvm::any_of(
MI->operands(), [&](
const MachineOperand &MO) {
3502 return MO.isReg() && MO.isUse() && !MO.isUndef() && MO.getReg() &&
3503 TRI->regsOverlap(MO.getReg(), DestReg);
3507 PotentialGenCur =
false;
3513 if (!GenCur && !PotentialGenCur)
3519 !canGenCurBypass(DestBundle,
const_cast<MachineInstr *
>(DepTrue),
3520 NextBI.getInstrIterator(), VecDstDep, SrcMI,
Other)) {
3521 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: unsafe intermediate RAW "
3522 "producer (canGenCurBypass).\n");
3532 int ScalarDstDep = dstDependence(DestBundle, ParentBundle.first, SrcMII,
3533 ScalarDepTrue, ScalarDepAnti);
3534 int DstDepMasked = ScalarDstDep & ~Dependency::WAR_SameBundle;
3535 if (GenCur || PotentialGenCur)
3536 DstDepMasked &= ~Dependency::RAW;
3537 if (!Dependency::hasOnly(DstDepMasked, Dependency::Independent)) {
3538 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: scalar dest dependence not "
3539 "independent after masking.\n");
3543 int SrcDep = srcDependence(ParentBundle.first, ParentBundle.second, SrcMII);
3544 if (!Dependency::hasOnly(SrcDep, Dependency::Independent)) {
3545 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: source dependence not "
3551 const InstrItineraryData *
II =
ST.getInstrItineraryData();
3552 if (canCauseInterlockStall(DestBundle, SrcMI,
II)) {
3553 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: would cause an interlock "
3559 bool UndoDestBundle =
false;
3560 if (!DestBundle->isBundle()) {
3561 finalizeBundle(*DestBundle->getParent(), DestBundle, std::next(DestBundle));
3562 DestBundle = std::prev(DestBundle);
3563 UndoDestBundle =
true;
3566 MIInstIterType DestPos = DestBundle;
3567 getResourcesReadyForBundle(DestPos);
3571 bool CanReserve = canReserveResources(SrcMII) && !PotentialGenCur;
3572 bool CanBeMoved =
false;
3574 const MachineInstr *LoadBypassCand = PotentialGenCur ?
Other :
nullptr;
3576 "lastChanceGlobalPullup (LoadBypassCand="
3577 << (LoadBypassCand ?
"Other" :
"null") <<
").\n");
3579 lastChanceGlobalPullup(DestPos, SrcMII, LoadBypassCand))) {
3580 DestBundle = DestPos;
3586 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Packet has room; packetizing "
3588 CanBeMoved = isLegalToPacketizeWith(DestBundle, SrcMI);
3593 bool FixNextBI =
false;
3594 if (SrcMII->isBundled()) {
3596 if (&*SrcBundle == &*NextBI)
3598 }
else if (&*SrcMII == &*NextBI) {
3599 NextBI = DestBundle;
3602 MachineInstr *SingleInstr =
nullptr;
3603 MachineInstr *
Instr =
removeMI(&*SrcMII, QII,
nullptr, &SingleInstr);
3604 if (SingleInstr && FixNextBI)
3608 genLoadBypass(
const_cast<MachineInstr *
>(DepTrue), Instr);
3610 MBB->
insert(std::next(DestBundle), Instr);
3613 <<
"): " << *Instr);
3615 PulledUpSet.
insert(Instr);
3620 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: candidate could not be moved.\n");
3621 if (UndoDestBundle) {
3622 MIInstIterType
I = std::next(DestPos);
3623 I->unbundleFromPred();
3624 DestBundle->eraseFromParent();
3632void GreedyPullup::localPullup(SchedulingRegion_t::SRAtom_t *SR) {
3633 localPullupImpl(SR,
false);
3639void GreedyPullup::localPullupImpl(SchedulingRegion_t::SRAtom_t *SR,
3642 LLVM_DEBUG(
dbgs() <<
"\n\n\t\t[LocalPullup]\tBB#" << SR->getNumber(););
3655 MIIterType NextBI = SR->begin();
3656 for (MIIterType BI = NextBI; BI != std::prev(SR->end()) && BI != SR->end();
3658 NextBI = std::next(BI);
3659 if (BI->isDebugInstr())
3661 MIInstIterType DestBundle = BI.getInstrIterator();
3662 if (!DestBundle->isBundle() && AllowSparsePullup) {
3663 if (isSoloInstruction(*DestBundle))
3666 std::next(DestBundle));
3667 DestBundle = std::prev(DestBundle);
3670 if (!DestBundle->isBundle())
3673 MIIterType SrcBundle = BI;
3677 unsigned NumBundles = 0;
3678 MIIterType
Next = std::next(SrcBundle);
3679 for (SrcBundle =
Next; SrcBundle != SR->end(); SrcBundle =
Next) {
3680 Next = std::next(SrcBundle);
3682 if (SrcBundle->isDebugInstr())
3692 bool isNextBISrc =
false;
3693 if (SrcBundle == std::next(BI))
3707 if (!SrcBundle->isBundle() && isNextBISrc) {
3708 MIInstIterType MII = SrcBundle.getInstrIterator();
3709 MachineInstr &
MI = *MII;
3710 if (
MI.getOpcode() == Hexagon::J2_call) {
3711 if (isLegalToPacketizeWith(BI, &
MI)) {
3712 MIInstIterType DestPos = DestBundle;
3713 getResourcesReadyForBundle(DestPos);
3715 while (DestPos->isInsideBundle())
3718 if (canReserveResources(MII)) {
3719 NextBI = std::next(SrcBundle);
3720 Next = std::next(SrcBundle);
3721 MachineInstr *InstrToMove =
removeMI(&
MI, QII,
nullptr);
3722 MIBundleBuilder Bundle(&*DestBundle);
3724 Bundle.insert(std::next(DestPos), InstrToMove);
3730 <<
"\nMBB: " << *InstrToMove->
getParent());
3740 if (SrcBundle->getBundleSize() <= 2 && !AllowSparsePullup)
3743 MIInstIterType SrcMII = SrcBundle.getInstrIterator();
3744 if (!SrcMII->isBundled() && AllowSparsePullup) {
3745 if (isSoloInstruction(*SrcMII) || shouldNotBePulledUp(&*SrcMII))
3747 finalizeBundle(*(SrcMII->getParent()), SrcMII, std::next(SrcMII));
3748 MIInstIterType BundleHead = std::prev(SrcMII);
3749 SrcBundle = BundleHead;
3751 NextBI = BundleHead;
3752 SrcMII = BundleHead;
3754 MIInstIterType SrcBundleEnd =
getBundleEnd(SrcBundle.getInstrIterator());
3757 SmallPtrSet<MachineInstr *, 4> SrcVisitedSet;
3760 (SrcMII != SR->instr_end()) && SrcMII->isInsideBundle(); ++SrcMII) {
3761 MachineInstr *SrcMI = &*SrcMII;
3762 if (isSoloInstruction(*SrcMI) || SrcMI->
isDebugInstr() ||
3763 !SrcVisitedSet.
insert(SrcMI).second)
3771 SmallSetVector<MachineInstr *, 4> DepSet;
3773 srcDependence(SrcBundle.getInstrIterator(), SrcBundleEnd, SrcMII,
3774 AllowClusterPullup ? &DepSet :
nullptr);
3775 SmallSetVector<MachineInstr *, 4> SrcClusterSet;
3776 if (Dependency::hasOnly(SrcDep, Dependency::Independent)) {
3777 SrcClusterSet.
insert(SrcMI);
3783 if (AllowClusterPullup) {
3785 if (Dependency::hasOnly(SrcDep, Dependency::SamePacketRAW) ||
3786 Dependency::hasOnly(SrcDep, Dependency::SamePacketWAR) ||
3787 Dependency::hasOnly(SrcDep, Dependency::SamePacket)) {
3788 LLVM_DEBUG(
dbgs() <<
"\nFound cluster of dependent instructions in "
3790 for (
auto *DepMI : DepSet) {
3792 SrcClusterSet.
insert(DepMI);
3800 if (AllowClusterPullup) {
3802 [](
const auto &ClusterA,
const auto &ClusterB) {
3803 return ClusterA.size() > ClusterB.size();
3824 SmallPtrSet<MachineInstr *, 4> PulledUpSet;
3825 SmallPtrSet<MachineInstr *, 4> InsertAtEndSet;
3826 for (
auto ClusterSet : SrcClusterMIs) {
3827 bool RejectCluster =
false;
3829 MachineInstr *GenCurLoad =
nullptr;
3830 MachineInstr *GenCurConsumer =
nullptr;
3831 LLVM_DEBUG(
dbgs() <<
"\nChecking pullup legality for cluster: \n";
3832 for (
auto *SrcMI : ClusterSet) SrcMI->
dump(););
3836 MachineInstr *SrcMI = *ClusterSet.begin();
3837 if (tryTwoLoadCurBypass(BI, SR, DestBundle, SrcBundle, NextBI,
3838 isNextBISrc,
Next, SrcMI, PulledUpSet)) {
3844 for (
auto *SrcMI : ClusterSet) {
3848 RejectCluster =
true;
3856 for (
auto *SrcMI : ClusterSet) {
3866 if ((!QII->
isTC1(*SrcMI) && !(HVXMode && QII->
isHVXVec(*SrcMI)) &&
3872 RejectCluster =
true;
3876 if (!isLegalToPacketizeWith(BI, SrcMI) || isSoloInstruction(*SrcMI) ||
3877 shouldNotBePulledUp(SrcMI)) {
3879 RejectCluster =
true;
3882 MICInstIterType DepTrue = SR->instr_end(), DepAnti = SR->instr_end();
3883 int DstDep = dstDependence(DestBundle, SrcBundle.getInstrIterator(),
3891 bool ClusterGenCur =
3893 Dependency::has(DstDep, Dependency::RAW) &&
3894 DepTrue != SR->instr_end() &&
3895 canGenCurBypass(DestBundle,
const_cast<MachineInstr *
>(&*DepTrue),
3896 NextBI.getInstrIterator(), DstDep, SrcMI);
3900 int DstDepMasked = DstDep & ~Dependency::WAR_SameBundle;
3902 DstDepMasked &= ~Dependency::RAW;
3903 if (!(Dependency::hasOnly(DstDepMasked, Dependency::Independent))) {
3905 RejectCluster =
true;
3908 if (DstDep & Dependency::WAR_SameBundle)
3909 InsertAtEndSet.
insert(SrcMI);
3911 if (ClusterGenCur) {
3912 GenCurLoad =
const_cast<MachineInstr *
>(&*DepTrue);
3913 GenCurConsumer = SrcMI;
3921 const InstrItineraryData *
II =
ST.getInstrItineraryData();
3922 if (canCauseInterlockStall(DestBundle, SrcMI,
II)) {
3924 RejectCluster =
true;
3939 MIInstIterType DestPos = DestBundle;
3940 getResourcesReadyForBundle(DestPos);
3941 for (
auto *SrcMI : ClusterSet) {
3944 if (canReserveResources(SrcMII))
3945 addToPacketPseudo(SrcMI);
3947 RejectCluster =
true;
3957 for (
auto *SrcMI : ClusterSet) {
3960 if (!SrcBundle->isBundled() || SrcBundle->getBundleSize() == 1) {
3962 NextBI = std::next(SrcBundle);
3963 Next = std::next(SrcBundle);
3966 SmallVector<MachineInstr *, 4> UnbundledMIs;
3967 MachineInstr *InstrToMove =
removeMI(SrcMI, QII, &UnbundledMIs);
3968 MIBundleBuilder Bundle(&*DestBundle);
3973 if (GenCurLoad && SrcMI == GenCurConsumer) {
3974 genLoadBypass(GenCurLoad, InstrToMove);
3988 MIInstIterType InsertPos = std::next(DestPos);
3992 Bundle.insert(InsertPos, InstrToMove);
3998 PulledUpSet.
insert(InstrToMove);
3999 LLVM_DEBUG(
dbgs() <<
"\nSpeculated Local MI:\t" << *InstrToMove);
4004 if (UnbundledMIs.
empty())
4006 MIInstIterType UnbundledII = UnbundledMIs.
front()->getIterator();
4007 if (AllowSparsePullup) {
4008 MIInstIterType LastUnbundledII = UnbundledMIs.
back()->getIterator();
4010 std::next(LastUnbundledII));
4014 NextBI = UnbundledII;
4015 SrcBundle = UnbundledII;
4024 if (AllowSparsePullup && DestBundle->isBundle())
4029 MIIterType LastBundle = std::prev(SR->end());
4030 if (AllowSparsePullup && LastBundle->isBundle())
4034bool GreedyPullup::mergePacketsAround(
4035 const SmallVectorImpl<MachineInstr *> &Candidates) {
4037 for (
auto CMII : Candidates) {
4038 MachineInstr *
MI = CMII;
4039 MIIterType DestBundle =
MI;
4045 while (DestBundle !=
MBB->
end() && DestBundle->isDebugInstr())
4048 if (DestBundle ==
MBB->
end() || !DestBundle->isBundle() ||
4050 shouldNotBePulledUp(
MI))
4054 MIInstIterType DestEnd =
getBundleEnd(DestBundle.getInstrIterator());
4055 int DstDep = dstDependence(DestBundle.getInstrIterator(), DestEnd,
4056 MI->getIterator(), DepTrue, DepAnti,
4059 int DstDepMasked = DstDep & ~Dependency::WAR_SameBundle;
4060 if (!Dependency::hasOnly(DstDepMasked, Dependency::Independent))
4065 getResourcesReadyForBundle(DestBundle.getInstrIterator());
4066 if (canReserveResources(
MI->getIterator()) &&
4067 isLegalToPacketizeWith(DestBundle,
MI)) {
4069 MachineInstr *MIToInsert =
MI->removeFromParent();
4070 MIInstIterType InsertPos = DestBundle.getInstrIterator();
4083bool GreedyPullup::mergePackets() {
4085 for (
auto MBB : LocalMergePackCand) {
4089 SmallVector<MachineInstr *, 2> Candidates;
4091 if (!MII->isBundle())
4094 Changed |= mergePacketsAround(Candidates);
4100void GreedyPullup::globalPullup(SchedulingRegion_t &SR) {
4103 SchedulingRegion_t::reverse_iterator BlockI = SR.rbegin();
4104 assert(!SR.empty() &&
"Empty scheduling region!");
4105 DenseSet<const MachineBasicBlock *> IgnoreBlocks;
4106 for (++BlockI; BlockI != SR.rend(); ++BlockI) {
4107 if (IgnoreBlocks.
find(*BlockI) != IgnoreBlocks.
end())
4109 SchedulingRegion_t::reverse_iterator SrcBlock = BlockI - 1;
4111 << (*SrcBlock)->getNumber() <<
" to MBB#"
4112 << (*BlockI)->getNumber() <<
"*****");
4115 PulledUpII::iterator hasSpeculatedIns = SpeculatedIns.find(*BlockI);
4116 if (hasSpeculatedIns != SpeculatedIns.end()) {
4118 dbgs() <<
"\nSrcMBB Already contains speculated ins. Skipping!";);
4121 for (MIInstIterType MII = (*SrcBlock)->instr_begin();
4122 MII != (*SrcBlock)->instr_end();) {
4123 MachineInstr *MIToMove = &*MII;
4129 MIIterType TargetPacket = (*BlockI)->end();
4130 if (canMIBeSpeculated(MIToMove, *SrcBlock, *BlockI, TargetPacket)) {
4135 bool done = speculateMI(MIToMove, *SrcBlock, *BlockI, TargetPacket);
4136 SpeculatedIns[*BlockI].push_back(MIToMove);
4137 ++GlobalSpeculatedIns;
4139 LocalMergePackCand.insert(*SrcBlock);
4141 assert(done &&
"Wrong Analysis");
4146 BlockI = SR.rbegin();
4147 for (++BlockI; BlockI != SR.rend(); ++BlockI) {
4148 if (IgnoreBlocks.
find(*BlockI) != IgnoreBlocks.
end())
4150 SchedulingRegion_t::reverse_iterator SrcBlock = BlockI - 1;
4152 << (*SrcBlock)->getNumber() <<
" to MBB#"
4153 << (*BlockI)->getNumber() <<
"*****");
4155 for (MIInstIterType MII = (*SrcBlock)->instr_begin();
4156 MII != (*SrcBlock)->instr_end();) {
4157 MachineInstr *MIToMove = &*MII;
4163 MIIterType TargetPacket = (*BlockI)->end();
4164 MICInstIterType WithinPacket = (*BlockI)->instr_end();
4165 bool NeedToNewify =
false, PreciousMIToInsert =
false;
4166 if (canMIBePredicated(MIToMove, *SrcBlock, *BlockI, TargetPacket,
4167 WithinPacket, NeedToNewify, PreciousMIToInsert)) {
4172 bool done = predicateMI(MIToMove, *SrcBlock, *BlockI, TargetPacket,
4173 WithinPacket, NeedToNewify, PreciousMIToInsert);
4175 assert(done &&
"Wrong Analysis");
4176 ++GlobalPredicatedIns;
4178 LocalMergePackCand.insert(*SrcBlock);
4179 if (PreciousMIToInsert) {
4185 IgnoreBlocks.
insert(*BlockI);
4186 ToBeSqueezed.insert(&*TargetPacket);
4188 IgnorePostSchedOpt.push_back(*BlockI);
4190 <<
" from further speculation";);
4213MIIterType GreedyPullup::findInsertPtSpec(MachineInstr *SrcMI,
4214 MachineBasicBlock *DstBB,
4215 bool SpecPreciousMI,
4216 bool SpecPreciousMIComp,
4217 bool GlobalPrecious) {
4219 MIIterType DstEnd = DstBB->
end();
4220 MIInstIterType InsPos = DstBB->
instr_end();
4222 if (shouldNotBePulledUp(SrcMI) ||
4224 isSoloInstruction(*SrcMI))
4230 for (; MII != DstBB->
rend(); ++MII) {
4231 MachineInstr *
MI = &*MII;
4232 if (
MI->isDebugInstr())
4236 if (
MI->isBundle()) {
4237 int DstDep = dstDependence(
MI->getIterator(), InsPos,
4239 if (!Dependency::hasOnly(DstDep, Dependency::Independent))
4245 InsPos =
MI->getIterator();
4248 dbgs() <<
"\n\tNo conflict (BUNDLE)! Checking resources.....\n";);
4250 getResourcesReadyForBundle(
MI->getIterator(), SpecPreciousMI);
4254 InsPos =
MI->getIterator();
4255 LLVM_DEBUG(
dbgs() <<
".....(Resources available. Checking higher up)";);
4256 if (GlobalPrecious && InsPos->isBundle())
4261 dbgs() <<
".....(No Resources (BUNDLE) Checking further up)";);
4266 int DstDep = dstDependence(
MI->getIterator(), InsPos,
4268 if (!Dependency::hasOnly(DstDep, Dependency::Independent))
4277 LLVM_DEBUG(
dbgs() <<
"\nLoopN cannot be pulled too far into MBB";);
4282 InsPos =
MI->getIterator();
4283 LLVM_DEBUG(
dbgs() <<
"No conflict! Checking further up .....\n";);
4290 assert(!InsPos->isInsideBundle());
4302 if (!InsPos->isBundle() && !SpecPreciousMIComp && (DstBB->
succ_size() > 1))
4309MIIterType GreedyPullup::findInsertPtPred(MachineInstr *SrcMI,
4310 MachineBasicBlock *DstBB,
4312 MICInstIterType &WithinPacket,
4313 bool PredPreciousMI) {
4315 MIIterType DstEnd = DstBB->
end();
4316 MIInstIterType InsPos = DstBB->
instr_end();
4318 if (shouldNotBePulledUp(SrcMI) || !QII->
isPredicated(*SrcMI) ||
4321 NeedToNewify =
false;
4323 LLVM_DEBUG(
dbgs() <<
"\n[FindInsertionPointPredicatedIns]" << *SrcMI;);
4326 for (MIRIterType MII = DstBB->
rbegin(); MII != DstBB->
rend(); ++MII) {
4327 MachineInstr *
MI = &*MII;
4328 if (
MI->isDebugInstr())
4332 if (
MI->isBundle()) {
4334 getResourcesReadyForBundle(
MI->getIterator(), PredPreciousMI);
4339 int DstDep = dstDependence(
MI->getIterator(), InsPos,
4342 if (Dependency::hasOnly(DstDep, Dependency::RAW)) {
4349 <<
"Read after write dependency with: " << *DepTrue;);
4351 if ((InsPos != DepTrue) && hasOneDef(&*DepTrue, DefP)) {
4352 if (canNewifyPredReg(DefP, DepTrue, DstBB->
instr_end()) &&
4354 isNewifiable(*SrcMI, &Hexagon::PredRegsRegClass)) {
4355 InsPos =
MI->getIterator();
4356 WithinPacket = DepTrue;
4357 NeedToNewify =
true;
4366 int DstDepMasked = DstDep & ~Dependency::WAR_SameBundle;
4367 if (Dependency::hasOnly(DstDepMasked, Dependency::RAW)) {
4368 unsigned DepReg = 0;
4369 LLVM_DEBUG(
dbgs() <<
"WAR_SameBundle dependency with: " << *DepAnti;);
4370 if (singleTrueDep(
MI->getIterator(), InsPos, SrcMI->
getIterator(),
4372 MICIterType MIBundle = &*MII;
4374 MIBundle == DstBB->
end()
4376 : std::next(MIBundle).getInstrIterator();
4377 if (canNewifyPredReg(DepReg, DepTrue,
Sentinel)) {
4379 InsPos =
MI->getIterator();
4380 WithinPacket = laterOfTheTwo(DepTrue, DepAnti,
Sentinel);
4382 NeedToNewify =
true;
4383 LLVM_DEBUG(
dbgs() <<
"Single RAW dependency with: " << *DepTrue;);
4390 if (Dependency::hasOnly(DstDep, Dependency::Independent)) {
4391 InsPos =
MI->getIterator();
4392 LLVM_DEBUG(
dbgs() <<
"No conflict (BUNDLE)! Checking further up!\n";);
4405 int DstDep = dstDependence(
MI->getIterator(), InsPos,
4407 if (!Dependency::hasOnly(DstDep, Dependency::Independent))
4410 InsPos =
MI->getIterator();
4411 LLVM_DEBUG(
dbgs() <<
"No conflict! Checking further up .....\n";);
4417 assert(!InsPos->isInsideBundle());
4442GreedyPullup::isLastChanceGlobalPullupCand(
const MachineInstr *
MI) {
4443 if (
MI->getOpcode() != Hexagon::V6_vcombine)
4447 unsigned Op0 =
MI->getOperand(0).getReg();
4448 unsigned Op1 =
MI->getOperand(1).getReg();
4449 unsigned Op2 =
MI->getOperand(2).getReg();
4460 for (MCRegAliasIterator AI(Op0,
TRI,
true); AI.isValid(); ++AI)
4470 for (
auto *MO : regmaskMOs)
4475 MachineInstr *InsertBundle = (MachineInstr *)(
const_cast<MachineInstr *
>(
4481 return InsertBundle;
4517bool GreedyPullup::lastChanceGlobalPullup(MIInstIterType &BundleHead,
4518 MIInstIterType InstrToMove,
4519 const MachineInstr *LoadBypassCand) {
4525 if (!isSuperblockLoop(
Loop))
4527 if (InstrToMove->getOpcode() == Hexagon::V6_vcombine)
4529 auto MII = BundleHead->getIterator();
4530 MachineInstr *SingleInstr =
nullptr;
4531 std::vector<std::pair<MachineInstr *, MachineInstr *>> candidatesForHoist;
4534 for (++MII; MII->isInsideBundle(); ++MII)
4535 if (
auto *InsertBundle = isLastChanceGlobalPullupCand(&*MII))
4536 candidatesForHoist.emplace_back(&*MII, InsertBundle);
4539 for (
auto Cand : candidatesForHoist) {
4541 getResourcesReadyForBundle(BundleHead->getIterator(),
false,
4543 if (!canReserveResources(InstrToMove))
4547 auto *
Pred =
Loop->getLoopPredecessor();
4550 auto TargetPacket = findInsertPtSpec(Cand.first,
Pred,
false,
true);
4551 if (TargetPacket !=
Pred->end()) {
4553 for (MIIterType BI = Cand.second; BI !=
MBB->
end(); ++BI) {
4554 if (LoadBypassCand && LoadBypassCand != &*Cand.first)
4556 if (!BI->isBundle())
4558 getResourcesReadyForBundle(BI->getIterator());
4559 if (canReserveResources(Cand.first->getIterator())) {
4560 if (isLegalToPacketizeWith(BI, Cand.first)) {
4564 MachineInstr *ClonedInstr = MF.CloneMachineInstr(Cand.first);
4565 MIBundleBuilder Bundle(&*BI);
4567 if (HLV->
isUsedWithin(std::next(BI.getInstrIterator()),
4569 Cand.first->getOperand(0).getReg(), Use)) {
4571 MachineInstr *
UseMI =
const_cast<MachineInstr *
>(&*
Use);
4575 MIInstIterType DestPos = BI->getIterator();
4576 Bundle.insert(std::next(DestPos), ClonedInstr);
4582 Cand.first->getOperand(0).getReg());
4595 std::next(BundleHead));
4596 BundleHead = std::prev(BundleHead);
4600 MIInstIterType DestPos = TargetPacket.getInstrIterator();
4602 if (DestPos->isBundle()) {
4603 MIBundleBuilder HoistBundle(&*TargetPacket);
4604 HoistBundle.insert(std::next(DestPos), Instr);
4608 auto CheckBundle = std::prev(DestPos);
4609 getResourcesReadyForBundle(CheckBundle);
4610 if (canReserveResources(
Instr->getIterator()) &&
4611 isLegalToPacketizeWith(CheckBundle, Instr)) {
4612 MIBundleBuilder HoistBundle(&*CheckBundle);
4613 HoistBundle.insert(std::next(CheckBundle), Instr);
4615 DestPos->unbundleFromPred();
4616 CheckBundle->eraseFromParent();
4617 Pred->insertAfter(&*DestPos, Instr);
4633bool GreedyPullup::canMIBeSpeculated(MachineInstr *
MI, MachineBasicBlock *SrcBB,
4634 MachineBasicBlock *DstBB,
4635 MIIterType &TargetPacket) {
4637 TargetPacket = DstBB->
end();
4640 bool isSuperBlockL = isSuperblockLoop(L);
4641 if (isSuperBlockL) {
4642 if (!isLoopInvariant(*
MI))
4647 if (shouldNotBePulledUp(
MI) || findInsertionPtSpecLocal(
MI) != SrcBB->
begin())
4650 ABI &abi = AnalyzeBranchInfo.getOrCreate(DstBB, QII);
4652 if (!abi.AnalysisValid)
4654 assert(abi.TBB && abi.FBB);
4657 bool SrcIsTBB = (abi.TBB == SrcBB);
4658 MachineBasicBlock *OtherBB = abi.FBB;
4660 assert(abi.FBB == SrcBB);
4666 if (!safeToSpeculate(
MI, DstBB))
4674 MIIterType TargetPacketIntMI = DstBB->
end();
4675 bool PreciousMIShouldBeSpeculated =
false;
4676 if (DefLiveInOtherBB) {
4678 if (!preciousToSpeculate(
MI, SrcBB, DstBB, OtherBB))
4681 if (!preciousMICanBeSpeculated(
MI, SrcBB, DstBB, OtherBB, TargetPacketIntMI,
4684 PreciousMIShouldBeSpeculated =
true;
4691 bool GlobalPrecious =
false;
4694 isLoopInvariant(*
MI))
4695 GlobalPrecious =
true;
4697 TargetPacket = findInsertPtSpec(
MI, DstBB, PreciousMIShouldBeSpeculated,
4698 false, GlobalPrecious);
4701 if (DefLiveInOtherBB) {
4704 while (TestIt != TargetPacketIntMI.getInstrIterator()) {
4705 assert(TestIt != TargetPacket.getInstrIterator());
4710 if (TargetPacket != DstBB->
end()) {
4713 <<
"\nInsert MI inside: " << *TargetPacket;
4714 else dbgs() <<
"\nInsert MI just before: " << *TargetPacket;);
4717 assert(!PreciousMIShouldBeSpeculated);
4721bool GreedyPullup::safeToSpeculate(MachineInstr *
MI,
4722 MachineBasicBlock *DstBB)
const {
4726 MI->hasUnmodeledSideEffects())
4732 for (
const MachineOperand &
Op :
MI->operands()) {
4733 if (!
Op.isReg() || !
Op.isDef())
4735 unsigned R =
Op.getReg();
4743 unsigned DefR = 0, UseR = 0, BaseR = 0, OffR = 0;
4744 if (!
MI->hasOneMemOperand() || (*
MI->memoperands_begin())->isVolatile() ||
4745 !(*
MI->memoperands_begin())->getValue() ||
4746 !hasOneDefAndUse(
MI, DefR, UseR))
4749 MICInstIterType DefUseR = DstBB->
instr_end();
4750 if (!
MI->getParent()->isLiveIn(UseR) ||
4756 const Value *MIVal =
4760 bool LoadFromSameObject =
false;
4767 const MachineInstr *MII = &*MIIt;
4770 unsigned DefP = 0, UseP = 0, BaseP = 0, OffP = 0;
4773 shouldNotBeCrossed(MII,
MI)) &&
4779 MICInstIterType DefUseP = DstBB->
instr_end();
4784 if (mmo->isVolatile() || !mmo->getValue() ||
4789 if ((*
MI->memoperands_begin())->getAlign() > mmo->getAlign())
4792 MI->getOperand(BaseR).getReg());
4794 if (MIVal == MIIVal) {
4795 LoadFromSameObject =
true;
4803 LoadFromSameObject =
false;
4805 return LoadFromSameObject;
4818bool GreedyPullup::preciousMICanBeSpeculated(MachineInstr *
MI,
4819 MachineBasicBlock *SrcBB,
4820 MachineBasicBlock *DstBB,
4821 MachineBasicBlock *OtherBB,
4822 MIIterType &TargetPacket,
4823 unsigned ApproachNum) {
4825 assert(preciousToSpeculate(
MI, SrcBB, DstBB, OtherBB));
4826 if (ApproachNum != 1)
4829 unsigned DefR, UseR;
4830 if (!hasOneDefAndUse(
MI, DefR, UseR))
4838 MachineBasicBlock *LP =
nullptr;
4843 LLVM_DEBUG(
dbgs() <<
"\nCritical edge will be split during speculation.";);
4853 TargetPacket = findInsertPtSpec(
MI, DstBB,
true,
false);
4854 if (TargetPacket == DstBB->
end()) {
4858 LLVM_DEBUG(
dbgs() <<
"<<Resource available for speculating PreciousMI>>";);
4860 <<
"\nInsert MI inside: " << *TargetPacket;
4861 else dbgs() <<
"\nInsert MI just before: " << *TargetPacket;);
4864 std::unique_ptr<RegScavenger>
RS(
new RegScavenger());
4865 RS->enterBasicBlock(*DstBB);
4868 markLiveInsAsUsed(RS, DstBB);
4869 markLiveInsAsUsed(RS, SrcBB);
4870 markLiveInsAsUsed(RS, OtherBB);
4871 markLiveOutsAsUsed(RS, DstBB);
4872 markLiveOutsAsUsed(RS, SrcBB);
4876 markAllRegsAsUsed(RS, DstBB);
4877 if (!Hexagon::IntRegsRegClass.
contains(DefR))
4882 unsigned ScratchReg =
RS->FindUnusedReg(RC);
4883 if (ScratchReg == 0 || SrcBB->
isLiveIn(ScratchReg))
4886 <<
" available for speculating PreciousMI\n\t" << *
MI;);
4891 for (MIInstIterType OMBBI = OtherBB->
instr_begin();
4892 OMBBI != OtherBB->
instr_end(); ++OMBBI) {
4893 if (OMBBI->modifiesRegister(ScratchReg,
TRI))
4896 if (OMBBI->hasRegisterImplicitUseOperand(DefR))
4903 QII->get(Hexagon::A2_tfr), ScratchReg)
4906 MIIterType NewMIPos = findInsertPtSpec(NewMI, DstBB,
false,
true);
4912 bool invalidSpeculation = (NewMIPos == DstBB->
end());
4914 MICInstIterType laterOne =
4915 laterOfTheTwo(TargetPacket.getInstrIterator(),
4916 NewMIPos.getInstrIterator(), DstBB->
instr_end());
4921 if (laterOne == NewMIPos.getInstrIterator()) {
4922 if (TargetPacket->isBundle() || (NewMIPos != TargetPacket))
4923 invalidSpeculation =
true;
4926 if (invalidSpeculation) {
4928 dbgs() <<
"\nSupporting MI for precious MI cannot be speculated: "
4933 LLVM_DEBUG(
dbgs() <<
"<<Resource available for speculating Comp code>>";);
4935 <<
"\nInsert MI inside: " << *NewMIPos;
4936 else dbgs() <<
"\nInsert MI just before: " << *NewMIPos;);
4942 assert(laterOfTheTwo(TargetPacket.getInstrIterator(),
4943 NewMIPos.getInstrIterator(),
4944 DstBB->
instr_end()) == TargetPacket.getInstrIterator());
4949 replaceUsageOfReg(DefR, ScratchReg, OtherBB, NewMI);
4953 bool done = speculateMI(NewMI, OtherBB, DstBB, NewMIPos);
4955 assert(done &&
"Wrong Analysis of speculation of precious MI");
4956 TargetPacket = NewMIPos;
4957 SpeculatedIns[DstBB].push_back(NewMI);
4959 MachineBasicBlock *NewCritBB = NULL;
4960 NewCritBB = splitCriticalEdge(DstBB, OtherBB, LP);
4966 QII->get(Hexagon::A2_tfr), DefR)
4972 LLVM_DEBUG(
dbgs() <<
"\nAdding MI to New critical MBB:" << *NewCritBB;);
4977bool GreedyPullup::preciousToSpeculate(
const MachineInstr *
MI,
4978 const MachineBasicBlock *SrcBB,
4979 const MachineBasicBlock *DstBB,
4980 const MachineBasicBlock *OtherBB)
const {
4986 MachineLoop *SrcLoop = MLI.
getLoopFor(SrcBB);
4987 MachineLoop *DstLoop = MLI.
getLoopFor(DstBB);
4988 MachineLoop *OtherBBLoop = MLI.
getLoopFor(OtherBB);
4989 return (SrcLoop &&
MI->mayLoad() && (SrcLoop != OtherBBLoop) &&
4990 (SrcLoop == DstLoop));
4993bool GreedyPullup::preciousToPredicate(
const MachineInstr *
MI,
4994 const MachineBasicBlock *SrcBB,
4995 const MachineBasicBlock *DstBB,
4996 const MachineBasicBlock *OtherBB)
const {
5000 MachineLoop *SrcLoop = MLI.
getLoopFor(SrcBB);
5001 MachineLoop *DstLoop = MLI.
getLoopFor(DstBB);
5002 MachineLoop *OtherBBLoop = MLI.
getLoopFor(OtherBB);
5004 return (
MI->mayLoad() && SrcLoop && (SrcLoop == DstLoop) &&
5005 (SrcLoop != OtherBBLoop));
5008bool GreedyPullup::canMIBePredicated(MachineInstr *
MI, MachineBasicBlock *SrcBB,
5009 MachineBasicBlock *DstBB,
5010 MIIterType &TargetPacket,
5011 MICInstIterType &WithinPacket,
5012 bool &NeedToNewify,
bool &Precious) {
5014 TargetPacket = DstBB->
end();
5022 if (isSuperblockLoop(L)) {
5023 if (!isLoopInvariant(*
MI))
5028 if (shouldNotBePulledUp(
MI) || findInsertionPtSpecLocal(
MI) != SrcBB->
begin())
5031 ABI &abi = AnalyzeBranchInfo.getOrCreate(DstBB, QII);
5032 if (!abi.AnalysisValid)
5034 assert(abi.TBB && abi.FBB);
5037 bool SrcIsTBB = (abi.TBB == SrcBB);
5038 MachineBasicBlock *OtherBB = abi.FBB;
5040 assert(abi.FBB == SrcBB);
5043 SmallVector<MachineOperand, 4>
Cond = abi.Cond;
5053 MachineInstr *PredMI =
5055 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i)
5068 LLVM_DEBUG(
dbgs() <<
"\nPredicated MI for analysis:" << *PredMI);
5070 findInsertPtPred(PredMI, DstBB, NeedToNewify, WithinPacket,
false);
5071 if (TargetPacket == DstBB->
end()) {
5072 LLVM_DEBUG(
dbgs() <<
"\nNo resources available for MI; is it precious?";);
5073 Precious = preciousToPredicate(
MI, SrcBB, DstBB, OtherBB);
5076 dbgs() <<
"\nMI is Precious, trying the squeeze the dest. BUNDLE");
5078 findInsertPtPred(PredMI, DstBB, NeedToNewify, WithinPacket, Precious);
5082 if (TargetPacket != DstBB->
end()) {
5085 <<
"\nInsert MI inside: " << *TargetPacket;
5086 else dbgs() <<
"\nInsert MI just before: " << *TargetPacket;);
5094bool GreedyPullup::speculateMI(MachineInstr *
MI, MachineBasicBlock *SrcBB,
5095 MachineBasicBlock *DstBB, MIIterType DstPos) {
5098 assert(DstPos->getParent() == DstBB);
5099 MachineInstr *InstrToMove;
5101 if (DstPos->isBundle()) {
5102 MIInstIterType DstPack = DstPos.getInstrIterator();
5103 getResourcesReadyForBundle(DstPack);
5104 if (canReserveResources(
MI->getIterator()) &&
5105 isLegalToPacketizeWith(DstPos,
MI)) {
5106 InstrToMove =
removeMI(
MI, QII, &SpecPacketOpp);
5107 MIBundleBuilder Bundle(&*DstPos);
5108 Bundle.insert(std::next(DstPack), InstrToMove);
5112 InstrToMove =
removeMI(
MI, QII, &SpecPacketOpp);
5113 DstBB->
insert(DstPos, InstrToMove);
5118 InstrToMove =
removeMI(
MI, QII, &SpecPacketOpp);
5119 DstBB->
insert(DstPos, InstrToMove);
5130 LLVM_DEBUG(
dbgs() <<
"\nDstMBB: " << *DstBB <<
"\nSrcMBB: " << *SrcBB);
5134bool GreedyPullup::predicateMI(MachineInstr *
MI, MachineBasicBlock *SrcBB,
5135 MachineBasicBlock *DstBB, MIIterType DstPack,
5136 MICInstIterType WithinPacket,
bool NeedToNewify,
5142 assert(DstPack->getParent() == DstBB);
5143 ABI &abi = AnalyzeBranchInfo.getOrCreate(DstBB, QII);
5144 assert(abi.AnalysisValid);
5145 bool SrcIsTBB = (abi.TBB == SrcBB);
5146 SmallVector<MachineOperand, 4>
Cond = abi.Cond;
5159 MI->setDesc(QII->get(NewOpcode));
5162 MachineInstr *InstrToMove;
5163 if (DstPack->isBundle()) {
5164 MIInstIterType DstPos = DstPack.getInstrIterator();
5165 getResourcesReadyForBundle(DstPos);
5168 [[maybe_unused]]
bool Reserved = canReserveResources(
MI->getIterator());
5169 [[maybe_unused]]
bool LegalPacket = isLegalToPacketizeWith(DstPack,
MI);
5170 assert(((
Reserved && LegalPacket) || Precious) &&
"Wrong analysis.");
5172 InstrToMove =
removeMI(
MI, QII, &SpecPacketOpp);
5173 MIBundleBuilder Bundle(&*DstPack);
5174 if (WithinPacket != DstBB->
instr_end()) {
5175 assert(WithinPacket->isBundledWithPred());
5176 DstPos =
const_cast<MachineInstr *
>(&*WithinPacket)->getIterator();
5178 Bundle.insert(std::next(DstPos), InstrToMove);
5181 InstrToMove =
removeMI(
MI, QII, &SpecPacketOpp);
5182 DstBB->
insert(DstPack, InstrToMove);
5194 LLVM_DEBUG(
dbgs() <<
"\nDstMBB: " << *DstBB <<
"\nSrcMBB: " << *SrcBB);
5203 GS->generateCFG(MF);
5206bool HexagonGlobalSchedulerV2::runOnMachineFunction(
MachineFunction &MF) {
5211 TM =
static_cast<const HexagonTargetMachine *
>(&MF.
getTarget());
5212 QII =
ST.getInstrInfo();
5213 TRI =
ST.getRegisterInfo();
5215 AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
5216 HexagonLiveVariables *HLV = &getAnalysis<HexagonLiveVariables>();
5218 LLVM_DEBUG(
dbgs() <<
"\nHexagon Live Variables Analysis unavailable."
5219 <<
"\nExiting Hexagon Global Scheduler");
5222 const MachineBranchProbabilityInfo *MBPI =
5223 &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
5224 MachineLoopInfo *MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
5226 GS.reset(
new GreedyPullup(TM, QII,
TRI, MF, *MLI, AA, MBPI, HLV));
5229 "Invalid IR after Packetizer");
5233 "Invalid CFG transformation.");
5234 GS->formSchedulingRegions(MF);
5236 GS->printSchedulingRegions();
5237 GS->pruneSchedulingRegions(&GlobalScheduler::pruneCriteria);
5239 GS->printSchedulingRegions();
5240 GS->sortSchedulingRegions();
5242 GS->printSchedulingRegions();
5246 "Invalid Scheduling.");
5255 GS->performExposedOptimizations();
5259 GS->generateCompoundsPostSched();
5260 GS->generateAutoAndPredicates(MF);
5269 return new HexagonGlobalSchedulerV2();
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
MachineBasicBlock MachineBasicBlock::iterator MBBI
Function Alias Analysis false
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static cl::opt< bool > EnableSqueeze("try-squeeze-bundle", cl::Hidden, cl::init(true), cl::desc("Try putting 5 instructions in bundle"))
static cl::opt< bool > PullUnsafeMemops("pull-unsafe-memops", cl::Hidden, cl::init(true), cl::desc("Pull up loads which have no memoperands"))
static cl::opt< bool > DisableHVXLocalPullupAndSuperblock("disable-hvx-sched-superblock", cl::Hidden, cl::init(false), cl::desc("Disable single block loops in superblocks and the HVX local " "pull-up pass."))
static cl::opt< bool > PostSchedOpt("post-sched-opt", cl::Hidden, cl::init(true), cl::desc("Optimize after global scheduling"))
static MachineInstr * updateBundle(MachineInstr *BundleHead, const HexagonInstrInfo *QII, SmallVectorImpl< MachineInstr * > *SpecScope=nullptr)
static bool isEmptyBlock(MachineBasicBlock *MBB)
static std::pair< MIInstIterType, MIInstIterType > getParentBundle(MachineInstr *MI)
static MIRIterType getSpeculationBeginPoint(MachineInstr *SrcMI, MachineBasicBlock *DstBB)
static cl::opt< bool > NoPredInBundle("nopred-sched-bundle", cl::Hidden, cl::desc("Do not put predicated instructions in bundle"))
static cl::opt< bool > EnableLocalMergePackets("enable-local-merge-packets", cl::Hidden, cl::init(true), cl::desc("Push down local MIs into the next packet"))
static cl::opt< bool > PreventDuplexSep("prevent-duplex-sep-v2", cl::Hidden, cl::init(true), cl::desc("Prevent separation of duplex while pullup in global scheduler V2"))
static cl::opt< bool > EnableSparsePullup("enable-sparse-pullup", cl::Hidden, cl::init(true), cl::desc("Enable pullup to/from sparse packets"))
static cl::opt< bool > PostSchedPack("post-sched-packet", cl::Hidden, cl::init(true), cl::desc("Packetize unbundled MIs after global scheduling"))
static cl::opt< bool > EnableHVXLocalPullup("enable-hvx-local-pullup", cl::Hidden, cl::init(true), cl::desc("Enable the HVX local pull-up pass."))
static cl::opt< bool > EnableHVXLocalPullupInnermost("enable-hvx-local-pullup-innermost", cl::Hidden, cl::init(false), cl::desc("Enable the HVX local pull-up pass on single block innermost " "loops only."))
static cl::opt< unsigned > MaxPUBundles("hexagon-pullup-distance", cl::Hidden, cl::init(10), cl::desc("Maximum number of bundles to check for local pull-up"))
static cl::opt< bool > VerifyGlobalSched("verify-global-sched", cl::Hidden, cl::init(false), cl::desc("Run the machine verifier at several stages of global scheduler"))
static void transferLiveIns(const MachineBasicBlock *From, MachineBasicBlock *To)
static MachineBasicBlock * ShouldSplitCriticalEdge(MachineBasicBlock *FromBB, MachineBasicBlock *ToBB)
static cl::opt< bool > NoSpecInBundle("nospec-sched-bundle", cl::Hidden, cl::desc("Do not put speculated instructions in bundle"))
static cl::opt< bool > EnableLocalPullup("enable-local-pullup", cl::Hidden, cl::init(true), cl::desc("Speculate local MIs in parent BUNDLEs"))
static MachineInstr * removeMI(MachineInstr *MI, const HexagonInstrInfo *QII, SmallVectorImpl< MachineInstr * > *SpecPacketOpp=nullptr, MachineInstr **singleInstr=nullptr)
static cl::opt< bool > EnableHVXLCGP("enable-hvx-lcgp", cl::Hidden, cl::init(true), cl::desc("Enable the HVX last chance global pull-up pass."))
static bool checkIfSameBundle(MachineInstr *MI1, MachineInstr *MI2)
static cl::opt< bool > EnableLCGPForLoadBypassOnly("enable-lcgp-for-load-bypass-only", cl::Hidden, cl::init(true), cl::desc("Enable the HVX last chance global pull-up for load-bypass-only"))
static cl::opt< bool > EnableHVXSuperblockHeuristic("enable-hvx-superblock-heuristic", cl::Hidden, cl::init(true), cl::desc("Run HVX scheduling only on blocks whose ratio of HVX " "instructions exceeds -hvx-sched-profit-ratio."))
static cl::opt< bool > EnableSingleLoopSuperblock("enable-single-loop-superblock", cl::Hidden, cl::init(true), cl::desc("Enable superblocks consisting of single block loops."))
static cl::opt< bool > EnableHVXTwoLoadCurBypass("enable-hvx-two-load-cur-bypass", cl::Hidden, cl::init(false), cl::desc("Enable the two-load .cur load-bypass case in local pull-up."))
static std::vector< const MachineOperand * > collectRegmaskMOs(const MachineBasicBlock &MBB)
static cl::opt< bool > EnableHLXLoadStorePullup("enable-hlx-load-store-pullup", cl::Hidden, cl::init(true), cl::desc("Pull up HLX .tmp load uses and .new HLX stores to remove the " "copies inserted by HLX load/store expansion"))
static cl::opt< bool > EnableClusterPullup("enable-cluster-pullup", cl::Hidden, cl::init(true), cl::desc("Enable pullup for dependent instruction groups"))
Enable cluster pullup optimization.
static cl::opt< bool > EnableHVXLocalPullupLB("enable-hvx-local-pullup-load-bypass", cl::Hidden, cl::init(true), cl::desc("Enable the HVX local pull-up pass (load bypass only)."))
static cl::opt< bool > EnablePullupPred("enable-pullup-pred", cl::Hidden, cl::init(true), cl::desc("Use pullup predication"))
static bool inBetweenBarriers(MICIterType Iter, const MachineBasicBlock *DstBB)
static cl::opt< bool > EnableGlobalSched("enable-global-sched", cl::Hidden, cl::init(false), cl::desc("Enable global scheduler optimizer"))
static cl::opt< bool > PredicateAllInst("predicate-all-inst", cl::Hidden, cl::desc("Try predicating all possible instructions."))
bool endloopAfterMI(MachineBasicBlock::instr_iterator MII, MachineBasicBlock *MBB)
static cl::opt< float > HVXSchedProfitRatio("hvx-sched-profit-ratio", cl::Hidden, cl::init(0.80), cl::desc("Ratio of HVX instruction/Total instructions " "for the profitability check."))
static cl::opt< bool > SpeculateAllInst("speculate-all-inst", cl::Hidden, cl::desc("Try speculating all possible instructions."))
cl::opt< bool > OneComplexPerPacket
static const unsigned SafetyBuffer
cl::opt< bool > EnableDefUseSamePacket("enable-def-use-same-packet", cl::Hidden, cl::init(true), cl::desc("Allow moving an instruction out of a packet that redefines one of " "its source registers"))
cl::opt< bool > OneFloatPerPacket
#define HEXAGON_PACKET_SIZE
static bool isSchedBarrier(const MachineInstr &MI)
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
SmallVector< MachineOperand, 4 > Cond
if(!TBB||Cond.size()< 1) return false
Register const TargetRegisterInfo * TRI
Promote Memory to Register
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
This file declares the machine register scavenger class.
Remove Loads Into Fake Uses
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static MachineBasicBlock & splitEdge(MachineBasicBlock &MBB, MachineBasicBlock &Succ, int SuccCount, MachineInstr *Br, MachineInstr *&UncondBr, const X86InstrInfo &TII)
AnalysisUsage & addRequired()
int find_first() const
Returns the index of the first set bit, -1 if none of the bits are set.
size_type count() const
Returns the number of bits which are set.
iterator find(const_arg_type_t< KeyT > Val)
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
FunctionPass class - This class is used to implement most global optimizations.
bool isHLXVec(const MachineInstr &MI) const
bool isPredicated(const MachineInstr &MI) const override
Returns true if the instruction is already predicated.
bool definesPredicateReg(const MachineInstr &MI) const
bool isCompoundBranchInstr(const MachineInstr &MI) const
bool isJumpR(const MachineInstr &MI) const
bool invertAndChangeJumpTarget(MachineInstr &MI, MachineBasicBlock *NewTarget) const
bool isDotTmpInst(const MachineInstr &MI) const
bool isSoftwarePipelineLoopInstr(const MachineInstr &MI) const
unsigned nonDbgBundleSize(MachineBasicBlock::const_iterator BundleHead) const
int getDotNewPredOp(const MachineInstr &MI, const MachineBranchProbabilityInfo *MBPI) const
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
bool isPredicatedNew(const MachineInstr &MI) const
bool isJumpWithinBranchRange(const MachineInstr &MI, unsigned offset) const
std::optional< unsigned > getOperandLatency(const InstrItineraryData *ItinData, const MachineInstr &DefMI, unsigned DefIdx, const MachineInstr &UseMI, unsigned UseIdx) const override
getOperandLatency - Compute and return the use operand latency of a given pair of def and use.
bool hasCallUseInBundle(MachineBasicBlock::const_instr_iterator MIBegin, MachineBasicBlock::const_instr_iterator MIEnd, unsigned Reg, const TargetRegisterInfo &TRI) const
void findInstrPredicate(MachineInstr *MI, SmallVectorImpl< MachineOperand > &Cond) const
findInstrPredicate - Set the predicate of the instruction in the specified predicate vector First ent...
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
Reverses the branch condition of the specified condition list, returning false on success and true if...
bool isLoopN(const MachineInstr &MI) const
bool isFloatCompare(const MachineInstr &MI) const
bool isConstExtended(const MachineInstr &MI) const
bool PredOpcodeHasJMP_c(unsigned Opcode) const
bool isDotCurInst(const MachineInstr &MI) const
bool areMemAccessesDependent(AAResults *AA, const MachineInstr *MIa, const MachineInstr *MIb) const
Return true if the memory accesses of MIa and MIb may depend on each other.
bool isExtended(const MachineInstr &MI) const
bool isPredicateLate(unsigned Opcode) const
bool isMemOp(const MachineInstr &MI) const
bool isDeallocRet(const MachineInstr &MI) const
bool isDotNewInst(const MachineInstr &MI) const
void translateInstrsForDup(MachineFunction &MF, bool ToBigInstrs=true) const
bool isTC1(const MachineInstr &MI) const
std::optional< unsigned > getInvertedCompare(const int OpC) const
bool isDotNewOperand(const MachineInstr &MI, unsigned OpIndex) const
Should be able to tell if the operand in question is .new.
int getDotCurOp(const MachineInstr &MI) const
bool isSafeInBundleWAR(const MachineInstr &DefMI, unsigned DefOpIdx, const MachineInstr &UseMI, unsigned UseOpIdx, bool IsPullDown=false, bool DefViaRegMask=false) const
A use (UseMI) can share a bundle with an instruction that redefines the same register (DefMI),...
bool isHVXVec(const MachineInstr &MI) const
bool mayBeCurLoad(const MachineInstr &MI) const
bool isNewValueJump(const MachineInstr &MI) const
bool PredicateInstruction(MachineInstr &MI, ArrayRef< MachineOperand > Cond) const override
Convert the instruction into a predicated instruction.
int srcDependence(MachineBasicBlock::instr_iterator SrcBeg, MachineBasicBlock::instr_iterator SrcEnd, MachineBasicBlock::instr_iterator RMII, const MIUseDefMap &MIUseDefs, SmallSetVector< MachineInstr *, 4 > *DepSet=nullptr, bool PreventDuplexSep=true, bool IsPullDown=false) const
Compute the intra-packet dependence of RMII against the rest of the bundle [SrcBeg,...
unsigned nonDbgBBSize(const MachineBasicBlock *BB) const
getInstrTimingClassLatency - Compute the instruction latency of a given instruction using Timing Clas...
bool isEndLoopN(unsigned Opcode) const
bool getBaseAndOffsetPosition(const MachineInstr &MI, unsigned &BasePos, unsigned &OffsetPos) const override
For instructions with a base and offset, return the position of the base register and offset operands...
bool isPredicable(const MachineInstr &MI) const override
Return true if the specified instruction can be predicated.
HexagonII::CompoundGroup getCompoundCandidateGroup(const MachineInstr &MI) const
SmallVector< MachineInstr *, 2 > getBranchingInstrs(MachineBasicBlock &MBB) const
bool isNewValueStore(const MachineInstr &MI) const
unsigned getCompoundOpcode(const MachineInstr &GA, const MachineInstr &GB) const
unsigned getDistanceBetween(const MachineBasicBlock *From, const MachineBasicBlock *To, unsigned BufferPerMBB=HEXAGON_INSTR_SIZE) const
Returns the linear distance (as per layout) of MI from the Function.
bool isUsedWithin(MICInstIterType MIBegin, MICInstIterType MIEnd, unsigned Reg, MICInstIterType &Use, SmallPtrSet< MachineInstr *, 2 > *ExceptionsList=nullptr) const
bool incrementalUpdate(MICInstIterType MIDelta, MachineBasicBlock *From, MachineBasicBlock *To)
incrementalUpdate - update the liveness when MIDelta is moved from From to To.
bool updateLocalLiveness(MachineFunction &Fn)
updateLocalLiveness - update only kill flags of operands.
const BitVector & getLiveOuts(const MachineBasicBlock *MBB) const
bool isDefLiveIn(const MachineInstr *MI, const MachineBasicBlock *MBB) const
MIUseDef_t & getMIUseDefs()
void regenerateDistanceMap(const MachineFunction &Fn)
void addNewMBB(MachineBasicBlock *MBB)
addNewMBB - inform the LiveVariable Analysis that new MBB has been added.
bool isLiveOut(const MachineBasicBlock *MBB, unsigned Reg) const
bool isDefinedWithin(MICInstIterType MIBegin, MICInstIterType MIEnd, unsigned Reg, MICInstIterType &Def) const
void addNewMI(MachineInstr *MI, MachineBasicBlock *MBB)
void initPacketizerState() override
unsigned getLoopDepth() const
Return the nesting level of this loop.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
unsigned pred_size() const
livein_iterator livein_end() const
bool isEHPad() const
Returns true if the block is a landing pad.
instr_iterator instr_begin()
MachineInstrBundleIterator< const MachineInstr > const_iterator
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
LLVM_ABI iterator getFirstNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the first non-debug instruction in the basic block, or end().
succ_iterator succ_begin()
LLVM_ABI void removeLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll())
Remove the specified register from the live in set.
LiveInVector::const_iterator livein_iterator
LLVM_ABI livein_iterator livein_begin() const
unsigned succ_size() const
bool hasAddressTaken() const
Test whether this block is used as something other than the target of a terminator,...
LLVM_ABI void dump() const
LLVM_ABI void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
SmallVectorImpl< MachineBasicBlock * >::iterator succ_iterator
SmallVectorImpl< MachineBasicBlock * >::iterator pred_iterator
LLVM_ABI void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
Instructions::iterator instr_iterator
pred_iterator pred_begin()
LLVM_ABI void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New)
Given a machine basic block that branched to 'Old', change the code and CFG so that it branches to 'N...
MachineInstrBundleIterator< MachineInstr, true > reverse_iterator
succ_reverse_iterator succ_rbegin()
LLVM_ABI bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
instr_iterator instr_end()
Instructions::const_iterator const_instr_iterator
void addLiveIn(MCRegister PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
reverse_iterator rbegin()
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
MachineInstr & instr_front()
LLVM_ABI bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void dump() const
dump - Print the current MachineFunction to cerr, useful for debugger use.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool verify(Pass *p=nullptr, const char *Banner=nullptr, raw_ostream *OS=nullptr, bool AbortOnError=true) const
Run the current MachineFunction through the machine code verifier, useful for debugger use.
Function & getFunction()
Return the LLVM function that this machine code represents.
BasicBlockListType::iterator iterator
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
void splice(iterator InsertPt, iterator MBBI)
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
static MachineInstrBundleIterator getAtBundleBegin(instr_iterator MI)
Get the bundle iterator for the given instruction's bundle.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isReturn(QueryType Type=AnyInBundle) const
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
const MachineBasicBlock * getParent() const
bool readsRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr reads the specified register.
bool isCall(QueryType Type=AnyInBundle) const
bool isDebugInstr() const
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_ABI MachineInstr * removeFromBundle()
Unlink this instruction from its basic block and return it without deleting it.
bool isBranch(QueryType Type=AnyInBundle) const
Returns true if this is a conditional, unconditional, or indirect branch.
bool modifiesRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr modifies (fully define or partially define) the specified register.
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
LLVM_ABI int findRegisterUseOperandIdx(Register Reg, const TargetRegisterInfo *TRI, bool isKill=false) const
Returns the operand index that is a use of the specific register or -1 if it is not found.
LLVM_ABI unsigned getBundleSize() const
Return the number of instructions inside the MI bundle, excluding the bundle header.
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
bool isUnconditionalBranch(QueryType Type=AnyInBundle) const
Return true if this is a branch which always transfers control flow to some other block.
LLVM_ABI void eraseFromBundle()
Unlink 'this' from its basic block and delete it.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
bool isInsideBundle() const
Return true if MI is in a bundle (but not the first MI in a bundle).
LLVM_ABI bool hasOrderedMemoryRef() const
Return true if this instruction may have an ordered or volatile memory reference, or if the informati...
MachineOperand * findRegisterUseOperand(Register Reg, const TargetRegisterInfo *TRI, bool isKill=false)
Wrapper for findRegisterUseOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
LLVM_ABI void removeOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with.
LLVM_ABI void dump() const
LLVM_ABI void addRegisterDefined(Register Reg, const TargetRegisterInfo *RegInfo=nullptr)
We have determined MI defines a register.
const MachineOperand & getOperand(unsigned i) const
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool isBundled() const
Return true if this instruction part of a bundle.
LLVM_ABI bool ReplaceMBBInJumpTables(MachineBasicBlock *Old, MachineBasicBlock *New)
ReplaceMBBInJumpTables - If Old is the target of any jump tables, update the jump tables to branch to...
LLVM_ABI unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
void setIsInternalRead(bool Val=true)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
MachineBasicBlock * getMBB() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
Register getReg() const
getReg - Returns the register number.
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool insert(const value_type &X)
Insert a new element into the SetVector.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
iterator erase(const_iterator CI)
typename SuperClass::iterator iterator
void push_back(const T &Elt)
std::reverse_iterator< const_iterator > const_reverse_iterator
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
std::reverse_iterator< iterator > reverse_iterator
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
constexpr const char * data() const
Get a pointer to the start of the string (which may not be null terminated).
CodeGenOptLevel getOptLevel() const
Returns the optimization level: None, Less, Default, or Aggressive.
LLVM_ABI void dump() const
Support for debugging, callable in GDB: V->dump()
int getNumOccurrences() const
std::pair< iterator, bool > insert(const ValueT &V)
iterator find(const_arg_type_t< ValueT > V)
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
initializer< Ty > init(const Ty &Val)
NodeAddr< DefNode * > Def
NodeAddr< InstrNode * > Instr
NodeAddr< UseNode * > Use
std::reverse_iterator< iterator > rbegin() const
std::reverse_iterator< iterator > rend() const
LLVM_ABI iterator begin() const
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool operator<(int64_t V1, const APSInt &V2)
void stable_sort(R &&Range)
LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
MachineBasicBlock::instr_iterator getBundleStart(MachineBasicBlock::instr_iterator I)
Returns an iterator to the first instruction in the bundle containing I.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
void initializeHexagonGlobalSchedulerV2Pass(PassRegistry &)
bool operator>=(int64_t V1, const APSInt &V2)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
bool operator>(int64_t V1, const APSInt &V2)
LLVM_ABI void verifyMachineFunction(const std::string &Banner, const MachineFunction &MF)
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MachineBasicBlock::instr_iterator getBundleEnd(MachineBasicBlock::instr_iterator I)
Returns an iterator pointing beyond the bundle containing I.
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
FunctionPass * createHexagonGlobalSchedulerV2()
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
DWARFExpression::Operation Op
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
@ End
Run the machine verifier once, at the end of the pipeline.
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
DenseMap< const MachineInstr *, UseDef_t > MIUseDef_t
bool operator<=(int64_t V1, const APSInt &V2)
MCRegisterClass TargetRegisterClass