Skip to content

Instantly share code, notes, and snippets.

@RealNeGate
Last active August 9, 2023 09:34
Show Gist options
  • Save RealNeGate/4071b78df236803738836e3dd4aec088 to your computer and use it in GitHub Desktop.
Save RealNeGate/4071b78df236803738836e3dd4aec088 to your computer and use it in GitHub Desktop.

during IPO (interprocedural optimizations) you wanna have some cases around specializing a function without the early exits, they should be hoisted outside of the function and inlining decisions should be made once that's done.

void foo(int* ptr) {
  if (ptr != NULL) {
    ...
  }
}

// use site
foo(abc);

Gets converted into...

// specializations have private linkage
static void foo$1(void* ptr) {
  // notify the optimizer of the already checked assumption.
  if (ptr == NULL) unreachable();

  ...
}

// use site
if (abc) {
  foo$1(abc);
}

this is basically always good or at least it's predictably good enough to do where you should always do it given a small enough set of early outs. reason being there's never less information known about the condition/parameter outside at the call site (99% of the time there's more), also it's helpful because we can specialize out large unused cases.

analysis of early outs is all heuristics games but the case where it's a direct path to a return should always count, then using a similar "instruction count estimator" to the inliner you wanna consider the case where it's close to direct return (usually this will be things like error reporting).

that's all for today folks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment