[llvm-bugs] [Bug 48602] New: Optimize switch wirh icmp with and

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Dec 26 01:37:24 PST 2020


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

            Bug ID: 48602
           Summary: Optimize switch wirh icmp with and
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: david.bolvansky at gmail.com
                CC: llvm-bugs at lists.llvm.org

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

-- 
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/20201226/39669e17/attachment.html>


More information about the llvm-bugs mailing list