[llvm-bugs] [Bug 35363] New: Incorrect order of evaluation for || somehow involving ! of _Bool

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Nov 20 06:28:02 PST 2017


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

            Bug ID: 35363
           Summary: Incorrect order of evaluation for || somehow involving
                    ! of _Bool
           Product: clang
           Version: 5.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangbugs at nondot.org
          Reporter: jseward at acm.org
                CC: llvm-bugs at lists.llvm.org

Incorrect order of evaluation for || somehow involving ! of _Bool

For the following input (in C)

  void ff(void);
  void gg(void);
  _Bool hh(int*);

  void test()
  {
    int a;
    _Bool q = hh(&a);
    if (!q || (a == 42))
      ff();
    else
      gg();
  }

"clang version 5.0.0 (tags/RELEASE_500/final) (llvm/tags/RELEASE_500/final
312553)" on x86_64-Linux, at -O and above, produces code that firsts tests
|a == 42| and only then |!q|:

  test:                                   # @test
          .cfi_startproc
  # BB#0:
          pushq   %rax
  .Lcfi0:
          .cfi_def_cfa_offset 16
          leaq    4(%rsp), %rdi
          callq   hh
          cmpl    $42, 4(%rsp)
          je      .LBB0_2
  # BB#1:
          testb   %al, %al
          je      .LBB0_2
  # BB#3:
          callq   gg
          popq    %rax
          retq
  .LBB0_2:
          callq   ff
          popq    %rax
          retq

I don't see how it can justify comparing |a==42| before looking at the
return value from hh.

I notice also that it produces the "expected" order of tests (testb, then
cmpl) for either of the following changes:

* using |unsigned char| or |int| or |long| instead of |_Bool|
* changing the first term from |!q| to simply |q|

It's as if the compiler believes that ! of _Bool is undefined behavior in
C, so it can do what it likes.  But I can't imagine why it would believe
that.

-- 
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/20171120/da2858b4/attachment.html>


More information about the llvm-bugs mailing list