[llvm] e56b2e5 - [InstCombine] Precommit tests for D105088 (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 13 11:35:16 PDT 2021


Author: Krishna Kariya
Date: 2021-07-13T20:35:04+02:00
New Revision: e56b2e57067652710418973e11bb9b118f37b177

URL: https://github.com/llvm/llvm-project/commit/e56b2e57067652710418973e11bb9b118f37b177
DIFF: https://github.com/llvm/llvm-project/commit/e56b2e57067652710418973e11bb9b118f37b177.diff

LOG: [InstCombine] Precommit tests for D105088 (NFC)

Add tests for D105088, as well as an option to disable the
(generally) unsound inttoptr of ptrtoint optimization.

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

Added: 
    llvm/test/Transforms/InstCombine/ptr-int-ptr-icmp.ll

Modified: 
    llvm/lib/IR/Instructions.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp
index 68df26932a69..5b01c70dec8d 100644
--- a/llvm/lib/IR/Instructions.cpp
+++ b/llvm/lib/IR/Instructions.cpp
@@ -45,6 +45,10 @@
 
 using namespace llvm;
 
+static cl::opt<bool> DisableI2pP2iOpt(
+    "disable-i2p-p2i-opt", cl::init(false),
+    cl::desc("Disables inttoptr/ptrtoint roundtrip optimization"));
+
 //===----------------------------------------------------------------------===//
 //                            AllocaInst Class
 //===----------------------------------------------------------------------===//
@@ -2845,6 +2849,10 @@ unsigned CastInst::isEliminableCastPair(
         return secondOp;
       return 0;
     case 7: {
+      // Disable inttoptr/ptrtoint optimization if enabled.
+      if (DisableI2pP2iOpt)
+        return 0;
+
       // Cannot simplify if address spaces are 
diff erent!
       if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
         return 0;

diff  --git a/llvm/test/Transforms/InstCombine/ptr-int-ptr-icmp.ll b/llvm/test/Transforms/InstCombine/ptr-int-ptr-icmp.ll
new file mode 100644
index 000000000000..9784f1873084
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/ptr-int-ptr-icmp.ll
@@ -0,0 +1,97 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -instcombine -S -disable-i2p-p2i-opt < %s | FileCheck %s
+
+target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
+target triple = "x86_64-unknown-linux-gnu"
+
+; icmp (inttoptr (ptrtoint p1)), p2 --> icmp p1, p2.
+
+define i1 @func(i8* %X, i8* %Y) {
+; CHECK-LABEL: @func(
+; CHECK-NEXT:    [[I:%.*]] = ptrtoint i8* [[X:%.*]] to i64
+; CHECK-NEXT:    [[P:%.*]] = inttoptr i64 [[I]] to i8*
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[P]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %i = ptrtoint i8* %X to i64
+  %p = inttoptr i64 %i to i8*
+  %cmp = icmp eq i8* %p, %Y
+  ret i1 %cmp
+}
+
+define i1 @func_pointer_
diff erent_types(i16* %X, i8* %Y) {
+; CHECK-LABEL: @func_pointer_
diff erent_types(
+; CHECK-NEXT:    [[I:%.*]] = ptrtoint i16* [[X:%.*]] to i64
+; CHECK-NEXT:    [[P:%.*]] = inttoptr i64 [[I]] to i8*
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[P]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %i = ptrtoint i16* %X to i64
+  %p = inttoptr i64 %i to i8*
+  %cmp = icmp eq i8* %p, %Y
+  ret i1 %cmp
+}
+
+declare i8* @gen8ptr()
+
+define i1 @func_commutative(i16* %X) {
+; CHECK-LABEL: @func_commutative(
+; CHECK-NEXT:    [[Y:%.*]] = call i8* @gen8ptr()
+; CHECK-NEXT:    [[I:%.*]] = ptrtoint i16* [[X:%.*]] to i64
+; CHECK-NEXT:    [[P:%.*]] = inttoptr i64 [[I]] to i8*
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[Y]], [[P]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %Y = call i8* @gen8ptr() ; thwart complexity-based canonicalization
+  %i = ptrtoint i16* %X to i64
+  %p = inttoptr i64 %i to i8*
+  %cmp = icmp eq i8* %Y, %p
+  ret i1 %cmp
+}
+
+; Negative test - Wrong Integer type.
+
+define i1 @func_integer_type_too_small(i16* %X, i8* %Y) {
+; CHECK-LABEL: @func_integer_type_too_small(
+; CHECK-NEXT:    [[TMP1:%.*]] = ptrtoint i16* [[X:%.*]] to i64
+; CHECK-NEXT:    [[TMP2:%.*]] = and i64 [[TMP1]], 4294967295
+; CHECK-NEXT:    [[P:%.*]] = inttoptr i64 [[TMP2]] to i8*
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8* [[P]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %i = ptrtoint i16* %X to i32
+  %p = inttoptr i32 %i to i8*
+  %cmp = icmp eq i8* %Y, %p
+  ret i1 %cmp
+}
+
+; Negative test - Pointers in 
diff erent address space
+
+define i1 @func_ptr_
diff erent_addrspace(i8* %X, i16 addrspace(3)* %Y){
+; CHECK-LABEL: @func_ptr_
diff erent_addrspace(
+; CHECK-NEXT:    [[I:%.*]] = ptrtoint i8* [[X:%.*]] to i64
+; CHECK-NEXT:    [[P:%.*]] = inttoptr i64 [[I]] to i16 addrspace(3)*
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i16 addrspace(3)* [[P]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %i = ptrtoint i8* %X to i64
+  %p = inttoptr i64 %i to i16 addrspace(3)*
+  %cmp = icmp eq i16 addrspace(3)* %Y, %p
+  ret i1 %cmp
+}
+
+; Negative test - Pointers in 
diff erent address space
+
+define i1 @func_ptr_
diff erent_addrspace1(i8 addrspace(2)* %X, i16* %Y){
+; CHECK-LABEL: @func_ptr_
diff erent_addrspace1(
+; CHECK-NEXT:    [[TMP1:%.*]] = ptrtoint i8 addrspace(2)* [[X:%.*]] to i32
+; CHECK-NEXT:    [[I:%.*]] = zext i32 [[TMP1]] to i64
+; CHECK-NEXT:    [[P:%.*]] = inttoptr i64 [[I]] to i16*
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i16* [[P]], [[Y:%.*]]
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %i = ptrtoint i8 addrspace(2)* %X to i64
+  %p = inttoptr i64 %i to i16*
+  %cmp = icmp eq i16* %Y, %p
+  ret i1 %cmp
+}


        


More information about the llvm-commits mailing list