<html>
    <head>
      <base href="https://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 tries to create invalid IR"
   href="https://llvm.org/bugs/show_bug.cgi?id=27236">27236</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Instcombine tries to create invalid IR
          </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>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>anton@korobeynikov.info
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=16174" name="attach_16174" title="Testcase">attachment 16174</a> <a href="attachment.cgi?id=16174&action=edit" title="Testcase">[details]</a></span>
Testcase

Consider the attached IR.

opt -instcombine -debug yields:

IC: Replacing   %.sroa.4.0 = select i1 %4, float %3, float 0.000000e+00
    with   %2 = select i1 %1, i32 1, i32 %0
Assertion failed: (New->getType() == getType() && "replaceAllUses of value with
new value of different type!"), function replaceAllUsesWith, file
/Users/asl/Projects/llvm/2commit/llvm/lib/IR/Value.cpp, line 375.

Which is certainly incorrect. I believe the problem was exposed by the recent
changes in min/max optimizations and min(min) / max(max) folding.

The "big hammer" fix is to have something like this:

Index: lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineSelect.cpp    (revision 265487)
+++ lib/Transforms/InstCombine/InstCombineSelect.cpp    (working copy)
@@ -642,6 +642,9 @@
                                         Value *A, Value *B,
                                         Instruction &Outer,
                                         SelectPatternFlavor SPF2, Value *C) {
+  if (Outer.getType() != Inner->getType())
+    return nullptr;
+
   if (C == A || C == B) {
     // MAX(MAX(A, B), B) -> MAX(A, B)
     // MIN(MIN(a, b), a) -> MIN(a, b)

However, I believe we need to make sure we never call stuff this way</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>