Description
Bugzilla Link | 35102 |
Version | trunk |
OS | Linux |
Attachments | cpp code that removes for loop away |
CC | @majnemer,@vns-mn,@aqjune,@nunoplopes,@RalfJung,@regehr,@sanjoy,@rotateright |
Extended Description
Compiling this code with -O3 generates following assembly code:
-- unreachable.cpp --
#include <stdint.h>
void unreachable();
static void compare_to_all(uintptr_t i) {
uintptr_t cur = 0;
while(true) {
if ((int*)i == (int*)cur) return;
if (cur == UINTPTR_MAX) break;
++cur;
};
unreachable();
}
void test() {
int i = 1;
compare_to_all((uintptr_t)&i);
}
--
-- unreachable.s --
test(): # @test()
jmp unreachable() # TAILCALL
--
InstCombiner::foldAllocaCmp folds (int*)i == (int*)cur
into false after checking that there exists only one icmp instruction which uses alloca i.
If the purpose of this optimization was to fold single pointer equality comparison into false, syntactically checking the number of icmp isn't enough. It should also check whether the icmp is not in a loop. In following code (test2.c), this optimization folds the pointer comparison in foo(), but not in gee().
-- test2.c --
void foo(int* p) {
int i;
for (int j = 0; j < 2; j++) {
if (&i == p+j) {
printf("ok\n");
}
}
}
void gee(int* p) {
int i;
if (&i == p+0) {
printf("ok\n");
}
if (&i == p+1) {
printf("ok\n");
}
}
unreachable.cpp written by Ralf Jung, test2.c written by Gil Hur