From 95a3be0ba44e96308c65c28ee859acc36149ddd8 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Mon, 9 Nov 2009 15:36:28 +0000 Subject: [PATCH] add zextOrTrunc and sextOrTrunc methods, that are similar to the ones in APInt git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@86549 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/ConstantRange.h | 8 ++++++++ lib/Support/ConstantRange.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/llvm/Support/ConstantRange.h b/include/llvm/Support/ConstantRange.h index 3846d46dd34..438ec143e87 100644 --- a/include/llvm/Support/ConstantRange.h +++ b/include/llvm/Support/ConstantRange.h @@ -187,6 +187,14 @@ public: /// truncated to the specified type. ConstantRange truncate(uint32_t BitWidth) const; + /// zextOrTrunc - make this range have the bit width given by \p BitWidth. The + /// value is zero extended, truncated, or left alone to make it that width. + ConstantRange zextOrTrunc(uint32_t BitWidth) const; + + /// sextOrTrunc - make this range have the bit width given by \p BitWidth. The + /// value is sign extended, truncated, or left alone to make it that width. + ConstantRange sextOrTrunc(uint32_t BitWidth) const; + /// add - Return a new range representing the possible values resulting /// from an addition of a value in this range and a value in Other. ConstantRange add(const ConstantRange &Other) const; diff --git a/lib/Support/ConstantRange.cpp b/lib/Support/ConstantRange.cpp index 423e90d9935..a194ac4ff55 100644 --- a/lib/Support/ConstantRange.cpp +++ b/lib/Support/ConstantRange.cpp @@ -492,6 +492,30 @@ ConstantRange ConstantRange::truncate(uint32_t DstTySize) const { return ConstantRange(L, U); } +/// zextOrTrunc - make this range have the bit width given by \p DstTySize. The +/// value is zero extended, truncated, or left alone to make it that width. +ConstantRange ConstantRange::zextOrTrunc(uint32_t DstTySize) const { + unsigned SrcTySize = getBitWidth(); + if (SrcTySize > DstTySize) + return truncate(DstTySize); + else if (SrcTySize < DstTySize) + return zeroExtend(DstTySize); + else + return *this; +} + +/// sextOrTrunc - make this range have the bit width given by \p DstTySize. The +/// value is sign extended, truncated, or left alone to make it that width. +ConstantRange ConstantRange::sextOrTrunc(uint32_t DstTySize) const { + unsigned SrcTySize = getBitWidth(); + if (SrcTySize > DstTySize) + return truncate(DstTySize); + else if (SrcTySize < DstTySize) + return signExtend(DstTySize); + else + return *this; +} + ConstantRange ConstantRange::add(const ConstantRange &Other) const { if (isEmptySet() || Other.isEmptySet())