[PATCH] D64768: [InstructionSimplify] Apply sext/trunc after pointer stripping

Michael Liao via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 15 13:48:55 PDT 2019


hliao created this revision.
hliao added a reviewer: jdoerfert.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
hliao added a comment.

this fixes crashes found on targets where `addrspacecast` is used.


- 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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64768

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/compare.ll


Index: llvm/test/Transforms/InstSimplify/compare.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/compare.ll
+++ llvm/test/Transforms/InstSimplify/compare.ll
@@ -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 @@
   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" }
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -660,6 +660,10 @@
   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())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64768.209949.patch
Type: text/x-patch
Size: 1509 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190715/272e4ac0/attachment.bin>


More information about the llvm-commits mailing list