[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:09:40 PST 2024


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

    (and X, (C<<12|0xfff)) -> (ANDN X, ~C<<12)
    (or  X, (C<<12|0xfff)) -> (ORN  X, ~C<<12)
    (xor X, (C<<12|0xfff)) -> (XNOR X, ~C<<12)

Saves an `ADDI HI, -1` instruction.

>From f13007fdb523bcf5bbe357a12dec96fabb19727a Mon Sep 17 00:00:00 2001
From: Piotr Fusik <p.fusik at samsung.com>
Date: Tue, 17 Dec 2024 13:07:20 +0100
Subject: [PATCH] [RISCV] Select and/or/xor with some constants to Zbb
 ANDN/ORN/XNOR

    (and X, (C<<12|0xfff)) -> (ANDN X, ~C<<12)
    (or  X, (C<<12|0xfff)) -> (ORN  X, ~C<<12)
    (xor X, (C<<12|0xfff)) -> (XNOR X, ~C<<12)

Saves an `ADDI HI, -1` instruction.
---
 llvm/lib/Target/RISCV/RISCVInstrInfoZb.td | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
index a78091cd02a35f..ae53a1361700af 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td
@@ -481,6 +481,25 @@ def : Pat<(XLenVT (or  GPR:$rs1, (not GPR:$rs2))), (ORN  GPR:$rs1, GPR:$rs2)>;
 def : Pat<(XLenVT (xor GPR:$rs1, (not GPR:$rs2))), (XNOR GPR:$rs1, GPR:$rs2)>;
 } // Predicates = [HasStdExtZbbOrZbkb]
 
+// A 32-bit signed immediate with all 12 low bits set, but not -1
+def simm32fff : ImmLeaf<XLenVT, [{
+  return isInt<32>(Imm) && (Imm & 0xfff) == 0xfff && Imm != -1;}]>;
+
+def NotImm : SDNodeXForm<imm, [{
+  return CurDAG->getTargetConstant(~N->getSExtValue(), SDLoc(N),
+                                   N->getValueType(0));
+}]>;
+
+class PatGprSimm32fff<SDPatternOperator OpNode, RVInst Inst>
+    : Pat<(XLenVT (OpNode (vt GPR:$rs1), simm32fff:$imm)),
+          (Inst GPR:$rs1, (NotImm simm32fff:$imm))>;
+
+let Predicates = [HasStdExtZbbOrZbkb] in {
+// TODO: and, except for (srli (slli X, C), C)
+def : PatGprSimm32fff<or, ORN>;
+//def : PatGprSimm32fff<xor, XNOR>;
+} // Predicates = [HasStdExtZbbOrZbkb]
+
 let Predicates = [HasStdExtZbbOrZbkb] in {
 def : PatGprGpr<shiftop<rotl>, ROL>;
 def : PatGprGpr<shiftop<rotr>, ROR>;



More information about the llvm-commits mailing list