[all-commits] [llvm/llvm-project] 52a400: [InlineCost] model calls to llvm.is.constant* more...

Nick Desaulniers via All-commits all-commits at lists.llvm.org
Tue Jan 11 21:12:55 PST 2022


  Branch: refs/heads/release/13.x
  Home:   https://github.com/llvm/llvm-project
  Commit: 52a400d8e4c46876fae5d732c92df254a9ceae8f
      https://github.com/llvm/llvm-project/commit/52a400d8e4c46876fae5d732c92df254a9ceae8f
  Author: Nick Desaulniers <ndesaulniers at google.com>
  Date:   2022-01-11 (Tue, 11 Jan 2022)

  Changed paths:
    M llvm/lib/Analysis/InlineCost.cpp
    A llvm/test/Transforms/Inline/call-intrinsic-is-constant.ll

  Log Message:
  -----------
  [InlineCost] model calls to llvm.is.constant* more carefully

llvm.is.constant* intrinsics are evaluated to 0 or 1 integral values.

A common use case for llvm.is.constant comes from the higher level
__builtin_constant_p. A common usage pattern of __builtin_constant_p in
the Linux kernel is:

    void foo (int bar) {
      if (__builtin_constant_p(bar)) {
        // lots of code that will fold away to a constant.
      } else {
        // a little bit of code, usually a libcall.
      }
    }

A minor issue in InlineCost calculations is when `bar` is _not_ Constant
and still will not be after inlining, we don't discount the true branch
and the inline cost of `foo` ends up being the cost of both branches
together, rather than just the false branch.

This leads to code like the above where inlining will not help prove bar
Constant, but it still would be beneficial to inline foo, because the
"true" branch is irrelevant from a cost perspective.

For example, IPSCCP can sink a passed constant argument to foo:

    const int x = 42;
    void bar (void) { foo(x); }

This improves our inlining decisions, and fixes a few head scratching
cases were the disassembly shows a relatively small `foo` not inlined
into a lone caller.

We could further improve this modeling by tracking whether the argument
to llvm.is.constant* is a parameter of the function, and if inlining
would allow that parameter to become Constant. This idea is noted in a
FIXME comment.

Link: https://github.com/ClangBuiltLinux/linux/issues/1302

Reviewed By: kazu

Differential Revision: https://reviews.llvm.org/D111272

(cherry picked from commit 9697f93587f46300814f1c6c68af347441d6e05d)




More information about the All-commits mailing list