diff --git a/llvm/include/llvm/Cheerp/StoreMerging.h b/llvm/include/llvm/Cheerp/StoreMerging.h index a304a18ac49b..62431d06c87f 100644 --- a/llvm/include/llvm/Cheerp/StoreMerging.h +++ b/llvm/include/llvm/Cheerp/StoreMerging.h @@ -36,6 +36,7 @@ class StoreMerging std::pair findBasePointerAndOffset(const llvm::Value* pointer); std::pair compatibleAndOffset(const llvm::Value* currPtr, const llvm::Value* referencePtr); static void filterAlreadyProcessedStores(std::vector& groupedSamePointer); + static void sortStores(std::vector& groupedSamePointer); bool processBlockOfStores(std::vector& groupedSamePointer); bool processBlockOfStores(const uint32_t dim, std::vector & groupedSamePointer); bool runOnBasicBlock(llvm::BasicBlock& BB); diff --git a/llvm/lib/CheerpUtils/StoreMerging.cpp b/llvm/lib/CheerpUtils/StoreMerging.cpp index ea7809703738..d902c88d6417 100644 --- a/llvm/lib/CheerpUtils/StoreMerging.cpp +++ b/llvm/lib/CheerpUtils/StoreMerging.cpp @@ -80,6 +80,20 @@ bool StoreMerging::runOnBasicBlock(BasicBlock& BB) return Changed; } +void StoreMerging::sortStores(std::vector& groupedSamePointer) +{ + // We find candidates for merging by scanning this vector and looking + // at adjacent value. The order should be: + // 1) By size, since we can only merge stores of the same size + // 2) By offset, since we want adjacent values to be close to each other + std::sort(groupedSamePointer.begin(), groupedSamePointer.end(), + [](const StoreAndOffset& left, const StoreAndOffset& right) -> bool + { + return left.offset < right.offset; + }); + +} + void StoreMerging::filterAlreadyProcessedStores(std::vector& groupedSamePointer) { //Bookkeeping 3: remove the stores with size set to 0 @@ -101,13 +115,7 @@ bool StoreMerging::processBlockOfStores(std::vector& groupedSame if (groupedSamePointer.size() < 2) return false; - //Sort based on the offset - std::sort(groupedSamePointer.begin(), groupedSamePointer.end(), - [](const StoreAndOffset& left, const StoreAndOffset& right) -> bool - { - return left.offset < right.offset; - }); - + sortStores(groupedSamePointer); const uint32_t N = groupedSamePointer.size(); bool overlap = false; for (uint32_t i=0; i+1