llvm / llvm/llvm-project

[AggressiveInstCombine] Fold reversed consecutive byte loads using bswap

Open
#215,496 6 comments 0 reactions 1 assignee Claimed by @topperc View on GitHub
llvm:instcombine missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

`foldConsecutiveLoads()` can combine consecutive byte loads when their shift order matches the target endianness.

However, the reversed form is currently missed. For example, on a little-endian target:

```llvm
%a = load i8, ptr %p
%b = load i8, ptr %p1
%c = load i8, ptr %p2
%d = load i8, ptr %p3

; zext omitted
%a0 = shl i32 %a, 24
%a1 = shl i32 %b, 16
%a2 = shl i32 %c, 8

%r0 = or i32 %a0, %a1
%r1 = or i32 %r0, %a2
%r2 = or i32 %r1, %d
```

could be represented as:

```llvm
%wide = load i32, ptr %p
%result = call i32 @llvm.bswap.i32(i32 %wide)
```

When the load order is consecutive but the bit placement is reversed relative to the target endianness, `foldConsecutiveLoads()` could generate a wide load followed by `llvm.bswap`.

This is independently useful and also forms part of the optimization requested in #201107.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.