Add simplify function for for loops, #4840