[LLVMdev] Newbie Question: not using local variables

John McCall rjmccall at apple.com
Mon Feb 7 17:41:27 PST 2011


On Feb 7, 2011, at 3:28 PM, Brian Hurt wrote:

> 
> I'm writing an ML-like language, and using LLVM as my target back end.  I 
> have one question though, and thought I'd throw it out onto the list.  In 
> my language, variables aren't mutable- once assigned, they can be 
> shadowed, but not changed.  And shadowing of variables is handled by 
> alpha-renaming.  What I mean by this that when I see:
> 	let x = 3 in ...
> the value of x can not be changed.  It can be shadowed, like:
> 	let x = f () in
> 	let x = x + 4 in
> 	...
> but one of the first things I do is rename all the variables so they're 
> unique, so the above code might get changed into:
> 	let x_5734 = f () in
> 	let x_8643 = x_5734 + 4 in
> 	...
> 
> Now, my question is this: is there any downside to not having any 
> stack-based local variables at all- just put everything into registers and 
> let the register allocation code decide what needs to get spilled onto the 
> stack?  In other words, is there any problem with generating the following 
> IR for the above code:
> 	%r17 = call i64 @f(i64 0)
> 	%r18 = add i64 %r17 4
> 
> The upside of doing this is simpler code generation, and hopefully more 
> efficient code, as I am explicitly telling the optimizer I don't care 
> where this variable lives.
> 
> But, I'm somewhat worried that by not having any local variables at all, 
> this may put too much pressure on the register allocator (especially for 
> large functions).

What Reid said, plus:  the worst case of putting pressure on the register
allocator is that it spills something to the stack.  Forcing the use of local
variables, by contrast, is basically just spilling to the stack all the time, so
you're no better off than you were before.

John.





More information about the llvm-dev mailing list