<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 - Failure to optimize add loop in -O3 as well as in -Oz"
   href="https://bugs.llvm.org/show_bug.cgi?id=49024">49024</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Failure to optimize add loop in -O3 as well as in -Oz
          </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>gabravier@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>int f(uint8_t max_value) {
    int sum = 0;
    for (int i = 0; i < max_value; i++) {
        sum += i;
    }
    return sum;
}

This can be optimized to `return (((unsigned _ExtInt(33))max_value) * (unsigned
_ExtInt(33))((uint32_t)max_value - 1)) >> 1;`. This transformation is done
under `-Oz`, but `-O3` seems to generate far inferior code, as the IR shows:

define dso_local i32 @_Z1fh(i8 zeroext %0) local_unnamed_addr #0 {
  %2 = zext i8 %0 to i32
  %3 = icmp eq i8 %0, 0
  br i1 %3, label %14, label %4

4: ; preds = %1
  %5 = add nsw i32 %2, -1
  %6 = zext i32 %5 to i33
  %7 = add nsw i32 %2, -2
  %8 = zext i32 %7 to i33
  %9 = mul i33 %6, %8
  %10 = lshr i33 %9, 1
  %11 = trunc i33 %10 to i32
  %12 = add i32 %2, %11
  %13 = add i32 %12, -1
  br label %14

14: ; preds = %4, %1
  %15 = phi i32 [ 0, %1 ], [ %13, %4 ]
  ret i32 %15
}

Compared to the far simpler and faster code generated on -Oz:

define dso_local i32 @_Z1fh(i8 zeroext %0) local_unnamed_addr #0 {
  %2 = zext i8 %0 to i32
  %3 = zext i8 %0 to i33
  %4 = add nsw i32 %2, -1
  %5 = zext i32 %4 to i33
  %6 = mul i33 %3, %5
  %7 = lshr i33 %6, 1
  %8 = trunc i33 %7 to i32
  ret i32 %8
}

Godbolt comparison: <a href="https://godbolt.org/z/vW5jKz">https://godbolt.org/z/vW5jKz</a>
alive2 comparison: <a href="https://alive2.llvm.org/ce/z/NVhmdm">https://alive2.llvm.org/ce/z/NVhmdm</a></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>