Given a simple loop in C or C++ such as the following:

for (int i=0, j=0; i<5; i=i*2){

I would like to use a tool (gcc, clang or any other) to optimize the code without actually compiling it. I just want to see my own source code optimized. Is such a tool available?

  • 1
    What do you imagine the output of this optimizer would be? Commented Oct 21, 2018 at 1:44
  • 4
    @Daniel: Such a static analysis tool is likely to tell you that you have an infinite loop (0 times anything is still 0, so i will never change), rather than to optimize this loop. Commented Oct 21, 2018 at 3:04
  • 3
    @Daniel: That is impossible. The definition of a compiler is "a program that takes a program as input and outputs a semantically equivalent program as output". What you want is impossible to do "without compilation", because what you want to do is exactly the definition of "compilation". So, what precisely is it that you want to do? Can you give a precise, exact, unambiguous, objective definition of what you mean by "optimization" and "without compilation"? Also, are you talking about C or C++? Those are two very different languages, it does not make much sense to mush them together Commented Oct 21, 2018 at 9:40
  • 3
    Whoa, that's an ambitious idea. Now, there aren't many job openings in that area, and they are rather highly specialized. I mean, this is a kind of job you make CS PhD for. Actually, making CS PhD in this area is likely the fastest way you can get there. Are you considering this? Oherwise, I would suggest that career of C/C++ developer and treating compilers as a hobby is a much easier and reliable way to go.
    – Frax
    Commented Oct 21, 2018 at 15:42
  • 2
    @Daniel Since the loop has no visible side effects, the optimizer should simply remove it.
    – Calchas
    Commented Oct 21, 2018 at 22:12

2 Answers 2


Optimization is a part of the compilation/linking process. You cannot really separate it from that process. Your best bet is to look at the actual assembly code created by your compiler. As far as the real-world impact of optimization is concerned that’s the result that matters.

Probably best known in the C++ world is the online Compiler Explorer by Matt Goldbolt where you can compile and look at the assembly created by a wide variety of compilers, platforms and settings. Also, you get a pretty good visual mapping between lines in your C++ code and the corresponding assembly.

I seem to remember vaguely that Visual Studio has a similar mode that shows assembly mapped to the source code. If that’s your IDE you might want to look into it.


Depending on where you want to stop along the way LLVM's intermediate representation might be what you are looking for. Still, it requires you to run clang and might not be what you are looking for.

  • IR is more similar to assembly than C source code, so doesn't really help. I'm surprised no such tool is out there
    – Daniel
    Commented Oct 21, 2018 at 1:56
  • 1
    That I don't know one doesn't mean there isn't. And honestly - if you care so much about optimization you should learn assembly as part of learning about your target CPU. Or just trust your compiler. Oh, and optimization is all about measuring things anyways. Modern CPUs are so complicated that from optimization POV they can be treated as non-deterministic.
    – jaskij
    Commented Oct 21, 2018 at 1:58
  • @JanDorniak: It is pretty easy to prove that such a tool cannot possibly exist. An optimizer is a program that takes a program as input and outputs a semantically equivalent program that is, by some measure of "efficiency" (runtime, memory usage, code size, …) intended to be (hopefully) more efficient. Compilation is the act transforming an input program into a semantically equivalent output program. Therefore, optimization is by definition compilation and a tool "to optimize the code without actually compiling it" cannot possibly exist. Commented Oct 21, 2018 at 9:50
  • @Daniel, optimization in compilers happens only in IR or Assembly. If you want to learn about compiler optimization, this is the way to go.
    – Frax
    Commented Oct 21, 2018 at 14:29
  • You can look at gcc's optimization passes with the -fdump-tree options. Some of these emit the pass in GIMPLE, which is a language not very different from C syntax, a bit like LLVM's IR. But it's worth saying that it isn't possible to represent the optimized code back in the C language in some way that perfectly preserves the nuance of the optimizations. C isn't flexible enough for that and wasn't designed to do that.
    – Calchas
    Commented Oct 21, 2018 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.