I think it just tokenizes everything and does pattern matching to find compositions it can exploit. It's not unlike compiler optimization.