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" 20 #include "llvm/IR/ConstantRange.h" 21 #include "llvm/Support/Debug.h" 22 #include <cstddef> 23 #include <cstdint> 24 25 namespace llvm { 26 27 class raw_ostream; 28 29 /// This class represents a list of constant ranges. 30 class [[nodiscard]] ConstantRangeList { 31 SmallVector<ConstantRange, 2> Ranges; 32 33 public: 34 ConstantRangeList() = default; 35 ConstantRangeList(ArrayRef<ConstantRange> RangesRef) { 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; } 49 SmallVectorImpl<ConstantRange>::iterator begin() { return Ranges.begin(); } 50 SmallVectorImpl<ConstantRange>::iterator end() { return Ranges.end(); } 51 SmallVectorImpl<ConstantRange>::const_iterator begin() const { 52 return Ranges.begin(); 53 } 54 SmallVectorImpl<ConstantRange>::const_iterator end() const { 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 105