diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -2686,8 +2686,7 @@ K->setMetadata(Kind, JMD); break; case LLVMContext::MD_nonnull: - // If K does move, keep nonull if it is present in both instructions. - if (DoesKMove) + if (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef)) K->setMetadata(Kind, JMD); break; case LLVMContext::MD_invariant_group: diff --git a/llvm/test/Transforms/InstCombine/load-combine-metadata-dominance.ll b/llvm/test/Transforms/InstCombine/load-combine-metadata-dominance.ll --- a/llvm/test/Transforms/InstCombine/load-combine-metadata-dominance.ll +++ b/llvm/test/Transforms/InstCombine/load-combine-metadata-dominance.ll @@ -3,14 +3,14 @@ target datalayout = "e-m:e-p:64:64:64-i64:64-f80:128-n8:16:32:64-S128" -; Check that nonnull metadata is propagated from dominating load. +; Check that nonnull metadata is not propagated from dominating load. define void @combine_metadata_dominance1(ptr %p) { ; CHECK-LABEL: define void @combine_metadata_dominance1 ; CHECK-SAME: (ptr [[P:%.*]]) { ; CHECK-NEXT: entry: ; CHECK-NEXT: br label [[BB1:%.*]] ; CHECK: bb1: -; CHECK-NEXT: [[A:%.*]] = load ptr, ptr [[P]], align 8, !nonnull !0 +; CHECK-NEXT: [[A:%.*]] = load ptr, ptr [[P]], align 8 ; CHECK-NEXT: store i32 0, ptr [[A]], align 4 ; CHECK-NEXT: ret void ; @@ -55,5 +55,46 @@ ret void } +define void @combine_metadata_dominance3(ptr %p) { +; CHECK-LABEL: define void @combine_metadata_dominance3 +; CHECK-SAME: (ptr [[P:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[BB1:%.*]] +; CHECK: bb1: +; CHECK-NEXT: [[A:%.*]] = load ptr, ptr [[P]], align 8, !nonnull !0, !noundef !0 +; CHECK-NEXT: store i32 0, ptr [[A]], align 4 +; CHECK-NEXT: ret void +; +entry: + %a = load ptr, ptr %p, !nonnull !0, !noundef !1 + br label %bb1 + +bb1: + %b = load ptr, ptr %p + store i32 0, ptr %a + store i32 0, ptr %b + ret void +} + +define void @combine_metadata_dominance4(ptr %p) { +; CHECK-LABEL: define void @combine_metadata_dominance4 +; CHECK-SAME: (ptr [[P:%.*]]) { +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[BB1:%.*]] +; CHECK: bb1: +; CHECK-NEXT: [[A:%.*]] = load ptr, ptr [[P]], align 8 +; CHECK-NEXT: store i32 0, ptr [[A]], align 4 +; CHECK-NEXT: ret void +; +entry: + %a = load ptr, ptr %p, !nonnull !0 + br label %bb1 +bb1: + %b = load ptr, ptr %p, !noundef !1 + store i32 0, ptr %a + store i32 0, ptr %b + ret void +} !0 = !{} +!1 = !{} diff --git a/llvm/test/Transforms/NewGVN/metadata-nonnull.ll b/llvm/test/Transforms/NewGVN/metadata-nonnull.ll --- a/llvm/test/Transforms/NewGVN/metadata-nonnull.ll +++ b/llvm/test/Transforms/NewGVN/metadata-nonnull.ll @@ -7,7 +7,7 @@ ; CHECK-LABEL: define ptr @test1 ; CHECK-SAME: (ptr [[V0:%.*]], ptr [[V1:%.*]]) { ; CHECK-NEXT: top: -; CHECK-NEXT: [[V2:%.*]] = load ptr, ptr [[V0]], align 8, !nonnull !0 +; CHECK-NEXT: [[V2:%.*]] = load ptr, ptr [[V0]], align 8 ; CHECK-NEXT: store ptr [[V2]], ptr [[V1]], align 8 ; CHECK-NEXT: ret ptr [[V2]] ; @@ -92,7 +92,7 @@ ; CHECK-LABEL: define ptr @test5 ; CHECK-SAME: (ptr [[V0:%.*]]) { ; CHECK-NEXT: top: -; CHECK-NEXT: [[V1:%.*]] = load ptr, ptr [[V0]], align 8, !nonnull !0 +; CHECK-NEXT: [[V1:%.*]] = load ptr, ptr [[V0]], align 8 ; CHECK-NEXT: call void @use1(ptr [[V1]]) ; CHECK-NEXT: br i1 undef, label [[BB1:%.*]], label [[BB2:%.*]] ; CHECK: bb1: @@ -120,7 +120,7 @@ ; CHECK-NEXT: top: ; CHECK-NEXT: br i1 undef, label [[BB1:%.*]], label [[BB2:%.*]] ; CHECK: bb1: -; CHECK-NEXT: [[V2:%.*]] = load ptr, ptr [[V0]], align 8, !nonnull !0 +; CHECK-NEXT: [[V2:%.*]] = load ptr, ptr [[V0]], align 8 ; CHECK-NEXT: store ptr [[V2]], ptr [[V1]], align 8 ; CHECK-NEXT: ret ptr [[V2]] ; CHECK: bb2: