<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 - InstCombiner::foldAllocaCmp does not check whether icmp is not in a loop"
   href="https://bugs.llvm.org/show_bug.cgi?id=35102">35102</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>InstCombiner::foldAllocaCmp does not check whether icmp is not in a loop
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </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>new bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>juneyoung.lee@sf.snu.ac.kr
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=19352" name="attach_19352" title="cpp code that removes for loop away">attachment 19352</a> <a href="attachment.cgi?id=19352&action=edit" title="cpp code that removes for loop away">[details]</a></span>
cpp code that removes for loop away

Compiling this code with -O3 generates following assembly code:

-- unreachable.cpp --
#include <stdint.h>
void unreachable();
static void compare_to_all(uintptr_t i) {
    uintptr_t cur = 0;
    while(true) {
        if ((int*)i == (int*)cur) return;
        if (cur == UINTPTR_MAX) break;
        ++cur;
    };
    unreachable();
}
void test() {
    int i = 1;
    compare_to_all((uintptr_t)&i);
}
--

-- unreachable.s --
test(): # @test()
  jmp unreachable() # TAILCALL
--

InstCombiner::foldAllocaCmp folds `(int*)i == (int*)cur` into false after
checking that there exists only one icmp instruction which uses alloca i.
If the purpose of this optimization was to fold single pointer equality
comparison into false, syntactically checking the number of icmp isn't enough.
It should also check whether the icmp is not in a loop. In following code
(test2.c), this optimization folds the pointer comparison in foo(), but not in
gee().

-- test2.c --
void foo(int* p) {
  int i;

  for (int j = 0; j < 2; j++) {
      if (&i == p+j) {
        printf("ok\n");
      }
  }
}

void gee(int* p) {
  int i;

  if (&i == p+0) {
    printf("ok\n");
  }

  if (&i == p+1) {
    printf("ok\n");
  }
}
--

unreachable.cpp written by Ralf Jung, test2.c written by Gil Hur</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>