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

Jan Voung via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 16 06:37:40 PDT 2024


================
@@ -0,0 +1,83 @@
+//===- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Represent a list of signed ConstantRange and do NOT support wrap around the
+// end of the numeric range. Ranges in the list are ordered and no overlapping.
+// Ranges should have the same bitwidth. Each range's lower should be less than
+// its upper. Special lists (take 8-bit as an example):
+//
+// {[0, 0)}     = Empty set
+// {[255, 255)} = Full Set
+//
+// For EmptySet or FullSet, the list size is 1 but it's not allowed to access
+// the range.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/ConstantRangeList.h"
+#include <cstddef>
+
+using namespace llvm;
+
+ConstantRangeList::ConstantRangeList(uint32_t BitWidth, bool Full) {
+  APInt Lower =
+      Full ? APInt::getMaxValue(BitWidth) : APInt::getMinValue(BitWidth);
+  Ranges.push_back(ConstantRange(Lower, Lower));
+}
+
+void ConstantRangeList::insert(const ConstantRange &Range) {
+  assert(Range.getLower().slt(Range.getUpper()));
+  assert(getBitWidth() == Range.getBitWidth());
+  if (isFullSet())
+    return;
+  if (isEmptySet()) {
+    Ranges[0] = Range;
+    return;
+  }
+
+  ConstantRange RangeToInsert = Range;
+  SmallVector<ConstantRange, 2> ExistingRanges(Ranges.begin(), Ranges.end());
+  Ranges.clear();
----------------
jvoung wrote:

Thanks for adjusting the append -> insert and keeping things sorted. I do realize that most current uses this will insert at the end, so the old "append" made sense in that way.

Is it possible to optimize this a bit more to avoid potential quadratic behavior in the common case of insert at end?

- instead of copying to ExistingRanges and clearing Ranges, keep as much of Ranges for Case 1, and only have potential copying for the tail where it becomes case 2 or 3?
- std::lower_bound might help find where case 2 and 3 start?

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


More information about the llvm-commits mailing list