@@ -16693,38 +16693,51 @@ SDValue DAGCombiner::visitBITCAST(SDNode *N) {
1669316693 }
1669416694
1669516695 // fold (conv (load x)) -> (load (conv*)x)
16696+ // fold (conv (freeze (load x))) -> (freeze (load (conv*)x))
1669616697 // If the resultant load doesn't need a higher alignment than the original!
16697- if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
16698- // Do not remove the cast if the types differ in endian layout.
16699- TLI.hasBigEndianPartOrdering(N0.getValueType(), DAG.getDataLayout()) ==
16700- TLI.hasBigEndianPartOrdering(VT, DAG.getDataLayout()) &&
16701- // If the load is volatile, we only want to change the load type if the
16702- // resulting load is legal. Otherwise we might increase the number of
16703- // memory accesses. We don't care if the original type was legal or not
16704- // as we assume software couldn't rely on the number of accesses of an
16705- // illegal type.
16706- ((!LegalOperations && cast<LoadSDNode>(N0)->isSimple()) ||
16707- TLI.isOperationLegal(ISD::LOAD, VT))) {
16708- LoadSDNode *LN0 = cast<LoadSDNode>(N0);
16698+ auto CastLoad = [this, &VT](SDValue N0, const SDLoc &DL) {
16699+ auto *LN0 = dyn_cast<LoadSDNode>(N0);
16700+ if (!LN0 || !ISD::isNormalLoad(LN0) || !N0.hasOneUse())
16701+ return SDValue();
1670916702
16710- if (TLI.isLoadBitCastBeneficial(N0.getValueType(), VT, DAG,
16711- *LN0->getMemOperand())) {
16712- // If the range metadata type does not match the new memory
16713- // operation type, remove the range metadata.
16714- if (const MDNode *MD = LN0->getRanges()) {
16715- ConstantInt *Lower = mdconst::extract<ConstantInt>(MD->getOperand(0));
16716- if (Lower->getBitWidth() != VT.getScalarSizeInBits() ||
16717- !VT.isInteger()) {
16718- LN0->getMemOperand()->clearRanges();
16719- }
16703+ // Do not remove the cast if the types differ in endian layout.
16704+ if (TLI.hasBigEndianPartOrdering(N0.getValueType(), DAG.getDataLayout()) !=
16705+ TLI.hasBigEndianPartOrdering(VT, DAG.getDataLayout()))
16706+ return SDValue();
16707+
16708+ // If the load is volatile, we only want to change the load type if the
16709+ // resulting load is legal. Otherwise we might increase the number of
16710+ // memory accesses. We don't care if the original type was legal or not
16711+ // as we assume software couldn't rely on the number of accesses of an
16712+ // illegal type.
16713+ if (((LegalOperations || !LN0->isSimple()) &&
16714+ !TLI.isOperationLegal(ISD::LOAD, VT)))
16715+ return SDValue();
16716+
16717+ if (!TLI.isLoadBitCastBeneficial(N0.getValueType(), VT, DAG,
16718+ *LN0->getMemOperand()))
16719+ return SDValue();
16720+
16721+ // If the range metadata type does not match the new memory
16722+ // operation type, remove the range metadata.
16723+ if (const MDNode *MD = LN0->getRanges()) {
16724+ ConstantInt *Lower = mdconst::extract<ConstantInt>(MD->getOperand(0));
16725+ if (Lower->getBitWidth() != VT.getScalarSizeInBits() || !VT.isInteger()) {
16726+ LN0->getMemOperand()->clearRanges();
1672016727 }
16721- SDValue Load =
16722- DAG.getLoad(VT, SDLoc(N), LN0->getChain(), LN0->getBasePtr(),
16723- LN0->getMemOperand());
16724- DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
16725- return Load;
1672616728 }
16727- }
16729+ SDValue Load = DAG.getLoad(VT, DL, LN0->getChain(), LN0->getBasePtr(),
16730+ LN0->getMemOperand());
16731+ DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
16732+ return Load;
16733+ };
16734+
16735+ if (SDValue NewLd = CastLoad(N0, SDLoc(N)))
16736+ return NewLd;
16737+
16738+ if (N0.getOpcode() == ISD::FREEZE && N0.hasOneUse())
16739+ if (SDValue NewLd = CastLoad(N0.getOperand(0), SDLoc(N)))
16740+ return DAG.getFreeze(NewLd);
1672816741
1672916742 if (SDValue V = foldBitcastedFPLogic(N, DAG, TLI))
1673016743 return V;
0 commit comments