[all-commits] [llvm/llvm-project] 5d4a0d: [InstCombine] Teach takeLog2 about right shifts, t...

David Majnemer via All-commits all-commits at lists.llvm.org
Sun Oct 27 22:31:16 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 5d4a0d54b5269bad1410e6db957836fe98634069
      https://github.com/llvm/llvm-project/commit/5d4a0d54b5269bad1410e6db957836fe98634069
  Author: David Majnemer <david.majnemer at gmail.com>
  Date:   2024-10-28 (Mon, 28 Oct 2024)

  Changed paths:
    M llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
    M llvm/test/Transforms/InstCombine/div.ll
    M llvm/test/Transforms/InstCombine/shift.ll

  Log Message:
  -----------
  [InstCombine] Teach takeLog2 about right shifts, truncation and bitwise-and

We left some easy opportunities for further simplifications.

log2(trunc(x)) is simply trunc(log2(x)). This is safe if we know that
trunc is NUW because it means that the truncation didn't drop any bits.
It is also safe if the caller is OK with zero as a possible answer.

log2(x >>u y) is simply `log2(x) - y`.

log2(x & y) is a funny one. It comes up when doing something like:
```
unsigned int f(unsigned int x, unsigned int y) {
  unsigned char a = 1u << x;
  return y / a;
}
```

LLVM would canonicalize this to:
```
  %shl = shl nuw i32 1, %x
  %conv1 = and i32 %shl, 255
  %div = udiv i32 %y, %conv1
```

In cases like these, we can ignore the mask entirely.
This is equivalent to `y >> x`.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list