[llvm-commits] [llvm] r160223 - in /llvm/trunk/include/llvm/ADT: APInt.h APSInt.h

Chandler Carruth chandlerc at google.com
Sat Jul 14 17:27:01 PDT 2012


If you're doing this inside of APInt and APSInt, you could do it much more
cheaply by actually comparing the values rather than building a larger
zext'ed copy and then using the existing operator== logic...


On Sat, Jul 14, 2012 at 5:23 PM, Eric Christopher <echristo at apple.com>wrote:

> Author: echristo
> Date: Sat Jul 14 19:23:36 2012
> New Revision: 160223
>
> URL: http://llvm.org/viewvc/llvm-project?rev=160223&view=rev
> Log:
> Move IsSameValue from clang's ASTImporter to be methods on the
> APInt/APSInt classes.
>
> Part of rdar://11875995
>
> Modified:
>     llvm/trunk/include/llvm/ADT/APInt.h
>     llvm/trunk/include/llvm/ADT/APSInt.h
>
> Modified: llvm/trunk/include/llvm/ADT/APInt.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=160223&r1=160222&r2=160223&view=diff
>
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/APInt.h (original)
> +++ llvm/trunk/include/llvm/ADT/APInt.h Sat Jul 14 19:23:36 2012
> @@ -511,6 +511,18 @@
>      return getAllOnesValue(numBits).lshr(numBits - loBitsSet);
>    }
>
> +  /// \brief Determine if two APInts have the same value, after
> zero-extending
> +  /// one of them (if needed!) to ensure that the bit-widths match.
> +  static bool isSameValue(const APInt &I1, const APInt &I2) {
> +    if (I1.getBitWidth() == I2.getBitWidth())
> +      return I1 == I2;
> +
> +    if (I1.getBitWidth() > I2.getBitWidth())
> +      return I1 == I2.zext(I1.getBitWidth());
> +
> +    return I1.zext(I2.getBitWidth()) == I2;
> +  }
> +
>    /// \brief Overload to compute a hash_code for an APInt value.
>    friend hash_code hash_value(const APInt &Arg);
>
>
> Modified: llvm/trunk/include/llvm/ADT/APSInt.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APSInt.h?rev=160223&r1=160222&r2=160223&view=diff
>
> ==============================================================================
> --- llvm/trunk/include/llvm/ADT/APSInt.h (original)
> +++ llvm/trunk/include/llvm/ADT/APSInt.h Sat Jul 14 19:23:36 2012
> @@ -250,6 +250,33 @@
>                             : APInt::getSignedMinValue(numBits), Unsigned);
>    }
>
> +  /// \brief Determine if two APSInts have the same value, zero- or
> +  /// sign-extending as needed.
> +  static bool isSameValue(const APSInt &I1, const APSInt &I2) {
> +    if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() ==
> I2.isSigned())
> +      return I1 == I2;
> +
> +    // Check for a bit-width mismatch.
> +    if (I1.getBitWidth() > I2.getBitWidth())
> +      return isSameValue(I1, I2.extend(I1.getBitWidth()));
> +    else if (I2.getBitWidth() > I1.getBitWidth())
> +      return isSameValue(I1.extend(I2.getBitWidth()), I2);
> +
> +    // We have a signedness mismatch. Turn the signed value into an
> unsigned
> +    // value.
> +    if (I1.isSigned()) {
> +      if (I1.isNegative())
> +        return false;
> +
> +      return APSInt(I1, true) == I2;
> +    }
> +
> +    if (I2.isNegative())
> +      return false;
> +
> +    return I1 == APSInt(I2, true);
> +  }
> +
>    /// Profile - Used to insert APSInt objects, or objects that contain
> APSInt
>    ///  objects, into FoldingSets.
>    void Profile(FoldingSetNodeID& ID) const;
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20120714/c480f150/attachment.html>


More information about the llvm-commits mailing list