[llvm-bugs] [Bug 43010] New: __builtin_assume causes a call to a function marked as __attribute__((const)) or ((pure)) to be needlessly propagated

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Aug 15 11:31:22 PDT 2019


https://bugs.llvm.org/show_bug.cgi?id=43010

            Bug ID: 43010
           Summary: __builtin_assume causes a call to a function marked as
                    __attribute__((const)) or ((pure)) to be needlessly
                    propagated
           Product: libraries
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: mike.k at digitalcarbide.com
                CC: llvm-bugs at lists.llvm.org

In a situation when a call to an external __attribute__((const)) or
__attribute__((pure)) is encapsulated in a __builtin_assume, and then called,
the function is still called despite having no side-effects and the result
value being known.

extern int foo() __attribute__((const));
extern void oof();

void bar() {
    __builtin_assume(foo() == 1);

    if (foo() == 1) {
        oof();
    }
}

results in the following x86-64 assembly with -03:

bar(): # @bar()
  push rax
  call foo()
  pop rax
  jmp oof() # TAILCALL

The `call foo()`, as stated, is unnecessary.

If the code is refactored to use a temporary variable for the result, as such:

extern int foo() __attribute__((const));
extern void oof();

void bar() {
    int foo_result = foo();
    __builtin_assume(foo_result == 1);

    if (foo_result == 1) {
        oof();
    }
}

It then produces the expected output:

bar(): # @bar()
  jmp oof() # TAILCALL

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20190815/95f94255/attachment.html>


More information about the llvm-bugs mailing list