/// GetIntegerValue - Convert this numeric literal value to an APInt that /// matches Val's input width. If there is an overflow, set Val to the low bits /// of the result and return true. Otherwise, return false. bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { // Fast path: Compute a conservative bound on the maximum number of // bits per digit in this radix. If we can't possibly overflow a // uint64 based on that bound then do the simple conversion to // integer. This avoids the expensive overflow checking below, and // handles the common cases that matter (small decimal integers and // hex/octal values which don't overflow). const unsigned NumDigits = SuffixBegin - DigitsBegin; if (alwaysFitsInto64Bits(radix, NumDigits)) { uint64_t N = 0; for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr) if (!isDigitSeparator(*Ptr)) N = N * radix + llvm::hexDigitValue(*Ptr); // This will truncate the value to Val's input width. Simply check // for overflow by comparing. Val = N; return Val.getZExtValue() != N; } Val = 0; const char *Ptr = DigitsBegin; llvm::APInt RadixVal(Val.getBitWidth(), radix); llvm::APInt CharVal(Val.getBitWidth(), 0); llvm::APInt OldVal = Val; bool OverflowOccurred = false; while (Ptr < SuffixBegin) { if (isDigitSeparator(*Ptr)) { ++Ptr; continue; } unsigned C = llvm::hexDigitValue(*Ptr++); // If this letter is out of bound for this radix, reject it. assert(C < radix && "NumericLiteralParser ctor should have rejected this"); CharVal = C; // Add the digit to the value in the appropriate radix. If adding in digits // made the value smaller, then this overflowed. OldVal = Val; // Multiply by radix, did overflow occur on the multiply? Val *= RadixVal; OverflowOccurred |= Val.udiv(RadixVal) != OldVal; // Add value, did overflow occur on the value? // (a + b) ult b <=> overflow Val += CharVal; OverflowOccurred |= Val.ult(CharVal); } return OverflowOccurred; }
ClusteredBitVector ClusteredBitVector::fromAPInt(const llvm::APInt &bits) { // This is not a very efficient algorithm. ClusteredBitVector result; for (unsigned i = 0, e = bits.getBitWidth(); i != e; ++i) { if (bits[i]) { result.appendSetBits(1); } else { result.appendClearBits(1); } } return result; }
/// \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 llvm::APInt &I1, const llvm::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; }
//===----------------------------------------------------------------------===// // Primary Expressions. //===----------------------------------------------------------------------===// void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) { if (hasAllocation()) C.Deallocate(pVal); BitWidth = Val.getBitWidth(); unsigned NumWords = Val.getNumWords(); const uint64_t* Words = Val.getRawData(); if (NumWords > 1) { pVal = new (C) uint64_t[NumWords]; std::copy(Words, Words + NumWords, pVal); } else if (NumWords == 1) VAL = Words[0]; else VAL = 0; }
std::string Inst::getKnownBitsString(llvm::APInt Zero, llvm::APInt One) { std::string Str; for (int K=Zero.getBitWidth()-1; K>=0; --K) { if (Zero[K] && One[K]) llvm_unreachable("KnownZero and KnownOnes bit can't be set to 1 together"); if (Zero[K]) { Str.append("0"); } else { if (One[K]) Str.append("1"); else Str.append("x"); } } return Str; }
bool CompareEquals(llvm::APInt a,llvm::APInt b) { if (a.getBitWidth()!=b.getBitWidth()) { if (a.getBitWidth()<b.getBitWidth()) a=a.zext(b.getBitWidth()); else b=b.zext(a.getBitWidth()); } return a==b; }