Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp =================================================================== --- llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -1107,8 +1107,18 @@ DecomposedGEP DecompGEP2 = DecomposeGEPExpression(V2, DL, &AC, DT); // Bail if we were not able to decompose anything. - if (DecompGEP1.Base == GEP1 && DecompGEP2.Base == V2) - return AliasResult::MayAlias; + if (DecompGEP1.Base == GEP1 && DecompGEP2.Base == V2) { + // Check to see if this V1 is to a constant offset from the V2 ptr. + if (!isa(V2)) + return AliasResult::MayAlias; + + auto *EltTy1 = GEP1->getResultElementType(); + auto *EltTy2 = cast(V2)->getResultElementType(); + Optional Offset = isPointerOffset(GEP1, V2, DL); + if ((EltTy1 != EltTy2) || !Offset || + DL.getTypeStoreSize(EltTy1).isScalable()) + return AliasResult::MayAlias; + } // Subtract the GEP2 pointer from the GEP1 pointer to find out their // symbolic difference. Index: llvm/test/Transforms/GVN/vscale.ll =================================================================== --- llvm/test/Transforms/GVN/vscale.ll +++ llvm/test/Transforms/GVN/vscale.ll @@ -164,7 +164,7 @@ define i32 @store_clobber_load() { ; CHECK-LABEL: @store_clobber_load( -; CHECK-NEXT: [[ALLOC:%.*]] = alloca +; CHECK-NEXT: [[ALLOC:%.*]] = alloca , align 16 ; CHECK-NEXT: store undef, * [[ALLOC]], align 16 ; CHECK-NEXT: [[PTR:%.*]] = getelementptr , * [[ALLOC]], i32 0, i32 1 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, i32* [[PTR]], align 4 @@ -230,7 +230,7 @@ define * @load_from_alloc_replaced_with_undef() { ; CHECK-LABEL: @load_from_alloc_replaced_with_undef( ; CHECK-NEXT: entry: -; CHECK-NEXT: [[A:%.*]] = alloca +; CHECK-NEXT: [[A:%.*]] = alloca , align 16 ; CHECK-NEXT: br i1 undef, label [[IF_END:%.*]], label [[IF_THEN:%.*]] ; CHECK: if.then: ; CHECK-NEXT: store zeroinitializer, * [[A]], align 16 @@ -281,7 +281,7 @@ ret i32 %result } -; TODO: BasicAA return MayAlias for %gep1,%gep2, could improve as NoAlias. +; BasicAA return MayAlias for %gep1,%gep2, could improve as NoAlias. define void @redundant_load_elimination_2(i1 %c, * %p, i32* %q, %v) { ; CHECK-LABEL: @redundant_load_elimination_2( ; CHECK-NEXT: entry: @@ -291,8 +291,7 @@ ; CHECK-NEXT: store i32 1, i32* [[GEP2]], align 4 ; CHECK-NEXT: br i1 [[C:%.*]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]] ; CHECK: if.then: -; CHECK-NEXT: [[T:%.*]] = load i32, i32* [[GEP1]], align 4 -; CHECK-NEXT: store i32 [[T]], i32* [[Q:%.*]], align 4 +; CHECK-NEXT: store i32 0, i32* [[Q:%.*]], align 4 ; CHECK-NEXT: ret void ; CHECK: if.else: ; CHECK-NEXT: ret void