Re: Global versus Stack variables

torbenm@app-5.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
21 Nov 2005 22:46:30 -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: torbenm@app-5.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
Newsgroups: comp.compilers
Date: 21 Nov 2005 22:46:30 -0500
Organization: Department of Computer Science, University of Copenhagen
References: 05-11-094
Keywords: storage, optimize

"shrey" <shreyas76@gmail.com> writes:




> 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.


As John said, it depends on your language and your compiler. But in
C-like languages on modern CPU's (with modern compilers), local
variables tend to be register allocated (unless their address is
taken) where global variables are memory allocated. This means that
access to gobal variables usually require a memory reference, though a
function can sometimes make a temporary copy in a register and use
this for several accesses. It must write back before returning,
calling another function or accessing a variable that might be aliased
with the global variable.


Local variables sometimes also need to be stored across function
calls, though, but you can often reduce the need for this by expoiting
the caller-saves/callee-saves division of registers.


                Torben


Post a followup to this message

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