# Code optimization - syntax tree vs. intermediate representation

I'm working on a compiler for my own custom language. As I was reading an article on code optimization, I noticed that it assumed that the intermediate representation of the code had already been formed. Though I haven't yet started writing the optimization section of my compiler, I've been going through it in my head and it seems preferable to have the optimizer operate on the syntax tree before converting to the intermediate representation.

Is there a reason to prefer one approach to the other or is mostly a matter of personal taste?

For example, suppose I have an if block like

if ( some_expression ) {
do_stuff
}


If it could be recognized by the compiler that some_expression will always evaluate to true and have no side effects, then I could remove its computation simply by pruning the tree.

However, if I had already converted the tree to an intermediate representation, such as an assembly-ish list of simple instructions, the processes of recognizing and resolving the scenario would be (in my imagination since I haven't yet attempted to implemented this) far more complicated.