I noticed that GCC tends to litter the assembly code with null checks and jumps (especially for dynamic casts) even when the pointer cannot be null. This is especially true for "this": unless I am mistaken, calling a nonstatic member with "this" not pointing to an object of the correct type (or a derived type) is undefined behavior. So GCC should assume it is not null. I tried the following testcase with 3.4.6, 4.0.3, and 4.1.0; GCC was never able to optimize the return value to "true" when compiling with -O3.
struct A { bool g(); }; bool A::g() { return this; } -- Summary: "this" pointer is not assumed to be not null Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: guillaume dot melquiond at ens-lyon dot fr http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27336