[llvm] [ValueTracking] Support horizontal vector add in computeKnownBits (PR #174410)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sun Jan 11 03:47:52 PST 2026
================
@@ -601,6 +601,46 @@ KnownBits KnownBits::abs(bool IntMinIsPoison) const {
return KnownAbs;
}
+KnownBits KnownBits::reduceAdd(unsigned NumElts) const {
+ if (NumElts == 0)
+ return KnownBits(getBitWidth());
+
+ unsigned BitWidth = getBitWidth();
+ KnownBits Result(BitWidth);
+
+ if (isConstant())
+ // If all elements are the same constant, we can simply compute it
+ return KnownBits::makeConstant(NumElts * getConstant());
+
+ // The main idea is as follows.
+ //
+ // If KnownBits for each element has L leading zeros then
+ // X_i < 2^(W - L) for every i from [1, N].
+ //
+ // ADD X_i <= ADD max(X_i) = N * max(X_i)
+ // < N * 2^(W - L)
+ // < 2^(W - L + ceil(log2(N)))
+ //
+ // As the result, we can conclude that
+ //
+ // L' = L - ceil(log2(N)) = L - bit_width(N - 1)
+ //
+ // Similar logic can be applied to leading ones.
+ unsigned LostBits = NumElts > 1 ? llvm::bit_width(NumElts - 1) : 0;
+
+ if (isNonNegative()) {
----------------
dtcxzyw wrote:
Why not use `KnownBits::mul` directly? Actually I don't think we need a new helper function.
https://github.com/llvm/llvm-project/pull/174410
More information about the llvm-commits
mailing list