[llvm-bugs] [Bug 35102] New: InstCombiner::foldAllocaCmp does not check whether icmp is not in a loop

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Oct 26 17:45:14 PDT 2017


            Bug ID: 35102
           Summary: InstCombiner::foldAllocaCmp does not check whether
                    icmp is not in a loop
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: juneyoung.lee at sf.snu.ac.kr
                CC: llvm-bugs at lists.llvm.org

Created attachment 19352
  --> https://bugs.llvm.org/attachment.cgi?id=19352&action=edit
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;
void test() {
    int i = 1;

-- 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

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

  for (int j = 0; j < 2; j++) {
      if (&i == p+j) {

void gee(int* p) {
  int i;

  if (&i == p+0) {

  if (&i == p+1) {

unreachable.cpp written by Ralf Jung, test2.c written by Gil Hur

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20171027/8f2c060e/attachment.html>

More information about the llvm-bugs mailing list