[llvm] [RISCV] Select and/or/xor with some constants to Zbb ANDN/ORN/XNOR (PR #120221)

Piotr Fusik via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 17 04:12:05 PST 2024


pfusik wrote:

I'd like to implement an optimization so that

```c
long orlow(long x) {
    return x | ((1L << 24) - 1);
}
```

that is now compiled into:

```
        lui     a1, 4096
        addiw   a1, a1, -1
        or      a0, a0, a1
        ret
```

gets instead compiled to:

```
        lui     a1, -4096
        orn     a0, a0, a1
        ret
```

and similarly for AND and XOR.

I'm trying to implement this with tablegen patterns, but I'm new to that technology and I got stuck with the following error:

```
[16/130 -j3] Building RISCVGenGlobalISel.inc...
FAILED: lib/Target/RISCV/RISCVGenGlobalISel.inc /home/p.fusik/upstream/llvm-project/build/lib/Target/RISCV/RISCVGenGlobalISel.inc
cd /home/p.fusik/upstream/llvm-project/build && /home/p.fusik/upstream/llvm-project/build/bin/llvm-tblgen -gen-global-isel -I /home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV -I/home/p.fusik/upstream/llvm-project/build/include -I/home/p.fusik/upstream/llvm-project/llvm/include -I /home/p.fusik/upstream/llvm-project/llvm/lib/Target /home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV/RISCVGISel.td --write-if-changed -o lib/Target/RISCV/RISCVGenGlobalISel.inc -d lib/Target/RISCV/RISCVGenGlobalISel.inc.d
anonymous_10632:        (or:{ *:[i32] m1:[i64] } (vt:{} GPR:{ *:[i16 i32] m1:[i16 i32 i64 f64] }:$rs1), (imm:{ *:[i32] m1:[i64] })<<P:Predicate_simm32fff>>:$imm)
Included from /home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV/RISCVGISel.td:16:
Included from /home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV/RISCV.td:36:
Included from /home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV/RISCVInstrInfo.td:2113:
/home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td:499:1: error: In anonymous_10632: vt node requires exactly 0 operands!
def : PatGprSimm32fff<or, ORN>;
^
anonymous_10632:        (or:{ *:[i32] m1:[i64] } (vt:{} GPR:{ *:[i16 i32] m1:[i16 i32 i64 f64] }:$rs1), (imm:{ *:[i32] m1:[i64] })<<P:Predicate_simm32fff>>:$imm)
anonymous_10632:        (ORN:{ *:[i32] m1:[i64] } GPR:{ *:[i16 i32] m1:[i16 i32 i64 f64] }:$rs1, (NotImm:{ *:[i16 i32] m1:[i16 i32 i64 f64] } (imm:{ *:[i32] m1:[i64] })<<P:Predicate_simm32fff>>:$imm))
Included from /home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV/RISCVGISel.td:16:
Included from /home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV/RISCV.td:36:
Included from /home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV/RISCVInstrInfo.td:2113:
/home/p.fusik/upstream/llvm-project/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td:499:1: error: In anonymous_10632: Could not infer all types in pattern result!
def : PatGprSimm32fff<or, ORN>;
^
```

Please let me know how can I fix it or if perhaps this approach is completely wrong.


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


More information about the llvm-commits mailing list