English 中文(简体)
B. 编辑一级的处预测
原标题:Branch prediction in compiler level
  • 时间:2012-05-12 02:57:31
  •  标签:
  • cpu

I have been reading about branch prediction but the only implementation I find are mostly in the hardware side of computer. processors seem to take care of most of the prediction. My question is that, can a compiler do a branch prediction? The only thing I found is 2 methods, function inlining and loop unrolling. Are these considered correct? Are they still used?

最佳回答

Sure. A compiler can get predictive information if it knows:

  • Statistical branch probabilities collected by instrumentation runs
  • Statistical distribution of varible values collected by instrumentation runs; it can then predict the average outcome of a conditional and thus the branch
  • Programmer-assertions as to frequency or bias of a conditional
  • Estimates of loop bounds based on ranges (or defaults to "10" if unknown :)
  • Knowledge that a branch is backwards to the top of loop (predict "taken"

利用这种信息,可以预测条件的可能结果,然后产生倾向于由硬件正确“预测”的分支指示。

一些汇编者进行的一套特别令人感兴趣的优化材料是:。 通过确定最大概率途径,汇编者可以在整个道路上进行优化,而只是在一个基本范围内这样做。

Sometimes compilers will generate branching code that indirectly uses the hardware s branch prediction capability. Compiled OO languages (static or JITted) have to compile method calls, and jump indirects are expensive. A cheap trick is to keep a small dynamic cache of most-recently invoked methods at each call site, and check the object type being dispatched. If the same type of object is frequently used for dispatch at a call site, the compare/branch sequence for the first (and somewhat less for the second) entry in the cache is highly probable, and the executed code thus avoids a mispredict. This is much better than a jump indirect.

最后一种标准trick计:如果你能够避免做一个分支,那么你就不必正确预测。 许多法典顺序类似:

  if (exp1 relop exp2)
      X = Y
  endif

Modern CPUs have "predicated" instructions which are in effect "MOV_if_relop A to B", for all relational conditions equal, not equal, less, etc. So rather than generate a branch for the above construct, the compiler generates:

  <compute exp1 and exp2>
  CMP  exp1,exp2 ; sets condition code
  MOVif_relop  X,Y
问题回答

暂无回答




相关问题
CPU检查 C#

是否有任何人知道如何从C#中检查CPU是否支持人口计算?

Using Java to retrieve the CPU Usage for Window s Processes

I am looking for a Java solution to finding the CPU usage for a running process in Windows. After looking around the web, there seems to be little information on a solution in Java. Keep in mind, I am ...

How can I do a CPU cache flush in x86 Windows?

I am interested in forcing a CPU cache flush in Windows (for benchmarking reasons, I want to emulate starting with no data in CPU cache), preferably a basic C implementation or Win32 call. Is there a ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Optimal number of threads per core

Let s say I have a 4-core CPU, and I want to run some process in the minimum amount of time. The process is ideally parallelizable, so I can run chunks of it on an infinite number of threads and each ...

Diagnosing runaway CPU in a .Net production application

Does anyone know of a tool that can help me figure out why we are seeing runaway CPU in a managed app? What I am not looking for: Process explorer, it has this awesome feature that lets you see CPU ...

热门标签