[llvm-bugs] [Bug 27888] New: Instcombine crashes in foldUDivShl when 2nd argument is constant

via llvm-bugs llvm-bugs at lists.llvm.org
Thu May 26 04:12:38 PDT 2016


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

            Bug ID: 27888
           Summary: Instcombine crashes in foldUDivShl when 2nd argument
                    is constant
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: mikael.holmen at ericsson.com
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

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

-- 
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/20160526/f4607c26/attachment.html>


More information about the llvm-bugs mailing list