diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp @@ -745,6 +745,7 @@ LLVMContext::MD_dereferenceable, LLVMContext::MD_dereferenceable_or_null, LLVMContext::MD_access_group, + LLVMContext::MD_noundef, }; for (unsigned ID : KnownIDs) 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 @@ -2698,6 +2698,11 @@ case LLVMContext::MD_preserve_access_index: // Preserve !preserve.access.index in K. break; + case LLVMContext::MD_noundef: + // If K does move, keep noundef if it is present in both instructions. + if (DoesKMove) + K->setMetadata(Kind, JMD); + break; } } // Set !invariant.group from J if J has it. If both instructions have it diff --git a/llvm/test/Transforms/InstCombine/phi-load-metadata-4.ll b/llvm/test/Transforms/InstCombine/phi-load-metadata-4.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/InstCombine/phi-load-metadata-4.ll @@ -0,0 +1,70 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -passes=instcombine -S < %s | FileCheck %s + +declare void @bar() +declare void @baz() + +; Check that !noundef metadata is combined +define ptr @test_phi_combine_load_metadata(i1 %c, ptr dereferenceable(8) %p1, ptr dereferenceable(8) %p2) { +; CHECK-LABEL: @test_phi_combine_load_metadata( +; CHECK-NEXT: br i1 [[C:%.*]], label [[T:%.*]], label [[F:%.*]] +; CHECK: t: +; CHECK-NEXT: call void @bar() +; CHECK-NEXT: br label [[CONT:%.*]] +; CHECK: f: +; CHECK-NEXT: call void @baz() +; CHECK-NEXT: br label [[CONT]] +; CHECK: cont: +; CHECK-NEXT: [[RES_IN:%.*]] = phi ptr [ [[P1:%.*]], [[T]] ], [ [[P2:%.*]], [[F]] ] +; CHECK-NEXT: [[RES:%.*]] = load ptr, ptr [[RES_IN]], align 8, !noundef !0 +; CHECK-NEXT: ret ptr [[RES]] +; + br i1 %c, label %t, label %f +t: + call void @bar() + %v1 = load ptr, ptr %p1, align 8, !noundef !0 + br label %cont + +f: + call void @baz() + %v2 = load ptr, ptr %p2, align 8, !noundef !0 + br label %cont + +cont: + %res = phi ptr [ %v1, %t ], [ %v2, %f ] + ret ptr %res +} + +; Check that !noundef metadata is not combined as a result of +; there is a load don't have a noundef metadata. +define ptr @test_phi_combine_load_metadata_negative(i1 %c, ptr dereferenceable(8) %p1, ptr dereferenceable(8) %p2) { +; CHECK-LABEL: @test_phi_combine_load_metadata_negative( +; CHECK-NEXT: br i1 [[C:%.*]], label [[T:%.*]], label [[F:%.*]] +; CHECK: t: +; CHECK-NEXT: call void @bar() +; CHECK-NEXT: br label [[CONT:%.*]] +; CHECK: f: +; CHECK-NEXT: call void @baz() +; CHECK-NEXT: br label [[CONT]] +; CHECK: cont: +; CHECK-NEXT: [[RES_IN:%.*]] = phi ptr [ [[P1:%.*]], [[T]] ], [ [[P2:%.*]], [[F]] ] +; CHECK-NEXT: [[RES:%.*]] = load ptr, ptr [[RES_IN]], align 8 +; CHECK-NEXT: ret ptr [[RES]] +; + br i1 %c, label %t, label %f +t: + call void @bar() + %v1 = load ptr, ptr %p1, align 8, !noundef !0 + br label %cont + +f: + call void @baz() + %v2 = load ptr, ptr %p2, align 8 + br label %cont + +cont: + %res = phi ptr [ %v1, %t ], [ %v2, %f ] + ret ptr %res +} + +!0 = !{}