Index: llvm/lib/Analysis/BasicAliasAnalysis.cpp =================================================================== --- llvm/lib/Analysis/BasicAliasAnalysis.cpp +++ llvm/lib/Analysis/BasicAliasAnalysis.cpp @@ -1222,6 +1222,21 @@ } if (!DecompGEP1.VarIndices.empty()) { + // Pick context instruction for ValueTracking queries. Using either + // instruction is correct, but the one with the larger context (the "more + // dominated" one) is preferable. If both instructions are GEPs not in a + // dominance relationship, don't use a context instruction, as we have no + // select criterion that is independent of the order in the alias() call. + const auto *CxtI = dyn_cast(GEP1); + if (CxtI) { + if (const auto *I2 = dyn_cast(V2)) { + if (DT->dominates(CxtI, I2)) + CxtI = I2; + else if (!DT->dominates(I2, CxtI)) + CxtI = nullptr; + } + } + APInt GCD; bool AllNonNegative = DecompGEP1.Offset.isNonNegative(); bool AllNonPositive = DecompGEP1.Offset.isNonPositive(); @@ -1238,8 +1253,7 @@ // give up if we can't determine conditions that hold for every cycle: const Value *V = DecompGEP1.VarIndices[i].V; - KnownBits Known = - computeKnownBits(V, DL, 0, &AC, dyn_cast(GEP1), DT); + KnownBits Known = computeKnownBits(V, DL, 0, &AC, CxtI, DT); bool SignKnownZero = Known.isNonNegative(); bool SignKnownOne = Known.isNegative(); Index: llvm/test/Analysis/BasicAA/assume-index-positive.ll =================================================================== --- llvm/test/Analysis/BasicAA/assume-index-positive.ll +++ llvm/test/Analysis/BasicAA/assume-index-positive.ll @@ -113,4 +113,20 @@ ret void } +define void @symmetry([0 x i8]* %ptr, i32 %a, i32 %b, i32 %c) { +; CHECK-LABEL: Function: symmetry +; CHECK: NoAlias: i8* %gep1, i8* %gep2 +; + %c.off = add nuw nsw i32 %c, 1 + %gep1 = getelementptr [0 x i8], [0 x i8]* %ptr, i32 %a, i32 %b + call void @barrier() + %b.cmp = icmp slt i32 %b, 0 + call void @llvm.assume(i1 %b.cmp) + %c.cmp = icmp sgt i32 %c, -1 + call void @llvm.assume(i1 %c.cmp) + %gep2 = getelementptr [0 x i8], [0 x i8]* %ptr, i32 %a, i32 %c.off + ret void +} + declare void @llvm.assume(i1 %cond) +declare void @barrier()