fix Issue 8525 - optimizer loops infinitely

https://d.puremagic.com/issues/show_bug.cgi?id=8525