1 //===-- KnownBits.cpp - Stores known zeros/ones ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a class for representing known zeros and ones used by
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/KnownBits.h"
19 KnownBits
KnownBits::computeForAddSub(bool Add
, bool NSW
,
20 const KnownBits
&LHS
, KnownBits RHS
) {
21 // Carry in a 1 for a subtract, rather than 0.
24 // Sum = LHS + ~RHS + 1
25 std::swap(RHS
.Zero
, RHS
.One
);
29 APInt PossibleSumZero
= ~LHS
.Zero
+ ~RHS
.Zero
+ CarryIn
;
30 APInt PossibleSumOne
= LHS
.One
+ RHS
.One
+ CarryIn
;
32 // Compute known bits of the carry.
33 APInt CarryKnownZero
= ~(PossibleSumZero
^ LHS
.Zero
^ RHS
.Zero
);
34 APInt CarryKnownOne
= PossibleSumOne
^ LHS
.One
^ RHS
.One
;
36 // Compute set of known bits (where all three relevant bits are known).
37 APInt LHSKnownUnion
= LHS
.Zero
| LHS
.One
;
38 APInt RHSKnownUnion
= RHS
.Zero
| RHS
.One
;
39 APInt CarryKnownUnion
= std::move(CarryKnownZero
) | CarryKnownOne
;
40 APInt Known
= std::move(LHSKnownUnion
) & RHSKnownUnion
& CarryKnownUnion
;
42 assert((PossibleSumZero
& Known
) == (PossibleSumOne
& Known
) &&
43 "known bits of sum differ");
45 // Compute known bits of the result.
47 KnownOut
.Zero
= ~std::move(PossibleSumZero
) & Known
;
48 KnownOut
.One
= std::move(PossibleSumOne
) & Known
;
50 // Are we still trying to solve for the sign bit?
51 if (!Known
.isSignBitSet()) {
53 // Adding two non-negative numbers, or subtracting a negative number from
54 // a non-negative one, can't wrap into negative.
55 if (LHS
.isNonNegative() && RHS
.isNonNegative())
56 KnownOut
.makeNonNegative();
57 // Adding two negative numbers, or subtracting a non-negative number from
58 // a negative one, can't wrap into non-negative.
59 else if (LHS
.isNegative() && RHS
.isNegative())
60 KnownOut
.makeNegative();