100% found this document useful (1 vote)
98 views

CS 404 Introduction To Compiler Design: Lecture 12 + 13 Ahmed Ezzat

This document summarizes lecture notes on compiler design topics including code optimizations, runtime environments, target code generation, and related tools. Specifically, it discusses techniques for optimizing intermediate representations and basic blocks, how programs are organized in memory with code, static data, stacks and heaps, and how runtime stacks are used to save state during function calls through activation records.

Uploaded by

maverick_33
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
98 views

CS 404 Introduction To Compiler Design: Lecture 12 + 13 Ahmed Ezzat

This document summarizes lecture notes on compiler design topics including code optimizations, runtime environments, target code generation, and related tools. Specifically, it discusses techniques for optimizing intermediate representations and basic blocks, how programs are organized in memory with code, static data, stacks and heaps, and how runtime stacks are used to save state during function calls through activation records.

Uploaded by

maverick_33
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

CS 404

Introduction to Compiler Design

Lecture 12 + 13
Ahmed Ezzat

1 CS 404 Ahmed Ezzat


Remaining Topics

 Code optimizations
 Runtime environment
 Target code generation
 Assemblers, linkers, loaders, debuggers,
profilers

 Readings for this lecture: Sec 9.4, Sec 10.1,


10.2, 10.3

2 CS 404 Ahmed Ezzat


Code Optimizations

 Goal: improve code to be faster and/or smaller (more


efficient)
 Requirements
– Improved code has same output
– Improved code has same side effect
– Improved code is as correct as original
 Example tasks
– Eliminate redundant computations
– Efficient use of registers
– Eliminate loops or more computation out of loops

3 CS 404 Ahmed Ezzat


When Can Improvements Occur?

 Programmar
– Use better algorithms (n vs n^2)
– Move computation out of loops
– Remove redundant computations
 Intermediate representation (machine
independent)
 Target code (machine dependent)

4 CS 404 Ahmed Ezzat


Intermediate Representation

 The best place for optimizations


 Each instruction is simple
 By analyzing sequence of IR, one can
accumulate information about definitions and
use of names
 Can detect patters and match them, replace
them with better code

5 CS 404 Ahmed Ezzat


Control Flow Graph

 Basic blocks are nodes


 Flow of control as edges
 Capture the dynamic structure (returns,
branches, jumps)

6 CS 404 Ahmed Ezzat


Basic Blocks

 A list of instructions
 Once the block is entered, each instruction is
executed exactly once
 Only the first statement can be reached from
outside the block (no jumping in)
 Execution continues from first instruction to
last in the block (no jumping out)

7 CS 404 Ahmed Ezzat


Basic Blocks

 Begins (Leader statements)


– Procedure entry point
– Label (branch target)
– Instruction after branch or return
 Ends
– Jump
– Branch (conditional or unconditional)
– Return

8 CS 404 Ahmed Ezzat


Local Optimizations Within Basic
Blocks

 Rearrange code or replace them with


different code that does the same thing
 Reordering within a block is okay because
we know everything gets executed exactly
once
 Some local techniques generalize across
blocks

9 CS 404 Ahmed Ezzat


Structure-Preserving Transformation
on Basic Blocks

 Common sub-expression elimination


– A common sub-expression E means that E was
previously computed, and the values of variables
in E have not changed since the previous
computation
 Dead code elimination
– Code that are never reached
– Code that does not affect the result of the
program

10 CS 404 Ahmed Ezzat


Structure-Preserving Transformation
on Basic Blocks (2)

 Renaming of temporary variables


– Make normal form blocks, blocks that with no re-
use of temporary variables
– Help to decide whether two blocks are equivalent
– Allow interchange of statements
 Interchange of two independent adjacent
statement

11 CS 404 Ahmed Ezzat


More Optimization Techniques

 Constant folding and propagation


– Detect and combine values that will be constant
 Algebraic simplification and re-association
– Use law of arithmetic to reorder or simplify
operations
– Exploit arithmetic and logical identities

12 CS 404 Ahmed Ezzat


More Optimization Techniques (2)

 Reduction in strength
– Some operations are faster than others
 Multiplications -> additions
 Multiply or divided by 2 -> shift left or right
– Be careful with correctness
 Overflow behavior preserved
 Other side-effect of operations (e.g., interrupts)

13 CS 404 Ahmed Ezzat


Loop Optimizations

 Code motion: decrease the amount of code in a loop


– A loop-invariant computation: an expression that yields the
same result independent of the loop
– Move loop-invariant computation out of the loop
 Induction variables: every time the value of a
variable change, the value of the other variable
change accordingly
– Reduce of strength of induction variables
– Eliminate induction variables

14 CS 404 Ahmed Ezzat


How a Program Run?

 Where to put code and data?


 How to access code and data?
 How to pass parameters to a procedure or
function?
 How to return results?

15 CS 404 Ahmed Ezzat


Program Related Definitions

 Procedure: a program is made of procedures


– The entire program is a procedure (main)
– A function is a procedure with return values
– Procedure calls can be nested
– A procedure has a name, a body and some
parameters
 Formal parameters: those in definition
 Actual parameters: those passed, arguments

16 CS 404 Ahmed Ezzat


More Program Related Definitions

 A program activation: an execution


 Life time of an activation: sequence of steps
from the first statement to the last statement
 Recursive: new activation can begin before
an earlier activation of the same procedure
has ended
 Activation tree: each node is an activation of
a procedure, root is main

17 CS 404 Ahmed Ezzat


Control Stack and Scope

 Control stack: one way to implement


procedure activations
 Scope: a portion of a program that a name
declaration applies
– Local name: within a procedure
– Non-local name: can use outside of a procedure

18 CS 404 Ahmed Ezzat


Storage Organization

 How a program is laid out in memory


– Target code
– Data objects (static, dynamic)
– Control stack
 Typical layout
– Code
– Data (static)
– Stack
– …
– Heap (dynamic)

19 CS 404 Ahmed Ezzat


Code Segment

 Actual machine instructions


– Arithmetic/logical instructions
– Comparison instructions
– Branches (short distance)
– Jumps (long distance)
– Load/store registers
– Data movements
– Constant manipulations
 Code segment is often write-protected, so running
code cannot overwrite itself

20 CS 404 Ahmed Ezzat


Data Segment

 The data objects whose size is known at


compile time
 The data objects whose lifetime is the full run
of the program, not just during a function
invocation

21 CS 404 Ahmed Ezzat


Two Types of Data in the Data
Segment

 Static Data: things won’t change, can be


write protected
– E.g., string literals, arithmetic literals
 Global Data: global variables
– E.g., “static variables” in C, which value retains
the same when used next time in a different
function invocation

22 CS 404 Ahmed Ezzat


Heap: Dynamic Data

 Data created by malloc() or new()


 Size not known at compile time
 Heap data lives until de-allocated or until
program ends
 Garbage collection: Automatically de-allocate
the data no longer used

23 CS 404 Ahmed Ezzat


Runtime Stack

 Data used for function invocation


 Basic idea: when a call occurs, everything
about the current activation is saved on the
stack
– Register values, program counters, data
 When a function return, restore the data
saved on the stack

24 CS 404 Ahmed Ezzat


Activation Records

 Returned value
 Actual parameters
 Optional control link: to parent procedure
 Optional access link: non-local data held elsewhere
 Saved machine status: registers, program counters
 Local data: variables declared local to a function
 Temporary data: used by target code

25 CS 404 Ahmed Ezzat

You might also like