Replace all weird loops with appropriate split/join instructions