One embodiment of the present invention provides a system that generates
code to perform anticipatory prefetching for data references. During
operation, the system receives code to be executed on a computer system.
Next, the system analyzes the code to identify data references to be
prefetched. This analysis can involve: using a two-phase marking process
in which blocks that are certain to execute are considered before other
blocks; and analyzing complex array subscripts. Next, the system inserts
prefetch instructions into the code in advance of the identified data
references. This insertion can involve: dealing with non-constant or
unknown stride values; moving prefetch instructions into preceding basic
blocks; and issuing multiple prefetches for the same data reference.