From 9630a9999b43bc3fc3ac87710540aed0f451c721 Mon Sep 17 00:00:00 2001 From: luxufan Date: Sun, 26 Mar 2023 22:25:21 +0800 Subject: [PATCH] [Local] Preserve !nonnull only when K dominate J and K has a !noundef Similar to D142687 Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D146799 --- llvm/lib/Transforms/Utils/Local.cpp | 3 +- .../InstCombine/load-combine-metadata-dominance.ll | 45 +++++++++++++++++++++- llvm/test/Transforms/NewGVN/metadata-nonnull.ll | 6 +-- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index 848db3f..c7b1dc9 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -2686,8 +2686,7 @@ void llvm::combineMetadata(Instruction *K, const Instruction *J, 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 index 4949b00..5f86c16 100644 --- 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 @@ bb2: 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 index d76d46e..cd0922f 100644 --- a/llvm/test/Transforms/NewGVN/metadata-nonnull.ll +++ b/llvm/test/Transforms/NewGVN/metadata-nonnull.ll @@ -7,7 +7,7 @@ define ptr @test1(ptr %v0, ptr %v1) { ; 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 @@ define ptr @test5(ptr %v0) { ; 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 @@ define ptr @test6(ptr %v0, ptr %v1) { ; 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: -- 2.7.4