2017-08-09 00:29:35 +08:00
|
|
|
//===-- KnownBits.cpp - Stores known zeros/ones ---------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-08-09 00:29:35 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a class for representing known zeros and ones used by
|
|
|
|
// computeKnownBits.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/KnownBits.h"
|
2020-03-03 01:24:11 +08:00
|
|
|
#include <cassert>
|
2017-08-09 00:29:35 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2019-04-13 02:18:08 +08:00
|
|
|
static KnownBits computeForAddCarry(
|
|
|
|
const KnownBits &LHS, const KnownBits &RHS,
|
|
|
|
bool CarryZero, bool CarryOne) {
|
|
|
|
assert(!(CarryZero && CarryOne) &&
|
|
|
|
"Carry can't be zero and one at the same time");
|
2017-08-09 00:29:35 +08:00
|
|
|
|
2019-12-03 22:45:47 +08:00
|
|
|
APInt PossibleSumZero = LHS.getMaxValue() + RHS.getMaxValue() + !CarryZero;
|
|
|
|
APInt PossibleSumOne = LHS.getMinValue() + RHS.getMinValue() + CarryOne;
|
2017-08-09 00:29:35 +08:00
|
|
|
|
|
|
|
// Compute known bits of the carry.
|
|
|
|
APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
|
|
|
|
APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
|
|
|
|
|
|
|
|
// Compute set of known bits (where all three relevant bits are known).
|
|
|
|
APInt LHSKnownUnion = LHS.Zero | LHS.One;
|
|
|
|
APInt RHSKnownUnion = RHS.Zero | RHS.One;
|
|
|
|
APInt CarryKnownUnion = std::move(CarryKnownZero) | CarryKnownOne;
|
|
|
|
APInt Known = std::move(LHSKnownUnion) & RHSKnownUnion & CarryKnownUnion;
|
|
|
|
|
|
|
|
assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
|
|
|
|
"known bits of sum differ");
|
|
|
|
|
|
|
|
// Compute known bits of the result.
|
|
|
|
KnownBits KnownOut;
|
|
|
|
KnownOut.Zero = ~std::move(PossibleSumZero) & Known;
|
|
|
|
KnownOut.One = std::move(PossibleSumOne) & Known;
|
2019-04-13 02:18:08 +08:00
|
|
|
return KnownOut;
|
|
|
|
}
|
|
|
|
|
|
|
|
KnownBits KnownBits::computeForAddCarry(
|
|
|
|
const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) {
|
|
|
|
assert(Carry.getBitWidth() == 1 && "Carry must be 1-bit");
|
|
|
|
return ::computeForAddCarry(
|
|
|
|
LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
KnownBits KnownBits::computeForAddSub(bool Add, bool NSW,
|
|
|
|
const KnownBits &LHS, KnownBits RHS) {
|
|
|
|
KnownBits KnownOut;
|
|
|
|
if (Add) {
|
|
|
|
// Sum = LHS + RHS + 0
|
|
|
|
KnownOut = ::computeForAddCarry(
|
|
|
|
LHS, RHS, /*CarryZero*/true, /*CarryOne*/false);
|
|
|
|
} else {
|
|
|
|
// Sum = LHS + ~RHS + 1
|
|
|
|
std::swap(RHS.Zero, RHS.One);
|
|
|
|
KnownOut = ::computeForAddCarry(
|
|
|
|
LHS, RHS, /*CarryZero*/false, /*CarryOne*/true);
|
|
|
|
}
|
2017-08-09 00:29:35 +08:00
|
|
|
|
|
|
|
// Are we still trying to solve for the sign bit?
|
2019-04-13 02:18:08 +08:00
|
|
|
if (!KnownOut.isNegative() && !KnownOut.isNonNegative()) {
|
2017-08-09 00:29:35 +08:00
|
|
|
if (NSW) {
|
|
|
|
// Adding two non-negative numbers, or subtracting a negative number from
|
|
|
|
// a non-negative one, can't wrap into negative.
|
|
|
|
if (LHS.isNonNegative() && RHS.isNonNegative())
|
|
|
|
KnownOut.makeNonNegative();
|
|
|
|
// Adding two negative numbers, or subtracting a non-negative number from
|
|
|
|
// a negative one, can't wrap into non-negative.
|
|
|
|
else if (LHS.isNegative() && RHS.isNegative())
|
|
|
|
KnownOut.makeNegative();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return KnownOut;
|
|
|
|
}
|