1 //===-- KnownBits.cpp - Stores known zeros/ones ---------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a class for representing known zeros and ones used by
10 // computeKnownBits.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/KnownBits.h"
15 #include <cassert>
16 
17 using namespace llvm;
18 
19 static KnownBits computeForAddCarry(
20     const KnownBits &LHS, const KnownBits &RHS,
21     bool CarryZero, bool CarryOne) {
22   assert(!(CarryZero && CarryOne) &&
23          "Carry can't be zero and one at the same time");
24 
25   APInt PossibleSumZero = LHS.getMaxValue() + RHS.getMaxValue() + !CarryZero;
26   APInt PossibleSumOne = LHS.getMinValue() + RHS.getMinValue() + CarryOne;
27 
28   // Compute known bits of the carry.
29   APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
30   APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
31 
32   // Compute set of known bits (where all three relevant bits are known).
33   APInt LHSKnownUnion = LHS.Zero | LHS.One;
34   APInt RHSKnownUnion = RHS.Zero | RHS.One;
35   APInt CarryKnownUnion = std::move(CarryKnownZero) | CarryKnownOne;
36   APInt Known = std::move(LHSKnownUnion) & RHSKnownUnion & CarryKnownUnion;
37 
38   assert((PossibleSumZero & Known) == (PossibleSumOne & Known) &&
39          "known bits of sum differ");
40 
41   // Compute known bits of the result.
42   KnownBits KnownOut;
43   KnownOut.Zero = ~std::move(PossibleSumZero) & Known;
44   KnownOut.One = std::move(PossibleSumOne) & Known;
45   return KnownOut;
46 }
47 
48 KnownBits KnownBits::computeForAddCarry(
49     const KnownBits &LHS, const KnownBits &RHS, const KnownBits &Carry) {
50   assert(Carry.getBitWidth() == 1 && "Carry must be 1-bit");
51   return ::computeForAddCarry(
52       LHS, RHS, Carry.Zero.getBoolValue(), Carry.One.getBoolValue());
53 }
54 
55 KnownBits KnownBits::computeForAddSub(bool Add, bool NSW,
56                                       const KnownBits &LHS, KnownBits RHS) {
57   KnownBits KnownOut;
58   if (Add) {
59     // Sum = LHS + RHS + 0
60     KnownOut = ::computeForAddCarry(
61         LHS, RHS, /*CarryZero*/true, /*CarryOne*/false);
62   } else {
63     // Sum = LHS + ~RHS + 1
64     std::swap(RHS.Zero, RHS.One);
65     KnownOut = ::computeForAddCarry(
66         LHS, RHS, /*CarryZero*/false, /*CarryOne*/true);
67   }
68 
69   // Are we still trying to solve for the sign bit?
70   if (!KnownOut.isNegative() && !KnownOut.isNonNegative()) {
71     if (NSW) {
72       // Adding two non-negative numbers, or subtracting a negative number from
73       // a non-negative one, can't wrap into negative.
74       if (LHS.isNonNegative() && RHS.isNonNegative())
75         KnownOut.makeNonNegative();
76       // Adding two negative numbers, or subtracting a non-negative number from
77       // a negative one, can't wrap into non-negative.
78       else if (LHS.isNegative() && RHS.isNegative())
79         KnownOut.makeNegative();
80     }
81   }
82 
83   return KnownOut;
84 }
85 
86 KnownBits KnownBits::makeGE(const APInt &Val) const {
87   // Count the number of leading bit positions where our underlying value is
88   // known to be less than or equal to Val.
89   unsigned N = (Zero | Val).countLeadingOnes();
90 
91   // For each of those bit positions, if Val has a 1 in that bit then our
92   // underlying value must also have a 1.
93   APInt MaskedVal(Val);
94   MaskedVal.clearLowBits(getBitWidth() - N);
95   return KnownBits(Zero, One | MaskedVal);
96 }
97 
98 KnownBits KnownBits::umax(const KnownBits &LHS, const KnownBits &RHS) {
99   // If we can prove that LHS >= RHS then use LHS as the result. Likewise for
100   // RHS. Ideally our caller would already have spotted these cases and
101   // optimized away the umax operation, but we handle them here for
102   // completeness.
103   if (LHS.getMinValue().uge(RHS.getMaxValue()))
104     return LHS;
105   if (RHS.getMinValue().uge(LHS.getMaxValue()))
106     return RHS;
107 
108   // If the result of the umax is LHS then it must be greater than or equal to
109   // the minimum possible value of RHS. Likewise for RHS. Any known bits that
110   // are common to these two values are also known in the result.
111   KnownBits L = LHS.makeGE(RHS.getMinValue());
112   KnownBits R = RHS.makeGE(LHS.getMinValue());
113   return KnownBits(L.Zero & R.Zero, L.One & R.One);
114 }
115 
116 KnownBits KnownBits::umin(const KnownBits &LHS, const KnownBits &RHS) {
117   // Flip the range of values: [0, 0xFFFFFFFF] <-> [0xFFFFFFFF, 0]
118   auto Flip = [](const KnownBits &Val) { return KnownBits(Val.One, Val.Zero); };
119   return Flip(umax(Flip(LHS), Flip(RHS)));
120 }
121 
122 KnownBits KnownBits::smax(const KnownBits &LHS, const KnownBits &RHS) {
123   // Flip the range of values: [-0x80000000, 0x7FFFFFFF] <-> [0, 0xFFFFFFFF]
124   auto Flip = [](const KnownBits &Val) {
125     unsigned SignBitPosition = Val.getBitWidth() - 1;
126     APInt Zero = Val.Zero;
127     APInt One = Val.One;
128     Zero.setBitVal(SignBitPosition, Val.One[SignBitPosition]);
129     One.setBitVal(SignBitPosition, Val.Zero[SignBitPosition]);
130     return KnownBits(Zero, One);
131   };
132   return Flip(umax(Flip(LHS), Flip(RHS)));
133 }
134 
135 KnownBits KnownBits::smin(const KnownBits &LHS, const KnownBits &RHS) {
136   // Flip the range of values: [-0x80000000, 0x7FFFFFFF] <-> [0xFFFFFFFF, 0]
137   auto Flip = [](const KnownBits &Val) {
138     unsigned SignBitPosition = Val.getBitWidth() - 1;
139     APInt Zero = Val.One;
140     APInt One = Val.Zero;
141     Zero.setBitVal(SignBitPosition, Val.Zero[SignBitPosition]);
142     One.setBitVal(SignBitPosition, Val.One[SignBitPosition]);
143     return KnownBits(Zero, One);
144   };
145   return Flip(umax(Flip(LHS), Flip(RHS)));
146 }
147 
148 KnownBits KnownBits::abs() const {
149   // If the source's MSB is zero then we know the rest of the bits already.
150   if (isNonNegative())
151     return *this;
152 
153   // Assume we know nothing.
154   KnownBits KnownAbs(getBitWidth());
155 
156   // We only know that the absolute values's MSB will be zero iff there is
157   // a set bit that isn't the sign bit (otherwise it could be INT_MIN).
158   APInt Val = One;
159   Val.clearSignBit();
160   if (!Val.isNullValue())
161     KnownAbs.Zero.setSignBit();
162 
163   return KnownAbs;
164 }
165 
166 KnownBits &KnownBits::operator&=(const KnownBits &RHS) {
167   // Result bit is 0 if either operand bit is 0.
168   Zero |= RHS.Zero;
169   // Result bit is 1 if both operand bits are 1.
170   One &= RHS.One;
171   return *this;
172 }
173 
174 KnownBits &KnownBits::operator|=(const KnownBits &RHS) {
175   // Result bit is 0 if both operand bits are 0.
176   Zero &= RHS.Zero;
177   // Result bit is 1 if either operand bit is 1.
178   One |= RHS.One;
179   return *this;
180 }
181 
182 KnownBits &KnownBits::operator^=(const KnownBits &RHS) {
183   // Result bit is 0 if both operand bits are 0 or both are 1.
184   APInt Z = (Zero & RHS.Zero) | (One & RHS.One);
185   // Result bit is 1 if one operand bit is 0 and the other is 1.
186   One = (Zero & RHS.One) | (One & RHS.Zero);
187   Zero = std::move(Z);
188   return *this;
189 }
190