Index: lib/Transforms/Scalar/NewGVN.cpp =================================================================== --- lib/Transforms/Scalar/NewGVN.cpp +++ lib/Transforms/Scalar/NewGVN.cpp @@ -873,7 +873,13 @@ } bool LoadExpression::equals(const Expression &Other) const { - return equalsLoadStoreHelper(*this, Other); + if (!equalsLoadStoreHelper(*this, Other)) + return false; + // Check that the nonnull metadata agrees. + if (const auto *L = dyn_cast(&Other)) + return Load->getMetadata(LLVMContext::MD_nonnull) == + L->Load->getMetadata(LLVMContext::MD_nonnull); + return true; } bool StoreExpression::equals(const Expression &Other) const { Index: test/Transforms/NewGVN/load-nonnull.ll =================================================================== --- /dev/null +++ test/Transforms/NewGVN/load-nonnull.ll @@ -0,0 +1,24 @@ +; RUN: opt < %s -S -newgvn | FileCheck %s + +; CHECK-LABEL: bb1: +; CHECK-NEXT: %load1 = load {}*, {}** %arg +; CHECK-NEXT: %cmp1 = icmp eq {}* %load1, null + +; CHECK-LABEL: bb2: +; CHECK: ret i1 false + +define i1 @test({}** %arg, i1 %arg2) { + br i1 %arg2, label %bb1, label %bb2 + +bb1: + %load1 = load {}*, {}** %arg + %cmp1 = icmp eq {}* %load1, null + ret i1 %cmp1 + +bb2: + %load2 = load {}*, {}** %arg, !nonnull !1 + %cmp2 = icmp eq {}* %load2, null + ret i1 %cmp2 +} + +!1 = !{}