Appears as: Multiple instances in dispatcher
IL Pattern:
w0 = and(wN, MASK) // Extract bits
result = or(wM, w0) // Combine with another register
Equivalent Arithmetic:
a XOR b = (a | b) - (a & b)
a OR b = (a | b) // When semantics match
Frequency: 3 blocks (1, 5, 7)
Found at: 0x9b60eb9c, 0x9b60ed28, 0x9b60ee6c
Appears as: Dispatch field extraction
IL Pattern:
w0 = asr(wN, SHIFT) // Extract field via shift
w0 = and(w0, MASK) // Mask to field width
result = or(w0, CONSTANT) // OR with flag/constant
Equivalent Arithmetic:
extract_field(x, shift, mask) = (x >> shift) & mask
extract_field_or(x, s, m, c) = ((x >> s) & m) | c
Frequency: 3 blocks (3, 4, 6) + duplicates
Found at: 0x9b60eca8, 0x9b60ece8, 0x9b60ee2c
Common Operands:
- Shift by 12 (0xc): Extract bits[12:18]
- Shift by 6 (0x6): Extract bits[6:12]
- Mask 0x3f: 6-bit field width
- OR constant: 0x80 (dispatch flag)
Appears as: Heavy-duty field building
IL Pattern (Simplified):
t1 = and(a, b)
t1 = shl(t1, SHIFT1)
t2 = and(c, d)
t3 = or(t1, t2)
t4 = add(t3, OFFSET)
result = or(asr(t4, SHIFT2), CONSTANT)
Equivalent Arithmetic:
result = ((((a & b) << s1) | (c & d) + offset) >> s2) | const
Frequency: 1 block (2)
Found at: 0x9b60ec58
Critical Path: 6-7 instructions
Optimization Benefit: HIGHEST
Appears as: Loop reduction pattern
IL Pattern:
accumulator = or(accumulator, new_value)
counter = add(counter, 1)
counter = and(counter, MASK) // Wraparound
Equivalent Arithmetic:
acc |= val // Accumulate via OR
cnt = (cnt + 1) & 0xffff // 16-bit wraparound
Frequency: 1 block (8)
Found at: 0x9b613680
Optimization Benefit: Macro-op fusion potential
Look for IL lines with these operations in sequence:
and(...) // AND operation
or(...) // OR operation
asr(...) // Arithmetic shift right (often paired with AND)
shl(...) // Shift left (for field composition)
sub(...), add(...) // Arithmetic ops paired with bitwise
- 2 ops = Type A (AND+OR)
- 3 ops = Type B (SHIFT+AND+OR)
- 4+ ops = Type C (Complex composition)
- Loop with OR = Type D (Accumulation)
These are suspicious and indicate bit field operations:
Masks:
0x3f (6-bit field)
0xff (8-bit field)
0xffff (16-bit field)
0xffffff (24-bit field)
Shifts:
6 bits (common in dispatch tables)
12 bits (field offsets)
18-20 bits (multi-field packing)
Constants:
0x80 (bit 7 set - flag)
0xf0 (nibble operations)
0x10000 (field offset)
Not all AND+OR = XOR. Check if:
- The operation computes
(a | b) & ~(a & b)→ REAL XOR - The operation computes
(a | b)with different sources → OR decomposition - The operation extracts and combines fields → Field composition (optimize via pattern fusion)
Current Cost: 2-3 cycles Optimized: 1 cycle (or 2 if immediate)
BEFORE:
w0 = and(w26, 0x3f)
w2 = or(w25, w0)
AFTER (Proposed):
w2 = or(w25, and(w26, 0x3f)) // Single operation with fold
OR
w2 = extractfield_or(w26, 0, 6, w25) // Specialized instruction
Implementation: Recognize pattern in IL optimizer, emit fused instruction
Current Cost: 3 cycles Optimized: 1-2 cycles (specialized instruction)
BEFORE:
w0 = asr(w26, 0x6)
w0 = and(w0, 0x3f)
w2 = or(w0, 0x80)
AFTER (Proposed):
w2 = extractfield_or_const(w26, 6, 0x3f, 0x80) // Single instruction
OR (if no specialized instruction):
w0 = and(asr(w26, 6), 0x3f)
w2 = or(w0, 0x80) // 2 ops instead of 3
Implementation: Create ARM64 optimization pattern to fuse operations
Current Cost: 6-7 cycles in dependency chain Optimized: 3-4 cycles (pattern recognition + micro-op fusion)
BEFORE:
w1 = and(w23, w26)
w1 = shl(w1, 0xa)
w0 = and(w21, w0)
w0 = or(shl(and(w23, w26), 0xa), w0)
w26 = add(w0, 0x10000)
w0 = asr(w26, 0x12)
w2 = or(w0, 0xf0)
AFTER (Proposed):
// Recognize as field_compose(w23, w26, 0xa, w21, w0, 0x10000, 0x12, 0xf0)
w2 = field_compose_and_shift_or(
src1_a=w23, src1_b=w26, shift1=0xa,
src2_a=w21, src2_b=w0,
add_offset=0x10000,
shift2=0x12,
or_const=0xf0
)
Implementation: Complex pattern, consider as micro-kernel optimization
Current Cost: 1 cycle (ORR) Optimized: 1 cycle (ORR, but with macro-op fusion for counter)
BEFORE:
orr w22, w22, w0 // 1 cycle
add w23, w23, #1 // 1 cycle (dep on w23)
...
add w17, w17, #1 // 1 cycle (counter)
and w17, w17, #0xffff // 1 cycle (dep on add)
cbnz w17, loop // 1 cycle (dep on and)
AFTER (Proposed):
orr w22, w22, w0 // 1 cycle
add w23, w23, #1 // Macro-fused with w22 ORR
...
add_and_check w17, 0xffff // Combined add+mask+compare (2 micro-ops)
cbnz w17, loop
Implementation: Compiler-level macro-op fusion
jq -r 'select(.block_il | any(test("and\\(|or\\(")) | length > 0)
| "\(.start): \(.block_il | join(" ; "))"' \
jit_exec_alias_*.il.jsonl | head -50jq -r 'select(.block_il | any(test("asr|shl")) | length > 0)
| select(.block_il | any(test("and\\(|or\\(")) | length > 0)
| "\(.start): SHIFT+AND+OR"' \
jit_exec_alias_*.il.jsonljq -r 'select(.block_il | any(test("\\|")) and .jitted_block_asm | any(test("bnz|cbnz")))
| "\(.start): LOOP_WITH_OR"' \
jit_exec_alias_*.il.jsonl| Pattern | Blocks | Ops | Current | Optimized | Saving | Impact |
|---|---|---|---|---|---|---|
| Type A | 3 | 2 | 2 cyc | 1 cyc | 1 | LOW |
| Type B | 3 | 3 | 3 cyc | 1-2 cyc | 1-2 | MEDIUM |
| Type C | 1 | 7 | 6 cyc | 3-4 cyc | 3 | HIGH |
| Type D | 1 | 4 | 4 cyc | 2-3 cyc* | 1-2 | LOW |
| TOTAL | 8 | - | ~17 | ~9-11 | 6-8 | 35-47% |
*With macro-op fusion support
XOR_DECOMPOSITION_ANALYSIS.md— Detailed analysis with full IL sequencesXOR_DECOMPOSITION_PATTERNS.json— Structured pattern data for toolingPATTERN_RECOGNITION_GUIDE.md— This file (detection & optimization)
- Implement Pattern Recognizer in IL optimizer
- Generate Specialized Instructions for Type B & C patterns
- Evaluate Performance Impact via benchmark runs
- Profile Frequency of each pattern type in real workloads
- Consider Code Consolidation (3 duplicate Type A blocks at 1, 5, 7)