<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 crashes in foldUDivShl when 2nd argument is constant"
   href="https://llvm.org/bugs/show_bug.cgi?id=27888">27888</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Instcombine crashes in foldUDivShl when 2nd argument is constant
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>mikael.holmen@ericsson.com
          </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=16420" name="attach_16420" title="Reproducer ll file">attachment 16420</a> <a href="attachment.cgi?id=16420&action=edit" title="Reproducer ll file">[details]</a></span>
Reproducer ll file

build-all/bin/opt -S -instcombine -o - ./RM10519_foldUDivShl.ll

gives

opt: ../include/llvm/Support/Casting.h:237: typename cast_retty<X, Y
*>::ret_type llvm::cast(Y *) [X = llvm::Instruction, Y = llvm::Value]:
Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.

With -debug:

INSTCOMBINE ITERATION #1 on f1
IC: ADDING: 4 instrs to worklist
IC: Visiting:   %_tmp1 = load i16, i16* @g
IC: Visiting:   %_tmp2 = udiv i16 %_tmp1, shl (i16 1, i16 zext (i2 ptrtoint
(void ()* @f1 to i2) to i16))

The code crashing is:

// X udiv (C1 << N), where C1 is "1<<C2"  -->  X >> (N+C2)
static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator
&I,
                                InstCombiner &IC) {
  Instruction *ShiftLeft = cast<Instruction>(Op1);
  if (isa<ZExtInst>(ShiftLeft))
    ShiftLeft = cast<Instruction>(ShiftLeft->getOperand(0));

  const APInt &CI =
      cast<Constant>(ShiftLeft->getOperand(0))->getUniqueInteger();

Op1 isn't an Instruction so the cast<Instruction>(Op1) fails.

As seen in the debug printout Op1 is

shl (i16 1, i16 zext (i2 ptrtoint (void ()* @f1 to i2) to i16))

The following change seems to solve the problem:

-  Instruction *ShiftLeft = cast<Instruction>(Op1);
-  if (isa<ZExtInst>(ShiftLeft))
-    ShiftLeft = cast<Instruction>(ShiftLeft->getOperand(0));
+  User *ShiftLeft;
+  if (isa<Constant>(Op1)) {
+    ShiftLeft = cast<User>(Op1);
+  } else {
+    Instruction *ShiftLeftInstr = cast<Instruction>(Op1);
+    if (isa<ZExtInst>(ShiftLeftInstr))
+      ShiftLeftInstr = cast<Instruction>(ShiftLeftInstr->getOperand(0));
+    ShiftLeft = ShiftLeftInstr;
+  }</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>