In both cases, we are able to perform optimizations from the alias knowledge (assuming that no other thread updating the same locations can interleave with the current thread, or that the language memory model permits those updates to be not immediately visible to the current thread in absence of explicit synchronization constructs). On the other hand, if it is not known if p and q alias or not, then no optimizations can be performed and the whole of the code must be executed to get the result.
In both cases, we can apply optimization techniques based on alias knowledge (assuming that concurrent modifications by other threads won't interfere with the current thread or that language memory models permit updates to remain hidden). However, if it's unclear whether p and q are related, no optimizations can be applied, and the entire program must be executed to obtain the outcome.