[llvm] [ConstantRange] Bail out early in unsignedMulMayOverflow for wide integer types (PR #192275)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 15 08:45:58 PDT 2026


Firebear518 wrote:

Hi @RKSimon,

That's a great point. I did look into APInt::umul_ov, and you're right that the core issue lies there.

Currently, umul_ov detects overflow early via the countl_zero check, but still proceeds to compute the full O(n²) product to satisfy the APInt return value contract. In ConstantRange::unsignedMulMayOverflow, both return values are explicitly discarded with (void), so this work is entirely wasted for very wide types.

To fix this at the APInt level, we would likely need either:                                                                                         
- A new API — something like bool APInt::umul_overflows(const APInt &RHS) that only returns the boolean flag without computing the product, or
- A faster algorithm — replacing the current O(n²) schoolbook multiplication in tcMultiply with Karatsuba or similar.

Both felt like significantly larger changes. My intention with this PR was to provide a minimal, safe guard specifically for ConstantRange analysis. 

I'm happy to explore the APInt improvement as a follow-up patch if you feel that's the better long-term path. Would you prefer I keep this localized fix and address APInt separately?

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


More information about the llvm-commits mailing list