[PATCH] D111272: [InlineCost] model calls to llvm.is.constant* more carefully

Nick Desaulniers via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 6 14:21:54 PDT 2021


nickdesaulniers created this revision.
Herald added subscribers: haicheng, hiraditya, eraman.
nickdesaulniers requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D111272

Files:
  llvm/lib/Analysis/InlineCost.cpp
  llvm/test/Transforms/Inline/call-intrinsic-is-constant.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111272.377684.patch
Type: text/x-patch
Size: 67354 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211006/d291b1c4/attachment.bin>


More information about the llvm-commits mailing list