summaryrefslogtreecommitdiffstats
path: root/llvm/lib/IR/ConstantRange.cpp
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2019-03-17 20:24:02 +0000
committerNikita Popov <nikita.ppv@gmail.com>2019-03-17 20:24:02 +0000
commitef2d9799435807d0b4143ff4975336842c7eab0b (patch)
treeca26a9c8d46b4b7d2f42c9dda8dc4513cad445c8 /llvm/lib/IR/ConstantRange.cpp
parentb3bcd95771810f75736c1be0088618a03c9f0fe3 (diff)
downloadbcm5719-llvm-ef2d9799435807d0b4143ff4975336842c7eab0b.tar.gz
bcm5719-llvm-ef2d9799435807d0b4143ff4975336842c7eab0b.zip
[ConstantRange] Add fromKnownBits() method
Following the suggestion in D59450, I'm moving the code for constructing a ConstantRange from KnownBits out of ValueTracking, which also allows us to test this code independently. I'm adding this method to ConstantRange rather than KnownBits (which would have been a bit nicer API wise) to avoid creating a dependency from Support to IR, where ConstantRange lives. Differential Revision: https://reviews.llvm.org/D59475 llvm-svn: 356339
Diffstat (limited to 'llvm/lib/IR/ConstantRange.cpp')
-rw-r--r--llvm/lib/IR/ConstantRange.cpp19
1 files changed, 19 insertions, 0 deletions
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 23737bb4650..d41914ba4a9 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -31,6 +31,7 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/KnownBits.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
@@ -53,6 +54,24 @@ ConstantRange::ConstantRange(APInt L, APInt U)
"Lower == Upper, but they aren't min or max value!");
}
+ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known,
+ bool IsSigned) {
+ if (Known.isUnknown())
+ return ConstantRange(Known.getBitWidth(), /* full */ true);
+
+ // For unsigned ranges, or signed ranges with known sign bit, create a simple
+ // range between the smallest and largest possible value.
+ if (!IsSigned || Known.isNegative() || Known.isNonNegative())
+ return ConstantRange(Known.One, ~Known.Zero + 1);
+
+ // If we don't know the sign bit, pick the lower bound as a negative number
+ // and the upper bound as a non-negative one.
+ APInt Lower = Known.One, Upper = ~Known.Zero;
+ Lower.setSignBit();
+ Upper.clearSignBit();
+ return ConstantRange(Lower, Upper + 1);
+}
+
ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
const ConstantRange &CR) {
if (CR.isEmptySet())
OpenPOWER on IntegriCloud