Skip to content

[Model] MinimumRegisterSufficiencyForLoops #873

@isPANN

Description

@isPANN

Motivation

REGISTER SUFFICIENCY FOR LOOPS (P295) from Garey & Johnson, A11 PO3. Equivalent to circular arc graph coloring — a fundamental problem in compiler optimization and graph theory.

Associated rules:

Structural connections:

  • Equivalent to circular arc graph coloring: the chromatic number of the intersection graph of arcs on a circle
  • Generalizes interval graph coloring (which is polynomial — chromatic number = clique number for interval graphs)
  • Applications: loop register allocation in compilers, channel assignment in wireless networks

Definition

Name: MinimumRegisterSufficiencyForLoops
Reference: Garey & Johnson, Computers and Intractability, A11 PO3

Mathematical definition:

INSTANCE: Set V of loop variables, loop length N ∈ Z⁺, for each variable v ∈ V a start time s(v) ∈ Z₀⁺ and a duration l(v) ∈ Z⁺.
OBJECTIVE: Find an assignment f: V → Z⁺ that minimizes the number of registers used (i.e., |image(f)|), subject to the constraint that no two variables assigned to the same register have overlapping live ranges (considering wrap-around modulo N).

Two variables u, v conflict iff their circular arcs [s(u), s(u)+l(u)) mod N and [s(v), s(v)+l(v)) mod N overlap.

Variables

  • Count: |V| (one variable per loop variable)
  • Per-variable domain: {1, 2, ..., |V|}
  • Meaning: f(v) = k means loop variable v is stored in register k. Minimize the number of distinct registers used while ensuring no two conflicting variables share a register.

Schema (data type)

Type name: MinimumRegisterSufficiencyForLoops
Variants: none

Field Type Description
loop_length usize The loop length N (circle circumference)
variables Vec<(usize, usize)> For each loop variable: (start_time, duration)

Notes:

  • This is an optimization problem: Value = Min<usize>.
  • Key getter methods: loop_length() (= N), num_variables() (= |V|).

Complexity

  • Decision complexity: NP-complete (Garey, Johnson, Miller, Papadimitriou, 1980; transformation from Permutation Generation). Solvable in polynomial time for any fixed K.
  • Best known exact algorithm: O*(K^|V|) brute force over all register assignments.
  • Concrete complexity expression: "num_variables^num_variables" (for declare_variants!; K ≤ |V|)
  • Relationship to clique number: For circular arc graphs, χ can exceed ω by at most 1 (Tucker, 1975).
  • References:
    • M.R. Garey, D.S. Johnson, G.L. Miller, C.H. Papadimitriou (1980). "The Complexity of Coloring Circular Arcs and Chords." SIAM J. Algebraic and Discrete Methods 1(2), pp. 216-227.
    • A. Tucker (1975). "Coloring a Family of Circular Arcs." SIAM J. Applied Math 29(3), pp. 493-502.

Extra Remark

Full book text:

INSTANCE: Set V of loop variables, a loop length N ∈ Z+, for each variable v ∈ V a start time s(v) ∈ Z0+ and a duration l(v) ∈ Z+, and a positive integer K.
QUESTION: Can the loop variables be safely stored in K registers, i.e., is there an assignment f: V→{1,2,...,K} such that if f(v) = f(u) for some u ≠ v ∈ V, then s(u) ≤ s(v) implies s(u) + l(u) ≤ s(v) and s(v) + l(v)(mod N) ≤ s(u)?
Reference: [Garey, Johnson, Miller, and Papadimitriou, 1978]. Transformation from permutation generation.
Comment: Solvable in polynomial time for any fixed K.

How to solve

  • It can be solved by (existing) bruteforce. (Enumerate all f: V → {1,...,|V|}; find minimum valid coloring.)
  • It can be solved by reducing to integer programming. (Integer variable per loop variable; for each conflicting pair, add f(u) ≠ f(v) constraints; minimize number of distinct values. Companion rule issue to be filed separately.)
  • Other: For fixed K, polynomial-time DP on circular arc intersection graph.

Example Instance

Input:
Loop length N = 6, 3 variables:

Variable Start Duration Arc
v₁ 0 3 [0, 3)
v₂ 2 3 [2, 5)
v₃ 4 3 [4, 1) wrap

Conflict pairs (overlapping arcs): v₁-v₂ (overlap [2,3)), v₂-v₃ (overlap [4,5)), v₃-v₁ (wrap: [4,1) ∩ [0,3) = [0,1)).

All three pairs conflict — this is a 3-clique. The minimum number of registers is 3.

Optimal assignment: f(v₁)=1, f(v₂)=2, f(v₃)=3. All conflicts resolved.
Optimal value: Min(3).

No 2-coloring exists because the conflict graph is K₃ (complete graph on 3 vertices requires 3 colors).

Python validation script
from itertools import product

N = 6
vars = [(0, 3), (2, 3), (4, 3)]  # (start, duration)

def arcs_overlap(s1, l1, s2, l2, N):
    # Check if circular arcs [s1, s1+l1) and [s2, s2+l2) mod N overlap
    for t in range(N):
        in1 = any((s1 + d) % N == t for d in range(l1))
        in2 = any((s2 + d) % N == t for d in range(l2))
        if in1 and in2:
            return True
    return False

# Build conflict pairs
conflicts = []
for i in range(3):
    for j in range(i+1, 3):
        if arcs_overlap(vars[i][0], vars[i][1], vars[j][0], vars[j][1], N):
            conflicts.append((i, j))

assert len(conflicts) == 3  # 3-clique
print(f"Conflicts: {conflicts} (3-clique)")

# Check minimum coloring
for K in range(1, 4):
    count = 0
    for assign in product(range(1, K+1), repeat=3):
        if all(assign[i] != assign[j] for i, j in conflicts):
            count += 1
    print(f"K={K}: {count} valid colorings")
    if count > 0:
        print(f"Minimum colors: {K}")
        break

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoodAn issue passed all checks.modelA model problem to be implemented.

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions