http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47673
Summary: Redundant NULL check
Product: gcc
Version: 4.6.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: tree-optimization
AssignedTo: unassig...@gcc.gnu.org
ReportedBy: i...@airs.com
Consider this C code:
struct s1 { int x; };
struct s2 { struct s1 *p1; };
extern void f1(struct s1 *);
static inline void inline_func(struct s1 *p1) { if (p1) f1 (p1); }
void global_function(struct s2 *p2) {
if (p2->p1 != 0 && p2->p1 != (struct s1 *) -1)
inline_func(p2->p1);
}
When I compile this with -O2 with current mainline on x86_64, I get this:
movq (%rdi), %rdi
leaq -1(%rdi), %rax
cmpq $-3, %rax
ja .L1
testq %rdi, %rdi
je .L1
jmp f1
If %rdi is 0, %rax will be -1, and the ja branch will be taken. Therefore, the
je following the testq %rdi,%rdi will never be taken. The test and branch
should be eliminated.