llvm/llvm-project

Improve bitfield arithmetic

Open

#33.874 aberto em 8 de set. de 2017

Ver no GitHub
 (12 comments) (0 reactions) (2 assignees)C++ (10.782 forks)batch import
bugzillaconfirmedgood first issuellvm:codegenllvm:instcombine

Métricas do repositório

Stars
 (26.378 stars)
Métricas de merge de PR
 (Mesclagem média 1d 2h) (1.000 fundiu PRs em 30d)

Description

Bugzilla Link 34526
Version trunk
OS Windows NT
CC @alexey-bataev,@legrosbuffle,@rotateright

Extended Description

We could improve math ops on irregular bitfields with the relevant SWAR style patterns:

struct Counters {
  unsigned a:7;
  unsigned b:16;
  unsigned c:9;
};

Counters IncCounters(Counters x) {
  x.a++;
  x.b++;
  x.c++;
  return x;
}

Counters AddCounters(Counters x, Counters y) {
  Counters c;
  c.a = x.a + y.a;
  c.b = x.b + y.b;
  c.c = x.c + y.c;
  return c;
}
define i32 @IncCounters(i32) {
  %2 = add i32 %0, 1
  %3 = and i32 %2, 127
  %4 = add i32 %0, 128
  %5 = and i32 %4, 8388480
  %6 = or i32 %5, %3
  %7 = add i32 %0, 8388608
  %8 = and i32 %7, -8388608
  %9 = or i32 %6, %8
  ret i32 %9
}

define i32 @AddCounters(i32, i32) {
  %3 = add i32 %1, %0
  %4 = and i32 %3, 127
  %5 = and i32 %0, 8388480
  %6 = add i32 %5, %1
  %7 = and i32 %6, 8388480
  %8 = or i32 %4, %7
  %9 = and i32 %0, -8388608
  %10 = add i32 %9, %1
  %11 = and i32 %10, -8388608
  %12 = or i32 %8, %11
  ret i32 %12
}

llc -mcpu=btver2

IncCounters(Counters): # @IncCounters(Counters)
  movl %edi, %ecx
  leal 1(%rdi), %eax
  addl $8388608, %edi # imm = 0x800000
  subl $-128, %ecx
  andl $127, %eax
  andl $-8388608, %edi # imm = 0xFF800000
  andl $8388480, %ecx # imm = 0x7FFF80
  orl %ecx, %eax
  orl %edi, %eax
  retq

AddCounters(Counters, Counters): # @AddCounters(Counters, Counters)
  movl %edi, %ecx
  leal (%rsi,%rdi), %eax
  andl $-8388608, %edi # imm = 0xFF800000
  andl $8388480, %ecx # imm = 0x7FFF80
  addl %esi, %edi
  andl $127, %eax
  addl %esi, %ecx
  andl $-8388608, %edi # imm = 0xFF800000
  andl $8388480, %ecx # imm = 0x7FFF80
  orl %ecx, %eax
  orl %edi, %eax
  retq

Guia do colaborador