Docsity

Dynamic Binary Optimization - Lecture Notes | EECS 700, Exams of Deductive Database Systems

Partial preview of the text

Download Dynamic Binary Optimization - Lecture Notes | EECS 700 and more Exams Deductive Database Systems in PDF only on Docsity!

Dynamic Binary Optimization

Introduction

Application profiling

Optimizing translation blocks

Compatibility

Code reordering

Other code optimizations

Optimization Overview

Identify frequently executed hot code regions

● basic blocks

● paths – indicate control flow

edges – approximation to paths

Dynamic profiling

count execution frequencies

● software or hardware implemented

Form large translation blocks

● traces and superblocks

Schedule and optimize large blocks

Optimization Based On Profiling (2)

Basic Block A

... ...

R3 ^ …

R7 ^ ...

R1 ^ R2 + R

BEQ L1 if R3 ==

L1: R1 ^0

Basic Block C Basic Block B

...

R6 ^ R1 + R

Superblock

... ...

R3 ^ …

R7 ^ ...

BNE L2 if R3 !=

R1 ^0

Basic Block B L2:...

R6 ^ R1 + R

Compensation code

R1 ^ R2 + R

Program Behavior

Many aspects of a program's behavior are

predictable

● branches, data values

Backward branch primarily taken

Forward branch mostly not taken

R3 ← 100 loop: R1 ← mem(R2) ; load from memory Br found if R1 == -1 ; look for - R2 ← R2 + 4 R3 ← R3 - Br loop if R3 != 0 ; loop closing branch . . found:

Branch Behavior (2)

Most branches decided the same way as on

previous execution

backward conditional branches are mostly taken

forward conditional branches taken less often

176.gcc 181.mcf 197.parser 252.eon 256.bzip2^ 171.swim 173.applu 177.mesa 187.facerec 189.lucas Percent Dynamic Branches Decided Same As Previous Time

Other Program Behavior

Some indirect jumps have a single target

• others have several targets (e.g. returns)

Predictability extends to data values

• many instructions always produce the same result

0

All Add/Sub Load Logic^ Shift Set Instruction Type Fraction with Constant Value static dynamic

Conventional ( Offline ) Profiling

Multiple passes through compiler

Done at program development time

• profile overhead is a small issue

Can be based on global analysis

B C D E F A Compiler Front-end

HLL

Program Instrumented Code Optimizing Compiler Test Data Program Execution Compiler Back-end Program Statistics Optmized Binary intermediate form Instrumented Code

VM-Based ( Online ) Profiling

Profile overhead is very important

• profile time part of total execution time

Limited view of program (no a priori global view)

• profile probes cannot be carefully placed

B D E A

Interpreter

Program

Binary

Translator/

Optimizer

Program

Data

Partial

Program

Statistics

partially

"discovered"

code

Collecting Profiles

Instrumentation-based

• software probes

 slows down program more

 requires less total time than sampling

• hardware probes

 less overhead than software

 less well-supported in processors

 typically event counters

Sampling based

• interrupt at random intervals and take sample

 slows down program less

 requires longer time to get same amount of data

• not useful during interpretation

Profiling During Interpretation

Instruction function list . branch_conditional(inst) { BO = extract(inst,25,5); BI = extract(inst,20,5); displacement = extract(inst,15,14) * 4; . . // code to compute whether branch should be taken . . profile_addr = lookup(PC); if (branch_taken) profile_cnt(profile_addr, taken)++; PC = PC + displacement; Else profile_cnt(profile_addr, nottaken)++; PC = PC + 4; }

Branch PC

taken

PC count

not taken

count

HASH

Sampling

Set interval counter

Interrupt when counter hits zero

Sample PC at that point

Gives block profile

Could be modified to give edge profile

Zero Detect Interval Counter Instruction Address decrement for each instruction Program Counter Load PC Initialize Counter TRAP Sample PC

Improving Code Locality

A

B

E

A

B

E

Provide more optimization

opportunities.

Spatial locality

● consecutive memory

accesses are adjacent

Temporal locality

● same memory access is

repeated in near future

Reasons for spatial and

temporal locality

● loops and sequential

program flow

Improving Locality: Example (2)

Little locality (spatial or temporal) in cache line that

spans blocks E and F

F seldom used

• wasted I-cache space and I-fetch bandwidth

Heavily used discontiguous code blocks

• e.g., C and D

• still wastes I-fetch bandwidth

E F F F

Br uncond

Improving Locality: Rearrange Code

Br cond1 == true Br cond2 == false Br uncond Br cond3 == true Br uncond Br cond4 == true A B C D E F G Br cond1 == false A Br cond3 == true D E Br cond2 == false Br uncond B C Br cond4 == true G F Br uncond Br uncond