https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107666
Bug ID: 107666 Summary: IPA: Speculatively dereferencing function pointers Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: ipa Assignee: unassigned at gcc dot gnu.org Reporter: christophm30 at gmail dot com CC: marxin at gcc dot gnu.org Target Milestone: --- This is a feature request, not a bug report. GCC already does speculative call graph optimizations (ipa-devirt does so when enabled via -fdevirtualize-speculatively). The idea is to identify virtual functions that will most likely always have the same target, which cannot be (easily) proven, and convert them into direct calls. A major benefit is that direct calls can be optimized by ipa-cp. The idea is applicable to function pointers as well: identify indirect calls, which most likely will call the same target, and convert them into guarded direct calls. Given the existing infrastructure to add speculative edges, we only need to focus on "strongly believe". One approach would be to do the following: * Analysis 1: Collection of function pointer type assignments (e.g. p->cb = mycb ==> field "cb" of record points to function "mycb") * Analysis 2: Collection of indirect calls (e.g. p->cb() ==> field "cb" of record is used in a the analysed function) * Propagation: Iterate over indirect calls and lookup possible function pointer type assignments; If one is available, then convert it into a speculative (guarded) direct call. * Transformation: Use existing infrastructure to emit guarded direct calls (gimple_ic). Similar to `-devirtualize-speculatively`, such an optimization can be gated using a flag (e.g. `-fipa-guarded-deref`). One example where this optimization would trigger is x264 (also part of CPU2017), where the function pointer `get_ref` is assigned a single time during startup, and then called multiple times with constant arguments (8 or 16). Dereferencing these function pointers would allow further optimization if propagated by ipa-cp.