]> granicus.if.org Git - llvm/commitdiff
[InstructionSimplify] Apply sext/trunc after pointer stripping
authorMichael Liao <michael.hliao@gmail.com>
Tue, 16 Jul 2019 01:03:06 +0000 (01:03 +0000)
committerMichael Liao <michael.hliao@gmail.com>
Tue, 16 Jul 2019 01:03:06 +0000 (01:03 +0000)
Summary:
- As the pointer stripping could trace through `addrspacecast` now, need
  to sext/trunc the offset to ensure it has the same width as the
  pointer after stripping.

Reviewers: jdoerfert

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D64768

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366162 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstSimplify/compare.ll

index c0d69f9275d1d97dbd2c13b8867e1250f935569b..e34bf6f4e43f57237c0f3e0105832f01c622394c 100644 (file)
@@ -660,6 +660,10 @@ static Constant *stripAndComputeConstantOffsets(const DataLayout &DL, Value *&V,
   APInt Offset = APInt::getNullValue(IntPtrTy->getIntegerBitWidth());
 
   V = V->stripAndAccumulateConstantOffsets(DL, Offset, AllowNonInbounds);
+  // As that strip may trace through `addrspacecast`, need to sext or trunc
+  // the offset calculated.
+  IntPtrTy = DL.getIntPtrType(V->getType())->getScalarType();
+  Offset = Offset.sextOrTrunc(IntPtrTy->getIntegerBitWidth());
 
   Constant *OffsetIntPtr = ConstantInt::get(IntPtrTy, Offset);
   if (V->getType()->isVectorTy())
index 899f198d48a31203d1fd6ec6383a2e0289d891ea..570239eaf0c6effa019b35cf7634df5860fb842d 100644 (file)
@@ -1,5 +1,5 @@
 ; RUN: opt < %s -instsimplify -S | FileCheck %s
-target datalayout = "p:32:32"
+target datalayout = "p:32:32-p1:64:64"
 
 define i1 @ptrtoint() {
 ; CHECK-LABEL: @ptrtoint(
@@ -1358,4 +1358,13 @@ define i1 @constant_fold_null_inttoptr() {
   ret i1 %x
 }
 
+; CHECK-LABEL: @cmp_through_addrspacecast(
+; CHECK-NEXT: ret i1 true
+define i1 @cmp_through_addrspacecast(i32 addrspace(1)* %p1) {
+  %p0 = addrspacecast i32 addrspace(1)* %p1 to i32*
+  %p0.1 = getelementptr inbounds i32, i32* %p0, i64 1
+  %cmp = icmp ne i32* %p0, %p0.1
+  ret i1 %cmp
+}
+
 attributes #0 = { "null-pointer-is-valid"="true" }