Add a `memoized` function and use it throughout rustc