Getting the Least Out Least Out Getting the Coding tips and usage - - PowerPoint PPT Presentation

getting the least out least out getting the
SMART_READER_LITE
LIVE PREVIEW

Getting the Least Out Least Out Getting the Coding tips and usage - - PowerPoint PPT Presentation

This Class This Class Getting the Least Out Least Out Getting the Coding tips and usage tips for C Coding tips and usage tips for C To keep code size down To keep code size down of Your Your C C Compiler Compiler of


slide-1
SLIDE 1

1

Getting the Getting the Least Out Least Out

  • f
  • f Your

Your C C Compiler Compiler

Embedded Embedded Systems Systems Conference Conference San Fransisco 2001 San Fransisco 2001 Class Class #508 #508

Jakob Engblom Jakob Engblom

Strategic Development IAR Systems jakob.engblom@iar.se

  • Dept. of Computer Systems

Uppsala University, Sweden jakob@docs.uu.se

2

This Class This Class

  • Coding tips and usage tips for C

Coding tips and usage tips for C

  • To keep code size down

To keep code size down

  • To help the compiler optimize

To help the compiler optimize

  • Assumed background:

Assumed background:

  • Embedded Systems Hardware

Embedded Systems Hardware

  • Embedded Systems Software

Embedded Systems Software

  • C Programming

C Programming

  • Level: basic

Level: basic

3

The Target Systems The Target Systems

  • Microcontrollers:

Microcontrollers:

  • CPU Core

CPU Core

  • Integrated memory

Integrated memory

  • Integrated peripherals

Integrated peripherals

  • Integrated services

Integrated services

  • “System on a chip”

“System on a chip”

  • 8-16 bit most

8-16 bit most common common

CPU Core RAM (small) ROM (big)

UART A/D Timer LCD D

Outside World Outside World

4

Why Care About Size? Why Care About Size?

  • Keep memory cost down

Keep memory cost down

  • Avoid external memory

Avoid external memory

  • Use smaller external memory

Use smaller external memory

  • Use a smaller derivative

Use a smaller derivative

  • Keep absolute limits

Keep absolute limits

  • HW done before the software

HW done before the software

  • Application can only

Application can only use on-chip memory use on-chip memory

slide-2
SLIDE 2

5

Compiler Compiler Technology Technology

6

Compiler System Compiler System

Linker C Source Compiler Object File C Source C Source Object File Object File C Library C Runtime Other Lib OS Compiler System Compiler System Hardware

User Code User Code Third-Party Code Third-Party Code

Executable

7

The Compiler The Compiler

C Source Parser Intermediate Code High-Level Optimizer Code Generator Target Code Low-Level Optimizer Assembler Object Code Compiler Compiler ld R10,$_x add R10,#15 st $_x,R10 = + 15 x x x = x + 15; 000100110101 101111011101

8

Optimization Optimization

  • Analysis:

Analysis:

  • “Understand” the code

“Understand” the code

  • Data flow, control flow

Data flow, control flow

  • More info = better code

More info = better code

  • Transformation:

Transformation:

  • Change the code

Change the code

  • Based on heuristics

Based on heuristics

  • Should improve

Should improve

  • … but does not have to

… but does not have to

  • Improvement not guaranteed!

Improvement not guaranteed!

And definitely not “optimality”

  • f the code!
slide-3
SLIDE 3

9

Remove useless Remove useless computations computations Propagate Propagate constant values constant values Use cheaper Use cheaper

  • peration
  • peration

Basic Transformations Basic Transformations

a=b+b a=b*2 temp=c*d a=b+temp e=f+temp a=b+c*d e=f+c*d a=17 b=90 a=17 b=56+2*a a=b*c+k a=k+7 a=b*c+k a=k+7

Find common Find common calculations calculations

10

Move Move invariant invariant code out code out

  • f loops
  • f loops

Remove Remove unreachable unreachable code code

Basic Transformations Basic Transformations

if(a>10) { b=b*c+k; if(a<5) a+=6; }

b = k * c; for(i=0;i<10;i++) { p[i] = b; }

if(a>10) { b=b*c+k; if(a<5) a+=6; }

for(i=0;i<10;i++) { b = k * c; p[i] = b; }

11

Pitfall: Empty Loops Pitfall: Empty Loops

  • Empty delay loop

Empty delay loop does not work does not work

  • Code with no effect

Code with no effect

  • Gets removed

Gets removed

  • Delay is zero

Delay is zero

  • For delay:

For delay:

  • Access volatile variable

Access volatile variable

  • Use OS services

Use OS services

  • Use CPU timer

Use CPU timer

  • Use delay intrinsic

Use delay intrinsic

void delay(int time) { int i; for (i=0;i<time;i++) ; return; } void InitHW(void) { /* Timing-dependent code */ OUT_SIGNAL (0x20); delay(120); OUT_SIGNAL (0x21); delay(121); OUT_SIGNAL (0x19); }

12

Optimization Settings Optimization Settings

  • Typically available:

Typically available:

  • Minimize size (“size”)

Minimize size (“size”)

  • Minimize execution time (“speed”)

Minimize execution time (“speed”)

  • Enable/disable individual transformations

Enable/disable individual transformations

  • Use highest settings!

Use highest settings!

  • Settings only approximate

Settings only approximate

  • Best guess by compiler writer

Best guess by compiler writer

  • Test several settings, check the results

Test several settings, check the results

  • Disable destructive transformations

Disable destructive transformations

slide-4
SLIDE 4

13

Optimization Settings Optimization Settings

  • Global setting

Global setting

  • Over entire project

Over entire project

  • IDE or command-line

IDE or command-line

  • Per-file settings

Per-file settings

  • Example: collect all

Example: collect all speed in one file speed in one file

  • Per-function

Per-function

  • #

#pragma pragma in source file in source file

  • Compiler dependent

Compiler dependent

#pragma optimize(all,off) void delay(int time) { … }

14

Code Generation Code Generation

  • From Intermediate to Target

From Intermediate to Target

  • Implement C operations

Implement C operations

  • Break down big operations

Break down big operations

  • Insert calls to C runtime

Insert calls to C runtime (more on this later)

(more on this later)

  • May introduce jumps, loops, etc.

May introduce jumps, loops, etc.

  • Assign variables to registers

Assign variables to registers

  • “Register Allocation”

“Register Allocation”

  • Heuristics for “best” variables

Heuristics for “best” variables

  • Language rules followed

Language rules followed (volatile)

(volatile)

  • Shouldn’t be considered “optimization”

Shouldn’t be considered “optimization”

15

Compiler Libraries Compiler Libraries

Linker C Source Compiler Object File C Source C Source Object File Object File C Library C Runtime Other Lib OS Compiler System Compiler System Hardware

User Code User Code Third-Party Code Third-Party Code

Executable

16

The C Library The C Library

“printf

printf() ()”etc.

”etc.

  • Programmer-visible

Programmer-visible

  • Standardized in ANSI C

Standardized in ANSI C

  • “Standalone” profile:

“Standalone” profile:

  • No file operations

No file operations

  • Suitable for run-from-ROM systems

Suitable for run-from-ROM systems

  • Limited versions:

Limited versions:

  • Smaller code through less functionality

Smaller code through less functionality

  • Provided with compiler

Provided with compiler

  • Non-standard

Non-standard

slide-5
SLIDE 5

17

The C Run-Time System The C Run-Time System

  • Not very well known item

Not very well known item

  • Part of compiler

Part of compiler

  • Designed with the compiler

Designed with the compiler

  • Implements complex functions

Implements complex functions

  • Called from compiled code

Called from compiled code

  • Not visible to programmer

Not visible to programmer

  • Whatever is needed that the

Whatever is needed that the hardware does not support hardware does not support

  • Bigger for simpler machines

Bigger for simpler machines

18

The C Run-Time System The C Run-Time System

  • 32-bit arithmetic

32-bit arithmetic

  • Floating-point arithmetic

Floating-point arithmetic

  • Pointer use:

Pointer use:

  • Banked, generic, large, function pointers, …

Banked, generic, large, function pointers, …

  • Operations missing from CPU:

Operations missing from CPU:

  • Multiply, divide, modulo, …

Multiply, divide, modulo, …

  • Variable-length shifts (“

Variable-length shifts (“a<<x

a<<x”)

”)

  • May have a large footprint

May have a large footprint

  • Tens of kilobytes of code

Tens of kilobytes of code

  • Tens of bytes of data

Tens of bytes of data

19

Coding Coding Techniques Techniques

20

Structuring a Program Structuring a Program

Hardware Hardware Device-dependent Program Files Device-dependent Program Files Generic Generic Program Program Files Files Tuned Tuned Program Program Files Files

  • To be efficient and portable

To be efficient and portable

  • Isolate device-dependent code

Isolate device-dependent code

  • Leave most of the code undisturbed

Leave most of the code undisturbed

  • Use tuned code where needed

Use tuned code where needed

slide-6
SLIDE 6

21

Device Descriptions Device Descriptions

  • Chips have many derivatives

Chips have many derivatives

  • Different I/O ports

Different I/O ports

  • Different available memories, sizes, etc.

Different available memories, sizes, etc.

  • Special features

Special features

  • Compiler device description:

Compiler device description:

  • As

As #

#include include files and command-line options

files and command-line options

  • #include

#include files from compiler: files from compiler:

  • Describes chip in best way for compiler

Describes chip in best way for compiler

  • Saves work for programmer

Saves work for programmer

  • Do not write your own unless necessary!

Do not write your own unless necessary!

22

Use of Data Types Use of Data Types

  • Types very important

Types very important

  • Factors to consider:

Factors to consider:

  • Size

Size

  • Signed or unsigned

Signed or unsigned

  • Floating point or integers

Floating point or integers

  • Memory placement

Memory placement

  • Pointer types

Pointer types

  • Casting

Casting

23

Data Size Data Size

  • Performing operations:

Performing operations:

  • 32-bit operations hard for 8-bit CPU

32-bit operations hard for 8-bit CPU

  • 8-bit operations less efficient on 32-bit

8-bit operations less efficient on 32-bit

  • Sometimes type is forced

Sometimes type is forced

  • char

char for byte I/O etc

for byte I/O etc

  • long

long for large counters

for large counters

  • Often, we can choose

Often, we can choose

  • Smallest possible type for 8-bit

Smallest possible type for 8-bit

  • int

int for 32-bit machines

for 32-bit machines

24

Data Size: Header Files Data Size: Header Files

  • 8-bit machine

8-bit machine

/* Fixed size types */ typedef unsigned char uint8_t; typedef short int16_t; typedef unsigned long uint32_t; /* Adjusting types */ typedef char best_int8_t; typedef short best_int16_t; typedef long best_int32_t;

  • 32-bit machine

32-bit machine

/* Fixed size types */ typedef unsigned char uint8_t; typedef short int16_t; typedef unsigned int uint32_t; /* Adjusting types */ typedef int best_int8_t; typedef int best_int16_t; typedef int best_int32_t; All are 32-bit

  • values. Most

efficient for calculations

/* Use best_X_t for ranges */ best_int8_t i; for(i=0; i<10; i++) …

slide-7
SLIDE 7

25

Signed or Unsigned Signed or Unsigned

  • Think about the

Think about the signedness signedness

  • Signed

Signed

  • Negative values possible

Negative values possible

  • Arithmetic operations performed

Arithmetic operations performed

  • Unsigned

Unsigned

  • Negative values impossible

Negative values impossible

  • Bit operations performed

Bit operations performed

<< >> | << >> | & ^ ~ & ^ ~ + - * / %

26

Integer or Floating Point Integer or Floating Point

  • Floating point very expensive

Floating point very expensive

  • Brings large library (from C runtime lib)

Brings large library (from C runtime lib)

  • Use only when really needed

Use only when really needed

  • Can be done inadvertently:

Can be done inadvertently:

  • Example code:

Example code:

#define Other 20 #define ImportantRatio (1.95 * Other) int i=a + b * ImportantRatio;

This is a floating-point multiplication. Float library linked in!

To obtain an integer constant from ImportantRatio, and thus integer code:

#define ImportantRatio ((int)(1.95 * Other))

27

Memory Placement Memory Placement

Far Far Near Near Tiny Tiny

8 bits 24 bits 16 bits

  • Use smaller areas:

Use smaller areas:

  • Smaller addresses

Smaller addresses

  • Smaller instructions

Smaller instructions

  • Smaller pointers

Smaller pointers

  • =more efficient

=more efficient

  • =less code!

=less code!

  • Place variables:

Place variables:

  • Using keywords

Using keywords

”__tiny

__tiny char a char a; ;”

  • Using

Using #

#pragma pragma

  • In tuned files

In tuned files

  • Typical micro-

Typical micro- controller memory: controller memory:

28

Pointers & Memory Pointers & Memory

  • Pointer types:

Pointer types:

  • One for each addressing space/mode

One for each addressing space/mode

”__near

__near char * char * nearptr nearptr; ;”

”__tiny

__tiny int int * * tinyptr tinyptr; ;”

  • Generic, huge, etc:

Generic, huge, etc:

  • Point to all memories

Point to all memories (for disjoint memories)

(for disjoint memories)

  • Point to objects crossing banks

Point to objects crossing banks

  • Software-emulated = expensive

Software-emulated = expensive

  • Use the smallest applicable type

Use the smallest applicable type

  • Beware of compiler default settings!

Beware of compiler default settings!

  • “Memory model”, “Data model”, …

“Memory model”, “Data model”, …

slide-8
SLIDE 8

29

Casting Casting

  • C has implicit and explicit casts

C has implicit and explicit casts

  • Casting is not free

Casting is not free

  • Sign-extend and zero-extend

Sign-extend and zero-extend

  • Complex conversions to/from floats

Complex conversions to/from floats

  • Casting to/from pointers is bad

Casting to/from pointers is bad

  • Can lose information (different size)

Can lose information (different size)

  • Inefficient code

Inefficient code

  • Avoid unless really necessary

Avoid unless really necessary

  • Don’t do explicit casts

Don’t do explicit casts

  • Don’t mix types in expressions

Don’t mix types in expressions

30

Register Allocation Register Allocation

  • Important for good code

Important for good code

  • To facilitate:

To facilitate:

  • Register keyword?

Register keyword?

  • Use parameters

Use parameters

  • Use local variables

Use local variables

  • Don’t take addresses

Don’t take addresses

  • Group function calls

Group function calls

  • Don’t use

Don’t use varargs varargs

31

Register keyword Register keyword

  • Hint to compiler

Hint to compiler

  • Cannot take address of variable

Cannot take address of variable

  • May be ignored

May be ignored

  • Modern compilers ignore “

Modern compilers ignore “register

register”

  • Register allocation heuristics usually better

Register allocation heuristics usually better

32

Use Parameters & Locals Use Parameters & Locals

  • Global variables

Global variables

  • Written back at function calls

Written back at function calls

  • Have to be given memory

Have to be given memory

  • Do not use globals for parameter passing!

Do not use globals for parameter passing!

  • Parameters and local variables

Parameters and local variables

  • Give compiler more freedom

Give compiler more freedom

  • Need not be given memory

Need not be given memory

  • Parameter passing:

Parameter passing:

  • Parameters often passed in registers

Parameters often passed in registers

  • Registers to use = “calling convention”

Registers to use = “calling convention”

slide-9
SLIDE 9

33

Use Parameters & Locals Use Parameters & Locals

  • Register allocation targets:

Register allocation targets:

  • Simple values (integers, pointers, floats)

Simple values (integers, pointers, floats)

  • Small CPUs: maybe not the biggest values

Small CPUs: maybe not the biggest values

  • Hard to allocate:

Hard to allocate:

  • Arrays – indexing addressing the norm

Arrays – indexing addressing the norm

  • Structs

Structs – large, often pointed to – large, often pointed to

  • Struct

Struct as parameter: as parameter:

  • Value copied, and a pointer passed

Value copied, and a pointer passed

  • Pass pointer directly for efficiency!

Pass pointer directly for efficiency!

34

Use Parameters & Locals Use Parameters & Locals

  • Register usage:

Register usage:

  • Variables can share

Variables can share a register a register

  • Only present in

Only present in register while “live” register while “live”

  • Don’t worry about

Don’t worry about “extra” variables “extra” variables

  • Optimize:

Optimize:

  • Minimize lifetime

Minimize lifetime

  • Minimize overlap

Minimize overlap

void foo(char a) void foo(char a) { { char b,i,j; char b,i,j; char h[10]; char h[10]; b=a<<5; b=a<<5; for(i=0;… ) for(i=0;… ) OUTPUT(b); OUTPUT(b); for(j=0;… ) for(j=0;… ) h[j]=a; h[j]=a; } }

a a b b i i j

j

live ranges live ranges j j can use the same register as i i or b b Many registers needed here Array usually not in registers j, i, b all “optimized away at this point”

35

Don’t Take Addresses Don’t Take Addresses

  • Address taken

Address taken

  • not in register

not in register

  • Always written back to memory

Always written back to memory

  • Variable has to have memory

Variable has to have memory

  • Since:

Since:

  • The address “escapes” a function

The address “escapes” a function

  • Compiler cannot know who reads

Compiler cannot know who reads

  • Note: not too bad for globals

Note: not too bad for globals

  • Local variables behave like global

Local variables behave like global

  • register

register keyword prevents

keyword prevents

36

Examples of address taking Examples of address taking

  • Input functions:

Input functions:

int a; scanf(“%d”,&a); int a, temp; scanf(“%d”,&temp); a=temp;

  • Watch out for smart tricks:

Watch out for smart tricks:

#define highbyte(x) ( *((char *)(&x)+1) ) #define highbyte(x) ( (x>>8) & 0xFF )

slide-10
SLIDE 10

37

Group Function Calls Group Function Calls

  • Function calls:

Function calls:

  • Increase register pressure

Increase register pressure

  • Require certain registers

Require certain registers

  • =impediment to register allocation

=impediment to register allocation

  • Group to minimize effects

Group to minimize effects

s.A = a; s.B = bar(b); s.C = c; s.D = c; s.E = baz(d); s.A = a; s.C = c; s.D = c; s.B = bar(b); s.E = baz(d);

38

Don’t Use Don’t Use Varargs Varargs

  • Variable number of arguments

Variable number of arguments

“int printf int printf(char *, (char *, ... ...)” )”

  • Special macros to access arguments

Special macros to access arguments

  • Force arguments to the stack

Force arguments to the stack

  • Step through them using pointers

Step through them using pointers

  • No parameters in registers

No parameters in registers

  • Source code gets more complex

Source code gets more complex

  • Object code gets bigger

Object code gets bigger

  • Bad idea, simply

Bad idea, simply

39

Facilitate Transformations Facilitate Transformations

  • Convey maximal information

Convey maximal information

  • Write clear code

Write clear code

  • Techniques:

Techniques:

  • Use function prototypes

Use function prototypes

  • Mark file-locals using static

Mark file-locals using static

  • Don’t use inline assembler

Don’t use inline assembler

  • Don’t write clever code

Don’t write clever code

  • Write nice loops

Write nice loops

  • Access hardware as variables

Access hardware as variables

40

Use Function Prototypes Use Function Prototypes

  • C and function calls:

C and function calls:

  • No declaration: call out of the blue

No declaration: call out of the blue

  • K&R declarations: “

K&R declarations: “extern void foo();

extern void foo();”

  • ANSI prototypes: “

ANSI prototypes: “char bar(short);

char bar(short);”

  • Without ANSI prototypes:

Without ANSI prototypes:

  • No proper type checking

No proper type checking

  • All arguments promoted

All arguments promoted

  • Introduces casting code

Introduces casting code

  • Parameter data is larger = more stack used

Parameter data is larger = more stack used

  • Always use ANSI prototypes!

Always use ANSI prototypes!

  • Turn on checking in compiler!

Turn on checking in compiler!

slide-11
SLIDE 11

41

File Locals File Locals

  • The static keyword:

The static keyword:

  • Object not visible outside the file

Object not visible outside the file

  • All references known

All references known

  • More aggressive optimization possible

More aggressive optimization possible

  • Functions:

Functions:

  • Higher chance of inlining

Higher chance of inlining

  • Variables:

Variables:

  • Knows address taken, etc. for all uses

Knows address taken, etc. for all uses

  • Better optimization possible

Better optimization possible

42

Don’t Use Inline Assembly Don’t Use Inline Assembly

  • Major hinder to optimization

Major hinder to optimization

  • Inline assembly can do anything

Inline assembly can do anything

  • (Some inline assemblers allow annotations)

(Some inline assemblers allow annotations)

  • Put assembly into:

Put assembly into:

  • Separate assembly-source files

Separate assembly-source files

  • Assembly-only functions in C files

Assembly-only functions in C files

  • To access special instructions:

To access special instructions:

  • Use intrinsic functions (if available)

Use intrinsic functions (if available)

“__disable_interrupts() __disable_interrupts()”

“__delay(

__delay(n n) )”

  • Generates single assembly instructions

Generates single assembly instructions

43

Don’t Write “Clever” Code Don’t Write “Clever” Code

  • Clever code:

Clever code:

  • “Fewer source characters = better code”

“Fewer source characters = better code”

  • Using dark corners of C semantics

Using dark corners of C semantics

  • Straightforward code:

Straightforward code:

  • Easier for compiler – and humans

Easier for compiler – and humans

  • Safer for portability

Safer for portability

  • Use common constructions:

Use common constructions:

  • Likely to be better optimized

Likely to be better optimized

  • Less likely to contain bugs

Less likely to contain bugs

44

Example “Clever” Code Example “Clever” Code

  • Check If Low 21 Bits Zero

Check If Low 21 Bits Zero

  • Calculating with Conditionals

Calculating with Conditionals

unsigned long int a; unsigned char b; b |= !!(a << 11); unsigned long int a; unsigned char b; if( (a & 0x1FFFFF) != 0) b |= 0x01;

Use of constant: high likelihood of creating bit-set instruction Explicit check against zero for the lower bits Depends on semantics of NOT

  • perator in C

int bad(char *str) { foo(str+(*str=='+')); } int good(char *str) { if(*str=='+') str++; foo(str); }

Add with result of comparison: forces generation

  • f 1 or 0

Explicit check:

  • nly do an “inc”

if needed. Smaller code.

slide-12
SLIDE 12

45

Write Nice Loops Write Nice Loops

  • The compiler is able to:

The compiler is able to:

  • Adjust loop statements to the machine

Adjust loop statements to the machine

  • Change count direction

Change count direction

  • Utilize loop instructions if available (DJNZ)

Utilize loop instructions if available (DJNZ)

  • Remove initial check in “for”-loops

Remove initial check in “for”-loops

  • Convert array indexing to pointer walking

Convert array indexing to pointer walking

  • Unroll loops, fuse loops,

Unroll loops, fuse loops, … many other tricks

… many other tricks

  • Regular, simple loops:

Regular, simple loops:

  • Easier to understand for maintenance

Easier to understand for maintenance

  • Easier to optimize for compiler

Easier to optimize for compiler

46

Write Nice Loops Write Nice Loops

int i=0; do { i+=2; temp = b[i]; i-=1; b[i] = temp + 2; } while (i<80); /* use that i is 80 after loop*/ a = i; best_int8_t i=0; for(i=0; i<80; i++) { b[i+1]=b[i+2]+2; } a = 80; for()-loop should always be used if possible! Update loop index only in the loop declaration Do not read value

  • f loop index after

the loop

“Think “Think Fortran” Fortran”

47

Access to Hardware Ports Access to Hardware Ports

  • Do not cast a constant:

Do not cast a constant:

#define PORT (*((BYTE *) 0x41FF)) #define PORT (*((BYTE *) 0x41FF))

  • Use a variable:

Use a variable:

volatile __no_init BYTE PORT; volatile __no_init BYTE PORT;

  • Locate it: special syntax, or use linker

Locate it: special syntax, or use linker

  • Easier to locate in the right memory

Easier to locate in the right memory

  • Often provided in “

Often provided in “deviceXX

deviceXX.h .h”

  • Provides better type checking

Provides better type checking

  • Gives better code

Gives better code

  • Can be simulated on PC

Can be simulated on PC

48

Inhibit Optimizations Inhibit Optimizations

  • Sometimes, optimizations

Sometimes, optimizations are not desirable are not desirable

  • Reordering of computations

Reordering of computations

  • Removing redundant code

Removing redundant code

  • Register allocation

Register allocation

  • Examples:

Examples:

  • Shared variables

Shared variables

  • I/O ports

I/O ports

  • Side effects must be ordered

Side effects must be ordered

  • Use

Use volatile volatile variables! variables!

slide-13
SLIDE 13

49

Volatile Semantics Volatile Semantics

  • Volatile

Volatile keyword means keyword means

  • “Can change outside program control”

“Can change outside program control” = Can change = Can change asynchronously asynchronously

  • Effect on compiler:

Effect on compiler:

  • Never in registers

Never in registers

  • All reads and writes to memory immediately

All reads and writes to memory immediately

  • Accesses not reordered

Accesses not reordered

  • No assignments considered dead

No assignments considered dead

  • Put in:

Put in:

  • Header files for I/O and shared data

Header files for I/O and shared data

50

Volatile Examples Volatile Examples

volatile Boolean gLock; … while( gLock == kFalse ) /* spinlock */ ; volatile __no_init uint8_t outPortA; volatile __no_init uint16_t outPortB; … /* Setup sequence for hardware */

  • utPortA = 0x00;
  • utPortB = 0x1000;
  • utPortA |= 0x3A;
  • utPortB = 0xFFFF;

51

Outside Outside the Code the Code

52

Use Another Compiler Use Another Compiler

  • Compilers are different

Compilers are different

  • Different priorities

Different priorities

  • Different sets of optimizations

Different sets of optimizations

  • Different ideas of target programs

Different ideas of target programs

  • Test several compilers

Test several compilers

  • See what works best with your code

See what works best with your code

  • Use real application code!

Use real application code!

  • Benchmarks are usually misleading

Benchmarks are usually misleading (“ (“benchmarketing benchmarketing”) ”)

slide-14
SLIDE 14

53

Try Better Architectures Try Better Architectures

  • Some chips are inherently better

Some chips are inherently better

  • More suited for application

More suited for application

  • More suited for C programming

More suited for C programming

  • More efficient instruction sets

More efficient instruction sets

  • (Good example: AVR

(Good example: AVR vs

  • vs. 8051)

. 8051)

  • Compacted instruction sets

Compacted instruction sets

  • Works in practice, gives smaller code

Works in practice, gives smaller code

  • ARM/Thumb

ARM/Thumb

  • MIPS16

MIPS16

54

Closing Closing Remarks Remarks

55

Optimization gives bugs? Optimization gives bugs?

  • Exposes existing bugs

Exposes existing bugs

  • Very particular about C semantics

Very particular about C semantics

  • Reduces redundancy in code

Reduces redundancy in code

  • Code was wrong to begin with

Code was wrong to begin with

  • “Only correct code is optimized correctly”

“Only correct code is optimized correctly”

  • Optimization is necessary

Optimization is necessary

  • Write maintainable, non-tuned code

Write maintainable, non-tuned code

  • Trust compiler to optimize

Trust compiler to optimize

  • Without optimization, compiler handicapped

Without optimization, compiler handicapped

56

Summary: Code Summary: Code

  • Write easy-to-understand code

Write easy-to-understand code

  • Provide maximal information

Provide maximal information

  • Maintenance easier

Maintenance easier

  • Optimizations work better

Optimizations work better

  • =Everybody happy

=Everybody happy

  • Do not

Do not suboptimize suboptimize

  • Keep code free of target assumptions

Keep code free of target assumptions

  • Let the compiler optimize for target

Let the compiler optimize for target

  • Manually optimize only where needed

Manually optimize only where needed

slide-15
SLIDE 15

57

Summary: C Compilers Summary: C Compilers

  • Compilers are heuristic

Compilers are heuristic

  • Usually work OK

Usually work OK

  • No guarantees of improving code

No guarantees of improving code

  • No hard facts like “

No hard facts like “always always gains gains n n%” %”

  • Try different compiler settings

Try different compiler settings

  • All programs are different

All programs are different

  • Use common constructions

Use common constructions

  • Fewer bugs, better optimized

Fewer bugs, better optimized

  • (Many more details in paper)

(Many more details in paper)

The The End End

  • Fill in Evaluation Forms

Fill in Evaluation Forms

  • Updates of Paper:

Updates of Paper:

www. www.docs docs. .uu uu.se/~ .se/~jakob jakob