Delay Slots in Instruction Scheduling
A delay slot is a specific instruction position immediately following a branch or jump instruction that is always executed regardless of whether the branch is taken or not. This architectural feature was common in early RISC processors (like MIPS, SPARC, and some RISC-V implementations) to improve pipeline efficiency.
The Core Concept
In pipelined processors, when a branch instruction is executed, the processor normally:
- Detects the branch
- Resolves the branch condition
- Fetches instructions from the new target address
This creates a pipeline bubble (wasted cycles) while waiting for the branch to resolve. Delay slots solve this by allowing useful work during this otherwise wasted time.
How Delay Slots Work
branch instruction ; Jump to label
delay slot instruction ; This ALWAYS executes before the jump!
target_label:
next instruction at branch target
The instruction in the delay slot is executed even if the branch is taken. This execution happens while the processor is figuring out where to branch to.
Types of Delay Slots
- Branch delay slots: Follow conditional branches
- Jump delay slots: Follow unconditional jumps
- Load delay slots: In some architectures, follow load instructions
Instruction Scheduling Strategies
Compilers use several strategies to effectively utilize delay slots:
-
Independent Instructions: Place an instruction that doesn't depend on the branch outcome or modify conditions the branch tests.
-
From Before the Branch: Move an instruction that appears before the branch into the delay slot if it doesn't affect the branch condition.
// Original code
r1 = r2 + r3
if (r4 == 0) goto label
r5 = r6 + r7
// With delay slot optimization
r1 = r2 + r3
if (r4 == 0) goto label
r5 = r6 + r7 // Moved into delay slot
-
From Target Path: If the branch is likely taken, move an instruction from the target.
// Original
if (likely_condition) goto label
...
label:
r1 = r2 + r3 // First instruction of target
...
// With delay slot
if (likely_condition) goto label
r1 = r2 + r3 // Target's first instruction moved to delay slot
-
From Fall-Through Path: If the branch is likely not taken, use the instruction after the branch.
-
NOP Insertion: When no useful instruction can be placed, insert a no-operation instruction.
Advantages and Disadvantages
Advantages:
- Utilizes otherwise wasted cycles
- Can improve performance in simple pipelines
- Simplifies some aspects of pipeline design
Disadvantages:
- Complicates compiler design and code generation
- Makes debugging more difficult
- Imposes architectural constraints on future processor designs
- Can cause unexpected behavior if not properly understood
Modern Perspective
Most modern processors have moved away from delay slots, preferring:
- More sophisticated branch prediction
- Speculative execution
- Out-of-order execution
However, understanding delay slots remains important for:
- Working with legacy architectures
- Some embedded systems
- Certain specialized processors
Example: MIPS Branch Delay Slot
lw $t0, 0($s0) # Load a value
beq $t0, $zero, L1 # Branch if value is zero
addi $s0, $s0, 4 # DELAY SLOT: Increment pointer regardless of branch
L1: ... # Branch target
The pointer increment happens regardless of whether the branch is taken, making efficient use of what would otherwise be a wasted cycle.