PR17925 bugfix.
authorStepan Dyatkovskiy <stpworld@narod.ru>
Tue, 26 Nov 2013 16:11:03 +0000 (16:11 +0000)
committerStepan Dyatkovskiy <stpworld@narod.ru>
Tue, 26 Nov 2013 16:11:03 +0000 (16:11 +0000)
Short description.

This issue is about case of treating pointers as integers.
We treat pointers as different if they references different address space.
At the same time, we treat pointers equal to integers (with machine address
width). It was a point of false-positive. Consider next case on 32bit machine:

void foo0(i32 addrespace(1)* %p)
void foo1(i32 addrespace(2)* %p)
void foo2(i32 %p)

foo0 != foo1, while
foo1 == foo2 and foo0 == foo2.

As you can see it breaks transitivity. That means that result depends on order
of how functions are presented in module. Next order causes merging of foo0
and foo1: foo2, foo0, foo1
First foo0 will be merged with foo2, foo0 will be erased. Second foo1 will be
merged with foo2.
Depending on order, things could be merged we don't expect to.

The fix:
Forbid to treat any pointer as integer, except for those, who belong to address space 0.

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

lib/Transforms/IPO/MergeFunctions.cpp
test/Transforms/MergeFunc/inttoptr-address-space-2.ll [deleted file]
test/Transforms/MergeFunc/ptr-int-transitivity-1.ll [new file with mode: 0644]
test/Transforms/MergeFunc/ptr-int-transitivity-2.ll [new file with mode: 0644]
test/Transforms/MergeFunc/ptr-int-transitivity-3.ll [new file with mode: 0644]

index b8397d6414b97a57b35d3343fbc3a3714eb2fb5f..38614216c3ccbc59a8c91cdfc469b8c2bb11e924 100644 (file)
@@ -210,19 +210,20 @@ private:
 // Any two pointers in the same address space are equivalent, intptr_t and
 // pointers are equivalent. Otherwise, standard type equivalence rules apply.
 bool FunctionComparator::isEquivalentType(Type *Ty1, Type *Ty2) const {
+
+  PointerType *PTy1 = dyn_cast<PointerType>(Ty1);
+  PointerType *PTy2 = dyn_cast<PointerType>(Ty2);
+
+  if (TD) {
+    if (PTy1 && PTy1->getAddressSpace() == 0) Ty1 = TD->getIntPtrType(Ty1);
+    if (PTy2 && PTy2->getAddressSpace() == 0) Ty2 = TD->getIntPtrType(Ty2);
+  }
+
   if (Ty1 == Ty2)
     return true;
-  if (Ty1->getTypeID() != Ty2->getTypeID()) {
-    if (TD) {
-
-      if (isa<PointerType>(Ty1) && Ty2 == TD->getIntPtrType(Ty1))
-        return true;
 
-      if (isa<PointerType>(Ty2) && Ty1 == TD->getIntPtrType(Ty2))
-        return true;
-    }
+  if (Ty1->getTypeID() != Ty2->getTypeID())
     return false;
-  }
 
   switch (Ty1->getTypeID()) {
   default:
@@ -244,8 +245,7 @@ bool FunctionComparator::isEquivalentType(Type *Ty1, Type *Ty2) const {
     return true;
 
   case Type::PointerTyID: {
-    PointerType *PTy1 = cast<PointerType>(Ty1);
-    PointerType *PTy2 = cast<PointerType>(Ty2);
+    assert(PTy1 && PTy2 && "Both types must be pointers here.");
     return PTy1->getAddressSpace() == PTy2->getAddressSpace();
   }
 
diff --git a/test/Transforms/MergeFunc/inttoptr-address-space-2.ll b/test/Transforms/MergeFunc/inttoptr-address-space-2.ll
deleted file mode 100644 (file)
index ea350cc..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-; RUN: opt -mergefunc -S < %s | FileCheck %s
-target datalayout = "e-p:32:32:32-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-n8:16:32-S128"
-
-%.qux.2496 = type { i16, %.qux.2497 }
-%.qux.2497 = type { i8, i16 }
-%.qux.2585 = type { i16, i16, i8 addrspace(1)* }
-
-@g2 = external addrspace(1) constant [9 x i8], align 1
-@g3 = internal hidden addrspace(1) constant [1 x i8*] [i8* bitcast (i8 addrspace(1)* (%.qux.2585 addrspace(1)*)* @func35 to i8*)]
-
-
-define internal hidden i16 @func10(%.qux.2496 addrspace(1)* nocapture %this) align 2 {
-bb:
-  %tmp = getelementptr inbounds %.qux.2496 addrspace(1)* %this, i32 0, i32 1, i32 1
-  %tmp1 = load i16 addrspace(1)* %tmp, align 4
-  ret i16 %tmp1
-}
-
-; Checks that this can be merged with an address space differently sized than 0
-define internal hidden i8 addrspace(1)* @func35(%.qux.2585 addrspace(1)* nocapture %this) align 2 {
-bb:
-; CHECK-LABEL: @func35(
-; CHECK: %[[V2:.+]] = bitcast %.qux.2585 addrspace(1)* %{{.*}} to %.qux.2496 addrspace(1)*
-; CHECK: %[[V3:.+]] = tail call i16 @func10(%.qux.2496 addrspace(1)* %[[V2]])
-; CHECK: %{{.*}} = inttoptr i16 %[[V3]] to i8 addrspace(1)*
-  %tmp = getelementptr inbounds %.qux.2585 addrspace(1)* %this, i32 0, i32 2
-  %tmp1 = load i8 addrspace(1)* addrspace(1)* %tmp, align 4
-  ret i8 addrspace(1)* %tmp1
-}
diff --git a/test/Transforms/MergeFunc/ptr-int-transitivity-1.ll b/test/Transforms/MergeFunc/ptr-int-transitivity-1.ll
new file mode 100644 (file)
index 0000000..d6ff10f
--- /dev/null
@@ -0,0 +1,21 @@
+; RUN: opt -S -mergefunc < %s | not grep "functions merged"
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+
+declare void @stuff()
+
+define void @f0(i64 %p0) {
+entry:
+  call void @stuff()
+  call void @stuff()
+  call void @stuff()
+  ret void
+}
+
+define void @f2(i64 addrspace(1)* %p0) {
+entry:
+  call void @stuff()
+  call void @stuff()
+  call void @stuff()
+  ret void
+}
+
diff --git a/test/Transforms/MergeFunc/ptr-int-transitivity-2.ll b/test/Transforms/MergeFunc/ptr-int-transitivity-2.ll
new file mode 100644 (file)
index 0000000..c9fb6a6
--- /dev/null
@@ -0,0 +1,25 @@
+; RUN: opt -S -mergefunc < %s | FileCheck %s
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+
+declare void @stuff()
+
+define void @f0(i64 %p0) {
+entry:
+  call void @stuff()
+  call void @stuff()
+  call void @stuff()
+  ret void
+}
+
+; CHECK-LABEL: @f0
+; CHECK:  %2 = ptrtoint i64* %0 to i64
+; CHECK:  tail call void @f0(i64 %2)
+; CHECK:  ret void
+define void @f1(i64 addrspace(0)* %p0) {
+entry:
+  call void @stuff()
+  call void @stuff()
+  call void @stuff()
+  ret void
+}
+
diff --git a/test/Transforms/MergeFunc/ptr-int-transitivity-3.ll b/test/Transforms/MergeFunc/ptr-int-transitivity-3.ll
new file mode 100644 (file)
index 0000000..8f00f03
--- /dev/null
@@ -0,0 +1,21 @@
+; RUN: opt -S -mergefunc < %s | not grep "functions merged"
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
+
+declare void @stuff()
+
+define void @f0(i64 addrspace(0)* %p0) {
+entry:
+  call void @stuff()
+  call void @stuff()
+  call void @stuff()
+  ret void
+}
+
+define void @f2(i64 addrspace(1)* %p0) {
+entry:
+  call void @stuff()
+  call void @stuff()
+  call void @stuff()
+  ret void
+}
+