llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.98k stars 11.94k forks source link

[AggressiveInstCombine] Missed optimization: a reversed chain of contiguous unsigned icmps could be merged #91569

Closed FLZ101 closed 1 month ago

FLZ101 commented 6 months ago

Assuming the machine is little-endian, a reversed chain of contiguous unsigned icmps could be merged. For example,

struct Foo {
  unsigned short n;
  unsigned char o;
  unsigned char p;
};

int compare(struct Foo *f1, struct Foo *f2) {
 if (f1->p > f2->p)
   return 1;
 if (f1->p < f2->p)
   return -1;

  if (f1->o > f2->o)
    return 1;
  if (f1->o < f2->o)
    return -1;

  if (f1->n > f2->n)
    return 1;
  if (f1->n < f2->n)
    return -1;

  return 0;
}

the function compare() could be transformed as below,

int compare(struct Foo *f1, struct Foo *f2) {
  unsigned int a = *(unsigned int *)&(f1->n);
  unsigned int b = *(unsigned int *)&(f2->n);
  return a > b ? 1 : a < b ?  -1 : 0;
}

This kind of pattern is found in velvet (a de novo genome assembler), more specificly the hot function compareKmers().

wangbyby commented 5 months ago

Seems too high level.