Methods and apparatus for preserving precise exceptions in code reordering
by using control speculation are disclosed. A disclosed system uses a
control speculation module to reorder instructions within an application
program and preserve precise exceptions. Instructions, excepting and
non-excepting, can be reordered by the control speculation module if the
instructions meet certain conditions. When an excepting instruction is
reordered, a check instruction is inserted into the program execution
path and a recovery block is generated. The check instruction determines
if the reordered excepting instruction actually needs to generate an
exception. The recovery block contains instructions to revert the effects
of code reordering. If the check instruction detects the need for an
exception, the recovery block is executed to restore the architectural
state of the processor and the exception is handled.