[LLVMdev] Does LLVM optimize recursive call?

Preston Briggs preston.briggs at gmail.com
Wed Oct 3 10:01:24 PDT 2012

> From: "Journeyer J. Joh" <oosaprogrammer at gmail.com>
> I have a simple question about LLVM.
> I learned that we need to use iterations than recursions in C programming.
> That is because recursion is expensive. It can easily consume out all
> the stack given to a program. And the call/return consumes time much
> more.
> But I've read a sentence that nowadays compilers can optimize
> recursions in C or whatever languages not to consume heavy stack and
> call/return.
> I wonder what kind of optimization this is and if LLVM support this
> kind of optimization.

At this point in your programming career, you should focus on writing
clean, correct code. It's the same thing I'm supposed to focus on at
this point in my own career!  If the desired algorithm is expressed
cleanly using recursion, use recursion (and procedure calls in
general). If it's cleaner to use a loop, use a loop. The compiler will
help when it can. Later, when you study compilers, you can learn
exactly when and where the compiler can do various optimizations.

Think about costs asymptotically; that's what matters. Calls and
returns require constant time, just like addition and multiplication.


More information about the llvm-dev mailing list