39#define DEBUG_TYPE "riscv-vl-optimizer"
40#define PASS_NAME "RISC-V VL Optimizer"
49 static DemandedVL vlmax() {
56 DemandedVL
max(
const DemandedVL &
X)
const {
61 return DemandedVL::vlmax();
86 DemandedVL getMinimumVLForUser(
const MachineOperand &UserOp)
const;
102 return MO.isReg() && MO.getReg().isVirtual() &&
103 RISCVRegisterInfo::isRVVRegClass(MRI->getRegClass(MO.getReg()));
114 std::optional<std::pair<unsigned, bool>> EMUL;
121 OperandInfo(std::pair<unsigned, bool> EMUL,
unsigned Log2EEW)
122 : EMUL(EMUL), Log2EEW(Log2EEW) {}
124 OperandInfo(
unsigned Log2EEW) : Log2EEW(Log2EEW) {}
126 OperandInfo() =
delete;
130 static bool areCompatible(
const OperandInfo &Def,
const OperandInfo &
User) {
131 if (Def.Log2EEW !=
User.Log2EEW)
133 if (
User.EMUL && Def.EMUL !=
User.EMUL)
145 OS <<
"EMUL: none\n";
146 OS <<
", EEW: " << (1 << Log2EEW);
152char RISCVVLOptimizer::ID = 0;
158 return new RISCVVLOptimizer();
169 const std::optional<OperandInfo> &OI) {
179static std::pair<unsigned, bool>
191 unsigned MISEW = 1 << MILog2SEW;
193 unsigned EEW = 1 << Log2EEW;
196 unsigned Num = EEW, Denom = MISEW;
197 int GCD = MILMULIsFractional ? std::gcd(Num, Denom * MILMUL)
198 : std::gcd(Num * MILMUL, Denom);
199 Num = MILMULIsFractional ? Num / GCD : Num * MILMUL / GCD;
200 Denom = MILMULIsFractional ? Denom * MILMUL / GCD : Denom / GCD;
201 return std::make_pair(Num > Denom ? Num : Denom, Denom > Num);
215 unsigned MISEW = 1 << MILog2SEW;
216 unsigned EEW = MISEW / Factor;
217 unsigned Log2EEW =
Log2_32(EEW);
222#define VSEG_CASES(Prefix, EEW) \
223 RISCV::Prefix##SEG2E##EEW##_V: \
224 case RISCV::Prefix##SEG3E##EEW##_V: \
225 case RISCV::Prefix##SEG4E##EEW##_V: \
226 case RISCV::Prefix##SEG5E##EEW##_V: \
227 case RISCV::Prefix##SEG6E##EEW##_V: \
228 case RISCV::Prefix##SEG7E##EEW##_V: \
229 case RISCV::Prefix##SEG8E##EEW##_V
230#define VSSEG_CASES(EEW) VSEG_CASES(VS, EEW)
231#define VSSSEG_CASES(EEW) VSEG_CASES(VSS, EEW)
232#define VSUXSEG_CASES(EEW) VSEG_CASES(VSUX, I##EEW)
233#define VSOXSEG_CASES(EEW) VSEG_CASES(VSOX, I##EEW)
239 RISCVVPseudosTable::getPseudoInfo(
MI.getOpcode());
240 assert(
RVV &&
"Could not find MI in PseudoTable");
255 Info.RegClass == RISCV::VMV0RegClassID)
260 switch (
RVV->BaseInstr) {
264 case RISCV::VSETIVLI:
286 case RISCV::VLSE16_V:
287 case RISCV::VSSE16_V:
293 case RISCV::VLSE32_V:
294 case RISCV::VSSE32_V:
300 case RISCV::VLSE64_V:
301 case RISCV::VSSE64_V:
309 case RISCV::VLUXEI8_V:
310 case RISCV::VLOXEI8_V:
311 case RISCV::VSUXEI8_V:
312 case RISCV::VSOXEI8_V:
319 case RISCV::VLUXEI16_V:
320 case RISCV::VLOXEI16_V:
321 case RISCV::VSUXEI16_V:
322 case RISCV::VSOXEI16_V:
329 case RISCV::VLUXEI32_V:
330 case RISCV::VLOXEI32_V:
331 case RISCV::VSUXEI32_V:
332 case RISCV::VSOXEI32_V:
339 case RISCV::VLUXEI64_V:
340 case RISCV::VLOXEI64_V:
341 case RISCV::VSUXEI64_V:
342 case RISCV::VSOXEI64_V:
357 case RISCV::VRSUB_VI:
358 case RISCV::VRSUB_VX:
382 case RISCV::VMINU_VV:
383 case RISCV::VMINU_VX:
386 case RISCV::VMAXU_VV:
387 case RISCV::VMAXU_VX:
394 case RISCV::VMULH_VV:
395 case RISCV::VMULH_VX:
396 case RISCV::VMULHU_VV:
397 case RISCV::VMULHU_VX:
398 case RISCV::VMULHSU_VV:
399 case RISCV::VMULHSU_VX:
402 case RISCV::VDIVU_VV:
403 case RISCV::VDIVU_VX:
406 case RISCV::VREMU_VV:
407 case RISCV::VREMU_VX:
412 case RISCV::VMACC_VV:
413 case RISCV::VMACC_VX:
414 case RISCV::VNMSAC_VV:
415 case RISCV::VNMSAC_VX:
416 case RISCV::VMADD_VV:
417 case RISCV::VMADD_VX:
418 case RISCV::VNMSUB_VV:
419 case RISCV::VNMSUB_VX:
424 case RISCV::VMERGE_VIM:
425 case RISCV::VMERGE_VVM:
426 case RISCV::VMERGE_VXM:
427 case RISCV::VADC_VIM:
428 case RISCV::VADC_VVM:
429 case RISCV::VADC_VXM:
430 case RISCV::VSBC_VVM:
431 case RISCV::VSBC_VXM:
440 case RISCV::VSADDU_VI:
441 case RISCV::VSADDU_VV:
442 case RISCV::VSADDU_VX:
443 case RISCV::VSADD_VI:
444 case RISCV::VSADD_VV:
445 case RISCV::VSADD_VX:
446 case RISCV::VSSUBU_VV:
447 case RISCV::VSSUBU_VX:
448 case RISCV::VSSUB_VV:
449 case RISCV::VSSUB_VX:
450 case RISCV::VAADDU_VV:
451 case RISCV::VAADDU_VX:
452 case RISCV::VAADD_VV:
453 case RISCV::VAADD_VX:
454 case RISCV::VASUBU_VV:
455 case RISCV::VASUBU_VX:
456 case RISCV::VASUB_VV:
457 case RISCV::VASUB_VX:
461 case RISCV::VSMUL_VV:
462 case RISCV::VSMUL_VX:
465 case RISCV::VSSRL_VI:
466 case RISCV::VSSRL_VV:
467 case RISCV::VSSRL_VX:
468 case RISCV::VSSRA_VI:
469 case RISCV::VSSRA_VV:
470 case RISCV::VSSRA_VX:
477 case RISCV::VFMV_F_S:
478 case RISCV::VFMV_S_F:
481 case RISCV::VSLIDEUP_VI:
482 case RISCV::VSLIDEUP_VX:
483 case RISCV::VSLIDEDOWN_VI:
484 case RISCV::VSLIDEDOWN_VX:
485 case RISCV::VSLIDE1UP_VX:
486 case RISCV::VFSLIDE1UP_VF:
487 case RISCV::VSLIDE1DOWN_VX:
488 case RISCV::VFSLIDE1DOWN_VF:
491 case RISCV::VRGATHER_VI:
492 case RISCV::VRGATHER_VV:
493 case RISCV::VRGATHER_VX:
497 case RISCV::VFADD_VF:
498 case RISCV::VFADD_VV:
499 case RISCV::VFSUB_VF:
500 case RISCV::VFSUB_VV:
501 case RISCV::VFRSUB_VF:
503 case RISCV::VFMUL_VF:
504 case RISCV::VFMUL_VV:
505 case RISCV::VFDIV_VF:
506 case RISCV::VFDIV_VV:
507 case RISCV::VFRDIV_VF:
509 case RISCV::VFMACC_VV:
510 case RISCV::VFMACC_VF:
511 case RISCV::VFNMACC_VV:
512 case RISCV::VFNMACC_VF:
513 case RISCV::VFMSAC_VV:
514 case RISCV::VFMSAC_VF:
515 case RISCV::VFNMSAC_VV:
516 case RISCV::VFNMSAC_VF:
517 case RISCV::VFMADD_VV:
518 case RISCV::VFMADD_VF:
519 case RISCV::VFNMADD_VV:
520 case RISCV::VFNMADD_VF:
521 case RISCV::VFMSUB_VV:
522 case RISCV::VFMSUB_VF:
523 case RISCV::VFNMSUB_VV:
524 case RISCV::VFNMSUB_VF:
526 case RISCV::VFSQRT_V:
528 case RISCV::VFRSQRT7_V:
530 case RISCV::VFREC7_V:
532 case RISCV::VFMIN_VF:
533 case RISCV::VFMIN_VV:
534 case RISCV::VFMAX_VF:
535 case RISCV::VFMAX_VV:
537 case RISCV::VFSGNJ_VF:
538 case RISCV::VFSGNJ_VV:
539 case RISCV::VFSGNJN_VV:
540 case RISCV::VFSGNJN_VF:
541 case RISCV::VFSGNJX_VF:
542 case RISCV::VFSGNJX_VV:
544 case RISCV::VFCLASS_V:
546 case RISCV::VFMV_V_F:
548 case RISCV::VFCVT_XU_F_V:
549 case RISCV::VFCVT_X_F_V:
550 case RISCV::VFCVT_RTZ_XU_F_V:
551 case RISCV::VFCVT_RTZ_X_F_V:
552 case RISCV::VFCVT_F_XU_V:
553 case RISCV::VFCVT_F_X_V:
555 case RISCV::VFMERGE_VFM:
559 case RISCV::VFIRST_M:
562 case RISCV::VANDN_VV:
563 case RISCV::VANDN_VX:
567 case RISCV::VBREV8_V:
585 case RISCV::VCLMUL_VV:
586 case RISCV::VCLMUL_VX:
588 case RISCV::VCLMULH_VV:
589 case RISCV::VCLMULH_VX:
594 case RISCV::VABDU_VV:
598 case RISCV::VWSLL_VI:
599 case RISCV::VWSLL_VX:
600 case RISCV::VWSLL_VV:
603 case RISCV::VWADDU_VV:
604 case RISCV::VWADDU_VX:
605 case RISCV::VWSUBU_VV:
606 case RISCV::VWSUBU_VX:
607 case RISCV::VWADD_VV:
608 case RISCV::VWADD_VX:
609 case RISCV::VWSUB_VV:
610 case RISCV::VWSUB_VX:
613 case RISCV::VWMUL_VV:
614 case RISCV::VWMUL_VX:
615 case RISCV::VWMULSU_VV:
616 case RISCV::VWMULSU_VX:
617 case RISCV::VWMULU_VV:
618 case RISCV::VWMULU_VX:
624 case RISCV::VWMACCU_VV:
625 case RISCV::VWMACCU_VX:
626 case RISCV::VWMACC_VV:
627 case RISCV::VWMACC_VX:
628 case RISCV::VWMACCSU_VV:
629 case RISCV::VWMACCSU_VX:
630 case RISCV::VWMACCUS_VX:
632 case RISCV::VFWMACC_VF:
633 case RISCV::VFWMACC_VV:
634 case RISCV::VFWNMACC_VF:
635 case RISCV::VFWNMACC_VV:
636 case RISCV::VFWMSAC_VF:
637 case RISCV::VFWMSAC_VV:
638 case RISCV::VFWNMSAC_VF:
639 case RISCV::VFWNMSAC_VV:
640 case RISCV::VFWMACCBF16_VV:
641 case RISCV::VFWMACCBF16_VF:
644 case RISCV::VFWADD_VV:
645 case RISCV::VFWADD_VF:
646 case RISCV::VFWSUB_VV:
647 case RISCV::VFWSUB_VF:
649 case RISCV::VFWMUL_VF:
650 case RISCV::VFWMUL_VV:
652 case RISCV::VFWCVT_XU_F_V:
653 case RISCV::VFWCVT_X_F_V:
654 case RISCV::VFWCVT_RTZ_XU_F_V:
655 case RISCV::VFWCVT_RTZ_X_F_V:
656 case RISCV::VFWCVT_F_XU_V:
657 case RISCV::VFWCVT_F_X_V:
658 case RISCV::VFWCVT_F_F_V:
659 case RISCV::VFWCVTBF16_F_F_V:
661 case RISCV::VWABDA_VV:
662 case RISCV::VWABDAU_VV:
663 return IsMODef ? MILog2SEW + 1 : MILog2SEW;
666 case RISCV::VWADDU_WV:
667 case RISCV::VWADDU_WX:
668 case RISCV::VWSUBU_WV:
669 case RISCV::VWSUBU_WX:
670 case RISCV::VWADD_WV:
671 case RISCV::VWADD_WX:
672 case RISCV::VWSUB_WV:
673 case RISCV::VWSUB_WX:
675 case RISCV::VFWADD_WF:
676 case RISCV::VFWADD_WV:
677 case RISCV::VFWSUB_WF:
678 case RISCV::VFWSUB_WV: {
679 bool IsOp1 = (HasPassthru && !IsTied) ? MO.
getOperandNo() == 2
681 bool TwoTimes = IsMODef || IsOp1;
682 return TwoTimes ? MILog2SEW + 1 : MILog2SEW;
686 case RISCV::VZEXT_VF2:
687 case RISCV::VSEXT_VF2:
689 case RISCV::VZEXT_VF4:
690 case RISCV::VSEXT_VF4:
692 case RISCV::VZEXT_VF8:
693 case RISCV::VSEXT_VF8:
698 case RISCV::VNSRL_WX:
699 case RISCV::VNSRL_WI:
700 case RISCV::VNSRL_WV:
701 case RISCV::VNSRA_WI:
702 case RISCV::VNSRA_WV:
703 case RISCV::VNSRA_WX:
706 case RISCV::VNCLIPU_WI:
707 case RISCV::VNCLIPU_WV:
708 case RISCV::VNCLIPU_WX:
709 case RISCV::VNCLIP_WI:
710 case RISCV::VNCLIP_WV:
711 case RISCV::VNCLIP_WX:
713 case RISCV::VFNCVT_XU_F_W:
714 case RISCV::VFNCVT_X_F_W:
715 case RISCV::VFNCVT_RTZ_XU_F_W:
716 case RISCV::VFNCVT_RTZ_X_F_W:
717 case RISCV::VFNCVT_F_XU_W:
718 case RISCV::VFNCVT_F_X_W:
719 case RISCV::VFNCVT_F_F_W:
720 case RISCV::VFNCVT_ROD_F_F_W:
721 case RISCV::VFNCVTBF16_F_F_W: {
724 bool TwoTimes = IsOp1;
725 return TwoTimes ? MILog2SEW + 1 : MILog2SEW;
737 case RISCV::VMAND_MM:
738 case RISCV::VMNAND_MM:
739 case RISCV::VMANDN_MM:
740 case RISCV::VMXOR_MM:
742 case RISCV::VMNOR_MM:
743 case RISCV::VMORN_MM:
744 case RISCV::VMXNOR_MM:
747 case RISCV::VMSOF_M: {
754 case RISCV::VCOMPRESS_VM:
760 case RISCV::VIOTA_M: {
768 case RISCV::VMSEQ_VI:
769 case RISCV::VMSEQ_VV:
770 case RISCV::VMSEQ_VX:
771 case RISCV::VMSNE_VI:
772 case RISCV::VMSNE_VV:
773 case RISCV::VMSNE_VX:
774 case RISCV::VMSLTU_VV:
775 case RISCV::VMSLTU_VX:
776 case RISCV::VMSLT_VV:
777 case RISCV::VMSLT_VX:
778 case RISCV::VMSLEU_VV:
779 case RISCV::VMSLEU_VI:
780 case RISCV::VMSLEU_VX:
781 case RISCV::VMSLE_VV:
782 case RISCV::VMSLE_VI:
783 case RISCV::VMSLE_VX:
784 case RISCV::VMSGTU_VI:
785 case RISCV::VMSGTU_VX:
786 case RISCV::VMSGT_VI:
787 case RISCV::VMSGT_VX:
790 case RISCV::VMADC_VIM:
791 case RISCV::VMADC_VVM:
792 case RISCV::VMADC_VXM:
793 case RISCV::VMSBC_VVM:
794 case RISCV::VMSBC_VXM:
796 case RISCV::VMADC_VV:
797 case RISCV::VMADC_VI:
798 case RISCV::VMADC_VX:
799 case RISCV::VMSBC_VV:
800 case RISCV::VMSBC_VX:
803 case RISCV::VMFEQ_VF:
804 case RISCV::VMFEQ_VV:
805 case RISCV::VMFNE_VF:
806 case RISCV::VMFNE_VV:
807 case RISCV::VMFLT_VF:
808 case RISCV::VMFLT_VV:
809 case RISCV::VMFLE_VF:
810 case RISCV::VMFLE_VV:
811 case RISCV::VMFGT_VF:
812 case RISCV::VMFGE_VF: {
820 case RISCV::VREDAND_VS:
821 case RISCV::VREDMAX_VS:
822 case RISCV::VREDMAXU_VS:
823 case RISCV::VREDMIN_VS:
824 case RISCV::VREDMINU_VS:
825 case RISCV::VREDOR_VS:
826 case RISCV::VREDSUM_VS:
827 case RISCV::VREDXOR_VS:
829 case RISCV::VFREDMAX_VS:
830 case RISCV::VFREDMIN_VS:
831 case RISCV::VFREDOSUM_VS:
832 case RISCV::VFREDUSUM_VS: {
839 case RISCV::VWREDSUM_VS:
840 case RISCV::VWREDSUMU_VS:
842 case RISCV::VFWREDOSUM_VS:
843 case RISCV::VFWREDUSUM_VS: {
845 return TwoTimes ? MILog2SEW + 1 : MILog2SEW;
850 case RISCV::VRGATHEREI16_VV: {
864 RISCVVPseudosTable::getPseudoInfo(
MI.getOpcode());
865 assert(
RVV &&
"Could not find MI in PseudoTable");
871 switch (
RVV->BaseInstr) {
878 case RISCV::VREDAND_VS:
879 case RISCV::VREDMAX_VS:
880 case RISCV::VREDMAXU_VS:
881 case RISCV::VREDMIN_VS:
882 case RISCV::VREDMINU_VS:
883 case RISCV::VREDOR_VS:
884 case RISCV::VREDSUM_VS:
885 case RISCV::VREDXOR_VS:
886 case RISCV::VWREDSUM_VS:
887 case RISCV::VWREDSUMU_VS:
888 case RISCV::VFWREDOSUM_VS:
889 case RISCV::VFWREDUSUM_VS:
891 return OperandInfo(*Log2EEW);
902bool RISCVVLOptimizer::isSupportedInstr(
const MachineInstr &
MI)
const {
910 assert(!(
MI.getNumExplicitDefs() == 0 && !
MI.mayStore() &&
912 "No defs but elements don't depend on VL?");
916 if (RVVOpc == RISCV::VMV_S_X || RVVOpc == RISCV::VFMV_S_F)
932 RISCVVPseudosTable::getPseudoInfo(
MI->getOpcode());
937 switch (
RVV->BaseInstr) {
939 case RISCV::VREDAND_VS:
940 case RISCV::VREDMAX_VS:
941 case RISCV::VREDMAXU_VS:
942 case RISCV::VREDMIN_VS:
943 case RISCV::VREDMINU_VS:
944 case RISCV::VREDOR_VS:
945 case RISCV::VREDSUM_VS:
946 case RISCV::VREDXOR_VS:
947 case RISCV::VWREDSUM_VS:
948 case RISCV::VWREDSUMU_VS:
949 case RISCV::VFREDMAX_VS:
950 case RISCV::VFREDMIN_VS:
951 case RISCV::VFREDOSUM_VS:
952 case RISCV::VFREDUSUM_VS:
953 case RISCV::VFWREDOSUM_VS:
954 case RISCV::VFWREDUSUM_VS:
957 case RISCV::VFMV_F_S:
964bool RISCVVLOptimizer::isCandidate(
const MachineInstr &
MI)
const {
965 const MCInstrDesc &
Desc =
MI.getDesc();
969 if (
MI.getNumExplicitDefs() != 1)
974 if (!
MI.allImplicitDefsAreDead()) {
975 LLVM_DEBUG(
dbgs() <<
"Not a candidate because has non-dead implicit def\n");
979 if (
MI.mayRaiseFPException()) {
980 LLVM_DEBUG(
dbgs() <<
"Not a candidate because may raise FP exception\n");
984 for (
const MachineMemOperand *MMO :
MI.memoperands()) {
985 if (MMO->isVolatile()) {
986 LLVM_DEBUG(
dbgs() <<
"Not a candidate because contains volatile MMO\n");
991 if (!isSupportedInstr(
MI)) {
992 LLVM_DEBUG(
dbgs() <<
"Not a candidate due to unsupported instruction: "
999 "Instruction shouldn't be supported if elements depend on VL");
1003 "All supported instructions produce a vector register result");
1005 LLVM_DEBUG(
dbgs() <<
"Found a candidate for VL reduction: " <<
MI <<
"\n");
1020static std::optional<DemandedVL>
1025 return std::nullopt;
1028 return std::nullopt;
1032 return std::nullopt;
1036 return std::nullopt;
1038 if (SlideAmtDef->
getOpcode() != RISCV::ADDI ||
1041 return std::nullopt;
1046RISCVVLOptimizer::getMinimumVLForUser(
const MachineOperand &UserOp)
const {
1047 const MachineInstr &UserMI = *UserOp.
getParent();
1051 return DemandedVLs.lookup(&UserMI);
1056 return DemandedVL::vlmax();
1064 LLVM_DEBUG(
dbgs() <<
" Abort because used by unsafe instruction\n");
1065 return DemandedVL::vlmax();
1069 const MachineOperand &VLOp = UserMI.
getOperand(VLOpNum);
1072 "Did not expect X0 VL");
1081 "instruction with demanded tail\n");
1082 return DemandedVL::vlmax();
1089 LLVM_DEBUG(
dbgs() <<
" Used this operand as a scalar operand\n");
1096 return DemandedVLs.lookup(&UserMI);
1105 if (!
MI.isInsertSubreg())
1119 unsigned SubRegIdx =
MI.getOperand(3).getImm();
1121 assert(!IsFractional &&
"unexpected LMUL for tuple register classes");
1149bool RISCVVLOptimizer::checkUsers(
const MachineInstr &
MI)
const {
1153 SmallSetVector<MachineOperand *, 8> OpWorklist;
1154 SmallPtrSet<const MachineInstr *, 4> PHISeen;
1155 for (
auto &UserOp : MRI->
use_operands(
MI.getOperand(0).getReg()))
1156 OpWorklist.
insert(&UserOp);
1158 while (!OpWorklist.
empty()) {
1160 const MachineInstr &UserMI = *UserOp.
getParent();
1171 LLVM_DEBUG(
dbgs().indent(4) <<
"Peeking through uses of INSERT_SUBREG\n");
1172 for (MachineOperand &UseOp :
1174 const MachineInstr &CandidateMI = *UseOp.getParent();
1182 OpWorklist.
insert(&UseOp);
1187 if (UserMI.
isPHI()) {
1189 if (!PHISeen.
insert(&UserMI).second)
1202 std::optional<OperandInfo> ConsumerInfo =
getOperandInfo(UserOp);
1203 std::optional<OperandInfo> ProducerInfo =
getOperandInfo(
MI.getOperand(0));
1204 if (!ConsumerInfo || !ProducerInfo) {
1205 LLVM_DEBUG(
dbgs() <<
" Abort due to unknown operand information.\n");
1206 LLVM_DEBUG(
dbgs() <<
" ConsumerInfo is: " << ConsumerInfo <<
"\n");
1207 LLVM_DEBUG(
dbgs() <<
" ProducerInfo is: " << ProducerInfo <<
"\n");
1211 if (!OperandInfo::areCompatible(*ProducerInfo, *ConsumerInfo)) {
1214 <<
" Abort due to incompatible information for EMUL or EEW.\n");
1215 LLVM_DEBUG(
dbgs() <<
" ConsumerInfo is: " << ConsumerInfo <<
"\n");
1216 LLVM_DEBUG(
dbgs() <<
" ProducerInfo is: " << ProducerInfo <<
"\n");
1224bool RISCVVLOptimizer::tryReduceVL(MachineInstr &
MI,
1225 MachineOperand CommonVL)
const {
1229 MachineOperand &VLOp =
MI.getOperand(VLOpNum);
1232 "Expected VL to be an Imm or virtual Reg");
1236 if (CommonVL.
isReg()) {
1238 if (RISCVInstrInfo::isFaultOnlyFirstLoad(*VLMI) &&
1250 dbgs() <<
" Abort due to CommonVL == VLOp, no point in reducing.\n");
1254 if (CommonVL.
isImm()) {
1256 << CommonVL.
getImm() <<
" for " <<
MI <<
"\n");
1261 auto VLDominates = [
this, &VLMI](
const MachineInstr &
MI) {
1264 if (!VLDominates(
MI)) {
1265 assert(
MI.getNumExplicitDefs() == 1);
1268 return Use.getParent() ==
MI.getParent();
1271 all_of(UsesSameBB, VLDominates) &&
1282 <<
" for " <<
MI <<
"\n");
1295void RISCVVLOptimizer::transfer(
const MachineInstr &
MI) {
1297 DemandedVLs[&
MI] = DemandedVL::vlmax();
1299 for (
const MachineOperand &MO : virtual_vec_uses(
MI)) {
1301 DemandedVL Prev = DemandedVLs[
Def];
1302 DemandedVLs[
Def] = DemandedVLs[
Def].max(getMinimumVLForUser(MO));
1303 if (DemandedVLs[Def] != Prev)
1304 Worklist.insert(Def);
1308bool RISCVVLOptimizer::runOnMachineFunction(MachineFunction &MF) {
1313 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
1316 if (!
ST.hasVInstructions())
1319 TII =
ST.getInstrInfo();
1321 assert(DemandedVLs.empty());
1328 if (!
MI.isDebugInstr())
1329 Worklist.insert(&
MI);
1332 while (!Worklist.empty()) {
1333 const MachineInstr *
MI = Worklist.front();
1334 Worklist.remove(
MI);
1340 bool MadeChange =
false;
1341 for (
auto &[
MI, VL] : DemandedVLs) {
1345 if (!tryReduceVL(*
const_cast<MachineInstr *
>(
MI), VL.VL))
1350 DemandedVLs.clear();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * TII
static bool isCandidate(const MachineInstr *MI, Register &DefedReg, Register FrameReg)
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static std::optional< DemandedVL > getMinimumVLForVSLIDEDOWN_VX(const MachineOperand &UserOp, const MachineRegisterInfo *MRI)
Given a vslidedown.vx like:
static unsigned getIntegerExtensionOperandEEW(unsigned Factor, const MachineInstr &MI, const MachineOperand &MO)
Dest has EEW=SEW.
static std::optional< OperandInfo > getOperandInfo(const MachineOperand &MO)
#define VSOXSEG_CASES(EEW)
static bool isSegmentedStoreInstr(const MachineInstr &MI)
static bool isVectorOpUsedAsScalarOp(const MachineOperand &MO)
Return true if MO is a vector operand but is used as a scalar operand.
static std::optional< unsigned > getOperandLog2EEW(const MachineOperand &MO)
static std::pair< unsigned, bool > getEMULEqualsEEWDivSEWTimesLMUL(unsigned Log2EEW, const MachineInstr &MI)
Return EMUL = (EEW / SEW) * LMUL where EEW comes from Log2EEW and LMUL and SEW are from the TSFlags o...
#define VSUXSEG_CASES(EEW)
static bool isPhysical(const MachineOperand &MO)
#define VSSSEG_CASES(EEW)
static bool isTupleInsertInstr(const MachineInstr &MI)
Return true if MI is an instruction used for assembling registers for segmented store instructions,...
Remove Loads Into Fake Uses
This file implements a set that has insertion order iteration characteristics.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
bool isReachableFromEntry(const NodeT *A) const
isReachableFromEntry - Return true if A is dominated by the entry block of the function containing it...
FunctionPass class - This class is used to implement most global optimizations.
Describe properties that are true of each instruction in the target description file.
This holds information about one operand of a machine instruction, indicating the register class for ...
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool dominates(const MachineInstr *A, const MachineInstr *B) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
LLVM_ABI unsigned getNumExplicitDefs() const
Returns the number of non-implicit definitions.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
LLVM_ABI unsigned getOperandNo() const
Returns the index of this operand in the instruction that it belongs to.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
LLVM_ABI void ChangeToImmediate(int64_t ImmVal, unsigned TargetFlags=0)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value.
LLVM_ABI void ChangeToRegister(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static MachineOperand CreateImm(int64_t Val)
Register getReg() const
getReg - Returns the register number.
LLVM_ABI bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
iterator_range< use_instr_iterator > use_instructions(Register Reg) const
const TargetRegisterInfo * getTargetRegisterInfo() const
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
iterator_range< use_iterator > use_operands(Register Reg) const
LLVM_ABI MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
This class implements a map that also provides access to all stored values in a deterministic order.
static bool isSafeToMove(const MachineInstr &From, const MachineBasicBlock::iterator &To)
Return true if moving From down to To won't cause any physical register reads or writes to be clobber...
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
A vector that has set insertion semantics.
void insert_range(Range &&R)
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
value_type pop_back_val()
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent a constant reference to a string, i.e.
TargetInstrInfo - Interface to description of machine instruction set.
const uint8_t TSFlags
Configurable target specific flags.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
self_iterator getIterator()
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
static bool readsPastVL(uint64_t TSFlags)
static bool isTiedPseudo(uint64_t TSFlags)
static RISCVVType::VLMUL getLMul(uint64_t TSFlags)
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static bool hasVLOp(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
static bool elementsDependOnVL(uint64_t TSFlags)
static bool hasSEWOp(uint64_t TSFlags)
static bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc)
static unsigned getNF(uint8_t TSFlags)
static bool isVRegClass(uint8_t TSFlags)
static RISCVVType::VLMUL getLMul(uint8_t TSFlags)
LLVM_ABI std::pair< unsigned, bool > decodeVLMUL(VLMUL VLMul)
bool isVLKnownLE(const MachineOperand &LHS, const MachineOperand &RHS)
Given two VL operands, do we know that LHS <= RHS?
unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode)
static constexpr unsigned RVVBitsPerBlock
static constexpr int64_t VLMaxSentinel
NodeAddr< DefNode * > Def
NodeAddr< UseNode * > Use
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
bool operator!=(uint64_t V1, const APInt &V2)
FunctionPass * createRISCVVLOptimizerPass()
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
auto post_order(const T &G)
Post-order traversal of a graph.
constexpr NextUseDistance max(NextUseDistance A, NextUseDistance B)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.