Re: Global versus Stack variables

napi@cs.indiana.edu (Mohd Hanafiah Abdullah)
26 Nov 2005 00:18:25 -0500

          From comp.compilers

Related articles
Global versus Stack variables shreyas76@gmail.com (shrey) (2005-11-19)
Re: Global versus Stack variables oliverhunt@gmail.com (oliverhunt@gmail.com) (2005-11-21)
Re: Global versus Stack variables gah@ugcs.caltech.edu (glen herrmannsfeldt) (2005-11-21)
Re: Global versus Stack variables henry@spsystems.net (2005-11-21)
Re: Global versus Stack variables torbenm@app-5.diku.dk (2005-11-21)
Re: Global versus Stack variables jatin.bhateja@amdocs.com (Jatin Bhateja) (2005-11-26)
Re: Global versus Stack variables napi@cs.indiana.edu (2005-11-26)
Re: Global versus Stack variables david.thompson1@worldnet.att.net (Dave Thompson) (2005-12-05)
| List of all articles for this month |

From: napi@cs.indiana.edu (Mohd Hanafiah Abdullah)
Newsgroups: comp.compilers
Date: 26 Nov 2005 00:18:25 -0500
Organization: Computer Science, Indiana University
References: 05-11-094
Keywords: design, optimize
Posted-Date: 26 Nov 2005 00:18:25 EST

  shrey <shreyas76@gmail.com> wrote:
> I was wondering if ppl can give their opinion on the following
>question. What is the impact of a variable being a global or a local
>variable on optimizations. In other words, is it likelier to produce
>better code if a variable is local than global. The reason I ask this
>is I wonder if register allocation is harder if a variable is global
>instead of local. Are there other optimizations which get affected due
>to this consideration.


>[It depends on your language, but in languages with pointers, the
>biggest advantage of stack locals is that the compiler can assume
>no aliasing of stack variables that haven't had their address taken.
>-John]


And for local variables you need to do some cost-analysis in order to not
over-allocate them to registers. Sometime it's better to keep them in memory
instead of registers if there are too many instances of calls to functions
that will require saving the register content in memory (callee/caller saves)
each time. This register-moves cost-analysis is explained in the paper by
Fred Chow and John Hennessy on Priority-based Graph Coloring Register
Allocation technique.


To allocate global variables to registers requires inter-module alias analysis
(for separate compilation) which I haven't tried. With faster CPU and memory
nowadays the performance gap between saving data in registers vs memory is
getting very narrow. But, there will always be instances where fast is better
than less fast, especially in hard real-time systems requirements.


Napi
--
http://www.axiomsol.com
http://www.cs.indiana.edu/hyplan/napi.html


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.