https://gcc.gnu.org/bugzilla/show_bug.cgi?id=12277
Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |missed-optimization Priority|P2 |P3 Last reconfirmed|2006-02-26 19:21:22 |2017-1-11 CC| |msebor at gcc dot gnu.org Summary|Warn on dynamic casts with |eliminate and warn on |known NULL results. |dynamic casts with known | |NULL results Known to fail| |3.3, 4.5.3, 4.8.3, 4.9.3, | |5.3.0, 6.3.0, 7.0 --- Comment #10 from Martin Sebor <msebor at gcc dot gnu.org> --- Besides warning, GCC could also optimize the dynamic_cast away when it's known to fail. The GCC 7.0 dump below for a slightly modified version of the test case from comment #3 shows that it does neither. At the same time, since this is a rare case, I don't think it justifies P2. Lowering to P3. $ gcc$ cat t.C && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout t.C struct Base { virtual ~Base () {} }; class Derived : Base { friend int main (); }; int main () { Base *p = new Derived; if (dynamic_cast<Derived *>(p) != 0) __builtin_abort (); } ... ;; Function int main() (main, funcdef_no=4, decl_uid=2303, cgraph_uid=10, symbol_order=10) (executed once) int main() () { void * _1; void * _4; <bb 2> [100.00%]: _4 = operator new (8); MEM[(struct Derived *)_4].D.2316._vptr.Base = &MEM[(void *)&_ZTV7Derived + 16B]; _1 = __dynamic_cast (_4, &_ZTI4Base, &_ZTI7Derived, -2); if (_1 != 0B) goto <bb 3>; [0.04%] else goto <bb 4>; [99.96%] <bb 3> [0.04%]: __builtin_abort (); <bb 4> [99.96%]: return 0; }