Mips Branch Delay Slot Instruction

admin  4/6/2022

This ties in with the other delay slot issues such as issue #330 for mips and so should be considered when implementing their fix. I have come across another related issue to the mips branch delay problems. .The original SPARC and MIPS processors each used a single branch delay slot to eliminate single-cycle stalls after branches.The instruction after a conditional branch is always executedin those machines, regardless of whether the branch is taken or not! CSE 141, S2'06 Jeff Brown.

When a program is executing, its instructions are located in main memory. The address of an instruction is the address of the first (the lowest addressed) byte of the four-byte instruction.

Each machine cycle executes one machine instruction. At the top of the machine cycle, the PC (program counter) contains the address of an instruction to fetch from memory. The instruction is fetched into the processor and is prepared for execution.

In the middle of the machine cycle the PC is incremented by four so that it points to the instruction that follows the one just fetched. Then the fetched instruction is executed and the cycle repeats. The machine cycle automatically executes instructions in sequence.

When a jump instruction executes (in the last step of the machine cycle), it puts a new address into the PC. Now the fetch at the top of the next machine cycle fetches the instruction at that new address. Instead of executing the instruction that follows the jump instruction in memory, the processor 'jumps' to an instruction somewhere else in memory.

Mips branch delay slot instructions

However, it takes an extra machine cycle before the change in the PC takes effect. Before the PC changes, the instruction that follows the jump instruction in memory is fetched and executed. After that instruction executes, the next instruction to execute is the one that was jumped to. The instruction that follows a jump instruction in memory is said to be in the branch delay slot.

The reason for this delay is that MIPS is pipelined. Normally, instructions are executed one after another in sequence. In order to gain speed, the processor cleverly fetches several sequential instructions and starts working on them all. When the machine cycle calls for one of these instructions to be executed, much of the work has already been done. These instructions are in an instruction pipe.

This means that the instruction in the branch delay slot has mostly been completed when the jump is executed. Rather than waste this effort, the instruction in the branch delay slot is allowed to finish. Only then is the PC changed by the jump instruction.

Mips Branch Delay Slot Instructions

The instruction that follows a jump instruction in memory (in the branch delay slot) is always executed. Often this is a no-op instruction. After it executes, the next instruction to execute is the one that was the target of the jump instruction.

The SPIM simulator allows you to turn the pipeline feature off, but this is not an option with actual hardware. So, for now, leave this option on.

Mips Instruction Set Branch Delay Slot

  • Restricted Project
jrtc27
sdardis
Reviewers
petarj
zoran.jovanovic
atanasyan
mstojanovic
sdardis
Commits
rGaaac4e0ac6ee: Merging r354672:
rL358920: Merging r354672:
rG6083106b1216: [mips][micromips] fix filling delay slots for PseudoIndirectBranch_MM
rL354672: [mips][micromips] fix filling delay slots for PseudoIndirectBranch_MM

Filling a delay slot in 32bit jump instructions with a 16bit instruction can cause issues. According to documentation such an operation is unpredictable. Multiple test from test-suite that fail on microMIPS show this spot as source of failure. This patch adds opcode Mips::PseudoIndirectBranch_MM alongside Mips::PseudoIndirectBranch and other instructions that are expanded to jr instruction and do not allow a 16bit instruction in their delay slots.

Event Timeline

mbrkusanin created this revision.Feb 21 2019, 7:30 AM
Herald added subscribers: arichardson, sdardis. · View Herald TranscriptFeb 21 2019, 7:30 AM
sdardis accepted this revision.Feb 21 2019, 11:17 AM
Comment Actions

LGTM apart from some minor nits. Please address them before committing.

test/CodeGen/Mips/pseudo-jump-fill.ll
1 ↗(On Diff #187792)

Drop the '--check-prefixes=MIPS'. Also use the update_llc_checks.py script.

2 ↗(On Diff #187792)

Add a quick description of the purpose of this test is, i.e. 'Test that the delay slot filler correctly handles indirect branches for microMIPS.'

5 ↗(On Diff #187792)

These check lines can be removed, just rely on the update_llc_checks.py script.

16 ↗(On Diff #187792)
This revision is now accepted and ready to land.Feb 21 2019, 11:17 AM
sdardis added a subscriber: llvm-commits.Feb 21 2019, 1:47 PM
Comment Actions

Sorry I didn't spot this earlier, but in future please ensure 'llvm-commits' is one of the subscribers when creating a review request for LLVM. If you add it after creating a review request, manually add it and write something in the comments field to trigger Phabricator into sending an email or abandon the review request and re-open it with the relevant -commits list as an initial subscriber.

Posting review requests without the relevant -commits list means that only the subscribers added, subscribers added through Herald rules and initial reviewers will see the request. It is policy that patches are emailed to the relevant list for review[1]. Submitting patches through Phabricator is fine, provided the relevant -commits list is in the subscribers.

Thanks.

[1] http://www.llvm.org/docs/DeveloperPolicy.html#making-and-submitting-a-patch

mbrkusanin updated this revision to Diff 187927.Feb 22 2019, 4:59 AM
Comment Actions
mbrkusanin marked 5 inline comments as done.Feb 22 2019, 5:01 AM
Closed by commit rL354672: [mips][micromips] fix filling delay slots for PseudoIndirectBranch_MM (authored by petarj). · Explain WhyFeb 22 2019, 6:55 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 22 2019, 6:55 AM
Herald added a subscriber: jrtc27. · View Herald Transcript
PathSize
Target/
1 line
CodeGen/
68 lines