<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 - SCCP generates wrong code for ssa_copy"
   href="https://bugs.llvm.org/show_bug.cgi?id=39814">39814</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>SCCP generates wrong code for ssa_copy
          </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>carrot@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Compile following code with 

opt test.ll -S -ipsccp

define void @foo(i64*, i64) {
entry:
  %address = getelementptr inbounds i64, i64* %0, i64 2
  %cond3 = icmp eq i64 %1, 10
  br i1 %cond3, label %loop_header, label %exit

loop_header:
  %v23 = phi i64 [ %ivar, %loop_last ], [ 0, %entry ]
  %v25 = getelementptr inbounds i64, i64* %0, i64 %v23
  %v26 = load i64, i64* %v25, align 4
  %v27 = and i64 %v26, 1879048192
  %cond2 = icmp eq i64 %v27, 1610612736
  br i1 %cond2, label %loop_2, label %exit

loop_2:
  %ivar = add nuw nsw i64 %v23, 1
  %cond1 = icmp eq i64 %v26, %ivar
  br i1 %cond1, label %dont_delete, label %exit

dont_delete:
  br label %loop_last

loop_last:
  %loop_cond = icmp ult i64 %ivar, 10
  br i1 %loop_cond, label %loop_header, label %loop_exit

loop_exit:
  store i64 %v27, i64* %address, align 8
  br label %exit

exit:
  ret void
}

I got:

define void @foo(i64*, i64) {
entry:
  %address = getelementptr inbounds i64, i64* %0, i64 2
  %cond3 = icmp eq i64 %1, 10
  br i1 %cond3, label %loop_header, label %exit

loop_header:                                      ; preds = %loop_last, %entry
  %v23 = phi i64 [ 1, %loop_last ], [ 0, %entry ]        // WRONG!
  %v25 = getelementptr inbounds i64, i64* %0, i64 %v23
  %v26 = load i64, i64* %v25, align 4
  %v27 = and i64 %v26, 1879048192
  %cond2 = icmp eq i64 %v27, 1610612736
  br i1 %cond2, label %loop_2, label %exit

loop_2:                                           ; preds = %loop_header
  %ivar = add nuw nsw i64 %v23, 1
  %cond1 = icmp eq i64 %v26, %ivar
  br i1 %cond1, label %dont_delete, label %exit

dont_delete:                                      ; preds = %loop_2
  br label %loop_last

loop_last:                                        ; preds = %dont_delete
  %loop_cond = icmp ult i64 %ivar, 10
  br i1 %loop_cond, label %loop_header, label %loop_exit

loop_exit:                                        ; preds = %loop_last
  store i64 1610612736, i64* %address, align 8
  br label %exit

exit:                                             ; preds = %loop_exit,
%loop_2, %loop_header, %entry
  ret void
}

Note that

   %v23 = phi i64 [ 1, %loop_last ], [ 0, %entry ]

the value comes from BB %loop_last should not be constant 1.


The problem is, when ssa_copy is inserted, we have

%ivar = add nuw nsw i64 %v23, 1
  ...
%ivar.0 = call i64 @llvm.ssa.copy.93825120544720(i64 %ivar)
  ...
%ivar.0.1 = call i64 @llvm.ssa.copy.93825120544720(i64 %ivar.0)

When llvm is processing the last instruction in function
SCCPSolver::visitCallSite, it uses constant information from the first
instruction.

return (void)mergeInValue(IV, I, getValueState(PBranch->OriginalOp));

At this time we have:

(gdb) p I->dump()
  %ivar.0.1 = call i64 @llvm.ssa.copy.93825120544720(i64 %ivar.0)
$6 = void
(gdb) p CopyOf->dump()
  %ivar.0 = call i64 @llvm.ssa.copy.93825120544720(i64 %ivar)
(gdb) p PBranch->OriginalOp->dump()
  %ivar = add nuw nsw i64 %v23, 1

Basically the last instruction is a copy of %ivar.0, which is a copy of %ivar,
so which instruction is used to get constant information doesn't matter too
much, so current implementation uses the first instruction. But the problem is
the last instruction is not a direct user of the first instruction, so when the
constant information of %ivar is updated, the last instruction isn't updated
accordingly. So the correct fix should tell SCCP that I is a user of
PBranch->OriginalOp:
    addAdditionalUser(PBranch->OriginalOp, I);

On the other hand, when computing the constant information of the second
instruction, it combines guarded condition information, so in theory it may
have more precise information than first instruction. So I think a better fix
is:
   return (void)mergeInValue(IV, I, OriginalVal);</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>