[llvm] [InstCombine] Avoid overflow in `foldVecExtTruncToExtElt` (PR #180414)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 8 07:43:22 PST 2026


================
@@ -711,17 +715,19 @@ static Instruction *foldVecExtTruncToExtElt(TruncInst &Trunc,
       return nullptr;
 
     uint64_t IdxOfs = ShiftAmount->udiv(DstBits).getZExtValue();
+    // IdxOfs is guaranteed to be less than TruncRatio, so we won't overflow in
+    // the adjustment.
     NewIdx = IC.getDataLayout().isBigEndian() ? (NewIdx - IdxOfs)
                                               : (NewIdx + IdxOfs);
   }
 
   assert(BitCastNumElts <= std::numeric_limits<uint32_t>::max() &&
-         NewIdx <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
+         "overflow 32-bits");
 
   auto *BitCastTo =
       VectorType::get(DstType, BitCastNumElts, VecElts.isScalable());
   Value *BitCast = IC.Builder.CreateBitCast(VecOp, BitCastTo);
-  return ExtractElementInst::Create(BitCast, IC.Builder.getInt32(NewIdx));
+  return ExtractElementInst::Create(BitCast, IC.Builder.getInt64(NewIdx));
----------------
dtcxzyw wrote:

`getPreferredVectorIndex` uses i64 as the canonical type of indices.

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


More information about the llvm-commits mailing list