eliminating redundant pointer arithmethics

Is there an optimization pass that eliminates redundant pointer
arithmethics ? In the example below llvm is only able to optimize
away the subtraction in test1, but not in test2/3.

Nope, there isn't anything yet. This should be handled by the instruction combiner. I'll file an enhancement PR for this, and sometime when I have a couple of spare cycles I'll implement it (unless someone beats me to it :slight_smile:

Thanks for noticing this!

-Chris

#include <stddef.h>

int test1(int a, int b) {
   int d = b - a;
   a = b - d; // no-op
   return a;
}

char* test2(char* a, char* b) {
   ptrdiff_t d = b - a;
   a = b - d; // no-op on all llvm-supported machines
   return a;
}

int test3(char* a, char* b, char* c) {
   ptrdiff_t d = b - a;
   return (d == 8) || ((a = b - d) < c); // a = ... is a no-op
}

-Chris