<html>
    <head>
      <base href="http://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 removes an icmp and select"
   href="http://llvm.org/bugs/show_bug.cgi?id=21199">21199</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>InstCombine removes an icmp and select
          </td>
        </tr>

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

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

        <tr>
          <th>Hardware</th>
          <td>All
          </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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>hans@chromium.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Consider the following IR:


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

declare void @f(i32)

define void @test(i32 %len) {
entry:
  %cmp = icmp ult i32 %len, 8
  %cond = select i1 %cmp, i32 %len, i32 8
  %cmp11 = icmp ult i32 0, %cond
  br i1 %cmp11, label %for.body, label %for.end

for.body:                                         ; preds = %entry, %for.body
  %i.02 = phi i32 [ %inc, %for.body ], [ 0, %entry ]
  tail call void @f(i32 %cond)
  %inc = add i32 %i.02, 1
  %cmp1 = icmp ult i32 %inc, %cond
  br i1 %cmp1, label %for.body, label %for.end

for.end:                                          ; preds = %for.body, %entry
  ret void
}

Passing it through instcombine like so:

$ bin/opt -instcombine < /tmp/c.ll -S -o -

Results in the first comparison and select going missing:

define void @test(i32 %len) {
entry:
  %0 = icmp eq i32 %len, 0
  br i1 %0, label %for.end, label %for.body

for.body:                                         ; preds = %entry, %for.body
  %i.02 = phi i32 [ %inc, %for.body ], [ 0, %entry ]
  tail call void @f(i32 %len)
  %inc = add i32 %i.02, 1
  %cmp1 = icmp ult i32 %inc, %len
  br i1 %cmp1, label %for.body, label %for.end</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>