0% found this document useful (0 votes)
12 views3 pages

Ccs3207 Compiler Construction Assign - 2

The document outlines an optimization plan for a pathfinding algorithm, utilizing Loop-Invariant Code Motion (LICM) and Constant Propagation to enhance execution speed and reduce memory usage. It provides a comparison of the trade-offs associated with these techniques, highlighting their advantages and disadvantages for real-time systems. The conclusion emphasizes that while these optimizations are beneficial, careful profiling is essential to avoid performance degradation in resource-constrained environments.

Uploaded by

Walter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Ccs3207 Compiler Construction Assign - 2

The document outlines an optimization plan for a pathfinding algorithm, utilizing Loop-Invariant Code Motion (LICM) and Constant Propagation to enhance execution speed and reduce memory usage. It provides a comparison of the trade-offs associated with these techniques, highlighting their advantages and disadvantages for real-time systems. The conclusion emphasizes that while these optimizations are beneficial, careful profiling is essential to avoid performance degradation in resource-constrained environments.

Uploaded by

Walter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C026-01-0978/2022 WALTER ONYANGO

C026-01-1333/2019 Gibson Gichuru Githinji


C026-01-0940/2022 SAMWEL MUGO N.
C026-01-0984/2022 MBARUK ALI
C026-01-0957/2022-BRIAN CHEGE M.
C026-01-0972/2022 CLINT G.L. SIMIYU
CCS 3207 COMPILER CONSTRUCTION ASSIGNMENT

TASK 1: Synthesis (Create)


Step-by-Step Optimization Plan
Objective: Optimise the pathfinding algorithm by reducing redundant calculations, removing
dead code, and improving register usage.

Techniques Chosen

1. Loop-Invariant Code Motion (LICM):


Identifies computations inside loops that do not depend on loop variables.
Moves these computations outside the loop, reducing the number of iterations and
redundant operations.

2. Constant Propagation:
Replaces variables that have constant values with their actual constants at compile
time.
Eliminates unnecessary calculations and simplifies expressions.

Justification for Techniques


● LICM: Suitable because pathfinding algorithms often involve iterative structures (e.g.,
Dijkstra’s or A*). By reducing redundant calculations within loops, the execution
speed improves.
● Constant Propagation: Ideal for simplifying computations and removing dead code,
ensuring faster execution and smaller memory footprints.

Annotated Pseudo-Code Example

Original Code (Unoptimized):

def pathfinding_algorithm(a, b, c, x, n):


for i in range(n):
distance = a + b # Redundant computation
if x == 5: # Constant condition
y = 10
z = distance + c # Inefficient computation
process(z)

def process(value):
# Placeholder for some computational logic
print(f"Processing value: {value}")

Optimised Code with LICM and Constant Propagation:

def pathfinding_algorithm_optimized(a, b, c, x, n):


# Loop-Invariant Code Motion: Precompute values that don't depend on loop variables
distance = a + b

# Constant Propagation: Simplify constant condition if x is known


if x == 5:
y = 10 # Precompute y if x remains constant throughout

# Loop execution
for i in range(n):
z = distance + c # Use the precomputed invariant distance
process(z)

def process(value):
# Placeholder for some computational logic
print(f"Processing value: {value}")

● LICM:
○ distance = a + b moved outside the loop.
○ The computation distance = a + b is moved outside the loop since it
does not depend on the loop variable i.
○ This reduces redundant calculations in every iteration.

● Constant Propagation:
○ if (x == 5) evaluated at compile time if x is immutable.
○ The condition if x == 5 is evaluated at compile-time if x is known to be
constant. The y assignment is simplified outside the loop.

TASK 2: Evaluation (Judge)


Trade-Offs of Optimization Techniques

Aspect LICM Constant Propagation

Execution Speed Reduces redundant Simplifies expressions,


computations, enhancing reducing runtime
loop efficiency. computation.

Memory Usage May require additional Slightly reduces memory


memory for precomputed usage by removing unused
values. variables.

Maintainability Can make debugging harder Simplifies code readability


if moved computations are by reducing complexity.
misinterpreted.

Suitability for Real-Time Systems

● Advantages:
○ Both techniques significantly improve execution speed, critical for real-time
systems.
○ Constant Propagation reduces memory overhead, a common constraint in
embedded environments.

● Disadvantages:
○ LICM may increase register pressure due to additional precomputed values.
○ Maintaining optimised code could become challenging if algorithms change
frequently.

Conclusion:

Yes, these optimizations are suitable for real-time systems because they strike a balance
between speed and memory efficiency, two primary constraints in embedded environments.
However, careful profiling is necessary to ensure register pressure does not degrade
performance in resource-constrained systems.

You might also like