[llvm] 9a24f21 - [MergeFuncs] Handle ConstantRangeList attributes

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 6 03:21:57 PST 2024


Author: Nikita Popov
Date: 2024-12-06T12:21:45+01:00
New Revision: 9a24f2198ec02960c9e9afedace96ba6afa9c5b1

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

LOG: [MergeFuncs] Handle ConstantRangeList attributes

Support comparison of ConstantRangeList attributes in
FunctionComparator.

Added: 
    llvm/test/Transforms/MergeFunc/initializes-attr.ll

Modified: 
    llvm/include/llvm/Transforms/Utils/FunctionComparator.h
    llvm/lib/Transforms/Utils/FunctionComparator.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Transforms/Utils/FunctionComparator.h b/llvm/include/llvm/Transforms/Utils/FunctionComparator.h
index c28f868039a1f7..19c5f7449f23ee 100644
--- a/llvm/include/llvm/Transforms/Utils/FunctionComparator.h
+++ b/llvm/include/llvm/Transforms/Utils/FunctionComparator.h
@@ -317,6 +317,7 @@ class FunctionComparator {
   int cmpNumbers(uint64_t L, uint64_t R) const;
   int cmpAligns(Align L, Align R) const;
   int cmpAPInts(const APInt &L, const APInt &R) const;
+  int cmpConstantRanges(const ConstantRange &L, const ConstantRange &R) const;
   int cmpAPFloats(const APFloat &L, const APFloat &R) const;
   int cmpMem(StringRef L, StringRef R) const;
 

diff  --git a/llvm/lib/Transforms/Utils/FunctionComparator.cpp b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
index 760341a29d8c8a..6d4026e8209de2 100644
--- a/llvm/lib/Transforms/Utils/FunctionComparator.cpp
+++ b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
@@ -83,6 +83,13 @@ int FunctionComparator::cmpAPInts(const APInt &L, const APInt &R) const {
   return 0;
 }
 
+int FunctionComparator::cmpConstantRanges(const ConstantRange &L,
+                                          const ConstantRange &R) const {
+  if (int Res = cmpAPInts(L.getLower(), R.getLower()))
+    return Res;
+  return cmpAPInts(L.getUpper(), R.getUpper());
+}
+
 int FunctionComparator::cmpAPFloats(const APFloat &L, const APFloat &R) const {
   // Floats are ordered first by semantics (i.e. float, double, half, etc.),
   // then by value interpreted as a bitstring (aka APInt).
@@ -147,12 +154,22 @@ int FunctionComparator::cmpAttrs(const AttributeList L,
         if (LA.getKindAsEnum() != RA.getKindAsEnum())
           return cmpNumbers(LA.getKindAsEnum(), RA.getKindAsEnum());
 
-        const ConstantRange &LCR = LA.getRange();
-        const ConstantRange &RCR = RA.getRange();
-        if (int Res = cmpAPInts(LCR.getLower(), RCR.getLower()))
+        if (int Res = cmpConstantRanges(LA.getRange(), RA.getRange()))
           return Res;
-        if (int Res = cmpAPInts(LCR.getUpper(), RCR.getUpper()))
+        continue;
+      } else if (LA.isConstantRangeListAttribute() &&
+                 RA.isConstantRangeListAttribute()) {
+        if (LA.getKindAsEnum() != RA.getKindAsEnum())
+          return cmpNumbers(LA.getKindAsEnum(), RA.getKindAsEnum());
+
+        ArrayRef<ConstantRange> CRL = LA.getValueAsConstantRangeList();
+        ArrayRef<ConstantRange> CRR = RA.getValueAsConstantRangeList();
+        if (int Res = cmpNumbers(CRL.size(), CRR.size()))
           return Res;
+
+        for (const auto &[L, R] : zip(CRL, CRR))
+          if (int Res = cmpConstantRanges(L, R))
+            return Res;
         continue;
       }
       if (LA < RA)
@@ -441,9 +458,7 @@ int FunctionComparator::cmpConstants(const Constant *L,
       if (InRangeL) {
         if (!InRangeR)
           return 1;
-        if (int Res = cmpAPInts(InRangeL->getLower(), InRangeR->getLower()))
-          return Res;
-        if (int Res = cmpAPInts(InRangeL->getUpper(), InRangeR->getUpper()))
+        if (int Res = cmpConstantRanges(*InRangeL, *InRangeR))
           return Res;
       } else if (InRangeR) {
         return -1;

diff  --git a/llvm/test/Transforms/MergeFunc/initializes-attr.ll b/llvm/test/Transforms/MergeFunc/initializes-attr.ll
new file mode 100644
index 00000000000000..0bd4fe2e474dd2
--- /dev/null
+++ b/llvm/test/Transforms/MergeFunc/initializes-attr.ll
@@ -0,0 +1,53 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=mergefunc < %s | FileCheck %s
+
+define internal void @test1(ptr initializes((0, 1)) %p) {
+; CHECK-LABEL: define internal void @test1(
+; CHECK-SAME: ptr initializes((0, 1)) [[P:%.*]]) {
+; CHECK-NEXT:    store i16 0, ptr [[P]], align 2
+; CHECK-NEXT:    ret void
+;
+  store i16 0, ptr %p
+  ret void
+}
+
+define internal void @test2(ptr initializes((0, 1)) %p) {
+  store i16 0, ptr %p
+  ret void
+}
+
+define internal void @test3(ptr initializes((0, 2)) %p) {
+; CHECK-LABEL: define internal void @test3(
+; CHECK-SAME: ptr initializes((0, 2)) [[P:%.*]]) {
+; CHECK-NEXT:    store i16 0, ptr [[P]], align 2
+; CHECK-NEXT:    ret void
+;
+  store i16 0, ptr %p
+  ret void
+}
+
+define internal void @test4(ptr initializes((0, 1), (2, 3)) %p) {
+; CHECK-LABEL: define internal void @test4(
+; CHECK-SAME: ptr initializes((0, 1), (2, 3)) [[P:%.*]]) {
+; CHECK-NEXT:    store i16 0, ptr [[P]], align 2
+; CHECK-NEXT:    ret void
+;
+  store i16 0, ptr %p
+  ret void
+}
+
+define void @do_calls(ptr %p) {
+; CHECK-LABEL: define void @do_calls(
+; CHECK-SAME: ptr [[P:%.*]]) {
+; CHECK-NEXT:    call void @test1(ptr [[P]])
+; CHECK-NEXT:    call void @test1(ptr [[P]])
+; CHECK-NEXT:    call void @test3(ptr [[P]])
+; CHECK-NEXT:    call void @test4(ptr [[P]])
+; CHECK-NEXT:    ret void
+;
+  call void @test1(ptr %p)
+  call void @test2(ptr %p)
+  call void @test3(ptr %p)
+  call void @test4(ptr %p)
+  ret void
+}


        


More information about the llvm-commits mailing list