<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 - [InstCombine] GEP is wrongly optimized for 32-bit targets"
   href="https://bugs.llvm.org/show_bug.cgi?id=39908">39908</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[InstCombine] GEP is wrongly optimized for 32-bit targets
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </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>mati865@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Forwarding <a href="https://github.com/rust-lang/rust/issues/52026">https://github.com/rust-lang/rust/issues/52026</a>

Minimal example by neivv and rkruppe:
```
target datalayout = "p:32:32"

%S = type { [2 x i32] }

define i1 @foo([0 x %S]* %p, i32 %n) {
  %start.cast = bitcast [0 x %S]* %p to %S*
  %end = getelementptr inbounds [0 x %S], [0 x %S]* %p, i32 0, i32 %n, i32 0,
i32 0
  %end.cast = bitcast i32* %end to %S*
  %last = getelementptr inbounds %S, %S* %end.cast, i32 -1
  %cmp = icmp eq %S* %last, %start.cast
  ret i1 %cmp
}
```
GodBolt link: <a href="https://godbolt.org/z/C84Axg">https://godbolt.org/z/C84Axg</a>

For 32 bit targets it turns to:
define i1 @foo([0 x %S]* %p, i32 %n) {
  %cmp = icmp eq i32 %n, -536870911
  ret i1 %cmp
}

While 64 bit targets are optimized correctly:
define i1 @foo([0 x %S]* %p, i32 %n) {
  %cmp = icmp eq i32 %n, 1
  ret i1 %cmp
}

Explanation of the issue from
<a href="https://github.com/rust-lang/rust/issues/52026#issuecomment-403628144">https://github.com/rust-lang/rust/issues/52026#issuecomment-403628144</a>:
In this issue, the code is comparing roughly (S *)slice_start == (S
*)slice_start + slice_len - 1.
This function is extracting the constant byte offset, -8, and dividing it by 8,
sizeof(S). However, before that, compiling to a 32-bit target, the values are
masked by PtrSizeMask 0xFFFFFFFF, which discards the sign bit of int64 and
gives +4294967288, which divided by 8 is the same 536870911 which ends up being
in the result.</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>