[llvm] [CodeGenPrepare] Fix signed overflow (PR #141487)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Mon May 26 11:01:42 PDT 2025


================
@@ -5447,11 +5447,18 @@ bool AddressingModeMatcher::matchAddr(Value *Addr, unsigned Depth) {
       TPT.getRestorationPoint();
   if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) {
     if (CI->getValue().isSignedIntN(64)) {
-      // Fold in immediates if legal for the target.
-      AddrMode.BaseOffs += CI->getSExtValue();
-      if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
-        return true;
-      AddrMode.BaseOffs -= CI->getSExtValue();
+      // Check if the addition would result in a signed overflow.
+      bool Overflow = false;
+      APInt BaseOffs(64, AddrMode.BaseOffs, true);
+      APInt CIVal(64, CI->getValue().getSExtValue(), true);
+      auto Result = BaseOffs.sadd_ov(CIVal, Overflow);
+      if (!Overflow) {
----------------
mshockwave wrote:

could we use `llvm::AddOverflow` from MathExtras.h?

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


More information about the llvm-commits mailing list