<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 - ValueTracking: Compute known bits doesn't leverage range information on gep"
   href="https://bugs.llvm.org/show_bug.cgi?id=47241">47241</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>ValueTracking: Compute known bits doesn't leverage range information on gep
          </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>All
          </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>Global Analyses
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>quentin.colombet@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=23863" name="attach_23863" title="Case that gets simplified">attachment 23863</a> <a href="attachment.cgi?id=23863&action=edit" title="Case that gets simplified">[details]</a></span>
Case that gets simplified

The ValueTracking analysis only tries to fill up trailing zeros when going
through GEPs.
In particular, the analysis doesn't take advantage of the range information
that may be available, resulting and suboptimal code.

* Example *

Consider the following snippet:
```
define i1 @test4_gep(i64* %ptr) {
entry:
  %val = load volatile i64, i64* %ptr, !range !{i64 64, i64 65536}
  %valptr = inttoptr i64 %val to float*
  %gep = getelementptr float, float* %valptr, i64 128 
  %res = icmp ugt float* %gep, inttoptr (i64 523 to float*)
  ret i1 %res
}
```

Using the range information this example can be simplified into the following
code, since 64 + 4*128 is guaranteed to be bigger than 523:
```
define i1 @test4_gep(i64* %ptr) {
entry:
  %val = load volatile i64, i64* %ptr, !range !{i64 64, i64 65536}
  ret i1 true
}
```

But given ValueTracking doesn't leverage the range information, no
simplification happens.

Now, rewrite this code with an add like so:
```
define i1 @test3_add(i64* %ptr) {
entry:
  %val = load volatile i64, i64* %ptr, !range !{i64 64, i64 65536}
  %valPlus512 = add i64 %val, 512
  %res = icmp ugt i64 %valPlus512, 523
  ret i1 %res
}
```

And ValueTracking provides the proper known bits and the simplification can
happen.

* To Reproduce *
# Not simplified.
opt -S -instcombine value_tracking_gep.ll  -o -
# Simplified.
opt -S -instcombine value_tracking_add.ll  -o -

Note: I have to use a volatile load to prevent instcombine for dropping the
range information. I'll file a separate PR for that.</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>