Abstract
Many recent analyses for conventional imperative programs begin by transforming programs into logic programs, capitalising on existing LP analyses and simple LP semantics. We propose using logic programs as an intermediate program representation throughout the compilation process. With restrictions ensuring determinism and single-modedness, a logic program can easily be transformed to machine language or other low-level language, while maintaining the simple semantics that makes it suitable as a language for program analysis and transformation. We present a simple LP language that enforces determinism and single-modedness, and show that it makes a convenient program representation for analysis and transformation.
Introduction: The Limitations of SSA
Most modern compilers, such as LLVM and GCC, rely on Static Single Assignment (SSA) form as their primary intermediate representation (IR). In SSA, every variable is assigned exactly once, which simplifies many optimizations. However, SSA possesses several inherent drawbacks. It is often "path oblivious," making it difficult to track the logical conditions (guards) that lead to specific code blocks. Furthermore, SSA exhibits a "forward bias," complicating backward analyses like determining the root cause of a crash. The use of $\phi$-nodes to handle merging control flow paths is mathematically cumbersome and requires specialized handling during analysis.
Proposed Representation: LP Form
To address these issues, we propose LP Form, a restricted logic programming language based on Horn Clauses. Unlike standard Prolog, which allows for non-determinism and backtracking, LP Form is strictly deterministic and single-moded. This means that for any set of inputs, only one clause will ever succeed, and arguments are strictly partitioned into inputs and outputs.
The transformation from standard three-address code to LP Form replaces jumps and loops with procedure calls and recursion. Instead of $\phi$-nodes, LP Form utilizes standard parameter passing. Control flow branching is handled through logical guards—conditions that must be satisfied for a specific clause to be executed.
Key Insights and Advantages
The shift from a functional view (SSA) to a relational view (LP) offers several advantages for program analysis:
- Path Awareness for Free: Because guards are integrated into the logical definition of clauses, forward analyses naturally incorporate path constraints without additional bookkeeping.
- Uniformity: Loops are represented as recursive calls, and branches as choices between clauses. This allows the compiler to apply the same analytical logic to local blocks and entire programs.
- Native Multiple Outputs: LP Form naturally supports functions returning multiple values, mirroring the capabilities of modern CPU architectures that often produce multiple results (e.g., quotient and remainder) from a single instruction.
Analysis and Transformation
The utility of LP Form is demonstrated through two primary analyses:
- Interval Analysis: LP Form simplifies proving the absence of errors (like division-by-zero) in complex branching code where SSA-based methods often require elaborate extensions like Gated SSA.
- Neededness Analysis: A two-phase interprocedural analysis combining liveness and dead-code elimination. The relational structure of LP Form allows this analysis to scale seamlessly from individual blocks to the whole program.
Conclusion and Future Work
While LP Form can lead to code duplication in its naive representation, this is mitigated by using tree-based structures to factor out common prefixes. Preliminary work on the LPVM compiler demonstrates that this high-level logical representation remains close enough to machine code for efficient code generation. Future efforts will focus on maturing the LPVM implementation and using it as a backend for languages that bridge the gap between declarative and imperative paradigms, eventually translating optimized LP Form into LLVM IR for final hardware-specific tuning.
Summary updated