Thread: Is there a way to see optimized code?

  1. #1
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665

    Is there a way to see optimized code?

    So, when I compile a .c/.cpp file with the -O3 flag, I imagine that it "fixes" a lot of the things that I did "wrong".

    My question is, is there a way to make gcc or some other compiler spit out a human readable file that isn't assembly showing me how the code was written?

    Like, a 1-to-1 mapping between a normal C/C++ ASCII file and the assembly it produces, assuming no other architecture specific optimizations were used (32 vs 64 bit is okay, I mean something like the -march=native flag).

    I'm just curious how super optimized C/C++ would look or what could I have written differently.

    Is -fdump-tree-optimized what I'm after here?

    For example, this code :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void) {
    
    
        const int size = 1000;
    
    
        int *x = malloc(size * sizeof(*x));
    
    
        for (int i = 0; i < size; ++i) {
    
    
            x[i] = i*i;
            printf("%d\n", x[i]);
        }
    
    
        free(x);
    
    
        return 0;
    }
    compiled with gcc -std=c11 -fdump-tree-optimized -O3 yields this .optimized file :
    Code:
    ;; Function main (main, funcdef_no=8, decl_uid=2122, symbol_order=8) (executed once)
    
    
    Removing basic block 5
    main ()
    {
      unsigned long ivtmp.4;
      int i;
      int * x;
      int _9;
    
    
      <bb 2>:
      x_5 = malloc (4000);
    
    
      <bb 3>:
      # ivtmp.4_14 = PHI <ivtmp.4_1(3), 0(2)>
      i_16 = (int) ivtmp.4_14;
      _9 = i_16 * i_16;
      MEM[base: x_5, index: ivtmp.4_14, step: 4, offset: 0B] = _9;
      printf ("%d\n", _9);
      ivtmp.4_1 = ivtmp.4_14 + 1;
      if (ivtmp.4_1 != 1000)
        goto <bb 3>;
      else
        goto <bb 4>;
    
    
      <bb 4>:
      free (x_5);
      return 0;
    
    
    }
    Now, I don't think this will even compile but go the compiler for the yolo goto's though?
    Last edited by MutantJohn; 06-05-2014 at 01:57 AM.

  2. #2
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    So basically a WYSIWYG auto-debugger?

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I mean, debugging is optional at this point in time. I was just wanna write better code lol.

    Or at least, I wanna see what the compiler is doing that I'm not. Like, how could I have written my code to make it better.

    I googled around and apparently most of the optimizations don't even happen at the C level anyway which I think is interesting. I wonder if anyone has ever invented anything like this before.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I'd say, given that you are so determined to consider "micro optimization", you should just learn assembler.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    .Net and Java have the capability to be de-compiled back to source code, and in many cases, the resulting source is higher quality code than what the programmer originally wrote. It becomes much more difficult with C++, because concepts in the various native machine languages don't necessarily map directly to C++ concepts in the way that MSIL does to C# or VB.Net. LLVM and Clang might have this capability. I know they use a sort of intermediate code, but that's literally as far as my knowledge of LLVM goes.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MutantJohn View Post
    Or at least, I wanna see what the compiler is doing that I'm not. Like, how could I have written my code to make it better.
    Don't use the compiler's optimized output as a reference for "better quality" code. The compiler does not produce optimized code that is supposed to be read by humans.
    I'd say you should go to a university and take some IT courses. You have a thirst for knowledge--and that's fine--and a university should help you fill in those gaps. Learn to code properly instead of relying on a compiler to do the job for you.

    I googled around and apparently most of the optimizations don't even happen at the C level anyway which I think is interesting. I wonder if anyone has ever invented anything like this before.
    They don't happen at C level for a good reason! For a compiler, C is like greek. It's a foreign language it knows little about other than how to translate.
    So all compilers translate it into a richer immediate language in which they are experts. There they can tweak and mess around with the source and output it into a binary.
    Beware that this level is pretty primitive - compilers don't usually work with "loop" constructs. They work with basic blocks, which are just a bunch of code with a goto at the end.
    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.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The -fverbose-asm flag, when combined with -S flag, will produce assembly output with source code interspersed as comments.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    So all compilers translate it into a richer immediate language in which they are experts.
    Every compiler I've ever used has been an ignoramus that happens to be able to consistently process code it doesn't understand.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by grumpy View Post
    Every compiler I've ever used has been an ignoramus that happens to be able to consistently process code it doesn't understand.
    Some people, when hearing a foreign language attempts to translate it into their native language before parsing its meaning. I see compilers doing this.
    Of course, there are some people who native translate a language directly into its semantic meaning without first translating it into their native language. For a computer program, that's very difficult.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Optimized 'strlen'
    By GReaper in forum C Programming
    Replies: 8
    Last Post: 01-11-2011, 09:48 AM
  2. Optimized C Compiler Capable of 16b Code
    By User Name: in forum C Programming
    Replies: 11
    Last Post: 12-29-2009, 05:43 PM
  3. Library Code Optimized?
    By MacNilly in forum C Programming
    Replies: 4
    Last Post: 11-06-2007, 05:27 AM
  4. Which is more optimized?
    By Moony in forum C Programming
    Replies: 7
    Last Post: 08-21-2007, 01:36 AM
  5. Compiler Optimized code
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 04-18-2004, 01:07 PM