https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61289

            Bug ID: 61289
           Summary: Bad jump threading generates infinite loop
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xinliangli at gmail dot com

Build the following program with -fno-exceptions -O2, the program aborts at
runtime.

Adding -fno-tree-dominator-opts, the program runs fine.

To trigger the problem, the first iteration needs to return list == new_list
--> the code will then get into the cloned path which is actually an infinite
loop.

(the toy program does something nonsense, but it should finish without
aborting).

jpthread.h
---------

struct MyList {
 inline int LastHit () const { return last_; }
 // inline int FirstHit () const { return last_-1; }

 MyList* PushBack(int);
// MyList* PushFront(int);
 void Destroy();

 static MyList* Create(int);
 MyList(int i): last_(i) {}
 void PopBack();
 int last_;
};

jpthread.cc
------------

#include "jpthread.h"

void test  ()
{

  MyList *list = MyList::Create(20);

  for (int i = 0; i < 2; ) {
    MyList* new_list = list->PushBack(
        list->LastHit() + 10);
    if (new_list != list) {
      list->Destroy();
      list = new_list;
      ++i;
    }
  }

  list->Destroy();
}
int main()
{
 test();
}


m.cc
-----
#include <stdlib.h>
#include <stdio.h>
#include "jpthread.h"


int cnt = 0;
MyList *prev = 0;

MyList* MyList::PushBack(int i)
{
  cnt ++;

  if (cnt == 1)
     return prev;

  if (cnt > 3) {
     fprintf (stderr, "Loop not ending, aborting ..\n");
     abort ();
  }

  prev = this;
  return new MyList(i+last_);
}

MyList* MyList::Create(int i)
{
  prev = new MyList(i);

  return prev;
}

void MyList::Destroy() {
   delete this;
}

Reply via email to