Compiler Unit 5
Compiler Unit 5
• But more often, the redundancy is a side effect of having written the
program in a high-level language.
1.1 Causes of Redundancy
• In most languages (other than C or C++, where pointer arithmetic is
allowed), programmers have no choice but to refer to elements of an
array or fields in a structure through accesses like A[i][j] or X -> f 1.
• As a program is compiled, each of these high-level data-structure
accesses expands into a number of low-level arithmetic operations,
such as the computation of the location of the (i, j)th element of a
matrix A.
• Accesses to the same data structure often share many common low-
level operations. Programmers are not aware of these low-level
operations and cannot eliminate the redundancies themselves.
1.1 Causes of Redundancy
• As a result of this modification, the new program has to make only 20 iterations, instead of 100.
• Afterwards, only 20% of the jumps and conditional branches need to be taken, and represents, over
many iterations, a potentially significant decrease in the loop administration overhead.
3.3 Loop fission and fusion
• loop fission (or loop distribution) is a compiler optimization in which
a loop is broken into multiple loops over the same index range with
each taking only a part of the original loop's body.
• The goal is to break down a large loop body into smaller ones to
achieve better utilization of locality of reference.
• The variable used in the inner loop switches to the outer loop, and
vice versa.
for j from 0 to 20
for i from 0 to 10
a[i,j] = i + j
3.5 Loop interchange
• The utility of loop interchange
• The major purpose of loop interchange is to take advantage of the CPU cache when
accessing array elements.
• When a processor accesses an array element for the first time, it will retrieve an
entire block of data from memory to cache.
• That block is likely to have many more consecutive elements after the first one, so
on the next array element access, it will be brought directly from cache (which is
faster than getting it from slow main memory).
• Cache misses occur if the contiguously accessed array elements within the loop
come from a different cache block, and loop interchange can help prevent this.
3.5 Loop interchange
• The effectiveness of loop interchange depends on and must be considered in
light of the cache model used by the underlying hardware and the array
model used by the compiler.
• In C programming language, array elements in the same row are stored
consecutively in memory (a[1,1], a[1,2], a[1,3]) ‒ in row-major order.
• On the other hand, FORTRAN programs store array elements from the same
column together (a[1,1], a[2,1], a[3,1]), using column-major.
• Optimizing compilers can detect the improper ordering by programmers and
interchange the order to achieve better cache performance.
UNIT V: CODE OPTIMIZATION
Topics we are going to cover
• The compiler must cooperate with the operating system and other
systems software to support these abstractions on the target
4. Runtime Environments
• To do so, the compiler creates and manages a run-time environment
in which it assumes its target programs are being executed.
• This environment deals with a variety of issues such as the
• layout and allocation of storage locations for the objects named in the source
program,
• the mechanisms used by the target program to access variables,
• the linkages between procedures,
• the mechanisms for passing parameters,
• and the interfaces to the operating system, input/output devices, and other
programs.
UNIT V: CODE OPTIMIZATION
Topics we are going to cover
• These areas are dynamic; their size can change as the program
executes.
• For example, C has the functions malloc and free that can be
used to obtain and give back arbitrary chunks of storage.