LLVM tail call optimization -
here understanding of things:
a function "f" tail recursive when calling last action. tail-recursion can optimized forming loop instead of calling function again; function's parameters updated in place, , body ran again. called recursive tail call optimization.
llvm implements recursive tail call optimization when using fastcc, ghc, or hipe calling convention. http://llvm.org/docs/codegenerator.html#tail-call-optimization
i have questions: let's consider silly example:
int h(int x){ if (x <= 0) return x; else h(x-1); }
1) in example, keyword "tail" preceeds call. elsewhere read keyword optional. suppose function above translated llvm appropriately, last few lines need be
%x' = load *i32 %x %m = tail call fastcc i32 @h(i32 %x') ret %m
2) meaning of inreg option in example?
3) not want perform tail call optimizations on place, recursive functions. there way can llvm perform optimization (when available) recursive functions?
apparently answer yes. have change definition of h see (because optimizer good! figures out h either identity or returns 0).
consider
int factorial (int x, int y){ if (x==0) return y; else return factorial(x-1,y*x); }
compiled clang -s -emit-llvm, no optimization performed. 1 sees no calling conventions directly specified, means default calling convention enough support tail recursion optimization (whether or not supports tail calling in general different story -- interesting know, guess different question).
the file emitted clang -s -emit-llvm main.s (assuming factorial definition in main.c). if run
opt -o3 main.s -s -o mainopt.s
then can see tail recursion eliminated. there optimization called tailcallelim may turned on -o3. it's hard tell because file, opt --help, says -o3 similar gcc -o3.
the point can see calling convention not need specified this. maybe fastcc not needed, or maybe default? (1) partially answered; however, still not know (2) or (3).
Comments
Post a Comment