Issue 244808
Summary [DAGCombiner] Wrong code at -O2: `and(BUILD_VECTOR(x, undef), <3, 0>)` is folded to poison, so a `<3 x i64>` select loses its mask
Labels new issue
Assignees
Reporter haoxintu
    ## Description

```c
typedef long v3 __attribute__((ext_vector_type(3)));

__attribute__((noinline)) void f(long *g, long *h)
{
  v3 a;
  for (int i = 0; i < 3; i++)
    a[i] = g[i];
 v3 r = __builtin_elementwise_fshr(
      a.yyy ? 3 : a.xxx,
 __builtin_elementwise_clzg(a & (v3){3}, (v3){7, 0, 7}) == 0 ? (v3){7, 7} : a,
      a ^ 1);
  for (int i = 0; i < 3; i++)
    h[i] = r[i];
}

int main(void)
{
  long g[3] = {0, 1, 2}, h[3];
  f(g, h);
  /* lane 1: a.y = 1 (sign bit clear) -> first operand 0; clzg(1 & 0, 0) = 0 -> second operand 7;
     shift 1 ^ 1 = 0 -> fshr(0, 7, 0) = 7 */
  if (h[1] != 7)
 __builtin_abort();
  return 0;
}
```

```
$ clang -O2 repro.c && ./a.out      # Aborted (also -O3); -O0, -O1, -Os are fine
```

The -O2 IR is correct. The bug is in the X86 backend: `llc` gets it wrong at both -O0 and -O2. IR reproducer (`repro.ll`):

```llvm
target triple = "x86_64-unknown-linux-gnu"

; lane 1 of the result must be fshr(3, 7, 1 ^ 1) = 7
define i64 @f(i64 %x, ptr %p, ptr %h) noinline {
  %ins = insertelement <3 x i64> poison, i64 %x, i64 0
  %l = load <2 x i64>, ptr %p, align 8
  %w = shufflevector <2 x i64> %l, <2 x i64> poison, <3 x i32> <i32 0, i32 1, i32 poison>
  %v = shufflevector <3 x i64> %ins, <3 x i64> %w, <3 x i32> <i32 0, i32 3, i32 4>
  %and = and <3 x i64> %v, <i64 3, i64 0, i64 0>
  %iszero = icmp eq <3 x i64> %and, zeroinitializer
  %c = and <3 x i1> %iszero, <i1 false, i1 true, i1 false>
  %y = select <3 x i1> %c, <3 x i64> <i64 7, i64 7, i64 0>, <3 x i64> splat (i64 1)
  %s = xor <3 x i64> %v, splat (i64 1)
  %r = call <3 x i64> @llvm.fshr.v3i64(<3 x i64> splat (i64 3), <3 x i64> %y, <3 x i64> %s)
  %r2 = shufflevector <3 x i64> %r, <3 x i64> poison, <2 x i32> <i32 0, i32 1>
  store <2 x i64> %r2, ptr %h, align 8
  %e2 = extractelement <3 x i64> %r, i64 2
  ret i64 %e2
}

@G = global [2 x i64] [i64 1, i64 2]
@H = global [2 x i64] zeroinitializer

define i32 @main() {
  %u = call i64 @f(i64 0, ptr @G, ptr @H)
  %p = getelementptr i8, ptr @H, i64 8
  %e = load i64, ptr %p
 %ok = icmp eq i64 %e, 7
  br i1 %ok, label %y, label %n
n:
  call void @abort()
  unreachable
y:
  ret i32 0
}
declare void @abort()
```

```
$ llc -O0 -relocation-model=pic repro.ll -o r.s && clang r.s && ./a.out   # Aborted (trunk); llc 21.1.8: exit 0
```

## Analysis

Lane 1 of `%and` is `%l[0] & 0 = 0`, so `%c` lane 1 is always true and `%y` lane 1 is always 7.

`-debug-_only_=isel,dagcombine` shows:

1. Because the `and` mask is 0 in lane 1, the shuffle `%v` is dropped from the `and`. It becomes `and (BUILD_VECTOR %x, undef), <3, 0>`, lowered as `and (scalar_to_vector %x), (vzext_movl <3>)`. Lane 1 should still be `undef & 0 = 0`.
2. In the post-legalize combine only lane 1 of that `and` is demanded (by the `pshufd` that splats the i64 compare result). The node is then replaced by poison:
   ```
   Replacing.2 t244: v2i64 = and t278, t306
   With: t333: v2i64 = poison
   ```
   SCALAR_TO_VECTOR's upper lanes count as poison (#217185), so `poison & 0` is taken as poison.
3. `pcmpeq undef` then folds to undef, the select mask to 0, and `or(and(M, 7), andn(M, 1))` to splat 1. The emitted code never contains the 7.

So a defined lane (`x & 0`) becomes poison. The rewrite in step 1 replaces an operand lane with undef because the other `and` operand is 0 there. That is only sound if undef-or-poison `& 0` folds to 0, which no longer holds once the lane is treated as poison.

Sanitizers (ASan, UBSan, MSan) are clean.

Reproduced in Godbolt (x86-64 clang assertions trunk, -O2): https://godbolt.org/z/Y98djjPP5

## Checked Versions

- Wrong at -O2/-O3 with clang trunk 2012344e5da8 (2026-11-19); -O0/-O1/-Os are correct.
- `repro.ll` is wrong with trunk `llc` at -O0 and -O2, and correct with llc 21.1.8, so this is a regression. clang 21 does not have `__builtin_elementwise_fshr`.

Thanks.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to