Path: blob/main/contrib/llvm-project/llvm/lib/IR/ConstantRangeList.cpp
35234 views
//===- ConstantRangeList.cpp - ConstantRangeList implementation -----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/IR/ConstantRangeList.h"9#include <cstddef>1011using namespace llvm;1213bool ConstantRangeList::isOrderedRanges(ArrayRef<ConstantRange> RangesRef) {14if (RangesRef.empty())15return true;16auto Range = RangesRef[0];17if (Range.getLower().sge(Range.getUpper()))18return false;19for (unsigned i = 1; i < RangesRef.size(); i++) {20auto CurRange = RangesRef[i];21auto PreRange = RangesRef[i - 1];22if (CurRange.getLower().sge(CurRange.getUpper()) ||23CurRange.getLower().sle(PreRange.getUpper()))24return false;25}26return true;27}2829std::optional<ConstantRangeList>30ConstantRangeList::getConstantRangeList(ArrayRef<ConstantRange> RangesRef) {31if (!isOrderedRanges(RangesRef))32return std::nullopt;33return ConstantRangeList(RangesRef);34}3536void ConstantRangeList::insert(const ConstantRange &NewRange) {37if (NewRange.isEmptySet())38return;39assert(!NewRange.isFullSet() && "Do not support full set");40assert(NewRange.getLower().slt(NewRange.getUpper()));41assert(getBitWidth() == NewRange.getBitWidth());42// Handle common cases.43if (empty() || Ranges.back().getUpper().slt(NewRange.getLower())) {44Ranges.push_back(NewRange);45return;46}47if (NewRange.getUpper().slt(Ranges.front().getLower())) {48Ranges.insert(Ranges.begin(), NewRange);49return;50}5152auto LowerBound = lower_bound(53Ranges, NewRange, [](const ConstantRange &a, const ConstantRange &b) {54return a.getLower().slt(b.getLower());55});56if (LowerBound != Ranges.end() && LowerBound->contains(NewRange))57return;5859// Slow insert.60SmallVector<ConstantRange, 2> ExistingTail(LowerBound, Ranges.end());61Ranges.erase(LowerBound, Ranges.end());62// Merge consecutive ranges.63if (!Ranges.empty() && NewRange.getLower().sle(Ranges.back().getUpper())) {64APInt NewLower = Ranges.back().getLower();65APInt NewUpper =66APIntOps::smax(NewRange.getUpper(), Ranges.back().getUpper());67Ranges.back() = ConstantRange(NewLower, NewUpper);68} else {69Ranges.push_back(NewRange);70}71for (auto Iter = ExistingTail.begin(); Iter != ExistingTail.end(); Iter++) {72if (Ranges.back().getUpper().slt(Iter->getLower())) {73Ranges.push_back(*Iter);74} else {75APInt NewLower = Ranges.back().getLower();76APInt NewUpper =77APIntOps::smax(Iter->getUpper(), Ranges.back().getUpper());78Ranges.back() = ConstantRange(NewLower, NewUpper);79}80}81}8283void ConstantRangeList::subtract(const ConstantRange &SubRange) {84if (SubRange.isEmptySet() || empty())85return;86assert(!SubRange.isFullSet() && "Do not support full set");87assert(SubRange.getLower().slt(SubRange.getUpper()));88assert(getBitWidth() == SubRange.getBitWidth());89// Handle common cases.90if (Ranges.back().getUpper().sle(SubRange.getLower()))91return;92if (SubRange.getUpper().sle(Ranges.front().getLower()))93return;9495SmallVector<ConstantRange, 2> Result;96auto AppendRangeIfNonEmpty = [&Result](APInt Start, APInt End) {97if (Start.slt(End))98Result.push_back(ConstantRange(Start, End));99};100for (auto &Range : Ranges) {101if (SubRange.getUpper().sle(Range.getLower()) ||102Range.getUpper().sle(SubRange.getLower())) {103// "Range" and "SubRange" do not overlap.104// L---U : Range105// L---U : SubRange (Case1)106// L---U : SubRange (Case2)107Result.push_back(Range);108} else if (Range.getLower().sle(SubRange.getLower()) &&109SubRange.getUpper().sle(Range.getUpper())) {110// "Range" contains "SubRange".111// L---U : Range112// L-U : SubRange113// Note that ConstantRange::contains(ConstantRange) checks unsigned,114// but we need signed checking here.115AppendRangeIfNonEmpty(Range.getLower(), SubRange.getLower());116AppendRangeIfNonEmpty(SubRange.getUpper(), Range.getUpper());117} else if (SubRange.getLower().sle(Range.getLower()) &&118Range.getUpper().sle(SubRange.getUpper())) {119// "SubRange" contains "Range".120// L-U : Range121// L---U : SubRange122continue;123} else if (Range.getLower().sge(SubRange.getLower()) &&124Range.getLower().sle(SubRange.getUpper())) {125// "Range" and "SubRange" overlap at the left.126// L---U : Range127// L---U : SubRange128AppendRangeIfNonEmpty(SubRange.getUpper(), Range.getUpper());129} else {130// "Range" and "SubRange" overlap at the right.131// L---U : Range132// L---U : SubRange133assert(SubRange.getLower().sge(Range.getLower()) &&134SubRange.getLower().sle(Range.getUpper()));135AppendRangeIfNonEmpty(Range.getLower(), SubRange.getLower());136}137}138139Ranges = Result;140}141142ConstantRangeList143ConstantRangeList::unionWith(const ConstantRangeList &CRL) const {144assert(getBitWidth() == CRL.getBitWidth() &&145"ConstantRangeList bitwidths don't agree!");146// Handle common cases.147if (empty())148return CRL;149if (CRL.empty())150return *this;151152ConstantRangeList Result;153size_t i = 0, j = 0;154// "PreviousRange" tracks the lowest unioned range that is being processed.155// Its lower is fixed and the upper may be updated over iterations.156ConstantRange PreviousRange(getBitWidth(), false);157if (Ranges[i].getLower().slt(CRL.Ranges[j].getLower())) {158PreviousRange = Ranges[i++];159} else {160PreviousRange = CRL.Ranges[j++];161}162163// Try to union "PreviousRange" and "CR". If they are disjoint, push164// "PreviousRange" to the result and assign it to "CR", a new union range.165// Otherwise, update the upper of "PreviousRange" to cover "CR". Note that,166// the lower of "PreviousRange" is always less or equal the lower of "CR".167auto UnionAndUpdateRange = [&PreviousRange,168&Result](const ConstantRange &CR) {169if (PreviousRange.getUpper().slt(CR.getLower())) {170Result.Ranges.push_back(PreviousRange);171PreviousRange = CR;172} else {173PreviousRange = ConstantRange(174PreviousRange.getLower(),175APIntOps::smax(PreviousRange.getUpper(), CR.getUpper()));176}177};178while (i < size() || j < CRL.size()) {179if (j == CRL.size() ||180(i < size() && Ranges[i].getLower().slt(CRL.Ranges[j].getLower()))) {181// Merge PreviousRange with this.182UnionAndUpdateRange(Ranges[i++]);183} else {184// Merge PreviousRange with CRL.185UnionAndUpdateRange(CRL.Ranges[j++]);186}187}188Result.Ranges.push_back(PreviousRange);189return Result;190}191192ConstantRangeList193ConstantRangeList::intersectWith(const ConstantRangeList &CRL) const {194assert(getBitWidth() == CRL.getBitWidth() &&195"ConstantRangeList bitwidths don't agree!");196197// Handle common cases.198if (empty())199return *this;200if (CRL.empty())201return CRL;202203ConstantRangeList Result;204size_t i = 0, j = 0;205while (i < size() && j < CRL.size()) {206auto &Range = this->Ranges[i];207auto &OtherRange = CRL.Ranges[j];208209// The intersection of two Ranges is (max(lowers), min(uppers)), and it's210// possible that max(lowers) > min(uppers) if they don't have intersection.211// Add the intersection to result only if it's non-empty.212// To keep simple, we don't call ConstantRange::intersectWith() as it213// considers the complex upper wrapped case and may result two ranges,214// like (2, 8) && (6, 4) = {(2, 4), (6, 8)}.215APInt Start = APIntOps::smax(Range.getLower(), OtherRange.getLower());216APInt End = APIntOps::smin(Range.getUpper(), OtherRange.getUpper());217if (Start.slt(End))218Result.Ranges.push_back(ConstantRange(Start, End));219220// Move to the next Range in one list determined by the uppers.221// For example: A = {(0, 2), (4, 8)}; B = {(-2, 5), (6, 10)}222// We need to intersect three pairs: A0 && B0; A1 && B0; A1 && B1.223if (Range.getUpper().slt(OtherRange.getUpper()))224i++;225else226j++;227}228return Result;229}230231void ConstantRangeList::print(raw_ostream &OS) const {232interleaveComma(Ranges, OS, [&](ConstantRange CR) {233OS << "(" << CR.getLower() << ", " << CR.getUpper() << ")";234});235}236237#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)238LLVM_DUMP_METHOD void ConstantRangeList::dump() const {239print(dbgs());240dbgs() << '\n';241}242#endif243244245