<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 - Backend produces sub-optimal code for switch conditions with illegal types"
   href="https://bugs.llvm.org/show_bug.cgi?id=39569">39569</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Backend produces sub-optimal code for switch conditions with illegal types
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>denis.bakhvalov@intel.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=21094" name="attach_21094" title="Reproducer">attachment 21094</a> <a href="attachment.cgi?id=21094&action=edit" title="Reproducer">[details]</a></span>
Reproducer

This bug is forked from <a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Redundent and instruction caused by switch condition operand shrinking"
   href="show_bug.cgi?id=29009">https://bugs.llvm.org/show_bug.cgi?id=29009</a>.

If we have illegal types for switch conditions:

  %trunc = trunc i32 %3 to i3
  switch i3 %trunc, label %11 [
    i3 0, label %4
    i3 3, label %5
    i3 -2, label %6
    i3 2, label %7
    i3 -4, label %8
    i3 -3, label %9
  ]

backend will generate sub-optimal code (notice redundant MOV's and AND's):

$ llc 1.ll

        movl    %eax, %edx
        andb    $7, %dl
        movl    $6, %ecx
        cmpb    $7, %dl
        je      .LBB0_10
# %bb.3:                                
        movl    %eax, %edx
        andl    $7, %edx
        jmpq    *.LJTI0_0(,%rdx,8)

Full reproducer attached.

Comments by Craig Topper:

CodeGenPrepare inserts a zext to turn the i3 back into i8. This leaves us with
a trunc+zext pair. This non-canonical IR as far as InstCombine would say.
Canonical IR would be trunc to i8 followed by an and with 7.

Then SelectionDAGBuilder inserts a zext i8 to i64 for the jump table lookup.
getNode sees that we have a (i32 (zext (i8 (zext (i3)))) and decides that we
should by pass the i3->i8 zext. So now we have an i3->i64 zext and a i3->i8
zext seprately.

Now DAG combine runs and a setcc optimization runs that sees (setcc (i8 (zext
(i3 trunc)))) and replaces it with (setcc (i3 trunc)). Separately the (i64
(zext (i3 trunc)) pair is turned into a (i64 (and (any_extend (i32)), 7).

Then type legalization runs and rewrites the setcc input to an (i8 (and
(trunc(i3)), 7)).

And that's how we got 2 different ands.

I tried making codegen prepare emit canonical IR so the i3 type would
disappear. But DAGCombiner still seems to prefer turning (zext (and)) into (and
(any_extend)) even when the 'and' has an additional user.

I also tried a patch to force SelectionDAGBuilder to emit the zext for the
jumptable in the basic block with the jump table load instead of in the basic
block with the setcc. That actually produces reasonable code, but I didn't see
much perf change from our benchmark list.</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>