Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:57

0001 //===- ConstantRangeList.h - A list of constant ranges ----------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Represent a list of signed ConstantRange and do NOT support wrap around the
0010 // end of the numeric range. Ranges in the list are ordered and not overlapping.
0011 // Ranges should have the same bitwidth. Each range's lower should be less than
0012 // its upper.
0013 //
0014 //===----------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_IR_CONSTANTRANGELIST_H
0017 #define LLVM_IR_CONSTANTRANGELIST_H
0018 
0019 #include "llvm/ADT/APInt.h"
0020 #include "llvm/IR/ConstantRange.h"
0021 #include "llvm/Support/Debug.h"
0022 #include <cstddef>
0023 #include <cstdint>
0024 
0025 namespace llvm {
0026 
0027 class raw_ostream;
0028 
0029 /// This class represents a list of constant ranges.
0030 class [[nodiscard]] ConstantRangeList {
0031   SmallVector<ConstantRange, 2> Ranges;
0032 
0033 public:
0034   ConstantRangeList() = default;
0035   ConstantRangeList(ArrayRef<ConstantRange> RangesRef) {
0036     assert(isOrderedRanges(RangesRef));
0037     for (const ConstantRange &R : RangesRef) {
0038       assert(empty() || R.getBitWidth() == getBitWidth());
0039       Ranges.push_back(R);
0040     }
0041   }
0042 
0043   // Return true if the ranges are non-overlapping and increasing.
0044   static bool isOrderedRanges(ArrayRef<ConstantRange> RangesRef);
0045   static std::optional<ConstantRangeList>
0046   getConstantRangeList(ArrayRef<ConstantRange> RangesRef);
0047 
0048   ArrayRef<ConstantRange> rangesRef() const { return Ranges; }
0049   SmallVectorImpl<ConstantRange>::iterator begin() { return Ranges.begin(); }
0050   SmallVectorImpl<ConstantRange>::iterator end() { return Ranges.end(); }
0051   SmallVectorImpl<ConstantRange>::const_iterator begin() const {
0052     return Ranges.begin();
0053   }
0054   SmallVectorImpl<ConstantRange>::const_iterator end() const {
0055     return Ranges.end();
0056   }
0057   ConstantRange getRange(unsigned i) const { return Ranges[i]; }
0058 
0059   /// Return true if this list contains no members.
0060   bool empty() const { return Ranges.empty(); }
0061 
0062   /// Get the bit width of this ConstantRangeList. It is invalid to call this
0063   /// with an empty range.
0064   uint32_t getBitWidth() const { return Ranges.front().getBitWidth(); }
0065 
0066   /// Return the number of ranges in this ConstantRangeList.
0067   size_t size() const { return Ranges.size(); }
0068 
0069   /// Insert a new range to Ranges and keep the list ordered.
0070   void insert(const ConstantRange &NewRange);
0071   void insert(int64_t Lower, int64_t Upper) {
0072     insert(ConstantRange(APInt(64, Lower, /*isSigned=*/true),
0073                          APInt(64, Upper, /*isSigned=*/true)));
0074   }
0075 
0076   void subtract(const ConstantRange &SubRange);
0077 
0078   /// Return the range list that results from the union of this
0079   /// ConstantRangeList with another ConstantRangeList, "CRL".
0080   ConstantRangeList unionWith(const ConstantRangeList &CRL) const;
0081 
0082   /// Return the range list that results from the intersection of this
0083   /// ConstantRangeList with another ConstantRangeList, "CRL".
0084   ConstantRangeList intersectWith(const ConstantRangeList &CRL) const;
0085 
0086   /// Return true if this range list is equal to another range list.
0087   bool operator==(const ConstantRangeList &CRL) const {
0088     return Ranges == CRL.Ranges;
0089   }
0090   bool operator!=(const ConstantRangeList &CRL) const {
0091     return !operator==(CRL);
0092   }
0093 
0094   /// Print out the ranges to a stream.
0095   void print(raw_ostream &OS) const;
0096 
0097 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0098   void dump() const;
0099 #endif
0100 };
0101 
0102 } // end namespace llvm
0103 
0104 #endif // LLVM_IR_CONSTANTRANGELIST_H