<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 - Optimize switch wirh icmp with and"
   href="https://bugs.llvm.org/show_bug.cgi?id=48602">48602</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Optimize switch wirh icmp with and
          </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>Linux
          </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>david.bolvansky@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>enum output_type
{
  type_pde,
  type_relocatable,
  type_pie,
  type_dll,
};

struct bfd_link_info
{
  enum output_type type : 2;
  unsigned int pad : 30;
};

#define bfd_link_pde(info)         ((info)->type == type_pde)
#define bfd_link_dll(info)         ((info)->type == type_dll)
#define bfd_link_relocatable(info) ((info)->type == type_relocatable)
#define bfd_link_pie(info)         ((info)->type == type_pie)
#define bfd_link_executable(info)  (bfd_link_pde (info) || bfd_link_pie (info))
#define bfd_link_pic(info)         (bfd_link_dll (info) || bfd_link_pie (info))

int result;

void test_pic (struct bfd_link_info *info)
{
  if (bfd_link_pic (info))
    result++;
}
void test_exe (struct bfd_link_info *info)
{
  if (bfd_link_executable (info))
    result++;
}



%struct.bfd_link_info = type { i32 }

@result = dso_local local_unnamed_addr global i32 0, align 4

define dso_local void @_Z8test_picP13bfd_link_info(%struct.bfd_link_info*
nocapture readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr %struct.bfd_link_info, %struct.bfd_link_info* %0, i64 0,
i32 0
  %3 = load i32, i32* %2, align 4
  %4 = and i32 %3, 2
  %5 = icmp eq i32 %4, 0
  br i1 %5, label %9, label %6

6:                                                ; preds = %1
  %7 = load i32, i32* @result, align 4, !tbaa !2
  %8 = add nsw i32 %7, 1
  store i32 %8, i32* @result, align 4, !tbaa !2
  br label %9

9:                                                ; preds = %1, %6
  ret void
}

define dso_local void @_Z8test_exeP13bfd_link_info(%struct.bfd_link_info*
nocapture readonly %0) local_unnamed_addr #0 {
  %2 = getelementptr %struct.bfd_link_info, %struct.bfd_link_info* %0, i64 0,
i32 0
  %3 = load i32, i32* %2, align 4
  %4 = and i32 %3, 3
  switch i32 %4, label %8 [
    i32 0, label %5
    i32 2, label %5
  ]

5:                                                ; preds = %1, %1
  %6 = load i32, i32* @result, align 4, !tbaa !2
  %7 = add nsw i32 %6, 1
  store i32 %7, i32* @result, align 4, !tbaa !2
  br label %8

8:                                                ; preds = %1, %5
  ret void
}


It should be possible to optimize switch away.


Current LLVM codegen:
test_exe(bfd_link_info*):            # @test_exe(bfd_link_info*)
        mov     eax, dword ptr [rdi]
        or      eax, 2
        and     eax, 3
        cmp     eax, 2
        jne     .LBB1_2
        add     dword ptr [rip + result], 1
.LBB1_2:
        ret


GCC:
test_exe(bfd_link_info*):
        test    BYTE PTR [rdi], 1
        jne     .L10
        add     DWORD PTR result[rip], 1
.L10:
        ret</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>