Re: where exactly c++,c fail and Ada gets thru'



Martin Krischik wrote:

The point is - of corse - it that the semantic tree of the Ada
compiler contains the information that X will be in the range of (1 ..
5) and then the optimizer can easily calculate that (1 .. 5) + 3 < 10
and then optimize away the check.

The sematinc tree of the C++ will contain the information that X is in
the range of (-2*32 .. +2*32-1) and that is a pretty useless
information to the optimizer.

Except that the optimizer is not limited to use only this information.
With the two example ranges (1..5 and 1..10) the result of inlining will be that the following operations - all operating on the single value - will be placed one after another:

1. compare against 5
2. add 3
3. compare against 10

It doesn't take a rocket scientist to deduce that the third operation has a dead branch and can be therefore eliminated.

Of course, Ada provides more information to the compiler and the more information the compiler has, the wider are the optimization opportunities, therefore languages with richer type systems are easier to optimize. Nobody questions that. But this is very far from the optimization being guaranteed in Ada or impossible in C++ - neither is the case.


--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
.



Relevant Pages

  • Re: left-to-right (was In-Out Parameters for functions)
    ... > What I would expect from a good compiler is factoring out the cycle ... > implicit order is the one assumed by the programmer. ... That is not possible in Ada 95; it would be possible in "left-to-right ... > sort of premature optimization Knuth wrote about. ...
    (comp.lang.ada)
  • Re: GNAT Optimization of Constant Expressions
    ... Ada, so I surmised that the constant math expressions in the code ... below were being precomputed by the compiler in C and Fortran, ... the compiler has to do a loop-hoisting optimization in order to ... I suspect that from your results, Gnat ...
    (comp.lang.ada)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... what experienced programmers do, ... optimization, ... Thugs" ad nauseum fits that a lot more closely than discussing compiler ... be modified outside a loop, and guessing ...
    (comp.programming)
  • long(!) Re: need help on CFLAGS in /etc/make.conf please
    ... For example, MPlayer sets this high on purpose, so GCC will actually ... and the K&R compiler would've known exactly the kind of optimization we wanted. ... >> A msg from Richard Coleman, taken together with the GCC 3.x Known Bugs ...
    (freebsd-questions)
  • Re: SETF and variable issues in Self Similar program.
    ... The way to get a compiler to optimize a tail-call ... call optimization turned on. ... It changes the semantics of the language in a big way. ... In Common Lisp it has to interact with condition handling, ...
    (comp.lang.lisp)