[llvm] Preserve range metadata when load is narrowed (PR #128144)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 22 09:55:43 PST 2025


================
@@ -14903,12 +14903,40 @@ SDValue DAGCombiner::reduceLoadWidth(SDNode *N) {
   AddToWorklist(NewPtr.getNode());
 
   SDValue Load;
-  if (ExtType == ISD::NON_EXTLOAD)
-    Load = DAG.getLoad(VT, DL, LN0->getChain(), NewPtr,
-                       LN0->getPointerInfo().getWithOffset(PtrOff),
-                       LN0->getOriginalAlign(),
-                       LN0->getMemOperand()->getFlags(), LN0->getAAInfo());
-  else
+  if (ExtType == ISD::NON_EXTLOAD) {
+    const MDNode *OldRanges = LN0->getRanges();
+    const MDNode *NewRanges = nullptr;
+    /* If LSBs are loaded and all bounds in the OldRanges metadata fit in
+       the narrower size, preserve the range information by translating
+       to the the new narrower type, NewTy */
+    if (ShAmt == 0 && OldRanges) {
+      Type *NewTy = VT.getTypeForEVT(*DAG.getContext());
+      const unsigned NumOperands = OldRanges->getNumOperands();
+      const unsigned NewWidth = NewTy->getIntegerBitWidth();
+      Metadata *Bounds[2];
+      /* Don't preserve all sub-ranges.  Make one range that contains all
+         OldRanges. Use lower bound of first range and higher bound of last
+         range */
+      const APInt &LowValue =
+          mdconst::extract<ConstantInt>(OldRanges->getOperand(0))->getValue();
+      const APInt &HighValue =
+          mdconst::extract<ConstantInt>(OldRanges->getOperand(NumOperands - 1))
+              ->getValue();
+      ConstantRange CurRange(LowValue, HighValue);
----------------
nikic wrote:

Use getConstantRangeFromMetadata() here.

https://github.com/llvm/llvm-project/pull/128144


More information about the llvm-commits mailing list