Thread: Testing Compiler Optimization

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    Testing Compiler Optimization

    I'm trying to find out how exactly my compiler optimizes certain code.

    The issue is that in any test program I have written MSVC will completely optimize out all the code knowing that it has no visible result!

    For example, in an attempt to see how much the compiler can optimize this code:
    Code:
    Vector3D TestFn( float x, float y, float z )
    {
       return Vector3D( 1, 2, 3 ) * Vector3D( x, y, z );
    }
    (This is just a bad example)
    Calling the function and saving the result will all simply be compiled out because it knows that I don't use the result.

    Code:
    Vector a = TestFn( 4, 5, 6 );
    How can I trick MSVC into thinking that the return value is used so it will not compile it completely out? Or something similar.

    Thanks!

    EDIT: I'm looking at the assembly by the way, I want it to generate assembly as if I used the return value instead of generating nothing.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Just do something with the result, like print it out.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    Quote Originally Posted by oogabooga View Post
    Just do something with the result, like print it out.
    Derp... I'm literally retarded.

    Thanks!

    EDIT: I also need to have the components (in the given example) of the vector be input by the console too for it to generate the code I want it to.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jamesleighe View Post
    EDIT: I also need to have the components (in the given example) of the vector be input by the console too for it to generate the code I want it to.
    Yeah, otherwise the compiler will see that the code is constant--ie, the result will always be the same, so it can optimize it away and simply hard code the result.
    Another way of forcing the compiler to not optimize away something is to add some randomness to it. For example, calculate your vectors, then add a random constant, then print the result.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Usually making the result variable volatile will do, too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  2. Replies: 10
    Last Post: 07-17-2008, 11:21 AM
  3. help w/optimization
    By dudeomanodude in forum C++ Programming
    Replies: 3
    Last Post: 02-08-2008, 02:45 PM
  4. Replies: 2
    Last Post: 02-04-2008, 02:34 AM
  5. Testing Testing 123.. Oh yeah and...
    By minime6696 in forum Windows Programming
    Replies: 0
    Last Post: 08-13-2001, 09:07 AM