Consolidate MemRefs handling from BranchFolding and correct latent bug