diff --git a/include/llvm/IR/PatternMatch.h b/include/llvm/IR/PatternMatch.h index 2efb2948947..530ad824b37 100644 --- a/include/llvm/IR/PatternMatch.h +++ b/include/llvm/IR/PatternMatch.h @@ -362,6 +362,29 @@ struct bind_const_intval_ty { } }; +/// Match a specified integer value or vector of all elements of that value. +struct specific_intval { + uint64_t Val; + specific_intval(uint64_t V) : Val(V) {} + + template + bool match(ITy *V) { + ConstantInt *CI = dyn_cast(V); + if (!CI && V->getType()->isVectorTy()) + if (const auto *C = dyn_cast(V)) + CI = dyn_cast_or_null(C->getSplatValue()); + + if (CI && CI->getBitWidth() <= 64) + return CI->getZExtValue() == Val; + + return false; + } +}; + +/// Match a specific integer value or vector with all elements equal to the +/// value. +inline specific_intval m_SpecificInt(uint64_t V) { return specific_intval(V); } + /// m_ConstantInt - Match a ConstantInt and bind to its value. This does not /// match ConstantInts wider than 64-bits. inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; } diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index da5c83ca8e4..118579d5b72 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -2799,29 +2799,71 @@ Value *llvm::SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, static Value *SimplifyGEPInst(ArrayRef Ops, const Query &Q, unsigned) { // The type of the GEP pointer operand. PointerType *PtrTy = cast(Ops[0]->getType()->getScalarType()); + unsigned AS = PtrTy->getAddressSpace(); // getelementptr P -> P. if (Ops.size() == 1) return Ops[0]; - if (isa(Ops[0])) { - // Compute the (pointer) type returned by the GEP instruction. - Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1)); - Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace()); - if (VectorType *VT = dyn_cast(Ops[0]->getType())) - GEPTy = VectorType::get(GEPTy, VT->getNumElements()); + // Compute the (pointer) type returned by the GEP instruction. + Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1)); + Type *GEPTy = PointerType::get(LastType, AS); + if (VectorType *VT = dyn_cast(Ops[0]->getType())) + GEPTy = VectorType::get(GEPTy, VT->getNumElements()); + + if (isa(Ops[0])) return UndefValue::get(GEPTy); - } if (Ops.size() == 2) { // getelementptr P, 0 -> P. if (match(Ops[1], m_Zero())) return Ops[0]; - // getelementptr P, N -> P if P points to a type of zero size. - if (Q.DL) { - Type *Ty = PtrTy->getElementType(); - if (Ty->isSized() && Q.DL->getTypeAllocSize(Ty) == 0) + + Type *Ty = PtrTy->getElementType(); + if (Q.DL && Ty->isSized()) { + Value *P; + 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) return Ops[0]; + + // The following transforms are only safe if the ptrtoint cast + // doesn't truncate the pointers. + if (Ops[1]->getType()->getScalarSizeInBits() == + Q.DL->getPointerSizeInBits(AS)) { + auto PtrToIntOrZero = [GEPTy](Value *P) -> Value * { + if (match(P, m_Zero())) + return Constant::getNullValue(GEPTy); + Value *Temp; + if (match(P, m_PtrToInt(m_Value(Temp)))) + return Temp; + return nullptr; + }; + + // getelementptr V, (sub P, V) -> P if P points to a type of size 1. + if (TyAllocSize == 1 && + match(Ops[1], m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))))) + if (Value *R = PtrToIntOrZero(P)) + return R; + + // getelementptr V, (ashr (sub P, V), C) -> Q + // if P points to a type of size 1 << C. + if (match(Ops[1], + m_AShr(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), + m_ConstantInt(C))) && + TyAllocSize == 1ULL << C) + if (Value *R = PtrToIntOrZero(P)) + return R; + + // getelementptr V, (sdiv (sub P, V), C) -> Q + // if P points to a type of size C. + if (match(Ops[1], + m_SDiv(m_Sub(m_Value(P), m_PtrToInt(m_Specific(Ops[0]))), + m_SpecificInt(TyAllocSize)))) + if (Value *R = PtrToIntOrZero(P)) + return R; + } } } diff --git a/test/Transforms/InstSimplify/gep.ll b/test/Transforms/InstSimplify/gep.ll new file mode 100644 index 00000000000..d1704bf80bc --- /dev/null +++ b/test/Transforms/InstSimplify/gep.ll @@ -0,0 +1,66 @@ +; RUN: opt -S -instsimplify < %s | FileCheck %s + +target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" + +%struct.A = type { [7 x i8] } + +define %struct.A* @test1(%struct.A* %b, %struct.A* %e) { + %e_ptr = ptrtoint %struct.A* %e to i64 + %b_ptr = ptrtoint %struct.A* %b to i64 + %sub = sub i64 %e_ptr, %b_ptr + %sdiv = sdiv exact i64 %sub, 7 + %gep = getelementptr inbounds %struct.A* %b, i64 %sdiv + ret %struct.A* %gep +; CHECK-LABEL: @test1 +; CHECK-NEXT: ret %struct.A* %e +} + +define i8* @test2(i8* %b, i8* %e) { + %e_ptr = ptrtoint i8* %e to i64 + %b_ptr = ptrtoint i8* %b to i64 + %sub = sub i64 %e_ptr, %b_ptr + %gep = getelementptr inbounds i8* %b, i64 %sub + ret i8* %gep +; CHECK-LABEL: @test2 +; CHECK-NEXT: ret i8* %e +} + +define i64* @test3(i64* %b, i64* %e) { + %e_ptr = ptrtoint i64* %e to i64 + %b_ptr = ptrtoint i64* %b to i64 + %sub = sub i64 %e_ptr, %b_ptr + %ashr = ashr exact i64 %sub, 3 + %gep = getelementptr inbounds i64* %b, i64 %ashr + ret i64* %gep +; CHECK-LABEL: @test3 +; CHECK-NEXT: ret i64* %e +} + +define %struct.A* @test4(%struct.A* %b) { + %b_ptr = ptrtoint %struct.A* %b to i64 + %sub = sub i64 0, %b_ptr + %sdiv = sdiv exact i64 %sub, 7 + %gep = getelementptr inbounds %struct.A* %b, i64 %sdiv + ret %struct.A* %gep +; CHECK-LABEL: @test4 +; CHECK-NEXT: ret %struct.A* null +} + +define i8* @test5(i8* %b) { + %b_ptr = ptrtoint i8* %b to i64 + %sub = sub i64 0, %b_ptr + %gep = getelementptr inbounds i8* %b, i64 %sub + ret i8* %gep +; CHECK-LABEL: @test5 +; CHECK-NEXT: ret i8* null +} + +define i64* @test6(i64* %b) { + %b_ptr = ptrtoint i64* %b to i64 + %sub = sub i64 0, %b_ptr + %ashr = ashr exact i64 %sub, 3 + %gep = getelementptr inbounds i64* %b, i64 %ashr + ret i64* %gep +; CHECK-LABEL: @test6 +; CHECK-NEXT: ret i64* null +}