[llvm] Add the 'initializes' attribute langref and support (PR #84803)

Haopeng Liu via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 20 10:44:33 PDT 2024


================
@@ -0,0 +1,97 @@
+//===- ConstantRangeList.cpp - ConstantRangeList implementation -----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/ConstantRangeList.h"
+#include <cstddef>
+
+using namespace llvm;
+
+bool ConstantRangeList::isOrderedRanges(ArrayRef<ConstantRange> RangesRef) {
+  if (RangesRef.empty())
+    return true;
+  auto Range = RangesRef[0];
+  if (Range.getLower().sge(Range.getUpper()))
+    return false;
+  for (unsigned i = 1; i < RangesRef.size(); i++) {
+    auto CurRange = RangesRef[i];
+    auto PreRange = RangesRef[i - 1];
+    if (CurRange.getLower().sge(CurRange.getUpper()) ||
+        CurRange.getLower().sle(PreRange.getLower()) ||
+        CurRange.getLower().sle(PreRange.getUpper()))
+      return false;
+  }
+  return true;
+}
+
+std::optional<ConstantRangeList>
+ConstantRangeList::getConstantRangeList(ArrayRef<ConstantRange> RangesRef) {
+  if (!isOrderedRanges(RangesRef))
+    return std::nullopt;
+  return ConstantRangeList(RangesRef);
+}
+
+void ConstantRangeList::insert(const ConstantRange &NewRange) {
+  if (NewRange.isEmptySet())
+    return;
+  assert(!NewRange.isFullSet() && "Do not support full set");
+  assert(NewRange.getLower().slt(NewRange.getUpper()));
+  assert(getBitWidth() == NewRange.getBitWidth());
+  // Handle common cases.
+  if (empty() || Ranges.back().getUpper().slt(NewRange.getLower())) {
+    Ranges.push_back(NewRange);
+    return;
+  }
+  if (NewRange.getUpper().slt(Ranges.front().getLower())) {
+    Ranges.insert(Ranges.begin(), NewRange);
+    return;
+  }
+
+  auto LowerBound =
+      std::lower_bound(Ranges.begin(), Ranges.end(), NewRange,
+                       [](const ConstantRange &a, const ConstantRange &b) {
+                         return a.getLower().slt(b.getLower());
+                       });
+  if (LowerBound != Ranges.end() && *LowerBound == NewRange)
----------------
haopliu wrote:

Yes, changed to check whether LowerBound contains NewRange.

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


More information about the llvm-commits mailing list