[llvm] r364523 - IR: compare type attributes deeply when looking into functions.

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 27 04:44:46 PDT 2019


Author: tnorthover
Date: Thu Jun 27 04:44:45 2019
New Revision: 364523

URL: http://llvm.org/viewvc/llvm-project?rev=364523&view=rev
Log:
IR: compare type attributes deeply when looking into functions.

FunctionComparator attempts to produce a stable comparison of two Function
instances by looking at all available properties. Since ByVal attributes now
contain a Type pointer, they are not trivially ordered and FunctionComparator
should use its own Type comparison logic to sort them.

Added:
    llvm/trunk/test/Transforms/MergeFunc/byval-attr-type.ll
Modified:
    llvm/trunk/lib/Transforms/Utils/FunctionComparator.cpp

Modified: llvm/trunk/lib/Transforms/Utils/FunctionComparator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/FunctionComparator.cpp?rev=364523&r1=364522&r2=364523&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/FunctionComparator.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/FunctionComparator.cpp Thu Jun 27 04:44:45 2019
@@ -113,6 +113,19 @@ int FunctionComparator::cmpAttrs(const A
     for (; LI != LE && RI != RE; ++LI, ++RI) {
       Attribute LA = *LI;
       Attribute RA = *RI;
+      if (LA.isTypeAttribute() && RA.isTypeAttribute()) {
+        if (LA.getKindAsEnum() != RA.getKindAsEnum())
+          return cmpNumbers(LA.getKindAsEnum(), RA.getKindAsEnum());
+
+        Type *TyL = LA.getValueAsType();
+        Type *TyR = RA.getValueAsType();
+        if (TyL && TyR)
+          return cmpTypes(TyL, TyR);
+
+        // Two pointers, at least one null, so the comparison result is
+        // independent of the value of a real pointer.
+        return cmpNumbers((uint64_t)TyL, (uint64_t)TyR);
+      }
       if (LA < RA)
         return -1;
       if (RA < LA)

Added: llvm/trunk/test/Transforms/MergeFunc/byval-attr-type.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/MergeFunc/byval-attr-type.ll?rev=364523&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/MergeFunc/byval-attr-type.ll (added)
+++ llvm/trunk/test/Transforms/MergeFunc/byval-attr-type.ll Thu Jun 27 04:44:45 2019
@@ -0,0 +1,37 @@
+; RUN: opt -S -mergefunc %s | FileCheck %s
+
+ at i = global i32 0
+ at f = global float 0.0
+
+define internal void @foo() {
+; CHECK: define internal void @foo()
+  call void @callee_i32(i32* byval(i32) @i)
+  ret void
+}
+
+define internal void @bar() {
+; CHECK: define internal void @bar()
+  call void @callee_float(float* byval(float) @f)
+  ret void
+}
+
+define internal void @baz() {
+; CHECK-NOT: define{{.*}}@bar
+  call void @callee_float(float* byval(float) @f)
+  ret void
+}
+
+define void @user() {
+; CHECK-LABEL: define void @user
+; CHECK: call void @foo()
+; CHECK: call void @bar()
+; CHECK: call void @bar()
+
+  call void @foo()
+  call void @bar()
+  call void @baz()
+  ret void
+}
+
+declare void @callee_i32(i32* byval(i32))
+declare void @callee_float(float* byval(float))




More information about the llvm-commits mailing list