45#define DEBUG_TYPE "gi-combiner"
54 cl::desc(
"Force all indexed operations to be "
55 "legal for the GlobalISel combiner"));
64 TII(
Builder.getMF().getSubtarget().getInstrInfo()),
65 RBI(
Builder.getMF().getSubtarget().getRegBankInfo()),
66 TRI(
Builder.getMF().getSubtarget().getRegisterInfo()) {
71 return *
Builder.getMF().getSubtarget().getTargetLowering();
85 assert(
I < ByteWidth &&
"I must be in [0, ByteWidth)");
93 LLT Ty = MRI.getType(V);
104 assert(
I < ByteWidth &&
"I must be in [0, ByteWidth)");
105 return ByteWidth -
I - 1;
125static std::optional<bool>
129 unsigned Width = MemOffset2Idx.
size();
132 bool BigEndian =
true, LittleEndian =
true;
133 for (
unsigned MemOffset = 0; MemOffset < Width; ++ MemOffset) {
134 auto MemOffsetAndIdx = MemOffset2Idx.
find(MemOffset);
135 if (MemOffsetAndIdx == MemOffset2Idx.
end())
137 const int64_t Idx = MemOffsetAndIdx->second - LowestIdx;
138 assert(Idx >= 0 &&
"Expected non-negative byte offset?");
141 if (!BigEndian && !LittleEndian)
145 assert((BigEndian != LittleEndian) &&
146 "Pattern cannot be both big and little endian!");
153 assert(
LI &&
"Must have LegalizerInfo to query isLegal!");
181 return isLegal({TargetOpcode::G_BUILD_VECTOR, {Ty, EltTy}}) &&
182 isLegal({TargetOpcode::G_CONSTANT, {EltTy}});
189 if (
MRI.constrainRegAttrs(ToReg, FromReg))
190 MRI.replaceRegWith(FromReg, ToReg);
192 Builder.buildCopy(FromReg, ToReg);
194 Observer.finishedChangingAllUsesOfReg();
209 unsigned ToOpcode)
const {
224 MRI.setRegBank(Reg, *RegBank);
228 if (
MI.getOpcode() != TargetOpcode::COPY)
238 MI.eraseFromParent();
243 assert(
MI.getOpcode() == TargetOpcode::G_FREEZE &&
"Invalid instruction");
249 if (!
MRI.hasOneNonDBGUse(OrigOp))
270 std::optional<MachineOperand> MaybePoisonOperand;
272 if (!Operand.isReg())
278 if (!MaybePoisonOperand)
279 MaybePoisonOperand = Operand;
288 if (!MaybePoisonOperand) {
293 B.buildCopy(
DstOp, OrigOp);
298 Register MaybePoisonOperandReg = MaybePoisonOperand->getReg();
299 LLT MaybePoisonOperandRegTy =
MRI.getType(MaybePoisonOperandReg);
302 {TargetOpcode::G_FREEZE, {MaybePoisonOperandRegTy}}))
310 auto Freeze =
B.buildFreeze(MaybePoisonOperandRegTy, MaybePoisonOperandReg);
321 assert(
MI.getOpcode() == TargetOpcode::G_CONCAT_VECTORS &&
322 "Invalid instruction");
334 if (!
MRI.hasOneNonDBGUse(Reg))
336 switch (Def->getOpcode()) {
337 case TargetOpcode::G_BUILD_VECTOR:
342 Ops.push_back(BuildVecMO.getReg());
344 case TargetOpcode::G_IMPLICIT_DEF: {
345 LLT OpType =
MRI.getType(Reg);
352 OpType.getScalarType() &&
353 "All undefs should have the same type");
356 for (
unsigned EltIdx = 0, EltEnd = OpType.getNumElements();
357 EltIdx != EltEnd; ++EltIdx)
358 Ops.push_back(
Undef->getOperand(0).getReg());
367 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
369 {TargetOpcode::G_BUILD_VECTOR, {DstTy,
MRI.getType(
Ops[0])}})) {
384 Register NewDstReg =
MRI.cloneVirtualRegister(DstReg);
397 MI.eraseFromParent();
406 if (!Unmerge || Unmerge->
getReg(0) != BV.getSourceReg(0))
411 LLT InputTy =
MRI.getType(BCSrc);
413 if (!InputTy.
isScalar() || BV.getNumSources() % Factor != 0)
418 if (!
isLegal({TargetOpcode::G_BUILD_VECTOR, {BVDstTy, InputTy}}))
422 for (
unsigned Idx = 0; Idx < BV.getNumSources(); Idx += Factor) {
438 Ops.push_back(BCSrc);
459 auto BV =
Builder.buildBuildVector(BVDstTy,
Ops);
460 Builder.buildBitcast(
MI.getOperand(0).getReg(), BV);
461 MI.eraseFromParent();
467 Register SrcVec1 = Shuffle.getSrc1Reg();
468 Register SrcVec2 = Shuffle.getSrc2Reg();
469 LLT EltTy =
MRI.getType(SrcVec1).getElementType();
470 int Width =
MRI.getType(SrcVec1).getNumElements();
472 auto Unmerge1 =
Builder.buildUnmerge(EltTy, SrcVec1);
473 auto Unmerge2 =
Builder.buildUnmerge(EltTy, SrcVec2);
477 for (
int Val : Shuffle.getMask()) {
480 else if (Val < Width)
481 Extracts.
push_back(Unmerge1.getReg(Val));
483 Extracts.
push_back(Unmerge2.getReg(Val - Width));
485 assert(Extracts.
size() > 0 &&
"Expected at least one element in the shuffle");
486 if (Extracts.
size() == 1)
487 Builder.buildCopy(
MI.getOperand(0).getReg(), Extracts[0]);
489 Builder.buildBuildVector(
MI.getOperand(0).getReg(), Extracts);
490 MI.eraseFromParent();
507 LLT ShuffleSrcTy1 =
MRI.getType(
MI.getOperand(1).getReg());
509 for (
unsigned i = 0; i < Mask.size(); i += ConcatSrcNumElt) {
513 for (
unsigned j = 1; j < ConcatSrcNumElt; j++) {
514 if (i + j >= Mask.size())
516 if (Mask[i + j] != -1)
520 {TargetOpcode::G_IMPLICIT_DEF, {ConcatSrcTy}}))
523 }
else if (Mask[i] % ConcatSrcNumElt == 0) {
524 for (
unsigned j = 1; j < ConcatSrcNumElt; j++) {
525 if (i + j >= Mask.size())
527 if (Mask[i + j] != Mask[i] +
static_cast<int>(j))
544 {TargetOpcode::G_CONCAT_VECTORS,
545 {
MRI.getType(
MI.getOperand(0).getReg()), ConcatSrcTy}}))
556 SrcTy =
MRI.getType(Reg);
558 assert(SrcTy.isValid() &&
"Unexpected full undef vector in concat combine");
565 UndefReg =
Builder.buildUndef(SrcTy).getReg(0);
571 Builder.buildConcatVectors(
MI.getOperand(0).getReg(),
Ops);
574 MI.eraseFromParent();
579 assert(
MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR &&
580 "Invalid instruction kind");
581 LLT DstType =
MRI.getType(
MI.getOperand(0).getReg());
583 LLT SrcType =
MRI.getType(Src1);
585 unsigned DstNumElts = DstType.getNumElements();
586 unsigned SrcNumElts = SrcType.getNumElements();
603 if (DstNumElts < 2 * SrcNumElts)
608 if (DstNumElts % SrcNumElts != 0)
614 unsigned NumConcat = DstNumElts / SrcNumElts;
617 for (
unsigned i = 0; i != DstNumElts; ++i) {
624 if ((Idx % SrcNumElts != (i % SrcNumElts)) ||
625 (ConcatSrcs[i / SrcNumElts] >= 0 &&
626 ConcatSrcs[i / SrcNumElts] != (
int)(Idx / SrcNumElts)))
629 ConcatSrcs[i / SrcNumElts] = Idx / SrcNumElts;
636 for (
auto Src : ConcatSrcs) {
640 UndefReg =
Builder.buildUndef(SrcType).getReg(0);
642 Ops.push_back(UndefReg);
655 Register NewDstReg =
MRI.cloneVirtualRegister(DstReg);
663 MI.eraseFromParent();
672 const LLT TyForCandidate,
673 unsigned OpcodeForCandidate,
678 return {TyForCandidate, OpcodeForCandidate, MIForCandidate};
689 if (OpcodeForCandidate == TargetOpcode::G_ANYEXT &&
692 else if (CurrentUse.
ExtendOpcode == TargetOpcode::G_ANYEXT &&
693 OpcodeForCandidate != TargetOpcode::G_ANYEXT)
694 return {TyForCandidate, OpcodeForCandidate, MIForCandidate};
702 OpcodeForCandidate == TargetOpcode::G_ZEXT)
704 else if (CurrentUse.
ExtendOpcode == TargetOpcode::G_ZEXT &&
705 OpcodeForCandidate == TargetOpcode::G_SEXT)
706 return {TyForCandidate, OpcodeForCandidate, MIForCandidate};
715 return {TyForCandidate, OpcodeForCandidate, MIForCandidate};
726static void InsertInsnsWithoutSideEffectsBeforeUse(
738 InsertBB = PredBB->
getMBB();
743 if (InsertBB ==
DefMI.getParent()) {
745 Inserter(InsertBB, std::next(InsertPt), UseMO);
764 unsigned CandidateLoadOpc;
766 case TargetOpcode::G_ANYEXT:
767 CandidateLoadOpc = TargetOpcode::G_LOAD;
769 case TargetOpcode::G_SEXT:
770 CandidateLoadOpc = TargetOpcode::G_SEXTLOAD;
772 case TargetOpcode::G_ZEXT:
773 CandidateLoadOpc = TargetOpcode::G_ZEXTLOAD;
778 return CandidateLoadOpc;
795 LLT LoadValueTy =
MRI.getType(LoadReg);
817 unsigned PreferredOpcode =
819 ? TargetOpcode::G_ANYEXT
821 Preferred = {
LLT(), PreferredOpcode,
nullptr};
822 for (
auto &
UseMI :
MRI.use_nodbg_instructions(LoadReg)) {
823 if (
UseMI.getOpcode() == TargetOpcode::G_SEXT ||
824 UseMI.getOpcode() == TargetOpcode::G_ZEXT ||
825 (
UseMI.getOpcode() == TargetOpcode::G_ANYEXT)) {
826 const auto &MMO = LoadMI->
getMMO();
834 LLT UseTy =
MRI.getType(
UseMI.getOperand(0).getReg());
836 if (
LI->getAction({CandidateLoadOpc, {UseTy, SrcTy}, {MMDesc}})
840 Preferred = ChoosePreferredUse(
MI, Preferred,
841 MRI.getType(
UseMI.getOperand(0).getReg()),
851 assert(Preferred.Ty != LoadValueTy &&
"Extending to same type?");
869 if (PreviouslyEmitted) {
876 Builder.setInsertPt(*InsertIntoBB, InsertBefore);
877 Register NewDstReg =
MRI.cloneVirtualRegister(
MI.getOperand(0).getReg());
879 EmittedInsns[InsertIntoBB] = NewMI;
885 MI.setDesc(
Builder.getTII().get(LoadOpc));
892 for (
auto *UseMO :
Uses) {
898 UseMI->getOpcode() == TargetOpcode::G_ANYEXT) {
901 const LLT UseDstTy =
MRI.getType(UseDstReg);
902 if (UseDstReg != ChosenDstReg) {
903 if (Preferred.
Ty == UseDstTy) {
940 InsertInsnsWithoutSideEffectsBeforeUse(
Builder,
MI, *UseMO,
955 InsertInsnsWithoutSideEffectsBeforeUse(
Builder,
MI, *UseMO, InsertTruncAt);
958 MI.getOperand(0).setReg(ChosenDstReg);
964 assert(
MI.getOpcode() == TargetOpcode::G_AND);
975 if (
MRI.getType(Dst).isVector())
983 APInt MaskVal = MaybeMask->Value;
998 LLT RegTy =
MRI.getType(LoadReg);
1001 unsigned MaskSizeBits = MaskVal.
countr_one();
1004 !
MRI.hasOneNonDBGUse(LoadReg))
1009 if (MaskSizeBits > LoadSizeBits)
1028 else if (LoadSizeBits > MaskSizeBits || LoadSizeBits ==
RegSize)
1033 {TargetOpcode::G_ZEXTLOAD, {RegTy,
MRI.getType(PtrReg)}, {MemDesc}}))
1037 B.setInstrAndDebugLoc(*LoadMI);
1038 auto &MF =
B.getMF();
1040 auto *NewMMO = MF.getMachineMemOperand(MMO, PtrInfo, MemDesc.
MemoryTy);
1041 B.buildLoadInstr(TargetOpcode::G_ZEXTLOAD, Dst, PtrReg, *NewMMO);
1051 "shouldn't consider debug uses");
1059 if (DefOrUse ==
MBB.end())
1061 return &*DefOrUse == &
DefMI;
1067 "shouldn't consider debug uses");
1070 else if (
DefMI.getParent() !=
UseMI.getParent())
1077 assert(
MI.getOpcode() == TargetOpcode::G_SEXT_INREG);
1081 if (
MRI.getType(SrcReg).isVector())
1086 LoadUser = TruncSrc;
1088 uint64_t SizeInBits =
MI.getOperand(2).getImm();
1093 auto LoadSizeBits = LoadMI->getMemSizeInBits();
1095 MRI.getType(TruncSrc).getSizeInBits() < LoadSizeBits.getValue())
1097 if (LoadSizeBits == SizeInBits)
1104 MachineInstr &
MI, std::tuple<Register, unsigned> &MatchInfo)
const {
1105 assert(
MI.getOpcode() == TargetOpcode::G_SEXT_INREG);
1108 LLT RegTy =
MRI.getType(DstReg);
1121 uint64_t ExtFrom =
MI.getOperand(2).getImm();
1123 if (MemBits > ExtFrom && !
MRI.hasOneNonDBGUse(SrcReg))
1129 unsigned NewSizeBits = std::min(ExtFrom, MemBits);
1132 if (NewSizeBits < 8)
1145 else if (MemBits > NewSizeBits || MemBits == RegTy.
getSizeInBits())
1150 {TargetOpcode::G_SEXTLOAD, {RegTy,
MRI.getType(PtrReg)}, {MMDesc}}))
1153 MatchInfo = std::make_tuple(SrcReg, NewSizeBits);
1158 MachineInstr &
MI, std::tuple<Register, unsigned> &MatchInfo)
const {
1159 assert(
MI.getOpcode() == TargetOpcode::G_SEXT_INREG);
1161 unsigned ScalarSizeBits;
1162 std::tie(LoadReg, ScalarSizeBits) = MatchInfo;
1171 auto &MMO = LoadDef->
getMMO();
1172 Builder.setInstrAndDebugLoc(*LoadDef);
1174 auto PtrInfo = MMO.getPointerInfo();
1175 auto *NewMMO = MF.getMachineMemOperand(&MMO, PtrInfo, ScalarSizeBits / 8);
1176 Builder.buildLoadInstr(TargetOpcode::G_SEXTLOAD,
MI.getOperand(0).getReg(),
1179 MI.eraseFromParent();
1190 auto *MF =
MI->getMF();
1197 AM.
BaseOffs = CstOff->getSExtValue();
1202 MF->getDataLayout(), AM,
1204 MF->getFunction().getContext()),
1205 MI->getMMO().getAddrSpace());
1210 case TargetOpcode::G_LOAD:
1211 return TargetOpcode::G_INDEXED_LOAD;
1212 case TargetOpcode::G_STORE:
1213 return TargetOpcode::G_INDEXED_STORE;
1214 case TargetOpcode::G_ZEXTLOAD:
1215 return TargetOpcode::G_INDEXED_ZEXTLOAD;
1216 case TargetOpcode::G_SEXTLOAD:
1217 return TargetOpcode::G_INDEXED_SEXTLOAD;
1223bool CombinerHelper::isIndexedLoadStoreLegal(
GLoadStore &LdSt)
const {
1233 if (IndexedOpc == TargetOpcode::G_INDEXED_STORE)
1234 OpTys = {PtrTy, Ty, Ty};
1236 OpTys = {Ty, PtrTy};
1238 LegalityQuery Q(IndexedOpc, OpTys, MemDescrs);
1244 cl::desc(
"Number of uses of a base pointer to check before it is no longer "
1245 "considered for post-indexing."));
1249 bool &RematOffset)
const {
1262 if (!isIndexedLoadStoreLegal(LdSt))
1273 unsigned NumUsesChecked = 0;
1286 if (StoredValDef == &
Use)
1289 Offset = PtrAdd->getOffsetReg();
1291 !TLI.isIndexingLegal(LdSt, PtrAdd->getBaseReg(),
Offset,
1299 RematOffset =
false;
1303 if (OffsetDef->
getOpcode() != TargetOpcode::G_CONSTANT)
1308 for (
auto &BasePtrUse :
MRI.use_nodbg_instructions(PtrAdd->getBaseReg())) {
1309 if (&BasePtrUse == PtrDef)
1315 if (BasePtrLdSt && BasePtrLdSt != &LdSt &&
1317 isIndexedLoadStoreLegal(*BasePtrLdSt))
1323 Register PtrAddDefReg = BasePtrUseDef->getReg(0);
1324 for (
auto &BaseUseUse :
MRI.use_nodbg_instructions(PtrAddDefReg)) {
1327 if (BaseUseUse.getParent() != LdSt.
getParent())
1339 Addr = PtrAdd->getReg(0);
1340 Base = PtrAdd->getBaseReg();
1355 MRI.hasOneNonDBGUse(Addr))
1362 if (!isIndexedLoadStoreLegal(LdSt))
1366 if (BaseDef->
getOpcode() == TargetOpcode::G_FRAME_INDEX)
1371 if (
Base == St->getValueReg())
1376 if (St->getValueReg() == Addr)
1381 for (
auto &AddrUse :
MRI.use_nodbg_instructions(Addr))
1382 if (AddrUse.getParent() != LdSt.
getParent())
1387 bool RealUse =
false;
1388 for (
auto &AddrUse :
MRI.use_nodbg_instructions(Addr)) {
1406 assert(
MI.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT);
1416 assert(
MRI.getType(
MI.getOperand(0).getReg()) == VecEltTy);
1423 if (!LoadMI->isSimple())
1435 const unsigned MaxIter = 20;
1438 if (
II->isLoadFoldBarrier())
1440 if (Iter++ == MaxIter)
1456 int Elt = CVal->getZExtValue();
1469 Register VecPtr = LoadMI->getPointerReg();
1470 LLT PtrTy =
MRI.getType(VecPtr);
1478 {TargetOpcode::G_LOAD, {VecEltTy, PtrTy}, {MMDesc}}))
1501 B.buildLoad(Result, finalPtr, PtrInfo, Alignment);
1516 MatchInfo.
IsPre = findPreIndexCandidate(LdSt, MatchInfo.
Addr, MatchInfo.
Base,
1518 if (!MatchInfo.
IsPre &&
1519 !findPostIndexCandidate(LdSt, MatchInfo.
Addr, MatchInfo.
Base,
1529 unsigned Opcode =
MI.getOpcode();
1530 bool IsStore = Opcode == TargetOpcode::G_STORE;
1536 auto *OldCst =
MRI.getVRegDef(MatchInfo.
Offset);
1538 *OldCst->getOperand(1).getCImm());
1539 MatchInfo.
Offset = NewCst.getReg(0);
1542 auto MIB =
Builder.buildInstr(NewOpcode);
1544 MIB.addDef(MatchInfo.
Addr);
1545 MIB.addUse(
MI.getOperand(0).getReg());
1547 MIB.addDef(
MI.getOperand(0).getReg());
1548 MIB.addDef(MatchInfo.
Addr);
1551 MIB.addUse(MatchInfo.
Base);
1552 MIB.addUse(MatchInfo.
Offset);
1553 MIB.addImm(MatchInfo.
IsPre);
1554 MIB->cloneMemRefs(*
MI.getMF(),
MI);
1555 MI.eraseFromParent();
1563 unsigned Opcode =
MI.getOpcode();
1564 bool IsDiv, IsSigned;
1569 case TargetOpcode::G_SDIV:
1570 case TargetOpcode::G_UDIV: {
1572 IsSigned = Opcode == TargetOpcode::G_SDIV;
1575 case TargetOpcode::G_SREM:
1576 case TargetOpcode::G_UREM: {
1578 IsSigned = Opcode == TargetOpcode::G_SREM;
1584 unsigned DivOpcode, RemOpcode, DivremOpcode;
1586 DivOpcode = TargetOpcode::G_SDIV;
1587 RemOpcode = TargetOpcode::G_SREM;
1588 DivremOpcode = TargetOpcode::G_SDIVREM;
1590 DivOpcode = TargetOpcode::G_UDIV;
1591 RemOpcode = TargetOpcode::G_UREM;
1592 DivremOpcode = TargetOpcode::G_UDIVREM;
1610 for (
auto &
UseMI :
MRI.use_nodbg_instructions(Src1)) {
1611 if (
MI.getParent() ==
UseMI.getParent() &&
1612 ((IsDiv &&
UseMI.getOpcode() == RemOpcode) ||
1613 (!IsDiv &&
UseMI.getOpcode() == DivOpcode)) &&
1626 unsigned Opcode =
MI.getOpcode();
1627 assert(OtherMI &&
"OtherMI shouldn't be empty.");
1630 if (Opcode == TargetOpcode::G_SDIV || Opcode == TargetOpcode::G_UDIV) {
1631 DestDivReg =
MI.getOperand(0).getReg();
1635 DestRemReg =
MI.getOperand(0).getReg();
1639 Opcode == TargetOpcode::G_SDIV || Opcode == TargetOpcode::G_SREM;
1646 Builder.setInstrAndDebugLoc(*FirstInst);
1648 Builder.buildInstr(IsSigned ? TargetOpcode::G_SDIVREM
1649 : TargetOpcode::G_UDIVREM,
1650 {DestDivReg, DestRemReg},
1652 MI.eraseFromParent();
1658 assert(
MI.getOpcode() == TargetOpcode::G_BR);
1675 if (BrIt ==
MBB->begin())
1677 assert(std::next(BrIt) ==
MBB->end() &&
"expected G_BR to be a terminator");
1679 BrCond = &*std::prev(BrIt);
1680 if (BrCond->
getOpcode() != TargetOpcode::G_BRCOND)
1686 return BrCondTarget !=
MI.getOperand(0).getMBB() &&
1687 MBB->isLayoutSuccessor(BrCondTarget);
1693 Builder.setInstrAndDebugLoc(*BrCond);
1704 MI.getOperand(0).setMBB(FallthroughBB);
1717 unsigned MaxLen)
const {
1718 auto &[Dst, Src, KnownLen, Alignment, DstAlignCanChange, MemOps] = MatchInfo;
1720 DstAlignCanChange, MemOps);
1725 auto &[Dst, Src, KnownLen, Alignment, DstAlignCanChange, MemOps] = MatchInfo;
1730 DstAlignCanChange, MemOps) ==
1732 assert(
Changed &&
"expected memcpy-family instruction to lower");
1737 unsigned MaxLen)
const {
1749 switch (
MI.getOpcode()) {
1752 case TargetOpcode::G_FNEG: {
1753 Result.changeSign();
1756 case TargetOpcode::G_FABS: {
1760 case TargetOpcode::G_FCEIL:
1763 case TargetOpcode::G_FFLOOR:
1766 case TargetOpcode::G_INTRINSIC_TRUNC:
1769 case TargetOpcode::G_INTRINSIC_ROUND:
1772 case TargetOpcode::G_INTRINSIC_ROUNDEVEN:
1775 case TargetOpcode::G_FRINT:
1776 case TargetOpcode::G_FNEARBYINT:
1780 case TargetOpcode::G_FPEXT:
1781 case TargetOpcode::G_FPTRUNC: {
1788 case TargetOpcode::G_FSQRT: {
1792 Result =
APFloat(sqrt(Result.convertToDouble()));
1795 case TargetOpcode::G_FLOG2: {
1815 Builder.buildFConstant(
MI.getOperand(0), *NewCst);
1816 MI.eraseFromParent();
1827 if (
MI.getOpcode() != TargetOpcode::G_PTR_ADD)
1850 Type *AccessTy =
nullptr;
1851 auto &MF = *
MI.getMF();
1852 for (
auto &
UseMI :
MRI.use_nodbg_instructions(
MI.getOperand(0).getReg())) {
1855 MF.getFunction().getContext());
1860 APInt CombinedImm = MaybeImmVal->Value + MaybeImm2Val->Value;
1865 AMOld.
BaseOffs = MaybeImmVal->Value.getSExtValue();
1867 unsigned AS =
MRI.getType(Add2).getAddressSpace();
1868 const auto &TLI = *MF.getSubtarget().getTargetLowering();
1869 if (TLI.isLegalAddressingMode(MF.getDataLayout(), AMOld, AccessTy, AS) &&
1870 !TLI.isLegalAddressingMode(MF.getDataLayout(), AMNew, AccessTy, AS))
1879 unsigned PtrAddFlags =
MI.getFlags();
1895 MatchInfo.
Flags = Flags;
1901 assert(
MI.getOpcode() == TargetOpcode::G_PTR_ADD &&
"Expected G_PTR_ADD");
1903 LLT OffsetTy =
MRI.getType(
MI.getOperand(2).getReg());
1907 MI.getOperand(1).setReg(MatchInfo.
Base);
1908 MI.getOperand(2).setReg(NewOffset.getReg(0));
1922 unsigned Opcode =
MI.getOpcode();
1923 assert((Opcode == TargetOpcode::G_SHL || Opcode == TargetOpcode::G_ASHR ||
1924 Opcode == TargetOpcode::G_LSHR || Opcode == TargetOpcode::G_SSHLSAT ||
1925 Opcode == TargetOpcode::G_USHLSAT) &&
1926 "Expected G_SHL, G_ASHR, G_LSHR, G_SSHLSAT or G_USHLSAT");
1946 (MaybeImmVal->Value.getZExtValue() + MaybeImm2Val->Value).getZExtValue();
1951 if (Opcode == TargetOpcode::G_USHLSAT &&
1952 MatchInfo.
Imm >=
MRI.getType(Shl2).getScalarSizeInBits())
1960 unsigned Opcode =
MI.getOpcode();
1961 assert((Opcode == TargetOpcode::G_SHL || Opcode == TargetOpcode::G_ASHR ||
1962 Opcode == TargetOpcode::G_LSHR || Opcode == TargetOpcode::G_SSHLSAT ||
1963 Opcode == TargetOpcode::G_USHLSAT) &&
1964 "Expected G_SHL, G_ASHR, G_LSHR, G_SSHLSAT or G_USHLSAT");
1966 LLT Ty =
MRI.getType(
MI.getOperand(1).getReg());
1967 unsigned const ScalarSizeInBits = Ty.getScalarSizeInBits();
1968 auto Imm = MatchInfo.
Imm;
1970 if (
Imm >= ScalarSizeInBits) {
1972 if (Opcode == TargetOpcode::G_SHL || Opcode == TargetOpcode::G_LSHR) {
1973 Builder.buildConstant(
MI.getOperand(0), 0);
1974 MI.eraseFromParent();
1979 Imm = ScalarSizeInBits - 1;
1982 LLT ImmTy =
MRI.getType(
MI.getOperand(2).getReg());
1985 MI.getOperand(1).setReg(MatchInfo.
Reg);
1986 MI.getOperand(2).setReg(NewImm);
2002 unsigned ShiftOpcode =
MI.getOpcode();
2003 assert((ShiftOpcode == TargetOpcode::G_SHL ||
2004 ShiftOpcode == TargetOpcode::G_ASHR ||
2005 ShiftOpcode == TargetOpcode::G_LSHR ||
2006 ShiftOpcode == TargetOpcode::G_USHLSAT ||
2007 ShiftOpcode == TargetOpcode::G_SSHLSAT) &&
2008 "Expected G_SHL, G_ASHR, G_LSHR, G_USHLSAT and G_SSHLSAT");
2011 Register LogicDest =
MI.getOperand(1).getReg();
2012 if (!
MRI.hasOneNonDBGUse(LogicDest))
2018 unsigned LogicOpcode = LogicMI->
getOpcode();
2019 if (LogicOpcode != TargetOpcode::G_AND && LogicOpcode != TargetOpcode::G_OR &&
2020 LogicOpcode != TargetOpcode::G_XOR)
2024 const Register C1 =
MI.getOperand(2).getReg();
2026 if (!MaybeImmVal || MaybeImmVal->Value == 0)
2029 const uint64_t C1Val = MaybeImmVal->Value.getZExtValue();
2031 auto matchFirstShift = [&](
const MachineInstr *
MI, uint64_t &ShiftVal) {
2033 if (
MI->getOpcode() != ShiftOpcode ||
2034 !
MRI.hasOneNonDBGUse(
MI->getOperand(0).getReg()))
2043 ShiftVal = MaybeImmVal->Value.getSExtValue();
2057 if (matchFirstShift(LogicMIOp1, C0Val)) {
2059 MatchInfo.
Shift2 = LogicMIOp1;
2060 }
else if (matchFirstShift(LogicMIOp2, C0Val)) {
2062 MatchInfo.
Shift2 = LogicMIOp2;
2066 MatchInfo.
ValSum = C0Val + C1Val;
2069 if (MatchInfo.
ValSum >=
MRI.getType(LogicDest).getScalarSizeInBits())
2072 MatchInfo.
Logic = LogicMI;
2078 unsigned Opcode =
MI.getOpcode();
2079 assert((Opcode == TargetOpcode::G_SHL || Opcode == TargetOpcode::G_ASHR ||
2080 Opcode == TargetOpcode::G_LSHR || Opcode == TargetOpcode::G_USHLSAT ||
2081 Opcode == TargetOpcode::G_SSHLSAT) &&
2082 "Expected G_SHL, G_ASHR, G_LSHR, G_USHLSAT and G_SSHLSAT");
2084 LLT ShlType =
MRI.getType(
MI.getOperand(2).getReg());
2085 LLT DestType =
MRI.getType(
MI.getOperand(0).getReg());
2091 Builder.buildInstr(Opcode, {DestType}, {Shift1Base, Const}).
getReg(0);
2100 Register Shift2Const =
MI.getOperand(2).getReg();
2102 .buildInstr(Opcode, {DestType},
2112 MI.eraseFromParent();
2124 assert(
MI.getOpcode() == TargetOpcode::G_LSHR &&
"Expected a G_LSHR");
2128 unsigned OpSizeInBits =
MRI.getType(N0).getScalarSizeInBits();
2143 LLT InnerShiftTy =
MRI.getType(InnerShift);
2145 if ((N1C + N001C).ult(InnerShiftSize)) {
2151 if ((N001C + OpSizeInBits) == InnerShiftSize)
2153 if (
MRI.hasOneUse(N0) &&
MRI.hasOneUse(InnerShift)) {
2154 MatchInfo.
Mask =
true;
2164 assert(
MI.getOpcode() == TargetOpcode::G_LSHR &&
"Expected a G_LSHR");
2171 if (MatchInfo.
Mask ==
true) {
2179 Builder.buildTrunc(Dst, Shift);
2180 MI.eraseFromParent();
2184 unsigned &ShiftVal)
const {
2185 assert(
MI.getOpcode() == TargetOpcode::G_MUL &&
"Expected a G_MUL");
2191 ShiftVal = MaybeImmVal->Value.exactLogBase2();
2192 return (
static_cast<int32_t
>(ShiftVal) != -1);
2196 unsigned &ShiftVal)
const {
2197 assert(
MI.getOpcode() == TargetOpcode::G_MUL &&
"Expected a G_MUL");
2199 LLT ShiftTy =
MRI.getType(
MI.getOperand(0).getReg());
2202 MI.setDesc(MIB.
getTII().
get(TargetOpcode::G_SHL));
2203 MI.getOperand(2).setReg(ShiftCst.getReg(0));
2224 auto NegCst =
B.buildConstant(Ty, -
Imm);
2226 MI.setDesc(
B.getTII().get(TargetOpcode::G_ADD));
2227 MI.getOperand(2).setReg(NegCst.getReg(0));
2229 if (
Imm.isMinSignedValue())
2239 assert(
MI.getOpcode() == TargetOpcode::G_SHL &&
VT);
2253 if (!MaybeShiftAmtVal)
2257 LLT SrcTy =
MRI.getType(ExtSrc);
2267 int64_t ShiftAmt = MaybeShiftAmtVal->getSExtValue();
2268 MatchData.
Reg = ExtSrc;
2269 MatchData.
Imm = ShiftAmt;
2271 unsigned MinLeadingZeros =
VT->getKnownZeroes(ExtSrc).countl_one();
2272 unsigned SrcTySize =
MRI.getType(ExtSrc).getScalarSizeInBits();
2273 return MinLeadingZeros >= ShiftAmt && ShiftAmt < SrcTySize;
2279 int64_t ShiftAmtVal = MatchData.
Imm;
2281 LLT ExtSrcTy =
MRI.getType(ExtSrcReg);
2282 auto ShiftAmt =
Builder.buildConstant(ExtSrcTy, ShiftAmtVal);
2284 Builder.buildShl(ExtSrcTy, ExtSrcReg, ShiftAmt,
MI.getFlags());
2285 Builder.buildZExt(
MI.getOperand(0), NarrowShift);
2286 MI.eraseFromParent();
2299 assert(
MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES &&
2300 "Expected an unmerge");
2309 LLT SrcMergeTy =
MRI.getType(SrcInstr->getSourceReg(0));
2310 LLT Dst0Ty =
MRI.getType(Unmerge.getReg(0));
2312 if (SrcMergeTy != Dst0Ty && !SameSize)
2316 for (
unsigned Idx = 0; Idx < SrcInstr->getNumSources(); ++Idx)
2317 Operands.push_back(SrcInstr->getSourceReg(Idx));
2323 assert(
MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES &&
2324 "Expected an unmerge");
2326 "Not enough operands to replace all defs");
2327 unsigned NumElems =
MI.getNumOperands() - 1;
2330 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
2331 bool CanReuseInputDirectly = DstTy == SrcTy;
2332 for (
unsigned Idx = 0; Idx < NumElems; ++Idx) {
2333 Register DstReg =
MI.getOperand(Idx).getReg();
2338 const auto &DstCB =
MRI.getRegClassOrRegBank(DstReg);
2339 if (!DstCB.isNull() && DstCB !=
MRI.getRegClassOrRegBank(SrcReg)) {
2340 SrcReg =
Builder.buildCopy(
MRI.getType(SrcReg), SrcReg).getReg(0);
2341 MRI.setRegClassOrRegBank(SrcReg, DstCB);
2344 if (CanReuseInputDirectly)
2347 Builder.buildCast(DstReg, SrcReg);
2349 MI.eraseFromParent();
2354 unsigned SrcIdx =
MI.getNumOperands() - 1;
2355 Register SrcReg =
MI.getOperand(SrcIdx).getReg();
2361 LLT Dst0Ty =
MRI.getType(
MI.getOperand(0).getReg());
2364 for (
unsigned Idx = 0; Idx != SrcIdx; ++Idx) {
2366 Val = Val.
lshr(ShiftAmt);
2374 assert(
MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES &&
2375 "Expected an unmerge");
2377 "Not enough operands to replace all defs");
2378 unsigned NumElems =
MI.getNumOperands() - 1;
2379 for (
unsigned Idx = 0; Idx < NumElems; ++Idx) {
2380 Register DstReg =
MI.getOperand(Idx).getReg();
2381 Builder.buildConstant(DstReg, Csts[Idx]);
2384 MI.eraseFromParent();
2390 unsigned SrcIdx =
MI.getNumOperands() - 1;
2391 Register SrcReg =
MI.getOperand(SrcIdx).getReg();
2393 unsigned NumElems =
MI.getNumOperands() - 1;
2394 for (
unsigned Idx = 0; Idx < NumElems; ++Idx) {
2395 Register DstReg =
MI.getOperand(Idx).getReg();
2396 B.buildUndef(DstReg);
2404 assert(
MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES &&
2405 "Expected an unmerge");
2406 if (!
MRI.getType(
MI.getOperand(0).getReg()).isScalar() ||
2407 !
MRI.getType(
MI.getOperand(
MI.getNumDefs()).getReg()).isScalar())
2410 for (
unsigned Idx = 1, EndIdx =
MI.getNumDefs(); Idx != EndIdx; ++Idx) {
2411 if (!
MRI.use_nodbg_empty(
MI.getOperand(Idx).getReg()))
2419 Register SrcReg =
MI.getOperand(
MI.getNumDefs()).getReg();
2420 Register Dst0Reg =
MI.getOperand(0).getReg();
2421 Builder.buildTrunc(Dst0Reg, SrcReg);
2422 MI.eraseFromParent();
2426 assert(
MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES &&
2427 "Expected an unmerge");
2428 Register Dst0Reg =
MI.getOperand(0).getReg();
2429 LLT Dst0Ty =
MRI.getType(Dst0Reg);
2435 Register SrcReg =
MI.getOperand(
MI.getNumDefs()).getReg();
2436 LLT SrcTy =
MRI.getType(SrcReg);
2437 if (SrcTy.isVector())
2447 LLT ZExtSrcTy =
MRI.getType(ZExtSrcReg);
2452 assert(
MI.getOpcode() == TargetOpcode::G_UNMERGE_VALUES &&
2453 "Expected an unmerge");
2455 Register Dst0Reg =
MI.getOperand(0).getReg();
2460 LLT Dst0Ty =
MRI.getType(Dst0Reg);
2461 LLT ZExtSrcTy =
MRI.getType(ZExtSrcReg);
2464 Builder.buildZExt(Dst0Reg, ZExtSrcReg);
2467 "ZExt src doesn't fit in destination");
2472 for (
unsigned Idx = 1, EndIdx =
MI.getNumDefs(); Idx != EndIdx; ++Idx) {
2474 ZeroReg =
Builder.buildConstant(Dst0Ty, 0).getReg(0);
2477 MI.eraseFromParent();
2481 unsigned TargetShiftSize,
2482 unsigned &ShiftVal)
const {
2483 assert((
MI.getOpcode() == TargetOpcode::G_SHL ||
2484 MI.getOpcode() == TargetOpcode::G_LSHR ||
2485 MI.getOpcode() == TargetOpcode::G_ASHR) &&
"Expected a shift");
2487 LLT Ty =
MRI.getType(
MI.getOperand(0).getReg());
2492 unsigned Size = Ty.getSizeInBits();
2493 if (
Size <= TargetShiftSize)
2501 ShiftVal = MaybeImmVal->Value.getSExtValue();
2502 return ShiftVal >=
Size / 2 && ShiftVal <
Size;
2509 LLT Ty =
MRI.getType(SrcReg);
2510 unsigned Size = Ty.getSizeInBits();
2511 unsigned HalfSize =
Size / 2;
2512 assert(ShiftVal >= HalfSize);
2516 auto Unmerge =
Builder.buildUnmerge(HalfTy, SrcReg);
2517 unsigned NarrowShiftAmt = ShiftVal - HalfSize;
2519 if (
MI.getOpcode() == TargetOpcode::G_LSHR) {
2520 Register Narrowed = Unmerge.getReg(1);
2527 if (NarrowShiftAmt != 0) {
2528 Narrowed =
Builder.buildLShr(HalfTy, Narrowed,
2529 Builder.buildConstant(HalfTy, NarrowShiftAmt)).getReg(0);
2532 auto Zero =
Builder.buildConstant(HalfTy, 0);
2533 Builder.buildMergeLikeInstr(DstReg, {Narrowed, Zero});
2534 }
else if (
MI.getOpcode() == TargetOpcode::G_SHL) {
2535 Register Narrowed = Unmerge.getReg(0);
2540 if (NarrowShiftAmt != 0) {
2541 Narrowed =
Builder.buildShl(HalfTy, Narrowed,
2542 Builder.buildConstant(HalfTy, NarrowShiftAmt)).getReg(0);
2545 auto Zero =
Builder.buildConstant(HalfTy, 0);
2546 Builder.buildMergeLikeInstr(DstReg, {Zero, Narrowed});
2548 assert(
MI.getOpcode() == TargetOpcode::G_ASHR);
2550 HalfTy, Unmerge.getReg(1),
2551 Builder.buildConstant(HalfTy, HalfSize - 1));
2553 if (ShiftVal == HalfSize) {
2556 Builder.buildMergeLikeInstr(DstReg, {Unmerge.getReg(1),
Hi});
2557 }
else if (ShiftVal ==
Size - 1) {
2565 HalfTy, Unmerge.getReg(1),
2566 Builder.buildConstant(HalfTy, ShiftVal - HalfSize));
2574 MI.eraseFromParent();
2590 assert(
MI.getOpcode() == TargetOpcode::G_ANYEXT &&
"Expected a G_ANYEXT");
2595 SrcReg = OriginalSrcReg;
2596 LLT DstTy =
MRI.getType(DstReg);
2604 assert(
MI.getOpcode() == TargetOpcode::G_ZEXT &&
"Expected a G_ZEXT");
2607 LLT DstTy =
MRI.getType(DstReg);
2612 unsigned SrcSize =
MRI.getType(SrcReg).getScalarSizeInBits();
2613 return VT->getKnownBits(Reg).countMinLeadingZeros() >= DstSize - SrcSize;
2623 if (ShiftSize > 32 && TruncSize < 32)
2636 MachineInstr &
MI, std::pair<MachineInstr *, LLT> &MatchInfo)
const {
2637 assert(
MI.getOpcode() == TargetOpcode::G_TRUNC &&
"Expected a G_TRUNC");
2641 if (!
MRI.hasOneNonDBGUse(SrcReg))
2644 LLT SrcTy =
MRI.getType(SrcReg);
2645 LLT DstTy =
MRI.getType(DstReg);
2654 case TargetOpcode::G_SHL: {
2663 case TargetOpcode::G_LSHR:
2664 case TargetOpcode::G_ASHR: {
2670 for (
auto &
User :
MRI.use_instructions(DstReg))
2671 if (
User.getOpcode() == TargetOpcode::G_STORE)
2675 if (NewShiftTy == SrcTy)
2689 {NewShiftTy, TL.getPreferredShiftAmountTy(NewShiftTy)}}))
2692 MatchInfo = std::make_pair(SrcMI, NewShiftTy);
2697 MachineInstr &
MI, std::pair<MachineInstr *, LLT> &MatchInfo)
const {
2699 LLT NewShiftTy = MatchInfo.second;
2702 LLT DstTy =
MRI.getType(Dst);
2706 ShiftSrc =
Builder.buildTrunc(NewShiftTy, ShiftSrc).getReg(0);
2709 LLT PrefShiftTy = TL.getPreferredShiftAmountTy(NewShiftTy);
2710 if (
MRI.getType(ShiftAmt) != PrefShiftTy)
2711 ShiftAmt =
Builder.buildZExtOrTrunc(PrefShiftTy, ShiftAmt).getReg(0);
2715 .buildInstr(ShiftMI->
getOpcode(), {NewShiftTy}, {ShiftSrc, ShiftAmt})
2718 if (NewShiftTy == DstTy)
2721 Builder.buildTrunc(Dst, NewShift);
2728 return !MO.isReg() ||
2729 getOpcodeDef(TargetOpcode::G_IMPLICIT_DEF, MO.getReg(), MRI);
2734 assert(
MI.getOpcode() == TargetOpcode::G_SHUFFLE_VECTOR);
2736 return all_of(Mask, [](
int Elt) {
return Elt < 0; });
2741 assert((
MI.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT ||
2742 MI.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT) &&
2743 "Expected an insert/extract element op");
2744 LLT VecTy =
MRI.getType(
MI.getOperand(1).getReg());
2749 MI.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT ? 2 : 3;
2797 if (I1->mayLoadOrStore() && !I1->isDereferenceableInvariantLoad())
2824 return MO.isReg() && MO.getReg().isPhysical();
2834 return I1->isIdenticalTo(*I2);
2842 if (
Builder.getTII().produceSameValue(*I1, *I2, &
MRI)) {
2849 return I1->findRegisterDefOperandIdx(InstAndDef1->Reg,
nullptr) ==
2870 std::optional<FPValueAndVReg> MaybeCst;
2874 return MaybeCst->Value.isExactlyValue(
C);
2878 unsigned OpIdx)
const {
2879 assert(
MI.getNumExplicitDefs() == 1 &&
"Expected one explicit def?");
2881 Register Replacement =
MI.getOperand(OpIdx).getReg();
2884 MI.eraseFromParent();
2889 assert(
MI.getNumExplicitDefs() == 1 &&
"Expected one explicit def?");
2893 MI.eraseFromParent();
2897 unsigned ConstIdx)
const {
2898 Register ConstReg =
MI.getOperand(ConstIdx).getReg();
2899 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
2911 assert((
MI.getOpcode() == TargetOpcode::G_FSHL ||
2912 MI.getOpcode() == TargetOpcode::G_FSHR) &&
2913 "This is not a funnel shift operation");
2915 Register ConstReg =
MI.getOperand(3).getReg();
2916 LLT ConstTy =
MRI.getType(ConstReg);
2917 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
2920 assert((VRegAndVal) &&
"Value is not a constant");
2923 APInt NewConst = VRegAndVal->Value.
urem(
2928 MI.getOpcode(), {MI.getOperand(0)},
2929 {MI.getOperand(1), MI.getOperand(2), NewConstInstr.getReg(0)});
2931 MI.eraseFromParent();
2935 assert(
MI.getOpcode() == TargetOpcode::G_SELECT);
2949 assert(
MI.getNumDefs() == 1 &&
"Expected only one def?");
2951 MI.eraseFromParent();
2956 assert(
MI.getNumDefs() == 1 &&
"Expected only one def?");
2958 MI.eraseFromParent();
2962 assert(
MI.getNumDefs() == 1 &&
"Expected only one def?");
2964 MI.eraseFromParent();
2969 assert(
MI.getNumDefs() == 1 &&
"Expected only one def?");
2971 MI.eraseFromParent();
2975 assert(
MI.getNumDefs() == 1 &&
"Expected only one def?");
2977 MI.eraseFromParent();
2982 assert(
MI.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT &&
2985 LLT DstTy =
MRI.getType(DstReg);
2994 if (
MRI.hasOneUse(DstReg) &&
MRI.use_instr_begin(DstReg)->getOpcode() ==
2995 TargetOpcode::G_INSERT_VECTOR_ELT)
3001 MatchInfo.
resize(NumElts);
3005 if (IntImm >= NumElts || IntImm < 0)
3007 if (!MatchInfo[IntImm])
3008 MatchInfo[IntImm] = TmpReg;
3012 if (CurrInst->
getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT)
3014 if (TmpInst->
getOpcode() == TargetOpcode::G_BUILD_VECTOR) {
3023 return TmpInst->
getOpcode() == TargetOpcode::G_IMPLICIT_DEF ||
3030 auto GetUndef = [&]() {
3033 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
3041 Builder.buildBuildVector(
MI.getOperand(0).getReg(), MatchInfo);
3042 MI.eraseFromParent();
3055 unsigned InnerOpc = InnerDef->
getOpcode();
3056 if (InnerOpc != TargetOpcode::G_ADD && InnerOpc != TargetOpcode::G_SUB)
3080 if (!TryMatch(InnerLHS, InnerRHS) &&
3081 !(InnerOpc == TargetOpcode::G_ADD && TryMatch(InnerRHS, InnerLHS)))
3085 unsigned FlippedOpc = (InnerOpc == TargetOpcode::G_ADD) ? TargetOpcode::G_SUB
3086 : TargetOpcode::G_ADD;
3089 MatchInfo = [=](MachineIRBuilder &
Builder) {
3090 auto NewInner =
Builder.buildInstr(FlippedOpc, {Ty}, {
B,
C});
3091 auto NewNot =
Builder.buildNot(Ty, NewInner);
3092 Builder.buildInstr(RootOpc, {Dst}, {
A, NewNot});
3104 unsigned RootOpc =
MI.getOpcode();
3106 LLT Ty =
MRI.getType(Dst);
3111 return matchBinopWithNegInner(LHS, RHS, RootOpc, Dst, Ty, MatchInfo) ||
3112 matchBinopWithNegInner(RHS, LHS, RootOpc, Dst, Ty, MatchInfo);
3123 unsigned LogicOpcode =
MI.getOpcode();
3124 assert(LogicOpcode == TargetOpcode::G_AND ||
3125 LogicOpcode == TargetOpcode::G_OR ||
3126 LogicOpcode == TargetOpcode::G_XOR);
3133 if (!
MRI.hasOneNonDBGUse(LHSReg) || !
MRI.hasOneNonDBGUse(RHSReg))
3139 if (!LeftHandInst || !RightHandInst)
3141 unsigned HandOpcode = LeftHandInst->
getOpcode();
3142 if (HandOpcode != RightHandInst->
getOpcode())
3156 if (!XTy.
isValid() || XTy != YTy)
3161 switch (HandOpcode) {
3164 case TargetOpcode::G_ANYEXT:
3165 case TargetOpcode::G_SEXT:
3166 case TargetOpcode::G_ZEXT: {
3170 case TargetOpcode::G_TRUNC: {
3175 LLT DstTy =
MRI.getType(Dst);
3184 case TargetOpcode::G_AND:
3185 case TargetOpcode::G_ASHR:
3186 case TargetOpcode::G_LSHR:
3187 case TargetOpcode::G_SHL: {
3192 ExtraHandOpSrcReg = ZOp.
getReg();
3203 auto NewLogicDst =
MRI.createGenericVirtualRegister(XTy);
3214 if (ExtraHandOpSrcReg.
isValid())
3226 "Expected at least one instr to build?");
3228 assert(InstrToBuild.Opcode &&
"Expected a valid opcode?");
3229 assert(InstrToBuild.OperandFns.size() &&
"Expected at least one operand?");
3231 for (
auto &OperandFn : InstrToBuild.OperandFns)
3234 MI.eraseFromParent();
3238 MachineInstr &
MI, std::tuple<Register, int64_t> &MatchInfo)
const {
3239 assert(
MI.getOpcode() == TargetOpcode::G_ASHR);
3240 int64_t ShlCst, AshrCst;
3246 if (ShlCst != AshrCst)
3249 {TargetOpcode::G_SEXT_INREG,
3252 {
MRI.getType(Src).getScalarSizeInBits() - ShlCst}}))
3254 MatchInfo = std::make_tuple(Src, ShlCst);
3259 MachineInstr &
MI, std::tuple<Register, int64_t> &MatchInfo)
const {
3260 assert(
MI.getOpcode() == TargetOpcode::G_ASHR);
3263 std::tie(Src, ShiftAmt) = MatchInfo;
3264 unsigned Size =
MRI.getType(Src).getScalarSizeInBits();
3265 Builder.buildSExtInReg(
MI.getOperand(0).getReg(), Src,
Size - ShiftAmt);
3266 MI.eraseFromParent();
3273 assert(
MI.getOpcode() == TargetOpcode::G_AND);
3276 LLT Ty =
MRI.getType(Dst);
3288 B.buildAnd(Dst, R,
B.buildConstant(Ty, C1 & C2));
3291 auto Zero =
B.buildConstant(Ty, 0);
3314 assert(
MI.getOpcode() == TargetOpcode::G_AND);
3338 (LHSBits.
Zero | RHSBits.
One).isAllOnes()) {
3345 (LHSBits.
One | RHSBits.
Zero).isAllOnes()) {
3362 assert(
MI.getOpcode() == TargetOpcode::G_OR);
3380 (LHSBits.
One | RHSBits.
Zero).isAllOnes()) {
3387 (LHSBits.
Zero | RHSBits.
One).isAllOnes()) {
3398 unsigned ExtBits =
MI.getOperand(2).getImm();
3399 unsigned TypeSize =
MRI.getType(Src).getScalarSizeInBits();
3400 return VT->computeNumSignBits(Src) >= (
TypeSize - ExtBits + 1);
3404 int64_t Cst,
bool IsVector,
bool IsFP) {
3406 return (ScalarSizeBits == 1 && Cst == -1) ||
3428 unsigned BuildUseCount = BV.getNumSources();
3429 if (BuildUseCount % 2 != 0)
3432 unsigned NumUnmerge = BuildUseCount / 2;
3438 if (!Unmerge || Unmerge->getNumDefs() != NumUnmerge)
3441 UnmergeSrc = Unmerge->getSourceReg();
3443 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
3444 LLT UnmergeSrcTy =
MRI.getType(UnmergeSrc);
3451 !
isLegal({TargetOpcode::G_CONCAT_VECTORS, {DstTy, UnmergeSrcTy}}))
3456 for (
unsigned I = 0;
I < NumUnmerge; ++
I) {
3457 auto MaybeUnmergeReg = BV.getSourceReg(
I);
3460 if (!LoopUnmerge || LoopUnmerge != Unmerge)
3463 if (LoopUnmerge->getOperand(
I).getReg() != MaybeUnmergeReg)
3468 if (Unmerge->getNumDefs() != NumUnmerge)
3472 for (
unsigned I = NumUnmerge;
I < BuildUseCount; ++
I) {
3475 if (
Undef->getOpcode() != TargetOpcode::G_IMPLICIT_DEF)
3486 assert(UnmergeSrc &&
"Expected there to be one matching G_UNMERGE_VALUES");
3487 B.setInstrAndDebugLoc(
MI);
3489 Register UndefVec =
B.buildUndef(
MRI.getType(UnmergeSrc)).getReg(0);
3490 B.buildConcatVectors(
MI.getOperand(0), {UnmergeSrc, UndefVec});
3492 MI.eraseFromParent();
3514 unsigned NumOperands =
BuildMI->getNumSources();
3524 for (
I = 0;
I < NumOperands; ++
I) {
3536 UnmergeMI != UnmergeSrcMI)
3547 for (;
I < NumOperands; ++
I) {
3554 LLT UnmergeSrcTy =
MRI.getType(MatchInfo);
3564 LLT UnmergeDstEltTy =
MRI.getType(UnmergeDstReg);
3565 if (UnmergeSrcEltTy != UnmergeDstEltTy)
3573 !
isLegal({TargetOpcode::G_CONCAT_VECTORS, {MidTy, UnmergeSrcTy}}))
3576 if (!
isLegal({TargetOpcode::G_TRUNC, {DstTy, MidTy}}))
3588 LLT DstTy =
MRI.getType(DstReg);
3589 LLT UnmergeSrcTy =
MRI.getType(MatchInfo);
3594 if (DstTyNumElt / UnmergeSrcTyNumElt == 1) {
3599 for (
unsigned I = 1;
I < DstTyNumElt / UnmergeSrcTyNumElt; ++
I)
3603 MidReg =
Builder.buildConcatVectors(MidTy, ConcatRegs).getReg(0);
3606 Builder.buildTrunc(DstReg, MidReg);
3607 MI.eraseFromParent();
3612 assert(
MI.getOpcode() == TargetOpcode::G_XOR);
3613 LLT Ty =
MRI.getType(
MI.getOperand(0).getReg());
3614 const auto &TLI = *
Builder.getMF().getSubtarget().getTargetLowering();
3622 if (!
MRI.hasOneNonDBGUse(XorSrc))
3632 for (
unsigned I = 0;
I < RegsToNegate.
size(); ++
I) {
3634 if (!
MRI.hasOneNonDBGUse(Reg))
3639 switch (Def->getOpcode()) {
3644 case TargetOpcode::G_ICMP:
3650 case TargetOpcode::G_FCMP:
3656 case TargetOpcode::G_AND:
3657 case TargetOpcode::G_OR:
3663 RegsToNegate.
push_back(Def->getOperand(1).getReg());
3664 RegsToNegate.
push_back(Def->getOperand(2).getReg());
3672 if (Ty.isVector()) {
3676 if (!
isConstValidTrue(TLI, Ty.getScalarSizeInBits(), SplatCst,
true, IsFP))
3690 for (
Register Reg : RegsToNegate) {
3695 switch (Def->getOpcode()) {
3698 case TargetOpcode::G_ICMP:
3699 case TargetOpcode::G_FCMP: {
3706 case TargetOpcode::G_AND:
3707 Def->setDesc(
Builder.getTII().get(TargetOpcode::G_OR));
3709 case TargetOpcode::G_OR:
3710 Def->setDesc(
Builder.getTII().get(TargetOpcode::G_AND));
3717 MI.eraseFromParent();
3721 MachineInstr &
MI, std::pair<Register, Register> &MatchInfo)
const {
3723 assert(
MI.getOpcode() == TargetOpcode::G_XOR);
3727 Register SharedReg =
MI.getOperand(2).getReg();
3741 if (!
MRI.hasOneNonDBGUse(AndReg))
3748 return Y == SharedReg;
3752 MachineInstr &
MI, std::pair<Register, Register> &MatchInfo)
const {
3755 std::tie(
X,
Y) = MatchInfo;
3758 MI.setDesc(
Builder.getTII().get(TargetOpcode::G_AND));
3759 MI.getOperand(1).setReg(Not->getOperand(0).getReg());
3760 MI.getOperand(2).setReg(
Y);
3766 Register DstReg = PtrAdd.getReg(0);
3767 LLT Ty =
MRI.getType(DstReg);
3770 if (
DL.isNonIntegralAddressSpace(Ty.getScalarType().getAddressSpace()))
3785 Register Pow2Src1 =
MI.getOperand(2).getReg();
3786 LLT Ty =
MRI.getType(DstReg);
3789 auto NegOne =
Builder.buildConstant(Ty, -1);
3790 auto Add =
Builder.buildAdd(Ty, Pow2Src1, NegOne);
3792 MI.eraseFromParent();
3796 unsigned &SelectOpNo)
const {
3808 !
MRI.hasOneNonDBGUse(LHS)) {
3809 OtherOperandReg = LHS;
3813 !
MRI.hasOneNonDBGUse(RHS))
3831 unsigned BinOpcode =
MI.getOpcode();
3836 bool CanFoldNonConst =
3837 (BinOpcode == TargetOpcode::G_AND || BinOpcode == TargetOpcode::G_OR) &&
3842 if (CanFoldNonConst)
3867 LLT Ty =
MRI.getType(Dst);
3868 unsigned BinOpcode =
MI.getOpcode();
3875 if (SelectOperand == 1) {
3879 FoldTrue =
Builder.buildInstr(BinOpcode, {Ty}, {SelectTrue, RHS}).
getReg(0);
3881 Builder.buildInstr(BinOpcode, {Ty}, {SelectFalse, RHS}).
getReg(0);
3883 FoldTrue =
Builder.buildInstr(BinOpcode, {Ty}, {LHS, SelectTrue}).
getReg(0);
3885 Builder.buildInstr(BinOpcode, {Ty}, {LHS, SelectFalse}).
getReg(0);
3888 Builder.buildSelect(Dst, SelectCond, FoldTrue, FoldFalse,
MI.getFlags());
3889 MI.eraseFromParent();
3892std::optional<SmallVector<Register, 8>>
3893CombinerHelper::findCandidatesForLoadOrCombine(
const MachineInstr *Root)
const {
3894 assert(Root->
getOpcode() == TargetOpcode::G_OR &&
"Expected G_OR only!");
3923 const unsigned MaxIter =
3925 for (
unsigned Iter = 0; Iter < MaxIter; ++Iter) {
3934 return std::nullopt;
3950 if (RegsToVisit.
empty() || RegsToVisit.
size() % 2 != 0)
3951 return std::nullopt;
3963static std::optional<std::pair<GZExtLoad *, int64_t>>
3967 "Expected Reg to only have one non-debug use?");
3976 if (Shift % MemSizeInBits != 0)
3977 return std::nullopt;
3982 return std::nullopt;
3984 if (!
Load->isUnordered() ||
Load->getMemSizeInBits() != MemSizeInBits)
3985 return std::nullopt;
3987 return std::make_pair(
Load, Shift / MemSizeInBits);
3990std::optional<std::tuple<GZExtLoad *, int64_t, GZExtLoad *>>
3991CombinerHelper::findLoadOffsetsForLoadOrCombine(
3994 const unsigned MemSizeInBits)
const {
3997 SmallSetVector<const MachineInstr *, 8> Loads;
4003 GZExtLoad *LowestIdxLoad =
nullptr;
4006 SmallSet<int64_t, 8> SeenIdx;
4010 MachineBasicBlock *
MBB =
nullptr;
4011 const MachineMemOperand *MMO =
nullptr;
4014 GZExtLoad *EarliestLoad =
nullptr;
4017 GZExtLoad *LatestLoad =
nullptr;
4026 for (
auto Reg : RegsToVisit) {
4031 return std::nullopt;
4034 std::tie(
Load, DstPos) = *LoadAndPos;
4038 MachineBasicBlock *LoadMBB =
Load->getParent();
4042 return std::nullopt;
4045 auto &LoadMMO =
Load->getMMO();
4049 return std::nullopt;
4056 LoadPtr =
Load->getOperand(1).getReg();
4061 if (!SeenIdx.
insert(Idx).second)
4062 return std::nullopt;
4069 if (BasePtr != LoadPtr)
4070 return std::nullopt;
4072 if (Idx < LowestIdx) {
4074 LowestIdxLoad =
Load;
4081 if (!MemOffset2Idx.
try_emplace(DstPos, Idx).second)
4082 return std::nullopt;
4091 EarliestLoad =
Load;
4099 "Expected to find a load for each register?");
4100 assert(EarliestLoad != LatestLoad && EarliestLoad &&
4101 LatestLoad &&
"Expected at least two loads?");
4110 const unsigned MaxIter = 20;
4116 if (
MI.isLoadFoldBarrier())
4117 return std::nullopt;
4118 if (Iter++ == MaxIter)
4119 return std::nullopt;
4122 return std::make_tuple(LowestIdxLoad, LowestIdx, LatestLoad);
4128 assert(
MI.getOpcode() == TargetOpcode::G_OR);
4141 LLT Ty =
MRI.getType(Dst);
4147 const unsigned WideMemSizeInBits = Ty.getSizeInBits();
4148 if (WideMemSizeInBits < 16 || WideMemSizeInBits % 8 != 0)
4152 auto RegsToVisit = findCandidatesForLoadOrCombine(&
MI);
4159 const unsigned NarrowMemSizeInBits = WideMemSizeInBits / RegsToVisit->size();
4160 if (NarrowMemSizeInBits % 8 != 0)
4173 auto MaybeLoadInfo = findLoadOffsetsForLoadOrCombine(
4174 MemOffset2Idx, *RegsToVisit, NarrowMemSizeInBits);
4177 std::tie(LowestIdxLoad, LowestIdx, LatestLoad) = *MaybeLoadInfo;
4184 std::optional<bool> IsBigEndian =
isBigEndian(MemOffset2Idx, LowestIdx);
4187 bool NeedsBSwap = IsBigEndianTarget != *IsBigEndian;
4199 const unsigned NumLoadsInTy = WideMemSizeInBits / NarrowMemSizeInBits;
4200 const unsigned ZeroByteOffset =
4204 auto ZeroOffsetIdx = MemOffset2Idx.
find(ZeroByteOffset);
4205 if (ZeroOffsetIdx == MemOffset2Idx.
end() ||
4206 ZeroOffsetIdx->second != LowestIdx)
4216 {TargetOpcode::G_LOAD, {Ty,
MRI.getType(Ptr)}, {MMDesc}}))
4230 MIB.setInstrAndDebugLoc(*LatestLoad);
4231 Register LoadDst = NeedsBSwap ?
MRI.cloneVirtualRegister(Dst) : Dst;
4232 MIB.buildLoad(LoadDst, Ptr, *NewMMO);
4234 MIB.buildBSwap(Dst, LoadDst);
4246 if (
MRI.getType(DstReg).isVector())
4250 if (!
MRI.hasOneNonDBGUse(DstReg))
4252 ExtMI = &*
MRI.use_instr_nodbg_begin(DstReg);
4254 case TargetOpcode::G_ANYEXT:
4256 case TargetOpcode::G_ZEXT:
4257 case TargetOpcode::G_SEXT:
4264 if (
Builder.getTII().isExtendLikelyToBeFolded(*ExtMI,
MRI))
4271 for (
unsigned I = 0;
I <
PHI.getNumIncomingValues(); ++
I) {
4273 switch (
DefMI->getOpcode()) {
4274 case TargetOpcode::G_LOAD:
4275 case TargetOpcode::G_TRUNC:
4276 case TargetOpcode::G_SEXT:
4277 case TargetOpcode::G_ZEXT:
4278 case TargetOpcode::G_ANYEXT:
4279 case TargetOpcode::G_CONSTANT:
4283 if (InSrcs.
size() > 2)
4297 LLT ExtTy =
MRI.getType(DstReg);
4304 for (
unsigned I = 0;
I <
PHI.getNumIncomingValues(); ++
I) {
4305 auto SrcReg =
PHI.getIncomingValue(
I);
4309 if (!SrcMIs.
insert(SrcMI))
4315 if (InsertPt !=
MBB->end() && InsertPt->isPHI())
4316 InsertPt =
MBB->getFirstNonPHI();
4321 OldToNewSrcMap[SrcMI] = NewExt;
4326 auto NewPhi =
Builder.buildInstrNoInsert(TargetOpcode::G_PHI);
4327 NewPhi.addDef(DstReg);
4330 NewPhi.addMBB(MO.getMBB());
4333 auto *NewSrc = OldToNewSrcMap[
MRI.getVRegDef(MO.getReg())];
4334 NewPhi.addUse(NewSrc->getOperand(0).getReg());
4342 assert(
MI.getOpcode() == TargetOpcode::G_EXTRACT_VECTOR_ELT);
4346 LLT SrcTy =
MRI.getType(SrcVec);
4347 if (SrcTy.isScalableVector())
4351 if (!Cst || Cst->Value.getZExtValue() >= SrcTy.getNumElements())
4354 unsigned VecIdx = Cst->Value.getZExtValue();
4366 if (SrcVecMI->
getOpcode() != TargetOpcode::G_BUILD_VECTOR &&
4367 SrcVecMI->
getOpcode() != TargetOpcode::G_BUILD_VECTOR_TRUNC)
4371 if (!
MRI.hasOneNonDBGUse(SrcVec) &&
4383 LLT ScalarTy =
MRI.getType(Reg);
4385 LLT DstTy =
MRI.getType(DstReg);
4387 if (ScalarTy != DstTy) {
4389 Builder.buildTrunc(DstReg, Reg);
4390 MI.eraseFromParent();
4398 SmallVectorImpl<std::pair<Register, MachineInstr *>> &SrcDstPairs)
const {
4399 assert(
MI.getOpcode() == TargetOpcode::G_BUILD_VECTOR);
4417 LLT DstTy =
MRI.getType(DstReg);
4422 if (
II.getOpcode() != TargetOpcode::G_EXTRACT_VECTOR_ELT)
4427 unsigned Idx = Cst->getZExtValue();
4430 ExtractedElts.
set(Idx);
4431 SrcDstPairs.emplace_back(
4432 std::make_pair(
MI.getOperand(Idx + 1).getReg(), &
II));
4435 return ExtractedElts.
all();
4440 SmallVectorImpl<std::pair<Register, MachineInstr *>> &SrcDstPairs)
const {
4441 assert(
MI.getOpcode() == TargetOpcode::G_BUILD_VECTOR);
4442 for (
auto &Pair : SrcDstPairs) {
4443 auto *ExtMI = Pair.second;
4445 ExtMI->eraseFromParent();
4447 MI.eraseFromParent();
4454 MI.eraseFromParent();
4464 bool AllowScalarConstants,
4466 assert(
MI.getOpcode() == TargetOpcode::G_OR);
4469 LLT Ty =
MRI.getType(Dst);
4470 unsigned BitWidth = Ty.getScalarSizeInBits();
4472 Register ShlSrc, ShlAmt, LShrSrc, LShrAmt, Amt;
4473 unsigned FshOpc = 0;
4484 int64_t CstShlAmt = 0, CstLShrAmt;
4487 CstShlAmt + CstLShrAmt ==
BitWidth) {
4488 FshOpc = TargetOpcode::G_FSHR;
4494 FshOpc = TargetOpcode::G_FSHL;
4499 FshOpc = TargetOpcode::G_FSHR;
4504 LLT AmtTy =
MRI.getType(Amt);
4506 (!AllowScalarConstants || CstShlAmt == 0 || !Ty.isScalar()))
4510 B.buildInstr(FshOpc, {Dst}, {ShlSrc, LShrSrc, Amt});
4517 assert(
MI.getOpcode() == TargetOpcode::G_ROTL ||
4518 MI.getOpcode() == TargetOpcode::G_ROTR);
4520 MRI.getType(
MI.getOperand(0).getReg()).getScalarSizeInBits();
4522 bool OutOfRange =
false;
4523 auto MatchOutOfRange = [Bitsize, &OutOfRange](
const Constant *
C) {
4525 OutOfRange |= CI->getValue().uge(Bitsize);
4532 assert(
MI.getOpcode() == TargetOpcode::G_ROTL ||
4533 MI.getOpcode() == TargetOpcode::G_ROTR);
4535 MRI.getType(
MI.getOperand(0).getReg()).getScalarSizeInBits();
4537 LLT AmtTy =
MRI.getType(Amt);
4538 auto Bits =
Builder.buildConstant(AmtTy, Bitsize);
4539 Amt =
Builder.buildURem(AmtTy,
MI.getOperand(2).getReg(), Bits).getReg(0);
4541 MI.getOperand(2).setReg(Amt);
4546 int64_t &MatchInfo)
const {
4547 assert(
MI.getOpcode() == TargetOpcode::G_ICMP);
4558 auto KnownRHS =
VT->getKnownBits(
MI.getOperand(3).getReg());
4559 if (KnownRHS.isUnknown())
4562 std::optional<bool> KnownVal;
4563 if (KnownRHS.isZero()) {
4573 auto KnownLHS =
VT->getKnownBits(
MI.getOperand(2).getReg());
4583 MRI.getType(
MI.getOperand(0).getReg()).isVector(),
4592 assert(
MI.getOpcode() == TargetOpcode::G_ICMP);
4608 LLT DstTy =
MRI.getType(Dst);
4616 auto KnownLHS =
VT->getKnownBits(LHS);
4617 if (KnownLHS.getMinValue() != 0 || KnownLHS.getMaxValue() != 1)
4620 LLT LHSTy =
MRI.getType(LHS);
4623 unsigned Op = TargetOpcode::COPY;
4624 if (DstSize != LHSSize)
4625 Op = DstSize < LHSSize ? TargetOpcode::G_TRUNC : TargetOpcode::G_ZEXT;
4636 assert(
MI.getOpcode() == TargetOpcode::G_AND);
4640 LLT Ty =
MRI.getType(
MI.getOperand(0).getReg());
4646 int64_t AndMaskBits;
4654 if (AndMaskBits & OrMaskBits)
4660 if (
MI.getOperand(1).getReg() == AndMaskReg)
4661 MI.getOperand(2).setReg(AndMaskReg);
4662 MI.getOperand(1).setReg(Src);
4672 assert(
MI.getOpcode() == TargetOpcode::G_SEXT_INREG);
4675 LLT Ty =
MRI.getType(Src);
4677 if (!
LI || !
LI->isLegalOrCustom({TargetOpcode::G_SBFX, {Ty, ExtractTy}}))
4679 int64_t Width =
MI.getOperand(2).getImm();
4687 if (ShiftImm < 0 || ShiftImm + Width > Ty.getScalarSizeInBits())
4691 auto Cst1 =
B.buildConstant(ExtractTy, ShiftImm);
4692 auto Cst2 =
B.buildConstant(ExtractTy, Width);
4693 B.buildSbfx(Dst, ShiftSrc, Cst1, Cst2);
4703 LLT Ty =
MRI.getType(Dst);
4707 if (
LI && !
LI->isLegalOrCustom({TargetOpcode::G_UBFX, {Ty, ExtractTy}}))
4710 int64_t AndImm, LSBImm;
4712 const unsigned Size = Ty.getScalarSizeInBits();
4720 uint64_t MaybeMask =
static_cast<uint64_t
>(AndImm);
4725 if (MaybeMask & (MaybeMask + 1))
4729 if (
static_cast<uint64_t
>(LSBImm) >=
Size)
4737 if (
static_cast<uint64_t
>(LSBImm) + Width >
Size)
4741 auto WidthCst =
B.buildConstant(ExtractTy, Width);
4742 auto LSBCst =
B.buildConstant(ExtractTy, LSBImm);
4743 B.buildInstr(TargetOpcode::G_UBFX, {Dst}, {ShiftSrc, LSBCst, WidthCst});
4751 const unsigned Opcode =
MI.getOpcode();
4752 assert(Opcode == TargetOpcode::G_ASHR || Opcode == TargetOpcode::G_LSHR);
4754 const Register Dst =
MI.getOperand(0).getReg();
4756 const unsigned ExtrOpcode = Opcode == TargetOpcode::G_ASHR
4757 ? TargetOpcode::G_SBFX
4758 : TargetOpcode::G_UBFX;
4761 LLT Ty =
MRI.getType(Dst);
4763 if (!
LI || !
LI->isLegalOrCustom({ExtrOpcode, {Ty, ExtractTy}}))
4769 const unsigned Size = Ty.getScalarSizeInBits();
4779 if (ShlAmt < 0 || ShlAmt > ShrAmt || ShrAmt >=
Size)
4783 if (Opcode == TargetOpcode::G_ASHR && ShlAmt == ShrAmt)
4787 const int64_t Pos = ShrAmt - ShlAmt;
4788 const int64_t Width =
Size - ShrAmt;
4791 auto WidthCst =
B.buildConstant(ExtractTy, Width);
4792 auto PosCst =
B.buildConstant(ExtractTy, Pos);
4793 B.buildInstr(ExtrOpcode, {Dst}, {ShlSrc, PosCst, WidthCst});
4801 const unsigned Opcode =
MI.getOpcode();
4802 assert(Opcode == TargetOpcode::G_LSHR || Opcode == TargetOpcode::G_ASHR);
4804 const Register Dst =
MI.getOperand(0).getReg();
4805 LLT Ty =
MRI.getType(Dst);
4807 if (
LI && !
LI->isLegalOrCustom({TargetOpcode::G_UBFX, {Ty, ExtractTy}}))
4820 const unsigned Size = Ty.getScalarSizeInBits();
4821 if (ShrAmt < 0 || ShrAmt >=
Size)
4825 if (0 == (SMask >> ShrAmt)) {
4827 B.buildConstant(Dst, 0);
4840 const int64_t Pos = ShrAmt;
4845 if (Opcode == TargetOpcode::G_ASHR && Width + ShrAmt ==
Size)
4849 auto WidthCst =
B.buildConstant(ExtractTy, Width);
4850 auto PosCst =
B.buildConstant(ExtractTy, Pos);
4851 B.buildInstr(TargetOpcode::G_UBFX, {Dst}, {AndSrc, PosCst, WidthCst});
4856bool CombinerHelper::reassociationCanBreakAddressingModePattern(
4860 Register Src1Reg = PtrAdd.getBaseReg();
4865 Register Src2Reg = PtrAdd.getOffsetReg();
4867 if (
MRI.hasOneNonDBGUse(Src1Reg))
4877 const APInt &C1APIntVal = *C1;
4878 const APInt &C2APIntVal = *C2;
4879 const int64_t CombinedValue = (C1APIntVal + C2APIntVal).getSExtValue();
4881 for (
auto &
UseMI :
MRI.use_nodbg_instructions(PtrAdd.getReg(0))) {
4884 MachineInstr *ConvUseMI = &
UseMI;
4885 unsigned ConvUseOpc = ConvUseMI->
getOpcode();
4886 while (ConvUseOpc == TargetOpcode::G_INTTOPTR ||
4887 ConvUseOpc == TargetOpcode::G_PTRTOINT) {
4889 if (!
MRI.hasOneNonDBGUse(DefReg))
4891 ConvUseMI = &*
MRI.use_instr_nodbg_begin(DefReg);
4900 TargetLoweringBase::AddrMode AM;
4903 unsigned AS =
MRI.getType(LdStMI->getPointerReg()).getAddressSpace();
4905 PtrAdd.getMF()->getFunction().getContext());
4906 const auto &TLI = *PtrAdd.getMF()->getSubtarget().getTargetLowering();
4907 if (!TLI.isLegalAddressingMode(PtrAdd.getMF()->getDataLayout(), AM,
4913 if (!TLI.isLegalAddressingMode(PtrAdd.getMF()->getDataLayout(), AM,
4925 Register Src1Reg =
MI.getOperand(1).getReg();
4926 if (RHS->getOpcode() != TargetOpcode::G_ADD)
4938 unsigned PtrAddFlags =
MI.getFlags();
4939 unsigned AddFlags = RHS->getFlags();
4952 LLT PtrTy =
MRI.getType(
MI.getOperand(0).getReg());
4955 Builder.buildPtrAdd(PtrTy, Src1Reg, RHS->getOperand(1).getReg(), Flags);
4957 MI.getOperand(1).setReg(NewBase.getReg(0));
4958 MI.getOperand(2).setReg(RHS->getOperand(2).getReg());
4962 return !reassociationCanBreakAddressingModePattern(
MI);
4972 std::optional<ValueAndVReg> LHSCstOff;
4982 unsigned PtrAddFlags =
MI.getFlags();
4983 unsigned LHSPtrAddFlags = LHSPtrAdd->getFlags();
4985 bool IsNoUSWrap = IsNoUWrap && (PtrAddFlags & LHSPtrAddFlags &
4987 bool IsInBounds = IsNoUWrap && (PtrAddFlags & LHSPtrAddFlags &
5001 LHSPtrAdd->moveBefore(&
MI);
5004 auto NewCst =
B.buildConstant(
MRI.getType(RHSReg), LHSCstOff->Value);
5006 MI.getOperand(2).setReg(NewCst.getReg(0));
5009 Observer.changingInstr(*LHSPtrAdd);
5010 LHSPtrAdd->getOperand(2).setReg(RHSReg);
5011 LHSPtrAdd->setFlags(Flags);
5014 return !reassociationCanBreakAddressingModePattern(
MI);
5025 Register Src2Reg =
MI.getOperand(2).getReg();
5026 Register LHSSrc1 = LHSPtrAdd->getBaseReg();
5027 Register LHSSrc2 = LHSPtrAdd->getOffsetReg();
5040 unsigned PtrAddFlags =
MI.getFlags();
5041 unsigned LHSPtrAddFlags = LHSPtrAdd->getFlags();
5054 auto NewCst =
B.buildConstant(
MRI.getType(Src2Reg), *C1 + *C2);
5056 MI.getOperand(1).setReg(LHSSrc1);
5057 MI.getOperand(2).setReg(NewCst.getReg(0));
5061 return !reassociationCanBreakAddressingModePattern(
MI);
5101 LLT OpRHSTy =
MRI.getType(OpRHS);
5118 auto NewCst =
B.buildInstr(
Opc, {OpRHSTy}, {OpLHSRHS, OpRHS});
5119 B.buildInstr(
Opc, {DstReg}, {OpLHSLHS, NewCst});
5127 auto NewLHSLHS =
B.buildInstr(
Opc, {OpRHSTy}, {OpLHSLHS, OpRHS});
5128 B.buildInstr(
Opc, {DstReg}, {NewLHSLHS, OpLHSRHS});
5141 unsigned Opc =
MI.getOpcode();
5154 APInt &MatchInfo)
const {
5155 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
5159 MatchInfo = *MaybeCst;
5170 MI.getOperand(1).getReg(),
MRI);
5175 if (Csts.size() == 1)
5176 B.buildConstant(Dst, Csts[0]);
5178 B.buildBuildVectorConstant(Dst, Csts);
5184 APInt &MatchInfo)
const {
5190 MatchInfo = *MaybeCst;
5202 ConstantFP::get(
MI.getMF()->getFunction().getContext(), *MaybeCst);
5208 assert(
MI.getOpcode() == TargetOpcode::G_FMA ||
5209 MI.getOpcode() == TargetOpcode::G_FMAD);
5210 auto [
_, Op1, Op2, Op3] =
MI.getFirst4Regs();
5227 MatchInfo = ConstantFP::get(
MI.getMF()->getFunction().getContext(), Op1F);
5250 assert(
MI.getOpcode() == TargetOpcode::G_AND);
5254 LLT WideTy =
MRI.getType(Dst);
5258 if (!WideTy.
isScalar() || !
MRI.hasOneNonDBGUse(AndLHS))
5274 case TargetOpcode::G_ADD:
5275 case TargetOpcode::G_SUB:
5276 case TargetOpcode::G_MUL:
5277 case TargetOpcode::G_AND:
5278 case TargetOpcode::G_OR:
5279 case TargetOpcode::G_XOR:
5287 auto Mask = Cst->Value;
5292 unsigned NarrowWidth = Mask.countr_one();
5298 auto &MF = *
MI.getMF();
5301 if (!TLI.isTruncateFree(WideTy, NarrowTy, Ctx) ||
5302 !TLI.isZExtFree(NarrowTy, WideTy, Ctx))
5310 auto NarrowLHS =
Builder.buildTrunc(NarrowTy, BinOpLHS);
5311 auto NarrowRHS =
Builder.buildTrunc(NarrowTy, BinOpRHS);
5313 Builder.buildInstr(LHSOpc, {NarrowTy}, {NarrowLHS, NarrowRHS});
5314 auto Ext =
Builder.buildZExt(WideTy, NarrowBinOp);
5316 MI.getOperand(1).setReg(Ext.getReg(0));
5325 assert(
MI.getOpcode() == TargetOpcode::G_UMULO ||
5326 MI.getOpcode() == TargetOpcode::G_SMULO);
5335 B.buildConstant(Dst, 0);
5336 B.buildConstant(Carry, 0);
5344 if (SrcA.
isValid() && SrcA == SrcB)
5352 unsigned Opcode =
MI.getOpcode();
5353 assert(Opcode == TargetOpcode::G_UDIV || Opcode == TargetOpcode::G_UREM);
5355 Register Dst = UDivorRem.getReg(0);
5356 Register LHS = UDivorRem.getReg(1);
5357 Register RHS = UDivorRem.getReg(2);
5358 LLT Ty =
MRI.getType(Dst);
5366 bool UseSRL =
false;
5371 auto BuildExactUDIVPattern = [&](
const Constant *
C) {
5373 if (IsSplat && !Factors.
empty()) {
5380 APInt Divisor = CI->getValue();
5389 Shifts.
push_back(MIB.buildConstant(ScalarShiftAmtTy, Shift).getReg(0));
5390 Factors.
push_back(MIB.buildConstant(ScalarTy, Factor).getReg(0));
5400 if (Ty.isVector()) {
5401 Shift = MIB.buildBuildVector(ShiftAmtTy, Shifts).getReg(0);
5402 Factor = MIB.buildBuildVector(Ty, Factors).getReg(0);
5405 Factor = Factors[0];
5413 return MIB.buildMul(Ty, Res, Factor);
5416 unsigned KnownLeadingZeros =
5417 VT ?
VT->getKnownBits(LHS).countMinLeadingZeros() : 0;
5419 bool UseNPQ =
false;
5421 auto BuildUDIVPattern = [&](
const Constant *
C) {
5423 const APInt &Divisor = CI->getValue();
5425 bool SelNPQ =
false;
5427 unsigned PreShift = 0, PostShift = 0;
5432 if (!Divisor.
isOne()) {
5438 Divisor, std::min(KnownLeadingZeros, Divisor.
countl_zero()));
5440 Magic = std::move(magics.
Magic);
5443 "We shouldn't generate an undefined shift!");
5445 "We shouldn't generate an undefined shift!");
5449 SelNPQ = magics.
IsAdd;
5453 MIB.buildConstant(ScalarShiftAmtTy, PreShift).getReg(0));
5454 MagicFactors.
push_back(MIB.buildConstant(ScalarTy, Magic).getReg(0));
5456 MIB.buildConstant(ScalarTy,
5461 MIB.buildConstant(ScalarShiftAmtTy, PostShift).getReg(0));
5469 assert(Matched &&
"Expected unary predicate match to succeed");
5471 Register PreShift, PostShift, MagicFactor, NPQFactor;
5474 PreShift = MIB.buildBuildVector(ShiftAmtTy, PreShifts).getReg(0);
5475 MagicFactor = MIB.buildBuildVector(Ty, MagicFactors).getReg(0);
5476 NPQFactor = MIB.buildBuildVector(Ty, NPQFactors).getReg(0);
5477 PostShift = MIB.buildBuildVector(ShiftAmtTy, PostShifts).getReg(0);
5480 "Non-build_vector operation should have been a scalar");
5481 PreShift = PreShifts[0];
5482 MagicFactor = MagicFactors[0];
5483 PostShift = PostShifts[0];
5487 Q = MIB.buildLShr(Ty, Q, PreShift).getReg(0);
5490 Q = MIB.buildUMulH(Ty, Q, MagicFactor).getReg(0);
5493 Register NPQ = MIB.buildSub(Ty, LHS, Q).getReg(0);
5498 NPQ = MIB.buildUMulH(Ty, NPQ, NPQFactor).getReg(0);
5500 NPQ = MIB.buildLShr(Ty, NPQ, MIB.buildConstant(ShiftAmtTy, 1)).getReg(0);
5502 Q = MIB.buildAdd(Ty, NPQ, Q).getReg(0);
5505 Q = MIB.buildLShr(Ty, Q, PostShift).getReg(0);
5506 auto One = MIB.buildConstant(Ty, 1);
5507 auto IsOne = MIB.buildICmp(
5511 auto ret = MIB.buildSelect(Ty, IsOne, LHS, Q);
5513 if (Opcode == TargetOpcode::G_UREM) {
5514 auto Prod = MIB.buildMul(Ty, ret, RHS);
5515 return MIB.buildSub(Ty, LHS, Prod);
5521 unsigned Opcode =
MI.getOpcode();
5522 assert(Opcode == TargetOpcode::G_UDIV || Opcode == TargetOpcode::G_UREM);
5525 LLT DstTy =
MRI.getType(Dst);
5527 auto &MF = *
MI.getMF();
5537 if (MF.getFunction().hasMinSize())
5540 if (Opcode == TargetOpcode::G_UDIV &&
5543 MRI, RHS, [](
const Constant *
C) {
return C && !
C->isNullValue(); });
5558 {TargetOpcode::G_ICMP,
5562 if (Opcode == TargetOpcode::G_UREM &&
5568 MRI, RHS, [](
const Constant *
C) {
return C && !
C->isNullValue(); });
5577 unsigned Opcode =
MI.getOpcode();
5578 assert(Opcode == TargetOpcode::G_SDIV || Opcode == TargetOpcode::G_SREM);
5581 LLT DstTy =
MRI.getType(Dst);
5585 auto &MF = *
MI.getMF();
5595 if (MF.getFunction().hasMinSize())
5599 if (Opcode == TargetOpcode::G_SDIV &&
5602 MRI, RHS, [](
const Constant *
C) {
return C && !
C->isNullValue(); });
5614 if (!
isLegal({TargetOpcode::G_SMULH, {DstTy}}) &&
5617 if (Opcode == TargetOpcode::G_SREM &&
5623 MRI, RHS, [](
const Constant *
C) {
return C && !
C->isNullValue(); });
5632 unsigned Opcode =
MI.getOpcode();
5633 assert(
MI.getOpcode() == TargetOpcode::G_SDIV ||
5634 Opcode == TargetOpcode::G_SREM);
5636 Register Dst = SDivorRem.getReg(0);
5637 Register LHS = SDivorRem.getReg(1);
5638 Register RHS = SDivorRem.getReg(2);
5639 LLT Ty =
MRI.getType(Dst);
5646 bool UseSRA =
false;
5652 auto BuildExactSDIVPattern = [&](
const Constant *
C) {
5654 if (IsSplat && !ExactFactors.
empty()) {
5656 ExactFactors.
push_back(ExactFactors[0]);
5661 APInt Divisor = CI->getValue();
5671 ExactShifts.
push_back(MIB.buildConstant(ScalarShiftAmtTy, Shift).getReg(0));
5672 ExactFactors.
push_back(MIB.buildConstant(ScalarTy, Factor).getReg(0));
5680 assert(Matched &&
"Expected unary predicate match to succeed");
5683 if (Ty.isVector()) {
5684 Shift = MIB.buildBuildVector(ShiftAmtTy, ExactShifts).getReg(0);
5685 Factor = MIB.buildBuildVector(Ty, ExactFactors).getReg(0);
5687 Shift = ExactShifts[0];
5688 Factor = ExactFactors[0];
5696 return MIB.buildMul(Ty, Res, Factor);
5701 auto BuildSDIVPattern = [&](
const Constant *
C) {
5703 const APInt &Divisor = CI->getValue();
5707 int NumeratorFactor = 0;
5718 NumeratorFactor = 1;
5721 NumeratorFactor = -1;
5724 MagicFactors.
push_back(MIB.buildConstant(ScalarTy, Magics.
Magic).getReg(0));
5725 Factors.
push_back(MIB.buildConstant(ScalarTy, NumeratorFactor).getReg(0));
5727 MIB.buildConstant(ScalarShiftAmtTy, Magics.
ShiftAmount).getReg(0));
5728 ShiftMasks.
push_back(MIB.buildConstant(ScalarTy, ShiftMask).getReg(0));
5736 assert(Matched &&
"Expected unary predicate match to succeed");
5738 Register MagicFactor, Factor, Shift, ShiftMask;
5741 MagicFactor = MIB.buildBuildVector(Ty, MagicFactors).getReg(0);
5742 Factor = MIB.buildBuildVector(Ty, Factors).getReg(0);
5743 Shift = MIB.buildBuildVector(ShiftAmtTy, Shifts).getReg(0);
5744 ShiftMask = MIB.buildBuildVector(Ty, ShiftMasks).getReg(0);
5747 "Non-build_vector operation should have been a scalar");
5748 MagicFactor = MagicFactors[0];
5749 Factor = Factors[0];
5751 ShiftMask = ShiftMasks[0];
5755 Q = MIB.buildSMulH(Ty, LHS, MagicFactor).getReg(0);
5758 Factor = MIB.buildMul(Ty, LHS, Factor).getReg(0);
5759 Q = MIB.buildAdd(Ty, Q, Factor).getReg(0);
5762 Q = MIB.buildAShr(Ty, Q, Shift).getReg(0);
5765 auto SignShift = MIB.buildConstant(ShiftAmtTy, EltBits - 1);
5766 auto T = MIB.buildLShr(Ty, Q, SignShift);
5767 T = MIB.buildAnd(Ty,
T, ShiftMask);
5768 auto ret = MIB.buildAdd(Ty, Q,
T);
5770 if (Opcode == TargetOpcode::G_SREM) {
5771 auto Prod = MIB.buildMul(Ty, ret, RHS);
5772 return MIB.buildSub(Ty, LHS, Prod);
5778 assert((
MI.getOpcode() == TargetOpcode::G_SDIV ||
5779 MI.getOpcode() == TargetOpcode::G_UDIV) &&
5780 "Expected SDIV or UDIV");
5783 auto MatchPow2 = [&](
const Constant *
C) {
5785 return CI && (CI->getValue().isPowerOf2() ||
5786 (IsSigned && CI->getValue().isNegatedPowerOf2()));
5792 assert(
MI.getOpcode() == TargetOpcode::G_SDIV &&
"Expected SDIV");
5797 LLT Ty =
MRI.getType(Dst);
5817 unsigned BitWidth = Ty.getScalarSizeInBits();
5818 auto Zero =
Builder.buildConstant(Ty, 0);
5821 auto C1 =
Builder.buildCTTZ(ShiftAmtTy, RHS);
5822 auto Inexact =
Builder.buildSub(ShiftAmtTy, Bits, C1);
5824 auto Sign =
Builder.buildAShr(
5828 auto LSrl =
Builder.buildLShr(Ty, Sign, Inexact);
5834 auto One =
Builder.buildConstant(Ty, 1);
5835 auto MinusOne =
Builder.buildConstant(Ty, -1);
5839 auto IsOneOrMinusOne =
Builder.buildOr(CCVT, IsOne, IsMinusOne);
5840 AShr =
Builder.buildSelect(Ty, IsOneOrMinusOne, LHS, AShr);
5844 auto Neg =
Builder.buildNeg(Ty, AShr);
5846 Builder.buildSelect(
MI.getOperand(0).getReg(), IsNeg, Neg, AShr);
5847 MI.eraseFromParent();
5851 assert(
MI.getOpcode() == TargetOpcode::G_UDIV &&
"Expected UDIV");
5856 LLT Ty =
MRI.getType(Dst);
5859 auto C1 =
Builder.buildCTTZ(ShiftAmtTy, RHS);
5860 Builder.buildLShr(
MI.getOperand(0).getReg(), LHS, C1);
5861 MI.eraseFromParent();
5865 assert(
MI.getOpcode() == TargetOpcode::G_SREM &&
"Expected SREM");
5870 LLT Ty =
MRI.getType(Dst);
5889 unsigned BitWidth = Ty.getScalarSizeInBits();
5890 auto AbsRHS =
Builder.buildAbs(Ty, RHS);
5891 auto Mask =
Builder.buildSub(Ty, AbsRHS,
Builder.buildConstant(Ty, 1));
5893 auto Sign =
Builder.buildAShr(Ty, LHS, BWMinusOne);
5894 auto Bias =
Builder.buildAnd(Ty, Sign, Mask);
5895 auto Biased =
Builder.buildAdd(Ty, LHS, Bias);
5898 MI.eraseFromParent();
5902 assert(
MI.getOpcode() == TargetOpcode::G_UMULH);
5905 LLT Ty =
MRI.getType(Dst);
5906 LLT RHSTy =
MRI.getType(RHS);
5908 auto MatchPow2ExceptOne = [&](
const Constant *
C) {
5910 return CI->getValue().isPowerOf2() && !CI->getValue().isOne();
5925 LLT Ty =
MRI.getType(Dst);
5931 Builder.buildSub(Ty,
Builder.buildConstant(Ty, NumEltBits), LogBase2);
5932 auto Trunc =
Builder.buildZExtOrTrunc(ShiftAmtTy, ShiftAmt);
5933 Builder.buildLShr(Dst, LHS, Trunc);
5934 MI.eraseFromParent();
5941 LLT DstTy =
MRI.getType(Dst);
5942 LLT SrcTy =
MRI.getType(Src);
5944 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
5945 assert(NumSrcBits > NumDstBits &&
"Unexpected types for truncate operation");
5948 {TargetOpcode::G_TRUNC_SSAT_S, {DstTy, SrcTy}}))
5977 Builder.buildTruncSSatS(Dst, MatchInfo);
5978 MI.eraseFromParent();
5985 LLT DstTy =
MRI.getType(Dst);
5986 LLT SrcTy =
MRI.getType(Src);
5988 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
5989 assert(NumSrcBits > NumDstBits &&
"Unexpected types for truncate operation");
5992 {TargetOpcode::G_TRUNC_SSAT_U, {DstTy, SrcTy}}))
6010 Builder.buildTruncSSatU(Dst, MatchInfo);
6011 MI.eraseFromParent();
6018 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
6019 LLT SrcTy =
MRI.getType(Val);
6021 unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
6022 assert(NumSrcBits > NumDstBits &&
"Unexpected types for truncate operation");
6025 {TargetOpcode::G_TRUNC_SSAT_U, {DstTy, SrcTy}}))
6034 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
6042 assert(
MI.getOpcode() == TargetOpcode::G_FSUB);
6045 LLT Ty =
MRI.getType(
MI.getOperand(0).getReg());
6047 const auto LHSCst = Ty.isVector()
6061 if (LHSCst->Value.isNegZero())
6065 if (LHSCst->Value.isPosZero())
6074 if (
MI.getOpcode() != TargetOpcode::G_FMUL)
6088 bool &AllowFusionGlobally,
6090 bool CanReassociate)
const {
6092 auto *MF =
MI.getMF();
6093 const auto &TLI = *MF->getSubtarget().getTargetLowering();
6094 LLT DstType =
MRI.getType(
MI.getOperand(0).getReg());
6102 bool HasFMA = TLI.isFMAFasterThanFMulAndFAdd(*MF, DstType) &&
6105 if (!HasFMAD && !HasFMA)
6110 AllowFusionGlobally = HasFMAD;
6115 Aggressive = TLI.enableAggressiveFMAFusion(DstType);
6122 assert(
MI.getOpcode() == TargetOpcode::G_FADD);
6124 bool AllowFusionGlobally, HasFMAD,
Aggressive;
6136 unsigned PreferredFusedOpcode =
6137 HasFMAD ? TargetOpcode::G_FMAD : TargetOpcode::G_FMA;
6150 unsigned Flags =
MI.getFlags() & LHS.MI->getFlags();
6152 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6153 {LHS.MI->getOperand(1).getReg(),
6154 LHS.MI->getOperand(2).getReg(), RHS.Reg},
6163 unsigned Flags =
MI.getFlags() & RHS.MI->getFlags();
6165 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6166 {RHS.MI->getOperand(1).getReg(),
6167 RHS.MI->getOperand(2).getReg(), LHS.Reg},
6179 assert(
MI.getOpcode() == TargetOpcode::G_FADD);
6181 bool AllowFusionGlobally, HasFMAD,
Aggressive;
6185 const auto &TLI = *
MI.getMF()->getSubtarget().getTargetLowering();
6194 LLT DstType =
MRI.getType(
MI.getOperand(0).getReg());
6196 unsigned PreferredFusedOpcode =
6197 HasFMAD ? TargetOpcode::G_FMAD : TargetOpcode::G_FMA;
6200 bool LHSContractable =
6203 TLI.isFPExtFoldable(
MI, PreferredFusedOpcode, DstType,
6206 bool RHSContractable =
6209 TLI.isFPExtFoldable(
MI, PreferredFusedOpcode, DstType,
6213 if (LHSContractable || RHSContractable) {
6216 if (!LHSContractable ||
6217 (RHSContractable &&
hasMoreUses(*LHSFpExtSrc, *RHSFpExtSrc,
MRI))) {
6219 LHSFpExtSrc = RHSFpExtSrc;
6222 unsigned Flags =
MI.getFlags() & LHSFpExtSrc->
getFlags();
6226 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6227 {FpExtX.getReg(0), FpExtY.getReg(0), RHS.Reg}, Flags);
6238 assert(
MI.getOpcode() == TargetOpcode::G_FADD);
6240 bool AllowFusionGlobally, HasFMAD,
Aggressive;
6252 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
6254 unsigned PreferredFusedOpcode =
6255 HasFMAD ? TargetOpcode::G_FMAD : TargetOpcode::G_FMA;
6260 if (LHS.MI->getOpcode() == PreferredFusedOpcode &&
6263 MRI.hasOneNonDBGUse(LHS.MI->getOperand(0).getReg()) &&
6264 MRI.hasOneNonDBGUse(LHS.MI->getOperand(3).getReg())) {
6269 else if (RHS.MI->getOpcode() == PreferredFusedOpcode &&
6272 MRI.hasOneNonDBGUse(RHS.MI->getOperand(0).getReg()) &&
6273 MRI.hasOneNonDBGUse(RHS.MI->getOperand(3).getReg())) {
6282 Register X = FMA->getOperand(1).getReg();
6283 Register Y = FMA->getOperand(2).getReg();
6286 unsigned InnerFlags =
MI.getFlags() & FMulMI->
getFlags();
6287 unsigned OuterFlags =
MI.getFlags() & FMA->getFlags();
6290 Register InnerFMA =
MRI.createGenericVirtualRegister(DstTy);
6291 B.buildInstr(PreferredFusedOpcode, {InnerFMA}, {U, V, Z}, InnerFlags);
6292 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6293 {
X,
Y, InnerFMA}, OuterFlags);
6304 assert(
MI.getOpcode() == TargetOpcode::G_FADD);
6306 bool AllowFusionGlobally, HasFMAD,
Aggressive;
6313 const auto &TLI = *
MI.getMF()->getSubtarget().getTargetLowering();
6314 LLT DstType =
MRI.getType(
MI.getOperand(0).getReg());
6324 unsigned PreferredFusedOpcode =
6325 HasFMAD ? TargetOpcode::G_FMAD : TargetOpcode::G_FMA;
6339 Register FpExtU =
B.buildFPExt(DstType, U).getReg(0);
6340 Register FpExtV =
B.buildFPExt(DstType, V).getReg(0);
6341 Register InnerFMA =
B.buildInstr(PreferredFusedOpcode, {DstType},
6342 {FpExtU, FpExtV, Z}, InnerFlags)
6344 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6345 {
X,
Y, InnerFMA}, OuterFlags);
6351 if (LHS.MI->getOpcode() == PreferredFusedOpcode &&
6355 TLI.isFPExtFoldable(
MI, PreferredFusedOpcode, DstType,
6357 unsigned InnerFlags =
MI.getFlags() & FMulMI->
getFlags();
6358 unsigned OuterFlags =
MI.getFlags() & LHS.MI->getFlags();
6362 LHS.MI->getOperand(1).getReg(),
6363 LHS.MI->getOperand(2).getReg(), InnerFlags, OuterFlags,
B);
6374 FMAMI->
getOpcode() == PreferredFusedOpcode) {
6379 TLI.isFPExtFoldable(
MI, PreferredFusedOpcode, DstType,
6381 unsigned InnerFlags =
MI.getFlags() & FMulMI->
getFlags();
6382 unsigned OuterFlags =
MI.getFlags() & FMAMI->
getFlags();
6386 X =
B.buildFPExt(DstType,
X).getReg(0);
6387 Y =
B.buildFPExt(DstType,
Y).getReg(0);
6390 InnerFlags, OuterFlags,
B);
6399 if (RHS.MI->getOpcode() == PreferredFusedOpcode &&
6403 TLI.isFPExtFoldable(
MI, PreferredFusedOpcode, DstType,
6405 unsigned InnerFlags =
MI.getFlags() & FMulMI->
getFlags();
6406 unsigned OuterFlags =
MI.getFlags() & RHS.MI->getFlags();
6410 RHS.MI->getOperand(1).getReg(),
6411 RHS.MI->getOperand(2).getReg(), InnerFlags, OuterFlags,
B);
6422 FMAMI->
getOpcode() == PreferredFusedOpcode) {
6427 TLI.isFPExtFoldable(
MI, PreferredFusedOpcode, DstType,
6429 unsigned InnerFlags =
MI.getFlags() & FMulMI->
getFlags();
6430 unsigned OuterFlags =
MI.getFlags() & FMAMI->
getFlags();
6434 X =
B.buildFPExt(DstType,
X).getReg(0);
6435 Y =
B.buildFPExt(DstType,
Y).getReg(0);
6438 InnerFlags, OuterFlags,
B);
6450 assert(
MI.getOpcode() == TargetOpcode::G_FSUB);
6452 bool AllowFusionGlobally, HasFMAD,
Aggressive;
6464 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
6468 int FirstMulHasFewerUses =
true;
6472 FirstMulHasFewerUses =
false;
6474 unsigned PreferredFusedOpcode =
6475 HasFMAD ? TargetOpcode::G_FMAD : TargetOpcode::G_FMA;
6478 if (FirstMulHasFewerUses &&
6481 unsigned Flags =
MI.getFlags() & LHS.MI->getFlags();
6483 Register NegZ =
B.buildFNeg(DstTy, RHS.Reg).getReg(0);
6484 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6485 {LHS.MI->getOperand(1).getReg(),
6486 LHS.MI->getOperand(2).getReg(), NegZ},
6494 unsigned Flags =
MI.getFlags() & RHS.MI->getFlags();
6497 B.buildFNeg(DstTy, RHS.MI->getOperand(1).getReg()).getReg(0);
6498 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6499 {NegY, RHS.MI->getOperand(2).getReg(), LHS.Reg}, Flags);
6510 assert(
MI.getOpcode() == TargetOpcode::G_FSUB);
6512 bool AllowFusionGlobally, HasFMAD,
Aggressive;
6518 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
6520 unsigned PreferredFusedOpcode =
6521 HasFMAD ? TargetOpcode::G_FMAD : TargetOpcode::G_FMA;
6529 unsigned Flags =
MI.getFlags() & FMulMI->
getFlags();
6533 Register NegZ =
B.buildFNeg(DstTy, RHSReg).getReg(0);
6534 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6545 unsigned Flags =
MI.getFlags() & FMulMI->
getFlags();
6547 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6561 assert(
MI.getOpcode() == TargetOpcode::G_FSUB);
6563 bool AllowFusionGlobally, HasFMAD,
Aggressive;
6569 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
6571 unsigned PreferredFusedOpcode =
6572 HasFMAD ? TargetOpcode::G_FMAD : TargetOpcode::G_FMA;
6579 unsigned Flags =
MI.getFlags() & FMulMI->
getFlags();
6585 Register NegZ =
B.buildFNeg(DstTy, RHSReg).getReg(0);
6586 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6587 {FpExtX, FpExtY, NegZ}, Flags);
6596 unsigned Flags =
MI.getFlags() & FMulMI->
getFlags();
6600 Register NegY =
B.buildFNeg(DstTy, FpExtY).getReg(0);
6603 B.buildInstr(PreferredFusedOpcode, {
MI.getOperand(0).getReg()},
6604 {NegY, FpExtZ, LHSReg}, Flags);
6615 assert(
MI.getOpcode() == TargetOpcode::G_FSUB);
6617 bool AllowFusionGlobally, HasFMAD,
Aggressive;
6621 const auto &TLI = *
MI.getMF()->getSubtarget().getTargetLowering();
6622 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
6626 unsigned PreferredFusedOpcode =
6627 HasFMAD ? TargetOpcode::G_FMAD : TargetOpcode::G_FMA;
6631 Register FpExtX =
B.buildFPExt(DstTy,
X).getReg(0);
6632 Register FpExtY =
B.buildFPExt(DstTy,
Y).getReg(0);
6633 B.buildInstr(PreferredFusedOpcode, {Dst}, {FpExtX, FpExtY, Z}, Flags);
6644 TLI.isFPExtFoldable(
MI, PreferredFusedOpcode, DstTy,
6646 unsigned Flags =
MI.getFlags() & FMulMI->
getFlags();
6648 Register FMAReg =
MRI.createGenericVirtualRegister(DstTy);
6651 B.buildFNeg(
MI.getOperand(0).getReg(), FMAReg);
6661 TLI.isFPExtFoldable(
MI, PreferredFusedOpcode, DstTy,
6663 unsigned Flags =
MI.getFlags() & FMulMI->
getFlags();
6679 assert(
MI.getOpcode() == TargetOpcode::G_FDIV);
6689 return N0CFP && (N0CFP->isOne() || N0CFP->isMinusOne());
6706 for (
auto &U :
MRI.use_nodbg_instructions(
Y)) {
6707 if (&U == &
MI || U.getParent() !=
MI.getParent())
6709 if (U.getOpcode() == TargetOpcode::G_FDIV &&
6710 U.getOperand(2).getReg() ==
Y && U.getOperand(1).getReg() !=
Y &&
6711 !IsOne(U.getOperand(1).getReg())) {
6724 return MatchInfo.
size() >= MinUses;
6732 LLT Ty =
MRI.getType(MatchInfo[0]->getOperand(0).
getReg());
6733 auto Div =
Builder.buildFDiv(Ty,
Builder.buildFConstant(Ty, 1.0),
6734 MatchInfo[0]->getOperand(2).getReg(),
6735 MatchInfo[0]->getFlags());
6740 Builder.buildFMul(
MI->getOperand(0).getReg(),
MI->getOperand(1).getReg(),
6741 Div->getOperand(0).getReg(),
MI->getFlags());
6742 MI->eraseFromParent();
6764 LLT DstVecTy =
MRI.getType(
MI.getOperand(0).getReg());
6773 return MRI.getType(MatchInfo) == DstVecTy;
6776 std::optional<ValueAndVReg> ShiftAmount;
6785 return MRI.getType(MatchInfo) == DstVecTy;
6800 return MRI.getType(MatchInfo) ==
MRI.getType(
MI.getOperand(0).getReg());
6807 std::optional<ValueAndVReg> ShiftAmt;
6813 LLT MatchTy =
MRI.getType(MatchInfo);
6814 return ShiftAmt->Value.getZExtValue() == MatchTy.
getSizeInBits() &&
6815 MatchTy ==
MRI.getType(
MI.getOperand(0).getReg());
6818unsigned CombinerHelper::getFPMinMaxOpcForSelect(
6820 SelectPatternNaNBehaviour VsNaNRetVal)
const {
6821 assert(VsNaNRetVal != SelectPatternNaNBehaviour::NOT_APPLICABLE &&
6822 "Expected a NaN behaviour?");
6832 if (VsNaNRetVal == SelectPatternNaNBehaviour::RETURNS_OTHER)
6833 return TargetOpcode::G_FMAXNUM;
6834 if (VsNaNRetVal == SelectPatternNaNBehaviour::RETURNS_NAN)
6835 return TargetOpcode::G_FMAXIMUM;
6836 if (
isLegal({TargetOpcode::G_FMAXNUM, {DstTy}}))
6837 return TargetOpcode::G_FMAXNUM;
6838 if (
isLegal({TargetOpcode::G_FMAXIMUM, {DstTy}}))
6839 return TargetOpcode::G_FMAXIMUM;
6845 if (VsNaNRetVal == SelectPatternNaNBehaviour::RETURNS_OTHER)
6846 return TargetOpcode::G_FMINNUM;
6847 if (VsNaNRetVal == SelectPatternNaNBehaviour::RETURNS_NAN)
6848 return TargetOpcode::G_FMINIMUM;
6849 if (
isLegal({TargetOpcode::G_FMINNUM, {DstTy}}))
6850 return TargetOpcode::G_FMINNUM;
6851 if (!
isLegal({TargetOpcode::G_FMINIMUM, {DstTy}}))
6853 return TargetOpcode::G_FMINIMUM;
6857CombinerHelper::SelectPatternNaNBehaviour
6859 bool IsOrderedComparison)
const {
6860 bool LHSSafe =
VT->isKnownNeverNaN(
LHS);
6861 bool RHSSafe =
VT->isKnownNeverNaN(
RHS);
6863 if (!LHSSafe && !RHSSafe)
6864 return SelectPatternNaNBehaviour::NOT_APPLICABLE;
6865 if (LHSSafe && RHSSafe)
6866 return SelectPatternNaNBehaviour::RETURNS_ANY;
6869 if (IsOrderedComparison)
6870 return LHSSafe ? SelectPatternNaNBehaviour::RETURNS_NAN
6871 : SelectPatternNaNBehaviour::RETURNS_OTHER;
6874 return LHSSafe ? SelectPatternNaNBehaviour::RETURNS_OTHER
6875 : SelectPatternNaNBehaviour::RETURNS_NAN;
6884 LLT DstTy =
MRI.getType(Dst);
6897 SelectPatternNaNBehaviour ResWithKnownNaNInfo =
6899 if (ResWithKnownNaNInfo == SelectPatternNaNBehaviour::NOT_APPLICABLE)
6901 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
6904 if (ResWithKnownNaNInfo == SelectPatternNaNBehaviour::RETURNS_NAN)
6905 ResWithKnownNaNInfo = SelectPatternNaNBehaviour::RETURNS_OTHER;
6906 else if (ResWithKnownNaNInfo == SelectPatternNaNBehaviour::RETURNS_OTHER)
6907 ResWithKnownNaNInfo = SelectPatternNaNBehaviour::RETURNS_NAN;
6909 if (TrueVal != CmpLHS || FalseVal != CmpRHS)
6912 unsigned Opc = getFPMinMaxOpcForSelect(Pred, DstTy, ResWithKnownNaNInfo);
6917 if (
Opc != TargetOpcode::G_FMAXIMUM &&
Opc != TargetOpcode::G_FMINIMUM) {
6922 if (!KnownNonZeroSide || !KnownNonZeroSide->Value.isNonZero()) {
6924 if (!KnownNonZeroSide || !KnownNonZeroSide->Value.isNonZero())
6928 MatchInfo = [=](MachineIRBuilder &
B) {
6929 B.buildInstr(
Opc, {Dst}, {CmpLHS, CmpRHS});
6937 assert(
MI.getOpcode() == TargetOpcode::G_SELECT);
6944 Register TrueVal =
MI.getOperand(2).getReg();
6945 Register FalseVal =
MI.getOperand(3).getReg();
6946 return matchFPSelectToMinMax(Dst,
Cond, TrueVal, FalseVal, MatchInfo);
6951 assert(
MI.getOpcode() == TargetOpcode::G_ICMP);
6964 if (MatchedSub &&
X != OpLHS)
6972 Y =
X == OpLHS ? OpRHS :
X == OpRHS ? OpLHS :
Register();
6975 auto Zero =
B.buildConstant(
MRI.getType(
Y), 0);
6976 B.buildICmp(Pred, Dst,
Y, Zero);
6983static std::optional<unsigned>
6985 std::optional<int64_t> &Result) {
6986 assert((Opcode == TargetOpcode::G_SHL || Opcode == TargetOpcode::G_LSHR ||
6987 Opcode == TargetOpcode::G_ASHR) &&
6988 "Expect G_SHL, G_LSHR or G_ASHR.");
6989 auto SignificantBits = 0;
6991 case TargetOpcode::G_SHL:
6995 case TargetOpcode::G_LSHR:
6999 case TargetOpcode::G_ASHR:
7008 Result = std::nullopt;
7019 Register ShiftVal =
MI.getOperand(1).getReg();
7020 Register ShiftReg =
MI.getOperand(2).getReg();
7021 LLT ResTy =
MRI.getType(
MI.getOperand(0).getReg());
7022 auto IsShiftTooBig = [&](
const Constant *
C) {
7027 MatchInfo = std::nullopt;
7031 MI.getOpcode(), MatchInfo);
7032 return OptMaxUsefulShift && CI->uge(*OptMaxUsefulShift);
7038 unsigned LHSOpndIdx = 1;
7039 unsigned RHSOpndIdx = 2;
7040 switch (
MI.getOpcode()) {
7041 case TargetOpcode::G_UADDO:
7042 case TargetOpcode::G_SADDO:
7043 case TargetOpcode::G_UMULO:
7044 case TargetOpcode::G_SMULO:
7051 Register LHS =
MI.getOperand(LHSOpndIdx).getReg();
7052 Register RHS =
MI.getOperand(RHSOpndIdx).getReg();
7062 if (LHSDef->
getOpcode() != TargetOpcode::G_CONSTANT_FOLD_BARRIER)
7066 return RHSDef->
getOpcode() != TargetOpcode::G_CONSTANT_FOLD_BARRIER &&
7073 std::optional<FPValueAndVReg> ValAndVReg;
7079bool CombinerHelper::isOneOrOneSplat(
Register Src,
bool AllowUndefs)
const {
7081 if (SrcTy.isFixedVector())
7083 if (SrcTy.isScalar()) {
7087 return IConstant && IConstant->Value == 1;
7092bool CombinerHelper::isZeroOrZeroSplat(
Register Src,
bool AllowUndefs)
const {
7093 LLT SrcTy =
MRI.getType(Src);
7095 return isConstantSplatVector(Src, 0, AllowUndefs);
7100 return IConstant && IConstant->Value == 0;
7107bool CombinerHelper::isConstantSplatVector(
Register Src, int64_t SplatValue,
7108 bool AllowUndefs)
const {
7114 for (
unsigned I = 0;
I < NumSources; ++
I) {
7115 GImplicitDef *ImplicitDef =
7117 if (ImplicitDef && AllowUndefs)
7119 if (ImplicitDef && !AllowUndefs)
7121 std::optional<ValueAndVReg> IConstant =
7123 if (IConstant && IConstant->Value == SplatValue)
7133CombinerHelper::getConstantOrConstantSplatVector(
Register Src)
const {
7136 return IConstant->Value;
7140 return std::nullopt;
7143 std::optional<APInt>
Value = std::nullopt;
7144 for (
unsigned I = 0;
I < NumSources; ++
I) {
7145 std::optional<ValueAndVReg> IConstant =
7148 return std::nullopt;
7150 Value = IConstant->Value;
7151 else if (*
Value != IConstant->Value)
7152 return std::nullopt;
7158bool CombinerHelper::isConstantOrConstantVectorI(
Register Src)
const {
7168 for (
unsigned I = 0;
I < NumSources; ++
I) {
7169 std::optional<ValueAndVReg> IConstant =
7178bool CombinerHelper::tryFoldSelectOfConstants(
GSelect *
Select,
7185 LLT CondTy =
MRI.getType(
Select->getCondReg());
7186 LLT TrueTy =
MRI.getType(
Select->getTrueReg());
7196 std::optional<ValueAndVReg> TrueOpt =
7198 std::optional<ValueAndVReg> FalseOpt =
7201 if (!TrueOpt || !FalseOpt)
7204 APInt TrueValue = TrueOpt->Value;
7205 APInt FalseValue = FalseOpt->Value;
7209 MatchInfo = [=](MachineIRBuilder &
B) {
7210 B.setInstrAndDebugLoc(*
Select);
7211 B.buildZExtOrTrunc(Dest,
Cond);
7218 MatchInfo = [=](MachineIRBuilder &
B) {
7219 B.setInstrAndDebugLoc(*
Select);
7220 B.buildSExtOrTrunc(Dest,
Cond);
7227 MatchInfo = [=](MachineIRBuilder &
B) {
7228 B.setInstrAndDebugLoc(*
Select);
7229 Register Inner =
MRI.createGenericVirtualRegister(CondTy);
7230 B.buildNot(Inner,
Cond);
7231 B.buildZExtOrTrunc(Dest, Inner);
7238 MatchInfo = [=](MachineIRBuilder &
B) {
7239 B.setInstrAndDebugLoc(*
Select);
7240 Register Inner =
MRI.createGenericVirtualRegister(CondTy);
7241 B.buildNot(Inner,
Cond);
7242 B.buildSExtOrTrunc(Dest, Inner);
7248 if (TrueValue - 1 == FalseValue) {
7249 MatchInfo = [=](MachineIRBuilder &
B) {
7250 B.setInstrAndDebugLoc(*
Select);
7251 Register Inner =
MRI.createGenericVirtualRegister(TrueTy);
7252 B.buildZExtOrTrunc(Inner,
Cond);
7253 B.buildAdd(Dest, Inner,
False);
7259 if (TrueValue + 1 == FalseValue) {
7260 MatchInfo = [=](MachineIRBuilder &
B) {
7261 B.setInstrAndDebugLoc(*
Select);
7262 Register Inner =
MRI.createGenericVirtualRegister(TrueTy);
7263 B.buildSExtOrTrunc(Inner,
Cond);
7264 B.buildAdd(Dest, Inner,
False);
7271 MatchInfo = [=](MachineIRBuilder &
B) {
7272 B.setInstrAndDebugLoc(*
Select);
7273 Register Inner =
MRI.createGenericVirtualRegister(TrueTy);
7274 B.buildZExtOrTrunc(Inner,
Cond);
7277 auto ShAmtC =
B.buildConstant(ShiftTy, TrueValue.
exactLogBase2());
7278 B.buildShl(Dest, Inner, ShAmtC, Flags);
7285 MatchInfo = [=](MachineIRBuilder &
B) {
7286 B.setInstrAndDebugLoc(*
Select);
7288 B.buildNot(Not,
Cond);
7289 Register Inner =
MRI.createGenericVirtualRegister(TrueTy);
7290 B.buildZExtOrTrunc(Inner, Not);
7293 auto ShAmtC =
B.buildConstant(ShiftTy, FalseValue.
exactLogBase2());
7294 B.buildShl(Dest, Inner, ShAmtC, Flags);
7301 MatchInfo = [=](MachineIRBuilder &
B) {
7302 B.setInstrAndDebugLoc(*
Select);
7303 Register Inner =
MRI.createGenericVirtualRegister(TrueTy);
7304 B.buildSExtOrTrunc(Inner,
Cond);
7305 B.buildOr(Dest, Inner,
False, Flags);
7312 MatchInfo = [=](MachineIRBuilder &
B) {
7313 B.setInstrAndDebugLoc(*
Select);
7315 B.buildNot(Not,
Cond);
7316 Register Inner =
MRI.createGenericVirtualRegister(TrueTy);
7317 B.buildSExtOrTrunc(Inner, Not);
7318 B.buildOr(Dest, Inner,
True, Flags);
7327bool CombinerHelper::tryFoldBoolSelectToLogic(
GSelect *
Select,
7334 LLT CondTy =
MRI.getType(
Select->getCondReg());
7335 LLT TrueTy =
MRI.getType(
Select->getTrueReg());
7344 if (CondTy != TrueTy)
7350 MatchInfo = [=](MachineIRBuilder &
B) {
7351 B.setInstrAndDebugLoc(*
Select);
7352 Register Ext =
MRI.createGenericVirtualRegister(TrueTy);
7353 B.buildZExtOrTrunc(Ext,
Cond);
7354 auto FreezeFalse =
B.buildFreeze(TrueTy,
False);
7355 B.buildOr(DstReg, Ext, FreezeFalse, Flags);
7363 MatchInfo = [=](MachineIRBuilder &
B) {
7364 B.setInstrAndDebugLoc(*
Select);
7365 Register Ext =
MRI.createGenericVirtualRegister(TrueTy);
7366 B.buildZExtOrTrunc(Ext,
Cond);
7367 auto FreezeTrue =
B.buildFreeze(TrueTy,
True);
7368 B.buildAnd(DstReg, Ext, FreezeTrue);
7374 if (isOneOrOneSplat(
False,
true)) {
7375 MatchInfo = [=](MachineIRBuilder &
B) {
7376 B.setInstrAndDebugLoc(*
Select);
7378 Register Inner =
MRI.createGenericVirtualRegister(CondTy);
7379 B.buildNot(Inner,
Cond);
7381 Register Ext =
MRI.createGenericVirtualRegister(TrueTy);
7382 B.buildZExtOrTrunc(Ext, Inner);
7383 auto FreezeTrue =
B.buildFreeze(TrueTy,
True);
7384 B.buildOr(DstReg, Ext, FreezeTrue, Flags);
7390 if (isZeroOrZeroSplat(
True,
true)) {
7391 MatchInfo = [=](MachineIRBuilder &
B) {
7392 B.setInstrAndDebugLoc(*
Select);
7394 Register Inner =
MRI.createGenericVirtualRegister(CondTy);
7395 B.buildNot(Inner,
Cond);
7397 Register Ext =
MRI.createGenericVirtualRegister(TrueTy);
7398 B.buildZExtOrTrunc(Ext, Inner);
7399 auto FreezeFalse =
B.buildFreeze(TrueTy,
False);
7400 B.buildAnd(DstReg, Ext, FreezeFalse);
7422 LLT DstTy =
MRI.getType(DstReg);
7427 if (!
MRI.hasOneNonDBGUse(CondReg))
7436 if (
True == CmpRHS &&
False == CmpLHS) {
7484 assert(
MI.getOpcode() == TargetOpcode::G_SUB);
7485 Register DestReg =
MI.getOperand(0).getReg();
7486 LLT DestTy =
MRI.getType(DestReg);
7500 if (
isLegal({NewOpc, {DestTy}})) {
7502 B.buildInstr(NewOpc, {DestReg}, {
X, Sub0});
7514 if (tryFoldSelectOfConstants(
Select, MatchInfo))
7517 if (tryFoldBoolSelectToLogic(
Select, MatchInfo))
7527bool CombinerHelper::tryFoldAndOrOrICmpsUsingRanges(
7529 assert(Logic->
getOpcode() != TargetOpcode::G_XOR &&
"unexpected xor");
7530 bool IsAnd = Logic->
getOpcode() == TargetOpcode::G_AND;
7534 unsigned Flags = Logic->
getFlags();
7553 std::optional<ValueAndVReg> MaybeC1 =
7557 C1 = MaybeC1->Value;
7559 std::optional<ValueAndVReg> MaybeC2 =
7563 C2 = MaybeC2->Value;
7584 std::optional<APInt> Offset1;
7585 std::optional<APInt> Offset2;
7588 std::optional<ValueAndVReg> MaybeOffset1 =
7591 R1 =
Add->getLHSReg();
7592 Offset1 = MaybeOffset1->Value;
7596 std::optional<ValueAndVReg> MaybeOffset2 =
7599 R2 =
Add->getLHSReg();
7600 Offset2 = MaybeOffset2->Value;
7619 bool CreateMask =
false;
7632 if (!LowerDiff.
isPowerOf2() || LowerDiff != UpperDiff ||
7645 CR->getEquivalentICmp(NewPred, NewC,
Offset);
7654 MatchInfo = [=](MachineIRBuilder &
B) {
7655 if (CreateMask &&
Offset != 0) {
7656 auto TildeLowerDiff =
B.buildConstant(CmpOperandTy, ~LowerDiff);
7657 auto And =
B.buildAnd(CmpOperandTy, R1, TildeLowerDiff);
7658 auto OffsetC =
B.buildConstant(CmpOperandTy,
Offset);
7659 auto Add =
B.buildAdd(CmpOperandTy,
And, OffsetC, Flags);
7660 auto NewCon =
B.buildConstant(CmpOperandTy, NewC);
7661 auto ICmp =
B.buildICmp(NewPred, CmpTy,
Add, NewCon);
7662 B.buildZExtOrTrunc(DstReg, ICmp);
7663 }
else if (CreateMask &&
Offset == 0) {
7664 auto TildeLowerDiff =
B.buildConstant(CmpOperandTy, ~LowerDiff);
7665 auto And =
B.buildAnd(CmpOperandTy, R1, TildeLowerDiff);
7666 auto NewCon =
B.buildConstant(CmpOperandTy, NewC);
7667 auto ICmp =
B.buildICmp(NewPred, CmpTy,
And, NewCon);
7668 B.buildZExtOrTrunc(DstReg, ICmp);
7669 }
else if (!CreateMask &&
Offset != 0) {
7670 auto OffsetC =
B.buildConstant(CmpOperandTy,
Offset);
7671 auto Add =
B.buildAdd(CmpOperandTy, R1, OffsetC, Flags);
7672 auto NewCon =
B.buildConstant(CmpOperandTy, NewC);
7673 auto ICmp =
B.buildICmp(NewPred, CmpTy,
Add, NewCon);
7674 B.buildZExtOrTrunc(DstReg, ICmp);
7675 }
else if (!CreateMask &&
Offset == 0) {
7676 auto NewCon =
B.buildConstant(CmpOperandTy, NewC);
7677 auto ICmp =
B.buildICmp(NewPred, CmpTy, R1, NewCon);
7678 B.buildZExtOrTrunc(DstReg, ICmp);
7686bool CombinerHelper::tryFoldLogicOfFCmps(
GLogicalBinOp *Logic,
7692 bool IsAnd = Logic->
getOpcode() == TargetOpcode::G_AND;
7704 LLT CmpTy =
MRI.getType(Cmp1->
getReg(0));
7710 {TargetOpcode::G_FCMP, {CmpTy, CmpOperandTy}}) ||
7711 !
MRI.hasOneNonDBGUse(Logic->
getReg(0)) ||
7712 !
MRI.hasOneNonDBGUse(Cmp1->
getReg(0)) ||
7713 !
MRI.hasOneNonDBGUse(Cmp2->
getReg(0)) ||
7724 if (LHS0 == RHS1 && LHS1 == RHS0) {
7730 if (LHS0 == RHS0 && LHS1 == RHS1) {
7734 unsigned NewPred = IsAnd ? CmpCodeL & CmpCodeR : CmpCodeL | CmpCodeR;
7736 MatchInfo = [=](MachineIRBuilder &
B) {
7741 auto False =
B.buildConstant(CmpTy, 0);
7742 B.buildZExtOrTrunc(DestReg,
False);
7749 B.buildZExtOrTrunc(DestReg,
True);
7751 auto Cmp =
B.buildFCmp(Pred, CmpTy, LHS0, LHS1, Flags);
7752 B.buildZExtOrTrunc(DestReg, Cmp);
7764 if (tryFoldAndOrOrICmpsUsingRanges(
And, MatchInfo))
7767 if (tryFoldLogicOfFCmps(
And, MatchInfo))
7776 if (tryFoldAndOrOrICmpsUsingRanges(
Or, MatchInfo))
7779 if (tryFoldLogicOfFCmps(
Or, MatchInfo))
7794 bool IsSigned =
Add->isSigned();
7795 LLT DstTy =
MRI.getType(Dst);
7796 LLT CarryTy =
MRI.getType(Carry);
7799 if (isConstantOrConstantVectorI(LHS) && !isConstantOrConstantVectorI(RHS)) {
7802 B.buildSAddo(Dst, Carry, RHS, LHS);
7808 B.buildUAddo(Dst, Carry, RHS, LHS);
7813 std::optional<APInt> MaybeLHS = getConstantOrConstantSplatVector(LHS);
7814 std::optional<APInt> MaybeRHS = getConstantOrConstantSplatVector(RHS);
7820 APInt Result = IsSigned ? MaybeLHS->sadd_ov(*MaybeRHS, Overflow)
7821 : MaybeLHS->uadd_ov(*MaybeRHS, Overflow);
7823 B.buildConstant(Dst, Result);
7824 B.buildConstant(Carry, Overflow);
7832 B.buildCopy(Dst, LHS);
7833 B.buildConstant(Carry, 0);
7842 if (MaybeRHS && AddLHS &&
MRI.hasOneNonDBGUse(
Add->getReg(0)) &&
7845 std::optional<APInt> MaybeAddRHS =
7846 getConstantOrConstantSplatVector(AddLHS->
getRHSReg());
7849 APInt NewC = IsSigned ? MaybeAddRHS->sadd_ov(*MaybeRHS, Overflow)
7850 : MaybeAddRHS->uadd_ov(*MaybeRHS, Overflow);
7854 auto ConstRHS =
B.buildConstant(DstTy, NewC);
7855 B.buildSAddo(Dst, Carry, AddLHS->
getLHSReg(), ConstRHS);
7861 auto ConstRHS =
B.buildConstant(DstTy, NewC);
7862 B.buildUAddo(Dst, Carry, AddLHS->
getLHSReg(), ConstRHS);
7887 B.buildConstant(Carry, 0);
7894 B.buildAdd(Dst, LHS, RHS);
7895 B.buildConstant(Carry, 1);
7907 if (
VT->computeNumSignBits(RHS) > 1 &&
VT->computeNumSignBits(LHS) > 1) {
7910 B.buildConstant(Carry, 0);
7926 B.buildConstant(Carry, 0);
7933 B.buildAdd(Dst, LHS, RHS);
7934 B.buildConstant(Carry, 1);
7952 bool OptForSize =
MI.getMF()->getFunction().hasOptSize();
7958 auto [Dst,
Base] =
MI.getFirst2Regs();
7959 LLT Ty =
MRI.getType(Dst);
7963 Builder.buildFConstant(Dst, 1.0);
7964 MI.removeFromParent();
7976 std::optional<SrcOp> Res;
7978 while (ExpVal > 0) {
7983 Res =
Builder.buildFMul(Ty, *Res, CurSquare);
7986 CurSquare =
Builder.buildFMul(Ty, CurSquare, CurSquare);
7993 Res =
Builder.buildFDiv(Ty,
Builder.buildFConstant(Ty, 1.0), *Res,
7997 MI.eraseFromParent();
8008 if (!
MRI.hasOneNonDBGUse(
Sub->getLHSReg()))
8015 LLT DstTy =
MRI.getType(Dst);
8018 auto Const =
B.buildConstant(DstTy, C1 - C2);
8019 B.buildAdd(Dst,
A, Const);
8033 if (!
MRI.hasOneNonDBGUse(
Sub->getRHSReg()))
8040 LLT DstTy =
MRI.getType(Dst);
8043 auto Const =
B.buildConstant(DstTy, C2 - C1);
8044 B.buildSub(Dst, Const,
A);
8065 LLT DstTy =
MRI.getType(Dst);
8068 auto Const =
B.buildConstant(DstTy, C1 + C2);
8069 B.buildSub(Dst,
A, Const);
8090 LLT DstTy =
MRI.getType(Dst);
8093 auto Const =
B.buildConstant(DstTy, C1 - C2);
8094 B.buildSub(Dst, Const,
A);
8108 if (!
MRI.hasOneNonDBGUse(
Add->getLHSReg()))
8115 LLT DstTy =
MRI.getType(Dst);
8118 auto Const =
B.buildConstant(DstTy, C2 - C1);
8119 B.buildAdd(Dst,
A, Const);
8163 if (!
MRI.hasOneNonDBGUse(BV->
getReg(0)))
8171 LLT SmallBvTy = DstTy;
8175 {TargetOpcode::G_BUILD_VECTOR, {SmallBvTy, SmallBvElemenTy}}))
8180 {TargetOpcode::G_ANYEXT,
8192 auto AnyExt =
B.buildAnyExt(SmallBvElemenTy, SourceArray);
8193 Ops.push_back(AnyExt.getReg(0));
8211 const LLT SrcTy =
MRI.getType(Shuffle.getSrc1Reg());
8212 const unsigned NumSrcElems = SrcTy.isVector() ? SrcTy.getNumElements() : 1;
8213 const unsigned NumDstElts = OrigMask.
size();
8214 for (
unsigned i = 0; i != NumDstElts; ++i) {
8215 int Idx = OrigMask[i];
8216 if (Idx >= (
int)NumSrcElems) {
8227 B.buildShuffleVector(
MI.getOperand(0),
MI.getOperand(1),
MI.getOperand(2),
8228 std::move(NewMask));
8235 const unsigned MaskSize = Mask.size();
8236 for (
unsigned I = 0;
I < MaskSize; ++
I) {
8241 if (Idx < (
int)NumElems)
8242 Mask[
I] = Idx + NumElems;
8244 Mask[
I] = Idx - NumElems;
8254 if (
getOpcodeDef(TargetOpcode::G_IMPLICIT_DEF, Shuffle.getSrc1Reg(),
MRI))
8257 if (
getOpcodeDef(TargetOpcode::G_IMPLICIT_DEF, Shuffle.getSrc2Reg(),
MRI))
8260 const LLT DstTy =
MRI.getType(Shuffle.getReg(0));
8261 const LLT Src1Ty =
MRI.getType(Shuffle.getSrc1Reg());
8263 {TargetOpcode::G_SHUFFLE_VECTOR, {DstTy, Src1Ty}}))
8267 const unsigned NumSrcElems = Src1Ty.getNumElements();
8269 bool TouchesSrc1 =
false;
8270 bool TouchesSrc2 =
false;
8271 const unsigned NumElems = Mask.size();
8272 for (
unsigned Idx = 0; Idx < NumElems; ++Idx) {
8276 if (Mask[Idx] < (
int)NumSrcElems)
8282 if (TouchesSrc1 == TouchesSrc2)
8285 Register NewSrc1 = Shuffle.getSrc1Reg();
8288 NewSrc1 = Shuffle.getSrc2Reg();
8293 auto Undef =
B.buildUndef(Src1Ty);
8294 B.buildShuffleVector(Shuffle.getReg(0), NewSrc1,
Undef, NewMask);
8308 LLT DstTy =
MRI.getType(Dst);
8309 LLT CarryTy =
MRI.getType(Carry);
8331 B.buildConstant(Carry, 0);
8338 B.buildSub(Dst, LHS, RHS);
8356 B.buildConstant(Carry, 0);
8363 B.buildSub(Dst, LHS, RHS);
8380 CtlzMI.
getOpcode() == TargetOpcode::G_CTLZ_ZERO_POISON) &&
8381 "Expected G_CTLZ variant");
8386 LLT Ty =
MRI.getType(Dst);
8387 LLT SrcTy =
MRI.getType(Src);
8389 if (!(Ty.isValid() && Ty.isScalar()))
8398 switch (
LI->getAction(Query).Action) {
8409 bool NeedAdd =
true;
8417 unsigned BitWidth = Ty.getScalarSizeInBits();
8428 B.buildCTLS(Dst,
X);
8432 auto Ctls =
B.buildCTLS(Ty,
X);
8433 auto One =
B.buildConstant(Ty, 1);
8435 B.buildAdd(Dst, Ctls, One);
8443 return C &&
C->getValueAPF().isNaN();
8447 unsigned ZeroPoisonOpc)
const {
8448 const bool IsCtlzPair =
Opc == TargetOpcode::G_CTLZ &&
8449 ZeroPoisonOpc == TargetOpcode::G_CTLZ_ZERO_POISON;
8450 const bool IsCttzPair =
Opc == TargetOpcode::G_CTTZ &&
8451 ZeroPoisonOpc == TargetOpcode::G_CTTZ_ZERO_POISON;
8452 if (
MI.getOpcode() !=
Opc || (!IsCtlzPair && !IsCttzPair))
8458 if (!
VT->isKnownNeverZero(Src))
8461 LLT DstTy =
MRI.getType(
MI.getOperand(0).getReg());
8462 LLT SrcTy =
MRI.getType(Src);
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
This file declares a class to represent arbitrary precision floating point values and provide a varie...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool hasMoreUses(const MachineInstr &MI0, const MachineInstr &MI1, const MachineRegisterInfo &MRI)
static bool isContractableFMul(MachineInstr &MI, bool AllowFusionGlobally)
Checks if MI is TargetOpcode::G_FMUL and contractable either due to global flags or MachineInstr flag...
static unsigned getIndexedOpc(unsigned LdStOpc)
static APFloat constantFoldFpUnary(const MachineInstr &MI, const MachineRegisterInfo &MRI, const APFloat &Val)
static std::optional< std::pair< GZExtLoad *, int64_t > > matchLoadAndBytePosition(Register Reg, unsigned MemSizeInBits, const MachineRegisterInfo &MRI)
Helper function for findLoadOffsetsForLoadOrCombine.
static std::optional< unsigned > getMinUselessShift(KnownBits ValueKB, unsigned Opcode, std::optional< int64_t > &Result)
Return the minimum useless shift amount that results in complete loss of the source value.
static Register peekThroughBitcast(Register Reg, const MachineRegisterInfo &MRI)
static unsigned bigEndianByteAt(const unsigned ByteWidth, const unsigned I)
static cl::opt< bool > ForceLegalIndexing("force-legal-indexing", cl::Hidden, cl::init(false), cl::desc("Force all indexed operations to be " "legal for the GlobalISel combiner"))
static void commuteMask(MutableArrayRef< int > Mask, const unsigned NumElems)
static cl::opt< unsigned > PostIndexUseThreshold("post-index-use-threshold", cl::Hidden, cl::init(32), cl::desc("Number of uses of a base pointer to check before it is no longer " "considered for post-indexing."))
static std::optional< bool > isBigEndian(const SmallDenseMap< int64_t, int64_t, 8 > &MemOffset2Idx, int64_t LowestIdx)
Given a map from byte offsets in memory to indices in a load/store, determine if that map corresponds...
static unsigned getExtLoadOpcForExtend(unsigned ExtOpc)
static bool isConstValidTrue(const TargetLowering &TLI, unsigned ScalarSizeBits, int64_t Cst, bool IsVector, bool IsFP)
static LLT getMidVTForTruncRightShiftCombine(LLT ShiftTy, LLT TruncTy)
static bool canFoldInAddressingMode(GLoadStore *MI, const TargetLowering &TLI, MachineRegisterInfo &MRI)
Return true if 'MI' is a load or a store that may be fold it's address operand into the load / store ...
static unsigned littleEndianByteAt(const unsigned ByteWidth, const unsigned I)
static Register buildLogBase2(Register V, MachineIRBuilder &MIB)
Determines the LogBase2 value for a non-null input value using the transform: LogBase2(V) = (EltBits ...
This contains common combine transformations that may be used in a combine pass,or by the target else...
This contains common code to allow clients to notify changes to machine instr.
Provides analysis for querying information about KnownBits during GISel passes.
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Interface for Targets to specify which operations they can successfully select and how the others sho...
static bool isConstantSplatVector(SDValue N, APInt &SplatValue, unsigned MinSizeInBits)
Implement a low-level type suitable for MachineInstr level instruction selection.
Contains matchers for matching SSA Machine Instructions.
This file declares the MachineIRBuilder class.
Promote Memory to Register
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
uint64_t IntrinsicInst * II
const SmallVectorImpl< MachineOperand > & Cond
Remove Loads Into Fake Uses
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
This file implements a set that has insertion order iteration characteristics.
This file implements the SmallBitVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
This file describes how to lower LLVM code to machine code.
static constexpr roundingMode rmTowardZero
static const fltSemantics & IEEEdouble()
static constexpr roundingMode rmTowardNegative
static constexpr roundingMode rmNearestTiesToEven
static constexpr roundingMode rmTowardPositive
static constexpr roundingMode rmNearestTiesToAway
const fltSemantics & getSemantics() const
opStatus fusedMultiplyAdd(const APFloat &Multiplicand, const APFloat &Addend, roundingMode RM)
Class for arbitrary precision integers.
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
uint64_t getZExtValue() const
Get zero extended value.
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
unsigned getBitWidth() const
Return the number of bits in the APInt.
bool ult(const APInt &RHS) const
Unsigned less than comparison.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
bool isNegative() const
Determine sign of this APInt.
int32_t exactLogBase2() const
void ashrInPlace(unsigned ShiftAmt)
Arithmetic right-shift this APInt by ShiftAmt in place.
unsigned countr_zero() const
Count the number of trailing zero bits.
unsigned countl_zero() const
The APInt version of std::countl_zero.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
static bool isSameValue(const APInt &I1, const APInt &I2, bool SignedCompare=false)
Determine if two APInts have the same value, after zero-extending or sign-extending (if SignedCompare...
LLVM_ABI APInt multiplicativeInverse() const
bool isMask(unsigned numBits) const
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
bool isOne() const
Determine if this is a value of 1.
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
int64_t getSExtValue() const
Get sign extended value.
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
unsigned countr_one() const
Count the number of trailing one bits.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
bool isEquality() const
Determine if this is an equals/not equals predicate.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
@ ICMP_SLT
signed less than
@ ICMP_SLE
signed less or equal
@ FCMP_OLT
0 1 0 0 True if ordered and less than
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
@ ICMP_UGE
unsigned greater or equal
@ ICMP_UGT
unsigned greater than
@ ICMP_SGT
signed greater than
@ FCMP_ULT
1 1 0 0 True if unordered or less than
@ ICMP_ULT
unsigned less than
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
@ ICMP_SGE
signed greater or equal
@ ICMP_ULE
unsigned less or equal
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
LLVM_ABI void applyCombineBuildVectorOfBitcast(MachineInstr &MI, SmallVector< Register > &Ops) const
LLVM_ABI void applyCombineExtendingLoads(MachineInstr &MI, PreferredTuple &MatchInfo) const
LLVM_ABI bool matchRepeatedFPDivisor(MachineInstr &MI, SmallVector< MachineInstr * > &MatchInfo) const
LLVM_ABI bool matchFoldC2MinusAPlusC1(const MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchLoadOrCombine(MachineInstr &MI, BuildFnTy &MatchInfo) const
Match expression trees of the form.
LLVM_ABI const RegisterBank * getRegBank(Register Reg) const
Get the register bank of Reg.
LLVM_ABI bool matchEqualDefs(const MachineOperand &MOP1, const MachineOperand &MOP2) const
Return true if MOP1 and MOP2 are register operands are defined by equivalent instructions.
LLVM_ABI void applyUDivOrURemByConst(MachineInstr &MI) const
LLVM_ABI bool matchConstantFoldBinOp(MachineInstr &MI, APInt &MatchInfo) const
Do constant folding when opportunities are exposed after MIR building.
LLVM_ABI void applyCombineUnmergeWithDeadLanesToTrunc(MachineInstr &MI) const
LLVM_ABI bool matchUnmergeValuesAnyExtBuildVector(const MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchFsubToFneg(MachineInstr &MI) const
LLVM_ABI bool matchCtls(MachineInstr &CtlzMI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchSelectSameVal(MachineInstr &MI) const
Optimize (cond ? x : x) -> x.
LLVM_ABI bool matchReassocConstantInnerRHS(GPtrAdd &MI, MachineInstr *RHS, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchBitfieldExtractFromShr(MachineInstr &MI, BuildFnTy &MatchInfo) const
Match: shr (shl x, n), k -> sbfx/ubfx x, pos, width.
LLVM_ABI bool matchFoldAMinusC1PlusC2(const MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchTruncSSatU(MachineInstr &MI, Register &MatchInfo) const
LLVM_ABI void applySimplifyURemByPow2(MachineInstr &MI) const
Combine G_UREM x, (known power of 2) to an add and bitmasking.
LLVM_ABI bool matchCombineUnmergeZExtToZExt(MachineInstr &MI) const
Transform X, Y = G_UNMERGE(G_ZEXT(Z)) -> X = G_ZEXT(Z); Y = G_CONSTANT 0.
const TargetInstrInfo * TII
LLVM_ABI void applyCombineConcatVectors(MachineInstr &MI, SmallVector< Register > &Ops) const
Replace MI with a flattened build_vector with Ops or an implicit_def if Ops is empty.
LLVM_ABI void applyXorOfAndWithSameReg(MachineInstr &MI, std::pair< Register, Register > &MatchInfo) const
LLVM_ABI bool canCombineFMadOrFMA(MachineInstr &MI, bool &AllowFusionGlobally, bool &HasFMAD, bool &Aggressive, bool CanReassociate=false) const
LLVM_ABI bool matchFoldAPlusC1MinusC2(const MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchExtractVecEltBuildVec(MachineInstr &MI, Register &Reg) const
LLVM_ABI void applyCombineUnmergeConstant(MachineInstr &MI, SmallVectorImpl< APInt > &Csts) const
LLVM_ABI bool matchShiftsTooBig(MachineInstr &MI, std::optional< int64_t > &MatchInfo) const
Match shifts greater or equal to the range (the bitwidth of the result datatype, or the effective bit...
LLVM_ABI bool matchCombineFAddFpExtFMulToFMadOrFMA(MachineInstr &MI, BuildFnTy &MatchInfo) const
Transform (fadd (fpext (fmul x, y)), z) -> (fma (fpext x), (fpext y), z) (fadd (fpext (fmul x,...
LLVM_ABI bool matchCombineIndexedLoadStore(MachineInstr &MI, IndexedLoadStoreMatchInfo &MatchInfo) const
LLVM_ABI void applyCombineShuffleConcat(MachineInstr &MI, SmallVector< Register > &Ops) const
Replace MI with a flattened build_vector with Ops or an implicit_def if Ops is empty.
LLVM_ABI void replaceSingleDefInstWithReg(MachineInstr &MI, Register Replacement) const
Delete MI and replace all of its uses with Replacement.
LLVM_ABI void applyCombineShuffleToBuildVector(MachineInstr &MI) const
Replace MI with a build_vector.
LLVM_ABI bool matchCombineExtractedVectorLoad(MachineInstr &MI, BuildFnTy &MatchInfo) const
Combine a G_EXTRACT_VECTOR_ELT of a load into a narrowed load.
LLVM_ABI void replaceRegWith(MachineRegisterInfo &MRI, Register FromReg, Register ToReg) const
MachineRegisterInfo::replaceRegWith() and inform the observer of the changes.
LLVM_ABI void replaceRegOpWith(MachineRegisterInfo &MRI, MachineOperand &FromRegOp, Register ToReg) const
Replace a single register operand with a new register and inform the observer of the changes.
LLVM_ABI void applyCombineMemCpyFamily(MachineInstr &MI, MemCpyFamilyLoweringInfo &MatchInfo) const
LLVM_ABI bool matchReassocCommBinOp(MachineInstr &MI, BuildFnTy &MatchInfo) const
Reassociate commutative binary operations like G_ADD.
LLVM_ABI void applyBuildFnMO(const MachineOperand &MO, BuildFnTy &MatchInfo) const
Use a function which takes in a MachineIRBuilder to perform a combine.
LLVM_ABI bool matchCommuteConstantToRHS(MachineInstr &MI) const
Match constant LHS ops that should be commuted.
LLVM_ABI bool matchSimplifyNegMinMax(MachineInstr &MI, BuildFnTy &MatchInfo) const
Tranform (neg (min/max x, (neg x))) into (max/min x, (neg x)).
LLVM_ABI bool matchCombineDivRem(MachineInstr &MI, MachineInstr *&OtherMI) const
Try to combine G_[SU]DIV and G_[SU]REM into a single G_[SU]DIVREM when their source operands are iden...
LLVM_ABI void applyUMulHToLShr(MachineInstr &MI) const
LLVM_ABI void applyNotCmp(MachineInstr &MI, SmallVectorImpl< Register > &RegsToNegate) const
LLVM_ABI bool isLegalOrHasFewerElements(const LegalityQuery &Query) const
LLVM_ABI bool matchShiftImmedChain(MachineInstr &MI, RegisterImmPair &MatchInfo) const
Fold (shift (shift base, x), y) -> (shift base (x+y))
LLVM_ABI bool matchTruncLshrBuildVectorFold(MachineInstr &MI, Register &MatchInfo) const
LLVM_ABI bool matchAllExplicitUsesAreUndef(MachineInstr &MI) const
Return true if all register explicit use operands on MI are defined by a G_IMPLICIT_DEF.
LLVM_ABI bool isPredecessor(const MachineInstr &DefMI, const MachineInstr &UseMI) const
Returns true if DefMI precedes UseMI or they are the same instruction.
LLVM_ABI bool matchPtrAddImmedChain(MachineInstr &MI, PtrAddChain &MatchInfo) const
LLVM_ABI bool matchTruncSSatS(MachineInstr &MI, Register &MatchInfo) const
LLVM_ABI const TargetLowering & getTargetLowering() const
LLVM_ABI bool matchShuffleUndefRHS(MachineInstr &MI, BuildFnTy &MatchInfo) const
Remove references to rhs if it is undef.
LLVM_ABI void applyBuildInstructionSteps(MachineInstr &MI, InstructionStepsMatchInfo &MatchInfo) const
Replace MI with a series of instructions described in MatchInfo.
LLVM_ABI void applySDivByPow2(MachineInstr &MI) const
LLVM_ABI void applyUDivByPow2(MachineInstr &MI) const
Given an G_UDIV MI expressing an unsigned divided by a pow2 constant, return expressions that impleme...
LLVM_ABI bool matchOr(MachineInstr &MI, BuildFnTy &MatchInfo) const
Combine ors.
LLVM_ABI bool matchLshrOfTruncOfLshr(MachineInstr &MI, LshrOfTruncOfLshr &MatchInfo, MachineInstr &ShiftMI) const
Fold (lshr (trunc (lshr x, C1)), C2) -> trunc (shift x, (C1 + C2))
LLVM_ABI void replaceInstWithConstant(MachineInstr &MI, int64_t C) const
Replace an instruction with a G_CONSTANT with value C.
LLVM_ABI bool matchCombineFSubFpExtFMulToFMadOrFMA(MachineInstr &MI, BuildFnTy &MatchInfo) const
Transform (fsub (fpext (fmul x, y)), z) -> (fma (fpext x), (fpext y), (fneg z)) (fsub (fpext (fmul x,...
LLVM_ABI bool matchConstantLargerBitWidth(MachineInstr &MI, unsigned ConstIdx) const
Checks if constant at ConstIdx is larger than MI 's bitwidth.
LLVM_ABI void applyCombineCopy(MachineInstr &MI) const
LLVM_ABI bool matchCombineShlOfExtend(MachineInstr &MI, RegisterImmPair &MatchData) const
LLVM_ABI bool matchCombineFSubFMulToFMadOrFMA(MachineInstr &MI, BuildFnTy &MatchInfo) const
Transform (fsub (fmul x, y), z) -> (fma x, y, -z) (fsub (fmul x, y), z) -> (fmad x,...
LLVM_ABI bool matchCombineFAddFMAFMulToFMadOrFMA(MachineInstr &MI, BuildFnTy &MatchInfo) const
Transform (fadd (fma x, y, (fmul u, v)), z) -> (fma x, y, (fma u, v, z)) (fadd (fmad x,...
LLVM_ABI bool matchSextTruncSextLoad(MachineInstr &MI) const
LLVM_ABI bool matchCombineInsertVecElts(MachineInstr &MI, SmallVectorImpl< Register > &MatchInfo) const
LLVM_ABI bool matchCombineBuildUnmerge(MachineInstr &MI, MachineRegisterInfo &MRI, Register &UnmergeSrc) const
LLVM_ABI bool matchDivByPow2(MachineInstr &MI, bool IsSigned) const
Given an G_SDIV MI expressing a signed divided by a pow2 constant, return expressions that implements...
LLVM_ABI bool matchNarrowBinopFeedingAnd(MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchCombineLoadWithAndMask(MachineInstr &MI, BuildFnTy &MatchInfo) const
Match (and (load x), mask) -> zextload x.
LLVM_ABI bool matchCombineFAddFMulToFMadOrFMA(MachineInstr &MI, BuildFnTy &MatchInfo) const
Transform (fadd (fmul x, y), z) -> (fma x, y, z) (fadd (fmul x, y), z) -> (fmad x,...
LLVM_ABI bool matchCombineCopy(MachineInstr &MI) const
LLVM_ABI bool matchExtendThroughPhis(MachineInstr &MI, MachineInstr *&ExtMI) const
LLVM_ABI void applyShiftImmedChain(MachineInstr &MI, RegisterImmPair &MatchInfo) const
LLVM_ABI bool matchXorOfAndWithSameReg(MachineInstr &MI, std::pair< Register, Register > &MatchInfo) const
Fold (xor (and x, y), y) -> (and (not x), y) {.
LLVM_ABI bool matchCombineShuffleVector(MachineInstr &MI, SmallVectorImpl< Register > &Ops) const
Check if the G_SHUFFLE_VECTOR MI can be replaced by a concat_vectors.
LLVM_ABI void replaceInstWithFConstant(MachineInstr &MI, double C) const
Replace an instruction with a G_FCONSTANT with value C.
LLVM_ABI bool matchOrShiftToFunnelShift(MachineInstr &MI, bool AllowScalarConstants, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchRedundantSExtInReg(MachineInstr &MI) const
LLVM_ABI void replaceOpcodeWith(MachineInstr &FromMI, unsigned ToOpcode) const
Replace the opcode in instruction with a new opcode and inform the observer of the changes.
LLVM_ABI void applyFunnelShiftConstantModulo(MachineInstr &MI) const
Replaces the shift amount in MI with ShiftAmt % BW.
LLVM_ABI bool matchFoldC1Minus2MinusC2(const MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI void applyCombineShlOfExtend(MachineInstr &MI, const RegisterImmPair &MatchData) const
LLVM_ABI void applyUseVectorTruncate(MachineInstr &MI, Register &MatchInfo) const
LLVM_ABI CombinerHelper(GISelChangeObserver &Observer, MachineIRBuilder &B, bool IsPreLegalize, GISelValueTracking *VT=nullptr, MachineDominatorTree *MDT=nullptr, const LegalizerInfo *LI=nullptr)
LLVM_ABI bool matchShuffleDisjointMask(MachineInstr &MI, BuildFnTy &MatchInfo) const
Turn shuffle a, b, mask -> shuffle undef, b, mask iff mask does not reference a.
LLVM_ABI bool matchCombineMulToShl(MachineInstr &MI, unsigned &ShiftVal) const
Transform a multiply by a power-of-2 value to a left shift.
LLVM_ABI void applyCombineShuffleVector(MachineInstr &MI, ArrayRef< Register > Ops) const
Replace MI with a concat_vectors with Ops.
LLVM_ABI bool matchCombineUnmergeUndef(MachineInstr &MI, std::function< void(MachineIRBuilder &)> &MatchInfo) const
Transform G_UNMERGE G_IMPLICIT_DEF -> G_IMPLICIT_DEF, G_IMPLICIT_DEF, ...
LLVM_ABI void applyFoldBinOpIntoSelect(MachineInstr &MI, const unsigned &SelectOpNo) const
SelectOperand is the operand in binary operator MI that is the select to fold.
LLVM_ABI bool matchFoldAMinusC1MinusC2(const MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI void applyCombineIndexedLoadStore(MachineInstr &MI, IndexedLoadStoreMatchInfo &MatchInfo) const
LLVM_ABI bool matchCombineShuffleConcat(MachineInstr &MI, SmallVector< Register > &Ops) const
LLVM_ABI void applySextInRegOfLoad(MachineInstr &MI, std::tuple< Register, unsigned > &MatchInfo) const
LLVM_ABI bool matchTruncUSatU(MachineInstr &MI, MachineInstr &MinMI) const
LLVM_ABI bool matchICmpToLHSKnownBits(MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchReassocPtrAdd(MachineInstr &MI, BuildFnTy &MatchInfo) const
Reassociate pointer calculations with G_ADD involved, to allow better addressing mode usage.
LLVM_ABI bool isPreLegalize() const
LLVM_ABI bool matchUndefShuffleVectorMask(MachineInstr &MI) const
Return true if a G_SHUFFLE_VECTOR instruction MI has an undef mask.
LLVM_ABI bool matchCombineSubToAdd(MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchShiftOfShiftedLogic(MachineInstr &MI, ShiftOfShiftedLogic &MatchInfo) const
If we have a shift-by-constant of a bitwise logic op that itself has a shift-by-constant operand with...
LLVM_ABI bool matchCombineConcatVectors(MachineInstr &MI, SmallVector< Register > &Ops) const
If MI is G_CONCAT_VECTORS, try to combine it.
LLVM_ABI bool matchInsertExtractVecEltOutOfBounds(MachineInstr &MI) const
Return true if a G_{EXTRACT,INSERT}_VECTOR_ELT has an out of range index.
LLVM_ABI bool matchExtractAllEltsFromBuildVector(MachineInstr &MI, SmallVectorImpl< std::pair< Register, MachineInstr * > > &MatchInfo) const
LLVM_ABI LLVMContext & getContext() const
LLVM_ABI void applyPtrAddImmedChain(MachineInstr &MI, PtrAddChain &MatchInfo) const
LLVM_ABI bool isConstantLegalOrBeforeLegalizer(const LLT Ty) const
LLVM_ABI bool matchNotCmp(MachineInstr &MI, SmallVectorImpl< Register > &RegsToNegate) const
Combine inverting a result of a compare into the opposite cond code.
LLVM_ABI bool matchSextInRegOfLoad(MachineInstr &MI, std::tuple< Register, unsigned > &MatchInfo) const
Match sext_inreg(load p), imm -> sextload p.
LLVM_ABI bool matchSelectIMinMax(const MachineOperand &MO, BuildFnTy &MatchInfo) const
Combine select to integer min/max.
LLVM_ABI bool matchConstantFoldUnaryIntOp(MachineInstr &MI, BuildFnTy &MatchInfo) const
Constant fold a unary integer op (G_CTLZ, G_CTTZ, G_CTPOP and their _ZERO_POISON variants,...
LLVM_ABI void applyCombineConstantFoldFpUnary(MachineInstr &MI, const ConstantFP *Cst) const
Transform fp_instr(cst) to constant result of the fp operation.
LLVM_ABI bool isLegal(const LegalityQuery &Query) const
LLVM_ABI bool matchICmpToTrueFalseKnownBits(MachineInstr &MI, int64_t &MatchInfo) const
LLVM_ABI bool matchOperandIsKnownToBeAPowerOfTwo(const MachineOperand &MO, bool OrNegative=false) const
Check if operand MO is known to be a power of 2.
LLVM_ABI bool tryReassocBinOp(unsigned Opc, Register DstReg, Register Op0, Register Op1, BuildFnTy &MatchInfo) const
Try to reassociate to reassociate operands of a commutative binop.
LLVM_ABI void eraseInst(MachineInstr &MI) const
Erase MI.
LLVM_ABI bool matchConstantFoldFPBinOp(MachineInstr &MI, ConstantFP *&MatchInfo) const
Do constant FP folding when opportunities are exposed after MIR building.
LLVM_ABI void applyBuildFnNoErase(MachineInstr &MI, BuildFnTy &MatchInfo) const
Use a function which takes in a MachineIRBuilder to perform a combine.
LLVM_ABI bool matchUseVectorTruncate(MachineInstr &MI, Register &MatchInfo) const
MachineRegisterInfo & MRI
LLVM_ABI void applyExtendThroughPhis(MachineInstr &MI, MachineInstr *&ExtMI) const
LLVM_ABI bool matchConstantFPOp(const MachineOperand &MOP, double C) const
Return true if MOP is defined by a G_FCONSTANT or splat with a value exactly equal to C.
LLVM_ABI MachineInstr * buildUDivOrURemUsingMul(MachineInstr &MI) const
Given an G_UDIV MI or G_UREM MI expressing a divide by constant, return an expression that implements...
LLVM_ABI void applyExtractVecEltBuildVec(MachineInstr &MI, Register &Reg) const
LLVM_ABI bool matchFoldBinOpIntoSelect(MachineInstr &MI, unsigned &SelectOpNo) const
Push a binary operator through a select on constants.
LLVM_ABI bool tryCombineShiftToUnmerge(MachineInstr &MI, unsigned TargetShiftAmount) const
LLVM_ABI bool isConstantNaN(Register Reg) const
LLVM_ABI bool matchPtrAddZeroVector(MachineInstr &MI) const
}
LLVM_ABI bool tryCombineExtendingLoads(MachineInstr &MI) const
If MI is extend that consumes the result of a load, try to combine it.
LLVM_ABI bool isLegalOrBeforeLegalizer(const LegalityQuery &Query) const
LLVM_ABI bool matchBuildVectorIdentityFold(MachineInstr &MI, Register &MatchInfo) const
LLVM_ABI bool matchBitfieldExtractFromShrAnd(MachineInstr &MI, BuildFnTy &MatchInfo) const
Match: shr (and x, n), k -> ubfx x, pos, width.
LLVM_ABI void applyTruncSSatS(MachineInstr &MI, Register &MatchInfo) const
LLVM_ABI bool matchConstantFoldCastOp(MachineInstr &MI, APInt &MatchInfo) const
Do constant folding when opportunities are exposed after MIR building.
LLVM_ABI void applyRotateOutOfRange(MachineInstr &MI) const
LLVM_ABI bool matchReassocFoldConstantsInSubTree(GPtrAdd &MI, MachineInstr *LHS, MachineInstr *RHS, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchHoistLogicOpWithSameOpcodeHands(MachineInstr &MI, InstructionStepsMatchInfo &MatchInfo) const
Match (logic_op (op x...), (op y...)) -> (op (logic_op x, y))
LLVM_ABI bool matchBitfieldExtractFromAnd(MachineInstr &MI, BuildFnTy &MatchInfo) const
Match: and (lshr x, cst), mask -> ubfx x, cst, width.
LLVM_ABI bool matchBitfieldExtractFromSExtInReg(MachineInstr &MI, BuildFnTy &MatchInfo) const
Form a G_SBFX from a G_SEXT_INREG fed by a right shift.
LLVM_ABI bool matchAndOrDisjointMask(MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI void replaceInstWithUndef(MachineInstr &MI) const
Replace an instruction with a G_IMPLICIT_DEF.
LLVM_ABI bool isDesirableToCommuteWithShift(const MachineInstr &MI) const
LLVM_ABI bool matchRedundantBinOpInEquality(MachineInstr &MI, BuildFnTy &MatchInfo) const
Transform: (X + Y) == X -> Y == 0 (X - Y) == X -> Y == 0 (X ^ Y) == X -> Y == 0 (X + Y) !...
LLVM_ABI bool matchOptBrCondByInvertingCond(MachineInstr &MI, MachineInstr *&BrCond) const
If a brcond's true block is not the fallthrough, make it so by inverting the condition and swapping o...
LLVM_ABI bool matchAddOverflow(MachineInstr &MI, BuildFnTy &MatchInfo) const
Combine addos.
LLVM_ABI void applyAshShlToSextInreg(MachineInstr &MI, std::tuple< Register, int64_t > &MatchInfo) const
LLVM_ABI bool matchSelect(MachineInstr &MI, BuildFnTy &MatchInfo) const
Combine selects.
LLVM_ABI bool matchCombineExtendingLoads(MachineInstr &MI, PreferredTuple &MatchInfo) const
LLVM_ABI bool matchCombineUnmergeWithDeadLanesToTrunc(MachineInstr &MI) const
Transform X, Y<dead> = G_UNMERGE Z -> X = G_TRUNC Z.
LLVM_ABI bool matchRotateOutOfRange(MachineInstr &MI) const
LLVM_ABI void applyExpandFPowI(MachineInstr &MI, int64_t Exponent) const
Expands FPOWI into a series of multiplications and a division if the exponent is negative.
LLVM_ABI void setRegBank(Register Reg, const RegisterBank *RegBank) const
Set the register bank of Reg.
LLVM_ABI bool matchCommuteFPConstantToRHS(MachineInstr &MI) const
Match constant LHS FP ops that should be commuted.
LLVM_ABI void applyCombineDivRem(MachineInstr &MI, MachineInstr *&OtherMI) const
LLVM_ABI bool matchRedundantOr(MachineInstr &MI, Register &Replacement) const
LLVM_ABI void applyTruncSSatU(MachineInstr &MI, Register &MatchInfo) const
LLVM_ABI void applySimplifySRemByPow2(MachineInstr &MI) const
Combine G_SREM x, (+/-2^k) to a bias-and-mask sequence.
LLVM_ABI bool matchCombineFSubFpExtFNegFMulToFMadOrFMA(MachineInstr &MI, BuildFnTy &MatchInfo) const
Transform (fsub (fpext (fneg (fmul x, y))), z) -> (fneg (fma (fpext x), (fpext y),...
LLVM_ABI bool matchTruncBuildVectorFold(MachineInstr &MI, Register &MatchInfo) const
LLVM_ABI void applyCombineTruncOfShift(MachineInstr &MI, std::pair< MachineInstr *, LLT > &MatchInfo) const
LLVM_ABI bool matchConstantOp(const MachineOperand &MOP, int64_t C) const
Return true if MOP is defined by a G_CONSTANT or splat with a value equal to C.
LLVM_ABI void applyCombineMulToShl(MachineInstr &MI, unsigned &ShiftVal) const
LLVM_ABI void applyCombineBuildUnmerge(MachineInstr &MI, MachineRegisterInfo &MRI, MachineIRBuilder &B, Register &UnmergeSrc) const
LLVM_ABI bool matchUMulHToLShr(MachineInstr &MI) const
MachineDominatorTree * MDT
LLVM_ABI bool matchSimplifySelectToMinMax(MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI void applyRepeatedFPDivisor(SmallVector< MachineInstr * > &MatchInfo) const
LLVM_ABI bool matchTruncUSatUToFPTOUISat(MachineInstr &MI, MachineInstr &SrcMI) const
const RegisterBankInfo * RBI
LLVM_ABI bool matchMulOBy0(MachineInstr &MI, BuildFnTy &MatchInfo) const
Match: (G_*MULO x, 0) -> 0 + no carry out.
LLVM_ABI bool matchBinopWithNeg(MachineInstr &MI, BuildFnTy &MatchInfo) const
Fold a bitwiseop (~b +/- c) -> a bitwiseop ~(b -/+ c)
LLVM_ABI bool matchCombineUnmergeConstant(MachineInstr &MI, SmallVectorImpl< APInt > &Csts) const
Transform G_UNMERGE Constant -> Constant1, Constant2, ...
LLVM_ABI bool matchCountZeroToZeroPoison(MachineInstr &MI, unsigned Opc, unsigned ZeroPoisonOpc) const
LLVM_ABI void applyShiftOfShiftedLogic(MachineInstr &MI, ShiftOfShiftedLogic &MatchInfo) const
const TargetRegisterInfo * TRI
LLVM_ABI bool matchRedundantAnd(MachineInstr &MI, Register &Replacement) const
LLVM_ABI bool dominates(const MachineInstr &DefMI, const MachineInstr &UseMI) const
Returns true if DefMI dominates UseMI.
GISelChangeObserver & Observer
LLVM_ABI void applyBuildFn(MachineInstr &MI, BuildFnTy &MatchInfo) const
Use a function which takes in a MachineIRBuilder to perform a combine.
LLVM_ABI bool matchCombineTruncOfShift(MachineInstr &MI, std::pair< MachineInstr *, LLT > &MatchInfo) const
Transform trunc (shl x, K) to shl (trunc x), K if K < VT.getScalarSizeInBits().
LLVM_ABI bool matchCombineShiftToUnmerge(MachineInstr &MI, unsigned TargetShiftSize, unsigned &ShiftVal) const
Reduce a shift by a constant to an unmerge and a shift on a half sized type.
LLVM_ABI bool matchUDivOrURemByConst(MachineInstr &MI) const
Combine G_UDIV or G_UREM by constant into a multiply by magic constant.
LLVM_ABI bool matchAnd(MachineInstr &MI, BuildFnTy &MatchInfo) const
Combine ands.
LLVM_ABI bool matchSuboCarryOut(const MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchConstantFoldFMA(MachineInstr &MI, ConstantFP *&MatchInfo) const
Constant fold G_FMA/G_FMAD.
LLVM_ABI bool matchCombineFSubFNegFMulToFMadOrFMA(MachineInstr &MI, BuildFnTy &MatchInfo) const
Transform (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z)) (fsub (fneg (fmul,...
LLVM_ABI bool matchCombineZextTrunc(MachineInstr &MI, Register &Reg) const
Transform zext(trunc(x)) to x.
LLVM_ABI bool matchSameRegOrICstOrSplat(Register A, Register B) const
True when A and B are the same register, including through copies, or different registers holding the...
LLVM_ABI void applyLshrOfTruncOfLshr(MachineInstr &MI, LshrOfTruncOfLshr &MatchInfo) const
LLVM_ABI bool tryCombineMemCpyFamily(MachineInstr &MI, unsigned MaxLen=0) const
Optimize memcpy intrinsics et al, e.g.
LLVM_ABI bool matchFreezeOfSingleMaybePoisonOperand(MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI void applySDivOrSRemByConst(MachineInstr &MI) const
LLVM_ABI bool matchCombineMemCpyFamily(MachineInstr &MI, MemCpyFamilyLoweringInfo &MatchInfo, unsigned MaxLen=0) const
LLVM_ABI MachineInstr * buildSDivOrSRemUsingMul(MachineInstr &MI) const
Given an G_SDIV MI or G_SREM MI expressing a signed divide by constant, return an expression that imp...
LLVM_ABI bool isLegalOrHasWidenScalar(const LegalityQuery &Query) const
LLVM_ABI bool matchReassocConstantInnerLHS(GPtrAdd &MI, MachineInstr *LHS, MachineInstr *RHS, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchOverlappingAnd(MachineInstr &MI, BuildFnTy &MatchInfo) const
Fold and(and(x, C1), C2) -> C1&C2 ? and(x, C1&C2) : 0.
LLVM_ABI bool matchCombineAnyExtTrunc(MachineInstr &MI, Register &Reg) const
Transform anyext(trunc(x)) to x.
LLVM_ABI void applyExtractAllEltsFromBuildVector(MachineInstr &MI, SmallVectorImpl< std::pair< Register, MachineInstr * > > &MatchInfo) const
MachineIRBuilder & Builder
LLVM_ABI void replaceSingleDefInstWithOperand(MachineInstr &MI, unsigned OpIdx) const
Delete MI and replace all of its uses with its OpIdx-th operand.
LLVM_ABI const MachineFunction & getMachineFunction() const
LLVM_ABI bool matchCombineBuildVectorOfBitcast(MachineInstr &MI, SmallVector< Register > &Ops) const
Combine G_BUILD_VECTOR(G_UNMERGE(G_BITCAST), Undef) to G_BITCAST(G_BUILD_VECTOR(.....
LLVM_ABI bool matchCombineFAddFpExtFMulToFMadOrFMAAggressive(MachineInstr &MI, BuildFnTy &MatchInfo) const
LLVM_ABI bool matchSDivOrSRemByConst(MachineInstr &MI) const
Combine G_SDIV or G_SREM by constant into a multiply by magic constant.
LLVM_ABI void applyOptBrCondByInvertingCond(MachineInstr &MI, MachineInstr *&BrCond) const
LLVM_ABI void applyCombineShiftToUnmerge(MachineInstr &MI, const unsigned &ShiftVal) const
LLVM_ABI bool matchFPowIExpansion(MachineInstr &MI, int64_t Exponent) const
Match FPOWI if it's safe to extend it into a series of multiplications.
LLVM_ABI void applyCombineInsertVecElts(MachineInstr &MI, SmallVectorImpl< Register > &MatchInfo) const
LLVM_ABI bool matchCombineUnmergeMergeToPlainValues(MachineInstr &MI, SmallVectorImpl< Register > &Operands) const
Transform <ty,...> G_UNMERGE(G_MERGE ty X, Y, Z) -> ty X, Y, Z.
LLVM_ABI void applyCombineUnmergeMergeToPlainValues(MachineInstr &MI, SmallVectorImpl< Register > &Operands) const
LLVM_ABI bool matchAshrShlToSextInreg(MachineInstr &MI, std::tuple< Register, int64_t > &MatchInfo) const
Match ashr (shl x, C), C -> sext_inreg (C)
LLVM_ABI void applyCombineUnmergeZExtToZExt(MachineInstr &MI) const
ConstantFP - Floating Point Values [float, double].
const APFloat & getValue() const
const APFloat & getValueAPF() const
This class represents a range of values.
LLVM_ABI std::optional< ConstantRange > exactUnionWith(const ConstantRange &CR) const
Union the two ranges and return the result if it can be represented exactly, otherwise return std::nu...
LLVM_ABI ConstantRange subtract(const APInt &CI) const
Subtract the specified constant from the endpoints of this constant range.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
const APInt & getLower() const
Return the lower value for this range.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI bool isWrappedSet() const
Return true if this set wraps around the unsigned domain.
const APInt & getUpper() const
Return the upper value for this range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
This is an important base class in LLVM.
A parsed version of the target data layout string in and methods for querying it.
iterator find(const_arg_type_t< KeyT > Val)
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Represents overflowing add operations.
Represents an integer addition.
Represents a logical and.
CmpInst::Predicate getCond() const
Register getLHSReg() const
Register getRHSReg() const
Represents any generic load, including sign/zero extending variants.
Register getDstReg() const
Get the definition register of the loaded value.
Register getCarryOutReg() const
Register getRHSReg() const
Register getLHSReg() const
Register getLHSReg() const
Register getRHSReg() const
Represents a G_BUILD_VECTOR.
Register getSrcReg() const
Represents a G_CONCAT_VECTORS.
Abstract class that contains various methods for clients to notify about changes.
Simple wrapper observer that takes several observers, and calls each one for each event.
Represents any type of generic load or store.
Register getPointerReg() const
Get the source register of the pointer value.
Represents a logical binary operation.
MachineMemOperand & getMMO() const
Get the MachineMemOperand on this instruction.
bool isAtomic() const
Returns true if the attached MachineMemOperand has the atomic flag set.
LocationSize getMemSizeInBits() const
Returns the size in bits of the memory access.
Register getSourceReg(unsigned I) const
Returns the I'th source register.
unsigned getNumSources() const
Returns the number of source registers.
Represents overflowing sub operations.
Represents an integer subtraction.
Represents a G_UNMERGE_VALUES.
unsigned getNumDefs() const
Returns the number of def registers.
Register getSourceReg() const
Get the unmerge source register.
Register getReg(unsigned Idx) const
Access the Idx'th operand as a register and return it.
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
constexpr bool isScalableVector() const
Returns true if the LLT is a scalable vector.
constexpr unsigned getScalarSizeInBits() const
constexpr bool isScalar() const
constexpr LLT changeElementType(LLT NewEltTy) const
If this type is a vector, return a vector with the same number of elements but the new element type.
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
LLT getScalarType() const
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isValid() const
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
constexpr bool isByteSized() const
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr bool isPointer() const
constexpr ElementCount getElementCount() const
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
constexpr bool isPointerOrPointerVector() const
constexpr bool isFixedVector() const
Returns true if the LLT is a fixed vector.
static LLT integer(unsigned SizeInBits)
constexpr TypeSize getSizeInBytes() const
Returns the total size of the type in bytes, i.e.
LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
LLT changeElementSize(unsigned NewEltSize) const
If this type is a vector, return a vector with the same number of elements but the new element size.
This is an important class for using LLVM in a threaded context.
LLVM_ABI LegalizeResult lowerMemCpyFamily(MachineInstr &MI, Register Dst, Register Src, uint64_t KnownLen, Align Alignment, bool DstAlignCanChange, ArrayRef< LLT > MemOps)
@ Legalized
Instruction has been legalized and the MachineFunction changed.
LLVM_ABI Register getVectorElementPointer(Register VecPtr, LLT VecTy, Register Index)
Get a pointer to vector element Index located in memory for a vector of type VecTy starting at a base...
TypeSize getValue() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
LLVM_ABI iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
const DataLayout & getDataLayout() const
Return the DataLayout attached to the Module associated to this MF.
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
Helper class to build MachineInstr.
const TargetInstrInfo & getTII()
MachineInstrBuilder buildSub(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1, std::optional< unsigned > Flags=std::nullopt)
Build and insert Res = G_SUB Op0, Op1.
MachineInstrBuilder buildCTLZ(const DstOp &Dst, const SrcOp &Src0)
Build and insert Res = G_CTLZ Op0, Src0.
MachineFunction & getMF()
Getter for the function we currently build.
MachineRegisterInfo * getMRI()
Getter for MRI.
virtual MachineInstrBuilder buildConstant(const DstOp &Res, const ConstantInt &Val)
Build and insert Res = G_CONSTANT Val.
Register getReg(unsigned Idx) const
Get the register for the operand index.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
const MachineBasicBlock * getParent() const
LLVM_ABI bool isDereferenceableInvariantLoad() const
Return true if this load instruction never traps and points to a memory location whose value doesn't ...
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
mop_range uses()
Returns all operands which may be register uses.
MachineOperand * findRegisterUseOperand(Register Reg, const TargetRegisterInfo *TRI, bool isKill=false)
Wrapper for findRegisterUseOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
const MachineOperand & getOperand(unsigned i) const
uint32_t getFlags() const
Return the MI flags bitvector.
LLVM_ABI int findRegisterDefOperandIdx(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false) const
Returns the operand index that is a def of the specified register or -1 if it is not found.
LLVM_ABI MachineInstrBundleIterator< MachineInstr > eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
A description of a memory reference used in the backend.
LLT getMemoryType() const
Return the memory type of the memory reference.
unsigned getAddrSpace() const
bool isAtomic() const
Returns true if this operation has an atomic ordering requirement of unordered or higher,...
const MachinePointerInfo & getPointerInfo() const
LLVM_ABI Align getAlign() const
Return the minimum known alignment in bytes of the actual memory reference.
LocationSize getSizeInBits() const
Return the size in bits of the memory reference.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
void setMBB(MachineBasicBlock *MBB)
void setPredicate(unsigned Predicate)
Register getReg() const
getReg - Returns the register number.
unsigned getPredicate() const
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
bool use_nodbg_empty(Register RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register.
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
use_instr_nodbg_iterator use_instr_nodbg_begin(Register RegNo) const
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(Register Reg) const
static use_instr_nodbg_iterator use_instr_nodbg_end()
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
This class implements the register bank concept.
Wrapper class representing virtual and physical registers.
constexpr bool isValid() const
size_type size() const
Determine the number of elements in the SetVector.
size_type count(const_arg_type key) const
Count the number of elements of a given key in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
This is a 'bitvector' (really, a variable-sized bit array), optimized for the case when the array is ...
bool all() const
Returns true if all bits are set.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
virtual LLVM_READONLY LLT getPreferredShiftAmountTy(LLT ShiftValueTy) const
Return the preferred type to use for a shift opcode, given the shifted amount type is ShiftValueTy.
bool isBeneficialToExpandPowI(int64_t Exponent, bool OptForSize) const
Return true if it is beneficial to expand an @llvm.powi.
virtual bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty, unsigned AddrSpace, Instruction *I=nullptr) const
Return true if the addressing mode represented by AM is legal for this target, for a load/store of th...
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
virtual bool isDesirableToCommuteWithShift(const SDNode *N, CombineLevel Level) const
Return true if it is profitable to move this shift by a constant amount through its operand,...
virtual unsigned combineRepeatedFPDivisors() const
Indicate whether this target prefers to combine FDIVs with the same divisor.
virtual const TargetLowering * getTargetLowering() const
The instances of the Type class are immutable: once they are created, they are never changed.
A Use represents the edge between a Value definition and its users.
constexpr bool isKnownMultipleOf(ScalarTy RHS) const
This function tells the caller whether the element count is known at compile time to be a multiple of...
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ FewerElements
The (vector) operation should be implemented by splitting it into sub-vectors where the operation is ...
@ Legal
The operation is expected to be selectable directly by the target, and no transformation is necessary...
@ WidenScalar
The operation should be implemented in terms of a wider scalar base-type.
@ Custom
The target wants to do something special with this combination of operand and type.
operand_type_match m_Reg()
SpecificConstantMatch m_SpecificICst(const APInt &RequestedValue)
Matches a constant equal to RequestedValue.
GInstrBind< GBuildVector > m_GBuildVector(GBuildVector *&Inst)
GCstAndRegMatch m_GCst(std::optional< ValueAndVReg > &ValReg)
LoadOp_match< GLoad, PtrP > m_GLoad(const PtrP &Ptr)
MIFlagsRef m_MIFlags(uint32_t &Flags)
operand_type_match m_Pred()
BinaryOp_match< LHS, RHS, TargetOpcode::G_UMIN, true > m_GUMin(const LHS &L, const RHS &R)
UnaryOp_match< SrcTy, TargetOpcode::G_ZEXT > m_GZExt(const SrcTy &Src)
BinaryOp_match< LHS, RHS, TargetOpcode::G_XOR, true > m_GXor(const LHS &L, const RHS &R)
UnaryOp_match< SrcTy, TargetOpcode::G_SEXT > m_GSExt(const SrcTy &Src)
UnaryOp_match< SrcTy, TargetOpcode::G_FPEXT > m_GFPExt(const SrcTy &Src)
ConstantMatch< APInt > m_ICst(APInt &Cst)
BinaryOp_match< LHS, RHS, TargetOpcode::G_ADD, true > m_GAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, TargetOpcode::G_OR, true > m_GOr(const LHS &L, const RHS &R)
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
ICstOrSplatMatch< APInt > m_ICstOrSplat(APInt &Cst)
ImplicitDefMatch m_GImplicitDef()
OneNonDBGUse_match< SubPat > m_OneNonDBGUse(const SubPat &SP)
GInstrBind< GConcatVectors > m_GConcatVectors(GConcatVectors *&Inst)
GConstantBitsMatch m_GConstantOrFConstantBits(APInt &Bits)
CheckType m_SpecificType(LLT Ty)
deferred_ty< Register > m_DeferredReg(Register &R)
Similar to m_SpecificReg/Type, but the specific value to match originated from an earlier sub-pattern...
BinaryOp_match< LHS, RHS, TargetOpcode::G_UMAX, true > m_GUMax(const LHS &L, const RHS &R)
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
CompareOp_match< Pred, LHS, RHS, TargetOpcode::G_ICMP > m_GICmp(const Pred &P, const LHS &L, const RHS &R)
GInstrBind< GUnmerge > m_GUnmerge(GUnmerge *&Inst)
Instruction binders for ops with no operand-form matcher (constant-immediate or variadic-source ops).
MMORef m_MMO(const MachineMemOperand *&MMO)
BinaryOp_match< LHS, RHS, TargetOpcode::G_SUB > m_GSub(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, TargetOpcode::G_ASHR, false > m_GAShr(const LHS &L, const RHS &R)
TernaryOp_match< Src0Ty, Src1Ty, Src2Ty, TargetOpcode::G_SELECT > m_GISelect(const Src0Ty &Src0, const Src1Ty &Src1, const Src2Ty &Src2)
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
BinaryOp_match< LHS, RHS, TargetOpcode::G_PTR_ADD, false > m_GPtrAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, TargetOpcode::G_SHL, false > m_GShl(const LHS &L, const RHS &R)
Or< Preds... > m_any_of(Preds &&... preds)
SpecificConstantOrSplatMatch m_SpecificICstOrSplat(const APInt &RequestedValue)
Matches a RequestedValue constant or a constant splat of RequestedValue.
BinaryOp_match< LHS, RHS, TargetOpcode::G_AND, true > m_GAnd(const LHS &L, const RHS &R)
UnaryOp_match< SrcTy, TargetOpcode::G_BITCAST > m_GBitcast(const SrcTy &Src)
BinaryOp_match< LHS, RHS, TargetOpcode::G_BUILD_VECTOR_TRUNC, false > m_GBuildVectorTrunc(const LHS &L, const RHS &R)
bind_ty< MachineInstr * > m_MInstr(MachineInstr *&MI)
UnaryOp_match< SrcTy, TargetOpcode::G_FNEG > m_GFNeg(const SrcTy &Src)
CompareOp_match< Pred, LHS, RHS, TargetOpcode::G_ICMP, true > m_c_GICmp(const Pred &P, const LHS &L, const RHS &R)
G_ICMP matcher that also matches commuted compares.
LoadOp_match< GAnyLoad, PtrP > m_GAnyLoad(const PtrP &Ptr)
TernaryOp_match< Src0Ty, Src1Ty, Src2Ty, TargetOpcode::G_INSERT_VECTOR_ELT > m_GInsertVecElt(const Src0Ty &Src0, const Src1Ty &Src1, const Src2Ty &Src2)
GFCstOrSplatGFCstMatch m_GFCstOrSplat(std::optional< FPValueAndVReg > &FPValReg)
And< Preds... > m_all_of(Preds &&... preds)
BinaryOp_match< LHS, RHS, TargetOpcode::G_SMIN, true > m_GSMin(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, TargetOpcode::G_LSHR, false > m_GLShr(const LHS &L, const RHS &R)
UnaryOp_match< SrcTy, TargetOpcode::G_ANYEXT > m_GAnyExt(const SrcTy &Src)
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
BinaryOp_match< LHS, RHS, TargetOpcode::G_FMUL, true > m_GFMul(const LHS &L, const RHS &R)
UnaryOp_match< SrcTy, TargetOpcode::G_TRUNC > m_GTrunc(const SrcTy &Src)
BinaryOp_match< LHS, RHS, TargetOpcode::G_SMAX, true > m_GSMax(const LHS &L, const RHS &R)
CompareOp_match< Pred, LHS, RHS, TargetOpcode::G_FCMP > m_GFCmp(const Pred &P, const LHS &L, const RHS &R)
auto m_BinOp()
Match an arbitrary binary operation and ignore it.
Not(const Pred &P) -> Not< Pred >
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
LLVM_ABI std::optional< APInt > isConstantOrConstantSplatVector(Register Def, const MachineRegisterInfo &MRI)
Determines if Def defines a constant integer or a splat vector of constant integers.
LLVM_ABI bool isBuildVectorAllZeros(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndef=false)
Return true if the specified instruction is a G_BUILD_VECTOR or G_BUILD_VECTOR_TRUNC where all of the...
LLVM_ABI Type * getTypeForLLT(LLT Ty, LLVMContext &C)
Get the type back from LLT.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI MachineInstr * getOpcodeDef(unsigned Opcode, Register Reg, const MachineRegisterInfo &MRI)
See if Reg is defined by an single def instruction that is Opcode.
static double log2(double V)
LLVM_ABI std::optional< APFloat > isConstantOrConstantSplatVectorFP(Register Def, const MachineRegisterInfo &MRI)
Determines if Def defines a float constant integer or a splat vector of float constant integers.
LLVM_ABI const ConstantFP * getConstantFPVRegVal(Register VReg, const MachineRegisterInfo &MRI)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI std::optional< APInt > getIConstantVRegVal(Register VReg, const MachineRegisterInfo &MRI)
If VReg is defined by a G_CONSTANT, return the corresponding value.
LLVM_ABI std::optional< APInt > getIConstantSplatVal(const Register Reg, const MachineRegisterInfo &MRI)
LLVM_ABI bool isAllOnesOrAllOnesSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant -1 integer or a splatted vector of a constant -1 integer (with...
@ Known
Known to have no common set bits.
@ Undef
Value of the register doesn't matter.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
std::function< void(MachineIRBuilder &)> BuildFnTy
LLVM_ABI const llvm::fltSemantics & getFltSemanticForLLT(LLT Ty)
Get the appropriate floating point arithmetic semantic based on the bit size of the given scalar LLT.
LLVM_ABI std::optional< APFloat > ConstantFoldFPBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
@ Load
The value being inserted comes from a load (InsertElement only).
LLVM_ABI bool matchUnaryPredicate(const MachineRegisterInfo &MRI, Register Reg, llvm::function_ref< bool(const Constant *ConstVal)> Match, bool AllowUndefs=false)
Attempt to match a unary predicate against a scalar/splat constant or every element of a constant G_B...
LLVM_ABI MVT getMVTForLLT(LLT Ty)
Get a rough equivalent of an MVT for a given LLT.
LLVM_ABI bool isNullOrNullSplat(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowUndefs=false)
Return true if the value is a constant 0 integer or a splatted vector of a constant 0 integer (with n...
LLVM_ABI MachineInstr * getDefIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, folding away any trivial copies.
LLVM_ABI bool isConstTrueVal(const TargetLowering &TLI, int64_t Val, bool IsVector, bool IsFP)
Returns true if given the TargetLowering's boolean contents information, the value Val contains a tru...
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< APInt > ConstantFoldBinOp(unsigned Opcode, const Register Op1, const Register Op2, const MachineRegisterInfo &MRI)
constexpr bool has_single_bit(T Value) noexcept
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI const APInt & getIConstantFromReg(Register VReg, const MachineRegisterInfo &MRI)
VReg is defined by a G_CONSTANT, return the corresponding value.
LLVM_ABI bool isConstantOrConstantVector(const MachineInstr &MI, const MachineRegisterInfo &MRI, bool AllowFP=true, bool AllowOpaqueConstants=true)
Return true if the specified instruction is known to be a constant, or a vector of constants.
SmallVector< std::function< void(MachineInstrBuilder &)>, 4 > OperandBuildSteps
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
LLVM_ABI bool canReplaceReg(Register DstReg, Register SrcReg, MachineRegisterInfo &MRI)
Check if DstReg can be replaced with SrcReg depending on the register constraints.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
std::tuple< Register, Register, uint64_t, Align, bool, std::vector< LLT > > MemCpyFamilyLoweringInfo
constexpr bool isMask_64(uint64_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
auto instructionsWithoutDebug(IterT It, IterT End, bool SkipPseudoOp=true)
Construct a range iterator which begins at It and moves forwards until End is reached,...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI std::optional< FPValueAndVReg > getFConstantSplat(Register VReg, const MachineRegisterInfo &MRI, bool AllowUndef=true)
Returns a floating point scalar constant of a build vector splat if it exists.
LLVM_ABI EVT getApproximateEVTForLLT(LLT Ty, LLVMContext &Ctx)
LLVM_ABI std::optional< APInt > ConstantFoldCastOp(unsigned Opcode, LLT DstTy, const Register Op0, const MachineRegisterInfo &MRI)
LLVM_ABI bool canLowerMemCpyFamily(const MachineInstr &MI, const MachineRegisterInfo &MRI, unsigned MaxLen, Register &Dst, Register &Src, uint64_t &KnownLen, Align &Alignment, bool &DstAlignCanChange, std::vector< LLT > &MemOps)
Matcher for memcpy-like instructions.
LLVM_ABI unsigned getInverseGMinMaxOpcode(unsigned MinMaxOpc)
Returns the inverse opcode of MinMaxOpc, which is a generic min/max opcode like G_SMIN.
@ Xor
Bitwise or logical XOR of integers.
@ And
Bitwise or logical AND of integers.
@ Sub
Subtraction of integers.
@ Fast
Assign the register banks as fast as possible (default).
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
LLVM_ABI std::optional< FPValueAndVReg > getFConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_FCONSTANT returns it...
constexpr unsigned BitWidth
LLVM_ABI int64_t getICmpTrueVal(const TargetLowering &TLI, bool IsVector, bool IsFP)
Returns an integer representing true, as defined by the TargetBooleanContents.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI std::optional< ValueAndVReg > getIConstantVRegValWithLookThrough(Register VReg, const MachineRegisterInfo &MRI, bool LookThroughInstrs=true)
If VReg is defined by a statically evaluable chain of instructions rooted on a G_CONSTANT returns its...
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
LLVM_ABI std::optional< DefinitionAndSourceRegister > getDefSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the def instruction for Reg, and underlying value Register folding away any copies.
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
LLVM_ABI SmallVector< APInt > ConstantFoldUnaryIntOp(unsigned Opcode, LLT DstTy, Register Src, const MachineRegisterInfo &MRI)
Tries to constant fold a unary integer operation (G_CTLZ, G_CTTZ, G_CTPOP and their _ZERO_POISON vari...
LLVM_ABI Register getSrcRegIgnoringCopies(Register Reg, const MachineRegisterInfo &MRI)
Find the source register for Reg, folding away any trivial copies.
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
unsigned getFCmpCode(CmpInst::Predicate CC)
Similar to getICmpCode but for FCmpInst.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Simple struct used to hold a Register value and the instruction which defines it.
SmallVector< InstructionBuildSteps, 2 > InstrsToBuild
Describes instructions to be built during a combine.
bool isNonNegative() const
Returns true if this value is known to be non-negative.
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
bool isUnknown() const
Returns true if we don't know any bits.
unsigned getBitWidth() const
Get the bit width of this value.
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
bool isNegative() const
Returns true if this value is known to be negative.
The LegalityQuery object bundles together all the information that's needed to decide whether a given...
This class contains a discriminated union of information about pointers in memory operands,...
LLVM_ABI unsigned getAddrSpace() const
Return the LLVM IR address space number that this pointer points into.
MachinePointerInfo getWithOffset(int64_t O) const
const RegisterBank * Bank
Register LogicNonShiftReg
Magic data for optimising signed division by a constant.
unsigned ShiftAmount
shift amount
static LLVM_ABI SignedDivisionByConstantInfo get(const APInt &D)
Calculate the magic numbers required to implement a signed integer division by a constant as a sequen...
This represents an addressing mode of: BaseGV + BaseOffs + BaseReg + Scale*ScaleReg + ScalableOffset*...
Magic data for optimising unsigned division by a constant.
unsigned PreShift
pre-shift amount
unsigned PostShift
post-shift amount
static LLVM_ABI UnsignedDivisionByConstantInfo get(const APInt &D, unsigned LeadingZeros=0, bool AllowEvenDivisorOptimization=true, bool AllowWidenOptimization=false)
Calculate the magic numbers required to implement an unsigned integer division by a constant as a seq...