3 ms·
That’s certainly true, a correctly predicted branch costs little more than a nop or unconditional jmp. Code doesn’t have to be branch free, just branch in a pr
by jleahy 4y ago
That’s certainly true, a correctly predicted branch costs little more than a nop or unconditional jmp.
Code doesn’t have to be branch free, just branch in a predictable way.
Statically branch free code is great when you have things that are genuinely unpredictable, think branches driven by data. You might reach for min/max as an example of that, but in most cases these tend to be highly predictable. Control flow in a decompressor is probably a better example (eg. literal vs copy).
- mhh__ 4y agoA correctly predicted branch, in a sense can cost less than a nop in that it does actual work and requires no additional effort from the backend.
- sweetjuly 4y agoI mean, not really. All branches will need to hit the backend in order to verify that they were correctly predicted. RISC-V conditional branches, for example, require doing register register comparisons for verification which means they must be entered into the ROB and issued like any other instruction. This consumes real backend resources and is significantly more than a nop (which can be eliminated by the front end).
- mhh__ 4y agoThe "in a sense" was doing a lot of heavy lifting. They still hit the backend but the idea is that a nop also hits the backend but does no work. Programmers, I think, sometimes still think of a very backend heavy idea of branch prediction (e.g. old static predictors)
- titzer 4y agoBy "hitting the backend" do you mean generating μ-ops? Because nops don't generate μ-ops yet branches definitely do.
- moonchild 4y agoNops do not hit the backend. And conditional branches do eventually make their way to the backend—at some point, you have to actually check that the condition was satisfied. Refer to agner (https://www.agner.org/optimize/instruction_tables.pdf https://www.agner.org/optimize/instruction_tables.pdf): on intel, conditional jumps can be filled by ports 0 and 6.