[llvm] [MergeFunc] Handle ConstantRange attributes. (PR #88584)
Andreas Jonson via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 13 00:20:12 PDT 2024
https://github.com/andjo403 updated https://github.com/llvm/llvm-project/pull/88584
>From a9da350aadfb5c86d36ae18398471558b22c1309 Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Fri, 12 Apr 2024 23:35:47 +0200
Subject: [PATCH 1/2] [MergeFunc] Handle ConstantRange attributes.
---
.../Transforms/Utils/FunctionComparator.cpp | 8 ++
.../call-and-invoke-with-ranges-attr.ll | 111 ++++++++++++++++++
2 files changed, 119 insertions(+)
create mode 100644 llvm/test/Transforms/MergeFunc/call-and-invoke-with-ranges-attr.ll
diff --git a/llvm/lib/Transforms/Utils/FunctionComparator.cpp b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
index 8cfb8ed07d240e..0c520ec7c5d2dd 100644
--- a/llvm/lib/Transforms/Utils/FunctionComparator.cpp
+++ b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
@@ -143,6 +143,14 @@ int FunctionComparator::cmpAttrs(const AttributeList L,
if (int Res = cmpNumbers((uint64_t)TyL, (uint64_t)TyR))
return Res;
continue;
+ } else if (LA.isConstantRangeAttribute() &&
+ RA.isConstantRangeAttribute()) {
+ ConstantRange LCR = LA.getRange();
+ ConstantRange RCR = RA.getRange();
+ if (int Res = cmpAPInts(LCR.getLower(), RCR.getLower()))
+ return Res;
+ if (int Res = cmpAPInts(LCR.getUpper(), RCR.getUpper()))
+ return Res;
}
if (LA < RA)
return -1;
diff --git a/llvm/test/Transforms/MergeFunc/call-and-invoke-with-ranges-attr.ll b/llvm/test/Transforms/MergeFunc/call-and-invoke-with-ranges-attr.ll
new file mode 100644
index 00000000000000..e5d62319bf9db7
--- /dev/null
+++ b/llvm/test/Transforms/MergeFunc/call-and-invoke-with-ranges-attr.ll
@@ -0,0 +1,111 @@
+; RUN: opt -passes=mergefunc -S < %s | FileCheck %s
+
+define i8 @call_with_range_attr(i8 range(i8 0, 2) %v) {
+ %out = call i8 @dummy2(i8 %v)
+ ret i8 %out
+}
+
+define i8 @call_no_range_attr(i8 %v) {
+; CHECK-LABEL: @call_no_range_attr
+; CHECK-NEXT: %out = call i8 @dummy2(i8 %v)
+; CHECK-NEXT: ret i8 %out
+ %out = call i8 @dummy2(i8 %v)
+ ret i8 %out
+}
+
+define i8 @call_different_range_attr(i8 range(i8 5, 7) %v) {
+; CHECK-LABEL: @call_different_range_attr
+; CHECK-NEXT: %out = call i8 @dummy2(i8 %v)
+; CHECK-NEXT: ret i8 %out
+ %out = call i8 @dummy2(i8 %v)
+ ret i8 %out
+}
+
+define i8 @call_with_range() {
+ %out = call range(i8 0, 2) i8 @dummy()
+ ret i8 %out
+}
+
+define i8 @call_no_range() {
+; CHECK-LABEL: @call_no_range
+; CHECK-NEXT: %out = call i8 @dummy()
+; CHECK-NEXT: ret i8 %out
+ %out = call i8 @dummy()
+ ret i8 %out
+}
+
+define i8 @call_different_range() {
+; CHECK-LABEL: @call_different_range
+; CHECK-NEXT: %out = call range(i8 5, 7) i8 @dummy()
+; CHECK-NEXT: ret i8 %out
+ %out = call range(i8 5, 7) i8 @dummy()
+ ret i8 %out
+}
+
+define i8 @invoke_with_range() personality ptr undef {
+ %out = invoke range(i8 0, 2) i8 @dummy() to label %next unwind label %lpad
+
+next:
+ ret i8 %out
+
+lpad:
+ %pad = landingpad { ptr, i32 } cleanup
+ resume { ptr, i32 } zeroinitializer
+}
+
+define i8 @invoke_no_range() personality ptr undef {
+; CHECK-LABEL: @invoke_no_range()
+; CHECK-NEXT: invoke i8 @dummy
+ %out = invoke i8 @dummy() to label %next unwind label %lpad
+
+next:
+ ret i8 %out
+
+lpad:
+ %pad = landingpad { ptr, i32 } cleanup
+ resume { ptr, i32 } zeroinitializer
+}
+
+define i8 @invoke_different_range() personality ptr undef {
+; CHECK-LABEL: @invoke_different_range()
+; CHECK-NEXT: invoke range(i8 5, 7) i8 @dummy
+ %out = invoke range(i8 5, 7) i8 @dummy() to label %next unwind label %lpad
+
+next:
+ ret i8 %out
+
+lpad:
+ %pad = landingpad { ptr, i32 } cleanup
+ resume { ptr, i32 } zeroinitializer
+}
+
+define i8 @invoke_with_same_range() personality ptr undef {
+; CHECK-LABEL: @invoke_with_same_range()
+; CHECK: tail call i8 @invoke_with_range()
+ %out = invoke range(i8 0, 2) i8 @dummy() to label %next unwind label %lpad
+
+next:
+ ret i8 %out
+
+lpad:
+ %pad = landingpad { ptr, i32 } cleanup
+ resume { ptr, i32 } zeroinitializer
+}
+
+define i8 @call_with_same_range() {
+; CHECK-LABEL: @call_with_same_range
+; CHECK: tail call i8 @call_with_range
+ %out = call range(i8 0, 2) i8 @dummy()
+ ret i8 %out
+}
+
+define i8 @call_with_same_range_attr(i8 range(i8 0, 2) %v) {
+; CHECK-LABEL: @call_with_same_range_attr
+; CHECK: tail call i8 @call_with_range_attr
+ %out = call i8 @dummy2(i8 %v)
+ ret i8 %out
+}
+
+declare i8 @dummy();
+declare i8 @dummy2(i8);
+declare i32 @__gxx_personality_v0(...)
>From 9978b7cca6405f901fbcf072f19d92f4ad2e979b Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Sat, 13 Apr 2024 09:17:34 +0200
Subject: [PATCH 2/2] fixup! [MergeFunc] Handle ConstantRange attributes.
---
llvm/lib/Transforms/Utils/FunctionComparator.cpp | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/llvm/lib/Transforms/Utils/FunctionComparator.cpp b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
index 0c520ec7c5d2dd..67aeba7048f860 100644
--- a/llvm/lib/Transforms/Utils/FunctionComparator.cpp
+++ b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
@@ -145,12 +145,16 @@ int FunctionComparator::cmpAttrs(const AttributeList L,
continue;
} else if (LA.isConstantRangeAttribute() &&
RA.isConstantRangeAttribute()) {
+ if (LA.getKindAsEnum() != RA.getKindAsEnum())
+ return cmpNumbers(LA.getKindAsEnum(), RA.getKindAsEnum());
+
ConstantRange LCR = LA.getRange();
ConstantRange RCR = RA.getRange();
if (int Res = cmpAPInts(LCR.getLower(), RCR.getLower()))
return Res;
if (int Res = cmpAPInts(LCR.getUpper(), RCR.getUpper()))
return Res;
+ continue;
}
if (LA < RA)
return -1;
More information about the llvm-commits
mailing list