Hi,
I was wondering if there is an optimization pass in LLVM or Polly that can optimize this given loop:
int main(int argc, char* argv) {
char s = 0;
// Loop is setting s = argv[1][0];
for (int i=0; i<argv[1][0]; i++) {
s++;
}
printf(“s = %c\n”, s);
return 0;
}
into:
int main(int argc, char* argv) {
// Optimized loop
char s = argv[1][0];
printf(“s = %c\n”, s);
return 0;
}
Is there an optimization that can do this?
And if not, maybe someone knows what hinders the optimizer?!
Thanks,
Peter
Without testing it I'd say loop idiom recognition (Transforms/Scalar/LoopIdiomRecognize.h) is the one that should kick in and optimize this.
Thanks a lot for your help.
Indeed if I compile with -OZ the loop gets replaced with something like this:
if ( *v3 < 0 )
v4 = 0;
Is there a reason why it does not get applied with O3 ?
I mean this a huge improvement to the loop.
I also noticed that it only works when I compile from C to IR but when I try to compile IR to IR or use opt to optimize BC it fails.
Any idea why ?
How do you invoke opt and could you please attach the input file.