@@ -14903,12 +14903,39 @@ SDValue DAGCombiner::reduceLoadWidth(SDNode *N) {
1490314903 AddToWorklist(NewPtr.getNode());
1490414904
1490514905 SDValue Load;
14906- if (ExtType == ISD::NON_EXTLOAD)
14907- Load = DAG.getLoad(VT, DL, LN0->getChain(), NewPtr,
14908- LN0->getPointerInfo().getWithOffset(PtrOff),
14909- LN0->getOriginalAlign(),
14910- LN0->getMemOperand()->getFlags(), LN0->getAAInfo());
14911- else
14906+ if (ExtType == ISD::NON_EXTLOAD) {
14907+ const MDNode *OldRanges = LN0->getRanges();
14908+ const MDNode *NewRanges = nullptr;
14909+ /* If LSBs are loaded and all bounds in the OldRanges metadata fit in
14910+ the narrower size, preserve the range information by translating
14911+ to the the new narrower type, NewTy */
14912+ if (ShAmt == 0 && OldRanges) {
14913+ Type *NewTy = VT.getTypeForEVT(*DAG.getContext());
14914+ const unsigned NumOperands = OldRanges->getNumOperands();
14915+ const unsigned NewWidth = NewTy->getIntegerBitWidth();
14916+ bool InRange = true;
14917+ SmallVector<Metadata *, 4> Bounds;
14918+ Bounds.reserve(NumOperands);
14919+
14920+ for (unsigned i = 0; i < NumOperands; ++i) {
14921+ const APInt &BoundValue =
14922+ mdconst::extract<ConstantInt>(OldRanges->getOperand(i))->getValue();
14923+ if (BoundValue.getBitWidth() - BoundValue.getNumSignBits() >=
14924+ NewWidth) {
14925+ InRange = false;
14926+ break;
14927+ }
14928+ Bounds.push_back(ConstantAsMetadata::get(
14929+ ConstantInt::get(NewTy, BoundValue.trunc(NewWidth))));
14930+ }
14931+ if (InRange)
14932+ NewRanges = MDNode::get(*DAG.getContext(), Bounds);
14933+ }
14934+ Load = DAG.getLoad(
14935+ VT, DL, LN0->getChain(), NewPtr,
14936+ LN0->getPointerInfo().getWithOffset(PtrOff), LN0->getOriginalAlign(),
14937+ LN0->getMemOperand()->getFlags(), LN0->getAAInfo(), NewRanges);
14938+ } else
1491214939 Load = DAG.getExtLoad(ExtType, DL, VT, LN0->getChain(), NewPtr,
1491314940 LN0->getPointerInfo().getWithOffset(PtrOff), ExtVT,
1491414941 LN0->getOriginalAlign(),
0 commit comments