LLVM 20.0.0git
ConstantRangeList.h
Go to the documentation of this file.
1//===- ConstantRangeList.h - A list of constant ranges ----------*- 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// Represent a list of signed ConstantRange and do NOT support wrap around the
10// end of the numeric range. Ranges in the list are ordered and not overlapping.
11// Ranges should have the same bitwidth. Each range's lower should be less than
12// its upper.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_IR_CONSTANTRANGELIST_H
17#define LLVM_IR_CONSTANTRANGELIST_H
18
19#include "llvm/ADT/APInt.h"
21#include "llvm/Support/Debug.h"
22#include <cstddef>
23#include <cstdint>
24
25namespace llvm {
26
27class raw_ostream;
28
29/// This class represents a list of constant ranges.
30class [[nodiscard]] ConstantRangeList {
32
33public:
34 ConstantRangeList() = default;
36 assert(isOrderedRanges(RangesRef));
37 for (const ConstantRange &R : RangesRef) {
38 assert(empty() || R.getBitWidth() == getBitWidth());
39 Ranges.push_back(R);
40 }
41 }
42
43 // Return true if the ranges are non-overlapping and increasing.
44 static bool isOrderedRanges(ArrayRef<ConstantRange> RangesRef);
45 static std::optional<ConstantRangeList>
46 getConstantRangeList(ArrayRef<ConstantRange> RangesRef);
47
48 ArrayRef<ConstantRange> rangesRef() const { return Ranges; }
52 return Ranges.begin();
53 }
55 return Ranges.end();
56 }
57 ConstantRange getRange(unsigned i) const { return Ranges[i]; }
58
59 /// Return true if this list contains no members.
60 bool empty() const { return Ranges.empty(); }
61
62 /// Get the bit width of this ConstantRangeList. It is invalid to call this
63 /// with an empty range.
64 uint32_t getBitWidth() const { return Ranges.front().getBitWidth(); }
65
66 /// Return the number of ranges in this ConstantRangeList.
67 size_t size() const { return Ranges.size(); }
68
69 /// Insert a new range to Ranges and keep the list ordered.
70 void insert(const ConstantRange &NewRange);
71 void insert(int64_t Lower, int64_t Upper) {
72 insert(ConstantRange(APInt(64, Lower, /*isSigned=*/true),
73 APInt(64, Upper, /*isSigned=*/true)));
74 }
75
76 void subtract(const ConstantRange &SubRange);
77
78 /// Return the range list that results from the union of this
79 /// ConstantRangeList with another ConstantRangeList, "CRL".
80 ConstantRangeList unionWith(const ConstantRangeList &CRL) const;
81
82 /// Return the range list that results from the intersection of this
83 /// ConstantRangeList with another ConstantRangeList, "CRL".
84 ConstantRangeList intersectWith(const ConstantRangeList &CRL) const;
85
86 /// Return true if this range list is equal to another range list.
87 bool operator==(const ConstantRangeList &CRL) const {
88 return Ranges == CRL.Ranges;
89 }
90 bool operator!=(const ConstantRangeList &CRL) const {
91 return !operator==(CRL);
92 }
93
94 /// Print out the ranges to a stream.
95 void print(raw_ostream &OS) const;
96
97#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
98 void dump() const;
99#endif
100};
101
102} // end namespace llvm
103
104#endif // LLVM_IR_CONSTANTRANGELIST_H
This file implements a class to represent arbitrary precision integral constant values and operations...
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Class for arbitrary precision integers.
Definition: APInt.h:78
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This class represents a list of constant ranges.
ConstantRange getRange(unsigned i) const
uint32_t getBitWidth() const
Get the bit width of this ConstantRangeList.
SmallVectorImpl< ConstantRange >::iterator begin()
SmallVectorImpl< ConstantRange >::const_iterator begin() const
SmallVectorImpl< ConstantRange >::const_iterator end() const
bool empty() const
Return true if this list contains no members.
SmallVectorImpl< ConstantRange >::iterator end()
ArrayRef< ConstantRange > rangesRef() const
void insert(int64_t Lower, int64_t Upper)
size_t size() const
Return the number of ranges in this ConstantRangeList.
bool operator!=(const ConstantRangeList &CRL) const
bool operator==(const ConstantRangeList &CRL) const
Return true if this range list is equal to another range list.
ConstantRangeList(ArrayRef< ConstantRange > RangesRef)
This class represents a range of values.
Definition: ConstantRange.h:47
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:578
typename SuperClass::iterator iterator
Definition: SmallVector.h:577
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)