The problem of determining from a program and an input, whether the program will finish running on that input or continue to run forever, is known as the

halting problem.

The halting problem is (proven to be) undecidable in general for idealized computers.

Also undecidable in general are variations like "does a sub-routine fall in a loop for all possible inputs?" or "exists an input, for which it loops?". The halting problem is even undecidable for programs without input, because for any given pair of program and input, the input can be hard coded in the program, so that an equivalent program without input is obtained.

The relevant difference between idealized computers (i.e. mathematical models of computers, for example

Turing machines) and real computers is, that idealized computers have an infinite memory.

However even though we are running our programs on real computers with finite memory, regarding the loop detection the compiler had to determine, whether program execution loops endlessly, when there is sufficient memory, thus whether it loops endlessly on an idealized computer.

So, in general, detection of all endless loops by the compiler is impossible. In special cases it would be possible of course, but would a solution for only some special cases help much?

Regards

Martin