Only save/restore stack in for loops when necessary.

Fixes bug with saving/restoring stack on ptx.