Add a `memoized` function and use it throughout rustc

I think it would be better for this argument to be before the closure, to improve readability.