<html>
    <head>
      <base href="http://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 incorrectly flags add recurrences as nsw"
   href="http://llvm.org/bugs/show_bug.cgi?id=22179">22179</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>SCEV incorrectly flags add recurrences as nsw
          </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>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Loop Optimizer
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>sanjoy@playingwithpointers.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>When running opt -analyze -scalar-evolution on the following program

%struct.anon = type { i8 }
%struct.S = type { i32 }

@a = common global %struct.anon zeroinitializer, align 1
@b = common global %struct.S zeroinitializer, align 4

; Function Attrs: nounwind ssp uwtable
define i32 @main() {
  store i8 0, i8* getelementptr inbounds (%struct.anon* @a, i64 0, i32 0),
align 1
  br label %loop

loop:
  %storemerge1 = phi i8 [ 0, %0 ], [ %inc, %loop ]
  %m = load volatile i32* getelementptr inbounds (%struct.S* @b, i64 0, i32 0),
align 4
  %inc = add nuw i8 %storemerge1, 1
  %exitcond = icmp eq i8 %inc, -128
  br i1 %exitcond, label %exit, label %loop

exit:
  store i8 -128, i8* getelementptr inbounds (%struct.anon* @a, i64 0, i32 0),
align 1
  ret i32 0
}


we get the SCEV {1,+,1}<nuw><nsw><%loop> for %inc = add nuw i8 %storemerge1, 1.
 However, the llvm expression can legitimately signed-overflow.

The real issue is that in several places in ScalarEvolution, we have

  // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
  // And vice-versa.


The vice-versa part is incorrect I think -- it is possible for {1,+,1} to not
unsigned-overflow, but it is okay for it to signed-overflow, for instance.</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>