1 //===- llvm/Support/KnownBits.h - Stores known zeros/ones -------*- C++ -*-===//
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 #ifndef LLVM_SUPPORT_KNOWNBITS_H
16 #define LLVM_SUPPORT_KNOWNBITS_H
18 #include "llvm/ADT/APInt.h"
22 // Struct for tracking the known zeros and ones of a value.
28 // Internal constructor for creating a ConstantRange from two APInts.
29 KnownBits(APInt Zero, APInt One)
30 : Zero(std::move(Zero)), One(std::move(One)) {}
33 // Default construct Zero and One.
36 /// Create a known bits object of BitWidth bits initialized to unknown.
37 KnownBits(unsigned BitWidth) : Zero(BitWidth, 0), One(BitWidth, 0) {}
39 /// Get the bit width of this value.
40 unsigned getBitWidth() const {
41 assert(Zero.getBitWidth() == One.getBitWidth() &&
42 "Zero and One should have the same width!");
43 return Zero.getBitWidth();
46 /// Returns true if there is conflicting information.
47 bool hasConflict() const { return Zero.intersects(One); }
49 /// Returns true if we know the value of all bits.
50 bool isConstant() const {
51 assert(!hasConflict() && "KnownBits conflict!");
52 return Zero.countPopulation() + One.countPopulation() == getBitWidth();
55 /// Returns the value when all bits have a known value. This just returns One
56 /// with a protective assertion.
57 const APInt &getConstant() const {
58 assert(isConstant() && "Can only get value when all bits are known");
62 /// Returns true if we don't know any bits.
63 bool isUnknown() const { return Zero.isNullValue() && One.isNullValue(); }
65 /// Resets the known state of all bits.
71 /// Returns true if value is all zero.
73 assert(!hasConflict() && "KnownBits conflict!");
74 return Zero.isAllOnesValue();
77 /// Returns true if value is all one bits.
78 bool isAllOnes() const {
79 assert(!hasConflict() && "KnownBits conflict!");
80 return One.isAllOnesValue();
83 /// Make all bits known to be zero and discard any previous information.
89 /// Make all bits known to be one and discard any previous information.
95 /// Returns true if this value is known to be negative.
96 bool isNegative() const { return One.isSignBitSet(); }
98 /// Returns true if this value is known to be non-negative.
99 bool isNonNegative() const { return Zero.isSignBitSet(); }
101 /// Make this value negative.
102 void makeNegative() {
103 assert(!isNonNegative() && "Can't make a non-negative value negative");
107 /// Make this value negative.
108 void makeNonNegative() {
109 assert(!isNegative() && "Can't make a negative value non-negative");
113 /// Truncate the underlying known Zero and One bits. This is equivalent
114 /// to truncating the value we're tracking.
115 KnownBits trunc(unsigned BitWidth) {
116 return KnownBits(Zero.trunc(BitWidth), One.trunc(BitWidth));
119 /// Zero extends the underlying known Zero and One bits. This is equivalent
120 /// to zero extending the value we're tracking.
121 KnownBits zext(unsigned BitWidth) {
122 return KnownBits(Zero.zext(BitWidth), One.zext(BitWidth));
125 /// Sign extends the underlying known Zero and One bits. This is equivalent
126 /// to sign extending the value we're tracking.
127 KnownBits sext(unsigned BitWidth) {
128 return KnownBits(Zero.sext(BitWidth), One.sext(BitWidth));
131 /// Zero extends or truncates the underlying known Zero and One bits. This is
132 /// equivalent to zero extending or truncating the value we're tracking.
133 KnownBits zextOrTrunc(unsigned BitWidth) {
134 return KnownBits(Zero.zextOrTrunc(BitWidth), One.zextOrTrunc(BitWidth));
137 /// Returns the minimum number of trailing zero bits.
138 unsigned countMinTrailingZeros() const {
139 return Zero.countTrailingOnes();
142 /// Returns the minimum number of trailing one bits.
143 unsigned countMinTrailingOnes() const {
144 return One.countTrailingOnes();
147 /// Returns the minimum number of leading zero bits.
148 unsigned countMinLeadingZeros() const {
149 return Zero.countLeadingOnes();
152 /// Returns the minimum number of leading one bits.
153 unsigned countMinLeadingOnes() const {
154 return One.countLeadingOnes();
157 /// Returns the number of times the sign bit is replicated into the other
159 unsigned countMinSignBits() const {
161 return countMinLeadingZeros();
163 return countMinLeadingOnes();
167 /// Returns the maximum number of trailing zero bits possible.
168 unsigned countMaxTrailingZeros() const {
169 return One.countTrailingZeros();
172 /// Returns the maximum number of trailing one bits possible.
173 unsigned countMaxTrailingOnes() const {
174 return Zero.countTrailingZeros();
177 /// Returns the maximum number of leading zero bits possible.
178 unsigned countMaxLeadingZeros() const {
179 return One.countLeadingZeros();
182 /// Returns the maximum number of leading one bits possible.
183 unsigned countMaxLeadingOnes() const {
184 return Zero.countLeadingZeros();
187 /// Returns the number of bits known to be one.
188 unsigned countMinPopulation() const {
189 return One.countPopulation();
192 /// Returns the maximum number of bits that could be one.
193 unsigned countMaxPopulation() const {
194 return getBitWidth() - Zero.countPopulation();
198 } // end namespace llvm