33#define DEBUG_TYPE "hexbit"
45 cl::init(std::numeric_limits<unsigned>::max()));
48 cl::init(std::numeric_limits<unsigned>::max()));
58 RegisterSet() =
default;
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);
93 unsigned T = LRU.front();
101 unsigned Idx = v2x(R);
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);
129 bool has(
unsigned R)
const {
130 unsigned Idx = v2x(R);
139 bool includes(
const RegisterSet &Rs)
const {
141 return !Rs.Bits.test(Bits);
143 bool intersects(
const RegisterSet &Rs)
const {
144 return Bits.anyCommon(Rs.Bits);
149 std::deque<unsigned> LRU;
151 void ensure(
unsigned Idx) {
153 Bits.resize(std::max(
Idx+1, 32U));
156 static inline unsigned v2x(
unsigned v) {
160 static inline unsigned x2v(
unsigned x) {
170 const PrintRegSet &
P);
181 for (
unsigned R =
P.RS.find_first(); R; R =
P.RS.find_next(R))
187 class Transformation;
196 return "Hexagon bit simplification";
207 static void getInstrDefs(
const MachineInstr &
MI, RegisterSet &Defs);
227 static bool getUsedBitsInStore(
unsigned Opc,
BitVector &Bits,
229 static bool getUsedBits(
unsigned Opc,
unsigned OpN,
BitVector &Bits,
242 unsigned NewSub = Hexagon::NoSubRegister);
245 using HBS = HexagonBitSimplify;
250 class Transformation {
254 Transformation(
bool TD) :
TopDown(TD) {}
255 virtual ~Transformation() =
default;
262char HexagonBitSimplify::ID = 0;
265 "Hexagon bit simplification",
false,
false)
272 bool Changed =
false;
275 Changed =
T.processBlock(
B, AVs);
279 getInstrDefs(
I, Defs);
280 RegisterSet NewAVs = AVs;
283 for (
auto *DTN : children<MachineDomTreeNode*>(MDT->getNode(&
B)))
284 Changed |= visitBlock(*(DTN->getBlock()),
T, NewAVs);
287 Changed |=
T.processBlock(
B, AVs);
297 for (
auto &
Op :
MI.operands()) {
298 if (!
Op.isReg() || !
Op.isDef())
309 for (
auto &
Op :
MI.operands()) {
310 if (!
Op.isReg() || !
Op.isUse())
330 if (RC1[B1+i] != RC2[B2+i])
365 auto Begin =
MRI.use_begin(OldR),
End =
MRI.use_end();
367 for (
auto I = Begin;
I !=
End;
I = NextI) {
368 NextI = std::next(
I);
379 if (hasTiedUse(OldR,
MRI, NewSR))
381 auto Begin =
MRI.use_begin(OldR),
End =
MRI.use_end();
383 for (
auto I = Begin;
I !=
End;
I = NextI) {
384 NextI = std::next(
I);
391bool HexagonBitSimplify::replaceSubWithSub(
Register OldR,
unsigned OldSR,
396 if (OldSR != NewSR && hasTiedUse(OldR,
MRI, NewSR))
398 auto Begin =
MRI.use_begin(OldR),
End =
MRI.use_end();
400 for (
auto I = Begin;
I !=
End;
I = NextI) {
401 NextI = std::next(
I);
402 if (
I->getSubReg() != OldSR)
418 Width =
MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC);
424 switch (RC->
getID()) {
425 case Hexagon::DoubleRegsRegClassID:
426 case Hexagon::HvxWRRegClassID:
427 Width =
MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 2;
428 if (RR.
Sub == Hexagon::isub_hi || RR.
Sub == Hexagon::vsub_hi)
440bool HexagonBitSimplify::parseRegSequence(
const MachineInstr &
I,
443 assert(
I.getOpcode() == TargetOpcode::REG_SEQUENCE);
444 unsigned Sub1 =
I.getOperand(2).getImm(), Sub2 =
I.getOperand(4).getImm();
445 auto &DstRC = *
MRI.getRegClass(
I.getOperand(0).getReg());
447 *
MRI.getTargetRegisterInfo());
450 assert((Sub1 == SubLo && Sub2 == SubHi) || (Sub1 == SubHi && Sub2 == SubLo));
451 if (Sub1 == SubLo && Sub2 == SubHi) {
452 SL =
I.getOperand(1);
453 SH =
I.getOperand(3);
456 if (Sub1 == SubHi && Sub2 == SubLo) {
457 SH =
I.getOperand(1);
458 SL =
I.getOperand(3);
470bool HexagonBitSimplify::getUsedBitsInStore(
unsigned Opc,
BitVector &Bits,
472 using namespace Hexagon;
477 case S2_storerbnew_io:
478 case S2_pstorerbt_io:
479 case S2_pstorerbf_io:
480 case S4_pstorerbtnew_io:
481 case S4_pstorerbfnew_io:
482 case S2_pstorerbnewt_io:
483 case S2_pstorerbnewf_io:
484 case S4_pstorerbnewtnew_io:
485 case S4_pstorerbnewfnew_io:
487 case S2_storerbnew_pi:
488 case S2_pstorerbt_pi:
489 case S2_pstorerbf_pi:
490 case S2_pstorerbtnew_pi:
491 case S2_pstorerbfnew_pi:
492 case S2_pstorerbnewt_pi:
493 case S2_pstorerbnewf_pi:
494 case S2_pstorerbnewtnew_pi:
495 case S2_pstorerbnewfnew_pi:
497 case S4_storerbnew_ap:
499 case S2_storerbnew_pr:
501 case S4_storerbnew_ur:
503 case S2_storerbnew_pbr:
505 case S2_storerbnew_pci:
507 case S2_storerbnew_pcr:
509 case S4_storerbnew_rr:
510 case S4_pstorerbt_rr:
511 case S4_pstorerbf_rr:
512 case S4_pstorerbtnew_rr:
513 case S4_pstorerbfnew_rr:
514 case S4_pstorerbnewt_rr:
515 case S4_pstorerbnewf_rr:
516 case S4_pstorerbnewtnew_rr:
517 case S4_pstorerbnewfnew_rr:
519 case S2_storerbnewgp:
520 case S4_pstorerbt_abs:
521 case S4_pstorerbf_abs:
522 case S4_pstorerbtnew_abs:
523 case S4_pstorerbfnew_abs:
524 case S4_pstorerbnewt_abs:
525 case S4_pstorerbnewf_abs:
526 case S4_pstorerbnewtnew_abs:
527 case S4_pstorerbnewfnew_abs:
528 Bits.set(Begin, Begin+8);
533 case S2_storerhnew_io:
534 case S2_pstorerht_io:
535 case S2_pstorerhf_io:
536 case S4_pstorerhtnew_io:
537 case S4_pstorerhfnew_io:
538 case S2_pstorerhnewt_io:
539 case S2_pstorerhnewf_io:
540 case S4_pstorerhnewtnew_io:
541 case S4_pstorerhnewfnew_io:
543 case S2_storerhnew_pi:
544 case S2_pstorerht_pi:
545 case S2_pstorerhf_pi:
546 case S2_pstorerhtnew_pi:
547 case S2_pstorerhfnew_pi:
548 case S2_pstorerhnewt_pi:
549 case S2_pstorerhnewf_pi:
550 case S2_pstorerhnewtnew_pi:
551 case S2_pstorerhnewfnew_pi:
553 case S4_storerhnew_ap:
555 case S2_storerhnew_pr:
557 case S4_storerhnew_ur:
559 case S2_storerhnew_pbr:
561 case S2_storerhnew_pci:
563 case S2_storerhnew_pcr:
565 case S4_pstorerht_rr:
566 case S4_pstorerhf_rr:
567 case S4_pstorerhtnew_rr:
568 case S4_pstorerhfnew_rr:
569 case S4_storerhnew_rr:
570 case S4_pstorerhnewt_rr:
571 case S4_pstorerhnewf_rr:
572 case S4_pstorerhnewtnew_rr:
573 case S4_pstorerhnewfnew_rr:
575 case S2_storerhnewgp:
576 case S4_pstorerht_abs:
577 case S4_pstorerhf_abs:
578 case S4_pstorerhtnew_abs:
579 case S4_pstorerhfnew_abs:
580 case S4_pstorerhnewt_abs:
581 case S4_pstorerhnewf_abs:
582 case S4_pstorerhnewtnew_abs:
583 case S4_pstorerhnewfnew_abs:
584 Bits.set(Begin, Begin+16);
589 case S2_pstorerft_io:
590 case S2_pstorerff_io:
591 case S4_pstorerftnew_io:
592 case S4_pstorerffnew_io:
594 case S2_pstorerft_pi:
595 case S2_pstorerff_pi:
596 case S2_pstorerftnew_pi:
597 case S2_pstorerffnew_pi:
605 case S4_pstorerft_rr:
606 case S4_pstorerff_rr:
607 case S4_pstorerftnew_rr:
608 case S4_pstorerffnew_rr:
610 case S4_pstorerft_abs:
611 case S4_pstorerff_abs:
612 case S4_pstorerftnew_abs:
613 case S4_pstorerffnew_abs:
614 Bits.set(Begin+16, Begin+32);
629bool HexagonBitSimplify::getUsedBits(
unsigned Opc,
unsigned OpN,
631 using namespace Hexagon;
635 if (OpN ==
D.getNumOperands()-1)
636 return getUsedBitsInStore(
Opc, Bits, Begin);
648 Bits.set(Begin, Begin+8);
661 Bits.set(Begin, Begin+16);
669 Bits.set(Begin+16, Begin+32);
679 Bits.set(Begin, Begin+8);
689 case A2_addh_h16_sat_ll:
691 case A2_addh_l16_sat_ll:
694 case A2_subh_h16_sat_ll:
696 case A2_subh_l16_sat_ll:
697 case M2_mpy_acc_ll_s0:
698 case M2_mpy_acc_ll_s1:
699 case M2_mpy_acc_sat_ll_s0:
700 case M2_mpy_acc_sat_ll_s1:
703 case M2_mpy_nac_ll_s0:
704 case M2_mpy_nac_ll_s1:
705 case M2_mpy_nac_sat_ll_s0:
706 case M2_mpy_nac_sat_ll_s1:
707 case M2_mpy_rnd_ll_s0:
708 case M2_mpy_rnd_ll_s1:
709 case M2_mpy_sat_ll_s0:
710 case M2_mpy_sat_ll_s1:
711 case M2_mpy_sat_rnd_ll_s0:
712 case M2_mpy_sat_rnd_ll_s1:
713 case M2_mpyd_acc_ll_s0:
714 case M2_mpyd_acc_ll_s1:
717 case M2_mpyd_nac_ll_s0:
718 case M2_mpyd_nac_ll_s1:
719 case M2_mpyd_rnd_ll_s0:
720 case M2_mpyd_rnd_ll_s1:
721 case M2_mpyu_acc_ll_s0:
722 case M2_mpyu_acc_ll_s1:
725 case M2_mpyu_nac_ll_s0:
726 case M2_mpyu_nac_ll_s1:
727 case M2_mpyud_acc_ll_s0:
728 case M2_mpyud_acc_ll_s1:
731 case M2_mpyud_nac_ll_s0:
732 case M2_mpyud_nac_ll_s1:
733 if (OpN == 1 || OpN == 2) {
734 Bits.set(Begin, Begin+16);
741 case A2_addh_h16_sat_lh:
744 case A2_subh_h16_sat_lh:
745 case M2_mpy_acc_lh_s0:
746 case M2_mpy_acc_lh_s1:
747 case M2_mpy_acc_sat_lh_s0:
748 case M2_mpy_acc_sat_lh_s1:
751 case M2_mpy_nac_lh_s0:
752 case M2_mpy_nac_lh_s1:
753 case M2_mpy_nac_sat_lh_s0:
754 case M2_mpy_nac_sat_lh_s1:
755 case M2_mpy_rnd_lh_s0:
756 case M2_mpy_rnd_lh_s1:
757 case M2_mpy_sat_lh_s0:
758 case M2_mpy_sat_lh_s1:
759 case M2_mpy_sat_rnd_lh_s0:
760 case M2_mpy_sat_rnd_lh_s1:
761 case M2_mpyd_acc_lh_s0:
762 case M2_mpyd_acc_lh_s1:
765 case M2_mpyd_nac_lh_s0:
766 case M2_mpyd_nac_lh_s1:
767 case M2_mpyd_rnd_lh_s0:
768 case M2_mpyd_rnd_lh_s1:
769 case M2_mpyu_acc_lh_s0:
770 case M2_mpyu_acc_lh_s1:
773 case M2_mpyu_nac_lh_s0:
774 case M2_mpyu_nac_lh_s1:
775 case M2_mpyud_acc_lh_s0:
776 case M2_mpyud_acc_lh_s1:
779 case M2_mpyud_nac_lh_s0:
780 case M2_mpyud_nac_lh_s1:
783 case A2_addh_l16_sat_hl:
785 case A2_subh_l16_sat_hl:
787 Bits.set(Begin, Begin+16);
791 Bits.set(Begin+16, Begin+32);
798 case A2_addh_h16_sat_hl:
801 case A2_subh_h16_sat_hl:
802 case M2_mpy_acc_hl_s0:
803 case M2_mpy_acc_hl_s1:
804 case M2_mpy_acc_sat_hl_s0:
805 case M2_mpy_acc_sat_hl_s1:
808 case M2_mpy_nac_hl_s0:
809 case M2_mpy_nac_hl_s1:
810 case M2_mpy_nac_sat_hl_s0:
811 case M2_mpy_nac_sat_hl_s1:
812 case M2_mpy_rnd_hl_s0:
813 case M2_mpy_rnd_hl_s1:
814 case M2_mpy_sat_hl_s0:
815 case M2_mpy_sat_hl_s1:
816 case M2_mpy_sat_rnd_hl_s0:
817 case M2_mpy_sat_rnd_hl_s1:
818 case M2_mpyd_acc_hl_s0:
819 case M2_mpyd_acc_hl_s1:
822 case M2_mpyd_nac_hl_s0:
823 case M2_mpyd_nac_hl_s1:
824 case M2_mpyd_rnd_hl_s0:
825 case M2_mpyd_rnd_hl_s1:
826 case M2_mpyu_acc_hl_s0:
827 case M2_mpyu_acc_hl_s1:
830 case M2_mpyu_nac_hl_s0:
831 case M2_mpyu_nac_hl_s1:
832 case M2_mpyud_acc_hl_s0:
833 case M2_mpyud_acc_hl_s1:
836 case M2_mpyud_nac_hl_s0:
837 case M2_mpyud_nac_hl_s1:
839 Bits.set(Begin+16, Begin+32);
843 Bits.set(Begin, Begin+16);
850 case A2_addh_h16_sat_hh:
853 case A2_subh_h16_sat_hh:
854 case M2_mpy_acc_hh_s0:
855 case M2_mpy_acc_hh_s1:
856 case M2_mpy_acc_sat_hh_s0:
857 case M2_mpy_acc_sat_hh_s1:
860 case M2_mpy_nac_hh_s0:
861 case M2_mpy_nac_hh_s1:
862 case M2_mpy_nac_sat_hh_s0:
863 case M2_mpy_nac_sat_hh_s1:
864 case M2_mpy_rnd_hh_s0:
865 case M2_mpy_rnd_hh_s1:
866 case M2_mpy_sat_hh_s0:
867 case M2_mpy_sat_hh_s1:
868 case M2_mpy_sat_rnd_hh_s0:
869 case M2_mpy_sat_rnd_hh_s1:
870 case M2_mpyd_acc_hh_s0:
871 case M2_mpyd_acc_hh_s1:
874 case M2_mpyd_nac_hh_s0:
875 case M2_mpyd_nac_hh_s1:
876 case M2_mpyd_rnd_hh_s0:
877 case M2_mpyd_rnd_hh_s1:
878 case M2_mpyu_acc_hh_s0:
879 case M2_mpyu_acc_hh_s1:
882 case M2_mpyu_nac_hh_s0:
883 case M2_mpyu_nac_hh_s1:
884 case M2_mpyud_acc_hh_s0:
885 case M2_mpyud_acc_hh_s1:
888 case M2_mpyud_nac_hh_s0:
889 case M2_mpyud_nac_hh_s1:
890 if (OpN == 1 || OpN == 2) {
891 Bits.set(Begin+16, Begin+32);
907 auto *RC =
MRI.getRegClass(RR.
Reg);
911 *
MRI.getTargetRegisterInfo());
919 switch (RC->
getID()) {
920 case Hexagon::DoubleRegsRegClassID:
921 VerifySR(RC, RR.
Sub);
922 return &Hexagon::IntRegsRegClass;
923 case Hexagon::HvxWRRegClassID:
924 VerifySR(RC, RR.
Sub);
925 return &Hexagon::HvxVRRegClass;
939 auto *DRC = getFinalVRegClass(RD,
MRI);
943 return DRC == getFinalVRegClass(RS,
MRI);
952 return Op.getSubReg() != NewSub && Op.isTied();
958 class DeadCodeElimination {
962 MDT(mdt),
MRI(mf.getRegInfo()) {}
965 return runOnNode(MDT.getRootNode());
969 bool isDead(
unsigned R)
const;
980bool DeadCodeElimination::isDead(
unsigned R)
const {
997 bool Changed =
false;
999 for (
auto *DTN : children<MachineDomTreeNode*>(
N))
1000 Changed |= runOnNode(DTN);
1003 std::vector<MachineInstr*> Instrs;
1005 Instrs.push_back(&
MI);
1007 for (
auto *
MI : Instrs) {
1008 unsigned Opc =
MI->getOpcode();
1011 if (
Opc == TargetOpcode::LIFETIME_START ||
1012 Opc == TargetOpcode::LIFETIME_END)
1015 if (
MI->isInlineAsm())
1018 if (!
MI->isPHI() && !
MI->isSafeToMove(Store))
1021 bool AllDead =
true;
1023 for (
auto &
Op :
MI->operands()) {
1024 if (!
Op.isReg() || !
Op.isDef())
1027 if (!
R.isVirtual() || !
isDead(R)) {
1037 for (
unsigned Reg : Regs)
1038 MRI.markUsesInDebugValueAsUndef(Reg);
1057 class RedundantInstrElimination :
public Transformation {
1061 : Transformation(
true), HII(hii), HRI(hri),
MRI(mri),
BT(bt) {}
1067 unsigned &LostB,
unsigned &LostE);
1069 unsigned &LostB,
unsigned &LostE);
1070 bool computeUsedBits(
unsigned Reg,
BitVector &Bits);
1086bool RedundantInstrElimination::isLossyShiftLeft(
const MachineInstr &
MI,
1087 unsigned OpN,
unsigned &LostB,
unsigned &LostE) {
1088 using namespace Hexagon;
1090 unsigned Opc =
MI.getOpcode();
1091 unsigned ImN, RegN, Width;
1098 case S2_asl_i_p_acc:
1099 case S2_asl_i_p_and:
1100 case S2_asl_i_p_nac:
1102 case S2_asl_i_p_xacc:
1112 case S2_addasl_rrri:
1113 case S4_andi_asl_ri:
1115 case S4_addi_asl_ri:
1116 case S4_subi_asl_ri:
1117 case S2_asl_i_r_acc:
1118 case S2_asl_i_r_and:
1119 case S2_asl_i_r_nac:
1121 case S2_asl_i_r_sat:
1122 case S2_asl_i_r_xacc:
1134 assert(
MI.getOperand(ImN).isImm());
1135 unsigned S =
MI.getOperand(ImN).getImm();
1146bool RedundantInstrElimination::isLossyShiftRight(
const MachineInstr &
MI,
1147 unsigned OpN,
unsigned &LostB,
unsigned &LostE) {
1148 using namespace Hexagon;
1150 unsigned Opc =
MI.getOpcode();
1158 case S2_asr_i_p_acc:
1159 case S2_asr_i_p_and:
1160 case S2_asr_i_p_nac:
1162 case S2_lsr_i_p_acc:
1163 case S2_lsr_i_p_and:
1164 case S2_lsr_i_p_nac:
1166 case S2_lsr_i_p_xacc:
1175 case S4_andi_lsr_ri:
1177 case S4_addi_lsr_ri:
1178 case S4_subi_lsr_ri:
1179 case S2_asr_i_r_acc:
1180 case S2_asr_i_r_and:
1181 case S2_asr_i_r_nac:
1183 case S2_lsr_i_r_acc:
1184 case S2_lsr_i_r_and:
1185 case S2_lsr_i_r_nac:
1187 case S2_lsr_i_r_xacc:
1199 assert(
MI.getOperand(ImN).isImm());
1200 unsigned S =
MI.getOperand(ImN).getImm();
1210bool RedundantInstrElimination::computeUsedBits(
unsigned Reg,
BitVector &Bits) {
1213 std::vector<unsigned> Pending;
1214 Pending.push_back(Reg);
1216 for (
unsigned i = 0; i < Pending.size(); ++i) {
1217 unsigned R = Pending[i];
1221 for (
auto I =
MRI.use_begin(R), E =
MRI.use_end();
I != E; ++
I) {
1224 if (!HBS::getSubregMask(UR,
B, W,
MRI))
1231 Pending.push_back(DefR);
1233 if (!computeUsedBits(UseI,
I.getOperandNo(), Used,
B))
1256bool RedundantInstrElimination::computeUsedBits(
const MachineInstr &
MI,
1258 unsigned Opc =
MI.getOpcode();
1260 bool GotBits = HBS::getUsedBits(
Opc, OpN,
T, Begin, HII);
1265 if (isLossyShiftLeft(
MI, OpN, LB, LE) || isLossyShiftRight(
MI, OpN, LB, LE)) {
1266 assert(
MI.getOperand(OpN).isReg());
1269 uint16_t Width = HRI.getRegSizeInBits(*RC);
1272 T.set(Begin, Begin+Width);
1273 assert(LB <= LE && LB < Width && LE <= Width);
1274 T.reset(Begin+LB, Begin+LE);
1290 if (!HBS::getSubregMask(RD, DB, DW,
MRI))
1293 if (!HBS::getSubregMask(RS, SB, SW,
MRI))
1299 if (!computeUsedBits(RD.
Reg, Used))
1302 for (
unsigned i = 0; i != DW; ++i)
1303 if (Used[i+DB] && DC[DB+i] != SC[SB+i])
1309 const RegisterSet&) {
1312 bool Changed =
false;
1314 for (
auto I =
B.begin(), E =
B.end();
I != E; ++
I) {
1317 if (
MI->getOpcode() == TargetOpcode::COPY)
1319 if (
MI->isPHI() ||
MI->hasUnmodeledSideEffects() ||
MI->isInlineAsm())
1321 unsigned NumD =
MI->getDesc().getNumDefs();
1332 for (
auto &
Op :
MI->uses()) {
1338 if (!HBS::isTransparentCopy(RD, RS,
MRI))
1342 if (!HBS::getSubregMask(RS, BN, BW,
MRI))
1346 if (!usedBitsEqual(RD, RS) && !HBS::isEqual(DC, 0, SC, BN, BW))
1354 BuildMI(
B, At,
DL, HII.get(TargetOpcode::COPY), NewR)
1356 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0,
MRI);
1379 class ConstGeneration :
public Transformation {
1383 : Transformation(
true), HII(hii),
MRI(mri),
BT(bt) {}
1401 unsigned Opc =
MI.getOpcode();
1403 case Hexagon::A2_combineii:
1404 case Hexagon::A4_combineii:
1405 case Hexagon::A2_tfrsi:
1406 case Hexagon::A2_tfrpi:
1407 case Hexagon::PS_true:
1408 case Hexagon::PS_false:
1409 case Hexagon::CONST32:
1410 case Hexagon::CONST64:
1423 if (RC == &Hexagon::IntRegsRegClass) {
1424 BuildMI(
B, At,
DL, HII.get(Hexagon::A2_tfrsi), Reg)
1429 if (RC == &Hexagon::DoubleRegsRegClass) {
1431 BuildMI(
B, At,
DL, HII.get(Hexagon::A2_tfrpi), Reg)
1437 if (isInt<8>(
Lo) || isInt<8>(
Hi)) {
1438 unsigned Opc = isInt<8>(
Lo) ? Hexagon::A2_combineii
1439 : Hexagon::A4_combineii;
1449 if (!HST.isTinyCore() ||
1451 BuildMI(
B, At,
DL, HII.get(Hexagon::CONST64), Reg)
1457 if (RC == &Hexagon::PredRegsRegClass) {
1460 Opc = Hexagon::PS_false;
1461 else if ((
C & 0xFF) == 0xFF)
1462 Opc = Hexagon::PS_true;
1475 bool Changed =
false;
1478 for (
auto I =
B.begin(), E =
B.end();
I != E; ++
I) {
1482 HBS::getInstrDefs(*
I, Defs);
1483 if (Defs.count() != 1)
1490 if (HBS::getConst(DRC, 0, DRC.
width(), U)) {
1493 auto At =
I->isPHI() ?
B.getFirstNonPHI() :
I;
1496 HBS::replaceReg(DR, ImmReg,
MRI);
1497 BT.
put(ImmReg, DRC);
1510 class CopyGeneration :
public Transformation {
1514 : Transformation(
true), HII(hii), HRI(hri),
MRI(mri),
BT(bt) {}
1531 class CopyPropagation :
public Transformation {
1534 : Transformation(
false), HRI(hri),
MRI(mri) {}
1538 static bool isCopyReg(
unsigned Opc,
bool NoConv);
1556 auto *FRC = HBS::getFinalVRegClass(Inp,
MRI);
1558 if (!HBS::getSubregMask(Inp,
B, W,
MRI))
1561 for (
Register R = AVs.find_first(); R; R = AVs.find_next(R)) {
1562 if (!
BT.
has(R) || Forbidden[R])
1565 unsigned RW = RC.
width();
1567 if (FRC !=
MRI.getRegClass(R))
1569 if (!HBS::isTransparentCopy(R, Inp,
MRI))
1571 if (!HBS::isEqual(InpRC,
B, RC, 0, W))
1582 if (
MRI.getRegClass(R) != &Hexagon::DoubleRegsRegClass)
1585 if (HBS::isEqual(InpRC,
B, RC, 0, W))
1586 Out.
Sub = Hexagon::isub_lo;
1587 else if (HBS::isEqual(InpRC,
B, RC, W, W))
1588 Out.
Sub = Hexagon::isub_hi;
1592 if (HBS::isTransparentCopy(Out, Inp,
MRI))
1599 const RegisterSet &AVs) {
1603 bool Changed =
false;
1606 for (
auto I =
B.begin(), E =
B.end();
I != E; ++
I, AVB.insert(Defs)) {
1608 HBS::getInstrDefs(*
I, Defs);
1610 unsigned Opc =
I->getOpcode();
1611 if (CopyPropagation::isCopyReg(
Opc,
false) ||
1612 ConstGeneration::isTfrConst(*
I))
1616 auto At =
I->isPHI() ?
B.getFirstNonPHI() :
I;
1618 for (
Register R = Defs.find_first(); R; R = Defs.find_next(R)) {
1620 auto *FRC = HBS::getFinalVRegClass(R,
MRI);
1622 if (findMatch(R, MR, AVB)) {
1624 BuildMI(
B, At,
DL, HII.get(TargetOpcode::COPY), NewR)
1627 HBS::replaceReg(R, NewR,
MRI);
1628 Forbidden.insert(R);
1632 if (FRC == &Hexagon::DoubleRegsRegClass ||
1633 FRC == &Hexagon::HvxWRRegClass) {
1640 if (findMatch(TL,
ML, AVB) && findMatch(TH, MH, AVB)) {
1641 auto *FRC = HBS::getFinalVRegClass(R,
MRI);
1643 BuildMI(
B, At,
DL, HII.get(TargetOpcode::REG_SEQUENCE), NewR)
1649 HBS::replaceReg(R, NewR,
MRI);
1650 Forbidden.insert(R);
1659bool CopyPropagation::isCopyReg(
unsigned Opc,
bool NoConv) {
1661 case TargetOpcode::COPY:
1662 case TargetOpcode::REG_SEQUENCE:
1663 case Hexagon::A4_combineir:
1664 case Hexagon::A4_combineri:
1666 case Hexagon::A2_tfr:
1667 case Hexagon::A2_tfrp:
1668 case Hexagon::A2_combinew:
1669 case Hexagon::V6_vcombine:
1678 bool Changed =
false;
1679 unsigned Opc =
MI.getOpcode();
1681 assert(
MI.getOperand(0).getSubReg() == 0);
1684 case TargetOpcode::COPY:
1685 case Hexagon::A2_tfr:
1686 case Hexagon::A2_tfrp: {
1688 if (!HBS::isTransparentCopy(RD, RS,
MRI))
1691 Changed = HBS::replaceRegWithSub(RD.
Reg, RS.
Reg, RS.
Sub,
MRI);
1693 Changed = HBS::replaceReg(RD.
Reg, RS.
Reg,
MRI);
1696 case TargetOpcode::REG_SEQUENCE: {
1698 if (HBS::parseRegSequence(
MI, SL, SH,
MRI)) {
1702 Changed = HBS::replaceSubWithSub(RD.
Reg, SubLo, SL.
Reg, SL.
Sub,
MRI);
1703 Changed |= HBS::replaceSubWithSub(RD.
Reg, SubHi, SH.
Reg, SH.
Sub,
MRI);
1707 case Hexagon::A2_combinew:
1708 case Hexagon::V6_vcombine: {
1713 Changed = HBS::replaceSubWithSub(RD.
Reg, SubLo, RL.Reg, RL.Sub,
MRI);
1714 Changed |= HBS::replaceSubWithSub(RD.
Reg, SubHi, RH.
Reg, RH.
Sub,
MRI);
1717 case Hexagon::A4_combineir:
1718 case Hexagon::A4_combineri: {
1719 unsigned SrcX = (
Opc == Hexagon::A4_combineir) ? 2 : 1;
1720 unsigned Sub = (
Opc == Hexagon::A4_combineir) ? Hexagon::isub_lo
1731 std::vector<MachineInstr*> Instrs;
1733 Instrs.push_back(&
MI);
1735 bool Changed =
false;
1736 for (
auto *
I : Instrs) {
1737 unsigned Opc =
I->getOpcode();
1738 if (!CopyPropagation::isCopyReg(
Opc,
true))
1740 Changed |= propagateRegCopy(*
I);
1751 class BitSimplification :
public Transformation {
1756 : Transformation(
true), MDT(mdt), HII(hii), HRI(hri),
MRI(mri),
1767 unsigned B, RegHalf &RH);
1772 unsigned getCombineOpcode(
bool HLow,
bool LLow);
1794 std::vector<MachineInstr*> NewMIs;
1809bool BitSimplification::matchHalf(
unsigned SelfR,
1820 while (
I <
B+16 && RC[
I].num())
1826 unsigned P = RC[
I].RefI.Pos;
1829 unsigned Pos =
P - (
I-
B);
1831 if (Reg == 0 || Reg == SelfR)
1833 if (!
Reg.isVirtual())
1839 if (Pos+16 > SC.
width())
1842 for (
unsigned i = 0; i < 16; ++i) {
1851 if (RC[i+
B] != SC[i+Pos])
1858 Sub = Hexagon::isub_lo;
1862 Sub = Hexagon::isub_lo;
1866 Sub = Hexagon::isub_hi;
1870 Sub = Hexagon::isub_hi;
1881 if (!HBS::getFinalVRegClass(RH,
MRI))
1889 auto *OpRC = HII.getRegClass(HII.get(
Opc), OpNum, &HRI, MF);
1890 auto *RRC = HBS::getFinalVRegClass(R,
MRI);
1891 return OpRC->hasSubClassEq(RRC);
1896bool BitSimplification::matchPackhl(
unsigned SelfR,
1899 RegHalf L1, H1, L2, H2;
1901 if (!matchHalf(SelfR, RC, 0, L2) || !matchHalf(SelfR, RC, 16, L1))
1903 if (!matchHalf(SelfR, RC, 32, H2) || !matchHalf(SelfR, RC, 48, H1))
1907 if (H1.Reg != L1.Reg || H1.Sub != L1.Sub || H1.Low || !L1.Low)
1909 if (H2.Reg != L2.Reg || H2.Sub != L2.Sub || H2.Low || !L2.Low)
1917unsigned BitSimplification::getCombineOpcode(
bool HLow,
bool LLow) {
1918 return HLow ? LLow ? Hexagon::A2_combine_ll
1919 : Hexagon::A2_combine_lh
1920 : LLow ? Hexagon::A2_combine_hl
1921 : Hexagon::A2_combine_hh;
1928 unsigned Opc =
MI->getOpcode();
1929 if (
Opc != Hexagon::S2_storerh_io)
1938 unsigned B = (RS.
Sub == Hexagon::isub_hi) ? 32 : 0;
1939 if (!matchHalf(0, RC,
B,
H))
1943 MI->setDesc(HII.get(Hexagon::S2_storerf_io));
1952 unsigned Opc =
MI->getOpcode();
1955 case Hexagon::S2_storeri_io:
1958 case Hexagon::S2_storerh_io:
1961 case Hexagon::S2_storerb_io:
1968 if (!
MI->getOperand(0).isReg())
1984 if (!HBS::getConst(RC, 0, RC.
width(), U))
1990 case Hexagon::S2_storerb_io:
1993 case Hexagon::S2_storerh_io:
1996 case Hexagon::S2_storeri_io:
2007 MI->removeOperand(2);
2009 case Hexagon::S2_storerb_io:
2010 MI->setDesc(HII.get(Hexagon::S4_storeirb_io));
2012 case Hexagon::S2_storerh_io:
2013 MI->setDesc(HII.get(Hexagon::S4_storeirh_io));
2015 case Hexagon::S2_storeri_io:
2016 MI->setDesc(HII.get(Hexagon::S4_storeiri_io));
2029 unsigned Opc =
MI->getOpcode();
2030 if (
Opc == Hexagon::S2_packhl)
2033 if (!matchPackhl(RD.
Reg, RC, Rs, Rt))
2035 if (!validateReg(Rs, Hexagon::S2_packhl, 1) ||
2036 !validateReg(Rt, Hexagon::S2_packhl, 2))
2040 Register NewR =
MRI.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
2042 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2044 BuildMI(
B, At,
DL, HII.get(Hexagon::S2_packhl), NewR)
2047 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0,
MRI);
2058 if (!matchHalf(RD.
Reg, RC, 0, L) || !HBS::isZero(RC, 16, 16))
2061 unsigned Opc =
MI->getOpcode();
2068 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2070 if (
L.Low &&
Opc != Hexagon::A2_zxth) {
2071 if (validateReg(L, Hexagon::A2_zxth, 1)) {
2072 NewR =
MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2073 BuildMI(
B, At,
DL, HII.get(Hexagon::A2_zxth), NewR)
2076 }
else if (!
L.Low &&
Opc != Hexagon::S2_lsr_i_r) {
2077 if (validateReg(L, Hexagon::S2_lsr_i_r, 1)) {
2078 NewR =
MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2086 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0,
MRI);
2097 if (!matchHalf(RD.
Reg, RC, 0, L) || !matchHalf(RD.
Reg, RC, 16,
H))
2100 if (
L.Reg ==
H.Reg &&
L.Sub ==
H.Sub && !
H.Low &&
L.Low)
2103 unsigned Opc =
MI->getOpcode();
2104 unsigned COpc = getCombineOpcode(
H.Low,
L.Low);
2107 if (!validateReg(
H, COpc, 1) || !validateReg(L, COpc, 2))
2112 Register NewR =
MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2113 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2118 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0,
MRI);
2127 unsigned Opc =
MI->getOpcode();
2129 case Hexagon::A2_zxtb:
2130 case Hexagon::A2_zxth:
2131 case Hexagon::S2_extractu:
2134 if (
Opc == Hexagon::A2_andir &&
MI->getOperand(2).isImm()) {
2135 int32_t
Imm =
MI->getOperand(2).getImm();
2140 if (
MI->hasUnmodeledSideEffects() ||
MI->isInlineAsm())
2143 while (W > 0 && RC[W-1].is(0))
2145 if (W == 0 || W == RC.
width())
2147 unsigned NewOpc = (
W == 8) ? Hexagon::A2_zxtb
2148 : (W == 16) ? Hexagon::A2_zxth
2149 : (
W < 10) ? Hexagon::A2_andir
2150 : Hexagon::S2_extractu;
2154 for (
auto &
Op :
MI->uses()) {
2162 if (!HBS::getSubregMask(RS, BN, BW,
MRI))
2164 if (BW < W || !HBS::isEqual(RC, 0, SC, BN, W))
2166 if (!validateReg(RS, NewOpc, 1))
2169 Register NewR =
MRI.createVirtualRegister(&Hexagon::IntRegsRegClass);
2170 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2172 auto MIB =
BuildMI(
B, At,
DL, HII.get(NewOpc), NewR)
2174 if (NewOpc == Hexagon::A2_andir)
2175 MIB.
addImm((1 << W) - 1);
2176 else if (NewOpc == Hexagon::S2_extractu)
2178 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0,
MRI);
2187 const RegisterSet &AVs) {
2195 unsigned Opc =
MI->getOpcode();
2197 case Hexagon::A4_bitsplit:
2198 case Hexagon::A4_bitspliti:
2207 unsigned Z =
C.width();
2208 while (Z > 0 &&
C[Z-1].is(0))
2210 return C.width() -
Z;
2214 unsigned Z = ctlz(RC);
2215 if (Z == 0 || Z == W)
2229 for (
unsigned i = 1; i <
W-
Z; ++i) {
2233 if (
V.RefI.Reg != SrcR ||
V.RefI.Pos != Pos+i)
2238 for (
unsigned S = AVs.find_first(); S; S = AVs.find_next(S)) {
2241 unsigned SRC =
MRI.getRegClass(S)->getID();
2242 if (SRC != Hexagon::IntRegsRegClassID &&
2243 SRC != Hexagon::DoubleRegsRegClassID)
2248 if (SC.
width() != W || ctlz(SC) != W-Z)
2256 if (Pos <=
P && (Pos + W-Z) !=
P)
2258 if (
P < Pos && (
P + Z) != Pos)
2261 if (std::min(
P, Pos) != 0 && std::min(
P, Pos) != 32)
2265 for (
I = 1;
I <
Z; ++
I) {
2269 if (
V.RefI.Reg != SrcR ||
V.RefI.Pos !=
P+
I)
2282 auto At = DefS->
isPHI() ?
B.getFirstNonPHI()
2284 if (
MRI.getRegClass(SrcR)->getID() == Hexagon::DoubleRegsRegClassID)
2285 SrcSR = (std::min(Pos,
P) == 32) ? Hexagon::isub_hi : Hexagon::isub_lo;
2286 if (!validateReg({SrcR,SrcSR}, Hexagon::A4_bitspliti, 1))
2288 unsigned ImmOp = Pos <=
P ?
W-
Z :
Z;
2293 if (
In->getOpcode() != Hexagon::A4_bitspliti)
2298 if (
In->getOperand(2).getImm() != ImmOp)
2304 if (!MDT.dominates(DefI, &*At))
2313 NewR =
MRI.createVirtualRegister(&Hexagon::DoubleRegsRegClass);
2314 auto NewBS =
BuildMI(
B, At,
DL, HII.get(Hexagon::A4_bitspliti), NewR)
2317 NewMIs.push_back(NewBS);
2320 HBS::replaceRegWithSub(RD.
Reg, NewR, Hexagon::isub_lo,
MRI);
2321 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_hi,
MRI);
2323 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_lo,
MRI);
2324 HBS::replaceRegWithSub(RD.
Reg, NewR, Hexagon::isub_hi,
MRI);
2340 unsigned Opc =
MI->getOpcode();
2341 if (
Opc != Hexagon::S2_tstbit_i)
2344 unsigned BN =
MI->getOperand(2).getImm();
2348 if (!
BT.
has(RS.
Reg) || !HBS::getSubregMask(RS,
F, W,
MRI))
2351 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2361 unsigned P = std::numeric_limits<unsigned>::max();
2363 if (TC == &Hexagon::DoubleRegsRegClass) {
2365 RR.
Sub = Hexagon::isub_lo;
2368 RR.
Sub = Hexagon::isub_hi;
2370 }
else if (TC == &Hexagon::IntRegsRegClass) {
2373 if (
P != std::numeric_limits<unsigned>::max()) {
2374 Register NewR =
MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
2375 BuildMI(
B, At,
DL, HII.get(Hexagon::S2_tstbit_i), NewR)
2378 HBS::replaceReg(RD.
Reg, NewR,
MRI);
2382 }
else if (
V.is(0) ||
V.is(1)) {
2383 Register NewR =
MRI.createVirtualRegister(&Hexagon::PredRegsRegClass);
2384 unsigned NewOpc =
V.is(0) ? Hexagon::PS_false : Hexagon::PS_true;
2386 HBS::replaceReg(RD.
Reg, NewR,
MRI);
2400 const RegisterSet &AVs) {
2418 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2438 for (
unsigned I = 0;
I !=
W; ++
I) {
2447 if (TopV.
is(0) || TopV.
is(1)) {
2448 bool S = TopV.
is(1);
2449 for (--W;
W > 0 && RC[
W-1].is(S); --
W)
2461 for (--W;
W > 0 && RC[
W-1] == TopV; --
W)
2476 dbgs() <<
"Cell: " << RC <<
'\n';
2477 dbgs() <<
"Expected bitfield size: " <<
Len <<
" bits, "
2478 << (
Signed ?
"sign" :
"zero") <<
"-extended\n";
2481 bool Changed =
false;
2483 for (
unsigned R = AVs.find_first();
R != 0;
R = AVs.find_next(R)) {
2487 unsigned SW = SC.
width();
2493 if (SW < RW || (SW % RW) != 0)
2500 while (Off <= SW-Len) {
2501 unsigned OE = (
Off+
Len)/RW;
2512 if (HBS::isEqual(RC, 0, SC, Off, Len))
2521 unsigned ExtOpc = 0;
2524 ExtOpc =
Signed ? Hexagon::A2_sxtb : Hexagon::A2_zxtb;
2526 ExtOpc =
Signed ? Hexagon::A2_sxth : Hexagon::A2_zxth;
2527 else if (Len < 10 && !
Signed)
2528 ExtOpc = Hexagon::A2_andir;
2532 Signed ? (RW == 32 ? Hexagon::S4_extract : Hexagon::S4_extractp)
2533 : (RW == 32 ? Hexagon::S2_extractu : Hexagon::S2_extractup);
2537 if (RW != SW && RW*2 != SW)
2540 SR = (
Off/RW == 0) ? Hexagon::isub_lo : Hexagon::isub_hi;
2543 if (!validateReg({
R,SR}, ExtOpc, 1))
2547 if (
MI->getOpcode() == ExtOpc) {
2557 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2559 auto MIB =
BuildMI(
B, At,
DL, HII.get(ExtOpc), NewR)
2562 case Hexagon::A2_sxtb:
2563 case Hexagon::A2_zxtb:
2564 case Hexagon::A2_sxth:
2565 case Hexagon::A2_zxth:
2567 case Hexagon::A2_andir:
2568 MIB.
addImm((1u << Len) - 1);
2570 case Hexagon::S4_extract:
2571 case Hexagon::S2_extractu:
2572 case Hexagon::S4_extractp:
2573 case Hexagon::S2_extractup:
2581 HBS::replaceReg(RD.
Reg, NewR,
MRI);
2592 unsigned Opc =
MI->getOpcode();
2593 if (
Opc != Hexagon::A4_rcmpeqi &&
Opc != Hexagon::A4_rcmpneqi)
2600 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2606 auto At =
MI->isPHI() ?
B.getFirstNonPHI()
2609 bool KnownNZ =
false;
2616 if (!HBS::getSubregMask(SR,
F, W,
MRI))
2627 auto ReplaceWithConst = [&](
int C) {
2629 BuildMI(
B, At,
DL, HII.get(Hexagon::A2_tfrsi), NewR)
2631 HBS::replaceReg(RD.
Reg, NewR,
MRI);
2642 if (
Op.isGlobal() ||
Op.isBlockAddress())
2645 return Op.getImm() != 0;
2647 return !
Op.getCImm()->isZero();
2649 return !
Op.getFPImm()->isZero();
2654 if (
Op.isGlobal() ||
Op.isBlockAddress())
2657 return Op.getImm() == 0;
2659 return Op.getCImm()->isZero();
2661 return Op.getFPImm()->isZero();
2667 if (KnownZ || KnownNZ) {
2668 assert(KnownZ != KnownNZ &&
"Register cannot be both 0 and non-0");
2669 return ReplaceWithConst(KnownZ == (
Opc == Hexagon::A4_rcmpeqi));
2677 if (SR.
Sub == 0 && InpDef->
getOpcode() == Hexagon::C2_muxii) {
2681 bool KnownNZ1 = IsNonZero(Src1), KnownNZ2 = IsNonZero(Src2);
2682 if (KnownNZ1 && KnownNZ2)
2683 return ReplaceWithConst(
Opc == Hexagon::A4_rcmpneqi);
2685 bool KnownZ1 = IsZero(Src1), KnownZ2 = IsZero(Src2);
2686 if (KnownZ1 && KnownZ2)
2687 return ReplaceWithConst(
Opc == Hexagon::A4_rcmpeqi);
2692 if ((KnownZ1 || KnownNZ1) && (KnownZ2 || KnownNZ2)) {
2694 BuildMI(
B, At,
DL, HII.get(Hexagon::C2_muxii), NewR)
2696 .
addImm(KnownZ1 == (
Opc == Hexagon::A4_rcmpeqi))
2697 .
addImm(KnownZ2 == (
Opc == Hexagon::A4_rcmpeqi));
2698 HBS::replaceReg(RD.
Reg, NewR,
MRI);
2712 const RegisterSet &AVs) {
2715 bool Changed =
false;
2719 for (
auto I =
B.begin(), E =
B.end();
I != E; ++
I, AVB.insert(Defs)) {
2722 HBS::getInstrDefs(*
MI, Defs);
2724 unsigned Opc =
MI->getOpcode();
2725 if (
Opc == TargetOpcode::COPY ||
Opc == TargetOpcode::REG_SEQUENCE)
2728 if (
MI->mayStore()) {
2729 bool T = genStoreUpperHalf(
MI);
2730 T =
T || genStoreImmediate(
MI);
2735 if (Defs.count() != 1)
2746 if (FRC->
getID() == Hexagon::DoubleRegsRegClassID) {
2747 bool T = genPackhl(
MI, RD, RC);
2748 T =
T || simplifyExtractLow(
MI, RD, RC, AVB);
2753 if (FRC->
getID() == Hexagon::IntRegsRegClassID) {
2754 bool T = genBitSplit(
MI, RD, RC, AVB);
2755 T =
T || simplifyExtractLow(
MI, RD, RC, AVB);
2756 T =
T || genExtractHalf(
MI, RD, RC);
2757 T =
T || genCombineHalf(
MI, RD, RC);
2758 T =
T || genExtractLow(
MI, RD, RC);
2759 T =
T || simplifyRCmp0(
MI, RD);
2764 if (FRC->
getID() == Hexagon::PredRegsRegClassID) {
2765 bool T = simplifyTstbit(
MI, RD, RC);
2779 auto &HII = *HST.getInstrInfo();
2781 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
2785 Changed = DeadCodeElimination(MF, *MDT).run();
2795 ConstGeneration ImmG(
BT, HII,
MRI);
2796 Changed |= visitBlock(Entry, ImmG, AIG);
2799 RedundantInstrElimination RIE(
BT, HII, HRI,
MRI);
2800 bool Ried = visitBlock(Entry, RIE, ARE);
2807 CopyGeneration CopyG(
BT, HII, HRI,
MRI);
2808 Changed |= visitBlock(Entry, CopyG, ACG);
2811 CopyPropagation CopyP(HRI,
MRI);
2812 Changed |= visitBlock(Entry, CopyP, ACP);
2814 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2818 BitSimplification BitS(
BT, *MDT, HII, HRI,
MRI, MF);
2819 Changed |= visitBlock(Entry, BitS, ABS);
2821 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2827 DeadCodeElimination(MF, *MDT).run();
2916 using InstrList = std::vector<MachineInstr *>;
2930 bool isConst(
unsigned Reg)
const;
2933 bool isShuffleOf(
unsigned OutR,
unsigned InpR)
const;
2934 bool isSameShuffle(
unsigned OutR1,
unsigned InpR1,
unsigned OutR2,
2935 unsigned &InpR2)
const;
2938 bool processLoop(LoopCand &
C);
2943char HexagonLoopRescheduling::ID = 0;
2946 "Hexagon Loop Rescheduling",
false,
false)
2950 DefR = HexagonLoopRescheduling::getDefReg(&
P);
2953 for (
unsigned i = 1, n =
P.getNumOperands(); i < n; i += 2) {
2956 LR =
P.getOperand(i);
2960 PR =
P.getOperand(i);
2964unsigned HexagonLoopRescheduling::getDefReg(
const MachineInstr *
MI) {
2966 HBS::getInstrDefs(*
MI, Defs);
2967 if (Defs.count() != 1)
2969 return Defs.find_first();
2972bool HexagonLoopRescheduling::isConst(
unsigned Reg)
const {
2976 for (
unsigned i = 0, w = RC.
width(); i < w; ++i) {
2978 if (!
V.is(0) && !
V.is(1))
2984bool HexagonLoopRescheduling::isBitShuffle(
const MachineInstr *
MI,
2985 unsigned DefR)
const {
2986 unsigned Opc =
MI->getOpcode();
2988 case TargetOpcode::COPY:
2989 case Hexagon::S2_lsr_i_r:
2990 case Hexagon::S2_asr_i_r:
2991 case Hexagon::S2_asl_i_r:
2992 case Hexagon::S2_lsr_i_p:
2993 case Hexagon::S2_asr_i_p:
2994 case Hexagon::S2_asl_i_p:
2995 case Hexagon::S2_insert:
2996 case Hexagon::A2_or:
2997 case Hexagon::A2_orp:
2998 case Hexagon::A2_and:
2999 case Hexagon::A2_andp:
3000 case Hexagon::A2_combinew:
3001 case Hexagon::A4_combineri:
3002 case Hexagon::A4_combineir:
3003 case Hexagon::A2_combineii:
3004 case Hexagon::A4_combineii:
3005 case Hexagon::A2_combine_ll:
3006 case Hexagon::A2_combine_lh:
3007 case Hexagon::A2_combine_hl:
3008 case Hexagon::A2_combine_hh:
3014bool HexagonLoopRescheduling::isStoreInput(
const MachineInstr *
MI,
3015 unsigned InpR)
const {
3016 for (
unsigned i = 0, n =
MI->getNumOperands(); i < n; ++i) {
3020 if (
Op.getReg() == InpR)
3026bool HexagonLoopRescheduling::isShuffleOf(
unsigned OutR,
unsigned InpR)
const {
3027 if (!BTP->has(OutR) || !BTP->has(InpR))
3030 for (
unsigned i = 0, w = OutC.
width(); i < w; ++i) {
3034 if (
V.RefI.Reg != InpR)
3040bool HexagonLoopRescheduling::isSameShuffle(
unsigned OutR1,
unsigned InpR1,
3041 unsigned OutR2,
unsigned &InpR2)
const {
3042 if (!BTP->has(OutR1) || !BTP->has(InpR1) || !BTP->has(OutR2))
3046 unsigned W = OutC1.
width();
3047 unsigned MatchR = 0;
3048 if (W != OutC2.
width())
3050 for (
unsigned i = 0; i <
W; ++i) {
3056 if (V1.
RefI.
Pos != V2.RefI.Pos)
3060 if (V2.RefI.Reg == 0 || V2.RefI.Reg == OutR2)
3063 MatchR = V2.RefI.Reg;
3064 else if (V2.RefI.Reg != MatchR)
3073 unsigned NewPredR) {
3077 Register PhiR =
MRI->createVirtualRegister(PhiRC);
3078 BuildMI(LB, At, At->getDebugLoc(), HII->get(TargetOpcode::PHI), PhiR)
3083 RegMap.
insert(std::make_pair(
G.Inp.Reg, PhiR));
3086 unsigned DR = getDefReg(SI);
3091 auto MIB =
BuildMI(LB, At,
DL, HII->get(
SI->getOpcode()), NewDR);
3099 unsigned UseR = RegMap[
Op.getReg()];
3100 MIB.addReg(UseR, 0,
Op.getSubReg());
3102 RegMap.
insert(std::make_pair(DR, NewDR));
3105 HBS::replaceReg(OldPhiR, RegMap[
G.Out.Reg], *
MRI);
3108bool HexagonLoopRescheduling::processLoop(LoopCand &
C) {
3111 std::vector<PhiInfo> Phis;
3112 for (
auto &
I : *
C.LB) {
3115 unsigned PR = getDefReg(&
I);
3118 bool BadUse =
false, GoodUse =
false;
3125 if (isBitShuffle(UseI, PR) || isStoreInput(UseI, PR))
3128 if (BadUse || !GoodUse)
3135 dbgs() <<
"Phis: {";
3136 for (
auto &
I : Phis) {
3138 <<
printReg(
I.PR.Reg, HRI,
I.PR.Sub) <<
":b" <<
I.PB->getNumber()
3139 <<
',' <<
printReg(
I.LR.Reg, HRI,
I.LR.Sub) <<
":b"
3140 <<
I.LB->getNumber() <<
')';
3148 bool Changed =
false;
3156 if (
MI.isTerminator())
3162 HBS::getInstrDefs(
MI, Defs);
3163 if (Defs.count() != 1)
3168 if (!isBitShuffle(&
MI, DefR))
3171 bool BadUse =
false;
3172 for (
auto UI =
MRI->use_begin(DefR), UE =
MRI->use_end(); UI != UE; ++UI) {
3175 if (UseI->
isPHI()) {
3178 unsigned Idx = UI.getOperandNo();
3188 if (
C.EB ==
nullptr)
3197 ShufIns.push_back(&
MI);
3209 using InstrGroupList = std::vector<InstrGroup>;
3212 for (
unsigned i = 0, n = ShufIns.size(); i < n; ++i) {
3218 G.Ins.push_back(SI);
3219 G.Out.Reg = getDefReg(SI);
3221 HBS::getInstrUses(*SI, Inputs);
3223 for (
unsigned j = i+1;
j < n; ++
j) {
3228 HBS::getInstrDefs(*
MI, Defs);
3230 if (!Defs.intersects(Inputs))
3234 G.Ins.push_back(
MI);
3235 Inputs.remove(Defs);
3237 HBS::getInstrUses(*
MI, Inputs);
3238 ShufIns[
j] =
nullptr;
3242 if (Inputs.count() > 1)
3244 auto LoopInpEq = [
G] (
const PhiInfo &
P) ->
bool {
3245 return G.Out.Reg ==
P.LR.Reg;
3250 G.Inp.Reg = Inputs.find_first();
3255 for (
unsigned i = 0, n =
Groups.size(); i < n; ++i) {
3256 InstrGroup &G = Groups[i];
3257 dbgs() <<
"Group[" << i <<
"] inp: "
3258 << printReg(G.Inp.Reg, HRI, G.Inp.Sub)
3259 <<
" out: " << printReg(G.Out.Reg, HRI, G.Out.Sub) <<
"\n";
3260 for (const MachineInstr *MI : G.Ins)
3261 dbgs() <<
" " << MI;
3265 for (InstrGroup &
G :
Groups) {
3266 if (!isShuffleOf(
G.Out.Reg,
G.Inp.Reg))
3268 auto LoopInpEq = [
G] (
const PhiInfo &
P) ->
bool {
3269 return G.Out.Reg ==
P.LR.Reg;
3272 if (
F == Phis.end())
3275 if (!isSameShuffle(
G.Out.Reg,
G.Inp.Reg,
F->PR.Reg, PrehR)) {
3278 if (
Opc != Hexagon::A2_tfrsi &&
Opc != Hexagon::A2_tfrpi)
3285 if (RC !=
MRI->getRegClass(
F->PR.Reg)) {
3286 PrehR =
MRI->createVirtualRegister(RC);
3287 unsigned TfrI = (RC == &Hexagon::IntRegsRegClass) ? Hexagon::A2_tfrsi
3288 : Hexagon::A2_tfrpi;
3289 auto T =
C.PB->getFirstTerminator();
3302 if (
MRI->getRegClass(PrehR) !=
MRI->getRegClass(
G.Inp.Reg))
3304 moveGroup(
G, *
F->LB, *
F->PB,
F->LB->getFirstNonPHI(),
F->DefR, PrehR);
3311bool HexagonLoopRescheduling::runOnMachineFunction(
MachineFunction &MF) {
3316 HII = HST.getInstrInfo();
3325 std::vector<LoopCand> Cand;
3327 for (
auto &
B : MF) {
3328 if (
B.pred_size() != 2 ||
B.succ_size() != 2)
3331 bool IsLoop =
false;
3347 if (Succ->pred_size() == 1)
3355 bool Changed =
false;
3356 for (
auto &
C : Cand)
3357 Changed |= processLoop(
C);
3367 return new HexagonLoopRescheduling();
3371 return new HexagonBitSimplify();
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_ATTRIBUTE_UNUSED
static std::optional< ArrayRef< InsnRange >::iterator > intersects(const MachineInstr *StartMI, const MachineInstr *EndMI, const ArrayRef< InsnRange > &Ranges, const InstructionOrdering &Ordering)
Check if the instruction range [StartMI, EndMI] intersects any instruction range in Ranges.
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
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()))
hexagon bit Hexagon bit simplification
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)
Register const TargetRegisterInfo * TRI
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)
This file defines the SmallVector class.
static const X86InstrFMA3Group Groups[]
support::ulittle16_t & Lo
support::ulittle16_t & Hi
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
This class represents an Operation in the Expression.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Base class for the actual dominator tree node.
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
Describe properties that are true of each instruction in the target description file.
void push_back(MachineInstr *MI)
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
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 & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register 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
bool isDebugInstr() 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.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Wrapper class representing virtual and physical registers.
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
unsigned getID() const
Return the register class ID number.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
The instances of the Type class are immutable: once they are created, they are never changed.
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 IsConst[]
Key for Kernel::Arg::Metadata::mIsConst.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
@ ABS
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth.
SmallVector< const MachineInstr * > InstrList
bool isConst(unsigned Opc)
Reg
All possible values of the reg field in the ModR/M byte.
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
std::set< RegisterRef > RegisterSet
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 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.
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.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
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.
Maximum remaining allocation size observed for a phi node, and how often the allocation size has alre...
This struct is a compact representation of a valid (non-zero power of two) alignment.
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