Sei sulla pagina 1di 12

R. V.

College of Engineering
An autonomous institution affiliated to VTU

Department of
Electronics &Communication
V Semester
Embedded systems & Microcontrollers Assignments










Report on
Methods to Reduce Program Memory Size`
Submitted by,
Mohan Kumar N 1RV09EC044

Date oI Submission:
Marks Awarded:
StaII Incharge: R1

Methods oI Program Size Reduction

RvCE,ECE Page

Contents Page No.


Introduction 3
The Code Reduction Method 4
Code Optimization in C compilers 6
Convolution and Propagation oI Constants 6
Peephole Optimization 7
Dead-Code Elimination 9
Caching OI Memory Access 10

Switch Table Optimization 11

ReIerences 12














Methods oI Program Size Reduction

RvCE,ECE Page

NTRODUCTON
e are surrounded by embedded systems. Their perIormance is constrained by power
dissipation, energy consumption, and size. Particularly, they are constrained by memory size.
The constraints are not only on the physical memory hierarchy, but on the virtual memory
size as well. In Iact, many embedded systems have poor or no support Ior virtual memory.
Memory behaviour including both the size and the number oI access is playing a more and
more important role in the embedded system design. As new processors continuously
improve the perIormance oI embedded systems, the processor-memory gap widens and
memory represents a major bottleneck in terms oI speed, area and power Ior many
applications. Constraints on the memory size oI embedded systems require reducing the
image size oI executing programs. Thus, reducing the size oI loaded executable images is oI
paramount importance. Common techniques include code compression and reduced
instruction sets. The Iormer requires that the executable is stored in a compressed state and is
decompressed beIore execution. Thus, it is possible that critical code or data must be
decompressed beIore it can be used. Run-time decompression can help solve the issue oI non-
runnable executable Iiles. However, in run-time decompression, both the compressed image
program and the decompression subroutine, can consume substantial memory during runtime,
making such techniques ineIIicient in many cases. In reduced size instruction sets, the size oI
all instructions are diminished, usually Irom 32 to 16 bits. However, this greatly limits the
quality oI the compiled code.


CODE-SE REDUCTON BACKGROUND
Code size reduction is a technique to reduce code size. There are two popular techniques:
code compaction and code compression.
The Iirst technique, code compression, uses data compression algorithms on machine codes.
On the other hand, code compaction reduces the program size by using compiler optimization
to rearrange and eliminate superIluous codes. This allows the compressed program to be
executed immediately without needing decompression as in the code compression technique.
Decompression will show down the system operation in code compression. However, using
compression algorithm in code compression will reduce the program size more than using
code compaction. The eIIiciency oI the code size reduction technique is measured through the
compression ratio as in equation.


Compression Ratio Compressed size / Uncompressed size.


Methods oI Program Size Reduction

RvCE,ECE Page

THE CODE REDUCTON METHOD



Dead Code: A portion oI the program that never executes Ior any program trace. Compiler
optimizations usually remove these sections.
Frozen Code: A code area within the program Iile that is not executed when run on a
representative workload.
Cold Code: A code area within the program Iile that is rarely executed, relatively to other
parts oI the program, when run on a representative workload.
Hot Code: A code area within the program Iile that is Irequently executed, relatively to other
parts oI the program, when run on a representative workload.
Thawed Code: A code area within the program that was marked as Irozen but during
runtime was accessed.
Frozen/Cold/Hot/Thawed Data Variable: A data variable within a given executable Iile,
that is not/rarely/Irequently accessed, when run on a representative workload.

In general, the code reduction method relocates all Irozen basic blocks in the given code,
groups them together in a separate non-loadable module and replaces each control transIer to
and Irom them by an appropriate interrupt. In the event oI a reIerence to an unloaded code
instruction, the interrupt handler is invoked and loads the relevant code regions Irom
secondary storage. The interrupt is replaced with a direct or indirect branch (dependent on its
placement in memory).The time consuming interrupt mechanism will be rarely invoked
during run-time, and will thereIore, have no or little eIIect on perIormance.
The Iirst step in the proposed reduction method is to globally reorder the program code, based
on proIiling data gathered with an appropriate representative workload. For example,
consider the Iollowing pseudo assembly instructions beIore code reordering, in which hot
code is shown in bold Iace:
compare r1, r2
jump-false L1
(Frozen Then Part)
L1: (Hot Continue Part)



Methods oI Program Size Reduction

RvCE,ECE Page


The above code aIter reordering will have the Iollowing Iorm:
compare r1, r2
jump-true L2
L1: (Hot Continue Part)
...
L2: (Frozen Then Part)
jump L1

In the reordered code above, the condition oI the conditional jump instruction is reversed, and
managed to group the hot code together and place the Irozen code Iurther away in the
program. Code reordering has important properties oI reducing instruction cache misses and
the number oI branches in the code. In order to maintain correctness, an additional
unconditional jump instruction to L1 was added at the end oI the relocated Irozen code part.
























Methods oI Program Size Reduction

RvCE,ECE Page

COMMON OPTMATONS N C COMPLERS



This section describes common optimization techniques oIten Iound in optimizing C
compilers.

Convolution and Propagation of Constants

This optimization method propagates constants in place oI variables whenever possible and
computes any constant expression at compile time rather than at run time. Consider the C
program example in Figure. For the C statement in line 3, the C compiler computes the
addition oI two constants at compile time rather than at execution time. Similarly, Ior C
statements Irom lines 4 to 6, the constant in line 4 is propagated to lines 5 and 6. This
optimization technique reduces program size and increases execution speed.
Program:

Methods oI Program Size Reduction

RvCE,ECE Page

Peephole optimization
Peephole optimization is a kind oI optimization perIormed over a very small set oI
instructions in a segment oI generated code. The set is called a "peephole" or a "window". It
works by recognising sets oI instructions that don't actually do anything, or that can be
replaced by a leaner set oI instructions.
Removing redundant code
This example is to eliminate redundant load stores.
a b c;
d a e;
is straightIorwardly implemented as
MOV R0,b # Copy b to the register 0
MOV A,R0 # Add b to the Accumulator
MOV R0,c # Copy the c to the register 0
ADD A,R0 # Add c to b
MOV A,
ADD A,e # Add e to the register, the register is now ae |(bc)e|
MOV d,A # Copy the register to d

But can be optimised to
MOV b, R0 # Copy b to the register
ADD c, R0 # Add c to the register, which is now bc (a)
MOV R0, a # Copy the register to a
ADD e, R0 # Add e to the register, which is now bce |(a)e|
MOV R0, d # Copy the register to d
Furthermore, iI the compiler knew that the variable a was not used again, the middle
operation could be omitted.
Removing redundant stack instructions
II the compiler saves registers on the stack beIore calling a subroutine and restores them
when returning, consecutive calls to subroutines may have redundant stack instructions.





Methods oI Program Size Reduction

RvCE,ECE Page 8

Suppose the compiler generates the Iollowing instructions Ior each procedure call:
PUSH AF
PUSH BC
PUSH DE
PUSH HL
CALL ADDR
POP HL
POP DE
POP BC
POP AF
II there were two consecutive subroutine calls, they would look like this:
PUSH AF
PUSH BC
PUSH DE
PUSH HL
CALL ADDR1
POP HL
POP DE
POP BC
POP AF
PUSH AF
PUSH BC
PUSH DE
PUSH HL
CALL ADDR2
POP HL
POP DE
POP BC
POP AF
The sequence POP registers Iollowed by PUSH Ior the same registers is generally redundant.
In cases where it is redundant, a peephole optimization would remove these instructions. In
the example, this would cause another redundant POP/PUSH pair to appear in the peephole,
and these would be removed in turn. Removing all oI the redundant code in the example
above would eventually leave the Iollowing code:
PUSH AF
PUSH BC
PUSH DE
PUSH HL
CALL ADDR1
Methods oI Program Size Reduction

RvCE,ECE Page 9

CALL ADDR2
POP HL
POP DE
POP BC
POP AF


Dead-Code Elimination
This optimization method deletes unused variables at compile time. Consider
the C program example. ith dead-code elimination optimization, the C
compiler eliminates the C statement in line 3.












Methods oI Program Size Reduction

RvCE,ECE Page


Caching Of Memory Access

This optimization method reduces the number oI memory access, thus speeding up program
execution. Consider the C program example in Figure. ith optimization, the number oI
memory access is reduced Irom three to one. Also, the resultant code occupies less memory
(12 bytes versus 20 bytes).

Program:










Switch Table Optimization
Methods oI Program Size Reduction

RvCE,ECE Page

Switch statements are very common in C programs. This optimization calls Ior the C
compiler to analyse the nature oI the case values in the switch statement, then decide on the
optimum way to implement the switch statement. Consider the C programme examples in
Figure. The C compiler implements the switch statement in the C program 1 as a series oI
compare and branch instructions. For the C program 2, the C Compiler uses a diIIerent
coding style to improve code eIIiciency


Switch Table Optimization

Methods oI Program Size Reduction

RvCE,ECE Page

References:
1)Reducing Program Image Size by Extracting Frozen Code and Data
by Daniel Citron, Gadi Haber, Roy Levin (EMSOFT`04, September 2729, 2004, Pisa,
Italy.)

2)Code-Size Reduction Ior Embedded Systems using Bytecode Translation Unit
by Phanupan Nanthanavoot & Prabhas Chongstitvatana
Department oI Computer Engineering, Faculty oI Engineering, Chulalongkorn University,
Phyathai Road, Bangkok 10330, Thailand.

3) http://en.wikipedia.org/wiki/Compileroptimization
4)http://en.wikipedia.org/wiki/Deadcodeelimination
5)http://en.wikipedia.org/wiki/Peepholeoptimization
6) http://www.scribd.com/doc/47945009/riting-Optimized-C-Code-For-Microcontroller-
Applications

Potrebbero piacerti anche