<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 materialized twice due to phi node value"
   href="https://llvm.org/bugs/show_bug.cgi?id=25269">25269</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Constant materialized twice due to phi node value
          </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>All
          </td>
        </tr>

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

        <tr>
          <th>Keywords</th>
          <td>code-quality
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>nunoplopes@sapo.pt
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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</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>