1

How can we find loop iterator in static analysis? What are different condition for a variable to be iterator?

In a super simplified for loop like for(i = 0; i < n; i++); we can assume that, lhs of initialization expression is iterator. But how can we find the iterator in a while loop or a more complex for loop?

I'm ready with following concepts:

  • Reaching definitions
  • Range Values
  • Control flow graph
niyasc
  • 4,440
  • 1
  • 23
  • 50
  • I'm afraid you can't even generally assume that "lhs of initialization expression is iterator". Plenty of `for` loops leave that part out completely, others have multiple declarations in there, yet others use a different iterator from the one initialized in there. – YePhIcK Sep 25 '15 at 02:51
  • I don't think SO is designed for this question. A whole book could be (and probably has been) written on loop analysis. – owacoder Sep 25 '15 at 02:53
  • @YePhIcK. I'm using that strategy for a super simple for loop. Question is also meant for other types of for loops as well. – niyasc Sep 25 '15 at 03:01
  • Not sure what this question is about. It isn't even mandatory that a loop has an interator at all (and I mean all types of loop). – Havenard Sep 25 '15 at 03:03

1 Answers1

2

In general there is no loop iterator. For example, a single iterator variable may be insufficient (e.g., an array can be traversed from both ends simultaneously), termination of a loop can depend on external data (e.g., user input) and there are useful infinite loops (where iterators are useless).

However some heuristics can be used to identify iterator expressions. For loops they would frequently express two properties: progress and termination. Progress is often related to an update of the same variable with an value that depends on this variable, for example:

i++; // For indexed structures
p = p -> next; // For linked structures

Termination is often related to a comparison of a progress expression to some constant (meaning "constant for the loop"), for example:

i <= n; // For scalar values
p == null; // For pointers

In general there could be several progress expressions and therefore several termination constants, and several progress variables can be used in a termination expression.

So, a basic strategy would be:

  1. Collect all variables from an exit condition of a loop.
  2. Check which of them are modified in a loop body with a dependency on themselves.

But keep in mind, that some loops have no progress expressions, some have no termination conditions, variable updates can be indirect and there is aliasing.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • This answer seems to be fair. At least a good start. Thanks. I'm closing this question for time being. – niyasc Sep 25 '15 at 16:14