88#define DEBUG_TYPE "dagcombine"
90STATISTIC(NodesCombined ,
"Number of dag nodes combined");
91STATISTIC(PreIndexedNodes ,
"Number of pre-indexed nodes created");
92STATISTIC(PostIndexedNodes,
"Number of post-indexed nodes created");
93STATISTIC(OpsNarrowed ,
"Number of load/op/store narrowed");
94STATISTIC(LdStFP2Int ,
"Number of fp load/store pairs transformed to int");
96STATISTIC(NumFPLogicOpsConv,
"Number of logic ops converted to fp ops");
99 "Controls whether a DAG combine is performed for a node");
103 cl::desc(
"Enable DAG combiner's use of IR alias analysis"));
107 cl::desc(
"Enable DAG combiner's use of TBAA"));
112 cl::desc(
"Only use DAG-combiner alias analysis in this"
120 cl::desc(
"Bypass the profitability model of load slicing"),
125 cl::desc(
"DAG combiner may split indexing from loads"));
129 cl::desc(
"DAG combiner enable merging multiple stores "
130 "into a wider store"));
134 cl::desc(
"Limit the number of operands to inline for Token Factors"));
138 cl::desc(
"Limit the number of times for the same StoreNode and RootNode "
139 "to bail out in store merging dependence check"));
143 cl::desc(
"DAG combiner enable reducing the width of load/op/store "
146 "combiner-reduce-load-op-store-width-force-narrowing-profitable",
148 cl::desc(
"DAG combiner force override the narrowing profitable check when "
149 "reducing the width of load/op/store sequences"));
153 cl::desc(
"DAG combiner enable load/<replace bytes>/store with "
154 "a narrower store"));
158 cl::desc(
"DAG combiner nodes consistently processed in topological order"));
162 cl::desc(
"Disable the DAG combiner"));
172 bool LegalDAG =
false;
173 bool LegalOperations =
false;
174 bool LegalTypes =
false;
176 bool DisableGenericCombines;
212 void AddUsersToWorklist(
SDNode *
N) {
218 void AddToWorklistWithUsers(SDNode *
N) {
219 AddUsersToWorklist(
N);
226 void clearAddedDanglingWorklistEntries() {
228 while (!PruningList.empty()) {
229 auto *
N = PruningList.pop_back_val();
231 recursivelyDeleteUnusedNodes(
N);
235 SDNode *getNextWorklistEntry() {
237 clearAddedDanglingWorklistEntries();
241 while (!
N && !Worklist.empty()) {
242 N = Worklist.pop_back_val();
246 assert(
N->getCombinerWorklistIndex() >= 0 &&
247 "Found a worklist entry without a corresponding map entry!");
249 N->setCombinerWorklistIndex(-2);
259 : DAG(
D), TLI(
D.getTargetLoweringInfo()),
260 STI(
D.getSubtarget().getSelectionDAGInfo()), OptLevel(OL),
262 ForCodeSize = DAG.shouldOptForSize();
263 DisableGenericCombines =
267 void ConsiderForPruning(SDNode *
N) {
269 PruningList.insert(
N);
274 void AddToWorklist(SDNode *
N,
bool IsCandidateForPruning =
true,
275 bool SkipIfCombinedBefore =
false) {
277 "Deleted Node added to Worklist");
284 if (SkipIfCombinedBefore &&
N->getCombinerWorklistIndex() == -2)
287 if (IsCandidateForPruning)
288 ConsiderForPruning(
N);
290 if (
N->getCombinerWorklistIndex() < 0) {
291 N->setCombinerWorklistIndex(Worklist.size());
292 Worklist.push_back(
N);
297 void removeFromWorklist(SDNode *
N) {
298 PruningList.remove(
N);
299 StoreRootCountMap.erase(
N);
301 int WorklistIndex =
N->getCombinerWorklistIndex();
305 if (WorklistIndex < 0)
309 Worklist[WorklistIndex] =
nullptr;
310 N->setCombinerWorklistIndex(-1);
313 void deleteAndRecombine(SDNode *
N);
314 bool recursivelyDeleteUnusedNodes(SDNode *
N);
322 return CombineTo(
N, &Res, 1, AddTo);
329 return CombineTo(
N, To, 2, AddTo);
332 SDValue CombineTo(SDNode *
N, SmallVectorImpl<SDValue> *To,
334 return CombineTo(
N, To->
data(), To->
size(), AddTo);
337 void CommitTargetLoweringOpt(
const TargetLowering::TargetLoweringOpt &TLO);
344 unsigned BitWidth =
Op.getScalarValueSizeInBits();
346 return SimplifyDemandedBits(
Op, DemandedBits);
349 bool SimplifyDemandedBits(
SDValue Op,
const APInt &DemandedBits) {
350 EVT VT =
Op.getValueType();
354 return SimplifyDemandedBits(
Op, DemandedBits, DemandedElts,
false);
360 bool SimplifyDemandedVectorElts(
SDValue Op) {
362 if (
Op.getValueType().isScalableVector())
365 unsigned NumElts =
Op.getValueType().getVectorNumElements();
367 return SimplifyDemandedVectorElts(
Op, DemandedElts);
370 bool SimplifyDemandedBits(
SDValue Op,
const APInt &DemandedBits,
371 const APInt &DemandedElts,
372 bool AssumeSingleUse =
false);
373 bool SimplifyDemandedVectorElts(
SDValue Op,
const APInt &DemandedElts,
374 bool AssumeSingleUse =
false);
376 bool CombineToPreIndexedLoadStore(SDNode *
N);
377 bool CombineToPostIndexedLoadStore(SDNode *
N);
378 SDValue SplitIndexingFromLoad(LoadSDNode *LD);
379 bool SliceUpLoad(SDNode *
N);
385 StoreSDNode *getUniqueStoreFeeding(LoadSDNode *LD, int64_t &
Offset);
387 SDValue ForwardStoreValueToDirectLoad(LoadSDNode *LD);
388 bool getTruncatedStoreValue(StoreSDNode *ST,
SDValue &Val);
389 bool extendLoadedValueToExtension(LoadSDNode *LD,
SDValue &Val);
391 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
400 SDValue foldShiftToAvg(SDNode *
N,
const SDLoc &
DL);
402 SDValue foldBitwiseOpWithNeg(SDNode *
N,
const SDLoc &
DL, EVT VT);
420 SDValue visitTokenFactor(SDNode *
N);
421 SDValue visitMERGE_VALUES(SDNode *
N);
425 SDNode *LocReference);
436 SDValue visitUADDO_CARRY(SDNode *
N);
437 SDValue visitSADDO_CARRY(SDNode *
N);
443 SDValue visitUSUBO_CARRY(SDNode *
N);
444 SDValue visitSSUBO_CARRY(SDNode *
N);
466 SDValue SimplifyVCastOp(SDNode *
N,
const SDLoc &
DL);
467 SDValue SimplifyVBinOp(SDNode *
N,
const SDLoc &
DL);
471 SDValue visitFunnelShift(SDNode *
N);
475 SDValue visitABS_MIN_POISON(SDNode *
N);
482 SDValue visitCTLZ_ZERO_POISON(SDNode *
N);
484 SDValue visitCTTZ_ZERO_POISON(SDNode *
N);
492 SDValue visitSIGN_EXTEND(SDNode *
N);
493 SDValue visitZERO_EXTEND(SDNode *
N);
496 SDValue visitAssertAlign(SDNode *
N);
498 SDValue visitSIGN_EXTEND_INREG(SDNode *
N);
499 SDValue visitEXTEND_VECTOR_INREG(SDNode *
N);
501 SDValue visitTRUNCATE_USAT_U(SDNode *
N);
506 SDValue visitSTRICT_FADD(SDNode *
N);
517 SDValue visitFCANONICALIZE(SDNode *
N);
537 SDValue replaceStoreOfFPConstant(StoreSDNode *ST);
538 SDValue replaceStoreOfInsertLoad(StoreSDNode *ST);
540 bool refineExtractVectorEltIntoMultipleNarrowExtractVectorElts(SDNode *
N);
541 SDValue combineStoreConcatTruncVector(StoreSDNode *
N);
543 SDValue visitATOMIC_STORE(SDNode *
N);
544 SDValue visitLIFETIME_END(SDNode *
N);
545 SDValue visitINSERT_VECTOR_ELT(SDNode *
N);
546 SDValue visitEXTRACT_VECTOR_ELT(SDNode *
N);
547 SDValue visitBUILD_VECTOR(SDNode *
N);
548 SDValue visitCONCAT_VECTORS(SDNode *
N);
549 SDValue visitVECTOR_INTERLEAVE(SDNode *
N);
550 SDValue visitEXTRACT_SUBVECTOR(SDNode *
N);
551 SDValue visitVECTOR_SHUFFLE(SDNode *
N);
552 SDValue visitSCALAR_TO_VECTOR(SDNode *
N);
553 SDValue visitINSERT_SUBVECTOR(SDNode *
N);
554 SDValue visitVECTOR_COMPRESS(SDNode *
N);
560 SDValue visitPARTIAL_REDUCE_MLA(SDNode *
N);
563 SDValue visitVP_STRIDED_LOAD(SDNode *
N);
564 SDValue visitVP_STRIDED_STORE(SDNode *
N);
571 SDValue visitGET_FPENV_MEM(SDNode *
N);
572 SDValue visitSET_FPENV_MEM(SDNode *
N);
574 SDValue visitFADDForFMACombine(SDNode *
N);
575 SDValue visitFSUBForFMACombine(SDNode *
N);
576 SDValue visitFMULForFMADistributiveCombine(SDNode *
N);
578 SDValue XformToShuffleWithZero(SDNode *
N);
579 bool reassociationCanBreakAddressingModePattern(
unsigned Opc,
585 SDValue N1, SDNodeFlags Flags);
587 SDValue N1, SDNodeFlags Flags);
588 SDValue reassociateReduction(
unsigned RedOpc,
unsigned Opc,
const SDLoc &
DL,
590 SDNodeFlags Flags = SDNodeFlags());
592 SDValue visitShiftByConstant(SDNode *
N);
594 SDValue foldSelectOfConstants(SDNode *
N);
595 SDValue foldVSelectOfConstants(SDNode *
N);
596 SDValue foldBinOpIntoSelect(SDNode *BO);
598 SDValue hoistLogicOpWithSameOpcodeHands(SDNode *
N);
602 bool NotExtCompare =
false);
603 SDValue convertSelectOfFPConstantsToLoadOffset(
606 SDValue foldSignChangeInBitcast(SDNode *
N);
609 SDValue foldSelectOfBinops(SDNode *
N);
613 SDValue foldSubToUSubSat(EVT DstVT, SDNode *
N,
const SDLoc &
DL);
614 SDValue foldABSToABD(SDNode *
N,
const SDLoc &
DL);
619 SDValue unfoldMaskedMerge(SDNode *
N);
620 SDValue unfoldExtremeBitClearingToShifts(SDNode *
N);
622 const SDLoc &
DL,
bool foldBooleans);
626 SDValue &CC,
bool MatchStrict =
false)
const;
627 bool isOneUseSetCC(
SDValue N)
const;
629 SDValue foldAddToAvg(SDNode *
N,
const SDLoc &
DL);
630 SDValue foldSubToAvg(SDNode *
N,
const SDLoc &
DL);
634 SDValue SimplifyNodeWithTwoResults(SDNode *
N,
unsigned LoOp,
636 SDValue CombineConsecutiveLoads(SDNode *
N, EVT VT);
637 SDValue foldBitcastedFPLogic(SDNode *
N, SelectionDAG &DAG,
638 const TargetLowering &TLI);
639 SDValue foldPartialReduceMLAMulOp(SDNode *
N);
640 SDValue foldPartialReduceAdd(SDNode *
N);
643 SDValue CombineZExtLogicopShiftLoad(SDNode *
N);
644 SDValue combineRepeatedFPDivisors(SDNode *
N);
645 SDValue combineFMulOrFDivWithIntPow2(SDNode *
N);
646 SDValue replaceShuffleOfInsert(ShuffleVectorSDNode *Shuf);
647 SDValue mergeInsertEltWithShuffle(SDNode *
N,
unsigned InsIndex);
648 SDValue combineInsertEltToShuffle(SDNode *
N,
unsigned InsIndex);
649 SDValue combineInsertEltToLoad(SDNode *
N,
unsigned InsIndex);
659 bool KnownNeverZero =
false,
660 bool InexpensiveOnly =
false,
661 std::optional<EVT> OutVT = std::nullopt);
671 bool DemandHighBits =
true);
675 bool HasPos,
unsigned PosOpcode,
676 unsigned NegOpcode,
const SDLoc &
DL);
679 bool HasPos,
unsigned PosOpcode,
680 unsigned NegOpcode,
const SDLoc &
DL);
683 SDValue MatchLoadCombine(SDNode *
N);
684 SDValue mergeTruncStores(StoreSDNode *
N);
686 SDValue ReduceLoadOpStoreWidth(SDNode *
N);
688 SDValue TransformFPLoadStorePair(SDNode *
N);
689 SDValue convertBuildVecExtToExt(SDNode *
N);
690 SDValue convertBuildVecZextToBuildVecWithZeros(SDNode *
N);
691 SDValue reduceBuildVecExtToExtBuildVec(SDNode *
N);
692 SDValue reduceBuildVecTruncToBitCast(SDNode *
N);
693 SDValue reduceBuildVecToShuffle(SDNode *
N);
694 SDValue createBuildVecShuffle(
const SDLoc &
DL, SDNode *
N,
695 ArrayRef<int> VectorMask,
SDValue VecIn1,
696 SDValue VecIn2,
unsigned LeftIdx,
698 SDValue matchVSelectOpSizesWithSetCC(SDNode *Cast);
702 void GatherAllAliases(SDNode *
N,
SDValue OriginalChain,
703 SmallVectorImpl<SDValue> &Aliases);
706 bool mayAlias(SDNode *Op0, SDNode *Op1)
const;
718 bool findBetterNeighborChains(StoreSDNode *St);
722 bool parallelizeChainedStores(StoreSDNode *St);
728 LSBaseSDNode *MemNode;
731 int64_t OffsetFromBase;
733 MemOpLink(LSBaseSDNode *
N, int64_t
Offset)
734 : MemNode(
N), OffsetFromBase(
Offset) {}
739 StoreSource getStoreSource(
SDValue StoreVal) {
743 return StoreSource::Constant;
747 return StoreSource::Constant;
748 return StoreSource::Unknown;
751 return StoreSource::Extract;
753 return StoreSource::Load;
755 return StoreSource::Unknown;
763 bool isMulAddWithConstProfitable(SDNode *MulNode,
SDValue AddNode,
769 bool isAndLoadExtLoad(ConstantSDNode *AndC, LoadSDNode *LoadN,
770 EVT LoadResultTy, EVT &ExtVT);
775 EVT &MemVT,
unsigned ShAmt = 0);
778 bool SearchForAndLoads(SDNode *
N, SmallVectorImpl<LoadSDNode*> &Loads,
779 SmallPtrSetImpl<SDNode*> &NodesWithConsts,
780 ConstantSDNode *Mask, SDNode *&NodeToMask);
783 bool BackwardsPropagateMask(SDNode *
N);
787 SDValue getMergeStoreChains(SmallVectorImpl<MemOpLink> &StoreNodes,
799 bool mergeStoresOfConstantsOrVecElts(SmallVectorImpl<MemOpLink> &StoreNodes,
800 EVT MemVT,
unsigned NumStores,
801 bool IsConstantSrc,
bool UseVector,
807 SDNode *getStoreMergeCandidates(StoreSDNode *St,
808 SmallVectorImpl<MemOpLink> &StoreNodes);
814 bool checkMergeStoreCandidatesForDependencies(
815 SmallVectorImpl<MemOpLink> &StoreNodes,
unsigned NumStores,
820 bool hasCallInLdStChain(StoreSDNode *St, LoadSDNode *Ld);
825 unsigned getConsecutiveStores(SmallVectorImpl<MemOpLink> &StoreNodes,
826 int64_t ElementSizeBytes)
const;
830 bool tryStoreMergeOfConstants(SmallVectorImpl<MemOpLink> &StoreNodes,
831 unsigned NumConsecutiveStores,
832 EVT MemVT, SDNode *Root,
bool AllowVectors);
838 bool tryStoreMergeOfExtracts(SmallVectorImpl<MemOpLink> &StoreNodes,
839 unsigned NumConsecutiveStores, EVT MemVT,
844 bool tryStoreMergeOfLoads(SmallVectorImpl<MemOpLink> &StoreNodes,
845 unsigned NumConsecutiveStores, EVT MemVT,
846 SDNode *Root,
bool AllowVectors,
847 bool IsNonTemporalStore,
bool IsNonTemporalLoad);
852 bool mergeConsecutiveStores(StoreSDNode *St);
860 SDValue distributeTruncateThroughAnd(SDNode *
N);
866 bool hasOperation(
unsigned Opcode, EVT VT) {
867 return TLI.isOperationLegalOrCustom(Opcode, VT, LegalOperations);
870 bool hasUMin(EVT VT)
const {
871 auto LK = TLI.getTypeConversion(*DAG.getContext(), VT);
874 TLI.isOperationLegalOrCustom(
ISD::UMIN, LK.second);
881 SelectionDAG &getDAG()
const {
return DAG; }
884 EVT getShiftAmountTy(EVT LHSTy) {
885 return TLI.getShiftAmountTy(LHSTy, DAG.getDataLayout());
890 bool isTypeLegal(
const EVT &VT) {
891 if (!LegalTypes)
return true;
892 return TLI.isTypeLegal(VT);
896 EVT getSetCCResultType(EVT VT)
const {
897 return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
900 void ExtendSetCCUses(
const SmallVectorImpl<SDNode *> &SetCCs,
911 explicit WorklistRemover(DAGCombiner &dc)
912 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
914 void NodeDeleted(SDNode *
N, SDNode *
E)
override {
915 DC.removeFromWorklist(
N);
923 explicit WorklistInserter(DAGCombiner &dc)
924 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
928 void NodeInserted(SDNode *
N)
override { DC.ConsiderForPruning(
N); }
938 ((DAGCombiner*)
DC)->AddToWorklist(
N);
943 return ((DAGCombiner*)
DC)->CombineTo(
N, &To[0], To.
size(), AddTo);
948 return ((DAGCombiner*)
DC)->CombineTo(
N, Res, AddTo);
953 return ((DAGCombiner*)
DC)->CombineTo(
N, Res0, Res1, AddTo);
958 return ((DAGCombiner*)
DC)->recursivelyDeleteUnusedNodes(
N);
963 return ((DAGCombiner*)
DC)->CommitTargetLoweringOpt(TLO);
970void DAGCombiner::deleteAndRecombine(
SDNode *
N) {
971 removeFromWorklist(
N);
979 if (
Op->hasOneUse() ||
Op->getNumValues() > 1)
980 AddToWorklist(
Op.getNode());
989 unsigned Bits =
Offset + std::max(
LHS.getBitWidth(),
RHS.getBitWidth());
1000 SDValue &CC,
bool MatchStrict)
const {
1002 LHS =
N.getOperand(0);
1003 RHS =
N.getOperand(1);
1011 LHS =
N.getOperand(1);
1012 RHS =
N.getOperand(2);
1025 LHS =
N.getOperand(0);
1026 RHS =
N.getOperand(1);
1034bool DAGCombiner::isOneUseSetCC(
SDValue N)
const {
1036 if (isSetCCEquivalent(
N, N0, N1, N2) &&
N->hasOneUse())
1048 MaskForTy = 0xFFULL;
1051 MaskForTy = 0xFFFFULL;
1054 MaskForTy = 0xFFFFFFFFULL;
1072 bool AllowTruncation =
false) {
1074 return !(Const->isOpaque() && NoOpaques);
1077 unsigned BitWidth =
N.getScalarValueSizeInBits();
1082 if (!Const || (Const->isOpaque() && NoOpaques))
1086 if ((AllowTruncation &&
1087 Const->getAPIntValue().getActiveBits() >
BitWidth) ||
1088 (!AllowTruncation && Const->getAPIntValue().getBitWidth() !=
BitWidth))
1110bool DAGCombiner::reassociationCanBreakAddressingModePattern(
unsigned Opc,
1138 : (N1.
getOperand(0).getConstantOperandVal(0) *
1143 ScalableOffset = -ScalableOffset;
1144 if (
all_of(
N->users(), [&](SDNode *Node) {
1145 if (auto *LoadStore = dyn_cast<MemSDNode>(Node);
1146 LoadStore && LoadStore->hasUniqueMemOperand() &&
1147 LoadStore->getBasePtr().getNode() == N) {
1148 TargetLoweringBase::AddrMode AM;
1149 AM.HasBaseReg = true;
1150 AM.ScalableOffset = ScalableOffset;
1151 EVT VT = LoadStore->getMemoryVT();
1152 unsigned AS = LoadStore->getAddressSpace();
1153 Type *AccessTy = VT.getTypeForEVT(*DAG.getContext());
1154 return TLI.isLegalAddressingMode(DAG.getDataLayout(), AM, AccessTy,
1169 const APInt &C2APIntVal = C2->getAPIntValue();
1177 const APInt &C1APIntVal = C1->getAPIntValue();
1178 const APInt CombinedValueIntVal = C1APIntVal + C2APIntVal;
1181 const int64_t CombinedValue = CombinedValueIntVal.
getSExtValue();
1183 for (SDNode *Node :
N->users()) {
1190 TargetLoweringBase::AddrMode AM;
1192 AM.
BaseOffs = C2APIntVal.getSExtValue();
1194 unsigned AS =
LoadStore->getAddressSpace();
1210 for (SDNode *Node :
N->users()) {
1212 if (!LoadStore || !
LoadStore->hasUniqueMemOperand())
1217 TargetLoweringBase::AddrMode AM;
1219 AM.
BaseOffs = C2APIntVal.getSExtValue();
1221 unsigned AS =
LoadStore->getAddressSpace();
1234SDValue DAGCombiner::reassociateOpsCommutative(
unsigned Opc,
const SDLoc &
DL,
1236 SDNodeFlags Flags) {
1246 SDNodeFlags NewFlags;
1248 Flags.hasNoUnsignedWrap())
1256 return DAG.
getNode(
Opc,
DL, VT, N00, OpNode, NewFlags);
1264 return DAG.
getNode(
Opc,
DL, VT, OpNode, N01, NewFlags);
1274 if (N1 == N00 || N1 == N01)
1320 if (CC1 == CC00 && CC1 != CC01) {
1324 if (CC1 == CC01 && CC1 != CC00) {
1338 SDValue N1, SDNodeFlags Flags) {
1344 if (!
Flags.hasAllowReassociation() || !
Flags.hasNoSignedZeros())
1347 if (
SDValue Combined = reassociateOpsCommutative(
Opc,
DL, N0, N1, Flags))
1349 if (
SDValue Combined = reassociateOpsCommutative(
Opc,
DL, N1, N0, Flags))
1357SDValue DAGCombiner::reassociateReduction(
unsigned RedOpc,
unsigned Opc,
1359 SDValue N1, SDNodeFlags Flags) {
1365 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
1383 A.getValueType() ==
C.getValueType() &&
1384 hasOperation(
Opc,
A.getValueType()) &&
1392 SelectionDAG::FlagInserter FlagsInserter(
1416 X.getValueType() !=
Y.getValueType() ||
1417 !hasOperation(
Opc,
X.getValueType()) ||
1421 (!Chain->getFlags().hasAllowReassociation() ||
1425 SelectionDAG::FlagInserter FlagsInserter(
1431 if (
SDValue V = FoldReductionChain(N0, N1))
1433 if (
SDValue V = FoldReductionChain(N1, N0))
1439SDValue DAGCombiner::CombineTo(SDNode *
N,
const SDValue *To,
unsigned NumTo,
1441 assert(
N->getNumValues() == NumTo &&
"Broken CombineTo call!");
1445 dbgs() <<
" and " << NumTo - 1 <<
" other values\n");
1446 for (
unsigned i = 0, e = NumTo; i !=
e; ++i)
1448 N->getValueType(i) == To[i].getValueType()) &&
1449 "Cannot combine value to value of different type!");
1451 WorklistRemover DeadNodes(*
this);
1455 for (
unsigned i = 0, e = NumTo; i !=
e; ++i) {
1457 AddToWorklistWithUsers(To[i].
getNode());
1465 deleteAndRecombine(
N);
1470CommitTargetLoweringOpt(
const TargetLowering::TargetLoweringOpt &TLO) {
1483 recursivelyDeleteUnusedNodes(TLO.
Old.
getNode());
1488bool DAGCombiner::SimplifyDemandedBits(
SDValue Op,
const APInt &DemandedBits,
1489 const APInt &DemandedElts,
1490 bool AssumeSingleUse) {
1491 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
1498 AddToWorklist(
Op.getNode());
1500 CommitTargetLoweringOpt(TLO);
1507bool DAGCombiner::SimplifyDemandedVectorElts(
SDValue Op,
1508 const APInt &DemandedElts,
1509 bool AssumeSingleUse) {
1510 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
1511 APInt KnownUndef, KnownZero;
1513 TLO, 0, AssumeSingleUse))
1517 AddToWorklist(
Op.getNode());
1519 CommitTargetLoweringOpt(TLO);
1523void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
1525 EVT VT =
Load->getValueType(0);
1534 AddToWorklist(Trunc.
getNode());
1535 recursivelyDeleteUnusedNodes(Load);
1543 EVT MemVT =
LD->getMemoryVT();
1545 :
LD->getExtensionType();
1548 LD->getChain(),
LD->getBasePtr(),
1549 MemVT,
LD->getMemOperand());
1552 unsigned Opc =
Op.getOpcode();
1556 if (
SDValue Op0 = SExtPromoteOperand(
Op.getOperand(0), PVT))
1560 if (
SDValue Op0 = ZExtPromoteOperand(
Op.getOperand(0), PVT))
1578 EVT OldVT =
Op.getValueType();
1584 AddToWorklist(NewOp.
getNode());
1587 ReplaceLoadWithPromotedLoad(
Op.getNode(), NewOp.
getNode());
1593 EVT OldVT =
Op.getValueType();
1599 AddToWorklist(NewOp.
getNode());
1602 ReplaceLoadWithPromotedLoad(
Op.getNode(), NewOp.
getNode());
1610 if (!LegalOperations)
1613 EVT VT =
Op.getValueType();
1619 unsigned Opc =
Op.getOpcode();
1627 assert(PVT != VT &&
"Don't know what type to promote to!");
1631 bool Replace0 =
false;
1633 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
1635 bool Replace1 =
false;
1637 SDValue NN1 = PromoteOperand(N1, PVT, Replace1);
1649 Replace1 &= (N0 != N1) && !N1->
hasOneUse();
1652 CombineTo(
Op.getNode(), RV);
1678 if (!LegalOperations)
1681 EVT VT =
Op.getValueType();
1687 unsigned Opc =
Op.getOpcode();
1695 assert(PVT != VT &&
"Don't know what type to promote to!");
1699 SDNodeFlags TruncFlags;
1703 N0 = SExtPromoteOperand(N0, PVT);
1705 N0 = ZExtPromoteOperand(N0, PVT);
1707 if (
Op->getFlags().hasNoUnsignedWrap()) {
1708 N0 = ZExtPromoteOperand(N0, PVT);
1710 }
else if (
Op->getFlags().hasNoSignedWrap()) {
1711 N0 = SExtPromoteOperand(N0, PVT);
1714 N0 = PromoteOperand(N0, PVT,
Replace);
1727 ReplaceLoadWithPromotedLoad(
Op.getOperand(0).getNode(), N0.
getNode());
1737 if (!LegalOperations)
1740 EVT VT =
Op.getValueType();
1746 unsigned Opc =
Op.getOpcode();
1754 assert(PVT != VT &&
"Don't know what type to promote to!");
1759 return DAG.
getNode(
Op.getOpcode(), SDLoc(
Op), VT,
Op.getOperand(0));
1764bool DAGCombiner::PromoteLoad(
SDValue Op) {
1765 if (!LegalOperations)
1771 EVT VT =
Op.getValueType();
1777 unsigned Opc =
Op.getOpcode();
1785 assert(PVT != VT &&
"Don't know what type to promote to!");
1788 SDNode *
N =
Op.getNode();
1790 EVT MemVT =
LD->getMemoryVT();
1792 :
LD->getExtensionType();
1794 LD->getChain(),
LD->getBasePtr(),
1795 MemVT,
LD->getMemOperand());
1804 AddToWorklist(
Result.getNode());
1805 recursivelyDeleteUnusedNodes(
N);
1818bool DAGCombiner::recursivelyDeleteUnusedNodes(SDNode *
N) {
1819 if (!
N->use_empty())
1822 SmallSetVector<SDNode *, 16> Nodes;
1829 if (
N->use_empty()) {
1830 for (
const SDValue &ChildN :
N->op_values())
1831 Nodes.
insert(ChildN.getNode());
1833 removeFromWorklist(
N);
1838 }
while (!Nodes.
empty());
1857 WorklistInserter AddNodes(*
this);
1859 if (UseTopologicalSorting)
1868 if (UseTopologicalSorting) {
1870 AddToWorklist(&Node,
Node.use_empty());
1872 for (SDNode &Node : DAG.
allnodes())
1873 AddToWorklist(&Node,
Node.use_empty());
1879 HandleSDNode Dummy(DAG.
getRoot());
1882 while (SDNode *
N = getNextWorklistEntry()) {
1886 if (recursivelyDeleteUnusedNodes(
N))
1889 WorklistRemover DeadNodes(*
this);
1894 SmallSetVector<SDNode *, 16> UpdatedNodes;
1897 for (SDNode *LN : UpdatedNodes)
1898 AddToWorklistWithUsers(LN);
1910 for (
const SDValue &ChildN :
N->op_values())
1911 AddToWorklist(ChildN.getNode(),
true,
1922 ChainsWithoutMergeableStores.
clear();
1933 "Node was deleted but visit returned new node!");
1941 N->getNumValues() == 1 &&
"Type mismatch");
1951 AddToWorklistWithUsers(RV.
getNode());
1957 recursivelyDeleteUnusedNodes(
N);
1961 DAG.
setRoot(Dummy.getValue());
1965SDValue DAGCombiner::visit(SDNode *
N) {
1967 switch (
N->getOpcode()) {
2118 return visitPARTIAL_REDUCE_MLA(
N);
2144#define BEGIN_REGISTER_VP_SDNODE(SDOPC, ...) case ISD::SDOPC:
2145#include "llvm/IR/VPIntrinsics.def"
2146 return visitVPOp(
N);
2152SDValue DAGCombiner::combine(SDNode *
N) {
2157 if (!DisableGenericCombines)
2163 "Node was deleted but visit returned NULL!");
2169 TargetLowering::DAGCombinerInfo
2170 DagCombineInfo(DAG, Level,
false,
this);
2178 switch (
N->getOpcode()) {
2186 RV = PromoteIntBinOp(
SDValue(
N, 0));
2191 RV = PromoteIntShiftOp(
SDValue(
N, 0));
2227 if (
unsigned NumOps =
N->getNumOperands()) {
2228 if (
N->getOperand(0).getValueType() == MVT::Other)
2229 return N->getOperand(0);
2230 if (
N->getOperand(
NumOps-1).getValueType() == MVT::Other)
2231 return N->getOperand(
NumOps-1);
2232 for (
unsigned i = 1; i <
NumOps-1; ++i)
2233 if (
N->getOperand(i).getValueType() == MVT::Other)
2234 return N->getOperand(i);
2239SDValue DAGCombiner::visitFCANONICALIZE(SDNode *
N) {
2240 SDValue Operand =
N->getOperand(0);
2252SDValue DAGCombiner::visitTokenFactor(SDNode *
N) {
2255 if (
N->getNumOperands() == 2) {
2257 return N->getOperand(0);
2259 return N->getOperand(1);
2274 AddToWorklist(*(
N->user_begin()));
2278 SmallPtrSet<SDNode*, 16> SeenOps;
2286 for (
unsigned i = 0; i < TFs.
size(); ++i) {
2291 for (
unsigned j = i;
j < TFs.
size();
j++)
2292 Ops.emplace_back(TFs[j], 0);
2299 SDNode *TF = TFs[i];
2302 switch (
Op.getOpcode()) {
2320 if (SeenOps.
insert(
Op.getNode()).second)
2331 for (
unsigned i = 1, e = TFs.
size(); i < e; i++)
2332 AddToWorklist(TFs[i]);
2342 SmallVector<unsigned, 8> OpWorkCount;
2343 SmallPtrSet<SDNode *, 16> SeenChains;
2344 bool DidPruneOps =
false;
2346 unsigned NumLeftToConsider = 0;
2348 Worklist.
push_back(std::make_pair(
Op.getNode(), NumLeftToConsider++));
2352 auto AddToWorklist = [&](
unsigned CurIdx, SDNode *
Op,
unsigned OpNumber) {
2358 unsigned OrigOpNumber = 0;
2359 while (OrigOpNumber <
Ops.size() &&
Ops[OrigOpNumber].getNode() !=
Op)
2362 "expected to find TokenFactor Operand");
2364 for (
unsigned i = CurIdx + 1; i < Worklist.
size(); ++i) {
2365 if (Worklist[i].second == OrigOpNumber) {
2366 Worklist[i].second = OpNumber;
2369 OpWorkCount[OpNumber] += OpWorkCount[OrigOpNumber];
2370 OpWorkCount[OrigOpNumber] = 0;
2371 NumLeftToConsider--;
2374 if (SeenChains.
insert(
Op).second) {
2375 OpWorkCount[OpNumber]++;
2380 for (
unsigned i = 0; i < Worklist.
size() && i < 1024; ++i) {
2382 if (NumLeftToConsider <= 1)
2384 auto CurNode = Worklist[i].first;
2385 auto CurOpNumber = Worklist[i].second;
2386 assert((OpWorkCount[CurOpNumber] > 0) &&
2387 "Node should not appear in worklist");
2388 switch (CurNode->getOpcode()) {
2394 NumLeftToConsider++;
2397 for (
const SDValue &
Op : CurNode->op_values())
2398 AddToWorklist(i,
Op.getNode(), CurOpNumber);
2404 AddToWorklist(i, CurNode->getOperand(0).getNode(), CurOpNumber);
2408 AddToWorklist(i, MemNode->getChain().getNode(), CurOpNumber);
2411 OpWorkCount[CurOpNumber]--;
2412 if (OpWorkCount[CurOpNumber] == 0)
2413 NumLeftToConsider--;
2427 if (SeenChains.
count(
Op.getNode()) == 0)
2441SDValue DAGCombiner::visitMERGE_VALUES(SDNode *
N) {
2442 WorklistRemover DeadNodes(*
this);
2448 AddUsersToWorklist(
N);
2453 }
while (!
N->use_empty());
2454 deleteAndRecombine(
N);
2462 return Const !=
nullptr && !Const->isOpaque() ? Const :
nullptr;
2472 Op =
N->getOperand(0);
2474 if (
N->getFlags().hasNoUnsignedWrap())
2475 Known.Zero.setBitsFrom(
N.getScalarValueSizeInBits());
2479 if (
N.getValueType().getScalarType() != MVT::i1 ||
2496 if (LD->isIndexed() || LD->getBasePtr().getNode() !=
N)
2498 VT = LD->getMemoryVT();
2499 AS = LD->getAddressSpace();
2501 if (ST->isIndexed() || ST->getBasePtr().getNode() !=
N)
2503 VT = ST->getMemoryVT();
2504 AS = ST->getAddressSpace();
2506 if (LD->isIndexed() || LD->getBasePtr().getNode() !=
N)
2508 VT = LD->getMemoryVT();
2509 AS = LD->getAddressSpace();
2511 if (ST->isIndexed() || ST->getBasePtr().getNode() !=
N)
2513 VT = ST->getMemoryVT();
2514 AS = ST->getAddressSpace();
2520 if (
N->isAnyAdd()) {
2529 }
else if (
N->getOpcode() ==
ISD::SUB) {
2551 bool ShouldCommuteOperands) {
2557 if (ShouldCommuteOperands)
2571 unsigned Opcode =
N->getOpcode();
2572 EVT VT =
N->getValueType(0);
2577 unsigned OpNo = ShouldCommuteOperands ? 0 : 1;
2597SDValue DAGCombiner::foldBinOpIntoSelect(SDNode *BO) {
2600 "Unexpected binary operator");
2612 unsigned SelOpNo = 0;
2649 bool CanFoldNonConst =
2655 if (!CanFoldNonConst &&
2664 if (CanFoldNonConst) {
2683 : DAG.FoldConstantArithmetic(BinOpcode,
DL, VT, {CT, CBO});
2688 : DAG.FoldConstantArithmetic(BinOpcode,
DL, VT, {CF, CBO});
2699 "Expecting add or sub");
2704 bool IsAdd =
N->getOpcode() ==
ISD::ADD;
2705 SDValue C = IsAdd ?
N->getOperand(1) :
N->getOperand(0);
2706 SDValue Z = IsAdd ?
N->getOperand(0) :
N->getOperand(1);
2712 if (Z.getOperand(0).getValueType() != MVT::i1)
2724 EVT VT =
C.getValueType();
2732SDValue DAGCombiner::foldSubToAvg(SDNode *
N,
const SDLoc &
DL) {
2737 if ((!LegalOperations || hasOperation(
ISD::AVGCEILU, VT)) &&
2742 if ((!LegalOperations || hasOperation(
ISD::AVGCEILS, VT)) &&
2753SDValue DAGCombiner::visitPTRADD(SDNode *
N) {
2763 "PTRADD with different operand types is not supported");
2774 !reassociationCanBreakAddressingModePattern(
ISD::PTRADD,
DL,
N, N0, N1)) {
2785 if ((YIsConstant && N0OneUse) || (YIsConstant && ZIsConstant)) {
2790 AddToWorklist(
Add.getNode());
2814 if (
const GlobalAddressSDNode *GA =
2829 AddToWorklist(Inner.
getNode());
2851 SDNodeFlags CommonFlags =
N->getFlags() & N1->
getFlags();
2859 if (ZIsConstant != YIsConstant) {
2863 AddToWorklist(Inner.
getNode());
2873 bool TransformCannotBreakAddrMode =
none_of(
N->users(), [&](SDNode *User) {
2874 return canFoldInAddressingMode(N, User, DAG, TLI);
2877 if (TransformCannotBreakAddrMode)
2889 "Expecting add or sub");
2893 bool IsAdd =
N->getOpcode() ==
ISD::ADD;
2894 SDValue ConstantOp = IsAdd ?
N->getOperand(1) :
N->getOperand(0);
2895 SDValue ShiftOp = IsAdd ?
N->getOperand(0) :
N->getOperand(1);
2917 {ConstantOp, DAG.getConstant(1, DL, VT)})) {
2919 Not.getOperand(0), ShAmt);
2935SDValue DAGCombiner::visitADDLike(SDNode *
N) {
2961 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
2993 if ((!LegalOperations ||
2996 X.getScalarValueSizeInBits() == 1) {
3012 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
3016 if (!reassociationCanBreakAddressingModePattern(
ISD::ADD,
DL,
N, N0, N1)) {
3028 SDNodeFlags NewFlags =
3118 auto MatchUSUBSAT = [](ConstantSDNode *
Max, ConstantSDNode *
Op) {
3119 return (!Max && !
Op) ||
3120 (
Max &&
Op &&
Max->getAPIntValue() == (-
Op->getAPIntValue()));
3161 !
N->getFlags().hasNoSignedWrap()))) {
3182 (CA * CM + CB->getAPIntValue()).getSExtValue())) {
3186 if (
N->getFlags().hasNoUnsignedWrap() &&
3190 if (
N->getFlags().hasNoSignedWrap() &&
3199 DAG.
getConstant(CA * CM + CB->getAPIntValue(),
DL, VT), Flags);
3207 (CA * CM + CB->getAPIntValue()).getSExtValue())) {
3213 if (
N->getFlags().hasNoUnsignedWrap() &&
3218 if (
N->getFlags().hasNoSignedWrap() &&
3229 DAG.
getConstant(CA * CM + CB->getAPIntValue(),
DL, VT), Flags);
3234 if (
SDValue Combined = visitADDLikeCommutative(N0, N1,
N))
3237 if (
SDValue Combined = visitADDLikeCommutative(N1, N0,
N))
3246SDValue DAGCombiner::foldAddToAvg(SDNode *
N,
const SDLoc &
DL) {
3272 if ((!LegalOperations || hasOperation(
ISD::AVGCEILU, VT)) &&
3279 if ((!LegalOperations || hasOperation(
ISD::AVGCEILS, VT)) &&
3290SDValue DAGCombiner::visitADD(SDNode *
N) {
3296 if (
SDValue Combined = visitADDLike(
N))
3305 if (
SDValue V = MatchRotate(N0, N1, SDLoc(
N),
true))
3339 APInt NewStep = C0 + C1;
3349 APInt NewStep = SV0 + SV1;
3357SDValue DAGCombiner::visitADDSAT(SDNode *
N) {
3358 unsigned Opcode =
N->getOpcode();
3376 return DAG.
getNode(Opcode,
DL, VT, N1, N0);
3380 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
3400 bool ForceCarryReconstruction =
false) {
3405 if (ForceCarryReconstruction && V.getValueType() == MVT::i1)
3409 V = V.getOperand(0);
3414 if (ForceCarryReconstruction)
3418 V = V.getOperand(0);
3426 if (V.getResNo() != 1)
3433 EVT VT = V->getValueType(0);
3477 SDNode *LocReference) {
3479 SDLoc
DL(LocReference);
3541 if (TN->
getVT() == MVT::i1) {
3558 DAG.
getVTList(VT, Carry.getValueType()), N0,
3564SDValue DAGCombiner::visitADDC(SDNode *
N) {
3571 if (!
N->hasAnyUseOfValue(1))
3611 return V.getOperand(0);
3617SDValue DAGCombiner::visitADDO(SDNode *
N) {
3623 EVT CarryVT =
N->getValueType(1);
3627 if (!
N->hasAnyUseOfValue(1))
3634 return DAG.
getNode(
N->getOpcode(),
DL,
N->getVTList(), N1, N0);
3659 if (
SDValue Combined = visitUADDOLike(N0, N1,
N))
3662 if (
SDValue Combined = visitUADDOLike(N1, N0,
N))
3693SDValue DAGCombiner::visitADDE(SDNode *
N) {
3712SDValue DAGCombiner::visitUADDO_CARRY(SDNode *
N) {
3726 if (!LegalOperations ||
3736 AddToWorklist(CarryExt.
getNode());
3742 if (
SDValue Combined = visitUADDO_CARRYLike(N0, N1, CarryIn,
N))
3745 if (
SDValue Combined = visitUADDO_CARRYLike(N1, N0, CarryIn,
N))
3892 EVT CarryOutType =
N->getValueType(0);
3908 unsigned CarryInOperandNum =
3910 if (Opcode ==
ISD::USUBO && CarryInOperandNum != 1)
3963 EVT IntVT =
A.getValueType();
4028SDValue DAGCombiner::visitSADDO_CARRY(SDNode *
N) {
4042 if (!LegalOperations ||
4047 if (
SDValue Combined = visitSADDO_CARRYLike(N0, N1, CarryIn,
N))
4050 if (
SDValue Combined = visitSADDO_CARRYLike(N1, N0, CarryIn,
N))
4062 "Illegal truncation");
4086SDValue DAGCombiner::foldSubToUSubSat(EVT DstVT, SDNode *
N,
const SDLoc &
DL) {
4088 !(!LegalOperations || hasOperation(
ISD::USUBSAT, DstVT)))
4091 EVT SubVT =
N->getValueType(0);
4159template <
class MatchContextClass>
4182 if ((
BitWidth - Src.getValueType().getScalarSizeInBits()) != BitWidthDiff)
4193 unsigned AndMaskWidth =
BitWidth - BitWidthDiff;
4194 if (!(AndMask.
isMask(AndMaskWidth) && XorMask.
countr_one() >= AndMaskWidth))
4229 if (
SDValue Res = CheckAndFoldMulCase(Mul0, Mul1))
4232 if (
SDValue Res = CheckAndFoldMulCase(Mul1, Mul0))
4270SDValue DAGCombiner::visitSUB(SDNode *
N) {
4290 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
4317 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
4340 if (
N->getFlags().hasNoUnsignedWrap())
4346 if (
N->getFlags().hasNoSignedWrap())
4372 if (hasOperation(NewOpc, VT))
4514 if (!reassociationCanBreakAddressingModePattern(
ISD::SUB,
DL,
N, N0, N1) &&
4552 if ((!LegalOperations || hasOperation(
ISD::ABS, VT)) &&
4562 if (GA->getGlobal() == GB->getGlobal())
4563 return DAG.
getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
4570 if (TN->
getVT() == MVT::i1) {
4581 if (!
IntVal.isPowerOf2() ||
4626 DAG.
getVTList(VT, Carry.getValueType()), NegX, Zero,
4632 const APInt &C0Val = C0->getAPIntValue();
4635 if (
N->getFlags().hasNoUnsignedWrap() && C0Val.
isMask())
4640 if (!C0->isOpaque()) {
4641 const APInt &MaybeOnes = ~DAG.computeKnownBits(N1).Zero;
4642 if ((C0Val - MaybeOnes) == (C0Val ^ MaybeOnes))
4648 if ((!LegalOperations || hasOperation(
ISD::ABDS, VT)) &&
4660 if ((!LegalOperations || hasOperation(
ISD::ABDU, VT)) &&
4674SDValue DAGCombiner::visitSUBSAT(SDNode *
N) {
4675 unsigned Opcode =
N->getOpcode();
4696 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
4715SDValue DAGCombiner::visitSUBC(SDNode *
N) {
4722 if (!
N->hasAnyUseOfValue(1))
4743SDValue DAGCombiner::visitSUBO(SDNode *
N) {
4749 EVT CarryVT =
N->getValueType(1);
4753 if (!
N->hasAnyUseOfValue(1))
4785SDValue DAGCombiner::visitSUBE(SDNode *
N) {
4797SDValue DAGCombiner::visitUSUBO_CARRY(SDNode *
N) {
4804 if (!LegalOperations ||
4812 !
N->hasAnyUseOfValue(1))
4819SDValue DAGCombiner::visitSSUBO_CARRY(SDNode *
N) {
4826 if (!LegalOperations ||
4836SDValue DAGCombiner::visitMULFIX(SDNode *
N) {
4849 return DAG.
getNode(
N->getOpcode(), SDLoc(
N), VT, N1, N0, Scale);
4858SDValue DAGCombiner::visitMUL(SDNode *
N) {
4878 bool N1IsConst =
false;
4879 bool N1IsOpaqueConst =
false;
4884 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
4889 "Splat APInt should be element width");
4899 if (N1IsConst && ConstValue1.
isZero())
4903 if (N1IsConst && ConstValue1.
isOne())
4906 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
4910 if (N1IsConst && ConstValue1.
isAllOnes())
4916 if (
SDValue LogBase2 = BuildLogBase2(N1,
DL)) {
4920 Flags.setNoUnsignedWrap(
N->getFlags().hasNoUnsignedWrap());
4923 if (
N->getFlags().hasNoSignedWrap() && N1IsConst &&
4925 Flags.setNoSignedWrap(
true);
4932 unsigned Log2Val = (-ConstValue1).logBase2();
4946 SDVTList LoHiVT = DAG.
getVTList(VT, VT);
4949 if (LoHi->hasAnyUseOfValue(1))
4952 if (LoHi->hasAnyUseOfValue(1))
4977 APInt MulC = ConstValue1.
abs();
4979 unsigned TZeros = MulC == 2 ? 0 : MulC.
countr_zero();
4981 if ((MulC - 1).isPowerOf2())
4983 else if ((MulC + 1).isPowerOf2())
4988 MathOp ==
ISD::ADD ? (MulC - 1).logBase2() : (MulC + 1).logBase2();
4991 "multiply-by-constant generated out of bounds shift");
4995 TZeros ? DAG.
getNode(MathOp,
DL, VT, Shl,
5050 APInt NewStep = C0 * MulVal;
5056 if ((!LegalOperations || hasOperation(
ISD::ABS, VT)) &&
5069 SmallBitVector ClearMask;
5071 auto IsClearMask = [&ClearMask](ConstantSDNode *
V) {
5072 if (!V ||
V->isZero()) {
5086 for (
unsigned I = 0;
I != NumElts; ++
I)
5113 EVT NodeType =
Node->getValueType(0);
5114 if (!NodeType.isSimple())
5116 switch (NodeType.getSimpleVT().SimpleTy) {
5117 default:
return false;
5118 case MVT::i8: LC=
isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8;
break;
5119 case MVT::i16: LC=
isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16;
break;
5120 case MVT::i32: LC=
isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32;
break;
5121 case MVT::i64: LC=
isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64;
break;
5122 case MVT::i128: LC=
isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128;
break;
5129SDValue DAGCombiner::useDivRem(SDNode *Node) {
5130 if (
Node->use_empty())
5133 unsigned Opcode =
Node->getOpcode();
5138 EVT VT =
Node->getValueType(0);
5152 unsigned OtherOpcode = 0;
5166 for (SDNode *User : Op0->
users()) {
5173 unsigned UserOpc =
User->getOpcode();
5174 if ((UserOpc == Opcode || UserOpc == OtherOpcode || UserOpc == DivRemOpc) &&
5175 User->getOperand(0) == Op0 &&
5176 User->getOperand(1) == Op1) {
5178 if (UserOpc == OtherOpcode) {
5180 combined = DAG.
getNode(DivRemOpc, SDLoc(Node), VTs, Op0, Op1);
5181 }
else if (UserOpc == DivRemOpc) {
5184 assert(UserOpc == Opcode);
5189 CombineTo(User, combined);
5191 CombineTo(User, combined.
getValue(1));
5200 EVT VT =
N->getValueType(0);
5203 unsigned Opc =
N->getOpcode();
5222 if (N0C && N0C->
isZero())
5242SDValue DAGCombiner::visitSDIV(SDNode *
N) {
5245 EVT VT =
N->getValueType(0);
5255 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
5272 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
5280 if (
SDValue V = visitSDIVLike(N0, N1,
N)) {
5287 if (!
N->getFlags().hasExact()) {
5290 AddToWorklist(
Mul.getNode());
5291 AddToWorklist(
Sub.getNode());
5292 CombineTo(RemNode,
Sub);
5313 if (
C->isZero() ||
C->isOpaque())
5315 if (
C->getAPIntValue().isPowerOf2())
5317 if (
C->getAPIntValue().isNegatedPowerOf2())
5328 EVT VT =
N->getValueType(0);
5339 if ((!
N->getFlags().hasExact() ||
BitWidth > MaxLegalDivRemBitWidth) &&
5357 AddToWorklist(Sign.
getNode());
5363 AddToWorklist(
Add.getNode());
5374 Sra = DAG.
getSelect(
DL, VT, IsOneOrAllOnes, N0, Sra);
5400SDValue DAGCombiner::visitUDIV(SDNode *
N) {
5403 EVT VT =
N->getValueType(0);
5413 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
5427 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
5430 if (
SDValue V = visitUDIVLike(N0, N1,
N)) {
5437 if (!
N->getFlags().hasExact()) {
5440 AddToWorklist(
Mul.getNode());
5441 AddToWorklist(
Sub.getNode());
5442 CombineTo(RemNode,
Sub);
5467 EVT VT =
N->getValueType(0);
5472 if (
SDValue LogBase2 = BuildLogBase2(N1,
DL)) {
5473 AddToWorklist(LogBase2.getNode());
5477 AddToWorklist(Trunc.
getNode());
5487 if (
SDValue LogBase2 = BuildLogBase2(N10,
DL)) {
5488 AddToWorklist(LogBase2.getNode());
5492 AddToWorklist(Trunc.
getNode());
5494 AddToWorklist(
Add.getNode());
5522SDValue DAGCombiner::visitREM(SDNode *
N) {
5523 unsigned Opcode =
N->getOpcode();
5526 EVT VT =
N->getValueType(0);
5548 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
5561 AddToWorklist(
Add.getNode());
5578 if (
SDValue OptimizedRem = buildOptimizedSREM(N0, N1,
N))
5579 return OptimizedRem;
5583 isSigned ? visitSDIVLike(N0, N1,
N) : visitUDIVLike(N0, N1,
N);
5586 unsigned DivOpcode = isSigned ? ISD::SDIV : ISD::UDIV;
5587 if (SDNode *DivNode = DAG.getNodeIfExists(DivOpcode, N->getVTList(),
5589 CombineTo(DivNode, OptimizedDiv);
5592 AddToWorklist(OptimizedDiv.
getNode());
5593 AddToWorklist(
Mul.getNode());
5600 return DivRem.getValue(1);
5623SDValue DAGCombiner::visitMULHS(SDNode *
N) {
5626 EVT VT =
N->getValueType(0);
5639 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
5682SDValue DAGCombiner::visitMULHU(SDNode *
N) {
5685 EVT VT =
N->getValueType(0);
5698 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
5722 (!LegalOperations || hasOperation(
ISD::SRL, VT))) {
5723 if (
SDValue LogBase2 = BuildLogBase2(N1,
DL)) {
5738 unsigned SimpleSize =
Simple.getSizeInBits();
5759SDValue DAGCombiner::visitAVG(SDNode *
N) {
5760 unsigned Opcode =
N->getOpcode();
5763 EVT VT =
N->getValueType(0);
5774 return DAG.
getNode(Opcode,
DL,
N->getVTList(), N1, N0);
5777 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
5803 X.getValueType() ==
Y.getValueType() &&
5804 hasOperation(Opcode,
X.getValueType())) {
5810 X.getValueType() ==
Y.getValueType() &&
5811 hasOperation(Opcode,
X.getValueType())) {
5842 if (IsSigned &&
Add->getFlags().hasNoSignedWrap())
5845 if (!IsSigned &&
Add->getFlags().hasNoUnsignedWrap())
5859SDValue DAGCombiner::visitABD(SDNode *
N) {
5860 unsigned Opcode =
N->getOpcode();
5863 EVT VT =
N->getValueType(0);
5873 return DAG.
getNode(Opcode,
DL,
N->getVTList(), N1, N0);
5876 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
5891 (!LegalOperations || hasOperation(
ISD::ABS, VT)))
5906 EVT SmallVT =
X.getScalarValueSizeInBits() >
Y.getScalarValueSizeInBits()
5909 if (!LegalOperations || hasOperation(Opcode, SmallVT)) {
5921 EVT SmallVT =
X.getValueType();
5922 if (!LegalOperations || hasOperation(Opcode, SmallVT)) {
5924 unsigned RelevantBits =
5931 const APInt &YConst =
C->getAsAPIntVal();
5938 if (RelevantBits <= Bits && TruncatingYIsCheap) {
5952SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *
N,
unsigned LoOp,
5955 bool HiExists =
N->hasAnyUseOfValue(1);
5956 if (!HiExists && (!LegalOperations ||
5959 return CombineTo(
N, Res, Res);
5963 bool LoExists =
N->hasAnyUseOfValue(0);
5964 if (!LoExists && (!LegalOperations ||
5967 return CombineTo(
N, Res, Res);
5971 if (LoExists && HiExists)
5977 AddToWorklist(
Lo.getNode());
5980 (!LegalOperations ||
5982 return CombineTo(
N, LoOpt, LoOpt);
5987 AddToWorklist(
Hi.getNode());
5990 (!LegalOperations ||
5992 return CombineTo(
N, HiOpt, HiOpt);
5998SDValue DAGCombiner::visitSMUL_LOHI(SDNode *
N) {
6004 EVT VT =
N->getValueType(0);
6020 unsigned SimpleSize =
Simple.getSizeInBits();
6032 return CombineTo(
N,
Lo,
Hi);
6039SDValue DAGCombiner::visitUMUL_LOHI(SDNode *
N) {
6045 EVT VT =
N->getValueType(0);
6060 return CombineTo(
N, Zero, Zero);
6066 return CombineTo(
N, N0, Zero);
6073 unsigned SimpleSize =
Simple.getSizeInBits();
6085 return CombineTo(
N,
Lo,
Hi);
6092SDValue DAGCombiner::visitMULO(SDNode *
N) {
6098 EVT CarryVT =
N->getValueType(1);
6119 return DAG.
getNode(
N->getOpcode(),
DL,
N->getVTList(), N1, N0);
6131 N->getVTList(), N0, N0);
6138 return CombineTo(
N,
And, Cmp);
6176 unsigned Opcode0 = isSignedMinMax(N0, N1, N2, N3, CC);
6230 unsigned Opcode1 = isSignedMinMax(N00, N01, N02, N03, N0CC);
6231 if (!Opcode1 || Opcode0 == Opcode1)
6241 APInt MinCPlus1 = MinC + 1;
6242 if (-MaxC == MinCPlus1 && MinCPlus1.
isPowerOf2()) {
6248 if (MaxC == 0 && MinC != 0 && MinCPlus1.
isPowerOf2()) {
6297 unsigned BW = (C1 + 1).exactLogBase2();
6311SDValue DAGCombiner::visitIMINMAX(SDNode *
N) {
6315 unsigned Opcode =
N->getOpcode();
6329 return DAG.
getNode(Opcode,
DL, VT, N1, N0);
6333 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
6337 if (
SDValue RMINMAX = reassociateOps(Opcode,
DL, N0, N1,
N->getFlags()))
6382 if (IsSatBroken || IsOpIllegal) {
6384 if (
A.isUndef() ||
B.isUndef())
6397 if (HasKnownSameSign(N0, N1)) {
6400 return DAG.
getNode(AltOpcode,
DL, VT, N0, N1);
6413 auto ReductionOpcode = [](
unsigned Opcode) {
6427 if (
SDValue SD = reassociateReduction(ReductionOpcode(Opcode), Opcode,
6428 SDLoc(
N), VT, N0, N1))
6436 return C0 > C1 ? N0 : N1;
6438 return C0 > C1 ? N1 : N0;
6447 const APInt &C1V = C1->getAPIntValue();
6466SDValue DAGCombiner::hoistLogicOpWithSameOpcodeHands(SDNode *
N) {
6469 unsigned LogicOpcode =
N->getOpcode();
6494 if (XVT !=
Y.getValueType())
6498 if ((VT.
isVector() || LegalOperations) &&
6508 SDNodeFlags LogicFlags;
6514 return DAG.
getNode(HandOpcode,
DL, VT, Logic);
6524 if (XVT !=
Y.getValueType())
6536 return DAG.
getNode(HandOpcode,
DL, VT, Logic);
6557 return DAG.
getNode(HandOpcode,
DL, VT, Logic);
6572 return DAG.
getNode(HandOpcode,
DL, VT, Logic0, Logic1, S);
6585 if (XVT.
isInteger() && XVT ==
Y.getValueType() &&
6589 return DAG.
getNode(HandOpcode,
DL, VT, Logic);
6608 assert(
X.getValueType() ==
Y.getValueType() &&
6609 "Inputs to shuffles are not the same type");
6615 if (!SVN0->hasOneUse() || !SVN1->hasOneUse() ||
6616 !SVN0->getMask().equals(SVN1->getMask()))
6652 SDValue LL, LR, RL, RR, N0CC, N1CC;
6653 if (!isSetCCEquivalent(N0, LL, LR, N0CC) ||
6654 !isSetCCEquivalent(N1, RL, RR, N1CC))
6658 "Unexpected operand types for bitwise logic op");
6661 "Unexpected operand types for setcc");
6677 if (LR == RR && CC0 == CC1 && IsInteger) {
6682 bool AndEqZero = IsAnd && CC1 ==
ISD::SETEQ && IsZero;
6684 bool AndGtNeg1 = IsAnd && CC1 ==
ISD::SETGT && IsNeg1;
6686 bool OrNeZero = !IsAnd && CC1 ==
ISD::SETNE && IsZero;
6688 bool OrLtZero = !IsAnd && CC1 ==
ISD::SETLT && IsZero;
6694 if (AndEqZero || AndGtNeg1 || OrNeZero || OrLtZero) {
6696 AddToWorklist(
Or.getNode());
6701 bool AndEqNeg1 = IsAnd && CC1 ==
ISD::SETEQ && IsNeg1;
6703 bool AndLtZero = IsAnd && CC1 ==
ISD::SETLT && IsZero;
6705 bool OrNeNeg1 = !IsAnd && CC1 ==
ISD::SETNE && IsNeg1;
6707 bool OrGtNeg1 = !IsAnd && CC1 ==
ISD::SETGT && IsNeg1;
6713 if (AndEqNeg1 || AndLtZero || OrNeNeg1 || OrGtNeg1) {
6715 AddToWorklist(
And.getNode());
6729 AddToWorklist(
Add.getNode());
6750 auto MatchDiffPow2 = [&](ConstantSDNode *C0, ConstantSDNode *C1) {
6756 return !C0->
isOpaque() && !C1->isOpaque() && (CMax - CMin).isPowerOf2();
6774 if (LL == RR && LR == RL) {
6781 if (LL == RL && LR == RR) {
6785 (!LegalOperations ||
6822 unsigned OrAndOpcode,
SelectionDAG &DAG,
bool isFMAXNUMFMINNUM_IEEE,
6823 bool isFMAXNUMFMINNUM) {
6834 isFMAXNUMFMINNUM_IEEE
6842 isFMAXNUMFMINNUM_IEEE
6860 isFMAXNUMFMINNUM_IEEE
6869 isFMAXNUMFMINNUM_IEEE
6880 (LogicOp->getOpcode() ==
ISD::AND || LogicOp->getOpcode() ==
ISD::OR) &&
6881 "Invalid Op to combine SETCC with");
6887 !
LHS->hasOneUse() || !
RHS->hasOneUse())
6894 LogicOp,
LHS.getNode(),
RHS.getNode());
6906 EVT VT = LogicOp->getValueType(0);
6929 (isFMAXNUMFMINNUM_IEEE || isFMAXNUMFMINNUM))) &&
6935 SDValue CommonValue, Operand1, Operand2;
6943 }
else if (LHS1 == RHS1) {
6956 }
else if (RHS0 == LHS1) {
6973 bool IsSigned = isSignedIntSetCC(CC);
6977 bool IsOr = (LogicOp->getOpcode() ==
ISD::OR);
6986 LogicOp->getOpcode(), DAG, isFMAXNUMFMINNUM_IEEE, isFMAXNUMFMINNUM);
6992 DAG.
getNode(NewOpcode,
DL, OpVT, Operand1, Operand2, Flags);
6993 return DAG.
getSetCC(
DL, VT, MinMaxValue, CommonValue, CC, {},
6999 if (LHS0 == LHS1 && RHS0 == RHS1 && CCL == CCR &&
7003 return DAG.
getSetCC(
DL, VT, LHS0, RHS0, CCL);
7010 LHS0 == RHS0 && LHS1C && RHS1C && OpVT.
isInteger()) {
7011 const APInt &APLhs = LHS1C->getAPIntValue();
7012 const APInt &APRhs = RHS1C->getAPIntValue();
7016 if (APLhs == (-APRhs) &&
7027 }
else if (TargetPreference &
7048 APInt Dif = MaxC - MinC;
7082 EVT CondVT =
Cond.getValueType();
7093 EVT OpVT =
T.getValueType();
7112 if (
SDValue V = foldLogicOfSetCCs(
true, N0, N1,
DL))
7129 APInt
ADDC = ADDI->getAPIntValue();
7130 APInt SRLC = SRLI->getAPIntValue();
7142 CombineTo(N0.
getNode(), NewAdd);
7155bool DAGCombiner::isAndLoadExtLoad(ConstantSDNode *AndC, LoadSDNode *LoadN,
7156 EVT LoadResultTy, EVT &ExtVT) {
7165 if (ExtVT == LoadedVT &&
7166 (!LegalOperations ||
7183 if (LegalOperations &&
7194bool DAGCombiner::isLegalNarrowLdSt(LSBaseSDNode *LDST,
7203 const unsigned ByteShAmt = ShAmt / 8;
7222 if (LdStMemVT.
bitsLT(MemVT))
7237 if (PtrType == MVT::Untyped || PtrType.
isExtended())
7244 if (!
SDValue(Load, 0).hasOneUse())
7247 if (LegalOperations &&
7249 Load->getAddressSpace(), ExtType,
false))
7257 if (
Load->getNumValues() > 2)
7276 if (LegalOperations &&
7278 Store->getAlign(),
Store->getAddressSpace()))
7284bool DAGCombiner::SearchForAndLoads(SDNode *
N,
7285 SmallVectorImpl<LoadSDNode*> &Loads,
7286 SmallPtrSetImpl<SDNode*> &NodesWithConsts,
7287 ConstantSDNode *Mask,
7288 SDNode *&NodeToMask) {
7292 if (
Op.getValueType().isVector())
7298 "Expected bitwise logic operation");
7299 if (!
C->getAPIntValue().isSubsetOf(
Mask->getAPIntValue()))
7304 if (!
Op.hasOneUse())
7307 switch(
Op.getOpcode()) {
7311 if (isAndLoadExtLoad(Mask, Load,
Load->getValueType(0), ExtVT) &&
7329 unsigned ActiveBits =
Mask->getAPIntValue().countr_one();
7333 Op.getOperand(0).getValueType();
7344 if (!SearchForAndLoads(
Op.getNode(), Loads, NodesWithConsts, Mask,
7355 NodeToMask =
Op.getNode();
7358 for (
unsigned i = 0, e = NodeToMask->
getNumValues(); i < e; ++i) {
7359 MVT VT =
SDValue(NodeToMask, i).getSimpleValueType();
7360 if (VT != MVT::Glue && VT != MVT::Other) {
7362 NodeToMask =
nullptr;
7374bool DAGCombiner::BackwardsPropagateMask(SDNode *
N) {
7379 if (!
Mask->getAPIntValue().isMask())
7387 SmallPtrSet<SDNode*, 2> NodesWithConsts;
7388 SDNode *FixupNode =
nullptr;
7389 if (SearchForAndLoads(
N, Loads, NodesWithConsts, Mask, FixupNode)) {
7402 SDValue(FixupNode, 0), MaskOp);
7404 if (
And.getOpcode() == ISD ::AND)
7409 for (
auto *LogicN : NodesWithConsts) {
7415 if (LogicN->getOpcode() ==
ISD::AND &&
7434 for (
auto *Load : Loads) {
7439 if (
And.getOpcode() == ISD ::AND)
7442 SDValue NewLoad = reduceLoadWidth(
And.getNode());
7444 "Shouldn't be masking the load if it can't be narrowed");
7445 CombineTo(Load, NewLoad, NewLoad.
getValue(1));
7458SDValue DAGCombiner::unfoldExtremeBitClearingToShifts(SDNode *
N) {
7469 unsigned OuterShift;
7470 unsigned InnerShift;
7472 auto matchMask = [&OuterShift, &InnerShift, &
Y](
SDValue M) ->
bool {
7475 OuterShift =
M->getOpcode();
7484 Y =
M->getOperand(1);
7491 else if (matchMask(N0))
7497 EVT VT =
N->getValueType(0);
7514 SDValue And0 =
And->getOperand(0), And1 =
And->getOperand(1);
7524 bool FoundNot =
false;
7527 Src = Src.getOperand(0);
7533 Src = Src.getOperand(0);
7537 if (Src.getOpcode() !=
ISD::SRL || !Src.hasOneUse())
7541 EVT SrcVT = Src.getValueType();
7550 if (!ShiftAmtC || !ShiftAmtC->getAPIntValue().ult(
BitWidth))
7554 Src = Src.getOperand(0);
7561 Src = Src.getOperand(0);
7585 EVT VT =
N->getValueType(0);
7611 unsigned LogicOpcode =
N->getOpcode();
7613 "Expected bitwise logic operation");
7615 if (!LogicOp.hasOneUse() || !ShiftOp.
hasOneUse())
7619 unsigned ShiftOpcode = ShiftOp.
getOpcode();
7620 if (LogicOp.getOpcode() != LogicOpcode ||
7632 if (LogicOp.getOperand(0).getOpcode() == ShiftOpcode &&
7633 LogicOp.getOperand(0).getOperand(1) ==
Y) {
7635 Z = LogicOp.getOperand(1);
7636 }
else if (LogicOp.getOperand(1).getOpcode() == ShiftOpcode &&
7637 LogicOp.getOperand(1).getOperand(1) ==
Y) {
7639 Z = LogicOp.getOperand(0);
7644 EVT VT =
N->getValueType(0);
7648 return DAG.
getNode(LogicOpcode,
DL, VT, NewShift, Z);
7659 unsigned LogicOpcode =
N->getOpcode();
7661 "Expected bitwise logic operation");
7662 if (LeftHand.
getOpcode() != LogicOpcode ||
7683 EVT VT =
N->getValueType(0);
7685 return DAG.
getNode(LogicOpcode,
DL, VT, CombinedShifts, W);
7697 "Must be called with ISD::OR or ISD::AND node");
7711 EVT VT = M.getValueType();
7719SDValue DAGCombiner::visitAND(SDNode *
N) {
7743 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
7760 if (BV0 && BV1 && !BV0->getSplatValue() && !BV1->getSplatValue() &&
7762 BV0->getOperand(0).getValueType() ==
7763 BV1->getOperand(0).getValueType()) {
7766 EVT EltVT = BV0->getOperand(0).getValueType();
7767 for (
unsigned I = 0;
I != NumElts; ++
I) {
7773 else if (C0 && C0->
isZero())
7775 else if (C1 && C1->isZero())
7779 else if (C1 && C1->isAllOnes())
7781 else if (BV0->getOperand(
I) == BV1->getOperand(
I))
7786 if (MergedOps.
size() == NumElts)
7795 EVT MemVT =
MLoad->getMemoryVT();
7806 MLoad->isExpandingLoad());
7807 CombineTo(
N, Frozen ? N0 : NewLoad);
7808 CombineTo(MLoad, NewLoad, NewLoad.
getValue(1));
7828 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
7841 auto MatchSubset = [](ConstantSDNode *
LHS, ConstantSDNode *
RHS) {
7842 return RHS->getAPIntValue().isSubsetOf(
LHS->getAPIntValue());
7852 APInt
Mask = ~N1C->getAPIntValue();
7877 {N0Op0.getOperand(1)})) {
7910 unsigned EltBitWidth =
Vector->getValueType(0).getScalarSizeInBits();
7911 APInt SplatValue, SplatUndef;
7912 unsigned SplatBitSize;
7919 const bool IsBigEndian =
false;
7921 Vector->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
7922 HasAnyUndefs, EltBitWidth, IsBigEndian);
7926 if (IsSplat && (SplatBitSize % EltBitWidth) == 0) {
7929 SplatValue |= SplatUndef;
7936 for (
unsigned i = 0, n = (SplatBitSize / EltBitWidth); i < n; ++i)
7937 Constant &= SplatValue.
extractBits(EltBitWidth, i * EltBitWidth);
7945 Load->getValueType(0),
Load->getMemoryVT(),
Load->getAlign(),
7954 switch (
Load->getExtensionType()) {
7955 default:
B =
false;
break;
7967 CombineTo(
N, (N0.
getNode() == Load) ? NewLoad : N0);
7971 Load->getValueType(0), SDLoc(Load),
7972 Load->getChain(),
Load->getBasePtr(),
7973 Load->getOffset(),
Load->getMemoryVT(),
7974 Load->getMemOperand());
7976 if (
Load->getNumValues() == 3) {
7978 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
7979 NewLoad.getValue(2) };
7980 CombineTo(Load, To, 3,
true);
7982 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
7992 if (
SDValue Shuffle = XformToShuffleWithZero(
N))
8019 EVT MemVT = GN0->getMemoryVT();
8022 if (
SDValue(GN0, 0).hasOneUse() &&
8025 SDValue Ops[] = {GN0->getChain(), GN0->getPassThru(), GN0->getMask(),
8026 GN0->getBasePtr(), GN0->getIndex(), GN0->getScale()};
8029 DAG.
getVTList(VT, MVT::Other), MemVT,
DL,
Ops, GN0->getMemOperand(),
8032 CombineTo(
N, ZExtLoad);
8033 AddToWorklist(ZExtLoad.
getNode());
8046 if (
SDValue Res = reduceLoadWidth(
N))
8055 if (BackwardsPropagateMask(
N))
8059 if (
SDValue Combined = visitANDLike(N0, N1,
N))
8064 if (
SDValue V = hoistLogicOpWithSameOpcodeHands(
N))
8095 if (
SDValue Folded = foldBitwiseOpWithNeg(
N,
DL, VT))
8117 X.getOperand(0).getScalarValueSizeInBits() == 1)
8120 X.getOperand(0).getScalarValueSizeInBits() == 1)
8135 EVT MemVT = LN0->getMemoryVT();
8142 ((!LegalOperations && LN0->isSimple()) ||
8143 TLI.
isLoadLegal(VT, MemVT, LN0->getAlign(), LN0->getAddressSpace(),
8147 LN0->getBasePtr(), MemVT, LN0->getMemOperand());
8161 if (
SDValue Shifts = unfoldExtremeBitClearingToShifts(
N))
8181 if (!
C->getAPIntValue().isMask(
8182 LHS.getOperand(0).getValueType().getScalarSizeInBits()))
8189 if (IsAndZeroExtMask(N0, N1) &&
8199 if (LegalOperations || VT.
isVector())
8212 bool DemandHighBits) {
8213 if (!LegalOperations)
8216 EVT VT =
N->getValueType(0);
8217 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
8223 bool LookPassAnd0 =
false;
8224 bool LookPassAnd1 =
false;
8239 LookPassAnd0 =
true;
8249 LookPassAnd1 =
true;
8275 LookPassAnd0 =
true;
8289 LookPassAnd1 =
true;
8298 if (OpSizeInBits > 16) {
8302 if (DemandHighBits && !LookPassAnd0)
8309 if (!LookPassAnd1) {
8310 unsigned HighBit = DemandHighBits ? OpSizeInBits : 24;
8318 if (OpSizeInBits > 16) {
8333 if (!
N->hasOneUse())
8336 unsigned Opc =
N.getOpcode();
8354 unsigned MaskByteOffset;
8358 case 0xFF: MaskByteOffset = 0;
break;
8359 case 0xFF00: MaskByteOffset = 1;
break;
8368 case 0xFF0000: MaskByteOffset = 2;
break;
8369 case 0xFF000000: MaskByteOffset = 3;
break;
8374 if (MaskByteOffset == 0 || MaskByteOffset == 2) {
8380 if (!
C ||
C->getZExtValue() != 8)
8388 if (!
C ||
C->getZExtValue() != 8)
8394 if (MaskByteOffset != 0 && MaskByteOffset != 2)
8397 if (!
C ||
C->getZExtValue() != 8)
8402 if (MaskByteOffset != 1 && MaskByteOffset != 3)
8405 if (!
C ||
C->getZExtValue() != 8)
8409 if (Parts[MaskByteOffset])
8424 if (!
C ||
C->getAPIntValue() != 16)
8426 Parts[0] = Parts[1] =
N.getOperand(0).getOperand(0).getNode();
8441 "MatchBSwapHWordOrAndAnd: expecting i32");
8451 if (!Mask0 || !Mask1)
8462 if (!ShiftAmt0 || !ShiftAmt1)
8482 if (!LegalOperations)
8485 EVT VT =
N->getValueType(0);
8503 SDNode *Parts[4] = {};
8523 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
8551 if (
SDValue V = foldLogicOfSetCCs(
false, N0, N1,
DL))
8560 if (
const ConstantSDNode *N0O1C =
8562 if (
const ConstantSDNode *N1O1C =
8566 const APInt &LHSMask = N0O1C->getAPIntValue();
8567 const APInt &RHSMask = N1O1C->getAPIntValue();
8601 auto peekThroughResize = [](
SDValue V) {
8603 return V->getOperand(0);
8607 SDValue N0Resized = peekThroughResize(N0);
8609 SDValue N1Resized = peekThroughResize(N1);
8614 if (N00 == N1Resized || N01 == N1Resized)
8621 if (peekThroughResize(NotOperand) == N1Resized)
8629 if (peekThroughResize(NotOperand) == N1Resized)
8650 auto peekThroughZext = [](
SDValue V) {
8652 return V->getOperand(0);
8692 if (S0 &&
S1 && S0->getZExtValue() < BW &&
S1->getZExtValue() < BW &&
8693 S0->getZExtValue() == (BW -
S1->getZExtValue())) {
8710 Lo.getScalarValueSizeInBits() == (BW / 2) &&
8711 Lo.getValueType() ==
Hi.getValueType()) {
8727SDValue DAGCombiner::visitOR(SDNode *
N) {
8748 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
8764 if (BV0 && BV1 && !BV0->getSplatValue() && !BV1->getSplatValue() &&
8766 BV0->getOperand(0).getValueType() ==
8767 BV1->getOperand(0).getValueType()) {
8770 EVT EltVT = BV0->getOperand(0).getValueType();
8771 for (
unsigned I = 0;
I != NumElts; ++
I) {
8777 else if (C0 && C0->
isZero())
8779 else if (C1 && C1->isZero())
8783 else if (C1 && C1->isAllOnes())
8785 else if (BV0->getOperand(
I) == BV1->getOperand(
I))
8790 if (MergedOps.
size() == NumElts)
8804 if ((ZeroN00 != ZeroN01) && (ZeroN10 != ZeroN11)) {
8805 assert((!ZeroN00 || !ZeroN01) &&
"Both inputs zero!");
8806 assert((!ZeroN10 || !ZeroN11) &&
"Both inputs zero!");
8807 bool CanFold =
true;
8809 SmallVector<int, 4>
Mask(NumElts, -1);
8811 for (
int i = 0; i != NumElts; ++i) {
8812 int M0 = SV0->getMaskElt(i);
8813 int M1 = SV1->getMaskElt(i);
8816 bool M0Zero =
M0 < 0 || (ZeroN00 == (
M0 < NumElts));
8817 bool M1Zero =
M1 < 0 || (ZeroN10 == (
M1 < NumElts));
8821 if ((M0Zero &&
M1 < 0) || (M1Zero &&
M0 < 0))
8825 if (M0Zero == M1Zero) {
8830 assert((
M0 >= 0 ||
M1 >= 0) &&
"Undef index!");
8836 Mask[i] = M1Zero ?
M0 % NumElts : (
M1 % NumElts) + NumElts;
8845 return LegalShuffle;
8859 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
8870 if (
SDValue Combined = visitORLike(N0, N1,
DL))
8880 if (
SDValue BSwap = MatchBSwapHWord(
N, N0, N1))
8882 if (
SDValue BSwap = MatchBSwapHWordLow(
N, N0, N1))
8896 auto MatchIntersect = [](ConstantSDNode *C1, ConstantSDNode *C2) {
8916 if (
SDValue V = hoistLogicOpWithSameOpcodeHands(
N))
8920 if (
SDValue Rot = MatchRotate(N0, N1,
DL,
false))
8923 if (
SDValue Load = MatchLoadCombine(
N))
8933 if (
SDValue Combined = visitADDLike(
N))
8938 if (LegalOperations || VT.
isVector())
8953 Mask =
Op.getOperand(1);
8954 return Op.getOperand(0);
8997 assert(OppShift && ExtractFrom &&
"Empty SDValue");
9025 bool IsMulOrDiv =
false;
9028 auto SelectOpcode = [&](
unsigned NeededShift,
unsigned MulOrDivVariant) {
9029 IsMulOrDiv = ExtractFrom.
getOpcode() == MulOrDivVariant;
9030 if (!IsMulOrDiv && ExtractFrom.
getOpcode() != NeededShift)
9032 Opcode = NeededShift;
9082 if (Rem != 0 || ResultAmt != OppLHSAmt)
9088 if (OppLHSAmt != ExtractFromAmt - NeededShiftAmt.
zextOrTrunc(
9097 return DAG.
getNode(Opcode,
DL, ResVT, OppShiftLHS, NewShiftNode);
9151 unsigned MaskLoBits = 0;
9153 unsigned Bits =
Log2_64(EltSize);
9155 if (NegBits >= Bits) {
9178 if (PosBits >= MaskLoBits) {
9200 if ((Pos == NegOp1) ||
9224 return Width.
getLoBits(MaskLoBits) == 0;
9225 return Width == EltSize;
9235 SDValue InnerNeg,
bool FromAdd,
9236 bool HasPos,
unsigned PosOpcode,
9237 unsigned NegOpcode,
const SDLoc &
DL) {
9248 return DAG.
getNode(HasPos ? PosOpcode : NegOpcode,
DL, VT, Shifted,
9249 HasPos ? Pos : Neg);
9262 SDValue InnerNeg,
bool FromAdd,
9263 bool HasPos,
unsigned PosOpcode,
9264 unsigned NegOpcode,
const SDLoc &
DL) {
9277 return DAG.
getNode(HasPos ? PosOpcode : NegOpcode,
DL, VT, N0, N1,
9278 HasPos ? Pos : Neg);
9323 EVT VT =
LHS.getValueType();
9328 bool HasROTL = hasOperation(
ISD::ROTL, VT);
9329 bool HasROTR = hasOperation(
ISD::ROTR, VT);
9330 bool HasFSHL = hasOperation(
ISD::FSHL, VT);
9331 bool HasFSHR = hasOperation(
ISD::FSHR, VT);
9342 if (LegalOperations && !HasROTL && !HasROTR && !HasFSHL && !HasFSHR)
9347 LHS.getOperand(0).getValueType() ==
RHS.getOperand(0).getValueType()) {
9350 MatchRotate(
LHS.getOperand(0),
RHS.getOperand(0),
DL, FromAdd))
9364 if (!LHSShift && !RHSShift)
9379 RHSShift = NewRHSShift;
9384 LHSShift = NewLHSShift;
9387 if (!RHSShift || !LHSShift)
9412 auto MatchRotateSum = [EltSizeInBits](ConstantSDNode *
LHS,
9413 ConstantSDNode *
RHS) {
9414 return (
LHS->getAPIntValue() +
RHS->getAPIntValue()) == EltSizeInBits;
9417 auto ApplyMasks = [&](
SDValue Res) {
9441 bool IsRotate = LHSShiftArg == RHSShiftArg;
9442 if (!IsRotate && !(HasFSHL || HasFSHR)) {
9451 if (CommonOp ==
Or.getOperand(0)) {
9453 Y =
Or.getOperand(1);
9456 if (CommonOp ==
Or.getOperand(1)) {
9458 Y =
Or.getOperand(0);
9465 if (matchOr(LHSShiftArg, RHSShiftArg)) {
9470 }
else if (matchOr(RHSShiftArg, LHSShiftArg)) {
9479 return ApplyMasks(Res);
9492 if (IsRotate && (HasROTL || HasROTR || !(HasFSHL || HasFSHR))) {
9493 bool UseROTL = !LegalOperations || HasROTL;
9495 UseROTL ? LHSShiftAmt : RHSShiftAmt);
9497 bool UseFSHL = !LegalOperations || HasFSHL;
9499 RHSShiftArg, UseFSHL ? LHSShiftAmt : RHSShiftAmt);
9502 return ApplyMasks(Res);
9507 if (!HasROTL && !HasROTR && !HasFSHL && !HasFSHR)
9516 SDValue LExtOp0 = LHSShiftAmt;
9517 SDValue RExtOp0 = RHSShiftAmt;
9530 if (IsRotate && (HasROTL || HasROTR)) {
9531 if (
SDValue TryL = MatchRotatePosNeg(LHSShiftArg, LHSShiftAmt, RHSShiftAmt,
9532 LExtOp0, RExtOp0, FromAdd, HasROTL,
9536 if (
SDValue TryR = MatchRotatePosNeg(RHSShiftArg, RHSShiftAmt, LHSShiftAmt,
9537 RExtOp0, LExtOp0, FromAdd, HasROTR,
9542 if (
SDValue TryL = MatchFunnelPosNeg(LHSShiftArg, RHSShiftArg, LHSShiftAmt,
9543 RHSShiftAmt, LExtOp0, RExtOp0, FromAdd,
9547 if (
SDValue TryR = MatchFunnelPosNeg(LHSShiftArg, RHSShiftArg, RHSShiftAmt,
9548 LHSShiftAmt, RExtOp0, LExtOp0, FromAdd,
9598static std::optional<SDByteProvider>
9600 std::optional<uint64_t> VectorIndex,
9601 unsigned StartingIndex = 0) {
9605 return std::nullopt;
9609 if (
Depth && !
Op.hasOneUse() &&
9610 (
Op.getOpcode() !=
ISD::LOAD || !
Op.getValueType().isVector()))
9611 return std::nullopt;
9615 if (
Op.getOpcode() !=
ISD::LOAD && VectorIndex.has_value())
9616 return std::nullopt;
9618 unsigned BitWidth =
Op.getScalarValueSizeInBits();
9620 return std::nullopt;
9622 assert(Index < ByteWidth &&
"invalid index requested");
9625 switch (
Op.getOpcode()) {
9630 return std::nullopt;
9634 return std::nullopt;
9636 if (
LHS->isConstantZero())
9638 if (
RHS->isConstantZero())
9640 return std::nullopt;
9645 return std::nullopt;
9647 uint64_t BitShift = ShiftOp->getZExtValue();
9649 if (BitShift % 8 != 0)
9650 return std::nullopt;
9656 return Index < ByteShift
9659 Depth + 1, VectorIndex, Index);
9666 if (NarrowBitWidth % 8 != 0)
9667 return std::nullopt;
9668 uint64_t NarrowByteWidth = NarrowBitWidth / 8;
9670 if (Index >= NarrowByteWidth)
9672 ? std::optional<SDByteProvider>(
9680 Depth + 1, VectorIndex, StartingIndex);
9684 return std::nullopt;
9686 VectorIndex =
OffsetOp->getZExtValue();
9690 if (NarrowBitWidth % 8 != 0)
9691 return std::nullopt;
9692 uint64_t NarrowByteWidth = NarrowBitWidth / 8;
9695 if (Index >= NarrowByteWidth)
9696 return std::nullopt;
9704 if (*VectorIndex * NarrowByteWidth > StartingIndex)
9705 return std::nullopt;
9706 if ((*VectorIndex + 1) * NarrowByteWidth <= StartingIndex)
9707 return std::nullopt;
9710 VectorIndex, StartingIndex);
9714 if (!L->isSimple() || L->isIndexed())
9715 return std::nullopt;
9717 unsigned NarrowBitWidth = L->getMemoryVT().getScalarSizeInBits();
9718 if (NarrowBitWidth % 8 != 0)
9719 return std::nullopt;
9720 uint64_t NarrowByteWidth = NarrowBitWidth / 8;
9725 if (Index >= NarrowByteWidth)
9727 ? std::optional<SDByteProvider>(
9731 unsigned BPVectorIndex = VectorIndex.value_or(0U);
9736 return std::nullopt;
9751 int64_t FirstOffset) {
9753 unsigned Width = ByteOffsets.
size();
9755 return std::nullopt;
9757 bool BigEndian =
true, LittleEndian =
true;
9758 for (
unsigned i = 0; i < Width; i++) {
9759 int64_t CurrentByteOffset = ByteOffsets[i] - FirstOffset;
9762 if (!BigEndian && !LittleEndian)
9763 return std::nullopt;
9766 assert((BigEndian != LittleEndian) &&
"It should be either big endian or"
9773 switch (
Value.getOpcode()) {
9778 return Value.getOperand(0);
9805SDValue DAGCombiner::mergeTruncStores(StoreSDNode *
N) {
9816 EVT MemVT =
N->getMemoryVT();
9817 if (!(MemVT == MVT::i8 || MemVT == MVT::i16 || MemVT == MVT::i32) ||
9818 !
N->isSimple() ||
N->isIndexed())
9825 unsigned MaxWideNumBits = 64;
9826 unsigned MaxStores = MaxWideNumBits / NarrowNumBits;
9835 if (
Store->getMemoryVT() != MemVT || !
Store->isSimple() ||
9839 Chain =
Store->getChain();
9840 if (MaxStores < Stores.
size())
9844 if (Stores.
size() < 2)
9849 unsigned NumStores = Stores.
size();
9850 unsigned WideNumBits = NumStores * NarrowNumBits;
9851 if (WideNumBits != 16 && WideNumBits != 32 && WideNumBits != 64)
9859 StoreSDNode *FirstStore =
nullptr;
9860 std::optional<BaseIndexOffset>
Base;
9861 for (
auto *Store : Stores) {
9880 if (ShiftAmtC % NarrowNumBits != 0)
9887 Offset = ShiftAmtC / NarrowNumBits;
9893 SourceValue = WideVal;
9894 else if (SourceValue != WideVal) {
9902 SourceValue = WideVal;
9911 int64_t ByteOffsetFromBase = 0;
9914 else if (!
Base->equalBaseIndex(Ptr, DAG, ByteOffsetFromBase))
9918 if (ByteOffsetFromBase < FirstOffset) {
9920 FirstOffset = ByteOffsetFromBase;
9926 OffsetMap[
Offset] = ByteOffsetFromBase;
9932 assert(FirstStore &&
"First store must be set");
9939 if (!Allowed || !
Fast)
9944 auto checkOffsets = [&](
bool MatchLittleEndian) {
9945 if (MatchLittleEndian) {
9946 for (
unsigned i = 0; i != NumStores; ++i)
9947 if (OffsetMap[i] != i * (NarrowNumBits / 8) + FirstOffset)
9950 for (
unsigned i = 0, j = NumStores - 1; i != NumStores; ++i, --
j)
9951 if (OffsetMap[j] != i * (NarrowNumBits / 8) + FirstOffset)
9958 bool NeedBswap =
false;
9959 bool NeedRotate =
false;
9962 if (NarrowNumBits == 8 && checkOffsets(Layout.
isBigEndian()))
9964 else if (NumStores == 2 && checkOffsets(Layout.
isBigEndian()))
9973 "Unexpected store value to merge");
9982 }
else if (NeedRotate) {
9983 assert(WideNumBits % 2 == 0 &&
"Unexpected type for rotate");
10027SDValue DAGCombiner::MatchLoadCombine(SDNode *
N) {
10029 "Can only match load combining against OR nodes");
10032 EVT VT =
N->getValueType(0);
10033 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
10039 assert(
P.hasSrc() &&
"Must be a memory byte provider");
10042 unsigned LoadBitWidth =
Load->getMemoryVT().getScalarSizeInBits();
10044 assert(LoadBitWidth % 8 == 0 &&
10045 "can only analyze providers for individual bytes not bit");
10046 unsigned LoadByteWidth = LoadBitWidth / 8;
10051 std::optional<BaseIndexOffset>
Base;
10054 SmallPtrSet<LoadSDNode *, 8> Loads;
10055 std::optional<SDByteProvider> FirstByteProvider;
10061 unsigned ZeroExtendedBytes = 0;
10062 for (
int i = ByteWidth - 1; i >= 0; --i) {
10069 if (
P->isConstantZero()) {
10072 if (++ZeroExtendedBytes != (ByteWidth -
static_cast<unsigned>(i)))
10076 assert(
P->hasSrc() &&
"provenance should either be memory or zero");
10083 else if (Chain != LChain)
10088 int64_t ByteOffsetFromBase = 0;
10097 if (
L->getMemoryVT().isVector()) {
10098 unsigned LoadWidthInBit =
L->getMemoryVT().getScalarSizeInBits();
10099 if (LoadWidthInBit % 8 != 0)
10101 unsigned ByteOffsetFromVector =
P->SrcOffset * LoadWidthInBit / 8;
10102 Ptr.addToOffset(ByteOffsetFromVector);
10108 else if (!
Base->equalBaseIndex(Ptr, DAG, ByteOffsetFromBase))
10112 ByteOffsetFromBase += MemoryByteOffset(*
P);
10113 ByteOffsets[i] = ByteOffsetFromBase;
10116 if (ByteOffsetFromBase < FirstOffset) {
10117 FirstByteProvider =
P;
10118 FirstOffset = ByteOffsetFromBase;
10124 assert(!Loads.
empty() &&
"All the bytes of the value must be loaded from "
10125 "memory, so there must be at least one load which produces the value");
10126 assert(
Base &&
"Base address of the accessed memory location must be set");
10129 bool NeedsZext = ZeroExtendedBytes > 0;
10140 ArrayRef(ByteOffsets).drop_back(ZeroExtendedBytes), FirstOffset);
10144 assert(FirstByteProvider &&
"must be set");
10148 if (MemoryByteOffset(*FirstByteProvider) != 0)
10156 if (LegalOperations &&
10157 !TLI.
isLoadLegal(VT, MemVT, FirstLoad->getAlign(),
10158 FirstLoad->getAddressSpace(),
10167 bool NeedsBswap = IsBigEndianTarget != *IsBigEndian;
10174 if (NeedsBswap && (LegalOperations || NeedsZext) &&
10180 if (NeedsBswap && NeedsZext && LegalOperations &&
10188 *FirstLoad->getMemOperand(), &
Fast);
10189 if (!Allowed || !
Fast)
10194 Chain, FirstLoad->getBasePtr(),
10195 FirstLoad->getPointerInfo(), MemVT, FirstLoad->getAlign());
10198 for (LoadSDNode *L : Loads)
10228SDValue DAGCombiner::unfoldMaskedMerge(SDNode *
N) {
10235 EVT VT =
N->getValueType(0);
10257 M =
And.getOperand(XorIdx ? 0 : 1);
10263 if (!matchAndXor(N0, 0, N1) && !matchAndXor(N0, 1, N1) &&
10264 !matchAndXor(N1, 0, N0) && !matchAndXor(N1, 1, N0))
10311SDValue DAGCombiner::visitXOR(SDNode *
N) {
10338 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
10350 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
10371 if (
SDValue Combined = visitADDLike(
N))
10379 isSetCCEquivalent(N0,
LHS,
RHS, CC,
true) &&
10381 N->use_begin()->getUser()->getOpcode() ==
ISD::AND)) {
10383 LHS.getValueType());
10384 if (!LegalOperations ||
10388 switch (N0Opcode) {
10405 CombineTo(
N, SetCC);
10407 recursivelyDeleteUnusedNodes(N0.
getNode());
10423 AddToWorklist(
V.getNode());
10432 if (isOneUseSetCC(N01) || isOneUseSetCC(N00)) {
10437 return DAG.
getNode(NewOpcode,
DL, VT, N00, N01);
10450 return DAG.
getNode(NewOpcode,
DL, VT, N00, N01);
10460 APInt NotYValue = ~YConst->getAPIntValue();
10476 AddToWorklist(NotX.
getNode());
10481 if (!LegalOperations || hasOperation(
ISD::ABS, VT)) {
10485 SDValue A0 =
A.getOperand(0), A1 =
A.getOperand(1);
10487 if ((A0 == S && A1 == S0) || (A1 == S && A0 == S0))
10524 if (
SDValue V = hoistLogicOpWithSameOpcodeHands(
N))
10535 if (
SDValue MM = unfoldMaskedMerge(
N))
10604 if (!LogicOp.hasOneUse())
10607 unsigned LogicOpcode = LogicOp.getOpcode();
10613 unsigned ShiftOpcode = Shift->
getOpcode();
10616 assert(C1Node &&
"Expected a shift with constant operand");
10619 const APInt *&ShiftAmtVal) {
10620 if (V.getOpcode() != ShiftOpcode || !V.hasOneUse())
10628 ShiftOp = V.getOperand(0);
10633 if (ShiftAmtVal->getBitWidth() != C1Val.
getBitWidth())
10638 bool Overflow =
false;
10639 APInt NewShiftAmt = C1Val.
uadd_ov(*ShiftAmtVal, Overflow);
10644 if (NewShiftAmt.
uge(V.getScalarValueSizeInBits()))
10652 const APInt *C0Val;
10653 if (matchFirstShift(LogicOp.getOperand(0),
X, C0Val))
10654 Y = LogicOp.getOperand(1);
10655 else if (matchFirstShift(LogicOp.getOperand(1),
X, C0Val))
10656 Y = LogicOp.getOperand(0);
10667 return DAG.
getNode(LogicOpcode,
DL, VT, NewShift1, NewShift2,
10677SDValue DAGCombiner::visitShiftByConstant(SDNode *
N) {
10697 switch (
LHS.getOpcode()) {
10721 if (!IsShiftByConstant && !IsCopyOrSelect)
10724 if (IsCopyOrSelect &&
N->hasOneUse())
10729 EVT VT =
N->getValueType(0);
10731 N->getOpcode(),
DL, VT, {LHS.getOperand(1), N->getOperand(1)})) {
10734 return DAG.
getNode(
LHS.getOpcode(),
DL, VT, NewShift, NewRHS);
10740SDValue DAGCombiner::distributeTruncateThroughAnd(SDNode *
N) {
10745 EVT TruncVT =
N->getValueType(0);
10746 if (
N->hasOneUse() &&
N->getOperand(0).hasOneUse() &&
10754 AddToWorklist(Trunc00.
getNode());
10755 AddToWorklist(Trunc01.
getNode());
10763SDValue DAGCombiner::visitRotate(SDNode *
N) {
10767 EVT VT =
N->getValueType(0);
10782 bool OutOfRange =
false;
10783 auto MatchOutOfRange = [Bitsize, &OutOfRange](ConstantSDNode *
C) {
10784 OutOfRange |=
C->getAPIntValue().uge(Bitsize);
10792 return DAG.
getNode(
N->getOpcode(), dl, VT, N0, Amt);
10797 if (RotAmtC && RotAmtC->getAPIntValue() == 8 &&
10808 if (
SDValue NewOp1 = distributeTruncateThroughAnd(N1.
getNode()))
10809 return DAG.
getNode(
N->getOpcode(), dl, VT, N0, NewOp1);
10821 bool SameSide = (
N->getOpcode() == NextOp);
10828 if (Norm1 && Norm2)
10830 CombineOp, dl, ShiftVT, {Norm1, Norm2})) {
10832 {CombinedShift, BitsizeC});
10834 ISD::UREM, dl, ShiftVT, {CombinedShift, BitsizeC});
10836 CombinedShiftNorm);
10843SDValue DAGCombiner::visitSHL(SDNode *
N) {
10860 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
10883 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
10893 if (
SDValue NewOp1 = distributeTruncateThroughAnd(N1.
getNode()))
10899 auto MatchOutOfRange = [OpSizeInBits](ConstantSDNode *
LHS,
10900 ConstantSDNode *
RHS) {
10901 APInt c1 =
LHS->getAPIntValue();
10902 APInt c2 =
RHS->getAPIntValue();
10904 return (c1 + c2).uge(OpSizeInBits);
10909 auto MatchInRange = [OpSizeInBits](ConstantSDNode *
LHS,
10910 ConstantSDNode *
RHS) {
10911 APInt c1 =
LHS->getAPIntValue();
10912 APInt c2 =
RHS->getAPIntValue();
10914 return (c1 + c2).ult(OpSizeInBits);
10936 auto MatchOutOfRange = [OpSizeInBits, InnerBitwidth](ConstantSDNode *
LHS,
10937 ConstantSDNode *
RHS) {
10938 APInt c1 =
LHS->getAPIntValue();
10939 APInt c2 =
RHS->getAPIntValue();
10941 return c2.
uge(OpSizeInBits - InnerBitwidth) &&
10942 (c1 + c2).uge(OpSizeInBits);
10949 auto MatchInRange = [OpSizeInBits, InnerBitwidth](ConstantSDNode *
LHS,
10950 ConstantSDNode *
RHS) {
10951 APInt c1 =
LHS->getAPIntValue();
10952 APInt c2 =
RHS->getAPIntValue();
10954 return c2.
uge(OpSizeInBits - InnerBitwidth) &&
10955 (c1 + c2).ult(OpSizeInBits);
10975 auto MatchEqual = [VT](ConstantSDNode *
LHS, ConstantSDNode *
RHS) {
10976 APInt c1 =
LHS->getAPIntValue();
10977 APInt c2 =
RHS->getAPIntValue();
10987 AddToWorklist(NewSHL.
getNode());
10993 auto MatchShiftAmount = [OpSizeInBits](ConstantSDNode *
LHS,
10994 ConstantSDNode *
RHS) {
10995 const APInt &LHSC =
LHS->getAPIntValue();
10996 const APInt &RHSC =
RHS->getAPIntValue();
10997 return LHSC.
ult(OpSizeInBits) && RHSC.
ult(OpSizeInBits) &&
11069 AddToWorklist(Shl0.
getNode());
11088 {Add.getOperand(1)})) {
11108 if (
SDValue NewSHL = visitShiftByConstant(
N))
11142 Flags.setNoUnsignedWrap(
N->getFlags().hasNoUnsignedWrap() &&
11155 APInt NewStep = C0 << ShlVal;
11170 "SRL or SRA node is required here!");
11179 SDValue ShiftOperand =
N->getOperand(0);
11194 if (!IsSignExt && !IsZeroExt)
11201 auto UserOfLowerBits = [NarrowVTSize](
SDNode *U) {
11206 if (!UShiftAmtSrc) {
11210 return UShiftAmt < NarrowVTSize;
11224 if (IsZeroExt && ShiftOperand.
hasOneUse() &&
11227 }
else if (IsSignExt && ShiftOperand.
hasOneUse() &&
11243 "Cannot have a multiply node with two different operand types.");
11254 if (ShiftAmt != NarrowVTSize)
11264 EVT TransformVT = NarrowVT;
11275 bool IsSigned =
N->getOpcode() ==
ISD::SRA;
11282 unsigned Opcode =
N->getOpcode();
11287 EVT VT =
N->getValueType(0);
11306SDValue DAGCombiner::visitSRA(SDNode *
N) {
11328 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
11331 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
11343 auto SumOfShifts = [&](ConstantSDNode *
LHS, ConstantSDNode *
RHS) {
11344 APInt c1 =
LHS->getAPIntValue();
11345 APInt c2 =
RHS->getAPIntValue();
11347 APInt Sum = c1 + c2;
11348 unsigned ShiftSum =
11349 Sum.
uge(OpSizeInBits) ? (OpSizeInBits - 1) : Sum.getZExtValue();
11359 "Expected matchBinaryPredicate to return one element for "
11363 ShiftValue = ShiftValues[0];
11377 APInt Sum = C1 + C2;
11381 return DAG.
getNOT(
DL, NewShift, VT);
11405 if ((ShiftAmt > 0) &&
11415 N->getValueType(0), Trunc);
11432 if (ConstantSDNode *AddC =
11449 DAG.
getConstant(AddC->getAPIntValue().lshr(ShiftAmt).trunc(
11466 if (
SDValue NewOp1 = distributeTruncateThroughAnd(N1.
getNode()))
11483 if (LargeShift->getAPIntValue() == TruncBits) {
11501 const APInt &AddVal = AddC->getAPIntValue();
11503 SDNodeFlags ShiftFlags =
N->getFlags();
11507 SDNodeFlags AddFlags = N0->
getFlags();
11522 if (
SDValue NewSRA = visitShiftByConstant(
N))
11531 if (
SDValue NarrowLoad = reduceLoadWidth(
N))
11540SDValue DAGCombiner::visitSRL(SDNode *
N) {
11557 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
11560 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
11571 auto MatchOutOfRange = [OpSizeInBits](ConstantSDNode *
LHS,
11572 ConstantSDNode *
RHS) {
11573 APInt c1 =
LHS->getAPIntValue();
11574 APInt c2 =
RHS->getAPIntValue();
11576 return (c1 + c2).uge(OpSizeInBits);
11581 auto MatchInRange = [OpSizeInBits](ConstantSDNode *
LHS,
11582 ConstantSDNode *
RHS) {
11583 APInt c1 =
LHS->getAPIntValue();
11584 APInt c2 =
RHS->getAPIntValue();
11586 return (c1 + c2).ult(OpSizeInBits);
11606 if (c1 + OpSizeInBits == InnerShiftSize) {
11607 if (c1 + c2 >= InnerShiftSize)
11617 c1 + c2 < InnerShiftSize) {
11622 OpSizeInBits - c2),
11639 auto MatchShiftAmount = [OpSizeInBits](ConstantSDNode *
LHS,
11640 ConstantSDNode *
RHS) {
11641 const APInt &LHSC =
LHS->getAPIntValue();
11642 const APInt &RHSC =
RHS->getAPIntValue();
11643 return LHSC.
ult(OpSizeInBits) && RHSC.
ult(OpSizeInBits) &&
11685 AddToWorklist(SmallShift.
getNode());
11713 APInt UnknownBits = ~Known.Zero;
11714 if (UnknownBits == 0)
return DAG.
getConstant(1, SDLoc(N0), VT);
11729 AddToWorklist(
Op.getNode());
11738 if (
SDValue NewOp1 = distributeTruncateThroughAnd(N1.
getNode()))
11768 if (N1C->
getZExtValue() == (NumElts - 1) * EltSizeInBits) {
11771 "Expected BUILD_VECTOR operand as wide as element type");
11788 const APInt &AddVal = AddC->getAPIntValue();
11790 SDNodeFlags ShiftFlags =
N->getFlags();
11794 SDNodeFlags AddFlags = N0->
getFlags();
11806 if (
SDValue NewSRL = visitShiftByConstant(
N))
11810 if (
SDValue NarrowLoad = reduceLoadWidth(
N))
11837 if (
N->hasOneUse()) {
11838 SDNode *
User = *
N->user_begin();
11846 AddToWorklist(User);
11862 X.getScalarValueSizeInBits() == HalfBW &&
11863 Y.getScalarValueSizeInBits() == HalfBW) {
11865 (!LegalOperations ||
11871 (!LegalOperations ||
11889SDValue DAGCombiner::visitFunnelShift(SDNode *
N) {
11890 EVT VT =
N->getValueType(0);
11908 return IsFSHL ? N0 : N1;
11910 auto IsUndefOrZero = [](
SDValue V) {
11919 if (Cst->getAPIntValue().uge(
BitWidth)) {
11920 uint64_t RotAmt = Cst->getAPIntValue().urem(
BitWidth);
11921 return DAG.
getNode(
N->getOpcode(),
DL, VT, N0, N1,
11927 return IsFSHL ? N0 : N1;
11933 if (IsUndefOrZero(N0))
11937 if (IsUndefOrZero(N1))
11951 "ShAmt must be in [1, BW-1] for the identity fold to be valid");
11953 unsigned C0Expected = IsFSHL ? ShAmt :
BitWidth - ShAmt;
11954 unsigned C1Expected = IsFSHL ?
BitWidth - ShAmt : ShAmt;
11977 if (
LHS &&
RHS &&
LHS->isSimple() &&
RHS->isSimple() &&
11978 LHS->getAddressSpace() ==
RHS->getAddressSpace() &&
11979 (
LHS->hasNUsesOfValue(1, 0) ||
RHS->hasNUsesOfValue(1, 0)) &&
11988 RHS->getAddressSpace(), NewAlign,
11989 RHS->getMemOperand()->getFlags(), &
Fast) &&
11993 AddToWorklist(NewPtr.
getNode());
11995 VT,
DL,
RHS->getChain(), NewPtr,
11996 RHS->getPointerInfo().getWithOffset(PtrOff), NewAlign,
11997 RHS->getMemOperand()->getFlags(),
RHS->getAAInfo());
12025 if (N0 == N1 && hasOperation(RotOpc, VT))
12026 return DAG.
getNode(RotOpc,
DL, VT, N0, N2);
12035SDValue DAGCombiner::visitSHLSAT(SDNode *
N) {
12070SDValue DAGCombiner::foldABSToABD(SDNode *
N,
const SDLoc &
DL) {
12071 EVT SrcVT =
N->getValueType(0);
12074 N =
N->getOperand(0).getNode();
12076 EVT VT =
N->getValueType(0);
12083 SDValue AbsOp0 =
N->getOperand(0);
12088 auto IsNotMinSignedInt = [VT](ConstantSDNode *
C) {
12091 return !
C->getAPIntValue()
12093 .isMinSignedValue();
12109 auto CreateZextedAbd = [&](
unsigned AbdOpc) {
12118 bool AbsOpWillNSW =
12130 bool AbsOpWillNUW = !IsAdd && DAG.
SignBitIsZero(Op0) && Op1SignBitIsOne;
12132 if (hasOperation(
ISD::ABDU, VT) && AbsOpWillNUW)
12141 assert(!IsAdd &&
"Unexpected abs(add(x,y)) pattern");
12155 EVT MaxVT = VT0.
bitsGT(VT1) ? VT0 : VT1;
12156 if ((VT0 == MaxVT || Op0->
hasOneUse()) &&
12158 (!LegalTypes || hasOperation(ABDOpcode, MaxVT))) {
12168 if (!LegalOperations || hasOperation(ABDOpcode, VT)) {
12176SDValue DAGCombiner::visitABS(SDNode *
N) {
12178 EVT VT =
N->getValueType(0);
12212SDValue DAGCombiner::visitABS_MIN_POISON(SDNode *
N) {
12214 EVT VT =
N->getValueType(0);
12258SDValue DAGCombiner::visitCLMUL(SDNode *
N) {
12259 unsigned Opcode =
N->getOpcode();
12262 EVT VT =
N->getValueType(0);
12272 return DAG.
getNode(Opcode,
DL, VT, N1, N0);
12293SDValue DAGCombiner::visitPEXT(SDNode *
N) {
12294 EVT VT =
N->getValueType(0);
12311SDValue DAGCombiner::visitPDEP(SDNode *
N) {
12312 EVT VT =
N->getValueType(0);
12335SDValue DAGCombiner::visitBSWAP(SDNode *
N) {
12337 EVT VT =
N->getValueType(0);
12362 if (ShAmt && ShAmt->getAPIntValue().ult(BW) &&
12363 ShAmt->getZExtValue() >= (BW / 2) && (ShAmt->getZExtValue() % 8) == 0 &&
12365 (!LegalOperations || hasOperation(
ISD::BSWAP, HalfVT))) {
12367 if (uint64_t NewShAmt = (ShAmt->getZExtValue() - (BW / 2)))
12383 if (ShAmt && ShAmt->getAPIntValue().ult(BW) &&
12384 ShAmt->getZExtValue() % 8 == 0) {
12399 if (
Known.isZero())
12405 if (BW - (LZ + TZ) == 8) {
12410 if (!LegalOperations || hasOperation(
Opc, VT)) {
12412 SDNodeFlags
Flags =
12422SDValue DAGCombiner::visitBITREVERSE(SDNode *
N) {
12424 EVT VT =
N->getValueType(0);
12458 EVT VT = Src.getValueType();
12468 bool NeedAdd =
true;
12490SDValue DAGCombiner::visitCTLZ(SDNode *
N) {
12492 EVT VT =
N->getValueType(0);
12504 if (
SDValue V = foldCTLZToCTLS(N0,
DL))
12510SDValue DAGCombiner::visitCTLZ_ZERO_POISON(SDNode *
N) {
12512 EVT VT =
N->getValueType(0);
12520 if (
SDValue V = foldCTLZToCTLS(N0,
DL))
12526SDValue DAGCombiner::visitCTTZ(SDNode *
N) {
12528 EVT VT =
N->getValueType(0);
12543SDValue DAGCombiner::visitCTTZ_ZERO_POISON(SDNode *
N) {
12545 EVT VT =
N->getValueType(0);
12555SDValue DAGCombiner::visitCTPOP(SDNode *
N) {
12557 EVT VT =
N->getValueType(0);
12569 const APInt &Amt = AmtC->getAPIntValue();
12570 if (Amt.
ult(NumBits)) {
12604 EVT VT =
LHS.getValueType();
12608 return Flags.hasNoSignedZeros() &&
12610 (Flags.hasNoNaNs() ||
12660SDValue DAGCombiner::foldShiftToAvg(SDNode *
N,
const SDLoc &
DL) {
12661 const unsigned Opcode =
N->getOpcode();
12665 EVT VT =
N->getValueType(0);
12666 bool IsUnsigned = Opcode ==
ISD::SRL;
12673 SDNodeFlags
Flags =
12680 if (hasOperation(FloorISD, VT))
12687SDValue DAGCombiner::foldBitwiseOpWithNeg(SDNode *
N,
const SDLoc &
DL, EVT VT) {
12688 unsigned Opc =
N->getOpcode();
12707 if ((
LHS == True &&
RHS == False) || (
LHS == False &&
RHS == True))
12713 True, DAG, LegalOperations, ForCodeSize);
12717 HandleSDNode NegTrueHandle(NegTrue);
12725 if (
LHS == NegTrue) {
12729 RHS, DAG, LegalOperations, ForCodeSize);
12731 HandleSDNode NegRHSHandle(NegRHS);
12732 if (NegRHS == False) {
12734 False, CC, TLI, DAG);
12754 EVT VT =
N->getValueType(0);
12756 VT !=
Cond.getOperand(0).getValueType())
12799SDValue DAGCombiner::foldSelectOfConstants(SDNode *
N) {
12803 EVT VT =
N->getValueType(0);
12804 EVT CondVT =
Cond.getValueType();
12815 if (CondVT != MVT::i1 || LegalOperations) {
12830 if (C1->
isZero() && C2->isOne()) {
12839 if (C1->
isOne() && C2->isZero() && CondVT == VT)
12850 assert(CondVT == MVT::i1 && !LegalOperations);
12853 if (C1->
isOne() && C2->isZero())
12861 if (C1->
isZero() && C2->isOne()) {
12868 if (C1->
isZero() && C2->isAllOnes()) {
12881 const APInt &C1Val = C1->getAPIntValue();
12882 const APInt &C2Val = C2->getAPIntValue();
12885 if (C1Val - 1 == C2Val) {
12891 if (C1Val + 1 == C2Val) {
12911 if (C2->isAllOnes()) {
12923template <
class MatchContextClass>
12927 N->getOpcode() == ISD::VP_SELECT) &&
12928 "Expected a (v)(vp.)select");
12930 SDValue T =
N->getOperand(1),
F =
N->getOperand(2);
12931 EVT VT =
N->getValueType(0);
12933 MatchContextClass matcher(DAG, TLI,
N);
12969 EVT VT =
N->getValueType(0);
13033 EVT VT =
LHS.getValueType();
13035 if (LegalOperations && !hasOperation(ABDOpc, VT))
13053 bool IsTypeLegalOrPromote =
13071 IsTypeLegalOrPromote)
13087 IsTypeLegalOrPromote)
13128 std::tuple<unsigned, SDValue, SDValue>
Invalid(0, {}, {});
13130 EVT VT =
LHS.getValueType();
13151 unsigned Opcode = 0;
13196 if (!TLI.isOperationLegalOrCustom(Opcode, VT))
13199 return {Opcode,
LHS,
RHS};
13207 EVT VT =
LHS.getValueType();
13212 bool IsStrict =
Cond->isStrictFPOpcode();
13218 DAG,
DL, CC, Op0, Op1,
LHS,
RHS,
N->getFlags(), IsStrict);
13226 {
Cond.getOperand(0), NewLHS, NewRHS});
13230 return DAG.
getNode(Opcode,
DL, VT, NewLHS, NewRHS);
13233SDValue DAGCombiner::visitSELECT(SDNode *
N) {
13237 EVT VT =
N->getValueType(0);
13240 SDNodeFlags
Flags =
N->getFlags();
13252 if (
SDValue V = foldSelectOfConstants(
N))
13256 if (SimplifySelectOps(
N, N1, N2))
13259 if (VT0 == MVT::i1) {
13268 bool normalizeToSequence =
13277 if (normalizeToSequence || !InnerSelect.
use_empty())
13279 InnerSelect, N2, Flags);
13282 recursivelyDeleteUnusedNodes(InnerSelect.
getNode());
13289 Cond1, N1, N2, Flags);
13290 if (normalizeToSequence || !InnerSelect.
use_empty())
13292 InnerSelect, Flags);
13295 recursivelyDeleteUnusedNodes(InnerSelect.
getNode());
13305 if (!normalizeToSequence) {
13311 if (
SDValue Combined = visitANDLike(N0, N1_0,
N)) {
13324 if (!normalizeToSequence) {
13330 if (
SDValue Combined = visitORLike(N0, N2_0,
DL))
13366 combineMinNumMaxNum(
DL, VT, Cond0, Cond1, N1, N2, CC))
13379 if (
C && NotC &&
C->getAPIntValue() == ~NotC->getAPIntValue()) {
13399 (!LegalOperations &&
13407 if (
SDValue ABD = foldSelectToABD(Cond0, Cond1, N1, N2, CC,
DL))
13410 if (
SDValue NewSel = SimplifySelect(
DL, N0, N1, N2))
13415 if (
SDValue UMin = foldSelectToUMin(Cond0, Cond1, N1, N2, CC,
DL))
13420 if (
SDValue BinOp = foldSelectOfBinops(
N))
13439 EVT VT =
N->getValueType(0);
13447 if (
LHS->getNumOperands() != 2 ||
RHS->getNumOperands() != 2)
13456 for (
int i = 0; i < NumElems / 2; ++i) {
13457 if (
Cond->getOperand(i)->isUndef())
13460 if (BottomHalf ==
nullptr)
13462 else if (
Cond->getOperand(i).getNode() != BottomHalf)
13468 for (
int i = NumElems / 2; i < NumElems; ++i) {
13469 if (
Cond->getOperand(i)->isUndef())
13472 if (TopHalf ==
nullptr)
13474 else if (
Cond->getOperand(i).getNode() != TopHalf)
13478 assert(TopHalf && BottomHalf &&
13479 "One half of the selector was all UNDEFs and the other was all the "
13480 "same value. This should have been addressed before this function.");
13483 BottomHalf->
isZero() ?
RHS->getOperand(0) :
LHS->getOperand(0),
13484 TopHalf->
isZero() ?
RHS->getOperand(1) :
LHS->getOperand(1));
13497 EVT VT = BasePtr.getValueType();
13501 SplatVal.getValueType() == VT) {
13507 if (Index.getOpcode() !=
ISD::ADD)
13534 Index = Index.getOperand(0);
13547 Index = Index.getOperand(0);
13554SDValue DAGCombiner::visitVPSCATTER(SDNode *
N) {
13585SDValue DAGCombiner::visitMSCATTER(SDNode *
N) {
13617SDValue DAGCombiner::visitMSTORE(SDNode *
N) {
13631 MST1->isSimple() && MST1->getBasePtr() == Ptr &&
13634 MST1->getMemoryVT().getStoreSize()) ||
13638 CombineTo(MST1, MST1->getChain());
13655 if (CombineToPreIndexedLoadStore(
N) || CombineToPostIndexedLoadStore(
N))
13659 Value.getValueType().isInteger() &&
13662 APInt TruncDemandedBits =
13689 Value.getOperand(0).getValueType());
13699SDValue DAGCombiner::visitVP_STRIDED_STORE(SDNode *
N) {
13704 CStride && CStride->getZExtValue() == EltVT.
getStoreSize()) {
13705 return DAG.
getStoreVP(SST->getChain(), SDLoc(
N), SST->getValue(),
13706 SST->getBasePtr(), SST->getOffset(), SST->getMask(),
13707 SST->getVectorLength(), SST->getMemoryVT(),
13708 SST->getMemOperand(), SST->getAddressingMode(),
13709 SST->isTruncatingStore(), SST->isCompressingStore());
13714SDValue DAGCombiner::visitVECTOR_COMPRESS(SDNode *
N) {
13718 SDValue Passthru =
N->getOperand(2);
13721 bool HasPassthru = !Passthru.
isUndef();
13734 unsigned NumSelected = 0;
13736 for (
unsigned I = 0;
I < NumElmts; ++
I) {
13745 Ops.push_back(VecI);
13749 for (
unsigned Rest = NumSelected; Rest < NumElmts; ++Rest) {
13755 Ops.push_back(Val);
13763SDValue DAGCombiner::visitVPGATHER(SDNode *
N) {
13791SDValue DAGCombiner::visitMGATHER(SDNode *
N) {
13804 return CombineTo(
N, PassThru, MGT->
getChain());
13823SDValue DAGCombiner::visitMLOAD(SDNode *
N) {
13839 return CombineTo(
N, NewLd, NewLd.
getValue(1));
13843 if (CombineToPreIndexedLoadStore(
N) || CombineToPostIndexedLoadStore(
N))
13849SDValue DAGCombiner::visitMHISTOGRAM(SDNode *
N) {
13859 EVT DataVT =
Index.getValueType();
13877SDValue DAGCombiner::visitPARTIAL_REDUCE_MLA(SDNode *
N) {
13878 if (
SDValue Res = foldPartialReduceMLAMulOp(
N))
13880 if (
SDValue Res = foldPartialReduceAdd(
N))
13898SDValue DAGCombiner::foldPartialReduceMLAMulOp(SDNode *
N) {
13920 bool IsMLS =
false;
13950 RHS.getValueType().getScalarType()));
13958 auto IsIntOrFPExtOpcode = [](
unsigned int Opcode) {
13962 unsigned LHSOpcode =
LHS->getOpcode();
13963 if (!IsIntOrFPExtOpcode(LHSOpcode))
13974 EVT OpVT =
Op.getValueType();
13977 if (OrigOp1.
getOpcode() == ISD::VP_MERGE)
13989 EVT AccVT = Acc.getValueType();
14000 unsigned LHSBits =
LHS.getValueType().getScalarSizeInBits();
14017 ApplyPredicate(
C, LHSExtOp);
14018 return GetMLA(NewOpcode, Acc, LHSExtOp,
C);
14021 unsigned RHSOpcode =
RHS->getOpcode();
14022 if (!IsIntOrFPExtOpcode(RHSOpcode))
14047 EVT AccElemVT = Acc.getValueType().getVectorElementType();
14049 NewOpc !=
N->getOpcode())
14059 ApplyPredicate(RHSExtOp, LHSExtOp);
14060 return GetMLA(NewOpc, Acc, LHSExtOp, RHSExtOp);
14070SDValue DAGCombiner::foldPartialReduceAdd(SDNode *
N) {
14090 bool IsMLS =
false;
14104 if (Op1IsSigned != NodeIsSigned &&
14133 : DAG.
getNode(NewOpcode,
DL, AccVT, Acc, UnextOp1, Constant);
14136SDValue DAGCombiner::visitVP_STRIDED_LOAD(SDNode *
N) {
14141 CStride && CStride->getZExtValue() == EltVT.
getStoreSize()) {
14143 SLD->getAddressingMode(), SLD->getExtensionType(), SLD->getValueType(0),
14144 SDLoc(
N), SLD->getChain(), SLD->getBasePtr(), SLD->getOffset(),
14145 SLD->getMask(), SLD->getVectorLength(), SLD->getMemoryVT(),
14146 SLD->getMemOperand(), SLD->isExpandingLoad());
14147 return CombineTo(
N, NewLd, NewLd.
getValue(1));
14154SDValue DAGCombiner::foldVSelectOfConstants(SDNode *
N) {
14158 EVT VT =
N->getValueType(0);
14159 if (!
Cond.hasOneUse() ||
Cond.getScalarValueSizeInBits() != 1 ||
14168 bool AllAddOne =
true;
14169 bool AllSubOne =
true;
14171 for (
unsigned i = 0; i != Elts; ++i) {
14194 if (AllAddOne || AllSubOne) {
14221SDValue DAGCombiner::visitVP_SELECT(SDNode *
N) {
14245 EVT CondVT =
Cond.getValueType();
14246 assert(CondVT.
isVector() &&
"Vector select expects a vector selector!");
14254 if (!IsTAllZero && !IsTAllOne && !IsFAllZero && !IsFAllOne)
14258 if (IsTAllZero && IsFAllZero) {
14267 Cond.getOperand(0).getValueType() == VT && VT.
isSimple() &&
14269 TValAPInt.
isOne() &&
14293 if (!IsTAllOne && !IsFAllZero &&
Cond.hasOneUse() &&
14297 if (IsTAllZero || IsFAllOne) {
14310 "Select condition no longer all-sign bits");
14313 if (IsTAllOne && IsFAllZero)
14342SDValue DAGCombiner::visitVSELECT(SDNode *
N) {
14346 EVT VT =
N->getValueType(0);
14379 bool isAbs =
false;
14398 AddToWorklist(Shift.
getNode());
14399 AddToWorklist(
Add.getNode());
14411 if (
SDValue FMinMax = combineMinNumMaxNum(
DL, VT,
LHS,
RHS, N1, N2, CC))
14426 EVT NarrowVT =
LHS.getValueType();
14434 SetCCWidth < WideWidth &&
14450 DAG.
getSetCC(
DL, WideSetCCVT, WideLHS, WideRHS, CC);
14486 (OpLHS == CondLHS || OpRHS == CondLHS))
14489 if (OpRHS.getOpcode() == CondRHS.getOpcode() &&
14492 CondLHS == OpLHS) {
14496 auto MatchUADDSAT = [](ConstantSDNode *
Op, ConstantSDNode *
Cond) {
14497 return Cond->getAPIntValue() == ~Op->getAPIntValue();
14538 if (OpLHS ==
LHS) {
14553 auto MatchUSUBSAT = [](ConstantSDNode *
Op, ConstantSDNode *
Cond) {
14554 return (!
Op && !
Cond) ||
14556 Cond->getAPIntValue() == (-
Op->getAPIntValue() - 1));
14592 if (SimplifySelectOps(
N, N1, N2))
14612 if (
SDValue V = foldVSelectOfConstants(
N))
14631SDValue DAGCombiner::visitSELECT_CC(SDNode *
N) {
14652 AddToWorklist(
SCC.getNode());
14657 return SCCC->isZero() ? N3 : N2;
14661 if (
SCC->isUndef())
14667 SCC.getOperand(0),
SCC.getOperand(1), N2, N3,
14668 SCC.getOperand(2),
SCC->getFlags());
14673 if (SimplifySelectOps(
N, N2, N3))
14677 DAG,
DL, CC, N0, N1, N2, N3,
N->getFlags(),
false);
14679 return DAG.
getNode(Opcode,
DL,
N->getValueType(0), NewLHS, NewRHS,
14683 return SimplifySelectCC(
DL, N0, N1, N2, N3, CC);
14686SDValue DAGCombiner::visitSETCC(SDNode *
N) {
14691 N->hasOneUse() && (
N->user_begin()->getOpcode() ==
ISD::BRCOND ||
14695 EVT VT =
N->getValueType(0);
14702 if (PreferSetCC && Combined.getOpcode() !=
ISD::SETCC) {
14703 SDValue NewSetCC = rebuildSetCC(Combined);
14731 A.getOperand(0) ==
B.getOperand(0);
14735 B.getOperand(0) ==
A;
14738 bool IsRotate =
false;
14741 if (IsAndWithShift(N0, N1)) {
14743 ShiftOrRotate = N1;
14744 }
else if (IsAndWithShift(N1, N0)) {
14746 ShiftOrRotate = N0;
14747 }
else if (IsRotateWithOp(N0, N1)) {
14750 ShiftOrRotate = N1;
14751 }
else if (IsRotateWithOp(N1, N0)) {
14754 ShiftOrRotate = N0;
14757 if (AndOrOp && ShiftOrRotate && ShiftOrRotate.hasOneUse() &&
14762 auto GetAPIntValue = [](
SDValue Op) -> std::optional<APInt> {
14765 if (CNode ==
nullptr)
14766 return std::nullopt;
14769 std::optional<APInt> AndCMask =
14770 IsRotate ? std::nullopt : GetAPIntValue(AndOrOp.
getOperand(1));
14771 std::optional<APInt> ShiftCAmt =
14772 GetAPIntValue(ShiftOrRotate.getOperand(1));
14776 if (ShiftCAmt && (IsRotate || AndCMask) && ShiftCAmt->ult(NumBits)) {
14777 unsigned ShiftOpc = ShiftOrRotate.getOpcode();
14779 bool CanTransform = IsRotate;
14780 if (!CanTransform) {
14782 CanTransform = *ShiftCAmt == (~*AndCMask).
popcount();
14784 CanTransform &= (*ShiftCAmt + AndCMask->popcount()) == NumBits;
14792 OpVT, ShiftOpc, ShiftCAmt->isPowerOf2(), *ShiftCAmt, AndCMask);
14794 if (CanTransform && NewShiftOpc != ShiftOpc) {
14796 DAG.
getNode(NewShiftOpc,
DL, OpVT, ShiftOrRotate.getOperand(0),
14797 ShiftOrRotate.getOperand(1));
14804 NumBits - ShiftCAmt->getZExtValue())
14805 : APInt::getLowBitsSet(NumBits,
14806 NumBits - ShiftCAmt->getZExtValue());
14814 return DAG.
getSetCC(
DL, VT, NewAndOrOp, NewShiftOrRotate,
Cond);
14822SDValue DAGCombiner::visitSETCCCARRY(SDNode *
N) {
14843 if (!
N.hasOneUse())
14872 unsigned Opcode =
N->getOpcode();
14874 EVT VT =
N->getValueType(0);
14877 "Expected EXTEND dag node in input!");
14919 unsigned Opcode =
N->getOpcode();
14921 EVT VT =
N->getValueType(0);
14924 "Expected EXTEND dag node in input!");
14930 return DAG.
getNode(Opcode,
DL, VT, N0);
14949 unsigned FoldOpc = Opcode;
14972 for (
unsigned i = 0; i != NumElts; ++i) {
14974 if (
Op.isUndef()) {
14985 APInt C =
Op->getAsAPIntVal().zextOrTrunc(EVTBits);
15003 bool HasCopyToRegUses =
false;
15018 for (
unsigned i = 0; i != 2; ++i) {
15036 HasCopyToRegUses =
true;
15039 if (HasCopyToRegUses) {
15040 bool BothLiveOut =
false;
15043 BothLiveOut =
true;
15050 return !ExtendNodes.
empty();
15055void DAGCombiner::ExtendSetCCUses(
const SmallVectorImpl<SDNode *> &SetCCs,
15060 for (SDNode *SetCC : SetCCs) {
15063 for (
unsigned j = 0;
j != 2; ++
j) {
15064 SDValue SOp = SetCC->getOperand(j);
15065 if (SOp == OrigLoad)
15066 Ops.push_back(ExtLoad);
15071 Ops.push_back(SetCC->getOperand(2));
15077SDValue DAGCombiner::CombineExtLoad(SDNode *
N) {
15079 EVT DstVT =
N->getValueType(0);
15084 "Unexpected node type (not an extend)!");
15122 EVT SplitSrcVT = SrcVT;
15123 EVT SplitDstVT = DstVT;
15138 const unsigned NumSplits =
15145 for (
unsigned Idx = 0; Idx < NumSplits; Idx++) {
15146 const unsigned Offset = Idx * Stride;
15164 AddToWorklist(NewChain.
getNode());
15166 CombineTo(
N, NewValue);
15172 ExtendSetCCUses(SetCCs, N0, NewValue, (
ISD::NodeType)
N->getOpcode());
15173 CombineTo(N0.
getNode(), Trunc, NewChain);
15179SDValue DAGCombiner::CombineZExtLogicopShiftLoad(SDNode *
N) {
15181 EVT VT =
N->getValueType(0);
15182 EVT OrigVT =
N->getOperand(0).getValueType();
15204 EVT MemVT =
Load->getMemoryVT();
15226 Load->getChain(),
Load->getBasePtr(),
15227 Load->getMemoryVT(),
Load->getMemOperand());
15240 if (
SDValue(Load, 0).hasOneUse()) {
15244 Load->getValueType(0), ExtLoad);
15245 CombineTo(Load, Trunc, ExtLoad.
getValue(1));
15249 recursivelyDeleteUnusedNodes(N0.
getNode());
15258SDValue DAGCombiner::matchVSelectOpSizesWithSetCC(SDNode *Cast) {
15259 unsigned CastOpcode = Cast->
getOpcode();
15263 "Unexpected opcode for vector select narrowing/widening");
15303 bool LegalOperations,
SDNode *
N,
15317 EVT MemVT = OldExtLoad->getMemoryVT();
15318 if ((LegalOperations || !OldExtLoad->isSimple() || VT.
isVector()) &&
15319 !TLI.
isLoadLegal(VT, MemVT, OldExtLoad->getAlign(),
15320 OldExtLoad->getAddressSpace(), ExtLoadType,
false))
15325 OldExtLoad->getBasePtr(), MemVT,
15326 OldExtLoad->getMemOperand());
15333 DAG.
getValueType(OldExtLoad->getValueType(0).getScalarType()));
15351 bool NonNegZExt =
false) {
15358 (Frozen && !Load->hasNUsesOfValue(1, 0)))
15364 "Unexpected load type or opcode");
15381 !TLI.
isLoadLegal(VT, Load->getValueType(0), Load->getAlign(),
15382 Load->getAddressSpace(), ExtLoadType,
false))
15385 bool DoXform =
true;
15389 ExtOpc, SetCCs, TLI);
15398 unsigned OldBits,
unsigned NewBits,
15403 bool IsVariadic = Dbg->isVariadic();
15406 for (
unsigned I = 0,
E = Locs.
size();
I !=
E; ++
I) {
15411 if (
Op.getSDNode() == Old.getNode() &&
Op.getResNo() == Old.getResNo()) {
15435 for (
unsigned ArgNo : AffectedArgs)
15442 Dbg->getAdditionalDependencies(), Dbg->isIndirect(), Dbg->getDebugLoc(),
15443 Dbg->getOrder(), Dbg->isVariadic());
15445 Dbg->setIsInvalidated();
15446 Dbg->setIsEmitted();
15454 auto SalvageToOldLoadSize = [&](
SDValue Old,
SDValue New,
bool IsSigned) {
15459 unsigned VarBitsOld = Old.getValueSizeInBits();
15460 unsigned VarBitsNew = New.getValueSizeInBits();
15463 if (Dbg->isInvalidated())
15466 SalvageDbgValue(Dbg, Old, New, VarBitsOld, VarBitsNew, IsSigned);
15471 DAG.
getExtLoad(ExtLoadType,
DL, VT, Load->getChain(), Load->getBasePtr(),
15472 Load->getValueType(0), Load->getMemOperand());
15479 DAG.
getValueType(Load->getValueType(0).getScalarType()));
15481 Combiner.ExtendSetCCUses(SetCCs, N0, Res, ExtOpc);
15484 if (
N->getHasDebugValue()) {
15488 if (NoReplaceTrunc) {
15490 if (Load->getHasDebugValue()) {
15492 SalvageToOldLoadSize(OldLoadVal, ExtLoad, IsSigned);
15547 EVT MemoryVT = ALoad->getMemoryVT();
15548 if (!TLI.
isLoadLegal(VT, MemoryVT, ALoad->getAlign(),
15549 ALoad->getAddressSpace(), ExtLoadType,
true))
15557 EVT OrigVT = ALoad->getValueType(0);
15560 ExtLoadType,
SDLoc(ALoad), MemoryVT, VT, ALoad->getChain(),
15561 ALoad->getBasePtr(), ALoad->getMemOperand()));
15571 bool LegalOperations) {
15583 EVT VT =
N->getValueType(0);
15584 EVT XVT =
X.getValueType();
15600 return DAG.
getNode(ShiftOpcode,
DL, VT, NotX, ShiftAmount);
15606SDValue DAGCombiner::foldSextSetcc(SDNode *
N) {
15614 EVT VT =
N->getValueType(0);
15624 if (VT.
isVector() && !LegalOperations &&
15637 return DAG.
getSetCC(
DL, VT, N00, N01, CC, {},
15644 if (SVT == MatchingVecType) {
15661 auto IsFreeToExtend = [&](
SDValue V) {
15680 for (SDUse &Use :
V->uses()) {
15682 SDNode *
User =
Use.getUser();
15683 if (
Use.getResNo() != 0 || User == N0.
getNode())
15688 if (
User->getOpcode() != ExtOpcode ||
User->getValueType(0) != VT)
15694 if (IsFreeToExtend(N00) && IsFreeToExtend(N01)) {
15697 return DAG.
getSetCC(
DL, VT, Ext0, Ext1, CC, {},
15715 SDValue ExtTrueVal = (SetCCWidth == 1)
15719 if (
SDValue SCC = SimplifySelectCC(
DL, N00, N01, ExtTrueVal, Zero, CC,
true))
15732 return DAG.
getSelect(
DL, VT, SetCC, ExtTrueVal, Zero, Flags);
15739SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *
N) {
15741 EVT VT =
N->getValueType(0);
15745 if (
SDValue FoldedVOp = SimplifyVCastOp(
N,
DL))
15791 if (NarrowLoad.getNode() != N0.
getNode()) {
15792 CombineTo(N0.
getNode(), NarrowLoad);
15794 AddToWorklist(oye);
15802 unsigned OpBits =
Op.getScalarValueSizeInBits();
15808 if (OpBits == DestBits) {
15814 if (OpBits < DestBits) {
15823 Flags.setNoSignedWrap(
true);
15831 if (OpBits < DestBits)
15833 else if (OpBits > DestBits)
15853 if (
SDValue ExtLoad = CombineExtLoad(
N))
15889 bool NoReplaceTruncAnd = !N0.
hasOneUse();
15890 bool NoReplaceTrunc =
SDValue(LN00, 0).hasOneUse();
15893 if (NoReplaceTruncAnd) {
15896 CombineTo(N0.
getNode(), TruncAnd);
15898 if (NoReplaceTrunc) {
15903 CombineTo(LN00, Trunc, ExtLoad.
getValue(1));
15922 if (
SDValue NewVSel = matchVSelectOpSizesWithSetCC(
N))
15953 if (NewXor.getNode() == N0.
getNode()) {
15979 "Expected extend op");
16024SDValue DAGCombiner::visitZERO_EXTEND(SDNode *
N) {
16026 EVT VT =
N->getValueType(0);
16030 if (
SDValue FoldedVOp = SimplifyVCastOp(
N,
DL))
16061 APInt TruncatedBits =
16063 APInt(
Op.getScalarValueSizeInBits(), 0) :
16064 APInt::getBitsSet(
Op.getScalarValueSizeInBits(),
16065 N0.getScalarValueSizeInBits(),
16066 std::
min(
Op.getScalarValueSizeInBits(),
16069 SDValue ZExtOrTrunc = DAG.getZExtOrTrunc(Op, DL, VT);
16070 DAG.salvageDebugInfo(*N0.getNode());
16072 return ZExtOrTrunc;
16082 if (NarrowLoad.getNode() != N0.
getNode()) {
16083 CombineTo(N0.
getNode(), NarrowLoad);
16085 AddToWorklist(oye);
16093 if (
N->getFlags().hasNonNeg()) {
16101 if (OpBits == DestBits) {
16107 if (OpBits < DestBits) {
16117 Flags.setNoSignedWrap(
true);
16118 Flags.setNoUnsignedWrap(
true);
16130 AddToWorklist(
Op.getNode());
16134 return ZExtOrTrunc;
16140 AddToWorklist(
Op.getNode());
16166 EVT SrcVT = Src.getValueType();
16195 if (
SDValue ExtLoad = CombineExtLoad(
N))
16216 bool DoXform =
true;
16223 if (isAndLoadExtLoad(AndC, LN00, LoadResultTy, ExtVT))
16239 bool NoReplaceTruncAnd = !N0.
hasOneUse();
16240 bool NoReplaceTrunc =
SDValue(LN00, 0).hasOneUse();
16243 if (NoReplaceTruncAnd) {
16246 CombineTo(N0.
getNode(), TruncAnd);
16248 if (NoReplaceTrunc) {
16253 CombineTo(LN00, Trunc, ExtLoad.
getValue(1));
16262 if (
SDValue ZExtLoad = CombineZExtLogicopShiftLoad(
N))
16275 SelectionDAG::FlagInserter FlagsInserter(DAG, N0->
getFlags());
16278 if (!LegalOperations && VT.
isVector() &&
16310 if (
SDValue SCC = SimplifySelectCC(
16330 if (ShAmtC->getAPIntValue().ugt(KnownZeroBits)) {
16351 if (
SDValue NewVSel = matchVSelectOpSizesWithSetCC(
N))
16373SDValue DAGCombiner::visitANY_EXTEND(SDNode *
N) {
16375 EVT VT =
N->getValueType(0);
16409 if (NarrowLoad.getNode() != N0.
getNode()) {
16410 CombineTo(N0.
getNode(), NarrowLoad);
16412 AddToWorklist(oye);
16438 EVT SrcVT = Src.getValueType();
16469 bool DoXform =
true;
16481 CombineTo(
N, ExtLoad);
16482 if (NoReplaceTrunc) {
16484 recursivelyDeleteUnusedNodes(LN0);
16488 CombineTo(LN0, Trunc, ExtLoad.
getValue(1));
16503 if (!LegalOperations ||
16509 CombineTo(
N, ExtLoad);
16511 recursivelyDeleteUnusedNodes(LN0);
16519 SelectionDAG::FlagInserter FlagsInserter(DAG, Flags);
16526 if (VT.
isVector() && !LegalOperations) {
16553 if (
SDValue SCC = SimplifySelectCC(
16569SDValue DAGCombiner::visitAssertExt(SDNode *
N) {
16570 unsigned Opcode =
N->getOpcode();
16594 EVT MinAssertVT = AssertVT.
bitsLT(BigA_AssertVT) ? AssertVT : BigA_AssertVT;
16609 if (AssertVT.
bitsLT(BigA_AssertVT)) {
16627 if (AssertVT.
bitsLT(BigA_AssertVT) &&
16647SDValue DAGCombiner::visitAssertAlign(SDNode *
N) {
16657 std::max(AL, AAN->getAlign()));
16668 unsigned AlignShift =
Log2(AL);
16673 if (LHSAlignShift >= AlignShift || RHSAlignShift >= AlignShift) {
16674 if (LHSAlignShift < AlignShift)
16676 if (RHSAlignShift < AlignShift)
16687SDValue DAGCombiner::visitIS_FPCLASS(SDNode *
N) {
16690 EVT VT =
N->getValueType(0);
16706 if ((Mask &
Known.KnownFPClasses) != Mask) {
16719SDValue DAGCombiner::reduceLoadWidth(SDNode *
N) {
16720 unsigned Opc =
N->getOpcode();
16724 EVT VT =
N->getValueType(0);
16734 unsigned ShAmt = 0;
16739 unsigned ShiftedOffset = 0;
16759 uint64_t MemoryWidth = LN->getMemoryVT().getScalarSizeInBits();
16760 if (MemoryWidth <= ShAmt)
16771 LN->getExtensionType() != ExtType)
16780 unsigned ActiveBits = 0;
16781 if (
Mask.isMask()) {
16782 ActiveBits =
Mask.countr_one();
16783 }
else if (
Mask.isShiftedMask(ShAmt, ActiveBits)) {
16784 ShiftedOffset = ShAmt;
16805 if (!
SRL.hasOneUse())
16818 ShAmt = SRL1C->getZExtValue();
16819 uint64_t MemoryWidth = LN->getMemoryVT().getSizeInBits();
16820 if (ShAmt >= MemoryWidth)
16845 SDNode *
Mask = *(
SRL->user_begin());
16848 unsigned Offset, ActiveBits;
16849 const APInt& ShiftMask =
Mask->getConstantOperandAPInt(1);
16850 if (ShiftMask.
isMask()) {
16856 LN->getAddressSpace(), ExtType,
false))
16866 LN->getAddressSpace(), ExtType,
false)) {
16874 N0 =
SRL.getOperand(0);
16882 unsigned ShLeftAmt = 0;
16886 ShLeftAmt = N01->getZExtValue();
16907 !isLegalNarrowLdSt(LN0, ExtType, ExtVT, ShAmt))
16913 if (FreezeNode && !FreezeNode.
hasOneUse() &&
16919 auto AdjustBigEndianShift = [&](
unsigned ShAmt) {
16920 unsigned LVTStoreBits =
16923 return LVTStoreBits - EVTStoreBits - ShAmt;
16928 unsigned PtrAdjustmentInBits =
16931 uint64_t PtrOff = PtrAdjustmentInBits / 8;
16937 AddToWorklist(NewPtr.
getNode());
16941 const MDNode *OldRanges = LN0->
getRanges();
16942 const MDNode *NewRanges =
nullptr;
16946 if (ShAmt == 0 && OldRanges) {
16954 ConstantRange TruncatedCR = CR.
truncate(BitSize);
16964 NewRanges = OldRanges;
16977 WorklistRemover DeadNodes(*
this);
16981 if (FreezeNode && !FreezeNode.
hasOneUse())
16998 if (ShLeftAmt != 0) {
17010 if (ShiftedOffset != 0) {
17024SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *
N) {
17027 EVT VT =
N->getValueType(0);
17058 if ((N00Bits <= ExtVTBits ||
17071 if ((N00Bits == ExtVTBits ||
17072 (!IsZext && (N00Bits < ExtVTBits ||
17074 (!LegalOperations ||
17099 if (
SDValue NarrowLoad = reduceLoadWidth(
N))
17107 if (ShAmt->getAPIntValue().ule(VTBits - ExtVTBits)) {
17111 if (((VTBits - ExtVTBits) - ShAmt->getZExtValue()) < InSignBits)
17130 CombineTo(
N, ExtLoad);
17132 AddToWorklist(ExtLoad.
getNode());
17142 ((!LegalOperations && LN0->
isSimple()) &&
17148 CombineTo(
N, ExtLoad);
17164 Ld->getMask(), Ld->getPassThru(), ExtVT, Ld->
getMemOperand(),
17166 CombineTo(
N, Frozen ? N0 : ExtMaskedLoad);
17167 CombineTo(Ld, ExtMaskedLoad, ExtMaskedLoad.
getValue(1));
17174 if (
SDValue(GN0, 0).hasOneUse() && ExtVT == GN0->getMemoryVT() &&
17176 SDValue Ops[] = {GN0->getChain(), GN0->getPassThru(), GN0->getMask(),
17177 GN0->getBasePtr(), GN0->getIndex(), GN0->getScale()};
17180 DAG.
getVTList(VT, MVT::Other), ExtVT,
DL,
Ops, GN0->getMemOperand(),
17183 CombineTo(
N, ExtLoad);
17185 AddToWorklist(ExtLoad.
getNode());
17208 (!LegalOperations ||
17222 bool LegalOperations) {
17223 unsigned InregOpcode =
N->getOpcode();
17227 EVT VT =
N->getValueType(0);
17229 *DAG.
getContext(), Src.getValueType().getVectorElementType());
17232 "Expected EXTEND_VECTOR_INREG dag node in input!");
17241 Src = Src.getOperand(0);
17242 if (Src.getValueType() != SrcVT)
17248 return DAG.
getNode(Opcode,
DL, VT, Src);
17251SDValue DAGCombiner::visitEXTEND_VECTOR_INREG(SDNode *
N) {
17253 EVT VT =
N->getValueType(0);
17277SDValue DAGCombiner::visitTRUNCATE_USAT_U(SDNode *
N) {
17278 EVT VT =
N->getValueType(0);
17299 unsigned NumSrcBits = In.getScalarValueSizeInBits();
17301 assert(NumSrcBits > NumDstBits &&
"Unexpected types for truncate operation");
17322 unsigned NumSrcBits = In.getScalarValueSizeInBits();
17324 assert(NumSrcBits > NumDstBits &&
"Unexpected types for truncate operation");
17345 unsigned NumSrcBits = In.getScalarValueSizeInBits();
17347 assert(NumSrcBits > NumDstBits &&
"Unexpected types for truncate operation");
17370 auto AllowedTruncateSat = [&](
unsigned Opc,
EVT SrcVT,
EVT VT) ->
bool {
17382 }
else if (Src.getOpcode() ==
ISD::UMIN) {
17394SDValue DAGCombiner::visitTRUNCATE(SDNode *
N) {
17396 EVT VT =
N->getValueType(0);
17411 return SaturatedTR;
17463 if (LegalTypes && !LegalOperations && VT.
isScalarInteger() && VT != MVT::i1 &&
17465 EVT TrTy =
N->getValueType(0);
17470 if (Src.getOpcode() ==
ISD::SRL && Src.getOperand(0)->hasOneUse()) {
17473 Src = Src.getOperand(0);
17480 EVT VecTy = Src.getOperand(0).getValueType();
17481 EVT ExTy = Src.getValueType();
17485 auto NewEltCnt = EltCnt * SizeRatio;
17490 SDValue EltNo = Src->getOperand(1);
17493 int Index = isLE ? (Elt * SizeRatio + EltOffset)
17494 : (Elt * SizeRatio + (SizeRatio - 1) - EltOffset);
17505 if (!LegalOperations ||
17528 AddToWorklist(Amt.
getNode());
17578 if (BuildVectEltTy == TruncVecEltTy) {
17582 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
17583 unsigned FirstElt = isLE ? 0 : (TruncEltOffset - 1);
17585 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
17586 "Invalid number of elements");
17589 for (
unsigned i = FirstElt, e = BuildVecNumElts; i <
e;
17590 i += TruncEltOffset)
17600 if (
SDValue Reduced = reduceLoadWidth(
N))
17623 unsigned NumDefs = 0;
17627 if (!
X.isUndef()) {
17644 if (NumDefs == 1) {
17645 assert(
V.getNode() &&
"The single defined operand is empty!");
17647 for (
unsigned i = 0, e = VTs.
size(); i != e; ++i) {
17653 AddToWorklist(
NV.getNode());
17668 (!LegalOperations ||
17696 if (
SDValue NewVSel = matchVSelectOpSizesWithSetCC(
N))
17710 if (!LegalOperations && N0.
hasOneUse() &&
17727 Flags.setNoUnsignedWrap(
true);
17752 if (!LegalOperations && N0.
hasOneUse() &&
17793 if (!LegalOperations && N0.
hasOneUse() &&
17801 bool CanFold =
false;
17809 unsigned NeededBits = SrcBits - TruncBits;
17835SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *
N, EVT VT) {
17848 !LD1->hasOneUse() || !LD2->hasOneUse() ||
17849 LD1->getAddressSpace() != LD2->getAddressSpace())
17852 unsigned LD1Fast = 0;
17853 EVT LD1VT = LD1->getValueType(0);
17858 *LD1->getMemOperand(), &LD1Fast) && LD1Fast)
17859 return DAG.
getLoad(VT, SDLoc(
N), LD1->getChain(), LD1->getBasePtr(),
17860 LD1->getPointerInfo(), LD1->getAlign());
17871SDValue DAGCombiner::foldBitcastedFPLogic(SDNode *
N, SelectionDAG &DAG,
17875 EVT VT =
N->getValueType(0);
17912 auto IsBitCastOrFree = [&TLI, FPOpcode](
SDValue Op, EVT VT) {
17926 IsBitCastOrFree(LogicOp0, VT)) {
17929 NumFPLogicOpsConv++;
17938SDValue DAGCombiner::visitBITCAST(SDNode *
N) {
17940 EVT VT =
N->getValueType(0);
17965 if (!LegalOperations ||
17971 if (
C.getNode() !=
N)
17984 auto IsFreeBitcast = [VT](
SDValue V) {
17986 V.getOperand(0).getValueType() == VT) ||
17999 auto CastLoad = [
this, &VT](
SDValue N0,
const SDLoc &
DL) {
18021 if ((LegalOperations || !LN0->
isSimple()) &&
18031 if (
const MDNode *MD = LN0->
getRanges()) {
18043 if (
SDValue NewLd = CastLoad(N0, SDLoc(
N)))
18050 if (
SDValue V = foldBitcastedFPLogic(
N, DAG, TLI))
18070 AddToWorklist(NewConv.
getNode());
18073 if (N0.
getValueType() == MVT::ppcf128 && !LegalTypes) {
18080 AddToWorklist(FlipBit.
getNode());
18087 AddToWorklist(
Hi.getNode());
18089 AddToWorklist(FlipBit.
getNode());
18093 AddToWorklist(FlipBits.
getNode());
18123 AddToWorklist(
X.getNode());
18127 if (OrigXWidth < VTWidth) {
18129 AddToWorklist(
X.getNode());
18130 }
else if (OrigXWidth > VTWidth) {
18135 X.getValueType(),
X,
18137 X.getValueType()));
18138 AddToWorklist(
X.getNode());
18140 AddToWorklist(
X.getNode());
18143 if (N0.
getValueType() == MVT::ppcf128 && !LegalTypes) {
18146 AddToWorklist(Cst.
getNode());
18148 AddToWorklist(
X.getNode());
18150 AddToWorklist(XorResult.
getNode());
18154 SDLoc(XorResult)));
18155 AddToWorklist(XorResult64.
getNode());
18158 DAG.
getConstant(SignBit, SDLoc(XorResult64), MVT::i64));
18159 AddToWorklist(FlipBit.
getNode());
18162 AddToWorklist(FlipBits.
getNode());
18168 AddToWorklist(
X.getNode());
18173 AddToWorklist(Cst.
getNode());
18181 if (
SDValue CombineLD = CombineConsecutiveLoads(N0.
getNode(), VT))
18204 auto PeekThroughBitcast = [&](
SDValue Op) {
18206 Op.getOperand(0).getValueType() == VT)
18223 SmallVector<int, 8> NewMask;
18225 for (
int i = 0; i != MaskScale; ++i)
18226 NewMask.
push_back(M < 0 ? -1 : M * MaskScale + i);
18231 return LegalShuffle;
18237SDValue DAGCombiner::visitBUILD_PAIR(SDNode *
N) {
18238 EVT VT =
N->getValueType(0);
18239 return CombineConsecutiveLoads(
N, VT);
18242SDValue DAGCombiner::visitFREEZE(SDNode *
N) {
18253 while (!
N->use_empty())
18258 assert(
N->getOperand(0) == FrozenN0 &&
"Expected cycle in DAG");
18288 bool AllowMultipleMaybePoisonOperands =
18316 SmallSet<SDValue, 8> MaybePoisonOperands;
18317 SmallVector<unsigned, 8> MaybePoisonOperandNumbers;
18322 bool HadMaybePoisonOperands = !MaybePoisonOperands.
empty();
18323 bool IsNewMaybePoisonOperand = MaybePoisonOperands.
insert(
Op).second;
18324 if (IsNewMaybePoisonOperand)
18325 MaybePoisonOperandNumbers.
push_back(OpNo);
18326 if (!HadMaybePoisonOperands)
18328 if (IsNewMaybePoisonOperand && !AllowMultipleMaybePoisonOperands) {
18337 for (
unsigned OpNo : MaybePoisonOperandNumbers) {
18348 SDValue MaybePoisonOperand =
N->getOperand(0).getOperand(OpNo);
18350 if (MaybePoisonOperand.
isUndef())
18357 FrozenMaybePoisonOperand.
getOperand(0) == FrozenMaybePoisonOperand) {
18361 MaybePoisonOperand);
18397 SDNodeFlags SrcFlags = N0->
getFlags();
18398 SDNodeFlags SafeFlags;
18412 N->getFlags().hasAllowContract();
18416SDValue DAGCombiner::visitFADDForFMACombine(SDNode *
N) {
18419 EVT VT =
N->getValueType(0);
18424 bool HasFMAD = (LegalOperations && TLI.
isFMADLegal(DAG,
N));
18432 if (!HasFMAD && !HasFMA)
18435 bool AllowFusionGlobally =
18438 if (!AllowFusionGlobally && !
N->getFlags().hasAllowContract())
18456 unsigned Opcode =
N.getOpcode();
18465 return AllowFusionGlobally ||
N->getFlags().hasAllowContract();
18495 bool CanReassociate =
N->getFlags().hasAllowReassociation();
18496 if (CanReassociate) {
18501 }
else if (isFusedOp(N1) && N1.
hasOneUse()) {
18507 while (
E && isFusedOp(TmpFMA) && TmpFMA.
hasOneUse()) {
18531 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
18545 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
18558 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
X,
Y,
18559 DAG.
getNode(PreferredFusedOpcode, SL, VT,
18564 if (isFusedOp(N0)) {
18588 DAG.
getNode(PreferredFusedOpcode, SL, VT,
18594 if (isFusedOp(N00)) {
18608 if (isFusedOp(N1)) {
18629 if (isFusedOp(N10)) {
18646SDValue DAGCombiner::visitFSUBForFMACombine(SDNode *
N) {
18649 EVT VT =
N->getValueType(0);
18654 bool HasFMAD = (LegalOperations && TLI.
isFMADLegal(DAG,
N));
18662 if (!HasFMAD && !HasFMA)
18665 const SDNodeFlags
Flags =
N->getFlags();
18666 bool AllowFusionGlobally =
18670 if (!AllowFusionGlobally && !
N->getFlags().hasAllowContract())
18679 bool NoSignedZero =
Flags.hasNoSignedZeros();
18686 return AllowFusionGlobally ||
N->getFlags().hasAllowContract();
18702 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
18714 if (
SDValue V = tryToFoldXSubYZ(N0, N1))
18717 if (
SDValue V = tryToFoldXYSubZ(N0, N1))
18721 if (
SDValue V = tryToFoldXYSubZ(N0, N1))
18724 if (
SDValue V = tryToFoldXSubYZ(N0, N1))
18733 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
18747 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
18763 PreferredFusedOpcode, SL, VT,
18785 DAG.
getNode(PreferredFusedOpcode, SL, VT,
18808 DAG.
getNode(PreferredFusedOpcode, SL, VT,
18821 unsigned Opcode =
N.getOpcode();
18826 if (
Aggressive &&
N->getFlags().hasAllowReassociation()) {
18827 bool CanFuse =
N->getFlags().hasAllowContract();
18830 if (CanFuse && isFusedOp(N0) &&
18831 isContractableAndReassociableFMUL(N0.
getOperand(2)) &&
18835 DAG.
getNode(PreferredFusedOpcode, SL, VT,
18843 if (CanFuse && isFusedOp(N1) &&
18844 isContractableAndReassociableFMUL(N1.
getOperand(2)) &&
18849 PreferredFusedOpcode, SL, VT,
18851 DAG.
getNode(PreferredFusedOpcode, SL, VT,
18857 if (isFusedOp(N0) && N0->
hasOneUse()) {
18861 if (isContractableAndReassociableFMUL(N020) &&
18867 PreferredFusedOpcode, SL, VT,
18883 if (isFusedOp(N00)) {
18885 if (isContractableAndReassociableFMUL(N002) &&
18889 PreferredFusedOpcode, SL, VT,
18893 PreferredFusedOpcode, SL, VT,
18906 if (isContractableAndReassociableFMUL(N120) &&
18912 PreferredFusedOpcode, SL, VT,
18914 DAG.
getNode(PreferredFusedOpcode, SL, VT,
18932 if (isContractableAndReassociableFMUL(N102) &&
18938 PreferredFusedOpcode, SL, VT,
18942 DAG.
getNode(PreferredFusedOpcode, SL, VT,
18956SDValue DAGCombiner::visitFMULForFMADistributiveCombine(SDNode *
N) {
18959 EVT VT =
N->getValueType(0);
18969 if (!
FAdd->getFlags().hasNoInfs())
18980 bool HasFMAD = LegalOperations && TLI.
isFMADLegal(DAG,
N);
18983 if (!HasFMAD && !HasFMA)
18996 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
X.getOperand(0),
Y,
18998 if (
C->isMinusOne())
18999 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
X.getOperand(0),
Y,
19006 if (
SDValue FMA = FuseFADD(N0, N1))
19008 if (
SDValue FMA = FuseFADD(N1, N0))
19019 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
19022 if (C0->isMinusOne())
19023 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
19029 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
X.getOperand(0),
Y,
19031 if (C1->isMinusOne())
19032 return DAG.
getNode(PreferredFusedOpcode, SL, VT,
X.getOperand(0),
Y,
19039 if (
SDValue FMA = FuseFSUB(N0, N1))
19041 if (
SDValue FMA = FuseFSUB(N1, N0))
19047SDValue DAGCombiner::visitFADD(SDNode *
N) {
19052 EVT VT =
N->getValueType(0);
19054 SDNodeFlags
Flags =
N->getFlags();
19055 SelectionDAG::FlagInserter FlagsInserter(DAG,
N);
19065 if (N0CFP && !N1CFP)
19070 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
19075 if (N1C && N1C->
isZero())
19079 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
19085 N1, DAG, LegalOperations, ForCodeSize))
19091 N0, DAG, LegalOperations, ForCodeSize))
19098 return C &&
C->isExactlyValue(-2.0);
19102 if (isFMulNegTwo(N0)) {
19108 if (isFMulNegTwo(N1)) {
19119 if (
Flags.hasNoNaNs() && AllowNewConst) {
19132 if (
Flags.hasAllowReassociation() &&
Flags.hasNoSignedZeros() &&
19150 if (CFP01 && !CFP00 && N0.
getOperand(0) == N1) {
19171 if (CFP11 && !CFP10 && N1.
getOperand(0) == N0) {
19218 if (
Flags.hasAllowReassociation() &&
Flags.hasNoSignedZeros()) {
19221 VT, N0, N1, Flags))
19226 if (
SDValue Fused = visitFADDForFMACombine(
N)) {
19228 AddToWorklist(Fused.getNode());
19234SDValue DAGCombiner::visitSTRICT_FADD(SDNode *
N) {
19238 EVT VT =
N->getValueType(0);
19239 EVT ChainVT =
N->getValueType(1);
19241 SelectionDAG::FlagInserter FlagsInserter(DAG,
N);
19246 N1, DAG, LegalOperations, ForCodeSize)) {
19248 {Chain, N0, NegN1});
19254 N0, DAG, LegalOperations, ForCodeSize)) {
19256 {Chain, N1, NegN0});
19261SDValue DAGCombiner::visitFSUB(SDNode *
N) {
19266 EVT VT =
N->getValueType(0);
19268 const SDNodeFlags
Flags =
N->getFlags();
19269 SelectionDAG::FlagInserter FlagsInserter(DAG,
N);
19280 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
19283 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
19287 if (N1CFP && N1CFP->
isZero()) {
19295 if (
Flags.hasNoNaNs())
19300 if (N0CFP && N0CFP->
isZero()) {
19317 if (
Flags.hasAllowReassociation() &&
Flags.hasNoSignedZeros() &&
19333 if (
SDValue Fused = visitFSUBForFMACombine(
N)) {
19334 AddToWorklist(Fused.getNode());
19356SDValue DAGCombiner::combineFMulOrFDivWithIntPow2(SDNode *
N) {
19357 EVT VT =
N->getValueType(0);
19363 std::optional<int> Mantissa;
19364 auto GetConstAndPow2Ops = [&](
unsigned ConstOpIdx) {
19365 if (ConstOpIdx == 1 &&
N->getOpcode() ==
ISD::FDIV)
19382 auto IsFPConstValid = [
N, MaxExpChange, &Mantissa](ConstantFPSDNode *CFP) {
19383 if (CFP ==
nullptr)
19386 const APFloat &APF = CFP->getValueAPF();
19394 int CurExp =
ilogb(APF);
19397 N->getOpcode() ==
ISD::FMUL ? CurExp : (CurExp - MaxExpChange);
19400 N->getOpcode() ==
ISD::FDIV ? CurExp : (CurExp + MaxExpChange);
19408 Mantissa = ThisMantissa;
19410 return *Mantissa == ThisMantissa && ThisMantissa > 0;
19417 if (!GetConstAndPow2Ops(0) && !GetConstAndPow2Ops(1))
19445 NewIntVT, DAG.
getBitcast(NewIntVT, ConstOp), Shift);
19450SDValue DAGCombiner::visitFMUL(SDNode *
N) {
19454 EVT VT =
N->getValueType(0);
19456 const SDNodeFlags
Flags =
N->getFlags();
19457 SelectionDAG::FlagInserter FlagsInserter(DAG,
N);
19473 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
19476 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
19479 if (
Flags.hasAllowReassociation()) {
19505 VT, N0, N1, Flags))
19529 HandleSDNode NegN0Handle(NegN0);
19539 if (
Flags.hasNoNaNs() &&
Flags.hasNoSignedZeros() &&
19570 if (TrueOpnd->isMinusOne() && FalseOpnd->isOne() &&
19574 if (TrueOpnd->isOne() && FalseOpnd->isMinusOne())
19583 if (
SDValue Fused = visitFMULForFMADistributiveCombine(
N)) {
19584 AddToWorklist(Fused.getNode());
19590 if (
SDValue R = combineFMulOrFDivWithIntPow2(
N))
19596SDValue DAGCombiner::visitFMA(SDNode *
N) {
19603 EVT VT =
N->getValueType(0);
19606 SelectionDAG::FlagInserter FlagsInserter(DAG,
N);
19621 HandleSDNode NegN0Handle(NegN0);
19629 if (
N->getFlags().hasNoNaNs() &&
N->getFlags().hasNoInfs()) {
19630 if (
N->getFlags().hasNoSignedZeros() || (N2CFP && !N2CFP->
isNegZero())) {
19631 if (N0CFP && N0CFP->
isZero())
19633 if (N1CFP && N1CFP->
isZero())
19638 if (N0CFP && N0CFP->
isOne())
19640 if (N1CFP && N1CFP->
isOne())
19648 bool CanReassociate =
N->getFlags().hasAllowReassociation();
19649 if (CanReassociate) {
19670 if (N1CFP->
isOne())
19676 AddToWorklist(RHSNeg.
getNode());
19690 if (CanReassociate) {
19692 if (N1CFP && N0 == N2) {
19710 SDValue(
N, 0), DAG, LegalOperations, ForCodeSize))
19715SDValue DAGCombiner::visitFMAD(SDNode *
N) {
19719 EVT VT =
N->getValueType(0);
19729SDValue DAGCombiner::visitFMULADD(SDNode *
N) {
19733 EVT VT =
N->getValueType(0);
19751SDValue DAGCombiner::combineRepeatedFPDivisors(SDNode *
N) {
19755 const SDNodeFlags
Flags =
N->getFlags();
19756 if (LegalDAG || !
Flags.hasAllowReciprocal())
19771 unsigned NumElts = 1;
19772 EVT VT =
N->getValueType(0);
19776 if (!MinUses || (N1->
use_size() * NumElts) < MinUses)
19781 SetVector<SDNode *>
Users;
19782 for (
auto *U : N1->
users()) {
19783 if (
U->getOpcode() ==
ISD::FDIV &&
U->getOperand(1) == N1) {
19785 if (
U->getOperand(1).getOpcode() ==
ISD::FSQRT &&
19786 U->getOperand(0) ==
U->getOperand(1).getOperand(0) &&
19787 U->getFlags().hasAllowReassociation() &&
19788 U->getFlags().hasNoSignedZeros())
19793 if (
U->getFlags().hasAllowReciprocal())
19800 if ((
Users.size() * NumElts) < MinUses)
19808 for (
auto *U :
Users) {
19809 SDValue Dividend =
U->getOperand(0);
19810 if (Dividend != FPOne) {
19812 Reciprocal, Flags);
19813 CombineTo(U, NewNode);
19814 }
else if (U != Reciprocal.
getNode()) {
19817 CombineTo(U, Reciprocal);
19823SDValue DAGCombiner::visitFDIV(SDNode *
N) {
19826 EVT VT =
N->getValueType(0);
19828 SDNodeFlags
Flags =
N->getFlags();
19829 SelectionDAG::FlagInserter FlagsInserter(DAG,
N);
19840 if (
SDValue FoldedVOp = SimplifyVBinOp(
N,
DL))
19843 if (
SDValue NewSel = foldBinOpIntoSelect(
N))
19860 (!LegalOperations ||
19870 if (
Flags.hasAllowReciprocal()) {
19879 N1AllowReciprocal) {
19912 A =
Y.getOperand(0);
19925 recursivelyDeleteUnusedNodes(AAZ.
getNode()