47 #define DEBUG_TYPE "hexbit" 83 unsigned find_first()
const {
90 unsigned find_next(
unsigned Prev)
const {
98 unsigned Idx = v2x(R);
103 unsigned Idx = v2x(R);
116 reference operator[](
unsigned R) {
117 unsigned Idx = v2x(R);
121 bool operator[](
unsigned R)
const {
122 unsigned Idx = v2x(R);
126 bool has(
unsigned R)
const {
127 unsigned Idx = v2x(R);
138 return !Rs.BitVector::test(*
this);
145 void ensure(
unsigned Idx) {
150 static inline unsigned v2x(
unsigned v) {
154 static inline unsigned x2v(
unsigned x) {
164 const PrintRegSet &
P);
175 for (
unsigned R = P.RS.find_first(); R; R = P.RS.find_next(R))
181 class Transformation;
190 return "Hexagon bit simplification";
209 static bool replaceReg(
unsigned OldR,
unsigned NewR,
213 static bool replaceRegWithSub(
unsigned OldR,
unsigned NewR,
215 static bool replaceSubWithSub(
unsigned OldR,
unsigned OldSR,
221 static bool getUsedBitsInStore(
unsigned Opc,
BitVector &
Bits,
223 static bool getUsedBits(
unsigned Opc,
unsigned OpN,
BitVector &Bits,
236 unsigned NewSub = Hexagon::NoSubRegister);
239 using HBS = HexagonBitSimplify;
244 class Transformation {
248 Transformation(
bool TD) : TopDown(TD) {}
249 virtual ~Transformation() =
default;
259 "Hexagon bit simplification",
false,
false)
266 bool Changed =
false;
269 Changed = T.processBlock(B, AVs);
273 getInstrDefs(
I, Defs);
277 for (
auto *DTN : children<MachineDomTreeNode*>(MDT->getNode(&B)))
278 Changed |= visitBlock(*(DTN->getBlock()), T, NewAVs);
281 Changed |= T.processBlock(B, AVs);
292 if (!
Op.isReg() || !
Op.isDef())
301 void HexagonBitSimplify::getInstrUses(
const MachineInstr &MI,
304 if (!
Op.isReg() || !
Op.isUse())
324 if (RC1[B1+i] != RC2[B2+i])
355 bool HexagonBitSimplify::replaceReg(
unsigned OldR,
unsigned NewR,
361 for (
auto I = Begin;
I != End;
I = NextI) {
362 NextI = std::next(
I);
368 bool HexagonBitSimplify::replaceRegWithSub(
unsigned OldR,
unsigned NewR,
372 if (hasTiedUse(OldR, MRI, NewSR))
376 for (
auto I = Begin;
I != End;
I = NextI) {
377 NextI = std::next(
I);
384 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::HvxWRRegClassID:
420 if (RR.
Sub == Hexagon::isub_hi || RR.
Sub == Hexagon::vsub_hi)
432 bool HexagonBitSimplify::parseRegSequence(
const MachineInstr &
I,
442 assert((Sub1 == SubLo && Sub2 == SubHi) || (Sub1 == SubHi && Sub2 == SubLo));
443 if (Sub1 == SubLo && Sub2 == SubHi) {
448 if (Sub1 == SubHi && Sub2 == SubLo) {
462 bool HexagonBitSimplify::getUsedBitsInStore(
unsigned Opc,
BitVector &
Bits,
464 using namespace Hexagon;
469 case S2_storerbnew_io:
470 case S2_pstorerbt_io:
471 case S2_pstorerbf_io:
472 case S4_pstorerbtnew_io:
473 case S4_pstorerbfnew_io:
474 case S2_pstorerbnewt_io:
475 case S2_pstorerbnewf_io:
476 case S4_pstorerbnewtnew_io:
477 case S4_pstorerbnewfnew_io:
479 case S2_storerbnew_pi:
480 case S2_pstorerbt_pi:
481 case S2_pstorerbf_pi:
482 case S2_pstorerbtnew_pi:
483 case S2_pstorerbfnew_pi:
484 case S2_pstorerbnewt_pi:
485 case S2_pstorerbnewf_pi:
486 case S2_pstorerbnewtnew_pi:
487 case S2_pstorerbnewfnew_pi:
489 case S4_storerbnew_ap:
491 case S2_storerbnew_pr:
493 case S4_storerbnew_ur:
495 case S2_storerbnew_pbr:
497 case S2_storerbnew_pci:
499 case S2_storerbnew_pcr:
501 case S4_storerbnew_rr:
502 case S4_pstorerbt_rr:
503 case S4_pstorerbf_rr:
504 case S4_pstorerbtnew_rr:
505 case S4_pstorerbfnew_rr:
506 case S4_pstorerbnewt_rr:
507 case S4_pstorerbnewf_rr:
508 case S4_pstorerbnewtnew_rr:
509 case S4_pstorerbnewfnew_rr:
511 case S2_storerbnewgp:
512 case S4_pstorerbt_abs:
513 case S4_pstorerbf_abs:
514 case S4_pstorerbtnew_abs:
515 case S4_pstorerbfnew_abs:
516 case S4_pstorerbnewt_abs:
517 case S4_pstorerbnewf_abs:
518 case S4_pstorerbnewtnew_abs:
519 case S4_pstorerbnewfnew_abs:
520 Bits.
set(Begin, Begin+8);
525 case S2_storerhnew_io:
526 case S2_pstorerht_io:
527 case S2_pstorerhf_io:
528 case S4_pstorerhtnew_io:
529 case S4_pstorerhfnew_io:
530 case S2_pstorerhnewt_io:
531 case S2_pstorerhnewf_io:
532 case S4_pstorerhnewtnew_io:
533 case S4_pstorerhnewfnew_io:
535 case S2_storerhnew_pi:
536 case S2_pstorerht_pi:
537 case S2_pstorerhf_pi:
538 case S2_pstorerhtnew_pi:
539 case S2_pstorerhfnew_pi:
540 case S2_pstorerhnewt_pi:
541 case S2_pstorerhnewf_pi:
542 case S2_pstorerhnewtnew_pi:
543 case S2_pstorerhnewfnew_pi:
545 case S4_storerhnew_ap:
547 case S2_storerhnew_pr:
549 case S4_storerhnew_ur:
551 case S2_storerhnew_pbr:
553 case S2_storerhnew_pci:
555 case S2_storerhnew_pcr:
557 case S4_pstorerht_rr:
558 case S4_pstorerhf_rr:
559 case S4_pstorerhtnew_rr:
560 case S4_pstorerhfnew_rr:
561 case S4_storerhnew_rr:
562 case S4_pstorerhnewt_rr:
563 case S4_pstorerhnewf_rr:
564 case S4_pstorerhnewtnew_rr:
565 case S4_pstorerhnewfnew_rr:
567 case S2_storerhnewgp:
568 case S4_pstorerht_abs:
569 case S4_pstorerhf_abs:
570 case S4_pstorerhtnew_abs:
571 case S4_pstorerhfnew_abs:
572 case S4_pstorerhnewt_abs:
573 case S4_pstorerhnewf_abs:
574 case S4_pstorerhnewtnew_abs:
575 case S4_pstorerhnewfnew_abs:
576 Bits.
set(Begin, Begin+16);
581 case S2_pstorerft_io:
582 case S2_pstorerff_io:
583 case S4_pstorerftnew_io:
584 case S4_pstorerffnew_io:
586 case S2_pstorerft_pi:
587 case S2_pstorerff_pi:
588 case S2_pstorerftnew_pi:
589 case S2_pstorerffnew_pi:
597 case S4_pstorerft_rr:
598 case S4_pstorerff_rr:
599 case S4_pstorerftnew_rr:
600 case S4_pstorerffnew_rr:
602 case S4_pstorerft_abs:
603 case S4_pstorerff_abs:
604 case S4_pstorerftnew_abs:
605 case S4_pstorerffnew_abs:
606 Bits.
set(Begin+16, Begin+32);
621 bool HexagonBitSimplify::getUsedBits(
unsigned Opc,
unsigned OpN,
623 using namespace Hexagon;
628 return getUsedBitsInStore(Opc, Bits, Begin);
640 Bits.
set(Begin, Begin+8);
653 Bits.
set(Begin, Begin+16);
661 Bits.
set(Begin+16, Begin+32);
671 Bits.
set(Begin, Begin+8);
681 case A2_addh_h16_sat_ll:
683 case A2_addh_l16_sat_ll:
686 case A2_subh_h16_sat_ll:
688 case A2_subh_l16_sat_ll:
689 case M2_mpy_acc_ll_s0:
690 case M2_mpy_acc_ll_s1:
691 case M2_mpy_acc_sat_ll_s0:
692 case M2_mpy_acc_sat_ll_s1:
695 case M2_mpy_nac_ll_s0:
696 case M2_mpy_nac_ll_s1:
697 case M2_mpy_nac_sat_ll_s0:
698 case M2_mpy_nac_sat_ll_s1:
699 case M2_mpy_rnd_ll_s0:
700 case M2_mpy_rnd_ll_s1:
701 case M2_mpy_sat_ll_s0:
702 case M2_mpy_sat_ll_s1:
703 case M2_mpy_sat_rnd_ll_s0:
704 case M2_mpy_sat_rnd_ll_s1:
705 case M2_mpyd_acc_ll_s0:
706 case M2_mpyd_acc_ll_s1:
709 case M2_mpyd_nac_ll_s0:
710 case M2_mpyd_nac_ll_s1:
711 case M2_mpyd_rnd_ll_s0:
712 case M2_mpyd_rnd_ll_s1:
713 case M2_mpyu_acc_ll_s0:
714 case M2_mpyu_acc_ll_s1:
717 case M2_mpyu_nac_ll_s0:
718 case M2_mpyu_nac_ll_s1:
719 case M2_mpyud_acc_ll_s0:
720 case M2_mpyud_acc_ll_s1:
723 case M2_mpyud_nac_ll_s0:
724 case M2_mpyud_nac_ll_s1:
725 if (OpN == 1 || OpN == 2) {
726 Bits.
set(Begin, Begin+16);
733 case A2_addh_h16_sat_lh:
736 case A2_subh_h16_sat_lh:
737 case M2_mpy_acc_lh_s0:
738 case M2_mpy_acc_lh_s1:
739 case M2_mpy_acc_sat_lh_s0:
740 case M2_mpy_acc_sat_lh_s1:
743 case M2_mpy_nac_lh_s0:
744 case M2_mpy_nac_lh_s1:
745 case M2_mpy_nac_sat_lh_s0:
746 case M2_mpy_nac_sat_lh_s1:
747 case M2_mpy_rnd_lh_s0:
748 case M2_mpy_rnd_lh_s1:
749 case M2_mpy_sat_lh_s0:
750 case M2_mpy_sat_lh_s1:
751 case M2_mpy_sat_rnd_lh_s0:
752 case M2_mpy_sat_rnd_lh_s1:
753 case M2_mpyd_acc_lh_s0:
754 case M2_mpyd_acc_lh_s1:
757 case M2_mpyd_nac_lh_s0:
758 case M2_mpyd_nac_lh_s1:
759 case M2_mpyd_rnd_lh_s0:
760 case M2_mpyd_rnd_lh_s1:
761 case M2_mpyu_acc_lh_s0:
762 case M2_mpyu_acc_lh_s1:
765 case M2_mpyu_nac_lh_s0:
766 case M2_mpyu_nac_lh_s1:
767 case M2_mpyud_acc_lh_s0:
768 case M2_mpyud_acc_lh_s1:
771 case M2_mpyud_nac_lh_s0:
772 case M2_mpyud_nac_lh_s1:
775 case A2_addh_l16_sat_hl:
777 case A2_subh_l16_sat_hl:
779 Bits.
set(Begin, Begin+16);
783 Bits.
set(Begin+16, Begin+32);
790 case A2_addh_h16_sat_hl:
793 case A2_subh_h16_sat_hl:
794 case M2_mpy_acc_hl_s0:
795 case M2_mpy_acc_hl_s1:
796 case M2_mpy_acc_sat_hl_s0:
797 case M2_mpy_acc_sat_hl_s1:
800 case M2_mpy_nac_hl_s0:
801 case M2_mpy_nac_hl_s1:
802 case M2_mpy_nac_sat_hl_s0:
803 case M2_mpy_nac_sat_hl_s1:
804 case M2_mpy_rnd_hl_s0:
805 case M2_mpy_rnd_hl_s1:
806 case M2_mpy_sat_hl_s0:
807 case M2_mpy_sat_hl_s1:
808 case M2_mpy_sat_rnd_hl_s0:
809 case M2_mpy_sat_rnd_hl_s1:
810 case M2_mpyd_acc_hl_s0:
811 case M2_mpyd_acc_hl_s1:
814 case M2_mpyd_nac_hl_s0:
815 case M2_mpyd_nac_hl_s1:
816 case M2_mpyd_rnd_hl_s0:
817 case M2_mpyd_rnd_hl_s1:
818 case M2_mpyu_acc_hl_s0:
819 case M2_mpyu_acc_hl_s1:
822 case M2_mpyu_nac_hl_s0:
823 case M2_mpyu_nac_hl_s1:
824 case M2_mpyud_acc_hl_s0:
825 case M2_mpyud_acc_hl_s1:
828 case M2_mpyud_nac_hl_s0:
829 case M2_mpyud_nac_hl_s1:
831 Bits.
set(Begin+16, Begin+32);
835 Bits.
set(Begin, Begin+16);
842 case A2_addh_h16_sat_hh:
845 case A2_subh_h16_sat_hh:
846 case M2_mpy_acc_hh_s0:
847 case M2_mpy_acc_hh_s1:
848 case M2_mpy_acc_sat_hh_s0:
849 case M2_mpy_acc_sat_hh_s1:
852 case M2_mpy_nac_hh_s0:
853 case M2_mpy_nac_hh_s1:
854 case M2_mpy_nac_sat_hh_s0:
855 case M2_mpy_nac_sat_hh_s1:
856 case M2_mpy_rnd_hh_s0:
857 case M2_mpy_rnd_hh_s1:
858 case M2_mpy_sat_hh_s0:
859 case M2_mpy_sat_hh_s1:
860 case M2_mpy_sat_rnd_hh_s0:
861 case M2_mpy_sat_rnd_hh_s1:
862 case M2_mpyd_acc_hh_s0:
863 case M2_mpyd_acc_hh_s1:
866 case M2_mpyd_nac_hh_s0:
867 case M2_mpyd_nac_hh_s1:
868 case M2_mpyd_rnd_hh_s0:
869 case M2_mpyd_rnd_hh_s1:
870 case M2_mpyu_acc_hh_s0:
871 case M2_mpyu_acc_hh_s1:
874 case M2_mpyu_nac_hh_s0:
875 case M2_mpyu_nac_hh_s1:
876 case M2_mpyud_acc_hh_s0:
877 case M2_mpyud_acc_hh_s1:
880 case M2_mpyud_nac_hh_s0:
881 case M2_mpyud_nac_hh_s1:
882 if (OpN == 1 || OpN == 2) {
883 Bits.
set(Begin+16, Begin+32);
911 switch (RC->
getID()) {
912 case Hexagon::DoubleRegsRegClassID:
913 VerifySR(RC, RR.
Sub);
914 return &Hexagon::IntRegsRegClass;
915 case Hexagon::HvxWRRegClassID:
916 VerifySR(RC, RR.
Sub);
917 return &Hexagon::HvxVRRegClass;
932 auto *DRC = getFinalVRegClass(RD, MRI);
936 return DRC == getFinalVRegClass(RS, MRI);
945 return Op.getSubReg() != NewSub &&
Op.isTied();
951 class DeadCodeElimination {
958 return runOnNode(MDT.getRootNode());
962 bool isDead(
unsigned R)
const;
973 bool DeadCodeElimination::isDead(
unsigned R)
const {
990 bool Changed =
false;
992 for (
auto *DTN : children<MachineDomTreeNode*>(N))
993 Changed |= runOnNode(DTN);
996 std::vector<MachineInstr*> Instrs;
998 Instrs.push_back(&*I);
1000 for (
auto MI : Instrs) {
1014 bool AllDead =
true;
1017 if (!
Op.isReg() || !
Op.isDef())
1030 for (
unsigned i = 0, n = Regs.
size(); i != n; ++i)
1050 class RedundantInstrElimination :
public Transformation {
1054 : Transformation(
true), HII(hii), HRI(hri),
MRI(mri),
BT(bt) {}
1059 bool isLossyShiftLeft(
const MachineInstr &MI,
unsigned OpN,
1060 unsigned &LostB,
unsigned &LostE);
1061 bool isLossyShiftRight(
const MachineInstr &MI,
unsigned OpN,
1062 unsigned &LostB,
unsigned &LostE);
1063 bool computeUsedBits(
unsigned Reg,
BitVector &Bits);
1079 bool RedundantInstrElimination::isLossyShiftLeft(
const MachineInstr &MI,
1080 unsigned OpN,
unsigned &LostB,
unsigned &LostE) {
1081 using namespace Hexagon;
1084 unsigned ImN, RegN,
Width;
1091 case S2_asl_i_p_acc:
1092 case S2_asl_i_p_and:
1093 case S2_asl_i_p_nac:
1095 case S2_asl_i_p_xacc:
1105 case S2_addasl_rrri:
1106 case S4_andi_asl_ri:
1108 case S4_addi_asl_ri:
1109 case S4_subi_asl_ri:
1110 case S2_asl_i_r_acc:
1111 case S2_asl_i_r_and:
1112 case S2_asl_i_r_nac:
1114 case S2_asl_i_r_sat:
1115 case S2_asl_i_r_xacc:
1139 bool RedundantInstrElimination::isLossyShiftRight(
const MachineInstr &MI,
1140 unsigned OpN,
unsigned &LostB,
unsigned &LostE) {
1141 using namespace Hexagon;
1151 case S2_asr_i_p_acc:
1152 case S2_asr_i_p_and:
1153 case S2_asr_i_p_nac:
1155 case S2_lsr_i_p_acc:
1156 case S2_lsr_i_p_and:
1157 case S2_lsr_i_p_nac:
1159 case S2_lsr_i_p_xacc:
1168 case S4_andi_lsr_ri:
1170 case S4_addi_lsr_ri:
1171 case S4_subi_lsr_ri:
1172 case S2_asr_i_r_acc:
1173 case S2_asr_i_r_and:
1174 case S2_asr_i_r_nac:
1176 case S2_lsr_i_r_acc:
1177 case S2_lsr_i_r_and:
1178 case S2_lsr_i_r_nac:
1180 case S2_lsr_i_r_xacc:
1203 bool RedundantInstrElimination::computeUsedBits(
unsigned Reg,
BitVector &Bits) {
1206 std::vector<unsigned> Pending;
1207 Pending.push_back(Reg);
1209 for (
unsigned i = 0; i < Pending.size(); ++i) {
1210 unsigned R = Pending[i];
1217 if (!HBS::getSubregMask(UR, B, W, MRI))
1224 Pending.push_back(DefR);
1249 bool RedundantInstrElimination::computeUsedBits(
const MachineInstr &MI,
1253 bool GotBits = HBS::getUsedBits(Opc, OpN,
T, Begin, HII);
1258 if (isLossyShiftLeft(MI, OpN, LB, LE) || isLossyShiftRight(MI, OpN, LB, LE)) {
1262 uint16_t Width = HRI.getRegSizeInBits(*RC);
1265 T.set(Begin, Begin+Width);
1266 assert(LB <= LE && LB < Width && LE <= Width);
1267 T.reset(Begin+LB, Begin+LE);
1283 if (!HBS::getSubregMask(RD, DB, DW, MRI))
1286 if (!HBS::getSubregMask(RS, SB, SW, MRI))
1292 if (!computeUsedBits(RD.
Reg, Used))
1295 for (
unsigned i = 0; i != DW; ++i)
1296 if (Used[i+DB] && DC[DB+i] != SC[SB+i])
1305 bool Changed =
false;
1307 for (
auto I = B.
begin(),
E = B.
end(), NextI =
I; I !=
E; ++
I) {
1308 NextI = std::next(I);
1311 if (MI->
getOpcode() == TargetOpcode::COPY)
1326 for (
auto &
Op : MI->
uses()) {
1332 if (!HBS::isTransparentCopy(RD, RS, MRI))
1336 if (!HBS::getSubregMask(RS, BN, BW, MRI))
1340 if (!usedBitsEqual(RD, RS) && !
HBS::isEqual(DC, 0, SC, BN, BW))
1348 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1349 .addReg(RS.
Reg, 0, RS.
Sub);
1350 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
1373 class ConstGeneration :
public Transformation {
1377 : Transformation(
true), HII(hii),
MRI(mri),
BT(bt) {}
1393 bool ConstGeneration::isTfrConst(
const MachineInstr &MI) {
1396 case Hexagon::A2_combineii:
1397 case Hexagon::A4_combineii:
1398 case Hexagon::A2_tfrsi:
1399 case Hexagon::A2_tfrpi:
1400 case Hexagon::PS_true:
1401 case Hexagon::PS_false:
1403 case Hexagon::CONST64:
1414 if (RC == &Hexagon::IntRegsRegClass) {
1415 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrsi),
Reg)
1416 .addImm(int32_t(C));
1420 if (RC == &Hexagon::DoubleRegsRegClass) {
1422 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrpi),
Reg)
1429 unsigned Opc =
isInt<8>(
Lo) ? Hexagon::A2_combineii
1430 : Hexagon::A4_combineii;
1432 .addImm(int32_t(
Hi))
1433 .addImm(int32_t(Lo));
1437 BuildMI(B, At, DL, HII.get(Hexagon::CONST64),
Reg)
1442 if (RC == &Hexagon::PredRegsRegClass) {
1445 Opc = Hexagon::PS_false;
1446 else if ((C & 0xFF) == 0xFF)
1447 Opc = Hexagon::PS_true;
1460 bool Changed =
false;
1463 for (
auto I = B.
begin(),
E = B.
end(); I !=
E; ++
I) {
1467 HBS::getInstrDefs(*I, Defs);
1468 if (Defs.count() != 1)
1470 unsigned DR = Defs.find_first();
1475 if (HBS::getConst(DRC, 0, DRC.
width(), U)) {
1479 unsigned ImmReg = genTfrConst(MRI.
getRegClass(DR),
C,
B, At, DL);
1481 HBS::replaceReg(DR, ImmReg, MRI);
1482 BT.
put(ImmReg, DRC);
1495 class CopyGeneration :
public Transformation {
1499 : Transformation(
true), HII(hii), HRI(hri),
MRI(mri),
BT(bt) {}
1516 class CopyPropagation :
public Transformation {
1519 : Transformation(
false), HRI(hri),
MRI(mri) {}
1523 static bool isCopyReg(
unsigned Opc,
bool NoConv);
1541 auto *FRC = HBS::getFinalVRegClass(Inp, MRI);
1543 if (!HBS::getSubregMask(Inp, B, W, MRI))
1546 for (
unsigned R = AVs.find_first(); R; R = AVs.find_next(R)) {
1547 if (!
BT.
has(R) || Forbidden[R])
1550 unsigned RW = RC.
width();
1554 if (!HBS::isTransparentCopy(R, Inp, MRI))
1567 if (MRI.
getRegClass(R) != &Hexagon::DoubleRegsRegClass)
1571 Out.
Sub = Hexagon::isub_lo;
1573 Out.
Sub = Hexagon::isub_hi;
1577 if (HBS::isTransparentCopy(Out, Inp, MRI))
1588 bool Changed =
false;
1591 for (
auto I = B.
begin(),
E = B.
end(), NextI =
I; I !=
E;
1592 ++
I, AVB.insert(Defs)) {
1593 NextI = std::next(I);
1595 HBS::getInstrDefs(*I, Defs);
1598 if (CopyPropagation::isCopyReg(Opc,
false) ||
1599 ConstGeneration::isTfrConst(*I))
1605 for (
unsigned R = Defs.find_first(); R; R = Defs.find_next(R)) {
1607 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1609 if (findMatch(R, MR, AVB)) {
1611 BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR)
1612 .addReg(MR.
Reg, 0, MR.
Sub);
1614 HBS::replaceReg(R, NewR, MRI);
1615 Forbidden.insert(R);
1619 if (FRC == &Hexagon::DoubleRegsRegClass ||
1620 FRC == &Hexagon::HvxWRRegClass) {
1627 if (findMatch(TL, ML, AVB) && findMatch(TH, MH, AVB)) {
1628 auto *FRC = HBS::getFinalVRegClass(R, MRI);
1630 BuildMI(B, At, DL, HII.get(TargetOpcode::REG_SEQUENCE), NewR)
1631 .addReg(ML.
Reg, 0, ML.
Sub)
1636 HBS::replaceReg(R, NewR, MRI);
1637 Forbidden.insert(R);
1646 bool CopyPropagation::isCopyReg(
unsigned Opc,
bool NoConv) {
1648 case TargetOpcode::COPY:
1649 case TargetOpcode::REG_SEQUENCE:
1650 case Hexagon::A4_combineir:
1651 case Hexagon::A4_combineri:
1653 case Hexagon::A2_tfr:
1654 case Hexagon::A2_tfrp:
1655 case Hexagon::A2_combinew:
1656 case Hexagon::V6_vcombine:
1664 bool CopyPropagation::propagateRegCopy(
MachineInstr &MI) {
1665 bool Changed =
false;
1671 case TargetOpcode::COPY:
1672 case Hexagon::A2_tfr:
1673 case Hexagon::A2_tfrp: {
1675 if (!HBS::isTransparentCopy(RD, RS, MRI))
1678 Changed = HBS::replaceRegWithSub(RD.
Reg, RS.
Reg, RS.
Sub, MRI);
1680 Changed = HBS::replaceReg(RD.
Reg, RS.
Reg, MRI);
1683 case TargetOpcode::REG_SEQUENCE: {
1685 if (HBS::parseRegSequence(MI, SL, SH, MRI)) {
1689 Changed = HBS::replaceSubWithSub(RD.
Reg, SubLo, SL.
Reg, SL.
Sub, MRI);
1690 Changed |= HBS::replaceSubWithSub(RD.
Reg, SubHi, SH.
Reg, SH.
Sub, MRI);
1694 case Hexagon::A2_combinew:
1695 case Hexagon::V6_vcombine: {
1700 Changed = HBS::replaceSubWithSub(RD.
Reg, SubLo, RL.Reg, RL.Sub, MRI);
1701 Changed |= HBS::replaceSubWithSub(RD.
Reg, SubHi, RH.
Reg, RH.
Sub, MRI);
1704 case Hexagon::A4_combineir:
1705 case Hexagon::A4_combineri: {
1706 unsigned SrcX = (Opc == Hexagon::A4_combineir) ? 2 : 1;
1707 unsigned Sub = (Opc == Hexagon::A4_combineir) ? Hexagon::isub_lo
1710 Changed = HBS::replaceSubWithSub(RD.
Reg, Sub, RS.
Reg, RS.
Sub, MRI);
1718 std::vector<MachineInstr*> Instrs;
1720 Instrs.push_back(&*I);
1722 bool Changed =
false;
1723 for (
auto I : Instrs) {
1725 if (!CopyPropagation::isCopyReg(Opc,
true))
1727 Changed |= propagateRegCopy(*I);
1738 class BitSimplification :
public Transformation {
1743 : Transformation(
true), MDT(mdt), HII(hii), HRI(hri),
MRI(mri),
1754 unsigned B, RegHalf &RH);
1759 unsigned getCombineOpcode(
bool HLow,
bool LLow);
1781 std::vector<MachineInstr*> NewMIs;
1796 bool BitSimplification::matchHalf(
unsigned SelfR,
1807 while (I < B+16 && RC[I].num())
1812 unsigned Reg = RC[
I].RefI.Reg;
1813 unsigned P = RC[
I].RefI.Pos;
1816 unsigned Pos = P - (I-
B);
1818 if (Reg == 0 || Reg == SelfR)
1826 if (Pos+16 > SC.
width())
1829 for (
unsigned i = 0; i < 16; ++i) {
1838 if (RC[i+B] != SC[i+Pos])
1845 Sub = Hexagon::isub_lo;
1849 Sub = Hexagon::isub_lo;
1853 Sub = Hexagon::isub_hi;
1857 Sub = Hexagon::isub_hi;
1868 if (!HBS::getFinalVRegClass(RH, MRI))
1876 auto *OpRC = HII.getRegClass(HII.get(Opc), OpNum, &HRI, MF);
1877 auto *
RRC = HBS::getFinalVRegClass(R, MRI);
1878 return OpRC->hasSubClassEq(
RRC);
1883 bool BitSimplification::matchPackhl(
unsigned SelfR,
1886 RegHalf L1, H1, L2, H2;
1888 if (!matchHalf(SelfR, RC, 0, L2) || !matchHalf(SelfR, RC, 16, L1))
1890 if (!matchHalf(SelfR, RC, 32, H2) || !matchHalf(SelfR, RC, 48, H1))
1894 if (H1.Reg != L1.Reg || H1.Sub != L1.Sub || H1.Low || !L1.Low)
1896 if (H2.Reg != L2.Reg || H2.Sub != L2.Sub || H2.Low || !L2.Low)
1904 unsigned BitSimplification::getCombineOpcode(
bool HLow,
bool LLow) {
1905 return HLow ? LLow ? Hexagon::A2_combine_ll
1906 : Hexagon::A2_combine_lh
1907 : LLow ? Hexagon::A2_combine_hl
1908 : Hexagon::A2_combine_hh;
1914 bool BitSimplification::genStoreUpperHalf(
MachineInstr *MI) {
1916 if (Opc != Hexagon::S2_storerh_io)
1925 if (!matchHalf(0, RC, 0, H))
1929 MI->
setDesc(HII.get(Hexagon::S2_storerf_io));
1937 bool BitSimplification::genStoreImmediate(
MachineInstr *MI) {
1941 case Hexagon::S2_storeri_io:
1944 case Hexagon::S2_storerh_io:
1947 case Hexagon::S2_storerb_io:
1960 int64_t Off = OffOp.
getImm();
1962 if (!
isUIntN(6+Align, Off) || (Off & ((1<<Align)-1)))
1970 if (!HBS::getConst(RC, 0, RC.
width(), U))
1976 case Hexagon::S2_storerb_io:
1979 case Hexagon::S2_storerh_io:
1982 case Hexagon::S2_storeri_io:
1995 case Hexagon::S2_storerb_io:
1996 MI->
setDesc(HII.get(Hexagon::S4_storeirb_io));
1998 case Hexagon::S2_storerh_io:
1999 MI->
setDesc(HII.get(Hexagon::S4_storeirh_io));
2001 case Hexagon::S2_storeri_io:
2002 MI->
setDesc(HII.get(Hexagon::S4_storeiri_io));
2016 if (Opc == Hexagon::S2_packhl)
2019 if (!matchPackhl(RD.
Reg, RC, Rs, Rt))
2021 if (!validateReg(Rs, Hexagon::S2_packhl, 1) ||
2022 !validateReg(Rt, Hexagon::S2_packhl, 2))
2030 BuildMI(B, At, DL, HII.get(Hexagon::S2_packhl), NewR)
2031 .addReg(Rs.
Reg, 0, Rs.
Sub)
2033 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2040 bool BitSimplification::genExtractHalf(
MachineInstr *MI,
2056 if (L.Low && Opc != Hexagon::A2_zxth) {
2057 if (validateReg(L, Hexagon::A2_zxth, 1)) {
2059 BuildMI(B, At, DL, HII.get(Hexagon::A2_zxth), NewR)
2060 .addReg(L.Reg, 0, L.Sub);
2062 }
else if (!L.Low && Opc != Hexagon::S2_lsr_i_r) {
2063 if (validateReg(L, Hexagon::S2_lsr_i_r, 1)) {
2065 BuildMI(B, MI, DL, HII.get(Hexagon::S2_lsr_i_r), NewR)
2066 .addReg(L.Reg, 0, L.Sub)
2072 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2079 bool BitSimplification::genCombineHalf(
MachineInstr *MI,
2083 if (!matchHalf(RD.
Reg, RC, 0, L) || !matchHalf(RD.
Reg, RC, 16, H))
2086 if (L.Reg == H.Reg && L.Sub == H.Sub && !H.Low && L.Low)
2090 unsigned COpc = getCombineOpcode(H.Low, L.Low);
2093 if (!validateReg(H, COpc, 1) || !validateReg(L, COpc, 2))
2101 BuildMI(B, At, DL, HII.get(COpc), NewR)
2102 .addReg(H.Reg, 0, H.Sub)
2103 .
addReg(L.Reg, 0, L.Sub);
2104 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2111 bool BitSimplification::genExtractLow(
MachineInstr *MI,
2115 case Hexagon::A2_zxtb:
2116 case Hexagon::A2_zxth:
2117 case Hexagon::S2_extractu:
2128 unsigned W = RC.
width();
2129 while (W > 0 && RC[W-1].is(0))
2131 if (W == 0 || W == RC.
width())
2133 unsigned NewOpc = (W == 8) ? Hexagon::A2_zxtb
2134 : (W == 16) ? Hexagon::A2_zxth
2135 : (W < 10) ? Hexagon::A2_andir
2136 : Hexagon::S2_extractu;
2140 for (
auto &
Op : MI->
uses()) {
2148 if (!HBS::getSubregMask(RS, BN, BW, MRI))
2152 if (!validateReg(RS, NewOpc, 1))
2158 auto MIB =
BuildMI(B, At, DL, HII.get(NewOpc), NewR)
2159 .addReg(RS.
Reg, 0, RS.
Sub);
2160 if (NewOpc == Hexagon::A2_andir)
2161 MIB.addImm((1 << W) - 1);
2162 else if (NewOpc == Hexagon::S2_extractu)
2163 MIB.addImm(W).addImm(0);
2164 HBS::replaceSubWithSub(RD.
Reg, RD.
Sub, NewR, 0, MRI);
2183 case Hexagon::A4_bitsplit:
2184 case Hexagon::A4_bitspliti:
2188 unsigned W = RC.
width();
2193 unsigned Z = C.width();
2194 while (Z > 0 && C[Z-1].is(0))
2196 return C.width() -
Z;
2200 unsigned Z = ctlz(RC);
2201 if (Z == 0 || Z == W)
2215 for (
unsigned i = 1; i < W-
Z; ++i) {
2224 for (
unsigned S = AVs.find_first(); S; S = AVs.find_next(S)) {
2228 if (SRC != Hexagon::IntRegsRegClassID &&
2229 SRC != Hexagon::DoubleRegsRegClassID)
2234 if (SC.
width() != W || ctlz(SC) != W-
Z)
2242 if (Pos <= P && (Pos + W-Z) !=
P)
2244 if (P < Pos && (P + Z) != Pos)
2247 if (std::min(P, Pos) != 0 && std::min(P, Pos) != 32)
2251 for (I = 1; I <
Z; ++
I) {
2271 SrcSR = (std::min(Pos, P) == 32) ? Hexagon::isub_hi : Hexagon::isub_lo;
2272 if (!validateReg({SrcR,SrcSR}, Hexagon::A4_bitspliti, 1))
2274 unsigned ImmOp = Pos <= P ? W-
Z :
Z;
2279 if (
In->getOpcode() != Hexagon::A4_bitspliti)
2284 if (
In->getOperand(2).getImm() != ImmOp)
2290 if (!MDT.dominates(DefI, &*At))
2300 auto NewBS =
BuildMI(B, At, DL, HII.get(Hexagon::A4_bitspliti), NewR)
2301 .addReg(SrcR, 0, SrcSR)
2303 NewMIs.push_back(NewBS);
2306 HBS::replaceRegWithSub(RD.
Reg, NewR, Hexagon::isub_lo, MRI);
2307 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_hi, MRI);
2309 HBS::replaceRegWithSub(S, NewR, Hexagon::isub_lo, MRI);
2310 HBS::replaceRegWithSub(RD.
Reg, NewR, Hexagon::isub_hi, MRI);
2324 bool BitSimplification::simplifyTstbit(
MachineInstr *MI,
2327 if (Opc != Hexagon::S2_tstbit_i)
2334 if (!
BT.
has(RS.
Reg) || !HBS::getSubregMask(RS, F, W, MRI))
2349 if (TC == &Hexagon::DoubleRegsRegClass) {
2351 RR.
Sub = Hexagon::isub_lo;
2354 RR.
Sub = Hexagon::isub_hi;
2356 }
else if (TC == &Hexagon::IntRegsRegClass) {
2361 BuildMI(B, At, DL, HII.get(Hexagon::S2_tstbit_i), NewR)
2362 .addReg(RR.
Reg, 0, RR.
Sub)
2364 HBS::replaceReg(RD.
Reg, NewR, MRI);
2368 }
else if (V.
is(0) || V.
is(1)) {
2370 unsigned NewOpc = V.
is(0) ? Hexagon::PS_false : Hexagon::PS_true;
2371 BuildMI(B, At, DL, HII.get(NewOpc), NewR);
2372 HBS::replaceReg(RD.
Reg, NewR, MRI);
2384 bool BitSimplification::simplifyExtractLow(
MachineInstr *MI,
2395 unsigned W = RC.
width();
2404 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2424 for (
unsigned I = 0; I !=
W; ++
I) {
2428 IsConst = IsConst && (V.
is(0) || V.
is(1));
2433 if (TopV.
is(0) || TopV.
is(1)) {
2434 bool S = TopV.
is(1);
2435 for (--W; W > 0 && RC[W-1].is(S); --
W)
2447 for (--W; W > 0 && RC[W-1] == TopV; --
W)
2462 dbgs() <<
"Cell: " << RC <<
'\n';
2463 dbgs() <<
"Expected bitfield size: " << Len <<
" bits, " 2464 << (Signed ?
"sign" :
"zero") <<
"-extended\n";
2467 bool Changed =
false;
2469 for (
unsigned R = AVs.find_first(); R != 0; R = AVs.find_next(R)) {
2473 unsigned SW = SC.
width();
2479 if (SW < RW || (SW % RW) != 0)
2486 while (Off <= SW-Len) {
2487 unsigned OE = (Off+Len)/RW;
2507 unsigned ExtOpc = 0;
2510 ExtOpc = Signed ? Hexagon::A2_sxtb : Hexagon::A2_zxtb;
2512 ExtOpc = Signed ? Hexagon::A2_sxth : Hexagon::A2_zxth;
2513 else if (Len < 10 && !Signed)
2514 ExtOpc = Hexagon::A2_andir;
2518 Signed ? (RW == 32 ? Hexagon::S4_extract : Hexagon::S4_extractp)
2519 : (RW == 32 ? Hexagon::S2_extractu : Hexagon::S2_extractup);
2523 if (RW != SW && RW*2 != SW)
2526 SR = (Off/RW == 0) ? Hexagon::isub_lo : Hexagon::isub_hi;
2529 if (!validateReg({R,SR}, ExtOpc, 1))
2545 auto MIB =
BuildMI(B, At, DL, HII.get(ExtOpc), NewR)
2548 case Hexagon::A2_sxtb:
2549 case Hexagon::A2_zxtb:
2550 case Hexagon::A2_sxth:
2551 case Hexagon::A2_zxth:
2553 case Hexagon::A2_andir:
2554 MIB.addImm((1u << Len) - 1);
2556 case Hexagon::S4_extract:
2557 case Hexagon::S2_extractu:
2558 case Hexagon::S4_extractp:
2559 case Hexagon::S2_extractup:
2567 HBS::replaceReg(RD.
Reg, NewR, MRI);
2576 bool BitSimplification::simplifyRCmp0(
MachineInstr *MI,
2579 if (Opc != Hexagon::A4_rcmpeqi && Opc != Hexagon::A4_rcmpneqi)
2586 if (FRC != &Hexagon::IntRegsRegClass && FRC != &Hexagon::DoubleRegsRegClass)
2595 bool KnownNZ =
false;
2602 if (!HBS::getSubregMask(SR, F, W, MRI))
2613 auto ReplaceWithConst = [&](
int C) {
2615 BuildMI(B, At, DL, HII.get(Hexagon::A2_tfrsi), NewR)
2617 HBS::replaceReg(RD.
Reg, NewR, MRI);
2628 if (
Op.isGlobal() ||
Op.isBlockAddress())
2631 return Op.getImm() != 0;
2633 return !
Op.getCImm()->isZero();
2635 return !
Op.getFPImm()->isZero();
2640 if (
Op.isGlobal() ||
Op.isBlockAddress())
2643 return Op.getImm() == 0;
2645 return Op.getCImm()->isZero();
2647 return Op.getFPImm()->isZero();
2653 if (KnownZ || KnownNZ) {
2654 assert(KnownZ != KnownNZ &&
"Register cannot be both 0 and non-0");
2655 return ReplaceWithConst(KnownZ == (Opc == Hexagon::A4_rcmpeqi));
2663 if (SR.
Sub == 0 && InpDef->
getOpcode() == Hexagon::C2_muxii) {
2667 bool KnownNZ1 = IsNonZero(Src1), KnownNZ2 = IsNonZero(Src2);
2668 if (KnownNZ1 && KnownNZ2)
2669 return ReplaceWithConst(Opc == Hexagon::A4_rcmpneqi);
2671 bool KnownZ1 = IsZero(Src1), KnownZ2 = IsZero(Src2);
2672 if (KnownZ1 && KnownZ2)
2673 return ReplaceWithConst(Opc == Hexagon::A4_rcmpeqi);
2678 if ((KnownZ1 || KnownNZ1) && (KnownZ2 || KnownNZ2)) {
2680 BuildMI(B, At, DL, HII.get(Hexagon::C2_muxii), NewR)
2682 .addImm(KnownZ1 == (Opc == Hexagon::A4_rcmpeqi))
2683 .addImm(KnownZ2 == (Opc == Hexagon::A4_rcmpeqi));
2684 HBS::replaceReg(RD.
Reg, NewR, MRI);
2701 bool Changed =
false;
2705 for (
auto I = B.
begin(),
E = B.
end(); I !=
E; ++
I, AVB.insert(Defs)) {
2708 HBS::getInstrDefs(*MI, Defs);
2711 if (Opc == TargetOpcode::COPY || Opc == TargetOpcode::REG_SEQUENCE)
2715 bool T = genStoreUpperHalf(MI);
2716 T = T || genStoreImmediate(MI);
2721 if (Defs.count() != 1)
2732 if (FRC->
getID() == Hexagon::DoubleRegsRegClassID) {
2733 bool T = genPackhl(MI, RD, RC);
2734 T = T || simplifyExtractLow(MI, RD, RC, AVB);
2739 if (FRC->
getID() == Hexagon::IntRegsRegClassID) {
2740 bool T = genBitSplit(MI, RD, RC, AVB);
2741 T = T || simplifyExtractLow(MI, RD, RC, AVB);
2742 T = T || genExtractHalf(MI, RD, RC);
2743 T = T || genCombineHalf(MI, RD, RC);
2744 T = T || genExtractLow(MI, RD, RC);
2745 T = T || simplifyRCmp0(MI, RD);
2750 if (FRC->
getID() == Hexagon::PredRegsRegClassID) {
2751 bool T = simplifyTstbit(MI, RD, RC);
2765 auto &HII = *HST.getInstrInfo();
2767 MDT = &getAnalysis<MachineDominatorTree>();
2771 Changed = DeadCodeElimination(MF, *MDT).run();
2781 ConstGeneration ImmG(BT, HII, MRI);
2782 Changed |= visitBlock(Entry, ImmG, AIG);
2785 RedundantInstrElimination RIE(BT, HII, HRI, MRI);
2786 bool Ried = visitBlock(Entry, RIE, ARE);
2793 CopyGeneration CopyG(BT, HII, HRI, MRI);
2794 Changed |= visitBlock(Entry, CopyG, ACG);
2797 CopyPropagation CopyP(HRI, MRI);
2798 Changed |= visitBlock(Entry, CopyP, ACP);
2800 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2804 BitSimplification BitS(BT, *MDT, HII, HRI, MRI, MF);
2805 Changed |= visitBlock(Entry, BitS, ABS);
2807 Changed = DeadCodeElimination(MF, *MDT).run() || Changed;
2813 DeadCodeElimination(MF, *MDT).run();
2911 using InstrList = std::vector<MachineInstr *>;
2925 bool isConst(
unsigned Reg)
const;
2926 bool isBitShuffle(
const MachineInstr *MI,
unsigned DefR)
const;
2927 bool isStoreInput(
const MachineInstr *MI,
unsigned DefR)
const;
2928 bool isShuffleOf(
unsigned OutR,
unsigned InpR)
const;
2929 bool isSameShuffle(
unsigned OutR1,
unsigned InpR1,
unsigned OutR2,
2930 unsigned &InpR2)
const;
2933 bool processLoop(LoopCand &C);
2941 "Hexagon Loop Rescheduling",
false,
false)
2945 DefR = HexagonLoopRescheduling::getDefReg(&P);
2948 for (
unsigned i = 1, n = P.getNumOperands(); i < n; i += 2) {
2951 LR = P.getOperand(i);
2955 PR = P.getOperand(i);
2959 unsigned HexagonLoopRescheduling::getDefReg(
const MachineInstr *MI) {
2961 HBS::getInstrDefs(*MI, Defs);
2962 if (Defs.count() != 1)
2964 return Defs.find_first();
2967 bool HexagonLoopRescheduling::isConst(
unsigned Reg)
const {
2971 for (
unsigned i = 0, w = RC.
width(); i < w; ++i) {
2973 if (!V.
is(0) && !V.
is(1))
2979 bool HexagonLoopRescheduling::isBitShuffle(
const MachineInstr *MI,
2980 unsigned DefR)
const {
2983 case TargetOpcode::COPY:
2984 case Hexagon::S2_lsr_i_r:
2985 case Hexagon::S2_asr_i_r:
2986 case Hexagon::S2_asl_i_r:
2987 case Hexagon::S2_lsr_i_p:
2988 case Hexagon::S2_asr_i_p:
2989 case Hexagon::S2_asl_i_p:
2990 case Hexagon::S2_insert:
2991 case Hexagon::A2_or:
2992 case Hexagon::A2_orp:
2993 case Hexagon::A2_and:
2994 case Hexagon::A2_andp:
2995 case Hexagon::A2_combinew:
2996 case Hexagon::A4_combineri:
2997 case Hexagon::A4_combineir:
2998 case Hexagon::A2_combineii:
2999 case Hexagon::A4_combineii:
3000 case Hexagon::A2_combine_ll:
3001 case Hexagon::A2_combine_lh:
3002 case Hexagon::A2_combine_hl:
3003 case Hexagon::A2_combine_hh:
3009 bool HexagonLoopRescheduling::isStoreInput(
const MachineInstr *MI,
3010 unsigned InpR)
const {
3021 bool HexagonLoopRescheduling::isShuffleOf(
unsigned OutR,
unsigned InpR)
const {
3022 if (!BTP->has(OutR) || !BTP->has(InpR))
3025 for (
unsigned i = 0, w = OutC.
width(); i < w; ++i) {
3035 bool HexagonLoopRescheduling::isSameShuffle(
unsigned OutR1,
unsigned InpR1,
3036 unsigned OutR2,
unsigned &InpR2)
const {
3037 if (!BTP->has(OutR1) || !BTP->has(InpR1) || !BTP->has(OutR2))
3041 unsigned W = OutC1.
width();
3042 unsigned MatchR = 0;
3043 if (W != OutC2.
width())
3045 for (
unsigned i = 0; i <
W; ++i) {
3051 if (V1.
RefI.
Pos != V2.RefI.Pos)
3055 if (V2.RefI.Reg == 0 || V2.RefI.Reg == OutR2)
3058 MatchR = V2.RefI.Reg;
3059 else if (V2.RefI.Reg != MatchR)
3068 unsigned NewPredR) {
3073 BuildMI(LB, At, At->getDebugLoc(), HII->get(TargetOpcode::PHI), PhiR)
3078 RegMap.
insert(std::make_pair(G.Inp.Reg, PhiR));
3080 for (
unsigned i = G.Ins.size(); i > 0; --i) {
3082 unsigned DR = getDefReg(SI);
3096 unsigned UseR = RegMap[Op.
getReg()];
3099 RegMap.
insert(std::make_pair(DR, NewDR));
3102 HBS::replaceReg(OldPhiR, RegMap[G.Out.Reg], *MRI);
3105 bool HexagonLoopRescheduling::processLoop(LoopCand &C) {
3108 std::vector<PhiInfo> Phis;
3109 for (
auto &I : *C.LB) {
3112 unsigned PR = getDefReg(&I);
3115 bool BadUse =
false, GoodUse =
false;
3122 if (isBitShuffle(UseI, PR) || isStoreInput(UseI, PR))
3125 if (BadUse || !GoodUse)
3128 Phis.push_back(PhiInfo(I, *C.LB));
3132 dbgs() <<
"Phis: {";
3133 for (
auto &I : Phis) {
3135 <<
printReg(I.PR.Reg, HRI, I.PR.Sub) <<
":b" << I.PB->getNumber()
3136 <<
',' <<
printReg(I.LR.Reg, HRI, I.LR.Sub) <<
":b" 3137 << I.LB->getNumber() <<
')';
3145 bool Changed =
false;
3152 for (
auto I = C.LB->rbegin(),
E = C.LB->rend(); I !=
E; ++
I) {
3159 HBS::getInstrDefs(*I, Defs);
3160 if (Defs.count() != 1)
3162 unsigned DefR = Defs.find_first();
3165 if (!isBitShuffle(&*I, DefR))
3168 bool BadUse =
false;
3169 for (
auto UI = MRI->
use_begin(DefR), UE = MRI->
use_end(); UI != UE; ++UI) {
3172 if (UseI->
isPHI()) {
3175 unsigned Idx = UI.getOperandNo();
3179 auto F =
find(ShufIns, UseI);
3180 if (
F == ShufIns.end())
3186 if (C.EB ==
nullptr)
3195 ShufIns.push_back(&*I);
3207 using InstrGroupList = std::vector<InstrGroup>;
3210 for (
unsigned i = 0, n = ShufIns.size(); i < n; ++i) {
3216 G.Ins.push_back(SI);
3217 G.Out.Reg = getDefReg(SI);
3219 HBS::getInstrUses(*SI, Inputs);
3221 for (
unsigned j = i+1; j < n; ++j) {
3226 HBS::getInstrDefs(*MI, Defs);
3228 if (!Defs.intersects(Inputs))
3232 G.Ins.push_back(MI);
3233 Inputs.remove(Defs);
3235 HBS::getInstrUses(*MI, Inputs);
3236 ShufIns[j] =
nullptr;
3240 if (Inputs.count() > 1)
3242 auto LoopInpEq = [
G] (
const PhiInfo &
P) ->
bool {
3243 return G.Out.Reg ==
P.LR.Reg;
3248 G.Inp.Reg = Inputs.find_first();
3249 Groups.push_back(G);
3253 for (
unsigned i = 0, n = Groups.size(); i < n; ++i) {
3254 InstrGroup &G = Groups[i];
3255 dbgs() <<
"Group[" << i <<
"] inp: " 3256 <<
printReg(G.Inp.Reg, HRI, G.Inp.Sub)
3257 <<
" out: " <<
printReg(G.Out.Reg, HRI, G.Out.Sub) <<
"\n";
3258 for (
unsigned j = 0, m = G.Ins.size(); j < m; ++j)
3259 dbgs() <<
" " << *G.Ins[j];
3263 for (
unsigned i = 0, n = Groups.size(); i < n; ++i) {
3264 InstrGroup &G = Groups[i];
3265 if (!isShuffleOf(G.Out.Reg, G.Inp.Reg))
3267 auto LoopInpEq = [
G] (
const PhiInfo &
P) ->
bool {
3268 return G.Out.Reg ==
P.LR.Reg;
3271 if (
F == Phis.end())
3274 if (!isSameShuffle(G.Out.Reg, G.Inp.Reg,
F->PR.Reg, PrehR)) {
3277 if (Opc != Hexagon::A2_tfrsi && Opc != Hexagon::A2_tfrpi)
3286 unsigned TfrI = (RC == &Hexagon::IntRegsRegClass) ? Hexagon::A2_tfrsi
3287 : Hexagon::A2_tfrpi;
3288 auto T = C.PB->getFirstTerminator();
3290 BuildMI(*C.PB,
T, DL, HII->get(TfrI), PrehR)
3303 moveGroup(G, *
F->LB, *
F->PB,
F->LB->getFirstNonPHI(),
F->DefR, PrehR);
3310 bool HexagonLoopRescheduling::runOnMachineFunction(
MachineFunction &MF) {
3315 HII = HST.getInstrInfo();
3316 HRI = HST.getRegisterInfo();
3324 std::vector<LoopCand> Cand;
3326 for (
auto &B : MF) {
3330 bool IsLoop =
false;
3346 if ((*SI)->pred_size() == 1)
3351 Cand.push_back(LoopCand(&B, PB, EB));
3354 bool Changed =
false;
3355 for (
auto &C : Cand)
3356 Changed |= processLoop(C);
3366 return new HexagonLoopRescheduling();
3370 return new HexagonBitSimplify();
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...
INITIALIZE_PASS_BEGIN(HexagonBitSimplify, "hexagon-bit-simplify", "Hexagon bit simplification", false, false) INITIALIZE_PASS_END(HexagonBitSimplify
MachineBasicBlock * getMBB() const
static unsigned virtReg2Index(unsigned Reg)
Convert a virtual register number to a 0-based index.
RegisterCell & fill(uint16_t B, uint16_t E, const BitValue &V)
void trace(bool On=false)
This class represents lattice values for constants.
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
constexpr uint32_t Lo_32(uint64_t Value)
Return the low 32 bits of a 64 bit value.
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Describe properties that are true of each instruction in the target description file.
unsigned getOperandNo(const_mop_iterator I) const
Returns the number of the operand iterator I points to.
Y = RRC X, rotate right via carry.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
void visit(const MachineInstr &MI)
unsigned getSubReg() const
bool test(unsigned Idx) const
constexpr bool isInt< 8 >(int64_t x)
unsigned const TargetRegisterInfo * TRI
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.
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
iterator_range< mop_iterator > operands()
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void clearKillInfo()
Clears kill flags on all operands.
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()
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...
unsigned getNumOperands() const
Return the number of declared MachineOperands for this MachineInstruction.
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
unsigned getNumOperands() const
Retuns the total number of operands.
int find_first() const
find_first - Returns the index of the first set bit, -1 if none of the bits are set.
ABS - Determine the unsigned absolute value of a signed integer value of the same bitwidth...
const HexagonRegisterInfo * getRegisterInfo() const override
int find_next(unsigned Prev) const
find_next - Returns the index of the next set bit following the "Prev" bit.
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
unsigned getID() const
Return the register class ID number.
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
static bool isEqual(const Function &Caller, const Function &Callee)
BitVector & operator|=(const BitVector &RHS)
FunctionPass * createHexagonBitSimplify()
Base class for the actual dominator tree node.
bool reached(const MachineBasicBlock *B) const
void setReg(Register Reg)
Change the register this operand corresponds to.
SmallSet< unsigned, 4 > RegisterSet
static cl::opt< unsigned > MaxExtract("hexbit-max-extract", cl::Hidden, cl::init(std::numeric_limits< unsigned >::max()))
reverse_iterator rbegin()
This corresponds to the llvm.lifetime.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
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"))
const TargetRegisterInfo * getTargetRegisterInfo() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
unsigned const MachineRegisterInfo * MRI
bool anyCommon(const BitVector &RHS) const
Test if any common bits are set.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
The instances of the Type class are immutable: once they are created, they are never changed...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const RegisterCell & lookup(unsigned Reg) const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool any() const
any - Returns true if any bit is set.
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
FunctionPass class - This class is used to implement most global optimizations.
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
FunctionPass * createHexagonLoopRescheduling()
succ_iterator succ_begin()
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
const MachineBasicBlock & front() const
pred_iterator pred_begin()
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This struct is a compact representation of a valid (non-zero power of two) alignment.
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
static cl::opt< unsigned > MaxBitSplit("hexbit-max-bitsplit", cl::Hidden, cl::init(std::numeric_limits< unsigned >::max()))
static cl::opt< bool > GenExtract("hexbit-extract", cl::Hidden, cl::init(true), cl::desc("Generate extract instructions"))
static const X86InstrFMA3Group Groups[]
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
size_type count() const
count - Returns the number of bits which are set.
Align max(MaybeAlign Lhs, Align Rhs)
static BitValue self(const BitRef &Self=BitRef())
void initializeHexagonBitSimplifyPass(PassRegistry &Registry)
static unsigned CountExtract
iterator_range< use_iterator > use_operands(unsigned Reg) const
bool isDebugValue() const
MachineOperand class - Representation of each machine instruction operand.
reference operator[](unsigned Idx)
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"))
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
bool is(unsigned T) const
unsigned getNumDefs() const
Return the number of MachineOperands that are register definitions.
unsigned pred_size() const
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
hexagon bit Hexagon bit simplification
bool mayStore() const
Return true if this instruction could possibly modify memory.
unsigned succ_size() const
INITIALIZE_PASS(HexagonLoopRescheduling, "hexagon-loop-resched", "Hexagon Loop Rescheduling", false, false) HexagonLoopRescheduling
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
static unsigned CountBitSplit
Representation of each machine instruction.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
use_iterator use_begin(unsigned RegNo) const
static MachineOperand CreateImm(int64_t Val)
void setSubReg(unsigned subReg)
bool has(unsigned Reg) const
size_type size() const
size - Returns the number of bits in this bitvector.
void markUsesInDebugValueAsUndef(unsigned Reg) const
markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the specified register as undefined wh...
constexpr char IsConst[]
Key for Kernel::Arg::Metadata::mIsConst.
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)
RegisterCell get(RegisterRef RR) const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isSafeToMove(AAResults *AA, bool &SawStore) const
Return true if it is safe to move this instruction.
constexpr uint32_t Hi_32(uint64_t Value)
Return the high 32 bits of a 64 bit value.
const HexagonInstrInfo * getInstrInfo() const override
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
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...
bool isEqual(const GCNRPTracker::LiveRegSet &S1, const GCNRPTracker::LiveRegSet &S2)
bool hasUnmodeledSideEffects() const
Return true if this instruction has side effects that are not modeled by mayLoad / mayStore...
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
unsigned getRegSizeInBits(const TargetRegisterClass &RC) const
Return the size in bits of a register from class RC.
Register getReg() const
getReg - Returns the register number.
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
const MachineOperand & getOperand(unsigned i) const
unsigned getHexagonSubRegIndex(const TargetRegisterClass &RC, unsigned GenIdx) const
const MachineInstrBuilder & addReg(Register 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...
Wrapper class representing virtual and physical registers.