CD Notes
Guide to indirect triples as an intermediate representation that combines the space efficiency of triples with the reordering flexibility of quadruples using an indirection array.
Definition
Indirect triples combine the compactness of triples with the reordering flexibility of quadruples. Instead of executing triples in array order, an additional array of pointers specifies the execution sequence. Rearranging code requires only reordering the pointer array, not moving the triples themselves.
Structure
| # | pointer | # | op | arg1 | arg2 | |
|---|---|---|---|---|---|---|
| 0 | (14) | ─────────────────→ | 14 | * | c | d |
| 1 | (15) | ─────────────────→ | 15 | + | b | (14) |
| 2 | (16) | ─────────────────→ | 16 | = | a | (15) |
Reordering Example
| Original order | Reordered (swap first two): |
| Statement array | Statement array: |
| [0] | triple 14 [0] → triple 15 ← swapped! |
| [1] | triple 15 [1] → triple 14 ← swapped! |
| [2] | triple 16 [2] → triple 16 |
| 14 | (*, c, d) |
| 15 | (+, b, (14)) |
| 16 | (=, a, (15)) |
Complete Example
| 0 | * | a | b |
|---|---|---|---|
| 1 | + | (0) | c |
| 2 | = | x | (1) |
| 4 | - | (3) | d |
| 5 | = | y | (4) |
Comparison of All Three Representations
| + | b | c | t1 |
|---|---|---|---|
| * | t1 | t2 | t3 |
| + | t3 | d | t4 |
| = | t4 | a | |
| 0 | + | b | c |
| 2 | * | (0) | (1) |
| 3 | + | (2) | d |
| 4 | = | a | (3) |
| Representation | Space | Reordering | Optimization |
| Quadruples | Most | Easy | Easy |
| Triples | Least | Hard | Hard |
| Indirect Triples | Medium | Easy | Medium |
When to Use Indirect Triples
Use indirect triples when
1. Memory is a concern (embedded compilers)
2. Some code rearrangement is needed (basic scheduling)
3. CSE by sharing triple references is desired
4. The IR is mostly consumed sequentially
Don't use when
1. Heavy optimization with many passes (use quadruples)
2. SSA-based optimization (use SSA IR directly)
3. Simplicity is paramount (use quadruples)
Implementation
Interview Questions
- Q: What problem do indirect triples solve that regular triples have?
A: Regular triples cannot be reordered without updating all positional references, making optimization difficult. Indirect triples add an indirection layer — reordering only changes the statement pointer array, while triple references remain stable. This gives near-quadruple flexibility with near-triple compactness.
- Q: Is the extra indirection array worth the overhead?
A: For compilers that perform code scheduling or instruction reordering, yes. The pointer array is small (one integer per instruction). Without it, every optimization pass that moves code would need to renumber all references — an error-prone O(n) operation per move. With indirection, moves are O(1).
- Q: How does CSE work with indirect triples?
A: When a duplicate triple is detected (same op, same args), instead of creating a new triple, the statement array simply points to the existing one. Multiple statement entries can reference the same triple, naturally representing shared computations without additional mechanisms.
- Q: Can indirect triples represent DAGs?
A: Yes, naturally. When multiple instructions reference the same triple (shared node), the structure forms a DAG. The triple pool contains unique computations, and the statement/reference structure captures sharing. This makes indirect triples well-suited for DAG-based basic block optimization.
- Q: In modern compilers, are triples or indirect triples still used?
A: Rarely in their pure form. Modern compilers (LLVM, GCC) use SSA-based IRs that combine the benefits of explicit naming (like quadruples) with efficient def-use chains. However, the concepts from triples (positional references, sharing) influence internal data structures used in instruction selection and scheduling passes.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Indirect Triples in Compiler Design.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, intermediate, code, generation, indirect
Related Compiler Design Topics