From c2d93c404820178d55f440d4aa46e75d4b5c7b64 Mon Sep 17 00:00:00 2001 From: Arnold Schwaighofer Date: Thu, 26 Jun 2014 19:51:19 +0000 Subject: GVN: Preserve invariant.load metadata If both instructions to be replaced are marked invariant the resulting instruction is invariant. rdar://13358910 Fix by Erik Eckstein! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211801 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/GVN.cpp | 4 ++++ test/Transforms/GVN/invariant-load.ll | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 test/Transforms/GVN/invariant-load.ll diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index 56781d44aa..106eba099c 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -1798,6 +1798,10 @@ static void patchReplacementInstruction(Instruction *I, Value *Repl) { case LLVMContext::MD_fpmath: ReplInst->setMetadata(Kind, MDNode::getMostGenericFPMath(IMD, ReplMD)); break; + case LLVMContext::MD_invariant_load: + // Only set the !invariant.load if it is present in both instructions. + ReplInst->setMetadata(Kind, IMD); + break; } } } diff --git a/test/Transforms/GVN/invariant-load.ll b/test/Transforms/GVN/invariant-load.ll new file mode 100644 index 0000000000..73a12787fb --- /dev/null +++ b/test/Transforms/GVN/invariant-load.ll @@ -0,0 +1,30 @@ +; RUN: opt -basicaa -gvn -S < %s | FileCheck %s + +define i32 @test1(i32* nocapture %p, i8* nocapture %q) { +; CHECK-LABEL: test1 +; CHECK: %x = load i32* %p, align 4, !invariant.load !0 +; CHECK-NOT: %y = load +entry: + %x = load i32* %p, align 4, !invariant.load !0 + %conv = trunc i32 %x to i8 + store i8 %conv, i8* %q, align 1 + %y = load i32* %p, align 4, !invariant.load !0 + %add = add i32 %y, 1 + ret i32 %add +} + +define i32 @test2(i32* nocapture %p, i8* nocapture %q) { +; CHECK-LABEL: test2 +; CHECK-NOT: !invariant.load +; CHECK-NOT: %y = load +entry: + %x = load i32* %p, align 4 + %conv = trunc i32 %x to i8 + store i8 %conv, i8* %q, align 1 + %y = load i32* %p, align 4, !invariant.load !0 + %add = add i32 %y, 1 + ret i32 %add +} + +!0 = metadata !{ } + -- cgit v1.2.3