Index: llvm/trunk/lib/Analysis/InstructionSimplify.cpp =================================================================== --- llvm/trunk/lib/Analysis/InstructionSimplify.cpp +++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp @@ -3723,7 +3723,7 @@ if (Ops.size() == 2) { // getelementptr P, 0 -> P. - if (match(Ops[1], m_Zero())) + if (match(Ops[1], m_Zero()) && Ops[0]->getType() == GEPTy) return Ops[0]; Type *Ty = SrcTy; @@ -3732,7 +3732,7 @@ uint64_t C; uint64_t TyAllocSize = Q.DL.getTypeAllocSize(Ty); // getelementptr P, N -> P if P points to a type of zero size. - if (TyAllocSize == 0) + if (TyAllocSize == 0 && Ops[0]->getType() == GEPTy) return Ops[0]; // The following transforms are only safe if the ptrtoint cast Index: llvm/trunk/lib/IR/ConstantFold.cpp =================================================================== --- llvm/trunk/lib/IR/ConstantFold.cpp +++ llvm/trunk/lib/IR/ConstantFold.cpp @@ -2027,8 +2027,16 @@ // If the first index is one and all the rest are zero, it's in bounds, // by the one-past-the-end rule. - if (!cast(Idxs[0])->isOne()) - return false; + if (auto *CI = dyn_cast(Idxs[0])) { + if (!CI->isOne()) + return false; + } else { + auto *CV = cast(Idxs[0]); + CI = dyn_cast_or_null(CV->getSplatValue()); + if (!CI || !CI->isOne()) + return false; + } + for (unsigned i = 1, e = Idxs.size(); i != e; ++i) if (!cast(Idxs[i])->isNullValue()) return false; @@ -2058,15 +2066,18 @@ ArrayRef Idxs) { if (Idxs.empty()) return C; - if (isa(C)) { - Type *GEPTy = GetElementPtrInst::getGEPReturnType( - C, makeArrayRef((Value * const *)Idxs.data(), Idxs.size())); + Type *GEPTy = GetElementPtrInst::getGEPReturnType( + C, makeArrayRef((Value *const *)Idxs.data(), Idxs.size())); + + if (isa(C)) return UndefValue::get(GEPTy); - } Constant *Idx0 = cast(Idxs[0]); if (Idxs.size() == 1 && (Idx0->isNullValue() || isa(Idx0))) - return C; + return GEPTy->isVectorTy() && !C->getType()->isVectorTy() + ? ConstantVector::getSplat( + cast(GEPTy)->getNumElements(), C) + : C; if (C->isNullValue()) { bool isNull = true; Index: llvm/trunk/test/Analysis/ConstantFolding/vectorgep-crash.ll =================================================================== --- llvm/trunk/test/Analysis/ConstantFolding/vectorgep-crash.ll +++ llvm/trunk/test/Analysis/ConstantFolding/vectorgep-crash.ll @@ -38,3 +38,29 @@ %VectorGep = getelementptr [65 x %struct.A], [65 x %struct.A]* @G, <16 x i32> zeroinitializer, <16 x i64> , <16 x i32> zeroinitializer ret <16 x i32*> %VectorGep } + +@g = external global i8, align 1 + +define <2 x i8*> @constant_zero_index() { +; CHECK-LABEL: @constant_zero_index( +; CHECK-NEXT: ret <2 x i8*> +; + %gep = getelementptr i8, i8* @g, <2 x i64> zeroinitializer + ret <2 x i8*> %gep +} + +define <2 x i8*> @constant_undef_index() { +; CHECK-LABEL: @constant_undef_index( +; CHECK-NEXT: ret <2 x i8*> +; + %gep = getelementptr i8, i8* @g, <2 x i64> undef + ret <2 x i8*> %gep +} + +define <2 x i8*> @constant_inbounds() { +; CHECK-LABEL: @constant_inbounds( +; CHECK-NEXT: ret <2 x i8*> getelementptr inbounds (i8, i8* @g, <2 x i64> ) +; + %gep = getelementptr i8, i8* @g, <2 x i64> + ret <2 x i8*> %gep +} Index: llvm/trunk/test/Transforms/InstSimplify/vector_gep.ll =================================================================== --- llvm/trunk/test/Transforms/InstSimplify/vector_gep.ll +++ llvm/trunk/test/Transforms/InstSimplify/vector_gep.ll @@ -86,3 +86,21 @@ %patatino = getelementptr i8, <4 x i8 *> undef, i64 undef ret <4 x i8*> %patatino } + +define <2 x i8*> @zero_index(i8* %p) { +; CHECK-LABEL: @zero_index( +; CHECK-NEXT: %gep = getelementptr i8, i8* %p, <2 x i64> zeroinitializer +; CHECK-NEXT: ret <2 x i8*> %gep +; + %gep = getelementptr i8, i8* %p, <2 x i64> zeroinitializer + ret <2 x i8*> %gep +} + +define <2 x {}*> @unsized({}* %p) { +; CHECK-LABEL: @unsized( +; CHECK-NEXT: %gep = getelementptr {}, {}* %p, <2 x i64> undef +; CHECK-NEXT: ret <2 x {}*> %gep +; + %gep = getelementptr {}, {}* %p, <2 x i64> undef + ret <2 x {}*> %gep +}