43 #define DEBUG_TYPE "hexbit"
79 unsigned find_first()
const {
86 unsigned find_next(
unsigned Prev)
const {
94 unsigned Idx = v2x(R);
99 unsigned Idx = v2x(R);
112 reference operator[](
unsigned R) {
113 unsigned Idx = v2x(R);
117 bool operator[](
unsigned R)
const {
118 unsigned Idx = v2x(R);
122 bool has(
unsigned R)
const {
123 unsigned Idx = v2x(R);
134 return !Rs.BitVector::test(*
this);
141 void ensure(
unsigned Idx) {
146 static inline unsigned v2x(
unsigned v) {
150 static inline unsigned x2v(
unsigned x) {
160 const PrintRegSet &
P);
171 for (
unsigned R = P.RS.find_first(); R; R = P.RS.find_next(R))
177 class Transformation;
188 return "Hexagon bit simplification";
206 uint16_t W, uint64_t &U);
207 static bool replaceReg(
unsigned OldR,
unsigned NewR,
211 static bool replaceRegWithSub(
unsigned OldR,
unsigned NewR,
213 static bool replaceSubWithSub(
unsigned OldR,
unsigned OldSR,
219 static bool getUsedBitsInStore(
unsigned Opc,
BitVector &
Bits,
221 static bool getUsedBits(
unsigned Opc,
unsigned OpN,
BitVector &
Bits,
234 unsigned NewSub = Hexagon::NoSubRegister);
238 typedef HexagonBitSimplify HBS;
243 class Transformation {
247 Transformation(
bool TD) : TopDown(TD) {}
248 virtual ~Transformation() =
default;
256 "Hexagon bit simplification",
false,
false)
263 bool Changed =
false;
266 Changed = T.processBlock(B, AVs);
270 getInstrDefs(
I, Defs);
274 for (
auto *DTN : children<MachineDomTreeNode*>(MDT->getNode(&B)))
275 Changed |= visitBlock(*(DTN->getBlock()), T, NewAVs);
278 Changed |= T.processBlock(B, AVs);
289 if (!
Op.isReg() || !
Op.isDef())
291 unsigned R =
Op.getReg();
298 void HexagonBitSimplify::getInstrUses(
const MachineInstr &MI,
301 if (!
Op.isReg() || !
Op.isUse())
303 unsigned R =
Op.getReg();
314 for (uint16_t i = 0; i < W; ++i) {
321 if (RC1[B1+i] != RC2[B2+i])
328 uint16_t
B, uint16_t W) {
330 for (uint16_t i = B; i < B+W; ++i)
337 uint16_t B, uint16_t W, uint64_t &U) {
340 for (uint16_t i = B+W; i >
B; --i) {
352 bool HexagonBitSimplify::replaceReg(
unsigned OldR,
unsigned NewR,
359 for (auto
I = Begin;
I !=
End;
I = NextI) {
360 NextI = std::next(
I);
366 bool HexagonBitSimplify::replaceRegWithSub(
unsigned OldR,
unsigned NewR,
371 if (hasTiedUse(OldR, MRI, NewSR))
375 for (auto
I = Begin;
I !=
End;
I = NextI) {
376 NextI = std::next(
I);
383 bool HexagonBitSimplify::replaceSubWithSub(
unsigned OldR,
unsigned OldSR,
388 if (OldSR != NewSR && hasTiedUse(OldR, MRI, NewSR))
392 for (auto
I = Begin;
I != End;
I = NextI) {
393 NextI = std::next(
I);
394 if (
I->getSubReg() != OldSR)
416 switch (RC->
getID()) {
417 case Hexagon::DoubleRegsRegClassID:
418 case Hexagon::VecDblRegsRegClassID:
419 case Hexagon::VecDblRegs128BRegClassID:
421 if (RR.
Sub == Hexagon::isub_hi || RR.
Sub == Hexagon::vsub_hi)
433 bool HexagonBitSimplify::parseRegSequence(
const MachineInstr &
I,
443 assert((Sub1 == SubLo && Sub2 == SubHi) || (Sub1 == SubHi && Sub2 == SubLo));
444 if (Sub1 == SubLo && Sub2 == SubHi) {
449 if (Sub1 == SubHi && Sub2 == SubLo) {
463 bool HexagonBitSimplify::getUsedBitsInStore(
unsigned Opc,
BitVector &
Bits,
465 using namespace Hexagon;
470 case S2_storerbnew_io:
471 case S2_pstorerbt_io:
472 case S2_pstorerbf_io:
473 case S4_pstorerbtnew_io:
474 case S4_pstorerbfnew_io:
475 case S2_pstorerbnewt_io:
476 case S2_pstorerbnewf_io:
477 case S4_pstorerbnewtnew_io:
478 case S4_pstorerbnewfnew_io:
480 case S2_storerbnew_pi:
481 case S2_pstorerbt_pi:
482 case S2_pstorerbf_pi:
483 case S2_pstorerbtnew_pi:
484 case S2_pstorerbfnew_pi:
485 case S2_pstorerbnewt_pi:
486 case S2_pstorerbnewf_pi:
487 case S2_pstorerbnewtnew_pi:
488 case S2_pstorerbnewfnew_pi:
490 case S4_storerbnew_ap:
492 case S2_storerbnew_pr:
494 case S4_storerbnew_ur:
496 case S2_storerbnew_pbr:
498 case S2_storerbnew_pci:
500 case S2_storerbnew_pcr:
502 case S4_storerbnew_rr:
503 case S4_pstorerbt_rr:
504 case S4_pstorerbf_rr:
505 case S4_pstorerbtnew_rr:
506 case S4_pstorerbfnew_rr:
507 case S4_pstorerbnewt_rr:
508 case S4_pstorerbnewf_rr:
509 case S4_pstorerbnewtnew_rr:
510 case S4_pstorerbnewfnew_rr:
512 case S2_storerbnewgp:
513 case S4_pstorerbt_abs:
514 case S4_pstorerbf_abs:
515 case S4_pstorerbtnew_abs:
516 case S4_pstorerbfnew_abs:
517 case S4_pstorerbnewt_abs:
518 case S4_pstorerbnewf_abs:
519 case S4_pstorerbnewtnew_abs:
520 case S4_pstorerbnewfnew_abs:
521 Bits.
set(Begin, Begin+8);
526 case S2_storerhnew_io:
527 case S2_pstorerht_io:
528 case S2_pstorerhf_io:
529 case S4_pstorerhtnew_io:
530 case S4_pstorerhfnew_io:
531 case S2_pstorerhnewt_io:
532 case S2_pstorerhnewf_io:
533 case S4_pstorerhnewtnew_io:
534 case S4_pstorerhnewfnew_io:
536 case S2_storerhnew_pi:
537 case S2_pstorerht_pi:
538 case S2_pstorerhf_pi:
539 case S2_pstorerhtnew_pi:
540 case S2_pstorerhfnew_pi:
541 case S2_pstorerhnewt_pi:
542 case S2_pstorerhnewf_pi:
543 case S2_pstorerhnewtnew_pi:
544 case S2_pstorerhnewfnew_pi:
546 case S4_storerhnew_ap:
548 case S2_storerhnew_pr:
550 case S4_storerhnew_ur:
552 case S2_storerhnew_pbr:
554 case S2_storerhnew_pci:
556 case S2_storerhnew_pcr:
558 case S4_pstorerht_rr:
559 case S4_pstorerhf_rr:
560 case S4_pstorerhtnew_rr:
561 case S4_pstorerhfnew_rr:
562 case S4_storerhnew_rr:
563 case S4_pstorerhnewt_rr:
564 case S4_pstorerhnewf_rr:
565 case S4_pstorerhnewtnew_rr:
566 case S4_pstorerhnewfnew_rr:
568 case S2_storerhnewgp:
569 case S4_pstorerht_abs:
570 case S4_pstorerhf_abs:
571 case S4_pstorerhtnew_abs:
572 case S4_pstorerhfnew_abs:
573 case S4_pstorerhnewt_abs:
574 case S4_pstorerhnewf_abs:
575 case S4_pstorerhnewtnew_abs:
576 case S4_pstorerhnewfnew_abs:
577 Bits.
set(Begin, Begin+16);
582 case S2_pstorerft_io:
583 case S2_pstorerff_io:
584 case S4_pstorerftnew_io:
585 case S4_pstorerffnew_io:
587 case S2_pstorerft_pi:
588 case S2_pstorerff_pi:
589 case S2_pstorerftnew_pi:
590 case S2_pstorerffnew_pi:
598 case S4_pstorerft_rr:
599 case S4_pstorerff_rr:
600 case S4_pstorerftnew_rr:
601 case S4_pstorerffnew_rr:
603 case S4_pstorerft_abs:
604 case S4_pstorerff_abs:
605 case S4_pstorerftnew_abs:
606 case S4_pstorerffnew_abs:
607 Bits.
set(Begin+16, Begin+32);
622 bool HexagonBitSimplify::getUsedBits(
unsigned Opc,
unsigned OpN,
624 using namespace Hexagon;
628 if (OpN ==
D.getNumOperands()-1)
629 return getUsedBitsInStore(Opc, Bits, Begin);
641 Bits.
set(Begin, Begin+8);
654 Bits.
set(Begin, Begin+16);
662 Bits.
set(Begin+16, Begin+32);
672 Bits.
set(Begin, Begin+8);
682 case A2_addh_h16_sat_ll:
684 case A2_addh_l16_sat_ll:
687 case A2_subh_h16_sat_ll:
689 case A2_subh_l16_sat_ll:
690 case M2_mpy_acc_ll_s0:
691 case M2_mpy_acc_ll_s1:
692 case M2_mpy_acc_sat_ll_s0:
693 case M2_mpy_acc_sat_ll_s1:
696 case M2_mpy_nac_ll_s0:
697 case M2_mpy_nac_ll_s1:
698 case M2_mpy_nac_sat_ll_s0:
699 case M2_mpy_nac_sat_ll_s1:
700 case M2_mpy_rnd_ll_s0:
701 case M2_mpy_rnd_ll_s1:
702 case M2_mpy_sat_ll_s0:
703 case M2_mpy_sat_ll_s1:
704 case M2_mpy_sat_rnd_ll_s0:
705 case M2_mpy_sat_rnd_ll_s1:
706 case M2_mpyd_acc_ll_s0:
707 case M2_mpyd_acc_ll_s1:
710 case M2_mpyd_nac_ll_s0:
711 case M2_mpyd_nac_ll_s1:
712 case M2_mpyd_rnd_ll_s0:
713 case M2_mpyd_rnd_ll_s1:
714 case M2_mpyu_acc_ll_s0:
715 case M2_mpyu_acc_ll_s1:
718 case M2_mpyu_nac_ll_s0:
719 case M2_mpyu_nac_ll_s1:
720 case M2_mpyud_acc_ll_s0:
721 case M2_mpyud_acc_ll_s1:
724 case M2_mpyud_nac_ll_s0:
725 case M2_mpyud_nac_ll_s1:
726 if (OpN == 1 || OpN == 2) {
727 Bits.
set(Begin, Begin+16);
734 case A2_addh_h16_sat_lh:
737 case A2_subh_h16_sat_lh:
738 case M2_mpy_acc_lh_s0:
739 case M2_mpy_acc_lh_s1:
740 case M2_mpy_acc_sat_lh_s0:
741 case M2_mpy_acc_sat_lh_s1:
744 case M2_mpy_nac_lh_s0:
745 case M2_mpy_nac_lh_s1:
746 case M2_mpy_nac_sat_lh_s0:
747 case M2_mpy_nac_sat_lh_s1:
748 case M2_mpy_rnd_lh_s0:
749 case M2_mpy_rnd_lh_s1:
750 case M2_mpy_sat_lh_s0:
751 case M2_mpy_sat_lh_s1:
752 case M2_mpy_sat_rnd_lh_s0:
753 case M2_mpy_sat_rnd_lh_s1:
754 case M2_mpyd_acc_lh_s0:
755 case M2_mpyd_acc_lh_s1:
758 case M2_mpyd_nac_lh_s0:
759 case M2_mpyd_nac_lh_s1:
760 case M2_mpyd_rnd_lh_s0:
761 case M2_mpyd_rnd_lh_s1:
762 case M2_mpyu_acc_lh_s0:
763 case M2_mpyu_acc_lh_s1:
766 case M2_mpyu_nac_lh_s0:
767 case M2_mpyu_nac_lh_s1:
768 case M2_mpyud_acc_lh_s0:
769 case M2_mpyud_acc_lh_s1:
772 case M2_mpyud_nac_lh_s0:
773 case M2_mpyud_nac_lh_s1:
776 case A2_addh_l16_sat_hl:
778 case A2_subh_l16_sat_hl:
780 Bits.
set(Begin, Begin+16);
784 Bits.
set(Begin+16, Begin+32);
791 case A2_addh_h16_sat_hl:
794 case A2_subh_h16_sat_hl:
795 case M2_mpy_acc_hl_s0:
796 case M2_mpy_acc_hl_s1:
797 case M2_mpy_acc_sat_hl_s0:
798 case M2_mpy_acc_sat_hl_s1:
801 case M2_mpy_nac_hl_s0:
802 case M2_mpy_nac_hl_s1:
803 case M2_mpy_nac_sat_hl_s0:
804 case M2_mpy_nac_sat_hl_s1:
805 case M2_mpy_rnd_hl_s0:
806 case M2_mpy_rnd_hl_s1:
807 case M2_mpy_sat_hl_s0:
808 case M2_mpy_sat_hl_s1:
809 case M2_mpy_sat_rnd_hl_s0:
810 case M2_mpy_sat_rnd_hl_s1:
811 case M2_mpyd_acc_hl_s0:
812 case M2_mpyd_acc_hl_s1:
815 case M2_mpyd_nac_hl_s0:
816 case M2_mpyd_nac_hl_s1:
817 case M2_mpyd_rnd_hl_s0:
818 case M2_mpyd_rnd_hl_s1:
819 case M2_mpyu_acc_hl_s0:
820 case M2_mpyu_acc_hl_s1:
823 case M2_mpyu_nac_hl_s0:
824 case M2_mpyu_nac_hl_s1:
825 case M2_mpyud_acc_hl_s0:
826 case M2_mpyud_acc_hl_s1:
829 case M2_mpyud_nac_hl_s0:
830 case M2_mpyud_nac_hl_s1:
832 Bits.
set(Begin+16, Begin+32);
836 Bits.
set(Begin, Begin+16);
843 case A2_addh_h16_sat_hh:
846 case A2_subh_h16_sat_hh:
847 case M2_mpy_acc_hh_s0:
848 case M2_mpy_acc_hh_s1:
849 case M2_mpy_acc_sat_hh_s0:
850 case M2_mpy_acc_sat_hh_s1:
853 case M2_mpy_nac_hh_s0:
854 case M2_mpy_nac_hh_s1:
855 case M2_mpy_nac_sat_hh_s0:
856 case M2_mpy_nac_sat_hh_s1:
857 case M2_mpy_rnd_hh_s0:
858 case M2_mpy_rnd_hh_s1:
859 case M2_mpy_sat_hh_s0:
860 case M2_mpy_sat_hh_s1:
861 case M2_mpy_sat_rnd_hh_s0:
862 case M2_mpy_sat_rnd_hh_s1:
863 case M2_mpyd_acc_hh_s0:
864 case M2_mpyd_acc_hh_s1:
867 case M2_mpyd_nac_hh_s0:
868 case M2_mpyd_nac_hh_s1:
869 case M2_mpyd_rnd_hh_s0:
870 case M2_mpyd_rnd_hh_s1:
871 case M2_mpyu_acc_hh_s0:
872 case M2_mpyu_acc_hh_s1:
875 case M2_mpyu_nac_hh_s0:
876 case M2_mpyu_nac_hh_s1:
877 case M2_mpyud_acc_hh_s0:
878 case M2_mpyud_acc_hh_s1:
881 case M2_mpyud_nac_hh_s0:
882 case M2_mpyud_nac_hh_s1:
883 if (OpN == 1 || OpN == 2) {
884 Bits.
set(Begin+16, Begin+32);
912 switch (RC->
getID()) {
913 case Hexagon::DoubleRegsRegClassID:
914 VerifySR(RC, RR.
Sub);
915 return &Hexagon::IntRegsRegClass;
916 case Hexagon::VecDblRegsRegClassID:
917 VerifySR(RC, RR.
Sub);
918 return &Hexagon::VectorRegsRegClass;
919 case Hexagon::VecDblRegs128BRegClassID:
920 VerifySR(RC, RR.
Sub);
921 return &Hexagon::VectorRegs128BRegClass;
936 auto *DRC = getFinalVRegClass(RD, MRI);
940 return DRC == getFinalVRegClass(RS, MRI);
949 return Op.getSubReg() != NewSub &&
Op.isTied();
955 class DeadCodeElimination {
959 MDT(mdt), MRI(mf.getRegInfo()) {}
962 return runOnNode(MDT.getRootNode());
966 bool isDead(
unsigned R)
const;
977 bool DeadCodeElimination::isDead(
unsigned R)
const {
994 bool Changed =
false;
996 for (
auto *DTN : children<MachineDomTreeNode*>(N))
997 Changed |= runOnNode(DTN);
1000 std::vector<MachineInstr*> Instrs;
1002 Instrs.push_back(&*I);
1004 for (
auto MI : Instrs) {
1018 bool AllDead =
true;
1021 if (!
Op.isReg() || !
Op.isDef())
1023 unsigned R =
Op.getReg();
1034 for (
unsigned i = 0, n = Regs.
size(); i != n; ++i)
1054 class RedundantInstrElimination :
public Transformation {
1058 : Transformation(
true), HII(hii), HRI(hri), MRI(mri),
BT(bt) {}
1063 bool isLossyShiftLeft(
const MachineInstr &MI,
unsigned OpN,
1064 unsigned &LostB,
unsigned &LostE);
1065 bool isLossyShiftRight(
const MachineInstr &MI,
unsigned OpN,
1066 unsigned &LostB,
unsigned &LostE);
1067 bool computeUsedBits(
unsigned Reg,
BitVector &Bits);
1083 bool RedundantInstrElimination::isLossyShiftLeft(
const MachineInstr &MI,
1084 unsigned OpN,
unsigned &LostB,
unsigned &LostE) {
1085 using namespace Hexagon;
1088 unsigned ImN, RegN, Width;
1095 case S2_asl_i_p_acc:
1096 case S2_asl_i_p_and:
1097 case S2_asl_i_p_nac:
1099 case S2_asl_i_p_xacc:
1109 case S2_addasl_rrri:
1110 case S4_andi_asl_ri:
1112 case S4_addi_asl_ri:
1113 case S4_subi_asl_ri:
1114 case S2_asl_i_r_acc:
1115 case S2_asl_i_r_and:
1116 case S2_asl_i_r_nac:
1118 case S2_asl_i_r_sat:
1119 case S2_asl_i_r_xacc:
1143 bool RedundantInstrElimination::isLossyShiftRight(
const MachineInstr &MI,
1144 unsigned OpN,
unsigned &LostB,
unsigned &LostE) {
1145 using namespace Hexagon;
1155 case S2_asr_i_p_acc:
1156 case S2_asr_i_p_and:
1157 case S2_asr_i_p_nac:
1159 case S2_lsr_i_p_acc:
1160 case S2_lsr_i_p_and:
1161 case S2_lsr_i_p_nac:
1163 case S2_lsr_i_p_xacc:
1172 case S4_andi_lsr_ri:
1174 case S4_addi_lsr_ri:
1175 case S4_subi_lsr_ri:
1176 case S2_asr_i_r_acc:
1177 case S2_asr_i_r_and:
1178 case S2_asr_i_r_nac:
1180 case S2_lsr_i_r_acc:
1181 case S2_lsr_i_r_and:
1182 case S2_lsr_i_r_nac:
1184 case S2_lsr_i_r_xacc:
1207 bool RedundantInstrElimination::computeUsedBits(
unsigned Reg,
BitVector &Bits) {
1210 std::vector<unsigned> Pending;
1211 Pending.push_back(Reg);
1213 for (
unsigned i = 0; i < Pending.size(); ++i) {
1214 unsigned R = Pending[i];
1221 if (!HBS::getSubregMask(UR, B, W, MRI))
1228 Pending.push_back(DefR);
1253 bool RedundantInstrElimination::computeUsedBits(
const MachineInstr &MI,
1254 unsigned OpN,
BitVector &Bits, uint16_t Begin) {
1257 bool GotBits = HBS::getUsedBits(Opc, OpN, T, Begin, HII);
1262 if (isLossyShiftLeft(MI, OpN, LB, LE) || isLossyShiftRight(MI, OpN, LB, LE)) {
1266 uint16_t Width = HRI.getRegSizeInBits(*RC);
1269 T.set(Begin, Begin+Width);
1270 assert(LB <= LE && LB < Width && LE <= Width);
1271 T.reset(Begin+LB, Begin+LE);
1287 if (!HBS::getSubregMask(RD, DB, DW, MRI))
1290 if (!HBS::getSubregMask(RS, SB, SW, MRI))
1296 if (!computeUsedBits(RD.
Reg, Used))
1299 for (
unsigned i = 0; i != DW; ++i)
1300 if (Used[i+DB] && DC[DB+i] != SC[SB+i])
1309 bool Changed =
false;
1311 for (
auto I = B.
begin(),
E = B.
end(), NextI =
I; I !=
E; ++
I) {
1312 NextI = std::next(I);
1315 if (MI->
getOpcode() == TargetOpcode::COPY)
1331 for (
auto &
Op : MI->
uses()) {
1337 if (!HBS::isTransparentCopy(RD, RS, MRI))
1341 if (!HBS::getSubregMask(RS, BN, BW, MRI))
1345 if (!usedBitsEqual(RD, RS) && !
HBS::isEqual(DC, 0, SC, BN, BW))
1353 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1354 .addReg(RS.
Reg, 0, RS.
Sub);
1355 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
1378 class ConstGeneration :
public Transformation {
1382 : Transformation(
true), HII(hii), MRI(mri),
BT(bt) {}
1398 bool ConstGeneration::isTfrConst(
const MachineInstr &MI) {
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:
1408 case Hexagon::CONST64:
1419 if (RC == &Hexagon::IntRegsRegClass) {
1420 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrsi),
Reg)
1421 .addImm(int32_t(C));
1425 if (RC == &Hexagon::DoubleRegsRegClass) {
1427 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrpi),
Reg)
1434 unsigned Opc =
isInt<8>(
Lo) ? Hexagon::A2_combineii
1435 : Hexagon::A4_combineii;
1437 .addImm(int32_t(
Hi))
1438 .addImm(int32_t(Lo));
1442 BuildMI(B, At, DL, HII.get(Hexagon::CONST64),
Reg)
1447 if (RC == &Hexagon::PredRegsRegClass) {
1450 Opc = Hexagon::PS_false;
1451 else if ((C & 0xFF) == 0xFF)
1452 Opc = Hexagon::PS_true;
1465 bool Changed =
false;
1468 for (
auto I = B.
begin(),
E = B.
end(); I !=
E; ++
I) {
1472 HBS::getInstrDefs(*I, Defs);
1473 if (Defs.count() != 1)
1475 unsigned DR = Defs.find_first();
1480 if (HBS::getConst(DRC, 0, DRC.
width(), U)) {
1486 HBS::replaceReg(DR, ImmReg, MRI);
1487 BT.
put(ImmReg, DRC);
1500 class CopyGeneration :
public Transformation {
1504 : Transformation(
true), HII(hii), HRI(hri), MRI(mri),
BT(bt) {}
1521 class CopyPropagation :
public Transformation {
1524 : Transformation(
false), HRI(hri), MRI(mri) {}
1528 static bool isCopyReg(
unsigned Opc,
bool NoConv);
1546 auto *FRC = HBS::getFinalVRegClass(Inp, MRI);
1548 if (!HBS::getSubregMask(Inp, B, W, MRI))
1551 for (
unsigned R = AVs.find_first(); R; R = AVs.find_next(R)) {
1552 if (!
BT.
has(R) || Forbidden[R])
1555 unsigned RW = RC.
width();
1559 if (!HBS::isTransparentCopy(R, Inp, MRI))
1572 if (MRI.
getRegClass(R) != &Hexagon::DoubleRegsRegClass)
1576 Out.
Sub = Hexagon::isub_lo;
1578 Out.
Sub = Hexagon::isub_hi;
1582 if (HBS::isTransparentCopy(Out, Inp, MRI))
1593 bool Changed =
false;
1596 for (
auto I = B.
begin(),
E = B.
end(), NextI =
I; I !=
E;
1597 ++
I, AVB.insert(Defs)) {
1598 NextI = std::next(I);
1600 HBS::getInstrDefs(*I, Defs);
1603 if (CopyPropagation::isCopyReg(Opc,
false) ||
1604 ConstGeneration::isTfrConst(*I))
1610 for (
unsigned R = Defs.find_first(); R; R = Defs.find_next(R)) {
1612 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1614 if (findMatch(R, MR, AVB)) {
1616 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1617 .addReg(MR.
Reg, 0, MR.
Sub);
1619 HBS::replaceReg(R, NewR, MRI);
1620 Forbidden.insert(R);
1624 if (FRC == &Hexagon::DoubleRegsRegClass ||
1625 FRC == &Hexagon::VecDblRegsRegClass ||
1626 FRC == &Hexagon::VecDblRegs128BRegClass) {
1633 if (findMatch(TL, ML, AVB) && findMatch(TH, MH, AVB)) {
1634 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1636 BuildMI(B, At, DL, HII.get(TargetOpcode::REG_SEQUENCE), NewR)
1637 .addReg(ML.
Reg, 0, ML.
Sub)
1642 HBS::replaceReg(R, NewR, MRI);
1643 Forbidden.insert(R);
1652 bool CopyPropagation::isCopyReg(
unsigned Opc,
bool NoConv) {
1654 case TargetOpcode::COPY:
1655 case TargetOpcode::REG_SEQUENCE:
1656 case Hexagon::A4_combineir:
1657 case Hexagon::A4_combineri:
1659 case Hexagon::A2_tfr:
1660 case Hexagon::A2_tfrp:
1661 case Hexagon::A2_combinew:
1662 case Hexagon::V6_vcombine:
1663 case Hexagon::V6_vcombine_128B:
1671 bool CopyPropagation::propagateRegCopy(
MachineInstr &MI) {
1672 bool Changed =
false;
1678 case TargetOpcode::COPY:
1679 case Hexagon::A2_tfr:
1680 case Hexagon::A2_tfrp: {
1682 if (!HBS::isTransparentCopy(RD, RS, MRI))
1685 Changed = HBS::replaceRegWithSub(RD.
Reg, RS.
Reg, RS.
Sub, MRI);
1687 Changed = HBS::replaceReg(RD.
Reg, RS.
Reg, MRI);
1690 case TargetOpcode::REG_SEQUENCE: {
1692 if (HBS::parseRegSequence(MI, SL, SH, MRI)) {
1696 Changed = HBS::replaceSubWithSub(RD.
Reg, SubLo, SL.
Reg, SL.
Sub, MRI);
1697 Changed |= HBS::replaceSubWithSub(RD.
Reg, SubHi, SH.
Reg, SH.
Sub, MRI);
1701 case Hexagon::A2_combinew:
1702 case Hexagon::V6_vcombine:
1703 case Hexagon::V6_vcombine_128B: {
1708 Changed = HBS::replaceSubWithSub(RD.
Reg, SubLo, RL.Reg, RL.Sub, MRI);
1709 Changed |= HBS::replaceSubWithSub(RD.
Reg, SubHi, RH.
Reg, RH.
Sub, MRI);
1712 case Hexagon::A4_combineir:
1713 case Hexagon::A4_combineri: {
1714 unsigned SrcX = (Opc == Hexagon::A4_combineir) ? 2 : 1;
1715 unsigned Sub = (Opc == Hexagon::A4_combineir) ? Hexagon::isub_lo
1718 Changed = HBS::replaceSubWithSub(RD.
Reg, Sub, RS.
Reg, RS.
Sub, MRI);
1726 std::vector<MachineInstr*> Instrs;
1728 Instrs.push_back(&*I);
1730 bool Changed =
false;
1731 for (
auto I : Instrs) {
1733 if (!CopyPropagation::isCopyReg(Opc,
true))
1735 Changed |= propagateRegCopy(*I);
1746 class BitSimplification :
public Transformation {
1751 : Transformation(
true), MDT(mdt), HII(hii), HRI(hri), MRI(mri),
1762 unsigned B, RegHalf &RH);
1767 unsigned getCombineOpcode(
bool HLow,
bool LLow);
1788 std::vector<MachineInstr*> NewMIs;
1803 bool BitSimplification::matchHalf(
unsigned SelfR,
1814 while (I < B+16 && RC[I].num())
1819 unsigned Reg = RC[
I].RefI.Reg;
1820 unsigned P = RC[
I].RefI.Pos;
1823 unsigned Pos = P - (I-
B);
1825 if (Reg == 0 || Reg == SelfR)
1833 if (Pos+16 > SC.
width())
1836 for (
unsigned i = 0; i < 16; ++i) {
1845 if (RC[i+B] != SC[i+Pos])
1852 Sub = Hexagon::isub_lo;
1856 Sub = Hexagon::isub_lo;
1860 Sub = Hexagon::isub_hi;
1864 Sub = Hexagon::isub_hi;
1875 if (!HBS::getFinalVRegClass(RH, MRI))
1883 auto *OpRC = HII.getRegClass(HII.get(Opc), OpNum, &HRI, MF);
1884 auto *
RRC = HBS::getFinalVRegClass(R, MRI);
1885 return OpRC->hasSubClassEq(
RRC);
1890 bool BitSimplification::matchPackhl(
unsigned SelfR,
1893 RegHalf L1, H1, L2, H2;
1895 if (!matchHalf(SelfR, RC, 0, L2) || !matchHalf(SelfR, RC, 16, L1))
1897 if (!matchHalf(SelfR, RC, 32, H2) || !matchHalf(SelfR, RC, 48, H1))
1901 if (H1.Reg != L1.Reg || H1.Sub != L1.Sub || H1.Low || !L1.Low)
1903 if (H2.Reg != L2.Reg || H2.Sub != L2.Sub || H2.Low || !L2.Low)
1911 unsigned BitSimplification::getCombineOpcode(
bool HLow,
bool LLow) {
1912 return HLow ? LLow ? Hexagon::A2_combine_ll
1913 : Hexagon::A2_combine_lh
1914 : LLow ? Hexagon::A2_combine_hl
1915 : Hexagon::A2_combine_hh;
1921 bool BitSimplification::genStoreUpperHalf(
MachineInstr *MI) {
1923 if (Opc != Hexagon::S2_storerh_io)
1932 if (!matchHalf(0, RC, 0, H))
1936 MI->
setDesc(HII.get(Hexagon::S2_storerf_io));
1944 bool BitSimplification::genStoreImmediate(
MachineInstr *MI) {
1948 case Hexagon::S2_storeri_io:
1951 case Hexagon::S2_storerh_io:
1954 case Hexagon::S2_storerb_io:
1967 int64_t Off = OffOp.
getImm();
1969 if (!
isUIntN(6+Align, Off) || (Off & ((1<<Align)-1)))
1977 if (!HBS::getConst(RC, 0, RC.
width(), U))
1983 case Hexagon::S2_storerb_io:
1986 case Hexagon::S2_storerh_io:
1989 case Hexagon::S2_storeri_io:
1998 case Hexagon::S2_storerb_io:
1999 MI->
setDesc(HII.get(Hexagon::S4_storeirb_io));
2001 case Hexagon::S2_storerh_io:
2002 MI->
setDesc(HII.get(Hexagon::S4_storeirh_io));
2004 case Hexagon::S2_storeri_io:
2005 MI->
setDesc(HII.get(Hexagon::S4_storeiri_io));
2019 if (Opc == Hexagon::S2_packhl)
2022 if (!matchPackhl(RD.
Reg, RC, Rs, Rt))
2024 if (!validateReg(Rs, Hexagon::S2_packhl, 1) ||
2025 !validateReg(Rt, Hexagon::S2_packhl, 2))
2033 BuildMI(B, At, DL, HII.get(Hexagon::S2_packhl), NewR)
2034 .addReg(Rs.
Reg, 0, Rs.
Sub)
2036 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2043 bool BitSimplification::genExtractHalf(
MachineInstr *MI,
2059 if (L.Low && Opc != Hexagon::A2_zxth) {
2060 if (validateReg(L, Hexagon::A2_zxth, 1)) {
2062 BuildMI(B, At, DL, HII.get(Hexagon::A2_zxth), NewR)
2063 .addReg(L.Reg, 0, L.Sub);
2065 }
else if (!L.Low && Opc != Hexagon::S2_lsr_i_r) {
2066 if (validateReg(L, Hexagon::S2_lsr_i_r, 1)) {
2068 BuildMI(B, MI, DL, HII.get(Hexagon::S2_lsr_i_r), NewR)
2069 .addReg(L.Reg, 0, L.Sub)
2075 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2082 bool BitSimplification::genCombineHalf(
MachineInstr *MI,
2086 if (!matchHalf(RD.
Reg, RC, 0, L) || !matchHalf(RD.
Reg, RC, 16, H))
2089 if (L.Reg == H.Reg && L.Sub == H.Sub && !H.Low && L.Low)
2093 unsigned COpc = getCombineOpcode(H.Low, L.Low);
2096 if (!validateReg(H, COpc, 1) || !validateReg(L, COpc, 2))
2104 BuildMI(B, At, DL, HII.get(COpc), NewR)
2105 .addReg(H.Reg, 0, H.Sub)
2106 .
addReg(L.Reg, 0, L.Sub);
2107 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2114 bool BitSimplification::genExtractLow(
MachineInstr *MI,
2118 case Hexagon::A2_zxtb:
2119 case Hexagon::A2_zxth:
2120 case Hexagon::S2_extractu:
2131 unsigned W = RC.
width();
2132 while (W > 0 && RC[W-1].is(0))
2134 if (W == 0 || W == RC.
width())
2136 unsigned NewOpc = (W == 8) ? Hexagon::A2_zxtb
2137 : (W == 16) ? Hexagon::A2_zxth
2138 : (W < 10) ? Hexagon::A2_andir
2139 : Hexagon::S2_extractu;
2143 for (
auto &
Op : MI->
uses()) {
2151 if (!HBS::getSubregMask(RS, BN, BW, MRI))
2155 if (!validateReg(RS, NewOpc, 1))
2161 auto MIB =
BuildMI(B, At, DL, HII.get(NewOpc), NewR)
2162 .addReg(RS.
Reg, 0, RS.
Sub);
2163 if (NewOpc == Hexagon::A2_andir)
2164 MIB.addImm((1 << W) - 1);
2165 else if (NewOpc == Hexagon::S2_extractu)
2166 MIB.addImm(W).addImm(0);
2167 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2186 case Hexagon::A4_bitsplit:
2187 case Hexagon::A4_bitspliti:
2191 unsigned W = RC.
width();
2196 unsigned Z = C.width();
2197 while (Z > 0 && C[Z-1].is(0))
2199 return C.width() - Z;
2203 unsigned Z = ctlz(RC);
2204 if (Z == 0 || Z == W)
2218 for (
unsigned i = 1; i < W-Z; ++i) {
2227 for (
unsigned S = AVs.find_first(); S; S = AVs.find_next(S)) {
2233 if (SC.
width() != W || ctlz(SC) != W-Z)
2241 if (Pos <= P && (Pos + W-Z) !=
P)
2243 if (P < Pos && (P + Z) != Pos)
2250 for (I = 1; I < Z; ++
I) {
2270 SrcSR = (
std::min(Pos, P) == 32) ? Hexagon::isub_hi : Hexagon::isub_lo;
2271 if (!validateReg({SrcR,SrcSR}, Hexagon::A4_bitspliti, 1))
2273 unsigned ImmOp = Pos <= P ? W-Z : Z;
2278 if (
In->getOpcode() != Hexagon::A4_bitspliti)
2283 if (
In->getOperand(2).getImm() != ImmOp)
2289 if (!MDT.dominates(DefI, &*At))
2299 auto NewBS =
BuildMI(B, At, DL, HII.get(Hexagon::A4_bitspliti), NewR)
2300 .addReg(SrcR, 0, SrcSR)
2302 NewMIs.push_back(NewBS);
2305 HBS::replaceRegWithSub(RD.
Reg, NewR, Hexagon::isub_lo, MRI);
2306 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_hi, MRI);
2308 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_lo, MRI);
2309 HBS::replaceRegWithSub(RD.
Reg, NewR, Hexagon::isub_hi, MRI);
2323 bool BitSimplification::simplifyTstbit(
MachineInstr *MI,
2326 if (Opc != Hexagon::S2_tstbit_i)
2333 if (!
BT.
has(RS.
Reg) || !HBS::getSubregMask(RS, F, W, MRI))
2348 if (TC == &Hexagon::DoubleRegsRegClass) {
2350 RR.Sub = Hexagon::isub_lo;
2353 RR.Sub = Hexagon::isub_hi;
2355 }
else if (TC == &Hexagon::IntRegsRegClass) {
2360 BuildMI(B, At, DL, HII.get(Hexagon::S2_tstbit_i), NewR)
2361 .addReg(RR.Reg, 0, RR.Sub)
2363 HBS::replaceReg(RD.
Reg, NewR, MRI);
2367 }
else if (V.
is(0) || V.
is(1)) {
2369 unsigned NewOpc = V.
is(0) ? Hexagon::PS_false : Hexagon::PS_true;
2370 BuildMI(B, At, DL, HII.get(NewOpc), NewR);
2371 HBS::replaceReg(RD.
Reg, NewR, MRI);
2383 bool BitSimplification::simplifyExtractLow(
MachineInstr *MI,
2394 unsigned W = RC.
width();
2403 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2423 for (
unsigned I = 0; I != W; ++
I) {
2427 IsConst = IsConst && (V.
is(0) || V.
is(1));
2432 if (TopV.
is(0) || TopV.
is(1)) {
2433 bool S = TopV.
is(1);
2434 for (--W; W > 0 && RC[W-1].is(S); --W)
2446 for (--W; W > 0 && RC[W-1] == TopV; --W)
2461 dbgs() <<
"Cell: " << RC <<
'\n';
2462 dbgs() <<
"Expected bitfield size: " << Len <<
" bits, "
2463 << (Signed ?
"sign" :
"zero") <<
"-extended\n";
2466 bool Changed =
false;
2468 for (
unsigned R = AVs.find_first(); R != 0; R = AVs.find_next(R)) {
2472 unsigned SW = SC.
width();
2478 if (SW < RW || (SW % RW) != 0)
2485 while (Off <= SW-Len) {
2486 unsigned OE = (Off+Len)/RW;
2506 unsigned ExtOpc = 0;
2509 ExtOpc = Signed ? Hexagon::A2_sxtb : Hexagon::A2_zxtb;
2511 ExtOpc = Signed ? Hexagon::A2_sxth : Hexagon::A2_zxth;
2512 else if (Len < 10 && !Signed)
2513 ExtOpc = Hexagon::A2_andir;
2517 Signed ? (RW == 32 ? Hexagon::S4_extract : Hexagon::S4_extractp)
2518 : (RW == 32 ? Hexagon::S2_extractu : Hexagon::S2_extractup);
2522 if (RW != SW && RW*2 != SW)
2525 SR = (Off/RW == 0) ? Hexagon::isub_lo : Hexagon::isub_hi;
2528 if (!validateReg({R,SR}, ExtOpc, 1))
2544 auto MIB =
BuildMI(B, At, DL, HII.get(ExtOpc), NewR)
2547 case Hexagon::A2_sxtb:
2548 case Hexagon::A2_zxtb:
2549 case Hexagon::A2_sxth:
2550 case Hexagon::A2_zxth:
2552 case Hexagon::A2_andir:
2553 MIB.addImm((1u << Len) - 1);
2555 case Hexagon::S4_extract:
2556 case Hexagon::S2_extractu:
2557 case Hexagon::S4_extractp:
2558 case Hexagon::S2_extractup:
2566 HBS::replaceReg(RD.
Reg, NewR, MRI);
2579 bool Changed =
false;
2583 for (
auto I = B.
begin(),
E = B.
end(); I !=
E; ++
I, AVB.insert(Defs)) {
2586 HBS::getInstrDefs(*MI, Defs);
2589 if (Opc == TargetOpcode::COPY || Opc == TargetOpcode::REG_SEQUENCE)
2593 bool T = genStoreUpperHalf(MI);
2594 T = T || genStoreImmediate(MI);
2599 if (Defs.count() != 1)
2610 if (FRC->
getID() == Hexagon::DoubleRegsRegClassID) {
2611 bool T = genPackhl(MI, RD, RC);
2612 T = T || simplifyExtractLow(MI, RD, RC, AVB);
2617 if (FRC->
getID() == Hexagon::IntRegsRegClassID) {
2618 bool T = genBitSplit(MI, RD, RC, AVB);
2619 T = T || simplifyExtractLow(MI, RD, RC, AVB);
2620 T = T || genExtractHalf(MI, RD, RC);
2621 T = T || genCombineHalf(MI, RD, RC);
2622 T = T || genExtractLow(MI, RD, RC);
2627 if (FRC->
getID() == Hexagon::PredRegsRegClassID) {
2628 bool T = simplifyTstbit(MI, RD, RC);
2642 auto &HII = *HST.getInstrInfo();
2644 MDT = &getAnalysis<MachineDominatorTree>();
2648 Changed = DeadCodeElimination(MF, *MDT).run();
2658 ConstGeneration ImmG(
BT, HII, MRI);
2659 Changed |= visitBlock(Entry, ImmG, AIG);
2662 RedundantInstrElimination RIE(
BT, HII, HRI, MRI);
2663 bool Ried = visitBlock(Entry, RIE, ARE);
2670 CopyGeneration CopyG(
BT, HII, HRI, MRI);
2671 Changed |= visitBlock(Entry, CopyG, ACG);
2674 CopyPropagation CopyP(HRI, MRI);
2675 Changed |= visitBlock(Entry, CopyP, ACP);
2677 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2681 BitSimplification BitS(
BT, *MDT, HII, HRI, MRI, MF);
2682 Changed |= visitBlock(Entry, BitS, ABS);
2684 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2690 DeadCodeElimination(MF, *MDT).run();
2771 HII(nullptr), HRI(nullptr), MRI(nullptr), BTP(nullptr) {
2788 typedef std::vector<MachineInstr*> InstrList;
2801 bool isConst(
unsigned Reg)
const;
2802 bool isBitShuffle(
const MachineInstr *MI,
unsigned DefR)
const;
2803 bool isStoreInput(
const MachineInstr *MI,
unsigned DefR)
const;
2804 bool isShuffleOf(
unsigned OutR,
unsigned InpR)
const;
2805 bool isSameShuffle(
unsigned OutR1,
unsigned InpR1,
unsigned OutR2,
2806 unsigned &InpR2)
const;
2809 bool processLoop(LoopCand &C);
2817 "Hexagon Loop Rescheduling",
false,
false)
2819 HexagonLoopRescheduling::PhiInfo::PhiInfo(
MachineInstr &P,
2821 DefR = HexagonLoopRescheduling::getDefReg(&P);
2824 for (
unsigned i = 1, n = P.getNumOperands(); i < n; i += 2) {
2827 LR = P.getOperand(i);
2831 PR = P.getOperand(i);
2835 unsigned HexagonLoopRescheduling::getDefReg(
const MachineInstr *MI) {
2837 HBS::getInstrDefs(*MI, Defs);
2838 if (Defs.count() != 1)
2840 return Defs.find_first();
2843 bool HexagonLoopRescheduling::isConst(
unsigned Reg)
const {
2847 for (
unsigned i = 0, w = RC.
width(); i < w; ++i) {
2849 if (!V.
is(0) && !V.
is(1))
2855 bool HexagonLoopRescheduling::isBitShuffle(
const MachineInstr *MI,
2856 unsigned DefR)
const {
2859 case TargetOpcode::COPY:
2860 case Hexagon::S2_lsr_i_r:
2861 case Hexagon::S2_asr_i_r:
2862 case Hexagon::S2_asl_i_r:
2863 case Hexagon::S2_lsr_i_p:
2864 case Hexagon::S2_asr_i_p:
2865 case Hexagon::S2_asl_i_p:
2866 case Hexagon::S2_insert:
2867 case Hexagon::A2_or:
2868 case Hexagon::A2_orp:
2869 case Hexagon::A2_and:
2870 case Hexagon::A2_andp:
2871 case Hexagon::A2_combinew:
2872 case Hexagon::A4_combineri:
2873 case Hexagon::A4_combineir:
2874 case Hexagon::A2_combineii:
2875 case Hexagon::A4_combineii:
2876 case Hexagon::A2_combine_ll:
2877 case Hexagon::A2_combine_lh:
2878 case Hexagon::A2_combine_hl:
2879 case Hexagon::A2_combine_hh:
2885 bool HexagonLoopRescheduling::isStoreInput(
const MachineInstr *MI,
2886 unsigned InpR)
const {
2897 bool HexagonLoopRescheduling::isShuffleOf(
unsigned OutR,
unsigned InpR)
const {
2898 if (!BTP->has(OutR) || !BTP->has(InpR))
2901 for (
unsigned i = 0, w = OutC.
width(); i < w; ++i) {
2911 bool HexagonLoopRescheduling::isSameShuffle(
unsigned OutR1,
unsigned InpR1,
2912 unsigned OutR2,
unsigned &InpR2)
const {
2913 if (!BTP->has(OutR1) || !BTP->has(InpR1) || !BTP->has(OutR2))
2917 unsigned W = OutC1.
width();
2918 unsigned MatchR = 0;
2919 if (W != OutC2.
width())
2921 for (
unsigned i = 0; i < W; ++i) {
2927 if (V1.
RefI.
Pos != V2.RefI.Pos)
2931 if (V2.RefI.Reg == 0 || V2.RefI.Reg == OutR2)
2934 MatchR = V2.RefI.Reg;
2935 else if (V2.RefI.Reg != MatchR)
2944 unsigned NewPredR) {
2948 unsigned PhiR = MRI->createVirtualRegister(PhiRC);
2949 BuildMI(LB, At, At->getDebugLoc(), HII->get(TargetOpcode::PHI), PhiR)
2954 RegMap.
insert(std::make_pair(G.Inp.Reg, PhiR));
2956 for (
unsigned i = G.Ins.size(); i > 0; --i) {
2958 unsigned DR = getDefReg(SI);
2960 unsigned NewDR = MRI->createVirtualRegister(RC);
2972 unsigned UseR = RegMap[Op.
getReg()];
2975 RegMap.
insert(std::make_pair(DR, NewDR));
2978 HBS::replaceReg(OldPhiR, RegMap[G.Out.Reg], *MRI);
2981 bool HexagonLoopRescheduling::processLoop(LoopCand &C) {
2982 DEBUG(
dbgs() <<
"Processing loop in BB#" << C.LB->getNumber() <<
"\n");
2983 std::vector<PhiInfo> Phis;
2984 for (
auto &I : *C.LB) {
2987 unsigned PR = getDefReg(&I);
2990 bool BadUse =
false, GoodUse =
false;
2991 for (
auto UI = MRI->use_begin(PR), UE = MRI->use_end(); UI != UE; ++UI) {
2997 if (isBitShuffle(UseI, PR) || isStoreInput(UseI, PR))
3000 if (BadUse || !GoodUse)
3003 Phis.push_back(PhiInfo(I, *C.LB));
3007 dbgs() <<
"Phis: {";
3008 for (
auto &I : Phis) {
3010 <<
PrintReg(I.PR.Reg, HRI, I.PR.Sub) <<
":b" << I.PB->getNumber()
3011 <<
',' <<
PrintReg(I.LR.Reg, HRI, I.LR.Sub) <<
":b"
3012 << I.LB->getNumber() <<
')';
3020 bool Changed =
false;
3027 for (
auto I = C.LB->rbegin(),
E = C.LB->rend(); I !=
E; ++
I) {
3028 if (I->isTerminator())
3034 HBS::getInstrDefs(*I, Defs);
3035 if (Defs.count() != 1)
3037 unsigned DefR = Defs.find_first();
3040 if (!isBitShuffle(&*I, DefR))
3043 bool BadUse =
false;
3044 for (
auto UI = MRI->use_begin(DefR), UE = MRI->use_end(); UI != UE; ++UI) {
3047 if (UseI->
isPHI()) {
3050 unsigned Idx = UI.getOperandNo();
3054 auto F =
find(ShufIns, UseI);
3055 if (F == ShufIns.end())
3061 if (C.EB ==
nullptr)
3070 ShufIns.push_back(&*I);
3082 typedef std::vector<InstrGroup> InstrGroupList;
3083 InstrGroupList Groups;
3085 for (
unsigned i = 0, n = ShufIns.size(); i < n; ++i) {
3091 G.Ins.push_back(SI);
3092 G.Out.Reg = getDefReg(SI);
3094 HBS::getInstrUses(*SI, Inputs);
3096 for (
unsigned j = i+1; j < n; ++j) {
3101 HBS::getInstrDefs(*MI, Defs);
3103 if (!Defs.intersects(Inputs))
3107 G.Ins.push_back(MI);
3108 Inputs.remove(Defs);
3110 HBS::getInstrUses(*MI, Inputs);
3111 ShufIns[j] =
nullptr;
3115 if (Inputs.count() > 1)
3117 auto LoopInpEq = [
G] (
const PhiInfo &
P) ->
bool {
3118 return G.Out.Reg == P.LR.Reg;
3123 G.Inp.Reg = Inputs.find_first();
3124 Groups.push_back(G);
3128 for (
unsigned i = 0, n = Groups.size(); i < n; ++i) {
3129 InstrGroup &G = Groups[i];
3130 dbgs() <<
"Group[" << i <<
"] inp: "
3131 <<
PrintReg(G.Inp.Reg, HRI, G.Inp.Sub)
3132 <<
" out: " <<
PrintReg(G.Out.Reg, HRI, G.Out.Sub) <<
"\n";
3133 for (
unsigned j = 0, m = G.Ins.size(); j < m; ++j)
3134 dbgs() <<
" " << *G.Ins[j];
3138 for (
unsigned i = 0, n = Groups.size(); i < n; ++i) {
3139 InstrGroup &G = Groups[i];
3140 if (!isShuffleOf(G.Out.Reg, G.Inp.Reg))
3142 auto LoopInpEq = [
G] (
const PhiInfo &
P) ->
bool {
3143 return G.Out.Reg == P.LR.Reg;
3146 if (F == Phis.end())
3149 if (!isSameShuffle(G.Out.Reg, G.Inp.Reg, F->PR.Reg, PrehR)) {
3150 const MachineInstr *DefPrehR = MRI->getVRegDef(F->PR.Reg);
3152 if (Opc != Hexagon::A2_tfrsi && Opc != Hexagon::A2_tfrpi)
3159 if (RC != MRI->getRegClass(F->PR.Reg)) {
3160 PrehR = MRI->createVirtualRegister(RC);
3161 unsigned TfrI = (RC == &Hexagon::IntRegsRegClass) ? Hexagon::A2_tfrsi
3162 : Hexagon::A2_tfrpi;
3163 auto T = C.PB->getFirstTerminator();
3165 BuildMI(*C.PB, T, DL, HII->get(TfrI), PrehR)
3176 if (MRI->getRegClass(PrehR) != MRI->getRegClass(G.Inp.Reg))
3178 moveGroup(G, *F->LB, *F->PB, F->LB->getFirstNonPHI(), F->DefR, PrehR);
3185 bool HexagonLoopRescheduling::runOnMachineFunction(
MachineFunction &MF) {
3190 HII = HST.getInstrInfo();
3199 std::vector<LoopCand> Cand;
3201 for (
auto &B : MF) {
3202 if (B.pred_size() != 2 || B.succ_size() != 2)
3205 bool IsLoop =
false;
3206 for (
auto PI = B.pred_begin(), PE = B.pred_end(); PI != PE; ++PI) {
3216 for (
auto SI = B.succ_begin(), SE = B.succ_end(); SI != SE; ++SI) {
3221 if ((*SI)->pred_size() == 1)
3226 Cand.push_back(LoopCand(&B, PB, EB));
3229 bool Changed =
false;
3230 for (
auto &C : Cand)
3231 Changed |= processLoop(C);
3241 return new HexagonLoopRescheduling();
3245 return new HexagonBitSimplify();
void push_back(const T &Elt)
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
size_type size() const
size - Returns the number of bits in this bitvector.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
void trace(bool On=false)
static unsigned virtReg2Index(unsigned Reg)
Convert a virtual register number to a 0-based index.
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
MachineBasicBlock * getMBB() const
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
static cl::opt< unsigned > MaxBitSplit("hexbit-max-bitsplit", cl::Hidden, cl::init(UINT_MAX))
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
unsigned getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
iterator_range< use_iterator > use_operands(unsigned Reg) const
Describe properties that are true of each instruction in the target description file.
constexpr char IsConst[]
Key for Kernel::Arg::Metadata::mIsConst.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
Y = RRC X, rotate right via carry.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
void visit(const MachineInstr &MI)
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
unsigned getID() const
Return the register class ID number.
constexpr bool isInt< 8 >(int64_t x)
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
bool any() const
any - Returns true if any bit is set.
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
iterator_range< mop_iterator > operands()
void markUsesInDebugValueAsUndef(unsigned Reg) const
markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the specified register as undefined wh...
unsigned getHexagonSubRegIndex(const TargetRegisterClass *RC, unsigned GenIdx) const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
void clear()
clear - Removes all bits from the bitvector. Does not change capacity.
static use_iterator use_end()
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
static ManagedStatic< DebugCounter > DC
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetRegisterInfo * getTargetRegisterInfo() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth...
const HexagonRegisterInfo * getRegisterInfo() const override
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
RegisterCell get(RegisterRef RR) const
Reg
All possible values of the reg field in the ModR/M byte.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
zlib-gnu style compression
unsigned getNumOperands() const
Access to explicit operands of the instruction.
void RemoveOperand(unsigned i)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
const MachineBasicBlock & front() const
bool is(unsigned T) const
static bool isEqual(const Function &Caller, const Function &Callee)
BitVector & operator|=(const BitVector &RHS)
FunctionPass * createHexagonBitSimplify()
Base class for the actual dominator tree node.
Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubRegIdx=0)
Prints virtual and physical registers with or without a TRI instance.
reverse_iterator rbegin()
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
This corresponds to the llvm.lifetime.
bool isDebugValue() const
INITIALIZE_PASS(HexagonEarlyIfConversion,"hexagon-eif","Hexagon early if conversion", false, false) bool HexagonEarlyIfConversion MachineBasicBlock * SB
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
MachineInstrBundleIterator< MachineInstr > iterator
static cl::opt< bool > GenBitSplit("hexbit-bitsplit", cl::Hidden, cl::init(true), cl::desc("Generate bitsplit instructions"))
size_type count() const
count - Returns the number of bits which are set.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
unsigned const MachineRegisterInfo * MRI
The instances of the Type class are immutable: once they are created, they are never changed...
const MachineOperand & getOperand(unsigned i) const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
#define LLVM_ATTRIBUTE_UNUSED
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
static const unsigned End
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
FunctionPass class - This class is used to implement most global optimizations.
static cl::opt< unsigned > MaxExtract("hexbit-max-extract", cl::Hidden, cl::init(UINT_MAX))
FunctionPass * createHexagonLoopRescheduling()
unsigned getSubReg() const
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
bool isSafeToMove(AliasAnalysis *AA, bool &SawStore) const
Return true if it is safe to move this instruction.
static cl::opt< bool > GenExtract("hexbit-extract", cl::Hidden, cl::init(true), cl::desc("Generate extract instructions"))
const RegisterCell & lookup(unsigned Reg) const
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
void initializeHexagonBitSimplifyPass(PassRegistry &Registry)
static unsigned CountExtract
MachineOperand class - Representation of each machine instruction operand.
reference operator[](unsigned Idx)
bool test(unsigned Idx) const
Hexagon bit simplification
CHAIN = SC CHAIN, Imm128 - System call.
static cl::opt< bool > PreserveTiedOps("hexbit-keep-tied", cl::Hidden, cl::init(true), cl::desc("Preserve subregisters in tied operands"))
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
static unsigned CountBitSplit
bool has(unsigned Reg) const
INITIALIZE_PASS_BEGIN(HexagonBitSimplify,"hexbit","Hexagon bit simplification", false, false) INITIALIZE_PASS_END(HexagonBitSimplify
Representation of each machine instruction.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
use_iterator use_begin(unsigned RegNo) const
void setReg(unsigned Reg)
Change the register this operand corresponds to.
static MachineOperand CreateImm(int64_t Val)
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
void setSubReg(unsigned subReg)
iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
void initializeHexagonLoopReschedulingPass(PassRegistry &)
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
bool reached(const MachineBasicBlock *B) const
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned char TargetFlags=0) const
void put(RegisterRef RR, const RegisterCell &RC)
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
std::set< RegisterRef > RegisterSet
INITIALIZE_PASS(HexagonLoopRescheduling,"hexagon-loop-resched","Hexagon Loop Rescheduling", false, false) HexagonLoopRescheduling
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...