[llvm-branch-commits] [llvm] [DAG][AArch64] Handle truncated buildvectors to allow and(subvector(anyext)) fold. (PR #133915)

David Green via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Apr 2 04:21:04 PDT 2025


================
@@ -7166,7 +7166,8 @@ SDValue DAGCombiner::visitAND(SDNode *N) {
 
   // if (and x, c) is known to be zero, return 0
   unsigned BitWidth = VT.getScalarSizeInBits();
-  ConstantSDNode *N1C = isConstOrConstSplat(N1);
+  ConstantSDNode *N1C =
+      isConstOrConstSplat(N1, /*AllowUndef*/ false, /*AllowTrunc*/ true);
----------------
davemgreen wrote:

> AllowTrunc means the input constant is truncated. But we don't promise zero-extension, I think, so the high bits could be anything. Therefore any comparison that checks those high bits is broken. The check on 7491 is most suspect in this respect, but 7346/7353 is also slightly suspect.

Checking for a mask (either via isMask with a shorter ScalarWidth or by checking == 0xffff as in 7491) would check that the top bits are 0. (MatchBSwapHWordLow also only handles scalars). So they should be OK, and truncating the constant should just make it is more likely to match in cases where the top bits are non-zero.

It would be a good idea to try and protect against future uses not realizing that the constant had been truncated, so a optional<APInt> sounds like a good idea.

https://github.com/llvm/llvm-project/pull/133915


More information about the llvm-branch-commits mailing list