diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h index 934fe912f01..f1b039e16dc 100644 --- a/include/llvm/ADT/APInt.h +++ b/include/llvm/ADT/APInt.h @@ -1437,6 +1437,12 @@ public: /// as "bitPosition". void flipBit(unsigned bitPosition); + /// Negate this APInt in place. + void negate() { + flipAllBits(); + ++(*this); + } + /// Insert the bits from a smaller APInt starting at bitPosition. void insertBits(const APInt &SubBits, unsigned bitPosition); @@ -1996,8 +2002,7 @@ inline raw_ostream &operator<<(raw_ostream &OS, const APInt &I) { } inline APInt operator-(APInt v) { - v.flipAllBits(); - ++v; + v.negate(); return v; } diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index 227cfbd2e2e..d43140b3551 100644 --- a/lib/Support/APInt.cpp +++ b/lib/Support/APInt.cpp @@ -1846,10 +1846,8 @@ void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) { *this += digit; } // If its negative, put it in two's complement form - if (isNeg) { - --(*this); - this->flipAllBits(); - } + if (isNeg) + this->negate(); } void APInt::toString(SmallVectorImpl &Str, unsigned Radix, @@ -1927,8 +1925,7 @@ void APInt::toString(SmallVectorImpl &Str, unsigned Radix, // They want to print the signed version and it is a negative value // Flip the bits and add one to turn it into the equivalent positive // value and put a '-' in the result. - Tmp.flipAllBits(); - ++Tmp; + Tmp.negate(); Str.push_back('-'); }