R600: Fix a potential iterator invalidation issue.

As a bonus this reduces the loop from O(n^2) to O(n).