[llvm] Add ConstantRangeList::unionWith() and ::intersectWith() (PR #96547)

Jan Voung via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 25 10:20:51 PDT 2024


================
@@ -81,6 +81,97 @@ void ConstantRangeList::insert(const ConstantRange &NewRange) {
   }
 }
 
+ConstantRangeList
+ConstantRangeList::unionWith(const ConstantRangeList &CRL) const {
+  assert(getBitWidth() == CRL.getBitWidth() &&
+         "ConstantRangeList bitwidths don't agree!");
+  // Handle common cases.
+  if (empty())
+    return CRL;
+  if (CRL.empty())
+    return *this;
+
+  ConstantRangeList Result;
+  size_t i = 0, j = 0;
+  // "PreviousRange" tracks the unioned range (its lower is fixed
+  // and the upper may be updated over iterations).
+  // If "PreviousRange" cannot contain a new unioned range, push it
+  // to the result and assign "PreviousRange" with the new range.
+  ConstantRange PreviousRange(getBitWidth(), false);
+  if (Ranges[i].getLower().slt(CRL.Ranges[j].getLower())) {
+    PreviousRange = Ranges[i++];
+  } else {
+    PreviousRange = CRL.Ranges[j++];
+  }
+
+  // Union "PreviousRange" and "CR". If they are disjoint, push "PreviousRange"
----------------
jvoung wrote:

nit: *Try* to union ...?


https://github.com/llvm/llvm-project/pull/96547


More information about the llvm-commits mailing list