[llvm-bugs] [Bug 34623] New: Folding of select breaks min/max

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Sep 15 05:47:07 PDT 2017


https://bugs.llvm.org/show_bug.cgi?id=34623

            Bug ID: 34623
           Summary: Folding of select breaks min/max
           Product: libraries
           Version: 4.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Transformation Utilities
          Assignee: unassignedbugs at nondot.org
          Reporter: arsene.perard at laposte.net
                CC: llvm-bugs at lists.llvm.org

Created attachment 19159
  --> https://bugs.llvm.org/attachment.cgi?id=19159&action=edit
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
>     // 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;
>     }
>

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170915/61abd188/attachment.html>


More information about the llvm-bugs mailing list