Binary Translation Using Peephole Superoptimizers

Sorav Bansal, Alex AikenOSDI 2008

Introduction

Binary translation is a critical technology for software portability, allowing applications compiled for one processor architecture (the source) to run on another (the destination). Historically, building high-performance translators like Apple’s Rosetta has required immense manual effort. Engineers must hand-code thousands of translation rules to map complex instruction sets (ISAs), a process that is both error-prone and difficult to optimize.

Researchers at Stanford have proposed a transformative approach: replacing manual rule-writing with peephole superoptimization. By automatically searching for the most efficient instruction sequences on the destination architecture that are semantically equivalent to source sequences, their system achieves high performance with significantly reduced engineering overhead.

The Superoptimization Engine

The core of the system is a superoptimizer that "learns" translation rules. Instead of relying on human-defined heuristics, the system employs a "harvester" to extract common instruction sequences from source programs. An "enumerator" then exhaustively searches for the shortest or most efficient equivalent sequence on the target architecture.

Examples of peephole optimizations discovered by the superoptimizer, where complex sequences are replaced by more efficient equivalents.

To ensure these discovered rules are correct, the system uses formal verification. A SAT solver mathematically proves that the source and destination sequences result in an identical machine state, eliminating the bugs common in manual translation.

Bridging Architectural Gaps

Translating between architectures like PowerPC and x86 involves more than just mapping instructions; it requires managing fundamental differences in register sets and data representation.

Register Mapping via Dynamic Programming

One of the most significant challenges is deciding which destination register should represent which source register at any given point. The authors developed a dynamic programming algorithm to find the optimal "register map" for each code block. This algorithm balances the efficiency of individual instructions against the overhead of moving data between registers when the mapping changes between blocks.

Sample translations from PowerPC to x86, illustrating how source registers are mapped to destination registers or memory locations.

Handling ISA Peculiarities

The system also includes specialized logic for architectural mismatches. For instance, it handles "Endianness" (byte-order swapping) and maps PowerPC’s eight condition registers to the single x86 EFLAGS register. As shown in Figure 6, instructions like bswap are automatically inserted when necessary to maintain data consistency across architectures.

Performance and Results

The superoptimizer-based translator was evaluated using the SPEC CINT2000 benchmark suite and various compute-intensive microbenchmarks. The results demonstrate that automated search can rival or exceed human-tuned systems:

  • Competitive Speed: The translator achieved a median performance of 67% relative to native code on SPEC benchmarks.
  • Outperforming Industry Tools: The system consistently beat the open-source emulator Qemu (by 1.3x to 4x) and was approximately 12% faster than Apple’s Rosetta on the tested workloads.
  • Efficiency in Translation: While the initial "learning phase" for rules is computationally expensive (taking roughly a processor week), the actual translation of new programs is nearly instantaneous once the rule database is populated.

Conclusion and Future Work

By leveraging superoptimization, the researchers have shown that binary translation can be both highly performant and largely automated. While the current prototype focuses on static translation and does not yet handle OS-level system calls, the underlying techniques are applicable to dynamic "just-in-time" (JIT) translators. Future work aims to reduce the code size of translated binaries—which are currently 5–6x larger than the originals—and to streamline the integration of system call emulation.

Summary updated

BibTeX
@inproceedings{bansal2008binary,
  author = {Sorav Bansal and Alex Aiken},
  title = {{Binary Translation Using Peephole Superoptimizers}},
  booktitle = {OSDI},
  year = {2008},
}
Figure