<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 - InstCombine fails when constant folding gep with vector result: Assertion `New->getType() == getType() && "replaceAllUses of value with new value of different type!"'"
   href="https://bugs.llvm.org/show_bug.cgi?id=34627">34627</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>InstCombine fails when constant folding gep with vector result: Assertion `New->getType() == getType() && "replaceAllUses of value with new value of different type!"'
          </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>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>Scalar Optimizations
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>mattias.v.eriksson@ericsson.com
          </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=19163" name="attach_19163" title="Bugpointed reproducer">attachment 19163</a> <a href="attachment.cgi?id=19163&action=edit" title="Bugpointed reproducer">[details]</a></span>
Bugpointed reproducer

This fails for me on trunk:

opt -instcombine  -S -o /dev/null ic.ll -debug

INSTCOMBINE ITERATION #1 on foo
IC: ConstFold to: <2 x i16> <i16 0, i16 undef> from:   %broadcast.splatinsert =
insertelement <2 x i16> undef, i16 0, i32 0
IC: ConstFold to: <2 x i16> zeroinitializer from:   %broadcast.splat =
shufflevector <2 x i16> <i16 0, i16 undef>, <2 x i16> undef, <2 x i32>
zeroinitializer
IC: DCE:   %induction = add <2 x i16> zeroinitializer, <i16 0, i16 1>
IC: ConstFold to: <2 x i16> undef from:   %0 = insertelement <2 x i16> undef,
i16 undef, i32 0
IC: ConstFold to: <2 x i16> undef from:   %0 = insertelement <2 x i16> undef,
i16 undef, i32 1
IC: ConstFold to: %stc2* null from:   %0 = getelementptr inbounds [10 x %stc2],
[10 x %stc2]* null, i16 0, <2 x i16> undef

[Note that the last argument to this getelementptr is a vector.]

opt: /home/qmaseon/git/llvm/lib/IR/Value.cpp:402: void
llvm::Value::doRAUW(llvm::Value *, bool): Assertion `New->getType() ==
getType() && "replaceAllUses of value with new value of different type!"'
failed.
#0 0x00000000031a8f6c llvm::sys::PrintStackTrace(llvm::raw_ostream&)
/home/qmaseon/git/llvm/lib/Support/Unix/Signals.inc:398:13
#1 0x00000000031a9119 PrintStackTraceSignalHandler(void*)
/home/qmaseon/git/llvm/lib/Support/Unix/Signals.inc:462:1
#2 0x00000000031a7603 llvm::sys::RunSignalHandlers()
/home/qmaseon/git/llvm/lib/Support/Signals.cpp:50:5
#3 0x00000000031a97af SignalHandler(int)
/home/qmaseon/git/llvm/lib/Support/Unix/Signals.inc:252:1
#4 0x00007f89af0b8330 __restore_rt
(/lib/x86_64-linux-gnu/libpthread.so.0+0x10330)
#5 0x00007f89adca7c37 gsignal
/build/eglibc-SvCtMH/eglibc-2.19/signal/../nptl/sysdeps/unix/sysv/linux/raise.c:56:0
#6 0x00007f89adcab028 abort
/build/eglibc-SvCtMH/eglibc-2.19/stdlib/abort.c:91:0
#7 0x00007f89adca0bf6 __assert_fail_base
/build/eglibc-SvCtMH/eglibc-2.19/assert/assert.c:92:0
#8 0x00007f89adca0ca2 (/lib/x86_64-linux-gnu/libc.so.6+0x2fca2)
#9 0x00000000029fac31 llvm::Value::doRAUW(llvm::Value*, bool)
/home/qmaseon/git/llvm/lib/IR/Value.cpp:405:7
#10 0x00000000029fb222 llvm::Value::replaceAllUsesWith(llvm::Value*)
/home/qmaseon/git/llvm/lib/IR/Value.cpp:430:1
#11 0x0000000002b96316 AddReachableCodeToWorklist(llvm::BasicBlock*,
llvm::DataLayout const&, llvm::SmallPtrSetImpl<llvm::BasicBlock*>&,
llvm::InstCombineWorklist&, llvm::TargetLibraryInfo const*)
/home/qmaseon/git/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp:3098:11
#12 0x0000000002b95ed3 prepareICWorklistFromFunction(llvm::Function&,
llvm::DataLayout const&, llvm::TargetLibraryInfo*, llvm::InstCombineWorklist&)
/home/qmaseon/git/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp:3179:7
#13 0x0000000002b9577d combineInstructionsOverFunction(llvm::Function&,
llvm::InstCombineWorklist&, llvm::AAResults*, llvm::AssumptionCache&,
llvm::TargetLibraryInfo&, llvm::DominatorTree&,
llvm::OptimizationRemarkEmitter&, bool, llvm::LoopInfo*)
/home/qmaseon/git/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp:3229:21
#14 0x0000000002b95ad1
llvm::InstructionCombiningPass::runOnFunction(llvm::Function&)
/home/qmaseon/git/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp:3293:10
#15 0x0000000002971210 llvm::FPPassManager::runOnFunction(llvm::Function&)
/home/qmaseon/git/llvm/lib/IR/LegacyPassManager.cpp:1514:23
#16 0x0000000002971545 llvm::FPPassManager::runOnModule(llvm::Module&)
/home/qmaseon/git/llvm/lib/IR/LegacyPassManager.cpp:1535:16
#17 0x0000000002971d31 (anonymous
namespace)::MPPassManager::runOnModule(llvm::Module&)
/home/qmaseon/git/llvm/lib/IR/LegacyPassManager.cpp:1591:23
#18 0x000000000297182b llvm::legacy::PassManagerImpl::run(llvm::Module&)
/home/qmaseon/git/llvm/lib/IR/LegacyPassManager.cpp:1694:16
#19 0x0000000002972271 llvm::legacy::PassManager::run(llvm::Module&)
/home/qmaseon/git/llvm/lib/IR/LegacyPassManager.cpp:1725:10
#20 0x0000000000b5ea4d main /home/qmaseon/git/llvm/tools/opt/opt.cpp:757:3
#21 0x00007f89adc92f45 __libc_start_main
/build/eglibc-SvCtMH/eglibc-2.19/csu/libc-start.c:321:0
#22 0x0000000000b32dc9 _start (/mnt/tb/builds/build-master/bin/opt+0xb32dc9)

I think the bug is in the function ConstantFoldGetElementPtr:
    if (isNull) {
      PointerType *PtrTy = cast<PointerType>(C->getType()->getScalarType());
      Type *Ty = GetElementPtrInst::getIndexedType(PointeeTy, Idxs);

      assert(Ty && "Invalid indices for GEP!");
      Type *GEPTy = PointerType::get(Ty, PtrTy->getAddressSpace());
      if (VectorType *VT = dyn_cast<VectorType>(C->getType()))
        GEPTy = VectorType::get(GEPTy, VT->getNumElements());

      // I think for this case we should return a NullValue of VectorType.

      return Constant::getNullValue(GEPTy);
    }
  }</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>