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 situations, we can leverage alias knowledge for optimization (assuming that concurrent modifications by other threads won't conflict with the current thread or that language memory models permit updates to remain invisible). Conversely, if p and q's connection is uncertain, no optimizations are feasible, and the entire code must be executed to determine the result.