[llvm] [IR] Implementation and lowering of bitinsert and bitextract (PR #200605)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 14 10:59:12 PDT 2026
================
@@ -11533,6 +11533,108 @@ The truth table used for the '`xor`' instruction is:
<result> = xor i32 %V, -1 ; yields i32:result = ~%V
```
+### Byte Operations
+
+Instructions for bit-range manipulation on {ref}`byte type <t_byte>` values.
+
+(i_bitextract)=
+
+#### '`bitextract`' Instruction
+
+##### Syntax:
+
+```
+<result> = bitextract <ty>, <bty> <source>, i32 <offset>
+```
+
+##### Overview:
+
+The '`bitextract`' instruction reads a contiguous range of bits from a
+{ref}`byte type <t_byte>` value and returns them as a value of type `ty`.
+
+##### Arguments:
+
+`<ty>` must be an {ref}`integer <t_integer>`, {ref}`floating-point
+<t_floating>`, {ref}`pointer <t_pointer>`, or {ref}`byte <t_byte>` type
+and specifies the result type. The first operand, `source`, must be a value
+of {ref}`byte type <t_byte>`. The `offset` operand is an `i32` giving the bit
+position at which the extraction begins within `source`.
+
+```{note}
+Vector types are not currently supported as the result type.
+```
+
+##### Semantics:
+
+The result is the bit range `source[offset : offset + bitwidth(ty))`,
+reinterpreted as a value of type `ty` as if by a {ref}`bitcast <i_bitcast>`.
+Bit `0` is the least significant bit of `source`.
+
+If `offset + bitwidth(ty)` is greater than `bitwidth(source)`,
+{ref}`poison value <poisonvalues>` is returned.
+
+##### Example:
+
+```text
+%result = bitextract i8, b32 %src, i32 24 ; Extract the 8 most-significant bits (bits [24..31]) of %src and return an 8-bit integer
+
+%result = bitextract i1, b32 %src, i32 5 ; Extract a single bit (bit 5) of %src and return it as an i1
+
+%result = bitextract i16, b64 %src, i32 16 ; Extract bits [16..31] of %src and return a 16-bit integer
+
+%result = bitextract float, b32 %src, i32 0 ; Extract bits [0..31] of %src and reinterpret them as a 32-bit float
+```
+
+(i_bitinsert)=
+
+#### '`bitinsert`' Instruction
+
+##### Syntax:
+```
+<result> = bitinsert <bty> <base>, <ty> <val>, i32 <offset>
+```
+##### Overview:
+
+The '`bitinsert`' instruction writes a contiguous range of bits from an
+integer, floating-point, pointer, or {ref}`byte type <t_byte>` value into a
+{ref}`byte type <t_byte>` value and returns the result as a value of the same
+byte type.
----------------
antoniofrighetto wrote:
```suggestion
The '`bitinsert`' instruction writes a contiguous range of bits from a
value of type `ty` into a {ref}`byte type <t_byte>` value and returns
the result as a value of the same byte type.
```
(Consistently with bitextract, Arguments section already spells out the allowed types.)
https://github.com/llvm/llvm-project/pull/200605
More information about the llvm-commits
mailing list