Memoization Refactoring based on Purity Analysis