13.07.2015 Views

Intel® 64 and IA-32 Architectures Optimization Reference Manual

Intel® 64 and IA-32 Architectures Optimization Reference Manual

Intel® 64 and IA-32 Architectures Optimization Reference Manual

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

GENERAL OPTIMIZATION GUIDELINESUser/Source Coding Rule 1. (M impact, L generality) If an indirect branch hastwo or more common taken targets <strong>and</strong> at least one of those targets is correlatedwith branch history leading up to the branch, then convert the indirect branch to atree where one or more indirect branches are preceded by conditional branches tothose targets. Apply this “peeling” procedure to the common target of an indirectbranch that correlates to branch history.The purpose of this rule is to reduce the total number of mispredictions by enhancingthe predictability of branches (even at the expense of adding more branches). Theadded branches must be predictable for this to be worthwhile. One reason for suchpredictability is a strong correlation with preceding branch history. That is, the directionstaken on preceding branches are a good indicator of the direction of the branchunder consideration.Example 3-8 shows a simple example of the correlation between a target of apreceding conditional branch <strong>and</strong> a target of an indirect branch.Example 3-8. Indirect Branch With Two Favored Targetsfunction (){int n = r<strong>and</strong>(); // r<strong>and</strong>om integer 0 to RAND_MAXif ( ! (n & 0x0) ) { // n will be 0 half the timesn = 0; // updates branch history to predict taken}// indirect branches with multiple taken targets// may have lower prediction rates}switch (n) {case 0: h<strong>and</strong>le_0(); break;case 1: h<strong>and</strong>le_(); break;case 3: h<strong>and</strong>le_3(); break;default: h<strong>and</strong>le_other();}// common target, correlated with// branch history that is forward taken// uncommon// uncommon// common targetCorrelation can be difficult to determine analytically, for a compiler <strong>and</strong> for anassembly language programmer. It may be fruitful to evaluate performance with <strong>and</strong>without peeling to get the best performance from a coding effort.An example of peeling out the most favored target of an indirect branch with correlatedbranch history is shown in Example 3-9.3-14

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!