LLVM 22.0.0git
IntegerInclusiveInterval.h
Go to the documentation of this file.
1//===- IntegerInclusiveInterval.h -------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the IntegerInclusiveInterval class and utilities for
10// handling lists of inclusive integer intervals, such as parsing interval
11// strings like "1-10,20-30,45", which are used in debugging and bisection
12// tools.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_SUPPORT_INTEGER_INCLUSIVE_INTERVAL_H
17#define LLVM_SUPPORT_INTEGER_INCLUSIVE_INTERVAL_H
18
19#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/Error.h"
23#include <cassert>
24#include <cstdint>
25
26namespace llvm {
27class raw_ostream;
28} // end namespace llvm
29
30namespace llvm {
31
32/// Represents an inclusive integer interval [Begin, End] where Begin <= End.
34 int64_t Begin;
35 int64_t End;
36
37public:
38 /// Create an interval [Begin, End].
39 IntegerInclusiveInterval(int64_t Begin, int64_t End)
40 : Begin(Begin), End(End) {
41 assert(Begin <= End && "Interval Begin must be <= End");
42 }
43 /// Create a singleton interval [Single, Single].
44 IntegerInclusiveInterval(int64_t Single) : Begin(Single), End(Single) {}
45
46 int64_t getBegin() const { return Begin; }
47 int64_t getEnd() const { return End; }
48
49 void setBegin(int64_t NewBegin) {
50 assert(NewBegin <= End && "Interval Begin must be <= End");
51 Begin = NewBegin;
52 }
53 void setEnd(int64_t NewEnd) {
54 assert(Begin <= NewEnd && "Interval Begin must be <= End");
55 End = NewEnd;
56 }
57
58 /// Check if the given value is within this interval (inclusive).
59 bool contains(int64_t Value) const { return Value >= Begin && Value <= End; }
60
61 /// Check if this interval overlaps with another interval.
63 return Begin <= Other.End && End >= Other.Begin;
64 }
65
66 /// Print the interval to the output stream.
67 void print(raw_ostream &OS) const {
68 if (Begin == End)
69 OS << Begin;
70 else
71 OS << Begin << "-" << End;
72 }
73
75 return Begin == Other.Begin && End == Other.End;
76 }
77};
78
80
81/// A list of integer intervals.
83
84/// Parse a interval specification string like "1-10,20-30,45" or
85/// "1-10:20-30:45". Intervals must be in increasing order and non-overlapping.
86/// \param IntervalStr The string to parse.
87/// \param Separator The separator character to use (',' or ':').
88/// \returns Expected<IntervalList> containing the parsed intervals on success,
89/// or an Error on failure.
91 char Separator = ',');
92
93/// Check if a value is contained in any of the intervals.
95
96/// Print intervals to output stream.
97/// \param OS The output stream to print to.
98/// \param Intervals The intervals to print.
99/// \param Separator The separator character to use between intervals (i.e. ','
100/// or
101/// ':').
104 char Separator = ',');
105
106/// Merge adjacent/consecutive intervals into single intervals.
107/// Example: [1-3, 4-6, 8-10] -> [1-6, 8-10].
110
111} // end namespace IntegerInclusiveIntervalUtils
112
113} // end namespace llvm
114
115#endif // LLVM_SUPPORT_INTEGER_INCLUSIVE_INTERVAL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the SmallVector class.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Tagged union holding either a T or a Error.
Definition Error.h:485
void print(raw_ostream &OS) const
Print the interval to the output stream.
bool overlaps(const IntegerInclusiveInterval &Other) const
Check if this interval overlaps with another interval.
bool operator==(const IntegerInclusiveInterval &Other) const
bool contains(int64_t Value) const
Check if the given value is within this interval (inclusive).
IntegerInclusiveInterval(int64_t Single)
Create a singleton interval [Single, Single].
IntegerInclusiveInterval(int64_t Begin, int64_t End)
Create an interval [Begin, End].
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
void printIntervals(raw_ostream &OS, ArrayRef< IntegerInclusiveInterval > Intervals, char Separator=',')
Print intervals to output stream.
IntervalList mergeAdjacentIntervals(ArrayRef< IntegerInclusiveInterval > Intervals)
Merge adjacent/consecutive intervals into single intervals.
bool contains(ArrayRef< IntegerInclusiveInterval > Intervals, int64_t Value)
Check if a value is contained in any of the intervals.
SmallVector< IntegerInclusiveInterval, 8 > IntervalList
A list of integer intervals.
Expected< IntervalList > parseIntervals(StringRef IntervalStr, char Separator=',')
Parse a interval specification string like "1-10,20-30,45" or "1-10:20-30:45".
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Other
Any other memory.
Definition ModRef.h:68