Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:40:14+00:00 2026-06-03T18:40:14+00:00

I have been reading about branch prediction but the only implementation I find are

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:40:16+00:00Added an answer on June 3, 2026 at 6:40 pm

    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”

    Using such information, it can predict the probable outcome of conditionals, and then generate branch instructions that tend to “predict” correctly by the hardware.

    A particularly interesting set of optimizations done by some compilers is trace scheduling, which determines the sets of paths through code based on probabilities of sequentially encountered branches. By determining the highest probability path, the compiler can do optimizations across that entire path rather that just in within a basic block.

    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.

    One last standard trick: if you can avoid doing a branch, you don’t have to predict it correctly! Many code sequences look something like this:

      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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been reading about the syncroot element but I can't find it in
I have been reading about the continuation passing style programming technique (C# 3.0 implementation).
I have been reading about exception handling on the Apple developer docs , but
I have been reading about dependency properties in several books but all have one
I have been reading about this but I have to do some thing. I
I have been reading about streams, but when I try and deviate from the
I've only just really started android developing and have been reading about services here
I have been reading about the thread-pool pattern and I can't seem to find
i have been reading about proximity security devices through bluetooth, but i am wondering
I have been reading about Domain Events and have seen codes from Udi's implementation(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.