[llvm-bugs] [Bug 25269] New: Constant materialized twice due to phi node value

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Oct 21 02:21:51 PDT 2015


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

            Bug ID: 25269
           Summary: Constant materialized twice due to phi node value
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Keywords: code-quality
          Severity: normal
          Priority: P
         Component: Common Code Generator Code
          Assignee: unassignedbugs at nondot.org
          Reporter: nunoplopes at sapo.pt
                CC: llvm-bugs at lists.llvm.org
    Classification: Unclassified

For the code below, the constant is being materialized twice on the same BB
because codegen combines two BBs where the constant appears in different PHI
nodes.  These could be combined into a single materialization.
This bug report is on code size only, since the problem occurs in the loop
prologue.


C code:

unsigned long f(const unsigned char *str) {
  unsigned long hash = 5381;
  while (int c = *str++)
    hash = (hash << 5u) + hash + c;
  return hash;
}



-O2 IR produced by clang (notice the 2 occurrences of the constant 5381):

define i64 @_Z1fPKh(i8* nocapture readonly %str) #0 {
  %1 = load i8, i8* %str, align 1, !tbaa !2
  %2 = icmp eq i8 %1, 0
  br i1 %2, label %._crit_edge, label %.thread.preheader

.thread.preheader:                                ; preds = %0
  br label %.thread

.thread:                                          ; preds = %.thread.preheader,
%.thread
  %3 = phi i8 [ %8, %.thread ], [ %1, %.thread.preheader ]
  %.013 = phi i8* [ %4, %.thread ], [ %str, %.thread.preheader ]
  %hash.02 = phi i64 [ %7, %.thread ], [ 5381, %.thread.preheader ]
  %4 = getelementptr inbounds i8, i8* %.013, i64 1
  %5 = mul i64 %hash.02, 33
  %6 = zext i8 %3 to i64
  %7 = add i64 %6, %5
  %8 = load i8, i8* %4, align 1, !tbaa !2
  %9 = icmp eq i8 %8, 0
  br i1 %9, label %._crit_edge.loopexit, label %.thread

._crit_edge.loopexit:                             ; preds = %.thread
  %.lcssa = phi i64 [ %7, %.thread ]
  br label %._crit_edge

._crit_edge:                                      ; preds =
%._crit_edge.loopexit, %0
  %hash.0.lcssa = phi i64 [ 5381, %0 ], [ %.lcssa, %._crit_edge.loopexit ]
  ret i64 %hash.0.lcssa
}

!2 = !{!3, !3, i64 0}
!3 = !{!"omnipotent char", !4, i64 0}
!4 = !{!"Simple C/C++ TBAA"}



x86-64 assembly:

        movb    (%rcx), %dl
        movl    $5381, %eax             # imm = 0x1505
        testb   %dl, %dl
        je      .LBB0_3
# BB#1:                                 # %.thread.preheader
        addq    $1, %rcx
        movl    $5381, %eax  # Redudant; %rax = 5381 already
        .align  16, 0x90
.LBB0_2:                                # %.thread
                                        # =>This Inner Loop Header: Depth=1
        imulq   $33, %rax, %r8
        movzbl  %dl, %eax
        addq    %r8, %rax
        movb    (%rcx), %dl
        addq    $1, %rcx
        testb   %dl, %dl
        jne     .LBB0_2
.LBB0_3:                                # %._crit_edge
        retq

-- 
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/20151021/0fab2ba4/attachment.html>


More information about the llvm-bugs mailing list