[llvm] [X86] Optimize (a & 0xff) | ((b & 0xff) << 8) into high-byte register insertions (PR #214645)

via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 6 23:07:23 PDT 2026


https://github.com/AZero13 updated https://github.com/llvm/llvm-project/pull/214645

>From eb4a27eed1b0a16043273371fa28374ad86dfa9d Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Fri, 7 Aug 2026 01:24:04 -0400
Subject: [PATCH 1/2] Pre-commit test (NFC)

---
 llvm/test/CodeGen/X86/h-register-insert.ll | 73 ++++++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/h-register-insert.ll

diff --git a/llvm/test/CodeGen/X86/h-register-insert.ll b/llvm/test/CodeGen/X86/h-register-insert.ll
new file mode 100644
index 0000000000000..77c1d8d6ffa24
--- /dev/null
+++ b/llvm/test/CodeGen/X86/h-register-insert.ll
@@ -0,0 +1,73 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s -mtriple=i686-unknown-unknown | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown | FileCheck %s --check-prefix=X64
+
+define i32 @test_insert_32(i8 inreg %A, i8 inreg %B) {
+; X86-LABEL: test_insert_32:
+; X86:       # %bb.0:
+; X86-NEXT:    movzbl %al, %ecx
+; X86-NEXT:    movzbl %dl, %eax
+; X86-NEXT:    shll $8, %eax
+; X86-NEXT:    orl %ecx, %eax
+; X86-NEXT:    retl
+;
+; X64-LABEL: test_insert_32:
+; X64:       # %bb.0:
+; X64-NEXT:    movzbl %dil, %ecx
+; X64-NEXT:    movzbl %sil, %eax
+; X64-NEXT:    shll $8, %eax
+; X64-NEXT:    orl %ecx, %eax
+; X64-NEXT:    retq
+  %conv = zext i8 %A to i32
+  %conv1 = zext i8 %B to i32
+  %shl = shl nuw nsw i32 %conv1, 8
+  %or = or disjoint i32 %shl, %conv
+  ret i32 %or
+}
+
+define i16 @test_insert_16(i8 inreg %A, i8 inreg %B) {
+; X86-LABEL: test_insert_16:
+; X86:       # %bb.0:
+; X86-NEXT:    movzbl %al, %eax
+; X86-NEXT:    shll $8, %edx
+; X86-NEXT:    orl %edx, %eax
+; X86-NEXT:    # kill: def $ax killed $ax killed $eax
+; X86-NEXT:    retl
+;
+; X64-LABEL: test_insert_16:
+; X64:       # %bb.0:
+; X64-NEXT:    movzbl %dil, %eax
+; X64-NEXT:    shll $8, %esi
+; X64-NEXT:    orl %esi, %eax
+; X64-NEXT:    # kill: def $ax killed $ax killed $eax
+; X64-NEXT:    retq
+  %conv = zext i8 %A to i16
+  %conv1 = zext i8 %B to i16
+  %shl = shl nuw nsw i16 %conv1, 8
+  %or = or disjoint i16 %shl, %conv
+  ret i16 %or
+}
+
+define i64 @test_insert_64(i8 inreg %A, i8 inreg %B) {
+; X86-LABEL: test_insert_64:
+; X86:       # %bb.0:
+; X86-NEXT:    movzbl %al, %ecx
+; X86-NEXT:    movzbl %dl, %eax
+; X86-NEXT:    shll $8, %eax
+; X86-NEXT:    orl %ecx, %eax
+; X86-NEXT:    xorl %edx, %edx
+; X86-NEXT:    retl
+;
+; X64-LABEL: test_insert_64:
+; X64:       # %bb.0:
+; X64-NEXT:    movzbl %dil, %ecx
+; X64-NEXT:    movzbl %sil, %eax
+; X64-NEXT:    shll $8, %eax
+; X64-NEXT:    orl %ecx, %eax
+; X64-NEXT:    retq
+  %conv = zext i8 %A to i64
+  %conv1 = zext i8 %B to i64
+  %shl = shl nuw nsw i64 %conv1, 8
+  %or = or disjoint i64 %shl, %conv
+  ret i64 %or
+}

>From e8cdd02f30da2c27ebe2c9c4a52c12eb4139335e Mon Sep 17 00:00:00 2001
From: AZero13 <gfunni234 at gmail.com>
Date: Fri, 7 Aug 2026 02:05:48 -0400
Subject: [PATCH 2/2] [X86] Optimize (a & 0xff) | ((b & 0xff) << 8) into
 high-byte register insertions

This had to be done in dagtodag because tablegen would not work.
---
 llvm/lib/Target/X86/X86ISelDAGToDAG.cpp    | 95 ++++++++++++++++++++++
 llvm/test/CodeGen/X86/h-register-insert.ll | 26 +++---
 2 files changed, 105 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
index d078117061677..1276110399552 100644
--- a/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
+++ b/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
@@ -599,6 +599,7 @@ namespace {
                         uint8_t Imm);
     bool tryVPTESTM(SDNode *Root, SDValue Setcc, SDValue Mask);
     bool tryMatchBitSelect(SDNode *N);
+    bool tryMatchHRegisterInsert(SDNode *N);
 
     MachineSDNode *emitPCMPISTR(unsigned ROpc, unsigned MOpc, bool MayFoldLoad,
                                 const SDLoc &dl, MVT VT, SDNode *Node);
@@ -5403,6 +5404,98 @@ bool X86DAGToDAGISel::tryMatchBitSelect(SDNode *N) {
                         Ternlog.getNode(), A, B, C, 0xCA);
 }
 
+bool X86DAGToDAGISel::tryMatchHRegisterInsert(SDNode *N) {
+  if (N->getOpcode() != ISD::OR)
+    return false;
+  if (N->getValueType(0) != MVT::i32 && N->getValueType(0) != MVT::i16 &&
+      N->getValueType(0) != MVT::i64)
+    return false;
+
+  SDValue LHS = N->getOperand(0);
+  SDValue RHS = N->getOperand(1);
+
+  auto isAnd255 = [](SDValue V, SDValue &Base) {
+    if (V.getOpcode() != ISD::AND)
+      return false;
+    auto *C = dyn_cast<ConstantSDNode>(V.getOperand(1));
+    if (!C || C->getZExtValue() != 255)
+      return false;
+    Base = V.getOperand(0);
+    return true;
+  };
+  auto isShl8And255 = [&](SDValue V, SDValue &Base) {
+    if (V.getOpcode() != ISD::SHL)
+      return false;
+    auto *C = dyn_cast<ConstantSDNode>(V.getOperand(1));
+    if (!C || C->getZExtValue() != 8)
+      return false;
+    return isAnd255(V.getOperand(0), Base);
+  };
+
+  SDValue LoBase, HiBase;
+  if (isAnd255(LHS, LoBase) && isShl8And255(RHS, HiBase)) {
+  } else if (isAnd255(RHS, LoBase) && isShl8And255(LHS, HiBase)) {
+  } else {
+    return false;
+  }
+
+  SDLoc dl(N);
+  SDValue Sub8 = CurDAG->getTargetConstant(X86::sub_8bit, dl, MVT::i32);
+
+  SDValue LoExtr = SDValue(CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
+                                                  dl, MVT::i8, LoBase, Sub8),
+                           0);
+  SDValue MovzxLo =
+      SDValue(CurDAG->getMachineNode(X86::MOVZX32rr8, dl, MVT::i32, LoExtr), 0);
+  SDValue RC =
+      CurDAG->getTargetConstant(X86::GR32_ABCDRegClassID, dl, MVT::i32);
+  MovzxLo = SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
+                                           MVT::i32, MovzxLo, RC),
+                    0);
+
+  SDValue HiExtr1 = SDValue(CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
+                                                   dl, MVT::i8, HiBase, Sub8),
+                            0);
+
+  SDValue HiExtr2;
+  if (Subtarget->is64Bit()) {
+    SDValue MovzxHi = SDValue(
+        CurDAG->getMachineNode(X86::MOVZX32rr8, dl, MVT::i32, HiExtr1), 0);
+    MovzxHi = SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
+                                             MVT::i32, MovzxHi, RC),
+                      0);
+    HiExtr2 = SDValue(CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
+                                             MVT::i8, MovzxHi, Sub8),
+                      0);
+  } else {
+    HiExtr2 = HiExtr1;
+  }
+  SDValue RC8 =
+      CurDAG->getTargetConstant(X86::GR8_NOREXRegClassID, dl, MVT::i32);
+  HiExtr2 = SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
+                                           MVT::i8, HiExtr2, RC8),
+                    0);
+
+  SDValue Sub8Hi = CurDAG->getTargetConstant(X86::sub_8bit_hi, dl, MVT::i32);
+
+  SDNode *Insert = CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
+                                          MVT::i32, MovzxLo, HiExtr2, Sub8Hi);
+
+  if (N->getValueType(0) == MVT::i16) {
+    SDValue Sub16 = CurDAG->getTargetConstant(X86::sub_16bit, dl, MVT::i32);
+    Insert = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl, MVT::i16,
+                                    SDValue(Insert, 0), Sub16);
+  } else if (N->getValueType(0) == MVT::i64) {
+    SDValue Sub32 = CurDAG->getTargetConstant(X86::sub_32bit, dl, MVT::i32);
+    Insert = CurDAG->getMachineNode(TargetOpcode::SUBREG_TO_REG, dl, MVT::i64,
+                                    CurDAG->getTargetConstant(0, dl, MVT::i64),
+                                    SDValue(Insert, 0), Sub32);
+  }
+
+  ReplaceNode(N, Insert);
+  return true;
+}
+
 void X86DAGToDAGISel::Select(SDNode *Node) {
   MVT NVT = Node->getSimpleValueType(0);
   unsigned Opcode = Node->getOpcode();
@@ -5715,6 +5808,8 @@ void X86DAGToDAGISel::Select(SDNode *Node) {
       return;
     if (Opcode == ISD::OR && tryMatchBitSelect(Node))
       return;
+    if (Opcode == ISD::OR && tryMatchHRegisterInsert(Node))
+      return;
     if (tryVPTERNLOG(Node))
       return;
 
diff --git a/llvm/test/CodeGen/X86/h-register-insert.ll b/llvm/test/CodeGen/X86/h-register-insert.ll
index 77c1d8d6ffa24..39c7622683db6 100644
--- a/llvm/test/CodeGen/X86/h-register-insert.ll
+++ b/llvm/test/CodeGen/X86/h-register-insert.ll
@@ -5,18 +5,15 @@
 define i32 @test_insert_32(i8 inreg %A, i8 inreg %B) {
 ; X86-LABEL: test_insert_32:
 ; X86:       # %bb.0:
-; X86-NEXT:    movzbl %al, %ecx
-; X86-NEXT:    movzbl %dl, %eax
-; X86-NEXT:    shll $8, %eax
-; X86-NEXT:    orl %ecx, %eax
+; X86-NEXT:    movzbl %al, %eax
+; X86-NEXT:    movb %dl, %ah
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: test_insert_32:
 ; X64:       # %bb.0:
-; X64-NEXT:    movzbl %dil, %ecx
-; X64-NEXT:    movzbl %sil, %eax
-; X64-NEXT:    shll $8, %eax
-; X64-NEXT:    orl %ecx, %eax
+; X64-NEXT:    movzbl %dil, %eax
+; X64-NEXT:    movzbl %sil, %ecx
+; X64-NEXT:    movb %cl, %ah
 ; X64-NEXT:    retq
   %conv = zext i8 %A to i32
   %conv1 = zext i8 %B to i32
@@ -51,19 +48,16 @@ define i16 @test_insert_16(i8 inreg %A, i8 inreg %B) {
 define i64 @test_insert_64(i8 inreg %A, i8 inreg %B) {
 ; X86-LABEL: test_insert_64:
 ; X86:       # %bb.0:
-; X86-NEXT:    movzbl %al, %ecx
-; X86-NEXT:    movzbl %dl, %eax
-; X86-NEXT:    shll $8, %eax
-; X86-NEXT:    orl %ecx, %eax
+; X86-NEXT:    movzbl %al, %eax
+; X86-NEXT:    movb %dl, %ah
 ; X86-NEXT:    xorl %edx, %edx
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: test_insert_64:
 ; X64:       # %bb.0:
-; X64-NEXT:    movzbl %dil, %ecx
-; X64-NEXT:    movzbl %sil, %eax
-; X64-NEXT:    shll $8, %eax
-; X64-NEXT:    orl %ecx, %eax
+; X64-NEXT:    movzbl %dil, %eax
+; X64-NEXT:    movzbl %sil, %ecx
+; X64-NEXT:    movb %cl, %ah
 ; X64-NEXT:    retq
   %conv = zext i8 %A to i64
   %conv1 = zext i8 %B to i64



More information about the llvm-commits mailing list