[LLVMdev] Does LLVM optimize recursive call?

Journeyer J. Joh oosaprogrammer at gmail.com
Wed Oct 3 00:22:49 PDT 2012

Hi David Blaikie and others who might be interested on this

Thank you very much!

#1. Then I'd like to know, to make Clang/LLVM optimize a recursion
into an iteration, how a recursion has to be implemented with any
compiler option? (if the language is C/C++)

Clang uses recursions, especially it uses recursive decent and
operator-precedence parser.
#2. I wonder if this kind of recursion is optimized to a iteration.

Sorry my question is getting deeper. ^^;

Thank you very much in advance

Journeyer J. Joh

2012/10/3 David Blaikie <dblaikie at gmail.com>:
> On Tue, Oct 2, 2012 at 11:44 PM, Journeyer J. Joh
> <oosaprogrammer at gmail.com> wrote:
>> Hi list,
>> 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.
>> I am not a compiler expert. Please consider this. ^^;
> The short answer is: sometimes.
> Some recursive code may be transformed into something similar to a
> loop, but sometimes the compiler won't be able to figure it out &
> it'll remain recursive.
> - David

Journeyer J. Joh
o o s a p r o g r a m m e r
a t
g m a i l  d o t  c o m

More information about the llvm-dev mailing list