NCL 602: Coalescing Instructions

In the previous lesson, we learned that communication between the CPU and other components takes time.

One particularly common source of communication is fetching program lines from the kernel.

Every time the CPU needs another executable line, it must request it.

But there's a useful feature of NCL we can take advantage of:

A single source line can contain multiple instructions.

Coalescing Instructions

Consider this simple sequence:

MOVE $010 0
ADD $010 $010 1
ADD $010 $010 2
ADD $010 $010 3

The CPU must fetch four separate lines to execute these four instructions.

We can instead write:

MOVE $010 0; ADD $010 $010 1; ADD $010 $010 2; ADD $010 $010 3

The semicolon (;) separates instructions on the same source line.

This is called coalescing.

Both versions execute the same four instructions, in the same order, and produce the same result.

But the coalesced version requires only one line fetch instead of four.

Using our illustrative 10 ms communication round trip from 601:

Separate lines Coalesced
Instructions 4 4
Line fetches 4 1
Fetch overhead 40 ms 10 ms

We've eliminated three quarters of the line-fetch overhead without removing a single instruction.

Of course, the instructions themselves still take time to execute. Coalescing doesn't make their calculations faster, nor does it eliminate communication with coprocessors or peripherals.

It simply reduces how often the CPU needs to ask the kernel for another line.

Entry Points

Coalescing several instructions is straightforward when they always execute in sequence.

But what happens when our program needs to jump somewhere?

Consider:

:calculate
MOVE $010 10; MUL $010 $010 2; ADD $010 $010 5

A branch to :calculate begins executing the first instruction on that line.

The CPU then continues through the remaining instructions.

An entry point always begins at the first instruction of an executable line.

We cannot branch directly into the middle of a coalesced line.

For example, suppose we wanted to execute the multiplication without repeating the preceding MOVE.

We would need a separate entry point:

:calculate
MOVE $010 10

:multiply
MUL $010 $010 2; ADD $010 $010 5

Now another part of the program can branch to :multiply without executing the MOVE.

This gives us our first rule for intelligent coalescing:

Instructions that need independent entry points must occupy separate executable lines.

Labels Don't Cost a Fetch

Notice that we've placed the labels on their own lines.

This is perfectly fine.

The kernel skips lines that contain no executable instructions, including:

  • Blank lines.
  • Comment-only lines.
  • Label-only lines.
  • Constant-only lines.

These lines don't introduce additional instruction fetches.

For example:

-- Calculate the final price.

#tax 5

:calculate
MOVE $010 100; ADD $010 $010 #tax

-- Store the result.
MOVE $011 $010

The comments, blank lines, constant declaration, and label don't each require a separate fetch.

Only the two executable lines do.

There's no performance benefit to removing useful whitespace or comments.

Branching Within a Line

A coalesced line doesn't necessarily execute every instruction it contains.

A branch can change where execution continues.

Consider:

MOVE $010 10; BEQ :done $010 10; ADD $010 $010 5

The CPU first executes MOVE.

It then evaluates the conditional branch.

Because $010 equals 10, the branch is taken.

Execution leaves the current line and continues at :done.

The final ADD is skipped.

If the condition had been false, the CPU would have continued with the ADD on the same line.

This gives us a useful pattern:

-- Work that always happens.
MOVE $010 10

-- Exit early if the condition is met.
BEQ :done $010 10; ADD $010 $010 5

:done

Conditional branches can divide a coalesced line into work that always executes and work that executes only when a condition allows it.

Unconditional Exits

Some instructions always transfer execution somewhere else.

For example:

JUMP :done

There is no condition to evaluate. Execution always leaves the current line.

This has an important consequence when coalescing:

MOVE $010 10; JUMP :done; ADD $010 $010 5

The ADD can never execute.

Once the CPU reaches JUMP, it abandons the remainder of the current line.

The same principle applies to other unconditional transfers of execution, such as RET.

Therefore, an unconditional exit should normally be the final instruction on its line.

MOVE $010 10; ADD $010 $010 5; JUMP :done

Everything before the jump executes normally.

Everything after it would be unreachable.

Intelligent Coalescing

We now have enough information to arrange coalesced instructions sensibly.

A useful general pattern is:

  1. Entry point — Where execution may begin.
  2. Unconditional work — Instructions that always execute.
  3. Conditional branch — An opportunity to leave the line early.
  4. Conditional work — Instructions that execute if the branch isn't taken.
  5. Unconditional exit — A final transfer of execution.

These aren't five required parts of every line.

A line can contain any combination of them, or omit them entirely.

They're simply a useful ordering when deciding which instructions belong together.

An Example

Suppose we want to examine a value, make it positive if necessary, and then continue elsewhere.

We might initially write:

:check
MOVE $011 $010
BGE :done $011 0
MUL $011 $011 -1
JUMP :done

:done
MOVE $012 $011

This works, but the first four executable instructions occupy four separate lines.

We can coalesce them:

:check
MOVE $011 $010; BGE :done $011 0; MUL $011 $011 -1; JUMP :done

:done
MOVE $012 $011

Let's follow the execution.

If $010 is positive or zero:

  1. MOVE copies the value to $011.
  2. BGE takes the branch to :done.
  3. The remaining instructions on the line are skipped.

If $010 is negative:

  1. MOVE copies the value to $011.
  2. BGE doesn't take the branch.
  3. MUL negates the value.
  4. JUMP transfers execution to :done.

Both paths produce the same result as the original program.

But the coalesced version uses fewer line fetches.

Notice how the instructions follow our general pattern:

Component Instruction
Entry point :check
Unconditional work MOVE
Conditional branch BGE
Conditional work MUL
Unconditional exit JUMP

The unconditional branch comes last, where it cannot make any useful instructions unreachable.

Don't Coalesce Everything

It's tempting to take this technique to its extreme and put as much of a program as possible onto a single line.

But that quickly becomes difficult to read, modify, and debug.

More importantly, not every instruction belongs in the same execution block.

A useful entry point may require a new line. A conditional branch may divide work into different paths. An unconditional exit makes any later instructions on the same line inaccessible.

Instead, think of each coalesced line as a logical block of execution.

Group instructions that naturally belong together, and keep separate lines where they make the program easier to understand or control.

For example:

-- Initialize the counters.
MOVE $010 0; MOVE $011 10; MOVE $012 1

:process
-- Calculate the next value.
ADD $010 $010 $012; SUB $011 $011 1

-- Decide whether to continue.
BGT :process $011 0

-- Save the result.
MOVE $020 $010

This remains easy to follow.

The comments describe each part of the program, while the instructions within those parts are coalesced where useful.

We're reducing unnecessary fetches, not trying to eliminate every source line.

What's Next?

Coalescing allows us to execute several instructions after fetching only one line.

And we've seen that branches can change execution before the CPU reaches the end of that line.

But there's another interesting possibility.

What happens when a branch sends execution back to instructions that are already on the current line?

In NCL 603: Hot Loops, we'll explore how this can dramatically reduce the cost of repeatedly executing the same code.