<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 --- - [SCEV] Phi-node backedge is assumed to be at the same index for all phi-nodes in getConstantEvolutionLoopExitValue"
   href="https://llvm.org/bugs/show_bug.cgi?id=25060">25060</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[SCEV] Phi-node backedge is assumed to be at the same index for all phi-nodes in getConstantEvolutionLoopExitValue
          </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>normal
          </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>mattias.v.eriksson@ericsson.com
          </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>Created <span class=""><a href="attachment.cgi?id=15006" name="attach_15006" title="Reduced input file">attachment 15006</a> <a href="attachment.cgi?id=15006&action=edit" title="Reduced input file">[details]</a></span>
Reduced input file

Run opt on the attached file like this:

opt -S  -instcombine -licm -sroa -indvars -o t1.ll

Results:

; ModuleID = './t1.ll'

@gi = global i16 0

define i16 @fn3() {
  br label %bb1

bb7:                                              ; preds = %bb2
  %_tmp2 = add i16 %i.9.0, -1
  br label %bb2

bb2:                                              ; preds = %bb2.preheader,
%bb7
  %i.9.0 = phi i16 [ %i.9.1, %bb2.preheader ], [ %_tmp2, %bb7 ]
  %_tmp6 = icmp eq i16 %i.9.0, 0
  br i1 %_tmp6, label %bb4, label %bb7

bb4:                                              ; preds = %bb2
  br label %bb1

bb1:                                              ; preds = %bb4, %0
  %i.9.1 = phi i16 [ 1, %0 ], [ 0, %bb4 ]
  %_tmp10 = icmp eq i16 %i.9.1, 0
  br i1 %_tmp10, label %bb5, label %bb2.preheader

bb2.preheader:                                    ; preds = %bb1
  br label %bb2

bb5:                                              ; preds = %bb1
  store i16 1, i16* @gi, align 2
  ret i16 0

bb6:                                              ; No predecessors!
  ret i16 0
}


The error is that value 1 is stored on the line
  store i16 1, i16* @gi, align 2
The correct value to store is 2.

I have been debugging this and found a problem in ScalarEvolution.cpp:
  // Since the loop is canonicalized, the PHI node must have two entries.  One
  // entry must be a constant (coming in from outside of the loop), and the
  // second must be derived from the same PHI.
  bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
  PHINode *PHI = nullptr;
  for (BasicBlock::iterator I = Header->begin();
       (PHI = dyn_cast<PHINode>(I)); ++I) {
    Constant *StartCST =
      dyn_cast<Constant>(PHI->getIncomingValue(!SecondIsBackedge));

Here the code assumes that all phi-nodes have the incoming backedge value at
the same index, but this is not true in this example.

Is this a problem in ScalarEvolution or is the problem already in the input
with badly created phi-nodes?</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>