<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - `add nsw` + `sadd.with.overflow` with constants should fold"
   href="https://bugs.llvm.org/show_bug.cgi?id=38146">38146</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>`add nsw` + `sadd.with.overflow` with constants should fold
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>me22.ca@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>```
  %3 = add nsw i32 %1, 7
  %4 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %3, i32 13)
```

should be folded to

```
  %4 = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %1, i32 20)
```

Repro in clang: <a href="https://godbolt.org/g/DM5Gq9">https://godbolt.org/g/DM5Gq9</a>

```
#include <optional>

std::optional<int> test_nsw_sadd_fold(int num) {
    num += 7;
    int r;
    if (__builtin_sadd_overflow(num, 13, &r)) {
        return {};
    }
    return r;
}
```

Cases for `.add_nsw(C1).sadd_overflow(C2)`:

- Same sign can be folded to a single overflow call
- Opposite signs with |C2|>|C1| can be folded to a single overflow call
- Opposite signs with |C2|<=|C1| can be folded to a single nsw call

(Similarly, just simpler, for `.add_nuw(C1).uadd_overflow(C2)`.)</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>