Code Optimization Techniques

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following is the primary goal of code optimization?

  • To improve execution speed and reduce memory usage. (correct)
  • To reduce the number of lines of code.
  • To make the code more readable for developers.
  • To add more features to the program.

Code optimization is a mandatory phase in the compilation process.

False (B)

What are the two main types of code optimization based on their scope?

local and global

A sequence of consecutive three-address statements, which may be entered only at the beginning and executed in sequence without halting or branching, is known as a ______ block.

<p>basic</p>
Signup and view all the answers

Match the following optimization techniques with their descriptions:

<p>Common Subexpression Elimination = Avoiding re-computation of expressions by using previously computed values. Copy Propagation = Replacing the use of a variable with the variable it was assigned from. Dead Code Elimination = Removing code that does not affect the program's output. Constant Folding = Evaluating constant expressions at compile time.</p>
Signup and view all the answers

Which of the following is NOT a criterion for code optimization?

<p>Reducing the number of functions in the code. (D)</p>
Signup and view all the answers

Machine-dependent optimizations are applicable to any machine architecture.

<p>False (B)</p>
Signup and view all the answers

What is the role of 'leader statements' in identifying basic blocks?

<p>start of new basic block</p>
Signup and view all the answers

The pictorial representation of control flow analysis in a program, showing the relationship among basic blocks, is called a ______ graph.

<p>flow</p>
Signup and view all the answers

Match the types of transformations with their scope:

<p>Local Optimization = Small segments of the program (basic blocks). Global Optimization = Large segments of the program (loops, procedures).</p>
Signup and view all the answers

What is an 'induction variable' in the context of loop optimization?

<p>A variable modified in lock-step with a loop index. (D)</p>
Signup and view all the answers

In 'loop unrolling', simple code is created which increases the number of times the loop executes to improve optimization.

<p>False (B)</p>
Signup and view all the answers

What is the primary goal of 'strength reduction' in loop optimization?

<p>replace with cheaper operations</p>
Signup and view all the answers

If the operations performed can be done in a single loop, then merge or combine the loops, this is known as loop ______.

<p>jamming</p>
Signup and view all the answers

Match the following loop optimization sub-techniques with their descriptions:

<p>Code Motion = Moving loop-invariant code outside the loop. Induction Variable Elimination = Removing redundant induction variables. Strength Reduction = Replacing expensive operations with cheaper ones.</p>
Signup and view all the answers

What is the main advantage of using Directed Acyclic Graphs (DAGs) in optimization?

<p>Implementing transformation on basic block. (B)</p>
Signup and view all the answers

In a DAG, a leaf node represents operators, while an interior node represents identifiers, names and constants.

<p>False (B)</p>
Signup and view all the answers

What is the primary purpose of 'peephole optimization'?

<p>improve target code</p>
Signup and view all the answers

Removing code that is never accessed because of programming constructs refers to removing ______ code.

<p>unreachable</p>
Signup and view all the answers

Match the following peephole optimization techniques with their descriptions:

<p>Redundant Load/Store = Eliminating unnecessary memory access instructions. Flow of Control Optimization = Removing or simplifying unnecessary jumps. Algebraic Simplifications = Replacing complex expressions with simpler ones.</p>
Signup and view all the answers

Which factor has a significant effect on the difficulty of constructing a good code generator?

<p>The instruction set architecture of the target machine. (C)</p>
Signup and view all the answers

Producing absolute machine language as output has the disadvantage that it cannot be placed in a fixed location in memory and immediately executed.

<p>False (B)</p>
Signup and view all the answers

Asides from generating output, what information from symbol table does code generator needs?

<p>name and width</p>
Signup and view all the answers

The code generator must map the IR (Intermediate Representation) into a code ______ that can be executed by the target machine.

<p>sequence</p>
Signup and view all the answers

Match the following architecture types with their appropriate characteristics:

<p>RISC = Many registers, three-address instructions. CISC = Fewer registers, two-address instructions. Stack Based = Operations performed using a stack.</p>
Signup and view all the answers

Regarding memory management, what are being mapped to addresses?

<p>Variable names (D)</p>
Signup and view all the answers

The cost of an instruction does not take into account the source and destination addressed mode.

<p>False (B)</p>
Signup and view all the answers

Name at least 2 instruction code patterns.

<p>MOV, ADD</p>
Signup and view all the answers

With assembly-language for MODE set to absolute the address is M and added COST should be ______.

<p>1</p>
Signup and view all the answers

Match the following architectures with address costs with assembly MODE and `FORM:

<p>Immediate = #C Register = R Indirect Indexed = <em>C</em>(R)</p>
Signup and view all the answers

Which data structure implemented transformation on basic blocks?

<p>DAG (A)</p>
Signup and view all the answers

Is code generation algorithm uses descriptors to keep of variables and constants.

<p>False (B)</p>
Signup and view all the answers

Name the data descriptor, besides register descriptor, for code generation.

<p>address descriptor</p>
Signup and view all the answers

If a register is ______, move its contents into some memory and then uses the register.

<p>already occupied</p>
Signup and view all the answers

Match the following actions with correct register conditions for getReg algorithms:

<p>Y in R = Uses Y register If some R available = Uses R Minimal number of load = Chooses a register</p>
Signup and view all the answers

Flashcards

Code Optimization

Transformations applied to make code faster or use less space, without changing the program's meaning.

Optimizing Compiler

A compiler that includes optimization techniques.

Optimization Phase

A phase in compilation that aims to improve code efficiency without changing its functionality.

Criteria for Optimization

Optimization should preserve the original program's meaning, improve efficiency, and be worth the computational effort.

Signup and view all the flashcards

Machine Dependent Optimization

Optimization tailored to a specific machine architecture.

Signup and view all the flashcards

Machine Independent Optimization

Optimization applicable across different machine architectures.

Signup and view all the flashcards

Local Optimization

Optimization within a small code segment.

Signup and view all the flashcards

Global Optimization

Optimization across larger code segments like loops and functions.

Signup and view all the flashcards

Basic Block

A sequence of consecutive statements executable in order without halting or branching.

Signup and view all the flashcards

Leader Statement

First instruction in a basic block. Usually target of a branch or the statement following a branch.

Signup and view all the flashcards

Flow Graph

Pictorial representation of the control flow in program.

Signup and view all the flashcards

Common Subexpression

Identical expressions whose computation is redundant.

Signup and view all the flashcards

Copy Propagation

An assignment that duplicates a variable's value.

Signup and view all the flashcards

Dead Code

Code that doesn't affect program output.

Signup and view all the flashcards

Constant Folding

Evaluating constant expressions at compile time.

Signup and view all the flashcards

Code Motion

Moving code outside a loop.

Signup and view all the flashcards

Induction Variables

Variables whose values change predictably with each loop iteration.

Signup and view all the flashcards

Reduction in Strength

Replacing expensive operations with simpler ones.

Signup and view all the flashcards

Loop Jamming

Merging multiple loops into one.

Signup and view all the flashcards

Loop Unrolling

The code to replace it's loop form with another code that can reduce execution number of times.

Signup and view all the flashcards

Unreachable Code

Eliminating unreachable parts of the code.

Signup and view all the flashcards

Interior Node

Node represents operators

Signup and view all the flashcards

Peephole Optimization

Technique used to improve target code by examining short sequences of instructions.

Signup and view all the flashcards

Code Generation

Last state happens on compiler model: generator code.

Signup and view all the flashcards

Memory Management

Mapping of variables to addresses.

Signup and view all the flashcards

Instruction Selection

Process of mapping IR into machine code.

Signup and view all the flashcards

Register Allocation

Deciding what values to store in each registor.

Signup and view all the flashcards

Register Descriptor

Track what each register currently holds.

Signup and view all the flashcards

Address Descriptor

Track the location of a variable's current value.

Signup and view all the flashcards

Study Notes

Code Optimization and Generation

  • Code optimization involves transformations to make code faster or use less space.
  • Optimizing compilers perform these optimizations.
  • Code optimization is optional and should not alter the program's meaning.

Need for Optimization

  • Compiler-produced code might not be perfect regarding speed and space.
  • Manual optimization is more time-consuming.
  • Programmers might lack knowledge of machine-level details.
  • Advanced architectures benefit from optimized code.
  • Optimization improves code reusability and maintainability.

Criteria for Code Optimization

  • Preserve program meaning without changing the output or causing errors; instead, improve efficiency
  • Worth the effort; optimization benefits must justify the effort.

Optimization Locations

  • Source code
  • Intermediate code
  • Target code

Types of Optimization

  • Machine-dependent optimization runs on a specific machine.
  • Machine-independent optimization can be used on any machine.

Optimization Phases

  • Local optimization is applied to basic blocks, small code segments executed sequentially.
  • Global optimization is applied to larger segments like loops.
  • Local optimization should precede global optimization.

Basic Block

  • A basic block is a sequence of consecutive three-address statements entered only at the beginning.
  • Statements are executed in sequence without halts or branches.

Identifying Basic Blocks and Flow Graphs

  • Leader statements need to be found to identify basic blocks.
  • The first three-address instruction is a leader.
  • Jump targets are leaders.
  • Instructions following conditional or unconditional jumps are leaders.
  • A basic block consists of a leader and subsequent instructions up to the next leader or the end of the program.
  • Flow graphs are pictorial representations of control flow, showing relationships between basic blocks
  • Nodes represent basic blocks, and edges represent control flow; G = (N, E) where N is basic blocks and E is control flows.
  • B1 is the predecessor of B2, and B2 is the successor of B1 if the control transfers to the first statement of B2

Function-Preserving Transformations

  • Compilers can improve programs without altering the function.
  • Common subexpression elimination avoids re-computing expressions.
  • Copy propagation replaces variables with their assigned values.
  • Dead code elimination removes unused code.
  • Constant folding evaluates constant expressions at compile time.

Common Subexpression Elimination (CSE)

  • An expression E is a common sub-expression if it was previously computed and variables in E have not changed.
  • Re-computing the expression can be avoided by using the previously computed value.

Copy Propagation

  • Assignments of the form f = g are copy statements.
  • Copy propagation uses g for f whenever possible after the copy statement.
  • It involves using one variable instead of another.

Dead-Code Elimination

  • A variable is live if its value is used subsequently.
  • Otherwise, it's dead.
  • Dead code includes statements that compute unused values.
  • Dead code may result from previous transformations

Constant Folding

  • Constant expressions are evaluated at compile time and the result replaces the original expression.
  • Improves the run time performance.

Loop Optimization

  • Loop performance can be improved by reducing the number of instructions in inner loops.
  • Code motion decreases the amount of code in a loop.
  • Loop-invariant code motion moves loop-invariant code outside the loop.
  • Induction variables are variables whose values remain in lock-step; every time the one value decreases by one, the other decreases by 4, such an example is called induction variables

Reduction In Strength

  • In loops with two or more induction variables, all but one can be potentially eliminated through induction-variable elimination.

Loop Jamming

  • Merging or combining loops.

Loop Unrolling

  • Replacing the loop with simple code, to reduce repetition and potentially be more efficient.

Optimization of Basic Blocks

  • Structure-preserving transformations
  • Algebraic transformations
  • Structure preserving includes dead code elimination, copy propagation, common sub expression elimination, strength reduction, constant folding and interchange of independant statements
  • Copy Propagation includes Variable Propagation and Constant Propagation,
  • Strength Reduction: Replace expensive statement/ Instruction with cheaper ones
  • Algebraic simplification includes reduction in strength

Directed Acyclic Graph (DAG)

  • DAG is an abstract syntax tree with a unique node for each value.
  • DAG is used for transformation on basic blocks.
  • Constructed from three address code.
  • Common subexpressions can be detected when adding a new node.

Applications of DAG

  • Determine common subexpressions.
  • Determine names used and computed outside the block.
  • Simplify quadruples by eliminating common sub expressions and not doing unnecassary assignments

Rules for DAG Construction

  • Leaf nodes represent identifiers, names, and constants.
  • Interior nodes represent operators.
  • A node is only created if one with the same children does not exist.

Machine Independent Optimization

  • all discussed optimization is machine independant

Machine Dependent Optimization

  • Machine dependent optimization is done on the target code.
  • Peephole optimization is a simple technique for locally improving target code.
  • Peephole optimization improves the performance of the target program by examining a short sequence of the instructions and replacing the instructions by a shorter, faster sequence.

Peephole Optimization Characteristics

  • Redundant Load/Store
  • Remove Unreachable Code
  • Flow of Control Optimization
  • Algebraic Simplifications
  • Use of Machine Idioms

Unreachable Code

  • Part of the program that is never accessed due to the programming constructs.

Flow of Control

  • There are instances in a code where the program control jumps back and forth without performing any significant task.
  • These jumps can be removed.

Algebraic Simplifications

  • It represents another important class of optimizations on basic blocks
  • Use Machine Idioms Some target instructions have equivalent machine instructions to perform some type of operations. Hence we can replace these target instructions with the corresponding machine instructions to improve the efficiency

Code Generation

  • The final compiler phase is the code generator.
  • It takes an intermediate representation of the source program and produces an equivalent target program.

Input to Code Generator

  • Input consists of the intermediate representation and information in the symbol table.
  • Intermediate language choices include postfix notation, three-address representation.

Target Programs

  • The code generator's output can be absolute machine language, relocatable machine language, or assembly language.
  • Absolute machine language can be placed in a memory with a fixed notation
  • An assembly language program as output makes the process of code generation somewhat easier
  • The target machine architecture affects code generation difficulty.
  • The most common target machines are RISC, CISC, and stack based.
  • RISC machines have many registers, three-address instructions, and simple instruction sets.
  • CISC machines have few registers, two-address instructions, and varied addressing modes.
  • Stack-based machines use the stack for operations.

Instruction Selection

  • The complexity of this mapping is determined by the level of the IR, the nature of the instruction-set architecture, and the desired quality of the generated code
  • Uniformity and completeness of the instruction is important factors
  • If we do not core about the efficiency of the target program, instruction selection is straightforward

Moving the Source and Destination

  • These locations can be registers and memory locations with address modes.

Simple Code Generator

  • It uses registers to store operands of three-address statements.
  • It uses descriptors to keep track of register contents and name addresses.
  • A Register Descriptor keeps track of what is currently in each register and is consulting whenever a new register is needed.
  • An Address Descriptor keeps track of the location where the current value of the name.

Code Generation Algorithm

  • It takes a sequence of three-address statements as input.
  • A function getreg is called to find the location for the result of y op z
  • The instruction OP z', L is generated., where z' is the location of z.

Considerations for using Register

  • If a variable Y is already in register R, it uses that register
  • If some register R is available, it uses that register
  • If above options are not possible, it chooses a register that requires minimal number of load and store instructions.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser