The memory hierarchy comprises main memory, which can store a large amount of data, and cache memory, which can access data at high speeds. By accessing data stored in the same array continuously, we can use cache memory without performing frequent data replacement from the main memory, which is crucial for high-speed execution of programs. In this study, we propose a novel code motion algorithm, named global store statement aggregation (GSA), to improve program execution by efficiently utilizing the memory hierarchy. To achieve this, GSA moves each store statement referring to an array immediately before the following store statements accessing the same array. We implemented GSA in a real compiler and evaluated it using sorting programs. The experimental results indicate that our algorithm effectively reduces the number of cache misses in comparison to a previous code motion algorithm.