204#include <unordered_map>
206#define DEBUG_TYPE "global_schedV2"
210STATISTIC(HexagonGSLocalPullups,
"Number of instructions pull-ups locally");
211STATISTIC(HexagonGSGlobalPullups,
"Number of instructions pull-ups globally");
215 cl::desc(
"Enable global scheduler optimizer"));
219 cl::desc(
"Use pullup predication"));
223 cl::desc(
"Speculate local MIs in parent BUNDLEs"));
227 cl::desc(
"Push down local MIs into the next packet"));
230 cl::desc(
"Optimize after global scheduling"));
234 cl::desc(
"Packetize unbundled MIs after global scheduling"));
238 cl::desc(
"Do not put predicated instructions in bundle"));
242 cl::desc(
"Do not put speculated instructions in bundle"));
246 cl::desc(
"Try putting 5 instructions in bundle"));
252 "Prevent separation of duplex while pullup in global scheduler V2"));
256 cl::desc(
"Pull up loads which have no memoperands"));
261 cl::desc(
"Run the machine verifier at several stages of global scheduler"));
265 cl::desc(
"Try predicating all possible instructions."));
269 cl::desc(
"Try speculating all possible instructions."));
273 cl::desc(
"Maximum number of bundles to check for local pull-up"));
277 cl::desc(
"Enable superblocks consisting of single block loops."));
281 cl::desc(
"Enable the HVX local pull-up pass (load bypass only)."));
285 cl::desc(
"Enable the HVX last chance global pull-up for load-bypass-only"));
292 cl::desc(
"Enable the two-load .cur load-bypass case in local pull-up."));
296 cl::desc(
"Enable the HVX local pull-up pass."));
300 cl::desc(
"Enable the HVX local pull-up pass on single block innermost "
305 cl::desc(
"Disable single block loops in superblocks and the HVX local "
310 cl::desc(
"Enable the HVX last chance global pull-up pass."));
314 cl::desc(
"Run HVX scheduling only on blocks whose ratio of HVX "
315 "instructions exceeds -hvx-sched-profit-ratio."));
319 cl::desc(
"Ratio of HVX instruction/Total instructions "
320 "for the profitability check."));
324 cl::desc(
"Pull up HLX .tmp load uses and .new HLX stores to remove the "
325 "copies inserted by HLX load/store expansion"));
329 cl::desc(
"Enable pullup to/from sparse packets"));
338 cl::desc(
"Enable pullup for dependent instruction groups"));
364 :
TBB(NULL), FBB(NULL), FirstTerm(NULL), SecondTerm(NULL),
365 AnalysisValid(
false) {}
366 bool analyzeBranch(MachineBasicBlock *
MBB,
const HexagonInstrInfo *QII,
370 if (FBB)
dbgs() <<
", FBB: " << FBB->getNumber();
371 if (FirstTerm)
dbgs() <<
", FirstTerm: " << *FirstTerm;
372 if (SecondTerm)
dbgs() <<
", SecondTerm: " << *SecondTerm);
375 struct AnalyzeBranchInfo_t {
376 typedef std::map<MachineBasicBlock *, ABI> BranchAnalyses_t;
377 BranchAnalyses_t BranchAnalyses;
381 ABI &getOrCreate(MachineBasicBlock *
MBB,
const HexagonInstrInfo *QII,
382 bool PostSched =
false) {
383 ABI &abi = BranchAnalyses[
MBB];
384 if (!abi.AnalysisValid)
385 abi.analyzeBranch(
MBB, QII, PostSched);
388 void invalidate(MachineBasicBlock *
MBB) {
389 iterator it = BranchAnalyses.find(
MBB);
390 assert((it != BranchAnalyses.end()) &&
391 "Cannot invalidate un-analyzed branch.");
392 BranchAnalyses.erase(it);
394 bool hasABInfo(MachineBasicBlock *
MBB)
const {
395 return BranchAnalyses.find(
MBB) != BranchAnalyses.end();
398 typedef BranchAnalyses_t::iterator iterator;
399 typedef BranchAnalyses_t::const_iterator const_iterator;
400 typedef BranchAnalyses_t::size_type size_type;
402 iterator
begin() {
return BranchAnalyses.begin(); }
403 const_iterator
begin()
const {
return BranchAnalyses.begin(); }
404 iterator
end() {
return BranchAnalyses.end(); }
405 const_iterator
end()
const {
return BranchAnalyses.end(); }
406 bool empty()
const {
return BranchAnalyses.empty(); }
407 void clear() { BranchAnalyses.clear(); }
408 size_type
size()
const {
return BranchAnalyses.size(); }
412 struct SchedulingRegion_t {
413 typedef SmallVector<MachineBasicBlock *, 2> SRMembers_t;
414 typedef MachineBasicBlock SRAtom_t;
415 BranchProbability EdgeProb;
417 SchedulingRegion_t() : EdgeProb(BranchProbability::getZero()) {}
419 void addSRMember(MachineBasicBlock *
MBB) { Members.push_back(
MBB); }
422 SmallVector<MachineBasicBlock *, 2>::iterator
MBBI = Members.
begin();
423 while (
MBBI != Members.
end()) {
434 iterator
begin() {
return Members.begin(); }
435 const_iterator
begin()
const {
return Members.begin(); }
436 reverse_iterator
rbegin() {
return Members.rbegin(); }
437 const_reverse_iterator
rbegin()
const {
return Members.rbegin(); }
438 iterator
end() {
return Members.end(); }
439 const_iterator
end()
const {
return Members.end(); }
440 reverse_iterator
rend() {
return Members.rend(); }
441 const_reverse_iterator
rend()
const {
return Members.rend(); }
442 bool empty()
const {
return Members.empty(); }
443 void clear() { Members.clear(); }
444 size_type
size()
const {
return Members.size(); }
446 return EdgeProb <
RHS.EdgeProb;
449 return EdgeProb <=
RHS.EdgeProb;
452 return EdgeProb >
RHS.EdgeProb;
455 return EdgeProb >=
RHS.EdgeProb;
459 using Dependency = HexagonInstrInfo::Dependency;
462 typedef SmallVector<SchedulingRegion_t, 2> SchedulingRegions_t;
463 typedef DenseMap<SchedulingRegion_t::SRAtom_t *,
468 GlobalScheduler(
const HexagonTargetMachine *tm,
const HexagonInstrInfo *qii,
471 const MachineBranchProbabilityInfo *mbpi,
472 HexagonLiveVariables *hlv)
473 : HexagonPacketizerList(mf, mli,
aa, mbpi,
false), TM(tm), QII(qii),
474 TRI(tri), HLV(hlv), MF(mf), MLI(mli), GlobalSpeculatedIns(0),
475 GlobalPredicatedIns(0), LocalPullups(0), SafetyBuffer(200),
476 SafetyBufferDelta(50) {
477 useHVXOps = mf.
getSubtarget<HexagonSubtarget>().useHVXOps();
478 useHLXOps = mf.
getSubtarget<HexagonSubtarget>().useHLXOps();
481 IsTinyCore = mf.
getSubtarget<HexagonSubtarget>().isTinyCore();
486 void initPacketizerStateLoc();
488 bool isLegalToPacketizeWith(MIIterType DestPack, MachineInstr *
MI);
490 inline bool isLegalToPacketizeTogetherMI(MachineInstr *BI, MachineInstr *
MI);
492 inline bool shouldNotBePulledUp(
const MachineInstr *
MI)
const;
502 inline bool shouldNotBeCrossed(
const MachineInstr *
MI,
503 const MachineInstr *ToMove,
504 bool IsMIInDestBundle =
false)
const;
507 void getResourcesReadyForBundle(MIInstIterType MII,
bool Squeeze =
false,
508 MachineInstr *InstrToSkip =
nullptr);
509 MachineInstr *generateCompoundIfPossible(MIInstIterType MII,
514 void generateCompoundsPostSched();
517 bool failsSpecialConditions(MachineInstr *BundleMI,
Register DestReg);
518 bool canNewifyPredReg(
unsigned Reg, MICInstIterType Producer,
520 bool getDefinitions(MachineInstr *
MI, MachineInstr *&Def1,
521 MachineInstr *&Def2, MachineInstr *&Def3);
527 bool canReserveResources(MIInstIterType MII)
const;
529 void replaceUsageOfReg(
unsigned Reg,
unsigned ScratchReg,
530 MachineBasicBlock *
MBB,
531 const MachineInstr *ExceptMI = NULL)
const;
532 inline void markLiveInsAsUsed(std::unique_ptr<RegScavenger> &RS,
533 MachineBasicBlock *
MBB)
const;
534 inline void markLiveOutsAsUsed(std::unique_ptr<RegScavenger> &RS,
535 MachineBasicBlock *
MBB)
const;
536 void markAllRegsAsUsed(std::unique_ptr<RegScavenger> &RS,
537 const MachineBasicBlock *
MBB)
const;
538 MachineBasicBlock *splitCriticalEdge(MachineBasicBlock *FromBB,
539 MachineBasicBlock *ToBB,
540 MachineBasicBlock *LP);
541 bool hasOneDef(
const MachineInstr *
MI,
unsigned &DefR)
const;
542 bool hasOneUse(
const MachineInstr *
MI,
unsigned &UseR)
const;
543 bool hasOneDefAndUse(
const MachineInstr *
MI,
unsigned &Def,
544 unsigned &Use)
const;
545 MICInstIterType laterOfTheTwo(MICInstIterType I1, MICInstIterType I2,
549 bool branchesInBetweenMBB(MIInstIterType MII, MachineBasicBlock *
MBB);
551 bool splitMBBAt(MIInstIterType MII, MachineBasicBlock *
MBB);
556 virtual void schedule() = 0;
559 void performExposedOptimizations();
560 void removeEmptyMBB(MachineBasicBlock *
MBB);
561 bool makeAsLayoutSuccessor(MachineBasicBlock *BB1, MachineBasicBlock *BB2);
562 bool removeRedundantBranches(MachineBasicBlock *
MBB, MachineBasicBlock *
TBB,
563 MachineInstr *FirstTerm, MachineBasicBlock *FBB,
564 MachineInstr *SecondTerm);
565 bool optimizeDualJumps(MachineBasicBlock *
MBB, MachineBasicBlock *
TBB,
566 MachineInstr *FirstTerm, MachineBasicBlock *FBB,
567 MachineInstr *SecondTerm);
568 MachineBasicBlock *optimizeBranches(MachineBasicBlock *
MBB,
569 MachineBasicBlock *
TBB,
570 MachineInstr *FirstTerm,
571 MachineBasicBlock *FBB);
587 void printSchedulingRegions() {
589 SchedulingRegions.begin();
590 SRI != SchedulingRegions.end(); ++SRI) {
591 dbgs() <<
"\nScheduling Region: ";
593 }
dbgs() <<
"\nTotal Scheduling Regions: "
594 << SchedulingRegions.size() <<
"\n";);
598 typedef bool (GlobalScheduler::*PruneCriteriaFP)(SchedulingRegion_t &)
const;
602 bool pruneCriteria(SchedulingRegion_t &SR)
const {
603 assert(!SR.Members.empty() &&
"Empty Scheduling region.");
604 if (SR.Members.size() == 1)
609 MachineLoop *HeadLI = MLI.getLoopFor(SR.Members[0]);
610 MachineLoop *LeafLI = MLI.getLoopFor(SR.Members[1]);
612 bool speculateInnerToOuter =
false;
615 if (HeadLI !=
nullptr && LeafLI !=
nullptr) {
617 speculateInnerToOuter =
true;
621 if (HeadLI != LeafLI && !speculateInnerToOuter)
627 virtual void pruneSchedulingRegions(PruneCriteriaFP PC);
630 virtual void sortSchedulingRegions();
632 bool isSuperblockLoop(MachineLoop *L);
633 virtual bool isProfitableToScheduleHVX(MachineLoop *L);
634 virtual bool isProfitableToScheduleHVX(MachineBasicBlock *
MBB);
638 const HexagonTargetMachine *TM;
639 const HexagonInstrInfo *QII;
640 const HexagonRegisterInfo *TRI;
641 HexagonLiveVariables *HLV;
643 MachineLoopInfo &MLI;
648 AnalyzeBranchInfo_t AnalyzeBranchInfo;
649 SchedulingRegions_t SchedulingRegions;
651 SmallVector<MachineInstr *, 4> SpecPacketOpp;
654 DenseSet<MachineInstr *> ToBeSqueezed;
658 PulledUpII SpeculatedIns;
661 std::set<MachineBasicBlock *> LocalMergePackCand;
663 std::unordered_map<MachineBasicBlock *, float> ProfitableHVXBlocks;
664 unsigned GlobalSpeculatedIns;
665 unsigned GlobalPredicatedIns;
666 unsigned LocalPullups;
667 const unsigned SafetyBuffer;
669 int SafetyBufferDelta;
671 bool useHVXOps =
false;
673 bool useHLXOps =
false;
676 bool IsTinyCore =
false;
679class GreedyPullup :
public GlobalScheduler {
681 GreedyPullup(
const HexagonTargetMachine *tm,
const HexagonInstrInfo *qii,
684 const MachineBranchProbabilityInfo *mbpi,
685 HexagonLiveVariables *hlv)
686 : GlobalScheduler(tm, qii, tri, mf, mli,
aa, mbpi, hlv) {}
688 void schedule()
override {
689 for (SchedulingRegions_t::iterator SRI = SchedulingRegions.begin();
690 SRI != SchedulingRegions.end(); ++SRI) {
691 unsigned currSpec = GlobalSpeculatedIns;
692 unsigned currPred = GlobalPredicatedIns;
698 dbgs() <<
"\n\nCurrent BB Global Pullup stats: Speculated Ins: "
699 << GlobalSpeculatedIns - currSpec
700 <<
", Predicated Ins: " << GlobalPredicatedIns - currPred;);
709 std::find(IgnorePostSchedOpt.begin(), IgnorePostSchedOpt.end(),
710 &*
MBBI) == IgnorePostSchedOpt.end()) {
711 localPullupHLX(&*
MBBI);
713 localPullupHVX(&*
MBBI);
718 dbgs() <<
"\n\nCurrent BB Local Pullup stats: Speculated Ins: "
723 HexagonGSLocalPullups += LocalPullups;
730 HexagonGSGlobalPullups = GlobalPredicatedIns + GlobalSpeculatedIns;
733 dbgs() <<
"\n\n**********Total Greedy Pullup stats**********"
734 <<
"\nGlobal speculated instructions: " << GlobalSpeculatedIns
735 <<
"\nGlobal predicated instructions: " << GlobalPredicatedIns
736 <<
"\nLocal speculated instruction: " << HexagonGSLocalPullups
741 bool canCauseInterlockStall(MIInstIterType DestBundle, MachineInstr *SrcMI,
742 const InstrItineraryData *
II);
744 void genLoadBypass(MachineInstr *LoadInstr, MachineInstr *InstrToMove);
749 bool canGenCurBypass(MIInstIterType DestBundle, MachineInstr *DepTrue,
750 MIInstIterType PktEnd,
int DstDep,
751 MachineInstr *Consumer,
752 const MachineInstr *Exempt =
nullptr);
757 bool tryTwoLoadCurBypass(MIIterType &BI, SchedulingRegion_t::SRAtom_t *SR,
758 MIInstIterType &DestBundle, MIIterType &SrcBundle,
759 MIIterType &NextBI,
bool isNextBISrc,
760 MIIterType &
Next, MachineInstr *SrcMI,
761 SmallPtrSetImpl<MachineInstr *> &PulledUpSet);
762 void localPullupHVX(SchedulingRegion_t::SRAtom_t *SR);
765 void localPullupImpl(SchedulingRegion_t::SRAtom_t *SR,
bool HVXMode);
768 void localPullupHLX(SchedulingRegion_t::SRAtom_t *SR);
769 void localPullupHLXLoadUses(SchedulingRegion_t::SRAtom_t *SR,
770 MachineInstr *HLXLoadMI, MachineInstr *HLXCopyMI);
771 void localPullupHLXStore(SchedulingRegion_t::SRAtom_t *SR,
772 MachineInstr *HLXStoreMI, MachineInstr *HLXCopyMI);
774 void localPullup(SchedulingRegion_t::SRAtom_t *SR);
776 bool mergePacketsAround(
const SmallVectorImpl<MachineInstr *> &Candidates);
779 void globalPullup(SchedulingRegion_t &SR);
782 bool canMIBeSpeculated(MachineInstr *
MI, MachineBasicBlock *SrcBB,
783 MachineBasicBlock *DstBB, MIIterType &TargetPacket);
785 bool safeToSpeculate(MachineInstr *
MI, MachineBasicBlock *DstBB)
const;
787 bool preciousMICanBeSpeculated(MachineInstr *
MI, MachineBasicBlock *SrcBB,
788 MachineBasicBlock *DstBB,
789 MachineBasicBlock *OtherBB,
790 MIIterType &TargetPacket,
791 unsigned ApproachNum);
792 inline bool preciousToSpeculate(
const MachineInstr *
MI,
793 const MachineBasicBlock *SrcBB,
794 const MachineBasicBlock *DstBB,
795 const MachineBasicBlock *OtherBB)
const;
796 inline bool preciousToPredicate(
const MachineInstr *
MI,
797 const MachineBasicBlock *SrcBB,
798 const MachineBasicBlock *DstBB,
799 const MachineBasicBlock *OtherBB)
const;
803 bool canMIBePredicated(MachineInstr *
MI, MachineBasicBlock *SrcBB,
804 MachineBasicBlock *DstBB, MIIterType &TargetPacket,
805 MICInstIterType &WithinPacket,
bool &NeedToNewify,
811 MIIterType findInsertPtSpec(MachineInstr *SrcMI, MachineBasicBlock *DstBB,
812 bool SpecPreciousMI,
bool SpecPreciousMIComp,
813 bool GlobalPrecious =
false);
817 MIIterType findInsertPtPred(MachineInstr *SrcMI, MachineBasicBlock *DstBB,
818 bool &NeedToNewify, MICInstIterType &WithinPacket,
819 bool PredPreciousMI =
false);
821 bool speculateMI(MachineInstr *
MI, MachineBasicBlock *SrcBB,
822 MachineBasicBlock *DstBB, MIIterType DstPos);
824 bool predicateMI(MachineInstr *
MI, MachineBasicBlock *SrcBB,
825 MachineBasicBlock *DstBB, MIIterType DstPack,
826 MICInstIterType WithinPacket,
bool NeedToNewify,
829 MIIterType findInsertionPtSpecLocal(MachineInstr *
MI);
844 int dstDependence(MICInstIterType DestBeg, MICInstIterType DestEnd,
845 MICInstIterType MII, MICInstIterType &DepTrue,
846 MICInstIterType &DepAnti,
unsigned HLXUseReg = 0,
847 bool IsPullDown =
false)
const;
850 int dstDependence(MICInstIterType DestBeg, MICInstIterType DestEnd,
852 std::vector<const MachineInstr *> &DepTrueVec,
853 std::vector<const MachineInstr *> &DepAntiVec,
854 std::vector<const MachineInstr *> &DepOutputVec,
855 std::vector<unsigned> *Regs =
nullptr)
const;
857 int srcDependence(MIInstIterType SrcBeg, MIInstIterType SrcEnd,
859 SmallPtrSetImpl<MachineInstr *> *DepSet =
nullptr)
const;
860 bool singleTrueDep(MICInstIterType SrcBeg, MICInstIterType SrcEnd,
861 MICInstIterType MII,
unsigned &
Reg)
const;
862 bool lastChanceGlobalPullup(MIInstIterType &BundleHead,
863 MIInstIterType InstrToMove,
864 const MachineInstr *LoadBypassCand =
nullptr);
865 bool isLoopInvariant(MachineInstr &
MI);
866 MachineInstr *isLastChanceGlobalPullupCand(
const MachineInstr *
MI);
873 HexagonGlobalSchedulerV2() : MachineFunctionPass(ID), GS(nullptr) {
877 StringRef getPassName()
const override {
878 return "Hexagon Global Scheduler V2";
881 void getAnalysisUsage(AnalysisUsage &AU)
const override {
884 AU.
addRequired<MachineBranchProbabilityInfoWrapperPass>();
903 const HexagonTargetMachine *TM;
904 const HexagonInstrInfo *QII;
905 const HexagonRegisterInfo *TRI;
906 std::unique_ptr<GlobalScheduler> GS;
909char HexagonGlobalSchedulerV2::ID = 0;
914 "Hexagon Global Scheduler V2",
false,
false)
920 return MI->getOpcode() == Hexagon::Y2_barrier;
927 auto [It,
Inserted] = ProfitableHVXBlocks.try_emplace(
MBB);
931 unsigned NumHVXInst = 0, NumTotalInst = 0;
940 if (NumTotalInst == 0)
943 It->second = (float)NumHVXInst / (
float)NumTotalInst;
950bool GlobalScheduler::isProfitableToScheduleHVX(MachineLoop *L) {
951 assert(
L->getNumBlocks() == 1 &&
"Expecting a single block loop");
954 auto *BB =
L->getBlocks()[0];
955 return isProfitableToScheduleHVX(BB);
960bool GlobalScheduler::isSuperblockLoop(MachineLoop *L) {
964 if (!L ||
L->getNumBlocks() != 1 || !
L->isInnermost())
966 if (
L->getBlocks()[0]->succ_size() < 2)
968 if (
L->getBlocks()[0]->pred_size() != 2)
971 return isProfitableToScheduleHVX(L);
974bool GlobalScheduler::shouldNotBePulledUp(
const MachineInstr *
MI)
const {
976 if (
MI->getOpcode() == Hexagon::LX_S16b_new)
978 if (
MI->mayStore() ||
MI->isCall() ||
MI->isBarrier() ||
MI->isInlineAsm() ||
981 if (
MI->isBundle() ||
MI->isDebugInstr())
987 return MI->hasOrderedMemoryRef();
1003bool GlobalScheduler::shouldNotBeCrossed(
const MachineInstr *
MI,
1004 const MachineInstr *ToMove,
1005 bool IsMIInDestBundle)
const {
1006 if (
MI->isBundle() ||
MI->isDebugInstr())
1009 if (
MI->isReturn() ||
MI->isCall() ||
MI->hasUnmodeledSideEffects() ||
1014 if (!
MI->mayLoadOrStore())
1025 if (
MI->mayLoad()) {
1039static std::vector<const MachineOperand *>
1041 std::vector<const MachineOperand *> regmaskMOs;
1042 for (MICInstIterType
II =
MBB.instr_begin(),
End =
MBB.instr_end();
II !=
End;
1044 if (
II->isBundle() ||
II->isDebugInstr())
1046 for (
auto &MO :
II->operands())
1048 regmaskMOs.push_back(&MO);
1057bool GreedyPullup::isLoopInvariant(MachineInstr &
MI) {
1059 MachineLoop *
Loop = MLI.getLoopFor(
MBB);
1062 if (!isSuperblockLoop(
Loop))
1066 for (
const MachineOperand &MO :
MI.operands()) {
1069 unsigned Op = MO.getReg();
1074 }
else if (MO.isDef()) {
1076 if (HLV->isDefinedWithin(std::next(
MI.getIterator()),
MBB->
instr_end(),
1083 for (MCRegAliasIterator AI(
Op,
TRI,
true); AI.isValid(); ++AI)
1084 if (HLV->isLiveOut(
MBB, *AI))
1089 for (
auto *MO : regmaskMOs)
1090 if (MO->clobbersPhysReg(
Op))
1099static std::pair<MIInstIterType, MIInstIterType>
1103 return std::make_pair(BundleStart, BundleEnd);
1108static MachineInstr *
1120 while (MIter != BundleEnd) {
1122 MI->unbundleFromPred();
1124 SpecScope->push_back(
MI);
1135static MachineInstr *
1141 if (
MI->isBundled()) {
1143 MIP =
MI->removeFromBundle();
1144 NewSingleInstr =
updateBundle(Bundle, QII, SpecPacketOpp);
1146 MIP =
MI->removeFromParent();
1149 *singleInstr = NewSingleInstr;
1158 while (MII !=
MBB->instr_end()) {
1159 if ((MII->getOpcode() == Hexagon::ENDLOOP0) ||
1160 (MII->getOpcode() == Hexagon::ENDLOOP1))
1181 MICIterType TestIt = DstBB->
begin();
1183 while (TestIt != Iter) {
1184 if (Iter->isBarrier())
1189 if (!Iter->isBarrier())
1192 while (Iter != DstBB->
end()) {
1193 if (Iter->isBarrier())
1203 if (
MBB->isEHPad() ||
MBB->hasAddressTaken() ||
MBB->pred_size() != 1 ||
1208 if (!
MBBI->isDebugInstr())
1218 MIRIterType Rb = DstBB->
rbegin();
1219 MIRIterType Re = DstBB->
rend();
1226 while ((&*Rb != &*
B.first) && (Rb != Re))
1235bool GlobalScheduler::ABI::analyzeBranch(MachineBasicBlock *
MBB,
1236 const HexagonInstrInfo *QII,
1239 if (AnalysisValid) {
1244 unsigned jumpers_size = Jumpers.
size();
1245 assert((jumpers_size == 1) || (jumpers_size == 2));
1246 FirstTerm = Jumpers.
back();
1247 if (jumpers_size == 2)
1248 SecondTerm = Jumpers[0];
1255 AnalysisValid =
false;
1258 return AnalysisValid;
1264bool GlobalScheduler::branchesInBetweenMBB(MIInstIterType MII,
1265 MachineBasicBlock *
MBB) {
1266 if (!MII->isBranch())
1269 while (MII->isInsideBundle())
1278 if (MII->isReturn())
1282 if (MII->isBranch())
1287bool GlobalScheduler::splitMBBAt(MIInstIterType MII, MachineBasicBlock *
MBB) {
1288 MachineBasicBlock *OrigTBB = NULL, *OrigFBB = NULL;
1289 SmallVector<MachineOperand, 4> OrigCond;
1300 if (OrigTBB == Succ1)
1313 MachineBasicBlock *FirstTBB = NULL, *FirstFBB = NULL;
1314 SmallVector<MachineOperand, 4> FirstCond;
1336 MachineBasicBlock *
MBB = &*
MBBI;
1339 MachineInstr *
MI = &*MII;
1341 if (branchesInBetweenMBB(
MI->getIterator(),
MBB)) {
1346 if (!splitMBBAt(std::next(
MI->getIterator()),
MBB))
1349 }
else if (
MI->getOpcode() == Hexagon::J2_jump &&
1350 MI->getOperand(0).isMBB()) {
1352 MachineBasicBlock *
Target =
MI->getOperand(0).getMBB();
1355 if (
MI->isBundled()) {
1357 MI->eraseFromBundle();
1360 MI->eraseFromParent();
1371bool GlobalScheduler::makeAsLayoutSuccessor(MachineBasicBlock *BB1,
1372 MachineBasicBlock *BB2) {
1380 if (SafetyBufferDelta < InsSize)
1382 SafetyBufferDelta -= InsSize;
1385 if (Jumpers.
size() > 1 || Jumpers.
empty())
1390 <<
" as LayoutSuccessor of " << BB1->
getNumber(););
1393 if (BB1LS != MF.
end()) {
1400void GlobalScheduler::removeEmptyMBB(MachineBasicBlock *
MBB) {
1414 <<
") from parent.\n");
1420bool GlobalScheduler::removeRedundantBranches(MachineBasicBlock *
MBB,
1421 MachineBasicBlock *
TBB,
1422 MachineInstr *FirstTerm,
1423 MachineBasicBlock *FBB,
1424 MachineInstr *SecondTerm) {
1425 bool Analyzed =
false;
1427 MachineInstr *Head = NULL, *ToErase = NULL;
1428 if (!FBB && (FirstTerm->
getOpcode() == Hexagon::J2_jump) &&
1432 dbgs() <<
"\nRemoving Uncond. jump to the layout successor in BB#"
1434 ToErase = FirstTerm;
1435 }
else if (SecondTerm && (
TBB == FBB) &&
1436 (SecondTerm->
getOpcode() == Hexagon::J2_jump)) {
1445 if (++FirstTermIter == SecondTermIter &&
1447 LLVM_DEBUG(
dbgs() <<
"\nRemoving multiple branching to same target in BB#"
1451 ToErase = FirstTerm;
1453 }
else if (SecondTerm && (SecondTerm->
getOpcode() == Hexagon::J2_jump) &&
1459 ToErase = SecondTerm;
1465 ToErase = FirstTerm;
1469 if (ToErase->isBundled()) {
1471 ToErase->eraseFromBundle();
1474 ToErase->eraseFromParent();
1488bool GlobalScheduler::optimizeDualJumps(MachineBasicBlock *
MBB,
1489 MachineBasicBlock *
TBB,
1490 MachineInstr *FirstTerm,
1491 MachineBasicBlock *FBB,
1492 MachineInstr *SecondTerm) {
1495 bool Analyzed =
false;
1498 (SecondTerm->
getOpcode() == Hexagon::J2_jump)) {
1514 <<
" out of range.");
1528 MachineInstr *SecondHead, *FirstHead;
1539 else if (!FirstHead) {
1543 }
else if (FirstHead == SecondHead) {
1545 assert((FirstHead && SecondHead) &&
"Unbundled Instruction");
1570MachineBasicBlock *GlobalScheduler::optimizeBranches(MachineBasicBlock *
MBB,
1571 MachineBasicBlock *
TBB,
1572 MachineInstr *FirstTerm,
1573 MachineBasicBlock *FBB) {
1583 MachineInstr *TBBMIb = &*
TBB->getFirstNonDebugInstr();
1584 if (TBBMIb->
getOpcode() == Hexagon::J2_jump &&
1587 if (
TBB->hasAddressTaken() ||
TBB->isEHPad() ||
1588 TBB == NewTarget || (
TBB->pred_size() != 1))
1597 <<
" out of range.");
1602 TBB->removeSuccessor(
TBB->succ_begin());
1607 SmallVector<MachineOperand, 4> PredCond;
1609 if (PredCond.
empty())
1612 if ((Opcode != Hexagon::PS_jmpret) && (Opcode != Hexagon::L4_return))
1614 MachineInstr *NewMI = FirstTerm;
1628 const MachineInstr *MI2Clone = TBBMIb;
1638 NewMI->
setDesc(QII->get(NewOpcode));
1643 !
TBB->hasAddressTaken() &&
TBB->pred_empty() &&
TBB->succ_empty())
1657 if (FBBMIb->
getOpcode() == Hexagon::J2_jump &&
1661 (FBB == NewTarget ) || (FBB->
pred_size() != 1))
1673 <<
" out of range.");
1688void GlobalScheduler::performExposedOptimizations() {
1692 MachineBasicBlock *
MBB = &*
MBBI;
1699 if (AnalyzeBranchInfo.hasABInfo(*pi))
1700 AnalyzeBranchInfo.invalidate(*pi);
1702 if (AnalyzeBranchInfo.hasABInfo(
MBB))
1703 AnalyzeBranchInfo.invalidate(
MBB);
1704 removeEmptyMBB(
MBB);
1708 MachineBasicBlock *
MBB = &*
MBBI;
1712 if (std::find(IgnorePostSchedOpt.begin(), IgnorePostSchedOpt.end(),
MBB) !=
1713 IgnorePostSchedOpt.end())
1716 ABI &abi = AnalyzeBranchInfo.getOrCreate(
MBB, QII,
true);
1717 if (!abi.AnalysisValid)
1719 MachineBasicBlock *
TBB = abi.TBB, *FBB = abi.FBB;
1720 MachineInstr *FirstTerm = abi.FirstTerm, *SecondTerm = abi.SecondTerm;
1723 if (
TBB && FirstTerm &&
1724 removeRedundantBranches(
MBB,
TBB, FirstTerm, FBB, SecondTerm)) {
1727 }
else if (FirstTerm && SecondTerm &&
1728 optimizeDualJumps(
MBB,
TBB, FirstTerm, FBB, SecondTerm)) {
1730 }
else if (
TBB && FBB && FirstTerm && !SecondTerm) {
1731 MachineBasicBlock *BBToErase = optimizeBranches(
MBB,
TBB, FirstTerm, FBB);
1733 if (AnalyzeBranchInfo.hasABInfo(BBToErase))
1734 AnalyzeBranchInfo.invalidate(BBToErase);
1745 AnalyzeBranchInfo.invalidate(
MBB);
1766 if (((MBBPredSize > 1) && ((MBBSuccSize == 1) || (MBBSuccSize == 2))) ||
1767 (MBBSuccSize == 2)) {
1768 SchedulingRegion_t SR;
1769 SR.addSRMember(&*
MBBI);
1779 SchedulingRegions_t::iterator SRI = SchedulingRegions.
begin();
1780 SchedulingRegions_t::iterator SRIPrev = SchedulingRegions.
end();
1781 LLVM_DEBUG(
dbgs() <<
"\nConstructing Scheduling Region for HEADs at";);
1782 while (SRI != SchedulingRegions.
end()) {
1783 MachineBasicBlock *RegHead = SRI->Members[0];
1791 if ((SRIPrev != SchedulingRegions.
end()) &&
1792 (SRIPrev->Members[0] == SRI->Members[0]))
1798 bool SuccIsSingleBlockLoop =
1799 isSuperblockLoop(L) && RegMBB->getNumber() != RegHead->
getNumber();
1802 if ((RegMBB->pred_size() > 1 && !SuccIsSingleBlockLoop) ||
1803 (RegMBB->succ_empty() && RegMBB->empty()))
1805 SRI->addSRMember(&*RegMBB);
1807 if (RegMBB->succ_size() != 1) {
1810 RegMBB = (*RegMBB->succ_begin())->getIterator();
1817void GlobalScheduler::pruneSchedulingRegions(PruneCriteriaFP PC) {
1818 LLVM_DEBUG(
dbgs() <<
"\nErasing Scheduling Region with HEAD at";);
1819 for (SchedulingRegions_t::iterator SRI = SchedulingRegions.
begin();
1820 SRI != SchedulingRegions.
end();) {
1821 if ((this->*PC)(*SRI)) {
1823 SRI = SchedulingRegions.
erase(SRI);
1830void GlobalScheduler::sortSchedulingRegions() {
1831 for (SchedulingRegions_t::iterator SRI = SchedulingRegions.
begin();
1832 SRI != SchedulingRegions.
end(); ++SRI) {
1833 SchedulingRegion_t &SR = *SRI;
1836 SR.EdgeProb = MBPI->getEdgeProbability(SR.Members[0], SR.Members[1]);
1837 if (SR.EdgeProb.getCompl() == SR.EdgeProb) {
1838 ABI &abi = AnalyzeBranchInfo.getOrCreate(SR.Members[0], QII);
1839 if (!abi.AnalysisValid)
1842 if (abi.TBB == SR.Members[1])
1843 SR.EdgeProb = BranchProbability(2, 3);
1845 SR.EdgeProb = BranchProbability(1, 3);
1848 SR.EdgeProb.dump(););
1851 std::sort(SchedulingRegions.
begin(), SchedulingRegions.
end(),
1852 std::greater<SchedulingRegions_t::value_type>());
1856GlobalScheduler::addToPacketPseudo(MachineInstr *
MI) {
1860 if (!
TII->isZeroCost(
MI->getOpcode()))
1861 ResourceTracker->reserveResources(*
MI);
1863 assert(canReserveResourcesForConstExt());
1864 reserveResourcesForConstExt();
1866 CurrentPacketMIs.push_back(
MI);
1870void GlobalScheduler::initPacketizerStateLoc() {
1872 CurrentPacketMIs.clear();
1876bool GlobalScheduler::isLegalToPacketizeWith(MIIterType DestPack,
1879 assert(DestPack->isBundle());
1881 if (isSoloInstruction(*
MI))
1886 MIInstIterType BI = (&*DestPack)->getIterator();
1888 const MachineInstr *BranchMI = NULL;
1890 for (++BI; (BI !=
MBB->
instr_end()) && BI->isInsideBundle(); ++BI) {
1894 if (!isLegalToPacketizeTogetherMI(&*BI,
MI))
1905 const MachineOperand &MO = BranchMI->
getOperand(i);
1909 LLVM_DEBUG(
dbgs() <<
"\nBranch might be constant extended later";);
1919bool GlobalScheduler::isLegalToPacketizeTogetherMI(MachineInstr *BI,
1921 if (cannotCoexist(*BI, *
MI))
1924 if (hasDeadDependence(*BI, *
MI))
1926 if (hasControlDependence(*BI, *
MI))
1928 if (hasDualStoreDependence(*BI, *
MI))
1967 (
MI->mayStore() ||
MI->getOpcode() == Hexagon::S2_allocframe ||
1985 if ((QII->
isMemOp(*BI) &&
MI->mayStore()) ||
1987 LLVM_DEBUG(
dbgs() <<
"\nSlot 0 not available for store because of memop.");
1998MachineInstr *GlobalScheduler::generateCompoundIfPossible(MIInstIterType MII,
2000 MIInstIterType &GB) {
2002 MachineInstr *NewMI = NULL;
2007 while ((MII !=
MBB->
instr_end()) && MII->isInsideBundle()) {
2008 if (MII->isCompare() &&
2016 if (MII->isBranch() &&
2029 if (CmpOpcode != -1) {
2031 assert(GA->getNumOperands() == 3);
2033 NewMI =
BuildMI(*
MBB, GB, dl, QII->get(CmpOpcode));
2034 Register PredReg = GA->getOperand(0).getReg();
2035 MachineOperand *PredUse =
2038 "Missing compound predicate use");
2046 SafetyBufferDelta)) {
2057void GlobalScheduler::generateCompoundsPostSched() {
2058 DenseSet<MachineInstr *>::iterator it = ToBeSqueezed.
begin();
2059 while (it != ToBeSqueezed.
end()) {
2062 MachineInstr *NewMI =
2063 generateCompoundIfPossible((*it)->getIterator(), GA, GB);
2065 assert(NewMI &&
"Wrong Analysis of compound packets.");
2066 GA->eraseFromBundle();
2067 GB->eraseFromBundle();
2090bool GlobalScheduler::failsSpecialConditions(MachineInstr *BundleMI,
2093 if (!BundleMI || !BundleMI->
getParent())
2098 MachineInstr &
MI = *
E;
2099 auto OpC =
MI.getOpcode();
2101 if (QII->
isEndLoopN(OpC) && DestReg == Hexagon::P3)
2104 if ((OpC == Hexagon::C2_tfrrp) &&
MI.getNumOperands() > 0 &&
2105 (DestReg ==
MI.getOperand(0).getReg()))
2108 if ((OpC == Hexagon::S2_cabacdecbin) ||
2111 MI.getNumOperands() > 0 &&
MI.getOperand(0).getReg() == DestReg) ||
2112 ((OpC == Hexagon::S2_storew_locked ||
2113 OpC == Hexagon::S4_stored_locked || OpC == Hexagon::A6_vminub_RdP ||
2114 OpC == Hexagon::A5_ACS || OpC == Hexagon::F2_sfinvsqrta ||
2115 OpC == Hexagon::F2_sfrecipa) &&
2116 MI.getNumOperands() > 1 &&
MI.getOperand(1).getReg() == DestReg) ||
2117 ((OpC == Hexagon::A4_addp_c || OpC == Hexagon::A4_subp_c) &&
2118 MI.getNumOperands() > 2 &&
MI.getOperand(3).getReg() == DestReg))
2128bool GlobalScheduler::canNewifyPredReg(
unsigned Reg, MICInstIterType Producer,
2132 if (
TRI->getMinimalPhysRegClass(
Reg) != &Hexagon::PredRegsRegClass)
2141bool GlobalScheduler::getDefinitions(MachineInstr *
MI, MachineInstr *&Def1,
2142 MachineInstr *&Def2, MachineInstr *&Def3) {
2145 if (
MI->getNumOperands() < 3)
2150 (
MI->getNumOperands() == 4) ?
MI->getOperand(3).getReg() :
Register(0);
2154 for (
auto DefSearch = std::prev(
MI->getIterator());
2156 if (DefSearch->isBundle())
2160 if (DefIter == MIUseDefs.
end())
2163 const BitVector &Defs = DefIter->second.second;
2168 if (Reg3 && Defs[Reg3])
2171 if (Def1 && Def2 && (!Reg3 || (Reg3 && Def3)))
2182 SmallVector<MachineInstr *, 4> ToBeErased;
2183 for (
auto &
MBB : Fn) {
2185 unsigned OpC = MII->getOpcode();
2187 if (OpC != Hexagon::C2_and && OpC != Hexagon::C2_andn &&
2188 OpC != Hexagon::C4_and_and && OpC != Hexagon::C4_and_andn)
2191 Register DestPred = MII->getOperand(0).getReg();
2192 Register Reg1 = MII->getOperand(1).getReg();
2193 Register Reg2 = MII->getOperand(2).getReg();
2194 auto Reg3 = MII->getNumOperands() == 4 ? MII->getOperand(3).getReg()
2200 if (Reg3 != 0 && Reg3 == Reg2)
2205 if (!MII->getOperand(1).isKill() || !MII->getOperand(2).isKill() ||
2206 (Reg3 != 0 && !MII->getOperand(3).isKill()))
2213 bool UsedByDotNew =
false;
2216 for (
auto BundledII = AndBundleStart; BundledII != AndBundleEnd;
2218 if (&*BundledII == &*MII)
2220 int UseIdx = BundledII->findRegisterUseOperandIdx(DestPred,
TRI);
2222 UsedByDotNew =
true;
2230 MachineInstr *Def1 =
nullptr;
2231 MachineInstr *Def2 =
nullptr;
2232 MachineInstr *Def3 =
nullptr;
2233 if (!getDefinitions(&*MII, Def1, Def2, Def3))
2250 MICInstIterType
Use;
2251 SmallPtrSet<MachineInstr *, 2>
Exception;
2255 if (HLV->
isUsedWithin(MIStart, MIEnd, Reg1, Use, &Exception) ||
2256 HLV->
isUsedWithin(MIStart, MIEnd, Reg2, Use, &Exception) ||
2257 (Def3 && HLV->
isUsedWithin(MIStart, MIEnd, Reg3, Use, &Exception)))
2261 if (failsSpecialConditions(&*MIStart, DestPred))
2267 std::optional<unsigned> NewOpC;
2271 case Hexagon::C2_and:
2280 case Hexagon::C2_andn:
2285 Def2->
setDesc(QII->get(NewOpC.value()));
2291 case Hexagon::C4_and_and:
2299 case Hexagon::C4_and_andn:
2304 Def3->
setDesc(QII->get(NewOpC.value()));
2319 for (MachineInstr *It : ToBeErased) {
2320 if (It->isBundled()) {
2322 It->eraseFromBundle();
2325 It->eraseFromParent();
2329void GlobalScheduler::packetize() {
2330 DenseSet<unsigned> DoneMBBs;
2331 HexagonPacketizerList
Packetizer(MF, MLI, AA, MBPI,
false);
2335 for (; PacketOpp != SpecPacketOpp.
end(); ++PacketOpp) {
2341 for (; RegionBeg !=
MBB->
end(); RegionBeg = RegionEnd) {
2344 while (RegionBeg !=
MBB->
end() && RegionBeg->isBundled())
2346 if (RegionBeg ==
MBB->
end())
2348 RegionEnd = std::next(RegionBeg);
2349 while (RegionEnd !=
MBB->
end() && !RegionEnd->isBundled())
2352 if (RegionBeg == std::prev(RegionEnd))
2362void GlobalScheduler::getResourcesReadyForBundle(MIInstIterType MII,
2364 MachineInstr *InstrToSkip) {
2367 bool GeneratedCompoundInstr =
false;
2368 MachineInstr *NewMI = NULL;
2372 MachineInstr *GAPos, *GBPos;
2377 NewMI = generateCompoundIfPossible(MII, GA, GB);
2378 if (NewMI != NULL) {
2379 GeneratedCompoundInstr =
true;
2380 MIInstIterType GANext = std::next(GA);
2381 MIInstIterType GBNext = std::next(GB);
2382 GAPos = GANext !=
MBB->
instr_end() ? &*GANext :
nullptr;
2383 GBPos = GBNext !=
MBB->
instr_end() ? &*GBNext :
nullptr;
2384 GA = GA->removeFromBundle()->getIterator();
2385 GB = GB->removeFromBundle()->getIterator();
2389 initPacketizerStateLoc();
2390 ResourceTracker->clearResources();
2395 MIInstIterType SavedMII = MII;
2396 auto &
ST = MII->getParent()->getParent()->getSubtarget<HexagonSubtarget>();
2397 if (
ST.isTinyCoreWithDuplex())
2400 while ((MII !=
MBB->
instr_end()) && MII->isInsideBundle()) {
2401 assert(ResourceTracker->canReserveResources(*MII) ||
2402 TII->isZeroCost(MII->getOpcode()));
2403 if (!(InstrToSkip && InstrToSkip == &*MII)) {
2404 addToPacketPseudo(&*MII);
2410 if (
ST.isTinyCoreWithDuplex())
2413 if (GeneratedCompoundInstr) {
2417 if (!GB->isBundled())
2418 GB->bundleWithPred();
2422 NewMI->eraseFromBundle();
2428bool GlobalScheduler::canReserveResources(MIInstIterType MII)
const {
2431 return ResourceTracker->canReserveResources(*MII);
2436void GlobalScheduler::replaceUsageOfReg(
unsigned Reg,
unsigned ScratchReg,
2437 MachineBasicBlock *
MBB,
2438 const MachineInstr *ExceptMI)
const {
2441 MII != MIE; ++MII) {
2442 MachineInstr *
MI = &*MII;
2445 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
2446 MachineOperand &MO =
MI->getOperand(i);
2453void GlobalScheduler::markLiveInsAsUsed(std::unique_ptr<RegScavenger> &RS,
2454 MachineBasicBlock *
MBB)
const {
2457 RS->setRegUsed((*lit).PhysReg);
2462void GlobalScheduler::markLiveOutsAsUsed(std::unique_ptr<RegScavenger> &RS,
2463 MachineBasicBlock *
MBB)
const {
2465 int Reg =
LO.find_first();
2467 RS->setRegUsed(
Reg);
2473void GlobalScheduler::markAllRegsAsUsed(std::unique_ptr<RegScavenger> &RS,
2474 const MachineBasicBlock *
MBB)
const {
2479 BitVector MBBUseDef(
TRI->getNumRegs());
2481 if (
I->isBundle() ||
I->isDebugInstr())
2485 MBBUseDef |= MIUseDef->second.first;
2486 MBBUseDef |= MIUseDef->second.second;
2488 int Reg = MBBUseDef.find_first();
2490 RS->setRegUsed(
Reg);
2492 Reg = MBBUseDef.find_next(
Reg);
2506 if ((*pi)->isLayoutSuccessor(ToBB))
2511MachineBasicBlock *GlobalScheduler::splitCriticalEdge(MachineBasicBlock *FromBB,
2512 MachineBasicBlock *ToBB,
2513 MachineBasicBlock *LP) {
2529 if ((LP != FromBB) &&
2530 (Jumpers.
empty() || !Jumpers[0]->isUnconditionalBranch())) {
2532 MachineInstr *JumpMI =
2542 MachineInstr *JumpMI =
2547 if (AnalyzeBranchInfo.hasABInfo(LP))
2548 AnalyzeBranchInfo.invalidate(LP);
2549 if (AnalyzeBranchInfo.hasABInfo(FromBB))
2550 AnalyzeBranchInfo.invalidate(FromBB);
2555bool GlobalScheduler::hasOneDef(
const MachineInstr *
MI,
unsigned &DefR)
const {
2559 if (MIUseDef == MIUseDefs.
end())
2561 const BitVector &Defs = MIUseDef->second.second;
2562 unsigned DefsCount = Defs.
count();
2570 return !MCSubRegIterator(DefR,
TRI,
false).isValid();
2574bool GlobalScheduler::hasOneUse(
const MachineInstr *
MI,
unsigned &UseR)
const {
2578 if (MIUseDef == MIUseDefs.
end())
2580 const BitVector &
Uses = MIUseDef->second.first;
2581 unsigned UsesCount =
Uses.count();
2585 UseR =
Uses.find_first();
2587 return !MCSubRegIterator(UseR,
TRI,
false).isValid();
2592bool GlobalScheduler::hasOneDefAndUse(
const MachineInstr *
MI,
unsigned &Def,
2593 unsigned &Use)
const {
2594 return hasOneDef(
MI, Def) && hasOneUse(
MI, Use);
2600MICInstIterType GlobalScheduler::laterOfTheTwo(MICInstIterType I1,
2605 MICInstIterType Temp =
I1;
2616int GreedyPullup::dstDependence(MICInstIterType DestBeg,
2617 MICInstIterType DestEnd, MICInstIterType MII,
2618 std::vector<const MachineInstr *> &DepTrueVec,
2619 std::vector<const MachineInstr *> &DepAntiVec,
2620 std::vector<const MachineInstr *> &DepOutputVec,
2621 std::vector<unsigned> *Regs)
const {
2623 if (DestBeg == DestEnd)
2626 unsigned dep = Dependency::Independent;
2628 SmallVector<unsigned> UseRegs;
2629 SmallVector<unsigned> DefRegs;
2630 for (
const MachineOperand &MO : MII->operands()) {
2634 else if (MO.
isDef())
2639 bool addedTrueDep =
false, addedFalseDep =
false;
2644 if (!
I->isBundle()) {
2645 if (shouldNotBeCrossed(&*
I, &*MII)) {
2646 dep = Dependency::Unknown;
2649 if (
I->isDebugInstr())
2651 for (
auto RegI = UseRegs.
begin(), RegE = UseRegs.
end(); RegI != RegE;
2653 for (MCRegAliasIterator AI(*RegI,
TRI,
true); AI.isValid(); ++AI) {
2654 if (MIUseDefs[&*
I].second[*AI]) {
2655 DepTrueVec.push_back(&*
I);
2657 Regs->push_back(*RegI);
2658 addedTrueDep =
true;
2659 dep |= Dependency::RAW;
2665 UseRegs.
erase(RegI);
2666 addedTrueDep =
false;
2671 for (
auto RegI = DefRegs.
begin(), RegE = DefRegs.
end(); RegI != RegE;
2673 for (MCRegAliasIterator AI(*RegI,
TRI,
true); AI.isValid(); ++AI) {
2674 if (MIUseDefs[&*
I].second[*AI]) {
2675 addedFalseDep =
true;
2676 dep |= Dependency::WAW;
2677 DepOutputVec.push_back(&*
I);
2682 for (MCRegAliasIterator AI(*RegI,
TRI,
true); AI.isValid(); ++AI) {
2683 if (MIUseDefs[&*
I].first[*AI]) {
2684 DepAntiVec.push_back(&*
I);
2685 dep |= Dependency::WAR;
2690 if (addedFalseDep) {
2691 DefRegs.
erase(RegI);
2692 addedFalseDep =
false;
2701 }
while (
I != DestBeg);
2718int GreedyPullup::dstDependence(MICInstIterType DestBeg,
2719 MICInstIterType DestEnd, MICInstIterType MII,
2720 MICInstIterType &DepTrue,
2721 MICInstIterType &DepAnti,
unsigned HLXUseReg,
2722 bool IsPullDown)
const {
2724 const MachineInstr *
MI = &*MII;
2725 const MachineBasicBlock *SrcMBB = MII->getParent();
2727 MICInstIterType DepTrueP = SrcMBB->
instr_end(),
2733 assert(!shouldNotBePulledUp(&*MII));
2734 MICInstIterType DestBundle =
2736 MICInstIterType MIBegin = DestBeg;
2737 bool IsMIInDestBundle =
false;
2738 for (; MIBegin != DestEnd; ++MIBegin) {
2741 IsMIInDestBundle =
false;
2742 if (!IsPullDown && DestBundle->isBundle() && MIBegin->isBundled())
2744 if (!shouldNotBeCrossed(&*MIBegin, &*MII, IsMIInDestBundle))
2747 return Dependency::Unknown;
2751 if (MII->isBranch() || MII->isTerminator() || MII->isReturn()) {
2756 if (SrcBundle->getBundleSize() != 1 || SrcMBB != DestBeg->getParent() ||
2757 (laterOfTheTwo(MII, DestBeg, SrcMBB->
instr_end()) == DestBeg) ||
2760 return Dependency::Unknown;
2763 unsigned dep = Dependency::Independent;
2765 auto CheckDep = [&](
const MachineOperand &MO,
unsigned Reg) ->
void {
2770 bool destIsBundle = (DestBeg->isBundle() || DestBeg->isBundledWithPred());
2775 bool depInDestBundle =
2781 bool regularDef = !tmpDef;
2782 if (depInDestBundle &&
2785 regularDef |= !tmpDef;
2788 bool hasHLXDep = depInDestBundle && (HLXUseReg ==
Reg);
2796 dep |= Dependency::RAW;
2798 if (hasHLXDep && !regularDef)
2801 dep |= Dependency::WAW;
2803 assert(DepTrueP != DestEnd);
2810 MICInstIterType LastII = DestEnd;
2812 bool depInDestBundle =
2814 const MachineInstr *DepMI = &*DepAntiP;
2815 int UseIdx = DepMI->findRegisterUseOperandIdx(
Reg,
TRI);
2818 bool DefViaRegMask = !MO.
isReg();
2822 dep |= Dependency::WAR;
2823 assert(DepAntiP != DestEnd);
2829 dep |= Dependency::WAR_SameBundle;
2836 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
2837 const MachineOperand &MO =
MI->getOperand(i);
2840 if (MCSubRegIterator(MO.
getReg(),
TRI,
false).isValid()) {
2844 CheckDep(MO, MO.
getReg());
2846 for (
unsigned R = 1, NR =
TRI->getNumRegs(); R != NR; ++R)
2858int GreedyPullup::srcDependence(MIInstIterType SrcBeg, MIInstIterType SrcEnd,
2859 MIInstIterType RMII,
2860 SmallPtrSetImpl<MachineInstr *> *DepSet)
const {
2866 PreventDuplex,
false);
2869bool GreedyPullup::singleTrueDep(MICInstIterType SrcBeg, MICInstIterType SrcEnd,
2870 MICInstIterType MII,
unsigned &
Reg)
const {
2871 const MachineInstr *
MI = &*MII;
2873 MICInstIterType DepTrueP = SrcEnd;
2874 for (
unsigned i = 0; i <
MI->getNumOperands(); ++i) {
2875 const MachineOperand &MO =
MI->getOperand(i);
2889MIIterType GreedyPullup::findInsertionPtSpecLocal(MachineInstr *
MI) {
2899 if (
MI->isBundled()) {
2902 unsigned SrcDep = srcDependence(ParentBundle.first, ParentBundle.second,
2905 if (!Dependency::hasOnly(SrcDep, Dependency::Independent))
2913 MIIterType InsPos = findInsertPtSpec(
MI,
MBB,
false,
false);
2921void GreedyPullup::localPullupHVX(SchedulingRegion_t::SRAtom_t *SR) {
2930 if (!isSuperblockLoop(L))
2942 localPullupImpl(SR,
true);
2948void GreedyPullup::genLoadBypass(MachineInstr *LoadInstr,
2949 MachineInstr *InstrToMove) {
2953 LoadInstr->
setDesc(QII->get(CurOpcode));
2954 MIBundleBuilder Bundle(DestBundle);
2955 Bundle.insert(std::next(LoadInstr->
getIterator()), InstrToMove);
2957 for (
auto &MO : InstrToMove->
operands()) {
2962 for (MCRegAliasIterator AI(MO.
getReg(),
TRI,
true); AI.isValid(); ++AI) {
2976bool GreedyPullup::canGenCurBypass(MIInstIterType DestBundle,
2977 MachineInstr *DepTrue, MIInstIterType PktEnd,
2978 int DstDep, MachineInstr *Consumer,
2979 const MachineInstr *Exempt) {
2980 assert(Consumer &&
"Consumer is required for .cur bypass correctness checks");
2981 if (!DepTrue || Dependency::has(DstDep, Dependency::WAW))
2990 if (DestBundle->isBundle() && !DepTrue->
isBundled())
2998 for (
const MachineOperand &MOP :
MI.operands()) {
2999 if (MOP.isReg() && MOP.isUse() && !MOP.isDebug() && MOP.getReg() &&
3000 !MOP.isUndef() &&
TRI->regsOverlap(MOP.getReg(), DestReg))
3008 for (
const MachineOperand &Def : DepTrue->
operands()) {
3009 if (!
Def.isReg() || !
Def.isDef() || !
Def.getReg() ||
3010 Def.getReg() == DestReg)
3012 for (
const MachineOperand &Use : Consumer->
operands()) {
3013 if (
Use.isReg() &&
Use.isUse() && !
Use.isUndef() &&
Use.getReg() &&
3014 TRI->regsOverlap(
Use.getReg(),
Def.getReg()))
3023 for (MIInstIterType
MI = std::next(DestBundle);
3027 if (&*
MI == DepTrue || &*
MI == Exempt ||
MI->isBundle())
3029 for (
const MachineOperand &Def :
MI->operands()) {
3030 if (!
Def.isReg() || !
Def.isDef() || !
Def.getReg())
3032 bool ConsumerReads =
3034 return Use.isReg() && Use.isUse() && !Use.isUndef() &&
3035 Use.getReg() && TRI->regsOverlap(Use.getReg(), Def.getReg());
3045bool GreedyPullup::canCauseInterlockStall(MIInstIterType DestBundle,
3046 MachineInstr *SrcMI,
3047 const InstrItineraryData *
II) {
3049 auto PrevEnd = std::prev(DestBundle);
3051 MachineInstr *PrevStart =
nullptr;
3052 if (PrevEnd !=
MBB->
end()) {
3053 if (PrevEnd->isBundled())
3056 PrevStart = &*PrevEnd;
3059 if (PrevEnd !=
MBB->
end() && PrevStart !=
nullptr) {
3060 std::vector<const MachineInstr *> DepTruePrevVec, DepAntiPrevVec,
3062 std::vector<unsigned> TrueRegs;
3063 int dep = dstDependence(PrevStart->
getIterator(), DestBundle,
3065 DepAntiPrevVec, DepOutputPrevVec, &TrueRegs);
3066 if (Dependency::has(dep, Dependency::RAW)) {
3067 unsigned RegIdx = 0;
3068 for (
auto DepTruePrev : DepTruePrevVec) {
3069 int DefIdx = -1, UseIdx = -1;
3070 for (MCRegAliasIterator AI(TrueRegs[RegIdx],
TRI,
true);
3071 AI.isValid() && DefIdx == -1; ++AI)
3072 DefIdx = DepTruePrev->findRegisterDefOperandIdx(*AI,
nullptr);
3073 for (MCRegAliasIterator AI(TrueRegs[RegIdx],
TRI,
true);
3074 AI.isValid() && UseIdx == -1; ++AI)
3076 std::optional<unsigned> OpLatency =
3083 *SrcMI,
const_cast<MachineInstr &
>(*DepTruePrev),
false,
3098void GreedyPullup::localPullupHLXLoadUses(SchedulingRegion_t::SRAtom_t *SR,
3099 MachineInstr *HLXLoadMI,
3100 MachineInstr *HLXCopyMI) {
3105 MIInstIterType DestBundle = BI.getInstrIterator();
3107 unsigned NumLoadUsesPullups = 0;
3109 SmallVector<MachineInstr *, 4> LoadUses;
3111 if (!HLXCopyMI || !HLXLoadDestReg ||
3115 dbgs() <<
"\nFinding uses of HLX load for pullup into: "
3118 MIInstIterType LoadBundleEnd =
getBundleEnd(DestBundle);
3120 if (LoadBundleEnd ==
MBB->
end())
3126 while (HLV->
isDefinedWithin(LoadBundleEnd, NextLoadRegDef, HLXLoadDestReg,
3128 NextLoadRegDef = DepTrueP;
3129 MICInstIterType PrevLoadRegUse = NextLoadRegDef;
3134 while (HLV->
isUsedWithin(LoadBundleEnd, PrevLoadRegUse, HLXLoadDestReg,
3137 LoadUses.
push_back(&*DepTrueP.getNonConst());
3138 PrevLoadRegUse = DepTrueP;
3142 bool LoadRegIsLiveOut = HLV->
isLiveOut(
MBB, HLXLoadDestReg) &&
3149 bool BundleHasCallUse =
3154 for (MachineInstr *SrcMI :
reverse(LoadUses)) {
3162 std::pair<MIInstIterType, MIInstIterType> ParentBundle =
3166 if (!isLegalToPacketizeWith(BI, SrcMI) || isSoloInstruction(*SrcMI) ||
3167 shouldNotBePulledUp(SrcMI))
3170 int DstDep = dstDependence(DestBundle, ParentBundle.first, SrcMII, DepTrue,
3171 DepAnti, HLXLoadDestReg);
3172 int SrcDep = srcDependence(ParentBundle.first, ParentBundle.second, SrcMII);
3173 MIInstIterType DestPos = DestBundle;
3174 getResourcesReadyForBundle(DestPos);
3176 if ((!Dependency::hasOnly(DstDep, Dependency::Independent) ||
3177 !canReserveResources(SrcMII)) &&
3178 !LoadRegIsLiveOut && (NumLoadUsesPullups == (LoadUses.
size() - 1)) &&
3179 SrcMI == LoadUses.
front() && !BundleHasCallUse) {
3180 LLVM_DEBUG(
dbgs() <<
"\nAttempting to pullup last use by erasing copy: ";
3181 HLXCopyMI->
dump(););
3183 DstDep = dstDependence(DestBundle, ParentBundle.first, SrcMII, DepTrue,
3184 DepAnti, HLXLoadDestReg);
3185 getResourcesReadyForBundle(DestPos);
3188 if (!(Dependency::hasOnly(DstDep, Dependency::Independent)) ||
3189 !(Dependency::hasOnly(SrcDep, Dependency::Independent)))
3192 if (!canReserveResources(SrcMII)) {
3197 MachineInstr *InstrToMove =
removeMI(SrcMI, QII,
nullptr);
3199 MachineOperand *
Op =
nullptr;
3200 for (MCRegAliasIterator AI(HLXLoadDestReg,
TRI,
true); AI.isValid(); ++AI) {
3201 Op = InstrToMove->findRegisterUseOperand(*AI,
nullptr);
3205 assert(
Op &&
"Could not find use operand!");
3206 Op->setIsInternalRead();
3211 MIBundleBuilder Bundle(&*DestBundle);
3212 Bundle.insert(std::next(HLXLoadMI->
getIterator()), InstrToMove);
3217 ++NumLoadUsesPullups;
3219 <<
"\nMBB: " << *InstrToMove->getParent());
3225 if (NumLoadUsesPullups != LoadUses.
size() || LoadRegIsLiveOut ||
3228 if (!HLXCopyMI->isBundled()) {
3230 MIBundleBuilder Bundle(&*DestBundle);
3231 Bundle.insert(std::next(HLXLoadMI->
getIterator()), HLXCopyMI);
3236 if (HLXCopyMI->isBundled())
3237 HLXCopyMI->eraseFromBundle();
3239 MF.deleteMachineInstr(HLXCopyMI);
3246void GreedyPullup::localPullupHLXStore(SchedulingRegion_t::SRAtom_t *SR,
3247 MachineInstr *HLXStoreMI,
3248 MachineInstr *HLXCopyMI) {
3253 MIInstIterType SrcBundle = BI.getInstrIterator();
3257 MachineInstr *DestMI =
nullptr;
3259 if (!HLXCopyMI || !HLXStoreSrcReg ||
3263 dbgs() <<
"\nFinding def for HLX store to be pulled up from: "
3270 HLXStoreSrcReg, DepTrueP))
3271 DestMI = &*DepTrueP.getNonConst();
3279 std::pair<MICInstIterType, MICInstIterType> ParentBundle =
3283 LLVM_DEBUG(
dbgs() <<
"\nChecking Resources for: " << *HLXStoreMI;);
3285 if (!isLegalToPacketizeWith(&*DestPos, HLXStoreMI))
3289 dstDependence(ParentBundle.first, SrcBundle, HLXStoreMI->
getIterator(),
3290 DepTrue, DepAnti, HLXStoreSrcReg);
3296 if (!Dependency::hasOnly(DstDep, Dependency::Independent))
3299 getResourcesReadyForBundle(DestPos);
3300 if (!canReserveResources(HLXStoreMI->
getIterator())) {
3308 MachineInstr *InstrToMove =
removeMI(HLXStoreMI, QII,
nullptr);
3312 MIBundleBuilder Bundle(&*(DestPos));
3313 Bundle.insert(std::next(DestMII), InstrToMove);
3324 <<
"\nMBB: " << *InstrToMove->getParent());
3333void GreedyPullup::localPullupHLX(SchedulingRegion_t::SRAtom_t *SR) {
3338 LLVM_DEBUG(
dbgs() <<
"\n\n\t\t[LocalPullupHLX]\tBB#" << SR->getNumber(););
3341 for (MIIterType BI = SR->begin(); BI != SR->end(); ++BI) {
3343 if (BI->isDebugInstr())
3347 if (!BI->isBundle() || (!BI->mayLoad() && !BI->mayStore()))
3349 MachineInstr *HLXLoadMI =
nullptr;
3350 MachineInstr *HLXStoreMI =
nullptr;
3351 SmallVector<MachineInstr *, 2> HLXCopies;
3352 MIInstIterType MII = BI.getInstrIterator();
3354 (MII != SR->instr_end()) && MII->isInsideBundle(); ++MII) {
3357 switch (MII->getOpcode()) {
3358 case Hexagon::LX_L16b_tmp:
3361 case Hexagon::LX_S16b_new:
3364 case Hexagon::LX_assign:
3365 if (MII->getOperand(0).getReg() == MII->getOperand(1).getReg()) {
3376 MachineInstr *HLXLoadCopyMI =
nullptr;
3377 MachineInstr *HLXStoreCopyMI =
nullptr;
3378 for (MachineInstr *CopyMI : HLXCopies) {
3379 Register HLXCopyReg = CopyMI->getOperand(0).getReg();
3382 bool IsInternal = CopyMI->getOperand(1).isInternalRead();
3385 if (HLXStoreMI && !HLXStoreCopyMI && !IsInternal &&
3387 HLXStoreCopyMI = CopyMI;
3388 else if (HLXLoadMI && !HLXLoadCopyMI &&
3390 HLXLoadCopyMI = CopyMI;
3393 HLXLoadPseudos.
push_back(std::make_pair(HLXLoadMI, HLXLoadCopyMI));
3395 HLXStorePseudos.
push_back(std::make_pair(HLXStoreMI, HLXStoreCopyMI));
3398 for (
auto &LoadPseudo : HLXLoadPseudos)
3399 localPullupHLXLoadUses(SR, LoadPseudo.first, LoadPseudo.second);
3401 for (
auto &StorePseudo : HLXStorePseudos)
3402 localPullupHLXStore(SR, StorePseudo.first, StorePseudo.second);
3418bool GreedyPullup::tryTwoLoadCurBypass(
3419 MIIterType &BI, SchedulingRegion_t::SRAtom_t *SR,
3420 MIInstIterType &DestBundle, MIIterType &SrcBundle, MIIterType &NextBI,
3421 bool isNextBISrc, MIIterType &
Next, MachineInstr *SrcMI,
3422 SmallPtrSetImpl<MachineInstr *> &PulledUpSet) {
3424 std::pair<MIInstIterType, MIInstIterType> ParentBundle =
3429 std::vector<const MachineInstr *> DepTrueVec, DepAntiVec, DepOutputVec;
3430 int VecDstDep = dstDependence(DestBundle, ParentBundle.first, SrcMII,
3431 DepTrueVec, DepAntiVec, DepOutputVec);
3433 LLVM_DEBUG(
dbgs() <<
"\n[TwoLoadCur] Candidate consumer: " << *SrcMI
3434 <<
"\t\tRAW producers=" << DepTrueVec.size()
3435 <<
" output deps=" << DepOutputVec.size() <<
"\n");
3437 if (!Dependency::has(VecDstDep, Dependency::RAW) || DepTrueVec.size() > 2) {
3438 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: no RAW dep or >2 producers.\n");
3442 bool GenCur =
false;
3443 bool PotentialGenCur =
false;
3444 const MachineInstr *DepTrue =
nullptr;
3445 const MachineInstr *
Other =
nullptr;
3447 auto IsCurLoad = [&](
const MachineInstr *
MI) {
3451 if (DepTrueVec.size() == 1) {
3452 const MachineInstr *
V0 = DepTrueVec[0];
3453 if (!Dependency::has(VecDstDep, Dependency::WAW) && IsCurLoad(V0) &&
3458 }
else if (DepTrueVec.size() == 2 && DepOutputVec.size() <= 1) {
3459 const MachineInstr *
V0 = DepTrueVec[0];
3460 const MachineInstr *
V1 = DepTrueVec[1];
3463 if (IsCurLoad(V0)) {
3466 }
else if (IsCurLoad(
V1)) {
3470 if (
Other && (!Dependency::has(VecDstDep, Dependency::WAW) ||
3471 Other == DepOutputVec[0]))
3472 PotentialGenCur =
true;
3476 if (!GenCur && !PotentialGenCur)
3480 << (DepTrueVec.size() == 1 ?
"single-load" :
"two-load")
3481 <<
" case: GenCur=" << GenCur
3482 <<
" PotentialGenCur=" << PotentialGenCur <<
"\n");
3485 if (GenCur && DestBundle->isBundle()) {
3496 for (MIInstIterType
MI = std::next(DestBundle);
3497 MI != NextBI.getInstrIterator(); ++
MI) {
3500 bool Reads =
llvm::any_of(
MI->operands(), [&](
const MachineOperand &MO) {
3501 return MO.isReg() && MO.isUse() && !MO.isUndef() && MO.getReg() &&
3502 TRI->regsOverlap(MO.getReg(), DestReg);
3506 PotentialGenCur =
false;
3512 if (!GenCur && !PotentialGenCur)
3518 !canGenCurBypass(DestBundle,
const_cast<MachineInstr *
>(DepTrue),
3519 NextBI.getInstrIterator(), VecDstDep, SrcMI,
Other)) {
3520 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: unsafe intermediate RAW "
3521 "producer (canGenCurBypass).\n");
3531 int ScalarDstDep = dstDependence(DestBundle, ParentBundle.first, SrcMII,
3532 ScalarDepTrue, ScalarDepAnti);
3533 int DstDepMasked = ScalarDstDep & ~Dependency::WAR_SameBundle;
3534 if (GenCur || PotentialGenCur)
3535 DstDepMasked &= ~Dependency::RAW;
3536 if (!Dependency::hasOnly(DstDepMasked, Dependency::Independent)) {
3537 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: scalar dest dependence not "
3538 "independent after masking.\n");
3542 int SrcDep = srcDependence(ParentBundle.first, ParentBundle.second, SrcMII);
3543 if (!Dependency::hasOnly(SrcDep, Dependency::Independent)) {
3544 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: source dependence not "
3550 const InstrItineraryData *
II =
ST.getInstrItineraryData();
3551 if (canCauseInterlockStall(DestBundle, SrcMI,
II)) {
3552 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: would cause an interlock "
3558 bool UndoDestBundle =
false;
3559 if (!DestBundle->isBundle()) {
3560 finalizeBundle(*DestBundle->getParent(), DestBundle, std::next(DestBundle));
3561 DestBundle = std::prev(DestBundle);
3562 UndoDestBundle =
true;
3565 MIInstIterType DestPos = DestBundle;
3566 getResourcesReadyForBundle(DestPos);
3570 bool CanReserve = canReserveResources(SrcMII) && !PotentialGenCur;
3571 bool CanBeMoved =
false;
3573 const MachineInstr *LoadBypassCand = PotentialGenCur ?
Other :
nullptr;
3575 "lastChanceGlobalPullup (LoadBypassCand="
3576 << (LoadBypassCand ?
"Other" :
"null") <<
").\n");
3578 lastChanceGlobalPullup(DestPos, SrcMII, LoadBypassCand))) {
3579 DestBundle = DestPos;
3585 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Packet has room; packetizing "
3587 CanBeMoved = isLegalToPacketizeWith(DestBundle, SrcMI);
3592 bool FixNextBI =
false;
3593 if (SrcMII->isBundled()) {
3595 if (&*SrcBundle == &*NextBI)
3597 }
else if (&*SrcMII == &*NextBI) {
3598 NextBI = DestBundle;
3601 MachineInstr *SingleInstr =
nullptr;
3602 MachineInstr *
Instr =
removeMI(&*SrcMII, QII,
nullptr, &SingleInstr);
3603 if (SingleInstr && FixNextBI)
3607 genLoadBypass(
const_cast<MachineInstr *
>(DepTrue), Instr);
3609 MBB->
insert(std::next(DestBundle), Instr);
3612 <<
"): " << *Instr);
3614 PulledUpSet.
insert(Instr);
3619 LLVM_DEBUG(
dbgs() <<
"[TwoLoadCur] Reject: candidate could not be moved.\n");
3620 if (UndoDestBundle) {
3621 MIInstIterType
I = std::next(DestPos);
3622 I->unbundleFromPred();
3623 DestBundle->eraseFromParent();
3631void GreedyPullup::localPullup(SchedulingRegion_t::SRAtom_t *SR) {
3632 localPullupImpl(SR,
false);
3638void GreedyPullup::localPullupImpl(SchedulingRegion_t::SRAtom_t *SR,
3641 LLVM_DEBUG(
dbgs() <<
"\n\n\t\t[LocalPullup]\tBB#" << SR->getNumber(););
3654 MIIterType NextBI = SR->begin();
3655 for (MIIterType BI = NextBI; BI != std::prev(SR->end()) && BI != SR->end();
3657 NextBI = std::next(BI);
3658 if (BI->isDebugInstr())
3660 MIInstIterType DestBundle = BI.getInstrIterator();
3661 if (!DestBundle->isBundle() && AllowSparsePullup) {
3662 if (isSoloInstruction(*DestBundle))
3665 std::next(DestBundle));
3666 DestBundle = std::prev(DestBundle);
3669 if (!DestBundle->isBundle())
3672 MIIterType SrcBundle = BI;
3676 unsigned NumBundles = 0;
3677 MIIterType
Next = std::next(SrcBundle);
3678 for (SrcBundle =
Next; SrcBundle != SR->end(); SrcBundle =
Next) {
3679 Next = std::next(SrcBundle);
3681 if (SrcBundle->isDebugInstr())
3691 bool isNextBISrc =
false;
3692 if (SrcBundle == std::next(BI))
3706 if (!SrcBundle->isBundle() && isNextBISrc) {
3707 MIInstIterType MII = SrcBundle.getInstrIterator();
3708 MachineInstr &
MI = *MII;
3709 if (
MI.getOpcode() == Hexagon::J2_call) {
3710 if (isLegalToPacketizeWith(BI, &
MI)) {
3711 MIInstIterType DestPos = DestBundle;
3712 getResourcesReadyForBundle(DestPos);
3714 while (DestPos->isInsideBundle())
3717 if (canReserveResources(MII)) {
3718 NextBI = std::next(SrcBundle);
3719 Next = std::next(SrcBundle);
3720 MachineInstr *InstrToMove =
removeMI(&
MI, QII,
nullptr);
3721 MIBundleBuilder Bundle(&*DestBundle);
3723 Bundle.insert(std::next(DestPos), InstrToMove);
3729 <<
"\nMBB: " << *InstrToMove->
getParent());
3739 if (SrcBundle->getBundleSize() <= 2 && !AllowSparsePullup)
3742 MIInstIterType SrcMII = SrcBundle.getInstrIterator();
3743 if (!SrcMII->isBundled() && AllowSparsePullup) {
3744 if (isSoloInstruction(*SrcMII) || shouldNotBePulledUp(&*SrcMII))
3746 finalizeBundle(*(SrcMII->getParent()), SrcMII, std::next(SrcMII));
3747 MIInstIterType BundleHead = std::prev(SrcMII);
3748 SrcBundle = BundleHead;
3750 NextBI = BundleHead;
3751 SrcMII = BundleHead;
3753 MIInstIterType SrcBundleEnd =
getBundleEnd(SrcBundle.getInstrIterator());
3756 SmallPtrSet<MachineInstr *, 4> SrcVisitedSet;
3759 (SrcMII != SR->instr_end()) && SrcMII->isInsideBundle(); ++SrcMII) {
3760 MachineInstr *SrcMI = &*SrcMII;
3761 if (isSoloInstruction(*SrcMI) || SrcMI->
isDebugInstr() ||
3762 !SrcVisitedSet.
insert(SrcMI).second)
3770 SmallPtrSet<MachineInstr *, 4> DepSet;
3772 srcDependence(SrcBundle.getInstrIterator(), SrcBundleEnd, SrcMII,
3773 AllowClusterPullup ? &DepSet :
nullptr);
3774 SmallPtrSet<MachineInstr *, 4> SrcClusterSet;
3775 if (Dependency::hasOnly(SrcDep, Dependency::Independent)) {
3776 SrcClusterSet.
insert(SrcMI);
3782 if (AllowClusterPullup) {
3784 if (Dependency::hasOnly(SrcDep, Dependency::SamePacketRAW) ||
3785 Dependency::hasOnly(SrcDep, Dependency::SamePacketWAR) ||
3786 Dependency::hasOnly(SrcDep, Dependency::SamePacket)) {
3787 LLVM_DEBUG(
dbgs() <<
"\nFound cluster of dependent instructions in "
3789 for (
auto *DepMI : DepSet) {
3791 SrcClusterSet.
insert(DepMI);
3799 if (AllowClusterPullup) {
3800 std::sort(SrcClusterMIs.
begin(), SrcClusterMIs.
end(),
3801 [](SmallPtrSet<MachineInstr *, 4> ClusterA,
3802 SmallPtrSet<MachineInstr *, 4> 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!")
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")
const HexagonInstrInfo * TII
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 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.
int srcDependence(MachineBasicBlock::instr_iterator SrcBeg, MachineBasicBlock::instr_iterator SrcEnd, MachineBasicBlock::instr_iterator RMII, const MIUseDefMap &MIUseDefs, SmallPtrSetImpl< MachineInstr * > *DepSet=nullptr, bool PreventDuplexSep=true, bool IsPullDown=false) const
Compute the intra-packet dependence of RMII against the rest of the bundle [SrcBeg,...
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.
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...
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)
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