<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Constant bit manipulations are not always propagated resulting in missed optimizations"
   href="https://bugs.llvm.org/show_bug.cgi?id=50982">50982</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Constant bit manipulations are not always propagated resulting in missed optimizations
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Scalar Optimizations
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>vgatherps@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>It looks like the propagation of information from bit manipulations by
constants isn't properly propagated. Specifically, this appears to happen as a
consequence of optimizing/merging branches

Compiling the code


void a();
void b();
void c();


// This function cannot recognize that reaching the call to a()
// implies the call to c()
void test_intermediate_condition(int mask) {
    if (mask & 0b1) {
        a();
        mask |= 0b100;
    }
    if (mask & 0b10) {
        b();
    }
    if (mask & 0b100) {
        c();
    }
}

// This function recognizes that reaching the call to a()
// implies the call to c()
void test_no_intermediate_condition(int mask) {
    if (mask & 0b1) {
        a();
        mask |= 0b100;
    }
    b();
    if (mask & 0b100) {
        c();
    }
}

Results in the following code generated:
-----------------------------------
test_intermediate_change(int):          # @test_intermediate_change(int)
        pushq   %rbx
        movl    %edi, %ebx
        testb   $1, %bl
        jne     .LBB0_1
        testb   $2, %bl
        jne     .LBB0_3
.LBB0_4:
        testb   $4, %bl
        jne     .LBB0_6  # When this is reached from (LBB0_1),
                         # The tested bit is already set 
.LBB0_5:
        popq    %rbx
        retq
.LBB0_1:
        callq   a()
        orl     $4, %ebx
        testb   $2, %bl
        je      .LBB0_4 # This could jump to LBB0_6
                        # since LBB0_4 just checks the bit we set in
                        # ebx
.LBB0_3:
        callq   b()
        testb   $4, %bl
        je      .LBB0_5
.LBB0_6:
        popq    %rbx
        jmp     c()                           # TAILCALL

-----------

test_no_intermediate_change(int):       # @test_no_intermediate_change(int)
        pushq   %rbx
        movl    %edi, %ebx
        testb   $1, %bl
        jne     .LBB1_1
        callq   b()
        testb   $4, %bl
        jne     .LBB1_2
        popq    %rbx
        retq
.LBB1_1:
        callq   a()
        callq   b()
.LBB1_2:
        popq    %rbx
        jmp     c()                           # TAILCALL
-----------------

As noted in the generated assembly, the first function is not able to propagate
0b100 being set after the call to a() to the check of bit 0b100 in the call to
c().

The optimization is performed when an irrelevant check against the mask is
removed from the function, so llvm can certainly perform the optimization.

For what it's worth, I was able to produce code like:

or $4, %ebx
testq $4, %ebx
jne .....

in real life code, but was not able to massage something like this out of a
reduced example</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>