Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

What is Spilling

  • Last Updated : 12 Aug, 2021

Introduction :
In a computer, all the instructions and data are stored in main or primary memory.  During execution, they have to be transferred to the processing unit, also known as the CPU, where operations are performed, depending on the instruction and if there is any result, it is sent back to the memory. But where are these instructions and data stored inside the CPU during execution?  The answer is a Register.

A register is a hardware unit that acts as a storage area that can be quickly accessed by the computer’s processor. They mainly store the address of an instruction, the instruction itself, the operands, as well as the result of an operation, which can later be loaded into the memory. The variables which are used in high-level programming languages are stored in the registers, or rather they are allocated to a particular register during the execution of the program.

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.

 In a programming language, there might be a number of variables in use, which are required to be stored in registers for faster access, making the program execute fast. It is the job of the compiler to decide how to allocate variables to the registers. Not all the variables are allocated to a register, but only the ones that are in use.



Problems without spilling :
Now here two problem arises – 

  1. In a computer, the number of registers is limited, since they are hardware units, making it impossible to increase or decrease the number of registers once the chip is fabricated.
  2. No two or more variables can be allocated space in the same register at a time.

The characteristic of an ideal processor is to execute an increasing number of instructions per cycle. Hence more space is required to store the variables and perform operations on them. As mentioned, the register space is limited. This leads to register allocation failures. Hence it is the job of the compiler to make use of register as well as memory space. 

If there are not enough registers to hold the variables. then some variables are moved to the main memory, to make space for the variable which requires a register. This is known as a register spill.

What is Spilling?
Formally speaking, spilling is a technique in which, a variable is moved out from a register space to the main memory(the RAM) to make space for other variables, which are to be used in the program currently under execution. 

Why Spilling?
Spilling is done in order to satisfy the below conditions –

  1. Avoid Register Allocation Failures – 
    This happens, when there is a limited number of registers to hold the live variables, hence they are swapped between register and memory.
  2. Parameter Passing in Function – 
    Parameters are arguments that are passed to a function. When they cannot be passed into registers, they are stored in memory locations.
  3. Dynamic Allocations – 
    A programmer may need to add names, which are generated during the run-time. As registers are statically named, the compiler relies on the memory for dynamic name generation.

A fourth problem exists, during function calls. The body of the called function accesses the entire set of registers, due to which a number of saves and restore occurs. To overcome the overhead, memory references are made, which is possible using spilling.

Advantage of Spilling –
It has few advantages, they are –

  1. The biggest advantage is an increase in the performance of the hardware.
  2. The access time is lower than that of a cache(for small spills).

Example –
Let us understand the whole process with help of an example. Consider a simple expression:

R = (A+B)*(C+D)

The corresponding One Address instruction is the following:-

1. LOAD A     // ACC = M[A]
2. ADD B      // ACC = ACC + M[B]
3. STORE T    // M[T] = ACC
4. LOAD C     // ACC = M[C]
5. ADD D      // ACC = ACC + M[D]
6. MUL T      // ACC = ACC * M[T]
7. STORE R    // M[R] = ACC

In this example, at instruction number 3, the value stored at the accumulator is moved to the memory and stored there to make space for new variables. This is an example of spilling.

My Personal Notes arrow_drop_up
Recommended Articles
Page :