R600: Add helper functions.

Extract these from some of my other patches, since this
is the only thing really making them dependent on each other.
Use cast instead of assert + dyn_cast