llvm / llvm/llvm-project

missed optimization: ((x ^ mask) >> n) ^ mask to arithmetic shift

Open
#204,851 3 comments 0 reactions 1 assignee View on GitHub

@GabrielKimm is already working on this.

Since Jul 25, 2026.

llvm:instcombine missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/P3dhcarEd
https://alive2.llvm.org/ce/z/_sSp6K

uint32_t asr(uint32_t x, int n) {
    return ((x ^ -(x >> 31)) >> n) ^ -(x >> 31);
}
define i32 @src(i32 %1, i32 %2) {
  %4 = ashr i32 %1, 31
  %5 = xor i32 %1, %4
  %6 = lshr i32 %5, %2
  %7 = xor i32 %6, %4
  ret i32 %7
}

Expected:

define i32 @tgt(i32 %1, i32 %2) {
  %4 = ashr i32 %1, %2
  ret i32 %4
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.