Sub topic 8
Quadruples, Triples, and Indirect Triples
Assume you know basic C-style expressions, parse trees, and three-address code (TAC). TAC is an intermediate representation in which each instruction has at most one operator and three operands. The compiler uses TAC before generating machine code because it makes optimization and control-flow analysis easier.
A common expression in three representations
Consider:
a = b * -c + b * -cA TAC translation can use separate temporaries for each occurrence:
t1 = minus c
t2 = b * t1
t3 = minus c
t4 = b * t3
t5 = t2 + t4
a = t5A compiler may notice later that both occurrences compute the same value. The representation affects how easily the compiler can refer to and rearrange these instructions.
Quadruples
A quadruple stores four fields:
(operator, argument1, argument2, result)The TAC above becomes:
(minus, c, -, t1)
(*, b, t1, t2)
(minus, c, -, t3)
(*, b, t3, t4)
(+, t2, t4, t5)
(=, t5, -, a)Each result has an explicit name. This makes quadruples convenient for optimization because an instruction can be moved without changing references to its result. The cost is that many temporary names must be maintained.
Triples
A triple omits the result field. The result is identified by the instruction's position:
(operator, argument1, argument2)The same code becomes:
(0) (minus, c, -)
(1) (*, b, (0))
(2) (minus, c, -)
(3) (*, b, (2))
(4) (+, (1), (3))
(5) (=, a, (4))Here, (0) means “the value produced by triple 0.” Triples reduce temporary-name management, but moving triple 0 requires updating every reference to (0). This makes code motion more difficult.
Indirect triples
Indirect triples add a separate pointer list. The triple records remain fixed, while the pointer list determines execution order:
Triple table:
(0) (minus, c, -)
(1) (*, b, (0))
(2) (minus, c, -)
(3) (*, b, (2))
(4) (+, (1), (3))
(5) (=, a, (4))
Pointer list:
[0, 1, 2, 3, 4, 5]If an optimizer wants to execute triple 4 earlier, it can change the pointer list instead of changing references inside the triples:
[0, 1, 2, 3, 5, 4]The change is safe only when dependencies and program semantics permit it.
Comparing the formats
| Representation | Result reference | Main advantage | Main drawback |
|---|---|---|---|
| Quadruple | Temporary name | Easy code motion | More temporary storage |
| Triple | Instruction position | Fewer names | Moving instructions changes references |
| Indirect triple | Pointer to a triple | Easier reordering than triples | Extra pointer structure |
Exercise
In a triple representation, what must be updated if triple 2 is moved to a different position?
Show answer
Every operand reference that identifies the old position of triple 2 must be updated. In an indirect-triple representation, the pointer list can often be changed instead.