Classifying and Formally Verifying Integer Constant Folding

Abstract Constant folding is a well-known optimization of compilers which evaluates constant expressions already at compile time. Constant folding is valid only if the results computed by the compiler are exactly the same as the results which would be computed at run-time by the target machine arithmetic. We classify different arithmetics by deriving a general condition under which a target-machine arithmetic can be replaced by a compiler arithmetic. Furthermore, we consider integer arithmetics as a special case. They can be described by residue class arithmetics. We show that these arithmetics form a lattice. Using the order relation in this lattice, we establish a necessary and sufficient criterion under which constant folding can be done in a residue class arithmetic that is different from the one of the target machine. Concerning formal verification, we have formalized our proofs in the Isabelle/HOL system. As examples, we discuss the Java and C integer arithmetics and show which compiler arithmetics are valid for constant folding. This discussion reveals also potential sources of incorrect behavior of C compilers.

[1]  L. Miles,et al.  2000 , 2000, RDH.

[2]  John McCarthy,et al.  Correctness of a compiler for arithmetic expressions , 1966 .

[3]  Amir Pnueli,et al.  The Code Validation Tool (CVT) , 1998, International Journal on Software Tools for Technology Transfer (STTT).

[4]  B. M. Fulk MATH , 1992 .

[5]  Guy L. Steele,et al.  The Java Language Specification , 1996 .

[6]  Sabine Glesner,et al.  Verified Code Generation for Embedded Systems , 2002, COCV@ETAPS.

[7]  James Gosling The Java Language Specification - Second Edition , 2000 .

[8]  Nachum Dershowitz,et al.  Abstract Effective Models , 2006, Electron. Notes Theor. Comput. Sci..

[9]  George C. Necula,et al.  Translation validation for an optimizing compiler , 2000, PLDI '00.

[10]  Gerd Baron,et al.  Algebraische Strukturen I , 1992 .

[11]  Stanley Burris,et al.  A course in universal algebra , 1981, Graduate texts in mathematics.

[12]  Lawrence Charles Paulson,et al.  Isabelle/HOL: A Proof Assistant for Higher-Order Logic , 2002 .

[13]  Amir Pnueli,et al.  Validation of Optimizing Compilers , 2001 .

[14]  Gerhard Goos,et al.  Verification of Compilers , 1999, Correct System Design.

[15]  George C. Necula,et al.  The design and implementation of a certifying compiler , 1998, PLDI.

[16]  James Gosling,et al.  The Java Language Specification, 3rd Edition , 2005 .