CD Home Assignment Co-4 TO0OAIN>
\. Construct DAG for the Following Code
Given the Sequence of operations:
65S
Copy code
a=b+e
b=a-d
c=bte
dza-d
A Divected Acyclic Gragh CAG) Lor these operations visually represents the
Computation And degendencies Among variables. I+ helys in Identifying common
subexpressions, variables lifetimes, And opportunities for optimization such aS reuse
of computations.
For this code sequence, constructing a DAG involves identifying, nedes for opevartions
G4, 2 nd edaps that connect these nodes to their operands (a, lo, ¢, d). Since it'5 a
Lit complex to visually describe a DAG have without drawing it, Til outline the stees
And key goints.
Stat with nodes for the initial values of lo, c, And d (let's ASSUME they hAVE Some
Witial values before these operations).
= b +c Create a + node with edaes From bb and ¢ to this mode; the result goints
toa.
lo =a - &: Create a - node with edges from a and d to this node; the result goints
to bo.
C= +c; This creates another + node. Since bo hAS A now value now, Connect this lo
And the old ¢ to the naw + node; the vasult goints to the now ¢
d= 0 - d; Similar to Stee % but now it's assigning to d. Since a’ value 6 from Steg1 and d has its initial valve, connect these to A - node; the result points to the
now d
1. Princigal Sources of Optimization with Exameles
Optimization in Compilers Aims to improve code oy making it run faster, use 155
memory, ov both, Princigal Sources include:
Constant Folding And Propagation. Evaluate constant exgressions at compile time.
For examele, veglacing x = > + 4 with x= 7
Dead Code Elimination: Remove code that does not affect the Program outcome
For exawmgle, removing An if statement that always evaluates to false.
Looe Optimization: Tansform logs to reduce overhead. For instance, loop Unvolling,
decreases the nimber of iterations loy executing more than one iteration Ree log
cycle.
Code Motion. Moves code outside of loops if it doesn't depend on the loog itselt,
xeducing the Amount of computation inside the loog
%, Optimization of Basic Blocks
A basic block i6 a sequence of instructions with no ranches (excegt at the end?
And no Code that cOm be ovOnched into (except at the oeqinningp. Optimizations on
loasic lolocks can include:
Local Constant Propagation: Within a loleck, i€ a variable is assigned a constant
value, use that value divectly in Mesequent Instructions.
Common Subexgression Elimination. T£ an expr2ssion has alveady been computed in a
loasic block and its oferAnds have not changed, reuse the result instead of
vecomputina,
4. Storage Allecation Strateaies for Recursive Procedure Calls
For recursive procedures, storage Allocation needs to manage wnultigie activations of
The SAme procedure, CACh with its own Set of locAl Variables And veturn Address,
The common strateay is to Use a call Stack, where each groceduve call gushes a new
Frame onto the STACK, And RoRRing it whon the Procedure returns.5. Conditions for Optimization Techniques
Constant Propagation. Requires that the value of a vavidlele is constant And Known
at compile time.
Dead Code Elimination. Code 15 dead if it dos not affect the Progra outgut (9,
Computations whose results Ove never used)
Looe Optimization: Requires analysis to ensure transformations maintain Progam
Coveectness eg, data dependencies are wespected).
Code Motion: Safe when the moved code's execution is guaranteed not to affect ov
oe affected boy the code in its new location.
b. Code Generation Phase
Logut to Code Generator: Intermediate vegresentation (LR) of the erograin,
Target Proqrawe The mOchin code ov As5eeloly code a2novated oy the compiler for
the target architecture.
Memory Management. Tnvalues stvateaies for efficient allocation, use, And reuse
of memory locations for varialeles, constants, And temporary values.
Tnstruction Selection: Choosing the Seacific machine instructions to mglement the
operations expressed in the IR.
Register Allocation: Deciding which variables should oe Kept im the limited set of CPU
¥2aistors to minimize memory access,
Evaivation Order. Determines the sequence in which instructions ave executed,
a€Fecting e¢Ficidncy And covedctness, especially in the geasence of sido effects
And dependencies.