AMDGPU: Use DFS to avoid second loop over function