[llvm] [SandboxVec] Simple Instruction Interval class (PR #108882)

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 18 08:37:40 PDT 2024


================
@@ -0,0 +1,109 @@
+//===- InstrInterval.h ------------------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// The InstrInterval class is an interval of instructions in a block.
+// It provides an API for some basic operations on the interval, including some
+// simple set operations, like union, interseciton and others.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_INSTRINTERVAL_H
+#define LLVM_TRANSFORMS_VECTORIZE_SANDBOXVECTORIZER_INSTRINTERVAL_H
+
+#include "llvm/SandboxIR/SandboxIR.h"
+#include <iterator>
+
+namespace llvm::sandboxir {
+
+/// A simple iterator for iterating the interval.
+template <typename DerefType, typename InstrIntervalType>
+class InstrIntervalIterator {
+  sandboxir::Instruction *I;
+  InstrIntervalType &R;
+
+public:
+  using difference_type = std::ptrdiff_t;
+  using value_type = sandboxir::Instruction;
+  using pointer = value_type *;
+  using reference = sandboxir::Instruction &;
+  using iterator_category = std::bidirectional_iterator_tag;
+
+  InstrIntervalIterator(sandboxir::Instruction *I, InstrIntervalType &R)
+      : I(I), R(R) {}
+  bool operator==(const InstrIntervalIterator &Other) const {
+    assert(&R == &Other.R && "Iterators belong to different regions!");
+    return Other.I == I;
+  }
+  bool operator!=(const InstrIntervalIterator &Other) const {
+    return !(*this == Other);
+  }
+  InstrIntervalIterator &operator++() {
+    assert(I != nullptr && "already at end()!");
+    I = I->getNextNode();
+    return *this;
+  }
+  InstrIntervalIterator operator++(int) {
+    auto ItCopy = *this;
+    ++*this;
+    return ItCopy;
+  }
+  InstrIntervalIterator &operator--() {
+    // `I` is nullptr for end() when ToI is the BB terminator.
+    I = I != nullptr ? I->getPrevNode() : R.ToI;
+    return *this;
+  }
+  InstrIntervalIterator operator--(int) {
+    auto ItCopy = *this;
+    --*this;
+    return ItCopy;
+  }
+  template <typename T =
+                std::enable_if<std::is_same<DerefType, Instruction *&>::value>>
+  sandboxir::Instruction &operator*() {
+    return *I;
+  }
+  DerefType operator*() const { return *I; }
+};
+
+class InstrInterval {
+  Instruction *FromI;
+  Instruction *ToI;
+
+public:
+  InstrInterval() : FromI(nullptr), ToI(nullptr) {}
+  InstrInterval(Instruction *FromI, Instruction *ToI)
+      : FromI(FromI), ToI(ToI) {}
----------------
aeubanks wrote:

`assert(FromI->comesBefore(ToI))`?

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


More information about the llvm-commits mailing list