<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 - Folding of select breaks min/max"
   href="https://bugs.llvm.org/show_bug.cgi?id=34623">34623</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Folding of select breaks min/max
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>4.0
          </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>enhancement
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Transformation Utilities
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>arsene.perard@laposte.net
          </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=19159" name="attach_19159" title="Patch to fix the incorrect folding of selects">attachment 19159</a> <a href="attachment.cgi?id=19159&action=edit" title="Patch to fix the incorrect folding of selects">[details]</a></span>
Patch to fix the incorrect folding of selects

The following LLVM IR:

define <8 x float> @minmax(<8 x float> %a, <8 x float> %b) {
  %a_bc = bitcast <8 x float> %a to <8 x i32>
  %b_bc = bitcast <8 x float> %b to <8 x i32>
  %cmp = icmp slt <8 x i32> %a_bc, %b_bc
  %min = select <8 x i1> %cmp, <8 x i32> %a_bc, <8 x i32> %b_bc
  %min_bc = bitcast <8 x i32> %min to <8 x float>
  ret <8 x float> %min_bc
}

produces:

minmax:                                 # @minmax
# BB#0:
        vpcmpgtd        %ymm0, %ymm1, %ymm2
        vblendvps       %ymm2, %ymm0, %ymm1, %ymm0
        retq

when compiled with

clang -O3 -mavx -mavx2

Here, the cmpgt + blendv can be replaced by a single minsd instruction on AVX2
hardware.
After some investigation, it turns out the problem is the folding of the select
in lib/Transforms/InstCombine/InstCombineSelect.cpp:157 (in LLVM 4.0.1). There
is no check to prevent folding selects that match a min/max pattern (through
bitcasts) in llvm::InstCombiner::foldSelectOpOp.

I have a small patch for this issue that works in LLVM 4.0.1 (also attached to
this bug report):

156a157,165
<span class="quote">>     // Prevent folding if the select matches a min/max pattern
>     {
>       Value *left, *right;
>       Instruction::CastOps castOp;
>       auto selectPattern = matchSelectPattern(&SI, left, right, &castOp);
>       if (SelectPatternResult::isMinOrMax(selectPattern.Flavor))
>         return nullptr;
>     }</span >
></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>