<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 - [IndVarSimplify] ReplaceExitValue=cheap cost modelling is too pessimistic"
   href="https://bugs.llvm.org/show_bug.cgi?id=44668">44668</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>[IndVarSimplify] ReplaceExitValue=cheap cost modelling is too pessimistic
          </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>lebedev.ri@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Apologies in advance for the crudeness of the "reduced" test case.
I'm providing the full sample for now, to be sure that i don't overreduce it.

<a href="https://godbolt.org/z/ZstrDC">https://godbolt.org/z/ZstrDC</a>

If we look at the original IR (see '-lcssa' output), we see:

  <...>
  %10 = load i32, i32* %width, align 8, !tbaa !15
  %16 = zext i32 %10 to i64
; ^ loop-invariant, available before loop entry etc etc
  <...>

for.body:                                         ; preds = %for.body,
%omp.inner.for.body
  %indvars.iv158 = phi i64 [ %indvars.iv.next159, %for.body ], [ 1,
%omp.inner.for.body ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 2,
%omp.inner.for.body ]
  <...>
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %cmp11 = icmp ult i64 %indvars.iv.next, %16
  %indvars.iv.next159 = add nuw nsw i64 %indvars.iv158, 1
  br i1 %cmp11, label %for.body, label %for.cond.invoke.cont15_crit_edge

for.cond.invoke.cont15_crit_edge:                 ; preds = %for.body
  %indvars.iv.lcssa = phi i64 [ %indvars.iv, %for.body ]
  %.lcssa = phi i16 [ %35, %for.body ]
  %conv.i.i.i74.le = sext i16 %.lcssa to i32
  %36 = trunc i64 %indvars.iv.lcssa to i32
  br label %invoke.cont15

invoke.cont15:                                    ; preds =
%for.cond.invoke.cont15_crit_edge, %omp.inner.for.body
  %col.0.lcssa = phi i32 [ %36, %for.cond.invoke.cont15_crit_edge ], [ 1,
%omp.inner.for.body ]
  %idxprom.i.i.i.i72.lcssa = phi i64 [ %indvars.iv.lcssa,
%for.cond.invoke.cont15_crit_edge ], [ 1, %omp.inner.for.body ]
  <...>


So %indvars.iv* is used outside of the loop.
I suspect this is one of the reasons preventing this loop from being
vectorized.

Indeed, IndVarSimplify *can* deal with this, but it doesn't due to costmodel,
with '-lcssa -indvars -replexitval=always', we get the 'expected':

  %10 = load i32, i32* %width, align 8, !tbaa !15
  %16 = zext i32 %10 to i64
  %17 = icmp ugt i64 %16, 3
  %umax = select i1 %17, i64 %16, i64 3
  %18 = add i64 %umax, -1
  <...>

for.body:                                         ; preds =
%for.body.preheader, %for.body
  %indvars.iv158 = phi i64 [ %indvars.iv.next159, %for.body ], [ 1,
%for.body.preheader ]
  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 2,
%for.body.preheader ]
  %29 = add nsw i64 %indvars.iv158, -1
  <...>
  %indvars.iv.next159 = add nuw nsw i64 %indvars.iv158, 1
  br i1 %cmp11, label %for.body, label %for.cond.invoke.cont15_crit_edge

for.cond.invoke.cont15_crit_edge:                 ; preds = %for.body
  %.lcssa = phi i16 [ %37, %for.body ]
  %conv.i.i.i74.le = sext i16 %.lcssa to i32
  %38 = trunc i64 %18 to i32
  br label %invoke.cont15

invoke.cont15:                                    ; preds =
%for.cond.invoke.cont15_crit_edge, %omp.inner.for.body
  %col.0.lcssa = phi i32 [ %38, %for.cond.invoke.cont15_crit_edge ], [ 1,
%omp.inner.for.body ]
  %idxprom.i.i.i.i72.lcssa = phi i64 [ %18, %for.cond.invoke.cont15_crit_edge
], [ 1, %omp.inner.for.body ]


So in old case, in %invoke.cont15 bb, the incoming value for
%col.0.lcssa was essentially %indvars.iv, while now it is %18,
with cost=3 extra instructions.

Is this something fixable in IndVarSimplify?</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>