From 8fadf8f4d3cade0bc50faa23a6575588c67ecd67 Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Tue, 2 Jun 2015 22:33:30 +0000 Subject: [PATCH] [SelectionDAG] Fix PR23603. Summary: LLVM's MI level notion of invariant_load is different from LLVM's IR level notion of invariant_load with respect to dereferenceability. The IR notion of invariant_load only guarantees that all *non-faulting* invariant loads result in the same value. The MI notion of invariant load guarantees that the load can be legally moved to any location within its containing function. The MI notion of invariant_load is stronger than the IR notion of invariant_load -- an MI invariant_load is an IR invariant_load + a guarantee that the location being loaded from is dereferenceable throughout the function's lifetime. Reviewers: hfinkel, reames Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D10075 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@238881 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../SelectionDAG/SelectionDAGBuilder.cpp | 12 +++++++++- test/CodeGen/X86/pr23603.ll | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/CodeGen/X86/pr23603.ll diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 03a9aa8189a..9a81ec51dda 100644 --- a/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -2848,7 +2848,17 @@ void SelectionDAGBuilder::visitLoad(const LoadInst &I) { bool isVolatile = I.isVolatile(); bool isNonTemporal = I.getMetadata(LLVMContext::MD_nontemporal) != nullptr; - bool isInvariant = I.getMetadata(LLVMContext::MD_invariant_load) != nullptr; + + // The IR notion of invariant_load only guarantees that all *non-faulting* + // invariant loads result in the same value. The MI notion of invariant load + // guarantees that the load can be legally moved to any location within its + // containing function. The MI notion of invariant_load is stronger than the + // IR notion of invariant_load -- an MI invariant_load is an IR invariant_load + // with a guarantee that the location being loaded from is dereferenceable + // throughout the function's lifetime. + + bool isInvariant = I.getMetadata(LLVMContext::MD_invariant_load) != nullptr && + isDereferenceablePointer(SV, *DAG.getTarget().getDataLayout()); unsigned Alignment = I.getAlignment(); AAMDNodes AAInfo; diff --git a/test/CodeGen/X86/pr23603.ll b/test/CodeGen/X86/pr23603.ll new file mode 100644 index 00000000000..6f856aedb8d --- /dev/null +++ b/test/CodeGen/X86/pr23603.ll @@ -0,0 +1,24 @@ +; RUN: llc < %s -mtriple=x86_64-apple-darwin | FileCheck %s + +declare void @free_v() + +define void @f(i32* %x, i32 %c32, i32* %y) { +; CHECK-LABEL: f + entry: + %v = load i32, i32* %x, !invariant.load !0 +; CHECK: movl (%rdi), %ebx +; CHECK: free_v +; CHECK-NOT: movl (%rdi), %ebx + call void @free_v() + %c = icmp ne i32 %c32, 0 + br i1 %c, label %left, label %merge + + left: + store i32 %v, i32* %y + br label %merge + + merge: + ret void +} + +!0 = !{}