0

I have an expression involving REAL as:

xf=w1*x1 + w2*x2 + w3*x3 + w1*y1 + w2*y2 + w3*y3

I want to know if the (Intel Fortran) compiler optimized it to:

xf=w1*(x1+y1) + w2*(x2+y2) + w3*(x3+y3)

How do I see the expression tree which was generated for this expression?

arunmoezhi
  • 3,082
  • 6
  • 35
  • 54
  • 1
    Which compiler? Which language? – Antimony Aug 20 '12 at 22:54
  • Note that this isn't common subexpression elimination. You're rewriting the expression to an equivalent expression with less operations by using the rules of arithmetic. You're not reusing any subexpression. – sepp2k Aug 20 '12 at 23:02
  • @sepp2k: Got confused with another question which I was about to post about CSE. I have updated the title now – arunmoezhi Aug 20 '12 at 23:13
  • @Antimony: intel fortran compiler – arunmoezhi Aug 20 '12 at 23:13
  • 1
    I'm assuming this is floating point arithmetic? If so, check out [Why doesn't GCC optimize a*a*a*a*a*a to (a*a*a)*(a*a*a)?](http://stackoverflow.com/q/6430448/395760) for another compiler's take on a related optimization. I wouldn't bet this transformation is correct w.r.t. rounding, and thus the compiler may not do it (unless asked via a particular flag). –  Aug 21 '12 at 04:21
  • @delnan: That is a wonderful read. Thanks a lot. I've updated my question. Yes, I'm doing floating point arithmetic (double precision). So from this article I can very well assume that the compiler did not optimize the expression. – arunmoezhi Aug 21 '12 at 07:45

2 Answers2

1

Your standard common subexpression schemes would not perform the above transformation, and some languages would regard the transformation as illegal, since it could result in different side-effects.

But high-performance FORTRAN compilers (which probably excludes Intel FORTRAN) might do it.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
0

How do I see the expression tree which was generated for this expression?

If your compiler has an option to display the tree after a given optimization phase, you can use that option. To find out whether that's the case, consult your compiler's optimization.

If your compiler does not have such an option, you won't be able to see which trees (or other internal representations) the compiler generated during its run. In that case your best bet would be to simply look at the generated assembly to see which arithmetic operations will be performed.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • The source code has more than 3k lines. Mapping source lines to assembly will be tedious with compiler optimization enabled. Is there a better way to do it. I tried to use gdb to get PC of a particular source line using `line info` and then get the assembly of it by using `disas [PC]`. But source line numbers do not match exactly when compiler optimization is enabled – arunmoezhi Aug 21 '12 at 01:29