Review products and earn money. Learn more
Earn advertising credits on boddunan. Click here to learn more.
Articles Education Engineering Something About >> Register Allocation and Assignment In A Compiler

Something About >> Register Allocation and Assignment In A Compiler

 


Register Allocation and Assignment




Instructions involving only register operands are faster than those involving memory operands. On modern machines, processor speeds are often an order of magnitude or more faster than memory speeds. Therefore, efficient utilization of registers is vitally important in generating good code. This article presents various strategies for deciding at each point in a program what values should reside in registers (register allocation) and in which register each value should reside (register assignment).


One approach to register allocation and assignment is to assign specific values in the target program to certain registers. For example, we could decide to assign base addresses to one group of registers, arithmetic computations to another, the top of the stack to a fixed register, and so on. This approach has the advantage that it simplifies the design of a code generator. Its disadvantage is that, applied too strictly, it uses registers inefficiently; certain registers may go unused over substantial portions of code, while unnecessary loads and stores are generated into the other registers. evertheless, it is reasonable in most computing environments to reserve a few registers for base registers, stack pointers, and the like, and to allow the remaining registers to be used by the code generator as it sees fit.


Global Register Allocation




The code generation algorithm in  used registers to hold values for the duration of a single basic block. However, all live variables were stored at the end of each block. To save some of these stores and corresponding loads, we might arrange to assign registers to frequently used variables and keep these registers consistent across block boundaries (globally). Since programs spend most of their time in inner loops, a natural approach to global register assignment is to try to keep a frequently used value in a fixed register throughout a loop. For the time being, assume that we know the loop structure of a flow graph, and that we know what values computed in a basic block are used outside that block.


One strategy for global register allocation is to assign some fixed number of registers to hold the most active values in each inner loop. The selected values may be different in different loops. Registers not already allocated may be used to hold values local to one block .


This approach has the drawback that the fixed number of registers is not always the right number to make available for global register allocation. Yet the method is simple to implement and was used in Fortran H, the optimizing Fortran compiler developed by IBM for the 360-series machines in the late 1960s. With early C compilers, a programmer could do some register allocation explicitly by using register declarations to keep certain values in registers for the duration of a procedure. Judicious use of register declarations did speed up many programs, but programmers were encouraged to first profile their programs to determine the program's hotspots before doing their own register allocation.


 



Usage Counts




In this article we shall assume that the savings to be realized by keeping a variable x in a register for the duration of a loop L is one unit of cost for each reference to x if x is already in a register. However, if we use the approach into generate code for a block, there is a good chance that after x has been computed in a block it will remain in a register if there are subsequent uses of x in that block. Thus we count a savings of one for each use of x in  loop L that is not preceded by an assignment to x in the same block. We also save two units if we can avoid a store of x at the end of a block. Thus, if x is allocated a register, we count a savings of two for each block in loop L for which x is live on exit and in which x is assigned a value.


On the debit side, if x is live on entry to the loop header, we must load x into its register just before entering loop L. This load costs two units. Similarly, for each exit block B of loop L at which x is live on entry to some successor of B outside of L, we must store x at a cost of two. However, on the assumption that the loop is iterated many times, we may neglect these debits since they occur only once each time we enter the loop.


 



Code Generation by Tiling an Input Tree




A tree-translation scheme works as follows. Given an input tree, the templates in the tree-rewriting rules are applied to tile its subtrees. If a template matches, the matching subtree in the input tree is replaced with the replacement node of the rule and the action associated with the rule is done. If the action contains a sequence of machine instructions, the instructions are emitted. This process is repeated until the tree is reduced to a single node, or until no more templates match. The sequence of machine instructions generated as the input tree is reduced to a single node constitutes the output of the tree-translation scheme on the given input tree.


The process of specifying a code generator becomes similar to that of using a syntax-directed translation scheme to specify a translator. We write a tree-translation scheme to describe the instruction set of a target machine. In practice, we would like to find a scheme that causes a minimal-cost instruction sequence to be generated for each input tree. Several tools are available to help build a code generator automatically from a tree-translation scheme. Before considering general tree matching, we consider a specialized approach that uses an LR parser to do the pattern matching. The input tree can be treated as a string by using its prefix representation.


A code-generation grammar is usually highly ambiguous, and some care needs to be given to how the parsing-action conflicts are resolved when the parser is constructed. In the absence of cost information, a general rule is to favor larger reductions over smaller ones. This means that in a reduce-reduce conflict, the longer reduction is favored; in a shift-reduce conflict, the shift move is chosen. This "maximal munch" approach causes a larger number of operations to be performed with a single machine instruction.


There are some benefits to using LR parsing in code generation. First, the parsing method is efficient and well understood, so reliable and efficient code generators can be produced using the algorithms


Second, it is relatively easy to retarget the resulting code generator; a code selector for a new machine can be constructed by writing a grammar to describe the instructions of the new machine. Third, the quality of the code generated can be made efficient by adding special-case productions to take advantage of machine idioms.


However, there are some challenges as well. A left-to-right order of evaluation is fixed by the parsing method. Also, for some machines with large numbers of addressing modes, the machine-description grammar and resulting parser can become inordinately large. As a consequence, specialized techniques are necessary to encode and process the machine-description grammars. We must also be careful that the resulting parser does not block (has no next move) while parsing an expression tree, either because the grammar does not handle some operator patterns or because the parser has made the wrong resolution of some parsing-action conflict. We must also make sure the parser does not get into an infinite loop of reductions of productions with single symbols on the right side. The looping problem can be solved using a state-splitting technique at the time the parser tables are generated.


 


General Tree Matching




The LR-parsing approach to pattern matching based on prefix representations favors the left operand of a binary operator. In a prefix representation op El E2, the limited-lookahead LR parsing decisions must be made on the basis of some prefix of El, since El can be arbitrarily long. Thus, pattern  atching can miss nuances of the target-instruction set that are due to right operands. Instead prefix representation, we could use a postfix representation. But, then an LR-parsing approach to pattern matching would favor the right operand. For a hand-written code generator, we can use tree templates. As a guide and write an ad-hoc matcher.


For a code-generator generator, we need a general tree-matching algorithm. An efficient top-down algorithm can be developed by extending the stringpattern- matching techniques. The idea is to represent each template as a set of strings, where a string corresponds to a path from the root to a leaf in the template. We treat all operands equally by including the position number of a child, from left to right, in the strings.


 


 


 


User reviews

There are no user reviews for this listing.

To write a review please register or log in.
 
 
 
Author Articles
More from this author:
Something About >> How To Traverse XML (181 Hits)
Programming > Others
  How To Traverse XML   To begin this article, we take a look at how to make the transformations dangled in front of you throughout the last...
Transforming XML (172 Hits)
Programming > Others
Transforming XML If you are a backend systems developer or a systems architect, you should be seeing the value ofXML by now. A language that...
About->>Motherboard (551 Hits)
Computers & Technology > Hardware & Troubleshooting
Motherboard: The motherboard is, without a doubt, the primary component of the entire system. Without the support circuitry and functions this...
Something About>> Recordable Drives (140 Hits)
Computers & Technology > Hardware & Troubleshooting
Recordable Drives Optical devices such as CD –ROMs and DVD –ROMs are good data storage solutions. They hold a lot of data. But the biggest...
Something About >> How To Traverse XML(Part2) (200 Hits)
Computers & Technology > New Technologies
The Document Object Model (DOM) The Document Object Model, unlike SAX, has its origins in the World Wide Web Consortium (W3C). Whereas SAX...
INTRODUCTION TO INTERNET (295 Hits)
Computers & Technology > New Technologies
INTRODUCTION TO INTERNET Internet is a worldwide network that is a widely used to connect universities, government offices, companies and private...
Get Knowledge About WEB BROWSER (290 Hits)
Computers & Technology > New Technologies
WEB BROWSERS Most browsers have a point and click interface-the browser displays information on the computer’s screen and permit a user to...
Related Articles
Related Articles:
Compiler & Phases of Compiler (4444 Hits)
vijay_u
Miscellaneous > General Reference
Before we know about the phases of compiler,we must know waht is compiler. Compiler : Compiler is a simple program  which reads a...
Compiler Construction Tools (1095 Hits)
vijay_u
Education > Engineering
There are many compiler construction tools available to enhance the compiler performance.Some of them 1.Parser Generators: These generators...
Understanding how compiler was developed (316 Hits)
vinay
Programming > C & C++
Hello everyone, This is my first article on this site however there are lots of computer related articles which I have written for other resources....
FAT - File Allocation Table (217 Hits)
mkiran
Computers & Technology > Hardware & Troubleshooting
File Allocation Table (FAT) is a partially patented file system developed by Microsoft for DOS and was the primary file system for consumer versions...
Assignment and Expression (364 Hits)
santoshkumarsingh
Programming > C & C++
//////////////////////////////////////// // The main() function. //////////////////////////////////////// int main() {     // Declare three...
Something About Flow Charts (561 Hits)
lohitseth
Education > Computer Science
Flow Charts An algorithm can be described in many ways. We have already studied how to represent an algorithm in an English-like language (pseudo...
Something About >> How To Traverse XML (181 Hits)
rohit.4cse@gmail.com
Programming > Others
  How To Traverse XML   To begin this article, we take a look at how to make the transformations dangled in front of you throughout the last...
Latest Articles
Latest Articles:
Transport System In India (6 Hits)
sajeetharan
People & Places > Social Life
It would be nice to discuss the public transport facilities that we have in our cities.Let me share How much we are satisfied and how we we are...
Device driver (4 Hits)
Binief
Computers & Technology > New Technologies
A device driver represents a computer programme that admits higher-level PC programs to communicate with a machine. It checks the interractions...
Writing an article effectively (3 Hits)
Binief
Miscellaneous > General Reference
Do you would like to know how you are able to compose an article in under thirty minutes? Whenever you would like to be more creative in the...
Be proud of our 'Sardars' ... Must read! (5 Hits)
sunaina badhwar
Entertainment > Jokes & Humour
We all love those santa-banta jokes.But do you know that sikhs are one of the most hard-working and prosperous communities of the world. After...
Asus J 502 (48 Hits)
sunnyrsingh
Reviews > Mobile Phones
The Asus J 502 is a Slider-Touchscreen phone comes with 24MB internal memory and memory can be expanded by microSD card. It has 3 megapixel camera...
Education System In India (19 Hits)
sajeetharan
Education > Careers
Indian Education system is going through progess period and will emerge to be great.Its not easy to spread literacy in a country with population...
Asus Z801 (45 Hits)
sunnyrsingh
Reviews > Mobile Phones
The Asus Z801 is a Clamshell-Touchscreen phone comes with 64MB internal memory and memory can be expanded by microSD card. It has 2 megapixel camera...
Most Read Articles
Most Read Articles:
Money making programs on boddunan.com (10180 Hits)
bnadmin
Boddunan.com is not only a platform to share knowledge and learn from the articles posted by other members but also provides an oportunity to make...
CANDICE BERGEN (9745 Hits)
xaskoiking
People & Places > Celebrities
Hai friends in this article let us discuss about the 5 time Emmy award winner and 2 time Gold Globe award winning American Actress and model CANDICE...
Understanding the Anatomy of Abdominal Muscles (8278 Hits)
soni2006
Education > Medicine & Surgery
To do any exercise correctly you need to know which muscles are involved and the movements they perform. Anatomically locating these...
History of Transportation – Your Complete Guide (8227 Hits)
soni2006
People & Places > Creators
Definition of transportation Transportation is a means of moving people or goods from one place to another. The modern commercial...
Origami (8152 Hits)
ktkps
Miscellaneous > General Reference
We all would at some point in our childhood (and in college) folded papers to make rocket and other things.But we do not know the name for that...
TOP 10 BICEPS EXERCISES PART 1 (7922 Hits)
sapu
Health & Fitness > Yoga & Excercises
Hi friends, well this article is exclusively for those men and may be, who knows even some girls who want to shape up their biceps by simple...
Jallianwala Bagh Massacre (7336 Hits)
xaskoiking
Miscellaneous > General Reference
#) More than 379 dead and 1500 plus were injured..... These are the number of victims of the cruel massacre by  a cruel man called dyer #)Shock...

Trackback(0)

TrackBack URI for this entry

Comments (2)

Subscribe to this comment's feed
...
kavishanigam
Good work .. Its useful. keep it up
kavishanigam , July 20, 2010
...
kavishanigam
Very good to share these experiences
kavishanigam , July 23, 2010

Write comment

smaller | bigger
security image
Write the displayed characters

busy

Sponsored Links

Leader Board

This Week
sajeetharan (2698 Points)
neetu020784 (1708 Points)
sunnyrsingh (892 Points)
jayen (830 Points)
bolisettypradeep (800 Points)
kdeepti (800 Points)
kumaresh (744 Points)
jasmeetsingh (650 Points)
ra_k (597 Points)
kalyani (585 Points)
Last
aasthagupta36 (1932 Points)
neetu020784 (1661 Points)
vijuagrawal (1596 Points)
sunnyrsingh (1263 Points)
kdeepti (1120 Points)
jobin (888 Points)
kalyani (836 Points)
ra_k (634 Points)
kumaresh (516 Points)
kavishanigam (428 Points)
Updated every 1 hour(s)

You are not logged in.

Boddunan Stats

  • 6819 registered
  • 8 today
  • 39 this week
  • 549 this month
  • Last: emad981

Subscribe Newsletter

Server Time