Skip to content

Register Allocation 04.1 phi two address

Mugundan edited this page Nov 27, 2026 · 1 revision

PHI Elimination and Two-Address Lowering

Relevant source files

The following files were used as context for generating this wiki page:

These two passes take SSA MIR out of SSA form. PHI elimination replaces each `PHI` with ordinary copies. Two-address lowering makes sure every instruction whose result must reuse an input register actually does. Both add `COPY`s and leave it to the [coalescer](#coalescer) to remove the unnecessary ones.

Purpose and Scope

This page explains what each pass produces, why, and which target hooks affect it. The SelectionDAG side of PHIs (how they are created) is not covered here.

PHI Elimination

The problem

A PHI picks a value depending on which predecessor block control came from:

bb.1.loop:
  %0:gpr64all = PHI %6, %bb.0, %3, %bb.1

No CPU has such an instruction. The value has to be placed in one agreed register before control reaches bb.1, on every incoming edge.

What the pass does

For each PHI result %d, PHIEliminationImpl::LowerPHINode:

  1. creates a new virtual register %t (the "incoming" register);
  2. at the end of each predecessor (before its terminator), inserts %t = COPY <value from that predecessor>;
  3. replaces the PHI with %d = COPY %t at the top of the block;
  4. updates LiveVariables kill flags (and LiveIntervals, if it already exists).

In the loop from Preparing Code for Allocation, the result is exactly what the two-address dump shows:

bb.0.entry:
  ...
  %15:gpr64common = COPY killed %6      ; incoming copy on edge bb.0 -> bb.1
  %16:gpr32 = COPY killed %7
bb.1.loop:
  %1:gpr32 = COPY killed %16            ; PHI replaced by a copy
  %0:gpr64common = COPY killed %15
  ...
  %15:gpr64common = COPY killed %3      ; incoming copy on edge bb.1 -> bb.1
  %16:gpr32 = COPY %2

%15 and %16 now have two definitions each, one per incoming edge. This is the point where MIR stops being SSA.

Critical edges

If a predecessor has several successors and the PHI's block has several predecessors, the edge between them is critical. A copy placed at the end of the predecessor would also run on its other outgoing edges, extending a live range where it is not needed. PHIEliminationImpl::SplitPHIEdges may split such edges (insert a new block) so copies go only where they are needed. It uses MachineLoopInfo to decide, which is why addOptimizedRegAlloc adds MachineLoopInfo just before PHI elimination ("Edge splitting is smarter with machine loop info").

Target involvement

None directly. PHI elimination uses only target-independent COPYs; turning a COPY into a real move instruction happens much later through TargetInstrInfo::copyPhysReg (see TargetInstrInfo Hooks).

Sources: llvm/lib/CodeGen/PHIElimination.cpp:236, llvm/lib/CodeGen/PHIElimination.cpp:319, llvm/lib/CodeGen/PHIElimination.cpp:367, llvm/lib/CodeGen/PHIElimination.cpp:803, llvm/lib/CodeGen/TargetPassConfig.cpp:1513-1515

Two-Address Lowering

The problem: tied operands

Many instruction sets (x86 most famously, and some instructions on every target) have two-address instructions: the destination register must be the same as one of the sources. add eax, ebx computes eax = eax + ebx; there is no way to write the result elsewhere.

TableGen expresses this as a constraint on the instruction definition:

let Constraints = "$dst = $src1" in
def ADD32rr : ... (outs GR32:$dst), (ins GR32:$src1, GR32:$src2) ...

In SSA MIR, the result and the tied input are still different virtual registers, and the tie is shown with tied-def:

%2:gr32 = ADD32rr %0(tied-def 0), %1

On AArch64 this is rarer: most instructions are three-address. Tied operands appear on instructions such as MOVK (insert 16 bits into an existing register), BFM/BFI (bitfield insert), many NEON accumulate forms (MLA, FMLA) and SVE destructive operations.

What the pass does

The example below is illustrative (x86-style), not llc output. Given

%2 = ADD32rr %0(tied-def 0), %1
... %0 used later ...

the allocator cannot simply give %2 and %0 the same register, because %0 is still needed afterwards. Two-address lowering rewrites it to

%2 = COPY %0
%2 = ADD32rr %2(tied-def 0), %1

After this, the instruction reads and writes the same virtual register, the constraint holds by construction, and the code is no longer SSA (%2 has two definitions). This is the TiedOpsRewritten property printed in MIR dumps.

If %0 is killed at the instruction (not used afterwards), the copy is still inserted, but the coalescer will almost always remove it, because %0 and %2 do not overlap.

Avoiding the copy

Before falling back to a copy, TwoAddressInstructionImpl::tryInstructionTransform tries cheaper options:

Trick Function Target hook it needs
Commute the instruction so the tied operand is a source that dies here tryInstructionCommute, isProfitableToCommute TargetInstrInfo::findCommutedOpIndices, commuteInstructionImpl (instruction marked isCommutable = 1)
Convert to three-address form (e.g. x86 ADD to LEA) convertInstTo3Addr TargetInstrInfo::convertToThreeAddress (instruction marked isConvertibleToThreeAddress = 1)
Reschedule the instruction below the last use of the source, so the source dies at it rescheduleMIBelowKill none

Then processTiedPairs inserts whatever copies are still needed.

What a backend needs to do

  • Declare tied operands with Constraints = "$dst = $src" in the instruction definition. Do not try to enforce the constraint yourself in instruction selection.
  • Mark commutable instructions isCommutable = 1. If the commutable operands are not simply operands 1 and 2, override findCommutedOpIndices.
  • If your target has a three-address equivalent for some two-address instruction, set isConvertibleToThreeAddress = 1 and implement convertToThreeAddress.
  • Use @earlyclobber $dst (in the Constraints string) for results that must not share a register with any input. This is the opposite constraint and is handled by liveness, not by this pass (see Liveness).

Sources: llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:647, llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:800, llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:927, llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:1284, llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:1349, llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:1592, llvm/lib/CodeGen/TwoAddressInstructionPass.cpp:1878, llvm/include/llvm/CodeGen/TargetInstrInfo.h:235, llvm/include/llvm/CodeGen/TargetInstrInfo.h:519, llvm/include/llvm/CodeGen/TargetInstrInfo.h:569

Summary

PHI elimination Two-address lowering
Input SSA MIR with PHIs SSA MIR with tied operands
Output COPYs on incoming edges; registers with several definitions Tied def and use are the same register; COPY before the instruction when needed
Ends SSA? Yes Yes
Target hooks None Commute, convert-to-three-address
Cleaned up by Register coalescer Register coalescer

Start here

Computer Organization & Architecture

Hexagon ISA

Operating Systems

C++ for LLVM Development

LLVM IR

SelectionDAG

Backend Assignments

Target Bring-up

Calling Conventions

Frame Lowering

MC Layer

Register Allocation

Scheduling

Clone this wiki locally