aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/ConstantRange.cpp
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2007-07-14 02:51:34 +0000
committerNick Lewycky <nicholas@mxc.ca>2007-07-14 02:51:34 +0000
commit61b4a265ee5eda57d390f7fbb54b1a091c21b2c0 (patch)
tree31d19bca3ec75463408f30f731d4e517f9c82ff4 /llvm/lib/Support/ConstantRange.cpp
parent4481b428db0f778ac919ddab7443a602e7cf25f4 (diff)
downloadllvm-61b4a265ee5eda57d390f7fbb54b1a091c21b2c0.zip
llvm-61b4a265ee5eda57d390f7fbb54b1a091c21b2c0.tar.gz
llvm-61b4a265ee5eda57d390f7fbb54b1a091c21b2c0.tar.bz2
Add alternate ConstantRange intersection algorithm.
llvm-svn: 39851
Diffstat (limited to 'llvm/lib/Support/ConstantRange.cpp')
-rw-r--r--llvm/lib/Support/ConstantRange.cpp81
1 files changed, 81 insertions, 0 deletions
diff --git a/llvm/lib/Support/ConstantRange.cpp b/llvm/lib/Support/ConstantRange.cpp
index 966de80..4f132e5 100644
--- a/llvm/lib/Support/ConstantRange.cpp
+++ b/llvm/lib/Support/ConstantRange.cpp
@@ -246,6 +246,87 @@ ConstantRange ConstantRange::intersectWith(const ConstantRange &CR) const {
return *this;
}
+/// maximalIntersectWith - Return the range that results from the intersection
+/// of this range with another range. The resultant range is guaranteed to
+/// include all elements contained in both input ranges, and is also guaranteed
+/// to be the smallest possible set that does so.
+ConstantRange ConstantRange::maximalIntersectWith(const ConstantRange &CR) const {
+ assert(getBitWidth() == CR.getBitWidth() &&
+ "ConstantRange types don't agree!");
+
+ // Handle common cases.
+ if ( isEmptySet() || CR.isFullSet()) return *this;
+ if (CR.isEmptySet() || isFullSet()) return CR;
+
+ if (!isWrappedSet() && CR.isWrappedSet())
+ return CR.maximalIntersectWith(*this);
+
+ if (!isWrappedSet() && !CR.isWrappedSet()) {
+ if (Lower.ult(CR.Lower)) {
+ if (Upper.ule(CR.Lower))
+ return ConstantRange(getBitWidth(), false);
+
+ if (Upper.ult(CR.Upper))
+ return ConstantRange(CR.Lower, Upper);
+
+ return CR;
+ } else {
+ if (Upper.ult(CR.Upper))
+ return *this;
+
+ if (Lower.ult(CR.Upper))
+ return ConstantRange(Lower, CR.Upper);
+
+ return ConstantRange(getBitWidth(), false);
+ }
+ }
+
+ if (isWrappedSet() && !CR.isWrappedSet()) {
+ if (CR.Lower.ult(Upper)) {
+ if (CR.Upper.ult(Upper))
+ return CR;
+
+ if (CR.Upper.ult(Lower))
+ return ConstantRange(CR.Lower, Upper);
+
+ if (getSetSize().ult(CR.getSetSize()))
+ return *this;
+ else
+ return CR;
+ } else if (CR.Lower.ult(Lower)) {
+ if (CR.Upper.ule(Lower))
+ return ConstantRange(getBitWidth(), false);
+
+ return ConstantRange(Lower, CR.Upper);
+ }
+ return CR;
+ }
+
+ if (CR.Upper.ult(Upper)) {
+ if (CR.Lower.ult(Upper)) {
+ if (getSetSize().ult(CR.getSetSize()))
+ return *this;
+ else
+ return CR;
+ }
+
+ if (CR.Lower.ult(Lower))
+ return ConstantRange(Lower, CR.Upper);
+
+ return CR;
+ } else if (CR.Upper.ult(Lower)) {
+ if (CR.Lower.ult(Lower))
+ return *this;
+
+ return ConstantRange(CR.Lower, Upper);
+ }
+ if (getSetSize().ult(CR.getSetSize()))
+ return *this;
+ else
+ return CR;
+}
+
+
/// unionWith - Return the range that results from the union of this range with
/// another range. The resultant range is guaranteed to include the elements of
/// both sets, but may contain more. For example, [3, 9) union [12,15) is