getting the least out least out getting the
play

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


  1. 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 � � To help the compiler optimize To help the compiler optimize � Jakob Engblom Jakob Engblom Assumed background: � Assumed background: � � Embedded Systems Hardware Embedded Systems Hardware Strategic Development Dept. of Computer Systems � IAR Systems Uppsala University, Sweden � Embedded Systems Software Embedded Systems Software � jakob.engblom@iar.se jakob@docs.uu.se � C Programming C Programming � Embedded Systems Systems Conference Conference San Fransisco 2001 San Fransisco 2001 Embedded Class #508 Class #508 � Level: basic Level: basic � 1 2 The Target Systems Why Care About Size? The Target Systems Why Care About Size? Microcontrollers: Keep memory cost down � Microcontrollers: � Keep memory cost down � � CPU Core RAM Avoid external memory � CPU Core � Avoid external memory � � (small) Integrated memory Use smaller external memory � Integrated memory ROM � Use smaller external memory � � (big) CPU Integrated peripherals Use a smaller derivative � Integrated peripherals � Use a smaller derivative � � Core Integrated services � Integrated services � LCD D UART Timer A/D “System on a chip” � “System on a chip” � Keep absolute limits � Keep absolute limits � 8-16 bit most � 8-16 bit most � HW done before the software � HW done before the software � common common Outside World Outside World Application can only � Application can only � use on-chip memory use on-chip memory 3 4

  2. Compiler System Compiler System Compiler System Compiler System C Runtime Compiler Compiler User Code User Code C Library C Source Object File Technology Technology C Source Object File Compiler C Source Object File Linker OS Executable Other Lib Hardware Third-Party Code Third-Party Code 5 6 The Compiler Optimization The Compiler Optimization x = x + 15; Analysis: � Analysis: C Source � “Understand” the code � “Understand” the code Compiler Compiler � Parser Data flow, control flow � Data flow, control flow � Intermediate High-Level More info = better code � More info = better code � Code Optimizer Transformation: � Transformation: � Code = Generator Change the code � Change the code � + x Low-Level Based on heuristics � Based on heuristics And definitely � Target Code x 15 Optimizer Should improve not “optimality” � Should improve � 000100110101 of the code! Assembler … but does not have to � … but does not have to 101111011101 � ld R10,$_x Improvement not guaranteed! � Improvement not guaranteed! add R10,#15 � Object Code st $_x,R10 7 8

  3. Basic Transformations Basic Transformations Basic Transformations Basic Transformations Use cheaper Use cheaper if(a>10) if(a>10) Remove Remove a=b*2 a=b+b { { operation operation unreachable unreachable b=b*c+k; b=b*c+k; if(a<5) if(a<5) Find common Find common code code temp=c*d a=b+c*d a+=6; a+=6; a=b+temp calculations calculations e=f+c*d } } e=f+temp Propagate Propagate Move a=17 a=17 Move constant values constant values b=56+2*a b=90 for(i=0;i<10;i++) b = k * c; invariant invariant { for(i=0;i<10;i++) code out b = k * c; { Remove useless Remove useless code out a=b*c+k a=b*c+k p[i] = b; p[i] = b; computations of loops computations of loops } } a=k+7 a=k+7 9 10 Pitfall: Empty Loops Optimization Settings Pitfall: Empty Loops Optimization Settings Empty delay loop � Empty delay loop � Typically available: Typically available: void delay(int time) � � { does not work does not work � Minimize size (“size”) Minimize size (“size”) int i; � � Code with no effect Code with no effect for (i=0;i<time;i++) ; � Minimize execution time (“speed”) Minimize execution time (“speed”) � � return; � Gets removed Gets removed � � Enable/disable individual transformations Enable/disable individual transformations � } � Delay is zero Delay is zero � � Use highest settings! Use highest settings! � void InitHW(void) { For delay: � For delay: Settings only approximate � Settings only approximate /* Timing-dependent code */ � � � Access volatile variable Access volatile variable OUT_SIGNAL (0x20); � � Best guess by compiler writer Best guess by compiler writer � delay(120); � Use OS services Use OS services � � Test several settings, check the results Test several settings, check the results OUT_SIGNAL (0x21); � � Use CPU timer Use CPU timer � delay(121); � Disable destructive transformations Disable destructive transformations � � Use delay intrinsic Use delay intrinsic OUT_SIGNAL (0x19); � } 11 12

  4. Optimization Settings Code Generation Optimization Settings Code Generation From Intermediate to Target � From Intermediate to Target Global setting � Global setting � � Implement C operations � Implement C operations � Over entire project Over entire project � � � IDE or command-line IDE or command-line Break down big operations � Break down big operations � � Per-file settings � Per-file settings � 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. � Example: collect all Example: collect all � � speed in one file speed in one file � Assign variables to registers Assign variables to registers � Per-function � Per-function � “Register Allocation” “Register Allocation” � � � # #pragma pragma in source file in source file #pragma optimize(all,off) � Heuristics for “best” variables Heuristics for “best” variables � � void delay(int time) � Compiler dependent Compiler dependent Language rules followed (volatile) � Language rules followed (volatile) � { � … Shouldn’t be considered “optimization” � Shouldn’t be considered “optimization” � } 13 14 Compiler Libraries The C Library Compiler Libraries The C Library “ printf ”etc. � “ () ”etc. printf() Compiler System Compiler System � C Runtime � Programmer-visible Programmer-visible User Code User Code � C Library � Standardized in ANSI C Standardized in ANSI C C Source Object File � � “Standalone” profile: “Standalone” profile: C Source Object File Compiler � � No file operations No file operations � C Source Object File Linker � Suitable for run-from-ROM systems Suitable for run-from-ROM systems � � Limited versions: Limited versions: � OS � Smaller code through less functionality Smaller code through less functionality � Executable Provided with compiler � Provided with compiler Other Lib � Non-standard � Non-standard Hardware � Third-Party Code Third-Party Code 15 16

  5. The C Run-Time System The C Run-Time System The C Run-Time System The C Run-Time System Not very well known item 32-bit arithmetic � Not very well known item � 32-bit arithmetic � � Part of compiler Floating-point arithmetic � Part of compiler � Floating-point arithmetic � � Designed with the compiler � Designed with the compiler Pointer use: � Pointer use: � � � Implements complex functions Implements complex functions Banked, generic, large, function pointers, … � � Banked, generic, large, function pointers, … � � Called from compiled code Called from compiled code � Operations missing from CPU: � Operations missing from CPU: � � Not visible to programmer Not visible to programmer � Multiply, divide, modulo, … � Multiply, divide, modulo, … � � Whatever is needed that the Whatever is needed that the � � Variable-length shifts (“ Variable-length shifts (“ a<<x a<<x ”) ”) � hardware does not support hardware does not support May have a large footprint � May have a large footprint � Bigger for simpler machines � Bigger for simpler machines � Tens of kilobytes of code Tens of kilobytes of code � � � Tens of bytes of data Tens of bytes of data � 17 18 Structuring a Program Structuring a Program To be efficient and portable � To be efficient and portable � Isolate device-dependent code � Isolate device-dependent code � Coding Coding � Leave most of the code undisturbed Leave most of the code undisturbed � � Use tuned code where needed Use tuned code where needed � Techniques Techniques Generic Generic Tuned Tuned Program Program Program Program Files Files Files Files Device-dependent Program Files Device-dependent Program Files Hardware Hardware 19 20

Recommend


More recommend