Bug type: miscompilation
Component: InstCombine
Bug location: llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp: InstCombinerImpl::SimplifyDemandedUseBits
Issue title: wrong code at -O{s,2,3} on x86_64-linux-gnu
Labels: miscompilation
confirmed
llvm:instcombine
Reported by: zhendongsu

It appears to be a recent regression as it doesn't reproduce with 18.1.0 and earlier.

Compiler Explorer: https://godbolt.org/z/o595xsMdb

```
[512] % clangtk -v
clang version 19.0.0git (https://github.com/llvm/llvm-project.git 765e2f9a8de27cc8fd8c75540844e9630d8229ad)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /local/suz-local/software/local/clang-trunk/bin
Build config: +assertions
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Candidate multilib: .;@m64
Selected multilib: .;@m64
[513] % 
[513] % clangtk -O1 small.c; ./a.out
1
[514] % clangtk -O3 small.c; ./a.out
-2
1
[515] % cat small.c
int printf(const char *, ...);
int a, b, c, e;
char d;
int main() {
  int f = 1;
  unsigned g = 1;
  for (; c < 2; c++) {
    if (g)
      b = 1;
    char h = f;
    f = ~h;
    d = ~b - ~g * (a || f);
    g = ~g;
    if (g < 1)
      break;
    if (d)
      printf("%d\n", g);
    f = e;
  }
  return 0;
}
```

Comments: author: dtcxzyw
body: There is a bug in `SimplifyDemandedInstructionBits` :)
I am working to provide a minimal reproducer.

author: dtcxzyw
body: @nikic https://github.com/llvm/llvm-project/blob/a9183b8899f2c02ba3710b27c0cbdde6831a627b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp#L411-L416

I will post a fix later.

author: dtcxzyw
body: Reproducer: https://alive2.llvm.org/ce/z/GAUsBC
```
define i8 @src(i16 %a, i16 %b, i1 %cond) {
  %sel = select i1 %cond, i16 1, i16 254
  %cond1 = icmp ne i16 %b, 0
  %mask = and i16 %a, 255
  %cond2 = icmp ne i16 %mask, 255
  %cond3 = or i1 %cond1, %cond2
  %sel2 = select i1 %cond3, i16 %sel, i16 255
  %sub = sub i16 %sel2, %a
  %trunc = trunc i16 %sub to i8
  ret i8 %trunc
}

define i8 @tgt(i16 %a1, i16 %a2, i1 %cond) {
  %1 = trunc i16 %a1 to i8
  %conv6 = xor i8 %1, -1
  ret i8 %conv6
}
```

Fix patch:
commit 722151664ecfd53295392ad875ff9bfdcd1deaaf
Author: Yingwei Zheng <dtcxzyw2333@gmail.com>
Date:   Tue Jul 9 22:04:55 2024 +0800

    [InstCombine] Fix typo in `adjustKnownBitsForSelectArm` (#98155)
    
    Fixes https://github.com/llvm/llvm-project/issues/98139.

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 98f085a2c756..081e783c964f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -411,7 +411,7 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
     // Only known if known in both the LHS and RHS.
     adjustKnownBitsForSelectArm(LHSKnown, I->getOperand(0), I->getOperand(1),
                                 /*Invert=*/false, Depth, Q);
-    adjustKnownBitsForSelectArm(LHSKnown, I->getOperand(0), I->getOperand(2),
+    adjustKnownBitsForSelectArm(RHSKnown, I->getOperand(0), I->getOperand(2),
                                 /*Invert=*/true, Depth, Q);
     Known = LHSKnown.intersectWith(RHSKnown);
     break;


Regression tests:
file: llvm/test/Transforms/InstCombine/pr98139.ll
commands: opt -S -passes=instcombine < %s
tests: test_name: pr98139
test_body: define i8 @pr98139(i16 %a, i16 %b, i1 %cond) {
  %sel = select i1 %cond, i16 1, i16 254
  %cond1 = icmp ne i16 %b, 0
  %mask = and i16 %a, 255
  %cond2 = icmp ne i16 %mask, 255
  %cond3 = or i1 %cond1, %cond2
  %sel2 = select i1 %cond3, i16 %sel, i16 255
  %sub = sub i16 %sel2, %a
  %trunc = trunc i16 %sub to i8
  ret i8 %trunc
}