<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 drops load !nonnull metadata on an inttoptr/ptrtoint"
   href="https://llvm.org/bugs/show_bug.cgi?id=30597">30597</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>instcombine drops load !nonnull metadata on an inttoptr/ptrtoint
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

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

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>arielb1@mail.tau.ac.il
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>## Description

When instcombine simplifies a inttoptr/ptrtoint pair that covers a load,
!nonnull metadata on the load seems to be dropped on the floor, leading to
missed optimizations.

This is one of the causes of <a href="https://github.com/rust-lang/rust/issues/36920">https://github.com/rust-lang/rust/issues/36920</a>
(the current version of rustc does not emit the required nonnull metadata, but
modifying it to emit the metadata leads to this issue).

Tested on Rust's fork of LLVM 3.9 and Debian's LLVM 3.7.

## STR

; ModuleID = 'test.cgu-0.rs'
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Function Attrs: readonly uwtable
define i32 @dot_ref_s(i32** noalias nocapture readonly dereferenceable(8)) {
entry-block:
  %loadedptr = load i32*, i32** %0, align 8, !nonnull !0
  %ptrtoint = ptrtoint i32* %loadedptr to i64
  %inttoptr = inttoptr i64 %ptrtoint to i32*
  %switchtmp = icmp eq i32* %inttoptr, null
  br i1 %switchtmp, label %bb6, label %bb7

bb6:
  ret i32 0

bb7:
  ret i32 42
}

!0 = !{}

## Expected Result

The null check is optimized out.

## Actual Result

; ModuleID = '<stdin>'
source_filename = "test.cgu-0.rs"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define i32 @dot_ref_s(i32** noalias nocapture readonly dereferenceable(8)) {
entry-block:
  %loadedptr12 = load i32*, i32** %0, align 8     ; <- where is my metadata?
  %switchtmp = icmp eq i32* %loadedptr12, null
  br i1 %switchtmp, label %bb6, label %bb7

bb6:                                              ; preds = %entry-block
  ret i32 0

bb7:                                              ; preds = %entry-block
  ret i32 42
}</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>