diff options
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 25 |
2 files changed, 20 insertions, 9 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 5e43cb7..67669c2 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3723,7 +3723,7 @@ static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, 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 @@ static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, 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 diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 92d35a2..7ad2d46 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -2027,8 +2027,16 @@ static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) { // 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<ConstantInt>(Idxs[0])->isOne()) - return false; + if (auto *CI = dyn_cast<ConstantInt>(Idxs[0])) { + if (!CI->isOne()) + return false; + } else { + auto *CV = cast<ConstantDataVector>(Idxs[0]); + CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()); + if (!CI || !CI->isOne()) + return false; + } + for (unsigned i = 1, e = Idxs.size(); i != e; ++i) if (!cast<Constant>(Idxs[i])->isNullValue()) return false; @@ -2058,15 +2066,18 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, ArrayRef<Value *> Idxs) { if (Idxs.empty()) return C; - if (isa<UndefValue>(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<UndefValue>(C)) return UndefValue::get(GEPTy); - } Constant *Idx0 = cast<Constant>(Idxs[0]); if (Idxs.size() == 1 && (Idx0->isNullValue() || isa<UndefValue>(Idx0))) - return C; + return GEPTy->isVectorTy() && !C->getType()->isVectorTy() + ? ConstantVector::getSplat( + cast<VectorType>(GEPTy)->getNumElements(), C) + : C; if (C->isNullValue()) { bool isNull = true; |