33#define DEBUG_TYPE "hexbit"
45 cl::init(std::numeric_limits<unsigned>::max()));
48 cl::init(std::numeric_limits<unsigned>::max()));
59 explicit RegisterSet(
unsigned s,
bool t =
false) : Bits(s, t) {}
67 unsigned count()
const {
71 unsigned find_first()
const {
78 unsigned find_next(
unsigned Prev)
const {
79 int Next =
Bits.find_next(v2x(Prev));
86 unsigned Idx = v2x(R);
88 bool Exists =
Bits.test(Idx);
93 unsigned T = LRU.front();
101 unsigned Idx = v2x(R);
102 if (Idx <
Bits.size()) {
103 bool Exists =
Bits.test(Idx);
115 for (
unsigned R = Rs.find_first(); R; R = Rs.find_next(R))
120 for (
unsigned R = Rs.find_first(); R; R = Rs.find_next(R))
125 bool operator[](
unsigned R)
const {
126 unsigned Idx = v2x(R);
127 return Idx <
Bits.size() ?
Bits[Idx] :
false;
129 bool has(
unsigned R)
const {
130 unsigned Idx = v2x(R);
131 if (Idx >=
Bits.size())
133 return Bits.test(Idx);
143 return Bits.anyCommon(Rs.Bits);
148 std::deque<unsigned> LRU;
150 void ensure(
unsigned Idx) {
151 if (
Bits.size() <= Idx)
152 Bits.resize(std::max(Idx+1, 32U));
155 static inline unsigned v2x(
unsigned v) {
159 static inline unsigned x2v(
unsigned x) {
160 return Register::index2VirtReg(x);
165 PrintRegSet(
const RegisterSet &S,
const TargetRegisterInfo *RI)
168 friend raw_ostream &
operator<< (raw_ostream &OS,
169 const PrintRegSet &
P);
173 const TargetRegisterInfo *TRI;
177 const PrintRegSet &
P);
180 for (
unsigned R =
P.RS.find_first(); R; R =
P.RS.find_next(R))
186 class Transformation;
192 HexagonBitSimplify() : MachineFunctionPass(ID) {}
194 StringRef getPassName()
const override {
195 return "Hexagon bit simplification";
198 void getAnalysisUsage(AnalysisUsage &AU)
const override {
206 static void getInstrDefs(
const MachineInstr &
MI,
RegisterSet &Defs);
208 static bool isEqual(
const BitTracker::RegisterCell &RC1, uint16_t B1,
209 const BitTracker::RegisterCell &RC2, uint16_t B2, uint16_t W);
210 static bool isZero(
const BitTracker::RegisterCell &RC, uint16_t
B,
212 static bool getConst(
const BitTracker::RegisterCell &RC, uint16_t
B,
215 MachineRegisterInfo &MRI);
216 static bool getSubregMask(
const BitTracker::RegisterRef &RR,
217 unsigned &Begin,
unsigned &Width, MachineRegisterInfo &MRI);
219 MachineRegisterInfo &MRI);
221 unsigned NewSR, MachineRegisterInfo &MRI);
222 static bool parseRegSequence(
const MachineInstr &
I,
223 BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH,
224 const MachineRegisterInfo &MRI);
226 static bool getUsedBitsInStore(
unsigned Opc, BitVector &Bits,
228 static bool getUsedBits(
unsigned Opc,
unsigned OpN, BitVector &Bits,
229 uint16_t Begin,
const HexagonInstrInfo &HII);
232 const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI);
233 static bool isTransparentCopy(
const BitTracker::RegisterRef &RD,
234 const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI);
237 MachineDominatorTree *MDT =
nullptr;
239 bool visitBlock(MachineBasicBlock &
B, Transformation &
T,
RegisterSet &AVs);
240 static bool hasTiedUse(
unsigned Reg, MachineRegisterInfo &MRI,
241 unsigned NewSub = Hexagon::NoSubRegister);
244 using HBS = HexagonBitSimplify;
249 class Transformation {
253 Transformation(
bool TD) : TopDown(TD) {}
254 virtual ~Transformation() =
default;
256 virtual bool processBlock(MachineBasicBlock &
B,
const RegisterSet &AVs) = 0;
261char HexagonBitSimplify::ID = 0;
264 "Hexagon bit simplification",
false,
false)
278 getInstrDefs(
I, Defs);
279 RegisterSet NewAVs = AVs;
283 Changed |= visitBlock(*(DTN->getBlock()),
T, NewAVs);
296 for (
auto &
Op :
MI.operands()) {
297 if (!
Op.isReg() || !
Op.isDef())
306void HexagonBitSimplify::getInstrUses(
const MachineInstr &
MI,
308 for (
auto &
Op :
MI.operands()) {
309 if (!
Op.isReg() || !
Op.isUse())
319bool HexagonBitSimplify::isEqual(
const BitTracker::RegisterCell &RC1,
320 uint16_t B1,
const BitTracker::RegisterCell &RC2, uint16_t B2,
322 for (uint16_t i = 0; i <
W; ++i) {
329 if (RC1[B1+i] != RC2[B2+i])
335bool HexagonBitSimplify::isZero(
const BitTracker::RegisterCell &RC,
336 uint16_t
B, uint16_t W) {
338 for (uint16_t i =
B; i <
B+
W; ++i)
344bool HexagonBitSimplify::getConst(
const BitTracker::RegisterCell &RC,
348 for (uint16_t i =
B+W; i >
B; --i) {
349 const BitTracker::BitValue &BV = RC[i-1];
361 MachineRegisterInfo &MRI) {
366 for (
auto I = Begin;
I != End;
I = NextI) {
367 NextI = std::next(
I);
375 MachineRegisterInfo &MRI) {
378 if (hasTiedUse(OldR, MRI, NewSR))
382 for (
auto I = Begin;
I != End;
I = NextI) {
383 NextI = std::next(
I);
390bool HexagonBitSimplify::replaceSubWithSub(
Register OldR,
unsigned OldSR,
392 MachineRegisterInfo &MRI) {
395 if (OldSR != NewSR && hasTiedUse(OldR, MRI, NewSR))
399 for (
auto I = Begin;
I != End;
I = NextI) {
400 NextI = std::next(
I);
401 if (
I->getSubReg() != OldSR)
412bool HexagonBitSimplify::getSubregMask(
const BitTracker::RegisterRef &RR,
413 unsigned &Begin,
unsigned &Width, MachineRegisterInfo &MRI) {
423 switch (RC->
getID()) {
424 case Hexagon::DoubleRegsRegClassID:
425 case Hexagon::HvxWRRegClassID:
427 if (RR.
Sub == Hexagon::isub_hi || RR.
Sub == Hexagon::vsub_hi)
439bool HexagonBitSimplify::parseRegSequence(
const MachineInstr &
I,
440 BitTracker::RegisterRef &SL, BitTracker::RegisterRef &SH,
441 const MachineRegisterInfo &MRI) {
442 assert(
I.getOpcode() == TargetOpcode::REG_SEQUENCE);
443 unsigned Sub1 =
I.getOperand(2).getImm(), Sub2 =
I.getOperand(4).getImm();
444 auto &DstRC = *MRI.
getRegClass(
I.getOperand(0).getReg());
445 auto &HRI =
static_cast<const HexagonRegisterInfo&
>(
449 assert((Sub1 == SubLo && Sub2 == SubHi) || (Sub1 == SubHi && Sub2 == SubLo));
450 if (Sub1 == SubLo && Sub2 == SubHi) {
451 SL =
I.getOperand(1);
452 SH =
I.getOperand(3);
455 if (Sub1 == SubHi && Sub2 == SubLo) {
456 SH =
I.getOperand(1);
457 SL =
I.getOperand(3);
469bool HexagonBitSimplify::getUsedBitsInStore(
unsigned Opc, BitVector &Bits,
471 using namespace Hexagon;
476 case S2_storerbnew_io:
477 case S2_pstorerbt_io:
478 case S2_pstorerbf_io:
479 case S4_pstorerbtnew_io:
480 case S4_pstorerbfnew_io:
481 case S2_pstorerbnewt_io:
482 case S2_pstorerbnewf_io:
483 case S4_pstorerbnewtnew_io:
484 case S4_pstorerbnewfnew_io:
486 case S2_storerbnew_pi:
487 case S2_pstorerbt_pi:
488 case S2_pstorerbf_pi:
489 case S2_pstorerbtnew_pi:
490 case S2_pstorerbfnew_pi:
491 case S2_pstorerbnewt_pi:
492 case S2_pstorerbnewf_pi:
493 case S2_pstorerbnewtnew_pi:
494 case S2_pstorerbnewfnew_pi:
496 case S4_storerbnew_ap:
498 case S2_storerbnew_pr:
500 case S4_storerbnew_ur:
502 case S2_storerbnew_pbr:
504 case S2_storerbnew_pci:
506 case S2_storerbnew_pcr:
508 case S4_storerbnew_rr:
509 case S4_pstorerbt_rr:
510 case S4_pstorerbf_rr:
511 case S4_pstorerbtnew_rr:
512 case S4_pstorerbfnew_rr:
513 case S4_pstorerbnewt_rr:
514 case S4_pstorerbnewf_rr:
515 case S4_pstorerbnewtnew_rr:
516 case S4_pstorerbnewfnew_rr:
518 case S2_storerbnewgp:
519 case S4_pstorerbt_abs:
520 case S4_pstorerbf_abs:
521 case S4_pstorerbtnew_abs:
522 case S4_pstorerbfnew_abs:
523 case S4_pstorerbnewt_abs:
524 case S4_pstorerbnewf_abs:
525 case S4_pstorerbnewtnew_abs:
526 case S4_pstorerbnewfnew_abs:
527 Bits.set(Begin, Begin+8);
532 case S2_storerhnew_io:
533 case S2_pstorerht_io:
534 case S2_pstorerhf_io:
535 case S4_pstorerhtnew_io:
536 case S4_pstorerhfnew_io:
537 case S2_pstorerhnewt_io:
538 case S2_pstorerhnewf_io:
539 case S4_pstorerhnewtnew_io:
540 case S4_pstorerhnewfnew_io:
542 case S2_storerhnew_pi:
543 case S2_pstorerht_pi:
544 case S2_pstorerhf_pi:
545 case S2_pstorerhtnew_pi:
546 case S2_pstorerhfnew_pi:
547 case S2_pstorerhnewt_pi:
548 case S2_pstorerhnewf_pi:
549 case S2_pstorerhnewtnew_pi:
550 case S2_pstorerhnewfnew_pi:
552 case S4_storerhnew_ap:
554 case S2_storerhnew_pr:
556 case S4_storerhnew_ur:
558 case S2_storerhnew_pbr:
560 case S2_storerhnew_pci:
562 case S2_storerhnew_pcr:
564 case S4_pstorerht_rr:
565 case S4_pstorerhf_rr:
566 case S4_pstorerhtnew_rr:
567 case S4_pstorerhfnew_rr:
568 case S4_storerhnew_rr:
569 case S4_pstorerhnewt_rr:
570 case S4_pstorerhnewf_rr:
571 case S4_pstorerhnewtnew_rr:
572 case S4_pstorerhnewfnew_rr:
574 case S2_storerhnewgp:
575 case S4_pstorerht_abs:
576 case S4_pstorerhf_abs:
577 case S4_pstorerhtnew_abs:
578 case S4_pstorerhfnew_abs:
579 case S4_pstorerhnewt_abs:
580 case S4_pstorerhnewf_abs:
581 case S4_pstorerhnewtnew_abs:
582 case S4_pstorerhnewfnew_abs:
583 Bits.set(Begin, Begin+16);
588 case S2_pstorerft_io:
589 case S2_pstorerff_io:
590 case S4_pstorerftnew_io:
591 case S4_pstorerffnew_io:
593 case S2_pstorerft_pi:
594 case S2_pstorerff_pi:
595 case S2_pstorerftnew_pi:
596 case S2_pstorerffnew_pi:
604 case S4_pstorerft_rr:
605 case S4_pstorerff_rr:
606 case S4_pstorerftnew_rr:
607 case S4_pstorerffnew_rr:
609 case S4_pstorerft_abs:
610 case S4_pstorerff_abs:
611 case S4_pstorerftnew_abs:
612 case S4_pstorerffnew_abs:
613 Bits.set(Begin+16, Begin+32);
628bool HexagonBitSimplify::getUsedBits(
unsigned Opc,
unsigned OpN,
629 BitVector &Bits, uint16_t Begin,
const HexagonInstrInfo &HII) {
630 using namespace Hexagon;
632 const MCInstrDesc &
D = HII.get(
Opc);
634 if (OpN ==
D.getNumOperands()-1)
635 return getUsedBitsInStore(
Opc, Bits, Begin);
647 Bits.set(Begin, Begin+8);
660 Bits.set(Begin, Begin+16);
668 Bits.set(Begin+16, Begin+32);
678 Bits.set(Begin, Begin+8);
688 case A2_addh_h16_sat_ll:
690 case A2_addh_l16_sat_ll:
693 case A2_subh_h16_sat_ll:
695 case A2_subh_l16_sat_ll:
696 case M2_mpy_acc_ll_s0:
697 case M2_mpy_acc_ll_s1:
698 case M2_mpy_acc_sat_ll_s0:
699 case M2_mpy_acc_sat_ll_s1:
702 case M2_mpy_nac_ll_s0:
703 case M2_mpy_nac_ll_s1:
704 case M2_mpy_nac_sat_ll_s0:
705 case M2_mpy_nac_sat_ll_s1:
706 case M2_mpy_rnd_ll_s0:
707 case M2_mpy_rnd_ll_s1:
708 case M2_mpy_sat_ll_s0:
709 case M2_mpy_sat_ll_s1:
710 case M2_mpy_sat_rnd_ll_s0:
711 case M2_mpy_sat_rnd_ll_s1:
712 case M2_mpyd_acc_ll_s0:
713 case M2_mpyd_acc_ll_s1:
716 case M2_mpyd_nac_ll_s0:
717 case M2_mpyd_nac_ll_s1:
718 case M2_mpyd_rnd_ll_s0:
719 case M2_mpyd_rnd_ll_s1:
720 case M2_mpyu_acc_ll_s0:
721 case M2_mpyu_acc_ll_s1:
724 case M2_mpyu_nac_ll_s0:
725 case M2_mpyu_nac_ll_s1:
726 case M2_mpyud_acc_ll_s0:
727 case M2_mpyud_acc_ll_s1:
730 case M2_mpyud_nac_ll_s0:
731 case M2_mpyud_nac_ll_s1:
732 if (OpN == 1 || OpN == 2) {
733 Bits.set(Begin, Begin+16);
740 case A2_addh_h16_sat_lh:
743 case A2_subh_h16_sat_lh:
744 case M2_mpy_acc_lh_s0:
745 case M2_mpy_acc_lh_s1:
746 case M2_mpy_acc_sat_lh_s0:
747 case M2_mpy_acc_sat_lh_s1:
750 case M2_mpy_nac_lh_s0:
751 case M2_mpy_nac_lh_s1:
752 case M2_mpy_nac_sat_lh_s0:
753 case M2_mpy_nac_sat_lh_s1:
754 case M2_mpy_rnd_lh_s0:
755 case M2_mpy_rnd_lh_s1:
756 case M2_mpy_sat_lh_s0:
757 case M2_mpy_sat_lh_s1:
758 case M2_mpy_sat_rnd_lh_s0:
759 case M2_mpy_sat_rnd_lh_s1:
760 case M2_mpyd_acc_lh_s0:
761 case M2_mpyd_acc_lh_s1:
764 case M2_mpyd_nac_lh_s0:
765 case M2_mpyd_nac_lh_s1:
766 case M2_mpyd_rnd_lh_s0:
767 case M2_mpyd_rnd_lh_s1:
768 case M2_mpyu_acc_lh_s0:
769 case M2_mpyu_acc_lh_s1:
772 case M2_mpyu_nac_lh_s0:
773 case M2_mpyu_nac_lh_s1:
774 case M2_mpyud_acc_lh_s0:
775 case M2_mpyud_acc_lh_s1:
778 case M2_mpyud_nac_lh_s0:
779 case M2_mpyud_nac_lh_s1:
782 case A2_addh_l16_sat_hl:
784 case A2_subh_l16_sat_hl:
786 Bits.set(Begin, Begin+16);
790 Bits.set(Begin+16, Begin+32);
797 case A2_addh_h16_sat_hl:
800 case A2_subh_h16_sat_hl:
801 case M2_mpy_acc_hl_s0:
802 case M2_mpy_acc_hl_s1:
803 case M2_mpy_acc_sat_hl_s0:
804 case M2_mpy_acc_sat_hl_s1:
807 case M2_mpy_nac_hl_s0:
808 case M2_mpy_nac_hl_s1:
809 case M2_mpy_nac_sat_hl_s0:
810 case M2_mpy_nac_sat_hl_s1:
811 case M2_mpy_rnd_hl_s0:
812 case M2_mpy_rnd_hl_s1:
813 case M2_mpy_sat_hl_s0:
814 case M2_mpy_sat_hl_s1:
815 case M2_mpy_sat_rnd_hl_s0:
816 case M2_mpy_sat_rnd_hl_s1:
817 case M2_mpyd_acc_hl_s0:
818 case M2_mpyd_acc_hl_s1:
821 case M2_mpyd_nac_hl_s0:
822 case M2_mpyd_nac_hl_s1:
823 case M2_mpyd_rnd_hl_s0:
824 case M2_mpyd_rnd_hl_s1:
825 case M2_mpyu_acc_hl_s0:
826 case M2_mpyu_acc_hl_s1:
829 case M2_mpyu_nac_hl_s0:
830 case M2_mpyu_nac_hl_s1:
831 case M2_mpyud_acc_hl_s0:
832 case M2_mpyud_acc_hl_s1:
835 case M2_mpyud_nac_hl_s0:
836 case M2_mpyud_nac_hl_s1:
838 Bits.set(Begin+16, Begin+32);
842 Bits.set(Begin, Begin+16);
849 case A2_addh_h16_sat_hh:
852 case A2_subh_h16_sat_hh:
853 case M2_mpy_acc_hh_s0:
854 case M2_mpy_acc_hh_s1:
855 case M2_mpy_acc_sat_hh_s0:
856 case M2_mpy_acc_sat_hh_s1:
859 case M2_mpy_nac_hh_s0:
860 case M2_mpy_nac_hh_s1:
861 case M2_mpy_nac_sat_hh_s0:
862 case M2_mpy_nac_sat_hh_s1:
863 case M2_mpy_rnd_hh_s0:
864 case M2_mpy_rnd_hh_s1:
865 case M2_mpy_sat_hh_s0:
866 case M2_mpy_sat_hh_s1:
867 case M2_mpy_sat_rnd_hh_s0:
868 case M2_mpy_sat_rnd_hh_s1:
869 case M2_mpyd_acc_hh_s0:
870 case M2_mpyd_acc_hh_s1:
873 case M2_mpyd_nac_hh_s0:
874 case M2_mpyd_nac_hh_s1:
875 case M2_mpyd_rnd_hh_s0:
876 case M2_mpyd_rnd_hh_s1:
877 case M2_mpyu_acc_hh_s0:
878 case M2_mpyu_acc_hh_s1:
881 case M2_mpyu_nac_hh_s0:
882 case M2_mpyu_nac_hh_s1:
883 case M2_mpyud_acc_hh_s0:
884 case M2_mpyud_acc_hh_s1:
887 case M2_mpyud_nac_hh_s0:
888 case M2_mpyud_nac_hh_s1:
889 if (OpN == 1 || OpN == 2) {
890 Bits.set(Begin+16, Begin+32);
903 const BitTracker::RegisterRef &RR, MachineRegisterInfo &MRI) {
909 auto &HRI =
static_cast<const HexagonRegisterInfo&
>(
918 switch (RC->
getID()) {
919 case Hexagon::DoubleRegsRegClassID:
920 VerifySR(RC, RR.
Sub);
921 return &Hexagon::IntRegsRegClass;
922 case Hexagon::HvxWRRegClassID:
923 VerifySR(RC, RR.
Sub);
924 return &Hexagon::HvxVRRegClass;
933bool HexagonBitSimplify::isTransparentCopy(
const BitTracker::RegisterRef &RD,
934 const BitTracker::RegisterRef &RS, MachineRegisterInfo &MRI) {
938 auto *DRC = getFinalVRegClass(RD, MRI);
942 return DRC == getFinalVRegClass(RS, MRI);
945bool HexagonBitSimplify::hasTiedUse(
unsigned Reg, MachineRegisterInfo &MRI,
950 [NewSub] (
const MachineOperand &
Op) ->
bool {
951 return Op.getSubReg() != NewSub && Op.isTied();
957 class DeadCodeElimination {
960 : MF(mf), HII(*MF.getSubtarget<HexagonSubtarget>().getInstrInfo()),
961 MDT(mdt), MRI(mf.getRegInfo()) {}
968 bool isDead(
unsigned R)
const;
972 const HexagonInstrInfo &HII;
973 MachineDominatorTree &MDT;
974 MachineRegisterInfo &MRI;
979bool DeadCodeElimination::isDead(
unsigned R)
const {
981 if (UseI.isDebugInstr())
984 assert(!UseI.getOperand(0).getSubReg());
985 Register DR = UseI.getOperand(0).getReg();
1000 MachineBasicBlock *
B =
N->getBlock();
1001 std::vector<MachineInstr*> Instrs;
1003 Instrs.push_back(&
MI);
1005 for (
auto *
MI : Instrs) {
1006 unsigned Opc =
MI->getOpcode();
1009 if (
Opc == TargetOpcode::LIFETIME_START ||
1010 Opc == TargetOpcode::LIFETIME_END)
1013 if (
MI->isInlineAsm())
1016 if (!
MI->isPHI() && !
MI->isSafeToMove(
Store))
1019 bool AllDead =
true;
1020 SmallVector<unsigned,2> Regs;
1021 for (
auto &
Op :
MI->operands()) {
1022 if (!
Op.isReg() || !
Op.isDef())
1025 if (!
R.isVirtual() || !
isDead(R)) {
1035 for (
unsigned Reg : Regs)
1055 class RedundantInstrElimination :
public Transformation {
1057 RedundantInstrElimination(BitTracker &bt,
const HexagonInstrInfo &hii,
1058 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
1059 : Transformation(
true), HII(hii), HRI(hri), MRI(mri),
BT(bt) {}
1061 bool processBlock(MachineBasicBlock &
B,
const RegisterSet &AVs)
override;
1064 bool isLossyShiftLeft(
const MachineInstr &
MI,
unsigned OpN,
1065 unsigned &LostB,
unsigned &LostE);
1066 bool isLossyShiftRight(
const MachineInstr &
MI,
unsigned OpN,
1067 unsigned &LostB,
unsigned &LostE);
1068 bool computeUsedBits(
unsigned Reg, BitVector &Bits);
1069 bool computeUsedBits(
const MachineInstr &
MI,
unsigned OpN, BitVector &Bits,
1071 bool usedBitsEqual(BitTracker::RegisterRef RD, BitTracker::RegisterRef RS);
1073 const HexagonInstrInfo &HII;
1074 const HexagonRegisterInfo &HRI;
1075 MachineRegisterInfo &MRI;
1084bool RedundantInstrElimination::isLossyShiftLeft(
const MachineInstr &
MI,
1085 unsigned OpN,
unsigned &LostB,
unsigned &LostE) {
1086 using namespace Hexagon;
1088 unsigned Opc =
MI.getOpcode();
1089 unsigned ImN, RegN, Width;
1096 case S2_asl_i_p_acc:
1097 case S2_asl_i_p_and:
1098 case S2_asl_i_p_nac:
1100 case S2_asl_i_p_xacc:
1110 case S2_addasl_rrri:
1111 case S4_andi_asl_ri:
1113 case S4_addi_asl_ri:
1114 case S4_subi_asl_ri:
1115 case S2_asl_i_r_acc:
1116 case S2_asl_i_r_and:
1117 case S2_asl_i_r_nac:
1119 case S2_asl_i_r_sat:
1120 case S2_asl_i_r_xacc:
1132 assert(
MI.getOperand(ImN).isImm());
1133 unsigned S =
MI.getOperand(ImN).getImm();
1144bool RedundantInstrElimination::isLossyShiftRight(
const MachineInstr &
MI,
1145 unsigned OpN,
unsigned &LostB,
unsigned &LostE) {
1146 using namespace Hexagon;
1148 unsigned Opc =
MI.getOpcode();
1156 case S2_asr_i_p_acc:
1157 case S2_asr_i_p_and:
1158 case S2_asr_i_p_nac:
1160 case S2_lsr_i_p_acc:
1161 case S2_lsr_i_p_and:
1162 case S2_lsr_i_p_nac:
1164 case S2_lsr_i_p_xacc:
1173 case S4_andi_lsr_ri:
1175 case S4_addi_lsr_ri:
1176 case S4_subi_lsr_ri:
1177 case S2_asr_i_r_acc:
1178 case S2_asr_i_r_and:
1179 case S2_asr_i_r_nac:
1181 case S2_lsr_i_r_acc:
1182 case S2_lsr_i_r_and:
1183 case S2_lsr_i_r_nac:
1185 case S2_lsr_i_r_xacc:
1197 assert(
MI.getOperand(ImN).isImm());
1198 unsigned S =
MI.getOperand(ImN).getImm();
1208bool RedundantInstrElimination::computeUsedBits(
unsigned Reg, BitVector &Bits) {
1211 std::vector<unsigned> Pending;
1212 Pending.push_back(
Reg);
1214 for (
unsigned i = 0; i < Pending.size(); ++i) {
1215 unsigned R = Pending[i];
1220 BitTracker::RegisterRef UR = *
I;
1222 if (!HBS::getSubregMask(UR,
B, W, MRI))
1224 MachineInstr &UseI = *
I->getParent();
1229 Pending.push_back(DefR);
1231 if (!computeUsedBits(UseI,
I.getOperandNo(), Used,
B))
1254bool RedundantInstrElimination::computeUsedBits(
const MachineInstr &
MI,
1255 unsigned OpN, BitVector &Bits, uint16_t Begin) {
1256 unsigned Opc =
MI.getOpcode();
1257 BitVector
T(
Bits.size());
1258 bool GotBits = HBS::getUsedBits(
Opc, OpN,
T, Begin, HII);
1263 if (isLossyShiftLeft(
MI, OpN, LB, LE) || isLossyShiftRight(
MI, OpN, LB, LE)) {
1264 assert(
MI.getOperand(OpN).isReg());
1265 BitTracker::RegisterRef RR =
MI.getOperand(OpN);
1267 uint16_t Width = HRI.getRegSizeInBits(*RC);
1270 T.set(Begin, Begin+Width);
1271 assert(LB <= LE && LB < Width && LE <= Width);
1272 T.reset(Begin+LB, Begin+LE);
1282bool RedundantInstrElimination::usedBitsEqual(BitTracker::RegisterRef RD,
1283 BitTracker::RegisterRef RS) {
1284 const BitTracker::RegisterCell &DC =
BT.
lookup(RD.
Reg);
1285 const BitTracker::RegisterCell &SC =
BT.
lookup(
RS.Reg);
1288 if (!HBS::getSubregMask(RD, DB, DW, MRI))
1291 if (!HBS::getSubregMask(RS, SB, SW, MRI))
1297 if (!computeUsedBits(RD.
Reg, Used))
1300 for (
unsigned i = 0; i != DW; ++i)
1301 if (Used[i+DB] && DC[DB+i] != SC[SB+i])
1306bool RedundantInstrElimination::processBlock(MachineBasicBlock &
B,
1312 for (
auto I =
B.begin(),
E =
B.end();
I !=
E; ++
I) {
1313 MachineInstr *
MI = &*
I;
1315 if (
MI->getOpcode() == TargetOpcode::COPY)
1317 if (
MI->isPHI() ||
MI->hasUnmodeledSideEffects() ||
MI->isInlineAsm())
1319 unsigned NumD =
MI->getDesc().getNumDefs();
1323 BitTracker::RegisterRef RD =
MI->getOperand(0);
1326 const BitTracker::RegisterCell &DC =
BT.
lookup(RD.
Reg);
1330 for (
auto &
Op :
MI->uses()) {
1333 BitTracker::RegisterRef
RS =
Op;
1336 if (!HBS::isTransparentCopy(RD, RS, MRI))
1340 if (!HBS::getSubregMask(RS, BN, BW, MRI))
1343 const BitTracker::RegisterCell &SC =
BT.
lookup(
RS.Reg);
1344 if (!usedBitsEqual(RD, RS) && !HBS::isEqual(DC, 0, SC, BN, BW))
1351 MachineInstr *CopyI =
1352 BuildMI(
B, At,
DL, HII.get(TargetOpcode::COPY), NewR)
1354 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
1377 class ConstGeneration :
public Transformation {
1379 ConstGeneration(BitTracker &bt,
const HexagonInstrInfo &hii,
1380 MachineRegisterInfo &mri)
1381 : Transformation(
true), HII(hii), MRI(mri),
BT(bt) {}
1383 bool processBlock(MachineBasicBlock &
B,
const RegisterSet &AVs)
override;
1384 static bool isTfrConst(
const MachineInstr &
MI);
1391 const HexagonInstrInfo &HII;
1392 MachineRegisterInfo &MRI;
1398bool ConstGeneration::isTfrConst(
const MachineInstr &
MI) {
1399 unsigned Opc =
MI.getOpcode();
1401 case Hexagon::A2_combineii:
1402 case Hexagon::A4_combineii:
1403 case Hexagon::A2_tfrsi:
1404 case Hexagon::A2_tfrpi:
1405 case Hexagon::PS_true:
1406 case Hexagon::PS_false:
1407 case Hexagon::CONST32:
1408 case Hexagon::CONST64:
1417 MachineBasicBlock &
B,
1421 if (RC == &Hexagon::IntRegsRegClass) {
1427 if (RC == &Hexagon::DoubleRegsRegClass) {
1437 : Hexagon::A4_combineii;
1447 if (!HST.isTinyCore() ||
1455 if (RC == &Hexagon::PredRegsRegClass) {
1458 Opc = Hexagon::PS_false;
1459 else if ((
C & 0xFF) == 0xFF)
1460 Opc = Hexagon::PS_true;
1470bool ConstGeneration::processBlock(MachineBasicBlock &
B,
const RegisterSet&) {
1476 for (
auto I =
B.begin(),
E =
B.end();
I !=
E; ++
I) {
1480 HBS::getInstrDefs(*
I, Defs);
1481 if (Defs.count() != 1)
1487 const BitTracker::RegisterCell &DRC =
BT.
lookup(DR);
1488 if (HBS::getConst(DRC, 0, DRC.
width(), U)) {
1491 auto At =
I->isPHI() ?
B.getFirstNonPHI() :
I;
1494 HBS::replaceReg(DR, ImmReg, MRI);
1495 BT.
put(ImmReg, DRC);
1508 class CopyGeneration :
public Transformation {
1510 CopyGeneration(BitTracker &bt,
const HexagonInstrInfo &hii,
1511 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
1512 : Transformation(
true), HII(hii), HRI(hri), MRI(mri),
BT(bt) {}
1514 bool processBlock(MachineBasicBlock &
B,
const RegisterSet &AVs)
override;
1517 bool findMatch(
const BitTracker::RegisterRef &Inp,
1518 BitTracker::RegisterRef &Out,
const RegisterSet &AVs);
1520 const HexagonInstrInfo &HII;
1521 const HexagonRegisterInfo &HRI;
1522 MachineRegisterInfo &MRI;
1529 class CopyPropagation :
public Transformation {
1531 CopyPropagation(
const HexagonRegisterInfo &hri, MachineRegisterInfo &mri)
1532 : Transformation(
false), HRI(hri), MRI(mri) {}
1534 bool processBlock(MachineBasicBlock &
B,
const RegisterSet &AVs)
override;
1536 static bool isCopyReg(
unsigned Opc,
bool NoConv);
1539 bool propagateRegCopy(MachineInstr &
MI);
1541 const HexagonRegisterInfo &HRI;
1542 MachineRegisterInfo &MRI;
1549bool CopyGeneration::findMatch(
const BitTracker::RegisterRef &Inp,
1550 BitTracker::RegisterRef &Out,
const RegisterSet &AVs) {
1553 const BitTracker::RegisterCell &InpRC =
BT.
lookup(Inp.
Reg);
1554 auto *FRC = HBS::getFinalVRegClass(Inp, MRI);
1556 if (!HBS::getSubregMask(Inp,
B, W, MRI))
1559 for (
Register R = AVs.find_first(); R; R = AVs.find_next(R)) {
1560 if (!
BT.
has(R) || Forbidden[R])
1562 const BitTracker::RegisterCell &RC =
BT.
lookup(R);
1563 unsigned RW = RC.
width();
1567 if (!HBS::isTransparentCopy(R, Inp, MRI))
1569 if (!HBS::isEqual(InpRC,
B, RC, 0, W))
1580 if (MRI.
getRegClass(R) != &Hexagon::DoubleRegsRegClass)
1583 if (HBS::isEqual(InpRC,
B, RC, 0, W))
1584 Out.
Sub = Hexagon::isub_lo;
1585 else if (HBS::isEqual(InpRC,
B, RC, W, W))
1586 Out.
Sub = Hexagon::isub_hi;
1590 if (HBS::isTransparentCopy(Out, Inp, MRI))
1596bool CopyGeneration::processBlock(MachineBasicBlock &
B,
1604 for (
auto I =
B.begin(),
E =
B.end();
I !=
E; ++
I, AVB.insert(Defs)) {
1606 HBS::getInstrDefs(*
I, Defs);
1608 unsigned Opc =
I->getOpcode();
1609 if (CopyPropagation::isCopyReg(
Opc,
false) ||
1610 ConstGeneration::isTfrConst(*
I))
1614 auto At =
I->isPHI() ?
B.getFirstNonPHI() :
I;
1616 for (
Register R = Defs.find_first(); R; R = Defs.find_next(R)) {
1617 BitTracker::RegisterRef MR;
1618 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1620 if (findMatch(R, MR, AVB)) {
1622 BuildMI(
B, At,
DL, HII.get(TargetOpcode::COPY), NewR)
1624 BT.
put(BitTracker::RegisterRef(NewR),
BT.
get(MR));
1625 HBS::replaceReg(R, NewR, MRI);
1626 Forbidden.insert(R);
1630 if (FRC == &Hexagon::DoubleRegsRegClass ||
1631 FRC == &Hexagon::HvxWRRegClass) {
1635 BitTracker::RegisterRef TL = {
R, SubLo };
1636 BitTracker::RegisterRef
TH = {
R, SubHi };
1637 BitTracker::RegisterRef
ML, MH;
1638 if (findMatch(TL,
ML, AVB) && findMatch(TH, MH, AVB)) {
1639 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1641 BuildMI(
B, At,
DL, HII.get(TargetOpcode::REG_SEQUENCE), NewR)
1646 BT.
put(BitTracker::RegisterRef(NewR),
BT.
get(R));
1647 HBS::replaceReg(R, NewR, MRI);
1648 Forbidden.insert(R);
1657bool CopyPropagation::isCopyReg(
unsigned Opc,
bool NoConv) {
1659 case TargetOpcode::COPY:
1660 case TargetOpcode::REG_SEQUENCE:
1661 case Hexagon::A4_combineir:
1662 case Hexagon::A4_combineri:
1664 case Hexagon::A2_tfr:
1665 case Hexagon::A2_tfrp:
1666 case Hexagon::A2_combinew:
1667 case Hexagon::V6_vcombine:
1675bool CopyPropagation::propagateRegCopy(MachineInstr &
MI) {
1677 unsigned Opc =
MI.getOpcode();
1678 BitTracker::RegisterRef RD =
MI.getOperand(0);
1679 assert(
MI.getOperand(0).getSubReg() == 0);
1682 case TargetOpcode::COPY:
1683 case Hexagon::A2_tfr:
1684 case Hexagon::A2_tfrp: {
1685 BitTracker::RegisterRef
RS =
MI.getOperand(1);
1686 if (!HBS::isTransparentCopy(RD, RS, MRI))
1694 case TargetOpcode::REG_SEQUENCE: {
1695 BitTracker::RegisterRef SL, SH;
1696 if (HBS::parseRegSequence(
MI, SL, SH, MRI)) {
1705 case Hexagon::A2_combinew:
1706 case Hexagon::V6_vcombine: {
1710 BitTracker::RegisterRef RH =
MI.getOperand(1), RL =
MI.getOperand(2);
1711 Changed = HBS::replaceSubWithSub(RD.
Reg, SubLo, RL.Reg, RL.Sub, MRI);
1715 case Hexagon::A4_combineir:
1716 case Hexagon::A4_combineri: {
1717 unsigned SrcX = (
Opc == Hexagon::A4_combineir) ? 2 : 1;
1718 unsigned Sub = (
Opc == Hexagon::A4_combineir) ? Hexagon::isub_lo
1720 BitTracker::RegisterRef
RS =
MI.getOperand(SrcX);
1728bool CopyPropagation::processBlock(MachineBasicBlock &
B,
const RegisterSet&) {
1729 std::vector<MachineInstr*> Instrs;
1731 Instrs.push_back(&
MI);
1734 for (
auto *
I : Instrs) {
1735 unsigned Opc =
I->getOpcode();
1736 if (!CopyPropagation::isCopyReg(
Opc,
true))
1749 class BitSimplification :
public Transformation {
1751 BitSimplification(BitTracker &bt,
const MachineDominatorTree &mdt,
1752 const HexagonInstrInfo &hii,
1753 const HexagonRegisterInfo &hri, MachineRegisterInfo &mri,
1755 : Transformation(
true), MDT(mdt), HII(hii), HRI(hri), MRI(mri),
BT(bt) {
1758 bool processBlock(MachineBasicBlock &
B,
const RegisterSet &AVs)
override;
1761 struct RegHalf :
public BitTracker::RegisterRef {
1765 bool matchHalf(
unsigned SelfR,
const BitTracker::RegisterCell &RC,
1766 unsigned B, RegHalf &RH);
1767 bool validateReg(BitTracker::RegisterRef R,
unsigned Opc,
unsigned OpNum);
1769 bool matchPackhl(
unsigned SelfR,
const BitTracker::RegisterCell &RC,
1770 BitTracker::RegisterRef &Rs, BitTracker::RegisterRef &Rt);
1771 unsigned getCombineOpcode(
bool HLow,
bool LLow);
1773 bool genStoreUpperHalf(MachineInstr *
MI);
1774 bool genStoreImmediate(MachineInstr *
MI);
1775 bool genPackhl(MachineInstr *
MI, BitTracker::RegisterRef RD,
1776 const BitTracker::RegisterCell &RC);
1777 bool genExtractHalf(MachineInstr *
MI, BitTracker::RegisterRef RD,
1778 const BitTracker::RegisterCell &RC);
1779 bool genCombineHalf(MachineInstr *
MI, BitTracker::RegisterRef RD,
1780 const BitTracker::RegisterCell &RC);
1781 bool genExtractLow(MachineInstr *
MI, BitTracker::RegisterRef RD,
1782 const BitTracker::RegisterCell &RC);
1783 bool genBitSplit(MachineInstr *
MI, BitTracker::RegisterRef RD,
1784 const BitTracker::RegisterCell &RC,
const RegisterSet &AVs);
1785 bool simplifyTstbit(MachineInstr *
MI, BitTracker::RegisterRef RD,
1786 const BitTracker::RegisterCell &RC);
1787 bool simplifyExtractLow(MachineInstr *
MI, BitTracker::RegisterRef RD,
1788 const BitTracker::RegisterCell &RC,
const RegisterSet &AVs);
1789 bool simplifyRCmp0(MachineInstr *
MI, BitTracker::RegisterRef RD);
1793 std::vector<MachineInstr*> NewMIs;
1795 const MachineDominatorTree &MDT;
1796 const HexagonInstrInfo &HII;
1797 [[maybe_unused]]
const HexagonRegisterInfo &HRI;
1798 MachineRegisterInfo &MRI;
1807bool BitSimplification::matchHalf(
unsigned SelfR,
1808 const BitTracker::RegisterCell &RC,
unsigned B, RegHalf &RH) {
1818 while (
I <
B+16 && RC[
I].num())
1824 unsigned P = RC[
I].RefI.Pos;
1827 unsigned Pos =
P - (
I-
B);
1829 if (
Reg == 0 ||
Reg == SelfR)
1836 const BitTracker::RegisterCell &SC =
BT.
lookup(
Reg);
1837 if (Pos+16 > SC.
width())
1840 for (
unsigned i = 0; i < 16; ++i) {
1841 const BitTracker::BitValue &RV = RC[i+
B];
1849 if (RC[i+
B] != SC[i+Pos])
1856 Sub = Hexagon::isub_lo;
1860 Sub = Hexagon::isub_lo;
1864 Sub = Hexagon::isub_hi;
1868 Sub = Hexagon::isub_hi;
1879 if (!HBS::getFinalVRegClass(RH, MRI))
1885bool BitSimplification::validateReg(BitTracker::RegisterRef R,
unsigned Opc,
1887 auto *OpRC = HII.getRegClass(HII.get(
Opc), OpNum);
1888 auto *RRC = HBS::getFinalVRegClass(R, MRI);
1889 return OpRC->hasSubClassEq(RRC);
1894bool BitSimplification::matchPackhl(
unsigned SelfR,
1895 const BitTracker::RegisterCell &RC, BitTracker::RegisterRef &Rs,
1896 BitTracker::RegisterRef &Rt) {
1897 RegHalf L1, H1, L2, H2;
1899 if (!matchHalf(SelfR, RC, 0, L2) || !matchHalf(SelfR, RC, 16, L1))
1901 if (!matchHalf(SelfR, RC, 32, H2) || !matchHalf(SelfR, RC, 48, H1))
1905 if (H1.Reg != L1.Reg || H1.Sub != L1.Sub || H1.Low || !L1.Low)
1907 if (H2.Reg != L2.Reg || H2.Sub != L2.Sub || H2.Low || !L2.Low)
1915unsigned BitSimplification::getCombineOpcode(
bool HLow,
bool LLow) {
1916 return HLow ? LLow ? Hexagon::A2_combine_ll
1917 : Hexagon::A2_combine_lh
1918 : LLow ? Hexagon::A2_combine_hl
1919 : Hexagon::A2_combine_hh;
1925bool BitSimplification::genStoreUpperHalf(MachineInstr *
MI) {
1926 unsigned Opc =
MI->getOpcode();
1927 if (
Opc != Hexagon::S2_storerh_io)
1930 MachineOperand &ValOp =
MI->getOperand(2);
1931 BitTracker::RegisterRef
RS = ValOp;
1934 const BitTracker::RegisterCell &RC =
BT.
lookup(
RS.Reg);
1936 unsigned B = (
RS.Sub == Hexagon::isub_hi) ? 32 : 0;
1937 if (!matchHalf(0, RC,
B,
H))
1941 MI->setDesc(HII.get(Hexagon::S2_storerf_io));
1949bool BitSimplification::genStoreImmediate(MachineInstr *
MI) {
1950 unsigned Opc =
MI->getOpcode();
1953 case Hexagon::S2_storeri_io:
1956 case Hexagon::S2_storerh_io:
1959 case Hexagon::S2_storerb_io:
1966 if (!
MI->getOperand(0).isReg())
1968 MachineOperand &OffOp =
MI->getOperand(1);
1974 if (!
isUIntN(6+Align, Off) || (Off & ((1<<Align)-1)))
1977 BitTracker::RegisterRef
RS =
MI->getOperand(2);
1980 const BitTracker::RegisterCell &RC =
BT.
lookup(
RS.Reg);
1982 if (!HBS::getConst(RC, 0, RC.
width(), U))
1988 case Hexagon::S2_storerb_io:
1991 case Hexagon::S2_storerh_io:
1994 case Hexagon::S2_storeri_io:
2005 MI->removeOperand(2);
2007 case Hexagon::S2_storerb_io:
2008 MI->setDesc(HII.get(Hexagon::S4_storeirb_io));
2010 case Hexagon::S2_storerh_io:
2011 MI->setDesc(HII.get(Hexagon::S4_storeirh_io));
2013 case Hexagon::S2_storeri_io:
2014 MI->setDesc(HII.get(Hexagon::S4_storeiri_io));
2025bool BitSimplification::genPackhl(MachineInstr *
MI,
2026 BitTracker::RegisterRef RD,
const BitTracker::RegisterCell &RC) {
2027 unsigned Opc =
MI->getOpcode();
2028 if (
Opc == Hexagon::S2_packhl)
2030 BitTracker::RegisterRef Rs, Rt;
2031 if (!matchPackhl(RD.
Reg, RC, Rs, Rt))
2033 if (!validateReg(Rs, Hexagon::S2_packhl, 1) ||
2034 !validateReg(Rt, Hexagon::S2_packhl, 2))
2037 MachineBasicBlock &
B = *
MI->getParent();
2040 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2042 BuildMI(
B, At,
DL, HII.get(Hexagon::S2_packhl), NewR)
2045 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2046 BT.
put(BitTracker::RegisterRef(NewR), RC);
2052bool BitSimplification::genExtractHalf(MachineInstr *
MI,
2053 BitTracker::RegisterRef RD,
const BitTracker::RegisterCell &RC) {
2056 if (!matchHalf(RD.
Reg, RC, 0, L) || !HBS::isZero(RC, 16, 16))
2059 unsigned Opc =
MI->getOpcode();
2060 MachineBasicBlock &
B = *
MI->getParent();
2066 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2068 if (
L.Low &&
Opc != Hexagon::A2_zxth) {
2069 if (validateReg(L, Hexagon::A2_zxth, 1)) {
2071 BuildMI(
B, At,
DL, HII.get(Hexagon::A2_zxth), NewR)
2074 }
else if (!
L.Low &&
Opc != Hexagon::S2_lsr_i_r) {
2075 if (validateReg(L, Hexagon::S2_lsr_i_r, 1)) {
2084 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2085 BT.
put(BitTracker::RegisterRef(NewR), RC);
2091bool BitSimplification::genCombineHalf(MachineInstr *
MI,
2092 BitTracker::RegisterRef RD,
const BitTracker::RegisterCell &RC) {
2095 if (!matchHalf(RD.
Reg, RC, 0, L) || !matchHalf(RD.
Reg, RC, 16,
H))
2098 if (
L.Reg ==
H.Reg &&
L.Sub ==
H.Sub && !
H.Low &&
L.Low)
2101 unsigned Opc =
MI->getOpcode();
2102 unsigned COpc = getCombineOpcode(
H.Low,
L.Low);
2105 if (!validateReg(
H, COpc, 1) || !validateReg(L, COpc, 2))
2108 MachineBasicBlock &
B = *
MI->getParent();
2111 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2116 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2117 BT.
put(BitTracker::RegisterRef(NewR), RC);
2123bool BitSimplification::genExtractLow(MachineInstr *
MI,
2124 BitTracker::RegisterRef RD,
const BitTracker::RegisterCell &RC) {
2125 unsigned Opc =
MI->getOpcode();
2127 case Hexagon::A2_zxtb:
2128 case Hexagon::A2_zxth:
2129 case Hexagon::S2_extractu:
2132 if (
Opc == Hexagon::A2_andir &&
MI->getOperand(2).isImm()) {
2133 int32_t
Imm =
MI->getOperand(2).getImm();
2138 if (
MI->hasUnmodeledSideEffects() ||
MI->isInlineAsm())
2141 while (W > 0 && RC[W-1].is(0))
2143 if (W == 0 || W == RC.
width())
2145 unsigned NewOpc = (
W == 8) ? Hexagon::A2_zxtb
2146 : (
W == 16) ? Hexagon::A2_zxth
2147 : (
W < 10) ? Hexagon::A2_andir
2148 : Hexagon::S2_extractu;
2149 MachineBasicBlock &
B = *
MI->getParent();
2152 for (
auto &
Op :
MI->uses()) {
2155 BitTracker::RegisterRef RS = Op;
2156 if (!BT.has(RS.Reg))
2158 const BitTracker::RegisterCell &SC = BT.lookup(RS.Reg);
2160 if (!HBS::getSubregMask(RS, BN, BW, MRI))
2162 if (BW < W || !HBS::isEqual(RC, 0, SC, BN, W))
2164 if (!validateReg(RS, NewOpc, 1))
2167 Register NewR = MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2168 auto At = MI->isPHI() ? B.getFirstNonPHI()
2169 : MachineBasicBlock::iterator(MI);
2171 BuildMI(B, At, DL, HII.get(NewOpc), NewR).addReg(RS.Reg, {},
RS.Sub);
2172 if (NewOpc == Hexagon::A2_andir)
2173 MIB.addImm((1 << W) - 1);
2174 else if (NewOpc == Hexagon::S2_extractu)
2175 MIB.addImm(W).addImm(0);
2176 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2177 BT.
put(BitTracker::RegisterRef(NewR), RC);
2183bool BitSimplification::genBitSplit(MachineInstr *
MI,
2184 BitTracker::RegisterRef RD,
const BitTracker::RegisterCell &RC,
2193 unsigned Opc =
MI->getOpcode();
2195 case Hexagon::A4_bitsplit:
2196 case Hexagon::A4_bitspliti:
2204 auto ctlz = [] (
const BitTracker::RegisterCell &
C) ->
unsigned {
2205 unsigned Z =
C.width();
2206 while (Z > 0 &&
C[Z-1].is(0))
2208 return C.width() -
Z;
2212 unsigned Z = ctlz(RC);
2213 if (Z == 0 || Z == W)
2218 const BitTracker::BitValue &
B0 = RC[0];
2222 unsigned SrcR =
B0.RefI.Reg;
2224 unsigned Pos =
B0.RefI.Pos;
2227 for (
unsigned i = 1; i <
W-
Z; ++i) {
2228 const BitTracker::BitValue &
V = RC[i];
2231 if (
V.RefI.Reg != SrcR ||
V.RefI.Pos != Pos+i)
2236 for (
unsigned S = AVs.find_first(); S; S = AVs.find_next(S)) {
2240 if (SRC != Hexagon::IntRegsRegClassID &&
2241 SRC != Hexagon::DoubleRegsRegClassID)
2245 const BitTracker::RegisterCell &SC =
BT.
lookup(S);
2246 if (SC.
width() != W || ctlz(SC) != W-Z)
2249 const BitTracker::BitValue &S0 = SC[0];
2254 if (Pos <=
P && (Pos + W-Z) !=
P)
2256 if (
P < Pos && (
P + Z) != Pos)
2259 if (std::min(
P, Pos) != 0 && std::min(
P, Pos) != 32)
2263 for (
I = 1;
I <
Z; ++
I) {
2264 const BitTracker::BitValue &
V = SC[
I];
2267 if (
V.RefI.Reg != SrcR ||
V.RefI.Pos !=
P+
I)
2280 auto At = DefS->
isPHI() ?
B.getFirstNonPHI()
2283 SrcSR = (std::min(Pos,
P) == 32) ? Hexagon::isub_hi : Hexagon::isub_lo;
2284 if (!validateReg({SrcR,SrcSR}, Hexagon::A4_bitspliti, 1))
2286 unsigned ImmOp = Pos <=
P ?
W-
Z :
Z;
2290 for (MachineInstr *In : NewMIs) {
2291 if (
In->getOpcode() != Hexagon::A4_bitspliti)
2293 MachineOperand &Op1 =
In->getOperand(1);
2296 if (
In->getOperand(2).getImm() != ImmOp)
2299 MachineOperand &Op0 =
In->getOperand(0);
2312 auto NewBS =
BuildMI(
B, At,
DL, HII.get(Hexagon::A4_bitspliti), NewR)
2315 NewMIs.push_back(NewBS);
2318 HBS::replaceRegWithSub(RD.
Reg, NewR, Hexagon::isub_lo, MRI);
2319 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_hi, MRI);
2321 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_lo, MRI);
2322 HBS::replaceRegWithSub(RD.
Reg, NewR, Hexagon::isub_hi, MRI);
2336bool BitSimplification::simplifyTstbit(MachineInstr *
MI,
2337 BitTracker::RegisterRef RD,
const BitTracker::RegisterCell &RC) {
2338 unsigned Opc =
MI->getOpcode();
2339 if (
Opc != Hexagon::S2_tstbit_i)
2342 unsigned BN =
MI->getOperand(2).getImm();
2343 BitTracker::RegisterRef
RS =
MI->getOperand(1);
2346 if (!
BT.
has(
RS.Reg) || !HBS::getSubregMask(RS,
F, W, MRI))
2349 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2352 const BitTracker::RegisterCell &SC =
BT.
lookup(
RS.Reg);
2353 const BitTracker::BitValue &
V = SC[
F+BN];
2359 unsigned P = std::numeric_limits<unsigned>::max();
2360 BitTracker::RegisterRef RR(
V.RefI.Reg, 0);
2361 if (TC == &Hexagon::DoubleRegsRegClass) {
2363 RR.
Sub = Hexagon::isub_lo;
2366 RR.
Sub = Hexagon::isub_hi;
2368 }
else if (TC == &Hexagon::IntRegsRegClass) {
2371 if (
P != std::numeric_limits<unsigned>::max()) {
2373 BuildMI(
B, At,
DL, HII.get(Hexagon::S2_tstbit_i), NewR)
2376 HBS::replaceReg(RD.
Reg, NewR, MRI);
2380 }
else if (
V.is(0) ||
V.is(1)) {
2382 unsigned NewOpc =
V.is(0) ? Hexagon::PS_false : Hexagon::PS_true;
2384 HBS::replaceReg(RD.
Reg, NewR, MRI);
2396bool BitSimplification::simplifyExtractLow(MachineInstr *
MI,
2397 BitTracker::RegisterRef RD,
const BitTracker::RegisterCell &RC,
2416 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2430 const BitTracker::BitValue &TopV = RC[
W-1];
2436 for (
unsigned I = 0;
I !=
W; ++
I) {
2437 const BitTracker::BitValue &
V = RC[
I];
2445 if (TopV.
is(0) || TopV.
is(1)) {
2446 bool S = TopV.
is(1);
2447 for (--W;
W > 0 && RC[
W-1].is(S); --
W)
2459 for (--W;
W > 0 && RC[
W-1] == TopV; --
W)
2474 dbgs() <<
"Cell: " << RC <<
'\n';
2475 dbgs() <<
"Expected bitfield size: " <<
Len <<
" bits, "
2476 << (
Signed ?
"sign" :
"zero") <<
"-extended\n";
2481 for (
unsigned R = AVs.find_first(); R != 0; R = AVs.find_next(R)) {
2484 const BitTracker::RegisterCell &SC =
BT.
lookup(R);
2485 unsigned SW = SC.
width();
2491 if (SW < RW || (SW % RW) != 0)
2498 while (Off <= SW-Len) {
2499 unsigned OE = (
Off+
Len)/RW;
2510 if (HBS::isEqual(RC, 0, SC, Off, Len))
2519 unsigned ExtOpc = 0;
2522 ExtOpc =
Signed ? Hexagon::A2_sxtb : Hexagon::A2_zxtb;
2524 ExtOpc =
Signed ? Hexagon::A2_sxth : Hexagon::A2_zxth;
2525 else if (Len < 10 && !
Signed)
2526 ExtOpc = Hexagon::A2_andir;
2530 Signed ? (RW == 32 ? Hexagon::S4_extract : Hexagon::S4_extractp)
2531 : (RW == 32 ? Hexagon::S2_extractu : Hexagon::S2_extractup);
2535 if (RW != SW && RW*2 != SW)
2538 SR = (
Off/RW == 0) ? Hexagon::isub_lo : Hexagon::isub_hi;
2541 if (!validateReg({
R,SR}, ExtOpc, 1))
2545 if (
MI->getOpcode() == ExtOpc) {
2547 const MachineOperand &SrcOp =
MI->getOperand(1);
2555 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2559 case Hexagon::A2_sxtb:
2560 case Hexagon::A2_zxtb:
2561 case Hexagon::A2_sxth:
2562 case Hexagon::A2_zxth:
2564 case Hexagon::A2_andir:
2565 MIB.addImm((1u << Len) - 1);
2567 case Hexagon::S4_extract:
2568 case Hexagon::S2_extractu:
2569 case Hexagon::S4_extractp:
2570 case Hexagon::S2_extractup:
2578 HBS::replaceReg(RD.
Reg, NewR, MRI);
2579 BT.
put(BitTracker::RegisterRef(NewR), RC);
2587bool BitSimplification::simplifyRCmp0(MachineInstr *
MI,
2588 BitTracker::RegisterRef RD) {
2589 unsigned Opc =
MI->getOpcode();
2590 if (
Opc != Hexagon::A4_rcmpeqi &&
Opc != Hexagon::A4_rcmpneqi)
2592 MachineOperand &CmpOp =
MI->getOperand(2);
2597 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2601 MachineBasicBlock &
B = *
MI->getParent();
2603 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2606 bool KnownNZ =
false;
2608 BitTracker::RegisterRef SR =
MI->getOperand(1);
2611 const BitTracker::RegisterCell &SC =
BT.
lookup(SR.
Reg);
2613 if (!HBS::getSubregMask(SR,
F, W, MRI))
2616 for (uint16_t
I =
F;
I !=
F+
W; ++
I) {
2617 const BitTracker::BitValue &
V = SC[
I];
2624 auto ReplaceWithConst = [&](
int C) {
2626 BuildMI(
B, At,
DL, HII.get(Hexagon::A2_tfrsi), NewR)
2628 HBS::replaceReg(RD.
Reg, NewR, MRI);
2629 BitTracker::RegisterCell NewRC(W);
2630 for (uint16_t
I = 0;
I !=
W; ++
I) {
2631 NewRC[
I] = BitTracker::BitValue(
C & 1);
2632 C = unsigned(
C) >> 1;
2634 BT.
put(BitTracker::RegisterRef(NewR), NewRC);
2638 auto IsNonZero = [] (
const MachineOperand &
Op) {
2639 if (
Op.isGlobal() ||
Op.isBlockAddress())
2642 return Op.getImm() != 0;
2644 return !
Op.getCImm()->isZero();
2646 return !
Op.getFPImm()->isZero();
2650 auto IsZero = [] (
const MachineOperand &
Op) {
2651 if (
Op.isGlobal() ||
Op.isBlockAddress())
2654 return Op.getImm() == 0;
2656 return Op.getCImm()->isZero();
2658 return Op.getFPImm()->isZero();
2664 if (KnownZ || KnownNZ) {
2665 assert(KnownZ != KnownNZ &&
"Register cannot be both 0 and non-0");
2666 return ReplaceWithConst(KnownZ == (
Opc == Hexagon::A4_rcmpeqi));
2674 if (SR.
Sub == 0 && InpDef->
getOpcode() == Hexagon::C2_muxii) {
2675 MachineOperand &Src1 = InpDef->
getOperand(2);
2676 MachineOperand &Src2 = InpDef->
getOperand(3);
2678 bool KnownNZ1 = IsNonZero(Src1), KnownNZ2 = IsNonZero(Src2);
2679 if (KnownNZ1 && KnownNZ2)
2680 return ReplaceWithConst(
Opc == Hexagon::A4_rcmpneqi);
2682 bool KnownZ1 = IsZero(Src1), KnownZ2 = IsZero(Src2);
2683 if (KnownZ1 && KnownZ2)
2684 return ReplaceWithConst(
Opc == Hexagon::A4_rcmpeqi);
2689 if ((KnownZ1 || KnownNZ1) && (KnownZ2 || KnownNZ2)) {
2691 BuildMI(
B, At,
DL, HII.get(Hexagon::C2_muxii), NewR)
2693 .
addImm(KnownZ1 == (
Opc == Hexagon::A4_rcmpeqi))
2694 .
addImm(KnownZ2 == (
Opc == Hexagon::A4_rcmpeqi));
2695 HBS::replaceReg(RD.
Reg, NewR, MRI);
2697 BitTracker::RegisterCell NewRC(W);
2700 BT.
put(BitTracker::RegisterRef(NewR), NewRC);
2708bool BitSimplification::processBlock(MachineBasicBlock &
B,
2716 for (
auto I =
B.begin(),
E =
B.end();
I !=
E; ++
I, AVB.insert(Defs)) {
2717 MachineInstr *
MI = &*
I;
2719 HBS::getInstrDefs(*
MI, Defs);
2721 unsigned Opc =
MI->getOpcode();
2722 if (
Opc == TargetOpcode::COPY ||
Opc == TargetOpcode::REG_SEQUENCE)
2725 if (
MI->mayStore()) {
2726 bool T = genStoreUpperHalf(
MI);
2727 T =
T || genStoreImmediate(
MI);
2732 if (Defs.count() != 1)
2734 const MachineOperand &Op0 =
MI->getOperand(0);
2737 BitTracker::RegisterRef RD = Op0;
2741 const BitTracker::RegisterCell &RC =
BT.
lookup(RD.
Reg);
2743 if (FRC->
getID() == Hexagon::DoubleRegsRegClassID) {
2744 bool T = genPackhl(
MI, RD, RC);
2745 T =
T || simplifyExtractLow(
MI, RD, RC, AVB);
2750 if (FRC->
getID() == Hexagon::IntRegsRegClassID) {
2751 bool T = genBitSplit(
MI, RD, RC, AVB);
2752 T =
T || simplifyExtractLow(
MI, RD, RC, AVB);
2753 T =
T || genExtractHalf(
MI, RD, RC);
2754 T =
T || genCombineHalf(
MI, RD, RC);
2755 T =
T || genExtractLow(
MI, RD, RC);
2756 T =
T || simplifyRCmp0(
MI, RD);
2761 if (FRC->
getID() == Hexagon::PredRegsRegClassID) {
2762 bool T = simplifyTstbit(
MI, RD, RC);
2776 auto &HII = *HST.getInstrInfo();
2778 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
2782 Changed = DeadCodeElimination(MF, *MDT).run();
2784 const HexagonEvaluator HE(HRI, MRI, HII, MF);
2785 BitTracker
BT(HE, MF);
2792 ConstGeneration ImmG(
BT, HII, MRI);
2793 Changed |= visitBlock(Entry, ImmG, AIG);
2796 RedundantInstrElimination RIE(
BT, HII, HRI, MRI);
2797 bool Ried = visitBlock(Entry, RIE, ARE);
2804 CopyGeneration CopyG(
BT, HII, HRI, MRI);
2805 Changed |= visitBlock(Entry, CopyG, ACG);
2808 CopyPropagation CopyP(HRI, MRI);
2809 Changed |= visitBlock(Entry, CopyP, ACP);
2815 BitSimplification BitS(
BT, *MDT, HII, HRI, MRI, MF);
2816 Changed |= visitBlock(Entry, BitS, ABS);
2824 DeadCodeElimination(MF, *MDT).run();
2893 class HexagonLoopRescheduling :
public MachineFunctionPass {
2897 HexagonLoopRescheduling() : MachineFunctionPass(
ID) {}
2902 const HexagonInstrInfo *HII =
nullptr;
2903 const HexagonRegisterInfo *HRI =
nullptr;
2904 MachineRegisterInfo *MRI =
nullptr;
2905 BitTracker *BTP =
nullptr;
2908 LoopCand(MachineBasicBlock *lb, MachineBasicBlock *pb,
2909 MachineBasicBlock *eb) : LB(lb),
PB(pb), EB(eb) {}
2911 MachineBasicBlock *LB, *
PB, *EB;
2913 using InstrList = std::vector<MachineInstr *>;
2915 BitTracker::RegisterRef Inp, Out;
2919 PhiInfo(MachineInstr &
P, MachineBasicBlock &
B);
2922 BitTracker::RegisterRef LR, PR;
2923 MachineBasicBlock *LB, *
PB;
2926 static unsigned getDefReg(
const MachineInstr *
MI);
2928 bool isBitShuffle(
const MachineInstr *
MI,
unsigned DefR)
const;
2929 bool isStoreInput(
const MachineInstr *
MI,
unsigned DefR)
const;
2930 bool isShuffleOf(
unsigned OutR,
unsigned InpR)
const;
2931 bool isSameShuffle(
unsigned OutR1,
unsigned InpR1,
unsigned OutR2,
2932 unsigned &InpR2)
const;
2933 void moveGroup(InstrGroup &
G, MachineBasicBlock &LB, MachineBasicBlock &
PB,
2940char HexagonLoopRescheduling::ID = 0;
2943 "Hexagon Loop Rescheduling",
false,
false)
2947 DefR = HexagonLoopRescheduling::getDefReg(&
P);
2950 for (
unsigned i = 1, n =
P.getNumOperands(); i < n; i += 2) {
2953 LR =
P.getOperand(i);
2957 PR =
P.getOperand(i);
2961unsigned HexagonLoopRescheduling::getDefReg(
const MachineInstr *
MI) {
2963 HBS::getInstrDefs(*
MI, Defs);
2964 if (Defs.count() != 1)
2966 return Defs.find_first();
2969bool HexagonLoopRescheduling::isConst(
unsigned Reg)
const {
2972 const BitTracker::RegisterCell &RC = BTP->
lookup(
Reg);
2973 for (
unsigned i = 0, w = RC.
width(); i < w; ++i) {
2974 const BitTracker::BitValue &
V = RC[i];
2975 if (!
V.is(0) && !
V.is(1))
2981bool HexagonLoopRescheduling::isBitShuffle(
const MachineInstr *
MI,
2982 unsigned DefR)
const {
2983 unsigned Opc =
MI->getOpcode();
2985 case TargetOpcode::COPY:
2986 case Hexagon::S2_lsr_i_r:
2987 case Hexagon::S2_asr_i_r:
2988 case Hexagon::S2_asl_i_r:
2989 case Hexagon::S2_lsr_i_p:
2990 case Hexagon::S2_asr_i_p:
2991 case Hexagon::S2_asl_i_p:
2992 case Hexagon::S2_insert:
2993 case Hexagon::A2_or:
2994 case Hexagon::A2_orp:
2995 case Hexagon::A2_and:
2996 case Hexagon::A2_andp:
2997 case Hexagon::A2_combinew:
2998 case Hexagon::A4_combineri:
2999 case Hexagon::A4_combineir:
3000 case Hexagon::A2_combineii:
3001 case Hexagon::A4_combineii:
3002 case Hexagon::A2_combine_ll:
3003 case Hexagon::A2_combine_lh:
3004 case Hexagon::A2_combine_hl:
3005 case Hexagon::A2_combine_hh:
3011bool HexagonLoopRescheduling::isStoreInput(
const MachineInstr *
MI,
3012 unsigned InpR)
const {
3013 for (
unsigned i = 0, n =
MI->getNumOperands(); i < n; ++i) {
3014 const MachineOperand &
Op =
MI->getOperand(i);
3017 if (
Op.getReg() == InpR)
3023bool HexagonLoopRescheduling::isShuffleOf(
unsigned OutR,
unsigned InpR)
const {
3024 if (!BTP->
has(OutR) || !BTP->
has(InpR))
3026 const BitTracker::RegisterCell &OutC = BTP->
lookup(OutR);
3027 for (
unsigned i = 0, w = OutC.
width(); i < w; ++i) {
3028 const BitTracker::BitValue &
V = OutC[i];
3031 if (
V.RefI.Reg != InpR)
3037bool HexagonLoopRescheduling::isSameShuffle(
unsigned OutR1,
unsigned InpR1,
3038 unsigned OutR2,
unsigned &InpR2)
const {
3039 if (!BTP->
has(OutR1) || !BTP->
has(InpR1) || !BTP->
has(OutR2))
3041 const BitTracker::RegisterCell &OutC1 = BTP->
lookup(OutR1);
3042 const BitTracker::RegisterCell &OutC2 = BTP->
lookup(OutR2);
3043 unsigned W = OutC1.
width();
3044 unsigned MatchR = 0;
3045 if (W != OutC2.
width())
3047 for (
unsigned i = 0; i <
W; ++i) {
3048 const BitTracker::BitValue &
V1 = OutC1[i], &V2 = OutC2[i];
3053 if (
V1.RefI.Pos != V2.RefI.Pos)
3055 if (
V1.RefI.Reg != InpR1)
3057 if (V2.RefI.Reg == 0 || V2.RefI.Reg == OutR2)
3060 MatchR = V2.RefI.Reg;
3061 else if (V2.RefI.Reg != MatchR)
3068void HexagonLoopRescheduling::moveGroup(InstrGroup &
G, MachineBasicBlock &LB,
3070 unsigned NewPredR) {
3071 DenseMap<unsigned,unsigned> RegMap;
3075 BuildMI(LB, At, At->getDebugLoc(), HII->get(TargetOpcode::PHI), PhiR)
3080 RegMap.
insert(std::make_pair(
G.Inp.Reg, PhiR));
3083 unsigned DR = getDefReg(SI);
3088 auto MIB =
BuildMI(LB, At,
DL, HII->get(
SI->getOpcode()), NewDR);
3089 for (
const MachineOperand &
Op :
SI->operands()) {
3096 unsigned UseR = RegMap[
Op.getReg()];
3097 MIB.addReg(UseR, {},
Op.getSubReg());
3099 RegMap.
insert(std::make_pair(DR, NewDR));
3102 HBS::replaceReg(OldPhiR, RegMap[
G.Out.Reg], *MRI);
3105bool HexagonLoopRescheduling::processLoop(LoopCand &
C) {
3108 std::vector<PhiInfo> Phis;
3109 for (
auto &
I : *
C.LB) {
3112 unsigned PR = getDefReg(&
I);
3115 bool BadUse =
false, GoodUse =
false;
3121 if (isBitShuffle(&UseI, PR) || isStoreInput(&UseI, PR))
3124 if (BadUse || !GoodUse)
3127 Phis.push_back(PhiInfo(
I, *
C.LB));
3131 dbgs() <<
"Phis: {";
3132 for (
auto &
I : Phis) {
3134 <<
printReg(
I.PR.Reg, HRI,
I.PR.Sub) <<
":b" <<
I.PB->getNumber()
3135 <<
',' <<
printReg(
I.LR.Reg, HRI,
I.LR.Sub) <<
":b"
3136 <<
I.LB->getNumber() <<
')';
3152 if (
MI.isTerminator())
3158 HBS::getInstrDefs(
MI, Defs);
3159 if (Defs.count() != 1)
3164 if (!isBitShuffle(&
MI, DefR))
3167 bool BadUse =
false;
3168 for (
auto UI = MRI->
use_begin(DefR), UE = MRI->
use_end(); UI != UE; ++UI) {
3171 if (UseI->
isPHI()) {
3174 unsigned Idx = UI.getOperandNo();
3184 if (
C.EB ==
nullptr)
3193 ShufIns.push_back(&
MI);
3205 using InstrGroupList = std::vector<InstrGroup>;
3208 for (
unsigned i = 0, n = ShufIns.size(); i < n; ++i) {
3209 MachineInstr *
SI = ShufIns[i];
3214 G.Ins.push_back(SI);
3215 G.Out.Reg = getDefReg(SI);
3217 HBS::getInstrUses(*SI, Inputs);
3219 for (
unsigned j = i+1;
j < n; ++
j) {
3220 MachineInstr *
MI = ShufIns[
j];
3224 HBS::getInstrDefs(*
MI, Defs);
3226 if (!Defs.intersects(Inputs))
3230 G.Ins.push_back(
MI);
3231 Inputs.remove(Defs);
3233 HBS::getInstrUses(*
MI, Inputs);
3234 ShufIns[
j] =
nullptr;
3238 if (Inputs.count() > 1)
3240 auto LoopInpEq = [
G] (
const PhiInfo &
P) ->
bool {
3241 return G.Out.Reg ==
P.LR.Reg;
3246 G.Inp.Reg = Inputs.find_first();
3251 for (
unsigned i = 0, n =
Groups.size(); i < n; ++i) {
3253 dbgs() <<
"Group[" << i <<
"] inp: "
3255 <<
" out: " <<
printReg(
G.Out.Reg, HRI,
G.Out.Sub) <<
"\n";
3256 for (
const MachineInstr *
MI :
G.Ins)
3261 for (InstrGroup &
G :
Groups) {
3262 if (!isShuffleOf(
G.Out.Reg,
G.Inp.Reg))
3264 auto LoopInpEq = [
G] (
const PhiInfo &
P) ->
bool {
3265 return G.Out.Reg ==
P.LR.Reg;
3268 if (
F == Phis.end())
3271 if (!isSameShuffle(
G.Out.Reg,
G.Inp.Reg,
F->PR.Reg, PrehR)) {
3272 const MachineInstr *DefPrehR = MRI->
getVRegDef(
F->PR.Reg);
3274 if (
Opc != Hexagon::A2_tfrsi &&
Opc != Hexagon::A2_tfrpi)
3283 unsigned TfrI = (RC == &Hexagon::IntRegsRegClass) ? Hexagon::A2_tfrsi
3284 : Hexagon::A2_tfrpi;
3285 auto T =
C.PB->getFirstTerminator();
3300 moveGroup(
G, *
F->LB, *
F->PB,
F->LB->getFirstNonPHI(),
F->DefR, PrehR);
3307bool HexagonLoopRescheduling::runOnMachineFunction(
MachineFunction &MF) {
3312 HII = HST.getInstrInfo();
3315 const HexagonEvaluator HE(*HRI, *MRI, *HII, MF);
3316 BitTracker
BT(HE, MF);
3321 std::vector<LoopCand> Cand;
3323 for (
auto &
B : MF) {
3324 if (
B.pred_size() != 2 ||
B.succ_size() != 2)
3326 MachineBasicBlock *
PB =
nullptr;
3327 bool IsLoop =
false;
3328 for (MachineBasicBlock *Pred :
B.predecessors()) {
3337 MachineBasicBlock *EB =
nullptr;
3338 for (MachineBasicBlock *Succ :
B.successors()) {
3343 if (Succ->pred_size() == 1)
3348 Cand.push_back(LoopCand(&
B,
PB, EB));
3352 for (
auto &
C : Cand)
3363 return new HexagonLoopRescheduling();
3367 return new HexagonBitSimplify();
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis false
This file implements the BitVector class.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static std::optional< ArrayRef< InsnRange >::iterator > intersects(const MachineInstr *StartMI, const MachineInstr *EndMI, ArrayRef< InsnRange > Ranges, const InstructionOrdering &Ordering)
Check if the instruction range [StartMI, EndMI] intersects any instruction range in Ranges.
static unsigned CountBitSplit
static cl::opt< bool > PreserveTiedOps("hexbit-keep-tied", cl::Hidden, cl::init(true), cl::desc("Preserve subregisters in tied operands"))
static cl::opt< bool > GenExtract("hexbit-extract", cl::Hidden, cl::init(true), cl::desc("Generate extract instructions"))
static cl::opt< unsigned > MaxBitSplit("hexbit-max-bitsplit", cl::Hidden, cl::init(std::numeric_limits< unsigned >::max()))
static cl::opt< bool > GenBitSplit("hexbit-bitsplit", cl::Hidden, cl::init(true), cl::desc("Generate bitsplit instructions"))
static cl::opt< unsigned > MaxExtract("hexbit-max-extract", cl::Hidden, cl::init(std::numeric_limits< unsigned >::max()))
static cl::opt< unsigned > RegisterSetLimit("hexbit-registerset-limit", cl::Hidden, cl::init(1000))
static unsigned CountExtract
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Promote Memory to Register
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Remove Loads Into Fake Uses
bool isDead(const MachineInstr &MI, const MachineRegisterInfo &MRI)
static bool processLoop(Loop &L, const AArch64Subtarget &ST, DataLayout DL)
This file defines the SmallVector class.
SmallSet< unsigned, 4 > RegisterSet
static const X86InstrFMA3Group Groups[]
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
bool subsetOf(const BitVector &RHS) const
Check if This is a subset of RHS.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
FunctionPass class - This class is used to implement most global optimizations.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
unsigned getHexagonSubRegIndex(const TargetRegisterClass &RC, unsigned GenIdx) const
const HexagonRegisterInfo * getRegisterInfo() const override
unsigned getID() const
getID() - Return the register class ID number.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
bool dominates(const MachineInstr *A, const MachineInstr *B) const
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & front() const
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
void setSubReg(unsigned subReg)
unsigned getSubReg() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static MachineOperand CreateImm(int64_t Val)
Register getReg() const
getReg - Returns the register number.
LLVM_ABI void markUsesInDebugValueAsUndef(Register Reg) const
markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the specified register as undefined wh...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI LLVM_READONLY MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
const TargetRegisterInfo * getTargetRegisterInfo() const
use_iterator use_begin(Register RegNo) const
static use_iterator use_end()
iterator_range< use_iterator > use_operands(Register Reg) const
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
std::pair< const_iterator, bool > insert(const unsigned &V)
void push_back(const T &Elt)
TypeSize getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char IsConst[]
Key for Kernel::Arg::Metadata::mIsConst.
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
SmallVector< const MachineInstr * > InstrList
bool isConst(unsigned Opc)
initializer< Ty > init(const Ty &Val)
DXILDebugInfoMap run(Module &M)
LLVM_ABI std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
This is an optimization pass for GlobalISel generic memory operations.
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
bool includes(R1 &&Range1, R2 &&Range2)
Provide wrappers to std::includes which take ranges instead of having to pass begin/end explicitly.
bool isEqual(const GCNRPTracker::LiveRegSet &S1, const GCNRPTracker::LiveRegSet &S2)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
@ Store
The extracted value is stored (ExtractElement only).
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
FunctionPass * createHexagonBitSimplify()
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
FunctionPass * createHexagonLoopRescheduling()
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ Sub
Subtraction of integers.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
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.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
MCRegisterClass TargetRegisterClass
Maximum remaining allocation size observed for a phi node, and how often the allocation size has alre...
bool is(unsigned T) const
static BitValue self(const BitRef &Self=BitRef())
bool has(unsigned Reg) const
const RegisterCell & lookup(unsigned Reg) const
bool reached(const MachineBasicBlock *B) const
void trace(bool On=false)
void put(RegisterRef RR, const RegisterCell &RC)
void visit(const MachineInstr &MI)
RegisterCell get(RegisterRef RR) const