tstellar / rpms / llvm

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