tstellar / rpms / llvm

Forked from rpms/llvm 5 years ago
Clone
0c3be08
From eff5dc809ed54701f2bb3e15c58d01881299cedf Mon Sep 17 00:00:00 2001
0c3be08
From: David Majnemer <david.majnemer@gmail.com>
0c3be08
Date: Tue, 11 Oct 2016 01:00:45 +0000
0c3be08
Subject: [rust-lang/llvm#55] [InstCombine] Transform !range metadata to
0c3be08
 !nonnull when combining loads
0c3be08
0c3be08
When combining an integer load with !range metadata that does not include 0 to a pointer load, make sure emit !nonnull metadata on the newly-created pointer load. This prevents the !nonnull metadata from being dropped during a ptrtoint/inttoptr pair.
0c3be08
0c3be08
This fixes PR30597.
0c3be08
0c3be08
Patch by Ariel Ben-Yehuda!
0c3be08
0c3be08
Differential Revision: https://reviews.llvm.org/D25215
0c3be08
0c3be08
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283836 91177308-0d34-0410-b5e6-96231b3b80d8
0c3be08
---
0c3be08
 .../InstCombine/InstCombineLoadStoreAlloca.cpp     | 12 ++++++--
0c3be08
 test/Transforms/InstCombine/PR30597.ll             | 32 ++++++++++++++++++++++
0c3be08
 2 files changed, 42 insertions(+), 2 deletions(-)
0c3be08
 create mode 100644 test/Transforms/InstCombine/PR30597.ll
0c3be08
0c3be08
diff --git a/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
0c3be08
index d312983ed51b..26f4e764501a 100644
0c3be08
--- a/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
0c3be08
+++ b/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
0c3be08
@@ -380,8 +380,16 @@ static LoadInst *combineLoadToNewType(InstCombiner &IC, LoadInst &LI, Type *NewT
0c3be08
       break;
0c3be08
     case LLVMContext::MD_range:
0c3be08
       // FIXME: It would be nice to propagate this in some way, but the type
0c3be08
-      // conversions make it hard. If the new type is a pointer, we could
0c3be08
-      // translate it to !nonnull metadata.
0c3be08
+      // conversions make it hard.
0c3be08
+
0c3be08
+      // If it's a pointer now and the range does not contain 0, make it !nonnull.
0c3be08
+      if (NewTy->isPointerTy()) {
0c3be08
+        unsigned BitWidth = IC.getDataLayout().getTypeSizeInBits(NewTy);
0c3be08
+        if (!getConstantRangeFromMetadata(*N).contains(APInt(BitWidth, 0))) {
0c3be08
+          MDNode *NN = MDNode::get(LI.getContext(), None);
0c3be08
+          NewLoad->setMetadata(LLVMContext::MD_nonnull, NN);
0c3be08
+        }
0c3be08
+      }
0c3be08
       break;
0c3be08
     }
0c3be08
   }
0c3be08
diff --git a/test/Transforms/InstCombine/PR30597.ll b/test/Transforms/InstCombine/PR30597.ll
0c3be08
new file mode 100644
0c3be08
index 000000000000..c0803ed71204
0c3be08
--- /dev/null
0c3be08
+++ b/test/Transforms/InstCombine/PR30597.ll
0c3be08
@@ -0,0 +1,32 @@
0c3be08
+; RUN: opt < %s -instcombine -S | FileCheck %s
0c3be08
+
0c3be08
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
0c3be08
+target triple = "x86_64-unknown-linux-gnu"
0c3be08
+
0c3be08
+; Function Attrs: readonly uwtable
0c3be08
+define i1 @dot_ref_s(i32** noalias nocapture readonly dereferenceable(8)) {
0c3be08
+entry-block:
0c3be08
+  %loadedptr = load i32*, i32** %0, align 8, !nonnull !0
0c3be08
+  %ptrtoint = ptrtoint i32* %loadedptr to i64
0c3be08
+  %inttoptr = inttoptr i64 %ptrtoint to i32*
0c3be08
+  %switchtmp = icmp eq i32* %inttoptr, null
0c3be08
+  ret i1 %switchtmp
0c3be08
+
0c3be08
+; CHECK-LABEL: @dot_ref_s
0c3be08
+; CHECK-NEXT: entry-block:
0c3be08
+; CHECK-NEXT: ret i1 false
0c3be08
+}
0c3be08
+
0c3be08
+; Function Attrs: readonly uwtable
0c3be08
+define i64* @function(i64* noalias nocapture readonly dereferenceable(8)) {
0c3be08
+entry-block:
0c3be08
+  %loaded = load i64, i64* %0, align 8, !range !1
0c3be08
+  %inttoptr = inttoptr i64 %loaded to i64*
0c3be08
+  ret i64* %inttoptr
0c3be08
+; CHECK-LABEL: @function
0c3be08
+; CHECK: %{{.+}} = load i64*, i64** %{{.+}}, align 8, !nonnull
0c3be08
+}
0c3be08
+
0c3be08
+
0c3be08
+!0 = !{}
0c3be08
+!1 = !{i64 1, i64 140737488355327}
0c3be08
-- 
0c3be08
2.9.3
0c3be08